blob: 5eb42e7d916584f8961d9ae674250fb3204df32e [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;
700 } else if (TextUtils.equals(feature, Phone.FEATURE_ENABLE_DUN)) {
701 usedNetworkType = ConnectivityManager.TYPE_MOBILE_DUN;
702 } else if (TextUtils.equals(feature, Phone.FEATURE_ENABLE_HIPRI)) {
703 usedNetworkType = ConnectivityManager.TYPE_MOBILE_HIPRI;
704 }
705 }
706 NetworkStateTracker network = mNetTrackers[usedNetworkType];
707 if (network != null) {
Robert Greenwalt0be1e982010-12-15 13:26:33 -0800708 Integer currentPid = new Integer(getCallingPid());
Robert Greenwalt42acef32009-08-12 16:08:25 -0700709 if (usedNetworkType != networkType) {
Robert Greenwalt42acef32009-08-12 16:08:25 -0700710 NetworkStateTracker radio = mNetTrackers[networkType];
711 NetworkInfo ni = network.getNetworkInfo();
712
713 if (ni.isAvailable() == false) {
Wink Savilleed9c02b2010-12-03 12:01:38 -0800714 if (DBG) log("special network not available");
Robert Greenwalt42acef32009-08-12 16:08:25 -0700715 return Phone.APN_TYPE_NOT_AVAILABLE;
716 }
717
Robert Greenwalt9c75d4a2009-09-27 17:27:04 -0700718 synchronized(this) {
719 mFeatureUsers.add(f);
720 if (!mNetRequestersPids[usedNetworkType].contains(currentPid)) {
721 // this gets used for per-pid dns when connected
722 mNetRequestersPids[usedNetworkType].add(currentPid);
723 }
Robert Greenwalt42acef32009-08-12 16:08:25 -0700724 }
Robert Greenwalt8dcc28b2010-09-23 10:05:56 -0700725 mHandler.sendMessageDelayed(mHandler.obtainMessage(EVENT_RESTORE_DEFAULT_NETWORK,
Robert Greenwalt9c75d4a2009-09-27 17:27:04 -0700726 f), getRestoreDefaultNetworkDelay());
727
Robert Greenwalt42acef32009-08-12 16:08:25 -0700728
Robert Greenwalta64bf832009-08-19 20:19:33 -0700729 if ((ni.isConnectedOrConnecting() == true) &&
730 !network.isTeardownRequested()) {
Robert Greenwalt42acef32009-08-12 16:08:25 -0700731 if (ni.isConnected() == true) {
732 // add the pid-specific dns
Robert Greenwaltc76b8fa2010-07-23 15:46:26 -0700733 handleDnsConfigurationChange(networkType);
Wink Savilleed9c02b2010-12-03 12:01:38 -0800734 if (DBG) log("special network already active");
Robert Greenwalt42acef32009-08-12 16:08:25 -0700735 return Phone.APN_ALREADY_ACTIVE;
736 }
Wink Savilleed9c02b2010-12-03 12:01:38 -0800737 if (DBG) log("special network already connecting");
Robert Greenwalt42acef32009-08-12 16:08:25 -0700738 return Phone.APN_REQUEST_STARTED;
739 }
740
741 // check if the radio in play can make another contact
742 // assume if cannot for now
743
Wink Savilleed9c02b2010-12-03 12:01:38 -0800744 if (DBG) log("reconnecting to special network");
Robert Greenwalt42acef32009-08-12 16:08:25 -0700745 network.reconnect();
746 return Phone.APN_REQUEST_STARTED;
747 } else {
Robert Greenwalt0be1e982010-12-15 13:26:33 -0800748 // need to remember this unsupported request so we respond appropriately on stop
749 synchronized(this) {
750 mFeatureUsers.add(f);
751 if (!mNetRequestersPids[usedNetworkType].contains(currentPid)) {
752 // this gets used for per-pid dns when connected
753 mNetRequestersPids[usedNetworkType].add(currentPid);
754 }
755 }
Robert Greenwalt02648a42010-05-18 10:52:51 -0700756 return -1;
Robert Greenwalt42acef32009-08-12 16:08:25 -0700757 }
758 }
759 return Phone.APN_TYPE_NOT_AVAILABLE;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800760 }
761
Robert Greenwalt9c75d4a2009-09-27 17:27:04 -0700762 // javadoc from interface
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800763 public int stopUsingNetworkFeature(int networkType, String feature) {
Robert Greenwaltb8f16342009-10-06 17:52:40 -0700764 enforceChangePermission();
765
Robert Greenwalt9c75d4a2009-09-27 17:27:04 -0700766 int pid = getCallingPid();
767 int uid = getCallingUid();
768
769 FeatureUser u = null;
770 boolean found = false;
771
772 synchronized(this) {
773 for (int i = 0; i < mFeatureUsers.size() ; i++) {
774 u = (FeatureUser)mFeatureUsers.get(i);
775 if (uid == u.mUid && pid == u.mPid &&
776 networkType == u.mNetworkType &&
777 TextUtils.equals(feature, u.mFeature)) {
778 found = true;
779 break;
780 }
781 }
782 }
783 if (found && u != null) {
784 // stop regardless of how many other time this proc had called start
785 return stopUsingNetworkFeature(u, true);
786 } else {
787 // none found!
Wink Savilleed9c02b2010-12-03 12:01:38 -0800788 if (DBG) log("ignoring stopUsingNetworkFeature - not a live request");
Robert Greenwalt9c75d4a2009-09-27 17:27:04 -0700789 return 1;
790 }
Robert Greenwalt42acef32009-08-12 16:08:25 -0700791 }
792
Robert Greenwalt9c75d4a2009-09-27 17:27:04 -0700793 private int stopUsingNetworkFeature(FeatureUser u, boolean ignoreDups) {
794 int networkType = u.mNetworkType;
795 String feature = u.mFeature;
796 int pid = u.mPid;
797 int uid = u.mUid;
798
799 NetworkStateTracker tracker = null;
800 boolean callTeardown = false; // used to carry our decision outside of sync block
801
Robert Greenwalt42acef32009-08-12 16:08:25 -0700802 if (DBG) {
Wink Savilleed9c02b2010-12-03 12:01:38 -0800803 log("stopUsingNetworkFeature for net " + networkType +
Robert Greenwalt42acef32009-08-12 16:08:25 -0700804 ": " + feature);
805 }
Robert Greenwaltb8f16342009-10-06 17:52:40 -0700806
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800807 if (!ConnectivityManager.isNetworkTypeValid(networkType)) {
808 return -1;
809 }
Robert Greenwalt42acef32009-08-12 16:08:25 -0700810
Robert Greenwalt9c75d4a2009-09-27 17:27:04 -0700811 // need to link the mFeatureUsers list with the mNetRequestersPids state in this
812 // sync block
813 synchronized(this) {
814 // check if this process still has an outstanding start request
815 if (!mFeatureUsers.contains(u)) {
Wink Savilleed9c02b2010-12-03 12:01:38 -0800816 if (DBG) log("ignoring - this process has no outstanding requests");
Robert Greenwalt42acef32009-08-12 16:08:25 -0700817 return 1;
818 }
Robert Greenwalt9c75d4a2009-09-27 17:27:04 -0700819 u.unlinkDeathRecipient();
820 mFeatureUsers.remove(mFeatureUsers.indexOf(u));
821 // If we care about duplicate requests, check for that here.
822 //
823 // This is done to support the extension of a request - the app
824 // can request we start the network feature again and renew the
825 // auto-shutoff delay. Normal "stop" calls from the app though
826 // do not pay attention to duplicate requests - in effect the
827 // API does not refcount and a single stop will counter multiple starts.
828 if (ignoreDups == false) {
829 for (int i = 0; i < mFeatureUsers.size() ; i++) {
830 FeatureUser x = (FeatureUser)mFeatureUsers.get(i);
831 if (x.mUid == u.mUid && x.mPid == u.mPid &&
832 x.mNetworkType == u.mNetworkType &&
833 TextUtils.equals(x.mFeature, u.mFeature)) {
Wink Savilleed9c02b2010-12-03 12:01:38 -0800834 if (DBG) log("ignoring stopUsingNetworkFeature as dup is found");
Robert Greenwalt9c75d4a2009-09-27 17:27:04 -0700835 return 1;
836 }
837 }
Robert Greenwalt42acef32009-08-12 16:08:25 -0700838 }
Robert Greenwalt9c75d4a2009-09-27 17:27:04 -0700839
840 // TODO - move to MobileDataStateTracker
841 int usedNetworkType = networkType;
842 if (networkType == ConnectivityManager.TYPE_MOBILE) {
843 if (TextUtils.equals(feature, Phone.FEATURE_ENABLE_MMS)) {
844 usedNetworkType = ConnectivityManager.TYPE_MOBILE_MMS;
845 } else if (TextUtils.equals(feature, Phone.FEATURE_ENABLE_SUPL)) {
846 usedNetworkType = ConnectivityManager.TYPE_MOBILE_SUPL;
847 } else if (TextUtils.equals(feature, Phone.FEATURE_ENABLE_DUN)) {
848 usedNetworkType = ConnectivityManager.TYPE_MOBILE_DUN;
849 } else if (TextUtils.equals(feature, Phone.FEATURE_ENABLE_HIPRI)) {
850 usedNetworkType = ConnectivityManager.TYPE_MOBILE_HIPRI;
851 }
852 }
853 tracker = mNetTrackers[usedNetworkType];
Robert Greenwalt5154ae762009-10-30 14:17:42 -0700854 if (tracker == null) {
Wink Savilleed9c02b2010-12-03 12:01:38 -0800855 if (DBG) log("ignoring - no known tracker for net type " + usedNetworkType);
Robert Greenwalt5154ae762009-10-30 14:17:42 -0700856 return -1;
857 }
858 if (usedNetworkType != networkType) {
Robert Greenwalt9c75d4a2009-09-27 17:27:04 -0700859 Integer currentPid = new Integer(pid);
Robert Greenwalt9c75d4a2009-09-27 17:27:04 -0700860 mNetRequestersPids[usedNetworkType].remove(currentPid);
Robert Greenwalt421c72b2009-12-17 14:54:59 -0800861 reassessPidDns(pid, true);
Robert Greenwalt9c75d4a2009-09-27 17:27:04 -0700862 if (mNetRequestersPids[usedNetworkType].size() != 0) {
Wink Savilleed9c02b2010-12-03 12:01:38 -0800863 if (DBG) log("not tearing down special network - " +
Robert Greenwalt9c75d4a2009-09-27 17:27:04 -0700864 "others still using it");
865 return 1;
866 }
867 callTeardown = true;
868 }
869 }
Wink Savilleed9c02b2010-12-03 12:01:38 -0800870 if (DBG) log("Doing network teardown");
Robert Greenwalt9c75d4a2009-09-27 17:27:04 -0700871 if (callTeardown) {
872 tracker.teardown();
Robert Greenwalt42acef32009-08-12 16:08:25 -0700873 return 1;
874 } else {
Robert Greenwalt02648a42010-05-18 10:52:51 -0700875 return -1;
Robert Greenwalt42acef32009-08-12 16:08:25 -0700876 }
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800877 }
878
879 /**
Robert Greenwalt585ac0f2010-08-27 09:24:29 -0700880 * @deprecated use requestRouteToHostAddress instead
881 *
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800882 * Ensure that a network route exists to deliver traffic to the specified
883 * host via the specified network interface.
Robert Greenwalt86e9e552009-07-16 17:21:39 -0700884 * @param networkType the type of the network over which traffic to the
885 * specified host is to be routed
886 * @param hostAddress the IP address of the host to which the route is
887 * desired
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800888 * @return {@code true} on success, {@code false} on failure
889 */
890 public boolean requestRouteToHost(int networkType, int hostAddress) {
Robert Greenwalt585ac0f2010-08-27 09:24:29 -0700891 InetAddress inetAddress = NetworkUtils.intToInetAddress(hostAddress);
892
893 if (inetAddress == null) {
894 return false;
895 }
896
897 return requestRouteToHostAddress(networkType, inetAddress.getAddress());
898 }
899
900 /**
901 * Ensure that a network route exists to deliver traffic to the specified
902 * host via the specified network interface.
903 * @param networkType the type of the network over which traffic to the
904 * specified host is to be routed
905 * @param hostAddress the IP address of the host to which the route is
906 * desired
907 * @return {@code true} on success, {@code false} on failure
908 */
909 public boolean requestRouteToHostAddress(int networkType, byte[] hostAddress) {
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800910 enforceChangePermission();
911 if (!ConnectivityManager.isNetworkTypeValid(networkType)) {
912 return false;
913 }
914 NetworkStateTracker tracker = mNetTrackers[networkType];
Robert Greenwalt8206ff32009-09-10 15:06:20 -0700915
Robert Greenwalt5154ae762009-10-30 14:17:42 -0700916 if (tracker == null || !tracker.getNetworkInfo().isConnected() ||
917 tracker.isTeardownRequested()) {
Robert Greenwalt8206ff32009-09-10 15:06:20 -0700918 if (DBG) {
Wink Savilleed9c02b2010-12-03 12:01:38 -0800919 log("requestRouteToHostAddress on down network " +
Robert Greenwalt585ac0f2010-08-27 09:24:29 -0700920 "(" + networkType + ") - dropped");
Robert Greenwalt8206ff32009-09-10 15:06:20 -0700921 }
922 return false;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800923 }
Robert Greenwalt47f69fe2010-06-15 15:43:39 -0700924 try {
Robert Greenwalt585ac0f2010-08-27 09:24:29 -0700925 InetAddress addr = InetAddress.getByAddress(hostAddress);
Robert Greenwalt47f69fe2010-06-15 15:43:39 -0700926 return addHostRoute(tracker, addr);
927 } catch (UnknownHostException e) {}
928 return false;
Irfan Sheriffd649c122010-06-09 15:39:36 -0700929 }
930
931 /**
932 * Ensure that a network route exists to deliver traffic to the specified
933 * host via the mobile data network.
934 * @param hostAddress the IP address of the host to which the route is desired,
935 * in network byte order.
Robert Greenwalt47f69fe2010-06-15 15:43:39 -0700936 * TODO - deprecate
Irfan Sheriffd649c122010-06-09 15:39:36 -0700937 * @return {@code true} on success, {@code false} on failure
938 */
Robert Greenwalt47f69fe2010-06-15 15:43:39 -0700939 private boolean addHostRoute(NetworkStateTracker nt, InetAddress hostAddress) {
Irfan Sheriffd649c122010-06-09 15:39:36 -0700940 if (nt.getNetworkInfo().getType() == ConnectivityManager.TYPE_WIFI) {
941 return false;
942 }
943
Robert Greenwalt37e65eb2010-08-30 10:56:47 -0700944 LinkProperties p = nt.getLinkProperties();
Robert Greenwalt47f69fe2010-06-15 15:43:39 -0700945 if (p == null) return false;
946 String interfaceName = p.getInterfaceName();
Irfan Sheriffd649c122010-06-09 15:39:36 -0700947
948 if (DBG) {
Wink Savilleed9c02b2010-12-03 12:01:38 -0800949 log("Requested host route to " + hostAddress + "(" + interfaceName + ")");
Irfan Sheriffd649c122010-06-09 15:39:36 -0700950 }
Robert Greenwalt47f69fe2010-06-15 15:43:39 -0700951 if (interfaceName != null) {
Robert Greenwalt585ac0f2010-08-27 09:24:29 -0700952 return NetworkUtils.addHostRoute(interfaceName, hostAddress, null);
Irfan Sheriffd649c122010-06-09 15:39:36 -0700953 } else {
Wink Savilleed9c02b2010-12-03 12:01:38 -0800954 if (DBG) loge("addHostRoute failed due to null interface name");
Irfan Sheriffd649c122010-06-09 15:39:36 -0700955 return false;
956 }
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800957 }
958
959 /**
960 * @see ConnectivityManager#getBackgroundDataSetting()
961 */
962 public boolean getBackgroundDataSetting() {
Robert Greenwaltd825ea42010-12-29 16:15:02 -0800963 return mBackgroundDataEnabled.get();
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800964 }
Robert Greenwalt86e9e552009-07-16 17:21:39 -0700965
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800966 /**
967 * @see ConnectivityManager#setBackgroundDataSetting(boolean)
968 */
969 public void setBackgroundDataSetting(boolean allowBackgroundDataUsage) {
970 mContext.enforceCallingOrSelfPermission(
971 android.Manifest.permission.CHANGE_BACKGROUND_DATA_SETTING,
972 "ConnectivityService");
Robert Greenwalt86e9e552009-07-16 17:21:39 -0700973
Robert Greenwaltd825ea42010-12-29 16:15:02 -0800974 mBackgroundDataEnabled.set(allowBackgroundDataUsage);
975
Robert Greenwalt8dcc28b2010-09-23 10:05:56 -0700976 mHandler.sendMessage(mHandler.obtainMessage(EVENT_SET_BACKGROUND_DATA,
977 (allowBackgroundDataUsage ? ENABLED : DISABLED), 0));
978 }
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800979
Robert Greenwalt8dcc28b2010-09-23 10:05:56 -0700980 private void handleSetBackgroundData(boolean enabled) {
981 if (enabled != getBackgroundDataSetting()) {
982 Settings.Secure.putInt(mContext.getContentResolver(),
983 Settings.Secure.BACKGROUND_DATA, enabled ? 1 : 0);
984 Intent broadcast = new Intent(
985 ConnectivityManager.ACTION_BACKGROUND_DATA_SETTING_CHANGED);
986 mContext.sendBroadcast(broadcast);
987 }
Robert Greenwalt86e9e552009-07-16 17:21:39 -0700988 }
989
Robert Greenwaltc03fa502010-02-23 18:58:05 -0800990 /**
991 * @see ConnectivityManager#getMobileDataEnabled()
992 */
993 public boolean getMobileDataEnabled() {
Wink Savillee7982682010-12-07 10:31:02 -0800994 // TODO: This detail should probably be in DataConnectionTracker's
995 // which is where we store the value and maybe make this
996 // asynchronous.
Robert Greenwaltc03fa502010-02-23 18:58:05 -0800997 enforceAccessPermission();
998 boolean retVal = Settings.Secure.getInt(mContext.getContentResolver(),
999 Settings.Secure.MOBILE_DATA, 1) == 1;
Wink Savilleed9c02b2010-12-03 12:01:38 -08001000 if (DBG) log("getMobileDataEnabled returning " + retVal);
Robert Greenwaltc03fa502010-02-23 18:58:05 -08001001 return retVal;
1002 }
1003
1004 /**
1005 * @see ConnectivityManager#setMobileDataEnabled(boolean)
1006 */
Robert Greenwalt8dcc28b2010-09-23 10:05:56 -07001007 public void setMobileDataEnabled(boolean enabled) {
Robert Greenwaltc03fa502010-02-23 18:58:05 -08001008 enforceChangePermission();
Wink Savilleed9c02b2010-12-03 12:01:38 -08001009 if (DBG) log("setMobileDataEnabled(" + enabled + ")");
Robert Greenwaltc03fa502010-02-23 18:58:05 -08001010
Robert Greenwalt8dcc28b2010-09-23 10:05:56 -07001011 mHandler.sendMessage(mHandler.obtainMessage(EVENT_SET_MOBILE_DATA,
1012 (enabled ? ENABLED : DISABLED), 0));
1013 }
1014
1015 private void handleSetMobileData(boolean enabled) {
Wink Savillee7982682010-12-07 10:31:02 -08001016 if (mNetTrackers[ConnectivityManager.TYPE_MOBILE] != null) {
1017 if (DBG) {
1018 Slog.d(TAG, mNetTrackers[ConnectivityManager.TYPE_MOBILE].toString() + enabled);
Robert Greenwaltc03fa502010-02-23 18:58:05 -08001019 }
Wink Savillee7982682010-12-07 10:31:02 -08001020 mNetTrackers[ConnectivityManager.TYPE_MOBILE].setDataEnable(enabled);
Robert Greenwaltc03fa502010-02-23 18:58:05 -08001021 }
1022 }
1023
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001024 private void enforceAccessPermission() {
Robert Greenwalt86e9e552009-07-16 17:21:39 -07001025 mContext.enforceCallingOrSelfPermission(
1026 android.Manifest.permission.ACCESS_NETWORK_STATE,
1027 "ConnectivityService");
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001028 }
1029
1030 private void enforceChangePermission() {
Robert Greenwalt86e9e552009-07-16 17:21:39 -07001031 mContext.enforceCallingOrSelfPermission(
1032 android.Manifest.permission.CHANGE_NETWORK_STATE,
1033 "ConnectivityService");
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001034 }
1035
Robert Greenwaltd0e18ff2010-01-26 11:40:34 -08001036 // TODO Make this a special check when it goes public
1037 private void enforceTetherChangePermission() {
1038 mContext.enforceCallingOrSelfPermission(
1039 android.Manifest.permission.CHANGE_NETWORK_STATE,
1040 "ConnectivityService");
1041 }
1042
Robert Greenwalt2a091d72010-02-11 18:18:40 -08001043 private void enforceTetherAccessPermission() {
1044 mContext.enforceCallingOrSelfPermission(
1045 android.Manifest.permission.ACCESS_NETWORK_STATE,
1046 "ConnectivityService");
1047 }
1048
Robert Greenwalt14f2ef42010-06-15 12:19:37 -07001049 private void enforceConnectivityInternalPermission() {
1050 mContext.enforceCallingOrSelfPermission(
1051 android.Manifest.permission.CONNECTIVITY_INTERNAL,
1052 "ConnectivityService");
1053 }
1054
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001055 /**
Robert Greenwalt86e9e552009-07-16 17:21:39 -07001056 * Handle a {@code DISCONNECTED} event. If this pertains to the non-active
1057 * network, we ignore it. If it is for the active network, we send out a
1058 * broadcast. But first, we check whether it might be possible to connect
1059 * to a different network.
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001060 * @param info the {@code NetworkInfo} for the network
1061 */
1062 private void handleDisconnect(NetworkInfo info) {
1063
Robert Greenwalt42acef32009-08-12 16:08:25 -07001064 int prevNetType = info.getType();
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001065
Robert Greenwalt42acef32009-08-12 16:08:25 -07001066 mNetTrackers[prevNetType].setTeardownRequested(false);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001067 /*
1068 * If the disconnected network is not the active one, then don't report
1069 * this as a loss of connectivity. What probably happened is that we're
1070 * getting the disconnect for a network that we explicitly disabled
1071 * in accordance with network preference policies.
1072 */
Robert Greenwalt42acef32009-08-12 16:08:25 -07001073 if (!mNetAttributes[prevNetType].isDefault()) {
1074 List pids = mNetRequestersPids[prevNetType];
1075 for (int i = 0; i<pids.size(); i++) {
1076 Integer pid = (Integer)pids.get(i);
1077 // will remove them because the net's no longer connected
1078 // need to do this now as only now do we know the pids and
1079 // can properly null things that are no longer referenced.
1080 reassessPidDns(pid.intValue(), false);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001081 }
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001082 }
1083
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001084 Intent intent = new Intent(ConnectivityManager.CONNECTIVITY_ACTION);
1085 intent.putExtra(ConnectivityManager.EXTRA_NETWORK_INFO, info);
1086 if (info.isFailover()) {
1087 intent.putExtra(ConnectivityManager.EXTRA_IS_FAILOVER, true);
1088 info.setFailover(false);
1089 }
1090 if (info.getReason() != null) {
1091 intent.putExtra(ConnectivityManager.EXTRA_REASON, info.getReason());
1092 }
1093 if (info.getExtraInfo() != null) {
Robert Greenwalt86e9e552009-07-16 17:21:39 -07001094 intent.putExtra(ConnectivityManager.EXTRA_EXTRA_INFO,
1095 info.getExtraInfo());
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001096 }
Robert Greenwalt42acef32009-08-12 16:08:25 -07001097
Robert Greenwaltcc4b4012010-01-25 17:54:29 -08001098 NetworkStateTracker newNet = null;
1099 if (mNetAttributes[prevNetType].isDefault()) {
1100 newNet = tryFailover(prevNetType);
1101 if (newNet != null) {
1102 NetworkInfo switchTo = newNet.getNetworkInfo();
Robert Greenwalt029be812010-09-20 18:01:43 -07001103 if (!switchTo.isConnected()) {
Wink Savilleed9c02b2010-12-03 12:01:38 -08001104 // if the other net is connected they've already reset this and perhaps even
1105 // gotten a positive report we don't want to overwrite, but if not we need to
1106 // clear this now to turn our cellular sig strength white
Robert Greenwalt029be812010-09-20 18:01:43 -07001107 mDefaultInetConditionPublished = 0;
Robert Greenwaltd68e3212010-12-21 11:43:28 -08001108 intent.putExtra(ConnectivityManager.EXTRA_NO_CONNECTIVITY, true);
Robert Greenwalt029be812010-09-20 18:01:43 -07001109 }
Robert Greenwaltcc4b4012010-01-25 17:54:29 -08001110 intent.putExtra(ConnectivityManager.EXTRA_OTHER_NETWORK_INFO, switchTo);
1111 } else {
Robert Greenwalt029be812010-09-20 18:01:43 -07001112 mDefaultInetConditionPublished = 0; // we're not connected anymore
Robert Greenwaltcc4b4012010-01-25 17:54:29 -08001113 intent.putExtra(ConnectivityManager.EXTRA_NO_CONNECTIVITY, true);
1114 }
Robert Greenwaltda03c4e2010-01-20 19:29:41 -08001115 }
Robert Greenwalt029be812010-09-20 18:01:43 -07001116 intent.putExtra(ConnectivityManager.EXTRA_INET_CONDITION, mDefaultInetConditionPublished);
Robert Greenwaltda03c4e2010-01-20 19:29:41 -08001117 // do this before we broadcast the change
Robert Greenwaltc76b8fa2010-07-23 15:46:26 -07001118 handleConnectivityChange(prevNetType);
Robert Greenwaltda03c4e2010-01-20 19:29:41 -08001119
1120 sendStickyBroadcast(intent);
1121 /*
1122 * If the failover network is already connected, then immediately send
1123 * out a followup broadcast indicating successful failover
1124 */
1125 if (newNet != null && newNet.getNetworkInfo().isConnected()) {
1126 sendConnectedBroadcast(newNet.getNetworkInfo());
1127 }
1128 }
1129
Robert Greenwaltcc4b4012010-01-25 17:54:29 -08001130 // returns null if no failover available
Robert Greenwaltda03c4e2010-01-20 19:29:41 -08001131 private NetworkStateTracker tryFailover(int prevNetType) {
Robert Greenwalt42acef32009-08-12 16:08:25 -07001132 /*
1133 * If this is a default network, check if other defaults are available
1134 * or active
1135 */
1136 NetworkStateTracker newNet = null;
1137 if (mNetAttributes[prevNetType].isDefault()) {
Robert Greenwalt42acef32009-08-12 16:08:25 -07001138 if (mActiveDefaultNetwork == prevNetType) {
1139 mActiveDefaultNetwork = -1;
1140 }
1141
1142 int newType = -1;
1143 int newPriority = -1;
Robert Greenwaltda03c4e2010-01-20 19:29:41 -08001144 for (int checkType=0; checkType <= ConnectivityManager.MAX_NETWORK_TYPE; checkType++) {
Robert Greenwalt5154ae762009-10-30 14:17:42 -07001145 if (checkType == prevNetType) continue;
1146 if (mNetAttributes[checkType] == null) continue;
Robert Greenwalt42acef32009-08-12 16:08:25 -07001147 if (mNetAttributes[checkType].isDefault()) {
1148 /* TODO - if we have multiple nets we could use
1149 * we may want to put more thought into which we choose
1150 */
1151 if (checkType == mNetworkPreference) {
1152 newType = checkType;
1153 break;
1154 }
Robert Greenwalt5154ae762009-10-30 14:17:42 -07001155 if (mNetAttributes[checkType].mPriority > newPriority) {
Robert Greenwalt42acef32009-08-12 16:08:25 -07001156 newType = checkType;
Robert Greenwalt5154ae762009-10-30 14:17:42 -07001157 newPriority = mNetAttributes[newType].mPriority;
Robert Greenwalt42acef32009-08-12 16:08:25 -07001158 }
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001159 }
1160 }
Robert Greenwalt42acef32009-08-12 16:08:25 -07001161
1162 if (newType != -1) {
1163 newNet = mNetTrackers[newType];
1164 /**
1165 * See if the other network is available to fail over to.
1166 * If is not available, we enable it anyway, so that it
1167 * will be able to connect when it does become available,
1168 * but we report a total loss of connectivity rather than
1169 * report that we are attempting to fail over.
1170 */
1171 if (newNet.isAvailable()) {
1172 NetworkInfo switchTo = newNet.getNetworkInfo();
1173 switchTo.setFailover(true);
Robert Greenwalta64bf832009-08-19 20:19:33 -07001174 if (!switchTo.isConnectedOrConnecting() ||
1175 newNet.isTeardownRequested()) {
Robert Greenwalt42acef32009-08-12 16:08:25 -07001176 newNet.reconnect();
1177 }
1178 if (DBG) {
1179 if (switchTo.isConnected()) {
Wink Savilleed9c02b2010-12-03 12:01:38 -08001180 log("Switching to already connected " + switchTo.getTypeName());
Robert Greenwalt42acef32009-08-12 16:08:25 -07001181 } else {
Wink Savilleed9c02b2010-12-03 12:01:38 -08001182 log("Attempting to switch to " + switchTo.getTypeName());
Robert Greenwalt42acef32009-08-12 16:08:25 -07001183 }
1184 }
Robert Greenwalt42acef32009-08-12 16:08:25 -07001185 } else {
1186 newNet.reconnect();
Robert Greenwaltf0fa39e2010-03-09 14:55:08 -08001187 newNet = null; // not officially avail.. try anyway, but
1188 // report no failover
Robert Greenwalt42acef32009-08-12 16:08:25 -07001189 }
Robert Greenwalt572172b2010-10-08 16:35:52 -07001190 } else {
Wink Savilleed9c02b2010-12-03 12:01:38 -08001191 loge("Network failover failing.");
Robert Greenwalt42acef32009-08-12 16:08:25 -07001192 }
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001193 }
Robert Greenwalt42acef32009-08-12 16:08:25 -07001194
Robert Greenwaltda03c4e2010-01-20 19:29:41 -08001195 return newNet;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001196 }
1197
1198 private void sendConnectedBroadcast(NetworkInfo info) {
Robert Greenwalt1e9aac22010-09-15 17:36:33 -07001199 sendGeneralBroadcast(info, ConnectivityManager.CONNECTIVITY_ACTION);
1200 }
1201
1202 private void sendInetConditionBroadcast(NetworkInfo info) {
1203 sendGeneralBroadcast(info, ConnectivityManager.INET_CONDITION_ACTION);
1204 }
1205
1206 private void sendGeneralBroadcast(NetworkInfo info, String bcastType) {
1207 Intent intent = new Intent(bcastType);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001208 intent.putExtra(ConnectivityManager.EXTRA_NETWORK_INFO, info);
1209 if (info.isFailover()) {
1210 intent.putExtra(ConnectivityManager.EXTRA_IS_FAILOVER, true);
1211 info.setFailover(false);
1212 }
1213 if (info.getReason() != null) {
1214 intent.putExtra(ConnectivityManager.EXTRA_REASON, info.getReason());
1215 }
1216 if (info.getExtraInfo() != null) {
Robert Greenwalt86e9e552009-07-16 17:21:39 -07001217 intent.putExtra(ConnectivityManager.EXTRA_EXTRA_INFO,
1218 info.getExtraInfo());
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001219 }
Robert Greenwaltd7085fc2010-09-08 15:24:47 -07001220 intent.putExtra(ConnectivityManager.EXTRA_INET_CONDITION, mDefaultInetConditionPublished);
Mike Lockwood0f79b542009-08-14 14:18:49 -04001221 sendStickyBroadcast(intent);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001222 }
1223
1224 /**
1225 * Called when an attempt to fail over to another network has failed.
1226 * @param info the {@link NetworkInfo} for the failed network
1227 */
1228 private void handleConnectionFailure(NetworkInfo info) {
1229 mNetTrackers[info.getType()].setTeardownRequested(false);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001230
Robert Greenwalt42acef32009-08-12 16:08:25 -07001231 String reason = info.getReason();
1232 String extraInfo = info.getExtraInfo();
Robert Greenwalt86e9e552009-07-16 17:21:39 -07001233
Robert Greenwalt572172b2010-10-08 16:35:52 -07001234 String reasonText;
1235 if (reason == null) {
1236 reasonText = ".";
1237 } else {
1238 reasonText = " (" + reason + ").";
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001239 }
Wink Savilleed9c02b2010-12-03 12:01:38 -08001240 loge("Attempt to connect to " + info.getTypeName() + " failed" + reasonText);
Robert Greenwalt42acef32009-08-12 16:08:25 -07001241
1242 Intent intent = new Intent(ConnectivityManager.CONNECTIVITY_ACTION);
1243 intent.putExtra(ConnectivityManager.EXTRA_NETWORK_INFO, info);
1244 if (getActiveNetworkInfo() == null) {
1245 intent.putExtra(ConnectivityManager.EXTRA_NO_CONNECTIVITY, true);
1246 }
1247 if (reason != null) {
1248 intent.putExtra(ConnectivityManager.EXTRA_REASON, reason);
1249 }
1250 if (extraInfo != null) {
1251 intent.putExtra(ConnectivityManager.EXTRA_EXTRA_INFO, extraInfo);
1252 }
1253 if (info.isFailover()) {
1254 intent.putExtra(ConnectivityManager.EXTRA_IS_FAILOVER, true);
1255 info.setFailover(false);
1256 }
Robert Greenwaltda03c4e2010-01-20 19:29:41 -08001257
Robert Greenwaltcc4b4012010-01-25 17:54:29 -08001258 NetworkStateTracker newNet = null;
1259 if (mNetAttributes[info.getType()].isDefault()) {
1260 newNet = tryFailover(info.getType());
1261 if (newNet != null) {
1262 NetworkInfo switchTo = newNet.getNetworkInfo();
Robert Greenwalt029be812010-09-20 18:01:43 -07001263 if (!switchTo.isConnected()) {
Robert Greenwalt572172b2010-10-08 16:35:52 -07001264 // if the other net is connected they've already reset this and perhaps
1265 // even gotten a positive report we don't want to overwrite, but if not
1266 // we need to clear this now to turn our cellular sig strength white
Robert Greenwalt029be812010-09-20 18:01:43 -07001267 mDefaultInetConditionPublished = 0;
1268 }
Robert Greenwaltcc4b4012010-01-25 17:54:29 -08001269 intent.putExtra(ConnectivityManager.EXTRA_OTHER_NETWORK_INFO, switchTo);
1270 } else {
Robert Greenwalt029be812010-09-20 18:01:43 -07001271 mDefaultInetConditionPublished = 0;
Robert Greenwaltcc4b4012010-01-25 17:54:29 -08001272 intent.putExtra(ConnectivityManager.EXTRA_NO_CONNECTIVITY, true);
1273 }
Robert Greenwaltda03c4e2010-01-20 19:29:41 -08001274 }
Robert Greenwaltcc4b4012010-01-25 17:54:29 -08001275
Robert Greenwalt029be812010-09-20 18:01:43 -07001276 intent.putExtra(ConnectivityManager.EXTRA_INET_CONDITION, mDefaultInetConditionPublished);
Mike Lockwood0f79b542009-08-14 14:18:49 -04001277 sendStickyBroadcast(intent);
Robert Greenwaltda03c4e2010-01-20 19:29:41 -08001278 /*
1279 * If the failover network is already connected, then immediately send
1280 * out a followup broadcast indicating successful failover
1281 */
1282 if (newNet != null && newNet.getNetworkInfo().isConnected()) {
1283 sendConnectedBroadcast(newNet.getNetworkInfo());
1284 }
Mike Lockwood0f79b542009-08-14 14:18:49 -04001285 }
1286
1287 private void sendStickyBroadcast(Intent intent) {
1288 synchronized(this) {
Dianne Hackborn1c633fc2009-12-08 19:45:14 -08001289 if (!mSystemReady) {
1290 mInitialBroadcast = new Intent(intent);
Mike Lockwood0f79b542009-08-14 14:18:49 -04001291 }
Dianne Hackborn1c633fc2009-12-08 19:45:14 -08001292 intent.addFlags(Intent.FLAG_RECEIVER_REGISTERED_ONLY_BEFORE_BOOT);
1293 mContext.sendStickyBroadcast(intent);
Mike Lockwood0f79b542009-08-14 14:18:49 -04001294 }
1295 }
1296
1297 void systemReady() {
1298 synchronized(this) {
1299 mSystemReady = true;
Dianne Hackborn1c633fc2009-12-08 19:45:14 -08001300 if (mInitialBroadcast != null) {
1301 mContext.sendStickyBroadcast(mInitialBroadcast);
1302 mInitialBroadcast = null;
Mike Lockwood0f79b542009-08-14 14:18:49 -04001303 }
1304 }
Robert Greenwalt434203a2010-10-11 16:00:27 -07001305 // load the global proxy at startup
1306 mHandler.sendMessage(mHandler.obtainMessage(EVENT_APPLY_GLOBAL_HTTP_PROXY));
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001307 }
1308
1309 private void handleConnect(NetworkInfo info) {
Robert Greenwalt42acef32009-08-12 16:08:25 -07001310 int type = info.getType();
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001311
1312 // snapshot isFailover, because sendConnectedBroadcast() resets it
1313 boolean isFailover = info.isFailover();
Robert Greenwalt42acef32009-08-12 16:08:25 -07001314 NetworkStateTracker thisNet = mNetTrackers[type];
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001315
Robert Greenwalt42acef32009-08-12 16:08:25 -07001316 // if this is a default net and other default is running
1317 // kill the one not preferred
1318 if (mNetAttributes[type].isDefault()) {
Robert Greenwalt42acef32009-08-12 16:08:25 -07001319 if (mActiveDefaultNetwork != -1 && mActiveDefaultNetwork != type) {
1320 if ((type != mNetworkPreference &&
1321 mNetAttributes[mActiveDefaultNetwork].mPriority >
1322 mNetAttributes[type].mPriority) ||
1323 mNetworkPreference == mActiveDefaultNetwork) {
1324 // don't accept this one
Wink Savilleed9c02b2010-12-03 12:01:38 -08001325 if (DBG) {
1326 log("Not broadcasting CONNECT_ACTION " +
Robert Greenwalt42acef32009-08-12 16:08:25 -07001327 "to torn down network " + info.getTypeName());
Wink Savilleed9c02b2010-12-03 12:01:38 -08001328 }
Robert Greenwalt42acef32009-08-12 16:08:25 -07001329 teardown(thisNet);
1330 return;
1331 } else {
1332 // tear down the other
1333 NetworkStateTracker otherNet =
1334 mNetTrackers[mActiveDefaultNetwork];
Wink Savilleed9c02b2010-12-03 12:01:38 -08001335 if (DBG) {
1336 log("Policy requires " + otherNet.getNetworkInfo().getTypeName() +
Robert Greenwalt42acef32009-08-12 16:08:25 -07001337 " teardown");
Wink Savilleed9c02b2010-12-03 12:01:38 -08001338 }
Robert Greenwalt42acef32009-08-12 16:08:25 -07001339 if (!teardown(otherNet)) {
Wink Savilleed9c02b2010-12-03 12:01:38 -08001340 loge("Network declined teardown request");
Robert Greenwalt42acef32009-08-12 16:08:25 -07001341 return;
1342 }
Robert Greenwalt14f2ef42010-06-15 12:19:37 -07001343 }
1344 }
1345 synchronized (ConnectivityService.this) {
1346 // have a new default network, release the transition wakelock in a second
1347 // if it's held. The second pause is to allow apps to reconnect over the
1348 // new network
1349 if (mNetTransitionWakeLock.isHeld()) {
1350 mHandler.sendMessageDelayed(mHandler.obtainMessage(
Robert Greenwaltf3331232010-09-24 14:32:21 -07001351 EVENT_CLEAR_NET_TRANSITION_WAKELOCK,
Robert Greenwalt14f2ef42010-06-15 12:19:37 -07001352 mNetTransitionWakeLockSerialNumber, 0),
1353 1000);
Robert Greenwalt42acef32009-08-12 16:08:25 -07001354 }
1355 }
1356 mActiveDefaultNetwork = type;
Robert Greenwaltd7085fc2010-09-08 15:24:47 -07001357 // this will cause us to come up initially as unconnected and switching
1358 // to connected after our normal pause unless somebody reports us as reall
1359 // disconnected
1360 mDefaultInetConditionPublished = 0;
1361 mDefaultConnectionSequence++;
1362 mInetConditionChangeInFlight = false;
1363 // Don't do this - if we never sign in stay, grey
1364 //reportNetworkCondition(mActiveDefaultNetwork, 100);
Robert Greenwalt42acef32009-08-12 16:08:25 -07001365 }
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001366 thisNet.setTeardownRequested(false);
Irfan Sheriffd649c122010-06-09 15:39:36 -07001367 updateNetworkSettings(thisNet);
Robert Greenwaltc76b8fa2010-07-23 15:46:26 -07001368 handleConnectivityChange(type);
Robert Greenwalt42acef32009-08-12 16:08:25 -07001369 sendConnectedBroadcast(info);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001370 }
1371
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001372 /**
Robert Greenwaltc76b8fa2010-07-23 15:46:26 -07001373 * After a change in the connectivity state of a network. We're mainly
1374 * concerned with making sure that the list of DNS servers is set up
1375 * according to which networks are connected, and ensuring that the
1376 * right routing table entries exist.
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001377 */
Robert Greenwaltc76b8fa2010-07-23 15:46:26 -07001378 private void handleConnectivityChange(int netType) {
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001379 /*
Robert Greenwalt42acef32009-08-12 16:08:25 -07001380 * If a non-default network is enabled, add the host routes that
Robert Greenwaltc76b8fa2010-07-23 15:46:26 -07001381 * will allow it's DNS servers to be accessed.
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001382 */
Robert Greenwaltc76b8fa2010-07-23 15:46:26 -07001383 handleDnsConfigurationChange(netType);
Robert Greenwalt42acef32009-08-12 16:08:25 -07001384
Robert Greenwaltc76b8fa2010-07-23 15:46:26 -07001385 if (mNetTrackers[netType].getNetworkInfo().isConnected()) {
1386 if (mNetAttributes[netType].isDefault()) {
Robert Greenwalt434203a2010-10-11 16:00:27 -07001387 handleApplyDefaultProxy(netType);
Robert Greenwaltc76b8fa2010-07-23 15:46:26 -07001388 addDefaultRoute(mNetTrackers[netType]);
Robert Greenwalt42acef32009-08-12 16:08:25 -07001389 } else {
Robert Greenwaltc76b8fa2010-07-23 15:46:26 -07001390 addPrivateDnsRoutes(mNetTrackers[netType]);
1391 }
1392 } else {
1393 if (mNetAttributes[netType].isDefault()) {
1394 removeDefaultRoute(mNetTrackers[netType]);
1395 } else {
1396 removePrivateDnsRoutes(mNetTrackers[netType]);
Robert Greenwalt42acef32009-08-12 16:08:25 -07001397 }
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001398 }
1399 }
1400
Irfan Sheriffd649c122010-06-09 15:39:36 -07001401 private void addPrivateDnsRoutes(NetworkStateTracker nt) {
Irfan Sheriffd649c122010-06-09 15:39:36 -07001402 boolean privateDnsRouteSet = nt.isPrivateDnsRouteSet();
Robert Greenwalt37e65eb2010-08-30 10:56:47 -07001403 LinkProperties p = nt.getLinkProperties();
Robert Greenwalt47f69fe2010-06-15 15:43:39 -07001404 if (p == null) return;
1405 String interfaceName = p.getInterfaceName();
Irfan Sheriffd649c122010-06-09 15:39:36 -07001406
1407 if (DBG) {
Wink Savilleed9c02b2010-12-03 12:01:38 -08001408 log("addPrivateDnsRoutes for " + nt +
Irfan Sheriffd649c122010-06-09 15:39:36 -07001409 "(" + interfaceName + ") - mPrivateDnsRouteSet = " + privateDnsRouteSet);
1410 }
Irfan Sheriffd649c122010-06-09 15:39:36 -07001411 if (interfaceName != null && !privateDnsRouteSet) {
Robert Greenwalt47f69fe2010-06-15 15:43:39 -07001412 Collection<InetAddress> dnsList = p.getDnses();
1413 for (InetAddress dns : dnsList) {
Wink Savilleed9c02b2010-12-03 12:01:38 -08001414 if (DBG) log(" adding " + dns);
Robert Greenwalt585ac0f2010-08-27 09:24:29 -07001415 NetworkUtils.addHostRoute(interfaceName, dns, null);
Irfan Sheriffd649c122010-06-09 15:39:36 -07001416 }
1417 nt.privateDnsRouteSet(true);
1418 }
1419 }
1420
1421 private void removePrivateDnsRoutes(NetworkStateTracker nt) {
1422 // TODO - we should do this explicitly but the NetUtils api doesnt
1423 // support this yet - must remove all. No worse than before
Robert Greenwalt37e65eb2010-08-30 10:56:47 -07001424 LinkProperties p = nt.getLinkProperties();
Robert Greenwalt47f69fe2010-06-15 15:43:39 -07001425 if (p == null) return;
1426 String interfaceName = p.getInterfaceName();
Irfan Sheriffd649c122010-06-09 15:39:36 -07001427 boolean privateDnsRouteSet = nt.isPrivateDnsRouteSet();
1428 if (interfaceName != null && privateDnsRouteSet) {
1429 if (DBG) {
Wink Savilleed9c02b2010-12-03 12:01:38 -08001430 log("removePrivateDnsRoutes for " + nt.getNetworkInfo().getTypeName() +
Irfan Sheriffd649c122010-06-09 15:39:36 -07001431 " (" + interfaceName + ")");
1432 }
1433 NetworkUtils.removeHostRoutes(interfaceName);
1434 nt.privateDnsRouteSet(false);
1435 }
1436 }
1437
Irfan Sheriffd649c122010-06-09 15:39:36 -07001438
1439 private void addDefaultRoute(NetworkStateTracker nt) {
Robert Greenwalt37e65eb2010-08-30 10:56:47 -07001440 LinkProperties p = nt.getLinkProperties();
Robert Greenwalt47f69fe2010-06-15 15:43:39 -07001441 if (p == null) return;
1442 String interfaceName = p.getInterfaceName();
1443 InetAddress defaultGatewayAddr = p.getGateway();
Irfan Sheriffd649c122010-06-09 15:39:36 -07001444
Robert Greenwaltc76b8fa2010-07-23 15:46:26 -07001445 if ((interfaceName != null) && (defaultGatewayAddr != null )) {
Robert Greenwalt585ac0f2010-08-27 09:24:29 -07001446 if (!NetworkUtils.addDefaultRoute(interfaceName, defaultGatewayAddr) && DBG) {
Robert Greenwalt47f69fe2010-06-15 15:43:39 -07001447 NetworkInfo networkInfo = nt.getNetworkInfo();
Wink Savilleed9c02b2010-12-03 12:01:38 -08001448 log("addDefaultRoute for " + networkInfo.getTypeName() +
Irfan Sheriffd649c122010-06-09 15:39:36 -07001449 " (" + interfaceName + "), GatewayAddr=" + defaultGatewayAddr);
1450 }
Irfan Sheriffd649c122010-06-09 15:39:36 -07001451 }
1452 }
1453
1454
1455 public void removeDefaultRoute(NetworkStateTracker nt) {
Robert Greenwalt37e65eb2010-08-30 10:56:47 -07001456 LinkProperties p = nt.getLinkProperties();
Robert Greenwalt47f69fe2010-06-15 15:43:39 -07001457 if (p == null) return;
1458 String interfaceName = p.getInterfaceName();
Irfan Sheriffd649c122010-06-09 15:39:36 -07001459
Robert Greenwaltc76b8fa2010-07-23 15:46:26 -07001460 if (interfaceName != null) {
1461 if ((NetworkUtils.removeDefaultRoute(interfaceName) >= 0) && DBG) {
Robert Greenwalt47f69fe2010-06-15 15:43:39 -07001462 NetworkInfo networkInfo = nt.getNetworkInfo();
Wink Savilleed9c02b2010-12-03 12:01:38 -08001463 log("removeDefaultRoute for " + networkInfo.getTypeName() + " (" +
Irfan Sheriffd649c122010-06-09 15:39:36 -07001464 interfaceName + ")");
1465 }
Irfan Sheriffd649c122010-06-09 15:39:36 -07001466 }
1467 }
1468
1469 /**
1470 * Reads the network specific TCP buffer sizes from SystemProperties
1471 * net.tcp.buffersize.[default|wifi|umts|edge|gprs] and set them for system
1472 * wide use
1473 */
1474 public void updateNetworkSettings(NetworkStateTracker nt) {
1475 String key = nt.getTcpBufferSizesPropName();
1476 String bufferSizes = SystemProperties.get(key);
1477
1478 if (bufferSizes.length() == 0) {
Wink Savilleed9c02b2010-12-03 12:01:38 -08001479 loge(key + " not found in system properties. Using defaults");
Irfan Sheriffd649c122010-06-09 15:39:36 -07001480
1481 // Setting to default values so we won't be stuck to previous values
1482 key = "net.tcp.buffersize.default";
1483 bufferSizes = SystemProperties.get(key);
1484 }
1485
1486 // Set values in kernel
1487 if (bufferSizes.length() != 0) {
1488 if (DBG) {
Wink Savilleed9c02b2010-12-03 12:01:38 -08001489 log("Setting TCP values: [" + bufferSizes
Irfan Sheriffd649c122010-06-09 15:39:36 -07001490 + "] which comes from [" + key + "]");
1491 }
1492 setBufferSize(bufferSizes);
1493 }
1494 }
1495
1496 /**
1497 * Writes TCP buffer sizes to /sys/kernel/ipv4/tcp_[r/w]mem_[min/def/max]
1498 * which maps to /proc/sys/net/ipv4/tcp_rmem and tcpwmem
1499 *
1500 * @param bufferSizes in the format of "readMin, readInitial, readMax,
1501 * writeMin, writeInitial, writeMax"
1502 */
1503 private void setBufferSize(String bufferSizes) {
1504 try {
1505 String[] values = bufferSizes.split(",");
1506
1507 if (values.length == 6) {
1508 final String prefix = "/sys/kernel/ipv4/tcp_";
1509 stringToFile(prefix + "rmem_min", values[0]);
1510 stringToFile(prefix + "rmem_def", values[1]);
1511 stringToFile(prefix + "rmem_max", values[2]);
1512 stringToFile(prefix + "wmem_min", values[3]);
1513 stringToFile(prefix + "wmem_def", values[4]);
1514 stringToFile(prefix + "wmem_max", values[5]);
1515 } else {
Wink Savilleed9c02b2010-12-03 12:01:38 -08001516 loge("Invalid buffersize string: " + bufferSizes);
Irfan Sheriffd649c122010-06-09 15:39:36 -07001517 }
1518 } catch (IOException e) {
Wink Savilleed9c02b2010-12-03 12:01:38 -08001519 loge("Can't set tcp buffer sizes:" + e);
Irfan Sheriffd649c122010-06-09 15:39:36 -07001520 }
1521 }
1522
1523 /**
1524 * Writes string to file. Basically same as "echo -n $string > $filename"
1525 *
1526 * @param filename
1527 * @param string
1528 * @throws IOException
1529 */
1530 private void stringToFile(String filename, String string) throws IOException {
1531 FileWriter out = new FileWriter(filename);
1532 try {
1533 out.write(string);
1534 } finally {
1535 out.close();
1536 }
1537 }
1538
1539
Robert Greenwalt42acef32009-08-12 16:08:25 -07001540 /**
1541 * Adjust the per-process dns entries (net.dns<x>.<pid>) based
1542 * on the highest priority active net which this process requested.
1543 * If there aren't any, clear it out
1544 */
1545 private void reassessPidDns(int myPid, boolean doBump)
1546 {
Wink Savilleed9c02b2010-12-03 12:01:38 -08001547 if (DBG) log("reassessPidDns for pid " + myPid);
Robert Greenwalt42acef32009-08-12 16:08:25 -07001548 for(int i : mPriorityList) {
1549 if (mNetAttributes[i].isDefault()) {
1550 continue;
1551 }
1552 NetworkStateTracker nt = mNetTrackers[i];
Robert Greenwalt86e9e552009-07-16 17:21:39 -07001553 if (nt.getNetworkInfo().isConnected() &&
1554 !nt.isTeardownRequested()) {
Robert Greenwalt37e65eb2010-08-30 10:56:47 -07001555 LinkProperties p = nt.getLinkProperties();
Robert Greenwalt47f69fe2010-06-15 15:43:39 -07001556 if (p == null) continue;
Robert Greenwalt42acef32009-08-12 16:08:25 -07001557 List pids = mNetRequestersPids[i];
1558 for (int j=0; j<pids.size(); j++) {
1559 Integer pid = (Integer)pids.get(j);
1560 if (pid.intValue() == myPid) {
Robert Greenwalt47f69fe2010-06-15 15:43:39 -07001561 Collection<InetAddress> dnses = p.getDnses();
1562 writePidDns(dnses, myPid);
Robert Greenwalt42acef32009-08-12 16:08:25 -07001563 if (doBump) {
1564 bumpDns();
1565 }
1566 return;
1567 }
1568 }
1569 }
1570 }
1571 // nothing found - delete
1572 for (int i = 1; ; i++) {
1573 String prop = "net.dns" + i + "." + myPid;
1574 if (SystemProperties.get(prop).length() == 0) {
1575 if (doBump) {
1576 bumpDns();
1577 }
1578 return;
1579 }
1580 SystemProperties.set(prop, "");
1581 }
1582 }
1583
Robert Greenwalt10398722010-12-17 15:20:36 -08001584 // return true if results in a change
1585 private boolean writePidDns(Collection <InetAddress> dnses, int pid) {
Robert Greenwalt42acef32009-08-12 16:08:25 -07001586 int j = 1;
Robert Greenwalt10398722010-12-17 15:20:36 -08001587 boolean changed = false;
Robert Greenwalt47f69fe2010-06-15 15:43:39 -07001588 for (InetAddress dns : dnses) {
Robert Greenwalt10398722010-12-17 15:20:36 -08001589 String dnsString = dns.getHostAddress();
1590 if (changed || !dnsString.equals(SystemProperties.get("net.dns" + j + "." + pid))) {
1591 changed = true;
1592 SystemProperties.set("net.dns" + j++ + "." + pid, dns.getHostAddress());
1593 }
Robert Greenwalt42acef32009-08-12 16:08:25 -07001594 }
Robert Greenwalt10398722010-12-17 15:20:36 -08001595 return changed;
Robert Greenwalt42acef32009-08-12 16:08:25 -07001596 }
1597
1598 private void bumpDns() {
1599 /*
1600 * Bump the property that tells the name resolver library to reread
1601 * the DNS server list from the properties.
1602 */
1603 String propVal = SystemProperties.get("net.dnschange");
1604 int n = 0;
1605 if (propVal.length() != 0) {
1606 try {
1607 n = Integer.parseInt(propVal);
1608 } catch (NumberFormatException e) {}
1609 }
1610 SystemProperties.set("net.dnschange", "" + (n+1));
Robert Greenwalt03595d02010-11-02 14:08:23 -07001611 /*
1612 * Tell the VMs to toss their DNS caches
1613 */
1614 Intent intent = new Intent(Intent.ACTION_CLEAR_DNS_CACHE);
1615 intent.addFlags(Intent.FLAG_RECEIVER_REPLACE_PENDING);
1616 mContext.sendBroadcast(intent);
Robert Greenwalt42acef32009-08-12 16:08:25 -07001617 }
1618
Robert Greenwaltc76b8fa2010-07-23 15:46:26 -07001619 private void handleDnsConfigurationChange(int netType) {
Robert Greenwalt42acef32009-08-12 16:08:25 -07001620 // add default net's dns entries
Robert Greenwaltc76b8fa2010-07-23 15:46:26 -07001621 NetworkStateTracker nt = mNetTrackers[netType];
1622 if (nt != null && nt.getNetworkInfo().isConnected() && !nt.isTeardownRequested()) {
Robert Greenwalt37e65eb2010-08-30 10:56:47 -07001623 LinkProperties p = nt.getLinkProperties();
Robert Greenwaltc76b8fa2010-07-23 15:46:26 -07001624 if (p == null) return;
1625 Collection<InetAddress> dnses = p.getDnses();
Robert Greenwalt10398722010-12-17 15:20:36 -08001626 boolean changed = false;
Robert Greenwaltc76b8fa2010-07-23 15:46:26 -07001627 if (mNetAttributes[netType].isDefault()) {
1628 int j = 1;
Robert Greenwalte90aa5e2010-09-01 11:34:05 -07001629 if (dnses.size() == 0 && mDefaultDns != null) {
Robert Greenwalt10398722010-12-17 15:20:36 -08001630 String dnsString = mDefaultDns.getHostAddress();
1631 if (!dnsString.equals(SystemProperties.get("net.dns1"))) {
1632 if (DBG) {
1633 log("no dns provided - using " + dnsString);
1634 }
1635 changed = true;
1636 SystemProperties.set("net.dns1", dnsString);
Robert Greenwalt42acef32009-08-12 16:08:25 -07001637 }
Robert Greenwalte90aa5e2010-09-01 11:34:05 -07001638 j++;
1639 } else {
1640 for (InetAddress dns : dnses) {
Robert Greenwalt10398722010-12-17 15:20:36 -08001641 String dnsString = dns.getHostAddress();
1642 if (!changed && dnsString.equals(SystemProperties.get("net.dns" + j))) {
1643 j++;
1644 continue;
1645 }
Robert Greenwalte90aa5e2010-09-01 11:34:05 -07001646 if (DBG) {
Wink Savilleed9c02b2010-12-03 12:01:38 -08001647 log("adding dns " + dns + " for " +
Robert Greenwalte90aa5e2010-09-01 11:34:05 -07001648 nt.getNetworkInfo().getTypeName());
1649 }
Robert Greenwalt10398722010-12-17 15:20:36 -08001650 changed = true;
1651 SystemProperties.set("net.dns" + j++, dnsString);
Robert Greenwalte90aa5e2010-09-01 11:34:05 -07001652 }
Robert Greenwaltc76b8fa2010-07-23 15:46:26 -07001653 }
1654 for (int k=j ; k<mNumDnsEntries; k++) {
Robert Greenwalt10398722010-12-17 15:20:36 -08001655 if (changed || !TextUtils.isEmpty(SystemProperties.get("net.dns" + k))) {
1656 if (DBG) log("erasing net.dns" + k);
1657 changed = true;
1658 SystemProperties.set("net.dns" + k, "");
1659 }
Robert Greenwaltc76b8fa2010-07-23 15:46:26 -07001660 }
1661 mNumDnsEntries = j;
1662 } else {
1663 // set per-pid dns for attached secondary nets
1664 List pids = mNetRequestersPids[netType];
1665 for (int y=0; y< pids.size(); y++) {
1666 Integer pid = (Integer)pids.get(y);
Robert Greenwalt10398722010-12-17 15:20:36 -08001667 changed = writePidDns(dnses, pid.intValue());
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001668 }
1669 }
Robert Greenwalt10398722010-12-17 15:20:36 -08001670 if (changed) bumpDns();
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001671 }
Robert Greenwalt42acef32009-08-12 16:08:25 -07001672 }
1673
1674 private int getRestoreDefaultNetworkDelay() {
1675 String restoreDefaultNetworkDelayStr = SystemProperties.get(
1676 NETWORK_RESTORE_DELAY_PROP_NAME);
1677 if(restoreDefaultNetworkDelayStr != null &&
1678 restoreDefaultNetworkDelayStr.length() != 0) {
1679 try {
1680 return Integer.valueOf(restoreDefaultNetworkDelayStr);
1681 } catch (NumberFormatException e) {
1682 }
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001683 }
Robert Greenwalt42acef32009-08-12 16:08:25 -07001684 return RESTORE_DEFAULT_NETWORK_DELAY;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001685 }
1686
1687 @Override
1688 protected void dump(FileDescriptor fd, PrintWriter pw, String[] args) {
Robert Greenwalt86e9e552009-07-16 17:21:39 -07001689 if (mContext.checkCallingOrSelfPermission(
1690 android.Manifest.permission.DUMP)
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001691 != PackageManager.PERMISSION_GRANTED) {
Robert Greenwalt86e9e552009-07-16 17:21:39 -07001692 pw.println("Permission Denial: can't dump ConnectivityService " +
1693 "from from pid=" + Binder.getCallingPid() + ", uid=" +
1694 Binder.getCallingUid());
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001695 return;
1696 }
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001697 pw.println();
1698 for (NetworkStateTracker nst : mNetTrackers) {
Robert Greenwaltb9285352009-12-21 18:24:07 -08001699 if (nst != null) {
1700 if (nst.getNetworkInfo().isConnected()) {
1701 pw.println("Active network: " + nst.getNetworkInfo().
1702 getTypeName());
1703 }
1704 pw.println(nst.getNetworkInfo());
1705 pw.println(nst);
1706 pw.println();
Robert Greenwalt42acef32009-08-12 16:08:25 -07001707 }
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001708 }
Robert Greenwaltb9285352009-12-21 18:24:07 -08001709
1710 pw.println("Network Requester Pids:");
1711 for (int net : mPriorityList) {
1712 String pidString = net + ": ";
1713 for (Object pid : mNetRequestersPids[net]) {
1714 pidString = pidString + pid.toString() + ", ";
1715 }
1716 pw.println(pidString);
1717 }
1718 pw.println();
1719
1720 pw.println("FeatureUsers:");
1721 for (Object requester : mFeatureUsers) {
1722 pw.println(requester.toString());
1723 }
1724 pw.println();
Robert Greenwalt2a091d72010-02-11 18:18:40 -08001725
Robert Greenwalt14f2ef42010-06-15 12:19:37 -07001726 synchronized (this) {
1727 pw.println("NetworkTranstionWakeLock is currently " +
1728 (mNetTransitionWakeLock.isHeld() ? "" : "not ") + "held.");
1729 pw.println("It was last requested for "+mNetTransitionWakeLockCausedBy);
1730 }
1731 pw.println();
1732
Robert Greenwalt2a091d72010-02-11 18:18:40 -08001733 mTethering.dump(fd, pw, args);
Robert Greenwalt4e8dfef2010-09-20 14:35:25 -07001734
1735 if (mInetLog != null) {
1736 pw.println();
1737 pw.println("Inet condition reports:");
1738 for(int i = 0; i < mInetLog.size(); i++) {
1739 pw.println(mInetLog.get(i));
1740 }
1741 }
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001742 }
1743
Robert Greenwalt42acef32009-08-12 16:08:25 -07001744 // must be stateless - things change under us.
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001745 private class MyHandler extends Handler {
Wink Savillebb08caf2010-09-02 19:23:52 -07001746 public MyHandler(Looper looper) {
1747 super(looper);
1748 }
1749
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001750 @Override
1751 public void handleMessage(Message msg) {
1752 NetworkInfo info;
1753 switch (msg.what) {
1754 case NetworkStateTracker.EVENT_STATE_CHANGED:
1755 info = (NetworkInfo) msg.obj;
Robert Greenwalt511288a2009-12-07 11:33:18 -08001756 int type = info.getType();
1757 NetworkInfo.State state = info.getState();
Robert Greenwalt6e6dec22010-01-25 16:14:00 -08001758 // only do this optimization for wifi. It going into scan mode for location
1759 // services generates alot of noise. Meanwhile the mms apn won't send out
1760 // subsequent notifications when on default cellular because it never
1761 // disconnects.. so only do this to wifi notifications. Fixed better when the
1762 // APN notifications are standardized.
1763 if (mNetAttributes[type].mLastState == state &&
1764 mNetAttributes[type].mRadio == ConnectivityManager.TYPE_WIFI) {
Robert Greenwalt511288a2009-12-07 11:33:18 -08001765 if (DBG) {
Robert Greenwalt6e6dec22010-01-25 16:14:00 -08001766 // TODO - remove this after we validate the dropping doesn't break
1767 // anything
Wink Savilleed9c02b2010-12-03 12:01:38 -08001768 log("Dropping ConnectivityChange for " +
Robert Greenwalt1193ae42010-01-13 09:36:31 -08001769 info.getTypeName() + ": " +
Robert Greenwalt511288a2009-12-07 11:33:18 -08001770 state + "/" + info.getDetailedState());
1771 }
1772 return;
1773 }
1774 mNetAttributes[type].mLastState = state;
1775
Wink Savilleed9c02b2010-12-03 12:01:38 -08001776 if (DBG) log("ConnectivityChange for " +
Robert Greenwalt86e9e552009-07-16 17:21:39 -07001777 info.getTypeName() + ": " +
Robert Greenwalt511288a2009-12-07 11:33:18 -08001778 state + "/" + info.getDetailedState());
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001779
1780 // Connectivity state changed:
1781 // [31-13] Reserved for future use
Robert Greenwalt86e9e552009-07-16 17:21:39 -07001782 // [12-9] Network subtype (for mobile network, as defined
1783 // by TelephonyManager)
1784 // [8-3] Detailed state ordinal (as defined by
1785 // NetworkInfo.DetailedState)
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001786 // [2-0] Network type (as defined by ConnectivityManager)
1787 int eventLogParam = (info.getType() & 0x7) |
1788 ((info.getDetailedState().ordinal() & 0x3f) << 3) |
1789 (info.getSubtype() << 9);
Doug Zongkerab5c49c2009-12-04 10:31:43 -08001790 EventLog.writeEvent(EventLogTags.CONNECTIVITY_STATE_CHANGED,
Robert Greenwalt86e9e552009-07-16 17:21:39 -07001791 eventLogParam);
1792
1793 if (info.getDetailedState() ==
1794 NetworkInfo.DetailedState.FAILED) {
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001795 handleConnectionFailure(info);
Robert Greenwalt511288a2009-12-07 11:33:18 -08001796 } else if (state == NetworkInfo.State.DISCONNECTED) {
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001797 handleDisconnect(info);
Robert Greenwalt511288a2009-12-07 11:33:18 -08001798 } else if (state == NetworkInfo.State.SUSPENDED) {
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001799 // TODO: need to think this over.
Robert Greenwalt86e9e552009-07-16 17:21:39 -07001800 // the logic here is, handle SUSPENDED the same as
1801 // DISCONNECTED. The only difference being we are
1802 // broadcasting an intent with NetworkInfo that's
1803 // suspended. This allows the applications an
1804 // opportunity to handle DISCONNECTED and SUSPENDED
1805 // differently, or not.
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001806 handleDisconnect(info);
Robert Greenwalt511288a2009-12-07 11:33:18 -08001807 } else if (state == NetworkInfo.State.CONNECTED) {
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001808 handleConnect(info);
1809 }
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001810 break;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001811 case NetworkStateTracker.EVENT_CONFIGURATION_CHANGED:
Robert Greenwaltc76b8fa2010-07-23 15:46:26 -07001812 info = (NetworkInfo) msg.obj;
1813 type = info.getType();
Robert Greenwalt434203a2010-10-11 16:00:27 -07001814 handleConnectivityChange(type);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001815 break;
Robert Greenwaltf3331232010-09-24 14:32:21 -07001816 case EVENT_CLEAR_NET_TRANSITION_WAKELOCK:
Robert Greenwalt14f2ef42010-06-15 12:19:37 -07001817 String causedBy = null;
1818 synchronized (ConnectivityService.this) {
1819 if (msg.arg1 == mNetTransitionWakeLockSerialNumber &&
1820 mNetTransitionWakeLock.isHeld()) {
1821 mNetTransitionWakeLock.release();
1822 causedBy = mNetTransitionWakeLockCausedBy;
1823 }
1824 }
1825 if (causedBy != null) {
Wink Savilleed9c02b2010-12-03 12:01:38 -08001826 log("NetTransition Wakelock for " + causedBy + " released by timeout");
Robert Greenwalt14f2ef42010-06-15 12:19:37 -07001827 }
Robert Greenwalt057d5e92010-09-09 14:05:10 -07001828 break;
Robert Greenwalt8dcc28b2010-09-23 10:05:56 -07001829 case EVENT_RESTORE_DEFAULT_NETWORK:
Robert Greenwalt9c75d4a2009-09-27 17:27:04 -07001830 FeatureUser u = (FeatureUser)msg.obj;
1831 u.expire();
Robert Greenwaltd7085fc2010-09-08 15:24:47 -07001832 break;
Robert Greenwalt8dcc28b2010-09-23 10:05:56 -07001833 case EVENT_INET_CONDITION_CHANGE:
1834 {
1835 int netType = msg.arg1;
1836 int condition = msg.arg2;
1837 handleInetConditionChange(netType, condition);
Robert Greenwalt14f2ef42010-06-15 12:19:37 -07001838 break;
Robert Greenwalt8dcc28b2010-09-23 10:05:56 -07001839 }
1840 case EVENT_INET_CONDITION_HOLD_END:
1841 {
1842 int netType = msg.arg1;
1843 int sequence = msg.arg2;
1844 handleInetConditionHoldEnd(netType, sequence);
Robert Greenwaltd7085fc2010-09-08 15:24:47 -07001845 break;
Robert Greenwalt8dcc28b2010-09-23 10:05:56 -07001846 }
1847 case EVENT_SET_NETWORK_PREFERENCE:
1848 {
1849 int preference = msg.arg1;
1850 handleSetNetworkPreference(preference);
1851 break;
1852 }
1853 case EVENT_SET_BACKGROUND_DATA:
1854 {
1855 boolean enabled = (msg.arg1 == ENABLED);
1856 handleSetBackgroundData(enabled);
1857 break;
1858 }
1859 case EVENT_SET_MOBILE_DATA:
1860 {
1861 boolean enabled = (msg.arg1 == ENABLED);
1862 handleSetMobileData(enabled);
1863 break;
1864 }
Robert Greenwalt434203a2010-10-11 16:00:27 -07001865 case EVENT_APPLY_GLOBAL_HTTP_PROXY:
1866 {
1867 handleDeprecatedGlobalHttpProxy();
1868 }
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001869 }
1870 }
1871 }
Robert Greenwaltd0e18ff2010-01-26 11:40:34 -08001872
1873 // javadoc from interface
Robert Greenwalt5a735062010-03-02 17:25:02 -08001874 public int tether(String iface) {
Robert Greenwaltd0e18ff2010-01-26 11:40:34 -08001875 enforceTetherChangePermission();
Robert Greenwalt5a735062010-03-02 17:25:02 -08001876
1877 if (isTetheringSupported()) {
1878 return mTethering.tether(iface);
1879 } else {
1880 return ConnectivityManager.TETHER_ERROR_UNSUPPORTED;
1881 }
Robert Greenwaltd0e18ff2010-01-26 11:40:34 -08001882 }
1883
1884 // javadoc from interface
Robert Greenwalt5a735062010-03-02 17:25:02 -08001885 public int untether(String iface) {
Robert Greenwaltd0e18ff2010-01-26 11:40:34 -08001886 enforceTetherChangePermission();
Robert Greenwalt5a735062010-03-02 17:25:02 -08001887
1888 if (isTetheringSupported()) {
1889 return mTethering.untether(iface);
1890 } else {
1891 return ConnectivityManager.TETHER_ERROR_UNSUPPORTED;
1892 }
1893 }
1894
1895 // javadoc from interface
1896 public int getLastTetherError(String iface) {
1897 enforceTetherAccessPermission();
1898
1899 if (isTetheringSupported()) {
1900 return mTethering.getLastTetherError(iface);
1901 } else {
1902 return ConnectivityManager.TETHER_ERROR_UNSUPPORTED;
1903 }
Robert Greenwalt2a091d72010-02-11 18:18:40 -08001904 }
1905
1906 // TODO - proper iface API for selection by property, inspection, etc
1907 public String[] getTetherableUsbRegexs() {
1908 enforceTetherAccessPermission();
1909 if (isTetheringSupported()) {
1910 return mTethering.getTetherableUsbRegexs();
1911 } else {
1912 return new String[0];
1913 }
1914 }
1915
1916 public String[] getTetherableWifiRegexs() {
1917 enforceTetherAccessPermission();
1918 if (isTetheringSupported()) {
1919 return mTethering.getTetherableWifiRegexs();
1920 } else {
1921 return new String[0];
1922 }
Robert Greenwaltd0e18ff2010-01-26 11:40:34 -08001923 }
1924
Danica Chang6fdd0c62010-08-11 14:54:43 -07001925 public String[] getTetherableBluetoothRegexs() {
1926 enforceTetherAccessPermission();
1927 if (isTetheringSupported()) {
1928 return mTethering.getTetherableBluetoothRegexs();
1929 } else {
1930 return new String[0];
1931 }
1932 }
1933
Robert Greenwaltd0e18ff2010-01-26 11:40:34 -08001934 // TODO - move iface listing, queries, etc to new module
1935 // javadoc from interface
1936 public String[] getTetherableIfaces() {
Robert Greenwalt2a091d72010-02-11 18:18:40 -08001937 enforceTetherAccessPermission();
Robert Greenwaltd0e18ff2010-01-26 11:40:34 -08001938 return mTethering.getTetherableIfaces();
1939 }
1940
1941 public String[] getTetheredIfaces() {
Robert Greenwalt2a091d72010-02-11 18:18:40 -08001942 enforceTetherAccessPermission();
Robert Greenwaltd0e18ff2010-01-26 11:40:34 -08001943 return mTethering.getTetheredIfaces();
1944 }
Robert Greenwalt2a091d72010-02-11 18:18:40 -08001945
Robert Greenwalt5a735062010-03-02 17:25:02 -08001946 public String[] getTetheringErroredIfaces() {
1947 enforceTetherAccessPermission();
1948 return mTethering.getErroredIfaces();
1949 }
1950
Robert Greenwalt2a091d72010-02-11 18:18:40 -08001951 // if ro.tether.denied = true we default to no tethering
1952 // gservices could set the secure setting to 1 though to enable it on a build where it
1953 // had previously been turned off.
1954 public boolean isTetheringSupported() {
1955 enforceTetherAccessPermission();
1956 int defaultVal = (SystemProperties.get("ro.tether.denied").equals("true") ? 0 : 1);
Robert Greenwaltc9d5fb72010-02-25 12:29:30 -08001957 boolean tetherEnabledInSettings = (Settings.Secure.getInt(mContext.getContentResolver(),
1958 Settings.Secure.TETHER_SUPPORTED, defaultVal) != 0);
1959 return tetherEnabledInSettings && mTetheringConfigValid;
Robert Greenwalt2a091d72010-02-11 18:18:40 -08001960 }
Robert Greenwalt14f2ef42010-06-15 12:19:37 -07001961
1962 // An API NetworkStateTrackers can call when they lose their network.
1963 // This will automatically be cleared after X seconds or a network becomes CONNECTED,
1964 // whichever happens first. The timer is started by the first caller and not
1965 // restarted by subsequent callers.
1966 public void requestNetworkTransitionWakelock(String forWhom) {
1967 enforceConnectivityInternalPermission();
1968 synchronized (this) {
1969 if (mNetTransitionWakeLock.isHeld()) return;
1970 mNetTransitionWakeLockSerialNumber++;
1971 mNetTransitionWakeLock.acquire();
1972 mNetTransitionWakeLockCausedBy = forWhom;
1973 }
1974 mHandler.sendMessageDelayed(mHandler.obtainMessage(
Robert Greenwaltf3331232010-09-24 14:32:21 -07001975 EVENT_CLEAR_NET_TRANSITION_WAKELOCK,
Robert Greenwalt14f2ef42010-06-15 12:19:37 -07001976 mNetTransitionWakeLockSerialNumber, 0),
1977 mNetTransitionWakeLockTimeout);
1978 return;
1979 }
Robert Greenwaltca4306c2010-09-09 13:15:32 -07001980
Robert Greenwaltd7085fc2010-09-08 15:24:47 -07001981 // 100 percent is full good, 0 is full bad.
1982 public void reportInetCondition(int networkType, int percentage) {
Wink Savilleed9c02b2010-12-03 12:01:38 -08001983 if (DBG) log("reportNetworkCondition(" + networkType + ", " + percentage + ")");
Robert Greenwaltd7085fc2010-09-08 15:24:47 -07001984 mContext.enforceCallingOrSelfPermission(
1985 android.Manifest.permission.STATUS_BAR,
1986 "ConnectivityService");
1987
Robert Greenwalt4e8dfef2010-09-20 14:35:25 -07001988 if (DBG) {
1989 int pid = getCallingPid();
1990 int uid = getCallingUid();
1991 String s = pid + "(" + uid + ") reports inet is " +
1992 (percentage > 50 ? "connected" : "disconnected") + " (" + percentage + ") on " +
1993 "network Type " + networkType + " at " + GregorianCalendar.getInstance().getTime();
1994 mInetLog.add(s);
1995 while(mInetLog.size() > INET_CONDITION_LOG_MAX_SIZE) {
1996 mInetLog.remove(0);
1997 }
1998 }
Robert Greenwaltd7085fc2010-09-08 15:24:47 -07001999 mHandler.sendMessage(mHandler.obtainMessage(
Robert Greenwalt8dcc28b2010-09-23 10:05:56 -07002000 EVENT_INET_CONDITION_CHANGE, networkType, percentage));
2001 }
2002
2003 private void handleInetConditionChange(int netType, int condition) {
2004 if (DBG) {
Wink Savilleed9c02b2010-12-03 12:01:38 -08002005 log("Inet connectivity change, net=" +
Robert Greenwalt8dcc28b2010-09-23 10:05:56 -07002006 netType + ", condition=" + condition +
2007 ",mActiveDefaultNetwork=" + mActiveDefaultNetwork);
2008 }
2009 if (mActiveDefaultNetwork == -1) {
Wink Savilleed9c02b2010-12-03 12:01:38 -08002010 if (DBG) log("no active default network - aborting");
Robert Greenwalt8dcc28b2010-09-23 10:05:56 -07002011 return;
2012 }
2013 if (mActiveDefaultNetwork != netType) {
Wink Savilleed9c02b2010-12-03 12:01:38 -08002014 if (DBG) log("given net not default - aborting");
Robert Greenwalt8dcc28b2010-09-23 10:05:56 -07002015 return;
2016 }
2017 mDefaultInetCondition = condition;
2018 int delay;
2019 if (mInetConditionChangeInFlight == false) {
Wink Savilleed9c02b2010-12-03 12:01:38 -08002020 if (DBG) log("starting a change hold");
Robert Greenwalt8dcc28b2010-09-23 10:05:56 -07002021 // setup a new hold to debounce this
2022 if (mDefaultInetCondition > 50) {
2023 delay = Settings.Secure.getInt(mContext.getContentResolver(),
2024 Settings.Secure.INET_CONDITION_DEBOUNCE_UP_DELAY, 500);
2025 } else {
2026 delay = Settings.Secure.getInt(mContext.getContentResolver(),
2027 Settings.Secure.INET_CONDITION_DEBOUNCE_DOWN_DELAY, 3000);
2028 }
2029 mInetConditionChangeInFlight = true;
2030 mHandler.sendMessageDelayed(mHandler.obtainMessage(EVENT_INET_CONDITION_HOLD_END,
2031 mActiveDefaultNetwork, mDefaultConnectionSequence), delay);
2032 } else {
2033 // we've set the new condition, when this hold ends that will get
2034 // picked up
Wink Savilleed9c02b2010-12-03 12:01:38 -08002035 if (DBG) log("currently in hold - not setting new end evt");
Robert Greenwalt8dcc28b2010-09-23 10:05:56 -07002036 }
2037 }
2038
2039 private void handleInetConditionHoldEnd(int netType, int sequence) {
2040 if (DBG) {
Wink Savilleed9c02b2010-12-03 12:01:38 -08002041 log("Inet hold end, net=" + netType +
Robert Greenwalt8dcc28b2010-09-23 10:05:56 -07002042 ", condition =" + mDefaultInetCondition +
2043 ", published condition =" + mDefaultInetConditionPublished);
2044 }
2045 mInetConditionChangeInFlight = false;
2046
2047 if (mActiveDefaultNetwork == -1) {
Wink Savilleed9c02b2010-12-03 12:01:38 -08002048 if (DBG) log("no active default network - aborting");
Robert Greenwalt8dcc28b2010-09-23 10:05:56 -07002049 return;
2050 }
2051 if (mDefaultConnectionSequence != sequence) {
Wink Savilleed9c02b2010-12-03 12:01:38 -08002052 if (DBG) log("event hold for obsolete network - aborting");
Robert Greenwalt8dcc28b2010-09-23 10:05:56 -07002053 return;
2054 }
2055 if (mDefaultInetConditionPublished == mDefaultInetCondition) {
Wink Savilleed9c02b2010-12-03 12:01:38 -08002056 if (DBG) log("no change in condition - aborting");
Robert Greenwalt8dcc28b2010-09-23 10:05:56 -07002057 return;
2058 }
2059 NetworkInfo networkInfo = mNetTrackers[mActiveDefaultNetwork].getNetworkInfo();
2060 if (networkInfo.isConnected() == false) {
Wink Savilleed9c02b2010-12-03 12:01:38 -08002061 if (DBG) log("default network not connected - aborting");
Robert Greenwalt8dcc28b2010-09-23 10:05:56 -07002062 return;
2063 }
2064 mDefaultInetConditionPublished = mDefaultInetCondition;
2065 sendInetConditionBroadcast(networkInfo);
2066 return;
Robert Greenwaltd7085fc2010-09-08 15:24:47 -07002067 }
Robert Greenwalt434203a2010-10-11 16:00:27 -07002068
2069 public synchronized ProxyProperties getProxy() {
2070 if (mGlobalProxy != null) return mGlobalProxy;
2071 if (mDefaultProxy != null) return mDefaultProxy;
2072 return null;
2073 }
2074
2075 public void setGlobalProxy(ProxyProperties proxyProperties) {
2076 enforceChangePermission();
2077 synchronized (mGlobalProxyLock) {
2078 if (proxyProperties == mGlobalProxy) return;
2079 if (proxyProperties != null && proxyProperties.equals(mGlobalProxy)) return;
2080 if (mGlobalProxy != null && mGlobalProxy.equals(proxyProperties)) return;
2081
2082 String host = "";
2083 int port = 0;
2084 String exclList = "";
2085 if (proxyProperties != null && !TextUtils.isEmpty(proxyProperties.getHost())) {
2086 mGlobalProxy = new ProxyProperties(proxyProperties);
2087 host = mGlobalProxy.getHost();
2088 port = mGlobalProxy.getPort();
2089 exclList = mGlobalProxy.getExclusionList();
2090 } else {
2091 mGlobalProxy = null;
2092 }
2093 ContentResolver res = mContext.getContentResolver();
2094 Settings.Secure.putString(res, Settings.Secure.GLOBAL_HTTP_PROXY_HOST, host);
2095 Settings.Secure.putInt(res, Settings.Secure.GLOBAL_HTTP_PROXY_PORT, port);
Robert Greenwaltb7090d62010-12-02 11:31:00 -08002096 Settings.Secure.putString(res, Settings.Secure.GLOBAL_HTTP_PROXY_EXCLUSION_LIST,
Robert Greenwalt434203a2010-10-11 16:00:27 -07002097 exclList);
2098 }
2099
2100 if (mGlobalProxy == null) {
2101 proxyProperties = mDefaultProxy;
2102 }
2103 sendProxyBroadcast(proxyProperties);
2104 }
2105
Robert Greenwaltb7090d62010-12-02 11:31:00 -08002106 private void loadGlobalProxy() {
2107 ContentResolver res = mContext.getContentResolver();
2108 String host = Settings.Secure.getString(res, Settings.Secure.GLOBAL_HTTP_PROXY_HOST);
2109 int port = Settings.Secure.getInt(res, Settings.Secure.GLOBAL_HTTP_PROXY_PORT, 0);
2110 String exclList = Settings.Secure.getString(res,
2111 Settings.Secure.GLOBAL_HTTP_PROXY_EXCLUSION_LIST);
2112 if (!TextUtils.isEmpty(host)) {
2113 ProxyProperties proxyProperties = new ProxyProperties(host, port, exclList);
2114 synchronized (mGlobalProxyLock) {
2115 mGlobalProxy = proxyProperties;
2116 }
2117 }
2118 }
2119
Robert Greenwalt434203a2010-10-11 16:00:27 -07002120 public ProxyProperties getGlobalProxy() {
2121 synchronized (mGlobalProxyLock) {
2122 return mGlobalProxy;
2123 }
2124 }
2125
2126 private void handleApplyDefaultProxy(int type) {
2127 // check if new default - push it out to all VM if so
2128 ProxyProperties proxy = mNetTrackers[type].getLinkProperties().getHttpProxy();
2129 synchronized (this) {
2130 if (mDefaultProxy != null && mDefaultProxy.equals(proxy)) return;
2131 if (mDefaultProxy == proxy) return;
2132 if (!TextUtils.isEmpty(proxy.getHost())) {
2133 mDefaultProxy = proxy;
2134 } else {
2135 mDefaultProxy = null;
2136 }
2137 }
Wink Savilleed9c02b2010-12-03 12:01:38 -08002138 if (DBG) log("changing default proxy to " + proxy);
Robert Greenwalt434203a2010-10-11 16:00:27 -07002139 if ((proxy == null && mGlobalProxy == null) || proxy.equals(mGlobalProxy)) return;
2140 if (mGlobalProxy != null) return;
2141 sendProxyBroadcast(proxy);
2142 }
2143
2144 private void handleDeprecatedGlobalHttpProxy() {
2145 String proxy = Settings.Secure.getString(mContext.getContentResolver(),
2146 Settings.Secure.HTTP_PROXY);
2147 if (!TextUtils.isEmpty(proxy)) {
2148 String data[] = proxy.split(":");
2149 String proxyHost = data[0];
2150 int proxyPort = 8080;
2151 if (data.length > 1) {
2152 try {
2153 proxyPort = Integer.parseInt(data[1]);
2154 } catch (NumberFormatException e) {
2155 return;
2156 }
2157 }
2158 ProxyProperties p = new ProxyProperties(data[0], proxyPort, "");
2159 setGlobalProxy(p);
2160 }
2161 }
2162
2163 private void sendProxyBroadcast(ProxyProperties proxy) {
Robert Greenwalt55985be2010-12-23 15:51:10 -08002164 if (proxy == null) proxy = new ProxyProperties("", 0, "");
Wink Savilleed9c02b2010-12-03 12:01:38 -08002165 log("sending Proxy Broadcast for " + proxy);
Robert Greenwalt434203a2010-10-11 16:00:27 -07002166 Intent intent = new Intent(Proxy.PROXY_CHANGE_ACTION);
2167 intent.addFlags(Intent.FLAG_RECEIVER_REPLACE_PENDING);
2168 intent.putExtra(Proxy.EXTRA_PROXY_INFO, proxy);
Robert Greenwalta2e13392010-12-06 11:29:17 -08002169 mContext.sendStickyBroadcast(intent);
Robert Greenwalt434203a2010-10-11 16:00:27 -07002170 }
2171
2172 private static class SettingsObserver extends ContentObserver {
2173 private int mWhat;
2174 private Handler mHandler;
2175 SettingsObserver(Handler handler, int what) {
2176 super(handler);
2177 mHandler = handler;
2178 mWhat = what;
2179 }
2180
2181 void observe(Context context) {
2182 ContentResolver resolver = context.getContentResolver();
2183 resolver.registerContentObserver(Settings.Secure.getUriFor(
2184 Settings.Secure.HTTP_PROXY), false, this);
2185 }
2186
2187 @Override
2188 public void onChange(boolean selfChange) {
2189 mHandler.obtainMessage(mWhat).sendToTarget();
2190 }
2191 }
Wink Savilleed9c02b2010-12-03 12:01:38 -08002192
2193 private void log(String s) {
2194 Slog.d(TAG, s);
2195 }
2196
2197 private void loge(String s) {
2198 Slog.e(TAG, s);
2199 }
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002200}