blob: f1fce3e69b3cc8532f1f561699dc36c8bff9f97a [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
19import android.app.Notification;
20import android.app.NotificationManager;
21import android.content.ContentResolver;
22import android.content.Context;
23import android.content.Intent;
24import android.content.pm.PackageManager;
Robert Greenwalt434203a2010-10-11 16:00:27 -070025import android.database.ContentObserver;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080026import android.net.ConnectivityManager;
27import android.net.IConnectivityManager;
28import android.net.MobileDataStateTracker;
29import android.net.NetworkInfo;
Robert Greenwalt37e65eb2010-08-30 10:56:47 -070030import android.net.LinkProperties;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080031import 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;
52
Robert Greenwaltd0e18ff2010-01-26 11:40:34 -080053import com.android.server.connectivity.Tethering;
54
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080055import java.io.FileDescriptor;
Irfan Sheriffd649c122010-06-09 15:39:36 -070056import java.io.FileWriter;
57import java.io.IOException;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080058import java.io.PrintWriter;
Robert Greenwalt47f69fe2010-06-15 15:43:39 -070059import java.net.InetAddress;
Robert Greenwalt434203a2010-10-11 16:00:27 -070060import java.net.InetSocketAddress;
Robert Greenwalt47f69fe2010-06-15 15:43:39 -070061import java.net.UnknownHostException;
Robert Greenwalt42acef32009-08-12 16:08:25 -070062import java.util.ArrayList;
Robert Greenwalt47f69fe2010-06-15 15:43:39 -070063import java.util.Collection;
Robert Greenwalt4e8dfef2010-09-20 14:35:25 -070064import java.util.GregorianCalendar;
Robert Greenwalt42acef32009-08-12 16:08:25 -070065import java.util.List;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080066
67/**
68 * @hide
69 */
70public class ConnectivityService extends IConnectivityManager.Stub {
71
Robert Greenwaltba175a52010-10-05 19:12:26 -070072 private static final boolean DBG = true;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080073 private static final String TAG = "ConnectivityService";
74
Robert Greenwalt42acef32009-08-12 16:08:25 -070075 // how long to wait before switching back to a radio's default network
76 private static final int RESTORE_DEFAULT_NETWORK_DELAY = 1 * 60 * 1000;
77 // system property that can override the above value
78 private static final String NETWORK_RESTORE_DELAY_PROP_NAME =
79 "android.telephony.apn-restore";
80
Robert Greenwaltd0e18ff2010-01-26 11:40:34 -080081 private Tethering mTethering;
Robert Greenwaltc9d5fb72010-02-25 12:29:30 -080082 private boolean mTetheringConfigValid = false;
Robert Greenwaltd0e18ff2010-01-26 11:40:34 -080083
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080084 /**
85 * Sometimes we want to refer to the individual network state
86 * trackers separately, and sometimes we just want to treat them
87 * abstractly.
88 */
89 private NetworkStateTracker mNetTrackers[];
Robert Greenwalt42acef32009-08-12 16:08:25 -070090
91 /**
92 * A per Net list of the PID's that requested access to the net
93 * used both as a refcount and for per-PID DNS selection
94 */
95 private List mNetRequestersPids[];
96
Irfan Sheriffa2a1b912010-06-07 09:03:04 -070097 private WifiWatchdogService mWifiWatchdogService;
98
Robert Greenwalt42acef32009-08-12 16:08:25 -070099 // priority order of the nettrackers
100 // (excluding dynamically set mNetworkPreference)
101 // TODO - move mNetworkTypePreference into this
102 private int[] mPriorityList;
103
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800104 private Context mContext;
105 private int mNetworkPreference;
Robert Greenwalt42acef32009-08-12 16:08:25 -0700106 private int mActiveDefaultNetwork = -1;
Robert Greenwaltd7085fc2010-09-08 15:24:47 -0700107 // 0 is full bad, 100 is full good
108 private int mDefaultInetCondition = 0;
109 private int mDefaultInetConditionPublished = 0;
110 private boolean mInetConditionChangeInFlight = false;
111 private int mDefaultConnectionSequence = 0;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800112
113 private int mNumDnsEntries;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800114
115 private boolean mTestMode;
Joe Onorato00092872010-09-01 21:18:22 -0700116 private static ConnectivityService sServiceInstance;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800117
Robert Greenwalt8dcc28b2010-09-23 10:05:56 -0700118 private static final int ENABLED = 1;
119 private static final int DISABLED = 0;
120
121 // Share the event space with NetworkStateTracker (which can't see this
122 // internal class but sends us events). If you change these, change
123 // NetworkStateTracker.java too.
124 private static final int MIN_NETWORK_STATE_TRACKER_EVENT = 1;
125 private static final int MAX_NETWORK_STATE_TRACKER_EVENT = 100;
126
127 /**
128 * used internally as a delayed event to make us switch back to the
129 * default network
130 */
131 private static final int EVENT_RESTORE_DEFAULT_NETWORK =
132 MAX_NETWORK_STATE_TRACKER_EVENT + 1;
133
134 /**
135 * used internally to change our mobile data enabled flag
136 */
137 private static final int EVENT_CHANGE_MOBILE_DATA_ENABLED =
138 MAX_NETWORK_STATE_TRACKER_EVENT + 2;
139
140 /**
141 * used internally to change our network preference setting
142 * arg1 = networkType to prefer
143 */
144 private static final int EVENT_SET_NETWORK_PREFERENCE =
145 MAX_NETWORK_STATE_TRACKER_EVENT + 3;
146
147 /**
148 * used internally to synchronize inet condition reports
149 * arg1 = networkType
150 * arg2 = condition (0 bad, 100 good)
151 */
152 private static final int EVENT_INET_CONDITION_CHANGE =
153 MAX_NETWORK_STATE_TRACKER_EVENT + 4;
154
155 /**
156 * used internally to mark the end of inet condition hold periods
157 * arg1 = networkType
158 */
159 private static final int EVENT_INET_CONDITION_HOLD_END =
160 MAX_NETWORK_STATE_TRACKER_EVENT + 5;
161
162 /**
163 * used internally to set the background data preference
164 * arg1 = TRUE for enabled, FALSE for disabled
165 */
166 private static final int EVENT_SET_BACKGROUND_DATA =
167 MAX_NETWORK_STATE_TRACKER_EVENT + 6;
168
169 /**
170 * used internally to set enable/disable cellular data
171 * arg1 = ENBALED or DISABLED
172 */
173 private static final int EVENT_SET_MOBILE_DATA =
174 MAX_NETWORK_STATE_TRACKER_EVENT + 7;
175
Robert Greenwaltf3331232010-09-24 14:32:21 -0700176 /**
177 * used internally to clear a wakelock when transitioning
178 * from one net to another
179 */
180 private static final int EVENT_CLEAR_NET_TRANSITION_WAKELOCK =
181 MAX_NETWORK_STATE_TRACKER_EVENT + 8;
182
Robert Greenwalt434203a2010-10-11 16:00:27 -0700183 /**
184 * used internally to reload global proxy settings
185 */
186 private static final int EVENT_APPLY_GLOBAL_HTTP_PROXY =
187 MAX_NETWORK_STATE_TRACKER_EVENT + 9;
188
Robert Greenwalt42acef32009-08-12 16:08:25 -0700189 private Handler mHandler;
190
191 // list of DeathRecipients used to make sure features are turned off when
192 // a process dies
193 private List mFeatureUsers;
194
Mike Lockwood0f79b542009-08-14 14:18:49 -0400195 private boolean mSystemReady;
Dianne Hackborn1c633fc2009-12-08 19:45:14 -0800196 private Intent mInitialBroadcast;
Mike Lockwood0f79b542009-08-14 14:18:49 -0400197
Robert Greenwalt14f2ef42010-06-15 12:19:37 -0700198 private PowerManager.WakeLock mNetTransitionWakeLock;
199 private String mNetTransitionWakeLockCausedBy = "";
200 private int mNetTransitionWakeLockSerialNumber;
201 private int mNetTransitionWakeLockTimeout;
202
Robert Greenwalte90aa5e2010-09-01 11:34:05 -0700203 private InetAddress mDefaultDns;
204
Robert Greenwalt4e8dfef2010-09-20 14:35:25 -0700205 // used in DBG mode to track inet condition reports
206 private static final int INET_CONDITION_LOG_MAX_SIZE = 15;
207 private ArrayList mInetLog;
208
Robert Greenwalt434203a2010-10-11 16:00:27 -0700209 // track the current default http proxy - tell the world if we get a new one (real change)
210 private ProxyProperties mDefaultProxy = null;
211 // track the global proxy.
212 private ProxyProperties mGlobalProxy = null;
213 private final Object mGlobalProxyLock = new Object();
214
215 private SettingsObserver mSettingsObserver;
216
Robert Greenwalt511288a2009-12-07 11:33:18 -0800217 private static class NetworkAttributes {
Robert Greenwalt42acef32009-08-12 16:08:25 -0700218 /**
219 * Class for holding settings read from resources.
220 */
221 public String mName;
222 public int mType;
223 public int mRadio;
224 public int mPriority;
Robert Greenwalt511288a2009-12-07 11:33:18 -0800225 public NetworkInfo.State mLastState;
Robert Greenwalt42acef32009-08-12 16:08:25 -0700226 public NetworkAttributes(String init) {
227 String fragments[] = init.split(",");
228 mName = fragments[0].toLowerCase();
Robert Greenwalt5154ae762009-10-30 14:17:42 -0700229 mType = Integer.parseInt(fragments[1]);
230 mRadio = Integer.parseInt(fragments[2]);
231 mPriority = Integer.parseInt(fragments[3]);
Robert Greenwalt511288a2009-12-07 11:33:18 -0800232 mLastState = NetworkInfo.State.UNKNOWN;
Robert Greenwalt42acef32009-08-12 16:08:25 -0700233 }
234 public boolean isDefault() {
235 return (mType == mRadio);
236 }
237 }
238 NetworkAttributes[] mNetAttributes;
Robert Greenwalt5154ae762009-10-30 14:17:42 -0700239 int mNetworksDefined;
Robert Greenwalt42acef32009-08-12 16:08:25 -0700240
Robert Greenwalt511288a2009-12-07 11:33:18 -0800241 private static class RadioAttributes {
Robert Greenwalt42acef32009-08-12 16:08:25 -0700242 public int mSimultaneity;
243 public int mType;
244 public RadioAttributes(String init) {
245 String fragments[] = init.split(",");
Robert Greenwalt5154ae762009-10-30 14:17:42 -0700246 mType = Integer.parseInt(fragments[0]);
247 mSimultaneity = Integer.parseInt(fragments[1]);
Robert Greenwalt42acef32009-08-12 16:08:25 -0700248 }
249 }
250 RadioAttributes[] mRadioAttributes;
251
Wink Savillebb08caf2010-09-02 19:23:52 -0700252 public static synchronized ConnectivityService getInstance(Context context) {
253 if (sServiceInstance == null) {
254 sServiceInstance = new ConnectivityService(context);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800255 }
Wink Savillebb08caf2010-09-02 19:23:52 -0700256 return sServiceInstance;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800257 }
Robert Greenwalt86e9e552009-07-16 17:21:39 -0700258
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800259 private ConnectivityService(Context context) {
Wink Savilleed9c02b2010-12-03 12:01:38 -0800260 if (DBG) log("ConnectivityService starting up");
Robert Greenwaltde8383c2010-01-14 17:47:58 -0800261
Wink Savillebb08caf2010-09-02 19:23:52 -0700262 HandlerThread handlerThread = new HandlerThread("ConnectivityServiceThread");
263 handlerThread.start();
264 mHandler = new MyHandler(handlerThread.getLooper());
265
Robert Greenwaltde8383c2010-01-14 17:47:58 -0800266 // setup our unique device name
Robert Greenwalt733c6292010-12-06 09:30:17 -0800267 if (TextUtils.isEmpty(SystemProperties.get("net.hostname"))) {
268 String id = Settings.Secure.getString(context.getContentResolver(),
269 Settings.Secure.ANDROID_ID);
270 if (id != null && id.length() > 0) {
271 String name = new String("android_").concat(id);
272 SystemProperties.set("net.hostname", name);
273 }
Robert Greenwaltde8383c2010-01-14 17:47:58 -0800274 }
275
Robert Greenwalte90aa5e2010-09-01 11:34:05 -0700276 // read our default dns server ip
277 String dns = Settings.Secure.getString(context.getContentResolver(),
278 Settings.Secure.DEFAULT_DNS_SERVER);
279 if (dns == null || dns.length() == 0) {
280 dns = context.getResources().getString(
281 com.android.internal.R.string.config_default_dns_server);
282 }
283 try {
284 mDefaultDns = InetAddress.getByName(dns);
285 } catch (UnknownHostException e) {
Wink Savilleed9c02b2010-12-03 12:01:38 -0800286 loge("Error setting defaultDns using " + dns);
Robert Greenwalte90aa5e2010-09-01 11:34:05 -0700287 }
288
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800289 mContext = context;
Robert Greenwalt14f2ef42010-06-15 12:19:37 -0700290
291 PowerManager powerManager = (PowerManager)mContext.getSystemService(Context.POWER_SERVICE);
292 mNetTransitionWakeLock = powerManager.newWakeLock(PowerManager.PARTIAL_WAKE_LOCK, TAG);
293 mNetTransitionWakeLockTimeout = mContext.getResources().getInteger(
294 com.android.internal.R.integer.config_networkTransitionTimeout);
295
Robert Greenwalt42acef32009-08-12 16:08:25 -0700296 mNetTrackers = new NetworkStateTracker[
297 ConnectivityManager.MAX_NETWORK_TYPE+1];
Robert Greenwalt86e9e552009-07-16 17:21:39 -0700298
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800299 mNetworkPreference = getPersistedNetworkPreference();
Robert Greenwalt86e9e552009-07-16 17:21:39 -0700300
Robert Greenwalt5154ae762009-10-30 14:17:42 -0700301 mRadioAttributes = new RadioAttributes[ConnectivityManager.MAX_RADIO_TYPE+1];
302 mNetAttributes = new NetworkAttributes[ConnectivityManager.MAX_NETWORK_TYPE+1];
303
Robert Greenwalt42acef32009-08-12 16:08:25 -0700304 // Load device network attributes from resources
Robert Greenwalt42acef32009-08-12 16:08:25 -0700305 String[] raStrings = context.getResources().getStringArray(
306 com.android.internal.R.array.radioAttributes);
Robert Greenwalt5154ae762009-10-30 14:17:42 -0700307 for (String raString : raStrings) {
308 RadioAttributes r = new RadioAttributes(raString);
309 if (r.mType > ConnectivityManager.MAX_RADIO_TYPE) {
Wink Savilleed9c02b2010-12-03 12:01:38 -0800310 loge("Error in radioAttributes - ignoring attempt to define type " + r.mType);
Robert Greenwalt5154ae762009-10-30 14:17:42 -0700311 continue;
312 }
313 if (mRadioAttributes[r.mType] != null) {
Wink Savilleed9c02b2010-12-03 12:01:38 -0800314 loge("Error in radioAttributes - ignoring attempt to redefine type " +
Robert Greenwalt5154ae762009-10-30 14:17:42 -0700315 r.mType);
316 continue;
317 }
Robert Greenwalt42acef32009-08-12 16:08:25 -0700318 mRadioAttributes[r.mType] = r;
319 }
320
Robert Greenwalt5154ae762009-10-30 14:17:42 -0700321 String[] naStrings = context.getResources().getStringArray(
322 com.android.internal.R.array.networkAttributes);
323 for (String naString : naStrings) {
324 try {
325 NetworkAttributes n = new NetworkAttributes(naString);
326 if (n.mType > ConnectivityManager.MAX_NETWORK_TYPE) {
Wink Savilleed9c02b2010-12-03 12:01:38 -0800327 loge("Error in networkAttributes - ignoring attempt to define type " +
Robert Greenwalt5154ae762009-10-30 14:17:42 -0700328 n.mType);
329 continue;
Robert Greenwalt42acef32009-08-12 16:08:25 -0700330 }
Robert Greenwalt5154ae762009-10-30 14:17:42 -0700331 if (mNetAttributes[n.mType] != null) {
Wink Savilleed9c02b2010-12-03 12:01:38 -0800332 loge("Error in networkAttributes - ignoring attempt to redefine type " +
Robert Greenwalt5154ae762009-10-30 14:17:42 -0700333 n.mType);
334 continue;
335 }
336 if (mRadioAttributes[n.mRadio] == null) {
Wink Savilleed9c02b2010-12-03 12:01:38 -0800337 loge("Error in networkAttributes - ignoring attempt to use undefined " +
Robert Greenwalt5154ae762009-10-30 14:17:42 -0700338 "radio " + n.mRadio + " in network type " + n.mType);
339 continue;
340 }
341 mNetAttributes[n.mType] = n;
342 mNetworksDefined++;
343 } catch(Exception e) {
344 // ignore it - leave the entry null
Robert Greenwalt42acef32009-08-12 16:08:25 -0700345 }
346 }
347
Robert Greenwalt5154ae762009-10-30 14:17:42 -0700348 // high priority first
349 mPriorityList = new int[mNetworksDefined];
350 {
351 int insertionPoint = mNetworksDefined-1;
352 int currentLowest = 0;
353 int nextLowest = 0;
354 while (insertionPoint > -1) {
355 for (NetworkAttributes na : mNetAttributes) {
356 if (na == null) continue;
357 if (na.mPriority < currentLowest) continue;
358 if (na.mPriority > currentLowest) {
359 if (na.mPriority < nextLowest || nextLowest == 0) {
360 nextLowest = na.mPriority;
361 }
362 continue;
363 }
364 mPriorityList[insertionPoint--] = na.mType;
365 }
366 currentLowest = nextLowest;
367 nextLowest = 0;
368 }
369 }
370
371 mNetRequestersPids = new ArrayList[ConnectivityManager.MAX_NETWORK_TYPE+1];
372 for (int i : mPriorityList) {
Robert Greenwalt42acef32009-08-12 16:08:25 -0700373 mNetRequestersPids[i] = new ArrayList();
374 }
375
376 mFeatureUsers = new ArrayList();
377
Robert Greenwalt5154ae762009-10-30 14:17:42 -0700378 mNumDnsEntries = 0;
379
380 mTestMode = SystemProperties.get("cm.test.mode").equals("true")
381 && SystemProperties.get("ro.build.type").equals("eng");
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800382 /*
383 * Create the network state trackers for Wi-Fi and mobile
384 * data. Maybe this could be done with a factory class,
385 * but it's not clear that it's worth it, given that
386 * the number of different network types is not going
387 * to change very often.
388 */
Robert Greenwaltc03fa502010-02-23 18:58:05 -0800389 boolean noMobileData = !getMobileDataEnabled();
Robert Greenwalt5154ae762009-10-30 14:17:42 -0700390 for (int netType : mPriorityList) {
391 switch (mNetAttributes[netType].mRadio) {
392 case ConnectivityManager.TYPE_WIFI:
Wink Savilleed9c02b2010-12-03 12:01:38 -0800393 if (DBG) log("Starting Wifi Service.");
Wink Savillec7a98342010-08-13 16:11:42 -0700394 WifiStateTracker wst = new WifiStateTracker();
Irfan Sheriff0d255342010-07-28 09:35:20 -0700395 WifiService wifiService = new WifiService(context);
Robert Greenwalt5154ae762009-10-30 14:17:42 -0700396 ServiceManager.addService(Context.WIFI_SERVICE, wifiService);
Irfan Sheriff0d255342010-07-28 09:35:20 -0700397 wifiService.checkAndStartWifi();
Robert Greenwalt5154ae762009-10-30 14:17:42 -0700398 mNetTrackers[ConnectivityManager.TYPE_WIFI] = wst;
Wink Savillec7a98342010-08-13 16:11:42 -0700399 wst.startMonitoring(context, mHandler);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800400
Irfan Sheriffa2a1b912010-06-07 09:03:04 -0700401 //TODO: as part of WWS refactor, create only when needed
Irfan Sheriff0d255342010-07-28 09:35:20 -0700402 mWifiWatchdogService = new WifiWatchdogService(context);
Irfan Sheriffa2a1b912010-06-07 09:03:04 -0700403
Robert Greenwalt5154ae762009-10-30 14:17:42 -0700404 break;
405 case ConnectivityManager.TYPE_MOBILE:
Wink Savillec7a98342010-08-13 16:11:42 -0700406 mNetTrackers[netType] = new MobileDataStateTracker(netType,
407 mNetAttributes[netType].mName);
408 mNetTrackers[netType].startMonitoring(context, mHandler);
Robert Greenwaltc03fa502010-02-23 18:58:05 -0800409 if (noMobileData) {
Wink Savilleed9c02b2010-12-03 12:01:38 -0800410 if (DBG) log("tearing down Mobile networks due to setting");
Robert Greenwaltc03fa502010-02-23 18:58:05 -0800411 mNetTrackers[netType].teardown();
412 }
Robert Greenwalt5154ae762009-10-30 14:17:42 -0700413 break;
414 default:
Wink Savilleed9c02b2010-12-03 12:01:38 -0800415 loge("Trying to create a DataStateTracker for an unknown radio type " +
Robert Greenwalt5154ae762009-10-30 14:17:42 -0700416 mNetAttributes[netType].mRadio);
417 continue;
418 }
419 }
Robert Greenwaltd0e18ff2010-01-26 11:40:34 -0800420
Robert Greenwaltdfadaea2010-03-11 15:03:08 -0800421 mTethering = new Tethering(mContext, mHandler.getLooper());
Robert Greenwaltc9d5fb72010-02-25 12:29:30 -0800422 mTetheringConfigValid = (((mNetTrackers[ConnectivityManager.TYPE_MOBILE_DUN] != null) ||
423 !mTethering.isDunRequired()) &&
424 (mTethering.getTetherableUsbRegexs().length != 0 ||
Danica Chang6fdd0c62010-08-11 14:54:43 -0700425 mTethering.getTetherableWifiRegexs().length != 0 ||
426 mTethering.getTetherableBluetoothRegexs().length != 0) &&
Robert Greenwaltc9d5fb72010-02-25 12:29:30 -0800427 mTethering.getUpstreamIfaceRegexs().length != 0);
428
Robert Greenwalt4e8dfef2010-09-20 14:35:25 -0700429 if (DBG) {
430 mInetLog = new ArrayList();
431 }
Robert Greenwalt434203a2010-10-11 16:00:27 -0700432
433 mSettingsObserver = new SettingsObserver(mHandler, EVENT_APPLY_GLOBAL_HTTP_PROXY);
434 mSettingsObserver.observe(mContext);
Robert Greenwaltb7090d62010-12-02 11:31:00 -0800435
436 loadGlobalProxy();
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800437 }
438
Robert Greenwalt5154ae762009-10-30 14:17:42 -0700439
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800440 /**
Robert Greenwalt86e9e552009-07-16 17:21:39 -0700441 * Sets the preferred network.
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800442 * @param preference the new preference
443 */
Robert Greenwalt8dcc28b2010-09-23 10:05:56 -0700444 public void setNetworkPreference(int preference) {
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800445 enforceChangePermission();
Robert Greenwalt8dcc28b2010-09-23 10:05:56 -0700446
447 mHandler.sendMessage(mHandler.obtainMessage(EVENT_SET_NETWORK_PREFERENCE, preference, 0));
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800448 }
449
450 public int getNetworkPreference() {
451 enforceAccessPermission();
Robert Greenwalt8dcc28b2010-09-23 10:05:56 -0700452 int preference;
453 synchronized(this) {
454 preference = mNetworkPreference;
455 }
456 return preference;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800457 }
458
Robert Greenwalt8dcc28b2010-09-23 10:05:56 -0700459 private void handleSetNetworkPreference(int preference) {
460 if (ConnectivityManager.isNetworkTypeValid(preference) &&
461 mNetAttributes[preference] != null &&
462 mNetAttributes[preference].isDefault()) {
463 if (mNetworkPreference != preference) {
464 final ContentResolver cr = mContext.getContentResolver();
465 Settings.Secure.putInt(cr, Settings.Secure.NETWORK_PREFERENCE, preference);
466 synchronized(this) {
467 mNetworkPreference = preference;
468 }
469 enforcePreference();
470 }
471 }
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800472 }
Robert Greenwalt86e9e552009-07-16 17:21:39 -0700473
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800474 private int getPersistedNetworkPreference() {
475 final ContentResolver cr = mContext.getContentResolver();
476
477 final int networkPrefSetting = Settings.Secure
478 .getInt(cr, Settings.Secure.NETWORK_PREFERENCE, -1);
479 if (networkPrefSetting != -1) {
480 return networkPrefSetting;
481 }
482
483 return ConnectivityManager.DEFAULT_NETWORK_PREFERENCE;
484 }
Robert Greenwalt86e9e552009-07-16 17:21:39 -0700485
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800486 /**
Robert Greenwalt86e9e552009-07-16 17:21:39 -0700487 * Make the state of network connectivity conform to the preference settings
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800488 * In this method, we only tear down a non-preferred network. Establishing
489 * a connection to the preferred network is taken care of when we handle
490 * the disconnect event from the non-preferred network
491 * (see {@link #handleDisconnect(NetworkInfo)}).
492 */
493 private void enforcePreference() {
Robert Greenwalt42acef32009-08-12 16:08:25 -0700494 if (mNetTrackers[mNetworkPreference].getNetworkInfo().isConnected())
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800495 return;
496
Robert Greenwalt42acef32009-08-12 16:08:25 -0700497 if (!mNetTrackers[mNetworkPreference].isAvailable())
498 return;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800499
Robert Greenwalt42acef32009-08-12 16:08:25 -0700500 for (int t=0; t <= ConnectivityManager.MAX_RADIO_TYPE; t++) {
Robert Greenwalt5154ae762009-10-30 14:17:42 -0700501 if (t != mNetworkPreference && mNetTrackers[t] != null &&
Robert Greenwalt42acef32009-08-12 16:08:25 -0700502 mNetTrackers[t].getNetworkInfo().isConnected()) {
Robert Greenwaltec9fe462009-08-20 15:25:14 -0700503 if (DBG) {
Wink Savilleed9c02b2010-12-03 12:01:38 -0800504 log("tearing down " + mNetTrackers[t].getNetworkInfo() +
Robert Greenwaltec9fe462009-08-20 15:25:14 -0700505 " in enforcePreference");
506 }
Robert Greenwalt42acef32009-08-12 16:08:25 -0700507 teardown(mNetTrackers[t]);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800508 }
509 }
510 }
511
512 private boolean teardown(NetworkStateTracker netTracker) {
513 if (netTracker.teardown()) {
514 netTracker.setTeardownRequested(true);
515 return true;
516 } else {
517 return false;
518 }
519 }
520
521 /**
522 * Return NetworkInfo for the active (i.e., connected) network interface.
523 * It is assumed that at most one network is active at a time. If more
524 * than one is active, it is indeterminate which will be returned.
Robert Greenwalt86e9e552009-07-16 17:21:39 -0700525 * @return the info for the active network, or {@code null} if none is
526 * active
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800527 */
528 public NetworkInfo getActiveNetworkInfo() {
529 enforceAccessPermission();
Robert Greenwalt42acef32009-08-12 16:08:25 -0700530 for (int type=0; type <= ConnectivityManager.MAX_NETWORK_TYPE; type++) {
Robert Greenwalt5154ae762009-10-30 14:17:42 -0700531 if (mNetAttributes[type] == null || !mNetAttributes[type].isDefault()) {
Robert Greenwalt42acef32009-08-12 16:08:25 -0700532 continue;
533 }
534 NetworkStateTracker t = mNetTrackers[type];
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800535 NetworkInfo info = t.getNetworkInfo();
536 if (info.isConnected()) {
Wink Savilleed9c02b2010-12-03 12:01:38 -0800537 if (DBG && type != mActiveDefaultNetwork) {
538 loge("connected default network is not mActiveDefaultNetwork!");
539 }
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800540 return info;
541 }
542 }
543 return null;
544 }
545
546 public NetworkInfo getNetworkInfo(int networkType) {
547 enforceAccessPermission();
548 if (ConnectivityManager.isNetworkTypeValid(networkType)) {
549 NetworkStateTracker t = mNetTrackers[networkType];
550 if (t != null)
551 return t.getNetworkInfo();
552 }
553 return null;
554 }
555
556 public NetworkInfo[] getAllNetworkInfo() {
557 enforceAccessPermission();
Robert Greenwalt5154ae762009-10-30 14:17:42 -0700558 NetworkInfo[] result = new NetworkInfo[mNetworksDefined];
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800559 int i = 0;
560 for (NetworkStateTracker t : mNetTrackers) {
Robert Greenwalt5154ae762009-10-30 14:17:42 -0700561 if(t != null) result[i++] = t.getNetworkInfo();
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800562 }
563 return result;
564 }
565
Robert Greenwaltd192dad2010-09-14 09:18:02 -0700566 /**
567 * Return LinkProperties for the active (i.e., connected) default
568 * network interface. It is assumed that at most one default network
569 * is active at a time. If more than one is active, it is indeterminate
570 * which will be returned.
571 * @return the ip properties for the active network, or {@code null} if
572 * none is active
573 */
574 public LinkProperties getActiveLinkProperties() {
575 enforceAccessPermission();
576 for (int type=0; type <= ConnectivityManager.MAX_NETWORK_TYPE; type++) {
577 if (mNetAttributes[type] == null || !mNetAttributes[type].isDefault()) {
578 continue;
579 }
580 NetworkStateTracker t = mNetTrackers[type];
581 NetworkInfo info = t.getNetworkInfo();
582 if (info.isConnected()) {
583 return t.getLinkProperties();
584 }
585 }
586 return null;
587 }
588
589 public LinkProperties getLinkProperties(int networkType) {
590 enforceAccessPermission();
591 if (ConnectivityManager.isNetworkTypeValid(networkType)) {
592 NetworkStateTracker t = mNetTrackers[networkType];
593 if (t != null) return t.getLinkProperties();
594 }
595 return null;
596 }
597
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800598 public boolean setRadios(boolean turnOn) {
599 boolean result = true;
600 enforceChangePermission();
601 for (NetworkStateTracker t : mNetTrackers) {
Robert Greenwalt5154ae762009-10-30 14:17:42 -0700602 if (t != null) result = t.setRadio(turnOn) && result;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800603 }
604 return result;
605 }
606
607 public boolean setRadio(int netType, boolean turnOn) {
608 enforceChangePermission();
609 if (!ConnectivityManager.isNetworkTypeValid(netType)) {
610 return false;
611 }
612 NetworkStateTracker tracker = mNetTrackers[netType];
613 return tracker != null && tracker.setRadio(turnOn);
614 }
615
Robert Greenwalt9c75d4a2009-09-27 17:27:04 -0700616 /**
617 * Used to notice when the calling process dies so we can self-expire
618 *
619 * Also used to know if the process has cleaned up after itself when
620 * our auto-expire timer goes off. The timer has a link to an object.
621 *
622 */
Robert Greenwalt42acef32009-08-12 16:08:25 -0700623 private class FeatureUser implements IBinder.DeathRecipient {
624 int mNetworkType;
625 String mFeature;
626 IBinder mBinder;
627 int mPid;
628 int mUid;
Robert Greenwaltb9285352009-12-21 18:24:07 -0800629 long mCreateTime;
Robert Greenwalt42acef32009-08-12 16:08:25 -0700630
631 FeatureUser(int type, String feature, IBinder binder) {
632 super();
633 mNetworkType = type;
634 mFeature = feature;
635 mBinder = binder;
636 mPid = getCallingPid();
637 mUid = getCallingUid();
Robert Greenwaltb9285352009-12-21 18:24:07 -0800638 mCreateTime = System.currentTimeMillis();
Robert Greenwalt9c75d4a2009-09-27 17:27:04 -0700639
Robert Greenwalt42acef32009-08-12 16:08:25 -0700640 try {
641 mBinder.linkToDeath(this, 0);
642 } catch (RemoteException e) {
643 binderDied();
644 }
645 }
646
647 void unlinkDeathRecipient() {
648 mBinder.unlinkToDeath(this, 0);
649 }
650
651 public void binderDied() {
Wink Savilleed9c02b2010-12-03 12:01:38 -0800652 log("ConnectivityService FeatureUser binderDied(" +
Robert Greenwaltb9285352009-12-21 18:24:07 -0800653 mNetworkType + ", " + mFeature + ", " + mBinder + "), created " +
654 (System.currentTimeMillis() - mCreateTime) + " mSec ago");
Robert Greenwalt9c75d4a2009-09-27 17:27:04 -0700655 stopUsingNetworkFeature(this, false);
Robert Greenwalt42acef32009-08-12 16:08:25 -0700656 }
657
Robert Greenwalt9c75d4a2009-09-27 17:27:04 -0700658 public void expire() {
Wink Savilleed9c02b2010-12-03 12:01:38 -0800659 log("ConnectivityService FeatureUser expire(" +
Robert Greenwaltb9285352009-12-21 18:24:07 -0800660 mNetworkType + ", " + mFeature + ", " + mBinder +"), created " +
661 (System.currentTimeMillis() - mCreateTime) + " mSec ago");
Robert Greenwalt9c75d4a2009-09-27 17:27:04 -0700662 stopUsingNetworkFeature(this, false);
663 }
Robert Greenwaltb9285352009-12-21 18:24:07 -0800664
665 public String toString() {
666 return "FeatureUser("+mNetworkType+","+mFeature+","+mPid+","+mUid+"), created " +
667 (System.currentTimeMillis() - mCreateTime) + " mSec ago";
668 }
Robert Greenwalt42acef32009-08-12 16:08:25 -0700669 }
670
Robert Greenwalt9c75d4a2009-09-27 17:27:04 -0700671 // javadoc from interface
Robert Greenwalt42acef32009-08-12 16:08:25 -0700672 public int startUsingNetworkFeature(int networkType, String feature,
673 IBinder binder) {
674 if (DBG) {
Wink Savilleed9c02b2010-12-03 12:01:38 -0800675 log("startUsingNetworkFeature for net " + networkType + ": " + feature);
Robert Greenwalt42acef32009-08-12 16:08:25 -0700676 }
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800677 enforceChangePermission();
Robert Greenwalt5154ae762009-10-30 14:17:42 -0700678 if (!ConnectivityManager.isNetworkTypeValid(networkType) ||
679 mNetAttributes[networkType] == null) {
Robert Greenwalt42acef32009-08-12 16:08:25 -0700680 return Phone.APN_REQUEST_FAILED;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800681 }
Robert Greenwalt42acef32009-08-12 16:08:25 -0700682
Robert Greenwalt9c75d4a2009-09-27 17:27:04 -0700683 FeatureUser f = new FeatureUser(networkType, feature, binder);
Robert Greenwalt42acef32009-08-12 16:08:25 -0700684
685 // TODO - move this into the MobileDataStateTracker
686 int usedNetworkType = networkType;
687 if(networkType == ConnectivityManager.TYPE_MOBILE) {
Robert Greenwaltc03fa502010-02-23 18:58:05 -0800688 if (!getMobileDataEnabled()) {
Wink Savilleed9c02b2010-12-03 12:01:38 -0800689 if (DBG) log("requested special network with data disabled - rejected");
Robert Greenwaltc03fa502010-02-23 18:58:05 -0800690 return Phone.APN_TYPE_NOT_AVAILABLE;
691 }
Robert Greenwalt42acef32009-08-12 16:08:25 -0700692 if (TextUtils.equals(feature, Phone.FEATURE_ENABLE_MMS)) {
693 usedNetworkType = ConnectivityManager.TYPE_MOBILE_MMS;
694 } else if (TextUtils.equals(feature, Phone.FEATURE_ENABLE_SUPL)) {
695 usedNetworkType = ConnectivityManager.TYPE_MOBILE_SUPL;
696 } else if (TextUtils.equals(feature, Phone.FEATURE_ENABLE_DUN)) {
697 usedNetworkType = ConnectivityManager.TYPE_MOBILE_DUN;
698 } else if (TextUtils.equals(feature, Phone.FEATURE_ENABLE_HIPRI)) {
699 usedNetworkType = ConnectivityManager.TYPE_MOBILE_HIPRI;
700 }
701 }
702 NetworkStateTracker network = mNetTrackers[usedNetworkType];
703 if (network != null) {
704 if (usedNetworkType != networkType) {
705 Integer currentPid = new Integer(getCallingPid());
706
707 NetworkStateTracker radio = mNetTrackers[networkType];
708 NetworkInfo ni = network.getNetworkInfo();
709
710 if (ni.isAvailable() == false) {
Wink Savilleed9c02b2010-12-03 12:01:38 -0800711 if (DBG) log("special network not available");
Robert Greenwalt42acef32009-08-12 16:08:25 -0700712 return Phone.APN_TYPE_NOT_AVAILABLE;
713 }
714
Robert Greenwalt9c75d4a2009-09-27 17:27:04 -0700715 synchronized(this) {
716 mFeatureUsers.add(f);
717 if (!mNetRequestersPids[usedNetworkType].contains(currentPid)) {
718 // this gets used for per-pid dns when connected
719 mNetRequestersPids[usedNetworkType].add(currentPid);
720 }
Robert Greenwalt42acef32009-08-12 16:08:25 -0700721 }
Robert Greenwalt8dcc28b2010-09-23 10:05:56 -0700722 mHandler.sendMessageDelayed(mHandler.obtainMessage(EVENT_RESTORE_DEFAULT_NETWORK,
Robert Greenwalt9c75d4a2009-09-27 17:27:04 -0700723 f), getRestoreDefaultNetworkDelay());
724
Robert Greenwalt42acef32009-08-12 16:08:25 -0700725
Robert Greenwalta64bf832009-08-19 20:19:33 -0700726 if ((ni.isConnectedOrConnecting() == true) &&
727 !network.isTeardownRequested()) {
Robert Greenwalt42acef32009-08-12 16:08:25 -0700728 if (ni.isConnected() == true) {
729 // add the pid-specific dns
Robert Greenwaltc76b8fa2010-07-23 15:46:26 -0700730 handleDnsConfigurationChange(networkType);
Wink Savilleed9c02b2010-12-03 12:01:38 -0800731 if (DBG) log("special network already active");
Robert Greenwalt42acef32009-08-12 16:08:25 -0700732 return Phone.APN_ALREADY_ACTIVE;
733 }
Wink Savilleed9c02b2010-12-03 12:01:38 -0800734 if (DBG) log("special network already connecting");
Robert Greenwalt42acef32009-08-12 16:08:25 -0700735 return Phone.APN_REQUEST_STARTED;
736 }
737
738 // check if the radio in play can make another contact
739 // assume if cannot for now
740
Wink Savilleed9c02b2010-12-03 12:01:38 -0800741 if (DBG) log("reconnecting to special network");
Robert Greenwalt42acef32009-08-12 16:08:25 -0700742 network.reconnect();
743 return Phone.APN_REQUEST_STARTED;
744 } else {
Robert Greenwalt02648a42010-05-18 10:52:51 -0700745 return -1;
Robert Greenwalt42acef32009-08-12 16:08:25 -0700746 }
747 }
748 return Phone.APN_TYPE_NOT_AVAILABLE;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800749 }
750
Robert Greenwalt9c75d4a2009-09-27 17:27:04 -0700751 // javadoc from interface
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800752 public int stopUsingNetworkFeature(int networkType, String feature) {
Robert Greenwaltb8f16342009-10-06 17:52:40 -0700753 enforceChangePermission();
754
Robert Greenwalt9c75d4a2009-09-27 17:27:04 -0700755 int pid = getCallingPid();
756 int uid = getCallingUid();
757
758 FeatureUser u = null;
759 boolean found = false;
760
761 synchronized(this) {
762 for (int i = 0; i < mFeatureUsers.size() ; i++) {
763 u = (FeatureUser)mFeatureUsers.get(i);
764 if (uid == u.mUid && pid == u.mPid &&
765 networkType == u.mNetworkType &&
766 TextUtils.equals(feature, u.mFeature)) {
767 found = true;
768 break;
769 }
770 }
771 }
772 if (found && u != null) {
773 // stop regardless of how many other time this proc had called start
774 return stopUsingNetworkFeature(u, true);
775 } else {
776 // none found!
Wink Savilleed9c02b2010-12-03 12:01:38 -0800777 if (DBG) log("ignoring stopUsingNetworkFeature - not a live request");
Robert Greenwalt9c75d4a2009-09-27 17:27:04 -0700778 return 1;
779 }
Robert Greenwalt42acef32009-08-12 16:08:25 -0700780 }
781
Robert Greenwalt9c75d4a2009-09-27 17:27:04 -0700782 private int stopUsingNetworkFeature(FeatureUser u, boolean ignoreDups) {
783 int networkType = u.mNetworkType;
784 String feature = u.mFeature;
785 int pid = u.mPid;
786 int uid = u.mUid;
787
788 NetworkStateTracker tracker = null;
789 boolean callTeardown = false; // used to carry our decision outside of sync block
790
Robert Greenwalt42acef32009-08-12 16:08:25 -0700791 if (DBG) {
Wink Savilleed9c02b2010-12-03 12:01:38 -0800792 log("stopUsingNetworkFeature for net " + networkType +
Robert Greenwalt42acef32009-08-12 16:08:25 -0700793 ": " + feature);
794 }
Robert Greenwaltb8f16342009-10-06 17:52:40 -0700795
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800796 if (!ConnectivityManager.isNetworkTypeValid(networkType)) {
797 return -1;
798 }
Robert Greenwalt42acef32009-08-12 16:08:25 -0700799
Robert Greenwalt9c75d4a2009-09-27 17:27:04 -0700800 // need to link the mFeatureUsers list with the mNetRequestersPids state in this
801 // sync block
802 synchronized(this) {
803 // check if this process still has an outstanding start request
804 if (!mFeatureUsers.contains(u)) {
Wink Savilleed9c02b2010-12-03 12:01:38 -0800805 if (DBG) log("ignoring - this process has no outstanding requests");
Robert Greenwalt42acef32009-08-12 16:08:25 -0700806 return 1;
807 }
Robert Greenwalt9c75d4a2009-09-27 17:27:04 -0700808 u.unlinkDeathRecipient();
809 mFeatureUsers.remove(mFeatureUsers.indexOf(u));
810 // If we care about duplicate requests, check for that here.
811 //
812 // This is done to support the extension of a request - the app
813 // can request we start the network feature again and renew the
814 // auto-shutoff delay. Normal "stop" calls from the app though
815 // do not pay attention to duplicate requests - in effect the
816 // API does not refcount and a single stop will counter multiple starts.
817 if (ignoreDups == false) {
818 for (int i = 0; i < mFeatureUsers.size() ; i++) {
819 FeatureUser x = (FeatureUser)mFeatureUsers.get(i);
820 if (x.mUid == u.mUid && x.mPid == u.mPid &&
821 x.mNetworkType == u.mNetworkType &&
822 TextUtils.equals(x.mFeature, u.mFeature)) {
Wink Savilleed9c02b2010-12-03 12:01:38 -0800823 if (DBG) log("ignoring stopUsingNetworkFeature as dup is found");
Robert Greenwalt9c75d4a2009-09-27 17:27:04 -0700824 return 1;
825 }
826 }
Robert Greenwalt42acef32009-08-12 16:08:25 -0700827 }
Robert Greenwalt9c75d4a2009-09-27 17:27:04 -0700828
829 // TODO - move to MobileDataStateTracker
830 int usedNetworkType = networkType;
831 if (networkType == ConnectivityManager.TYPE_MOBILE) {
832 if (TextUtils.equals(feature, Phone.FEATURE_ENABLE_MMS)) {
833 usedNetworkType = ConnectivityManager.TYPE_MOBILE_MMS;
834 } else if (TextUtils.equals(feature, Phone.FEATURE_ENABLE_SUPL)) {
835 usedNetworkType = ConnectivityManager.TYPE_MOBILE_SUPL;
836 } else if (TextUtils.equals(feature, Phone.FEATURE_ENABLE_DUN)) {
837 usedNetworkType = ConnectivityManager.TYPE_MOBILE_DUN;
838 } else if (TextUtils.equals(feature, Phone.FEATURE_ENABLE_HIPRI)) {
839 usedNetworkType = ConnectivityManager.TYPE_MOBILE_HIPRI;
840 }
841 }
842 tracker = mNetTrackers[usedNetworkType];
Robert Greenwalt5154ae762009-10-30 14:17:42 -0700843 if (tracker == null) {
Wink Savilleed9c02b2010-12-03 12:01:38 -0800844 if (DBG) log("ignoring - no known tracker for net type " + usedNetworkType);
Robert Greenwalt5154ae762009-10-30 14:17:42 -0700845 return -1;
846 }
847 if (usedNetworkType != networkType) {
Robert Greenwalt9c75d4a2009-09-27 17:27:04 -0700848 Integer currentPid = new Integer(pid);
Robert Greenwalt9c75d4a2009-09-27 17:27:04 -0700849 mNetRequestersPids[usedNetworkType].remove(currentPid);
Robert Greenwalt421c72b2009-12-17 14:54:59 -0800850 reassessPidDns(pid, true);
Robert Greenwalt9c75d4a2009-09-27 17:27:04 -0700851 if (mNetRequestersPids[usedNetworkType].size() != 0) {
Wink Savilleed9c02b2010-12-03 12:01:38 -0800852 if (DBG) log("not tearing down special network - " +
Robert Greenwalt9c75d4a2009-09-27 17:27:04 -0700853 "others still using it");
854 return 1;
855 }
856 callTeardown = true;
857 }
858 }
Wink Savilleed9c02b2010-12-03 12:01:38 -0800859 if (DBG) log("Doing network teardown");
Robert Greenwalt9c75d4a2009-09-27 17:27:04 -0700860 if (callTeardown) {
861 tracker.teardown();
Robert Greenwalt42acef32009-08-12 16:08:25 -0700862 return 1;
863 } else {
Robert Greenwalt02648a42010-05-18 10:52:51 -0700864 return -1;
Robert Greenwalt42acef32009-08-12 16:08:25 -0700865 }
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800866 }
867
868 /**
Robert Greenwalt585ac0f2010-08-27 09:24:29 -0700869 * @deprecated use requestRouteToHostAddress instead
870 *
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800871 * Ensure that a network route exists to deliver traffic to the specified
872 * host via the specified network interface.
Robert Greenwalt86e9e552009-07-16 17:21:39 -0700873 * @param networkType the type of the network over which traffic to the
874 * specified host is to be routed
875 * @param hostAddress the IP address of the host to which the route is
876 * desired
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800877 * @return {@code true} on success, {@code false} on failure
878 */
879 public boolean requestRouteToHost(int networkType, int hostAddress) {
Robert Greenwalt585ac0f2010-08-27 09:24:29 -0700880 InetAddress inetAddress = NetworkUtils.intToInetAddress(hostAddress);
881
882 if (inetAddress == null) {
883 return false;
884 }
885
886 return requestRouteToHostAddress(networkType, inetAddress.getAddress());
887 }
888
889 /**
890 * Ensure that a network route exists to deliver traffic to the specified
891 * host via the specified network interface.
892 * @param networkType the type of the network over which traffic to the
893 * specified host is to be routed
894 * @param hostAddress the IP address of the host to which the route is
895 * desired
896 * @return {@code true} on success, {@code false} on failure
897 */
898 public boolean requestRouteToHostAddress(int networkType, byte[] hostAddress) {
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800899 enforceChangePermission();
900 if (!ConnectivityManager.isNetworkTypeValid(networkType)) {
901 return false;
902 }
903 NetworkStateTracker tracker = mNetTrackers[networkType];
Robert Greenwalt8206ff32009-09-10 15:06:20 -0700904
Robert Greenwalt5154ae762009-10-30 14:17:42 -0700905 if (tracker == null || !tracker.getNetworkInfo().isConnected() ||
906 tracker.isTeardownRequested()) {
Robert Greenwalt8206ff32009-09-10 15:06:20 -0700907 if (DBG) {
Wink Savilleed9c02b2010-12-03 12:01:38 -0800908 log("requestRouteToHostAddress on down network " +
Robert Greenwalt585ac0f2010-08-27 09:24:29 -0700909 "(" + networkType + ") - dropped");
Robert Greenwalt8206ff32009-09-10 15:06:20 -0700910 }
911 return false;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800912 }
Robert Greenwalt47f69fe2010-06-15 15:43:39 -0700913 try {
Robert Greenwalt585ac0f2010-08-27 09:24:29 -0700914 InetAddress addr = InetAddress.getByAddress(hostAddress);
Robert Greenwalt47f69fe2010-06-15 15:43:39 -0700915 return addHostRoute(tracker, addr);
916 } catch (UnknownHostException e) {}
917 return false;
Irfan Sheriffd649c122010-06-09 15:39:36 -0700918 }
919
920 /**
921 * Ensure that a network route exists to deliver traffic to the specified
922 * host via the mobile data network.
923 * @param hostAddress the IP address of the host to which the route is desired,
924 * in network byte order.
Robert Greenwalt47f69fe2010-06-15 15:43:39 -0700925 * TODO - deprecate
Irfan Sheriffd649c122010-06-09 15:39:36 -0700926 * @return {@code true} on success, {@code false} on failure
927 */
Robert Greenwalt47f69fe2010-06-15 15:43:39 -0700928 private boolean addHostRoute(NetworkStateTracker nt, InetAddress hostAddress) {
Irfan Sheriffd649c122010-06-09 15:39:36 -0700929 if (nt.getNetworkInfo().getType() == ConnectivityManager.TYPE_WIFI) {
930 return false;
931 }
932
Robert Greenwalt37e65eb2010-08-30 10:56:47 -0700933 LinkProperties p = nt.getLinkProperties();
Robert Greenwalt47f69fe2010-06-15 15:43:39 -0700934 if (p == null) return false;
935 String interfaceName = p.getInterfaceName();
Irfan Sheriffd649c122010-06-09 15:39:36 -0700936
937 if (DBG) {
Wink Savilleed9c02b2010-12-03 12:01:38 -0800938 log("Requested host route to " + hostAddress + "(" + interfaceName + ")");
Irfan Sheriffd649c122010-06-09 15:39:36 -0700939 }
Robert Greenwalt47f69fe2010-06-15 15:43:39 -0700940 if (interfaceName != null) {
Robert Greenwalt585ac0f2010-08-27 09:24:29 -0700941 return NetworkUtils.addHostRoute(interfaceName, hostAddress, null);
Irfan Sheriffd649c122010-06-09 15:39:36 -0700942 } else {
Wink Savilleed9c02b2010-12-03 12:01:38 -0800943 if (DBG) loge("addHostRoute failed due to null interface name");
Irfan Sheriffd649c122010-06-09 15:39:36 -0700944 return false;
945 }
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800946 }
947
948 /**
949 * @see ConnectivityManager#getBackgroundDataSetting()
950 */
951 public boolean getBackgroundDataSetting() {
952 return Settings.Secure.getInt(mContext.getContentResolver(),
953 Settings.Secure.BACKGROUND_DATA, 1) == 1;
954 }
Robert Greenwalt86e9e552009-07-16 17:21:39 -0700955
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800956 /**
957 * @see ConnectivityManager#setBackgroundDataSetting(boolean)
958 */
959 public void setBackgroundDataSetting(boolean allowBackgroundDataUsage) {
960 mContext.enforceCallingOrSelfPermission(
961 android.Manifest.permission.CHANGE_BACKGROUND_DATA_SETTING,
962 "ConnectivityService");
Robert Greenwalt86e9e552009-07-16 17:21:39 -0700963
Robert Greenwalt8dcc28b2010-09-23 10:05:56 -0700964 mHandler.sendMessage(mHandler.obtainMessage(EVENT_SET_BACKGROUND_DATA,
965 (allowBackgroundDataUsage ? ENABLED : DISABLED), 0));
966 }
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800967
Robert Greenwalt8dcc28b2010-09-23 10:05:56 -0700968 private void handleSetBackgroundData(boolean enabled) {
969 if (enabled != getBackgroundDataSetting()) {
970 Settings.Secure.putInt(mContext.getContentResolver(),
971 Settings.Secure.BACKGROUND_DATA, enabled ? 1 : 0);
972 Intent broadcast = new Intent(
973 ConnectivityManager.ACTION_BACKGROUND_DATA_SETTING_CHANGED);
974 mContext.sendBroadcast(broadcast);
975 }
Robert Greenwalt86e9e552009-07-16 17:21:39 -0700976 }
977
Robert Greenwaltc03fa502010-02-23 18:58:05 -0800978 /**
979 * @see ConnectivityManager#getMobileDataEnabled()
980 */
981 public boolean getMobileDataEnabled() {
982 enforceAccessPermission();
983 boolean retVal = Settings.Secure.getInt(mContext.getContentResolver(),
984 Settings.Secure.MOBILE_DATA, 1) == 1;
Wink Savilleed9c02b2010-12-03 12:01:38 -0800985 if (DBG) log("getMobileDataEnabled returning " + retVal);
Robert Greenwaltc03fa502010-02-23 18:58:05 -0800986 return retVal;
987 }
988
989 /**
990 * @see ConnectivityManager#setMobileDataEnabled(boolean)
991 */
Robert Greenwalt8dcc28b2010-09-23 10:05:56 -0700992 public void setMobileDataEnabled(boolean enabled) {
Robert Greenwaltc03fa502010-02-23 18:58:05 -0800993 enforceChangePermission();
Wink Savilleed9c02b2010-12-03 12:01:38 -0800994 if (DBG) log("setMobileDataEnabled(" + enabled + ")");
Robert Greenwaltc03fa502010-02-23 18:58:05 -0800995
Robert Greenwalt8dcc28b2010-09-23 10:05:56 -0700996 mHandler.sendMessage(mHandler.obtainMessage(EVENT_SET_MOBILE_DATA,
997 (enabled ? ENABLED : DISABLED), 0));
998 }
999
1000 private void handleSetMobileData(boolean enabled) {
Robert Greenwaltc03fa502010-02-23 18:58:05 -08001001 if (getMobileDataEnabled() == enabled) return;
1002
1003 Settings.Secure.putInt(mContext.getContentResolver(),
1004 Settings.Secure.MOBILE_DATA, enabled ? 1 : 0);
1005
1006 if (enabled) {
1007 if (mNetTrackers[ConnectivityManager.TYPE_MOBILE] != null) {
Robert Greenwalt8dcc28b2010-09-23 10:05:56 -07001008 if (DBG) {
Wink Savilleed9c02b2010-12-03 12:01:38 -08001009 log("starting up " + mNetTrackers[ConnectivityManager.TYPE_MOBILE]);
Robert Greenwalt8dcc28b2010-09-23 10:05:56 -07001010 }
Robert Greenwaltc03fa502010-02-23 18:58:05 -08001011 mNetTrackers[ConnectivityManager.TYPE_MOBILE].reconnect();
1012 }
1013 } else {
1014 for (NetworkStateTracker nt : mNetTrackers) {
1015 if (nt == null) continue;
1016 int netType = nt.getNetworkInfo().getType();
1017 if (mNetAttributes[netType].mRadio == ConnectivityManager.TYPE_MOBILE) {
Wink Savilleed9c02b2010-12-03 12:01:38 -08001018 if (DBG) log("tearing down " + nt);
Robert Greenwaltc03fa502010-02-23 18:58:05 -08001019 nt.teardown();
1020 }
1021 }
1022 }
1023 }
1024
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001025 private int getNumConnectedNetworks() {
1026 int numConnectedNets = 0;
1027
1028 for (NetworkStateTracker nt : mNetTrackers) {
Robert Greenwalt5154ae762009-10-30 14:17:42 -07001029 if (nt != null && nt.getNetworkInfo().isConnected() &&
Robert Greenwalt86e9e552009-07-16 17:21:39 -07001030 !nt.isTeardownRequested()) {
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001031 ++numConnectedNets;
1032 }
1033 }
1034 return numConnectedNets;
1035 }
1036
1037 private void enforceAccessPermission() {
Robert Greenwalt86e9e552009-07-16 17:21:39 -07001038 mContext.enforceCallingOrSelfPermission(
1039 android.Manifest.permission.ACCESS_NETWORK_STATE,
1040 "ConnectivityService");
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001041 }
1042
1043 private void enforceChangePermission() {
Robert Greenwalt86e9e552009-07-16 17:21:39 -07001044 mContext.enforceCallingOrSelfPermission(
1045 android.Manifest.permission.CHANGE_NETWORK_STATE,
1046 "ConnectivityService");
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001047 }
1048
Robert Greenwaltd0e18ff2010-01-26 11:40:34 -08001049 // TODO Make this a special check when it goes public
1050 private void enforceTetherChangePermission() {
1051 mContext.enforceCallingOrSelfPermission(
1052 android.Manifest.permission.CHANGE_NETWORK_STATE,
1053 "ConnectivityService");
1054 }
1055
Robert Greenwalt2a091d72010-02-11 18:18:40 -08001056 private void enforceTetherAccessPermission() {
1057 mContext.enforceCallingOrSelfPermission(
1058 android.Manifest.permission.ACCESS_NETWORK_STATE,
1059 "ConnectivityService");
1060 }
1061
Robert Greenwalt14f2ef42010-06-15 12:19:37 -07001062 private void enforceConnectivityInternalPermission() {
1063 mContext.enforceCallingOrSelfPermission(
1064 android.Manifest.permission.CONNECTIVITY_INTERNAL,
1065 "ConnectivityService");
1066 }
1067
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001068 /**
Robert Greenwalt86e9e552009-07-16 17:21:39 -07001069 * Handle a {@code DISCONNECTED} event. If this pertains to the non-active
1070 * network, we ignore it. If it is for the active network, we send out a
1071 * broadcast. But first, we check whether it might be possible to connect
1072 * to a different network.
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001073 * @param info the {@code NetworkInfo} for the network
1074 */
1075 private void handleDisconnect(NetworkInfo info) {
1076
Robert Greenwalt42acef32009-08-12 16:08:25 -07001077 int prevNetType = info.getType();
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001078
Robert Greenwalt42acef32009-08-12 16:08:25 -07001079 mNetTrackers[prevNetType].setTeardownRequested(false);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001080 /*
1081 * If the disconnected network is not the active one, then don't report
1082 * this as a loss of connectivity. What probably happened is that we're
1083 * getting the disconnect for a network that we explicitly disabled
1084 * in accordance with network preference policies.
1085 */
Robert Greenwalt42acef32009-08-12 16:08:25 -07001086 if (!mNetAttributes[prevNetType].isDefault()) {
1087 List pids = mNetRequestersPids[prevNetType];
1088 for (int i = 0; i<pids.size(); i++) {
1089 Integer pid = (Integer)pids.get(i);
1090 // will remove them because the net's no longer connected
1091 // need to do this now as only now do we know the pids and
1092 // can properly null things that are no longer referenced.
1093 reassessPidDns(pid.intValue(), false);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001094 }
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001095 }
1096
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001097 Intent intent = new Intent(ConnectivityManager.CONNECTIVITY_ACTION);
1098 intent.putExtra(ConnectivityManager.EXTRA_NETWORK_INFO, info);
1099 if (info.isFailover()) {
1100 intent.putExtra(ConnectivityManager.EXTRA_IS_FAILOVER, true);
1101 info.setFailover(false);
1102 }
1103 if (info.getReason() != null) {
1104 intent.putExtra(ConnectivityManager.EXTRA_REASON, info.getReason());
1105 }
1106 if (info.getExtraInfo() != null) {
Robert Greenwalt86e9e552009-07-16 17:21:39 -07001107 intent.putExtra(ConnectivityManager.EXTRA_EXTRA_INFO,
1108 info.getExtraInfo());
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001109 }
Robert Greenwalt42acef32009-08-12 16:08:25 -07001110
Robert Greenwaltcc4b4012010-01-25 17:54:29 -08001111 NetworkStateTracker newNet = null;
1112 if (mNetAttributes[prevNetType].isDefault()) {
1113 newNet = tryFailover(prevNetType);
1114 if (newNet != null) {
1115 NetworkInfo switchTo = newNet.getNetworkInfo();
Robert Greenwalt029be812010-09-20 18:01:43 -07001116 if (!switchTo.isConnected()) {
Wink Savilleed9c02b2010-12-03 12:01:38 -08001117 // if the other net is connected they've already reset this and perhaps even
1118 // gotten a positive report we don't want to overwrite, but if not we need to
1119 // clear this now to turn our cellular sig strength white
Robert Greenwalt029be812010-09-20 18:01:43 -07001120 mDefaultInetConditionPublished = 0;
1121 }
Robert Greenwaltcc4b4012010-01-25 17:54:29 -08001122 intent.putExtra(ConnectivityManager.EXTRA_OTHER_NETWORK_INFO, switchTo);
1123 } else {
Robert Greenwalt029be812010-09-20 18:01:43 -07001124 mDefaultInetConditionPublished = 0; // we're not connected anymore
Robert Greenwaltcc4b4012010-01-25 17:54:29 -08001125 intent.putExtra(ConnectivityManager.EXTRA_NO_CONNECTIVITY, true);
1126 }
Robert Greenwaltda03c4e2010-01-20 19:29:41 -08001127 }
Robert Greenwalt029be812010-09-20 18:01:43 -07001128 intent.putExtra(ConnectivityManager.EXTRA_INET_CONDITION, mDefaultInetConditionPublished);
Robert Greenwaltda03c4e2010-01-20 19:29:41 -08001129 // do this before we broadcast the change
Robert Greenwaltc76b8fa2010-07-23 15:46:26 -07001130 handleConnectivityChange(prevNetType);
Robert Greenwaltda03c4e2010-01-20 19:29:41 -08001131
1132 sendStickyBroadcast(intent);
1133 /*
1134 * If the failover network is already connected, then immediately send
1135 * out a followup broadcast indicating successful failover
1136 */
1137 if (newNet != null && newNet.getNetworkInfo().isConnected()) {
1138 sendConnectedBroadcast(newNet.getNetworkInfo());
1139 }
1140 }
1141
Robert Greenwaltcc4b4012010-01-25 17:54:29 -08001142 // returns null if no failover available
Robert Greenwaltda03c4e2010-01-20 19:29:41 -08001143 private NetworkStateTracker tryFailover(int prevNetType) {
Robert Greenwalt42acef32009-08-12 16:08:25 -07001144 /*
1145 * If this is a default network, check if other defaults are available
1146 * or active
1147 */
1148 NetworkStateTracker newNet = null;
1149 if (mNetAttributes[prevNetType].isDefault()) {
Robert Greenwalt42acef32009-08-12 16:08:25 -07001150 if (mActiveDefaultNetwork == prevNetType) {
1151 mActiveDefaultNetwork = -1;
1152 }
1153
1154 int newType = -1;
1155 int newPriority = -1;
Robert Greenwalt35429592010-02-25 12:04:29 -08001156 boolean noMobileData = !getMobileDataEnabled();
Robert Greenwaltda03c4e2010-01-20 19:29:41 -08001157 for (int checkType=0; checkType <= ConnectivityManager.MAX_NETWORK_TYPE; checkType++) {
Robert Greenwalt5154ae762009-10-30 14:17:42 -07001158 if (checkType == prevNetType) continue;
1159 if (mNetAttributes[checkType] == null) continue;
Robert Greenwalt35429592010-02-25 12:04:29 -08001160 if (mNetAttributes[checkType].mRadio == ConnectivityManager.TYPE_MOBILE &&
1161 noMobileData) {
Wink Savilleed9c02b2010-12-03 12:01:38 -08001162 loge("not failing over to mobile type " + checkType +
Robert Greenwalt572172b2010-10-08 16:35:52 -07001163 " because Mobile Data Disabled");
Robert Greenwalt35429592010-02-25 12:04:29 -08001164 continue;
1165 }
Robert Greenwalt42acef32009-08-12 16:08:25 -07001166 if (mNetAttributes[checkType].isDefault()) {
1167 /* TODO - if we have multiple nets we could use
1168 * we may want to put more thought into which we choose
1169 */
1170 if (checkType == mNetworkPreference) {
1171 newType = checkType;
1172 break;
1173 }
Robert Greenwalt5154ae762009-10-30 14:17:42 -07001174 if (mNetAttributes[checkType].mPriority > newPriority) {
Robert Greenwalt42acef32009-08-12 16:08:25 -07001175 newType = checkType;
Robert Greenwalt5154ae762009-10-30 14:17:42 -07001176 newPriority = mNetAttributes[newType].mPriority;
Robert Greenwalt42acef32009-08-12 16:08:25 -07001177 }
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001178 }
1179 }
Robert Greenwalt42acef32009-08-12 16:08:25 -07001180
1181 if (newType != -1) {
1182 newNet = mNetTrackers[newType];
1183 /**
1184 * See if the other network is available to fail over to.
1185 * If is not available, we enable it anyway, so that it
1186 * will be able to connect when it does become available,
1187 * but we report a total loss of connectivity rather than
1188 * report that we are attempting to fail over.
1189 */
1190 if (newNet.isAvailable()) {
1191 NetworkInfo switchTo = newNet.getNetworkInfo();
1192 switchTo.setFailover(true);
Robert Greenwalta64bf832009-08-19 20:19:33 -07001193 if (!switchTo.isConnectedOrConnecting() ||
1194 newNet.isTeardownRequested()) {
Robert Greenwalt42acef32009-08-12 16:08:25 -07001195 newNet.reconnect();
1196 }
1197 if (DBG) {
1198 if (switchTo.isConnected()) {
Wink Savilleed9c02b2010-12-03 12:01:38 -08001199 log("Switching to already connected " + switchTo.getTypeName());
Robert Greenwalt42acef32009-08-12 16:08:25 -07001200 } else {
Wink Savilleed9c02b2010-12-03 12:01:38 -08001201 log("Attempting to switch to " + switchTo.getTypeName());
Robert Greenwalt42acef32009-08-12 16:08:25 -07001202 }
1203 }
Robert Greenwalt42acef32009-08-12 16:08:25 -07001204 } else {
1205 newNet.reconnect();
Robert Greenwaltf0fa39e2010-03-09 14:55:08 -08001206 newNet = null; // not officially avail.. try anyway, but
1207 // report no failover
Robert Greenwalt42acef32009-08-12 16:08:25 -07001208 }
Robert Greenwalt572172b2010-10-08 16:35:52 -07001209 } else {
Wink Savilleed9c02b2010-12-03 12:01:38 -08001210 loge("Network failover failing.");
Robert Greenwalt42acef32009-08-12 16:08:25 -07001211 }
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001212 }
Robert Greenwalt42acef32009-08-12 16:08:25 -07001213
Robert Greenwaltda03c4e2010-01-20 19:29:41 -08001214 return newNet;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001215 }
1216
1217 private void sendConnectedBroadcast(NetworkInfo info) {
Robert Greenwalt1e9aac22010-09-15 17:36:33 -07001218 sendGeneralBroadcast(info, ConnectivityManager.CONNECTIVITY_ACTION);
1219 }
1220
1221 private void sendInetConditionBroadcast(NetworkInfo info) {
1222 sendGeneralBroadcast(info, ConnectivityManager.INET_CONDITION_ACTION);
1223 }
1224
1225 private void sendGeneralBroadcast(NetworkInfo info, String bcastType) {
1226 Intent intent = new Intent(bcastType);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001227 intent.putExtra(ConnectivityManager.EXTRA_NETWORK_INFO, info);
1228 if (info.isFailover()) {
1229 intent.putExtra(ConnectivityManager.EXTRA_IS_FAILOVER, true);
1230 info.setFailover(false);
1231 }
1232 if (info.getReason() != null) {
1233 intent.putExtra(ConnectivityManager.EXTRA_REASON, info.getReason());
1234 }
1235 if (info.getExtraInfo() != null) {
Robert Greenwalt86e9e552009-07-16 17:21:39 -07001236 intent.putExtra(ConnectivityManager.EXTRA_EXTRA_INFO,
1237 info.getExtraInfo());
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001238 }
Robert Greenwaltd7085fc2010-09-08 15:24:47 -07001239 intent.putExtra(ConnectivityManager.EXTRA_INET_CONDITION, mDefaultInetConditionPublished);
Mike Lockwood0f79b542009-08-14 14:18:49 -04001240 sendStickyBroadcast(intent);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001241 }
1242
1243 /**
1244 * Called when an attempt to fail over to another network has failed.
1245 * @param info the {@link NetworkInfo} for the failed network
1246 */
1247 private void handleConnectionFailure(NetworkInfo info) {
1248 mNetTrackers[info.getType()].setTeardownRequested(false);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001249
Robert Greenwalt42acef32009-08-12 16:08:25 -07001250 String reason = info.getReason();
1251 String extraInfo = info.getExtraInfo();
Robert Greenwalt86e9e552009-07-16 17:21:39 -07001252
Robert Greenwalt572172b2010-10-08 16:35:52 -07001253 String reasonText;
1254 if (reason == null) {
1255 reasonText = ".";
1256 } else {
1257 reasonText = " (" + reason + ").";
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001258 }
Wink Savilleed9c02b2010-12-03 12:01:38 -08001259 loge("Attempt to connect to " + info.getTypeName() + " failed" + reasonText);
Robert Greenwalt42acef32009-08-12 16:08:25 -07001260
1261 Intent intent = new Intent(ConnectivityManager.CONNECTIVITY_ACTION);
1262 intent.putExtra(ConnectivityManager.EXTRA_NETWORK_INFO, info);
1263 if (getActiveNetworkInfo() == null) {
1264 intent.putExtra(ConnectivityManager.EXTRA_NO_CONNECTIVITY, true);
1265 }
1266 if (reason != null) {
1267 intent.putExtra(ConnectivityManager.EXTRA_REASON, reason);
1268 }
1269 if (extraInfo != null) {
1270 intent.putExtra(ConnectivityManager.EXTRA_EXTRA_INFO, extraInfo);
1271 }
1272 if (info.isFailover()) {
1273 intent.putExtra(ConnectivityManager.EXTRA_IS_FAILOVER, true);
1274 info.setFailover(false);
1275 }
Robert Greenwaltda03c4e2010-01-20 19:29:41 -08001276
Robert Greenwaltcc4b4012010-01-25 17:54:29 -08001277 NetworkStateTracker newNet = null;
1278 if (mNetAttributes[info.getType()].isDefault()) {
1279 newNet = tryFailover(info.getType());
1280 if (newNet != null) {
1281 NetworkInfo switchTo = newNet.getNetworkInfo();
Robert Greenwalt029be812010-09-20 18:01:43 -07001282 if (!switchTo.isConnected()) {
Robert Greenwalt572172b2010-10-08 16:35:52 -07001283 // if the other net is connected they've already reset this and perhaps
1284 // even gotten a positive report we don't want to overwrite, but if not
1285 // we need to clear this now to turn our cellular sig strength white
Robert Greenwalt029be812010-09-20 18:01:43 -07001286 mDefaultInetConditionPublished = 0;
1287 }
Robert Greenwaltcc4b4012010-01-25 17:54:29 -08001288 intent.putExtra(ConnectivityManager.EXTRA_OTHER_NETWORK_INFO, switchTo);
1289 } else {
Robert Greenwalt029be812010-09-20 18:01:43 -07001290 mDefaultInetConditionPublished = 0;
Robert Greenwaltcc4b4012010-01-25 17:54:29 -08001291 intent.putExtra(ConnectivityManager.EXTRA_NO_CONNECTIVITY, true);
1292 }
Robert Greenwaltda03c4e2010-01-20 19:29:41 -08001293 }
Robert Greenwaltcc4b4012010-01-25 17:54:29 -08001294
Robert Greenwalt029be812010-09-20 18:01:43 -07001295 intent.putExtra(ConnectivityManager.EXTRA_INET_CONDITION, mDefaultInetConditionPublished);
Mike Lockwood0f79b542009-08-14 14:18:49 -04001296 sendStickyBroadcast(intent);
Robert Greenwaltda03c4e2010-01-20 19:29:41 -08001297 /*
1298 * If the failover network is already connected, then immediately send
1299 * out a followup broadcast indicating successful failover
1300 */
1301 if (newNet != null && newNet.getNetworkInfo().isConnected()) {
1302 sendConnectedBroadcast(newNet.getNetworkInfo());
1303 }
Mike Lockwood0f79b542009-08-14 14:18:49 -04001304 }
1305
1306 private void sendStickyBroadcast(Intent intent) {
1307 synchronized(this) {
Dianne Hackborn1c633fc2009-12-08 19:45:14 -08001308 if (!mSystemReady) {
1309 mInitialBroadcast = new Intent(intent);
Mike Lockwood0f79b542009-08-14 14:18:49 -04001310 }
Dianne Hackborn1c633fc2009-12-08 19:45:14 -08001311 intent.addFlags(Intent.FLAG_RECEIVER_REGISTERED_ONLY_BEFORE_BOOT);
1312 mContext.sendStickyBroadcast(intent);
Mike Lockwood0f79b542009-08-14 14:18:49 -04001313 }
1314 }
1315
1316 void systemReady() {
1317 synchronized(this) {
1318 mSystemReady = true;
Dianne Hackborn1c633fc2009-12-08 19:45:14 -08001319 if (mInitialBroadcast != null) {
1320 mContext.sendStickyBroadcast(mInitialBroadcast);
1321 mInitialBroadcast = null;
Mike Lockwood0f79b542009-08-14 14:18:49 -04001322 }
1323 }
Robert Greenwalt434203a2010-10-11 16:00:27 -07001324 // load the global proxy at startup
1325 mHandler.sendMessage(mHandler.obtainMessage(EVENT_APPLY_GLOBAL_HTTP_PROXY));
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001326 }
1327
1328 private void handleConnect(NetworkInfo info) {
Robert Greenwalt42acef32009-08-12 16:08:25 -07001329 int type = info.getType();
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001330
1331 // snapshot isFailover, because sendConnectedBroadcast() resets it
1332 boolean isFailover = info.isFailover();
Robert Greenwalt42acef32009-08-12 16:08:25 -07001333 NetworkStateTracker thisNet = mNetTrackers[type];
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001334
Robert Greenwalt42acef32009-08-12 16:08:25 -07001335 // if this is a default net and other default is running
1336 // kill the one not preferred
1337 if (mNetAttributes[type].isDefault()) {
Robert Greenwalt42acef32009-08-12 16:08:25 -07001338 if (mActiveDefaultNetwork != -1 && mActiveDefaultNetwork != type) {
1339 if ((type != mNetworkPreference &&
1340 mNetAttributes[mActiveDefaultNetwork].mPriority >
1341 mNetAttributes[type].mPriority) ||
1342 mNetworkPreference == mActiveDefaultNetwork) {
1343 // don't accept this one
Wink Savilleed9c02b2010-12-03 12:01:38 -08001344 if (DBG) {
1345 log("Not broadcasting CONNECT_ACTION " +
Robert Greenwalt42acef32009-08-12 16:08:25 -07001346 "to torn down network " + info.getTypeName());
Wink Savilleed9c02b2010-12-03 12:01:38 -08001347 }
Robert Greenwalt42acef32009-08-12 16:08:25 -07001348 teardown(thisNet);
1349 return;
1350 } else {
1351 // tear down the other
1352 NetworkStateTracker otherNet =
1353 mNetTrackers[mActiveDefaultNetwork];
Wink Savilleed9c02b2010-12-03 12:01:38 -08001354 if (DBG) {
1355 log("Policy requires " + otherNet.getNetworkInfo().getTypeName() +
Robert Greenwalt42acef32009-08-12 16:08:25 -07001356 " teardown");
Wink Savilleed9c02b2010-12-03 12:01:38 -08001357 }
Robert Greenwalt42acef32009-08-12 16:08:25 -07001358 if (!teardown(otherNet)) {
Wink Savilleed9c02b2010-12-03 12:01:38 -08001359 loge("Network declined teardown request");
Robert Greenwalt42acef32009-08-12 16:08:25 -07001360 return;
1361 }
Robert Greenwalt14f2ef42010-06-15 12:19:37 -07001362 }
1363 }
1364 synchronized (ConnectivityService.this) {
1365 // have a new default network, release the transition wakelock in a second
1366 // if it's held. The second pause is to allow apps to reconnect over the
1367 // new network
1368 if (mNetTransitionWakeLock.isHeld()) {
1369 mHandler.sendMessageDelayed(mHandler.obtainMessage(
Robert Greenwaltf3331232010-09-24 14:32:21 -07001370 EVENT_CLEAR_NET_TRANSITION_WAKELOCK,
Robert Greenwalt14f2ef42010-06-15 12:19:37 -07001371 mNetTransitionWakeLockSerialNumber, 0),
1372 1000);
Robert Greenwalt42acef32009-08-12 16:08:25 -07001373 }
1374 }
1375 mActiveDefaultNetwork = type;
Robert Greenwaltd7085fc2010-09-08 15:24:47 -07001376 // this will cause us to come up initially as unconnected and switching
1377 // to connected after our normal pause unless somebody reports us as reall
1378 // disconnected
1379 mDefaultInetConditionPublished = 0;
1380 mDefaultConnectionSequence++;
1381 mInetConditionChangeInFlight = false;
1382 // Don't do this - if we never sign in stay, grey
1383 //reportNetworkCondition(mActiveDefaultNetwork, 100);
Robert Greenwalt42acef32009-08-12 16:08:25 -07001384 }
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001385 thisNet.setTeardownRequested(false);
Irfan Sheriffd649c122010-06-09 15:39:36 -07001386 updateNetworkSettings(thisNet);
Robert Greenwaltc76b8fa2010-07-23 15:46:26 -07001387 handleConnectivityChange(type);
Robert Greenwalt42acef32009-08-12 16:08:25 -07001388 sendConnectedBroadcast(info);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001389 }
1390
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001391 /**
Robert Greenwaltc76b8fa2010-07-23 15:46:26 -07001392 * After a change in the connectivity state of a network. We're mainly
1393 * concerned with making sure that the list of DNS servers is set up
1394 * according to which networks are connected, and ensuring that the
1395 * right routing table entries exist.
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001396 */
Robert Greenwaltc76b8fa2010-07-23 15:46:26 -07001397 private void handleConnectivityChange(int netType) {
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001398 /*
Robert Greenwalt42acef32009-08-12 16:08:25 -07001399 * If a non-default network is enabled, add the host routes that
Robert Greenwaltc76b8fa2010-07-23 15:46:26 -07001400 * will allow it's DNS servers to be accessed.
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001401 */
Robert Greenwaltc76b8fa2010-07-23 15:46:26 -07001402 handleDnsConfigurationChange(netType);
Robert Greenwalt42acef32009-08-12 16:08:25 -07001403
Robert Greenwaltc76b8fa2010-07-23 15:46:26 -07001404 if (mNetTrackers[netType].getNetworkInfo().isConnected()) {
1405 if (mNetAttributes[netType].isDefault()) {
Robert Greenwalt434203a2010-10-11 16:00:27 -07001406 handleApplyDefaultProxy(netType);
Robert Greenwaltc76b8fa2010-07-23 15:46:26 -07001407 addDefaultRoute(mNetTrackers[netType]);
Robert Greenwalt42acef32009-08-12 16:08:25 -07001408 } else {
Robert Greenwaltc76b8fa2010-07-23 15:46:26 -07001409 addPrivateDnsRoutes(mNetTrackers[netType]);
1410 }
1411 } else {
1412 if (mNetAttributes[netType].isDefault()) {
1413 removeDefaultRoute(mNetTrackers[netType]);
1414 } else {
1415 removePrivateDnsRoutes(mNetTrackers[netType]);
Robert Greenwalt42acef32009-08-12 16:08:25 -07001416 }
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001417 }
1418 }
1419
Irfan Sheriffd649c122010-06-09 15:39:36 -07001420 private void addPrivateDnsRoutes(NetworkStateTracker nt) {
Irfan Sheriffd649c122010-06-09 15:39:36 -07001421 boolean privateDnsRouteSet = nt.isPrivateDnsRouteSet();
Robert Greenwalt37e65eb2010-08-30 10:56:47 -07001422 LinkProperties p = nt.getLinkProperties();
Robert Greenwalt47f69fe2010-06-15 15:43:39 -07001423 if (p == null) return;
1424 String interfaceName = p.getInterfaceName();
Irfan Sheriffd649c122010-06-09 15:39:36 -07001425
1426 if (DBG) {
Wink Savilleed9c02b2010-12-03 12:01:38 -08001427 log("addPrivateDnsRoutes for " + nt +
Irfan Sheriffd649c122010-06-09 15:39:36 -07001428 "(" + interfaceName + ") - mPrivateDnsRouteSet = " + privateDnsRouteSet);
1429 }
Irfan Sheriffd649c122010-06-09 15:39:36 -07001430 if (interfaceName != null && !privateDnsRouteSet) {
Robert Greenwalt47f69fe2010-06-15 15:43:39 -07001431 Collection<InetAddress> dnsList = p.getDnses();
1432 for (InetAddress dns : dnsList) {
Wink Savilleed9c02b2010-12-03 12:01:38 -08001433 if (DBG) log(" adding " + dns);
Robert Greenwalt585ac0f2010-08-27 09:24:29 -07001434 NetworkUtils.addHostRoute(interfaceName, dns, null);
Irfan Sheriffd649c122010-06-09 15:39:36 -07001435 }
1436 nt.privateDnsRouteSet(true);
1437 }
1438 }
1439
1440 private void removePrivateDnsRoutes(NetworkStateTracker nt) {
1441 // TODO - we should do this explicitly but the NetUtils api doesnt
1442 // support this yet - must remove all. No worse than before
Robert Greenwalt37e65eb2010-08-30 10:56:47 -07001443 LinkProperties p = nt.getLinkProperties();
Robert Greenwalt47f69fe2010-06-15 15:43:39 -07001444 if (p == null) return;
1445 String interfaceName = p.getInterfaceName();
Irfan Sheriffd649c122010-06-09 15:39:36 -07001446 boolean privateDnsRouteSet = nt.isPrivateDnsRouteSet();
1447 if (interfaceName != null && privateDnsRouteSet) {
1448 if (DBG) {
Wink Savilleed9c02b2010-12-03 12:01:38 -08001449 log("removePrivateDnsRoutes for " + nt.getNetworkInfo().getTypeName() +
Irfan Sheriffd649c122010-06-09 15:39:36 -07001450 " (" + interfaceName + ")");
1451 }
1452 NetworkUtils.removeHostRoutes(interfaceName);
1453 nt.privateDnsRouteSet(false);
1454 }
1455 }
1456
Irfan Sheriffd649c122010-06-09 15:39:36 -07001457
1458 private void addDefaultRoute(NetworkStateTracker nt) {
Robert Greenwalt37e65eb2010-08-30 10:56:47 -07001459 LinkProperties p = nt.getLinkProperties();
Robert Greenwalt47f69fe2010-06-15 15:43:39 -07001460 if (p == null) return;
1461 String interfaceName = p.getInterfaceName();
1462 InetAddress defaultGatewayAddr = p.getGateway();
Irfan Sheriffd649c122010-06-09 15:39:36 -07001463
Robert Greenwaltc76b8fa2010-07-23 15:46:26 -07001464 if ((interfaceName != null) && (defaultGatewayAddr != null )) {
Robert Greenwalt585ac0f2010-08-27 09:24:29 -07001465 if (!NetworkUtils.addDefaultRoute(interfaceName, defaultGatewayAddr) && DBG) {
Robert Greenwalt47f69fe2010-06-15 15:43:39 -07001466 NetworkInfo networkInfo = nt.getNetworkInfo();
Wink Savilleed9c02b2010-12-03 12:01:38 -08001467 log("addDefaultRoute for " + networkInfo.getTypeName() +
Irfan Sheriffd649c122010-06-09 15:39:36 -07001468 " (" + interfaceName + "), GatewayAddr=" + defaultGatewayAddr);
1469 }
Irfan Sheriffd649c122010-06-09 15:39:36 -07001470 }
1471 }
1472
1473
1474 public void removeDefaultRoute(NetworkStateTracker nt) {
Robert Greenwalt37e65eb2010-08-30 10:56:47 -07001475 LinkProperties p = nt.getLinkProperties();
Robert Greenwalt47f69fe2010-06-15 15:43:39 -07001476 if (p == null) return;
1477 String interfaceName = p.getInterfaceName();
Irfan Sheriffd649c122010-06-09 15:39:36 -07001478
Robert Greenwaltc76b8fa2010-07-23 15:46:26 -07001479 if (interfaceName != null) {
1480 if ((NetworkUtils.removeDefaultRoute(interfaceName) >= 0) && DBG) {
Robert Greenwalt47f69fe2010-06-15 15:43:39 -07001481 NetworkInfo networkInfo = nt.getNetworkInfo();
Wink Savilleed9c02b2010-12-03 12:01:38 -08001482 log("removeDefaultRoute for " + networkInfo.getTypeName() + " (" +
Irfan Sheriffd649c122010-06-09 15:39:36 -07001483 interfaceName + ")");
1484 }
Irfan Sheriffd649c122010-06-09 15:39:36 -07001485 }
1486 }
1487
1488 /**
1489 * Reads the network specific TCP buffer sizes from SystemProperties
1490 * net.tcp.buffersize.[default|wifi|umts|edge|gprs] and set them for system
1491 * wide use
1492 */
1493 public void updateNetworkSettings(NetworkStateTracker nt) {
1494 String key = nt.getTcpBufferSizesPropName();
1495 String bufferSizes = SystemProperties.get(key);
1496
1497 if (bufferSizes.length() == 0) {
Wink Savilleed9c02b2010-12-03 12:01:38 -08001498 loge(key + " not found in system properties. Using defaults");
Irfan Sheriffd649c122010-06-09 15:39:36 -07001499
1500 // Setting to default values so we won't be stuck to previous values
1501 key = "net.tcp.buffersize.default";
1502 bufferSizes = SystemProperties.get(key);
1503 }
1504
1505 // Set values in kernel
1506 if (bufferSizes.length() != 0) {
1507 if (DBG) {
Wink Savilleed9c02b2010-12-03 12:01:38 -08001508 log("Setting TCP values: [" + bufferSizes
Irfan Sheriffd649c122010-06-09 15:39:36 -07001509 + "] which comes from [" + key + "]");
1510 }
1511 setBufferSize(bufferSizes);
1512 }
1513 }
1514
1515 /**
1516 * Writes TCP buffer sizes to /sys/kernel/ipv4/tcp_[r/w]mem_[min/def/max]
1517 * which maps to /proc/sys/net/ipv4/tcp_rmem and tcpwmem
1518 *
1519 * @param bufferSizes in the format of "readMin, readInitial, readMax,
1520 * writeMin, writeInitial, writeMax"
1521 */
1522 private void setBufferSize(String bufferSizes) {
1523 try {
1524 String[] values = bufferSizes.split(",");
1525
1526 if (values.length == 6) {
1527 final String prefix = "/sys/kernel/ipv4/tcp_";
1528 stringToFile(prefix + "rmem_min", values[0]);
1529 stringToFile(prefix + "rmem_def", values[1]);
1530 stringToFile(prefix + "rmem_max", values[2]);
1531 stringToFile(prefix + "wmem_min", values[3]);
1532 stringToFile(prefix + "wmem_def", values[4]);
1533 stringToFile(prefix + "wmem_max", values[5]);
1534 } else {
Wink Savilleed9c02b2010-12-03 12:01:38 -08001535 loge("Invalid buffersize string: " + bufferSizes);
Irfan Sheriffd649c122010-06-09 15:39:36 -07001536 }
1537 } catch (IOException e) {
Wink Savilleed9c02b2010-12-03 12:01:38 -08001538 loge("Can't set tcp buffer sizes:" + e);
Irfan Sheriffd649c122010-06-09 15:39:36 -07001539 }
1540 }
1541
1542 /**
1543 * Writes string to file. Basically same as "echo -n $string > $filename"
1544 *
1545 * @param filename
1546 * @param string
1547 * @throws IOException
1548 */
1549 private void stringToFile(String filename, String string) throws IOException {
1550 FileWriter out = new FileWriter(filename);
1551 try {
1552 out.write(string);
1553 } finally {
1554 out.close();
1555 }
1556 }
1557
1558
Robert Greenwalt42acef32009-08-12 16:08:25 -07001559 /**
1560 * Adjust the per-process dns entries (net.dns<x>.<pid>) based
1561 * on the highest priority active net which this process requested.
1562 * If there aren't any, clear it out
1563 */
1564 private void reassessPidDns(int myPid, boolean doBump)
1565 {
Wink Savilleed9c02b2010-12-03 12:01:38 -08001566 if (DBG) log("reassessPidDns for pid " + myPid);
Robert Greenwalt42acef32009-08-12 16:08:25 -07001567 for(int i : mPriorityList) {
1568 if (mNetAttributes[i].isDefault()) {
1569 continue;
1570 }
1571 NetworkStateTracker nt = mNetTrackers[i];
Robert Greenwalt86e9e552009-07-16 17:21:39 -07001572 if (nt.getNetworkInfo().isConnected() &&
1573 !nt.isTeardownRequested()) {
Robert Greenwalt37e65eb2010-08-30 10:56:47 -07001574 LinkProperties p = nt.getLinkProperties();
Robert Greenwalt47f69fe2010-06-15 15:43:39 -07001575 if (p == null) continue;
Robert Greenwalt42acef32009-08-12 16:08:25 -07001576 List pids = mNetRequestersPids[i];
1577 for (int j=0; j<pids.size(); j++) {
1578 Integer pid = (Integer)pids.get(j);
1579 if (pid.intValue() == myPid) {
Robert Greenwalt47f69fe2010-06-15 15:43:39 -07001580 Collection<InetAddress> dnses = p.getDnses();
1581 writePidDns(dnses, myPid);
Robert Greenwalt42acef32009-08-12 16:08:25 -07001582 if (doBump) {
1583 bumpDns();
1584 }
1585 return;
1586 }
1587 }
1588 }
1589 }
1590 // nothing found - delete
1591 for (int i = 1; ; i++) {
1592 String prop = "net.dns" + i + "." + myPid;
1593 if (SystemProperties.get(prop).length() == 0) {
1594 if (doBump) {
1595 bumpDns();
1596 }
1597 return;
1598 }
1599 SystemProperties.set(prop, "");
1600 }
1601 }
1602
Robert Greenwalt47f69fe2010-06-15 15:43:39 -07001603 private void writePidDns(Collection <InetAddress> dnses, int pid) {
Robert Greenwalt42acef32009-08-12 16:08:25 -07001604 int j = 1;
Robert Greenwalt47f69fe2010-06-15 15:43:39 -07001605 for (InetAddress dns : dnses) {
1606 SystemProperties.set("net.dns" + j++ + "." + pid, dns.getHostAddress());
Robert Greenwalt42acef32009-08-12 16:08:25 -07001607 }
1608 }
1609
1610 private void bumpDns() {
1611 /*
1612 * Bump the property that tells the name resolver library to reread
1613 * the DNS server list from the properties.
1614 */
1615 String propVal = SystemProperties.get("net.dnschange");
1616 int n = 0;
1617 if (propVal.length() != 0) {
1618 try {
1619 n = Integer.parseInt(propVal);
1620 } catch (NumberFormatException e) {}
1621 }
1622 SystemProperties.set("net.dnschange", "" + (n+1));
Robert Greenwalt03595d02010-11-02 14:08:23 -07001623 /*
1624 * Tell the VMs to toss their DNS caches
1625 */
1626 Intent intent = new Intent(Intent.ACTION_CLEAR_DNS_CACHE);
1627 intent.addFlags(Intent.FLAG_RECEIVER_REPLACE_PENDING);
1628 mContext.sendBroadcast(intent);
Robert Greenwalt42acef32009-08-12 16:08:25 -07001629 }
1630
Robert Greenwaltc76b8fa2010-07-23 15:46:26 -07001631 private void handleDnsConfigurationChange(int netType) {
Robert Greenwalt42acef32009-08-12 16:08:25 -07001632 // add default net's dns entries
Robert Greenwaltc76b8fa2010-07-23 15:46:26 -07001633 NetworkStateTracker nt = mNetTrackers[netType];
1634 if (nt != null && nt.getNetworkInfo().isConnected() && !nt.isTeardownRequested()) {
Robert Greenwalt37e65eb2010-08-30 10:56:47 -07001635 LinkProperties p = nt.getLinkProperties();
Robert Greenwaltc76b8fa2010-07-23 15:46:26 -07001636 if (p == null) return;
1637 Collection<InetAddress> dnses = p.getDnses();
1638 if (mNetAttributes[netType].isDefault()) {
1639 int j = 1;
Robert Greenwalte90aa5e2010-09-01 11:34:05 -07001640 if (dnses.size() == 0 && mDefaultDns != null) {
Robert Greenwaltc76b8fa2010-07-23 15:46:26 -07001641 if (DBG) {
Wink Savilleed9c02b2010-12-03 12:01:38 -08001642 log("no dns provided - using " + mDefaultDns.getHostAddress());
Robert Greenwalt42acef32009-08-12 16:08:25 -07001643 }
Robert Greenwalte90aa5e2010-09-01 11:34:05 -07001644 SystemProperties.set("net.dns1", mDefaultDns.getHostAddress());
1645 j++;
1646 } else {
1647 for (InetAddress dns : dnses) {
1648 if (DBG) {
Wink Savilleed9c02b2010-12-03 12:01:38 -08001649 log("adding dns " + dns + " for " +
Robert Greenwalte90aa5e2010-09-01 11:34:05 -07001650 nt.getNetworkInfo().getTypeName());
1651 }
1652 SystemProperties.set("net.dns" + j++, dns.getHostAddress());
1653 }
Robert Greenwaltc76b8fa2010-07-23 15:46:26 -07001654 }
1655 for (int k=j ; k<mNumDnsEntries; k++) {
Wink Savilleed9c02b2010-12-03 12:01:38 -08001656 if (DBG) log("erasing net.dns" + k);
Robert Greenwaltc76b8fa2010-07-23 15:46:26 -07001657 SystemProperties.set("net.dns" + k, "");
1658 }
1659 mNumDnsEntries = j;
1660 } else {
1661 // set per-pid dns for attached secondary nets
1662 List pids = mNetRequestersPids[netType];
1663 for (int y=0; y< pids.size(); y++) {
1664 Integer pid = (Integer)pids.get(y);
1665 writePidDns(dnses, pid.intValue());
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001666 }
1667 }
Robert Greenwaltc76b8fa2010-07-23 15:46:26 -07001668 bumpDns();
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001669 }
Robert Greenwalt42acef32009-08-12 16:08:25 -07001670 }
1671
1672 private int getRestoreDefaultNetworkDelay() {
1673 String restoreDefaultNetworkDelayStr = SystemProperties.get(
1674 NETWORK_RESTORE_DELAY_PROP_NAME);
1675 if(restoreDefaultNetworkDelayStr != null &&
1676 restoreDefaultNetworkDelayStr.length() != 0) {
1677 try {
1678 return Integer.valueOf(restoreDefaultNetworkDelayStr);
1679 } catch (NumberFormatException e) {
1680 }
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001681 }
Robert Greenwalt42acef32009-08-12 16:08:25 -07001682 return RESTORE_DEFAULT_NETWORK_DELAY;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001683 }
1684
1685 @Override
1686 protected void dump(FileDescriptor fd, PrintWriter pw, String[] args) {
Robert Greenwalt86e9e552009-07-16 17:21:39 -07001687 if (mContext.checkCallingOrSelfPermission(
1688 android.Manifest.permission.DUMP)
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001689 != PackageManager.PERMISSION_GRANTED) {
Robert Greenwalt86e9e552009-07-16 17:21:39 -07001690 pw.println("Permission Denial: can't dump ConnectivityService " +
1691 "from from pid=" + Binder.getCallingPid() + ", uid=" +
1692 Binder.getCallingUid());
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001693 return;
1694 }
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001695 pw.println();
1696 for (NetworkStateTracker nst : mNetTrackers) {
Robert Greenwaltb9285352009-12-21 18:24:07 -08001697 if (nst != null) {
1698 if (nst.getNetworkInfo().isConnected()) {
1699 pw.println("Active network: " + nst.getNetworkInfo().
1700 getTypeName());
1701 }
1702 pw.println(nst.getNetworkInfo());
1703 pw.println(nst);
1704 pw.println();
Robert Greenwalt42acef32009-08-12 16:08:25 -07001705 }
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001706 }
Robert Greenwaltb9285352009-12-21 18:24:07 -08001707
1708 pw.println("Network Requester Pids:");
1709 for (int net : mPriorityList) {
1710 String pidString = net + ": ";
1711 for (Object pid : mNetRequestersPids[net]) {
1712 pidString = pidString + pid.toString() + ", ";
1713 }
1714 pw.println(pidString);
1715 }
1716 pw.println();
1717
1718 pw.println("FeatureUsers:");
1719 for (Object requester : mFeatureUsers) {
1720 pw.println(requester.toString());
1721 }
1722 pw.println();
Robert Greenwalt2a091d72010-02-11 18:18:40 -08001723
Robert Greenwalt14f2ef42010-06-15 12:19:37 -07001724 synchronized (this) {
1725 pw.println("NetworkTranstionWakeLock is currently " +
1726 (mNetTransitionWakeLock.isHeld() ? "" : "not ") + "held.");
1727 pw.println("It was last requested for "+mNetTransitionWakeLockCausedBy);
1728 }
1729 pw.println();
1730
Robert Greenwalt2a091d72010-02-11 18:18:40 -08001731 mTethering.dump(fd, pw, args);
Robert Greenwalt4e8dfef2010-09-20 14:35:25 -07001732
1733 if (mInetLog != null) {
1734 pw.println();
1735 pw.println("Inet condition reports:");
1736 for(int i = 0; i < mInetLog.size(); i++) {
1737 pw.println(mInetLog.get(i));
1738 }
1739 }
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001740 }
1741
Robert Greenwalt42acef32009-08-12 16:08:25 -07001742 // must be stateless - things change under us.
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001743 private class MyHandler extends Handler {
Wink Savillebb08caf2010-09-02 19:23:52 -07001744 public MyHandler(Looper looper) {
1745 super(looper);
1746 }
1747
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001748 @Override
1749 public void handleMessage(Message msg) {
1750 NetworkInfo info;
1751 switch (msg.what) {
1752 case NetworkStateTracker.EVENT_STATE_CHANGED:
1753 info = (NetworkInfo) msg.obj;
Robert Greenwalt511288a2009-12-07 11:33:18 -08001754 int type = info.getType();
1755 NetworkInfo.State state = info.getState();
Robert Greenwalt6e6dec22010-01-25 16:14:00 -08001756 // only do this optimization for wifi. It going into scan mode for location
1757 // services generates alot of noise. Meanwhile the mms apn won't send out
1758 // subsequent notifications when on default cellular because it never
1759 // disconnects.. so only do this to wifi notifications. Fixed better when the
1760 // APN notifications are standardized.
1761 if (mNetAttributes[type].mLastState == state &&
1762 mNetAttributes[type].mRadio == ConnectivityManager.TYPE_WIFI) {
Robert Greenwalt511288a2009-12-07 11:33:18 -08001763 if (DBG) {
Robert Greenwalt6e6dec22010-01-25 16:14:00 -08001764 // TODO - remove this after we validate the dropping doesn't break
1765 // anything
Wink Savilleed9c02b2010-12-03 12:01:38 -08001766 log("Dropping ConnectivityChange for " +
Robert Greenwalt1193ae42010-01-13 09:36:31 -08001767 info.getTypeName() + ": " +
Robert Greenwalt511288a2009-12-07 11:33:18 -08001768 state + "/" + info.getDetailedState());
1769 }
1770 return;
1771 }
1772 mNetAttributes[type].mLastState = state;
1773
Wink Savilleed9c02b2010-12-03 12:01:38 -08001774 if (DBG) log("ConnectivityChange for " +
Robert Greenwalt86e9e552009-07-16 17:21:39 -07001775 info.getTypeName() + ": " +
Robert Greenwalt511288a2009-12-07 11:33:18 -08001776 state + "/" + info.getDetailedState());
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001777
1778 // Connectivity state changed:
1779 // [31-13] Reserved for future use
Robert Greenwalt86e9e552009-07-16 17:21:39 -07001780 // [12-9] Network subtype (for mobile network, as defined
1781 // by TelephonyManager)
1782 // [8-3] Detailed state ordinal (as defined by
1783 // NetworkInfo.DetailedState)
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001784 // [2-0] Network type (as defined by ConnectivityManager)
1785 int eventLogParam = (info.getType() & 0x7) |
1786 ((info.getDetailedState().ordinal() & 0x3f) << 3) |
1787 (info.getSubtype() << 9);
Doug Zongkerab5c49c2009-12-04 10:31:43 -08001788 EventLog.writeEvent(EventLogTags.CONNECTIVITY_STATE_CHANGED,
Robert Greenwalt86e9e552009-07-16 17:21:39 -07001789 eventLogParam);
1790
1791 if (info.getDetailedState() ==
1792 NetworkInfo.DetailedState.FAILED) {
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001793 handleConnectionFailure(info);
Robert Greenwalt511288a2009-12-07 11:33:18 -08001794 } else if (state == NetworkInfo.State.DISCONNECTED) {
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001795 handleDisconnect(info);
Robert Greenwalt511288a2009-12-07 11:33:18 -08001796 } else if (state == NetworkInfo.State.SUSPENDED) {
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001797 // TODO: need to think this over.
Robert Greenwalt86e9e552009-07-16 17:21:39 -07001798 // the logic here is, handle SUSPENDED the same as
1799 // DISCONNECTED. The only difference being we are
1800 // broadcasting an intent with NetworkInfo that's
1801 // suspended. This allows the applications an
1802 // opportunity to handle DISCONNECTED and SUSPENDED
1803 // differently, or not.
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001804 handleDisconnect(info);
Robert Greenwalt511288a2009-12-07 11:33:18 -08001805 } else if (state == NetworkInfo.State.CONNECTED) {
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001806 handleConnect(info);
1807 }
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001808 break;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001809 case NetworkStateTracker.EVENT_CONFIGURATION_CHANGED:
Robert Greenwaltc76b8fa2010-07-23 15:46:26 -07001810 info = (NetworkInfo) msg.obj;
1811 type = info.getType();
Robert Greenwalt434203a2010-10-11 16:00:27 -07001812 handleConnectivityChange(type);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001813 break;
Robert Greenwaltf3331232010-09-24 14:32:21 -07001814 case EVENT_CLEAR_NET_TRANSITION_WAKELOCK:
Robert Greenwalt14f2ef42010-06-15 12:19:37 -07001815 String causedBy = null;
1816 synchronized (ConnectivityService.this) {
1817 if (msg.arg1 == mNetTransitionWakeLockSerialNumber &&
1818 mNetTransitionWakeLock.isHeld()) {
1819 mNetTransitionWakeLock.release();
1820 causedBy = mNetTransitionWakeLockCausedBy;
1821 }
1822 }
1823 if (causedBy != null) {
Wink Savilleed9c02b2010-12-03 12:01:38 -08001824 log("NetTransition Wakelock for " + causedBy + " released by timeout");
Robert Greenwalt14f2ef42010-06-15 12:19:37 -07001825 }
Robert Greenwalt057d5e92010-09-09 14:05:10 -07001826 break;
Robert Greenwalt8dcc28b2010-09-23 10:05:56 -07001827 case EVENT_RESTORE_DEFAULT_NETWORK:
Robert Greenwalt9c75d4a2009-09-27 17:27:04 -07001828 FeatureUser u = (FeatureUser)msg.obj;
1829 u.expire();
Robert Greenwaltd7085fc2010-09-08 15:24:47 -07001830 break;
Robert Greenwalt8dcc28b2010-09-23 10:05:56 -07001831 case EVENT_INET_CONDITION_CHANGE:
1832 {
1833 int netType = msg.arg1;
1834 int condition = msg.arg2;
1835 handleInetConditionChange(netType, condition);
Robert Greenwalt14f2ef42010-06-15 12:19:37 -07001836 break;
Robert Greenwalt8dcc28b2010-09-23 10:05:56 -07001837 }
1838 case EVENT_INET_CONDITION_HOLD_END:
1839 {
1840 int netType = msg.arg1;
1841 int sequence = msg.arg2;
1842 handleInetConditionHoldEnd(netType, sequence);
Robert Greenwaltd7085fc2010-09-08 15:24:47 -07001843 break;
Robert Greenwalt8dcc28b2010-09-23 10:05:56 -07001844 }
1845 case EVENT_SET_NETWORK_PREFERENCE:
1846 {
1847 int preference = msg.arg1;
1848 handleSetNetworkPreference(preference);
1849 break;
1850 }
1851 case EVENT_SET_BACKGROUND_DATA:
1852 {
1853 boolean enabled = (msg.arg1 == ENABLED);
1854 handleSetBackgroundData(enabled);
1855 break;
1856 }
1857 case EVENT_SET_MOBILE_DATA:
1858 {
1859 boolean enabled = (msg.arg1 == ENABLED);
1860 handleSetMobileData(enabled);
1861 break;
1862 }
Robert Greenwalt434203a2010-10-11 16:00:27 -07001863 case EVENT_APPLY_GLOBAL_HTTP_PROXY:
1864 {
1865 handleDeprecatedGlobalHttpProxy();
1866 }
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001867 }
1868 }
1869 }
Robert Greenwaltd0e18ff2010-01-26 11:40:34 -08001870
1871 // javadoc from interface
Robert Greenwalt5a735062010-03-02 17:25:02 -08001872 public int tether(String iface) {
Robert Greenwaltd0e18ff2010-01-26 11:40:34 -08001873 enforceTetherChangePermission();
Robert Greenwalt5a735062010-03-02 17:25:02 -08001874
1875 if (isTetheringSupported()) {
1876 return mTethering.tether(iface);
1877 } else {
1878 return ConnectivityManager.TETHER_ERROR_UNSUPPORTED;
1879 }
Robert Greenwaltd0e18ff2010-01-26 11:40:34 -08001880 }
1881
1882 // javadoc from interface
Robert Greenwalt5a735062010-03-02 17:25:02 -08001883 public int untether(String iface) {
Robert Greenwaltd0e18ff2010-01-26 11:40:34 -08001884 enforceTetherChangePermission();
Robert Greenwalt5a735062010-03-02 17:25:02 -08001885
1886 if (isTetheringSupported()) {
1887 return mTethering.untether(iface);
1888 } else {
1889 return ConnectivityManager.TETHER_ERROR_UNSUPPORTED;
1890 }
1891 }
1892
1893 // javadoc from interface
1894 public int getLastTetherError(String iface) {
1895 enforceTetherAccessPermission();
1896
1897 if (isTetheringSupported()) {
1898 return mTethering.getLastTetherError(iface);
1899 } else {
1900 return ConnectivityManager.TETHER_ERROR_UNSUPPORTED;
1901 }
Robert Greenwalt2a091d72010-02-11 18:18:40 -08001902 }
1903
1904 // TODO - proper iface API for selection by property, inspection, etc
1905 public String[] getTetherableUsbRegexs() {
1906 enforceTetherAccessPermission();
1907 if (isTetheringSupported()) {
1908 return mTethering.getTetherableUsbRegexs();
1909 } else {
1910 return new String[0];
1911 }
1912 }
1913
1914 public String[] getTetherableWifiRegexs() {
1915 enforceTetherAccessPermission();
1916 if (isTetheringSupported()) {
1917 return mTethering.getTetherableWifiRegexs();
1918 } else {
1919 return new String[0];
1920 }
Robert Greenwaltd0e18ff2010-01-26 11:40:34 -08001921 }
1922
Danica Chang6fdd0c62010-08-11 14:54:43 -07001923 public String[] getTetherableBluetoothRegexs() {
1924 enforceTetherAccessPermission();
1925 if (isTetheringSupported()) {
1926 return mTethering.getTetherableBluetoothRegexs();
1927 } else {
1928 return new String[0];
1929 }
1930 }
1931
Robert Greenwaltd0e18ff2010-01-26 11:40:34 -08001932 // TODO - move iface listing, queries, etc to new module
1933 // javadoc from interface
1934 public String[] getTetherableIfaces() {
Robert Greenwalt2a091d72010-02-11 18:18:40 -08001935 enforceTetherAccessPermission();
Robert Greenwaltd0e18ff2010-01-26 11:40:34 -08001936 return mTethering.getTetherableIfaces();
1937 }
1938
1939 public String[] getTetheredIfaces() {
Robert Greenwalt2a091d72010-02-11 18:18:40 -08001940 enforceTetherAccessPermission();
Robert Greenwaltd0e18ff2010-01-26 11:40:34 -08001941 return mTethering.getTetheredIfaces();
1942 }
Robert Greenwalt2a091d72010-02-11 18:18:40 -08001943
Robert Greenwalt5a735062010-03-02 17:25:02 -08001944 public String[] getTetheringErroredIfaces() {
1945 enforceTetherAccessPermission();
1946 return mTethering.getErroredIfaces();
1947 }
1948
Robert Greenwalt2a091d72010-02-11 18:18:40 -08001949 // if ro.tether.denied = true we default to no tethering
1950 // gservices could set the secure setting to 1 though to enable it on a build where it
1951 // had previously been turned off.
1952 public boolean isTetheringSupported() {
1953 enforceTetherAccessPermission();
1954 int defaultVal = (SystemProperties.get("ro.tether.denied").equals("true") ? 0 : 1);
Robert Greenwaltc9d5fb72010-02-25 12:29:30 -08001955 boolean tetherEnabledInSettings = (Settings.Secure.getInt(mContext.getContentResolver(),
1956 Settings.Secure.TETHER_SUPPORTED, defaultVal) != 0);
1957 return tetherEnabledInSettings && mTetheringConfigValid;
Robert Greenwalt2a091d72010-02-11 18:18:40 -08001958 }
Robert Greenwalt14f2ef42010-06-15 12:19:37 -07001959
1960 // An API NetworkStateTrackers can call when they lose their network.
1961 // This will automatically be cleared after X seconds or a network becomes CONNECTED,
1962 // whichever happens first. The timer is started by the first caller and not
1963 // restarted by subsequent callers.
1964 public void requestNetworkTransitionWakelock(String forWhom) {
1965 enforceConnectivityInternalPermission();
1966 synchronized (this) {
1967 if (mNetTransitionWakeLock.isHeld()) return;
1968 mNetTransitionWakeLockSerialNumber++;
1969 mNetTransitionWakeLock.acquire();
1970 mNetTransitionWakeLockCausedBy = forWhom;
1971 }
1972 mHandler.sendMessageDelayed(mHandler.obtainMessage(
Robert Greenwaltf3331232010-09-24 14:32:21 -07001973 EVENT_CLEAR_NET_TRANSITION_WAKELOCK,
Robert Greenwalt14f2ef42010-06-15 12:19:37 -07001974 mNetTransitionWakeLockSerialNumber, 0),
1975 mNetTransitionWakeLockTimeout);
1976 return;
1977 }
Robert Greenwaltca4306c2010-09-09 13:15:32 -07001978
Robert Greenwaltd7085fc2010-09-08 15:24:47 -07001979 // 100 percent is full good, 0 is full bad.
1980 public void reportInetCondition(int networkType, int percentage) {
Wink Savilleed9c02b2010-12-03 12:01:38 -08001981 if (DBG) log("reportNetworkCondition(" + networkType + ", " + percentage + ")");
Robert Greenwaltd7085fc2010-09-08 15:24:47 -07001982 mContext.enforceCallingOrSelfPermission(
1983 android.Manifest.permission.STATUS_BAR,
1984 "ConnectivityService");
1985
Robert Greenwalt4e8dfef2010-09-20 14:35:25 -07001986 if (DBG) {
1987 int pid = getCallingPid();
1988 int uid = getCallingUid();
1989 String s = pid + "(" + uid + ") reports inet is " +
1990 (percentage > 50 ? "connected" : "disconnected") + " (" + percentage + ") on " +
1991 "network Type " + networkType + " at " + GregorianCalendar.getInstance().getTime();
1992 mInetLog.add(s);
1993 while(mInetLog.size() > INET_CONDITION_LOG_MAX_SIZE) {
1994 mInetLog.remove(0);
1995 }
1996 }
Robert Greenwaltd7085fc2010-09-08 15:24:47 -07001997 mHandler.sendMessage(mHandler.obtainMessage(
Robert Greenwalt8dcc28b2010-09-23 10:05:56 -07001998 EVENT_INET_CONDITION_CHANGE, networkType, percentage));
1999 }
2000
2001 private void handleInetConditionChange(int netType, int condition) {
2002 if (DBG) {
Wink Savilleed9c02b2010-12-03 12:01:38 -08002003 log("Inet connectivity change, net=" +
Robert Greenwalt8dcc28b2010-09-23 10:05:56 -07002004 netType + ", condition=" + condition +
2005 ",mActiveDefaultNetwork=" + mActiveDefaultNetwork);
2006 }
2007 if (mActiveDefaultNetwork == -1) {
Wink Savilleed9c02b2010-12-03 12:01:38 -08002008 if (DBG) log("no active default network - aborting");
Robert Greenwalt8dcc28b2010-09-23 10:05:56 -07002009 return;
2010 }
2011 if (mActiveDefaultNetwork != netType) {
Wink Savilleed9c02b2010-12-03 12:01:38 -08002012 if (DBG) log("given net not default - aborting");
Robert Greenwalt8dcc28b2010-09-23 10:05:56 -07002013 return;
2014 }
2015 mDefaultInetCondition = condition;
2016 int delay;
2017 if (mInetConditionChangeInFlight == false) {
Wink Savilleed9c02b2010-12-03 12:01:38 -08002018 if (DBG) log("starting a change hold");
Robert Greenwalt8dcc28b2010-09-23 10:05:56 -07002019 // setup a new hold to debounce this
2020 if (mDefaultInetCondition > 50) {
2021 delay = Settings.Secure.getInt(mContext.getContentResolver(),
2022 Settings.Secure.INET_CONDITION_DEBOUNCE_UP_DELAY, 500);
2023 } else {
2024 delay = Settings.Secure.getInt(mContext.getContentResolver(),
2025 Settings.Secure.INET_CONDITION_DEBOUNCE_DOWN_DELAY, 3000);
2026 }
2027 mInetConditionChangeInFlight = true;
2028 mHandler.sendMessageDelayed(mHandler.obtainMessage(EVENT_INET_CONDITION_HOLD_END,
2029 mActiveDefaultNetwork, mDefaultConnectionSequence), delay);
2030 } else {
2031 // we've set the new condition, when this hold ends that will get
2032 // picked up
Wink Savilleed9c02b2010-12-03 12:01:38 -08002033 if (DBG) log("currently in hold - not setting new end evt");
Robert Greenwalt8dcc28b2010-09-23 10:05:56 -07002034 }
2035 }
2036
2037 private void handleInetConditionHoldEnd(int netType, int sequence) {
2038 if (DBG) {
Wink Savilleed9c02b2010-12-03 12:01:38 -08002039 log("Inet hold end, net=" + netType +
Robert Greenwalt8dcc28b2010-09-23 10:05:56 -07002040 ", condition =" + mDefaultInetCondition +
2041 ", published condition =" + mDefaultInetConditionPublished);
2042 }
2043 mInetConditionChangeInFlight = false;
2044
2045 if (mActiveDefaultNetwork == -1) {
Wink Savilleed9c02b2010-12-03 12:01:38 -08002046 if (DBG) log("no active default network - aborting");
Robert Greenwalt8dcc28b2010-09-23 10:05:56 -07002047 return;
2048 }
2049 if (mDefaultConnectionSequence != sequence) {
Wink Savilleed9c02b2010-12-03 12:01:38 -08002050 if (DBG) log("event hold for obsolete network - aborting");
Robert Greenwalt8dcc28b2010-09-23 10:05:56 -07002051 return;
2052 }
2053 if (mDefaultInetConditionPublished == mDefaultInetCondition) {
Wink Savilleed9c02b2010-12-03 12:01:38 -08002054 if (DBG) log("no change in condition - aborting");
Robert Greenwalt8dcc28b2010-09-23 10:05:56 -07002055 return;
2056 }
2057 NetworkInfo networkInfo = mNetTrackers[mActiveDefaultNetwork].getNetworkInfo();
2058 if (networkInfo.isConnected() == false) {
Wink Savilleed9c02b2010-12-03 12:01:38 -08002059 if (DBG) log("default network not connected - aborting");
Robert Greenwalt8dcc28b2010-09-23 10:05:56 -07002060 return;
2061 }
2062 mDefaultInetConditionPublished = mDefaultInetCondition;
2063 sendInetConditionBroadcast(networkInfo);
2064 return;
Robert Greenwaltd7085fc2010-09-08 15:24:47 -07002065 }
Robert Greenwalt434203a2010-10-11 16:00:27 -07002066
2067 public synchronized ProxyProperties getProxy() {
2068 if (mGlobalProxy != null) return mGlobalProxy;
2069 if (mDefaultProxy != null) return mDefaultProxy;
2070 return null;
2071 }
2072
2073 public void setGlobalProxy(ProxyProperties proxyProperties) {
2074 enforceChangePermission();
2075 synchronized (mGlobalProxyLock) {
2076 if (proxyProperties == mGlobalProxy) return;
2077 if (proxyProperties != null && proxyProperties.equals(mGlobalProxy)) return;
2078 if (mGlobalProxy != null && mGlobalProxy.equals(proxyProperties)) return;
2079
2080 String host = "";
2081 int port = 0;
2082 String exclList = "";
2083 if (proxyProperties != null && !TextUtils.isEmpty(proxyProperties.getHost())) {
2084 mGlobalProxy = new ProxyProperties(proxyProperties);
2085 host = mGlobalProxy.getHost();
2086 port = mGlobalProxy.getPort();
2087 exclList = mGlobalProxy.getExclusionList();
2088 } else {
2089 mGlobalProxy = null;
2090 }
2091 ContentResolver res = mContext.getContentResolver();
2092 Settings.Secure.putString(res, Settings.Secure.GLOBAL_HTTP_PROXY_HOST, host);
2093 Settings.Secure.putInt(res, Settings.Secure.GLOBAL_HTTP_PROXY_PORT, port);
Robert Greenwaltb7090d62010-12-02 11:31:00 -08002094 Settings.Secure.putString(res, Settings.Secure.GLOBAL_HTTP_PROXY_EXCLUSION_LIST,
Robert Greenwalt434203a2010-10-11 16:00:27 -07002095 exclList);
2096 }
2097
2098 if (mGlobalProxy == null) {
2099 proxyProperties = mDefaultProxy;
2100 }
2101 sendProxyBroadcast(proxyProperties);
2102 }
2103
Robert Greenwaltb7090d62010-12-02 11:31:00 -08002104 private void loadGlobalProxy() {
2105 ContentResolver res = mContext.getContentResolver();
2106 String host = Settings.Secure.getString(res, Settings.Secure.GLOBAL_HTTP_PROXY_HOST);
2107 int port = Settings.Secure.getInt(res, Settings.Secure.GLOBAL_HTTP_PROXY_PORT, 0);
2108 String exclList = Settings.Secure.getString(res,
2109 Settings.Secure.GLOBAL_HTTP_PROXY_EXCLUSION_LIST);
2110 if (!TextUtils.isEmpty(host)) {
2111 ProxyProperties proxyProperties = new ProxyProperties(host, port, exclList);
2112 synchronized (mGlobalProxyLock) {
2113 mGlobalProxy = proxyProperties;
2114 }
2115 }
2116 }
2117
Robert Greenwalt434203a2010-10-11 16:00:27 -07002118 public ProxyProperties getGlobalProxy() {
2119 synchronized (mGlobalProxyLock) {
2120 return mGlobalProxy;
2121 }
2122 }
2123
2124 private void handleApplyDefaultProxy(int type) {
2125 // check if new default - push it out to all VM if so
2126 ProxyProperties proxy = mNetTrackers[type].getLinkProperties().getHttpProxy();
2127 synchronized (this) {
2128 if (mDefaultProxy != null && mDefaultProxy.equals(proxy)) return;
2129 if (mDefaultProxy == proxy) return;
2130 if (!TextUtils.isEmpty(proxy.getHost())) {
2131 mDefaultProxy = proxy;
2132 } else {
2133 mDefaultProxy = null;
2134 }
2135 }
Wink Savilleed9c02b2010-12-03 12:01:38 -08002136 if (DBG) log("changing default proxy to " + proxy);
Robert Greenwalt434203a2010-10-11 16:00:27 -07002137 if ((proxy == null && mGlobalProxy == null) || proxy.equals(mGlobalProxy)) return;
2138 if (mGlobalProxy != null) return;
2139 sendProxyBroadcast(proxy);
2140 }
2141
2142 private void handleDeprecatedGlobalHttpProxy() {
2143 String proxy = Settings.Secure.getString(mContext.getContentResolver(),
2144 Settings.Secure.HTTP_PROXY);
2145 if (!TextUtils.isEmpty(proxy)) {
2146 String data[] = proxy.split(":");
2147 String proxyHost = data[0];
2148 int proxyPort = 8080;
2149 if (data.length > 1) {
2150 try {
2151 proxyPort = Integer.parseInt(data[1]);
2152 } catch (NumberFormatException e) {
2153 return;
2154 }
2155 }
2156 ProxyProperties p = new ProxyProperties(data[0], proxyPort, "");
2157 setGlobalProxy(p);
2158 }
2159 }
2160
2161 private void sendProxyBroadcast(ProxyProperties proxy) {
Wink Savilleed9c02b2010-12-03 12:01:38 -08002162 log("sending Proxy Broadcast for " + proxy);
Robert Greenwalt434203a2010-10-11 16:00:27 -07002163 Intent intent = new Intent(Proxy.PROXY_CHANGE_ACTION);
2164 intent.addFlags(Intent.FLAG_RECEIVER_REPLACE_PENDING);
2165 intent.putExtra(Proxy.EXTRA_PROXY_INFO, proxy);
Robert Greenwalta2e13392010-12-06 11:29:17 -08002166 mContext.sendStickyBroadcast(intent);
Robert Greenwalt434203a2010-10-11 16:00:27 -07002167 }
2168
2169 private static class SettingsObserver extends ContentObserver {
2170 private int mWhat;
2171 private Handler mHandler;
2172 SettingsObserver(Handler handler, int what) {
2173 super(handler);
2174 mHandler = handler;
2175 mWhat = what;
2176 }
2177
2178 void observe(Context context) {
2179 ContentResolver resolver = context.getContentResolver();
2180 resolver.registerContentObserver(Settings.Secure.getUriFor(
2181 Settings.Secure.HTTP_PROXY), false, this);
2182 }
2183
2184 @Override
2185 public void onChange(boolean selfChange) {
2186 mHandler.obtainMessage(mWhat).sendToTarget();
2187 }
2188 }
Wink Savilleed9c02b2010-12-03 12:01:38 -08002189
2190 private void log(String s) {
2191 Slog.d(TAG, s);
2192 }
2193
2194 private void loge(String s) {
2195 Slog.e(TAG, s);
2196 }
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002197}