blob: feb7b63bd99cd1f3465796c5556998b7cf21767a [file] [log] [blame]
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001/*
2 * Copyright (C) 2008 The Android Open Source Project
3 *
4 * Licensed under the Apache License, Version 2.0 (the "License");
5 * you may not use this file except in compliance with the License.
6 * You may obtain a copy of the License at
7 *
8 * http://www.apache.org/licenses/LICENSE-2.0
9 *
10 * Unless required by applicable law or agreed to in writing, software
11 * distributed under the License is distributed on an "AS IS" BASIS,
12 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13 * See the License for the specific language governing permissions and
14 * limitations under the License.
15 */
16
17package com.android.server;
18
Jaikumar Ganesh15c74392010-12-21 22:31:44 -080019import android.bluetooth.BluetoothTetheringDataTracker;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080020import android.content.ContentResolver;
21import android.content.Context;
22import android.content.Intent;
23import android.content.pm.PackageManager;
Robert Greenwalt434203a2010-10-11 16:00:27 -070024import android.database.ContentObserver;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080025import android.net.ConnectivityManager;
Robert Greenwaltda3d5e62010-12-06 13:56:24 -080026import android.net.DummyDataStateTracker;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080027import android.net.IConnectivityManager;
Jaikumar Ganesh15c74392010-12-21 22:31:44 -080028import android.net.LinkProperties;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080029import android.net.MobileDataStateTracker;
30import android.net.NetworkInfo;
31import android.net.NetworkStateTracker;
Robert Greenwalt585ac0f2010-08-27 09:24:29 -070032import android.net.NetworkUtils;
Robert Greenwalt434203a2010-10-11 16:00:27 -070033import android.net.Proxy;
34import android.net.ProxyProperties;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080035import android.net.wifi.WifiStateTracker;
36import android.os.Binder;
37import android.os.Handler;
Wink Savillebb08caf2010-09-02 19:23:52 -070038import android.os.HandlerThread;
Robert Greenwalt42acef32009-08-12 16:08:25 -070039import android.os.IBinder;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080040import android.os.Looper;
41import android.os.Message;
Robert Greenwalt14f2ef42010-06-15 12:19:37 -070042import android.os.PowerManager;
Robert Greenwalt42acef32009-08-12 16:08:25 -070043import android.os.RemoteException;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080044import android.os.ServiceManager;
45import android.os.SystemProperties;
46import android.provider.Settings;
Robert Greenwalt42acef32009-08-12 16:08:25 -070047import android.text.TextUtils;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080048import android.util.EventLog;
Joe Onorato8a9b2202010-02-26 18:56:32 -080049import android.util.Slog;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080050
Robert Greenwalt42acef32009-08-12 16:08:25 -070051import com.android.internal.telephony.Phone;
Robert Greenwaltd0e18ff2010-01-26 11:40:34 -080052import com.android.server.connectivity.Tethering;
53
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080054import java.io.FileDescriptor;
Irfan Sheriffd649c122010-06-09 15:39:36 -070055import java.io.FileWriter;
56import java.io.IOException;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080057import java.io.PrintWriter;
Robert Greenwalt47f69fe2010-06-15 15:43:39 -070058import java.net.InetAddress;
59import java.net.UnknownHostException;
Robert Greenwalt42acef32009-08-12 16:08:25 -070060import java.util.ArrayList;
Robert Greenwalt47f69fe2010-06-15 15:43:39 -070061import java.util.Collection;
Robert Greenwalt4e8dfef2010-09-20 14:35:25 -070062import java.util.GregorianCalendar;
Robert Greenwalt42acef32009-08-12 16:08:25 -070063import java.util.List;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080064
65/**
66 * @hide
67 */
68public class ConnectivityService extends IConnectivityManager.Stub {
69
Robert Greenwaltba175a52010-10-05 19:12:26 -070070 private static final boolean DBG = true;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080071 private static final String TAG = "ConnectivityService";
72
Robert Greenwalt42acef32009-08-12 16:08:25 -070073 // how long to wait before switching back to a radio's default network
74 private static final int RESTORE_DEFAULT_NETWORK_DELAY = 1 * 60 * 1000;
75 // system property that can override the above value
76 private static final String NETWORK_RESTORE_DELAY_PROP_NAME =
77 "android.telephony.apn-restore";
78
Robert Greenwaltd0e18ff2010-01-26 11:40:34 -080079 private Tethering mTethering;
Robert Greenwaltc9d5fb72010-02-25 12:29:30 -080080 private boolean mTetheringConfigValid = false;
Robert Greenwaltd0e18ff2010-01-26 11:40:34 -080081
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080082 /**
83 * Sometimes we want to refer to the individual network state
84 * trackers separately, and sometimes we just want to treat them
85 * abstractly.
86 */
87 private NetworkStateTracker mNetTrackers[];
Robert Greenwalt42acef32009-08-12 16:08:25 -070088
89 /**
90 * A per Net list of the PID's that requested access to the net
91 * used both as a refcount and for per-PID DNS selection
92 */
93 private List mNetRequestersPids[];
94
Irfan Sheriffa2a1b912010-06-07 09:03:04 -070095 private WifiWatchdogService mWifiWatchdogService;
96
Robert Greenwalt42acef32009-08-12 16:08:25 -070097 // priority order of the nettrackers
98 // (excluding dynamically set mNetworkPreference)
99 // TODO - move mNetworkTypePreference into this
100 private int[] mPriorityList;
101
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800102 private Context mContext;
103 private int mNetworkPreference;
Robert Greenwalt42acef32009-08-12 16:08:25 -0700104 private int mActiveDefaultNetwork = -1;
Robert Greenwaltd7085fc2010-09-08 15:24:47 -0700105 // 0 is full bad, 100 is full good
106 private int mDefaultInetCondition = 0;
107 private int mDefaultInetConditionPublished = 0;
108 private boolean mInetConditionChangeInFlight = false;
109 private int mDefaultConnectionSequence = 0;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800110
111 private int mNumDnsEntries;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800112
113 private boolean mTestMode;
Joe Onorato00092872010-09-01 21:18:22 -0700114 private static ConnectivityService sServiceInstance;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800115
Robert Greenwalt8dcc28b2010-09-23 10:05:56 -0700116 private static final int ENABLED = 1;
117 private static final int DISABLED = 0;
118
119 // Share the event space with NetworkStateTracker (which can't see this
120 // internal class but sends us events). If you change these, change
121 // NetworkStateTracker.java too.
122 private static final int MIN_NETWORK_STATE_TRACKER_EVENT = 1;
123 private static final int MAX_NETWORK_STATE_TRACKER_EVENT = 100;
124
125 /**
126 * used internally as a delayed event to make us switch back to the
127 * default network
128 */
129 private static final int EVENT_RESTORE_DEFAULT_NETWORK =
130 MAX_NETWORK_STATE_TRACKER_EVENT + 1;
131
132 /**
133 * used internally to change our mobile data enabled flag
134 */
135 private static final int EVENT_CHANGE_MOBILE_DATA_ENABLED =
136 MAX_NETWORK_STATE_TRACKER_EVENT + 2;
137
138 /**
139 * used internally to change our network preference setting
140 * arg1 = networkType to prefer
141 */
142 private static final int EVENT_SET_NETWORK_PREFERENCE =
143 MAX_NETWORK_STATE_TRACKER_EVENT + 3;
144
145 /**
146 * used internally to synchronize inet condition reports
147 * arg1 = networkType
148 * arg2 = condition (0 bad, 100 good)
149 */
150 private static final int EVENT_INET_CONDITION_CHANGE =
151 MAX_NETWORK_STATE_TRACKER_EVENT + 4;
152
153 /**
154 * used internally to mark the end of inet condition hold periods
155 * arg1 = networkType
156 */
157 private static final int EVENT_INET_CONDITION_HOLD_END =
158 MAX_NETWORK_STATE_TRACKER_EVENT + 5;
159
160 /**
161 * used internally to set the background data preference
162 * arg1 = TRUE for enabled, FALSE for disabled
163 */
164 private static final int EVENT_SET_BACKGROUND_DATA =
165 MAX_NETWORK_STATE_TRACKER_EVENT + 6;
166
167 /**
168 * used internally to set enable/disable cellular data
169 * arg1 = ENBALED or DISABLED
170 */
171 private static final int EVENT_SET_MOBILE_DATA =
172 MAX_NETWORK_STATE_TRACKER_EVENT + 7;
173
Robert Greenwaltf3331232010-09-24 14:32:21 -0700174 /**
175 * used internally to clear a wakelock when transitioning
176 * from one net to another
177 */
178 private static final int EVENT_CLEAR_NET_TRANSITION_WAKELOCK =
179 MAX_NETWORK_STATE_TRACKER_EVENT + 8;
180
Robert Greenwalt434203a2010-10-11 16:00:27 -0700181 /**
182 * used internally to reload global proxy settings
183 */
184 private static final int EVENT_APPLY_GLOBAL_HTTP_PROXY =
185 MAX_NETWORK_STATE_TRACKER_EVENT + 9;
186
Robert Greenwalt42acef32009-08-12 16:08:25 -0700187 private Handler mHandler;
188
189 // list of DeathRecipients used to make sure features are turned off when
190 // a process dies
191 private List mFeatureUsers;
192
Mike Lockwood0f79b542009-08-14 14:18:49 -0400193 private boolean mSystemReady;
Dianne Hackborn1c633fc2009-12-08 19:45:14 -0800194 private Intent mInitialBroadcast;
Mike Lockwood0f79b542009-08-14 14:18:49 -0400195
Robert Greenwalt14f2ef42010-06-15 12:19:37 -0700196 private PowerManager.WakeLock mNetTransitionWakeLock;
197 private String mNetTransitionWakeLockCausedBy = "";
198 private int mNetTransitionWakeLockSerialNumber;
199 private int mNetTransitionWakeLockTimeout;
200
Robert Greenwalte90aa5e2010-09-01 11:34:05 -0700201 private InetAddress mDefaultDns;
202
Robert Greenwalt4e8dfef2010-09-20 14:35:25 -0700203 // used in DBG mode to track inet condition reports
204 private static final int INET_CONDITION_LOG_MAX_SIZE = 15;
205 private ArrayList mInetLog;
206
Robert Greenwalt434203a2010-10-11 16:00:27 -0700207 // track the current default http proxy - tell the world if we get a new one (real change)
208 private ProxyProperties mDefaultProxy = null;
209 // track the global proxy.
210 private ProxyProperties mGlobalProxy = null;
211 private final Object mGlobalProxyLock = new Object();
212
213 private SettingsObserver mSettingsObserver;
214
Robert Greenwalt511288a2009-12-07 11:33:18 -0800215 private static class NetworkAttributes {
Robert Greenwalt42acef32009-08-12 16:08:25 -0700216 /**
217 * Class for holding settings read from resources.
218 */
219 public String mName;
220 public int mType;
221 public int mRadio;
222 public int mPriority;
Robert Greenwalt511288a2009-12-07 11:33:18 -0800223 public NetworkInfo.State mLastState;
Robert Greenwalt42acef32009-08-12 16:08:25 -0700224 public NetworkAttributes(String init) {
225 String fragments[] = init.split(",");
226 mName = fragments[0].toLowerCase();
Robert Greenwalt5154ae762009-10-30 14:17:42 -0700227 mType = Integer.parseInt(fragments[1]);
228 mRadio = Integer.parseInt(fragments[2]);
229 mPriority = Integer.parseInt(fragments[3]);
Robert Greenwalt511288a2009-12-07 11:33:18 -0800230 mLastState = NetworkInfo.State.UNKNOWN;
Robert Greenwalt42acef32009-08-12 16:08:25 -0700231 }
232 public boolean isDefault() {
233 return (mType == mRadio);
234 }
235 }
236 NetworkAttributes[] mNetAttributes;
Robert Greenwalt5154ae762009-10-30 14:17:42 -0700237 int mNetworksDefined;
Robert Greenwalt42acef32009-08-12 16:08:25 -0700238
Robert Greenwalt511288a2009-12-07 11:33:18 -0800239 private static class RadioAttributes {
Robert Greenwalt42acef32009-08-12 16:08:25 -0700240 public int mSimultaneity;
241 public int mType;
242 public RadioAttributes(String init) {
243 String fragments[] = init.split(",");
Robert Greenwalt5154ae762009-10-30 14:17:42 -0700244 mType = Integer.parseInt(fragments[0]);
245 mSimultaneity = Integer.parseInt(fragments[1]);
Robert Greenwalt42acef32009-08-12 16:08:25 -0700246 }
247 }
248 RadioAttributes[] mRadioAttributes;
249
Wink Savillebb08caf2010-09-02 19:23:52 -0700250 public static synchronized ConnectivityService getInstance(Context context) {
251 if (sServiceInstance == null) {
252 sServiceInstance = new ConnectivityService(context);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800253 }
Wink Savillebb08caf2010-09-02 19:23:52 -0700254 return sServiceInstance;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800255 }
Robert Greenwalt86e9e552009-07-16 17:21:39 -0700256
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800257 private ConnectivityService(Context context) {
Wink Savilleed9c02b2010-12-03 12:01:38 -0800258 if (DBG) log("ConnectivityService starting up");
Robert Greenwaltde8383c2010-01-14 17:47:58 -0800259
Wink Savillebb08caf2010-09-02 19:23:52 -0700260 HandlerThread handlerThread = new HandlerThread("ConnectivityServiceThread");
261 handlerThread.start();
262 mHandler = new MyHandler(handlerThread.getLooper());
263
Robert Greenwaltde8383c2010-01-14 17:47:58 -0800264 // setup our unique device name
Robert Greenwalt733c6292010-12-06 09:30:17 -0800265 if (TextUtils.isEmpty(SystemProperties.get("net.hostname"))) {
266 String id = Settings.Secure.getString(context.getContentResolver(),
267 Settings.Secure.ANDROID_ID);
268 if (id != null && id.length() > 0) {
269 String name = new String("android_").concat(id);
270 SystemProperties.set("net.hostname", name);
271 }
Robert Greenwaltde8383c2010-01-14 17:47:58 -0800272 }
273
Robert Greenwalte90aa5e2010-09-01 11:34:05 -0700274 // read our default dns server ip
275 String dns = Settings.Secure.getString(context.getContentResolver(),
276 Settings.Secure.DEFAULT_DNS_SERVER);
277 if (dns == null || dns.length() == 0) {
278 dns = context.getResources().getString(
279 com.android.internal.R.string.config_default_dns_server);
280 }
281 try {
282 mDefaultDns = InetAddress.getByName(dns);
283 } catch (UnknownHostException e) {
Wink Savilleed9c02b2010-12-03 12:01:38 -0800284 loge("Error setting defaultDns using " + dns);
Robert Greenwalte90aa5e2010-09-01 11:34:05 -0700285 }
286
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800287 mContext = context;
Robert Greenwalt14f2ef42010-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 Greenwalt42acef32009-08-12 16:08:25 -0700294 mNetTrackers = new NetworkStateTracker[
295 ConnectivityManager.MAX_NETWORK_TYPE+1];
Robert Greenwalt86e9e552009-07-16 17:21:39 -0700296
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800297 mNetworkPreference = getPersistedNetworkPreference();
Robert Greenwalt86e9e552009-07-16 17:21:39 -0700298
Robert Greenwalt5154ae762009-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 Greenwalt42acef32009-08-12 16:08:25 -0700302 // Load device network attributes from resources
Robert Greenwalt42acef32009-08-12 16:08:25 -0700303 String[] raStrings = context.getResources().getStringArray(
304 com.android.internal.R.array.radioAttributes);
Robert Greenwalt5154ae762009-10-30 14:17:42 -0700305 for (String raString : raStrings) {
306 RadioAttributes r = new RadioAttributes(raString);
307 if (r.mType > ConnectivityManager.MAX_RADIO_TYPE) {
Wink Savilleed9c02b2010-12-03 12:01:38 -0800308 loge("Error in radioAttributes - ignoring attempt to define type " + r.mType);
Robert Greenwalt5154ae762009-10-30 14:17:42 -0700309 continue;
310 }
311 if (mRadioAttributes[r.mType] != null) {
Wink Savilleed9c02b2010-12-03 12:01:38 -0800312 loge("Error in radioAttributes - ignoring attempt to redefine type " +
Robert Greenwalt5154ae762009-10-30 14:17:42 -0700313 r.mType);
314 continue;
315 }
Robert Greenwalt42acef32009-08-12 16:08:25 -0700316 mRadioAttributes[r.mType] = r;
317 }
318
Robert Greenwalt5154ae762009-10-30 14:17:42 -0700319 String[] naStrings = context.getResources().getStringArray(
320 com.android.internal.R.array.networkAttributes);
321 for (String naString : naStrings) {
322 try {
323 NetworkAttributes n = new NetworkAttributes(naString);
324 if (n.mType > ConnectivityManager.MAX_NETWORK_TYPE) {
Wink Savilleed9c02b2010-12-03 12:01:38 -0800325 loge("Error in networkAttributes - ignoring attempt to define type " +
Robert Greenwalt5154ae762009-10-30 14:17:42 -0700326 n.mType);
327 continue;
Robert Greenwalt42acef32009-08-12 16:08:25 -0700328 }
Robert Greenwalt5154ae762009-10-30 14:17:42 -0700329 if (mNetAttributes[n.mType] != null) {
Wink Savilleed9c02b2010-12-03 12:01:38 -0800330 loge("Error in networkAttributes - ignoring attempt to redefine type " +
Robert Greenwalt5154ae762009-10-30 14:17:42 -0700331 n.mType);
332 continue;
333 }
334 if (mRadioAttributes[n.mRadio] == null) {
Wink Savilleed9c02b2010-12-03 12:01:38 -0800335 loge("Error in networkAttributes - ignoring attempt to use undefined " +
Robert Greenwalt5154ae762009-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 Greenwalt42acef32009-08-12 16:08:25 -0700343 }
344 }
345
Robert Greenwalt5154ae762009-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 Greenwalt42acef32009-08-12 16:08:25 -0700371 mNetRequestersPids[i] = new ArrayList();
372 }
373
374 mFeatureUsers = new ArrayList();
375
Robert Greenwalt5154ae762009-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 Project9066cfe2009-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 Greenwalt5154ae762009-10-30 14:17:42 -0700387 for (int netType : mPriorityList) {
388 switch (mNetAttributes[netType].mRadio) {
389 case ConnectivityManager.TYPE_WIFI:
Wink Savilleed9c02b2010-12-03 12:01:38 -0800390 if (DBG) log("Starting Wifi Service.");
Wink Savillec7a98342010-08-13 16:11:42 -0700391 WifiStateTracker wst = new WifiStateTracker();
Irfan Sheriff0d255342010-07-28 09:35:20 -0700392 WifiService wifiService = new WifiService(context);
Robert Greenwalt5154ae762009-10-30 14:17:42 -0700393 ServiceManager.addService(Context.WIFI_SERVICE, wifiService);
Irfan Sheriff0d255342010-07-28 09:35:20 -0700394 wifiService.checkAndStartWifi();
Robert Greenwalt5154ae762009-10-30 14:17:42 -0700395 mNetTrackers[ConnectivityManager.TYPE_WIFI] = wst;
Wink Savillec7a98342010-08-13 16:11:42 -0700396 wst.startMonitoring(context, mHandler);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800397
Irfan Sheriffa2a1b912010-06-07 09:03:04 -0700398 //TODO: as part of WWS refactor, create only when needed
Irfan Sheriff0d255342010-07-28 09:35:20 -0700399 mWifiWatchdogService = new WifiWatchdogService(context);
Irfan Sheriffa2a1b912010-06-07 09:03:04 -0700400
Robert Greenwalt5154ae762009-10-30 14:17:42 -0700401 break;
402 case ConnectivityManager.TYPE_MOBILE:
Wink Savillec7a98342010-08-13 16:11:42 -0700403 mNetTrackers[netType] = new MobileDataStateTracker(netType,
404 mNetAttributes[netType].mName);
405 mNetTrackers[netType].startMonitoring(context, mHandler);
Robert Greenwalt5154ae762009-10-30 14:17:42 -0700406 break;
Robert Greenwaltda3d5e62010-12-06 13:56:24 -0800407 case ConnectivityManager.TYPE_DUMMY:
408 mNetTrackers[netType] = new DummyDataStateTracker(netType,
409 mNetAttributes[netType].mName);
410 mNetTrackers[netType].startMonitoring(context, mHandler);
411 break;
Jaikumar Ganesh15c74392010-12-21 22:31:44 -0800412 case ConnectivityManager.TYPE_BLUETOOTH:
413 mNetTrackers[netType] = BluetoothTetheringDataTracker.getInstance();
414 mNetTrackers[netType].startMonitoring(context, mHandler);
415 break;
Robert Greenwalt5154ae762009-10-30 14:17:42 -0700416 default:
Wink Savilleed9c02b2010-12-03 12:01:38 -0800417 loge("Trying to create a DataStateTracker for an unknown radio type " +
Robert Greenwalt5154ae762009-10-30 14:17:42 -0700418 mNetAttributes[netType].mRadio);
419 continue;
420 }
421 }
Robert Greenwaltd0e18ff2010-01-26 11:40:34 -0800422
Robert Greenwaltdfadaea2010-03-11 15:03:08 -0800423 mTethering = new Tethering(mContext, mHandler.getLooper());
Robert Greenwaltc9d5fb72010-02-25 12:29:30 -0800424 mTetheringConfigValid = (((mNetTrackers[ConnectivityManager.TYPE_MOBILE_DUN] != null) ||
425 !mTethering.isDunRequired()) &&
426 (mTethering.getTetherableUsbRegexs().length != 0 ||
Danica Chang6fdd0c62010-08-11 14:54:43 -0700427 mTethering.getTetherableWifiRegexs().length != 0 ||
428 mTethering.getTetherableBluetoothRegexs().length != 0) &&
Robert Greenwaltc9d5fb72010-02-25 12:29:30 -0800429 mTethering.getUpstreamIfaceRegexs().length != 0);
430
Robert Greenwalt4e8dfef2010-09-20 14:35:25 -0700431 if (DBG) {
432 mInetLog = new ArrayList();
433 }
Robert Greenwalt434203a2010-10-11 16:00:27 -0700434
435 mSettingsObserver = new SettingsObserver(mHandler, EVENT_APPLY_GLOBAL_HTTP_PROXY);
436 mSettingsObserver.observe(mContext);
Robert Greenwaltb7090d62010-12-02 11:31:00 -0800437
438 loadGlobalProxy();
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800439 }
440
Robert Greenwalt5154ae762009-10-30 14:17:42 -0700441
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800442 /**
Robert Greenwalt86e9e552009-07-16 17:21:39 -0700443 * Sets the preferred network.
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800444 * @param preference the new preference
445 */
Robert Greenwalt8dcc28b2010-09-23 10:05:56 -0700446 public void setNetworkPreference(int preference) {
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800447 enforceChangePermission();
Robert Greenwalt8dcc28b2010-09-23 10:05:56 -0700448
449 mHandler.sendMessage(mHandler.obtainMessage(EVENT_SET_NETWORK_PREFERENCE, preference, 0));
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800450 }
451
452 public int getNetworkPreference() {
453 enforceAccessPermission();
Robert Greenwalt8dcc28b2010-09-23 10:05:56 -0700454 int preference;
455 synchronized(this) {
456 preference = mNetworkPreference;
457 }
458 return preference;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800459 }
460
Robert Greenwalt8dcc28b2010-09-23 10:05:56 -0700461 private void handleSetNetworkPreference(int preference) {
462 if (ConnectivityManager.isNetworkTypeValid(preference) &&
463 mNetAttributes[preference] != null &&
464 mNetAttributes[preference].isDefault()) {
465 if (mNetworkPreference != preference) {
466 final ContentResolver cr = mContext.getContentResolver();
467 Settings.Secure.putInt(cr, Settings.Secure.NETWORK_PREFERENCE, preference);
468 synchronized(this) {
469 mNetworkPreference = preference;
470 }
471 enforcePreference();
472 }
473 }
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800474 }
Robert Greenwalt86e9e552009-07-16 17:21:39 -0700475
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800476 private int getPersistedNetworkPreference() {
477 final ContentResolver cr = mContext.getContentResolver();
478
479 final int networkPrefSetting = Settings.Secure
480 .getInt(cr, Settings.Secure.NETWORK_PREFERENCE, -1);
481 if (networkPrefSetting != -1) {
482 return networkPrefSetting;
483 }
484
485 return ConnectivityManager.DEFAULT_NETWORK_PREFERENCE;
486 }
Robert Greenwalt86e9e552009-07-16 17:21:39 -0700487
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800488 /**
Robert Greenwalt86e9e552009-07-16 17:21:39 -0700489 * Make the state of network connectivity conform to the preference settings
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800490 * In this method, we only tear down a non-preferred network. Establishing
491 * a connection to the preferred network is taken care of when we handle
492 * the disconnect event from the non-preferred network
493 * (see {@link #handleDisconnect(NetworkInfo)}).
494 */
495 private void enforcePreference() {
Robert Greenwalt42acef32009-08-12 16:08:25 -0700496 if (mNetTrackers[mNetworkPreference].getNetworkInfo().isConnected())
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800497 return;
498
Robert Greenwalt42acef32009-08-12 16:08:25 -0700499 if (!mNetTrackers[mNetworkPreference].isAvailable())
500 return;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800501
Robert Greenwalt42acef32009-08-12 16:08:25 -0700502 for (int t=0; t <= ConnectivityManager.MAX_RADIO_TYPE; t++) {
Robert Greenwalt5154ae762009-10-30 14:17:42 -0700503 if (t != mNetworkPreference && mNetTrackers[t] != null &&
Robert Greenwalt42acef32009-08-12 16:08:25 -0700504 mNetTrackers[t].getNetworkInfo().isConnected()) {
Robert Greenwaltec9fe462009-08-20 15:25:14 -0700505 if (DBG) {
Wink Savilleed9c02b2010-12-03 12:01:38 -0800506 log("tearing down " + mNetTrackers[t].getNetworkInfo() +
Robert Greenwaltec9fe462009-08-20 15:25:14 -0700507 " in enforcePreference");
508 }
Robert Greenwalt42acef32009-08-12 16:08:25 -0700509 teardown(mNetTrackers[t]);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800510 }
511 }
512 }
513
514 private boolean teardown(NetworkStateTracker netTracker) {
515 if (netTracker.teardown()) {
516 netTracker.setTeardownRequested(true);
517 return true;
518 } else {
519 return false;
520 }
521 }
522
523 /**
524 * Return NetworkInfo for the active (i.e., connected) network interface.
525 * It is assumed that at most one network is active at a time. If more
526 * than one is active, it is indeterminate which will be returned.
Robert Greenwalt86e9e552009-07-16 17:21:39 -0700527 * @return the info for the active network, or {@code null} if none is
528 * active
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800529 */
530 public NetworkInfo getActiveNetworkInfo() {
531 enforceAccessPermission();
Robert Greenwalt42acef32009-08-12 16:08:25 -0700532 for (int type=0; type <= ConnectivityManager.MAX_NETWORK_TYPE; type++) {
Robert Greenwalt5154ae762009-10-30 14:17:42 -0700533 if (mNetAttributes[type] == null || !mNetAttributes[type].isDefault()) {
Robert Greenwalt42acef32009-08-12 16:08:25 -0700534 continue;
535 }
536 NetworkStateTracker t = mNetTrackers[type];
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800537 NetworkInfo info = t.getNetworkInfo();
538 if (info.isConnected()) {
Wink Savilleed9c02b2010-12-03 12:01:38 -0800539 if (DBG && type != mActiveDefaultNetwork) {
540 loge("connected default network is not mActiveDefaultNetwork!");
541 }
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800542 return info;
543 }
544 }
545 return null;
546 }
547
548 public NetworkInfo getNetworkInfo(int networkType) {
549 enforceAccessPermission();
550 if (ConnectivityManager.isNetworkTypeValid(networkType)) {
551 NetworkStateTracker t = mNetTrackers[networkType];
552 if (t != null)
553 return t.getNetworkInfo();
554 }
555 return null;
556 }
557
558 public NetworkInfo[] getAllNetworkInfo() {
559 enforceAccessPermission();
Robert Greenwalt5154ae762009-10-30 14:17:42 -0700560 NetworkInfo[] result = new NetworkInfo[mNetworksDefined];
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800561 int i = 0;
562 for (NetworkStateTracker t : mNetTrackers) {
Robert Greenwalt5154ae762009-10-30 14:17:42 -0700563 if(t != null) result[i++] = t.getNetworkInfo();
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800564 }
565 return result;
566 }
567
Robert Greenwaltd192dad2010-09-14 09:18:02 -0700568 /**
569 * Return LinkProperties for the active (i.e., connected) default
570 * network interface. It is assumed that at most one default network
571 * is active at a time. If more than one is active, it is indeterminate
572 * which will be returned.
573 * @return the ip properties for the active network, or {@code null} if
574 * none is active
575 */
576 public LinkProperties getActiveLinkProperties() {
577 enforceAccessPermission();
578 for (int type=0; type <= ConnectivityManager.MAX_NETWORK_TYPE; type++) {
579 if (mNetAttributes[type] == null || !mNetAttributes[type].isDefault()) {
580 continue;
581 }
582 NetworkStateTracker t = mNetTrackers[type];
583 NetworkInfo info = t.getNetworkInfo();
584 if (info.isConnected()) {
585 return t.getLinkProperties();
586 }
587 }
588 return null;
589 }
590
591 public LinkProperties getLinkProperties(int networkType) {
592 enforceAccessPermission();
593 if (ConnectivityManager.isNetworkTypeValid(networkType)) {
594 NetworkStateTracker t = mNetTrackers[networkType];
595 if (t != null) return t.getLinkProperties();
596 }
597 return null;
598 }
599
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800600 public boolean setRadios(boolean turnOn) {
601 boolean result = true;
602 enforceChangePermission();
603 for (NetworkStateTracker t : mNetTrackers) {
Robert Greenwalt5154ae762009-10-30 14:17:42 -0700604 if (t != null) result = t.setRadio(turnOn) && result;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800605 }
606 return result;
607 }
608
609 public boolean setRadio(int netType, boolean turnOn) {
610 enforceChangePermission();
611 if (!ConnectivityManager.isNetworkTypeValid(netType)) {
612 return false;
613 }
614 NetworkStateTracker tracker = mNetTrackers[netType];
615 return tracker != null && tracker.setRadio(turnOn);
616 }
617
Robert Greenwalt9c75d4a2009-09-27 17:27:04 -0700618 /**
619 * Used to notice when the calling process dies so we can self-expire
620 *
621 * Also used to know if the process has cleaned up after itself when
622 * our auto-expire timer goes off. The timer has a link to an object.
623 *
624 */
Robert Greenwalt42acef32009-08-12 16:08:25 -0700625 private class FeatureUser implements IBinder.DeathRecipient {
626 int mNetworkType;
627 String mFeature;
628 IBinder mBinder;
629 int mPid;
630 int mUid;
Robert Greenwaltb9285352009-12-21 18:24:07 -0800631 long mCreateTime;
Robert Greenwalt42acef32009-08-12 16:08:25 -0700632
633 FeatureUser(int type, String feature, IBinder binder) {
634 super();
635 mNetworkType = type;
636 mFeature = feature;
637 mBinder = binder;
638 mPid = getCallingPid();
639 mUid = getCallingUid();
Robert Greenwaltb9285352009-12-21 18:24:07 -0800640 mCreateTime = System.currentTimeMillis();
Robert Greenwalt9c75d4a2009-09-27 17:27:04 -0700641
Robert Greenwalt42acef32009-08-12 16:08:25 -0700642 try {
643 mBinder.linkToDeath(this, 0);
644 } catch (RemoteException e) {
645 binderDied();
646 }
647 }
648
649 void unlinkDeathRecipient() {
650 mBinder.unlinkToDeath(this, 0);
651 }
652
653 public void binderDied() {
Wink Savilleed9c02b2010-12-03 12:01:38 -0800654 log("ConnectivityService FeatureUser binderDied(" +
Robert Greenwaltb9285352009-12-21 18:24:07 -0800655 mNetworkType + ", " + mFeature + ", " + mBinder + "), created " +
656 (System.currentTimeMillis() - mCreateTime) + " mSec ago");
Robert Greenwalt9c75d4a2009-09-27 17:27:04 -0700657 stopUsingNetworkFeature(this, false);
Robert Greenwalt42acef32009-08-12 16:08:25 -0700658 }
659
Robert Greenwalt9c75d4a2009-09-27 17:27:04 -0700660 public void expire() {
Wink Savilleed9c02b2010-12-03 12:01:38 -0800661 log("ConnectivityService FeatureUser expire(" +
Robert Greenwaltb9285352009-12-21 18:24:07 -0800662 mNetworkType + ", " + mFeature + ", " + mBinder +"), created " +
663 (System.currentTimeMillis() - mCreateTime) + " mSec ago");
Robert Greenwalt9c75d4a2009-09-27 17:27:04 -0700664 stopUsingNetworkFeature(this, false);
665 }
Robert Greenwaltb9285352009-12-21 18:24:07 -0800666
667 public String toString() {
668 return "FeatureUser("+mNetworkType+","+mFeature+","+mPid+","+mUid+"), created " +
669 (System.currentTimeMillis() - mCreateTime) + " mSec ago";
670 }
Robert Greenwalt42acef32009-08-12 16:08:25 -0700671 }
672
Robert Greenwalt9c75d4a2009-09-27 17:27:04 -0700673 // javadoc from interface
Robert Greenwalt42acef32009-08-12 16:08:25 -0700674 public int startUsingNetworkFeature(int networkType, String feature,
675 IBinder binder) {
676 if (DBG) {
Wink Savilleed9c02b2010-12-03 12:01:38 -0800677 log("startUsingNetworkFeature for net " + networkType + ": " + feature);
Robert Greenwalt42acef32009-08-12 16:08:25 -0700678 }
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800679 enforceChangePermission();
Robert Greenwalt5154ae762009-10-30 14:17:42 -0700680 if (!ConnectivityManager.isNetworkTypeValid(networkType) ||
681 mNetAttributes[networkType] == null) {
Robert Greenwalt42acef32009-08-12 16:08:25 -0700682 return Phone.APN_REQUEST_FAILED;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800683 }
Robert Greenwalt42acef32009-08-12 16:08:25 -0700684
Robert Greenwalt9c75d4a2009-09-27 17:27:04 -0700685 FeatureUser f = new FeatureUser(networkType, feature, binder);
Robert Greenwalt42acef32009-08-12 16:08:25 -0700686
687 // TODO - move this into the MobileDataStateTracker
688 int usedNetworkType = networkType;
689 if(networkType == ConnectivityManager.TYPE_MOBILE) {
690 if (TextUtils.equals(feature, Phone.FEATURE_ENABLE_MMS)) {
691 usedNetworkType = ConnectivityManager.TYPE_MOBILE_MMS;
692 } else if (TextUtils.equals(feature, Phone.FEATURE_ENABLE_SUPL)) {
693 usedNetworkType = ConnectivityManager.TYPE_MOBILE_SUPL;
694 } else if (TextUtils.equals(feature, Phone.FEATURE_ENABLE_DUN)) {
695 usedNetworkType = ConnectivityManager.TYPE_MOBILE_DUN;
696 } else if (TextUtils.equals(feature, Phone.FEATURE_ENABLE_HIPRI)) {
697 usedNetworkType = ConnectivityManager.TYPE_MOBILE_HIPRI;
698 }
699 }
700 NetworkStateTracker network = mNetTrackers[usedNetworkType];
701 if (network != null) {
Robert Greenwalt0be1e982010-12-15 13:26:33 -0800702 Integer currentPid = new Integer(getCallingPid());
Robert Greenwalt42acef32009-08-12 16:08:25 -0700703 if (usedNetworkType != networkType) {
Robert Greenwalt42acef32009-08-12 16:08:25 -0700704 NetworkStateTracker radio = mNetTrackers[networkType];
705 NetworkInfo ni = network.getNetworkInfo();
706
707 if (ni.isAvailable() == false) {
Wink Savilleed9c02b2010-12-03 12:01:38 -0800708 if (DBG) log("special network not available");
Robert Greenwalt42acef32009-08-12 16:08:25 -0700709 return Phone.APN_TYPE_NOT_AVAILABLE;
710 }
711
Robert Greenwalt9c75d4a2009-09-27 17:27:04 -0700712 synchronized(this) {
713 mFeatureUsers.add(f);
714 if (!mNetRequestersPids[usedNetworkType].contains(currentPid)) {
715 // this gets used for per-pid dns when connected
716 mNetRequestersPids[usedNetworkType].add(currentPid);
717 }
Robert Greenwalt42acef32009-08-12 16:08:25 -0700718 }
Robert Greenwalt8dcc28b2010-09-23 10:05:56 -0700719 mHandler.sendMessageDelayed(mHandler.obtainMessage(EVENT_RESTORE_DEFAULT_NETWORK,
Robert Greenwalt9c75d4a2009-09-27 17:27:04 -0700720 f), getRestoreDefaultNetworkDelay());
721
Robert Greenwalt42acef32009-08-12 16:08:25 -0700722
Robert Greenwalta64bf832009-08-19 20:19:33 -0700723 if ((ni.isConnectedOrConnecting() == true) &&
724 !network.isTeardownRequested()) {
Robert Greenwalt42acef32009-08-12 16:08:25 -0700725 if (ni.isConnected() == true) {
726 // add the pid-specific dns
Robert Greenwaltc76b8fa2010-07-23 15:46:26 -0700727 handleDnsConfigurationChange(networkType);
Wink Savilleed9c02b2010-12-03 12:01:38 -0800728 if (DBG) log("special network already active");
Robert Greenwalt42acef32009-08-12 16:08:25 -0700729 return Phone.APN_ALREADY_ACTIVE;
730 }
Wink Savilleed9c02b2010-12-03 12:01:38 -0800731 if (DBG) log("special network already connecting");
Robert Greenwalt42acef32009-08-12 16:08:25 -0700732 return Phone.APN_REQUEST_STARTED;
733 }
734
735 // check if the radio in play can make another contact
736 // assume if cannot for now
737
Wink Savilleed9c02b2010-12-03 12:01:38 -0800738 if (DBG) log("reconnecting to special network");
Robert Greenwalt42acef32009-08-12 16:08:25 -0700739 network.reconnect();
740 return Phone.APN_REQUEST_STARTED;
741 } else {
Robert Greenwalt0be1e982010-12-15 13:26:33 -0800742 // need to remember this unsupported request so we respond appropriately on stop
743 synchronized(this) {
744 mFeatureUsers.add(f);
745 if (!mNetRequestersPids[usedNetworkType].contains(currentPid)) {
746 // this gets used for per-pid dns when connected
747 mNetRequestersPids[usedNetworkType].add(currentPid);
748 }
749 }
Robert Greenwalt02648a42010-05-18 10:52:51 -0700750 return -1;
Robert Greenwalt42acef32009-08-12 16:08:25 -0700751 }
752 }
753 return Phone.APN_TYPE_NOT_AVAILABLE;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800754 }
755
Robert Greenwalt9c75d4a2009-09-27 17:27:04 -0700756 // javadoc from interface
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800757 public int stopUsingNetworkFeature(int networkType, String feature) {
Robert Greenwaltb8f16342009-10-06 17:52:40 -0700758 enforceChangePermission();
759
Robert Greenwalt9c75d4a2009-09-27 17:27:04 -0700760 int pid = getCallingPid();
761 int uid = getCallingUid();
762
763 FeatureUser u = null;
764 boolean found = false;
765
766 synchronized(this) {
767 for (int i = 0; i < mFeatureUsers.size() ; i++) {
768 u = (FeatureUser)mFeatureUsers.get(i);
769 if (uid == u.mUid && pid == u.mPid &&
770 networkType == u.mNetworkType &&
771 TextUtils.equals(feature, u.mFeature)) {
772 found = true;
773 break;
774 }
775 }
776 }
777 if (found && u != null) {
778 // stop regardless of how many other time this proc had called start
779 return stopUsingNetworkFeature(u, true);
780 } else {
781 // none found!
Wink Savilleed9c02b2010-12-03 12:01:38 -0800782 if (DBG) log("ignoring stopUsingNetworkFeature - not a live request");
Robert Greenwalt9c75d4a2009-09-27 17:27:04 -0700783 return 1;
784 }
Robert Greenwalt42acef32009-08-12 16:08:25 -0700785 }
786
Robert Greenwalt9c75d4a2009-09-27 17:27:04 -0700787 private int stopUsingNetworkFeature(FeatureUser u, boolean ignoreDups) {
788 int networkType = u.mNetworkType;
789 String feature = u.mFeature;
790 int pid = u.mPid;
791 int uid = u.mUid;
792
793 NetworkStateTracker tracker = null;
794 boolean callTeardown = false; // used to carry our decision outside of sync block
795
Robert Greenwalt42acef32009-08-12 16:08:25 -0700796 if (DBG) {
Wink Savilleed9c02b2010-12-03 12:01:38 -0800797 log("stopUsingNetworkFeature for net " + networkType +
Robert Greenwalt42acef32009-08-12 16:08:25 -0700798 ": " + feature);
799 }
Robert Greenwaltb8f16342009-10-06 17:52:40 -0700800
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800801 if (!ConnectivityManager.isNetworkTypeValid(networkType)) {
802 return -1;
803 }
Robert Greenwalt42acef32009-08-12 16:08:25 -0700804
Robert Greenwalt9c75d4a2009-09-27 17:27:04 -0700805 // need to link the mFeatureUsers list with the mNetRequestersPids state in this
806 // sync block
807 synchronized(this) {
808 // check if this process still has an outstanding start request
809 if (!mFeatureUsers.contains(u)) {
Wink Savilleed9c02b2010-12-03 12:01:38 -0800810 if (DBG) log("ignoring - this process has no outstanding requests");
Robert Greenwalt42acef32009-08-12 16:08:25 -0700811 return 1;
812 }
Robert Greenwalt9c75d4a2009-09-27 17:27:04 -0700813 u.unlinkDeathRecipient();
814 mFeatureUsers.remove(mFeatureUsers.indexOf(u));
815 // If we care about duplicate requests, check for that here.
816 //
817 // This is done to support the extension of a request - the app
818 // can request we start the network feature again and renew the
819 // auto-shutoff delay. Normal "stop" calls from the app though
820 // do not pay attention to duplicate requests - in effect the
821 // API does not refcount and a single stop will counter multiple starts.
822 if (ignoreDups == false) {
823 for (int i = 0; i < mFeatureUsers.size() ; i++) {
824 FeatureUser x = (FeatureUser)mFeatureUsers.get(i);
825 if (x.mUid == u.mUid && x.mPid == u.mPid &&
826 x.mNetworkType == u.mNetworkType &&
827 TextUtils.equals(x.mFeature, u.mFeature)) {
Wink Savilleed9c02b2010-12-03 12:01:38 -0800828 if (DBG) log("ignoring stopUsingNetworkFeature as dup is found");
Robert Greenwalt9c75d4a2009-09-27 17:27:04 -0700829 return 1;
830 }
831 }
Robert Greenwalt42acef32009-08-12 16:08:25 -0700832 }
Robert Greenwalt9c75d4a2009-09-27 17:27:04 -0700833
834 // TODO - move to MobileDataStateTracker
835 int usedNetworkType = networkType;
836 if (networkType == ConnectivityManager.TYPE_MOBILE) {
837 if (TextUtils.equals(feature, Phone.FEATURE_ENABLE_MMS)) {
838 usedNetworkType = ConnectivityManager.TYPE_MOBILE_MMS;
839 } else if (TextUtils.equals(feature, Phone.FEATURE_ENABLE_SUPL)) {
840 usedNetworkType = ConnectivityManager.TYPE_MOBILE_SUPL;
841 } else if (TextUtils.equals(feature, Phone.FEATURE_ENABLE_DUN)) {
842 usedNetworkType = ConnectivityManager.TYPE_MOBILE_DUN;
843 } else if (TextUtils.equals(feature, Phone.FEATURE_ENABLE_HIPRI)) {
844 usedNetworkType = ConnectivityManager.TYPE_MOBILE_HIPRI;
845 }
846 }
847 tracker = mNetTrackers[usedNetworkType];
Robert Greenwalt5154ae762009-10-30 14:17:42 -0700848 if (tracker == null) {
Wink Savilleed9c02b2010-12-03 12:01:38 -0800849 if (DBG) log("ignoring - no known tracker for net type " + usedNetworkType);
Robert Greenwalt5154ae762009-10-30 14:17:42 -0700850 return -1;
851 }
852 if (usedNetworkType != networkType) {
Robert Greenwalt9c75d4a2009-09-27 17:27:04 -0700853 Integer currentPid = new Integer(pid);
Robert Greenwalt9c75d4a2009-09-27 17:27:04 -0700854 mNetRequestersPids[usedNetworkType].remove(currentPid);
Robert Greenwalt421c72b2009-12-17 14:54:59 -0800855 reassessPidDns(pid, true);
Robert Greenwalt9c75d4a2009-09-27 17:27:04 -0700856 if (mNetRequestersPids[usedNetworkType].size() != 0) {
Wink Savilleed9c02b2010-12-03 12:01:38 -0800857 if (DBG) log("not tearing down special network - " +
Robert Greenwalt9c75d4a2009-09-27 17:27:04 -0700858 "others still using it");
859 return 1;
860 }
861 callTeardown = true;
862 }
863 }
Wink Savilleed9c02b2010-12-03 12:01:38 -0800864 if (DBG) log("Doing network teardown");
Robert Greenwalt9c75d4a2009-09-27 17:27:04 -0700865 if (callTeardown) {
866 tracker.teardown();
Robert Greenwalt42acef32009-08-12 16:08:25 -0700867 return 1;
868 } else {
Robert Greenwalt02648a42010-05-18 10:52:51 -0700869 return -1;
Robert Greenwalt42acef32009-08-12 16:08:25 -0700870 }
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800871 }
872
873 /**
Robert Greenwalt585ac0f2010-08-27 09:24:29 -0700874 * @deprecated use requestRouteToHostAddress instead
875 *
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800876 * Ensure that a network route exists to deliver traffic to the specified
877 * host via the specified network interface.
Robert Greenwalt86e9e552009-07-16 17:21:39 -0700878 * @param networkType the type of the network over which traffic to the
879 * specified host is to be routed
880 * @param hostAddress the IP address of the host to which the route is
881 * desired
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800882 * @return {@code true} on success, {@code false} on failure
883 */
884 public boolean requestRouteToHost(int networkType, int hostAddress) {
Robert Greenwalt585ac0f2010-08-27 09:24:29 -0700885 InetAddress inetAddress = NetworkUtils.intToInetAddress(hostAddress);
886
887 if (inetAddress == null) {
888 return false;
889 }
890
891 return requestRouteToHostAddress(networkType, inetAddress.getAddress());
892 }
893
894 /**
895 * Ensure that a network route exists to deliver traffic to the specified
896 * host via the specified network interface.
897 * @param networkType the type of the network over which traffic to the
898 * specified host is to be routed
899 * @param hostAddress the IP address of the host to which the route is
900 * desired
901 * @return {@code true} on success, {@code false} on failure
902 */
903 public boolean requestRouteToHostAddress(int networkType, byte[] hostAddress) {
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800904 enforceChangePermission();
905 if (!ConnectivityManager.isNetworkTypeValid(networkType)) {
906 return false;
907 }
908 NetworkStateTracker tracker = mNetTrackers[networkType];
Robert Greenwalt8206ff32009-09-10 15:06:20 -0700909
Robert Greenwalt5154ae762009-10-30 14:17:42 -0700910 if (tracker == null || !tracker.getNetworkInfo().isConnected() ||
911 tracker.isTeardownRequested()) {
Robert Greenwalt8206ff32009-09-10 15:06:20 -0700912 if (DBG) {
Wink Savilleed9c02b2010-12-03 12:01:38 -0800913 log("requestRouteToHostAddress on down network " +
Robert Greenwalt585ac0f2010-08-27 09:24:29 -0700914 "(" + networkType + ") - dropped");
Robert Greenwalt8206ff32009-09-10 15:06:20 -0700915 }
916 return false;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800917 }
Robert Greenwalt47f69fe2010-06-15 15:43:39 -0700918 try {
Robert Greenwalt585ac0f2010-08-27 09:24:29 -0700919 InetAddress addr = InetAddress.getByAddress(hostAddress);
Robert Greenwalt47f69fe2010-06-15 15:43:39 -0700920 return addHostRoute(tracker, addr);
921 } catch (UnknownHostException e) {}
922 return false;
Irfan Sheriffd649c122010-06-09 15:39:36 -0700923 }
924
925 /**
926 * Ensure that a network route exists to deliver traffic to the specified
927 * host via the mobile data network.
928 * @param hostAddress the IP address of the host to which the route is desired,
929 * in network byte order.
Robert Greenwalt47f69fe2010-06-15 15:43:39 -0700930 * TODO - deprecate
Irfan Sheriffd649c122010-06-09 15:39:36 -0700931 * @return {@code true} on success, {@code false} on failure
932 */
Robert Greenwalt47f69fe2010-06-15 15:43:39 -0700933 private boolean addHostRoute(NetworkStateTracker nt, InetAddress hostAddress) {
Irfan Sheriffd649c122010-06-09 15:39:36 -0700934 if (nt.getNetworkInfo().getType() == ConnectivityManager.TYPE_WIFI) {
935 return false;
936 }
937
Robert Greenwalt37e65eb2010-08-30 10:56:47 -0700938 LinkProperties p = nt.getLinkProperties();
Robert Greenwalt47f69fe2010-06-15 15:43:39 -0700939 if (p == null) return false;
940 String interfaceName = p.getInterfaceName();
Irfan Sheriffd649c122010-06-09 15:39:36 -0700941
942 if (DBG) {
Wink Savilleed9c02b2010-12-03 12:01:38 -0800943 log("Requested host route to " + hostAddress + "(" + interfaceName + ")");
Irfan Sheriffd649c122010-06-09 15:39:36 -0700944 }
Robert Greenwalt47f69fe2010-06-15 15:43:39 -0700945 if (interfaceName != null) {
Robert Greenwalt585ac0f2010-08-27 09:24:29 -0700946 return NetworkUtils.addHostRoute(interfaceName, hostAddress, null);
Irfan Sheriffd649c122010-06-09 15:39:36 -0700947 } else {
Wink Savilleed9c02b2010-12-03 12:01:38 -0800948 if (DBG) loge("addHostRoute failed due to null interface name");
Irfan Sheriffd649c122010-06-09 15:39:36 -0700949 return false;
950 }
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800951 }
952
953 /**
954 * @see ConnectivityManager#getBackgroundDataSetting()
955 */
956 public boolean getBackgroundDataSetting() {
957 return Settings.Secure.getInt(mContext.getContentResolver(),
958 Settings.Secure.BACKGROUND_DATA, 1) == 1;
959 }
Robert Greenwalt86e9e552009-07-16 17:21:39 -0700960
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800961 /**
962 * @see ConnectivityManager#setBackgroundDataSetting(boolean)
963 */
964 public void setBackgroundDataSetting(boolean allowBackgroundDataUsage) {
965 mContext.enforceCallingOrSelfPermission(
966 android.Manifest.permission.CHANGE_BACKGROUND_DATA_SETTING,
967 "ConnectivityService");
Robert Greenwalt86e9e552009-07-16 17:21:39 -0700968
Robert Greenwalt8dcc28b2010-09-23 10:05:56 -0700969 mHandler.sendMessage(mHandler.obtainMessage(EVENT_SET_BACKGROUND_DATA,
970 (allowBackgroundDataUsage ? ENABLED : DISABLED), 0));
971 }
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800972
Robert Greenwalt8dcc28b2010-09-23 10:05:56 -0700973 private void handleSetBackgroundData(boolean enabled) {
974 if (enabled != getBackgroundDataSetting()) {
975 Settings.Secure.putInt(mContext.getContentResolver(),
976 Settings.Secure.BACKGROUND_DATA, enabled ? 1 : 0);
977 Intent broadcast = new Intent(
978 ConnectivityManager.ACTION_BACKGROUND_DATA_SETTING_CHANGED);
979 mContext.sendBroadcast(broadcast);
980 }
Robert Greenwalt86e9e552009-07-16 17:21:39 -0700981 }
982
Robert Greenwaltc03fa502010-02-23 18:58:05 -0800983 /**
984 * @see ConnectivityManager#getMobileDataEnabled()
985 */
986 public boolean getMobileDataEnabled() {
Wink Savillee7982682010-12-07 10:31:02 -0800987 // TODO: This detail should probably be in DataConnectionTracker's
988 // which is where we store the value and maybe make this
989 // asynchronous.
Robert Greenwaltc03fa502010-02-23 18:58:05 -0800990 enforceAccessPermission();
991 boolean retVal = Settings.Secure.getInt(mContext.getContentResolver(),
992 Settings.Secure.MOBILE_DATA, 1) == 1;
Wink Savilleed9c02b2010-12-03 12:01:38 -0800993 if (DBG) log("getMobileDataEnabled returning " + retVal);
Robert Greenwaltc03fa502010-02-23 18:58:05 -0800994 return retVal;
995 }
996
997 /**
998 * @see ConnectivityManager#setMobileDataEnabled(boolean)
999 */
Robert Greenwalt8dcc28b2010-09-23 10:05:56 -07001000 public void setMobileDataEnabled(boolean enabled) {
Robert Greenwaltc03fa502010-02-23 18:58:05 -08001001 enforceChangePermission();
Wink Savilleed9c02b2010-12-03 12:01:38 -08001002 if (DBG) log("setMobileDataEnabled(" + enabled + ")");
Robert Greenwaltc03fa502010-02-23 18:58:05 -08001003
Robert Greenwalt8dcc28b2010-09-23 10:05:56 -07001004 mHandler.sendMessage(mHandler.obtainMessage(EVENT_SET_MOBILE_DATA,
1005 (enabled ? ENABLED : DISABLED), 0));
1006 }
1007
1008 private void handleSetMobileData(boolean enabled) {
Wink Savillee7982682010-12-07 10:31:02 -08001009 if (mNetTrackers[ConnectivityManager.TYPE_MOBILE] != null) {
1010 if (DBG) {
1011 Slog.d(TAG, mNetTrackers[ConnectivityManager.TYPE_MOBILE].toString() + enabled);
Robert Greenwaltc03fa502010-02-23 18:58:05 -08001012 }
Wink Savillee7982682010-12-07 10:31:02 -08001013 mNetTrackers[ConnectivityManager.TYPE_MOBILE].setDataEnable(enabled);
Robert Greenwaltc03fa502010-02-23 18:58:05 -08001014 }
1015 }
1016
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001017 private void enforceAccessPermission() {
Robert Greenwalt86e9e552009-07-16 17:21:39 -07001018 mContext.enforceCallingOrSelfPermission(
1019 android.Manifest.permission.ACCESS_NETWORK_STATE,
1020 "ConnectivityService");
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001021 }
1022
1023 private void enforceChangePermission() {
Robert Greenwalt86e9e552009-07-16 17:21:39 -07001024 mContext.enforceCallingOrSelfPermission(
1025 android.Manifest.permission.CHANGE_NETWORK_STATE,
1026 "ConnectivityService");
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001027 }
1028
Robert Greenwaltd0e18ff2010-01-26 11:40:34 -08001029 // TODO Make this a special check when it goes public
1030 private void enforceTetherChangePermission() {
1031 mContext.enforceCallingOrSelfPermission(
1032 android.Manifest.permission.CHANGE_NETWORK_STATE,
1033 "ConnectivityService");
1034 }
1035
Robert Greenwalt2a091d72010-02-11 18:18:40 -08001036 private void enforceTetherAccessPermission() {
1037 mContext.enforceCallingOrSelfPermission(
1038 android.Manifest.permission.ACCESS_NETWORK_STATE,
1039 "ConnectivityService");
1040 }
1041
Robert Greenwalt14f2ef42010-06-15 12:19:37 -07001042 private void enforceConnectivityInternalPermission() {
1043 mContext.enforceCallingOrSelfPermission(
1044 android.Manifest.permission.CONNECTIVITY_INTERNAL,
1045 "ConnectivityService");
1046 }
1047
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001048 /**
Robert Greenwalt86e9e552009-07-16 17:21:39 -07001049 * Handle a {@code DISCONNECTED} event. If this pertains to the non-active
1050 * network, we ignore it. If it is for the active network, we send out a
1051 * broadcast. But first, we check whether it might be possible to connect
1052 * to a different network.
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001053 * @param info the {@code NetworkInfo} for the network
1054 */
1055 private void handleDisconnect(NetworkInfo info) {
1056
Robert Greenwalt42acef32009-08-12 16:08:25 -07001057 int prevNetType = info.getType();
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001058
Robert Greenwalt42acef32009-08-12 16:08:25 -07001059 mNetTrackers[prevNetType].setTeardownRequested(false);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001060 /*
1061 * If the disconnected network is not the active one, then don't report
1062 * this as a loss of connectivity. What probably happened is that we're
1063 * getting the disconnect for a network that we explicitly disabled
1064 * in accordance with network preference policies.
1065 */
Robert Greenwalt42acef32009-08-12 16:08:25 -07001066 if (!mNetAttributes[prevNetType].isDefault()) {
1067 List pids = mNetRequestersPids[prevNetType];
1068 for (int i = 0; i<pids.size(); i++) {
1069 Integer pid = (Integer)pids.get(i);
1070 // will remove them because the net's no longer connected
1071 // need to do this now as only now do we know the pids and
1072 // can properly null things that are no longer referenced.
1073 reassessPidDns(pid.intValue(), false);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001074 }
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001075 }
1076
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001077 Intent intent = new Intent(ConnectivityManager.CONNECTIVITY_ACTION);
1078 intent.putExtra(ConnectivityManager.EXTRA_NETWORK_INFO, info);
1079 if (info.isFailover()) {
1080 intent.putExtra(ConnectivityManager.EXTRA_IS_FAILOVER, true);
1081 info.setFailover(false);
1082 }
1083 if (info.getReason() != null) {
1084 intent.putExtra(ConnectivityManager.EXTRA_REASON, info.getReason());
1085 }
1086 if (info.getExtraInfo() != null) {
Robert Greenwalt86e9e552009-07-16 17:21:39 -07001087 intent.putExtra(ConnectivityManager.EXTRA_EXTRA_INFO,
1088 info.getExtraInfo());
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001089 }
Robert Greenwalt42acef32009-08-12 16:08:25 -07001090
Robert Greenwaltcc4b4012010-01-25 17:54:29 -08001091 NetworkStateTracker newNet = null;
1092 if (mNetAttributes[prevNetType].isDefault()) {
1093 newNet = tryFailover(prevNetType);
1094 if (newNet != null) {
1095 NetworkInfo switchTo = newNet.getNetworkInfo();
Robert Greenwalt029be812010-09-20 18:01:43 -07001096 if (!switchTo.isConnected()) {
Wink Savilleed9c02b2010-12-03 12:01:38 -08001097 // if the other net is connected they've already reset this and perhaps even
1098 // gotten a positive report we don't want to overwrite, but if not we need to
1099 // clear this now to turn our cellular sig strength white
Robert Greenwalt029be812010-09-20 18:01:43 -07001100 mDefaultInetConditionPublished = 0;
Robert Greenwaltd68e3212010-12-21 11:43:28 -08001101 intent.putExtra(ConnectivityManager.EXTRA_NO_CONNECTIVITY, true);
Robert Greenwalt029be812010-09-20 18:01:43 -07001102 }
Robert Greenwaltcc4b4012010-01-25 17:54:29 -08001103 intent.putExtra(ConnectivityManager.EXTRA_OTHER_NETWORK_INFO, switchTo);
1104 } else {
Robert Greenwalt029be812010-09-20 18:01:43 -07001105 mDefaultInetConditionPublished = 0; // we're not connected anymore
Robert Greenwaltcc4b4012010-01-25 17:54:29 -08001106 intent.putExtra(ConnectivityManager.EXTRA_NO_CONNECTIVITY, true);
1107 }
Robert Greenwaltda03c4e2010-01-20 19:29:41 -08001108 }
Robert Greenwalt029be812010-09-20 18:01:43 -07001109 intent.putExtra(ConnectivityManager.EXTRA_INET_CONDITION, mDefaultInetConditionPublished);
Robert Greenwaltda03c4e2010-01-20 19:29:41 -08001110 // do this before we broadcast the change
Robert Greenwaltc76b8fa2010-07-23 15:46:26 -07001111 handleConnectivityChange(prevNetType);
Robert Greenwaltda03c4e2010-01-20 19:29:41 -08001112
1113 sendStickyBroadcast(intent);
1114 /*
1115 * If the failover network is already connected, then immediately send
1116 * out a followup broadcast indicating successful failover
1117 */
1118 if (newNet != null && newNet.getNetworkInfo().isConnected()) {
1119 sendConnectedBroadcast(newNet.getNetworkInfo());
1120 }
1121 }
1122
Robert Greenwaltcc4b4012010-01-25 17:54:29 -08001123 // returns null if no failover available
Robert Greenwaltda03c4e2010-01-20 19:29:41 -08001124 private NetworkStateTracker tryFailover(int prevNetType) {
Robert Greenwalt42acef32009-08-12 16:08:25 -07001125 /*
1126 * If this is a default network, check if other defaults are available
1127 * or active
1128 */
1129 NetworkStateTracker newNet = null;
1130 if (mNetAttributes[prevNetType].isDefault()) {
Robert Greenwalt42acef32009-08-12 16:08:25 -07001131 if (mActiveDefaultNetwork == prevNetType) {
1132 mActiveDefaultNetwork = -1;
1133 }
1134
1135 int newType = -1;
1136 int newPriority = -1;
Robert Greenwaltda03c4e2010-01-20 19:29:41 -08001137 for (int checkType=0; checkType <= ConnectivityManager.MAX_NETWORK_TYPE; checkType++) {
Robert Greenwalt5154ae762009-10-30 14:17:42 -07001138 if (checkType == prevNetType) continue;
1139 if (mNetAttributes[checkType] == null) continue;
Robert Greenwalt42acef32009-08-12 16:08:25 -07001140 if (mNetAttributes[checkType].isDefault()) {
1141 /* TODO - if we have multiple nets we could use
1142 * we may want to put more thought into which we choose
1143 */
1144 if (checkType == mNetworkPreference) {
1145 newType = checkType;
1146 break;
1147 }
Robert Greenwalt5154ae762009-10-30 14:17:42 -07001148 if (mNetAttributes[checkType].mPriority > newPriority) {
Robert Greenwalt42acef32009-08-12 16:08:25 -07001149 newType = checkType;
Robert Greenwalt5154ae762009-10-30 14:17:42 -07001150 newPriority = mNetAttributes[newType].mPriority;
Robert Greenwalt42acef32009-08-12 16:08:25 -07001151 }
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001152 }
1153 }
Robert Greenwalt42acef32009-08-12 16:08:25 -07001154
1155 if (newType != -1) {
1156 newNet = mNetTrackers[newType];
1157 /**
1158 * See if the other network is available to fail over to.
1159 * If is not available, we enable it anyway, so that it
1160 * will be able to connect when it does become available,
1161 * but we report a total loss of connectivity rather than
1162 * report that we are attempting to fail over.
1163 */
1164 if (newNet.isAvailable()) {
1165 NetworkInfo switchTo = newNet.getNetworkInfo();
1166 switchTo.setFailover(true);
Robert Greenwalta64bf832009-08-19 20:19:33 -07001167 if (!switchTo.isConnectedOrConnecting() ||
1168 newNet.isTeardownRequested()) {
Robert Greenwalt42acef32009-08-12 16:08:25 -07001169 newNet.reconnect();
1170 }
1171 if (DBG) {
1172 if (switchTo.isConnected()) {
Wink Savilleed9c02b2010-12-03 12:01:38 -08001173 log("Switching to already connected " + switchTo.getTypeName());
Robert Greenwalt42acef32009-08-12 16:08:25 -07001174 } else {
Wink Savilleed9c02b2010-12-03 12:01:38 -08001175 log("Attempting to switch to " + switchTo.getTypeName());
Robert Greenwalt42acef32009-08-12 16:08:25 -07001176 }
1177 }
Robert Greenwalt42acef32009-08-12 16:08:25 -07001178 } else {
1179 newNet.reconnect();
Robert Greenwaltf0fa39e2010-03-09 14:55:08 -08001180 newNet = null; // not officially avail.. try anyway, but
1181 // report no failover
Robert Greenwalt42acef32009-08-12 16:08:25 -07001182 }
Robert Greenwalt572172b2010-10-08 16:35:52 -07001183 } else {
Wink Savilleed9c02b2010-12-03 12:01:38 -08001184 loge("Network failover failing.");
Robert Greenwalt42acef32009-08-12 16:08:25 -07001185 }
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001186 }
Robert Greenwalt42acef32009-08-12 16:08:25 -07001187
Robert Greenwaltda03c4e2010-01-20 19:29:41 -08001188 return newNet;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001189 }
1190
1191 private void sendConnectedBroadcast(NetworkInfo info) {
Robert Greenwalt1e9aac22010-09-15 17:36:33 -07001192 sendGeneralBroadcast(info, ConnectivityManager.CONNECTIVITY_ACTION);
1193 }
1194
1195 private void sendInetConditionBroadcast(NetworkInfo info) {
1196 sendGeneralBroadcast(info, ConnectivityManager.INET_CONDITION_ACTION);
1197 }
1198
1199 private void sendGeneralBroadcast(NetworkInfo info, String bcastType) {
1200 Intent intent = new Intent(bcastType);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001201 intent.putExtra(ConnectivityManager.EXTRA_NETWORK_INFO, info);
1202 if (info.isFailover()) {
1203 intent.putExtra(ConnectivityManager.EXTRA_IS_FAILOVER, true);
1204 info.setFailover(false);
1205 }
1206 if (info.getReason() != null) {
1207 intent.putExtra(ConnectivityManager.EXTRA_REASON, info.getReason());
1208 }
1209 if (info.getExtraInfo() != null) {
Robert Greenwalt86e9e552009-07-16 17:21:39 -07001210 intent.putExtra(ConnectivityManager.EXTRA_EXTRA_INFO,
1211 info.getExtraInfo());
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001212 }
Robert Greenwaltd7085fc2010-09-08 15:24:47 -07001213 intent.putExtra(ConnectivityManager.EXTRA_INET_CONDITION, mDefaultInetConditionPublished);
Mike Lockwood0f79b542009-08-14 14:18:49 -04001214 sendStickyBroadcast(intent);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001215 }
1216
1217 /**
1218 * Called when an attempt to fail over to another network has failed.
1219 * @param info the {@link NetworkInfo} for the failed network
1220 */
1221 private void handleConnectionFailure(NetworkInfo info) {
1222 mNetTrackers[info.getType()].setTeardownRequested(false);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001223
Robert Greenwalt42acef32009-08-12 16:08:25 -07001224 String reason = info.getReason();
1225 String extraInfo = info.getExtraInfo();
Robert Greenwalt86e9e552009-07-16 17:21:39 -07001226
Robert Greenwalt572172b2010-10-08 16:35:52 -07001227 String reasonText;
1228 if (reason == null) {
1229 reasonText = ".";
1230 } else {
1231 reasonText = " (" + reason + ").";
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001232 }
Wink Savilleed9c02b2010-12-03 12:01:38 -08001233 loge("Attempt to connect to " + info.getTypeName() + " failed" + reasonText);
Robert Greenwalt42acef32009-08-12 16:08:25 -07001234
1235 Intent intent = new Intent(ConnectivityManager.CONNECTIVITY_ACTION);
1236 intent.putExtra(ConnectivityManager.EXTRA_NETWORK_INFO, info);
1237 if (getActiveNetworkInfo() == null) {
1238 intent.putExtra(ConnectivityManager.EXTRA_NO_CONNECTIVITY, true);
1239 }
1240 if (reason != null) {
1241 intent.putExtra(ConnectivityManager.EXTRA_REASON, reason);
1242 }
1243 if (extraInfo != null) {
1244 intent.putExtra(ConnectivityManager.EXTRA_EXTRA_INFO, extraInfo);
1245 }
1246 if (info.isFailover()) {
1247 intent.putExtra(ConnectivityManager.EXTRA_IS_FAILOVER, true);
1248 info.setFailover(false);
1249 }
Robert Greenwaltda03c4e2010-01-20 19:29:41 -08001250
Robert Greenwaltcc4b4012010-01-25 17:54:29 -08001251 NetworkStateTracker newNet = null;
1252 if (mNetAttributes[info.getType()].isDefault()) {
1253 newNet = tryFailover(info.getType());
1254 if (newNet != null) {
1255 NetworkInfo switchTo = newNet.getNetworkInfo();
Robert Greenwalt029be812010-09-20 18:01:43 -07001256 if (!switchTo.isConnected()) {
Robert Greenwalt572172b2010-10-08 16:35:52 -07001257 // if the other net is connected they've already reset this and perhaps
1258 // even gotten a positive report we don't want to overwrite, but if not
1259 // we need to clear this now to turn our cellular sig strength white
Robert Greenwalt029be812010-09-20 18:01:43 -07001260 mDefaultInetConditionPublished = 0;
1261 }
Robert Greenwaltcc4b4012010-01-25 17:54:29 -08001262 intent.putExtra(ConnectivityManager.EXTRA_OTHER_NETWORK_INFO, switchTo);
1263 } else {
Robert Greenwalt029be812010-09-20 18:01:43 -07001264 mDefaultInetConditionPublished = 0;
Robert Greenwaltcc4b4012010-01-25 17:54:29 -08001265 intent.putExtra(ConnectivityManager.EXTRA_NO_CONNECTIVITY, true);
1266 }
Robert Greenwaltda03c4e2010-01-20 19:29:41 -08001267 }
Robert Greenwaltcc4b4012010-01-25 17:54:29 -08001268
Robert Greenwalt029be812010-09-20 18:01:43 -07001269 intent.putExtra(ConnectivityManager.EXTRA_INET_CONDITION, mDefaultInetConditionPublished);
Mike Lockwood0f79b542009-08-14 14:18:49 -04001270 sendStickyBroadcast(intent);
Robert Greenwaltda03c4e2010-01-20 19:29:41 -08001271 /*
1272 * If the failover network is already connected, then immediately send
1273 * out a followup broadcast indicating successful failover
1274 */
1275 if (newNet != null && newNet.getNetworkInfo().isConnected()) {
1276 sendConnectedBroadcast(newNet.getNetworkInfo());
1277 }
Mike Lockwood0f79b542009-08-14 14:18:49 -04001278 }
1279
1280 private void sendStickyBroadcast(Intent intent) {
1281 synchronized(this) {
Dianne Hackborn1c633fc2009-12-08 19:45:14 -08001282 if (!mSystemReady) {
1283 mInitialBroadcast = new Intent(intent);
Mike Lockwood0f79b542009-08-14 14:18:49 -04001284 }
Dianne Hackborn1c633fc2009-12-08 19:45:14 -08001285 intent.addFlags(Intent.FLAG_RECEIVER_REGISTERED_ONLY_BEFORE_BOOT);
1286 mContext.sendStickyBroadcast(intent);
Mike Lockwood0f79b542009-08-14 14:18:49 -04001287 }
1288 }
1289
1290 void systemReady() {
1291 synchronized(this) {
1292 mSystemReady = true;
Dianne Hackborn1c633fc2009-12-08 19:45:14 -08001293 if (mInitialBroadcast != null) {
1294 mContext.sendStickyBroadcast(mInitialBroadcast);
1295 mInitialBroadcast = null;
Mike Lockwood0f79b542009-08-14 14:18:49 -04001296 }
1297 }
Robert Greenwalt434203a2010-10-11 16:00:27 -07001298 // load the global proxy at startup
1299 mHandler.sendMessage(mHandler.obtainMessage(EVENT_APPLY_GLOBAL_HTTP_PROXY));
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001300 }
1301
1302 private void handleConnect(NetworkInfo info) {
Robert Greenwalt42acef32009-08-12 16:08:25 -07001303 int type = info.getType();
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001304
1305 // snapshot isFailover, because sendConnectedBroadcast() resets it
1306 boolean isFailover = info.isFailover();
Robert Greenwalt42acef32009-08-12 16:08:25 -07001307 NetworkStateTracker thisNet = mNetTrackers[type];
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001308
Robert Greenwalt42acef32009-08-12 16:08:25 -07001309 // if this is a default net and other default is running
1310 // kill the one not preferred
1311 if (mNetAttributes[type].isDefault()) {
Robert Greenwalt42acef32009-08-12 16:08:25 -07001312 if (mActiveDefaultNetwork != -1 && mActiveDefaultNetwork != type) {
1313 if ((type != mNetworkPreference &&
1314 mNetAttributes[mActiveDefaultNetwork].mPriority >
1315 mNetAttributes[type].mPriority) ||
1316 mNetworkPreference == mActiveDefaultNetwork) {
1317 // don't accept this one
Wink Savilleed9c02b2010-12-03 12:01:38 -08001318 if (DBG) {
1319 log("Not broadcasting CONNECT_ACTION " +
Robert Greenwalt42acef32009-08-12 16:08:25 -07001320 "to torn down network " + info.getTypeName());
Wink Savilleed9c02b2010-12-03 12:01:38 -08001321 }
Robert Greenwalt42acef32009-08-12 16:08:25 -07001322 teardown(thisNet);
1323 return;
1324 } else {
1325 // tear down the other
1326 NetworkStateTracker otherNet =
1327 mNetTrackers[mActiveDefaultNetwork];
Wink Savilleed9c02b2010-12-03 12:01:38 -08001328 if (DBG) {
1329 log("Policy requires " + otherNet.getNetworkInfo().getTypeName() +
Robert Greenwalt42acef32009-08-12 16:08:25 -07001330 " teardown");
Wink Savilleed9c02b2010-12-03 12:01:38 -08001331 }
Robert Greenwalt42acef32009-08-12 16:08:25 -07001332 if (!teardown(otherNet)) {
Wink Savilleed9c02b2010-12-03 12:01:38 -08001333 loge("Network declined teardown request");
Robert Greenwalt42acef32009-08-12 16:08:25 -07001334 return;
1335 }
Robert Greenwalt14f2ef42010-06-15 12:19:37 -07001336 }
1337 }
1338 synchronized (ConnectivityService.this) {
1339 // have a new default network, release the transition wakelock in a second
1340 // if it's held. The second pause is to allow apps to reconnect over the
1341 // new network
1342 if (mNetTransitionWakeLock.isHeld()) {
1343 mHandler.sendMessageDelayed(mHandler.obtainMessage(
Robert Greenwaltf3331232010-09-24 14:32:21 -07001344 EVENT_CLEAR_NET_TRANSITION_WAKELOCK,
Robert Greenwalt14f2ef42010-06-15 12:19:37 -07001345 mNetTransitionWakeLockSerialNumber, 0),
1346 1000);
Robert Greenwalt42acef32009-08-12 16:08:25 -07001347 }
1348 }
1349 mActiveDefaultNetwork = type;
Robert Greenwaltd7085fc2010-09-08 15:24:47 -07001350 // this will cause us to come up initially as unconnected and switching
1351 // to connected after our normal pause unless somebody reports us as reall
1352 // disconnected
1353 mDefaultInetConditionPublished = 0;
1354 mDefaultConnectionSequence++;
1355 mInetConditionChangeInFlight = false;
1356 // Don't do this - if we never sign in stay, grey
1357 //reportNetworkCondition(mActiveDefaultNetwork, 100);
Robert Greenwalt42acef32009-08-12 16:08:25 -07001358 }
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001359 thisNet.setTeardownRequested(false);
Irfan Sheriffd649c122010-06-09 15:39:36 -07001360 updateNetworkSettings(thisNet);
Robert Greenwaltc76b8fa2010-07-23 15:46:26 -07001361 handleConnectivityChange(type);
Robert Greenwalt42acef32009-08-12 16:08:25 -07001362 sendConnectedBroadcast(info);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001363 }
1364
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001365 /**
Robert Greenwaltc76b8fa2010-07-23 15:46:26 -07001366 * After a change in the connectivity state of a network. We're mainly
1367 * concerned with making sure that the list of DNS servers is set up
1368 * according to which networks are connected, and ensuring that the
1369 * right routing table entries exist.
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001370 */
Robert Greenwaltc76b8fa2010-07-23 15:46:26 -07001371 private void handleConnectivityChange(int netType) {
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001372 /*
Robert Greenwalt42acef32009-08-12 16:08:25 -07001373 * If a non-default network is enabled, add the host routes that
Robert Greenwaltc76b8fa2010-07-23 15:46:26 -07001374 * will allow it's DNS servers to be accessed.
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001375 */
Robert Greenwaltc76b8fa2010-07-23 15:46:26 -07001376 handleDnsConfigurationChange(netType);
Robert Greenwalt42acef32009-08-12 16:08:25 -07001377
Robert Greenwaltc76b8fa2010-07-23 15:46:26 -07001378 if (mNetTrackers[netType].getNetworkInfo().isConnected()) {
1379 if (mNetAttributes[netType].isDefault()) {
Robert Greenwalt434203a2010-10-11 16:00:27 -07001380 handleApplyDefaultProxy(netType);
Robert Greenwaltc76b8fa2010-07-23 15:46:26 -07001381 addDefaultRoute(mNetTrackers[netType]);
Robert Greenwalt42acef32009-08-12 16:08:25 -07001382 } else {
Robert Greenwaltc76b8fa2010-07-23 15:46:26 -07001383 addPrivateDnsRoutes(mNetTrackers[netType]);
1384 }
1385 } else {
1386 if (mNetAttributes[netType].isDefault()) {
1387 removeDefaultRoute(mNetTrackers[netType]);
1388 } else {
1389 removePrivateDnsRoutes(mNetTrackers[netType]);
Robert Greenwalt42acef32009-08-12 16:08:25 -07001390 }
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001391 }
1392 }
1393
Irfan Sheriffd649c122010-06-09 15:39:36 -07001394 private void addPrivateDnsRoutes(NetworkStateTracker nt) {
Irfan Sheriffd649c122010-06-09 15:39:36 -07001395 boolean privateDnsRouteSet = nt.isPrivateDnsRouteSet();
Robert Greenwalt37e65eb2010-08-30 10:56:47 -07001396 LinkProperties p = nt.getLinkProperties();
Robert Greenwalt47f69fe2010-06-15 15:43:39 -07001397 if (p == null) return;
1398 String interfaceName = p.getInterfaceName();
Irfan Sheriffd649c122010-06-09 15:39:36 -07001399
1400 if (DBG) {
Wink Savilleed9c02b2010-12-03 12:01:38 -08001401 log("addPrivateDnsRoutes for " + nt +
Irfan Sheriffd649c122010-06-09 15:39:36 -07001402 "(" + interfaceName + ") - mPrivateDnsRouteSet = " + privateDnsRouteSet);
1403 }
Irfan Sheriffd649c122010-06-09 15:39:36 -07001404 if (interfaceName != null && !privateDnsRouteSet) {
Robert Greenwalt47f69fe2010-06-15 15:43:39 -07001405 Collection<InetAddress> dnsList = p.getDnses();
1406 for (InetAddress dns : dnsList) {
Wink Savilleed9c02b2010-12-03 12:01:38 -08001407 if (DBG) log(" adding " + dns);
Robert Greenwalt585ac0f2010-08-27 09:24:29 -07001408 NetworkUtils.addHostRoute(interfaceName, dns, null);
Irfan Sheriffd649c122010-06-09 15:39:36 -07001409 }
1410 nt.privateDnsRouteSet(true);
1411 }
1412 }
1413
1414 private void removePrivateDnsRoutes(NetworkStateTracker nt) {
1415 // TODO - we should do this explicitly but the NetUtils api doesnt
1416 // support this yet - must remove all. No worse than before
Robert Greenwalt37e65eb2010-08-30 10:56:47 -07001417 LinkProperties p = nt.getLinkProperties();
Robert Greenwalt47f69fe2010-06-15 15:43:39 -07001418 if (p == null) return;
1419 String interfaceName = p.getInterfaceName();
Irfan Sheriffd649c122010-06-09 15:39:36 -07001420 boolean privateDnsRouteSet = nt.isPrivateDnsRouteSet();
1421 if (interfaceName != null && privateDnsRouteSet) {
1422 if (DBG) {
Wink Savilleed9c02b2010-12-03 12:01:38 -08001423 log("removePrivateDnsRoutes for " + nt.getNetworkInfo().getTypeName() +
Irfan Sheriffd649c122010-06-09 15:39:36 -07001424 " (" + interfaceName + ")");
1425 }
1426 NetworkUtils.removeHostRoutes(interfaceName);
1427 nt.privateDnsRouteSet(false);
1428 }
1429 }
1430
Irfan Sheriffd649c122010-06-09 15:39:36 -07001431
1432 private void addDefaultRoute(NetworkStateTracker nt) {
Robert Greenwalt37e65eb2010-08-30 10:56:47 -07001433 LinkProperties p = nt.getLinkProperties();
Robert Greenwalt47f69fe2010-06-15 15:43:39 -07001434 if (p == null) return;
1435 String interfaceName = p.getInterfaceName();
1436 InetAddress defaultGatewayAddr = p.getGateway();
Irfan Sheriffd649c122010-06-09 15:39:36 -07001437
Robert Greenwaltc76b8fa2010-07-23 15:46:26 -07001438 if ((interfaceName != null) && (defaultGatewayAddr != null )) {
Robert Greenwalt585ac0f2010-08-27 09:24:29 -07001439 if (!NetworkUtils.addDefaultRoute(interfaceName, defaultGatewayAddr) && DBG) {
Robert Greenwalt47f69fe2010-06-15 15:43:39 -07001440 NetworkInfo networkInfo = nt.getNetworkInfo();
Wink Savilleed9c02b2010-12-03 12:01:38 -08001441 log("addDefaultRoute for " + networkInfo.getTypeName() +
Irfan Sheriffd649c122010-06-09 15:39:36 -07001442 " (" + interfaceName + "), GatewayAddr=" + defaultGatewayAddr);
1443 }
Irfan Sheriffd649c122010-06-09 15:39:36 -07001444 }
1445 }
1446
1447
1448 public void removeDefaultRoute(NetworkStateTracker nt) {
Robert Greenwalt37e65eb2010-08-30 10:56:47 -07001449 LinkProperties p = nt.getLinkProperties();
Robert Greenwalt47f69fe2010-06-15 15:43:39 -07001450 if (p == null) return;
1451 String interfaceName = p.getInterfaceName();
Irfan Sheriffd649c122010-06-09 15:39:36 -07001452
Robert Greenwaltc76b8fa2010-07-23 15:46:26 -07001453 if (interfaceName != null) {
1454 if ((NetworkUtils.removeDefaultRoute(interfaceName) >= 0) && DBG) {
Robert Greenwalt47f69fe2010-06-15 15:43:39 -07001455 NetworkInfo networkInfo = nt.getNetworkInfo();
Wink Savilleed9c02b2010-12-03 12:01:38 -08001456 log("removeDefaultRoute for " + networkInfo.getTypeName() + " (" +
Irfan Sheriffd649c122010-06-09 15:39:36 -07001457 interfaceName + ")");
1458 }
Irfan Sheriffd649c122010-06-09 15:39:36 -07001459 }
1460 }
1461
1462 /**
1463 * Reads the network specific TCP buffer sizes from SystemProperties
1464 * net.tcp.buffersize.[default|wifi|umts|edge|gprs] and set them for system
1465 * wide use
1466 */
1467 public void updateNetworkSettings(NetworkStateTracker nt) {
1468 String key = nt.getTcpBufferSizesPropName();
1469 String bufferSizes = SystemProperties.get(key);
1470
1471 if (bufferSizes.length() == 0) {
Wink Savilleed9c02b2010-12-03 12:01:38 -08001472 loge(key + " not found in system properties. Using defaults");
Irfan Sheriffd649c122010-06-09 15:39:36 -07001473
1474 // Setting to default values so we won't be stuck to previous values
1475 key = "net.tcp.buffersize.default";
1476 bufferSizes = SystemProperties.get(key);
1477 }
1478
1479 // Set values in kernel
1480 if (bufferSizes.length() != 0) {
1481 if (DBG) {
Wink Savilleed9c02b2010-12-03 12:01:38 -08001482 log("Setting TCP values: [" + bufferSizes
Irfan Sheriffd649c122010-06-09 15:39:36 -07001483 + "] which comes from [" + key + "]");
1484 }
1485 setBufferSize(bufferSizes);
1486 }
1487 }
1488
1489 /**
1490 * Writes TCP buffer sizes to /sys/kernel/ipv4/tcp_[r/w]mem_[min/def/max]
1491 * which maps to /proc/sys/net/ipv4/tcp_rmem and tcpwmem
1492 *
1493 * @param bufferSizes in the format of "readMin, readInitial, readMax,
1494 * writeMin, writeInitial, writeMax"
1495 */
1496 private void setBufferSize(String bufferSizes) {
1497 try {
1498 String[] values = bufferSizes.split(",");
1499
1500 if (values.length == 6) {
1501 final String prefix = "/sys/kernel/ipv4/tcp_";
1502 stringToFile(prefix + "rmem_min", values[0]);
1503 stringToFile(prefix + "rmem_def", values[1]);
1504 stringToFile(prefix + "rmem_max", values[2]);
1505 stringToFile(prefix + "wmem_min", values[3]);
1506 stringToFile(prefix + "wmem_def", values[4]);
1507 stringToFile(prefix + "wmem_max", values[5]);
1508 } else {
Wink Savilleed9c02b2010-12-03 12:01:38 -08001509 loge("Invalid buffersize string: " + bufferSizes);
Irfan Sheriffd649c122010-06-09 15:39:36 -07001510 }
1511 } catch (IOException e) {
Wink Savilleed9c02b2010-12-03 12:01:38 -08001512 loge("Can't set tcp buffer sizes:" + e);
Irfan Sheriffd649c122010-06-09 15:39:36 -07001513 }
1514 }
1515
1516 /**
1517 * Writes string to file. Basically same as "echo -n $string > $filename"
1518 *
1519 * @param filename
1520 * @param string
1521 * @throws IOException
1522 */
1523 private void stringToFile(String filename, String string) throws IOException {
1524 FileWriter out = new FileWriter(filename);
1525 try {
1526 out.write(string);
1527 } finally {
1528 out.close();
1529 }
1530 }
1531
1532
Robert Greenwalt42acef32009-08-12 16:08:25 -07001533 /**
1534 * Adjust the per-process dns entries (net.dns<x>.<pid>) based
1535 * on the highest priority active net which this process requested.
1536 * If there aren't any, clear it out
1537 */
1538 private void reassessPidDns(int myPid, boolean doBump)
1539 {
Wink Savilleed9c02b2010-12-03 12:01:38 -08001540 if (DBG) log("reassessPidDns for pid " + myPid);
Robert Greenwalt42acef32009-08-12 16:08:25 -07001541 for(int i : mPriorityList) {
1542 if (mNetAttributes[i].isDefault()) {
1543 continue;
1544 }
1545 NetworkStateTracker nt = mNetTrackers[i];
Robert Greenwalt86e9e552009-07-16 17:21:39 -07001546 if (nt.getNetworkInfo().isConnected() &&
1547 !nt.isTeardownRequested()) {
Robert Greenwalt37e65eb2010-08-30 10:56:47 -07001548 LinkProperties p = nt.getLinkProperties();
Robert Greenwalt47f69fe2010-06-15 15:43:39 -07001549 if (p == null) continue;
Robert Greenwalt42acef32009-08-12 16:08:25 -07001550 List pids = mNetRequestersPids[i];
1551 for (int j=0; j<pids.size(); j++) {
1552 Integer pid = (Integer)pids.get(j);
1553 if (pid.intValue() == myPid) {
Robert Greenwalt47f69fe2010-06-15 15:43:39 -07001554 Collection<InetAddress> dnses = p.getDnses();
1555 writePidDns(dnses, myPid);
Robert Greenwalt42acef32009-08-12 16:08:25 -07001556 if (doBump) {
1557 bumpDns();
1558 }
1559 return;
1560 }
1561 }
1562 }
1563 }
1564 // nothing found - delete
1565 for (int i = 1; ; i++) {
1566 String prop = "net.dns" + i + "." + myPid;
1567 if (SystemProperties.get(prop).length() == 0) {
1568 if (doBump) {
1569 bumpDns();
1570 }
1571 return;
1572 }
1573 SystemProperties.set(prop, "");
1574 }
1575 }
1576
Robert Greenwalt10398722010-12-17 15:20:36 -08001577 // return true if results in a change
1578 private boolean writePidDns(Collection <InetAddress> dnses, int pid) {
Robert Greenwalt42acef32009-08-12 16:08:25 -07001579 int j = 1;
Robert Greenwalt10398722010-12-17 15:20:36 -08001580 boolean changed = false;
Robert Greenwalt47f69fe2010-06-15 15:43:39 -07001581 for (InetAddress dns : dnses) {
Robert Greenwalt10398722010-12-17 15:20:36 -08001582 String dnsString = dns.getHostAddress();
1583 if (changed || !dnsString.equals(SystemProperties.get("net.dns" + j + "." + pid))) {
1584 changed = true;
1585 SystemProperties.set("net.dns" + j++ + "." + pid, dns.getHostAddress());
1586 }
Robert Greenwalt42acef32009-08-12 16:08:25 -07001587 }
Robert Greenwalt10398722010-12-17 15:20:36 -08001588 return changed;
Robert Greenwalt42acef32009-08-12 16:08:25 -07001589 }
1590
1591 private void bumpDns() {
1592 /*
1593 * Bump the property that tells the name resolver library to reread
1594 * the DNS server list from the properties.
1595 */
1596 String propVal = SystemProperties.get("net.dnschange");
1597 int n = 0;
1598 if (propVal.length() != 0) {
1599 try {
1600 n = Integer.parseInt(propVal);
1601 } catch (NumberFormatException e) {}
1602 }
1603 SystemProperties.set("net.dnschange", "" + (n+1));
Robert Greenwalt03595d02010-11-02 14:08:23 -07001604 /*
1605 * Tell the VMs to toss their DNS caches
1606 */
1607 Intent intent = new Intent(Intent.ACTION_CLEAR_DNS_CACHE);
1608 intent.addFlags(Intent.FLAG_RECEIVER_REPLACE_PENDING);
1609 mContext.sendBroadcast(intent);
Robert Greenwalt42acef32009-08-12 16:08:25 -07001610 }
1611
Robert Greenwaltc76b8fa2010-07-23 15:46:26 -07001612 private void handleDnsConfigurationChange(int netType) {
Robert Greenwalt42acef32009-08-12 16:08:25 -07001613 // add default net's dns entries
Robert Greenwaltc76b8fa2010-07-23 15:46:26 -07001614 NetworkStateTracker nt = mNetTrackers[netType];
1615 if (nt != null && nt.getNetworkInfo().isConnected() && !nt.isTeardownRequested()) {
Robert Greenwalt37e65eb2010-08-30 10:56:47 -07001616 LinkProperties p = nt.getLinkProperties();
Robert Greenwaltc76b8fa2010-07-23 15:46:26 -07001617 if (p == null) return;
1618 Collection<InetAddress> dnses = p.getDnses();
Robert Greenwalt10398722010-12-17 15:20:36 -08001619 boolean changed = false;
Robert Greenwaltc76b8fa2010-07-23 15:46:26 -07001620 if (mNetAttributes[netType].isDefault()) {
1621 int j = 1;
Robert Greenwalte90aa5e2010-09-01 11:34:05 -07001622 if (dnses.size() == 0 && mDefaultDns != null) {
Robert Greenwalt10398722010-12-17 15:20:36 -08001623 String dnsString = mDefaultDns.getHostAddress();
1624 if (!dnsString.equals(SystemProperties.get("net.dns1"))) {
1625 if (DBG) {
1626 log("no dns provided - using " + dnsString);
1627 }
1628 changed = true;
1629 SystemProperties.set("net.dns1", dnsString);
Robert Greenwalt42acef32009-08-12 16:08:25 -07001630 }
Robert Greenwalte90aa5e2010-09-01 11:34:05 -07001631 j++;
1632 } else {
1633 for (InetAddress dns : dnses) {
Robert Greenwalt10398722010-12-17 15:20:36 -08001634 String dnsString = dns.getHostAddress();
1635 if (!changed && dnsString.equals(SystemProperties.get("net.dns" + j))) {
1636 j++;
1637 continue;
1638 }
Robert Greenwalte90aa5e2010-09-01 11:34:05 -07001639 if (DBG) {
Wink Savilleed9c02b2010-12-03 12:01:38 -08001640 log("adding dns " + dns + " for " +
Robert Greenwalte90aa5e2010-09-01 11:34:05 -07001641 nt.getNetworkInfo().getTypeName());
1642 }
Robert Greenwalt10398722010-12-17 15:20:36 -08001643 changed = true;
1644 SystemProperties.set("net.dns" + j++, dnsString);
Robert Greenwalte90aa5e2010-09-01 11:34:05 -07001645 }
Robert Greenwaltc76b8fa2010-07-23 15:46:26 -07001646 }
1647 for (int k=j ; k<mNumDnsEntries; k++) {
Robert Greenwalt10398722010-12-17 15:20:36 -08001648 if (changed || !TextUtils.isEmpty(SystemProperties.get("net.dns" + k))) {
1649 if (DBG) log("erasing net.dns" + k);
1650 changed = true;
1651 SystemProperties.set("net.dns" + k, "");
1652 }
Robert Greenwaltc76b8fa2010-07-23 15:46:26 -07001653 }
1654 mNumDnsEntries = j;
1655 } else {
1656 // set per-pid dns for attached secondary nets
1657 List pids = mNetRequestersPids[netType];
1658 for (int y=0; y< pids.size(); y++) {
1659 Integer pid = (Integer)pids.get(y);
Robert Greenwalt10398722010-12-17 15:20:36 -08001660 changed = writePidDns(dnses, pid.intValue());
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001661 }
1662 }
Robert Greenwalt10398722010-12-17 15:20:36 -08001663 if (changed) bumpDns();
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001664 }
Robert Greenwalt42acef32009-08-12 16:08:25 -07001665 }
1666
1667 private int getRestoreDefaultNetworkDelay() {
1668 String restoreDefaultNetworkDelayStr = SystemProperties.get(
1669 NETWORK_RESTORE_DELAY_PROP_NAME);
1670 if(restoreDefaultNetworkDelayStr != null &&
1671 restoreDefaultNetworkDelayStr.length() != 0) {
1672 try {
1673 return Integer.valueOf(restoreDefaultNetworkDelayStr);
1674 } catch (NumberFormatException e) {
1675 }
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001676 }
Robert Greenwalt42acef32009-08-12 16:08:25 -07001677 return RESTORE_DEFAULT_NETWORK_DELAY;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001678 }
1679
1680 @Override
1681 protected void dump(FileDescriptor fd, PrintWriter pw, String[] args) {
Robert Greenwalt86e9e552009-07-16 17:21:39 -07001682 if (mContext.checkCallingOrSelfPermission(
1683 android.Manifest.permission.DUMP)
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001684 != PackageManager.PERMISSION_GRANTED) {
Robert Greenwalt86e9e552009-07-16 17:21:39 -07001685 pw.println("Permission Denial: can't dump ConnectivityService " +
1686 "from from pid=" + Binder.getCallingPid() + ", uid=" +
1687 Binder.getCallingUid());
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001688 return;
1689 }
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001690 pw.println();
1691 for (NetworkStateTracker nst : mNetTrackers) {
Robert Greenwaltb9285352009-12-21 18:24:07 -08001692 if (nst != null) {
1693 if (nst.getNetworkInfo().isConnected()) {
1694 pw.println("Active network: " + nst.getNetworkInfo().
1695 getTypeName());
1696 }
1697 pw.println(nst.getNetworkInfo());
1698 pw.println(nst);
1699 pw.println();
Robert Greenwalt42acef32009-08-12 16:08:25 -07001700 }
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001701 }
Robert Greenwaltb9285352009-12-21 18:24:07 -08001702
1703 pw.println("Network Requester Pids:");
1704 for (int net : mPriorityList) {
1705 String pidString = net + ": ";
1706 for (Object pid : mNetRequestersPids[net]) {
1707 pidString = pidString + pid.toString() + ", ";
1708 }
1709 pw.println(pidString);
1710 }
1711 pw.println();
1712
1713 pw.println("FeatureUsers:");
1714 for (Object requester : mFeatureUsers) {
1715 pw.println(requester.toString());
1716 }
1717 pw.println();
Robert Greenwalt2a091d72010-02-11 18:18:40 -08001718
Robert Greenwalt14f2ef42010-06-15 12:19:37 -07001719 synchronized (this) {
1720 pw.println("NetworkTranstionWakeLock is currently " +
1721 (mNetTransitionWakeLock.isHeld() ? "" : "not ") + "held.");
1722 pw.println("It was last requested for "+mNetTransitionWakeLockCausedBy);
1723 }
1724 pw.println();
1725
Robert Greenwalt2a091d72010-02-11 18:18:40 -08001726 mTethering.dump(fd, pw, args);
Robert Greenwalt4e8dfef2010-09-20 14:35:25 -07001727
1728 if (mInetLog != null) {
1729 pw.println();
1730 pw.println("Inet condition reports:");
1731 for(int i = 0; i < mInetLog.size(); i++) {
1732 pw.println(mInetLog.get(i));
1733 }
1734 }
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001735 }
1736
Robert Greenwalt42acef32009-08-12 16:08:25 -07001737 // must be stateless - things change under us.
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001738 private class MyHandler extends Handler {
Wink Savillebb08caf2010-09-02 19:23:52 -07001739 public MyHandler(Looper looper) {
1740 super(looper);
1741 }
1742
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001743 @Override
1744 public void handleMessage(Message msg) {
1745 NetworkInfo info;
1746 switch (msg.what) {
1747 case NetworkStateTracker.EVENT_STATE_CHANGED:
1748 info = (NetworkInfo) msg.obj;
Robert Greenwalt511288a2009-12-07 11:33:18 -08001749 int type = info.getType();
1750 NetworkInfo.State state = info.getState();
Robert Greenwalt6e6dec22010-01-25 16:14:00 -08001751 // only do this optimization for wifi. It going into scan mode for location
1752 // services generates alot of noise. Meanwhile the mms apn won't send out
1753 // subsequent notifications when on default cellular because it never
1754 // disconnects.. so only do this to wifi notifications. Fixed better when the
1755 // APN notifications are standardized.
1756 if (mNetAttributes[type].mLastState == state &&
1757 mNetAttributes[type].mRadio == ConnectivityManager.TYPE_WIFI) {
Robert Greenwalt511288a2009-12-07 11:33:18 -08001758 if (DBG) {
Robert Greenwalt6e6dec22010-01-25 16:14:00 -08001759 // TODO - remove this after we validate the dropping doesn't break
1760 // anything
Wink Savilleed9c02b2010-12-03 12:01:38 -08001761 log("Dropping ConnectivityChange for " +
Robert Greenwalt1193ae42010-01-13 09:36:31 -08001762 info.getTypeName() + ": " +
Robert Greenwalt511288a2009-12-07 11:33:18 -08001763 state + "/" + info.getDetailedState());
1764 }
1765 return;
1766 }
1767 mNetAttributes[type].mLastState = state;
1768
Wink Savilleed9c02b2010-12-03 12:01:38 -08001769 if (DBG) log("ConnectivityChange for " +
Robert Greenwalt86e9e552009-07-16 17:21:39 -07001770 info.getTypeName() + ": " +
Robert Greenwalt511288a2009-12-07 11:33:18 -08001771 state + "/" + info.getDetailedState());
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001772
1773 // Connectivity state changed:
1774 // [31-13] Reserved for future use
Robert Greenwalt86e9e552009-07-16 17:21:39 -07001775 // [12-9] Network subtype (for mobile network, as defined
1776 // by TelephonyManager)
1777 // [8-3] Detailed state ordinal (as defined by
1778 // NetworkInfo.DetailedState)
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001779 // [2-0] Network type (as defined by ConnectivityManager)
1780 int eventLogParam = (info.getType() & 0x7) |
1781 ((info.getDetailedState().ordinal() & 0x3f) << 3) |
1782 (info.getSubtype() << 9);
Doug Zongkerab5c49c2009-12-04 10:31:43 -08001783 EventLog.writeEvent(EventLogTags.CONNECTIVITY_STATE_CHANGED,
Robert Greenwalt86e9e552009-07-16 17:21:39 -07001784 eventLogParam);
1785
1786 if (info.getDetailedState() ==
1787 NetworkInfo.DetailedState.FAILED) {
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001788 handleConnectionFailure(info);
Robert Greenwalt511288a2009-12-07 11:33:18 -08001789 } else if (state == NetworkInfo.State.DISCONNECTED) {
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001790 handleDisconnect(info);
Robert Greenwalt511288a2009-12-07 11:33:18 -08001791 } else if (state == NetworkInfo.State.SUSPENDED) {
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001792 // TODO: need to think this over.
Robert Greenwalt86e9e552009-07-16 17:21:39 -07001793 // the logic here is, handle SUSPENDED the same as
1794 // DISCONNECTED. The only difference being we are
1795 // broadcasting an intent with NetworkInfo that's
1796 // suspended. This allows the applications an
1797 // opportunity to handle DISCONNECTED and SUSPENDED
1798 // differently, or not.
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001799 handleDisconnect(info);
Robert Greenwalt511288a2009-12-07 11:33:18 -08001800 } else if (state == NetworkInfo.State.CONNECTED) {
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001801 handleConnect(info);
1802 }
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001803 break;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001804 case NetworkStateTracker.EVENT_CONFIGURATION_CHANGED:
Robert Greenwaltc76b8fa2010-07-23 15:46:26 -07001805 info = (NetworkInfo) msg.obj;
1806 type = info.getType();
Robert Greenwalt434203a2010-10-11 16:00:27 -07001807 handleConnectivityChange(type);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001808 break;
Robert Greenwaltf3331232010-09-24 14:32:21 -07001809 case EVENT_CLEAR_NET_TRANSITION_WAKELOCK:
Robert Greenwalt14f2ef42010-06-15 12:19:37 -07001810 String causedBy = null;
1811 synchronized (ConnectivityService.this) {
1812 if (msg.arg1 == mNetTransitionWakeLockSerialNumber &&
1813 mNetTransitionWakeLock.isHeld()) {
1814 mNetTransitionWakeLock.release();
1815 causedBy = mNetTransitionWakeLockCausedBy;
1816 }
1817 }
1818 if (causedBy != null) {
Wink Savilleed9c02b2010-12-03 12:01:38 -08001819 log("NetTransition Wakelock for " + causedBy + " released by timeout");
Robert Greenwalt14f2ef42010-06-15 12:19:37 -07001820 }
Robert Greenwalt057d5e92010-09-09 14:05:10 -07001821 break;
Robert Greenwalt8dcc28b2010-09-23 10:05:56 -07001822 case EVENT_RESTORE_DEFAULT_NETWORK:
Robert Greenwalt9c75d4a2009-09-27 17:27:04 -07001823 FeatureUser u = (FeatureUser)msg.obj;
1824 u.expire();
Robert Greenwaltd7085fc2010-09-08 15:24:47 -07001825 break;
Robert Greenwalt8dcc28b2010-09-23 10:05:56 -07001826 case EVENT_INET_CONDITION_CHANGE:
1827 {
1828 int netType = msg.arg1;
1829 int condition = msg.arg2;
1830 handleInetConditionChange(netType, condition);
Robert Greenwalt14f2ef42010-06-15 12:19:37 -07001831 break;
Robert Greenwalt8dcc28b2010-09-23 10:05:56 -07001832 }
1833 case EVENT_INET_CONDITION_HOLD_END:
1834 {
1835 int netType = msg.arg1;
1836 int sequence = msg.arg2;
1837 handleInetConditionHoldEnd(netType, sequence);
Robert Greenwaltd7085fc2010-09-08 15:24:47 -07001838 break;
Robert Greenwalt8dcc28b2010-09-23 10:05:56 -07001839 }
1840 case EVENT_SET_NETWORK_PREFERENCE:
1841 {
1842 int preference = msg.arg1;
1843 handleSetNetworkPreference(preference);
1844 break;
1845 }
1846 case EVENT_SET_BACKGROUND_DATA:
1847 {
1848 boolean enabled = (msg.arg1 == ENABLED);
1849 handleSetBackgroundData(enabled);
1850 break;
1851 }
1852 case EVENT_SET_MOBILE_DATA:
1853 {
1854 boolean enabled = (msg.arg1 == ENABLED);
1855 handleSetMobileData(enabled);
1856 break;
1857 }
Robert Greenwalt434203a2010-10-11 16:00:27 -07001858 case EVENT_APPLY_GLOBAL_HTTP_PROXY:
1859 {
1860 handleDeprecatedGlobalHttpProxy();
1861 }
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001862 }
1863 }
1864 }
Robert Greenwaltd0e18ff2010-01-26 11:40:34 -08001865
1866 // javadoc from interface
Robert Greenwalt5a735062010-03-02 17:25:02 -08001867 public int tether(String iface) {
Robert Greenwaltd0e18ff2010-01-26 11:40:34 -08001868 enforceTetherChangePermission();
Robert Greenwalt5a735062010-03-02 17:25:02 -08001869
1870 if (isTetheringSupported()) {
1871 return mTethering.tether(iface);
1872 } else {
1873 return ConnectivityManager.TETHER_ERROR_UNSUPPORTED;
1874 }
Robert Greenwaltd0e18ff2010-01-26 11:40:34 -08001875 }
1876
1877 // javadoc from interface
Robert Greenwalt5a735062010-03-02 17:25:02 -08001878 public int untether(String iface) {
Robert Greenwaltd0e18ff2010-01-26 11:40:34 -08001879 enforceTetherChangePermission();
Robert Greenwalt5a735062010-03-02 17:25:02 -08001880
1881 if (isTetheringSupported()) {
1882 return mTethering.untether(iface);
1883 } else {
1884 return ConnectivityManager.TETHER_ERROR_UNSUPPORTED;
1885 }
1886 }
1887
1888 // javadoc from interface
1889 public int getLastTetherError(String iface) {
1890 enforceTetherAccessPermission();
1891
1892 if (isTetheringSupported()) {
1893 return mTethering.getLastTetherError(iface);
1894 } else {
1895 return ConnectivityManager.TETHER_ERROR_UNSUPPORTED;
1896 }
Robert Greenwalt2a091d72010-02-11 18:18:40 -08001897 }
1898
1899 // TODO - proper iface API for selection by property, inspection, etc
1900 public String[] getTetherableUsbRegexs() {
1901 enforceTetherAccessPermission();
1902 if (isTetheringSupported()) {
1903 return mTethering.getTetherableUsbRegexs();
1904 } else {
1905 return new String[0];
1906 }
1907 }
1908
1909 public String[] getTetherableWifiRegexs() {
1910 enforceTetherAccessPermission();
1911 if (isTetheringSupported()) {
1912 return mTethering.getTetherableWifiRegexs();
1913 } else {
1914 return new String[0];
1915 }
Robert Greenwaltd0e18ff2010-01-26 11:40:34 -08001916 }
1917
Danica Chang6fdd0c62010-08-11 14:54:43 -07001918 public String[] getTetherableBluetoothRegexs() {
1919 enforceTetherAccessPermission();
1920 if (isTetheringSupported()) {
1921 return mTethering.getTetherableBluetoothRegexs();
1922 } else {
1923 return new String[0];
1924 }
1925 }
1926
Robert Greenwaltd0e18ff2010-01-26 11:40:34 -08001927 // TODO - move iface listing, queries, etc to new module
1928 // javadoc from interface
1929 public String[] getTetherableIfaces() {
Robert Greenwalt2a091d72010-02-11 18:18:40 -08001930 enforceTetherAccessPermission();
Robert Greenwaltd0e18ff2010-01-26 11:40:34 -08001931 return mTethering.getTetherableIfaces();
1932 }
1933
1934 public String[] getTetheredIfaces() {
Robert Greenwalt2a091d72010-02-11 18:18:40 -08001935 enforceTetherAccessPermission();
Robert Greenwaltd0e18ff2010-01-26 11:40:34 -08001936 return mTethering.getTetheredIfaces();
1937 }
Robert Greenwalt2a091d72010-02-11 18:18:40 -08001938
Robert Greenwalt5a735062010-03-02 17:25:02 -08001939 public String[] getTetheringErroredIfaces() {
1940 enforceTetherAccessPermission();
1941 return mTethering.getErroredIfaces();
1942 }
1943
Robert Greenwalt2a091d72010-02-11 18:18:40 -08001944 // if ro.tether.denied = true we default to no tethering
1945 // gservices could set the secure setting to 1 though to enable it on a build where it
1946 // had previously been turned off.
1947 public boolean isTetheringSupported() {
1948 enforceTetherAccessPermission();
1949 int defaultVal = (SystemProperties.get("ro.tether.denied").equals("true") ? 0 : 1);
Robert Greenwaltc9d5fb72010-02-25 12:29:30 -08001950 boolean tetherEnabledInSettings = (Settings.Secure.getInt(mContext.getContentResolver(),
1951 Settings.Secure.TETHER_SUPPORTED, defaultVal) != 0);
1952 return tetherEnabledInSettings && mTetheringConfigValid;
Robert Greenwalt2a091d72010-02-11 18:18:40 -08001953 }
Robert Greenwalt14f2ef42010-06-15 12:19:37 -07001954
1955 // An API NetworkStateTrackers can call when they lose their network.
1956 // This will automatically be cleared after X seconds or a network becomes CONNECTED,
1957 // whichever happens first. The timer is started by the first caller and not
1958 // restarted by subsequent callers.
1959 public void requestNetworkTransitionWakelock(String forWhom) {
1960 enforceConnectivityInternalPermission();
1961 synchronized (this) {
1962 if (mNetTransitionWakeLock.isHeld()) return;
1963 mNetTransitionWakeLockSerialNumber++;
1964 mNetTransitionWakeLock.acquire();
1965 mNetTransitionWakeLockCausedBy = forWhom;
1966 }
1967 mHandler.sendMessageDelayed(mHandler.obtainMessage(
Robert Greenwaltf3331232010-09-24 14:32:21 -07001968 EVENT_CLEAR_NET_TRANSITION_WAKELOCK,
Robert Greenwalt14f2ef42010-06-15 12:19:37 -07001969 mNetTransitionWakeLockSerialNumber, 0),
1970 mNetTransitionWakeLockTimeout);
1971 return;
1972 }
Robert Greenwaltca4306c2010-09-09 13:15:32 -07001973
Robert Greenwaltd7085fc2010-09-08 15:24:47 -07001974 // 100 percent is full good, 0 is full bad.
1975 public void reportInetCondition(int networkType, int percentage) {
Wink Savilleed9c02b2010-12-03 12:01:38 -08001976 if (DBG) log("reportNetworkCondition(" + networkType + ", " + percentage + ")");
Robert Greenwaltd7085fc2010-09-08 15:24:47 -07001977 mContext.enforceCallingOrSelfPermission(
1978 android.Manifest.permission.STATUS_BAR,
1979 "ConnectivityService");
1980
Robert Greenwalt4e8dfef2010-09-20 14:35:25 -07001981 if (DBG) {
1982 int pid = getCallingPid();
1983 int uid = getCallingUid();
1984 String s = pid + "(" + uid + ") reports inet is " +
1985 (percentage > 50 ? "connected" : "disconnected") + " (" + percentage + ") on " +
1986 "network Type " + networkType + " at " + GregorianCalendar.getInstance().getTime();
1987 mInetLog.add(s);
1988 while(mInetLog.size() > INET_CONDITION_LOG_MAX_SIZE) {
1989 mInetLog.remove(0);
1990 }
1991 }
Robert Greenwaltd7085fc2010-09-08 15:24:47 -07001992 mHandler.sendMessage(mHandler.obtainMessage(
Robert Greenwalt8dcc28b2010-09-23 10:05:56 -07001993 EVENT_INET_CONDITION_CHANGE, networkType, percentage));
1994 }
1995
1996 private void handleInetConditionChange(int netType, int condition) {
1997 if (DBG) {
Wink Savilleed9c02b2010-12-03 12:01:38 -08001998 log("Inet connectivity change, net=" +
Robert Greenwalt8dcc28b2010-09-23 10:05:56 -07001999 netType + ", condition=" + condition +
2000 ",mActiveDefaultNetwork=" + mActiveDefaultNetwork);
2001 }
2002 if (mActiveDefaultNetwork == -1) {
Wink Savilleed9c02b2010-12-03 12:01:38 -08002003 if (DBG) log("no active default network - aborting");
Robert Greenwalt8dcc28b2010-09-23 10:05:56 -07002004 return;
2005 }
2006 if (mActiveDefaultNetwork != netType) {
Wink Savilleed9c02b2010-12-03 12:01:38 -08002007 if (DBG) log("given net not default - aborting");
Robert Greenwalt8dcc28b2010-09-23 10:05:56 -07002008 return;
2009 }
2010 mDefaultInetCondition = condition;
2011 int delay;
2012 if (mInetConditionChangeInFlight == false) {
Wink Savilleed9c02b2010-12-03 12:01:38 -08002013 if (DBG) log("starting a change hold");
Robert Greenwalt8dcc28b2010-09-23 10:05:56 -07002014 // setup a new hold to debounce this
2015 if (mDefaultInetCondition > 50) {
2016 delay = Settings.Secure.getInt(mContext.getContentResolver(),
2017 Settings.Secure.INET_CONDITION_DEBOUNCE_UP_DELAY, 500);
2018 } else {
2019 delay = Settings.Secure.getInt(mContext.getContentResolver(),
2020 Settings.Secure.INET_CONDITION_DEBOUNCE_DOWN_DELAY, 3000);
2021 }
2022 mInetConditionChangeInFlight = true;
2023 mHandler.sendMessageDelayed(mHandler.obtainMessage(EVENT_INET_CONDITION_HOLD_END,
2024 mActiveDefaultNetwork, mDefaultConnectionSequence), delay);
2025 } else {
2026 // we've set the new condition, when this hold ends that will get
2027 // picked up
Wink Savilleed9c02b2010-12-03 12:01:38 -08002028 if (DBG) log("currently in hold - not setting new end evt");
Robert Greenwalt8dcc28b2010-09-23 10:05:56 -07002029 }
2030 }
2031
2032 private void handleInetConditionHoldEnd(int netType, int sequence) {
2033 if (DBG) {
Wink Savilleed9c02b2010-12-03 12:01:38 -08002034 log("Inet hold end, net=" + netType +
Robert Greenwalt8dcc28b2010-09-23 10:05:56 -07002035 ", condition =" + mDefaultInetCondition +
2036 ", published condition =" + mDefaultInetConditionPublished);
2037 }
2038 mInetConditionChangeInFlight = false;
2039
2040 if (mActiveDefaultNetwork == -1) {
Wink Savilleed9c02b2010-12-03 12:01:38 -08002041 if (DBG) log("no active default network - aborting");
Robert Greenwalt8dcc28b2010-09-23 10:05:56 -07002042 return;
2043 }
2044 if (mDefaultConnectionSequence != sequence) {
Wink Savilleed9c02b2010-12-03 12:01:38 -08002045 if (DBG) log("event hold for obsolete network - aborting");
Robert Greenwalt8dcc28b2010-09-23 10:05:56 -07002046 return;
2047 }
2048 if (mDefaultInetConditionPublished == mDefaultInetCondition) {
Wink Savilleed9c02b2010-12-03 12:01:38 -08002049 if (DBG) log("no change in condition - aborting");
Robert Greenwalt8dcc28b2010-09-23 10:05:56 -07002050 return;
2051 }
2052 NetworkInfo networkInfo = mNetTrackers[mActiveDefaultNetwork].getNetworkInfo();
2053 if (networkInfo.isConnected() == false) {
Wink Savilleed9c02b2010-12-03 12:01:38 -08002054 if (DBG) log("default network not connected - aborting");
Robert Greenwalt8dcc28b2010-09-23 10:05:56 -07002055 return;
2056 }
2057 mDefaultInetConditionPublished = mDefaultInetCondition;
2058 sendInetConditionBroadcast(networkInfo);
2059 return;
Robert Greenwaltd7085fc2010-09-08 15:24:47 -07002060 }
Robert Greenwalt434203a2010-10-11 16:00:27 -07002061
2062 public synchronized ProxyProperties getProxy() {
2063 if (mGlobalProxy != null) return mGlobalProxy;
2064 if (mDefaultProxy != null) return mDefaultProxy;
2065 return null;
2066 }
2067
2068 public void setGlobalProxy(ProxyProperties proxyProperties) {
2069 enforceChangePermission();
2070 synchronized (mGlobalProxyLock) {
2071 if (proxyProperties == mGlobalProxy) return;
2072 if (proxyProperties != null && proxyProperties.equals(mGlobalProxy)) return;
2073 if (mGlobalProxy != null && mGlobalProxy.equals(proxyProperties)) return;
2074
2075 String host = "";
2076 int port = 0;
2077 String exclList = "";
2078 if (proxyProperties != null && !TextUtils.isEmpty(proxyProperties.getHost())) {
2079 mGlobalProxy = new ProxyProperties(proxyProperties);
2080 host = mGlobalProxy.getHost();
2081 port = mGlobalProxy.getPort();
2082 exclList = mGlobalProxy.getExclusionList();
2083 } else {
2084 mGlobalProxy = null;
2085 }
2086 ContentResolver res = mContext.getContentResolver();
2087 Settings.Secure.putString(res, Settings.Secure.GLOBAL_HTTP_PROXY_HOST, host);
2088 Settings.Secure.putInt(res, Settings.Secure.GLOBAL_HTTP_PROXY_PORT, port);
Robert Greenwaltb7090d62010-12-02 11:31:00 -08002089 Settings.Secure.putString(res, Settings.Secure.GLOBAL_HTTP_PROXY_EXCLUSION_LIST,
Robert Greenwalt434203a2010-10-11 16:00:27 -07002090 exclList);
2091 }
2092
2093 if (mGlobalProxy == null) {
2094 proxyProperties = mDefaultProxy;
2095 }
2096 sendProxyBroadcast(proxyProperties);
2097 }
2098
Robert Greenwaltb7090d62010-12-02 11:31:00 -08002099 private void loadGlobalProxy() {
2100 ContentResolver res = mContext.getContentResolver();
2101 String host = Settings.Secure.getString(res, Settings.Secure.GLOBAL_HTTP_PROXY_HOST);
2102 int port = Settings.Secure.getInt(res, Settings.Secure.GLOBAL_HTTP_PROXY_PORT, 0);
2103 String exclList = Settings.Secure.getString(res,
2104 Settings.Secure.GLOBAL_HTTP_PROXY_EXCLUSION_LIST);
2105 if (!TextUtils.isEmpty(host)) {
2106 ProxyProperties proxyProperties = new ProxyProperties(host, port, exclList);
2107 synchronized (mGlobalProxyLock) {
2108 mGlobalProxy = proxyProperties;
2109 }
2110 }
2111 }
2112
Robert Greenwalt434203a2010-10-11 16:00:27 -07002113 public ProxyProperties getGlobalProxy() {
2114 synchronized (mGlobalProxyLock) {
2115 return mGlobalProxy;
2116 }
2117 }
2118
2119 private void handleApplyDefaultProxy(int type) {
2120 // check if new default - push it out to all VM if so
2121 ProxyProperties proxy = mNetTrackers[type].getLinkProperties().getHttpProxy();
2122 synchronized (this) {
2123 if (mDefaultProxy != null && mDefaultProxy.equals(proxy)) return;
2124 if (mDefaultProxy == proxy) return;
2125 if (!TextUtils.isEmpty(proxy.getHost())) {
2126 mDefaultProxy = proxy;
2127 } else {
2128 mDefaultProxy = null;
2129 }
2130 }
Wink Savilleed9c02b2010-12-03 12:01:38 -08002131 if (DBG) log("changing default proxy to " + proxy);
Robert Greenwalt434203a2010-10-11 16:00:27 -07002132 if ((proxy == null && mGlobalProxy == null) || proxy.equals(mGlobalProxy)) return;
2133 if (mGlobalProxy != null) return;
2134 sendProxyBroadcast(proxy);
2135 }
2136
2137 private void handleDeprecatedGlobalHttpProxy() {
2138 String proxy = Settings.Secure.getString(mContext.getContentResolver(),
2139 Settings.Secure.HTTP_PROXY);
2140 if (!TextUtils.isEmpty(proxy)) {
2141 String data[] = proxy.split(":");
2142 String proxyHost = data[0];
2143 int proxyPort = 8080;
2144 if (data.length > 1) {
2145 try {
2146 proxyPort = Integer.parseInt(data[1]);
2147 } catch (NumberFormatException e) {
2148 return;
2149 }
2150 }
2151 ProxyProperties p = new ProxyProperties(data[0], proxyPort, "");
2152 setGlobalProxy(p);
2153 }
2154 }
2155
2156 private void sendProxyBroadcast(ProxyProperties proxy) {
Wink Savilleed9c02b2010-12-03 12:01:38 -08002157 log("sending Proxy Broadcast for " + proxy);
Robert Greenwalt434203a2010-10-11 16:00:27 -07002158 Intent intent = new Intent(Proxy.PROXY_CHANGE_ACTION);
2159 intent.addFlags(Intent.FLAG_RECEIVER_REPLACE_PENDING);
2160 intent.putExtra(Proxy.EXTRA_PROXY_INFO, proxy);
Robert Greenwalta2e13392010-12-06 11:29:17 -08002161 mContext.sendStickyBroadcast(intent);
Robert Greenwalt434203a2010-10-11 16:00:27 -07002162 }
2163
2164 private static class SettingsObserver extends ContentObserver {
2165 private int mWhat;
2166 private Handler mHandler;
2167 SettingsObserver(Handler handler, int what) {
2168 super(handler);
2169 mHandler = handler;
2170 mWhat = what;
2171 }
2172
2173 void observe(Context context) {
2174 ContentResolver resolver = context.getContentResolver();
2175 resolver.registerContentObserver(Settings.Secure.getUriFor(
2176 Settings.Secure.HTTP_PROXY), false, this);
2177 }
2178
2179 @Override
2180 public void onChange(boolean selfChange) {
2181 mHandler.obtainMessage(mWhat).sendToTarget();
2182 }
2183 }
Wink Savilleed9c02b2010-12-03 12:01:38 -08002184
2185 private void log(String s) {
2186 Slog.d(TAG, s);
2187 }
2188
2189 private void loge(String s) {
2190 Slog.e(TAG, s);
2191 }
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002192}