blob: b5e3888441759a9b69fe688fdeab33b769ae8549 [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);
Robert Greenwalt6f7c6092010-12-02 11:31:00 -0800433
434 loadGlobalProxy();
The Android Open Source Project28527d22009-03-03 19:31:44 -0800435 }
436
Robert Greenwaltec05b3c2009-10-30 14:17:42 -0700437
The Android Open Source Project28527d22009-03-03 19:31:44 -0800438 /**
Robert Greenwalt0659da32009-07-16 17:21:39 -0700439 * Sets the preferred network.
The Android Open Source Project28527d22009-03-03 19:31:44 -0800440 * @param preference the new preference
441 */
Robert Greenwalt6a2db8a2010-09-23 10:05:56 -0700442 public void setNetworkPreference(int preference) {
The Android Open Source Project28527d22009-03-03 19:31:44 -0800443 enforceChangePermission();
Robert Greenwalt6a2db8a2010-09-23 10:05:56 -0700444
445 mHandler.sendMessage(mHandler.obtainMessage(EVENT_SET_NETWORK_PREFERENCE, preference, 0));
The Android Open Source Project28527d22009-03-03 19:31:44 -0800446 }
447
448 public int getNetworkPreference() {
449 enforceAccessPermission();
Robert Greenwalt6a2db8a2010-09-23 10:05:56 -0700450 int preference;
451 synchronized(this) {
452 preference = mNetworkPreference;
453 }
454 return preference;
The Android Open Source Project28527d22009-03-03 19:31:44 -0800455 }
456
Robert Greenwalt6a2db8a2010-09-23 10:05:56 -0700457 private void handleSetNetworkPreference(int preference) {
458 if (ConnectivityManager.isNetworkTypeValid(preference) &&
459 mNetAttributes[preference] != null &&
460 mNetAttributes[preference].isDefault()) {
461 if (mNetworkPreference != preference) {
462 final ContentResolver cr = mContext.getContentResolver();
463 Settings.Secure.putInt(cr, Settings.Secure.NETWORK_PREFERENCE, preference);
464 synchronized(this) {
465 mNetworkPreference = preference;
466 }
467 enforcePreference();
468 }
469 }
The Android Open Source Project28527d22009-03-03 19:31:44 -0800470 }
Robert Greenwalt0659da32009-07-16 17:21:39 -0700471
The Android Open Source Project28527d22009-03-03 19:31:44 -0800472 private int getPersistedNetworkPreference() {
473 final ContentResolver cr = mContext.getContentResolver();
474
475 final int networkPrefSetting = Settings.Secure
476 .getInt(cr, Settings.Secure.NETWORK_PREFERENCE, -1);
477 if (networkPrefSetting != -1) {
478 return networkPrefSetting;
479 }
480
481 return ConnectivityManager.DEFAULT_NETWORK_PREFERENCE;
482 }
Robert Greenwalt0659da32009-07-16 17:21:39 -0700483
The Android Open Source Project28527d22009-03-03 19:31:44 -0800484 /**
Robert Greenwalt0659da32009-07-16 17:21:39 -0700485 * Make the state of network connectivity conform to the preference settings
The Android Open Source Project28527d22009-03-03 19:31:44 -0800486 * In this method, we only tear down a non-preferred network. Establishing
487 * a connection to the preferred network is taken care of when we handle
488 * the disconnect event from the non-preferred network
489 * (see {@link #handleDisconnect(NetworkInfo)}).
490 */
491 private void enforcePreference() {
Robert Greenwalt2034b912009-08-12 16:08:25 -0700492 if (mNetTrackers[mNetworkPreference].getNetworkInfo().isConnected())
The Android Open Source Project28527d22009-03-03 19:31:44 -0800493 return;
494
Robert Greenwalt2034b912009-08-12 16:08:25 -0700495 if (!mNetTrackers[mNetworkPreference].isAvailable())
496 return;
The Android Open Source Project28527d22009-03-03 19:31:44 -0800497
Robert Greenwalt2034b912009-08-12 16:08:25 -0700498 for (int t=0; t <= ConnectivityManager.MAX_RADIO_TYPE; t++) {
Robert Greenwaltec05b3c2009-10-30 14:17:42 -0700499 if (t != mNetworkPreference && mNetTrackers[t] != null &&
Robert Greenwalt2034b912009-08-12 16:08:25 -0700500 mNetTrackers[t].getNetworkInfo().isConnected()) {
Robert Greenwaltf3f045b2009-08-20 15:25:14 -0700501 if (DBG) {
Joe Onoratoc2386bb2010-02-26 18:56:32 -0800502 Slog.d(TAG, "tearing down " +
Robert Greenwaltf3f045b2009-08-20 15:25:14 -0700503 mNetTrackers[t].getNetworkInfo() +
504 " in enforcePreference");
505 }
Robert Greenwalt2034b912009-08-12 16:08:25 -0700506 teardown(mNetTrackers[t]);
The Android Open Source Project28527d22009-03-03 19:31:44 -0800507 }
508 }
509 }
510
511 private boolean teardown(NetworkStateTracker netTracker) {
512 if (netTracker.teardown()) {
513 netTracker.setTeardownRequested(true);
514 return true;
515 } else {
516 return false;
517 }
518 }
519
520 /**
521 * Return NetworkInfo for the active (i.e., connected) network interface.
522 * It is assumed that at most one network is active at a time. If more
523 * than one is active, it is indeterminate which will be returned.
Robert Greenwalt0659da32009-07-16 17:21:39 -0700524 * @return the info for the active network, or {@code null} if none is
525 * active
The Android Open Source Project28527d22009-03-03 19:31:44 -0800526 */
527 public NetworkInfo getActiveNetworkInfo() {
528 enforceAccessPermission();
Robert Greenwalt2034b912009-08-12 16:08:25 -0700529 for (int type=0; type <= ConnectivityManager.MAX_NETWORK_TYPE; type++) {
Robert Greenwaltec05b3c2009-10-30 14:17:42 -0700530 if (mNetAttributes[type] == null || !mNetAttributes[type].isDefault()) {
Robert Greenwalt2034b912009-08-12 16:08:25 -0700531 continue;
532 }
533 NetworkStateTracker t = mNetTrackers[type];
The Android Open Source Project28527d22009-03-03 19:31:44 -0800534 NetworkInfo info = t.getNetworkInfo();
535 if (info.isConnected()) {
Joe Onoratoc2386bb2010-02-26 18:56:32 -0800536 if (DBG && type != mActiveDefaultNetwork) Slog.e(TAG,
Robert Greenwalt2034b912009-08-12 16:08:25 -0700537 "connected default network is not " +
538 "mActiveDefaultNetwork!");
The Android Open Source Project28527d22009-03-03 19:31:44 -0800539 return info;
540 }
541 }
542 return null;
543 }
544
545 public NetworkInfo getNetworkInfo(int networkType) {
546 enforceAccessPermission();
547 if (ConnectivityManager.isNetworkTypeValid(networkType)) {
548 NetworkStateTracker t = mNetTrackers[networkType];
549 if (t != null)
550 return t.getNetworkInfo();
551 }
552 return null;
553 }
554
555 public NetworkInfo[] getAllNetworkInfo() {
556 enforceAccessPermission();
Robert Greenwaltec05b3c2009-10-30 14:17:42 -0700557 NetworkInfo[] result = new NetworkInfo[mNetworksDefined];
The Android Open Source Project28527d22009-03-03 19:31:44 -0800558 int i = 0;
559 for (NetworkStateTracker t : mNetTrackers) {
Robert Greenwaltec05b3c2009-10-30 14:17:42 -0700560 if(t != null) result[i++] = t.getNetworkInfo();
The Android Open Source Project28527d22009-03-03 19:31:44 -0800561 }
562 return result;
563 }
564
Robert Greenwalt9f0ee4f2010-09-14 09:18:02 -0700565 /**
566 * Return LinkProperties for the active (i.e., connected) default
567 * network interface. It is assumed that at most one default network
568 * is active at a time. If more than one is active, it is indeterminate
569 * which will be returned.
570 * @return the ip properties for the active network, or {@code null} if
571 * none is active
572 */
573 public LinkProperties getActiveLinkProperties() {
574 enforceAccessPermission();
575 for (int type=0; type <= ConnectivityManager.MAX_NETWORK_TYPE; type++) {
576 if (mNetAttributes[type] == null || !mNetAttributes[type].isDefault()) {
577 continue;
578 }
579 NetworkStateTracker t = mNetTrackers[type];
580 NetworkInfo info = t.getNetworkInfo();
581 if (info.isConnected()) {
582 return t.getLinkProperties();
583 }
584 }
585 return null;
586 }
587
588 public LinkProperties getLinkProperties(int networkType) {
589 enforceAccessPermission();
590 if (ConnectivityManager.isNetworkTypeValid(networkType)) {
591 NetworkStateTracker t = mNetTrackers[networkType];
592 if (t != null) return t.getLinkProperties();
593 }
594 return null;
595 }
596
The Android Open Source Project28527d22009-03-03 19:31:44 -0800597 public boolean setRadios(boolean turnOn) {
598 boolean result = true;
599 enforceChangePermission();
600 for (NetworkStateTracker t : mNetTrackers) {
Robert Greenwaltec05b3c2009-10-30 14:17:42 -0700601 if (t != null) result = t.setRadio(turnOn) && result;
The Android Open Source Project28527d22009-03-03 19:31:44 -0800602 }
603 return result;
604 }
605
606 public boolean setRadio(int netType, boolean turnOn) {
607 enforceChangePermission();
608 if (!ConnectivityManager.isNetworkTypeValid(netType)) {
609 return false;
610 }
611 NetworkStateTracker tracker = mNetTrackers[netType];
612 return tracker != null && tracker.setRadio(turnOn);
613 }
614
Robert Greenwaltaffc3a12009-09-27 17:27:04 -0700615 /**
616 * Used to notice when the calling process dies so we can self-expire
617 *
618 * Also used to know if the process has cleaned up after itself when
619 * our auto-expire timer goes off. The timer has a link to an object.
620 *
621 */
Robert Greenwalt2034b912009-08-12 16:08:25 -0700622 private class FeatureUser implements IBinder.DeathRecipient {
623 int mNetworkType;
624 String mFeature;
625 IBinder mBinder;
626 int mPid;
627 int mUid;
Robert Greenwalt3eeb6032009-12-21 18:24:07 -0800628 long mCreateTime;
Robert Greenwalt2034b912009-08-12 16:08:25 -0700629
630 FeatureUser(int type, String feature, IBinder binder) {
631 super();
632 mNetworkType = type;
633 mFeature = feature;
634 mBinder = binder;
635 mPid = getCallingPid();
636 mUid = getCallingUid();
Robert Greenwalt3eeb6032009-12-21 18:24:07 -0800637 mCreateTime = System.currentTimeMillis();
Robert Greenwaltaffc3a12009-09-27 17:27:04 -0700638
Robert Greenwalt2034b912009-08-12 16:08:25 -0700639 try {
640 mBinder.linkToDeath(this, 0);
641 } catch (RemoteException e) {
642 binderDied();
643 }
644 }
645
646 void unlinkDeathRecipient() {
647 mBinder.unlinkToDeath(this, 0);
648 }
649
650 public void binderDied() {
Joe Onoratoc2386bb2010-02-26 18:56:32 -0800651 Slog.d(TAG, "ConnectivityService FeatureUser binderDied(" +
Robert Greenwalt3eeb6032009-12-21 18:24:07 -0800652 mNetworkType + ", " + mFeature + ", " + mBinder + "), created " +
653 (System.currentTimeMillis() - mCreateTime) + " mSec ago");
Robert Greenwaltaffc3a12009-09-27 17:27:04 -0700654 stopUsingNetworkFeature(this, false);
Robert Greenwalt2034b912009-08-12 16:08:25 -0700655 }
656
Robert Greenwaltaffc3a12009-09-27 17:27:04 -0700657 public void expire() {
Joe Onoratoc2386bb2010-02-26 18:56:32 -0800658 Slog.d(TAG, "ConnectivityService FeatureUser expire(" +
Robert Greenwalt3eeb6032009-12-21 18:24:07 -0800659 mNetworkType + ", " + mFeature + ", " + mBinder +"), created " +
660 (System.currentTimeMillis() - mCreateTime) + " mSec ago");
Robert Greenwaltaffc3a12009-09-27 17:27:04 -0700661 stopUsingNetworkFeature(this, false);
662 }
Robert Greenwalt3eeb6032009-12-21 18:24:07 -0800663
664 public String toString() {
665 return "FeatureUser("+mNetworkType+","+mFeature+","+mPid+","+mUid+"), created " +
666 (System.currentTimeMillis() - mCreateTime) + " mSec ago";
667 }
Robert Greenwalt2034b912009-08-12 16:08:25 -0700668 }
669
Robert Greenwaltaffc3a12009-09-27 17:27:04 -0700670 // javadoc from interface
Robert Greenwalt2034b912009-08-12 16:08:25 -0700671 public int startUsingNetworkFeature(int networkType, String feature,
672 IBinder binder) {
673 if (DBG) {
Joe Onoratoc2386bb2010-02-26 18:56:32 -0800674 Slog.d(TAG, "startUsingNetworkFeature for net " + networkType +
Robert Greenwalt2034b912009-08-12 16:08:25 -0700675 ": " + feature);
676 }
The Android Open Source Project28527d22009-03-03 19:31:44 -0800677 enforceChangePermission();
Robert Greenwaltec05b3c2009-10-30 14:17:42 -0700678 if (!ConnectivityManager.isNetworkTypeValid(networkType) ||
679 mNetAttributes[networkType] == null) {
Robert Greenwalt2034b912009-08-12 16:08:25 -0700680 return Phone.APN_REQUEST_FAILED;
The Android Open Source Project28527d22009-03-03 19:31:44 -0800681 }
Robert Greenwalt2034b912009-08-12 16:08:25 -0700682
Robert Greenwaltaffc3a12009-09-27 17:27:04 -0700683 FeatureUser f = new FeatureUser(networkType, feature, binder);
Robert Greenwalt2034b912009-08-12 16:08:25 -0700684
685 // TODO - move this into the MobileDataStateTracker
686 int usedNetworkType = networkType;
687 if(networkType == ConnectivityManager.TYPE_MOBILE) {
Robert Greenwalt1b21f6c2010-02-23 18:58:05 -0800688 if (!getMobileDataEnabled()) {
Joe Onoratoc2386bb2010-02-26 18:56:32 -0800689 if (DBG) Slog.d(TAG, "requested special network with data disabled - rejected");
Robert Greenwalt1b21f6c2010-02-23 18:58:05 -0800690 return Phone.APN_TYPE_NOT_AVAILABLE;
691 }
Robert Greenwalt2034b912009-08-12 16:08:25 -0700692 if (TextUtils.equals(feature, Phone.FEATURE_ENABLE_MMS)) {
693 usedNetworkType = ConnectivityManager.TYPE_MOBILE_MMS;
694 } else if (TextUtils.equals(feature, Phone.FEATURE_ENABLE_SUPL)) {
695 usedNetworkType = ConnectivityManager.TYPE_MOBILE_SUPL;
696 } else if (TextUtils.equals(feature, Phone.FEATURE_ENABLE_DUN)) {
697 usedNetworkType = ConnectivityManager.TYPE_MOBILE_DUN;
698 } else if (TextUtils.equals(feature, Phone.FEATURE_ENABLE_HIPRI)) {
699 usedNetworkType = ConnectivityManager.TYPE_MOBILE_HIPRI;
700 }
701 }
702 NetworkStateTracker network = mNetTrackers[usedNetworkType];
703 if (network != null) {
704 if (usedNetworkType != networkType) {
705 Integer currentPid = new Integer(getCallingPid());
706
707 NetworkStateTracker radio = mNetTrackers[networkType];
708 NetworkInfo ni = network.getNetworkInfo();
709
710 if (ni.isAvailable() == false) {
Joe Onoratoc2386bb2010-02-26 18:56:32 -0800711 if (DBG) Slog.d(TAG, "special network not available");
Robert Greenwalt2034b912009-08-12 16:08:25 -0700712 return Phone.APN_TYPE_NOT_AVAILABLE;
713 }
714
Robert Greenwaltaffc3a12009-09-27 17:27:04 -0700715 synchronized(this) {
716 mFeatureUsers.add(f);
717 if (!mNetRequestersPids[usedNetworkType].contains(currentPid)) {
718 // this gets used for per-pid dns when connected
719 mNetRequestersPids[usedNetworkType].add(currentPid);
720 }
Robert Greenwalt2034b912009-08-12 16:08:25 -0700721 }
Robert Greenwalt6a2db8a2010-09-23 10:05:56 -0700722 mHandler.sendMessageDelayed(mHandler.obtainMessage(EVENT_RESTORE_DEFAULT_NETWORK,
Robert Greenwaltaffc3a12009-09-27 17:27:04 -0700723 f), getRestoreDefaultNetworkDelay());
724
Robert Greenwalt2034b912009-08-12 16:08:25 -0700725
Robert Greenwalta52c75a2009-08-19 20:19:33 -0700726 if ((ni.isConnectedOrConnecting() == true) &&
727 !network.isTeardownRequested()) {
Robert Greenwalt2034b912009-08-12 16:08:25 -0700728 if (ni.isConnected() == true) {
729 // add the pid-specific dns
Robert Greenwalt3afbead2010-07-23 15:46:26 -0700730 handleDnsConfigurationChange(networkType);
Joe Onoratoc2386bb2010-02-26 18:56:32 -0800731 if (DBG) Slog.d(TAG, "special network already active");
Robert Greenwalt2034b912009-08-12 16:08:25 -0700732 return Phone.APN_ALREADY_ACTIVE;
733 }
Joe Onoratoc2386bb2010-02-26 18:56:32 -0800734 if (DBG) Slog.d(TAG, "special network already connecting");
Robert Greenwalt2034b912009-08-12 16:08:25 -0700735 return Phone.APN_REQUEST_STARTED;
736 }
737
738 // check if the radio in play can make another contact
739 // assume if cannot for now
740
Joe Onoratoc2386bb2010-02-26 18:56:32 -0800741 if (DBG) Slog.d(TAG, "reconnecting to special network");
Robert Greenwalt2034b912009-08-12 16:08:25 -0700742 network.reconnect();
743 return Phone.APN_REQUEST_STARTED;
744 } else {
Robert Greenwaltd391e892010-05-18 10:52:51 -0700745 return -1;
Robert Greenwalt2034b912009-08-12 16:08:25 -0700746 }
747 }
748 return Phone.APN_TYPE_NOT_AVAILABLE;
The Android Open Source Project28527d22009-03-03 19:31:44 -0800749 }
750
Robert Greenwaltaffc3a12009-09-27 17:27:04 -0700751 // javadoc from interface
The Android Open Source Project28527d22009-03-03 19:31:44 -0800752 public int stopUsingNetworkFeature(int networkType, String feature) {
Robert Greenwalt28f43012009-10-06 17:52:40 -0700753 enforceChangePermission();
754
Robert Greenwaltaffc3a12009-09-27 17:27:04 -0700755 int pid = getCallingPid();
756 int uid = getCallingUid();
757
758 FeatureUser u = null;
759 boolean found = false;
760
761 synchronized(this) {
762 for (int i = 0; i < mFeatureUsers.size() ; i++) {
763 u = (FeatureUser)mFeatureUsers.get(i);
764 if (uid == u.mUid && pid == u.mPid &&
765 networkType == u.mNetworkType &&
766 TextUtils.equals(feature, u.mFeature)) {
767 found = true;
768 break;
769 }
770 }
771 }
772 if (found && u != null) {
773 // stop regardless of how many other time this proc had called start
774 return stopUsingNetworkFeature(u, true);
775 } else {
776 // none found!
Joe Onoratoc2386bb2010-02-26 18:56:32 -0800777 if (DBG) Slog.d(TAG, "ignoring stopUsingNetworkFeature - not a live request");
Robert Greenwaltaffc3a12009-09-27 17:27:04 -0700778 return 1;
779 }
Robert Greenwalt2034b912009-08-12 16:08:25 -0700780 }
781
Robert Greenwaltaffc3a12009-09-27 17:27:04 -0700782 private int stopUsingNetworkFeature(FeatureUser u, boolean ignoreDups) {
783 int networkType = u.mNetworkType;
784 String feature = u.mFeature;
785 int pid = u.mPid;
786 int uid = u.mUid;
787
788 NetworkStateTracker tracker = null;
789 boolean callTeardown = false; // used to carry our decision outside of sync block
790
Robert Greenwalt2034b912009-08-12 16:08:25 -0700791 if (DBG) {
Joe Onoratoc2386bb2010-02-26 18:56:32 -0800792 Slog.d(TAG, "stopUsingNetworkFeature for net " + networkType +
Robert Greenwalt2034b912009-08-12 16:08:25 -0700793 ": " + feature);
794 }
Robert Greenwalt28f43012009-10-06 17:52:40 -0700795
The Android Open Source Project28527d22009-03-03 19:31:44 -0800796 if (!ConnectivityManager.isNetworkTypeValid(networkType)) {
797 return -1;
798 }
Robert Greenwalt2034b912009-08-12 16:08:25 -0700799
Robert Greenwaltaffc3a12009-09-27 17:27:04 -0700800 // need to link the mFeatureUsers list with the mNetRequestersPids state in this
801 // sync block
802 synchronized(this) {
803 // check if this process still has an outstanding start request
804 if (!mFeatureUsers.contains(u)) {
Robert Greenwalt46ceefa2010-03-10 16:10:43 -0800805 if (DBG) Slog.d(TAG, "ignoring - this process has no outstanding requests");
Robert Greenwalt2034b912009-08-12 16:08:25 -0700806 return 1;
807 }
Robert Greenwaltaffc3a12009-09-27 17:27:04 -0700808 u.unlinkDeathRecipient();
809 mFeatureUsers.remove(mFeatureUsers.indexOf(u));
810 // If we care about duplicate requests, check for that here.
811 //
812 // This is done to support the extension of a request - the app
813 // can request we start the network feature again and renew the
814 // auto-shutoff delay. Normal "stop" calls from the app though
815 // do not pay attention to duplicate requests - in effect the
816 // API does not refcount and a single stop will counter multiple starts.
817 if (ignoreDups == false) {
818 for (int i = 0; i < mFeatureUsers.size() ; i++) {
819 FeatureUser x = (FeatureUser)mFeatureUsers.get(i);
820 if (x.mUid == u.mUid && x.mPid == u.mPid &&
821 x.mNetworkType == u.mNetworkType &&
822 TextUtils.equals(x.mFeature, u.mFeature)) {
Joe Onoratoc2386bb2010-02-26 18:56:32 -0800823 if (DBG) Slog.d(TAG, "ignoring stopUsingNetworkFeature as dup is found");
Robert Greenwaltaffc3a12009-09-27 17:27:04 -0700824 return 1;
825 }
826 }
Robert Greenwalt2034b912009-08-12 16:08:25 -0700827 }
Robert Greenwaltaffc3a12009-09-27 17:27:04 -0700828
829 // TODO - move to MobileDataStateTracker
830 int usedNetworkType = networkType;
831 if (networkType == ConnectivityManager.TYPE_MOBILE) {
832 if (TextUtils.equals(feature, Phone.FEATURE_ENABLE_MMS)) {
833 usedNetworkType = ConnectivityManager.TYPE_MOBILE_MMS;
834 } else if (TextUtils.equals(feature, Phone.FEATURE_ENABLE_SUPL)) {
835 usedNetworkType = ConnectivityManager.TYPE_MOBILE_SUPL;
836 } else if (TextUtils.equals(feature, Phone.FEATURE_ENABLE_DUN)) {
837 usedNetworkType = ConnectivityManager.TYPE_MOBILE_DUN;
838 } else if (TextUtils.equals(feature, Phone.FEATURE_ENABLE_HIPRI)) {
839 usedNetworkType = ConnectivityManager.TYPE_MOBILE_HIPRI;
840 }
841 }
842 tracker = mNetTrackers[usedNetworkType];
Robert Greenwaltec05b3c2009-10-30 14:17:42 -0700843 if (tracker == null) {
Robert Greenwalt46ceefa2010-03-10 16:10:43 -0800844 if (DBG) Slog.d(TAG, "ignoring - no known tracker for net type " + usedNetworkType);
Robert Greenwaltec05b3c2009-10-30 14:17:42 -0700845 return -1;
846 }
847 if (usedNetworkType != networkType) {
Robert Greenwaltaffc3a12009-09-27 17:27:04 -0700848 Integer currentPid = new Integer(pid);
Robert Greenwaltaffc3a12009-09-27 17:27:04 -0700849 mNetRequestersPids[usedNetworkType].remove(currentPid);
Robert Greenwalt0ca68a02009-12-17 14:54:59 -0800850 reassessPidDns(pid, true);
Robert Greenwaltaffc3a12009-09-27 17:27:04 -0700851 if (mNetRequestersPids[usedNetworkType].size() != 0) {
Joe Onoratoc2386bb2010-02-26 18:56:32 -0800852 if (DBG) Slog.d(TAG, "not tearing down special network - " +
Robert Greenwaltaffc3a12009-09-27 17:27:04 -0700853 "others still using it");
854 return 1;
855 }
856 callTeardown = true;
857 }
858 }
Robert Greenwalt46ceefa2010-03-10 16:10:43 -0800859 if (DBG) Slog.d(TAG, "Doing network teardown");
Robert Greenwaltaffc3a12009-09-27 17:27:04 -0700860 if (callTeardown) {
861 tracker.teardown();
Robert Greenwalt2034b912009-08-12 16:08:25 -0700862 return 1;
863 } else {
Robert Greenwaltd391e892010-05-18 10:52:51 -0700864 return -1;
Robert Greenwalt2034b912009-08-12 16:08:25 -0700865 }
The Android Open Source Project28527d22009-03-03 19:31:44 -0800866 }
867
868 /**
Robert Greenwalt7fe44cb2010-08-27 09:24:29 -0700869 * @deprecated use requestRouteToHostAddress instead
870 *
The Android Open Source Project28527d22009-03-03 19:31:44 -0800871 * Ensure that a network route exists to deliver traffic to the specified
872 * host via the specified network interface.
Robert Greenwalt0659da32009-07-16 17:21:39 -0700873 * @param networkType the type of the network over which traffic to the
874 * specified host is to be routed
875 * @param hostAddress the IP address of the host to which the route is
876 * desired
The Android Open Source Project28527d22009-03-03 19:31:44 -0800877 * @return {@code true} on success, {@code false} on failure
878 */
879 public boolean requestRouteToHost(int networkType, int hostAddress) {
Robert Greenwalt7fe44cb2010-08-27 09:24:29 -0700880 InetAddress inetAddress = NetworkUtils.intToInetAddress(hostAddress);
881
882 if (inetAddress == null) {
883 return false;
884 }
885
886 return requestRouteToHostAddress(networkType, inetAddress.getAddress());
887 }
888
889 /**
890 * Ensure that a network route exists to deliver traffic to the specified
891 * host via the specified network interface.
892 * @param networkType the type of the network over which traffic to the
893 * specified host is to be routed
894 * @param hostAddress the IP address of the host to which the route is
895 * desired
896 * @return {@code true} on success, {@code false} on failure
897 */
898 public boolean requestRouteToHostAddress(int networkType, byte[] hostAddress) {
The Android Open Source Project28527d22009-03-03 19:31:44 -0800899 enforceChangePermission();
900 if (!ConnectivityManager.isNetworkTypeValid(networkType)) {
901 return false;
902 }
903 NetworkStateTracker tracker = mNetTrackers[networkType];
Robert Greenwalt4666ed02009-09-10 15:06:20 -0700904
Robert Greenwaltec05b3c2009-10-30 14:17:42 -0700905 if (tracker == null || !tracker.getNetworkInfo().isConnected() ||
906 tracker.isTeardownRequested()) {
Robert Greenwalt4666ed02009-09-10 15:06:20 -0700907 if (DBG) {
Robert Greenwalt7fe44cb2010-08-27 09:24:29 -0700908 Slog.d(TAG, "requestRouteToHostAddress on down network " +
909 "(" + networkType + ") - dropped");
Robert Greenwalt4666ed02009-09-10 15:06:20 -0700910 }
911 return false;
The Android Open Source Project28527d22009-03-03 19:31:44 -0800912 }
Robert Greenwalta7dfbd32010-06-15 15:43:39 -0700913 try {
Robert Greenwalt7fe44cb2010-08-27 09:24:29 -0700914 InetAddress addr = InetAddress.getByAddress(hostAddress);
Robert Greenwalta7dfbd32010-06-15 15:43:39 -0700915 return addHostRoute(tracker, addr);
916 } catch (UnknownHostException e) {}
917 return false;
Irfan Sheriff7f132d92010-06-09 15:39:36 -0700918 }
919
920 /**
921 * Ensure that a network route exists to deliver traffic to the specified
922 * host via the mobile data network.
923 * @param hostAddress the IP address of the host to which the route is desired,
924 * in network byte order.
Robert Greenwalta7dfbd32010-06-15 15:43:39 -0700925 * TODO - deprecate
Irfan Sheriff7f132d92010-06-09 15:39:36 -0700926 * @return {@code true} on success, {@code false} on failure
927 */
Robert Greenwalta7dfbd32010-06-15 15:43:39 -0700928 private boolean addHostRoute(NetworkStateTracker nt, InetAddress hostAddress) {
Irfan Sheriff7f132d92010-06-09 15:39:36 -0700929 if (nt.getNetworkInfo().getType() == ConnectivityManager.TYPE_WIFI) {
930 return false;
931 }
932
Robert Greenwalt1f1bcfe2010-08-30 10:56:47 -0700933 LinkProperties p = nt.getLinkProperties();
Robert Greenwalta7dfbd32010-06-15 15:43:39 -0700934 if (p == null) return false;
935 String interfaceName = p.getInterfaceName();
Irfan Sheriff7f132d92010-06-09 15:39:36 -0700936
937 if (DBG) {
Robert Greenwalta7dfbd32010-06-15 15:43:39 -0700938 Slog.d(TAG, "Requested host route to " + hostAddress + "(" + interfaceName + ")");
Irfan Sheriff7f132d92010-06-09 15:39:36 -0700939 }
Robert Greenwalta7dfbd32010-06-15 15:43:39 -0700940 if (interfaceName != null) {
Robert Greenwalt7fe44cb2010-08-27 09:24:29 -0700941 return NetworkUtils.addHostRoute(interfaceName, hostAddress, null);
Irfan Sheriff7f132d92010-06-09 15:39:36 -0700942 } else {
Robert Greenwalta7dfbd32010-06-15 15:43:39 -0700943 if (DBG) Slog.e(TAG, "addHostRoute failed due to null interface name");
Irfan Sheriff7f132d92010-06-09 15:39:36 -0700944 return false;
945 }
The Android Open Source Project28527d22009-03-03 19:31:44 -0800946 }
947
948 /**
949 * @see ConnectivityManager#getBackgroundDataSetting()
950 */
951 public boolean getBackgroundDataSetting() {
952 return Settings.Secure.getInt(mContext.getContentResolver(),
953 Settings.Secure.BACKGROUND_DATA, 1) == 1;
954 }
Robert Greenwalt0659da32009-07-16 17:21:39 -0700955
The Android Open Source Project28527d22009-03-03 19:31:44 -0800956 /**
957 * @see ConnectivityManager#setBackgroundDataSetting(boolean)
958 */
959 public void setBackgroundDataSetting(boolean allowBackgroundDataUsage) {
960 mContext.enforceCallingOrSelfPermission(
961 android.Manifest.permission.CHANGE_BACKGROUND_DATA_SETTING,
962 "ConnectivityService");
Robert Greenwalt0659da32009-07-16 17:21:39 -0700963
Robert Greenwalt6a2db8a2010-09-23 10:05:56 -0700964 mHandler.sendMessage(mHandler.obtainMessage(EVENT_SET_BACKGROUND_DATA,
965 (allowBackgroundDataUsage ? ENABLED : DISABLED), 0));
966 }
The Android Open Source Project28527d22009-03-03 19:31:44 -0800967
Robert Greenwalt6a2db8a2010-09-23 10:05:56 -0700968 private void handleSetBackgroundData(boolean enabled) {
969 if (enabled != getBackgroundDataSetting()) {
970 Settings.Secure.putInt(mContext.getContentResolver(),
971 Settings.Secure.BACKGROUND_DATA, enabled ? 1 : 0);
972 Intent broadcast = new Intent(
973 ConnectivityManager.ACTION_BACKGROUND_DATA_SETTING_CHANGED);
974 mContext.sendBroadcast(broadcast);
975 }
Robert Greenwalt0659da32009-07-16 17:21:39 -0700976 }
977
Robert Greenwalt1b21f6c2010-02-23 18:58:05 -0800978 /**
979 * @see ConnectivityManager#getMobileDataEnabled()
980 */
981 public boolean getMobileDataEnabled() {
982 enforceAccessPermission();
983 boolean retVal = Settings.Secure.getInt(mContext.getContentResolver(),
984 Settings.Secure.MOBILE_DATA, 1) == 1;
Joe Onoratoc2386bb2010-02-26 18:56:32 -0800985 if (DBG) Slog.d(TAG, "getMobileDataEnabled returning " + retVal);
Robert Greenwalt1b21f6c2010-02-23 18:58:05 -0800986 return retVal;
987 }
988
989 /**
990 * @see ConnectivityManager#setMobileDataEnabled(boolean)
991 */
Robert Greenwalt6a2db8a2010-09-23 10:05:56 -0700992 public void setMobileDataEnabled(boolean enabled) {
Robert Greenwalt1b21f6c2010-02-23 18:58:05 -0800993 enforceChangePermission();
Joe Onoratoc2386bb2010-02-26 18:56:32 -0800994 if (DBG) Slog.d(TAG, "setMobileDataEnabled(" + enabled + ")");
Robert Greenwalt1b21f6c2010-02-23 18:58:05 -0800995
Robert Greenwalt6a2db8a2010-09-23 10:05:56 -0700996 mHandler.sendMessage(mHandler.obtainMessage(EVENT_SET_MOBILE_DATA,
997 (enabled ? ENABLED : DISABLED), 0));
998 }
999
1000 private void handleSetMobileData(boolean enabled) {
Robert Greenwalt1b21f6c2010-02-23 18:58:05 -08001001 if (getMobileDataEnabled() == enabled) return;
1002
1003 Settings.Secure.putInt(mContext.getContentResolver(),
1004 Settings.Secure.MOBILE_DATA, enabled ? 1 : 0);
1005
1006 if (enabled) {
1007 if (mNetTrackers[ConnectivityManager.TYPE_MOBILE] != null) {
Robert Greenwalt6a2db8a2010-09-23 10:05:56 -07001008 if (DBG) {
1009 Slog.d(TAG, "starting up " + mNetTrackers[ConnectivityManager.TYPE_MOBILE]);
1010 }
Robert Greenwalt1b21f6c2010-02-23 18:58:05 -08001011 mNetTrackers[ConnectivityManager.TYPE_MOBILE].reconnect();
1012 }
1013 } else {
1014 for (NetworkStateTracker nt : mNetTrackers) {
1015 if (nt == null) continue;
1016 int netType = nt.getNetworkInfo().getType();
1017 if (mNetAttributes[netType].mRadio == ConnectivityManager.TYPE_MOBILE) {
Joe Onoratoc2386bb2010-02-26 18:56:32 -08001018 if (DBG) Slog.d(TAG, "tearing down " + nt);
Robert Greenwalt1b21f6c2010-02-23 18:58:05 -08001019 nt.teardown();
1020 }
1021 }
1022 }
1023 }
1024
The Android Open Source Project28527d22009-03-03 19:31:44 -08001025 private int getNumConnectedNetworks() {
1026 int numConnectedNets = 0;
1027
1028 for (NetworkStateTracker nt : mNetTrackers) {
Robert Greenwaltec05b3c2009-10-30 14:17:42 -07001029 if (nt != null && nt.getNetworkInfo().isConnected() &&
Robert Greenwalt0659da32009-07-16 17:21:39 -07001030 !nt.isTeardownRequested()) {
The Android Open Source Project28527d22009-03-03 19:31:44 -08001031 ++numConnectedNets;
1032 }
1033 }
1034 return numConnectedNets;
1035 }
1036
1037 private void enforceAccessPermission() {
Robert Greenwalt0659da32009-07-16 17:21:39 -07001038 mContext.enforceCallingOrSelfPermission(
1039 android.Manifest.permission.ACCESS_NETWORK_STATE,
1040 "ConnectivityService");
The Android Open Source Project28527d22009-03-03 19:31:44 -08001041 }
1042
1043 private void enforceChangePermission() {
Robert Greenwalt0659da32009-07-16 17:21:39 -07001044 mContext.enforceCallingOrSelfPermission(
1045 android.Manifest.permission.CHANGE_NETWORK_STATE,
1046 "ConnectivityService");
The Android Open Source Project28527d22009-03-03 19:31:44 -08001047 }
1048
Robert Greenwalt0c4828c2010-01-26 11:40:34 -08001049 // TODO Make this a special check when it goes public
1050 private void enforceTetherChangePermission() {
1051 mContext.enforceCallingOrSelfPermission(
1052 android.Manifest.permission.CHANGE_NETWORK_STATE,
1053 "ConnectivityService");
1054 }
1055
Robert Greenwalt8e87f122010-02-11 18:18:40 -08001056 private void enforceTetherAccessPermission() {
1057 mContext.enforceCallingOrSelfPermission(
1058 android.Manifest.permission.ACCESS_NETWORK_STATE,
1059 "ConnectivityService");
1060 }
1061
Robert Greenwalt93dc1042010-06-15 12:19:37 -07001062 private void enforceConnectivityInternalPermission() {
1063 mContext.enforceCallingOrSelfPermission(
1064 android.Manifest.permission.CONNECTIVITY_INTERNAL,
1065 "ConnectivityService");
1066 }
1067
The Android Open Source Project28527d22009-03-03 19:31:44 -08001068 /**
Robert Greenwalt0659da32009-07-16 17:21:39 -07001069 * Handle a {@code DISCONNECTED} event. If this pertains to the non-active
1070 * network, we ignore it. If it is for the active network, we send out a
1071 * broadcast. But first, we check whether it might be possible to connect
1072 * to a different network.
The Android Open Source Project28527d22009-03-03 19:31:44 -08001073 * @param info the {@code NetworkInfo} for the network
1074 */
1075 private void handleDisconnect(NetworkInfo info) {
1076
Robert Greenwalt2034b912009-08-12 16:08:25 -07001077 int prevNetType = info.getType();
The Android Open Source Project28527d22009-03-03 19:31:44 -08001078
Robert Greenwalt2034b912009-08-12 16:08:25 -07001079 mNetTrackers[prevNetType].setTeardownRequested(false);
The Android Open Source Project28527d22009-03-03 19:31:44 -08001080 /*
1081 * If the disconnected network is not the active one, then don't report
1082 * this as a loss of connectivity. What probably happened is that we're
1083 * getting the disconnect for a network that we explicitly disabled
1084 * in accordance with network preference policies.
1085 */
Robert Greenwalt2034b912009-08-12 16:08:25 -07001086 if (!mNetAttributes[prevNetType].isDefault()) {
1087 List pids = mNetRequestersPids[prevNetType];
1088 for (int i = 0; i<pids.size(); i++) {
1089 Integer pid = (Integer)pids.get(i);
1090 // will remove them because the net's no longer connected
1091 // need to do this now as only now do we know the pids and
1092 // can properly null things that are no longer referenced.
1093 reassessPidDns(pid.intValue(), false);
The Android Open Source Project28527d22009-03-03 19:31:44 -08001094 }
The Android Open Source Project28527d22009-03-03 19:31:44 -08001095 }
1096
The Android Open Source Project28527d22009-03-03 19:31:44 -08001097 Intent intent = new Intent(ConnectivityManager.CONNECTIVITY_ACTION);
1098 intent.putExtra(ConnectivityManager.EXTRA_NETWORK_INFO, info);
1099 if (info.isFailover()) {
1100 intent.putExtra(ConnectivityManager.EXTRA_IS_FAILOVER, true);
1101 info.setFailover(false);
1102 }
1103 if (info.getReason() != null) {
1104 intent.putExtra(ConnectivityManager.EXTRA_REASON, info.getReason());
1105 }
1106 if (info.getExtraInfo() != null) {
Robert Greenwalt0659da32009-07-16 17:21:39 -07001107 intent.putExtra(ConnectivityManager.EXTRA_EXTRA_INFO,
1108 info.getExtraInfo());
The Android Open Source Project28527d22009-03-03 19:31:44 -08001109 }
Robert Greenwalt2034b912009-08-12 16:08:25 -07001110
Robert Greenwalt3cc68d32010-01-25 17:54:29 -08001111 NetworkStateTracker newNet = null;
1112 if (mNetAttributes[prevNetType].isDefault()) {
1113 newNet = tryFailover(prevNetType);
1114 if (newNet != null) {
1115 NetworkInfo switchTo = newNet.getNetworkInfo();
Robert Greenwaltb2a9e492010-09-20 18:01:43 -07001116 if (!switchTo.isConnected()) {
1117 // if the other net is connected they've already reset this and perhaps even gotten
1118 // a positive report we don't want to overwrite, but if not we need to clear this now
1119 // to turn our cellular sig strength white
1120 mDefaultInetConditionPublished = 0;
1121 }
Robert Greenwalt3cc68d32010-01-25 17:54:29 -08001122 intent.putExtra(ConnectivityManager.EXTRA_OTHER_NETWORK_INFO, switchTo);
1123 } else {
Robert Greenwaltb2a9e492010-09-20 18:01:43 -07001124 mDefaultInetConditionPublished = 0; // we're not connected anymore
Robert Greenwalt3cc68d32010-01-25 17:54:29 -08001125 intent.putExtra(ConnectivityManager.EXTRA_NO_CONNECTIVITY, true);
1126 }
Robert Greenwaltf55ced92010-01-20 19:29:41 -08001127 }
Robert Greenwaltb2a9e492010-09-20 18:01:43 -07001128 intent.putExtra(ConnectivityManager.EXTRA_INET_CONDITION, mDefaultInetConditionPublished);
Robert Greenwaltf55ced92010-01-20 19:29:41 -08001129 // do this before we broadcast the change
Robert Greenwalt3afbead2010-07-23 15:46:26 -07001130 handleConnectivityChange(prevNetType);
Robert Greenwaltf55ced92010-01-20 19:29:41 -08001131
1132 sendStickyBroadcast(intent);
1133 /*
1134 * If the failover network is already connected, then immediately send
1135 * out a followup broadcast indicating successful failover
1136 */
1137 if (newNet != null && newNet.getNetworkInfo().isConnected()) {
1138 sendConnectedBroadcast(newNet.getNetworkInfo());
1139 }
1140 }
1141
Robert Greenwalt3cc68d32010-01-25 17:54:29 -08001142 // returns null if no failover available
Robert Greenwaltf55ced92010-01-20 19:29:41 -08001143 private NetworkStateTracker tryFailover(int prevNetType) {
Robert Greenwalt2034b912009-08-12 16:08:25 -07001144 /*
1145 * If this is a default network, check if other defaults are available
1146 * or active
1147 */
1148 NetworkStateTracker newNet = null;
1149 if (mNetAttributes[prevNetType].isDefault()) {
Robert Greenwalt2034b912009-08-12 16:08:25 -07001150 if (mActiveDefaultNetwork == prevNetType) {
1151 mActiveDefaultNetwork = -1;
1152 }
1153
1154 int newType = -1;
1155 int newPriority = -1;
Robert Greenwalt72451bf2010-02-25 12:04:29 -08001156 boolean noMobileData = !getMobileDataEnabled();
Robert Greenwaltf55ced92010-01-20 19:29:41 -08001157 for (int checkType=0; checkType <= ConnectivityManager.MAX_NETWORK_TYPE; checkType++) {
Robert Greenwaltec05b3c2009-10-30 14:17:42 -07001158 if (checkType == prevNetType) continue;
1159 if (mNetAttributes[checkType] == null) continue;
Robert Greenwalt72451bf2010-02-25 12:04:29 -08001160 if (mNetAttributes[checkType].mRadio == ConnectivityManager.TYPE_MOBILE &&
1161 noMobileData) {
Robert Greenwalte981bc52010-10-08 16:35:52 -07001162 Slog.e(TAG, "not failing over to mobile type " + checkType +
1163 " because Mobile Data Disabled");
Robert Greenwalt72451bf2010-02-25 12:04:29 -08001164 continue;
1165 }
Robert Greenwalt2034b912009-08-12 16:08:25 -07001166 if (mNetAttributes[checkType].isDefault()) {
1167 /* TODO - if we have multiple nets we could use
1168 * we may want to put more thought into which we choose
1169 */
1170 if (checkType == mNetworkPreference) {
1171 newType = checkType;
1172 break;
1173 }
Robert Greenwaltec05b3c2009-10-30 14:17:42 -07001174 if (mNetAttributes[checkType].mPriority > newPriority) {
Robert Greenwalt2034b912009-08-12 16:08:25 -07001175 newType = checkType;
Robert Greenwaltec05b3c2009-10-30 14:17:42 -07001176 newPriority = mNetAttributes[newType].mPriority;
Robert Greenwalt2034b912009-08-12 16:08:25 -07001177 }
The Android Open Source Project28527d22009-03-03 19:31:44 -08001178 }
1179 }
Robert Greenwalt2034b912009-08-12 16:08:25 -07001180
1181 if (newType != -1) {
1182 newNet = mNetTrackers[newType];
1183 /**
1184 * See if the other network is available to fail over to.
1185 * If is not available, we enable it anyway, so that it
1186 * will be able to connect when it does become available,
1187 * but we report a total loss of connectivity rather than
1188 * report that we are attempting to fail over.
1189 */
1190 if (newNet.isAvailable()) {
1191 NetworkInfo switchTo = newNet.getNetworkInfo();
1192 switchTo.setFailover(true);
Robert Greenwalta52c75a2009-08-19 20:19:33 -07001193 if (!switchTo.isConnectedOrConnecting() ||
1194 newNet.isTeardownRequested()) {
Robert Greenwalt2034b912009-08-12 16:08:25 -07001195 newNet.reconnect();
1196 }
1197 if (DBG) {
1198 if (switchTo.isConnected()) {
Joe Onoratoc2386bb2010-02-26 18:56:32 -08001199 Slog.v(TAG, "Switching to already connected " +
Robert Greenwalt2034b912009-08-12 16:08:25 -07001200 switchTo.getTypeName());
1201 } else {
Joe Onoratoc2386bb2010-02-26 18:56:32 -08001202 Slog.v(TAG, "Attempting to switch to " +
Robert Greenwalt2034b912009-08-12 16:08:25 -07001203 switchTo.getTypeName());
1204 }
1205 }
Robert Greenwalt2034b912009-08-12 16:08:25 -07001206 } else {
1207 newNet.reconnect();
Robert Greenwalt12984322010-03-09 14:55:08 -08001208 newNet = null; // not officially avail.. try anyway, but
1209 // report no failover
Robert Greenwalt2034b912009-08-12 16:08:25 -07001210 }
Robert Greenwalte981bc52010-10-08 16:35:52 -07001211 } else {
1212 Slog.e(TAG, "Network failover failing.");
Robert Greenwalt2034b912009-08-12 16:08:25 -07001213 }
The Android Open Source Project28527d22009-03-03 19:31:44 -08001214 }
Robert Greenwalt2034b912009-08-12 16:08:25 -07001215
Robert Greenwaltf55ced92010-01-20 19:29:41 -08001216 return newNet;
The Android Open Source Project28527d22009-03-03 19:31:44 -08001217 }
1218
1219 private void sendConnectedBroadcast(NetworkInfo info) {
Robert Greenwaltd3401f92010-09-15 17:36:33 -07001220 sendGeneralBroadcast(info, ConnectivityManager.CONNECTIVITY_ACTION);
1221 }
1222
1223 private void sendInetConditionBroadcast(NetworkInfo info) {
1224 sendGeneralBroadcast(info, ConnectivityManager.INET_CONDITION_ACTION);
1225 }
1226
1227 private void sendGeneralBroadcast(NetworkInfo info, String bcastType) {
1228 Intent intent = new Intent(bcastType);
The Android Open Source Project28527d22009-03-03 19:31:44 -08001229 intent.putExtra(ConnectivityManager.EXTRA_NETWORK_INFO, info);
1230 if (info.isFailover()) {
1231 intent.putExtra(ConnectivityManager.EXTRA_IS_FAILOVER, true);
1232 info.setFailover(false);
1233 }
1234 if (info.getReason() != null) {
1235 intent.putExtra(ConnectivityManager.EXTRA_REASON, info.getReason());
1236 }
1237 if (info.getExtraInfo() != null) {
Robert Greenwalt0659da32009-07-16 17:21:39 -07001238 intent.putExtra(ConnectivityManager.EXTRA_EXTRA_INFO,
1239 info.getExtraInfo());
The Android Open Source Project28527d22009-03-03 19:31:44 -08001240 }
Robert Greenwalt986c7412010-09-08 15:24:47 -07001241 intent.putExtra(ConnectivityManager.EXTRA_INET_CONDITION, mDefaultInetConditionPublished);
Mike Lockwoodfde2b762009-08-14 14:18:49 -04001242 sendStickyBroadcast(intent);
The Android Open Source Project28527d22009-03-03 19:31:44 -08001243 }
1244
1245 /**
1246 * Called when an attempt to fail over to another network has failed.
1247 * @param info the {@link NetworkInfo} for the failed network
1248 */
1249 private void handleConnectionFailure(NetworkInfo info) {
1250 mNetTrackers[info.getType()].setTeardownRequested(false);
The Android Open Source Project28527d22009-03-03 19:31:44 -08001251
Robert Greenwalt2034b912009-08-12 16:08:25 -07001252 String reason = info.getReason();
1253 String extraInfo = info.getExtraInfo();
Robert Greenwalt0659da32009-07-16 17:21:39 -07001254
Robert Greenwalte981bc52010-10-08 16:35:52 -07001255 String reasonText;
1256 if (reason == null) {
1257 reasonText = ".";
1258 } else {
1259 reasonText = " (" + reason + ").";
The Android Open Source Project28527d22009-03-03 19:31:44 -08001260 }
Robert Greenwalte981bc52010-10-08 16:35:52 -07001261 Slog.e(TAG, "Attempt to connect to " + info.getTypeName() + " failed" + reasonText);
Robert Greenwalt2034b912009-08-12 16:08:25 -07001262
1263 Intent intent = new Intent(ConnectivityManager.CONNECTIVITY_ACTION);
1264 intent.putExtra(ConnectivityManager.EXTRA_NETWORK_INFO, info);
1265 if (getActiveNetworkInfo() == null) {
1266 intent.putExtra(ConnectivityManager.EXTRA_NO_CONNECTIVITY, true);
1267 }
1268 if (reason != null) {
1269 intent.putExtra(ConnectivityManager.EXTRA_REASON, reason);
1270 }
1271 if (extraInfo != null) {
1272 intent.putExtra(ConnectivityManager.EXTRA_EXTRA_INFO, extraInfo);
1273 }
1274 if (info.isFailover()) {
1275 intent.putExtra(ConnectivityManager.EXTRA_IS_FAILOVER, true);
1276 info.setFailover(false);
1277 }
Robert Greenwaltf55ced92010-01-20 19:29:41 -08001278
Robert Greenwalt3cc68d32010-01-25 17:54:29 -08001279 NetworkStateTracker newNet = null;
1280 if (mNetAttributes[info.getType()].isDefault()) {
1281 newNet = tryFailover(info.getType());
1282 if (newNet != null) {
1283 NetworkInfo switchTo = newNet.getNetworkInfo();
Robert Greenwaltb2a9e492010-09-20 18:01:43 -07001284 if (!switchTo.isConnected()) {
Robert Greenwalte981bc52010-10-08 16:35:52 -07001285 // if the other net is connected they've already reset this and perhaps
1286 // even gotten a positive report we don't want to overwrite, but if not
1287 // we need to clear this now to turn our cellular sig strength white
Robert Greenwaltb2a9e492010-09-20 18:01:43 -07001288 mDefaultInetConditionPublished = 0;
1289 }
Robert Greenwalt3cc68d32010-01-25 17:54:29 -08001290 intent.putExtra(ConnectivityManager.EXTRA_OTHER_NETWORK_INFO, switchTo);
1291 } else {
Robert Greenwaltb2a9e492010-09-20 18:01:43 -07001292 mDefaultInetConditionPublished = 0;
Robert Greenwalt3cc68d32010-01-25 17:54:29 -08001293 intent.putExtra(ConnectivityManager.EXTRA_NO_CONNECTIVITY, true);
1294 }
Robert Greenwaltf55ced92010-01-20 19:29:41 -08001295 }
Robert Greenwalt3cc68d32010-01-25 17:54:29 -08001296
Robert Greenwaltb2a9e492010-09-20 18:01:43 -07001297 intent.putExtra(ConnectivityManager.EXTRA_INET_CONDITION, mDefaultInetConditionPublished);
Mike Lockwoodfde2b762009-08-14 14:18:49 -04001298 sendStickyBroadcast(intent);
Robert Greenwaltf55ced92010-01-20 19:29:41 -08001299 /*
1300 * If the failover network is already connected, then immediately send
1301 * out a followup broadcast indicating successful failover
1302 */
1303 if (newNet != null && newNet.getNetworkInfo().isConnected()) {
1304 sendConnectedBroadcast(newNet.getNetworkInfo());
1305 }
Mike Lockwoodfde2b762009-08-14 14:18:49 -04001306 }
1307
1308 private void sendStickyBroadcast(Intent intent) {
1309 synchronized(this) {
Dianne Hackborna417ff82009-12-08 19:45:14 -08001310 if (!mSystemReady) {
1311 mInitialBroadcast = new Intent(intent);
Mike Lockwoodfde2b762009-08-14 14:18:49 -04001312 }
Dianne Hackborna417ff82009-12-08 19:45:14 -08001313 intent.addFlags(Intent.FLAG_RECEIVER_REGISTERED_ONLY_BEFORE_BOOT);
1314 mContext.sendStickyBroadcast(intent);
Mike Lockwoodfde2b762009-08-14 14:18:49 -04001315 }
1316 }
1317
1318 void systemReady() {
1319 synchronized(this) {
1320 mSystemReady = true;
Dianne Hackborna417ff82009-12-08 19:45:14 -08001321 if (mInitialBroadcast != null) {
1322 mContext.sendStickyBroadcast(mInitialBroadcast);
1323 mInitialBroadcast = null;
Mike Lockwoodfde2b762009-08-14 14:18:49 -04001324 }
1325 }
Robert Greenwaltc3c5f862010-10-11 16:00:27 -07001326 // load the global proxy at startup
1327 mHandler.sendMessage(mHandler.obtainMessage(EVENT_APPLY_GLOBAL_HTTP_PROXY));
The Android Open Source Project28527d22009-03-03 19:31:44 -08001328 }
1329
1330 private void handleConnect(NetworkInfo info) {
Robert Greenwalt2034b912009-08-12 16:08:25 -07001331 int type = info.getType();
The Android Open Source Project28527d22009-03-03 19:31:44 -08001332
1333 // snapshot isFailover, because sendConnectedBroadcast() resets it
1334 boolean isFailover = info.isFailover();
Robert Greenwalt2034b912009-08-12 16:08:25 -07001335 NetworkStateTracker thisNet = mNetTrackers[type];
The Android Open Source Project28527d22009-03-03 19:31:44 -08001336
Robert Greenwalt2034b912009-08-12 16:08:25 -07001337 // if this is a default net and other default is running
1338 // kill the one not preferred
1339 if (mNetAttributes[type].isDefault()) {
Robert Greenwalt2034b912009-08-12 16:08:25 -07001340 if (mActiveDefaultNetwork != -1 && mActiveDefaultNetwork != type) {
1341 if ((type != mNetworkPreference &&
1342 mNetAttributes[mActiveDefaultNetwork].mPriority >
1343 mNetAttributes[type].mPriority) ||
1344 mNetworkPreference == mActiveDefaultNetwork) {
1345 // don't accept this one
Joe Onoratoc2386bb2010-02-26 18:56:32 -08001346 if (DBG) Slog.v(TAG, "Not broadcasting CONNECT_ACTION " +
Robert Greenwalt2034b912009-08-12 16:08:25 -07001347 "to torn down network " + info.getTypeName());
1348 teardown(thisNet);
1349 return;
1350 } else {
1351 // tear down the other
1352 NetworkStateTracker otherNet =
1353 mNetTrackers[mActiveDefaultNetwork];
Joe Onoratoc2386bb2010-02-26 18:56:32 -08001354 if (DBG) Slog.v(TAG, "Policy requires " +
Robert Greenwalt2034b912009-08-12 16:08:25 -07001355 otherNet.getNetworkInfo().getTypeName() +
1356 " teardown");
1357 if (!teardown(otherNet)) {
Joe Onoratoc2386bb2010-02-26 18:56:32 -08001358 Slog.e(TAG, "Network declined teardown request");
Robert Greenwalt2034b912009-08-12 16:08:25 -07001359 return;
1360 }
Robert Greenwalt93dc1042010-06-15 12:19:37 -07001361 }
1362 }
1363 synchronized (ConnectivityService.this) {
1364 // have a new default network, release the transition wakelock in a second
1365 // if it's held. The second pause is to allow apps to reconnect over the
1366 // new network
1367 if (mNetTransitionWakeLock.isHeld()) {
1368 mHandler.sendMessageDelayed(mHandler.obtainMessage(
Robert Greenwaltccb36f92010-09-24 14:32:21 -07001369 EVENT_CLEAR_NET_TRANSITION_WAKELOCK,
Robert Greenwalt93dc1042010-06-15 12:19:37 -07001370 mNetTransitionWakeLockSerialNumber, 0),
1371 1000);
Robert Greenwalt2034b912009-08-12 16:08:25 -07001372 }
1373 }
1374 mActiveDefaultNetwork = type;
Robert Greenwalt986c7412010-09-08 15:24:47 -07001375 // this will cause us to come up initially as unconnected and switching
1376 // to connected after our normal pause unless somebody reports us as reall
1377 // disconnected
1378 mDefaultInetConditionPublished = 0;
1379 mDefaultConnectionSequence++;
1380 mInetConditionChangeInFlight = false;
1381 // Don't do this - if we never sign in stay, grey
1382 //reportNetworkCondition(mActiveDefaultNetwork, 100);
Robert Greenwalt2034b912009-08-12 16:08:25 -07001383 }
The Android Open Source Project28527d22009-03-03 19:31:44 -08001384 thisNet.setTeardownRequested(false);
Irfan Sheriff7f132d92010-06-09 15:39:36 -07001385 updateNetworkSettings(thisNet);
Robert Greenwalt3afbead2010-07-23 15:46:26 -07001386 handleConnectivityChange(type);
Robert Greenwalt2034b912009-08-12 16:08:25 -07001387 sendConnectedBroadcast(info);
The Android Open Source Project28527d22009-03-03 19:31:44 -08001388 }
1389
The Android Open Source Project28527d22009-03-03 19:31:44 -08001390 /**
Robert Greenwalt3afbead2010-07-23 15:46:26 -07001391 * After a change in the connectivity state of a network. We're mainly
1392 * concerned with making sure that the list of DNS servers is set up
1393 * according to which networks are connected, and ensuring that the
1394 * right routing table entries exist.
The Android Open Source Project28527d22009-03-03 19:31:44 -08001395 */
Robert Greenwalt3afbead2010-07-23 15:46:26 -07001396 private void handleConnectivityChange(int netType) {
The Android Open Source Project28527d22009-03-03 19:31:44 -08001397 /*
Robert Greenwalt2034b912009-08-12 16:08:25 -07001398 * If a non-default network is enabled, add the host routes that
Robert Greenwalt3afbead2010-07-23 15:46:26 -07001399 * will allow it's DNS servers to be accessed.
The Android Open Source Project28527d22009-03-03 19:31:44 -08001400 */
Robert Greenwalt3afbead2010-07-23 15:46:26 -07001401 handleDnsConfigurationChange(netType);
Robert Greenwalt2034b912009-08-12 16:08:25 -07001402
Robert Greenwalt3afbead2010-07-23 15:46:26 -07001403 if (mNetTrackers[netType].getNetworkInfo().isConnected()) {
1404 if (mNetAttributes[netType].isDefault()) {
Robert Greenwaltc3c5f862010-10-11 16:00:27 -07001405 handleApplyDefaultProxy(netType);
Robert Greenwalt3afbead2010-07-23 15:46:26 -07001406 addDefaultRoute(mNetTrackers[netType]);
Robert Greenwalt2034b912009-08-12 16:08:25 -07001407 } else {
Robert Greenwalt3afbead2010-07-23 15:46:26 -07001408 addPrivateDnsRoutes(mNetTrackers[netType]);
1409 }
1410 } else {
1411 if (mNetAttributes[netType].isDefault()) {
1412 removeDefaultRoute(mNetTrackers[netType]);
1413 } else {
1414 removePrivateDnsRoutes(mNetTrackers[netType]);
Robert Greenwalt2034b912009-08-12 16:08:25 -07001415 }
The Android Open Source Project28527d22009-03-03 19:31:44 -08001416 }
1417 }
1418
Irfan Sheriff7f132d92010-06-09 15:39:36 -07001419 private void addPrivateDnsRoutes(NetworkStateTracker nt) {
Irfan Sheriff7f132d92010-06-09 15:39:36 -07001420 boolean privateDnsRouteSet = nt.isPrivateDnsRouteSet();
Robert Greenwalt1f1bcfe2010-08-30 10:56:47 -07001421 LinkProperties p = nt.getLinkProperties();
Robert Greenwalta7dfbd32010-06-15 15:43:39 -07001422 if (p == null) return;
1423 String interfaceName = p.getInterfaceName();
Irfan Sheriff7f132d92010-06-09 15:39:36 -07001424
1425 if (DBG) {
1426 Slog.d(TAG, "addPrivateDnsRoutes for " + nt +
1427 "(" + interfaceName + ") - mPrivateDnsRouteSet = " + privateDnsRouteSet);
1428 }
Irfan Sheriff7f132d92010-06-09 15:39:36 -07001429 if (interfaceName != null && !privateDnsRouteSet) {
Robert Greenwalta7dfbd32010-06-15 15:43:39 -07001430 Collection<InetAddress> dnsList = p.getDnses();
1431 for (InetAddress dns : dnsList) {
1432 if (DBG) Slog.d(TAG, " adding " + dns);
Robert Greenwalt7fe44cb2010-08-27 09:24:29 -07001433 NetworkUtils.addHostRoute(interfaceName, dns, null);
Irfan Sheriff7f132d92010-06-09 15:39:36 -07001434 }
1435 nt.privateDnsRouteSet(true);
1436 }
1437 }
1438
1439 private void removePrivateDnsRoutes(NetworkStateTracker nt) {
1440 // TODO - we should do this explicitly but the NetUtils api doesnt
1441 // support this yet - must remove all. No worse than before
Robert Greenwalt1f1bcfe2010-08-30 10:56:47 -07001442 LinkProperties p = nt.getLinkProperties();
Robert Greenwalta7dfbd32010-06-15 15:43:39 -07001443 if (p == null) return;
1444 String interfaceName = p.getInterfaceName();
Irfan Sheriff7f132d92010-06-09 15:39:36 -07001445 boolean privateDnsRouteSet = nt.isPrivateDnsRouteSet();
1446 if (interfaceName != null && privateDnsRouteSet) {
1447 if (DBG) {
1448 Slog.d(TAG, "removePrivateDnsRoutes for " + nt.getNetworkInfo().getTypeName() +
1449 " (" + interfaceName + ")");
1450 }
1451 NetworkUtils.removeHostRoutes(interfaceName);
1452 nt.privateDnsRouteSet(false);
1453 }
1454 }
1455
Irfan Sheriff7f132d92010-06-09 15:39:36 -07001456
1457 private void addDefaultRoute(NetworkStateTracker nt) {
Robert Greenwalt1f1bcfe2010-08-30 10:56:47 -07001458 LinkProperties p = nt.getLinkProperties();
Robert Greenwalta7dfbd32010-06-15 15:43:39 -07001459 if (p == null) return;
1460 String interfaceName = p.getInterfaceName();
1461 InetAddress defaultGatewayAddr = p.getGateway();
Irfan Sheriff7f132d92010-06-09 15:39:36 -07001462
Robert Greenwalt3afbead2010-07-23 15:46:26 -07001463 if ((interfaceName != null) && (defaultGatewayAddr != null )) {
Robert Greenwalt7fe44cb2010-08-27 09:24:29 -07001464 if (!NetworkUtils.addDefaultRoute(interfaceName, defaultGatewayAddr) && DBG) {
Robert Greenwalta7dfbd32010-06-15 15:43:39 -07001465 NetworkInfo networkInfo = nt.getNetworkInfo();
Irfan Sheriff7f132d92010-06-09 15:39:36 -07001466 Slog.d(TAG, "addDefaultRoute for " + networkInfo.getTypeName() +
1467 " (" + interfaceName + "), GatewayAddr=" + defaultGatewayAddr);
1468 }
Irfan Sheriff7f132d92010-06-09 15:39:36 -07001469 }
1470 }
1471
1472
1473 public void removeDefaultRoute(NetworkStateTracker nt) {
Robert Greenwalt1f1bcfe2010-08-30 10:56:47 -07001474 LinkProperties p = nt.getLinkProperties();
Robert Greenwalta7dfbd32010-06-15 15:43:39 -07001475 if (p == null) return;
1476 String interfaceName = p.getInterfaceName();
Irfan Sheriff7f132d92010-06-09 15:39:36 -07001477
Robert Greenwalt3afbead2010-07-23 15:46:26 -07001478 if (interfaceName != null) {
1479 if ((NetworkUtils.removeDefaultRoute(interfaceName) >= 0) && DBG) {
Robert Greenwalta7dfbd32010-06-15 15:43:39 -07001480 NetworkInfo networkInfo = nt.getNetworkInfo();
Irfan Sheriff7f132d92010-06-09 15:39:36 -07001481 Slog.d(TAG, "removeDefaultRoute for " + networkInfo.getTypeName() + " (" +
1482 interfaceName + ")");
1483 }
Irfan Sheriff7f132d92010-06-09 15:39:36 -07001484 }
1485 }
1486
1487 /**
1488 * Reads the network specific TCP buffer sizes from SystemProperties
1489 * net.tcp.buffersize.[default|wifi|umts|edge|gprs] and set them for system
1490 * wide use
1491 */
1492 public void updateNetworkSettings(NetworkStateTracker nt) {
1493 String key = nt.getTcpBufferSizesPropName();
1494 String bufferSizes = SystemProperties.get(key);
1495
1496 if (bufferSizes.length() == 0) {
1497 Slog.e(TAG, key + " not found in system properties. Using defaults");
1498
1499 // Setting to default values so we won't be stuck to previous values
1500 key = "net.tcp.buffersize.default";
1501 bufferSizes = SystemProperties.get(key);
1502 }
1503
1504 // Set values in kernel
1505 if (bufferSizes.length() != 0) {
1506 if (DBG) {
1507 Slog.v(TAG, "Setting TCP values: [" + bufferSizes
1508 + "] which comes from [" + key + "]");
1509 }
1510 setBufferSize(bufferSizes);
1511 }
1512 }
1513
1514 /**
1515 * Writes TCP buffer sizes to /sys/kernel/ipv4/tcp_[r/w]mem_[min/def/max]
1516 * which maps to /proc/sys/net/ipv4/tcp_rmem and tcpwmem
1517 *
1518 * @param bufferSizes in the format of "readMin, readInitial, readMax,
1519 * writeMin, writeInitial, writeMax"
1520 */
1521 private void setBufferSize(String bufferSizes) {
1522 try {
1523 String[] values = bufferSizes.split(",");
1524
1525 if (values.length == 6) {
1526 final String prefix = "/sys/kernel/ipv4/tcp_";
1527 stringToFile(prefix + "rmem_min", values[0]);
1528 stringToFile(prefix + "rmem_def", values[1]);
1529 stringToFile(prefix + "rmem_max", values[2]);
1530 stringToFile(prefix + "wmem_min", values[3]);
1531 stringToFile(prefix + "wmem_def", values[4]);
1532 stringToFile(prefix + "wmem_max", values[5]);
1533 } else {
1534 Slog.e(TAG, "Invalid buffersize string: " + bufferSizes);
1535 }
1536 } catch (IOException e) {
1537 Slog.e(TAG, "Can't set tcp buffer sizes:" + e);
1538 }
1539 }
1540
1541 /**
1542 * Writes string to file. Basically same as "echo -n $string > $filename"
1543 *
1544 * @param filename
1545 * @param string
1546 * @throws IOException
1547 */
1548 private void stringToFile(String filename, String string) throws IOException {
1549 FileWriter out = new FileWriter(filename);
1550 try {
1551 out.write(string);
1552 } finally {
1553 out.close();
1554 }
1555 }
1556
1557
Robert Greenwalt2034b912009-08-12 16:08:25 -07001558 /**
1559 * Adjust the per-process dns entries (net.dns<x>.<pid>) based
1560 * on the highest priority active net which this process requested.
1561 * If there aren't any, clear it out
1562 */
1563 private void reassessPidDns(int myPid, boolean doBump)
1564 {
Joe Onoratoc2386bb2010-02-26 18:56:32 -08001565 if (DBG) Slog.d(TAG, "reassessPidDns for pid " + myPid);
Robert Greenwalt2034b912009-08-12 16:08:25 -07001566 for(int i : mPriorityList) {
1567 if (mNetAttributes[i].isDefault()) {
1568 continue;
1569 }
1570 NetworkStateTracker nt = mNetTrackers[i];
Robert Greenwalt0659da32009-07-16 17:21:39 -07001571 if (nt.getNetworkInfo().isConnected() &&
1572 !nt.isTeardownRequested()) {
Robert Greenwalt1f1bcfe2010-08-30 10:56:47 -07001573 LinkProperties p = nt.getLinkProperties();
Robert Greenwalta7dfbd32010-06-15 15:43:39 -07001574 if (p == null) continue;
Robert Greenwalt2034b912009-08-12 16:08:25 -07001575 List pids = mNetRequestersPids[i];
1576 for (int j=0; j<pids.size(); j++) {
1577 Integer pid = (Integer)pids.get(j);
1578 if (pid.intValue() == myPid) {
Robert Greenwalta7dfbd32010-06-15 15:43:39 -07001579 Collection<InetAddress> dnses = p.getDnses();
1580 writePidDns(dnses, myPid);
Robert Greenwalt2034b912009-08-12 16:08:25 -07001581 if (doBump) {
1582 bumpDns();
1583 }
1584 return;
1585 }
1586 }
1587 }
1588 }
1589 // nothing found - delete
1590 for (int i = 1; ; i++) {
1591 String prop = "net.dns" + i + "." + myPid;
1592 if (SystemProperties.get(prop).length() == 0) {
1593 if (doBump) {
1594 bumpDns();
1595 }
1596 return;
1597 }
1598 SystemProperties.set(prop, "");
1599 }
1600 }
1601
Robert Greenwalta7dfbd32010-06-15 15:43:39 -07001602 private void writePidDns(Collection <InetAddress> dnses, int pid) {
Robert Greenwalt2034b912009-08-12 16:08:25 -07001603 int j = 1;
Robert Greenwalta7dfbd32010-06-15 15:43:39 -07001604 for (InetAddress dns : dnses) {
1605 SystemProperties.set("net.dns" + j++ + "." + pid, dns.getHostAddress());
Robert Greenwalt2034b912009-08-12 16:08:25 -07001606 }
1607 }
1608
1609 private void bumpDns() {
1610 /*
1611 * Bump the property that tells the name resolver library to reread
1612 * the DNS server list from the properties.
1613 */
1614 String propVal = SystemProperties.get("net.dnschange");
1615 int n = 0;
1616 if (propVal.length() != 0) {
1617 try {
1618 n = Integer.parseInt(propVal);
1619 } catch (NumberFormatException e) {}
1620 }
1621 SystemProperties.set("net.dnschange", "" + (n+1));
Robert Greenwalt051642b2010-11-02 14:08:23 -07001622 /*
1623 * Tell the VMs to toss their DNS caches
1624 */
1625 Intent intent = new Intent(Intent.ACTION_CLEAR_DNS_CACHE);
1626 intent.addFlags(Intent.FLAG_RECEIVER_REPLACE_PENDING);
1627 mContext.sendBroadcast(intent);
Robert Greenwalt2034b912009-08-12 16:08:25 -07001628 }
1629
Robert Greenwalt3afbead2010-07-23 15:46:26 -07001630 private void handleDnsConfigurationChange(int netType) {
Robert Greenwalt2034b912009-08-12 16:08:25 -07001631 // add default net's dns entries
Robert Greenwalt3afbead2010-07-23 15:46:26 -07001632 NetworkStateTracker nt = mNetTrackers[netType];
1633 if (nt != null && nt.getNetworkInfo().isConnected() && !nt.isTeardownRequested()) {
Robert Greenwalt1f1bcfe2010-08-30 10:56:47 -07001634 LinkProperties p = nt.getLinkProperties();
Robert Greenwalt3afbead2010-07-23 15:46:26 -07001635 if (p == null) return;
1636 Collection<InetAddress> dnses = p.getDnses();
1637 if (mNetAttributes[netType].isDefault()) {
1638 int j = 1;
Robert Greenwalt94daa182010-09-01 11:34:05 -07001639 if (dnses.size() == 0 && mDefaultDns != null) {
Robert Greenwalt3afbead2010-07-23 15:46:26 -07001640 if (DBG) {
Robert Greenwalt94daa182010-09-01 11:34:05 -07001641 Slog.d(TAG, "no dns provided - using " + mDefaultDns.getHostAddress());
Robert Greenwalt2034b912009-08-12 16:08:25 -07001642 }
Robert Greenwalt94daa182010-09-01 11:34:05 -07001643 SystemProperties.set("net.dns1", mDefaultDns.getHostAddress());
1644 j++;
1645 } else {
1646 for (InetAddress dns : dnses) {
1647 if (DBG) {
1648 Slog.d(TAG, "adding dns " + dns + " for " +
1649 nt.getNetworkInfo().getTypeName());
1650 }
1651 SystemProperties.set("net.dns" + j++, dns.getHostAddress());
1652 }
Robert Greenwalt3afbead2010-07-23 15:46:26 -07001653 }
1654 for (int k=j ; k<mNumDnsEntries; k++) {
1655 if (DBG) Slog.d(TAG, "erasing net.dns" + k);
1656 SystemProperties.set("net.dns" + k, "");
1657 }
1658 mNumDnsEntries = j;
1659 } else {
1660 // set per-pid dns for attached secondary nets
1661 List pids = mNetRequestersPids[netType];
1662 for (int y=0; y< pids.size(); y++) {
1663 Integer pid = (Integer)pids.get(y);
1664 writePidDns(dnses, pid.intValue());
The Android Open Source Project28527d22009-03-03 19:31:44 -08001665 }
1666 }
Robert Greenwalt3afbead2010-07-23 15:46:26 -07001667 bumpDns();
The Android Open Source Project28527d22009-03-03 19:31:44 -08001668 }
Robert Greenwalt2034b912009-08-12 16:08:25 -07001669 }
1670
1671 private int getRestoreDefaultNetworkDelay() {
1672 String restoreDefaultNetworkDelayStr = SystemProperties.get(
1673 NETWORK_RESTORE_DELAY_PROP_NAME);
1674 if(restoreDefaultNetworkDelayStr != null &&
1675 restoreDefaultNetworkDelayStr.length() != 0) {
1676 try {
1677 return Integer.valueOf(restoreDefaultNetworkDelayStr);
1678 } catch (NumberFormatException e) {
1679 }
The Android Open Source Project28527d22009-03-03 19:31:44 -08001680 }
Robert Greenwalt2034b912009-08-12 16:08:25 -07001681 return RESTORE_DEFAULT_NETWORK_DELAY;
The Android Open Source Project28527d22009-03-03 19:31:44 -08001682 }
1683
1684 @Override
1685 protected void dump(FileDescriptor fd, PrintWriter pw, String[] args) {
Robert Greenwalt0659da32009-07-16 17:21:39 -07001686 if (mContext.checkCallingOrSelfPermission(
1687 android.Manifest.permission.DUMP)
The Android Open Source Project28527d22009-03-03 19:31:44 -08001688 != PackageManager.PERMISSION_GRANTED) {
Robert Greenwalt0659da32009-07-16 17:21:39 -07001689 pw.println("Permission Denial: can't dump ConnectivityService " +
1690 "from from pid=" + Binder.getCallingPid() + ", uid=" +
1691 Binder.getCallingUid());
The Android Open Source Project28527d22009-03-03 19:31:44 -08001692 return;
1693 }
The Android Open Source Project28527d22009-03-03 19:31:44 -08001694 pw.println();
1695 for (NetworkStateTracker nst : mNetTrackers) {
Robert Greenwalt3eeb6032009-12-21 18:24:07 -08001696 if (nst != null) {
1697 if (nst.getNetworkInfo().isConnected()) {
1698 pw.println("Active network: " + nst.getNetworkInfo().
1699 getTypeName());
1700 }
1701 pw.println(nst.getNetworkInfo());
1702 pw.println(nst);
1703 pw.println();
Robert Greenwalt2034b912009-08-12 16:08:25 -07001704 }
The Android Open Source Project28527d22009-03-03 19:31:44 -08001705 }
Robert Greenwalt3eeb6032009-12-21 18:24:07 -08001706
1707 pw.println("Network Requester Pids:");
1708 for (int net : mPriorityList) {
1709 String pidString = net + ": ";
1710 for (Object pid : mNetRequestersPids[net]) {
1711 pidString = pidString + pid.toString() + ", ";
1712 }
1713 pw.println(pidString);
1714 }
1715 pw.println();
1716
1717 pw.println("FeatureUsers:");
1718 for (Object requester : mFeatureUsers) {
1719 pw.println(requester.toString());
1720 }
1721 pw.println();
Robert Greenwalt8e87f122010-02-11 18:18:40 -08001722
Robert Greenwalt93dc1042010-06-15 12:19:37 -07001723 synchronized (this) {
1724 pw.println("NetworkTranstionWakeLock is currently " +
1725 (mNetTransitionWakeLock.isHeld() ? "" : "not ") + "held.");
1726 pw.println("It was last requested for "+mNetTransitionWakeLockCausedBy);
1727 }
1728 pw.println();
1729
Robert Greenwalt8e87f122010-02-11 18:18:40 -08001730 mTethering.dump(fd, pw, args);
Robert Greenwalt0e80be12010-09-20 14:35:25 -07001731
1732 if (mInetLog != null) {
1733 pw.println();
1734 pw.println("Inet condition reports:");
1735 for(int i = 0; i < mInetLog.size(); i++) {
1736 pw.println(mInetLog.get(i));
1737 }
1738 }
The Android Open Source Project28527d22009-03-03 19:31:44 -08001739 }
1740
Robert Greenwalt2034b912009-08-12 16:08:25 -07001741 // must be stateless - things change under us.
The Android Open Source Project28527d22009-03-03 19:31:44 -08001742 private class MyHandler extends Handler {
Wink Saville775aad62010-09-02 19:23:52 -07001743 public MyHandler(Looper looper) {
1744 super(looper);
1745 }
1746
The Android Open Source Project28527d22009-03-03 19:31:44 -08001747 @Override
1748 public void handleMessage(Message msg) {
1749 NetworkInfo info;
1750 switch (msg.what) {
1751 case NetworkStateTracker.EVENT_STATE_CHANGED:
1752 info = (NetworkInfo) msg.obj;
Robert Greenwalt12c44552009-12-07 11:33:18 -08001753 int type = info.getType();
1754 NetworkInfo.State state = info.getState();
Robert Greenwalt24e2d2b2010-01-25 16:14:00 -08001755 // only do this optimization for wifi. It going into scan mode for location
1756 // services generates alot of noise. Meanwhile the mms apn won't send out
1757 // subsequent notifications when on default cellular because it never
1758 // disconnects.. so only do this to wifi notifications. Fixed better when the
1759 // APN notifications are standardized.
1760 if (mNetAttributes[type].mLastState == state &&
1761 mNetAttributes[type].mRadio == ConnectivityManager.TYPE_WIFI) {
Robert Greenwalt12c44552009-12-07 11:33:18 -08001762 if (DBG) {
Robert Greenwalt24e2d2b2010-01-25 16:14:00 -08001763 // TODO - remove this after we validate the dropping doesn't break
1764 // anything
Joe Onoratoc2386bb2010-02-26 18:56:32 -08001765 Slog.d(TAG, "Dropping ConnectivityChange for " +
Robert Greenwalt2adbc7f2010-01-13 09:36:31 -08001766 info.getTypeName() + ": " +
Robert Greenwalt12c44552009-12-07 11:33:18 -08001767 state + "/" + info.getDetailedState());
1768 }
1769 return;
1770 }
1771 mNetAttributes[type].mLastState = state;
1772
Joe Onoratoc2386bb2010-02-26 18:56:32 -08001773 if (DBG) Slog.d(TAG, "ConnectivityChange for " +
Robert Greenwalt0659da32009-07-16 17:21:39 -07001774 info.getTypeName() + ": " +
Robert Greenwalt12c44552009-12-07 11:33:18 -08001775 state + "/" + info.getDetailedState());
The Android Open Source Project28527d22009-03-03 19:31:44 -08001776
1777 // Connectivity state changed:
1778 // [31-13] Reserved for future use
Robert Greenwalt0659da32009-07-16 17:21:39 -07001779 // [12-9] Network subtype (for mobile network, as defined
1780 // by TelephonyManager)
1781 // [8-3] Detailed state ordinal (as defined by
1782 // NetworkInfo.DetailedState)
The Android Open Source Project28527d22009-03-03 19:31:44 -08001783 // [2-0] Network type (as defined by ConnectivityManager)
1784 int eventLogParam = (info.getType() & 0x7) |
1785 ((info.getDetailedState().ordinal() & 0x3f) << 3) |
1786 (info.getSubtype() << 9);
Doug Zongker2fc96232009-12-04 10:31:43 -08001787 EventLog.writeEvent(EventLogTags.CONNECTIVITY_STATE_CHANGED,
Robert Greenwalt0659da32009-07-16 17:21:39 -07001788 eventLogParam);
1789
1790 if (info.getDetailedState() ==
1791 NetworkInfo.DetailedState.FAILED) {
The Android Open Source Project28527d22009-03-03 19:31:44 -08001792 handleConnectionFailure(info);
Robert Greenwalt12c44552009-12-07 11:33:18 -08001793 } else if (state == NetworkInfo.State.DISCONNECTED) {
The Android Open Source Project28527d22009-03-03 19:31:44 -08001794 handleDisconnect(info);
Robert Greenwalt12c44552009-12-07 11:33:18 -08001795 } else if (state == NetworkInfo.State.SUSPENDED) {
The Android Open Source Project28527d22009-03-03 19:31:44 -08001796 // TODO: need to think this over.
Robert Greenwalt0659da32009-07-16 17:21:39 -07001797 // the logic here is, handle SUSPENDED the same as
1798 // DISCONNECTED. The only difference being we are
1799 // broadcasting an intent with NetworkInfo that's
1800 // suspended. This allows the applications an
1801 // opportunity to handle DISCONNECTED and SUSPENDED
1802 // differently, or not.
The Android Open Source Project28527d22009-03-03 19:31:44 -08001803 handleDisconnect(info);
Robert Greenwalt12c44552009-12-07 11:33:18 -08001804 } else if (state == NetworkInfo.State.CONNECTED) {
The Android Open Source Project28527d22009-03-03 19:31:44 -08001805 handleConnect(info);
1806 }
The Android Open Source Project28527d22009-03-03 19:31:44 -08001807 break;
The Android Open Source Project28527d22009-03-03 19:31:44 -08001808 case NetworkStateTracker.EVENT_CONFIGURATION_CHANGED:
Robert Greenwalt3afbead2010-07-23 15:46:26 -07001809 info = (NetworkInfo) msg.obj;
1810 type = info.getType();
Robert Greenwaltc3c5f862010-10-11 16:00:27 -07001811 handleConnectivityChange(type);
The Android Open Source Project28527d22009-03-03 19:31:44 -08001812 break;
Robert Greenwaltccb36f92010-09-24 14:32:21 -07001813 case EVENT_CLEAR_NET_TRANSITION_WAKELOCK:
Robert Greenwalt93dc1042010-06-15 12:19:37 -07001814 String causedBy = null;
1815 synchronized (ConnectivityService.this) {
1816 if (msg.arg1 == mNetTransitionWakeLockSerialNumber &&
1817 mNetTransitionWakeLock.isHeld()) {
1818 mNetTransitionWakeLock.release();
1819 causedBy = mNetTransitionWakeLockCausedBy;
1820 }
1821 }
1822 if (causedBy != null) {
1823 Slog.d(TAG, "NetTransition Wakelock for " +
1824 causedBy + " released by timeout");
1825 }
Robert Greenwaltcf1a56c2010-09-09 14:05:10 -07001826 break;
Robert Greenwalt6a2db8a2010-09-23 10:05:56 -07001827 case EVENT_RESTORE_DEFAULT_NETWORK:
Robert Greenwaltaffc3a12009-09-27 17:27:04 -07001828 FeatureUser u = (FeatureUser)msg.obj;
1829 u.expire();
Robert Greenwalt986c7412010-09-08 15:24:47 -07001830 break;
Robert Greenwalt6a2db8a2010-09-23 10:05:56 -07001831 case EVENT_INET_CONDITION_CHANGE:
1832 {
1833 int netType = msg.arg1;
1834 int condition = msg.arg2;
1835 handleInetConditionChange(netType, condition);
Robert Greenwalt93dc1042010-06-15 12:19:37 -07001836 break;
Robert Greenwalt6a2db8a2010-09-23 10:05:56 -07001837 }
1838 case EVENT_INET_CONDITION_HOLD_END:
1839 {
1840 int netType = msg.arg1;
1841 int sequence = msg.arg2;
1842 handleInetConditionHoldEnd(netType, sequence);
Robert Greenwalt986c7412010-09-08 15:24:47 -07001843 break;
Robert Greenwalt6a2db8a2010-09-23 10:05:56 -07001844 }
1845 case EVENT_SET_NETWORK_PREFERENCE:
1846 {
1847 int preference = msg.arg1;
1848 handleSetNetworkPreference(preference);
1849 break;
1850 }
1851 case EVENT_SET_BACKGROUND_DATA:
1852 {
1853 boolean enabled = (msg.arg1 == ENABLED);
1854 handleSetBackgroundData(enabled);
1855 break;
1856 }
1857 case EVENT_SET_MOBILE_DATA:
1858 {
1859 boolean enabled = (msg.arg1 == ENABLED);
1860 handleSetMobileData(enabled);
1861 break;
1862 }
Robert Greenwaltc3c5f862010-10-11 16:00:27 -07001863 case EVENT_APPLY_GLOBAL_HTTP_PROXY:
1864 {
1865 handleDeprecatedGlobalHttpProxy();
1866 }
The Android Open Source Project28527d22009-03-03 19:31:44 -08001867 }
1868 }
1869 }
Robert Greenwalt0c4828c2010-01-26 11:40:34 -08001870
1871 // javadoc from interface
Robert Greenwalt4283ded2010-03-02 17:25:02 -08001872 public int tether(String iface) {
Robert Greenwalt0c4828c2010-01-26 11:40:34 -08001873 enforceTetherChangePermission();
Robert Greenwalt4283ded2010-03-02 17:25:02 -08001874
1875 if (isTetheringSupported()) {
1876 return mTethering.tether(iface);
1877 } else {
1878 return ConnectivityManager.TETHER_ERROR_UNSUPPORTED;
1879 }
Robert Greenwalt0c4828c2010-01-26 11:40:34 -08001880 }
1881
1882 // javadoc from interface
Robert Greenwalt4283ded2010-03-02 17:25:02 -08001883 public int untether(String iface) {
Robert Greenwalt0c4828c2010-01-26 11:40:34 -08001884 enforceTetherChangePermission();
Robert Greenwalt4283ded2010-03-02 17:25:02 -08001885
1886 if (isTetheringSupported()) {
1887 return mTethering.untether(iface);
1888 } else {
1889 return ConnectivityManager.TETHER_ERROR_UNSUPPORTED;
1890 }
1891 }
1892
1893 // javadoc from interface
1894 public int getLastTetherError(String iface) {
1895 enforceTetherAccessPermission();
1896
1897 if (isTetheringSupported()) {
1898 return mTethering.getLastTetherError(iface);
1899 } else {
1900 return ConnectivityManager.TETHER_ERROR_UNSUPPORTED;
1901 }
Robert Greenwalt8e87f122010-02-11 18:18:40 -08001902 }
1903
1904 // TODO - proper iface API for selection by property, inspection, etc
1905 public String[] getTetherableUsbRegexs() {
1906 enforceTetherAccessPermission();
1907 if (isTetheringSupported()) {
1908 return mTethering.getTetherableUsbRegexs();
1909 } else {
1910 return new String[0];
1911 }
1912 }
1913
1914 public String[] getTetherableWifiRegexs() {
1915 enforceTetherAccessPermission();
1916 if (isTetheringSupported()) {
1917 return mTethering.getTetherableWifiRegexs();
1918 } else {
1919 return new String[0];
1920 }
Robert Greenwalt0c4828c2010-01-26 11:40:34 -08001921 }
1922
Danica Chang96567052010-08-11 14:54:43 -07001923 public String[] getTetherableBluetoothRegexs() {
1924 enforceTetherAccessPermission();
1925 if (isTetheringSupported()) {
1926 return mTethering.getTetherableBluetoothRegexs();
1927 } else {
1928 return new String[0];
1929 }
1930 }
1931
Robert Greenwalt0c4828c2010-01-26 11:40:34 -08001932 // TODO - move iface listing, queries, etc to new module
1933 // javadoc from interface
1934 public String[] getTetherableIfaces() {
Robert Greenwalt8e87f122010-02-11 18:18:40 -08001935 enforceTetherAccessPermission();
Robert Greenwalt0c4828c2010-01-26 11:40:34 -08001936 return mTethering.getTetherableIfaces();
1937 }
1938
1939 public String[] getTetheredIfaces() {
Robert Greenwalt8e87f122010-02-11 18:18:40 -08001940 enforceTetherAccessPermission();
Robert Greenwalt0c4828c2010-01-26 11:40:34 -08001941 return mTethering.getTetheredIfaces();
1942 }
Robert Greenwalt8e87f122010-02-11 18:18:40 -08001943
Robert Greenwalt4283ded2010-03-02 17:25:02 -08001944 public String[] getTetheringErroredIfaces() {
1945 enforceTetherAccessPermission();
1946 return mTethering.getErroredIfaces();
1947 }
1948
Robert Greenwalt8e87f122010-02-11 18:18:40 -08001949 // if ro.tether.denied = true we default to no tethering
1950 // gservices could set the secure setting to 1 though to enable it on a build where it
1951 // had previously been turned off.
1952 public boolean isTetheringSupported() {
1953 enforceTetherAccessPermission();
1954 int defaultVal = (SystemProperties.get("ro.tether.denied").equals("true") ? 0 : 1);
Robert Greenwaltf1b66e12010-02-25 12:29:30 -08001955 boolean tetherEnabledInSettings = (Settings.Secure.getInt(mContext.getContentResolver(),
1956 Settings.Secure.TETHER_SUPPORTED, defaultVal) != 0);
1957 return tetherEnabledInSettings && mTetheringConfigValid;
Robert Greenwalt8e87f122010-02-11 18:18:40 -08001958 }
Robert Greenwalt93dc1042010-06-15 12:19:37 -07001959
1960 // An API NetworkStateTrackers can call when they lose their network.
1961 // This will automatically be cleared after X seconds or a network becomes CONNECTED,
1962 // whichever happens first. The timer is started by the first caller and not
1963 // restarted by subsequent callers.
1964 public void requestNetworkTransitionWakelock(String forWhom) {
1965 enforceConnectivityInternalPermission();
1966 synchronized (this) {
1967 if (mNetTransitionWakeLock.isHeld()) return;
1968 mNetTransitionWakeLockSerialNumber++;
1969 mNetTransitionWakeLock.acquire();
1970 mNetTransitionWakeLockCausedBy = forWhom;
1971 }
1972 mHandler.sendMessageDelayed(mHandler.obtainMessage(
Robert Greenwaltccb36f92010-09-24 14:32:21 -07001973 EVENT_CLEAR_NET_TRANSITION_WAKELOCK,
Robert Greenwalt93dc1042010-06-15 12:19:37 -07001974 mNetTransitionWakeLockSerialNumber, 0),
1975 mNetTransitionWakeLockTimeout);
1976 return;
1977 }
Robert Greenwalt24118e82010-09-09 13:15:32 -07001978
Robert Greenwalt986c7412010-09-08 15:24:47 -07001979 // 100 percent is full good, 0 is full bad.
1980 public void reportInetCondition(int networkType, int percentage) {
1981 if (DBG) Slog.d(TAG, "reportNetworkCondition(" + networkType + ", " + percentage + ")");
1982 mContext.enforceCallingOrSelfPermission(
1983 android.Manifest.permission.STATUS_BAR,
1984 "ConnectivityService");
1985
Robert Greenwalt0e80be12010-09-20 14:35:25 -07001986 if (DBG) {
1987 int pid = getCallingPid();
1988 int uid = getCallingUid();
1989 String s = pid + "(" + uid + ") reports inet is " +
1990 (percentage > 50 ? "connected" : "disconnected") + " (" + percentage + ") on " +
1991 "network Type " + networkType + " at " + GregorianCalendar.getInstance().getTime();
1992 mInetLog.add(s);
1993 while(mInetLog.size() > INET_CONDITION_LOG_MAX_SIZE) {
1994 mInetLog.remove(0);
1995 }
1996 }
Robert Greenwalt986c7412010-09-08 15:24:47 -07001997 mHandler.sendMessage(mHandler.obtainMessage(
Robert Greenwalt6a2db8a2010-09-23 10:05:56 -07001998 EVENT_INET_CONDITION_CHANGE, networkType, percentage));
1999 }
2000
2001 private void handleInetConditionChange(int netType, int condition) {
2002 if (DBG) {
2003 Slog.d(TAG, "Inet connectivity change, net=" +
2004 netType + ", condition=" + condition +
2005 ",mActiveDefaultNetwork=" + mActiveDefaultNetwork);
2006 }
2007 if (mActiveDefaultNetwork == -1) {
2008 if (DBG) Slog.d(TAG, "no active default network - aborting");
2009 return;
2010 }
2011 if (mActiveDefaultNetwork != netType) {
2012 if (DBG) Slog.d(TAG, "given net not default - aborting");
2013 return;
2014 }
2015 mDefaultInetCondition = condition;
2016 int delay;
2017 if (mInetConditionChangeInFlight == false) {
2018 if (DBG) Slog.d(TAG, "starting a change hold");
2019 // setup a new hold to debounce this
2020 if (mDefaultInetCondition > 50) {
2021 delay = Settings.Secure.getInt(mContext.getContentResolver(),
2022 Settings.Secure.INET_CONDITION_DEBOUNCE_UP_DELAY, 500);
2023 } else {
2024 delay = Settings.Secure.getInt(mContext.getContentResolver(),
2025 Settings.Secure.INET_CONDITION_DEBOUNCE_DOWN_DELAY, 3000);
2026 }
2027 mInetConditionChangeInFlight = true;
2028 mHandler.sendMessageDelayed(mHandler.obtainMessage(EVENT_INET_CONDITION_HOLD_END,
2029 mActiveDefaultNetwork, mDefaultConnectionSequence), delay);
2030 } else {
2031 // we've set the new condition, when this hold ends that will get
2032 // picked up
2033 if (DBG) Slog.d(TAG, "currently in hold - not setting new end evt");
2034 }
2035 }
2036
2037 private void handleInetConditionHoldEnd(int netType, int sequence) {
2038 if (DBG) {
2039 Slog.d(TAG, "Inet hold end, net=" + netType +
2040 ", condition =" + mDefaultInetCondition +
2041 ", published condition =" + mDefaultInetConditionPublished);
2042 }
2043 mInetConditionChangeInFlight = false;
2044
2045 if (mActiveDefaultNetwork == -1) {
2046 if (DBG) Slog.d(TAG, "no active default network - aborting");
2047 return;
2048 }
2049 if (mDefaultConnectionSequence != sequence) {
2050 if (DBG) Slog.d(TAG, "event hold for obsolete network - aborting");
2051 return;
2052 }
2053 if (mDefaultInetConditionPublished == mDefaultInetCondition) {
2054 if (DBG) Slog.d(TAG, "no change in condition - aborting");
2055 return;
2056 }
2057 NetworkInfo networkInfo = mNetTrackers[mActiveDefaultNetwork].getNetworkInfo();
2058 if (networkInfo.isConnected() == false) {
2059 if (DBG) Slog.d(TAG, "default network not connected - aborting");
2060 return;
2061 }
2062 mDefaultInetConditionPublished = mDefaultInetCondition;
2063 sendInetConditionBroadcast(networkInfo);
2064 return;
Robert Greenwalt986c7412010-09-08 15:24:47 -07002065 }
Robert Greenwaltc3c5f862010-10-11 16:00:27 -07002066
2067 public synchronized ProxyProperties getProxy() {
2068 if (mGlobalProxy != null) return mGlobalProxy;
2069 if (mDefaultProxy != null) return mDefaultProxy;
2070 return null;
2071 }
2072
2073 public void setGlobalProxy(ProxyProperties proxyProperties) {
2074 enforceChangePermission();
2075 synchronized (mGlobalProxyLock) {
2076 if (proxyProperties == mGlobalProxy) return;
2077 if (proxyProperties != null && proxyProperties.equals(mGlobalProxy)) return;
2078 if (mGlobalProxy != null && mGlobalProxy.equals(proxyProperties)) return;
2079
2080 String host = "";
2081 int port = 0;
2082 String exclList = "";
2083 if (proxyProperties != null && !TextUtils.isEmpty(proxyProperties.getHost())) {
2084 mGlobalProxy = new ProxyProperties(proxyProperties);
2085 host = mGlobalProxy.getHost();
2086 port = mGlobalProxy.getPort();
2087 exclList = mGlobalProxy.getExclusionList();
2088 } else {
2089 mGlobalProxy = null;
2090 }
2091 ContentResolver res = mContext.getContentResolver();
2092 Settings.Secure.putString(res, Settings.Secure.GLOBAL_HTTP_PROXY_HOST, host);
2093 Settings.Secure.putInt(res, Settings.Secure.GLOBAL_HTTP_PROXY_PORT, port);
Robert Greenwalt6f7c6092010-12-02 11:31:00 -08002094 Settings.Secure.putString(res, Settings.Secure.GLOBAL_HTTP_PROXY_EXCLUSION_LIST,
Robert Greenwaltc3c5f862010-10-11 16:00:27 -07002095 exclList);
2096 }
2097
2098 if (mGlobalProxy == null) {
2099 proxyProperties = mDefaultProxy;
2100 }
2101 sendProxyBroadcast(proxyProperties);
2102 }
2103
Robert Greenwalt6f7c6092010-12-02 11:31:00 -08002104 private void loadGlobalProxy() {
2105 ContentResolver res = mContext.getContentResolver();
2106 String host = Settings.Secure.getString(res, Settings.Secure.GLOBAL_HTTP_PROXY_HOST);
2107 int port = Settings.Secure.getInt(res, Settings.Secure.GLOBAL_HTTP_PROXY_PORT, 0);
2108 String exclList = Settings.Secure.getString(res,
2109 Settings.Secure.GLOBAL_HTTP_PROXY_EXCLUSION_LIST);
2110 if (!TextUtils.isEmpty(host)) {
2111 ProxyProperties proxyProperties = new ProxyProperties(host, port, exclList);
2112 synchronized (mGlobalProxyLock) {
2113 mGlobalProxy = proxyProperties;
2114 }
2115 }
2116 }
2117
Robert Greenwaltc3c5f862010-10-11 16:00:27 -07002118 public ProxyProperties getGlobalProxy() {
2119 synchronized (mGlobalProxyLock) {
2120 return mGlobalProxy;
2121 }
2122 }
2123
2124 private void handleApplyDefaultProxy(int type) {
2125 // check if new default - push it out to all VM if so
2126 ProxyProperties proxy = mNetTrackers[type].getLinkProperties().getHttpProxy();
2127 synchronized (this) {
2128 if (mDefaultProxy != null && mDefaultProxy.equals(proxy)) return;
2129 if (mDefaultProxy == proxy) return;
2130 if (!TextUtils.isEmpty(proxy.getHost())) {
2131 mDefaultProxy = proxy;
2132 } else {
2133 mDefaultProxy = null;
2134 }
2135 }
2136 if (DBG) Slog.d(TAG, "changing default proxy to " + proxy);
2137 if ((proxy == null && mGlobalProxy == null) || proxy.equals(mGlobalProxy)) return;
2138 if (mGlobalProxy != null) return;
2139 sendProxyBroadcast(proxy);
2140 }
2141
2142 private void handleDeprecatedGlobalHttpProxy() {
2143 String proxy = Settings.Secure.getString(mContext.getContentResolver(),
2144 Settings.Secure.HTTP_PROXY);
2145 if (!TextUtils.isEmpty(proxy)) {
2146 String data[] = proxy.split(":");
2147 String proxyHost = data[0];
2148 int proxyPort = 8080;
2149 if (data.length > 1) {
2150 try {
2151 proxyPort = Integer.parseInt(data[1]);
2152 } catch (NumberFormatException e) {
2153 return;
2154 }
2155 }
2156 ProxyProperties p = new ProxyProperties(data[0], proxyPort, "");
2157 setGlobalProxy(p);
2158 }
2159 }
2160
2161 private void sendProxyBroadcast(ProxyProperties proxy) {
2162 Slog.d(TAG, "sending Proxy Broadcast for " + proxy);
2163 Intent intent = new Intent(Proxy.PROXY_CHANGE_ACTION);
2164 intent.addFlags(Intent.FLAG_RECEIVER_REPLACE_PENDING);
2165 intent.putExtra(Proxy.EXTRA_PROXY_INFO, proxy);
2166 mContext.sendBroadcast(intent);
2167 }
2168
2169 private static class SettingsObserver extends ContentObserver {
2170 private int mWhat;
2171 private Handler mHandler;
2172 SettingsObserver(Handler handler, int what) {
2173 super(handler);
2174 mHandler = handler;
2175 mWhat = what;
2176 }
2177
2178 void observe(Context context) {
2179 ContentResolver resolver = context.getContentResolver();
2180 resolver.registerContentObserver(Settings.Secure.getUriFor(
2181 Settings.Secure.HTTP_PROXY), false, this);
2182 }
2183
2184 @Override
2185 public void onChange(boolean selfChange) {
2186 mHandler.obtainMessage(mWhat).sendToTarget();
2187 }
2188 }
The Android Open Source Project28527d22009-03-03 19:31:44 -08002189}