blob: eeaeadc948875cd1fadc5bf89e5390789dedf510 [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 Greenwaltd825ea42010-12-29 16:15:02 -080062import java.util.concurrent.atomic.AtomicBoolean;
Robert Greenwalt4e8dfef2010-09-20 14:35:25 -070063import java.util.GregorianCalendar;
Robert Greenwalt42acef32009-08-12 16:08:25 -070064import java.util.List;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080065
66/**
67 * @hide
68 */
69public class ConnectivityService extends IConnectivityManager.Stub {
70
Robert Greenwaltba175a52010-10-05 19:12:26 -070071 private static final boolean DBG = true;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080072 private static final String TAG = "ConnectivityService";
73
Robert Greenwalt42acef32009-08-12 16:08:25 -070074 // how long to wait before switching back to a radio's default network
75 private static final int RESTORE_DEFAULT_NETWORK_DELAY = 1 * 60 * 1000;
76 // system property that can override the above value
77 private static final String NETWORK_RESTORE_DELAY_PROP_NAME =
78 "android.telephony.apn-restore";
79
Robert Greenwaltd0e18ff2010-01-26 11:40:34 -080080 private Tethering mTethering;
Robert Greenwaltc9d5fb72010-02-25 12:29:30 -080081 private boolean mTetheringConfigValid = false;
Robert Greenwaltd0e18ff2010-01-26 11:40:34 -080082
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080083 /**
84 * Sometimes we want to refer to the individual network state
85 * trackers separately, and sometimes we just want to treat them
86 * abstractly.
87 */
88 private NetworkStateTracker mNetTrackers[];
Robert Greenwalt42acef32009-08-12 16:08:25 -070089
90 /**
91 * A per Net list of the PID's that requested access to the net
92 * used both as a refcount and for per-PID DNS selection
93 */
94 private List mNetRequestersPids[];
95
Irfan Sheriffa2a1b912010-06-07 09:03:04 -070096 private WifiWatchdogService mWifiWatchdogService;
97
Robert Greenwalt42acef32009-08-12 16:08:25 -070098 // priority order of the nettrackers
99 // (excluding dynamically set mNetworkPreference)
100 // TODO - move mNetworkTypePreference into this
101 private int[] mPriorityList;
102
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800103 private Context mContext;
104 private int mNetworkPreference;
Robert Greenwalt42acef32009-08-12 16:08:25 -0700105 private int mActiveDefaultNetwork = -1;
Robert Greenwaltd7085fc2010-09-08 15:24:47 -0700106 // 0 is full bad, 100 is full good
107 private int mDefaultInetCondition = 0;
108 private int mDefaultInetConditionPublished = 0;
109 private boolean mInetConditionChangeInFlight = false;
110 private int mDefaultConnectionSequence = 0;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800111
112 private int mNumDnsEntries;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800113
114 private boolean mTestMode;
Joe Onorato00092872010-09-01 21:18:22 -0700115 private static ConnectivityService sServiceInstance;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800116
Robert Greenwaltd825ea42010-12-29 16:15:02 -0800117 private AtomicBoolean mBackgroundDataEnabled = new AtomicBoolean(true);
118
Robert Greenwalt8dcc28b2010-09-23 10:05:56 -0700119 private static final int ENABLED = 1;
120 private static final int DISABLED = 0;
121
122 // Share the event space with NetworkStateTracker (which can't see this
123 // internal class but sends us events). If you change these, change
124 // NetworkStateTracker.java too.
125 private static final int MIN_NETWORK_STATE_TRACKER_EVENT = 1;
126 private static final int MAX_NETWORK_STATE_TRACKER_EVENT = 100;
127
128 /**
129 * used internally as a delayed event to make us switch back to the
130 * default network
131 */
132 private static final int EVENT_RESTORE_DEFAULT_NETWORK =
133 MAX_NETWORK_STATE_TRACKER_EVENT + 1;
134
135 /**
136 * used internally to change our mobile data enabled flag
137 */
138 private static final int EVENT_CHANGE_MOBILE_DATA_ENABLED =
139 MAX_NETWORK_STATE_TRACKER_EVENT + 2;
140
141 /**
142 * used internally to change our network preference setting
143 * arg1 = networkType to prefer
144 */
145 private static final int EVENT_SET_NETWORK_PREFERENCE =
146 MAX_NETWORK_STATE_TRACKER_EVENT + 3;
147
148 /**
149 * used internally to synchronize inet condition reports
150 * arg1 = networkType
151 * arg2 = condition (0 bad, 100 good)
152 */
153 private static final int EVENT_INET_CONDITION_CHANGE =
154 MAX_NETWORK_STATE_TRACKER_EVENT + 4;
155
156 /**
157 * used internally to mark the end of inet condition hold periods
158 * arg1 = networkType
159 */
160 private static final int EVENT_INET_CONDITION_HOLD_END =
161 MAX_NETWORK_STATE_TRACKER_EVENT + 5;
162
163 /**
164 * used internally to set the background data preference
165 * arg1 = TRUE for enabled, FALSE for disabled
166 */
167 private static final int EVENT_SET_BACKGROUND_DATA =
168 MAX_NETWORK_STATE_TRACKER_EVENT + 6;
169
170 /**
171 * used internally to set enable/disable cellular data
172 * arg1 = ENBALED or DISABLED
173 */
174 private static final int EVENT_SET_MOBILE_DATA =
175 MAX_NETWORK_STATE_TRACKER_EVENT + 7;
176
Robert Greenwaltf3331232010-09-24 14:32:21 -0700177 /**
178 * used internally to clear a wakelock when transitioning
179 * from one net to another
180 */
181 private static final int EVENT_CLEAR_NET_TRANSITION_WAKELOCK =
182 MAX_NETWORK_STATE_TRACKER_EVENT + 8;
183
Robert Greenwalt434203a2010-10-11 16:00:27 -0700184 /**
185 * used internally to reload global proxy settings
186 */
187 private static final int EVENT_APPLY_GLOBAL_HTTP_PROXY =
188 MAX_NETWORK_STATE_TRACKER_EVENT + 9;
189
Robert Greenwalt42acef32009-08-12 16:08:25 -0700190 private Handler mHandler;
191
192 // list of DeathRecipients used to make sure features are turned off when
193 // a process dies
194 private List mFeatureUsers;
195
Mike Lockwood0f79b542009-08-14 14:18:49 -0400196 private boolean mSystemReady;
Dianne Hackborn1c633fc2009-12-08 19:45:14 -0800197 private Intent mInitialBroadcast;
Mike Lockwood0f79b542009-08-14 14:18:49 -0400198
Robert Greenwalt14f2ef42010-06-15 12:19:37 -0700199 private PowerManager.WakeLock mNetTransitionWakeLock;
200 private String mNetTransitionWakeLockCausedBy = "";
201 private int mNetTransitionWakeLockSerialNumber;
202 private int mNetTransitionWakeLockTimeout;
203
Robert Greenwalte90aa5e2010-09-01 11:34:05 -0700204 private InetAddress mDefaultDns;
205
Robert Greenwalt4e8dfef2010-09-20 14:35:25 -0700206 // used in DBG mode to track inet condition reports
207 private static final int INET_CONDITION_LOG_MAX_SIZE = 15;
208 private ArrayList mInetLog;
209
Robert Greenwalt434203a2010-10-11 16:00:27 -0700210 // track the current default http proxy - tell the world if we get a new one (real change)
211 private ProxyProperties mDefaultProxy = null;
212 // track the global proxy.
213 private ProxyProperties mGlobalProxy = null;
214 private final Object mGlobalProxyLock = new Object();
215
216 private SettingsObserver mSettingsObserver;
217
Robert Greenwalt511288a2009-12-07 11:33:18 -0800218 private static class NetworkAttributes {
Robert Greenwalt42acef32009-08-12 16:08:25 -0700219 /**
220 * Class for holding settings read from resources.
221 */
222 public String mName;
223 public int mType;
224 public int mRadio;
225 public int mPriority;
Robert Greenwalt511288a2009-12-07 11:33:18 -0800226 public NetworkInfo.State mLastState;
Robert Greenwalt42acef32009-08-12 16:08:25 -0700227 public NetworkAttributes(String init) {
228 String fragments[] = init.split(",");
229 mName = fragments[0].toLowerCase();
Robert Greenwalt5154ae762009-10-30 14:17:42 -0700230 mType = Integer.parseInt(fragments[1]);
231 mRadio = Integer.parseInt(fragments[2]);
232 mPriority = Integer.parseInt(fragments[3]);
Robert Greenwalt511288a2009-12-07 11:33:18 -0800233 mLastState = NetworkInfo.State.UNKNOWN;
Robert Greenwalt42acef32009-08-12 16:08:25 -0700234 }
235 public boolean isDefault() {
236 return (mType == mRadio);
237 }
238 }
239 NetworkAttributes[] mNetAttributes;
Robert Greenwalt5154ae762009-10-30 14:17:42 -0700240 int mNetworksDefined;
Robert Greenwalt42acef32009-08-12 16:08:25 -0700241
Robert Greenwalt511288a2009-12-07 11:33:18 -0800242 private static class RadioAttributes {
Robert Greenwalt42acef32009-08-12 16:08:25 -0700243 public int mSimultaneity;
244 public int mType;
245 public RadioAttributes(String init) {
246 String fragments[] = init.split(",");
Robert Greenwalt5154ae762009-10-30 14:17:42 -0700247 mType = Integer.parseInt(fragments[0]);
248 mSimultaneity = Integer.parseInt(fragments[1]);
Robert Greenwalt42acef32009-08-12 16:08:25 -0700249 }
250 }
251 RadioAttributes[] mRadioAttributes;
252
Wink Savillebb08caf2010-09-02 19:23:52 -0700253 public static synchronized ConnectivityService getInstance(Context context) {
254 if (sServiceInstance == null) {
255 sServiceInstance = new ConnectivityService(context);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800256 }
Wink Savillebb08caf2010-09-02 19:23:52 -0700257 return sServiceInstance;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800258 }
Robert Greenwalt86e9e552009-07-16 17:21:39 -0700259
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800260 private ConnectivityService(Context context) {
Wink Savilleed9c02b2010-12-03 12:01:38 -0800261 if (DBG) log("ConnectivityService starting up");
Robert Greenwaltde8383c2010-01-14 17:47:58 -0800262
Wink Savillebb08caf2010-09-02 19:23:52 -0700263 HandlerThread handlerThread = new HandlerThread("ConnectivityServiceThread");
264 handlerThread.start();
265 mHandler = new MyHandler(handlerThread.getLooper());
266
Robert Greenwaltd825ea42010-12-29 16:15:02 -0800267 mBackgroundDataEnabled.set(Settings.Secure.getInt(context.getContentResolver(),
268 Settings.Secure.BACKGROUND_DATA, 1) == 1);
269
Robert Greenwaltde8383c2010-01-14 17:47:58 -0800270 // setup our unique device name
Robert Greenwalt733c6292010-12-06 09:30:17 -0800271 if (TextUtils.isEmpty(SystemProperties.get("net.hostname"))) {
272 String id = Settings.Secure.getString(context.getContentResolver(),
273 Settings.Secure.ANDROID_ID);
274 if (id != null && id.length() > 0) {
275 String name = new String("android_").concat(id);
276 SystemProperties.set("net.hostname", name);
277 }
Robert Greenwaltde8383c2010-01-14 17:47:58 -0800278 }
279
Robert Greenwalte90aa5e2010-09-01 11:34:05 -0700280 // read our default dns server ip
281 String dns = Settings.Secure.getString(context.getContentResolver(),
282 Settings.Secure.DEFAULT_DNS_SERVER);
283 if (dns == null || dns.length() == 0) {
284 dns = context.getResources().getString(
285 com.android.internal.R.string.config_default_dns_server);
286 }
287 try {
288 mDefaultDns = InetAddress.getByName(dns);
289 } catch (UnknownHostException e) {
Wink Savilleed9c02b2010-12-03 12:01:38 -0800290 loge("Error setting defaultDns using " + dns);
Robert Greenwalte90aa5e2010-09-01 11:34:05 -0700291 }
292
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800293 mContext = context;
Robert Greenwalt14f2ef42010-06-15 12:19:37 -0700294
295 PowerManager powerManager = (PowerManager)mContext.getSystemService(Context.POWER_SERVICE);
296 mNetTransitionWakeLock = powerManager.newWakeLock(PowerManager.PARTIAL_WAKE_LOCK, TAG);
297 mNetTransitionWakeLockTimeout = mContext.getResources().getInteger(
298 com.android.internal.R.integer.config_networkTransitionTimeout);
299
Robert Greenwalt42acef32009-08-12 16:08:25 -0700300 mNetTrackers = new NetworkStateTracker[
301 ConnectivityManager.MAX_NETWORK_TYPE+1];
Robert Greenwalt86e9e552009-07-16 17:21:39 -0700302
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800303 mNetworkPreference = getPersistedNetworkPreference();
Robert Greenwalt86e9e552009-07-16 17:21:39 -0700304
Robert Greenwalt5154ae762009-10-30 14:17:42 -0700305 mRadioAttributes = new RadioAttributes[ConnectivityManager.MAX_RADIO_TYPE+1];
306 mNetAttributes = new NetworkAttributes[ConnectivityManager.MAX_NETWORK_TYPE+1];
307
Robert Greenwalt42acef32009-08-12 16:08:25 -0700308 // Load device network attributes from resources
Robert Greenwalt42acef32009-08-12 16:08:25 -0700309 String[] raStrings = context.getResources().getStringArray(
310 com.android.internal.R.array.radioAttributes);
Robert Greenwalt5154ae762009-10-30 14:17:42 -0700311 for (String raString : raStrings) {
312 RadioAttributes r = new RadioAttributes(raString);
313 if (r.mType > ConnectivityManager.MAX_RADIO_TYPE) {
Wink Savilleed9c02b2010-12-03 12:01:38 -0800314 loge("Error in radioAttributes - ignoring attempt to define type " + r.mType);
Robert Greenwalt5154ae762009-10-30 14:17:42 -0700315 continue;
316 }
317 if (mRadioAttributes[r.mType] != null) {
Wink Savilleed9c02b2010-12-03 12:01:38 -0800318 loge("Error in radioAttributes - ignoring attempt to redefine type " +
Robert Greenwalt5154ae762009-10-30 14:17:42 -0700319 r.mType);
320 continue;
321 }
Robert Greenwalt42acef32009-08-12 16:08:25 -0700322 mRadioAttributes[r.mType] = r;
323 }
324
Robert Greenwalt5154ae762009-10-30 14:17:42 -0700325 String[] naStrings = context.getResources().getStringArray(
326 com.android.internal.R.array.networkAttributes);
327 for (String naString : naStrings) {
328 try {
329 NetworkAttributes n = new NetworkAttributes(naString);
330 if (n.mType > ConnectivityManager.MAX_NETWORK_TYPE) {
Wink Savilleed9c02b2010-12-03 12:01:38 -0800331 loge("Error in networkAttributes - ignoring attempt to define type " +
Robert Greenwalt5154ae762009-10-30 14:17:42 -0700332 n.mType);
333 continue;
Robert Greenwalt42acef32009-08-12 16:08:25 -0700334 }
Robert Greenwalt5154ae762009-10-30 14:17:42 -0700335 if (mNetAttributes[n.mType] != null) {
Wink Savilleed9c02b2010-12-03 12:01:38 -0800336 loge("Error in networkAttributes - ignoring attempt to redefine type " +
Robert Greenwalt5154ae762009-10-30 14:17:42 -0700337 n.mType);
338 continue;
339 }
340 if (mRadioAttributes[n.mRadio] == null) {
Wink Savilleed9c02b2010-12-03 12:01:38 -0800341 loge("Error in networkAttributes - ignoring attempt to use undefined " +
Robert Greenwalt5154ae762009-10-30 14:17:42 -0700342 "radio " + n.mRadio + " in network type " + n.mType);
343 continue;
344 }
345 mNetAttributes[n.mType] = n;
346 mNetworksDefined++;
347 } catch(Exception e) {
348 // ignore it - leave the entry null
Robert Greenwalt42acef32009-08-12 16:08:25 -0700349 }
350 }
351
Robert Greenwalt5154ae762009-10-30 14:17:42 -0700352 // high priority first
353 mPriorityList = new int[mNetworksDefined];
354 {
355 int insertionPoint = mNetworksDefined-1;
356 int currentLowest = 0;
357 int nextLowest = 0;
358 while (insertionPoint > -1) {
359 for (NetworkAttributes na : mNetAttributes) {
360 if (na == null) continue;
361 if (na.mPriority < currentLowest) continue;
362 if (na.mPriority > currentLowest) {
363 if (na.mPriority < nextLowest || nextLowest == 0) {
364 nextLowest = na.mPriority;
365 }
366 continue;
367 }
368 mPriorityList[insertionPoint--] = na.mType;
369 }
370 currentLowest = nextLowest;
371 nextLowest = 0;
372 }
373 }
374
375 mNetRequestersPids = new ArrayList[ConnectivityManager.MAX_NETWORK_TYPE+1];
376 for (int i : mPriorityList) {
Robert Greenwalt42acef32009-08-12 16:08:25 -0700377 mNetRequestersPids[i] = new ArrayList();
378 }
379
380 mFeatureUsers = new ArrayList();
381
Robert Greenwalt5154ae762009-10-30 14:17:42 -0700382 mNumDnsEntries = 0;
383
384 mTestMode = SystemProperties.get("cm.test.mode").equals("true")
385 && SystemProperties.get("ro.build.type").equals("eng");
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800386 /*
387 * Create the network state trackers for Wi-Fi and mobile
388 * data. Maybe this could be done with a factory class,
389 * but it's not clear that it's worth it, given that
390 * the number of different network types is not going
391 * to change very often.
392 */
Robert Greenwalt5154ae762009-10-30 14:17:42 -0700393 for (int netType : mPriorityList) {
394 switch (mNetAttributes[netType].mRadio) {
395 case ConnectivityManager.TYPE_WIFI:
Wink Savilleed9c02b2010-12-03 12:01:38 -0800396 if (DBG) log("Starting Wifi Service.");
Wink Savillec7a98342010-08-13 16:11:42 -0700397 WifiStateTracker wst = new WifiStateTracker();
Irfan Sheriff0d255342010-07-28 09:35:20 -0700398 WifiService wifiService = new WifiService(context);
Robert Greenwalt5154ae762009-10-30 14:17:42 -0700399 ServiceManager.addService(Context.WIFI_SERVICE, wifiService);
Irfan Sheriff0d255342010-07-28 09:35:20 -0700400 wifiService.checkAndStartWifi();
Robert Greenwalt5154ae762009-10-30 14:17:42 -0700401 mNetTrackers[ConnectivityManager.TYPE_WIFI] = wst;
Wink Savillec7a98342010-08-13 16:11:42 -0700402 wst.startMonitoring(context, mHandler);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800403
Irfan Sheriffa2a1b912010-06-07 09:03:04 -0700404 //TODO: as part of WWS refactor, create only when needed
Irfan Sheriff0d255342010-07-28 09:35:20 -0700405 mWifiWatchdogService = new WifiWatchdogService(context);
Irfan Sheriffa2a1b912010-06-07 09:03:04 -0700406
Robert Greenwalt5154ae762009-10-30 14:17:42 -0700407 break;
408 case ConnectivityManager.TYPE_MOBILE:
Wink Savillec7a98342010-08-13 16:11:42 -0700409 mNetTrackers[netType] = new MobileDataStateTracker(netType,
410 mNetAttributes[netType].mName);
411 mNetTrackers[netType].startMonitoring(context, mHandler);
Robert Greenwalt5154ae762009-10-30 14:17:42 -0700412 break;
Robert Greenwaltda3d5e62010-12-06 13:56:24 -0800413 case ConnectivityManager.TYPE_DUMMY:
414 mNetTrackers[netType] = new DummyDataStateTracker(netType,
415 mNetAttributes[netType].mName);
416 mNetTrackers[netType].startMonitoring(context, mHandler);
417 break;
Jaikumar Ganesh15c74392010-12-21 22:31:44 -0800418 case ConnectivityManager.TYPE_BLUETOOTH:
419 mNetTrackers[netType] = BluetoothTetheringDataTracker.getInstance();
420 mNetTrackers[netType].startMonitoring(context, mHandler);
421 break;
Robert Greenwalt5154ae762009-10-30 14:17:42 -0700422 default:
Wink Savilleed9c02b2010-12-03 12:01:38 -0800423 loge("Trying to create a DataStateTracker for an unknown radio type " +
Robert Greenwalt5154ae762009-10-30 14:17:42 -0700424 mNetAttributes[netType].mRadio);
425 continue;
426 }
427 }
Robert Greenwaltd0e18ff2010-01-26 11:40:34 -0800428
Robert Greenwaltdfadaea2010-03-11 15:03:08 -0800429 mTethering = new Tethering(mContext, mHandler.getLooper());
Robert Greenwaltc9d5fb72010-02-25 12:29:30 -0800430 mTetheringConfigValid = (((mNetTrackers[ConnectivityManager.TYPE_MOBILE_DUN] != null) ||
431 !mTethering.isDunRequired()) &&
432 (mTethering.getTetherableUsbRegexs().length != 0 ||
Danica Chang6fdd0c62010-08-11 14:54:43 -0700433 mTethering.getTetherableWifiRegexs().length != 0 ||
434 mTethering.getTetherableBluetoothRegexs().length != 0) &&
Robert Greenwaltc9d5fb72010-02-25 12:29:30 -0800435 mTethering.getUpstreamIfaceRegexs().length != 0);
436
Robert Greenwalt4e8dfef2010-09-20 14:35:25 -0700437 if (DBG) {
438 mInetLog = new ArrayList();
439 }
Robert Greenwalt434203a2010-10-11 16:00:27 -0700440
441 mSettingsObserver = new SettingsObserver(mHandler, EVENT_APPLY_GLOBAL_HTTP_PROXY);
442 mSettingsObserver.observe(mContext);
Robert Greenwaltb7090d62010-12-02 11:31:00 -0800443
444 loadGlobalProxy();
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800445 }
446
Robert Greenwalt5154ae762009-10-30 14:17:42 -0700447
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800448 /**
Robert Greenwalt86e9e552009-07-16 17:21:39 -0700449 * Sets the preferred network.
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800450 * @param preference the new preference
451 */
Robert Greenwalt8dcc28b2010-09-23 10:05:56 -0700452 public void setNetworkPreference(int preference) {
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800453 enforceChangePermission();
Robert Greenwalt8dcc28b2010-09-23 10:05:56 -0700454
455 mHandler.sendMessage(mHandler.obtainMessage(EVENT_SET_NETWORK_PREFERENCE, preference, 0));
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800456 }
457
458 public int getNetworkPreference() {
459 enforceAccessPermission();
Robert Greenwalt8dcc28b2010-09-23 10:05:56 -0700460 int preference;
461 synchronized(this) {
462 preference = mNetworkPreference;
463 }
464 return preference;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800465 }
466
Robert Greenwalt8dcc28b2010-09-23 10:05:56 -0700467 private void handleSetNetworkPreference(int preference) {
468 if (ConnectivityManager.isNetworkTypeValid(preference) &&
469 mNetAttributes[preference] != null &&
470 mNetAttributes[preference].isDefault()) {
471 if (mNetworkPreference != preference) {
472 final ContentResolver cr = mContext.getContentResolver();
473 Settings.Secure.putInt(cr, Settings.Secure.NETWORK_PREFERENCE, preference);
474 synchronized(this) {
475 mNetworkPreference = preference;
476 }
477 enforcePreference();
478 }
479 }
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800480 }
Robert Greenwalt86e9e552009-07-16 17:21:39 -0700481
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800482 private int getPersistedNetworkPreference() {
483 final ContentResolver cr = mContext.getContentResolver();
484
485 final int networkPrefSetting = Settings.Secure
486 .getInt(cr, Settings.Secure.NETWORK_PREFERENCE, -1);
487 if (networkPrefSetting != -1) {
488 return networkPrefSetting;
489 }
490
491 return ConnectivityManager.DEFAULT_NETWORK_PREFERENCE;
492 }
Robert Greenwalt86e9e552009-07-16 17:21:39 -0700493
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800494 /**
Robert Greenwalt86e9e552009-07-16 17:21:39 -0700495 * Make the state of network connectivity conform to the preference settings
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800496 * In this method, we only tear down a non-preferred network. Establishing
497 * a connection to the preferred network is taken care of when we handle
498 * the disconnect event from the non-preferred network
499 * (see {@link #handleDisconnect(NetworkInfo)}).
500 */
501 private void enforcePreference() {
Robert Greenwalt42acef32009-08-12 16:08:25 -0700502 if (mNetTrackers[mNetworkPreference].getNetworkInfo().isConnected())
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800503 return;
504
Robert Greenwalt42acef32009-08-12 16:08:25 -0700505 if (!mNetTrackers[mNetworkPreference].isAvailable())
506 return;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800507
Robert Greenwalt42acef32009-08-12 16:08:25 -0700508 for (int t=0; t <= ConnectivityManager.MAX_RADIO_TYPE; t++) {
Robert Greenwalt5154ae762009-10-30 14:17:42 -0700509 if (t != mNetworkPreference && mNetTrackers[t] != null &&
Robert Greenwalt42acef32009-08-12 16:08:25 -0700510 mNetTrackers[t].getNetworkInfo().isConnected()) {
Robert Greenwaltec9fe462009-08-20 15:25:14 -0700511 if (DBG) {
Wink Savilleed9c02b2010-12-03 12:01:38 -0800512 log("tearing down " + mNetTrackers[t].getNetworkInfo() +
Robert Greenwaltec9fe462009-08-20 15:25:14 -0700513 " in enforcePreference");
514 }
Robert Greenwalt42acef32009-08-12 16:08:25 -0700515 teardown(mNetTrackers[t]);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800516 }
517 }
518 }
519
520 private boolean teardown(NetworkStateTracker netTracker) {
521 if (netTracker.teardown()) {
522 netTracker.setTeardownRequested(true);
523 return true;
524 } else {
525 return false;
526 }
527 }
528
529 /**
530 * Return NetworkInfo for the active (i.e., connected) network interface.
531 * It is assumed that at most one network is active at a time. If more
532 * than one is active, it is indeterminate which will be returned.
Robert Greenwalt86e9e552009-07-16 17:21:39 -0700533 * @return the info for the active network, or {@code null} if none is
534 * active
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800535 */
536 public NetworkInfo getActiveNetworkInfo() {
537 enforceAccessPermission();
Robert Greenwalt42acef32009-08-12 16:08:25 -0700538 for (int type=0; type <= ConnectivityManager.MAX_NETWORK_TYPE; type++) {
Robert Greenwalt5154ae762009-10-30 14:17:42 -0700539 if (mNetAttributes[type] == null || !mNetAttributes[type].isDefault()) {
Robert Greenwalt42acef32009-08-12 16:08:25 -0700540 continue;
541 }
542 NetworkStateTracker t = mNetTrackers[type];
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800543 NetworkInfo info = t.getNetworkInfo();
544 if (info.isConnected()) {
Wink Savilleed9c02b2010-12-03 12:01:38 -0800545 if (DBG && type != mActiveDefaultNetwork) {
546 loge("connected default network is not mActiveDefaultNetwork!");
547 }
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800548 return info;
549 }
550 }
551 return null;
552 }
553
554 public NetworkInfo getNetworkInfo(int networkType) {
555 enforceAccessPermission();
556 if (ConnectivityManager.isNetworkTypeValid(networkType)) {
557 NetworkStateTracker t = mNetTrackers[networkType];
558 if (t != null)
559 return t.getNetworkInfo();
560 }
561 return null;
562 }
563
564 public NetworkInfo[] getAllNetworkInfo() {
565 enforceAccessPermission();
Robert Greenwalt5154ae762009-10-30 14:17:42 -0700566 NetworkInfo[] result = new NetworkInfo[mNetworksDefined];
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800567 int i = 0;
568 for (NetworkStateTracker t : mNetTrackers) {
Robert Greenwalt5154ae762009-10-30 14:17:42 -0700569 if(t != null) result[i++] = t.getNetworkInfo();
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800570 }
571 return result;
572 }
573
Robert Greenwaltd192dad2010-09-14 09:18:02 -0700574 /**
575 * Return LinkProperties for the active (i.e., connected) default
576 * network interface. It is assumed that at most one default network
577 * is active at a time. If more than one is active, it is indeterminate
578 * which will be returned.
579 * @return the ip properties for the active network, or {@code null} if
580 * none is active
581 */
582 public LinkProperties getActiveLinkProperties() {
583 enforceAccessPermission();
584 for (int type=0; type <= ConnectivityManager.MAX_NETWORK_TYPE; type++) {
585 if (mNetAttributes[type] == null || !mNetAttributes[type].isDefault()) {
586 continue;
587 }
588 NetworkStateTracker t = mNetTrackers[type];
589 NetworkInfo info = t.getNetworkInfo();
590 if (info.isConnected()) {
591 return t.getLinkProperties();
592 }
593 }
594 return null;
595 }
596
597 public LinkProperties getLinkProperties(int networkType) {
598 enforceAccessPermission();
599 if (ConnectivityManager.isNetworkTypeValid(networkType)) {
600 NetworkStateTracker t = mNetTrackers[networkType];
601 if (t != null) return t.getLinkProperties();
602 }
603 return null;
604 }
605
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800606 public boolean setRadios(boolean turnOn) {
607 boolean result = true;
608 enforceChangePermission();
609 for (NetworkStateTracker t : mNetTrackers) {
Robert Greenwalt5154ae762009-10-30 14:17:42 -0700610 if (t != null) result = t.setRadio(turnOn) && result;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800611 }
612 return result;
613 }
614
615 public boolean setRadio(int netType, boolean turnOn) {
616 enforceChangePermission();
617 if (!ConnectivityManager.isNetworkTypeValid(netType)) {
618 return false;
619 }
620 NetworkStateTracker tracker = mNetTrackers[netType];
621 return tracker != null && tracker.setRadio(turnOn);
622 }
623
Robert Greenwalt9c75d4a2009-09-27 17:27:04 -0700624 /**
625 * Used to notice when the calling process dies so we can self-expire
626 *
627 * Also used to know if the process has cleaned up after itself when
628 * our auto-expire timer goes off. The timer has a link to an object.
629 *
630 */
Robert Greenwalt42acef32009-08-12 16:08:25 -0700631 private class FeatureUser implements IBinder.DeathRecipient {
632 int mNetworkType;
633 String mFeature;
634 IBinder mBinder;
635 int mPid;
636 int mUid;
Robert Greenwaltb9285352009-12-21 18:24:07 -0800637 long mCreateTime;
Robert Greenwalt42acef32009-08-12 16:08:25 -0700638
639 FeatureUser(int type, String feature, IBinder binder) {
640 super();
641 mNetworkType = type;
642 mFeature = feature;
643 mBinder = binder;
644 mPid = getCallingPid();
645 mUid = getCallingUid();
Robert Greenwaltb9285352009-12-21 18:24:07 -0800646 mCreateTime = System.currentTimeMillis();
Robert Greenwalt9c75d4a2009-09-27 17:27:04 -0700647
Robert Greenwalt42acef32009-08-12 16:08:25 -0700648 try {
649 mBinder.linkToDeath(this, 0);
650 } catch (RemoteException e) {
651 binderDied();
652 }
653 }
654
655 void unlinkDeathRecipient() {
656 mBinder.unlinkToDeath(this, 0);
657 }
658
659 public void binderDied() {
Wink Savilleed9c02b2010-12-03 12:01:38 -0800660 log("ConnectivityService FeatureUser binderDied(" +
Robert Greenwaltb9285352009-12-21 18:24:07 -0800661 mNetworkType + ", " + mFeature + ", " + mBinder + "), created " +
662 (System.currentTimeMillis() - mCreateTime) + " mSec ago");
Robert Greenwalt9c75d4a2009-09-27 17:27:04 -0700663 stopUsingNetworkFeature(this, false);
Robert Greenwalt42acef32009-08-12 16:08:25 -0700664 }
665
Robert Greenwalt9c75d4a2009-09-27 17:27:04 -0700666 public void expire() {
Wink Savilleed9c02b2010-12-03 12:01:38 -0800667 log("ConnectivityService FeatureUser expire(" +
Robert Greenwaltb9285352009-12-21 18:24:07 -0800668 mNetworkType + ", " + mFeature + ", " + mBinder +"), created " +
669 (System.currentTimeMillis() - mCreateTime) + " mSec ago");
Robert Greenwalt9c75d4a2009-09-27 17:27:04 -0700670 stopUsingNetworkFeature(this, false);
671 }
Robert Greenwaltb9285352009-12-21 18:24:07 -0800672
673 public String toString() {
674 return "FeatureUser("+mNetworkType+","+mFeature+","+mPid+","+mUid+"), created " +
675 (System.currentTimeMillis() - mCreateTime) + " mSec ago";
676 }
Robert Greenwalt42acef32009-08-12 16:08:25 -0700677 }
678
Robert Greenwalt9c75d4a2009-09-27 17:27:04 -0700679 // javadoc from interface
Robert Greenwalt42acef32009-08-12 16:08:25 -0700680 public int startUsingNetworkFeature(int networkType, String feature,
681 IBinder binder) {
682 if (DBG) {
Wink Savilleed9c02b2010-12-03 12:01:38 -0800683 log("startUsingNetworkFeature for net " + networkType + ": " + feature);
Robert Greenwalt42acef32009-08-12 16:08:25 -0700684 }
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800685 enforceChangePermission();
Robert Greenwalt5154ae762009-10-30 14:17:42 -0700686 if (!ConnectivityManager.isNetworkTypeValid(networkType) ||
687 mNetAttributes[networkType] == null) {
Robert Greenwalt42acef32009-08-12 16:08:25 -0700688 return Phone.APN_REQUEST_FAILED;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800689 }
Robert Greenwalt42acef32009-08-12 16:08:25 -0700690
Robert Greenwalt9c75d4a2009-09-27 17:27:04 -0700691 FeatureUser f = new FeatureUser(networkType, feature, binder);
Robert Greenwalt42acef32009-08-12 16:08:25 -0700692
693 // TODO - move this into the MobileDataStateTracker
694 int usedNetworkType = networkType;
695 if(networkType == ConnectivityManager.TYPE_MOBILE) {
696 if (TextUtils.equals(feature, Phone.FEATURE_ENABLE_MMS)) {
697 usedNetworkType = ConnectivityManager.TYPE_MOBILE_MMS;
698 } else if (TextUtils.equals(feature, Phone.FEATURE_ENABLE_SUPL)) {
699 usedNetworkType = ConnectivityManager.TYPE_MOBILE_SUPL;
Robert Greenwalte32e8122010-12-29 14:35:21 -0800700 } else if (TextUtils.equals(feature, Phone.FEATURE_ENABLE_DUN) ||
701 TextUtils.equals(feature, Phone.FEATURE_ENABLE_DUN_ALWAYS)) {
Robert Greenwalt42acef32009-08-12 16:08:25 -0700702 usedNetworkType = ConnectivityManager.TYPE_MOBILE_DUN;
703 } else if (TextUtils.equals(feature, Phone.FEATURE_ENABLE_HIPRI)) {
704 usedNetworkType = ConnectivityManager.TYPE_MOBILE_HIPRI;
705 }
706 }
707 NetworkStateTracker network = mNetTrackers[usedNetworkType];
708 if (network != null) {
Robert Greenwalt0be1e982010-12-15 13:26:33 -0800709 Integer currentPid = new Integer(getCallingPid());
Robert Greenwalt42acef32009-08-12 16:08:25 -0700710 if (usedNetworkType != networkType) {
Robert Greenwalt42acef32009-08-12 16:08:25 -0700711 NetworkStateTracker radio = mNetTrackers[networkType];
712 NetworkInfo ni = network.getNetworkInfo();
713
714 if (ni.isAvailable() == false) {
Wink Savilleed9c02b2010-12-03 12:01:38 -0800715 if (DBG) log("special network not available");
Robert Greenwalte32e8122010-12-29 14:35:21 -0800716 if (!TextUtils.equals(feature,Phone.FEATURE_ENABLE_DUN_ALWAYS)) {
717 return Phone.APN_TYPE_NOT_AVAILABLE;
718 } else {
719 // else make the attempt anyway - probably giving REQUEST_STARTED below
720 }
Robert Greenwalt42acef32009-08-12 16:08:25 -0700721 }
722
Robert Greenwalt9c75d4a2009-09-27 17:27:04 -0700723 synchronized(this) {
724 mFeatureUsers.add(f);
725 if (!mNetRequestersPids[usedNetworkType].contains(currentPid)) {
726 // this gets used for per-pid dns when connected
727 mNetRequestersPids[usedNetworkType].add(currentPid);
728 }
Robert Greenwalt42acef32009-08-12 16:08:25 -0700729 }
Robert Greenwalt8dcc28b2010-09-23 10:05:56 -0700730 mHandler.sendMessageDelayed(mHandler.obtainMessage(EVENT_RESTORE_DEFAULT_NETWORK,
Robert Greenwalt9c75d4a2009-09-27 17:27:04 -0700731 f), getRestoreDefaultNetworkDelay());
732
Robert Greenwalt42acef32009-08-12 16:08:25 -0700733
Robert Greenwalta64bf832009-08-19 20:19:33 -0700734 if ((ni.isConnectedOrConnecting() == true) &&
735 !network.isTeardownRequested()) {
Robert Greenwalt42acef32009-08-12 16:08:25 -0700736 if (ni.isConnected() == true) {
737 // add the pid-specific dns
Robert Greenwaltc76b8fa2010-07-23 15:46:26 -0700738 handleDnsConfigurationChange(networkType);
Wink Savilleed9c02b2010-12-03 12:01:38 -0800739 if (DBG) log("special network already active");
Robert Greenwalt42acef32009-08-12 16:08:25 -0700740 return Phone.APN_ALREADY_ACTIVE;
741 }
Wink Savilleed9c02b2010-12-03 12:01:38 -0800742 if (DBG) log("special network already connecting");
Robert Greenwalt42acef32009-08-12 16:08:25 -0700743 return Phone.APN_REQUEST_STARTED;
744 }
745
746 // check if the radio in play can make another contact
747 // assume if cannot for now
748
Wink Savilleed9c02b2010-12-03 12:01:38 -0800749 if (DBG) log("reconnecting to special network");
Robert Greenwalt42acef32009-08-12 16:08:25 -0700750 network.reconnect();
751 return Phone.APN_REQUEST_STARTED;
752 } else {
Robert Greenwalt0be1e982010-12-15 13:26:33 -0800753 // need to remember this unsupported request so we respond appropriately on stop
754 synchronized(this) {
755 mFeatureUsers.add(f);
756 if (!mNetRequestersPids[usedNetworkType].contains(currentPid)) {
757 // this gets used for per-pid dns when connected
758 mNetRequestersPids[usedNetworkType].add(currentPid);
759 }
760 }
Robert Greenwalt02648a42010-05-18 10:52:51 -0700761 return -1;
Robert Greenwalt42acef32009-08-12 16:08:25 -0700762 }
763 }
764 return Phone.APN_TYPE_NOT_AVAILABLE;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800765 }
766
Robert Greenwalt9c75d4a2009-09-27 17:27:04 -0700767 // javadoc from interface
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800768 public int stopUsingNetworkFeature(int networkType, String feature) {
Robert Greenwaltb8f16342009-10-06 17:52:40 -0700769 enforceChangePermission();
770
Robert Greenwalt9c75d4a2009-09-27 17:27:04 -0700771 int pid = getCallingPid();
772 int uid = getCallingUid();
773
774 FeatureUser u = null;
775 boolean found = false;
776
777 synchronized(this) {
778 for (int i = 0; i < mFeatureUsers.size() ; i++) {
779 u = (FeatureUser)mFeatureUsers.get(i);
780 if (uid == u.mUid && pid == u.mPid &&
781 networkType == u.mNetworkType &&
782 TextUtils.equals(feature, u.mFeature)) {
783 found = true;
784 break;
785 }
786 }
787 }
788 if (found && u != null) {
789 // stop regardless of how many other time this proc had called start
790 return stopUsingNetworkFeature(u, true);
791 } else {
792 // none found!
Wink Savilleed9c02b2010-12-03 12:01:38 -0800793 if (DBG) log("ignoring stopUsingNetworkFeature - not a live request");
Robert Greenwalt9c75d4a2009-09-27 17:27:04 -0700794 return 1;
795 }
Robert Greenwalt42acef32009-08-12 16:08:25 -0700796 }
797
Robert Greenwalt9c75d4a2009-09-27 17:27:04 -0700798 private int stopUsingNetworkFeature(FeatureUser u, boolean ignoreDups) {
799 int networkType = u.mNetworkType;
800 String feature = u.mFeature;
801 int pid = u.mPid;
802 int uid = u.mUid;
803
804 NetworkStateTracker tracker = null;
805 boolean callTeardown = false; // used to carry our decision outside of sync block
806
Robert Greenwalt42acef32009-08-12 16:08:25 -0700807 if (DBG) {
Wink Savilleed9c02b2010-12-03 12:01:38 -0800808 log("stopUsingNetworkFeature for net " + networkType +
Robert Greenwalt42acef32009-08-12 16:08:25 -0700809 ": " + feature);
810 }
Robert Greenwaltb8f16342009-10-06 17:52:40 -0700811
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800812 if (!ConnectivityManager.isNetworkTypeValid(networkType)) {
813 return -1;
814 }
Robert Greenwalt42acef32009-08-12 16:08:25 -0700815
Robert Greenwalt9c75d4a2009-09-27 17:27:04 -0700816 // need to link the mFeatureUsers list with the mNetRequestersPids state in this
817 // sync block
818 synchronized(this) {
819 // check if this process still has an outstanding start request
820 if (!mFeatureUsers.contains(u)) {
Wink Savilleed9c02b2010-12-03 12:01:38 -0800821 if (DBG) log("ignoring - this process has no outstanding requests");
Robert Greenwalt42acef32009-08-12 16:08:25 -0700822 return 1;
823 }
Robert Greenwalt9c75d4a2009-09-27 17:27:04 -0700824 u.unlinkDeathRecipient();
825 mFeatureUsers.remove(mFeatureUsers.indexOf(u));
826 // If we care about duplicate requests, check for that here.
827 //
828 // This is done to support the extension of a request - the app
829 // can request we start the network feature again and renew the
830 // auto-shutoff delay. Normal "stop" calls from the app though
831 // do not pay attention to duplicate requests - in effect the
832 // API does not refcount and a single stop will counter multiple starts.
833 if (ignoreDups == false) {
834 for (int i = 0; i < mFeatureUsers.size() ; i++) {
835 FeatureUser x = (FeatureUser)mFeatureUsers.get(i);
836 if (x.mUid == u.mUid && x.mPid == u.mPid &&
837 x.mNetworkType == u.mNetworkType &&
838 TextUtils.equals(x.mFeature, u.mFeature)) {
Wink Savilleed9c02b2010-12-03 12:01:38 -0800839 if (DBG) log("ignoring stopUsingNetworkFeature as dup is found");
Robert Greenwalt9c75d4a2009-09-27 17:27:04 -0700840 return 1;
841 }
842 }
Robert Greenwalt42acef32009-08-12 16:08:25 -0700843 }
Robert Greenwalt9c75d4a2009-09-27 17:27:04 -0700844
845 // TODO - move to MobileDataStateTracker
846 int usedNetworkType = networkType;
847 if (networkType == ConnectivityManager.TYPE_MOBILE) {
848 if (TextUtils.equals(feature, Phone.FEATURE_ENABLE_MMS)) {
849 usedNetworkType = ConnectivityManager.TYPE_MOBILE_MMS;
850 } else if (TextUtils.equals(feature, Phone.FEATURE_ENABLE_SUPL)) {
851 usedNetworkType = ConnectivityManager.TYPE_MOBILE_SUPL;
852 } else if (TextUtils.equals(feature, Phone.FEATURE_ENABLE_DUN)) {
853 usedNetworkType = ConnectivityManager.TYPE_MOBILE_DUN;
854 } else if (TextUtils.equals(feature, Phone.FEATURE_ENABLE_HIPRI)) {
855 usedNetworkType = ConnectivityManager.TYPE_MOBILE_HIPRI;
856 }
857 }
858 tracker = mNetTrackers[usedNetworkType];
Robert Greenwalt5154ae762009-10-30 14:17:42 -0700859 if (tracker == null) {
Wink Savilleed9c02b2010-12-03 12:01:38 -0800860 if (DBG) log("ignoring - no known tracker for net type " + usedNetworkType);
Robert Greenwalt5154ae762009-10-30 14:17:42 -0700861 return -1;
862 }
863 if (usedNetworkType != networkType) {
Robert Greenwalt9c75d4a2009-09-27 17:27:04 -0700864 Integer currentPid = new Integer(pid);
Robert Greenwalt9c75d4a2009-09-27 17:27:04 -0700865 mNetRequestersPids[usedNetworkType].remove(currentPid);
Robert Greenwalt421c72b2009-12-17 14:54:59 -0800866 reassessPidDns(pid, true);
Robert Greenwalt9c75d4a2009-09-27 17:27:04 -0700867 if (mNetRequestersPids[usedNetworkType].size() != 0) {
Wink Savilleed9c02b2010-12-03 12:01:38 -0800868 if (DBG) log("not tearing down special network - " +
Robert Greenwalt9c75d4a2009-09-27 17:27:04 -0700869 "others still using it");
870 return 1;
871 }
872 callTeardown = true;
873 }
874 }
Wink Savilleed9c02b2010-12-03 12:01:38 -0800875 if (DBG) log("Doing network teardown");
Robert Greenwalt9c75d4a2009-09-27 17:27:04 -0700876 if (callTeardown) {
877 tracker.teardown();
Robert Greenwalt42acef32009-08-12 16:08:25 -0700878 return 1;
879 } else {
Robert Greenwalt02648a42010-05-18 10:52:51 -0700880 return -1;
Robert Greenwalt42acef32009-08-12 16:08:25 -0700881 }
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800882 }
883
884 /**
Robert Greenwalt585ac0f2010-08-27 09:24:29 -0700885 * @deprecated use requestRouteToHostAddress instead
886 *
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800887 * Ensure that a network route exists to deliver traffic to the specified
888 * host via the specified network interface.
Robert Greenwalt86e9e552009-07-16 17:21:39 -0700889 * @param networkType the type of the network over which traffic to the
890 * specified host is to be routed
891 * @param hostAddress the IP address of the host to which the route is
892 * desired
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800893 * @return {@code true} on success, {@code false} on failure
894 */
895 public boolean requestRouteToHost(int networkType, int hostAddress) {
Robert Greenwalt585ac0f2010-08-27 09:24:29 -0700896 InetAddress inetAddress = NetworkUtils.intToInetAddress(hostAddress);
897
898 if (inetAddress == null) {
899 return false;
900 }
901
902 return requestRouteToHostAddress(networkType, inetAddress.getAddress());
903 }
904
905 /**
906 * Ensure that a network route exists to deliver traffic to the specified
907 * host via the specified network interface.
908 * @param networkType the type of the network over which traffic to the
909 * specified host is to be routed
910 * @param hostAddress the IP address of the host to which the route is
911 * desired
912 * @return {@code true} on success, {@code false} on failure
913 */
914 public boolean requestRouteToHostAddress(int networkType, byte[] hostAddress) {
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800915 enforceChangePermission();
916 if (!ConnectivityManager.isNetworkTypeValid(networkType)) {
917 return false;
918 }
919 NetworkStateTracker tracker = mNetTrackers[networkType];
Robert Greenwalt8206ff32009-09-10 15:06:20 -0700920
Robert Greenwalt5154ae762009-10-30 14:17:42 -0700921 if (tracker == null || !tracker.getNetworkInfo().isConnected() ||
922 tracker.isTeardownRequested()) {
Robert Greenwalt8206ff32009-09-10 15:06:20 -0700923 if (DBG) {
Wink Savilleed9c02b2010-12-03 12:01:38 -0800924 log("requestRouteToHostAddress on down network " +
Robert Greenwalt585ac0f2010-08-27 09:24:29 -0700925 "(" + networkType + ") - dropped");
Robert Greenwalt8206ff32009-09-10 15:06:20 -0700926 }
927 return false;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800928 }
Robert Greenwalt47f69fe2010-06-15 15:43:39 -0700929 try {
Robert Greenwalt585ac0f2010-08-27 09:24:29 -0700930 InetAddress addr = InetAddress.getByAddress(hostAddress);
Robert Greenwalt47f69fe2010-06-15 15:43:39 -0700931 return addHostRoute(tracker, addr);
932 } catch (UnknownHostException e) {}
933 return false;
Irfan Sheriffd649c122010-06-09 15:39:36 -0700934 }
935
936 /**
937 * Ensure that a network route exists to deliver traffic to the specified
938 * host via the mobile data network.
939 * @param hostAddress the IP address of the host to which the route is desired,
940 * in network byte order.
Robert Greenwalt47f69fe2010-06-15 15:43:39 -0700941 * TODO - deprecate
Irfan Sheriffd649c122010-06-09 15:39:36 -0700942 * @return {@code true} on success, {@code false} on failure
943 */
Robert Greenwalt47f69fe2010-06-15 15:43:39 -0700944 private boolean addHostRoute(NetworkStateTracker nt, InetAddress hostAddress) {
Irfan Sheriffd649c122010-06-09 15:39:36 -0700945 if (nt.getNetworkInfo().getType() == ConnectivityManager.TYPE_WIFI) {
946 return false;
947 }
948
Robert Greenwalt37e65eb2010-08-30 10:56:47 -0700949 LinkProperties p = nt.getLinkProperties();
Robert Greenwalt47f69fe2010-06-15 15:43:39 -0700950 if (p == null) return false;
951 String interfaceName = p.getInterfaceName();
Irfan Sheriffd649c122010-06-09 15:39:36 -0700952
953 if (DBG) {
Wink Savilleed9c02b2010-12-03 12:01:38 -0800954 log("Requested host route to " + hostAddress + "(" + interfaceName + ")");
Irfan Sheriffd649c122010-06-09 15:39:36 -0700955 }
Robert Greenwalt47f69fe2010-06-15 15:43:39 -0700956 if (interfaceName != null) {
Robert Greenwalt585ac0f2010-08-27 09:24:29 -0700957 return NetworkUtils.addHostRoute(interfaceName, hostAddress, null);
Irfan Sheriffd649c122010-06-09 15:39:36 -0700958 } else {
Wink Savilleed9c02b2010-12-03 12:01:38 -0800959 if (DBG) loge("addHostRoute failed due to null interface name");
Irfan Sheriffd649c122010-06-09 15:39:36 -0700960 return false;
961 }
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800962 }
963
964 /**
965 * @see ConnectivityManager#getBackgroundDataSetting()
966 */
967 public boolean getBackgroundDataSetting() {
Robert Greenwaltd825ea42010-12-29 16:15:02 -0800968 return mBackgroundDataEnabled.get();
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800969 }
Robert Greenwalt86e9e552009-07-16 17:21:39 -0700970
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800971 /**
972 * @see ConnectivityManager#setBackgroundDataSetting(boolean)
973 */
974 public void setBackgroundDataSetting(boolean allowBackgroundDataUsage) {
975 mContext.enforceCallingOrSelfPermission(
976 android.Manifest.permission.CHANGE_BACKGROUND_DATA_SETTING,
977 "ConnectivityService");
Robert Greenwalt86e9e552009-07-16 17:21:39 -0700978
Robert Greenwaltd825ea42010-12-29 16:15:02 -0800979 mBackgroundDataEnabled.set(allowBackgroundDataUsage);
980
Robert Greenwalt8dcc28b2010-09-23 10:05:56 -0700981 mHandler.sendMessage(mHandler.obtainMessage(EVENT_SET_BACKGROUND_DATA,
982 (allowBackgroundDataUsage ? ENABLED : DISABLED), 0));
983 }
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800984
Robert Greenwalt8dcc28b2010-09-23 10:05:56 -0700985 private void handleSetBackgroundData(boolean enabled) {
986 if (enabled != getBackgroundDataSetting()) {
987 Settings.Secure.putInt(mContext.getContentResolver(),
988 Settings.Secure.BACKGROUND_DATA, enabled ? 1 : 0);
989 Intent broadcast = new Intent(
990 ConnectivityManager.ACTION_BACKGROUND_DATA_SETTING_CHANGED);
991 mContext.sendBroadcast(broadcast);
992 }
Robert Greenwalt86e9e552009-07-16 17:21:39 -0700993 }
994
Robert Greenwaltc03fa502010-02-23 18:58:05 -0800995 /**
996 * @see ConnectivityManager#getMobileDataEnabled()
997 */
998 public boolean getMobileDataEnabled() {
Wink Savillee7982682010-12-07 10:31:02 -0800999 // TODO: This detail should probably be in DataConnectionTracker's
1000 // which is where we store the value and maybe make this
1001 // asynchronous.
Robert Greenwaltc03fa502010-02-23 18:58:05 -08001002 enforceAccessPermission();
1003 boolean retVal = Settings.Secure.getInt(mContext.getContentResolver(),
1004 Settings.Secure.MOBILE_DATA, 1) == 1;
Wink Savilleed9c02b2010-12-03 12:01:38 -08001005 if (DBG) log("getMobileDataEnabled returning " + retVal);
Robert Greenwaltc03fa502010-02-23 18:58:05 -08001006 return retVal;
1007 }
1008
1009 /**
1010 * @see ConnectivityManager#setMobileDataEnabled(boolean)
1011 */
Robert Greenwalt8dcc28b2010-09-23 10:05:56 -07001012 public void setMobileDataEnabled(boolean enabled) {
Robert Greenwaltc03fa502010-02-23 18:58:05 -08001013 enforceChangePermission();
Wink Savilleed9c02b2010-12-03 12:01:38 -08001014 if (DBG) log("setMobileDataEnabled(" + enabled + ")");
Robert Greenwaltc03fa502010-02-23 18:58:05 -08001015
Robert Greenwalt8dcc28b2010-09-23 10:05:56 -07001016 mHandler.sendMessage(mHandler.obtainMessage(EVENT_SET_MOBILE_DATA,
1017 (enabled ? ENABLED : DISABLED), 0));
1018 }
1019
1020 private void handleSetMobileData(boolean enabled) {
Wink Savillee7982682010-12-07 10:31:02 -08001021 if (mNetTrackers[ConnectivityManager.TYPE_MOBILE] != null) {
1022 if (DBG) {
1023 Slog.d(TAG, mNetTrackers[ConnectivityManager.TYPE_MOBILE].toString() + enabled);
Robert Greenwaltc03fa502010-02-23 18:58:05 -08001024 }
Wink Savillee7982682010-12-07 10:31:02 -08001025 mNetTrackers[ConnectivityManager.TYPE_MOBILE].setDataEnable(enabled);
Robert Greenwaltc03fa502010-02-23 18:58:05 -08001026 }
1027 }
1028
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001029 private void enforceAccessPermission() {
Robert Greenwalt86e9e552009-07-16 17:21:39 -07001030 mContext.enforceCallingOrSelfPermission(
1031 android.Manifest.permission.ACCESS_NETWORK_STATE,
1032 "ConnectivityService");
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001033 }
1034
1035 private void enforceChangePermission() {
Robert Greenwalt86e9e552009-07-16 17:21:39 -07001036 mContext.enforceCallingOrSelfPermission(
1037 android.Manifest.permission.CHANGE_NETWORK_STATE,
1038 "ConnectivityService");
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001039 }
1040
Robert Greenwaltd0e18ff2010-01-26 11:40:34 -08001041 // TODO Make this a special check when it goes public
1042 private void enforceTetherChangePermission() {
1043 mContext.enforceCallingOrSelfPermission(
1044 android.Manifest.permission.CHANGE_NETWORK_STATE,
1045 "ConnectivityService");
1046 }
1047
Robert Greenwalt2a091d72010-02-11 18:18:40 -08001048 private void enforceTetherAccessPermission() {
1049 mContext.enforceCallingOrSelfPermission(
1050 android.Manifest.permission.ACCESS_NETWORK_STATE,
1051 "ConnectivityService");
1052 }
1053
Robert Greenwalt14f2ef42010-06-15 12:19:37 -07001054 private void enforceConnectivityInternalPermission() {
1055 mContext.enforceCallingOrSelfPermission(
1056 android.Manifest.permission.CONNECTIVITY_INTERNAL,
1057 "ConnectivityService");
1058 }
1059
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001060 /**
Robert Greenwalt86e9e552009-07-16 17:21:39 -07001061 * Handle a {@code DISCONNECTED} event. If this pertains to the non-active
1062 * network, we ignore it. If it is for the active network, we send out a
1063 * broadcast. But first, we check whether it might be possible to connect
1064 * to a different network.
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001065 * @param info the {@code NetworkInfo} for the network
1066 */
1067 private void handleDisconnect(NetworkInfo info) {
1068
Robert Greenwalt42acef32009-08-12 16:08:25 -07001069 int prevNetType = info.getType();
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001070
Robert Greenwalt42acef32009-08-12 16:08:25 -07001071 mNetTrackers[prevNetType].setTeardownRequested(false);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001072 /*
1073 * If the disconnected network is not the active one, then don't report
1074 * this as a loss of connectivity. What probably happened is that we're
1075 * getting the disconnect for a network that we explicitly disabled
1076 * in accordance with network preference policies.
1077 */
Robert Greenwalt42acef32009-08-12 16:08:25 -07001078 if (!mNetAttributes[prevNetType].isDefault()) {
1079 List pids = mNetRequestersPids[prevNetType];
1080 for (int i = 0; i<pids.size(); i++) {
1081 Integer pid = (Integer)pids.get(i);
1082 // will remove them because the net's no longer connected
1083 // need to do this now as only now do we know the pids and
1084 // can properly null things that are no longer referenced.
1085 reassessPidDns(pid.intValue(), false);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001086 }
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001087 }
1088
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001089 Intent intent = new Intent(ConnectivityManager.CONNECTIVITY_ACTION);
1090 intent.putExtra(ConnectivityManager.EXTRA_NETWORK_INFO, info);
1091 if (info.isFailover()) {
1092 intent.putExtra(ConnectivityManager.EXTRA_IS_FAILOVER, true);
1093 info.setFailover(false);
1094 }
1095 if (info.getReason() != null) {
1096 intent.putExtra(ConnectivityManager.EXTRA_REASON, info.getReason());
1097 }
1098 if (info.getExtraInfo() != null) {
Robert Greenwalt86e9e552009-07-16 17:21:39 -07001099 intent.putExtra(ConnectivityManager.EXTRA_EXTRA_INFO,
1100 info.getExtraInfo());
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001101 }
Robert Greenwalt42acef32009-08-12 16:08:25 -07001102
Robert Greenwaltcc4b4012010-01-25 17:54:29 -08001103 NetworkStateTracker newNet = null;
1104 if (mNetAttributes[prevNetType].isDefault()) {
1105 newNet = tryFailover(prevNetType);
1106 if (newNet != null) {
1107 NetworkInfo switchTo = newNet.getNetworkInfo();
Robert Greenwalt029be812010-09-20 18:01:43 -07001108 if (!switchTo.isConnected()) {
Wink Savilleed9c02b2010-12-03 12:01:38 -08001109 // if the other net is connected they've already reset this and perhaps even
1110 // gotten a positive report we don't want to overwrite, but if not we need to
1111 // clear this now to turn our cellular sig strength white
Robert Greenwalt029be812010-09-20 18:01:43 -07001112 mDefaultInetConditionPublished = 0;
Robert Greenwaltd68e3212010-12-21 11:43:28 -08001113 intent.putExtra(ConnectivityManager.EXTRA_NO_CONNECTIVITY, true);
Robert Greenwalt029be812010-09-20 18:01:43 -07001114 }
Robert Greenwaltcc4b4012010-01-25 17:54:29 -08001115 intent.putExtra(ConnectivityManager.EXTRA_OTHER_NETWORK_INFO, switchTo);
1116 } else {
Robert Greenwalt029be812010-09-20 18:01:43 -07001117 mDefaultInetConditionPublished = 0; // we're not connected anymore
Robert Greenwaltcc4b4012010-01-25 17:54:29 -08001118 intent.putExtra(ConnectivityManager.EXTRA_NO_CONNECTIVITY, true);
1119 }
Robert Greenwaltda03c4e2010-01-20 19:29:41 -08001120 }
Robert Greenwalt029be812010-09-20 18:01:43 -07001121 intent.putExtra(ConnectivityManager.EXTRA_INET_CONDITION, mDefaultInetConditionPublished);
Robert Greenwaltda03c4e2010-01-20 19:29:41 -08001122 // do this before we broadcast the change
Robert Greenwaltc76b8fa2010-07-23 15:46:26 -07001123 handleConnectivityChange(prevNetType);
Robert Greenwaltda03c4e2010-01-20 19:29:41 -08001124
1125 sendStickyBroadcast(intent);
1126 /*
1127 * If the failover network is already connected, then immediately send
1128 * out a followup broadcast indicating successful failover
1129 */
1130 if (newNet != null && newNet.getNetworkInfo().isConnected()) {
1131 sendConnectedBroadcast(newNet.getNetworkInfo());
1132 }
1133 }
1134
Robert Greenwaltcc4b4012010-01-25 17:54:29 -08001135 // returns null if no failover available
Robert Greenwaltda03c4e2010-01-20 19:29:41 -08001136 private NetworkStateTracker tryFailover(int prevNetType) {
Robert Greenwalt42acef32009-08-12 16:08:25 -07001137 /*
1138 * If this is a default network, check if other defaults are available
1139 * or active
1140 */
1141 NetworkStateTracker newNet = null;
1142 if (mNetAttributes[prevNetType].isDefault()) {
Robert Greenwalt42acef32009-08-12 16:08:25 -07001143 if (mActiveDefaultNetwork == prevNetType) {
1144 mActiveDefaultNetwork = -1;
1145 }
1146
1147 int newType = -1;
1148 int newPriority = -1;
Robert Greenwaltda03c4e2010-01-20 19:29:41 -08001149 for (int checkType=0; checkType <= ConnectivityManager.MAX_NETWORK_TYPE; checkType++) {
Robert Greenwalt5154ae762009-10-30 14:17:42 -07001150 if (checkType == prevNetType) continue;
1151 if (mNetAttributes[checkType] == null) continue;
Robert Greenwalt42acef32009-08-12 16:08:25 -07001152 if (mNetAttributes[checkType].isDefault()) {
1153 /* TODO - if we have multiple nets we could use
1154 * we may want to put more thought into which we choose
1155 */
1156 if (checkType == mNetworkPreference) {
1157 newType = checkType;
1158 break;
1159 }
Robert Greenwalt5154ae762009-10-30 14:17:42 -07001160 if (mNetAttributes[checkType].mPriority > newPriority) {
Robert Greenwalt42acef32009-08-12 16:08:25 -07001161 newType = checkType;
Robert Greenwalt5154ae762009-10-30 14:17:42 -07001162 newPriority = mNetAttributes[newType].mPriority;
Robert Greenwalt42acef32009-08-12 16:08:25 -07001163 }
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001164 }
1165 }
Robert Greenwalt42acef32009-08-12 16:08:25 -07001166
1167 if (newType != -1) {
1168 newNet = mNetTrackers[newType];
1169 /**
1170 * See if the other network is available to fail over to.
1171 * If is not available, we enable it anyway, so that it
1172 * will be able to connect when it does become available,
1173 * but we report a total loss of connectivity rather than
1174 * report that we are attempting to fail over.
1175 */
1176 if (newNet.isAvailable()) {
1177 NetworkInfo switchTo = newNet.getNetworkInfo();
1178 switchTo.setFailover(true);
Robert Greenwalta64bf832009-08-19 20:19:33 -07001179 if (!switchTo.isConnectedOrConnecting() ||
1180 newNet.isTeardownRequested()) {
Robert Greenwalt42acef32009-08-12 16:08:25 -07001181 newNet.reconnect();
1182 }
1183 if (DBG) {
1184 if (switchTo.isConnected()) {
Wink Savilleed9c02b2010-12-03 12:01:38 -08001185 log("Switching to already connected " + switchTo.getTypeName());
Robert Greenwalt42acef32009-08-12 16:08:25 -07001186 } else {
Wink Savilleed9c02b2010-12-03 12:01:38 -08001187 log("Attempting to switch to " + switchTo.getTypeName());
Robert Greenwalt42acef32009-08-12 16:08:25 -07001188 }
1189 }
Robert Greenwalt42acef32009-08-12 16:08:25 -07001190 } else {
1191 newNet.reconnect();
Robert Greenwaltf0fa39e2010-03-09 14:55:08 -08001192 newNet = null; // not officially avail.. try anyway, but
1193 // report no failover
Robert Greenwalt42acef32009-08-12 16:08:25 -07001194 }
Robert Greenwalt572172b2010-10-08 16:35:52 -07001195 } else {
Wink Savilleed9c02b2010-12-03 12:01:38 -08001196 loge("Network failover failing.");
Robert Greenwalt42acef32009-08-12 16:08:25 -07001197 }
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001198 }
Robert Greenwalt42acef32009-08-12 16:08:25 -07001199
Robert Greenwaltda03c4e2010-01-20 19:29:41 -08001200 return newNet;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001201 }
1202
1203 private void sendConnectedBroadcast(NetworkInfo info) {
Robert Greenwalt1e9aac22010-09-15 17:36:33 -07001204 sendGeneralBroadcast(info, ConnectivityManager.CONNECTIVITY_ACTION);
1205 }
1206
1207 private void sendInetConditionBroadcast(NetworkInfo info) {
1208 sendGeneralBroadcast(info, ConnectivityManager.INET_CONDITION_ACTION);
1209 }
1210
1211 private void sendGeneralBroadcast(NetworkInfo info, String bcastType) {
1212 Intent intent = new Intent(bcastType);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001213 intent.putExtra(ConnectivityManager.EXTRA_NETWORK_INFO, info);
1214 if (info.isFailover()) {
1215 intent.putExtra(ConnectivityManager.EXTRA_IS_FAILOVER, true);
1216 info.setFailover(false);
1217 }
1218 if (info.getReason() != null) {
1219 intent.putExtra(ConnectivityManager.EXTRA_REASON, info.getReason());
1220 }
1221 if (info.getExtraInfo() != null) {
Robert Greenwalt86e9e552009-07-16 17:21:39 -07001222 intent.putExtra(ConnectivityManager.EXTRA_EXTRA_INFO,
1223 info.getExtraInfo());
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001224 }
Robert Greenwaltd7085fc2010-09-08 15:24:47 -07001225 intent.putExtra(ConnectivityManager.EXTRA_INET_CONDITION, mDefaultInetConditionPublished);
Mike Lockwood0f79b542009-08-14 14:18:49 -04001226 sendStickyBroadcast(intent);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001227 }
1228
1229 /**
1230 * Called when an attempt to fail over to another network has failed.
1231 * @param info the {@link NetworkInfo} for the failed network
1232 */
1233 private void handleConnectionFailure(NetworkInfo info) {
1234 mNetTrackers[info.getType()].setTeardownRequested(false);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001235
Robert Greenwalt42acef32009-08-12 16:08:25 -07001236 String reason = info.getReason();
1237 String extraInfo = info.getExtraInfo();
Robert Greenwalt86e9e552009-07-16 17:21:39 -07001238
Robert Greenwalt572172b2010-10-08 16:35:52 -07001239 String reasonText;
1240 if (reason == null) {
1241 reasonText = ".";
1242 } else {
1243 reasonText = " (" + reason + ").";
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001244 }
Wink Savilleed9c02b2010-12-03 12:01:38 -08001245 loge("Attempt to connect to " + info.getTypeName() + " failed" + reasonText);
Robert Greenwalt42acef32009-08-12 16:08:25 -07001246
1247 Intent intent = new Intent(ConnectivityManager.CONNECTIVITY_ACTION);
1248 intent.putExtra(ConnectivityManager.EXTRA_NETWORK_INFO, info);
1249 if (getActiveNetworkInfo() == null) {
1250 intent.putExtra(ConnectivityManager.EXTRA_NO_CONNECTIVITY, true);
1251 }
1252 if (reason != null) {
1253 intent.putExtra(ConnectivityManager.EXTRA_REASON, reason);
1254 }
1255 if (extraInfo != null) {
1256 intent.putExtra(ConnectivityManager.EXTRA_EXTRA_INFO, extraInfo);
1257 }
1258 if (info.isFailover()) {
1259 intent.putExtra(ConnectivityManager.EXTRA_IS_FAILOVER, true);
1260 info.setFailover(false);
1261 }
Robert Greenwaltda03c4e2010-01-20 19:29:41 -08001262
Robert Greenwaltcc4b4012010-01-25 17:54:29 -08001263 NetworkStateTracker newNet = null;
1264 if (mNetAttributes[info.getType()].isDefault()) {
1265 newNet = tryFailover(info.getType());
1266 if (newNet != null) {
1267 NetworkInfo switchTo = newNet.getNetworkInfo();
Robert Greenwalt029be812010-09-20 18:01:43 -07001268 if (!switchTo.isConnected()) {
Robert Greenwalt572172b2010-10-08 16:35:52 -07001269 // if the other net is connected they've already reset this and perhaps
1270 // even gotten a positive report we don't want to overwrite, but if not
1271 // we need to clear this now to turn our cellular sig strength white
Robert Greenwalt029be812010-09-20 18:01:43 -07001272 mDefaultInetConditionPublished = 0;
1273 }
Robert Greenwaltcc4b4012010-01-25 17:54:29 -08001274 intent.putExtra(ConnectivityManager.EXTRA_OTHER_NETWORK_INFO, switchTo);
1275 } else {
Robert Greenwalt029be812010-09-20 18:01:43 -07001276 mDefaultInetConditionPublished = 0;
Robert Greenwaltcc4b4012010-01-25 17:54:29 -08001277 intent.putExtra(ConnectivityManager.EXTRA_NO_CONNECTIVITY, true);
1278 }
Robert Greenwaltda03c4e2010-01-20 19:29:41 -08001279 }
Robert Greenwaltcc4b4012010-01-25 17:54:29 -08001280
Robert Greenwalt029be812010-09-20 18:01:43 -07001281 intent.putExtra(ConnectivityManager.EXTRA_INET_CONDITION, mDefaultInetConditionPublished);
Mike Lockwood0f79b542009-08-14 14:18:49 -04001282 sendStickyBroadcast(intent);
Robert Greenwaltda03c4e2010-01-20 19:29:41 -08001283 /*
1284 * If the failover network is already connected, then immediately send
1285 * out a followup broadcast indicating successful failover
1286 */
1287 if (newNet != null && newNet.getNetworkInfo().isConnected()) {
1288 sendConnectedBroadcast(newNet.getNetworkInfo());
1289 }
Mike Lockwood0f79b542009-08-14 14:18:49 -04001290 }
1291
1292 private void sendStickyBroadcast(Intent intent) {
1293 synchronized(this) {
Dianne Hackborn1c633fc2009-12-08 19:45:14 -08001294 if (!mSystemReady) {
1295 mInitialBroadcast = new Intent(intent);
Mike Lockwood0f79b542009-08-14 14:18:49 -04001296 }
Dianne Hackborn1c633fc2009-12-08 19:45:14 -08001297 intent.addFlags(Intent.FLAG_RECEIVER_REGISTERED_ONLY_BEFORE_BOOT);
1298 mContext.sendStickyBroadcast(intent);
Mike Lockwood0f79b542009-08-14 14:18:49 -04001299 }
1300 }
1301
1302 void systemReady() {
1303 synchronized(this) {
1304 mSystemReady = true;
Dianne Hackborn1c633fc2009-12-08 19:45:14 -08001305 if (mInitialBroadcast != null) {
1306 mContext.sendStickyBroadcast(mInitialBroadcast);
1307 mInitialBroadcast = null;
Mike Lockwood0f79b542009-08-14 14:18:49 -04001308 }
1309 }
Robert Greenwalt434203a2010-10-11 16:00:27 -07001310 // load the global proxy at startup
1311 mHandler.sendMessage(mHandler.obtainMessage(EVENT_APPLY_GLOBAL_HTTP_PROXY));
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001312 }
1313
1314 private void handleConnect(NetworkInfo info) {
Robert Greenwalt42acef32009-08-12 16:08:25 -07001315 int type = info.getType();
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001316
1317 // snapshot isFailover, because sendConnectedBroadcast() resets it
1318 boolean isFailover = info.isFailover();
Robert Greenwalt42acef32009-08-12 16:08:25 -07001319 NetworkStateTracker thisNet = mNetTrackers[type];
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001320
Robert Greenwalt42acef32009-08-12 16:08:25 -07001321 // if this is a default net and other default is running
1322 // kill the one not preferred
1323 if (mNetAttributes[type].isDefault()) {
Robert Greenwalt42acef32009-08-12 16:08:25 -07001324 if (mActiveDefaultNetwork != -1 && mActiveDefaultNetwork != type) {
1325 if ((type != mNetworkPreference &&
1326 mNetAttributes[mActiveDefaultNetwork].mPriority >
1327 mNetAttributes[type].mPriority) ||
1328 mNetworkPreference == mActiveDefaultNetwork) {
1329 // don't accept this one
Wink Savilleed9c02b2010-12-03 12:01:38 -08001330 if (DBG) {
1331 log("Not broadcasting CONNECT_ACTION " +
Robert Greenwalt42acef32009-08-12 16:08:25 -07001332 "to torn down network " + info.getTypeName());
Wink Savilleed9c02b2010-12-03 12:01:38 -08001333 }
Robert Greenwalt42acef32009-08-12 16:08:25 -07001334 teardown(thisNet);
1335 return;
1336 } else {
1337 // tear down the other
1338 NetworkStateTracker otherNet =
1339 mNetTrackers[mActiveDefaultNetwork];
Wink Savilleed9c02b2010-12-03 12:01:38 -08001340 if (DBG) {
1341 log("Policy requires " + otherNet.getNetworkInfo().getTypeName() +
Robert Greenwalt42acef32009-08-12 16:08:25 -07001342 " teardown");
Wink Savilleed9c02b2010-12-03 12:01:38 -08001343 }
Robert Greenwalt42acef32009-08-12 16:08:25 -07001344 if (!teardown(otherNet)) {
Wink Savilleed9c02b2010-12-03 12:01:38 -08001345 loge("Network declined teardown request");
Robert Greenwalt42acef32009-08-12 16:08:25 -07001346 return;
1347 }
Robert Greenwalt14f2ef42010-06-15 12:19:37 -07001348 }
1349 }
1350 synchronized (ConnectivityService.this) {
1351 // have a new default network, release the transition wakelock in a second
1352 // if it's held. The second pause is to allow apps to reconnect over the
1353 // new network
1354 if (mNetTransitionWakeLock.isHeld()) {
1355 mHandler.sendMessageDelayed(mHandler.obtainMessage(
Robert Greenwaltf3331232010-09-24 14:32:21 -07001356 EVENT_CLEAR_NET_TRANSITION_WAKELOCK,
Robert Greenwalt14f2ef42010-06-15 12:19:37 -07001357 mNetTransitionWakeLockSerialNumber, 0),
1358 1000);
Robert Greenwalt42acef32009-08-12 16:08:25 -07001359 }
1360 }
1361 mActiveDefaultNetwork = type;
Robert Greenwaltd7085fc2010-09-08 15:24:47 -07001362 // this will cause us to come up initially as unconnected and switching
1363 // to connected after our normal pause unless somebody reports us as reall
1364 // disconnected
1365 mDefaultInetConditionPublished = 0;
1366 mDefaultConnectionSequence++;
1367 mInetConditionChangeInFlight = false;
1368 // Don't do this - if we never sign in stay, grey
1369 //reportNetworkCondition(mActiveDefaultNetwork, 100);
Robert Greenwalt42acef32009-08-12 16:08:25 -07001370 }
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001371 thisNet.setTeardownRequested(false);
Irfan Sheriffd649c122010-06-09 15:39:36 -07001372 updateNetworkSettings(thisNet);
Robert Greenwaltc76b8fa2010-07-23 15:46:26 -07001373 handleConnectivityChange(type);
Robert Greenwalt42acef32009-08-12 16:08:25 -07001374 sendConnectedBroadcast(info);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001375 }
1376
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001377 /**
Robert Greenwaltc76b8fa2010-07-23 15:46:26 -07001378 * After a change in the connectivity state of a network. We're mainly
1379 * concerned with making sure that the list of DNS servers is set up
1380 * according to which networks are connected, and ensuring that the
1381 * right routing table entries exist.
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001382 */
Robert Greenwaltc76b8fa2010-07-23 15:46:26 -07001383 private void handleConnectivityChange(int netType) {
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001384 /*
Robert Greenwalt42acef32009-08-12 16:08:25 -07001385 * If a non-default network is enabled, add the host routes that
Robert Greenwaltc76b8fa2010-07-23 15:46:26 -07001386 * will allow it's DNS servers to be accessed.
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001387 */
Robert Greenwaltc76b8fa2010-07-23 15:46:26 -07001388 handleDnsConfigurationChange(netType);
Robert Greenwalt42acef32009-08-12 16:08:25 -07001389
Robert Greenwaltc76b8fa2010-07-23 15:46:26 -07001390 if (mNetTrackers[netType].getNetworkInfo().isConnected()) {
1391 if (mNetAttributes[netType].isDefault()) {
Robert Greenwalt434203a2010-10-11 16:00:27 -07001392 handleApplyDefaultProxy(netType);
Robert Greenwaltc76b8fa2010-07-23 15:46:26 -07001393 addDefaultRoute(mNetTrackers[netType]);
Robert Greenwalt42acef32009-08-12 16:08:25 -07001394 } else {
Robert Greenwaltc76b8fa2010-07-23 15:46:26 -07001395 addPrivateDnsRoutes(mNetTrackers[netType]);
1396 }
1397 } else {
1398 if (mNetAttributes[netType].isDefault()) {
1399 removeDefaultRoute(mNetTrackers[netType]);
1400 } else {
1401 removePrivateDnsRoutes(mNetTrackers[netType]);
Robert Greenwalt42acef32009-08-12 16:08:25 -07001402 }
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001403 }
1404 }
1405
Irfan Sheriffd649c122010-06-09 15:39:36 -07001406 private void addPrivateDnsRoutes(NetworkStateTracker nt) {
Irfan Sheriffd649c122010-06-09 15:39:36 -07001407 boolean privateDnsRouteSet = nt.isPrivateDnsRouteSet();
Robert Greenwalt37e65eb2010-08-30 10:56:47 -07001408 LinkProperties p = nt.getLinkProperties();
Robert Greenwalt47f69fe2010-06-15 15:43:39 -07001409 if (p == null) return;
1410 String interfaceName = p.getInterfaceName();
Irfan Sheriffd649c122010-06-09 15:39:36 -07001411
1412 if (DBG) {
Wink Savilleed9c02b2010-12-03 12:01:38 -08001413 log("addPrivateDnsRoutes for " + nt +
Irfan Sheriffd649c122010-06-09 15:39:36 -07001414 "(" + interfaceName + ") - mPrivateDnsRouteSet = " + privateDnsRouteSet);
1415 }
Irfan Sheriffd649c122010-06-09 15:39:36 -07001416 if (interfaceName != null && !privateDnsRouteSet) {
Robert Greenwalt47f69fe2010-06-15 15:43:39 -07001417 Collection<InetAddress> dnsList = p.getDnses();
1418 for (InetAddress dns : dnsList) {
Wink Savilleed9c02b2010-12-03 12:01:38 -08001419 if (DBG) log(" adding " + dns);
Robert Greenwalt585ac0f2010-08-27 09:24:29 -07001420 NetworkUtils.addHostRoute(interfaceName, dns, null);
Irfan Sheriffd649c122010-06-09 15:39:36 -07001421 }
1422 nt.privateDnsRouteSet(true);
1423 }
1424 }
1425
1426 private void removePrivateDnsRoutes(NetworkStateTracker nt) {
1427 // TODO - we should do this explicitly but the NetUtils api doesnt
1428 // support this yet - must remove all. No worse than before
Robert Greenwalt37e65eb2010-08-30 10:56:47 -07001429 LinkProperties p = nt.getLinkProperties();
Robert Greenwalt47f69fe2010-06-15 15:43:39 -07001430 if (p == null) return;
1431 String interfaceName = p.getInterfaceName();
Irfan Sheriffd649c122010-06-09 15:39:36 -07001432 boolean privateDnsRouteSet = nt.isPrivateDnsRouteSet();
1433 if (interfaceName != null && privateDnsRouteSet) {
1434 if (DBG) {
Wink Savilleed9c02b2010-12-03 12:01:38 -08001435 log("removePrivateDnsRoutes for " + nt.getNetworkInfo().getTypeName() +
Irfan Sheriffd649c122010-06-09 15:39:36 -07001436 " (" + interfaceName + ")");
1437 }
1438 NetworkUtils.removeHostRoutes(interfaceName);
1439 nt.privateDnsRouteSet(false);
1440 }
1441 }
1442
Irfan Sheriffd649c122010-06-09 15:39:36 -07001443
1444 private void addDefaultRoute(NetworkStateTracker nt) {
Robert Greenwalt37e65eb2010-08-30 10:56:47 -07001445 LinkProperties p = nt.getLinkProperties();
Robert Greenwalt47f69fe2010-06-15 15:43:39 -07001446 if (p == null) return;
1447 String interfaceName = p.getInterfaceName();
1448 InetAddress defaultGatewayAddr = p.getGateway();
Irfan Sheriffd649c122010-06-09 15:39:36 -07001449
Robert Greenwaltc76b8fa2010-07-23 15:46:26 -07001450 if ((interfaceName != null) && (defaultGatewayAddr != null )) {
Robert Greenwalt585ac0f2010-08-27 09:24:29 -07001451 if (!NetworkUtils.addDefaultRoute(interfaceName, defaultGatewayAddr) && DBG) {
Robert Greenwalt47f69fe2010-06-15 15:43:39 -07001452 NetworkInfo networkInfo = nt.getNetworkInfo();
Wink Savilleed9c02b2010-12-03 12:01:38 -08001453 log("addDefaultRoute for " + networkInfo.getTypeName() +
Irfan Sheriffd649c122010-06-09 15:39:36 -07001454 " (" + interfaceName + "), GatewayAddr=" + defaultGatewayAddr);
1455 }
Irfan Sheriffd649c122010-06-09 15:39:36 -07001456 }
1457 }
1458
1459
1460 public void removeDefaultRoute(NetworkStateTracker nt) {
Robert Greenwalt37e65eb2010-08-30 10:56:47 -07001461 LinkProperties p = nt.getLinkProperties();
Robert Greenwalt47f69fe2010-06-15 15:43:39 -07001462 if (p == null) return;
1463 String interfaceName = p.getInterfaceName();
Irfan Sheriffd649c122010-06-09 15:39:36 -07001464
Robert Greenwaltc76b8fa2010-07-23 15:46:26 -07001465 if (interfaceName != null) {
1466 if ((NetworkUtils.removeDefaultRoute(interfaceName) >= 0) && DBG) {
Robert Greenwalt47f69fe2010-06-15 15:43:39 -07001467 NetworkInfo networkInfo = nt.getNetworkInfo();
Wink Savilleed9c02b2010-12-03 12:01:38 -08001468 log("removeDefaultRoute for " + networkInfo.getTypeName() + " (" +
Irfan Sheriffd649c122010-06-09 15:39:36 -07001469 interfaceName + ")");
1470 }
Irfan Sheriffd649c122010-06-09 15:39:36 -07001471 }
1472 }
1473
1474 /**
1475 * Reads the network specific TCP buffer sizes from SystemProperties
1476 * net.tcp.buffersize.[default|wifi|umts|edge|gprs] and set them for system
1477 * wide use
1478 */
1479 public void updateNetworkSettings(NetworkStateTracker nt) {
1480 String key = nt.getTcpBufferSizesPropName();
1481 String bufferSizes = SystemProperties.get(key);
1482
1483 if (bufferSizes.length() == 0) {
Wink Savilleed9c02b2010-12-03 12:01:38 -08001484 loge(key + " not found in system properties. Using defaults");
Irfan Sheriffd649c122010-06-09 15:39:36 -07001485
1486 // Setting to default values so we won't be stuck to previous values
1487 key = "net.tcp.buffersize.default";
1488 bufferSizes = SystemProperties.get(key);
1489 }
1490
1491 // Set values in kernel
1492 if (bufferSizes.length() != 0) {
1493 if (DBG) {
Wink Savilleed9c02b2010-12-03 12:01:38 -08001494 log("Setting TCP values: [" + bufferSizes
Irfan Sheriffd649c122010-06-09 15:39:36 -07001495 + "] which comes from [" + key + "]");
1496 }
1497 setBufferSize(bufferSizes);
1498 }
1499 }
1500
1501 /**
1502 * Writes TCP buffer sizes to /sys/kernel/ipv4/tcp_[r/w]mem_[min/def/max]
1503 * which maps to /proc/sys/net/ipv4/tcp_rmem and tcpwmem
1504 *
1505 * @param bufferSizes in the format of "readMin, readInitial, readMax,
1506 * writeMin, writeInitial, writeMax"
1507 */
1508 private void setBufferSize(String bufferSizes) {
1509 try {
1510 String[] values = bufferSizes.split(",");
1511
1512 if (values.length == 6) {
1513 final String prefix = "/sys/kernel/ipv4/tcp_";
1514 stringToFile(prefix + "rmem_min", values[0]);
1515 stringToFile(prefix + "rmem_def", values[1]);
1516 stringToFile(prefix + "rmem_max", values[2]);
1517 stringToFile(prefix + "wmem_min", values[3]);
1518 stringToFile(prefix + "wmem_def", values[4]);
1519 stringToFile(prefix + "wmem_max", values[5]);
1520 } else {
Wink Savilleed9c02b2010-12-03 12:01:38 -08001521 loge("Invalid buffersize string: " + bufferSizes);
Irfan Sheriffd649c122010-06-09 15:39:36 -07001522 }
1523 } catch (IOException e) {
Wink Savilleed9c02b2010-12-03 12:01:38 -08001524 loge("Can't set tcp buffer sizes:" + e);
Irfan Sheriffd649c122010-06-09 15:39:36 -07001525 }
1526 }
1527
1528 /**
1529 * Writes string to file. Basically same as "echo -n $string > $filename"
1530 *
1531 * @param filename
1532 * @param string
1533 * @throws IOException
1534 */
1535 private void stringToFile(String filename, String string) throws IOException {
1536 FileWriter out = new FileWriter(filename);
1537 try {
1538 out.write(string);
1539 } finally {
1540 out.close();
1541 }
1542 }
1543
1544
Robert Greenwalt42acef32009-08-12 16:08:25 -07001545 /**
1546 * Adjust the per-process dns entries (net.dns<x>.<pid>) based
1547 * on the highest priority active net which this process requested.
1548 * If there aren't any, clear it out
1549 */
1550 private void reassessPidDns(int myPid, boolean doBump)
1551 {
Wink Savilleed9c02b2010-12-03 12:01:38 -08001552 if (DBG) log("reassessPidDns for pid " + myPid);
Robert Greenwalt42acef32009-08-12 16:08:25 -07001553 for(int i : mPriorityList) {
1554 if (mNetAttributes[i].isDefault()) {
1555 continue;
1556 }
1557 NetworkStateTracker nt = mNetTrackers[i];
Robert Greenwalt86e9e552009-07-16 17:21:39 -07001558 if (nt.getNetworkInfo().isConnected() &&
1559 !nt.isTeardownRequested()) {
Robert Greenwalt37e65eb2010-08-30 10:56:47 -07001560 LinkProperties p = nt.getLinkProperties();
Robert Greenwalt47f69fe2010-06-15 15:43:39 -07001561 if (p == null) continue;
Robert Greenwalt42acef32009-08-12 16:08:25 -07001562 List pids = mNetRequestersPids[i];
1563 for (int j=0; j<pids.size(); j++) {
1564 Integer pid = (Integer)pids.get(j);
1565 if (pid.intValue() == myPid) {
Robert Greenwalt47f69fe2010-06-15 15:43:39 -07001566 Collection<InetAddress> dnses = p.getDnses();
1567 writePidDns(dnses, myPid);
Robert Greenwalt42acef32009-08-12 16:08:25 -07001568 if (doBump) {
1569 bumpDns();
1570 }
1571 return;
1572 }
1573 }
1574 }
1575 }
1576 // nothing found - delete
1577 for (int i = 1; ; i++) {
1578 String prop = "net.dns" + i + "." + myPid;
1579 if (SystemProperties.get(prop).length() == 0) {
1580 if (doBump) {
1581 bumpDns();
1582 }
1583 return;
1584 }
1585 SystemProperties.set(prop, "");
1586 }
1587 }
1588
Robert Greenwalt10398722010-12-17 15:20:36 -08001589 // return true if results in a change
1590 private boolean writePidDns(Collection <InetAddress> dnses, int pid) {
Robert Greenwalt42acef32009-08-12 16:08:25 -07001591 int j = 1;
Robert Greenwalt10398722010-12-17 15:20:36 -08001592 boolean changed = false;
Robert Greenwalt47f69fe2010-06-15 15:43:39 -07001593 for (InetAddress dns : dnses) {
Robert Greenwalt10398722010-12-17 15:20:36 -08001594 String dnsString = dns.getHostAddress();
1595 if (changed || !dnsString.equals(SystemProperties.get("net.dns" + j + "." + pid))) {
1596 changed = true;
1597 SystemProperties.set("net.dns" + j++ + "." + pid, dns.getHostAddress());
1598 }
Robert Greenwalt42acef32009-08-12 16:08:25 -07001599 }
Robert Greenwalt10398722010-12-17 15:20:36 -08001600 return changed;
Robert Greenwalt42acef32009-08-12 16:08:25 -07001601 }
1602
1603 private void bumpDns() {
1604 /*
1605 * Bump the property that tells the name resolver library to reread
1606 * the DNS server list from the properties.
1607 */
1608 String propVal = SystemProperties.get("net.dnschange");
1609 int n = 0;
1610 if (propVal.length() != 0) {
1611 try {
1612 n = Integer.parseInt(propVal);
1613 } catch (NumberFormatException e) {}
1614 }
1615 SystemProperties.set("net.dnschange", "" + (n+1));
Robert Greenwalt03595d02010-11-02 14:08:23 -07001616 /*
1617 * Tell the VMs to toss their DNS caches
1618 */
1619 Intent intent = new Intent(Intent.ACTION_CLEAR_DNS_CACHE);
1620 intent.addFlags(Intent.FLAG_RECEIVER_REPLACE_PENDING);
1621 mContext.sendBroadcast(intent);
Robert Greenwalt42acef32009-08-12 16:08:25 -07001622 }
1623
Robert Greenwaltc76b8fa2010-07-23 15:46:26 -07001624 private void handleDnsConfigurationChange(int netType) {
Robert Greenwalt42acef32009-08-12 16:08:25 -07001625 // add default net's dns entries
Robert Greenwaltc76b8fa2010-07-23 15:46:26 -07001626 NetworkStateTracker nt = mNetTrackers[netType];
1627 if (nt != null && nt.getNetworkInfo().isConnected() && !nt.isTeardownRequested()) {
Robert Greenwalt37e65eb2010-08-30 10:56:47 -07001628 LinkProperties p = nt.getLinkProperties();
Robert Greenwaltc76b8fa2010-07-23 15:46:26 -07001629 if (p == null) return;
1630 Collection<InetAddress> dnses = p.getDnses();
Robert Greenwalt10398722010-12-17 15:20:36 -08001631 boolean changed = false;
Robert Greenwaltc76b8fa2010-07-23 15:46:26 -07001632 if (mNetAttributes[netType].isDefault()) {
1633 int j = 1;
Robert Greenwalte90aa5e2010-09-01 11:34:05 -07001634 if (dnses.size() == 0 && mDefaultDns != null) {
Robert Greenwalt10398722010-12-17 15:20:36 -08001635 String dnsString = mDefaultDns.getHostAddress();
1636 if (!dnsString.equals(SystemProperties.get("net.dns1"))) {
1637 if (DBG) {
1638 log("no dns provided - using " + dnsString);
1639 }
1640 changed = true;
1641 SystemProperties.set("net.dns1", dnsString);
Robert Greenwalt42acef32009-08-12 16:08:25 -07001642 }
Robert Greenwalte90aa5e2010-09-01 11:34:05 -07001643 j++;
1644 } else {
1645 for (InetAddress dns : dnses) {
Robert Greenwalt10398722010-12-17 15:20:36 -08001646 String dnsString = dns.getHostAddress();
1647 if (!changed && dnsString.equals(SystemProperties.get("net.dns" + j))) {
1648 j++;
1649 continue;
1650 }
Robert Greenwalte90aa5e2010-09-01 11:34:05 -07001651 if (DBG) {
Wink Savilleed9c02b2010-12-03 12:01:38 -08001652 log("adding dns " + dns + " for " +
Robert Greenwalte90aa5e2010-09-01 11:34:05 -07001653 nt.getNetworkInfo().getTypeName());
1654 }
Robert Greenwalt10398722010-12-17 15:20:36 -08001655 changed = true;
1656 SystemProperties.set("net.dns" + j++, dnsString);
Robert Greenwalte90aa5e2010-09-01 11:34:05 -07001657 }
Robert Greenwaltc76b8fa2010-07-23 15:46:26 -07001658 }
1659 for (int k=j ; k<mNumDnsEntries; k++) {
Robert Greenwalt10398722010-12-17 15:20:36 -08001660 if (changed || !TextUtils.isEmpty(SystemProperties.get("net.dns" + k))) {
1661 if (DBG) log("erasing net.dns" + k);
1662 changed = true;
1663 SystemProperties.set("net.dns" + k, "");
1664 }
Robert Greenwaltc76b8fa2010-07-23 15:46:26 -07001665 }
1666 mNumDnsEntries = j;
1667 } else {
1668 // set per-pid dns for attached secondary nets
1669 List pids = mNetRequestersPids[netType];
1670 for (int y=0; y< pids.size(); y++) {
1671 Integer pid = (Integer)pids.get(y);
Robert Greenwalt10398722010-12-17 15:20:36 -08001672 changed = writePidDns(dnses, pid.intValue());
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001673 }
1674 }
Robert Greenwalt10398722010-12-17 15:20:36 -08001675 if (changed) bumpDns();
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001676 }
Robert Greenwalt42acef32009-08-12 16:08:25 -07001677 }
1678
1679 private int getRestoreDefaultNetworkDelay() {
1680 String restoreDefaultNetworkDelayStr = SystemProperties.get(
1681 NETWORK_RESTORE_DELAY_PROP_NAME);
1682 if(restoreDefaultNetworkDelayStr != null &&
1683 restoreDefaultNetworkDelayStr.length() != 0) {
1684 try {
1685 return Integer.valueOf(restoreDefaultNetworkDelayStr);
1686 } catch (NumberFormatException e) {
1687 }
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001688 }
Robert Greenwalt42acef32009-08-12 16:08:25 -07001689 return RESTORE_DEFAULT_NETWORK_DELAY;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001690 }
1691
1692 @Override
1693 protected void dump(FileDescriptor fd, PrintWriter pw, String[] args) {
Robert Greenwalt86e9e552009-07-16 17:21:39 -07001694 if (mContext.checkCallingOrSelfPermission(
1695 android.Manifest.permission.DUMP)
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001696 != PackageManager.PERMISSION_GRANTED) {
Robert Greenwalt86e9e552009-07-16 17:21:39 -07001697 pw.println("Permission Denial: can't dump ConnectivityService " +
1698 "from from pid=" + Binder.getCallingPid() + ", uid=" +
1699 Binder.getCallingUid());
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001700 return;
1701 }
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001702 pw.println();
1703 for (NetworkStateTracker nst : mNetTrackers) {
Robert Greenwaltb9285352009-12-21 18:24:07 -08001704 if (nst != null) {
1705 if (nst.getNetworkInfo().isConnected()) {
1706 pw.println("Active network: " + nst.getNetworkInfo().
1707 getTypeName());
1708 }
1709 pw.println(nst.getNetworkInfo());
1710 pw.println(nst);
1711 pw.println();
Robert Greenwalt42acef32009-08-12 16:08:25 -07001712 }
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001713 }
Robert Greenwaltb9285352009-12-21 18:24:07 -08001714
1715 pw.println("Network Requester Pids:");
1716 for (int net : mPriorityList) {
1717 String pidString = net + ": ";
1718 for (Object pid : mNetRequestersPids[net]) {
1719 pidString = pidString + pid.toString() + ", ";
1720 }
1721 pw.println(pidString);
1722 }
1723 pw.println();
1724
1725 pw.println("FeatureUsers:");
1726 for (Object requester : mFeatureUsers) {
1727 pw.println(requester.toString());
1728 }
1729 pw.println();
Robert Greenwalt2a091d72010-02-11 18:18:40 -08001730
Robert Greenwalt14f2ef42010-06-15 12:19:37 -07001731 synchronized (this) {
1732 pw.println("NetworkTranstionWakeLock is currently " +
1733 (mNetTransitionWakeLock.isHeld() ? "" : "not ") + "held.");
1734 pw.println("It was last requested for "+mNetTransitionWakeLockCausedBy);
1735 }
1736 pw.println();
1737
Robert Greenwalt2a091d72010-02-11 18:18:40 -08001738 mTethering.dump(fd, pw, args);
Robert Greenwalt4e8dfef2010-09-20 14:35:25 -07001739
1740 if (mInetLog != null) {
1741 pw.println();
1742 pw.println("Inet condition reports:");
1743 for(int i = 0; i < mInetLog.size(); i++) {
1744 pw.println(mInetLog.get(i));
1745 }
1746 }
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001747 }
1748
Robert Greenwalt42acef32009-08-12 16:08:25 -07001749 // must be stateless - things change under us.
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001750 private class MyHandler extends Handler {
Wink Savillebb08caf2010-09-02 19:23:52 -07001751 public MyHandler(Looper looper) {
1752 super(looper);
1753 }
1754
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001755 @Override
1756 public void handleMessage(Message msg) {
1757 NetworkInfo info;
1758 switch (msg.what) {
1759 case NetworkStateTracker.EVENT_STATE_CHANGED:
1760 info = (NetworkInfo) msg.obj;
Robert Greenwalt511288a2009-12-07 11:33:18 -08001761 int type = info.getType();
1762 NetworkInfo.State state = info.getState();
Robert Greenwalt6e6dec22010-01-25 16:14:00 -08001763 // only do this optimization for wifi. It going into scan mode for location
1764 // services generates alot of noise. Meanwhile the mms apn won't send out
1765 // subsequent notifications when on default cellular because it never
1766 // disconnects.. so only do this to wifi notifications. Fixed better when the
1767 // APN notifications are standardized.
1768 if (mNetAttributes[type].mLastState == state &&
1769 mNetAttributes[type].mRadio == ConnectivityManager.TYPE_WIFI) {
Robert Greenwalt511288a2009-12-07 11:33:18 -08001770 if (DBG) {
Robert Greenwalt6e6dec22010-01-25 16:14:00 -08001771 // TODO - remove this after we validate the dropping doesn't break
1772 // anything
Wink Savilleed9c02b2010-12-03 12:01:38 -08001773 log("Dropping ConnectivityChange for " +
Robert Greenwalt1193ae42010-01-13 09:36:31 -08001774 info.getTypeName() + ": " +
Robert Greenwalt511288a2009-12-07 11:33:18 -08001775 state + "/" + info.getDetailedState());
1776 }
1777 return;
1778 }
1779 mNetAttributes[type].mLastState = state;
1780
Wink Savilleed9c02b2010-12-03 12:01:38 -08001781 if (DBG) log("ConnectivityChange for " +
Robert Greenwalt86e9e552009-07-16 17:21:39 -07001782 info.getTypeName() + ": " +
Robert Greenwalt511288a2009-12-07 11:33:18 -08001783 state + "/" + info.getDetailedState());
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001784
1785 // Connectivity state changed:
1786 // [31-13] Reserved for future use
Robert Greenwalt86e9e552009-07-16 17:21:39 -07001787 // [12-9] Network subtype (for mobile network, as defined
1788 // by TelephonyManager)
1789 // [8-3] Detailed state ordinal (as defined by
1790 // NetworkInfo.DetailedState)
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001791 // [2-0] Network type (as defined by ConnectivityManager)
1792 int eventLogParam = (info.getType() & 0x7) |
1793 ((info.getDetailedState().ordinal() & 0x3f) << 3) |
1794 (info.getSubtype() << 9);
Doug Zongkerab5c49c2009-12-04 10:31:43 -08001795 EventLog.writeEvent(EventLogTags.CONNECTIVITY_STATE_CHANGED,
Robert Greenwalt86e9e552009-07-16 17:21:39 -07001796 eventLogParam);
1797
1798 if (info.getDetailedState() ==
1799 NetworkInfo.DetailedState.FAILED) {
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001800 handleConnectionFailure(info);
Robert Greenwalt511288a2009-12-07 11:33:18 -08001801 } else if (state == NetworkInfo.State.DISCONNECTED) {
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001802 handleDisconnect(info);
Robert Greenwalt511288a2009-12-07 11:33:18 -08001803 } else if (state == NetworkInfo.State.SUSPENDED) {
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001804 // TODO: need to think this over.
Robert Greenwalt86e9e552009-07-16 17:21:39 -07001805 // the logic here is, handle SUSPENDED the same as
1806 // DISCONNECTED. The only difference being we are
1807 // broadcasting an intent with NetworkInfo that's
1808 // suspended. This allows the applications an
1809 // opportunity to handle DISCONNECTED and SUSPENDED
1810 // differently, or not.
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001811 handleDisconnect(info);
Robert Greenwalt511288a2009-12-07 11:33:18 -08001812 } else if (state == NetworkInfo.State.CONNECTED) {
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001813 handleConnect(info);
1814 }
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001815 break;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001816 case NetworkStateTracker.EVENT_CONFIGURATION_CHANGED:
Robert Greenwaltc76b8fa2010-07-23 15:46:26 -07001817 info = (NetworkInfo) msg.obj;
1818 type = info.getType();
Robert Greenwalt434203a2010-10-11 16:00:27 -07001819 handleConnectivityChange(type);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001820 break;
Robert Greenwaltf3331232010-09-24 14:32:21 -07001821 case EVENT_CLEAR_NET_TRANSITION_WAKELOCK:
Robert Greenwalt14f2ef42010-06-15 12:19:37 -07001822 String causedBy = null;
1823 synchronized (ConnectivityService.this) {
1824 if (msg.arg1 == mNetTransitionWakeLockSerialNumber &&
1825 mNetTransitionWakeLock.isHeld()) {
1826 mNetTransitionWakeLock.release();
1827 causedBy = mNetTransitionWakeLockCausedBy;
1828 }
1829 }
1830 if (causedBy != null) {
Wink Savilleed9c02b2010-12-03 12:01:38 -08001831 log("NetTransition Wakelock for " + causedBy + " released by timeout");
Robert Greenwalt14f2ef42010-06-15 12:19:37 -07001832 }
Robert Greenwalt057d5e92010-09-09 14:05:10 -07001833 break;
Robert Greenwalt8dcc28b2010-09-23 10:05:56 -07001834 case EVENT_RESTORE_DEFAULT_NETWORK:
Robert Greenwalt9c75d4a2009-09-27 17:27:04 -07001835 FeatureUser u = (FeatureUser)msg.obj;
1836 u.expire();
Robert Greenwaltd7085fc2010-09-08 15:24:47 -07001837 break;
Robert Greenwalt8dcc28b2010-09-23 10:05:56 -07001838 case EVENT_INET_CONDITION_CHANGE:
1839 {
1840 int netType = msg.arg1;
1841 int condition = msg.arg2;
1842 handleInetConditionChange(netType, condition);
Robert Greenwalt14f2ef42010-06-15 12:19:37 -07001843 break;
Robert Greenwalt8dcc28b2010-09-23 10:05:56 -07001844 }
1845 case EVENT_INET_CONDITION_HOLD_END:
1846 {
1847 int netType = msg.arg1;
1848 int sequence = msg.arg2;
1849 handleInetConditionHoldEnd(netType, sequence);
Robert Greenwaltd7085fc2010-09-08 15:24:47 -07001850 break;
Robert Greenwalt8dcc28b2010-09-23 10:05:56 -07001851 }
1852 case EVENT_SET_NETWORK_PREFERENCE:
1853 {
1854 int preference = msg.arg1;
1855 handleSetNetworkPreference(preference);
1856 break;
1857 }
1858 case EVENT_SET_BACKGROUND_DATA:
1859 {
1860 boolean enabled = (msg.arg1 == ENABLED);
1861 handleSetBackgroundData(enabled);
1862 break;
1863 }
1864 case EVENT_SET_MOBILE_DATA:
1865 {
1866 boolean enabled = (msg.arg1 == ENABLED);
1867 handleSetMobileData(enabled);
1868 break;
1869 }
Robert Greenwalt434203a2010-10-11 16:00:27 -07001870 case EVENT_APPLY_GLOBAL_HTTP_PROXY:
1871 {
1872 handleDeprecatedGlobalHttpProxy();
1873 }
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001874 }
1875 }
1876 }
Robert Greenwaltd0e18ff2010-01-26 11:40:34 -08001877
1878 // javadoc from interface
Robert Greenwalt5a735062010-03-02 17:25:02 -08001879 public int tether(String iface) {
Robert Greenwaltd0e18ff2010-01-26 11:40:34 -08001880 enforceTetherChangePermission();
Robert Greenwalt5a735062010-03-02 17:25:02 -08001881
1882 if (isTetheringSupported()) {
1883 return mTethering.tether(iface);
1884 } else {
1885 return ConnectivityManager.TETHER_ERROR_UNSUPPORTED;
1886 }
Robert Greenwaltd0e18ff2010-01-26 11:40:34 -08001887 }
1888
1889 // javadoc from interface
Robert Greenwalt5a735062010-03-02 17:25:02 -08001890 public int untether(String iface) {
Robert Greenwaltd0e18ff2010-01-26 11:40:34 -08001891 enforceTetherChangePermission();
Robert Greenwalt5a735062010-03-02 17:25:02 -08001892
1893 if (isTetheringSupported()) {
1894 return mTethering.untether(iface);
1895 } else {
1896 return ConnectivityManager.TETHER_ERROR_UNSUPPORTED;
1897 }
1898 }
1899
1900 // javadoc from interface
1901 public int getLastTetherError(String iface) {
1902 enforceTetherAccessPermission();
1903
1904 if (isTetheringSupported()) {
1905 return mTethering.getLastTetherError(iface);
1906 } else {
1907 return ConnectivityManager.TETHER_ERROR_UNSUPPORTED;
1908 }
Robert Greenwalt2a091d72010-02-11 18:18:40 -08001909 }
1910
1911 // TODO - proper iface API for selection by property, inspection, etc
1912 public String[] getTetherableUsbRegexs() {
1913 enforceTetherAccessPermission();
1914 if (isTetheringSupported()) {
1915 return mTethering.getTetherableUsbRegexs();
1916 } else {
1917 return new String[0];
1918 }
1919 }
1920
1921 public String[] getTetherableWifiRegexs() {
1922 enforceTetherAccessPermission();
1923 if (isTetheringSupported()) {
1924 return mTethering.getTetherableWifiRegexs();
1925 } else {
1926 return new String[0];
1927 }
Robert Greenwaltd0e18ff2010-01-26 11:40:34 -08001928 }
1929
Danica Chang6fdd0c62010-08-11 14:54:43 -07001930 public String[] getTetherableBluetoothRegexs() {
1931 enforceTetherAccessPermission();
1932 if (isTetheringSupported()) {
1933 return mTethering.getTetherableBluetoothRegexs();
1934 } else {
1935 return new String[0];
1936 }
1937 }
1938
Robert Greenwaltd0e18ff2010-01-26 11:40:34 -08001939 // TODO - move iface listing, queries, etc to new module
1940 // javadoc from interface
1941 public String[] getTetherableIfaces() {
Robert Greenwalt2a091d72010-02-11 18:18:40 -08001942 enforceTetherAccessPermission();
Robert Greenwaltd0e18ff2010-01-26 11:40:34 -08001943 return mTethering.getTetherableIfaces();
1944 }
1945
1946 public String[] getTetheredIfaces() {
Robert Greenwalt2a091d72010-02-11 18:18:40 -08001947 enforceTetherAccessPermission();
Robert Greenwaltd0e18ff2010-01-26 11:40:34 -08001948 return mTethering.getTetheredIfaces();
1949 }
Robert Greenwalt2a091d72010-02-11 18:18:40 -08001950
Robert Greenwalt5a735062010-03-02 17:25:02 -08001951 public String[] getTetheringErroredIfaces() {
1952 enforceTetherAccessPermission();
1953 return mTethering.getErroredIfaces();
1954 }
1955
Robert Greenwalt2a091d72010-02-11 18:18:40 -08001956 // if ro.tether.denied = true we default to no tethering
1957 // gservices could set the secure setting to 1 though to enable it on a build where it
1958 // had previously been turned off.
1959 public boolean isTetheringSupported() {
1960 enforceTetherAccessPermission();
1961 int defaultVal = (SystemProperties.get("ro.tether.denied").equals("true") ? 0 : 1);
Robert Greenwaltc9d5fb72010-02-25 12:29:30 -08001962 boolean tetherEnabledInSettings = (Settings.Secure.getInt(mContext.getContentResolver(),
1963 Settings.Secure.TETHER_SUPPORTED, defaultVal) != 0);
1964 return tetherEnabledInSettings && mTetheringConfigValid;
Robert Greenwalt2a091d72010-02-11 18:18:40 -08001965 }
Robert Greenwalt14f2ef42010-06-15 12:19:37 -07001966
1967 // An API NetworkStateTrackers can call when they lose their network.
1968 // This will automatically be cleared after X seconds or a network becomes CONNECTED,
1969 // whichever happens first. The timer is started by the first caller and not
1970 // restarted by subsequent callers.
1971 public void requestNetworkTransitionWakelock(String forWhom) {
1972 enforceConnectivityInternalPermission();
1973 synchronized (this) {
1974 if (mNetTransitionWakeLock.isHeld()) return;
1975 mNetTransitionWakeLockSerialNumber++;
1976 mNetTransitionWakeLock.acquire();
1977 mNetTransitionWakeLockCausedBy = forWhom;
1978 }
1979 mHandler.sendMessageDelayed(mHandler.obtainMessage(
Robert Greenwaltf3331232010-09-24 14:32:21 -07001980 EVENT_CLEAR_NET_TRANSITION_WAKELOCK,
Robert Greenwalt14f2ef42010-06-15 12:19:37 -07001981 mNetTransitionWakeLockSerialNumber, 0),
1982 mNetTransitionWakeLockTimeout);
1983 return;
1984 }
Robert Greenwaltca4306c2010-09-09 13:15:32 -07001985
Robert Greenwaltd7085fc2010-09-08 15:24:47 -07001986 // 100 percent is full good, 0 is full bad.
1987 public void reportInetCondition(int networkType, int percentage) {
Wink Savilleed9c02b2010-12-03 12:01:38 -08001988 if (DBG) log("reportNetworkCondition(" + networkType + ", " + percentage + ")");
Robert Greenwaltd7085fc2010-09-08 15:24:47 -07001989 mContext.enforceCallingOrSelfPermission(
1990 android.Manifest.permission.STATUS_BAR,
1991 "ConnectivityService");
1992
Robert Greenwalt4e8dfef2010-09-20 14:35:25 -07001993 if (DBG) {
1994 int pid = getCallingPid();
1995 int uid = getCallingUid();
1996 String s = pid + "(" + uid + ") reports inet is " +
1997 (percentage > 50 ? "connected" : "disconnected") + " (" + percentage + ") on " +
1998 "network Type " + networkType + " at " + GregorianCalendar.getInstance().getTime();
1999 mInetLog.add(s);
2000 while(mInetLog.size() > INET_CONDITION_LOG_MAX_SIZE) {
2001 mInetLog.remove(0);
2002 }
2003 }
Robert Greenwaltd7085fc2010-09-08 15:24:47 -07002004 mHandler.sendMessage(mHandler.obtainMessage(
Robert Greenwalt8dcc28b2010-09-23 10:05:56 -07002005 EVENT_INET_CONDITION_CHANGE, networkType, percentage));
2006 }
2007
2008 private void handleInetConditionChange(int netType, int condition) {
2009 if (DBG) {
Wink Savilleed9c02b2010-12-03 12:01:38 -08002010 log("Inet connectivity change, net=" +
Robert Greenwalt8dcc28b2010-09-23 10:05:56 -07002011 netType + ", condition=" + condition +
2012 ",mActiveDefaultNetwork=" + mActiveDefaultNetwork);
2013 }
2014 if (mActiveDefaultNetwork == -1) {
Wink Savilleed9c02b2010-12-03 12:01:38 -08002015 if (DBG) log("no active default network - aborting");
Robert Greenwalt8dcc28b2010-09-23 10:05:56 -07002016 return;
2017 }
2018 if (mActiveDefaultNetwork != netType) {
Wink Savilleed9c02b2010-12-03 12:01:38 -08002019 if (DBG) log("given net not default - aborting");
Robert Greenwalt8dcc28b2010-09-23 10:05:56 -07002020 return;
2021 }
2022 mDefaultInetCondition = condition;
2023 int delay;
2024 if (mInetConditionChangeInFlight == false) {
Wink Savilleed9c02b2010-12-03 12:01:38 -08002025 if (DBG) log("starting a change hold");
Robert Greenwalt8dcc28b2010-09-23 10:05:56 -07002026 // setup a new hold to debounce this
2027 if (mDefaultInetCondition > 50) {
2028 delay = Settings.Secure.getInt(mContext.getContentResolver(),
2029 Settings.Secure.INET_CONDITION_DEBOUNCE_UP_DELAY, 500);
2030 } else {
2031 delay = Settings.Secure.getInt(mContext.getContentResolver(),
2032 Settings.Secure.INET_CONDITION_DEBOUNCE_DOWN_DELAY, 3000);
2033 }
2034 mInetConditionChangeInFlight = true;
2035 mHandler.sendMessageDelayed(mHandler.obtainMessage(EVENT_INET_CONDITION_HOLD_END,
2036 mActiveDefaultNetwork, mDefaultConnectionSequence), delay);
2037 } else {
2038 // we've set the new condition, when this hold ends that will get
2039 // picked up
Wink Savilleed9c02b2010-12-03 12:01:38 -08002040 if (DBG) log("currently in hold - not setting new end evt");
Robert Greenwalt8dcc28b2010-09-23 10:05:56 -07002041 }
2042 }
2043
2044 private void handleInetConditionHoldEnd(int netType, int sequence) {
2045 if (DBG) {
Wink Savilleed9c02b2010-12-03 12:01:38 -08002046 log("Inet hold end, net=" + netType +
Robert Greenwalt8dcc28b2010-09-23 10:05:56 -07002047 ", condition =" + mDefaultInetCondition +
2048 ", published condition =" + mDefaultInetConditionPublished);
2049 }
2050 mInetConditionChangeInFlight = false;
2051
2052 if (mActiveDefaultNetwork == -1) {
Wink Savilleed9c02b2010-12-03 12:01:38 -08002053 if (DBG) log("no active default network - aborting");
Robert Greenwalt8dcc28b2010-09-23 10:05:56 -07002054 return;
2055 }
2056 if (mDefaultConnectionSequence != sequence) {
Wink Savilleed9c02b2010-12-03 12:01:38 -08002057 if (DBG) log("event hold for obsolete network - aborting");
Robert Greenwalt8dcc28b2010-09-23 10:05:56 -07002058 return;
2059 }
2060 if (mDefaultInetConditionPublished == mDefaultInetCondition) {
Wink Savilleed9c02b2010-12-03 12:01:38 -08002061 if (DBG) log("no change in condition - aborting");
Robert Greenwalt8dcc28b2010-09-23 10:05:56 -07002062 return;
2063 }
2064 NetworkInfo networkInfo = mNetTrackers[mActiveDefaultNetwork].getNetworkInfo();
2065 if (networkInfo.isConnected() == false) {
Wink Savilleed9c02b2010-12-03 12:01:38 -08002066 if (DBG) log("default network not connected - aborting");
Robert Greenwalt8dcc28b2010-09-23 10:05:56 -07002067 return;
2068 }
2069 mDefaultInetConditionPublished = mDefaultInetCondition;
2070 sendInetConditionBroadcast(networkInfo);
2071 return;
Robert Greenwaltd7085fc2010-09-08 15:24:47 -07002072 }
Robert Greenwalt434203a2010-10-11 16:00:27 -07002073
2074 public synchronized ProxyProperties getProxy() {
2075 if (mGlobalProxy != null) return mGlobalProxy;
2076 if (mDefaultProxy != null) return mDefaultProxy;
2077 return null;
2078 }
2079
2080 public void setGlobalProxy(ProxyProperties proxyProperties) {
2081 enforceChangePermission();
2082 synchronized (mGlobalProxyLock) {
2083 if (proxyProperties == mGlobalProxy) return;
2084 if (proxyProperties != null && proxyProperties.equals(mGlobalProxy)) return;
2085 if (mGlobalProxy != null && mGlobalProxy.equals(proxyProperties)) return;
2086
2087 String host = "";
2088 int port = 0;
2089 String exclList = "";
2090 if (proxyProperties != null && !TextUtils.isEmpty(proxyProperties.getHost())) {
2091 mGlobalProxy = new ProxyProperties(proxyProperties);
2092 host = mGlobalProxy.getHost();
2093 port = mGlobalProxy.getPort();
2094 exclList = mGlobalProxy.getExclusionList();
2095 } else {
2096 mGlobalProxy = null;
2097 }
2098 ContentResolver res = mContext.getContentResolver();
2099 Settings.Secure.putString(res, Settings.Secure.GLOBAL_HTTP_PROXY_HOST, host);
2100 Settings.Secure.putInt(res, Settings.Secure.GLOBAL_HTTP_PROXY_PORT, port);
Robert Greenwaltb7090d62010-12-02 11:31:00 -08002101 Settings.Secure.putString(res, Settings.Secure.GLOBAL_HTTP_PROXY_EXCLUSION_LIST,
Robert Greenwalt434203a2010-10-11 16:00:27 -07002102 exclList);
2103 }
2104
2105 if (mGlobalProxy == null) {
2106 proxyProperties = mDefaultProxy;
2107 }
2108 sendProxyBroadcast(proxyProperties);
2109 }
2110
Robert Greenwaltb7090d62010-12-02 11:31:00 -08002111 private void loadGlobalProxy() {
2112 ContentResolver res = mContext.getContentResolver();
2113 String host = Settings.Secure.getString(res, Settings.Secure.GLOBAL_HTTP_PROXY_HOST);
2114 int port = Settings.Secure.getInt(res, Settings.Secure.GLOBAL_HTTP_PROXY_PORT, 0);
2115 String exclList = Settings.Secure.getString(res,
2116 Settings.Secure.GLOBAL_HTTP_PROXY_EXCLUSION_LIST);
2117 if (!TextUtils.isEmpty(host)) {
2118 ProxyProperties proxyProperties = new ProxyProperties(host, port, exclList);
2119 synchronized (mGlobalProxyLock) {
2120 mGlobalProxy = proxyProperties;
2121 }
2122 }
2123 }
2124
Robert Greenwalt434203a2010-10-11 16:00:27 -07002125 public ProxyProperties getGlobalProxy() {
2126 synchronized (mGlobalProxyLock) {
2127 return mGlobalProxy;
2128 }
2129 }
2130
2131 private void handleApplyDefaultProxy(int type) {
2132 // check if new default - push it out to all VM if so
2133 ProxyProperties proxy = mNetTrackers[type].getLinkProperties().getHttpProxy();
2134 synchronized (this) {
2135 if (mDefaultProxy != null && mDefaultProxy.equals(proxy)) return;
2136 if (mDefaultProxy == proxy) return;
2137 if (!TextUtils.isEmpty(proxy.getHost())) {
2138 mDefaultProxy = proxy;
2139 } else {
2140 mDefaultProxy = null;
2141 }
2142 }
Wink Savilleed9c02b2010-12-03 12:01:38 -08002143 if (DBG) log("changing default proxy to " + proxy);
Robert Greenwalt434203a2010-10-11 16:00:27 -07002144 if ((proxy == null && mGlobalProxy == null) || proxy.equals(mGlobalProxy)) return;
2145 if (mGlobalProxy != null) return;
2146 sendProxyBroadcast(proxy);
2147 }
2148
2149 private void handleDeprecatedGlobalHttpProxy() {
2150 String proxy = Settings.Secure.getString(mContext.getContentResolver(),
2151 Settings.Secure.HTTP_PROXY);
2152 if (!TextUtils.isEmpty(proxy)) {
2153 String data[] = proxy.split(":");
2154 String proxyHost = data[0];
2155 int proxyPort = 8080;
2156 if (data.length > 1) {
2157 try {
2158 proxyPort = Integer.parseInt(data[1]);
2159 } catch (NumberFormatException e) {
2160 return;
2161 }
2162 }
2163 ProxyProperties p = new ProxyProperties(data[0], proxyPort, "");
2164 setGlobalProxy(p);
2165 }
2166 }
2167
2168 private void sendProxyBroadcast(ProxyProperties proxy) {
Robert Greenwalt55985be2010-12-23 15:51:10 -08002169 if (proxy == null) proxy = new ProxyProperties("", 0, "");
Wink Savilleed9c02b2010-12-03 12:01:38 -08002170 log("sending Proxy Broadcast for " + proxy);
Robert Greenwalt434203a2010-10-11 16:00:27 -07002171 Intent intent = new Intent(Proxy.PROXY_CHANGE_ACTION);
2172 intent.addFlags(Intent.FLAG_RECEIVER_REPLACE_PENDING);
2173 intent.putExtra(Proxy.EXTRA_PROXY_INFO, proxy);
Robert Greenwalta2e13392010-12-06 11:29:17 -08002174 mContext.sendStickyBroadcast(intent);
Robert Greenwalt434203a2010-10-11 16:00:27 -07002175 }
2176
2177 private static class SettingsObserver extends ContentObserver {
2178 private int mWhat;
2179 private Handler mHandler;
2180 SettingsObserver(Handler handler, int what) {
2181 super(handler);
2182 mHandler = handler;
2183 mWhat = what;
2184 }
2185
2186 void observe(Context context) {
2187 ContentResolver resolver = context.getContentResolver();
2188 resolver.registerContentObserver(Settings.Secure.getUriFor(
2189 Settings.Secure.HTTP_PROXY), false, this);
2190 }
2191
2192 @Override
2193 public void onChange(boolean selfChange) {
2194 mHandler.obtainMessage(mWhat).sendToTarget();
2195 }
2196 }
Wink Savilleed9c02b2010-12-03 12:01:38 -08002197
2198 private void log(String s) {
2199 Slog.d(TAG, s);
2200 }
2201
2202 private void loge(String s) {
2203 Slog.e(TAG, s);
2204 }
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002205}