blob: 758f9f3e37714a37779affb510d5e06e9112dee5 [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;
Robert Greenwaltda3d5e62010-12-06 13:56:24 -080027import android.net.DummyDataStateTracker;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080028import android.net.IConnectivityManager;
29import android.net.MobileDataStateTracker;
30import android.net.NetworkInfo;
Robert Greenwalt37e65eb2010-08-30 10:56:47 -070031import android.net.LinkProperties;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080032import android.net.NetworkStateTracker;
Robert Greenwalt585ac0f2010-08-27 09:24:29 -070033import android.net.NetworkUtils;
Robert Greenwalt434203a2010-10-11 16:00:27 -070034import android.net.Proxy;
35import android.net.ProxyProperties;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080036import android.net.wifi.WifiStateTracker;
37import android.os.Binder;
38import android.os.Handler;
Wink Savillebb08caf2010-09-02 19:23:52 -070039import android.os.HandlerThread;
Robert Greenwalt42acef32009-08-12 16:08:25 -070040import android.os.IBinder;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080041import android.os.Looper;
42import android.os.Message;
Robert Greenwalt14f2ef42010-06-15 12:19:37 -070043import android.os.PowerManager;
Robert Greenwalt42acef32009-08-12 16:08:25 -070044import android.os.RemoteException;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080045import android.os.ServiceManager;
46import android.os.SystemProperties;
47import android.provider.Settings;
Robert Greenwalt42acef32009-08-12 16:08:25 -070048import android.text.TextUtils;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080049import android.util.EventLog;
Joe Onorato8a9b2202010-02-26 18:56:32 -080050import android.util.Slog;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080051
Robert Greenwalt42acef32009-08-12 16:08:25 -070052import com.android.internal.telephony.Phone;
53
Robert Greenwaltd0e18ff2010-01-26 11:40:34 -080054import com.android.server.connectivity.Tethering;
55
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080056import java.io.FileDescriptor;
Irfan Sheriffd649c122010-06-09 15:39:36 -070057import java.io.FileWriter;
58import java.io.IOException;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080059import java.io.PrintWriter;
Robert Greenwalt47f69fe2010-06-15 15:43:39 -070060import java.net.InetAddress;
Robert Greenwalt434203a2010-10-11 16:00:27 -070061import java.net.InetSocketAddress;
Robert Greenwalt47f69fe2010-06-15 15:43:39 -070062import java.net.UnknownHostException;
Robert Greenwalt42acef32009-08-12 16:08:25 -070063import java.util.ArrayList;
Robert Greenwalt47f69fe2010-06-15 15:43:39 -070064import java.util.Collection;
Robert Greenwalt4e8dfef2010-09-20 14:35:25 -070065import java.util.GregorianCalendar;
Robert Greenwalt42acef32009-08-12 16:08:25 -070066import java.util.List;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080067
68/**
69 * @hide
70 */
71public class ConnectivityService extends IConnectivityManager.Stub {
72
Robert Greenwaltba175a52010-10-05 19:12:26 -070073 private static final boolean DBG = true;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080074 private static final String TAG = "ConnectivityService";
75
Robert Greenwalt42acef32009-08-12 16:08:25 -070076 // how long to wait before switching back to a radio's default network
77 private static final int RESTORE_DEFAULT_NETWORK_DELAY = 1 * 60 * 1000;
78 // system property that can override the above value
79 private static final String NETWORK_RESTORE_DELAY_PROP_NAME =
80 "android.telephony.apn-restore";
81
Robert Greenwaltd0e18ff2010-01-26 11:40:34 -080082 private Tethering mTethering;
Robert Greenwaltc9d5fb72010-02-25 12:29:30 -080083 private boolean mTetheringConfigValid = false;
Robert Greenwaltd0e18ff2010-01-26 11:40:34 -080084
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080085 /**
86 * Sometimes we want to refer to the individual network state
87 * trackers separately, and sometimes we just want to treat them
88 * abstractly.
89 */
90 private NetworkStateTracker mNetTrackers[];
Robert Greenwalt42acef32009-08-12 16:08:25 -070091
92 /**
93 * A per Net list of the PID's that requested access to the net
94 * used both as a refcount and for per-PID DNS selection
95 */
96 private List mNetRequestersPids[];
97
Irfan Sheriffa2a1b912010-06-07 09:03:04 -070098 private WifiWatchdogService mWifiWatchdogService;
99
Robert Greenwalt42acef32009-08-12 16:08:25 -0700100 // priority order of the nettrackers
101 // (excluding dynamically set mNetworkPreference)
102 // TODO - move mNetworkTypePreference into this
103 private int[] mPriorityList;
104
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800105 private Context mContext;
106 private int mNetworkPreference;
Robert Greenwalt42acef32009-08-12 16:08:25 -0700107 private int mActiveDefaultNetwork = -1;
Robert Greenwaltd7085fc2010-09-08 15:24:47 -0700108 // 0 is full bad, 100 is full good
109 private int mDefaultInetCondition = 0;
110 private int mDefaultInetConditionPublished = 0;
111 private boolean mInetConditionChangeInFlight = false;
112 private int mDefaultConnectionSequence = 0;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800113
114 private int mNumDnsEntries;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800115
116 private boolean mTestMode;
Joe Onorato00092872010-09-01 21:18:22 -0700117 private static ConnectivityService sServiceInstance;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800118
Robert Greenwalt8dcc28b2010-09-23 10:05:56 -0700119 private static final int ENABLED = 1;
120 private static final int DISABLED = 0;
121
122 // Share the event space with NetworkStateTracker (which can't see this
123 // internal class but sends us events). If you change these, change
124 // NetworkStateTracker.java too.
125 private static final int MIN_NETWORK_STATE_TRACKER_EVENT = 1;
126 private static final int MAX_NETWORK_STATE_TRACKER_EVENT = 100;
127
128 /**
129 * used internally as a delayed event to make us switch back to the
130 * default network
131 */
132 private static final int EVENT_RESTORE_DEFAULT_NETWORK =
133 MAX_NETWORK_STATE_TRACKER_EVENT + 1;
134
135 /**
136 * used internally to change our mobile data enabled flag
137 */
138 private static final int EVENT_CHANGE_MOBILE_DATA_ENABLED =
139 MAX_NETWORK_STATE_TRACKER_EVENT + 2;
140
141 /**
142 * used internally to change our network preference setting
143 * arg1 = networkType to prefer
144 */
145 private static final int EVENT_SET_NETWORK_PREFERENCE =
146 MAX_NETWORK_STATE_TRACKER_EVENT + 3;
147
148 /**
149 * used internally to synchronize inet condition reports
150 * arg1 = networkType
151 * arg2 = condition (0 bad, 100 good)
152 */
153 private static final int EVENT_INET_CONDITION_CHANGE =
154 MAX_NETWORK_STATE_TRACKER_EVENT + 4;
155
156 /**
157 * used internally to mark the end of inet condition hold periods
158 * arg1 = networkType
159 */
160 private static final int EVENT_INET_CONDITION_HOLD_END =
161 MAX_NETWORK_STATE_TRACKER_EVENT + 5;
162
163 /**
164 * used internally to set the background data preference
165 * arg1 = TRUE for enabled, FALSE for disabled
166 */
167 private static final int EVENT_SET_BACKGROUND_DATA =
168 MAX_NETWORK_STATE_TRACKER_EVENT + 6;
169
170 /**
171 * used internally to set enable/disable cellular data
172 * arg1 = ENBALED or DISABLED
173 */
174 private static final int EVENT_SET_MOBILE_DATA =
175 MAX_NETWORK_STATE_TRACKER_EVENT + 7;
176
Robert Greenwaltf3331232010-09-24 14:32:21 -0700177 /**
178 * used internally to clear a wakelock when transitioning
179 * from one net to another
180 */
181 private static final int EVENT_CLEAR_NET_TRANSITION_WAKELOCK =
182 MAX_NETWORK_STATE_TRACKER_EVENT + 8;
183
Robert Greenwalt434203a2010-10-11 16:00:27 -0700184 /**
185 * used internally to reload global proxy settings
186 */
187 private static final int EVENT_APPLY_GLOBAL_HTTP_PROXY =
188 MAX_NETWORK_STATE_TRACKER_EVENT + 9;
189
Robert Greenwalt42acef32009-08-12 16:08:25 -0700190 private Handler mHandler;
191
192 // list of DeathRecipients used to make sure features are turned off when
193 // a process dies
194 private List mFeatureUsers;
195
Mike Lockwood0f79b542009-08-14 14:18:49 -0400196 private boolean mSystemReady;
Dianne Hackborn1c633fc2009-12-08 19:45:14 -0800197 private Intent mInitialBroadcast;
Mike Lockwood0f79b542009-08-14 14:18:49 -0400198
Robert Greenwalt14f2ef42010-06-15 12:19:37 -0700199 private PowerManager.WakeLock mNetTransitionWakeLock;
200 private String mNetTransitionWakeLockCausedBy = "";
201 private int mNetTransitionWakeLockSerialNumber;
202 private int mNetTransitionWakeLockTimeout;
203
Robert Greenwalte90aa5e2010-09-01 11:34:05 -0700204 private InetAddress mDefaultDns;
205
Robert Greenwalt4e8dfef2010-09-20 14:35:25 -0700206 // used in DBG mode to track inet condition reports
207 private static final int INET_CONDITION_LOG_MAX_SIZE = 15;
208 private ArrayList mInetLog;
209
Robert Greenwalt434203a2010-10-11 16:00:27 -0700210 // track the current default http proxy - tell the world if we get a new one (real change)
211 private ProxyProperties mDefaultProxy = null;
212 // track the global proxy.
213 private ProxyProperties mGlobalProxy = null;
214 private final Object mGlobalProxyLock = new Object();
215
216 private SettingsObserver mSettingsObserver;
217
Robert Greenwalt511288a2009-12-07 11:33:18 -0800218 private static class NetworkAttributes {
Robert Greenwalt42acef32009-08-12 16:08:25 -0700219 /**
220 * Class for holding settings read from resources.
221 */
222 public String mName;
223 public int mType;
224 public int mRadio;
225 public int mPriority;
Robert Greenwalt511288a2009-12-07 11:33:18 -0800226 public NetworkInfo.State mLastState;
Robert Greenwalt42acef32009-08-12 16:08:25 -0700227 public NetworkAttributes(String init) {
228 String fragments[] = init.split(",");
229 mName = fragments[0].toLowerCase();
Robert Greenwalt5154ae762009-10-30 14:17:42 -0700230 mType = Integer.parseInt(fragments[1]);
231 mRadio = Integer.parseInt(fragments[2]);
232 mPriority = Integer.parseInt(fragments[3]);
Robert Greenwalt511288a2009-12-07 11:33:18 -0800233 mLastState = NetworkInfo.State.UNKNOWN;
Robert Greenwalt42acef32009-08-12 16:08:25 -0700234 }
235 public boolean isDefault() {
236 return (mType == mRadio);
237 }
238 }
239 NetworkAttributes[] mNetAttributes;
Robert Greenwalt5154ae762009-10-30 14:17:42 -0700240 int mNetworksDefined;
Robert Greenwalt42acef32009-08-12 16:08:25 -0700241
Robert Greenwalt511288a2009-12-07 11:33:18 -0800242 private static class RadioAttributes {
Robert Greenwalt42acef32009-08-12 16:08:25 -0700243 public int mSimultaneity;
244 public int mType;
245 public RadioAttributes(String init) {
246 String fragments[] = init.split(",");
Robert Greenwalt5154ae762009-10-30 14:17:42 -0700247 mType = Integer.parseInt(fragments[0]);
248 mSimultaneity = Integer.parseInt(fragments[1]);
Robert Greenwalt42acef32009-08-12 16:08:25 -0700249 }
250 }
251 RadioAttributes[] mRadioAttributes;
252
Wink Savillebb08caf2010-09-02 19:23:52 -0700253 public static synchronized ConnectivityService getInstance(Context context) {
254 if (sServiceInstance == null) {
255 sServiceInstance = new ConnectivityService(context);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800256 }
Wink Savillebb08caf2010-09-02 19:23:52 -0700257 return sServiceInstance;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800258 }
Robert Greenwalt86e9e552009-07-16 17:21:39 -0700259
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800260 private ConnectivityService(Context context) {
Wink Savilleed9c02b2010-12-03 12:01:38 -0800261 if (DBG) log("ConnectivityService starting up");
Robert Greenwaltde8383c2010-01-14 17:47:58 -0800262
Wink Savillebb08caf2010-09-02 19:23:52 -0700263 HandlerThread handlerThread = new HandlerThread("ConnectivityServiceThread");
264 handlerThread.start();
265 mHandler = new MyHandler(handlerThread.getLooper());
266
Robert Greenwaltde8383c2010-01-14 17:47:58 -0800267 // setup our unique device name
Robert Greenwalt733c6292010-12-06 09:30:17 -0800268 if (TextUtils.isEmpty(SystemProperties.get("net.hostname"))) {
269 String id = Settings.Secure.getString(context.getContentResolver(),
270 Settings.Secure.ANDROID_ID);
271 if (id != null && id.length() > 0) {
272 String name = new String("android_").concat(id);
273 SystemProperties.set("net.hostname", name);
274 }
Robert Greenwaltde8383c2010-01-14 17:47:58 -0800275 }
276
Robert Greenwalte90aa5e2010-09-01 11:34:05 -0700277 // read our default dns server ip
278 String dns = Settings.Secure.getString(context.getContentResolver(),
279 Settings.Secure.DEFAULT_DNS_SERVER);
280 if (dns == null || dns.length() == 0) {
281 dns = context.getResources().getString(
282 com.android.internal.R.string.config_default_dns_server);
283 }
284 try {
285 mDefaultDns = InetAddress.getByName(dns);
286 } catch (UnknownHostException e) {
Wink Savilleed9c02b2010-12-03 12:01:38 -0800287 loge("Error setting defaultDns using " + dns);
Robert Greenwalte90aa5e2010-09-01 11:34:05 -0700288 }
289
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800290 mContext = context;
Robert Greenwalt14f2ef42010-06-15 12:19:37 -0700291
292 PowerManager powerManager = (PowerManager)mContext.getSystemService(Context.POWER_SERVICE);
293 mNetTransitionWakeLock = powerManager.newWakeLock(PowerManager.PARTIAL_WAKE_LOCK, TAG);
294 mNetTransitionWakeLockTimeout = mContext.getResources().getInteger(
295 com.android.internal.R.integer.config_networkTransitionTimeout);
296
Robert Greenwalt42acef32009-08-12 16:08:25 -0700297 mNetTrackers = new NetworkStateTracker[
298 ConnectivityManager.MAX_NETWORK_TYPE+1];
Robert Greenwalt86e9e552009-07-16 17:21:39 -0700299
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800300 mNetworkPreference = getPersistedNetworkPreference();
Robert Greenwalt86e9e552009-07-16 17:21:39 -0700301
Robert Greenwalt5154ae762009-10-30 14:17:42 -0700302 mRadioAttributes = new RadioAttributes[ConnectivityManager.MAX_RADIO_TYPE+1];
303 mNetAttributes = new NetworkAttributes[ConnectivityManager.MAX_NETWORK_TYPE+1];
304
Robert Greenwalt42acef32009-08-12 16:08:25 -0700305 // Load device network attributes from resources
Robert Greenwalt42acef32009-08-12 16:08:25 -0700306 String[] raStrings = context.getResources().getStringArray(
307 com.android.internal.R.array.radioAttributes);
Robert Greenwalt5154ae762009-10-30 14:17:42 -0700308 for (String raString : raStrings) {
309 RadioAttributes r = new RadioAttributes(raString);
310 if (r.mType > ConnectivityManager.MAX_RADIO_TYPE) {
Wink Savilleed9c02b2010-12-03 12:01:38 -0800311 loge("Error in radioAttributes - ignoring attempt to define type " + r.mType);
Robert Greenwalt5154ae762009-10-30 14:17:42 -0700312 continue;
313 }
314 if (mRadioAttributes[r.mType] != null) {
Wink Savilleed9c02b2010-12-03 12:01:38 -0800315 loge("Error in radioAttributes - ignoring attempt to redefine type " +
Robert Greenwalt5154ae762009-10-30 14:17:42 -0700316 r.mType);
317 continue;
318 }
Robert Greenwalt42acef32009-08-12 16:08:25 -0700319 mRadioAttributes[r.mType] = r;
320 }
321
Robert Greenwalt5154ae762009-10-30 14:17:42 -0700322 String[] naStrings = context.getResources().getStringArray(
323 com.android.internal.R.array.networkAttributes);
324 for (String naString : naStrings) {
325 try {
326 NetworkAttributes n = new NetworkAttributes(naString);
327 if (n.mType > ConnectivityManager.MAX_NETWORK_TYPE) {
Wink Savilleed9c02b2010-12-03 12:01:38 -0800328 loge("Error in networkAttributes - ignoring attempt to define type " +
Robert Greenwalt5154ae762009-10-30 14:17:42 -0700329 n.mType);
330 continue;
Robert Greenwalt42acef32009-08-12 16:08:25 -0700331 }
Robert Greenwalt5154ae762009-10-30 14:17:42 -0700332 if (mNetAttributes[n.mType] != null) {
Wink Savilleed9c02b2010-12-03 12:01:38 -0800333 loge("Error in networkAttributes - ignoring attempt to redefine type " +
Robert Greenwalt5154ae762009-10-30 14:17:42 -0700334 n.mType);
335 continue;
336 }
337 if (mRadioAttributes[n.mRadio] == null) {
Wink Savilleed9c02b2010-12-03 12:01:38 -0800338 loge("Error in networkAttributes - ignoring attempt to use undefined " +
Robert Greenwalt5154ae762009-10-30 14:17:42 -0700339 "radio " + n.mRadio + " in network type " + n.mType);
340 continue;
341 }
342 mNetAttributes[n.mType] = n;
343 mNetworksDefined++;
344 } catch(Exception e) {
345 // ignore it - leave the entry null
Robert Greenwalt42acef32009-08-12 16:08:25 -0700346 }
347 }
348
Robert Greenwalt5154ae762009-10-30 14:17:42 -0700349 // high priority first
350 mPriorityList = new int[mNetworksDefined];
351 {
352 int insertionPoint = mNetworksDefined-1;
353 int currentLowest = 0;
354 int nextLowest = 0;
355 while (insertionPoint > -1) {
356 for (NetworkAttributes na : mNetAttributes) {
357 if (na == null) continue;
358 if (na.mPriority < currentLowest) continue;
359 if (na.mPriority > currentLowest) {
360 if (na.mPriority < nextLowest || nextLowest == 0) {
361 nextLowest = na.mPriority;
362 }
363 continue;
364 }
365 mPriorityList[insertionPoint--] = na.mType;
366 }
367 currentLowest = nextLowest;
368 nextLowest = 0;
369 }
370 }
371
372 mNetRequestersPids = new ArrayList[ConnectivityManager.MAX_NETWORK_TYPE+1];
373 for (int i : mPriorityList) {
Robert Greenwalt42acef32009-08-12 16:08:25 -0700374 mNetRequestersPids[i] = new ArrayList();
375 }
376
377 mFeatureUsers = new ArrayList();
378
Robert Greenwalt5154ae762009-10-30 14:17:42 -0700379 mNumDnsEntries = 0;
380
381 mTestMode = SystemProperties.get("cm.test.mode").equals("true")
382 && SystemProperties.get("ro.build.type").equals("eng");
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800383 /*
384 * Create the network state trackers for Wi-Fi and mobile
385 * data. Maybe this could be done with a factory class,
386 * but it's not clear that it's worth it, given that
387 * the number of different network types is not going
388 * to change very often.
389 */
Robert Greenwaltc03fa502010-02-23 18:58:05 -0800390 boolean noMobileData = !getMobileDataEnabled();
Robert Greenwalt5154ae762009-10-30 14:17:42 -0700391 for (int netType : mPriorityList) {
392 switch (mNetAttributes[netType].mRadio) {
393 case ConnectivityManager.TYPE_WIFI:
Wink Savilleed9c02b2010-12-03 12:01:38 -0800394 if (DBG) log("Starting Wifi Service.");
Wink Savillec7a98342010-08-13 16:11:42 -0700395 WifiStateTracker wst = new WifiStateTracker();
Irfan Sheriff0d255342010-07-28 09:35:20 -0700396 WifiService wifiService = new WifiService(context);
Robert Greenwalt5154ae762009-10-30 14:17:42 -0700397 ServiceManager.addService(Context.WIFI_SERVICE, wifiService);
Irfan Sheriff0d255342010-07-28 09:35:20 -0700398 wifiService.checkAndStartWifi();
Robert Greenwalt5154ae762009-10-30 14:17:42 -0700399 mNetTrackers[ConnectivityManager.TYPE_WIFI] = wst;
Wink Savillec7a98342010-08-13 16:11:42 -0700400 wst.startMonitoring(context, mHandler);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800401
Irfan Sheriffa2a1b912010-06-07 09:03:04 -0700402 //TODO: as part of WWS refactor, create only when needed
Irfan Sheriff0d255342010-07-28 09:35:20 -0700403 mWifiWatchdogService = new WifiWatchdogService(context);
Irfan Sheriffa2a1b912010-06-07 09:03:04 -0700404
Robert Greenwalt5154ae762009-10-30 14:17:42 -0700405 break;
406 case ConnectivityManager.TYPE_MOBILE:
Wink Savillec7a98342010-08-13 16:11:42 -0700407 mNetTrackers[netType] = new MobileDataStateTracker(netType,
408 mNetAttributes[netType].mName);
409 mNetTrackers[netType].startMonitoring(context, mHandler);
Robert Greenwaltc03fa502010-02-23 18:58:05 -0800410 if (noMobileData) {
Wink Savilleed9c02b2010-12-03 12:01:38 -0800411 if (DBG) log("tearing down Mobile networks due to setting");
Robert Greenwaltc03fa502010-02-23 18:58:05 -0800412 mNetTrackers[netType].teardown();
413 }
Robert Greenwalt5154ae762009-10-30 14:17:42 -0700414 break;
Robert Greenwaltda3d5e62010-12-06 13:56:24 -0800415 case ConnectivityManager.TYPE_DUMMY:
416 mNetTrackers[netType] = new DummyDataStateTracker(netType,
417 mNetAttributes[netType].mName);
418 mNetTrackers[netType].startMonitoring(context, mHandler);
419 break;
Robert Greenwalt5154ae762009-10-30 14:17:42 -0700420 default:
Wink Savilleed9c02b2010-12-03 12:01:38 -0800421 loge("Trying to create a DataStateTracker for an unknown radio type " +
Robert Greenwalt5154ae762009-10-30 14:17:42 -0700422 mNetAttributes[netType].mRadio);
423 continue;
424 }
425 }
Robert Greenwaltd0e18ff2010-01-26 11:40:34 -0800426
Robert Greenwaltdfadaea2010-03-11 15:03:08 -0800427 mTethering = new Tethering(mContext, mHandler.getLooper());
Robert Greenwaltc9d5fb72010-02-25 12:29:30 -0800428 mTetheringConfigValid = (((mNetTrackers[ConnectivityManager.TYPE_MOBILE_DUN] != null) ||
429 !mTethering.isDunRequired()) &&
430 (mTethering.getTetherableUsbRegexs().length != 0 ||
Danica Chang6fdd0c62010-08-11 14:54:43 -0700431 mTethering.getTetherableWifiRegexs().length != 0 ||
432 mTethering.getTetherableBluetoothRegexs().length != 0) &&
Robert Greenwaltc9d5fb72010-02-25 12:29:30 -0800433 mTethering.getUpstreamIfaceRegexs().length != 0);
434
Robert Greenwalt4e8dfef2010-09-20 14:35:25 -0700435 if (DBG) {
436 mInetLog = new ArrayList();
437 }
Robert Greenwalt434203a2010-10-11 16:00:27 -0700438
439 mSettingsObserver = new SettingsObserver(mHandler, EVENT_APPLY_GLOBAL_HTTP_PROXY);
440 mSettingsObserver.observe(mContext);
Robert Greenwaltb7090d62010-12-02 11:31:00 -0800441
442 loadGlobalProxy();
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800443 }
444
Robert Greenwalt5154ae762009-10-30 14:17:42 -0700445
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800446 /**
Robert Greenwalt86e9e552009-07-16 17:21:39 -0700447 * Sets the preferred network.
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800448 * @param preference the new preference
449 */
Robert Greenwalt8dcc28b2010-09-23 10:05:56 -0700450 public void setNetworkPreference(int preference) {
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800451 enforceChangePermission();
Robert Greenwalt8dcc28b2010-09-23 10:05:56 -0700452
453 mHandler.sendMessage(mHandler.obtainMessage(EVENT_SET_NETWORK_PREFERENCE, preference, 0));
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800454 }
455
456 public int getNetworkPreference() {
457 enforceAccessPermission();
Robert Greenwalt8dcc28b2010-09-23 10:05:56 -0700458 int preference;
459 synchronized(this) {
460 preference = mNetworkPreference;
461 }
462 return preference;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800463 }
464
Robert Greenwalt8dcc28b2010-09-23 10:05:56 -0700465 private void handleSetNetworkPreference(int preference) {
466 if (ConnectivityManager.isNetworkTypeValid(preference) &&
467 mNetAttributes[preference] != null &&
468 mNetAttributes[preference].isDefault()) {
469 if (mNetworkPreference != preference) {
470 final ContentResolver cr = mContext.getContentResolver();
471 Settings.Secure.putInt(cr, Settings.Secure.NETWORK_PREFERENCE, preference);
472 synchronized(this) {
473 mNetworkPreference = preference;
474 }
475 enforcePreference();
476 }
477 }
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800478 }
Robert Greenwalt86e9e552009-07-16 17:21:39 -0700479
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800480 private int getPersistedNetworkPreference() {
481 final ContentResolver cr = mContext.getContentResolver();
482
483 final int networkPrefSetting = Settings.Secure
484 .getInt(cr, Settings.Secure.NETWORK_PREFERENCE, -1);
485 if (networkPrefSetting != -1) {
486 return networkPrefSetting;
487 }
488
489 return ConnectivityManager.DEFAULT_NETWORK_PREFERENCE;
490 }
Robert Greenwalt86e9e552009-07-16 17:21:39 -0700491
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800492 /**
Robert Greenwalt86e9e552009-07-16 17:21:39 -0700493 * Make the state of network connectivity conform to the preference settings
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800494 * In this method, we only tear down a non-preferred network. Establishing
495 * a connection to the preferred network is taken care of when we handle
496 * the disconnect event from the non-preferred network
497 * (see {@link #handleDisconnect(NetworkInfo)}).
498 */
499 private void enforcePreference() {
Robert Greenwalt42acef32009-08-12 16:08:25 -0700500 if (mNetTrackers[mNetworkPreference].getNetworkInfo().isConnected())
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800501 return;
502
Robert Greenwalt42acef32009-08-12 16:08:25 -0700503 if (!mNetTrackers[mNetworkPreference].isAvailable())
504 return;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800505
Robert Greenwalt42acef32009-08-12 16:08:25 -0700506 for (int t=0; t <= ConnectivityManager.MAX_RADIO_TYPE; t++) {
Robert Greenwalt5154ae762009-10-30 14:17:42 -0700507 if (t != mNetworkPreference && mNetTrackers[t] != null &&
Robert Greenwalt42acef32009-08-12 16:08:25 -0700508 mNetTrackers[t].getNetworkInfo().isConnected()) {
Robert Greenwaltec9fe462009-08-20 15:25:14 -0700509 if (DBG) {
Wink Savilleed9c02b2010-12-03 12:01:38 -0800510 log("tearing down " + mNetTrackers[t].getNetworkInfo() +
Robert Greenwaltec9fe462009-08-20 15:25:14 -0700511 " in enforcePreference");
512 }
Robert Greenwalt42acef32009-08-12 16:08:25 -0700513 teardown(mNetTrackers[t]);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800514 }
515 }
516 }
517
518 private boolean teardown(NetworkStateTracker netTracker) {
519 if (netTracker.teardown()) {
520 netTracker.setTeardownRequested(true);
521 return true;
522 } else {
523 return false;
524 }
525 }
526
527 /**
528 * Return NetworkInfo for the active (i.e., connected) network interface.
529 * It is assumed that at most one network is active at a time. If more
530 * than one is active, it is indeterminate which will be returned.
Robert Greenwalt86e9e552009-07-16 17:21:39 -0700531 * @return the info for the active network, or {@code null} if none is
532 * active
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800533 */
534 public NetworkInfo getActiveNetworkInfo() {
535 enforceAccessPermission();
Robert Greenwalt42acef32009-08-12 16:08:25 -0700536 for (int type=0; type <= ConnectivityManager.MAX_NETWORK_TYPE; type++) {
Robert Greenwalt5154ae762009-10-30 14:17:42 -0700537 if (mNetAttributes[type] == null || !mNetAttributes[type].isDefault()) {
Robert Greenwalt42acef32009-08-12 16:08:25 -0700538 continue;
539 }
540 NetworkStateTracker t = mNetTrackers[type];
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800541 NetworkInfo info = t.getNetworkInfo();
542 if (info.isConnected()) {
Wink Savilleed9c02b2010-12-03 12:01:38 -0800543 if (DBG && type != mActiveDefaultNetwork) {
544 loge("connected default network is not mActiveDefaultNetwork!");
545 }
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800546 return info;
547 }
548 }
549 return null;
550 }
551
552 public NetworkInfo getNetworkInfo(int networkType) {
553 enforceAccessPermission();
554 if (ConnectivityManager.isNetworkTypeValid(networkType)) {
555 NetworkStateTracker t = mNetTrackers[networkType];
556 if (t != null)
557 return t.getNetworkInfo();
558 }
559 return null;
560 }
561
562 public NetworkInfo[] getAllNetworkInfo() {
563 enforceAccessPermission();
Robert Greenwalt5154ae762009-10-30 14:17:42 -0700564 NetworkInfo[] result = new NetworkInfo[mNetworksDefined];
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800565 int i = 0;
566 for (NetworkStateTracker t : mNetTrackers) {
Robert Greenwalt5154ae762009-10-30 14:17:42 -0700567 if(t != null) result[i++] = t.getNetworkInfo();
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800568 }
569 return result;
570 }
571
Robert Greenwaltd192dad2010-09-14 09:18:02 -0700572 /**
573 * Return LinkProperties for the active (i.e., connected) default
574 * network interface. It is assumed that at most one default network
575 * is active at a time. If more than one is active, it is indeterminate
576 * which will be returned.
577 * @return the ip properties for the active network, or {@code null} if
578 * none is active
579 */
580 public LinkProperties getActiveLinkProperties() {
581 enforceAccessPermission();
582 for (int type=0; type <= ConnectivityManager.MAX_NETWORK_TYPE; type++) {
583 if (mNetAttributes[type] == null || !mNetAttributes[type].isDefault()) {
584 continue;
585 }
586 NetworkStateTracker t = mNetTrackers[type];
587 NetworkInfo info = t.getNetworkInfo();
588 if (info.isConnected()) {
589 return t.getLinkProperties();
590 }
591 }
592 return null;
593 }
594
595 public LinkProperties getLinkProperties(int networkType) {
596 enforceAccessPermission();
597 if (ConnectivityManager.isNetworkTypeValid(networkType)) {
598 NetworkStateTracker t = mNetTrackers[networkType];
599 if (t != null) return t.getLinkProperties();
600 }
601 return null;
602 }
603
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800604 public boolean setRadios(boolean turnOn) {
605 boolean result = true;
606 enforceChangePermission();
607 for (NetworkStateTracker t : mNetTrackers) {
Robert Greenwalt5154ae762009-10-30 14:17:42 -0700608 if (t != null) result = t.setRadio(turnOn) && result;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800609 }
610 return result;
611 }
612
613 public boolean setRadio(int netType, boolean turnOn) {
614 enforceChangePermission();
615 if (!ConnectivityManager.isNetworkTypeValid(netType)) {
616 return false;
617 }
618 NetworkStateTracker tracker = mNetTrackers[netType];
619 return tracker != null && tracker.setRadio(turnOn);
620 }
621
Robert Greenwalt9c75d4a2009-09-27 17:27:04 -0700622 /**
623 * Used to notice when the calling process dies so we can self-expire
624 *
625 * Also used to know if the process has cleaned up after itself when
626 * our auto-expire timer goes off. The timer has a link to an object.
627 *
628 */
Robert Greenwalt42acef32009-08-12 16:08:25 -0700629 private class FeatureUser implements IBinder.DeathRecipient {
630 int mNetworkType;
631 String mFeature;
632 IBinder mBinder;
633 int mPid;
634 int mUid;
Robert Greenwaltb9285352009-12-21 18:24:07 -0800635 long mCreateTime;
Robert Greenwalt42acef32009-08-12 16:08:25 -0700636
637 FeatureUser(int type, String feature, IBinder binder) {
638 super();
639 mNetworkType = type;
640 mFeature = feature;
641 mBinder = binder;
642 mPid = getCallingPid();
643 mUid = getCallingUid();
Robert Greenwaltb9285352009-12-21 18:24:07 -0800644 mCreateTime = System.currentTimeMillis();
Robert Greenwalt9c75d4a2009-09-27 17:27:04 -0700645
Robert Greenwalt42acef32009-08-12 16:08:25 -0700646 try {
647 mBinder.linkToDeath(this, 0);
648 } catch (RemoteException e) {
649 binderDied();
650 }
651 }
652
653 void unlinkDeathRecipient() {
654 mBinder.unlinkToDeath(this, 0);
655 }
656
657 public void binderDied() {
Wink Savilleed9c02b2010-12-03 12:01:38 -0800658 log("ConnectivityService FeatureUser binderDied(" +
Robert Greenwaltb9285352009-12-21 18:24:07 -0800659 mNetworkType + ", " + mFeature + ", " + mBinder + "), created " +
660 (System.currentTimeMillis() - mCreateTime) + " mSec ago");
Robert Greenwalt9c75d4a2009-09-27 17:27:04 -0700661 stopUsingNetworkFeature(this, false);
Robert Greenwalt42acef32009-08-12 16:08:25 -0700662 }
663
Robert Greenwalt9c75d4a2009-09-27 17:27:04 -0700664 public void expire() {
Wink Savilleed9c02b2010-12-03 12:01:38 -0800665 log("ConnectivityService FeatureUser expire(" +
Robert Greenwaltb9285352009-12-21 18:24:07 -0800666 mNetworkType + ", " + mFeature + ", " + mBinder +"), created " +
667 (System.currentTimeMillis() - mCreateTime) + " mSec ago");
Robert Greenwalt9c75d4a2009-09-27 17:27:04 -0700668 stopUsingNetworkFeature(this, false);
669 }
Robert Greenwaltb9285352009-12-21 18:24:07 -0800670
671 public String toString() {
672 return "FeatureUser("+mNetworkType+","+mFeature+","+mPid+","+mUid+"), created " +
673 (System.currentTimeMillis() - mCreateTime) + " mSec ago";
674 }
Robert Greenwalt42acef32009-08-12 16:08:25 -0700675 }
676
Robert Greenwalt9c75d4a2009-09-27 17:27:04 -0700677 // javadoc from interface
Robert Greenwalt42acef32009-08-12 16:08:25 -0700678 public int startUsingNetworkFeature(int networkType, String feature,
679 IBinder binder) {
680 if (DBG) {
Wink Savilleed9c02b2010-12-03 12:01:38 -0800681 log("startUsingNetworkFeature for net " + networkType + ": " + feature);
Robert Greenwalt42acef32009-08-12 16:08:25 -0700682 }
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800683 enforceChangePermission();
Robert Greenwalt5154ae762009-10-30 14:17:42 -0700684 if (!ConnectivityManager.isNetworkTypeValid(networkType) ||
685 mNetAttributes[networkType] == null) {
Robert Greenwalt42acef32009-08-12 16:08:25 -0700686 return Phone.APN_REQUEST_FAILED;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800687 }
Robert Greenwalt42acef32009-08-12 16:08:25 -0700688
Robert Greenwalt9c75d4a2009-09-27 17:27:04 -0700689 FeatureUser f = new FeatureUser(networkType, feature, binder);
Robert Greenwalt42acef32009-08-12 16:08:25 -0700690
691 // TODO - move this into the MobileDataStateTracker
692 int usedNetworkType = networkType;
693 if(networkType == ConnectivityManager.TYPE_MOBILE) {
Robert Greenwaltc03fa502010-02-23 18:58:05 -0800694 if (!getMobileDataEnabled()) {
Wink Savilleed9c02b2010-12-03 12:01:38 -0800695 if (DBG) log("requested special network with data disabled - rejected");
Robert Greenwaltc03fa502010-02-23 18:58:05 -0800696 return Phone.APN_TYPE_NOT_AVAILABLE;
697 }
Robert Greenwalt42acef32009-08-12 16:08:25 -0700698 if (TextUtils.equals(feature, Phone.FEATURE_ENABLE_MMS)) {
699 usedNetworkType = ConnectivityManager.TYPE_MOBILE_MMS;
700 } else if (TextUtils.equals(feature, Phone.FEATURE_ENABLE_SUPL)) {
701 usedNetworkType = ConnectivityManager.TYPE_MOBILE_SUPL;
702 } else if (TextUtils.equals(feature, Phone.FEATURE_ENABLE_DUN)) {
703 usedNetworkType = ConnectivityManager.TYPE_MOBILE_DUN;
704 } else if (TextUtils.equals(feature, Phone.FEATURE_ENABLE_HIPRI)) {
705 usedNetworkType = ConnectivityManager.TYPE_MOBILE_HIPRI;
706 }
707 }
708 NetworkStateTracker network = mNetTrackers[usedNetworkType];
709 if (network != null) {
710 if (usedNetworkType != networkType) {
711 Integer currentPid = new Integer(getCallingPid());
712
713 NetworkStateTracker radio = mNetTrackers[networkType];
714 NetworkInfo ni = network.getNetworkInfo();
715
716 if (ni.isAvailable() == false) {
Wink Savilleed9c02b2010-12-03 12:01:38 -0800717 if (DBG) log("special network not available");
Robert Greenwalt42acef32009-08-12 16:08:25 -0700718 return Phone.APN_TYPE_NOT_AVAILABLE;
719 }
720
Robert Greenwalt9c75d4a2009-09-27 17:27:04 -0700721 synchronized(this) {
722 mFeatureUsers.add(f);
723 if (!mNetRequestersPids[usedNetworkType].contains(currentPid)) {
724 // this gets used for per-pid dns when connected
725 mNetRequestersPids[usedNetworkType].add(currentPid);
726 }
Robert Greenwalt42acef32009-08-12 16:08:25 -0700727 }
Robert Greenwalt8dcc28b2010-09-23 10:05:56 -0700728 mHandler.sendMessageDelayed(mHandler.obtainMessage(EVENT_RESTORE_DEFAULT_NETWORK,
Robert Greenwalt9c75d4a2009-09-27 17:27:04 -0700729 f), getRestoreDefaultNetworkDelay());
730
Robert Greenwalt42acef32009-08-12 16:08:25 -0700731
Robert Greenwalta64bf832009-08-19 20:19:33 -0700732 if ((ni.isConnectedOrConnecting() == true) &&
733 !network.isTeardownRequested()) {
Robert Greenwalt42acef32009-08-12 16:08:25 -0700734 if (ni.isConnected() == true) {
735 // add the pid-specific dns
Robert Greenwaltc76b8fa2010-07-23 15:46:26 -0700736 handleDnsConfigurationChange(networkType);
Wink Savilleed9c02b2010-12-03 12:01:38 -0800737 if (DBG) log("special network already active");
Robert Greenwalt42acef32009-08-12 16:08:25 -0700738 return Phone.APN_ALREADY_ACTIVE;
739 }
Wink Savilleed9c02b2010-12-03 12:01:38 -0800740 if (DBG) log("special network already connecting");
Robert Greenwalt42acef32009-08-12 16:08:25 -0700741 return Phone.APN_REQUEST_STARTED;
742 }
743
744 // check if the radio in play can make another contact
745 // assume if cannot for now
746
Wink Savilleed9c02b2010-12-03 12:01:38 -0800747 if (DBG) log("reconnecting to special network");
Robert Greenwalt42acef32009-08-12 16:08:25 -0700748 network.reconnect();
749 return Phone.APN_REQUEST_STARTED;
750 } else {
Robert Greenwalt02648a42010-05-18 10:52:51 -0700751 return -1;
Robert Greenwalt42acef32009-08-12 16:08:25 -0700752 }
753 }
754 return Phone.APN_TYPE_NOT_AVAILABLE;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800755 }
756
Robert Greenwalt9c75d4a2009-09-27 17:27:04 -0700757 // javadoc from interface
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800758 public int stopUsingNetworkFeature(int networkType, String feature) {
Robert Greenwaltb8f16342009-10-06 17:52:40 -0700759 enforceChangePermission();
760
Robert Greenwalt9c75d4a2009-09-27 17:27:04 -0700761 int pid = getCallingPid();
762 int uid = getCallingUid();
763
764 FeatureUser u = null;
765 boolean found = false;
766
767 synchronized(this) {
768 for (int i = 0; i < mFeatureUsers.size() ; i++) {
769 u = (FeatureUser)mFeatureUsers.get(i);
770 if (uid == u.mUid && pid == u.mPid &&
771 networkType == u.mNetworkType &&
772 TextUtils.equals(feature, u.mFeature)) {
773 found = true;
774 break;
775 }
776 }
777 }
778 if (found && u != null) {
779 // stop regardless of how many other time this proc had called start
780 return stopUsingNetworkFeature(u, true);
781 } else {
782 // none found!
Wink Savilleed9c02b2010-12-03 12:01:38 -0800783 if (DBG) log("ignoring stopUsingNetworkFeature - not a live request");
Robert Greenwalt9c75d4a2009-09-27 17:27:04 -0700784 return 1;
785 }
Robert Greenwalt42acef32009-08-12 16:08:25 -0700786 }
787
Robert Greenwalt9c75d4a2009-09-27 17:27:04 -0700788 private int stopUsingNetworkFeature(FeatureUser u, boolean ignoreDups) {
789 int networkType = u.mNetworkType;
790 String feature = u.mFeature;
791 int pid = u.mPid;
792 int uid = u.mUid;
793
794 NetworkStateTracker tracker = null;
795 boolean callTeardown = false; // used to carry our decision outside of sync block
796
Robert Greenwalt42acef32009-08-12 16:08:25 -0700797 if (DBG) {
Wink Savilleed9c02b2010-12-03 12:01:38 -0800798 log("stopUsingNetworkFeature for net " + networkType +
Robert Greenwalt42acef32009-08-12 16:08:25 -0700799 ": " + feature);
800 }
Robert Greenwaltb8f16342009-10-06 17:52:40 -0700801
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800802 if (!ConnectivityManager.isNetworkTypeValid(networkType)) {
803 return -1;
804 }
Robert Greenwalt42acef32009-08-12 16:08:25 -0700805
Robert Greenwalt9c75d4a2009-09-27 17:27:04 -0700806 // need to link the mFeatureUsers list with the mNetRequestersPids state in this
807 // sync block
808 synchronized(this) {
809 // check if this process still has an outstanding start request
810 if (!mFeatureUsers.contains(u)) {
Wink Savilleed9c02b2010-12-03 12:01:38 -0800811 if (DBG) log("ignoring - this process has no outstanding requests");
Robert Greenwalt42acef32009-08-12 16:08:25 -0700812 return 1;
813 }
Robert Greenwalt9c75d4a2009-09-27 17:27:04 -0700814 u.unlinkDeathRecipient();
815 mFeatureUsers.remove(mFeatureUsers.indexOf(u));
816 // If we care about duplicate requests, check for that here.
817 //
818 // This is done to support the extension of a request - the app
819 // can request we start the network feature again and renew the
820 // auto-shutoff delay. Normal "stop" calls from the app though
821 // do not pay attention to duplicate requests - in effect the
822 // API does not refcount and a single stop will counter multiple starts.
823 if (ignoreDups == false) {
824 for (int i = 0; i < mFeatureUsers.size() ; i++) {
825 FeatureUser x = (FeatureUser)mFeatureUsers.get(i);
826 if (x.mUid == u.mUid && x.mPid == u.mPid &&
827 x.mNetworkType == u.mNetworkType &&
828 TextUtils.equals(x.mFeature, u.mFeature)) {
Wink Savilleed9c02b2010-12-03 12:01:38 -0800829 if (DBG) log("ignoring stopUsingNetworkFeature as dup is found");
Robert Greenwalt9c75d4a2009-09-27 17:27:04 -0700830 return 1;
831 }
832 }
Robert Greenwalt42acef32009-08-12 16:08:25 -0700833 }
Robert Greenwalt9c75d4a2009-09-27 17:27:04 -0700834
835 // TODO - move to MobileDataStateTracker
836 int usedNetworkType = networkType;
837 if (networkType == ConnectivityManager.TYPE_MOBILE) {
838 if (TextUtils.equals(feature, Phone.FEATURE_ENABLE_MMS)) {
839 usedNetworkType = ConnectivityManager.TYPE_MOBILE_MMS;
840 } else if (TextUtils.equals(feature, Phone.FEATURE_ENABLE_SUPL)) {
841 usedNetworkType = ConnectivityManager.TYPE_MOBILE_SUPL;
842 } else if (TextUtils.equals(feature, Phone.FEATURE_ENABLE_DUN)) {
843 usedNetworkType = ConnectivityManager.TYPE_MOBILE_DUN;
844 } else if (TextUtils.equals(feature, Phone.FEATURE_ENABLE_HIPRI)) {
845 usedNetworkType = ConnectivityManager.TYPE_MOBILE_HIPRI;
846 }
847 }
848 tracker = mNetTrackers[usedNetworkType];
Robert Greenwalt5154ae762009-10-30 14:17:42 -0700849 if (tracker == null) {
Wink Savilleed9c02b2010-12-03 12:01:38 -0800850 if (DBG) log("ignoring - no known tracker for net type " + usedNetworkType);
Robert Greenwalt5154ae762009-10-30 14:17:42 -0700851 return -1;
852 }
853 if (usedNetworkType != networkType) {
Robert Greenwalt9c75d4a2009-09-27 17:27:04 -0700854 Integer currentPid = new Integer(pid);
Robert Greenwalt9c75d4a2009-09-27 17:27:04 -0700855 mNetRequestersPids[usedNetworkType].remove(currentPid);
Robert Greenwalt421c72b2009-12-17 14:54:59 -0800856 reassessPidDns(pid, true);
Robert Greenwalt9c75d4a2009-09-27 17:27:04 -0700857 if (mNetRequestersPids[usedNetworkType].size() != 0) {
Wink Savilleed9c02b2010-12-03 12:01:38 -0800858 if (DBG) log("not tearing down special network - " +
Robert Greenwalt9c75d4a2009-09-27 17:27:04 -0700859 "others still using it");
860 return 1;
861 }
862 callTeardown = true;
863 }
864 }
Wink Savilleed9c02b2010-12-03 12:01:38 -0800865 if (DBG) log("Doing network teardown");
Robert Greenwalt9c75d4a2009-09-27 17:27:04 -0700866 if (callTeardown) {
867 tracker.teardown();
Robert Greenwalt42acef32009-08-12 16:08:25 -0700868 return 1;
869 } else {
Robert Greenwalt02648a42010-05-18 10:52:51 -0700870 return -1;
Robert Greenwalt42acef32009-08-12 16:08:25 -0700871 }
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800872 }
873
874 /**
Robert Greenwalt585ac0f2010-08-27 09:24:29 -0700875 * @deprecated use requestRouteToHostAddress instead
876 *
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800877 * Ensure that a network route exists to deliver traffic to the specified
878 * host via the specified network interface.
Robert Greenwalt86e9e552009-07-16 17:21:39 -0700879 * @param networkType the type of the network over which traffic to the
880 * specified host is to be routed
881 * @param hostAddress the IP address of the host to which the route is
882 * desired
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800883 * @return {@code true} on success, {@code false} on failure
884 */
885 public boolean requestRouteToHost(int networkType, int hostAddress) {
Robert Greenwalt585ac0f2010-08-27 09:24:29 -0700886 InetAddress inetAddress = NetworkUtils.intToInetAddress(hostAddress);
887
888 if (inetAddress == null) {
889 return false;
890 }
891
892 return requestRouteToHostAddress(networkType, inetAddress.getAddress());
893 }
894
895 /**
896 * Ensure that a network route exists to deliver traffic to the specified
897 * host via the specified network interface.
898 * @param networkType the type of the network over which traffic to the
899 * specified host is to be routed
900 * @param hostAddress the IP address of the host to which the route is
901 * desired
902 * @return {@code true} on success, {@code false} on failure
903 */
904 public boolean requestRouteToHostAddress(int networkType, byte[] hostAddress) {
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800905 enforceChangePermission();
906 if (!ConnectivityManager.isNetworkTypeValid(networkType)) {
907 return false;
908 }
909 NetworkStateTracker tracker = mNetTrackers[networkType];
Robert Greenwalt8206ff32009-09-10 15:06:20 -0700910
Robert Greenwalt5154ae762009-10-30 14:17:42 -0700911 if (tracker == null || !tracker.getNetworkInfo().isConnected() ||
912 tracker.isTeardownRequested()) {
Robert Greenwalt8206ff32009-09-10 15:06:20 -0700913 if (DBG) {
Wink Savilleed9c02b2010-12-03 12:01:38 -0800914 log("requestRouteToHostAddress on down network " +
Robert Greenwalt585ac0f2010-08-27 09:24:29 -0700915 "(" + networkType + ") - dropped");
Robert Greenwalt8206ff32009-09-10 15:06:20 -0700916 }
917 return false;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800918 }
Robert Greenwalt47f69fe2010-06-15 15:43:39 -0700919 try {
Robert Greenwalt585ac0f2010-08-27 09:24:29 -0700920 InetAddress addr = InetAddress.getByAddress(hostAddress);
Robert Greenwalt47f69fe2010-06-15 15:43:39 -0700921 return addHostRoute(tracker, addr);
922 } catch (UnknownHostException e) {}
923 return false;
Irfan Sheriffd649c122010-06-09 15:39:36 -0700924 }
925
926 /**
927 * Ensure that a network route exists to deliver traffic to the specified
928 * host via the mobile data network.
929 * @param hostAddress the IP address of the host to which the route is desired,
930 * in network byte order.
Robert Greenwalt47f69fe2010-06-15 15:43:39 -0700931 * TODO - deprecate
Irfan Sheriffd649c122010-06-09 15:39:36 -0700932 * @return {@code true} on success, {@code false} on failure
933 */
Robert Greenwalt47f69fe2010-06-15 15:43:39 -0700934 private boolean addHostRoute(NetworkStateTracker nt, InetAddress hostAddress) {
Irfan Sheriffd649c122010-06-09 15:39:36 -0700935 if (nt.getNetworkInfo().getType() == ConnectivityManager.TYPE_WIFI) {
936 return false;
937 }
938
Robert Greenwalt37e65eb2010-08-30 10:56:47 -0700939 LinkProperties p = nt.getLinkProperties();
Robert Greenwalt47f69fe2010-06-15 15:43:39 -0700940 if (p == null) return false;
941 String interfaceName = p.getInterfaceName();
Irfan Sheriffd649c122010-06-09 15:39:36 -0700942
943 if (DBG) {
Wink Savilleed9c02b2010-12-03 12:01:38 -0800944 log("Requested host route to " + hostAddress + "(" + interfaceName + ")");
Irfan Sheriffd649c122010-06-09 15:39:36 -0700945 }
Robert Greenwalt47f69fe2010-06-15 15:43:39 -0700946 if (interfaceName != null) {
Robert Greenwalt585ac0f2010-08-27 09:24:29 -0700947 return NetworkUtils.addHostRoute(interfaceName, hostAddress, null);
Irfan Sheriffd649c122010-06-09 15:39:36 -0700948 } else {
Wink Savilleed9c02b2010-12-03 12:01:38 -0800949 if (DBG) loge("addHostRoute failed due to null interface name");
Irfan Sheriffd649c122010-06-09 15:39:36 -0700950 return false;
951 }
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800952 }
953
954 /**
955 * @see ConnectivityManager#getBackgroundDataSetting()
956 */
957 public boolean getBackgroundDataSetting() {
958 return Settings.Secure.getInt(mContext.getContentResolver(),
959 Settings.Secure.BACKGROUND_DATA, 1) == 1;
960 }
Robert Greenwalt86e9e552009-07-16 17:21:39 -0700961
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800962 /**
963 * @see ConnectivityManager#setBackgroundDataSetting(boolean)
964 */
965 public void setBackgroundDataSetting(boolean allowBackgroundDataUsage) {
966 mContext.enforceCallingOrSelfPermission(
967 android.Manifest.permission.CHANGE_BACKGROUND_DATA_SETTING,
968 "ConnectivityService");
Robert Greenwalt86e9e552009-07-16 17:21:39 -0700969
Robert Greenwalt8dcc28b2010-09-23 10:05:56 -0700970 mHandler.sendMessage(mHandler.obtainMessage(EVENT_SET_BACKGROUND_DATA,
971 (allowBackgroundDataUsage ? ENABLED : DISABLED), 0));
972 }
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800973
Robert Greenwalt8dcc28b2010-09-23 10:05:56 -0700974 private void handleSetBackgroundData(boolean enabled) {
975 if (enabled != getBackgroundDataSetting()) {
976 Settings.Secure.putInt(mContext.getContentResolver(),
977 Settings.Secure.BACKGROUND_DATA, enabled ? 1 : 0);
978 Intent broadcast = new Intent(
979 ConnectivityManager.ACTION_BACKGROUND_DATA_SETTING_CHANGED);
980 mContext.sendBroadcast(broadcast);
981 }
Robert Greenwalt86e9e552009-07-16 17:21:39 -0700982 }
983
Robert Greenwaltc03fa502010-02-23 18:58:05 -0800984 /**
985 * @see ConnectivityManager#getMobileDataEnabled()
986 */
987 public boolean getMobileDataEnabled() {
988 enforceAccessPermission();
989 boolean retVal = Settings.Secure.getInt(mContext.getContentResolver(),
990 Settings.Secure.MOBILE_DATA, 1) == 1;
Wink Savilleed9c02b2010-12-03 12:01:38 -0800991 if (DBG) log("getMobileDataEnabled returning " + retVal);
Robert Greenwaltc03fa502010-02-23 18:58:05 -0800992 return retVal;
993 }
994
995 /**
996 * @see ConnectivityManager#setMobileDataEnabled(boolean)
997 */
Robert Greenwalt8dcc28b2010-09-23 10:05:56 -0700998 public void setMobileDataEnabled(boolean enabled) {
Robert Greenwaltc03fa502010-02-23 18:58:05 -0800999 enforceChangePermission();
Wink Savilleed9c02b2010-12-03 12:01:38 -08001000 if (DBG) log("setMobileDataEnabled(" + enabled + ")");
Robert Greenwaltc03fa502010-02-23 18:58:05 -08001001
Robert Greenwalt8dcc28b2010-09-23 10:05:56 -07001002 mHandler.sendMessage(mHandler.obtainMessage(EVENT_SET_MOBILE_DATA,
1003 (enabled ? ENABLED : DISABLED), 0));
1004 }
1005
1006 private void handleSetMobileData(boolean enabled) {
Robert Greenwaltc03fa502010-02-23 18:58:05 -08001007 if (getMobileDataEnabled() == enabled) return;
1008
1009 Settings.Secure.putInt(mContext.getContentResolver(),
1010 Settings.Secure.MOBILE_DATA, enabled ? 1 : 0);
1011
1012 if (enabled) {
1013 if (mNetTrackers[ConnectivityManager.TYPE_MOBILE] != null) {
Robert Greenwalt8dcc28b2010-09-23 10:05:56 -07001014 if (DBG) {
Wink Savilleed9c02b2010-12-03 12:01:38 -08001015 log("starting up " + mNetTrackers[ConnectivityManager.TYPE_MOBILE]);
Robert Greenwalt8dcc28b2010-09-23 10:05:56 -07001016 }
Robert Greenwaltc03fa502010-02-23 18:58:05 -08001017 mNetTrackers[ConnectivityManager.TYPE_MOBILE].reconnect();
1018 }
1019 } else {
1020 for (NetworkStateTracker nt : mNetTrackers) {
1021 if (nt == null) continue;
1022 int netType = nt.getNetworkInfo().getType();
1023 if (mNetAttributes[netType].mRadio == ConnectivityManager.TYPE_MOBILE) {
Wink Savilleed9c02b2010-12-03 12:01:38 -08001024 if (DBG) log("tearing down " + nt);
Robert Greenwaltc03fa502010-02-23 18:58:05 -08001025 nt.teardown();
1026 }
1027 }
1028 }
1029 }
1030
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001031 private int getNumConnectedNetworks() {
1032 int numConnectedNets = 0;
1033
1034 for (NetworkStateTracker nt : mNetTrackers) {
Robert Greenwalt5154ae762009-10-30 14:17:42 -07001035 if (nt != null && nt.getNetworkInfo().isConnected() &&
Robert Greenwalt86e9e552009-07-16 17:21:39 -07001036 !nt.isTeardownRequested()) {
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001037 ++numConnectedNets;
1038 }
1039 }
1040 return numConnectedNets;
1041 }
1042
1043 private void enforceAccessPermission() {
Robert Greenwalt86e9e552009-07-16 17:21:39 -07001044 mContext.enforceCallingOrSelfPermission(
1045 android.Manifest.permission.ACCESS_NETWORK_STATE,
1046 "ConnectivityService");
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001047 }
1048
1049 private void enforceChangePermission() {
Robert Greenwalt86e9e552009-07-16 17:21:39 -07001050 mContext.enforceCallingOrSelfPermission(
1051 android.Manifest.permission.CHANGE_NETWORK_STATE,
1052 "ConnectivityService");
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001053 }
1054
Robert Greenwaltd0e18ff2010-01-26 11:40:34 -08001055 // TODO Make this a special check when it goes public
1056 private void enforceTetherChangePermission() {
1057 mContext.enforceCallingOrSelfPermission(
1058 android.Manifest.permission.CHANGE_NETWORK_STATE,
1059 "ConnectivityService");
1060 }
1061
Robert Greenwalt2a091d72010-02-11 18:18:40 -08001062 private void enforceTetherAccessPermission() {
1063 mContext.enforceCallingOrSelfPermission(
1064 android.Manifest.permission.ACCESS_NETWORK_STATE,
1065 "ConnectivityService");
1066 }
1067
Robert Greenwalt14f2ef42010-06-15 12:19:37 -07001068 private void enforceConnectivityInternalPermission() {
1069 mContext.enforceCallingOrSelfPermission(
1070 android.Manifest.permission.CONNECTIVITY_INTERNAL,
1071 "ConnectivityService");
1072 }
1073
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001074 /**
Robert Greenwalt86e9e552009-07-16 17:21:39 -07001075 * Handle a {@code DISCONNECTED} event. If this pertains to the non-active
1076 * network, we ignore it. If it is for the active network, we send out a
1077 * broadcast. But first, we check whether it might be possible to connect
1078 * to a different network.
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001079 * @param info the {@code NetworkInfo} for the network
1080 */
1081 private void handleDisconnect(NetworkInfo info) {
1082
Robert Greenwalt42acef32009-08-12 16:08:25 -07001083 int prevNetType = info.getType();
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001084
Robert Greenwalt42acef32009-08-12 16:08:25 -07001085 mNetTrackers[prevNetType].setTeardownRequested(false);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001086 /*
1087 * If the disconnected network is not the active one, then don't report
1088 * this as a loss of connectivity. What probably happened is that we're
1089 * getting the disconnect for a network that we explicitly disabled
1090 * in accordance with network preference policies.
1091 */
Robert Greenwalt42acef32009-08-12 16:08:25 -07001092 if (!mNetAttributes[prevNetType].isDefault()) {
1093 List pids = mNetRequestersPids[prevNetType];
1094 for (int i = 0; i<pids.size(); i++) {
1095 Integer pid = (Integer)pids.get(i);
1096 // will remove them because the net's no longer connected
1097 // need to do this now as only now do we know the pids and
1098 // can properly null things that are no longer referenced.
1099 reassessPidDns(pid.intValue(), false);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001100 }
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001101 }
1102
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001103 Intent intent = new Intent(ConnectivityManager.CONNECTIVITY_ACTION);
1104 intent.putExtra(ConnectivityManager.EXTRA_NETWORK_INFO, info);
1105 if (info.isFailover()) {
1106 intent.putExtra(ConnectivityManager.EXTRA_IS_FAILOVER, true);
1107 info.setFailover(false);
1108 }
1109 if (info.getReason() != null) {
1110 intent.putExtra(ConnectivityManager.EXTRA_REASON, info.getReason());
1111 }
1112 if (info.getExtraInfo() != null) {
Robert Greenwalt86e9e552009-07-16 17:21:39 -07001113 intent.putExtra(ConnectivityManager.EXTRA_EXTRA_INFO,
1114 info.getExtraInfo());
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001115 }
Robert Greenwalt42acef32009-08-12 16:08:25 -07001116
Robert Greenwaltcc4b4012010-01-25 17:54:29 -08001117 NetworkStateTracker newNet = null;
1118 if (mNetAttributes[prevNetType].isDefault()) {
1119 newNet = tryFailover(prevNetType);
1120 if (newNet != null) {
1121 NetworkInfo switchTo = newNet.getNetworkInfo();
Robert Greenwalt029be812010-09-20 18:01:43 -07001122 if (!switchTo.isConnected()) {
Wink Savilleed9c02b2010-12-03 12:01:38 -08001123 // if the other net is connected they've already reset this and perhaps even
1124 // gotten a positive report we don't want to overwrite, but if not we need to
1125 // clear this now to turn our cellular sig strength white
Robert Greenwalt029be812010-09-20 18:01:43 -07001126 mDefaultInetConditionPublished = 0;
1127 }
Robert Greenwaltcc4b4012010-01-25 17:54:29 -08001128 intent.putExtra(ConnectivityManager.EXTRA_OTHER_NETWORK_INFO, switchTo);
1129 } else {
Robert Greenwalt029be812010-09-20 18:01:43 -07001130 mDefaultInetConditionPublished = 0; // we're not connected anymore
Robert Greenwaltcc4b4012010-01-25 17:54:29 -08001131 intent.putExtra(ConnectivityManager.EXTRA_NO_CONNECTIVITY, true);
1132 }
Robert Greenwaltda03c4e2010-01-20 19:29:41 -08001133 }
Robert Greenwalt029be812010-09-20 18:01:43 -07001134 intent.putExtra(ConnectivityManager.EXTRA_INET_CONDITION, mDefaultInetConditionPublished);
Robert Greenwaltda03c4e2010-01-20 19:29:41 -08001135 // do this before we broadcast the change
Robert Greenwaltc76b8fa2010-07-23 15:46:26 -07001136 handleConnectivityChange(prevNetType);
Robert Greenwaltda03c4e2010-01-20 19:29:41 -08001137
1138 sendStickyBroadcast(intent);
1139 /*
1140 * If the failover network is already connected, then immediately send
1141 * out a followup broadcast indicating successful failover
1142 */
1143 if (newNet != null && newNet.getNetworkInfo().isConnected()) {
1144 sendConnectedBroadcast(newNet.getNetworkInfo());
1145 }
1146 }
1147
Robert Greenwaltcc4b4012010-01-25 17:54:29 -08001148 // returns null if no failover available
Robert Greenwaltda03c4e2010-01-20 19:29:41 -08001149 private NetworkStateTracker tryFailover(int prevNetType) {
Robert Greenwalt42acef32009-08-12 16:08:25 -07001150 /*
1151 * If this is a default network, check if other defaults are available
1152 * or active
1153 */
1154 NetworkStateTracker newNet = null;
1155 if (mNetAttributes[prevNetType].isDefault()) {
Robert Greenwalt42acef32009-08-12 16:08:25 -07001156 if (mActiveDefaultNetwork == prevNetType) {
1157 mActiveDefaultNetwork = -1;
1158 }
1159
1160 int newType = -1;
1161 int newPriority = -1;
Robert Greenwalt35429592010-02-25 12:04:29 -08001162 boolean noMobileData = !getMobileDataEnabled();
Robert Greenwaltda03c4e2010-01-20 19:29:41 -08001163 for (int checkType=0; checkType <= ConnectivityManager.MAX_NETWORK_TYPE; checkType++) {
Robert Greenwalt5154ae762009-10-30 14:17:42 -07001164 if (checkType == prevNetType) continue;
1165 if (mNetAttributes[checkType] == null) continue;
Robert Greenwalt35429592010-02-25 12:04:29 -08001166 if (mNetAttributes[checkType].mRadio == ConnectivityManager.TYPE_MOBILE &&
1167 noMobileData) {
Wink Savilleed9c02b2010-12-03 12:01:38 -08001168 loge("not failing over to mobile type " + checkType +
Robert Greenwalt572172b2010-10-08 16:35:52 -07001169 " because Mobile Data Disabled");
Robert Greenwalt35429592010-02-25 12:04:29 -08001170 continue;
1171 }
Robert Greenwalt42acef32009-08-12 16:08:25 -07001172 if (mNetAttributes[checkType].isDefault()) {
1173 /* TODO - if we have multiple nets we could use
1174 * we may want to put more thought into which we choose
1175 */
1176 if (checkType == mNetworkPreference) {
1177 newType = checkType;
1178 break;
1179 }
Robert Greenwalt5154ae762009-10-30 14:17:42 -07001180 if (mNetAttributes[checkType].mPriority > newPriority) {
Robert Greenwalt42acef32009-08-12 16:08:25 -07001181 newType = checkType;
Robert Greenwalt5154ae762009-10-30 14:17:42 -07001182 newPriority = mNetAttributes[newType].mPriority;
Robert Greenwalt42acef32009-08-12 16:08:25 -07001183 }
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001184 }
1185 }
Robert Greenwalt42acef32009-08-12 16:08:25 -07001186
1187 if (newType != -1) {
1188 newNet = mNetTrackers[newType];
1189 /**
1190 * See if the other network is available to fail over to.
1191 * If is not available, we enable it anyway, so that it
1192 * will be able to connect when it does become available,
1193 * but we report a total loss of connectivity rather than
1194 * report that we are attempting to fail over.
1195 */
1196 if (newNet.isAvailable()) {
1197 NetworkInfo switchTo = newNet.getNetworkInfo();
1198 switchTo.setFailover(true);
Robert Greenwalta64bf832009-08-19 20:19:33 -07001199 if (!switchTo.isConnectedOrConnecting() ||
1200 newNet.isTeardownRequested()) {
Robert Greenwalt42acef32009-08-12 16:08:25 -07001201 newNet.reconnect();
1202 }
1203 if (DBG) {
1204 if (switchTo.isConnected()) {
Wink Savilleed9c02b2010-12-03 12:01:38 -08001205 log("Switching to already connected " + switchTo.getTypeName());
Robert Greenwalt42acef32009-08-12 16:08:25 -07001206 } else {
Wink Savilleed9c02b2010-12-03 12:01:38 -08001207 log("Attempting to switch to " + switchTo.getTypeName());
Robert Greenwalt42acef32009-08-12 16:08:25 -07001208 }
1209 }
Robert Greenwalt42acef32009-08-12 16:08:25 -07001210 } else {
1211 newNet.reconnect();
Robert Greenwaltf0fa39e2010-03-09 14:55:08 -08001212 newNet = null; // not officially avail.. try anyway, but
1213 // report no failover
Robert Greenwalt42acef32009-08-12 16:08:25 -07001214 }
Robert Greenwalt572172b2010-10-08 16:35:52 -07001215 } else {
Wink Savilleed9c02b2010-12-03 12:01:38 -08001216 loge("Network failover failing.");
Robert Greenwalt42acef32009-08-12 16:08:25 -07001217 }
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001218 }
Robert Greenwalt42acef32009-08-12 16:08:25 -07001219
Robert Greenwaltda03c4e2010-01-20 19:29:41 -08001220 return newNet;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001221 }
1222
1223 private void sendConnectedBroadcast(NetworkInfo info) {
Robert Greenwalt1e9aac22010-09-15 17:36:33 -07001224 sendGeneralBroadcast(info, ConnectivityManager.CONNECTIVITY_ACTION);
1225 }
1226
1227 private void sendInetConditionBroadcast(NetworkInfo info) {
1228 sendGeneralBroadcast(info, ConnectivityManager.INET_CONDITION_ACTION);
1229 }
1230
1231 private void sendGeneralBroadcast(NetworkInfo info, String bcastType) {
1232 Intent intent = new Intent(bcastType);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001233 intent.putExtra(ConnectivityManager.EXTRA_NETWORK_INFO, info);
1234 if (info.isFailover()) {
1235 intent.putExtra(ConnectivityManager.EXTRA_IS_FAILOVER, true);
1236 info.setFailover(false);
1237 }
1238 if (info.getReason() != null) {
1239 intent.putExtra(ConnectivityManager.EXTRA_REASON, info.getReason());
1240 }
1241 if (info.getExtraInfo() != null) {
Robert Greenwalt86e9e552009-07-16 17:21:39 -07001242 intent.putExtra(ConnectivityManager.EXTRA_EXTRA_INFO,
1243 info.getExtraInfo());
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001244 }
Robert Greenwaltd7085fc2010-09-08 15:24:47 -07001245 intent.putExtra(ConnectivityManager.EXTRA_INET_CONDITION, mDefaultInetConditionPublished);
Mike Lockwood0f79b542009-08-14 14:18:49 -04001246 sendStickyBroadcast(intent);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001247 }
1248
1249 /**
1250 * Called when an attempt to fail over to another network has failed.
1251 * @param info the {@link NetworkInfo} for the failed network
1252 */
1253 private void handleConnectionFailure(NetworkInfo info) {
1254 mNetTrackers[info.getType()].setTeardownRequested(false);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001255
Robert Greenwalt42acef32009-08-12 16:08:25 -07001256 String reason = info.getReason();
1257 String extraInfo = info.getExtraInfo();
Robert Greenwalt86e9e552009-07-16 17:21:39 -07001258
Robert Greenwalt572172b2010-10-08 16:35:52 -07001259 String reasonText;
1260 if (reason == null) {
1261 reasonText = ".";
1262 } else {
1263 reasonText = " (" + reason + ").";
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001264 }
Wink Savilleed9c02b2010-12-03 12:01:38 -08001265 loge("Attempt to connect to " + info.getTypeName() + " failed" + reasonText);
Robert Greenwalt42acef32009-08-12 16:08:25 -07001266
1267 Intent intent = new Intent(ConnectivityManager.CONNECTIVITY_ACTION);
1268 intent.putExtra(ConnectivityManager.EXTRA_NETWORK_INFO, info);
1269 if (getActiveNetworkInfo() == null) {
1270 intent.putExtra(ConnectivityManager.EXTRA_NO_CONNECTIVITY, true);
1271 }
1272 if (reason != null) {
1273 intent.putExtra(ConnectivityManager.EXTRA_REASON, reason);
1274 }
1275 if (extraInfo != null) {
1276 intent.putExtra(ConnectivityManager.EXTRA_EXTRA_INFO, extraInfo);
1277 }
1278 if (info.isFailover()) {
1279 intent.putExtra(ConnectivityManager.EXTRA_IS_FAILOVER, true);
1280 info.setFailover(false);
1281 }
Robert Greenwaltda03c4e2010-01-20 19:29:41 -08001282
Robert Greenwaltcc4b4012010-01-25 17:54:29 -08001283 NetworkStateTracker newNet = null;
1284 if (mNetAttributes[info.getType()].isDefault()) {
1285 newNet = tryFailover(info.getType());
1286 if (newNet != null) {
1287 NetworkInfo switchTo = newNet.getNetworkInfo();
Robert Greenwalt029be812010-09-20 18:01:43 -07001288 if (!switchTo.isConnected()) {
Robert Greenwalt572172b2010-10-08 16:35:52 -07001289 // if the other net is connected they've already reset this and perhaps
1290 // even gotten a positive report we don't want to overwrite, but if not
1291 // we need to clear this now to turn our cellular sig strength white
Robert Greenwalt029be812010-09-20 18:01:43 -07001292 mDefaultInetConditionPublished = 0;
1293 }
Robert Greenwaltcc4b4012010-01-25 17:54:29 -08001294 intent.putExtra(ConnectivityManager.EXTRA_OTHER_NETWORK_INFO, switchTo);
1295 } else {
Robert Greenwalt029be812010-09-20 18:01:43 -07001296 mDefaultInetConditionPublished = 0;
Robert Greenwaltcc4b4012010-01-25 17:54:29 -08001297 intent.putExtra(ConnectivityManager.EXTRA_NO_CONNECTIVITY, true);
1298 }
Robert Greenwaltda03c4e2010-01-20 19:29:41 -08001299 }
Robert Greenwaltcc4b4012010-01-25 17:54:29 -08001300
Robert Greenwalt029be812010-09-20 18:01:43 -07001301 intent.putExtra(ConnectivityManager.EXTRA_INET_CONDITION, mDefaultInetConditionPublished);
Mike Lockwood0f79b542009-08-14 14:18:49 -04001302 sendStickyBroadcast(intent);
Robert Greenwaltda03c4e2010-01-20 19:29:41 -08001303 /*
1304 * If the failover network is already connected, then immediately send
1305 * out a followup broadcast indicating successful failover
1306 */
1307 if (newNet != null && newNet.getNetworkInfo().isConnected()) {
1308 sendConnectedBroadcast(newNet.getNetworkInfo());
1309 }
Mike Lockwood0f79b542009-08-14 14:18:49 -04001310 }
1311
1312 private void sendStickyBroadcast(Intent intent) {
1313 synchronized(this) {
Dianne Hackborn1c633fc2009-12-08 19:45:14 -08001314 if (!mSystemReady) {
1315 mInitialBroadcast = new Intent(intent);
Mike Lockwood0f79b542009-08-14 14:18:49 -04001316 }
Dianne Hackborn1c633fc2009-12-08 19:45:14 -08001317 intent.addFlags(Intent.FLAG_RECEIVER_REGISTERED_ONLY_BEFORE_BOOT);
1318 mContext.sendStickyBroadcast(intent);
Mike Lockwood0f79b542009-08-14 14:18:49 -04001319 }
1320 }
1321
1322 void systemReady() {
1323 synchronized(this) {
1324 mSystemReady = true;
Dianne Hackborn1c633fc2009-12-08 19:45:14 -08001325 if (mInitialBroadcast != null) {
1326 mContext.sendStickyBroadcast(mInitialBroadcast);
1327 mInitialBroadcast = null;
Mike Lockwood0f79b542009-08-14 14:18:49 -04001328 }
1329 }
Robert Greenwalt434203a2010-10-11 16:00:27 -07001330 // load the global proxy at startup
1331 mHandler.sendMessage(mHandler.obtainMessage(EVENT_APPLY_GLOBAL_HTTP_PROXY));
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001332 }
1333
1334 private void handleConnect(NetworkInfo info) {
Robert Greenwalt42acef32009-08-12 16:08:25 -07001335 int type = info.getType();
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001336
1337 // snapshot isFailover, because sendConnectedBroadcast() resets it
1338 boolean isFailover = info.isFailover();
Robert Greenwalt42acef32009-08-12 16:08:25 -07001339 NetworkStateTracker thisNet = mNetTrackers[type];
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001340
Robert Greenwalt42acef32009-08-12 16:08:25 -07001341 // if this is a default net and other default is running
1342 // kill the one not preferred
1343 if (mNetAttributes[type].isDefault()) {
Robert Greenwalt42acef32009-08-12 16:08:25 -07001344 if (mActiveDefaultNetwork != -1 && mActiveDefaultNetwork != type) {
1345 if ((type != mNetworkPreference &&
1346 mNetAttributes[mActiveDefaultNetwork].mPriority >
1347 mNetAttributes[type].mPriority) ||
1348 mNetworkPreference == mActiveDefaultNetwork) {
1349 // don't accept this one
Wink Savilleed9c02b2010-12-03 12:01:38 -08001350 if (DBG) {
1351 log("Not broadcasting CONNECT_ACTION " +
Robert Greenwalt42acef32009-08-12 16:08:25 -07001352 "to torn down network " + info.getTypeName());
Wink Savilleed9c02b2010-12-03 12:01:38 -08001353 }
Robert Greenwalt42acef32009-08-12 16:08:25 -07001354 teardown(thisNet);
1355 return;
1356 } else {
1357 // tear down the other
1358 NetworkStateTracker otherNet =
1359 mNetTrackers[mActiveDefaultNetwork];
Wink Savilleed9c02b2010-12-03 12:01:38 -08001360 if (DBG) {
1361 log("Policy requires " + otherNet.getNetworkInfo().getTypeName() +
Robert Greenwalt42acef32009-08-12 16:08:25 -07001362 " teardown");
Wink Savilleed9c02b2010-12-03 12:01:38 -08001363 }
Robert Greenwalt42acef32009-08-12 16:08:25 -07001364 if (!teardown(otherNet)) {
Wink Savilleed9c02b2010-12-03 12:01:38 -08001365 loge("Network declined teardown request");
Robert Greenwalt42acef32009-08-12 16:08:25 -07001366 return;
1367 }
Robert Greenwalt14f2ef42010-06-15 12:19:37 -07001368 }
1369 }
1370 synchronized (ConnectivityService.this) {
1371 // have a new default network, release the transition wakelock in a second
1372 // if it's held. The second pause is to allow apps to reconnect over the
1373 // new network
1374 if (mNetTransitionWakeLock.isHeld()) {
1375 mHandler.sendMessageDelayed(mHandler.obtainMessage(
Robert Greenwaltf3331232010-09-24 14:32:21 -07001376 EVENT_CLEAR_NET_TRANSITION_WAKELOCK,
Robert Greenwalt14f2ef42010-06-15 12:19:37 -07001377 mNetTransitionWakeLockSerialNumber, 0),
1378 1000);
Robert Greenwalt42acef32009-08-12 16:08:25 -07001379 }
1380 }
1381 mActiveDefaultNetwork = type;
Robert Greenwaltd7085fc2010-09-08 15:24:47 -07001382 // this will cause us to come up initially as unconnected and switching
1383 // to connected after our normal pause unless somebody reports us as reall
1384 // disconnected
1385 mDefaultInetConditionPublished = 0;
1386 mDefaultConnectionSequence++;
1387 mInetConditionChangeInFlight = false;
1388 // Don't do this - if we never sign in stay, grey
1389 //reportNetworkCondition(mActiveDefaultNetwork, 100);
Robert Greenwalt42acef32009-08-12 16:08:25 -07001390 }
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001391 thisNet.setTeardownRequested(false);
Irfan Sheriffd649c122010-06-09 15:39:36 -07001392 updateNetworkSettings(thisNet);
Robert Greenwaltc76b8fa2010-07-23 15:46:26 -07001393 handleConnectivityChange(type);
Robert Greenwalt42acef32009-08-12 16:08:25 -07001394 sendConnectedBroadcast(info);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001395 }
1396
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001397 /**
Robert Greenwaltc76b8fa2010-07-23 15:46:26 -07001398 * After a change in the connectivity state of a network. We're mainly
1399 * concerned with making sure that the list of DNS servers is set up
1400 * according to which networks are connected, and ensuring that the
1401 * right routing table entries exist.
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001402 */
Robert Greenwaltc76b8fa2010-07-23 15:46:26 -07001403 private void handleConnectivityChange(int netType) {
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001404 /*
Robert Greenwalt42acef32009-08-12 16:08:25 -07001405 * If a non-default network is enabled, add the host routes that
Robert Greenwaltc76b8fa2010-07-23 15:46:26 -07001406 * will allow it's DNS servers to be accessed.
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001407 */
Robert Greenwaltc76b8fa2010-07-23 15:46:26 -07001408 handleDnsConfigurationChange(netType);
Robert Greenwalt42acef32009-08-12 16:08:25 -07001409
Robert Greenwaltc76b8fa2010-07-23 15:46:26 -07001410 if (mNetTrackers[netType].getNetworkInfo().isConnected()) {
1411 if (mNetAttributes[netType].isDefault()) {
Robert Greenwalt434203a2010-10-11 16:00:27 -07001412 handleApplyDefaultProxy(netType);
Robert Greenwaltc76b8fa2010-07-23 15:46:26 -07001413 addDefaultRoute(mNetTrackers[netType]);
Robert Greenwalt42acef32009-08-12 16:08:25 -07001414 } else {
Robert Greenwaltc76b8fa2010-07-23 15:46:26 -07001415 addPrivateDnsRoutes(mNetTrackers[netType]);
1416 }
1417 } else {
1418 if (mNetAttributes[netType].isDefault()) {
1419 removeDefaultRoute(mNetTrackers[netType]);
1420 } else {
1421 removePrivateDnsRoutes(mNetTrackers[netType]);
Robert Greenwalt42acef32009-08-12 16:08:25 -07001422 }
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001423 }
1424 }
1425
Irfan Sheriffd649c122010-06-09 15:39:36 -07001426 private void addPrivateDnsRoutes(NetworkStateTracker nt) {
Irfan Sheriffd649c122010-06-09 15:39:36 -07001427 boolean privateDnsRouteSet = nt.isPrivateDnsRouteSet();
Robert Greenwalt37e65eb2010-08-30 10:56:47 -07001428 LinkProperties p = nt.getLinkProperties();
Robert Greenwalt47f69fe2010-06-15 15:43:39 -07001429 if (p == null) return;
1430 String interfaceName = p.getInterfaceName();
Irfan Sheriffd649c122010-06-09 15:39:36 -07001431
1432 if (DBG) {
Wink Savilleed9c02b2010-12-03 12:01:38 -08001433 log("addPrivateDnsRoutes for " + nt +
Irfan Sheriffd649c122010-06-09 15:39:36 -07001434 "(" + interfaceName + ") - mPrivateDnsRouteSet = " + privateDnsRouteSet);
1435 }
Irfan Sheriffd649c122010-06-09 15:39:36 -07001436 if (interfaceName != null && !privateDnsRouteSet) {
Robert Greenwalt47f69fe2010-06-15 15:43:39 -07001437 Collection<InetAddress> dnsList = p.getDnses();
1438 for (InetAddress dns : dnsList) {
Wink Savilleed9c02b2010-12-03 12:01:38 -08001439 if (DBG) log(" adding " + dns);
Robert Greenwalt585ac0f2010-08-27 09:24:29 -07001440 NetworkUtils.addHostRoute(interfaceName, dns, null);
Irfan Sheriffd649c122010-06-09 15:39:36 -07001441 }
1442 nt.privateDnsRouteSet(true);
1443 }
1444 }
1445
1446 private void removePrivateDnsRoutes(NetworkStateTracker nt) {
1447 // TODO - we should do this explicitly but the NetUtils api doesnt
1448 // support this yet - must remove all. No worse than before
Robert Greenwalt37e65eb2010-08-30 10:56:47 -07001449 LinkProperties p = nt.getLinkProperties();
Robert Greenwalt47f69fe2010-06-15 15:43:39 -07001450 if (p == null) return;
1451 String interfaceName = p.getInterfaceName();
Irfan Sheriffd649c122010-06-09 15:39:36 -07001452 boolean privateDnsRouteSet = nt.isPrivateDnsRouteSet();
1453 if (interfaceName != null && privateDnsRouteSet) {
1454 if (DBG) {
Wink Savilleed9c02b2010-12-03 12:01:38 -08001455 log("removePrivateDnsRoutes for " + nt.getNetworkInfo().getTypeName() +
Irfan Sheriffd649c122010-06-09 15:39:36 -07001456 " (" + interfaceName + ")");
1457 }
1458 NetworkUtils.removeHostRoutes(interfaceName);
1459 nt.privateDnsRouteSet(false);
1460 }
1461 }
1462
Irfan Sheriffd649c122010-06-09 15:39:36 -07001463
1464 private void addDefaultRoute(NetworkStateTracker nt) {
Robert Greenwalt37e65eb2010-08-30 10:56:47 -07001465 LinkProperties p = nt.getLinkProperties();
Robert Greenwalt47f69fe2010-06-15 15:43:39 -07001466 if (p == null) return;
1467 String interfaceName = p.getInterfaceName();
1468 InetAddress defaultGatewayAddr = p.getGateway();
Irfan Sheriffd649c122010-06-09 15:39:36 -07001469
Robert Greenwaltc76b8fa2010-07-23 15:46:26 -07001470 if ((interfaceName != null) && (defaultGatewayAddr != null )) {
Robert Greenwalt585ac0f2010-08-27 09:24:29 -07001471 if (!NetworkUtils.addDefaultRoute(interfaceName, defaultGatewayAddr) && DBG) {
Robert Greenwalt47f69fe2010-06-15 15:43:39 -07001472 NetworkInfo networkInfo = nt.getNetworkInfo();
Wink Savilleed9c02b2010-12-03 12:01:38 -08001473 log("addDefaultRoute for " + networkInfo.getTypeName() +
Irfan Sheriffd649c122010-06-09 15:39:36 -07001474 " (" + interfaceName + "), GatewayAddr=" + defaultGatewayAddr);
1475 }
Irfan Sheriffd649c122010-06-09 15:39:36 -07001476 }
1477 }
1478
1479
1480 public void removeDefaultRoute(NetworkStateTracker nt) {
Robert Greenwalt37e65eb2010-08-30 10:56:47 -07001481 LinkProperties p = nt.getLinkProperties();
Robert Greenwalt47f69fe2010-06-15 15:43:39 -07001482 if (p == null) return;
1483 String interfaceName = p.getInterfaceName();
Irfan Sheriffd649c122010-06-09 15:39:36 -07001484
Robert Greenwaltc76b8fa2010-07-23 15:46:26 -07001485 if (interfaceName != null) {
1486 if ((NetworkUtils.removeDefaultRoute(interfaceName) >= 0) && DBG) {
Robert Greenwalt47f69fe2010-06-15 15:43:39 -07001487 NetworkInfo networkInfo = nt.getNetworkInfo();
Wink Savilleed9c02b2010-12-03 12:01:38 -08001488 log("removeDefaultRoute for " + networkInfo.getTypeName() + " (" +
Irfan Sheriffd649c122010-06-09 15:39:36 -07001489 interfaceName + ")");
1490 }
Irfan Sheriffd649c122010-06-09 15:39:36 -07001491 }
1492 }
1493
1494 /**
1495 * Reads the network specific TCP buffer sizes from SystemProperties
1496 * net.tcp.buffersize.[default|wifi|umts|edge|gprs] and set them for system
1497 * wide use
1498 */
1499 public void updateNetworkSettings(NetworkStateTracker nt) {
1500 String key = nt.getTcpBufferSizesPropName();
1501 String bufferSizes = SystemProperties.get(key);
1502
1503 if (bufferSizes.length() == 0) {
Wink Savilleed9c02b2010-12-03 12:01:38 -08001504 loge(key + " not found in system properties. Using defaults");
Irfan Sheriffd649c122010-06-09 15:39:36 -07001505
1506 // Setting to default values so we won't be stuck to previous values
1507 key = "net.tcp.buffersize.default";
1508 bufferSizes = SystemProperties.get(key);
1509 }
1510
1511 // Set values in kernel
1512 if (bufferSizes.length() != 0) {
1513 if (DBG) {
Wink Savilleed9c02b2010-12-03 12:01:38 -08001514 log("Setting TCP values: [" + bufferSizes
Irfan Sheriffd649c122010-06-09 15:39:36 -07001515 + "] which comes from [" + key + "]");
1516 }
1517 setBufferSize(bufferSizes);
1518 }
1519 }
1520
1521 /**
1522 * Writes TCP buffer sizes to /sys/kernel/ipv4/tcp_[r/w]mem_[min/def/max]
1523 * which maps to /proc/sys/net/ipv4/tcp_rmem and tcpwmem
1524 *
1525 * @param bufferSizes in the format of "readMin, readInitial, readMax,
1526 * writeMin, writeInitial, writeMax"
1527 */
1528 private void setBufferSize(String bufferSizes) {
1529 try {
1530 String[] values = bufferSizes.split(",");
1531
1532 if (values.length == 6) {
1533 final String prefix = "/sys/kernel/ipv4/tcp_";
1534 stringToFile(prefix + "rmem_min", values[0]);
1535 stringToFile(prefix + "rmem_def", values[1]);
1536 stringToFile(prefix + "rmem_max", values[2]);
1537 stringToFile(prefix + "wmem_min", values[3]);
1538 stringToFile(prefix + "wmem_def", values[4]);
1539 stringToFile(prefix + "wmem_max", values[5]);
1540 } else {
Wink Savilleed9c02b2010-12-03 12:01:38 -08001541 loge("Invalid buffersize string: " + bufferSizes);
Irfan Sheriffd649c122010-06-09 15:39:36 -07001542 }
1543 } catch (IOException e) {
Wink Savilleed9c02b2010-12-03 12:01:38 -08001544 loge("Can't set tcp buffer sizes:" + e);
Irfan Sheriffd649c122010-06-09 15:39:36 -07001545 }
1546 }
1547
1548 /**
1549 * Writes string to file. Basically same as "echo -n $string > $filename"
1550 *
1551 * @param filename
1552 * @param string
1553 * @throws IOException
1554 */
1555 private void stringToFile(String filename, String string) throws IOException {
1556 FileWriter out = new FileWriter(filename);
1557 try {
1558 out.write(string);
1559 } finally {
1560 out.close();
1561 }
1562 }
1563
1564
Robert Greenwalt42acef32009-08-12 16:08:25 -07001565 /**
1566 * Adjust the per-process dns entries (net.dns<x>.<pid>) based
1567 * on the highest priority active net which this process requested.
1568 * If there aren't any, clear it out
1569 */
1570 private void reassessPidDns(int myPid, boolean doBump)
1571 {
Wink Savilleed9c02b2010-12-03 12:01:38 -08001572 if (DBG) log("reassessPidDns for pid " + myPid);
Robert Greenwalt42acef32009-08-12 16:08:25 -07001573 for(int i : mPriorityList) {
1574 if (mNetAttributes[i].isDefault()) {
1575 continue;
1576 }
1577 NetworkStateTracker nt = mNetTrackers[i];
Robert Greenwalt86e9e552009-07-16 17:21:39 -07001578 if (nt.getNetworkInfo().isConnected() &&
1579 !nt.isTeardownRequested()) {
Robert Greenwalt37e65eb2010-08-30 10:56:47 -07001580 LinkProperties p = nt.getLinkProperties();
Robert Greenwalt47f69fe2010-06-15 15:43:39 -07001581 if (p == null) continue;
Robert Greenwalt42acef32009-08-12 16:08:25 -07001582 List pids = mNetRequestersPids[i];
1583 for (int j=0; j<pids.size(); j++) {
1584 Integer pid = (Integer)pids.get(j);
1585 if (pid.intValue() == myPid) {
Robert Greenwalt47f69fe2010-06-15 15:43:39 -07001586 Collection<InetAddress> dnses = p.getDnses();
1587 writePidDns(dnses, myPid);
Robert Greenwalt42acef32009-08-12 16:08:25 -07001588 if (doBump) {
1589 bumpDns();
1590 }
1591 return;
1592 }
1593 }
1594 }
1595 }
1596 // nothing found - delete
1597 for (int i = 1; ; i++) {
1598 String prop = "net.dns" + i + "." + myPid;
1599 if (SystemProperties.get(prop).length() == 0) {
1600 if (doBump) {
1601 bumpDns();
1602 }
1603 return;
1604 }
1605 SystemProperties.set(prop, "");
1606 }
1607 }
1608
Robert Greenwalt47f69fe2010-06-15 15:43:39 -07001609 private void writePidDns(Collection <InetAddress> dnses, int pid) {
Robert Greenwalt42acef32009-08-12 16:08:25 -07001610 int j = 1;
Robert Greenwalt47f69fe2010-06-15 15:43:39 -07001611 for (InetAddress dns : dnses) {
1612 SystemProperties.set("net.dns" + j++ + "." + pid, dns.getHostAddress());
Robert Greenwalt42acef32009-08-12 16:08:25 -07001613 }
1614 }
1615
1616 private void bumpDns() {
1617 /*
1618 * Bump the property that tells the name resolver library to reread
1619 * the DNS server list from the properties.
1620 */
1621 String propVal = SystemProperties.get("net.dnschange");
1622 int n = 0;
1623 if (propVal.length() != 0) {
1624 try {
1625 n = Integer.parseInt(propVal);
1626 } catch (NumberFormatException e) {}
1627 }
1628 SystemProperties.set("net.dnschange", "" + (n+1));
Robert Greenwalt03595d02010-11-02 14:08:23 -07001629 /*
1630 * Tell the VMs to toss their DNS caches
1631 */
1632 Intent intent = new Intent(Intent.ACTION_CLEAR_DNS_CACHE);
1633 intent.addFlags(Intent.FLAG_RECEIVER_REPLACE_PENDING);
1634 mContext.sendBroadcast(intent);
Robert Greenwalt42acef32009-08-12 16:08:25 -07001635 }
1636
Robert Greenwaltc76b8fa2010-07-23 15:46:26 -07001637 private void handleDnsConfigurationChange(int netType) {
Robert Greenwalt42acef32009-08-12 16:08:25 -07001638 // add default net's dns entries
Robert Greenwaltc76b8fa2010-07-23 15:46:26 -07001639 NetworkStateTracker nt = mNetTrackers[netType];
1640 if (nt != null && nt.getNetworkInfo().isConnected() && !nt.isTeardownRequested()) {
Robert Greenwalt37e65eb2010-08-30 10:56:47 -07001641 LinkProperties p = nt.getLinkProperties();
Robert Greenwaltc76b8fa2010-07-23 15:46:26 -07001642 if (p == null) return;
1643 Collection<InetAddress> dnses = p.getDnses();
1644 if (mNetAttributes[netType].isDefault()) {
1645 int j = 1;
Robert Greenwalte90aa5e2010-09-01 11:34:05 -07001646 if (dnses.size() == 0 && mDefaultDns != null) {
Robert Greenwaltc76b8fa2010-07-23 15:46:26 -07001647 if (DBG) {
Wink Savilleed9c02b2010-12-03 12:01:38 -08001648 log("no dns provided - using " + mDefaultDns.getHostAddress());
Robert Greenwalt42acef32009-08-12 16:08:25 -07001649 }
Robert Greenwalte90aa5e2010-09-01 11:34:05 -07001650 SystemProperties.set("net.dns1", mDefaultDns.getHostAddress());
1651 j++;
1652 } else {
1653 for (InetAddress dns : dnses) {
1654 if (DBG) {
Wink Savilleed9c02b2010-12-03 12:01:38 -08001655 log("adding dns " + dns + " for " +
Robert Greenwalte90aa5e2010-09-01 11:34:05 -07001656 nt.getNetworkInfo().getTypeName());
1657 }
1658 SystemProperties.set("net.dns" + j++, dns.getHostAddress());
1659 }
Robert Greenwaltc76b8fa2010-07-23 15:46:26 -07001660 }
1661 for (int k=j ; k<mNumDnsEntries; k++) {
Wink Savilleed9c02b2010-12-03 12:01:38 -08001662 if (DBG) log("erasing net.dns" + k);
Robert Greenwaltc76b8fa2010-07-23 15:46:26 -07001663 SystemProperties.set("net.dns" + k, "");
1664 }
1665 mNumDnsEntries = j;
1666 } else {
1667 // set per-pid dns for attached secondary nets
1668 List pids = mNetRequestersPids[netType];
1669 for (int y=0; y< pids.size(); y++) {
1670 Integer pid = (Integer)pids.get(y);
1671 writePidDns(dnses, pid.intValue());
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001672 }
1673 }
Robert Greenwaltc76b8fa2010-07-23 15:46:26 -07001674 bumpDns();
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001675 }
Robert Greenwalt42acef32009-08-12 16:08:25 -07001676 }
1677
1678 private int getRestoreDefaultNetworkDelay() {
1679 String restoreDefaultNetworkDelayStr = SystemProperties.get(
1680 NETWORK_RESTORE_DELAY_PROP_NAME);
1681 if(restoreDefaultNetworkDelayStr != null &&
1682 restoreDefaultNetworkDelayStr.length() != 0) {
1683 try {
1684 return Integer.valueOf(restoreDefaultNetworkDelayStr);
1685 } catch (NumberFormatException e) {
1686 }
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001687 }
Robert Greenwalt42acef32009-08-12 16:08:25 -07001688 return RESTORE_DEFAULT_NETWORK_DELAY;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001689 }
1690
1691 @Override
1692 protected void dump(FileDescriptor fd, PrintWriter pw, String[] args) {
Robert Greenwalt86e9e552009-07-16 17:21:39 -07001693 if (mContext.checkCallingOrSelfPermission(
1694 android.Manifest.permission.DUMP)
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001695 != PackageManager.PERMISSION_GRANTED) {
Robert Greenwalt86e9e552009-07-16 17:21:39 -07001696 pw.println("Permission Denial: can't dump ConnectivityService " +
1697 "from from pid=" + Binder.getCallingPid() + ", uid=" +
1698 Binder.getCallingUid());
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001699 return;
1700 }
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001701 pw.println();
1702 for (NetworkStateTracker nst : mNetTrackers) {
Robert Greenwaltb9285352009-12-21 18:24:07 -08001703 if (nst != null) {
1704 if (nst.getNetworkInfo().isConnected()) {
1705 pw.println("Active network: " + nst.getNetworkInfo().
1706 getTypeName());
1707 }
1708 pw.println(nst.getNetworkInfo());
1709 pw.println(nst);
1710 pw.println();
Robert Greenwalt42acef32009-08-12 16:08:25 -07001711 }
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001712 }
Robert Greenwaltb9285352009-12-21 18:24:07 -08001713
1714 pw.println("Network Requester Pids:");
1715 for (int net : mPriorityList) {
1716 String pidString = net + ": ";
1717 for (Object pid : mNetRequestersPids[net]) {
1718 pidString = pidString + pid.toString() + ", ";
1719 }
1720 pw.println(pidString);
1721 }
1722 pw.println();
1723
1724 pw.println("FeatureUsers:");
1725 for (Object requester : mFeatureUsers) {
1726 pw.println(requester.toString());
1727 }
1728 pw.println();
Robert Greenwalt2a091d72010-02-11 18:18:40 -08001729
Robert Greenwalt14f2ef42010-06-15 12:19:37 -07001730 synchronized (this) {
1731 pw.println("NetworkTranstionWakeLock is currently " +
1732 (mNetTransitionWakeLock.isHeld() ? "" : "not ") + "held.");
1733 pw.println("It was last requested for "+mNetTransitionWakeLockCausedBy);
1734 }
1735 pw.println();
1736
Robert Greenwalt2a091d72010-02-11 18:18:40 -08001737 mTethering.dump(fd, pw, args);
Robert Greenwalt4e8dfef2010-09-20 14:35:25 -07001738
1739 if (mInetLog != null) {
1740 pw.println();
1741 pw.println("Inet condition reports:");
1742 for(int i = 0; i < mInetLog.size(); i++) {
1743 pw.println(mInetLog.get(i));
1744 }
1745 }
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001746 }
1747
Robert Greenwalt42acef32009-08-12 16:08:25 -07001748 // must be stateless - things change under us.
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001749 private class MyHandler extends Handler {
Wink Savillebb08caf2010-09-02 19:23:52 -07001750 public MyHandler(Looper looper) {
1751 super(looper);
1752 }
1753
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001754 @Override
1755 public void handleMessage(Message msg) {
1756 NetworkInfo info;
1757 switch (msg.what) {
1758 case NetworkStateTracker.EVENT_STATE_CHANGED:
1759 info = (NetworkInfo) msg.obj;
Robert Greenwalt511288a2009-12-07 11:33:18 -08001760 int type = info.getType();
1761 NetworkInfo.State state = info.getState();
Robert Greenwalt6e6dec22010-01-25 16:14:00 -08001762 // only do this optimization for wifi. It going into scan mode for location
1763 // services generates alot of noise. Meanwhile the mms apn won't send out
1764 // subsequent notifications when on default cellular because it never
1765 // disconnects.. so only do this to wifi notifications. Fixed better when the
1766 // APN notifications are standardized.
1767 if (mNetAttributes[type].mLastState == state &&
1768 mNetAttributes[type].mRadio == ConnectivityManager.TYPE_WIFI) {
Robert Greenwalt511288a2009-12-07 11:33:18 -08001769 if (DBG) {
Robert Greenwalt6e6dec22010-01-25 16:14:00 -08001770 // TODO - remove this after we validate the dropping doesn't break
1771 // anything
Wink Savilleed9c02b2010-12-03 12:01:38 -08001772 log("Dropping ConnectivityChange for " +
Robert Greenwalt1193ae42010-01-13 09:36:31 -08001773 info.getTypeName() + ": " +
Robert Greenwalt511288a2009-12-07 11:33:18 -08001774 state + "/" + info.getDetailedState());
1775 }
1776 return;
1777 }
1778 mNetAttributes[type].mLastState = state;
1779
Wink Savilleed9c02b2010-12-03 12:01:38 -08001780 if (DBG) log("ConnectivityChange for " +
Robert Greenwalt86e9e552009-07-16 17:21:39 -07001781 info.getTypeName() + ": " +
Robert Greenwalt511288a2009-12-07 11:33:18 -08001782 state + "/" + info.getDetailedState());
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001783
1784 // Connectivity state changed:
1785 // [31-13] Reserved for future use
Robert Greenwalt86e9e552009-07-16 17:21:39 -07001786 // [12-9] Network subtype (for mobile network, as defined
1787 // by TelephonyManager)
1788 // [8-3] Detailed state ordinal (as defined by
1789 // NetworkInfo.DetailedState)
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001790 // [2-0] Network type (as defined by ConnectivityManager)
1791 int eventLogParam = (info.getType() & 0x7) |
1792 ((info.getDetailedState().ordinal() & 0x3f) << 3) |
1793 (info.getSubtype() << 9);
Doug Zongkerab5c49c2009-12-04 10:31:43 -08001794 EventLog.writeEvent(EventLogTags.CONNECTIVITY_STATE_CHANGED,
Robert Greenwalt86e9e552009-07-16 17:21:39 -07001795 eventLogParam);
1796
1797 if (info.getDetailedState() ==
1798 NetworkInfo.DetailedState.FAILED) {
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001799 handleConnectionFailure(info);
Robert Greenwalt511288a2009-12-07 11:33:18 -08001800 } else if (state == NetworkInfo.State.DISCONNECTED) {
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001801 handleDisconnect(info);
Robert Greenwalt511288a2009-12-07 11:33:18 -08001802 } else if (state == NetworkInfo.State.SUSPENDED) {
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001803 // TODO: need to think this over.
Robert Greenwalt86e9e552009-07-16 17:21:39 -07001804 // the logic here is, handle SUSPENDED the same as
1805 // DISCONNECTED. The only difference being we are
1806 // broadcasting an intent with NetworkInfo that's
1807 // suspended. This allows the applications an
1808 // opportunity to handle DISCONNECTED and SUSPENDED
1809 // differently, or not.
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001810 handleDisconnect(info);
Robert Greenwalt511288a2009-12-07 11:33:18 -08001811 } else if (state == NetworkInfo.State.CONNECTED) {
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001812 handleConnect(info);
1813 }
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001814 break;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001815 case NetworkStateTracker.EVENT_CONFIGURATION_CHANGED:
Robert Greenwaltc76b8fa2010-07-23 15:46:26 -07001816 info = (NetworkInfo) msg.obj;
1817 type = info.getType();
Robert Greenwalt434203a2010-10-11 16:00:27 -07001818 handleConnectivityChange(type);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001819 break;
Robert Greenwaltf3331232010-09-24 14:32:21 -07001820 case EVENT_CLEAR_NET_TRANSITION_WAKELOCK:
Robert Greenwalt14f2ef42010-06-15 12:19:37 -07001821 String causedBy = null;
1822 synchronized (ConnectivityService.this) {
1823 if (msg.arg1 == mNetTransitionWakeLockSerialNumber &&
1824 mNetTransitionWakeLock.isHeld()) {
1825 mNetTransitionWakeLock.release();
1826 causedBy = mNetTransitionWakeLockCausedBy;
1827 }
1828 }
1829 if (causedBy != null) {
Wink Savilleed9c02b2010-12-03 12:01:38 -08001830 log("NetTransition Wakelock for " + causedBy + " released by timeout");
Robert Greenwalt14f2ef42010-06-15 12:19:37 -07001831 }
Robert Greenwalt057d5e92010-09-09 14:05:10 -07001832 break;
Robert Greenwalt8dcc28b2010-09-23 10:05:56 -07001833 case EVENT_RESTORE_DEFAULT_NETWORK:
Robert Greenwalt9c75d4a2009-09-27 17:27:04 -07001834 FeatureUser u = (FeatureUser)msg.obj;
1835 u.expire();
Robert Greenwaltd7085fc2010-09-08 15:24:47 -07001836 break;
Robert Greenwalt8dcc28b2010-09-23 10:05:56 -07001837 case EVENT_INET_CONDITION_CHANGE:
1838 {
1839 int netType = msg.arg1;
1840 int condition = msg.arg2;
1841 handleInetConditionChange(netType, condition);
Robert Greenwalt14f2ef42010-06-15 12:19:37 -07001842 break;
Robert Greenwalt8dcc28b2010-09-23 10:05:56 -07001843 }
1844 case EVENT_INET_CONDITION_HOLD_END:
1845 {
1846 int netType = msg.arg1;
1847 int sequence = msg.arg2;
1848 handleInetConditionHoldEnd(netType, sequence);
Robert Greenwaltd7085fc2010-09-08 15:24:47 -07001849 break;
Robert Greenwalt8dcc28b2010-09-23 10:05:56 -07001850 }
1851 case EVENT_SET_NETWORK_PREFERENCE:
1852 {
1853 int preference = msg.arg1;
1854 handleSetNetworkPreference(preference);
1855 break;
1856 }
1857 case EVENT_SET_BACKGROUND_DATA:
1858 {
1859 boolean enabled = (msg.arg1 == ENABLED);
1860 handleSetBackgroundData(enabled);
1861 break;
1862 }
1863 case EVENT_SET_MOBILE_DATA:
1864 {
1865 boolean enabled = (msg.arg1 == ENABLED);
1866 handleSetMobileData(enabled);
1867 break;
1868 }
Robert Greenwalt434203a2010-10-11 16:00:27 -07001869 case EVENT_APPLY_GLOBAL_HTTP_PROXY:
1870 {
1871 handleDeprecatedGlobalHttpProxy();
1872 }
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001873 }
1874 }
1875 }
Robert Greenwaltd0e18ff2010-01-26 11:40:34 -08001876
1877 // javadoc from interface
Robert Greenwalt5a735062010-03-02 17:25:02 -08001878 public int tether(String iface) {
Robert Greenwaltd0e18ff2010-01-26 11:40:34 -08001879 enforceTetherChangePermission();
Robert Greenwalt5a735062010-03-02 17:25:02 -08001880
1881 if (isTetheringSupported()) {
1882 return mTethering.tether(iface);
1883 } else {
1884 return ConnectivityManager.TETHER_ERROR_UNSUPPORTED;
1885 }
Robert Greenwaltd0e18ff2010-01-26 11:40:34 -08001886 }
1887
1888 // javadoc from interface
Robert Greenwalt5a735062010-03-02 17:25:02 -08001889 public int untether(String iface) {
Robert Greenwaltd0e18ff2010-01-26 11:40:34 -08001890 enforceTetherChangePermission();
Robert Greenwalt5a735062010-03-02 17:25:02 -08001891
1892 if (isTetheringSupported()) {
1893 return mTethering.untether(iface);
1894 } else {
1895 return ConnectivityManager.TETHER_ERROR_UNSUPPORTED;
1896 }
1897 }
1898
1899 // javadoc from interface
1900 public int getLastTetherError(String iface) {
1901 enforceTetherAccessPermission();
1902
1903 if (isTetheringSupported()) {
1904 return mTethering.getLastTetherError(iface);
1905 } else {
1906 return ConnectivityManager.TETHER_ERROR_UNSUPPORTED;
1907 }
Robert Greenwalt2a091d72010-02-11 18:18:40 -08001908 }
1909
1910 // TODO - proper iface API for selection by property, inspection, etc
1911 public String[] getTetherableUsbRegexs() {
1912 enforceTetherAccessPermission();
1913 if (isTetheringSupported()) {
1914 return mTethering.getTetherableUsbRegexs();
1915 } else {
1916 return new String[0];
1917 }
1918 }
1919
1920 public String[] getTetherableWifiRegexs() {
1921 enforceTetherAccessPermission();
1922 if (isTetheringSupported()) {
1923 return mTethering.getTetherableWifiRegexs();
1924 } else {
1925 return new String[0];
1926 }
Robert Greenwaltd0e18ff2010-01-26 11:40:34 -08001927 }
1928
Danica Chang6fdd0c62010-08-11 14:54:43 -07001929 public String[] getTetherableBluetoothRegexs() {
1930 enforceTetherAccessPermission();
1931 if (isTetheringSupported()) {
1932 return mTethering.getTetherableBluetoothRegexs();
1933 } else {
1934 return new String[0];
1935 }
1936 }
1937
Robert Greenwaltd0e18ff2010-01-26 11:40:34 -08001938 // TODO - move iface listing, queries, etc to new module
1939 // javadoc from interface
1940 public String[] getTetherableIfaces() {
Robert Greenwalt2a091d72010-02-11 18:18:40 -08001941 enforceTetherAccessPermission();
Robert Greenwaltd0e18ff2010-01-26 11:40:34 -08001942 return mTethering.getTetherableIfaces();
1943 }
1944
1945 public String[] getTetheredIfaces() {
Robert Greenwalt2a091d72010-02-11 18:18:40 -08001946 enforceTetherAccessPermission();
Robert Greenwaltd0e18ff2010-01-26 11:40:34 -08001947 return mTethering.getTetheredIfaces();
1948 }
Robert Greenwalt2a091d72010-02-11 18:18:40 -08001949
Robert Greenwalt5a735062010-03-02 17:25:02 -08001950 public String[] getTetheringErroredIfaces() {
1951 enforceTetherAccessPermission();
1952 return mTethering.getErroredIfaces();
1953 }
1954
Robert Greenwalt2a091d72010-02-11 18:18:40 -08001955 // if ro.tether.denied = true we default to no tethering
1956 // gservices could set the secure setting to 1 though to enable it on a build where it
1957 // had previously been turned off.
1958 public boolean isTetheringSupported() {
1959 enforceTetherAccessPermission();
1960 int defaultVal = (SystemProperties.get("ro.tether.denied").equals("true") ? 0 : 1);
Robert Greenwaltc9d5fb72010-02-25 12:29:30 -08001961 boolean tetherEnabledInSettings = (Settings.Secure.getInt(mContext.getContentResolver(),
1962 Settings.Secure.TETHER_SUPPORTED, defaultVal) != 0);
1963 return tetherEnabledInSettings && mTetheringConfigValid;
Robert Greenwalt2a091d72010-02-11 18:18:40 -08001964 }
Robert Greenwalt14f2ef42010-06-15 12:19:37 -07001965
1966 // An API NetworkStateTrackers can call when they lose their network.
1967 // This will automatically be cleared after X seconds or a network becomes CONNECTED,
1968 // whichever happens first. The timer is started by the first caller and not
1969 // restarted by subsequent callers.
1970 public void requestNetworkTransitionWakelock(String forWhom) {
1971 enforceConnectivityInternalPermission();
1972 synchronized (this) {
1973 if (mNetTransitionWakeLock.isHeld()) return;
1974 mNetTransitionWakeLockSerialNumber++;
1975 mNetTransitionWakeLock.acquire();
1976 mNetTransitionWakeLockCausedBy = forWhom;
1977 }
1978 mHandler.sendMessageDelayed(mHandler.obtainMessage(
Robert Greenwaltf3331232010-09-24 14:32:21 -07001979 EVENT_CLEAR_NET_TRANSITION_WAKELOCK,
Robert Greenwalt14f2ef42010-06-15 12:19:37 -07001980 mNetTransitionWakeLockSerialNumber, 0),
1981 mNetTransitionWakeLockTimeout);
1982 return;
1983 }
Robert Greenwaltca4306c2010-09-09 13:15:32 -07001984
Robert Greenwaltd7085fc2010-09-08 15:24:47 -07001985 // 100 percent is full good, 0 is full bad.
1986 public void reportInetCondition(int networkType, int percentage) {
Wink Savilleed9c02b2010-12-03 12:01:38 -08001987 if (DBG) log("reportNetworkCondition(" + networkType + ", " + percentage + ")");
Robert Greenwaltd7085fc2010-09-08 15:24:47 -07001988 mContext.enforceCallingOrSelfPermission(
1989 android.Manifest.permission.STATUS_BAR,
1990 "ConnectivityService");
1991
Robert Greenwalt4e8dfef2010-09-20 14:35:25 -07001992 if (DBG) {
1993 int pid = getCallingPid();
1994 int uid = getCallingUid();
1995 String s = pid + "(" + uid + ") reports inet is " +
1996 (percentage > 50 ? "connected" : "disconnected") + " (" + percentage + ") on " +
1997 "network Type " + networkType + " at " + GregorianCalendar.getInstance().getTime();
1998 mInetLog.add(s);
1999 while(mInetLog.size() > INET_CONDITION_LOG_MAX_SIZE) {
2000 mInetLog.remove(0);
2001 }
2002 }
Robert Greenwaltd7085fc2010-09-08 15:24:47 -07002003 mHandler.sendMessage(mHandler.obtainMessage(
Robert Greenwalt8dcc28b2010-09-23 10:05:56 -07002004 EVENT_INET_CONDITION_CHANGE, networkType, percentage));
2005 }
2006
2007 private void handleInetConditionChange(int netType, int condition) {
2008 if (DBG) {
Wink Savilleed9c02b2010-12-03 12:01:38 -08002009 log("Inet connectivity change, net=" +
Robert Greenwalt8dcc28b2010-09-23 10:05:56 -07002010 netType + ", condition=" + condition +
2011 ",mActiveDefaultNetwork=" + mActiveDefaultNetwork);
2012 }
2013 if (mActiveDefaultNetwork == -1) {
Wink Savilleed9c02b2010-12-03 12:01:38 -08002014 if (DBG) log("no active default network - aborting");
Robert Greenwalt8dcc28b2010-09-23 10:05:56 -07002015 return;
2016 }
2017 if (mActiveDefaultNetwork != netType) {
Wink Savilleed9c02b2010-12-03 12:01:38 -08002018 if (DBG) log("given net not default - aborting");
Robert Greenwalt8dcc28b2010-09-23 10:05:56 -07002019 return;
2020 }
2021 mDefaultInetCondition = condition;
2022 int delay;
2023 if (mInetConditionChangeInFlight == false) {
Wink Savilleed9c02b2010-12-03 12:01:38 -08002024 if (DBG) log("starting a change hold");
Robert Greenwalt8dcc28b2010-09-23 10:05:56 -07002025 // setup a new hold to debounce this
2026 if (mDefaultInetCondition > 50) {
2027 delay = Settings.Secure.getInt(mContext.getContentResolver(),
2028 Settings.Secure.INET_CONDITION_DEBOUNCE_UP_DELAY, 500);
2029 } else {
2030 delay = Settings.Secure.getInt(mContext.getContentResolver(),
2031 Settings.Secure.INET_CONDITION_DEBOUNCE_DOWN_DELAY, 3000);
2032 }
2033 mInetConditionChangeInFlight = true;
2034 mHandler.sendMessageDelayed(mHandler.obtainMessage(EVENT_INET_CONDITION_HOLD_END,
2035 mActiveDefaultNetwork, mDefaultConnectionSequence), delay);
2036 } else {
2037 // we've set the new condition, when this hold ends that will get
2038 // picked up
Wink Savilleed9c02b2010-12-03 12:01:38 -08002039 if (DBG) log("currently in hold - not setting new end evt");
Robert Greenwalt8dcc28b2010-09-23 10:05:56 -07002040 }
2041 }
2042
2043 private void handleInetConditionHoldEnd(int netType, int sequence) {
2044 if (DBG) {
Wink Savilleed9c02b2010-12-03 12:01:38 -08002045 log("Inet hold end, net=" + netType +
Robert Greenwalt8dcc28b2010-09-23 10:05:56 -07002046 ", condition =" + mDefaultInetCondition +
2047 ", published condition =" + mDefaultInetConditionPublished);
2048 }
2049 mInetConditionChangeInFlight = false;
2050
2051 if (mActiveDefaultNetwork == -1) {
Wink Savilleed9c02b2010-12-03 12:01:38 -08002052 if (DBG) log("no active default network - aborting");
Robert Greenwalt8dcc28b2010-09-23 10:05:56 -07002053 return;
2054 }
2055 if (mDefaultConnectionSequence != sequence) {
Wink Savilleed9c02b2010-12-03 12:01:38 -08002056 if (DBG) log("event hold for obsolete network - aborting");
Robert Greenwalt8dcc28b2010-09-23 10:05:56 -07002057 return;
2058 }
2059 if (mDefaultInetConditionPublished == mDefaultInetCondition) {
Wink Savilleed9c02b2010-12-03 12:01:38 -08002060 if (DBG) log("no change in condition - aborting");
Robert Greenwalt8dcc28b2010-09-23 10:05:56 -07002061 return;
2062 }
2063 NetworkInfo networkInfo = mNetTrackers[mActiveDefaultNetwork].getNetworkInfo();
2064 if (networkInfo.isConnected() == false) {
Wink Savilleed9c02b2010-12-03 12:01:38 -08002065 if (DBG) log("default network not connected - aborting");
Robert Greenwalt8dcc28b2010-09-23 10:05:56 -07002066 return;
2067 }
2068 mDefaultInetConditionPublished = mDefaultInetCondition;
2069 sendInetConditionBroadcast(networkInfo);
2070 return;
Robert Greenwaltd7085fc2010-09-08 15:24:47 -07002071 }
Robert Greenwalt434203a2010-10-11 16:00:27 -07002072
2073 public synchronized ProxyProperties getProxy() {
2074 if (mGlobalProxy != null) return mGlobalProxy;
2075 if (mDefaultProxy != null) return mDefaultProxy;
2076 return null;
2077 }
2078
2079 public void setGlobalProxy(ProxyProperties proxyProperties) {
2080 enforceChangePermission();
2081 synchronized (mGlobalProxyLock) {
2082 if (proxyProperties == mGlobalProxy) return;
2083 if (proxyProperties != null && proxyProperties.equals(mGlobalProxy)) return;
2084 if (mGlobalProxy != null && mGlobalProxy.equals(proxyProperties)) return;
2085
2086 String host = "";
2087 int port = 0;
2088 String exclList = "";
2089 if (proxyProperties != null && !TextUtils.isEmpty(proxyProperties.getHost())) {
2090 mGlobalProxy = new ProxyProperties(proxyProperties);
2091 host = mGlobalProxy.getHost();
2092 port = mGlobalProxy.getPort();
2093 exclList = mGlobalProxy.getExclusionList();
2094 } else {
2095 mGlobalProxy = null;
2096 }
2097 ContentResolver res = mContext.getContentResolver();
2098 Settings.Secure.putString(res, Settings.Secure.GLOBAL_HTTP_PROXY_HOST, host);
2099 Settings.Secure.putInt(res, Settings.Secure.GLOBAL_HTTP_PROXY_PORT, port);
Robert Greenwaltb7090d62010-12-02 11:31:00 -08002100 Settings.Secure.putString(res, Settings.Secure.GLOBAL_HTTP_PROXY_EXCLUSION_LIST,
Robert Greenwalt434203a2010-10-11 16:00:27 -07002101 exclList);
2102 }
2103
2104 if (mGlobalProxy == null) {
2105 proxyProperties = mDefaultProxy;
2106 }
2107 sendProxyBroadcast(proxyProperties);
2108 }
2109
Robert Greenwaltb7090d62010-12-02 11:31:00 -08002110 private void loadGlobalProxy() {
2111 ContentResolver res = mContext.getContentResolver();
2112 String host = Settings.Secure.getString(res, Settings.Secure.GLOBAL_HTTP_PROXY_HOST);
2113 int port = Settings.Secure.getInt(res, Settings.Secure.GLOBAL_HTTP_PROXY_PORT, 0);
2114 String exclList = Settings.Secure.getString(res,
2115 Settings.Secure.GLOBAL_HTTP_PROXY_EXCLUSION_LIST);
2116 if (!TextUtils.isEmpty(host)) {
2117 ProxyProperties proxyProperties = new ProxyProperties(host, port, exclList);
2118 synchronized (mGlobalProxyLock) {
2119 mGlobalProxy = proxyProperties;
2120 }
2121 }
2122 }
2123
Robert Greenwalt434203a2010-10-11 16:00:27 -07002124 public ProxyProperties getGlobalProxy() {
2125 synchronized (mGlobalProxyLock) {
2126 return mGlobalProxy;
2127 }
2128 }
2129
2130 private void handleApplyDefaultProxy(int type) {
2131 // check if new default - push it out to all VM if so
2132 ProxyProperties proxy = mNetTrackers[type].getLinkProperties().getHttpProxy();
2133 synchronized (this) {
2134 if (mDefaultProxy != null && mDefaultProxy.equals(proxy)) return;
2135 if (mDefaultProxy == proxy) return;
2136 if (!TextUtils.isEmpty(proxy.getHost())) {
2137 mDefaultProxy = proxy;
2138 } else {
2139 mDefaultProxy = null;
2140 }
2141 }
Wink Savilleed9c02b2010-12-03 12:01:38 -08002142 if (DBG) log("changing default proxy to " + proxy);
Robert Greenwalt434203a2010-10-11 16:00:27 -07002143 if ((proxy == null && mGlobalProxy == null) || proxy.equals(mGlobalProxy)) return;
2144 if (mGlobalProxy != null) return;
2145 sendProxyBroadcast(proxy);
2146 }
2147
2148 private void handleDeprecatedGlobalHttpProxy() {
2149 String proxy = Settings.Secure.getString(mContext.getContentResolver(),
2150 Settings.Secure.HTTP_PROXY);
2151 if (!TextUtils.isEmpty(proxy)) {
2152 String data[] = proxy.split(":");
2153 String proxyHost = data[0];
2154 int proxyPort = 8080;
2155 if (data.length > 1) {
2156 try {
2157 proxyPort = Integer.parseInt(data[1]);
2158 } catch (NumberFormatException e) {
2159 return;
2160 }
2161 }
2162 ProxyProperties p = new ProxyProperties(data[0], proxyPort, "");
2163 setGlobalProxy(p);
2164 }
2165 }
2166
2167 private void sendProxyBroadcast(ProxyProperties proxy) {
Wink Savilleed9c02b2010-12-03 12:01:38 -08002168 log("sending Proxy Broadcast for " + proxy);
Robert Greenwalt434203a2010-10-11 16:00:27 -07002169 Intent intent = new Intent(Proxy.PROXY_CHANGE_ACTION);
2170 intent.addFlags(Intent.FLAG_RECEIVER_REPLACE_PENDING);
2171 intent.putExtra(Proxy.EXTRA_PROXY_INFO, proxy);
Robert Greenwalta2e13392010-12-06 11:29:17 -08002172 mContext.sendStickyBroadcast(intent);
Robert Greenwalt434203a2010-10-11 16:00:27 -07002173 }
2174
2175 private static class SettingsObserver extends ContentObserver {
2176 private int mWhat;
2177 private Handler mHandler;
2178 SettingsObserver(Handler handler, int what) {
2179 super(handler);
2180 mHandler = handler;
2181 mWhat = what;
2182 }
2183
2184 void observe(Context context) {
2185 ContentResolver resolver = context.getContentResolver();
2186 resolver.registerContentObserver(Settings.Secure.getUriFor(
2187 Settings.Secure.HTTP_PROXY), false, this);
2188 }
2189
2190 @Override
2191 public void onChange(boolean selfChange) {
2192 mHandler.obtainMessage(mWhat).sendToTarget();
2193 }
2194 }
Wink Savilleed9c02b2010-12-03 12:01:38 -08002195
2196 private void log(String s) {
2197 Slog.d(TAG, s);
2198 }
2199
2200 private void loge(String s) {
2201 Slog.e(TAG, s);
2202 }
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002203}