blob: 5861505dc221298518e0a1408e197146fc7ca077 [file] [log] [blame]
The Android Open Source Project28527d22009-03-03 19:31:44 -08001/*
2 * Copyright (C) 2008 The Android Open Source Project
3 *
4 * Licensed under the Apache License, Version 2.0 (the "License");
5 * you may not use this file except in compliance with the License.
6 * You may obtain a copy of the License at
7 *
8 * http://www.apache.org/licenses/LICENSE-2.0
9 *
10 * Unless required by applicable law or agreed to in writing, software
11 * distributed under the License is distributed on an "AS IS" BASIS,
12 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13 * See the License for the specific language governing permissions and
14 * limitations under the License.
15 */
16
17package com.android.server;
18
Jaikumar Ganesh0db51a02010-12-21 22:31:44 -080019import android.bluetooth.BluetoothTetheringDataTracker;
The Android Open Source Project28527d22009-03-03 19:31:44 -080020import android.content.ContentResolver;
21import android.content.Context;
22import android.content.Intent;
23import android.content.pm.PackageManager;
Robert Greenwaltc3c5f862010-10-11 16:00:27 -070024import android.database.ContentObserver;
The Android Open Source Project28527d22009-03-03 19:31:44 -080025import android.net.ConnectivityManager;
Robert Greenwalteb123ac2010-12-06 13:56:24 -080026import android.net.DummyDataStateTracker;
Benoit Goby6cec7f32010-12-22 14:29:40 -080027import android.net.EthernetDataTracker;
The Android Open Source Project28527d22009-03-03 19:31:44 -080028import android.net.IConnectivityManager;
Robert Greenwalt355205c2011-05-10 15:05:02 -070029import android.net.LinkAddress;
Jaikumar Ganesh0db51a02010-12-21 22:31:44 -080030import android.net.LinkProperties;
Wink Saville051a6642011-07-13 13:44:13 -070031import android.net.LinkProperties.CompareAddressesResult;
The Android Open Source Project28527d22009-03-03 19:31:44 -080032import android.net.MobileDataStateTracker;
Robert Greenwalt34848c02011-03-25 13:09:25 -070033import android.net.NetworkConfig;
The Android Open Source Project28527d22009-03-03 19:31:44 -080034import android.net.NetworkInfo;
35import android.net.NetworkStateTracker;
Robert Greenwalt7fe44cb2010-08-27 09:24:29 -070036import android.net.NetworkUtils;
Robert Greenwaltc3c5f862010-10-11 16:00:27 -070037import android.net.Proxy;
38import android.net.ProxyProperties;
Robert Greenwalt5a901292011-04-28 14:28:50 -070039import android.net.RouteInfo;
Hung-ying Tyan4e723422011-01-19 16:48:38 +080040import android.net.vpn.VpnManager;
The Android Open Source Project28527d22009-03-03 19:31:44 -080041import android.net.wifi.WifiStateTracker;
42import android.os.Binder;
43import android.os.Handler;
Wink Saville775aad62010-09-02 19:23:52 -070044import android.os.HandlerThread;
Robert Greenwalt2034b912009-08-12 16:08:25 -070045import android.os.IBinder;
Robert Greenwalt355205c2011-05-10 15:05:02 -070046import android.os.INetworkManagementService;
The Android Open Source Project28527d22009-03-03 19:31:44 -080047import android.os.Looper;
48import android.os.Message;
Robert Greenwalt93dc1042010-06-15 12:19:37 -070049import android.os.PowerManager;
Robert Greenwalt2034b912009-08-12 16:08:25 -070050import android.os.RemoteException;
The Android Open Source Project28527d22009-03-03 19:31:44 -080051import android.os.ServiceManager;
52import android.os.SystemProperties;
53import android.provider.Settings;
Robert Greenwalt2034b912009-08-12 16:08:25 -070054import android.text.TextUtils;
The Android Open Source Project28527d22009-03-03 19:31:44 -080055import android.util.EventLog;
Joe Onoratoc2386bb2010-02-26 18:56:32 -080056import android.util.Slog;
The Android Open Source Project28527d22009-03-03 19:31:44 -080057
Robert Greenwalt2034b912009-08-12 16:08:25 -070058import com.android.internal.telephony.Phone;
Robert Greenwalt0c4828c2010-01-26 11:40:34 -080059import com.android.server.connectivity.Tethering;
60
The Android Open Source Project28527d22009-03-03 19:31:44 -080061import java.io.FileDescriptor;
Irfan Sheriff7f132d92010-06-09 15:39:36 -070062import java.io.FileWriter;
63import java.io.IOException;
The Android Open Source Project28527d22009-03-03 19:31:44 -080064import java.io.PrintWriter;
Wink Saville051a6642011-07-13 13:44:13 -070065import java.net.Inet6Address;
Robert Greenwalta7dfbd32010-06-15 15:43:39 -070066import java.net.InetAddress;
Robert Greenwalt355205c2011-05-10 15:05:02 -070067import java.net.Inet4Address;
Robert Greenwalta7dfbd32010-06-15 15:43:39 -070068import java.net.UnknownHostException;
Robert Greenwalt2034b912009-08-12 16:08:25 -070069import java.util.ArrayList;
Robert Greenwalta7dfbd32010-06-15 15:43:39 -070070import java.util.Collection;
Robert Greenwaltd62c7002010-12-29 16:15:02 -080071import java.util.concurrent.atomic.AtomicBoolean;
Robert Greenwalt0e80be12010-09-20 14:35:25 -070072import java.util.GregorianCalendar;
Robert Greenwalt2034b912009-08-12 16:08:25 -070073import java.util.List;
The Android Open Source Project28527d22009-03-03 19:31:44 -080074
75/**
76 * @hide
77 */
78public class ConnectivityService extends IConnectivityManager.Stub {
79
Robert Greenwalt063dc7d2010-10-05 19:12:26 -070080 private static final boolean DBG = true;
Wink Saville051a6642011-07-13 13:44:13 -070081 private static final boolean VDBG = true;
The Android Open Source Project28527d22009-03-03 19:31:44 -080082 private static final String TAG = "ConnectivityService";
83
Robert Greenwalt2034b912009-08-12 16:08:25 -070084 // how long to wait before switching back to a radio's default network
85 private static final int RESTORE_DEFAULT_NETWORK_DELAY = 1 * 60 * 1000;
86 // system property that can override the above value
87 private static final String NETWORK_RESTORE_DELAY_PROP_NAME =
88 "android.telephony.apn-restore";
89
Robert Greenwaltbd492212011-05-06 17:10:53 -070090 // used in recursive route setting to add gateways for the host for which
91 // a host route was requested.
92 private static final int MAX_HOSTROUTE_CYCLE_COUNT = 10;
93
Robert Greenwalt0c4828c2010-01-26 11:40:34 -080094 private Tethering mTethering;
Robert Greenwaltf1b66e12010-02-25 12:29:30 -080095 private boolean mTetheringConfigValid = false;
Robert Greenwalt0c4828c2010-01-26 11:40:34 -080096
The Android Open Source Project28527d22009-03-03 19:31:44 -080097 /**
98 * Sometimes we want to refer to the individual network state
99 * trackers separately, and sometimes we just want to treat them
100 * abstractly.
101 */
102 private NetworkStateTracker mNetTrackers[];
Robert Greenwalt2034b912009-08-12 16:08:25 -0700103
104 /**
Wink Saville051a6642011-07-13 13:44:13 -0700105 * The link properties that define the current links
106 */
107 private LinkProperties mCurrentLinkProperties[];
108
109 /**
Robert Greenwalt2034b912009-08-12 16:08:25 -0700110 * A per Net list of the PID's that requested access to the net
111 * used both as a refcount and for per-PID DNS selection
112 */
113 private List mNetRequestersPids[];
114
Irfan Sheriff653e2a22010-06-07 09:03:04 -0700115 private WifiWatchdogService mWifiWatchdogService;
116
Robert Greenwalt2034b912009-08-12 16:08:25 -0700117 // priority order of the nettrackers
118 // (excluding dynamically set mNetworkPreference)
119 // TODO - move mNetworkTypePreference into this
120 private int[] mPriorityList;
121
The Android Open Source Project28527d22009-03-03 19:31:44 -0800122 private Context mContext;
123 private int mNetworkPreference;
Robert Greenwalt2034b912009-08-12 16:08:25 -0700124 private int mActiveDefaultNetwork = -1;
Robert Greenwalt986c7412010-09-08 15:24:47 -0700125 // 0 is full bad, 100 is full good
126 private int mDefaultInetCondition = 0;
127 private int mDefaultInetConditionPublished = 0;
128 private boolean mInetConditionChangeInFlight = false;
129 private int mDefaultConnectionSequence = 0;
The Android Open Source Project28527d22009-03-03 19:31:44 -0800130
131 private int mNumDnsEntries;
The Android Open Source Project28527d22009-03-03 19:31:44 -0800132
133 private boolean mTestMode;
Joe Onorato56023ad2010-09-01 21:18:22 -0700134 private static ConnectivityService sServiceInstance;
The Android Open Source Project28527d22009-03-03 19:31:44 -0800135
Robert Greenwaltd62c7002010-12-29 16:15:02 -0800136 private AtomicBoolean mBackgroundDataEnabled = new AtomicBoolean(true);
137
Robert Greenwalt355205c2011-05-10 15:05:02 -0700138 private INetworkManagementService mNetd;
139
Robert Greenwalt6a2db8a2010-09-23 10:05:56 -0700140 private static final int ENABLED = 1;
141 private static final int DISABLED = 0;
142
143 // Share the event space with NetworkStateTracker (which can't see this
144 // internal class but sends us events). If you change these, change
145 // NetworkStateTracker.java too.
146 private static final int MIN_NETWORK_STATE_TRACKER_EVENT = 1;
147 private static final int MAX_NETWORK_STATE_TRACKER_EVENT = 100;
148
149 /**
150 * used internally as a delayed event to make us switch back to the
151 * default network
152 */
153 private static final int EVENT_RESTORE_DEFAULT_NETWORK =
154 MAX_NETWORK_STATE_TRACKER_EVENT + 1;
155
156 /**
157 * used internally to change our mobile data enabled flag
158 */
159 private static final int EVENT_CHANGE_MOBILE_DATA_ENABLED =
160 MAX_NETWORK_STATE_TRACKER_EVENT + 2;
161
162 /**
163 * used internally to change our network preference setting
164 * arg1 = networkType to prefer
165 */
166 private static final int EVENT_SET_NETWORK_PREFERENCE =
167 MAX_NETWORK_STATE_TRACKER_EVENT + 3;
168
169 /**
170 * used internally to synchronize inet condition reports
171 * arg1 = networkType
172 * arg2 = condition (0 bad, 100 good)
173 */
174 private static final int EVENT_INET_CONDITION_CHANGE =
175 MAX_NETWORK_STATE_TRACKER_EVENT + 4;
176
177 /**
178 * used internally to mark the end of inet condition hold periods
179 * arg1 = networkType
180 */
181 private static final int EVENT_INET_CONDITION_HOLD_END =
182 MAX_NETWORK_STATE_TRACKER_EVENT + 5;
183
184 /**
185 * used internally to set the background data preference
186 * arg1 = TRUE for enabled, FALSE for disabled
187 */
188 private static final int EVENT_SET_BACKGROUND_DATA =
189 MAX_NETWORK_STATE_TRACKER_EVENT + 6;
190
191 /**
192 * used internally to set enable/disable cellular data
193 * arg1 = ENBALED or DISABLED
194 */
195 private static final int EVENT_SET_MOBILE_DATA =
196 MAX_NETWORK_STATE_TRACKER_EVENT + 7;
197
Robert Greenwaltccb36f92010-09-24 14:32:21 -0700198 /**
199 * used internally to clear a wakelock when transitioning
200 * from one net to another
201 */
202 private static final int EVENT_CLEAR_NET_TRANSITION_WAKELOCK =
203 MAX_NETWORK_STATE_TRACKER_EVENT + 8;
204
Robert Greenwaltc3c5f862010-10-11 16:00:27 -0700205 /**
206 * used internally to reload global proxy settings
207 */
208 private static final int EVENT_APPLY_GLOBAL_HTTP_PROXY =
209 MAX_NETWORK_STATE_TRACKER_EVENT + 9;
210
Robert Greenwalt34848c02011-03-25 13:09:25 -0700211 /**
212 * used internally to set external dependency met/unmet
213 * arg1 = ENABLED (met) or DISABLED (unmet)
214 * arg2 = NetworkType
215 */
216 private static final int EVENT_SET_DEPENDENCY_MET =
217 MAX_NETWORK_STATE_TRACKER_EVENT + 10;
218
Robert Greenwalt2034b912009-08-12 16:08:25 -0700219 private Handler mHandler;
220
221 // list of DeathRecipients used to make sure features are turned off when
222 // a process dies
223 private List mFeatureUsers;
224
Mike Lockwoodfde2b762009-08-14 14:18:49 -0400225 private boolean mSystemReady;
Dianne Hackborna417ff82009-12-08 19:45:14 -0800226 private Intent mInitialBroadcast;
Mike Lockwoodfde2b762009-08-14 14:18:49 -0400227
Robert Greenwalt93dc1042010-06-15 12:19:37 -0700228 private PowerManager.WakeLock mNetTransitionWakeLock;
229 private String mNetTransitionWakeLockCausedBy = "";
230 private int mNetTransitionWakeLockSerialNumber;
231 private int mNetTransitionWakeLockTimeout;
232
Robert Greenwalt94daa182010-09-01 11:34:05 -0700233 private InetAddress mDefaultDns;
234
Robert Greenwalt0e80be12010-09-20 14:35:25 -0700235 // used in DBG mode to track inet condition reports
236 private static final int INET_CONDITION_LOG_MAX_SIZE = 15;
237 private ArrayList mInetLog;
238
Robert Greenwaltc3c5f862010-10-11 16:00:27 -0700239 // track the current default http proxy - tell the world if we get a new one (real change)
240 private ProxyProperties mDefaultProxy = null;
241 // track the global proxy.
242 private ProxyProperties mGlobalProxy = null;
243 private final Object mGlobalProxyLock = new Object();
244
245 private SettingsObserver mSettingsObserver;
246
Robert Greenwalt34848c02011-03-25 13:09:25 -0700247 NetworkConfig[] mNetConfigs;
Robert Greenwaltec05b3c2009-10-30 14:17:42 -0700248 int mNetworksDefined;
Robert Greenwalt2034b912009-08-12 16:08:25 -0700249
Robert Greenwalt12c44552009-12-07 11:33:18 -0800250 private static class RadioAttributes {
Robert Greenwalt2034b912009-08-12 16:08:25 -0700251 public int mSimultaneity;
252 public int mType;
253 public RadioAttributes(String init) {
254 String fragments[] = init.split(",");
Robert Greenwaltec05b3c2009-10-30 14:17:42 -0700255 mType = Integer.parseInt(fragments[0]);
256 mSimultaneity = Integer.parseInt(fragments[1]);
Robert Greenwalt2034b912009-08-12 16:08:25 -0700257 }
258 }
259 RadioAttributes[] mRadioAttributes;
260
Robert Greenwalt6cac0742011-06-21 17:26:14 -0700261 // the set of network types that can only be enabled by system/sig apps
262 List mProtectedNetworks;
263
Wink Saville775aad62010-09-02 19:23:52 -0700264 public static synchronized ConnectivityService getInstance(Context context) {
265 if (sServiceInstance == null) {
266 sServiceInstance = new ConnectivityService(context);
The Android Open Source Project28527d22009-03-03 19:31:44 -0800267 }
Wink Saville775aad62010-09-02 19:23:52 -0700268 return sServiceInstance;
The Android Open Source Project28527d22009-03-03 19:31:44 -0800269 }
Robert Greenwalt0659da32009-07-16 17:21:39 -0700270
The Android Open Source Project28527d22009-03-03 19:31:44 -0800271 private ConnectivityService(Context context) {
Wink Savillee70c6f52010-12-03 12:01:38 -0800272 if (DBG) log("ConnectivityService starting up");
Robert Greenwaltd48f8ee2010-01-14 17:47:58 -0800273
Wink Saville775aad62010-09-02 19:23:52 -0700274 HandlerThread handlerThread = new HandlerThread("ConnectivityServiceThread");
275 handlerThread.start();
276 mHandler = new MyHandler(handlerThread.getLooper());
277
Robert Greenwaltd62c7002010-12-29 16:15:02 -0800278 mBackgroundDataEnabled.set(Settings.Secure.getInt(context.getContentResolver(),
279 Settings.Secure.BACKGROUND_DATA, 1) == 1);
280
Robert Greenwaltd48f8ee2010-01-14 17:47:58 -0800281 // setup our unique device name
Robert Greenwalt82cde132010-12-06 09:30:17 -0800282 if (TextUtils.isEmpty(SystemProperties.get("net.hostname"))) {
283 String id = Settings.Secure.getString(context.getContentResolver(),
284 Settings.Secure.ANDROID_ID);
285 if (id != null && id.length() > 0) {
286 String name = new String("android_").concat(id);
287 SystemProperties.set("net.hostname", name);
288 }
Robert Greenwaltd48f8ee2010-01-14 17:47:58 -0800289 }
290
Robert Greenwalt94daa182010-09-01 11:34:05 -0700291 // read our default dns server ip
292 String dns = Settings.Secure.getString(context.getContentResolver(),
293 Settings.Secure.DEFAULT_DNS_SERVER);
294 if (dns == null || dns.length() == 0) {
295 dns = context.getResources().getString(
296 com.android.internal.R.string.config_default_dns_server);
297 }
298 try {
Robert Greenwalt35e34d12011-02-22 16:00:42 -0800299 mDefaultDns = NetworkUtils.numericToInetAddress(dns);
300 } catch (IllegalArgumentException e) {
Wink Savillee70c6f52010-12-03 12:01:38 -0800301 loge("Error setting defaultDns using " + dns);
Robert Greenwalt94daa182010-09-01 11:34:05 -0700302 }
303
The Android Open Source Project28527d22009-03-03 19:31:44 -0800304 mContext = context;
Robert Greenwalt93dc1042010-06-15 12:19:37 -0700305
306 PowerManager powerManager = (PowerManager)mContext.getSystemService(Context.POWER_SERVICE);
307 mNetTransitionWakeLock = powerManager.newWakeLock(PowerManager.PARTIAL_WAKE_LOCK, TAG);
308 mNetTransitionWakeLockTimeout = mContext.getResources().getInteger(
309 com.android.internal.R.integer.config_networkTransitionTimeout);
310
Robert Greenwalt2034b912009-08-12 16:08:25 -0700311 mNetTrackers = new NetworkStateTracker[
312 ConnectivityManager.MAX_NETWORK_TYPE+1];
Wink Saville051a6642011-07-13 13:44:13 -0700313 mCurrentLinkProperties = new LinkProperties[ConnectivityManager.MAX_NETWORK_TYPE+1];
Robert Greenwalt0659da32009-07-16 17:21:39 -0700314
The Android Open Source Project28527d22009-03-03 19:31:44 -0800315 mNetworkPreference = getPersistedNetworkPreference();
Robert Greenwalt0659da32009-07-16 17:21:39 -0700316
Robert Greenwaltec05b3c2009-10-30 14:17:42 -0700317 mRadioAttributes = new RadioAttributes[ConnectivityManager.MAX_RADIO_TYPE+1];
Robert Greenwalt34848c02011-03-25 13:09:25 -0700318 mNetConfigs = new NetworkConfig[ConnectivityManager.MAX_NETWORK_TYPE+1];
Robert Greenwaltec05b3c2009-10-30 14:17:42 -0700319
Robert Greenwalt2034b912009-08-12 16:08:25 -0700320 // Load device network attributes from resources
Robert Greenwalt2034b912009-08-12 16:08:25 -0700321 String[] raStrings = context.getResources().getStringArray(
322 com.android.internal.R.array.radioAttributes);
Robert Greenwaltec05b3c2009-10-30 14:17:42 -0700323 for (String raString : raStrings) {
324 RadioAttributes r = new RadioAttributes(raString);
325 if (r.mType > ConnectivityManager.MAX_RADIO_TYPE) {
Wink Savillee70c6f52010-12-03 12:01:38 -0800326 loge("Error in radioAttributes - ignoring attempt to define type " + r.mType);
Robert Greenwaltec05b3c2009-10-30 14:17:42 -0700327 continue;
328 }
329 if (mRadioAttributes[r.mType] != null) {
Wink Savillee70c6f52010-12-03 12:01:38 -0800330 loge("Error in radioAttributes - ignoring attempt to redefine type " +
Robert Greenwaltec05b3c2009-10-30 14:17:42 -0700331 r.mType);
332 continue;
333 }
Robert Greenwalt2034b912009-08-12 16:08:25 -0700334 mRadioAttributes[r.mType] = r;
335 }
336
Robert Greenwaltec05b3c2009-10-30 14:17:42 -0700337 String[] naStrings = context.getResources().getStringArray(
338 com.android.internal.R.array.networkAttributes);
339 for (String naString : naStrings) {
340 try {
Robert Greenwalt34848c02011-03-25 13:09:25 -0700341 NetworkConfig n = new NetworkConfig(naString);
Wink Savillef2a62832011-04-07 14:23:45 -0700342 if (n.type > ConnectivityManager.MAX_NETWORK_TYPE) {
Wink Savillee70c6f52010-12-03 12:01:38 -0800343 loge("Error in networkAttributes - ignoring attempt to define type " +
Wink Savillef2a62832011-04-07 14:23:45 -0700344 n.type);
Robert Greenwaltec05b3c2009-10-30 14:17:42 -0700345 continue;
Robert Greenwalt2034b912009-08-12 16:08:25 -0700346 }
Wink Savillef2a62832011-04-07 14:23:45 -0700347 if (mNetConfigs[n.type] != null) {
Wink Savillee70c6f52010-12-03 12:01:38 -0800348 loge("Error in networkAttributes - ignoring attempt to redefine type " +
Wink Savillef2a62832011-04-07 14:23:45 -0700349 n.type);
Robert Greenwaltec05b3c2009-10-30 14:17:42 -0700350 continue;
351 }
Wink Savillef2a62832011-04-07 14:23:45 -0700352 if (mRadioAttributes[n.radio] == null) {
Wink Savillee70c6f52010-12-03 12:01:38 -0800353 loge("Error in networkAttributes - ignoring attempt to use undefined " +
Wink Savillef2a62832011-04-07 14:23:45 -0700354 "radio " + n.radio + " in network type " + n.type);
Robert Greenwaltec05b3c2009-10-30 14:17:42 -0700355 continue;
356 }
Wink Savillef2a62832011-04-07 14:23:45 -0700357 mNetConfigs[n.type] = n;
Robert Greenwaltec05b3c2009-10-30 14:17:42 -0700358 mNetworksDefined++;
359 } catch(Exception e) {
360 // ignore it - leave the entry null
Robert Greenwalt2034b912009-08-12 16:08:25 -0700361 }
362 }
363
Robert Greenwalt6cac0742011-06-21 17:26:14 -0700364 mProtectedNetworks = new ArrayList<Integer>();
365 int[] protectedNetworks = context.getResources().getIntArray(
366 com.android.internal.R.array.config_protectedNetworks);
367 for (int p : protectedNetworks) {
368 if ((mNetConfigs[p] != null) && (mProtectedNetworks.contains(p) == false)) {
369 mProtectedNetworks.add(p);
370 } else {
371 if (DBG) loge("Ignoring protectedNetwork " + p);
372 }
373 }
374
Robert Greenwaltec05b3c2009-10-30 14:17:42 -0700375 // high priority first
376 mPriorityList = new int[mNetworksDefined];
377 {
378 int insertionPoint = mNetworksDefined-1;
379 int currentLowest = 0;
380 int nextLowest = 0;
381 while (insertionPoint > -1) {
Robert Greenwalt34848c02011-03-25 13:09:25 -0700382 for (NetworkConfig na : mNetConfigs) {
Robert Greenwaltec05b3c2009-10-30 14:17:42 -0700383 if (na == null) continue;
Wink Savillef2a62832011-04-07 14:23:45 -0700384 if (na.priority < currentLowest) continue;
385 if (na.priority > currentLowest) {
386 if (na.priority < nextLowest || nextLowest == 0) {
387 nextLowest = na.priority;
Robert Greenwaltec05b3c2009-10-30 14:17:42 -0700388 }
389 continue;
390 }
Wink Savillef2a62832011-04-07 14:23:45 -0700391 mPriorityList[insertionPoint--] = na.type;
Robert Greenwaltec05b3c2009-10-30 14:17:42 -0700392 }
393 currentLowest = nextLowest;
394 nextLowest = 0;
395 }
396 }
397
398 mNetRequestersPids = new ArrayList[ConnectivityManager.MAX_NETWORK_TYPE+1];
399 for (int i : mPriorityList) {
Robert Greenwalt2034b912009-08-12 16:08:25 -0700400 mNetRequestersPids[i] = new ArrayList();
401 }
402
403 mFeatureUsers = new ArrayList();
404
Robert Greenwaltec05b3c2009-10-30 14:17:42 -0700405 mNumDnsEntries = 0;
406
407 mTestMode = SystemProperties.get("cm.test.mode").equals("true")
408 && SystemProperties.get("ro.build.type").equals("eng");
The Android Open Source Project28527d22009-03-03 19:31:44 -0800409 /*
410 * Create the network state trackers for Wi-Fi and mobile
411 * data. Maybe this could be done with a factory class,
412 * but it's not clear that it's worth it, given that
413 * the number of different network types is not going
414 * to change very often.
415 */
Robert Greenwaltec05b3c2009-10-30 14:17:42 -0700416 for (int netType : mPriorityList) {
Wink Savillef2a62832011-04-07 14:23:45 -0700417 switch (mNetConfigs[netType].radio) {
Robert Greenwaltec05b3c2009-10-30 14:17:42 -0700418 case ConnectivityManager.TYPE_WIFI:
Wink Savillee70c6f52010-12-03 12:01:38 -0800419 if (DBG) log("Starting Wifi Service.");
Wink Saville7fabfa22010-08-13 16:11:42 -0700420 WifiStateTracker wst = new WifiStateTracker();
Irfan Sheriff25be0762010-07-28 09:35:20 -0700421 WifiService wifiService = new WifiService(context);
Robert Greenwaltec05b3c2009-10-30 14:17:42 -0700422 ServiceManager.addService(Context.WIFI_SERVICE, wifiService);
Irfan Sheriff25be0762010-07-28 09:35:20 -0700423 wifiService.checkAndStartWifi();
Robert Greenwaltec05b3c2009-10-30 14:17:42 -0700424 mNetTrackers[ConnectivityManager.TYPE_WIFI] = wst;
Wink Saville7fabfa22010-08-13 16:11:42 -0700425 wst.startMonitoring(context, mHandler);
The Android Open Source Project28527d22009-03-03 19:31:44 -0800426
Irfan Sheriff653e2a22010-06-07 09:03:04 -0700427 //TODO: as part of WWS refactor, create only when needed
Irfan Sheriff25be0762010-07-28 09:35:20 -0700428 mWifiWatchdogService = new WifiWatchdogService(context);
Irfan Sheriff653e2a22010-06-07 09:03:04 -0700429
Robert Greenwaltec05b3c2009-10-30 14:17:42 -0700430 break;
431 case ConnectivityManager.TYPE_MOBILE:
Wink Saville7fabfa22010-08-13 16:11:42 -0700432 mNetTrackers[netType] = new MobileDataStateTracker(netType,
Wink Savillef2a62832011-04-07 14:23:45 -0700433 mNetConfigs[netType].name);
Wink Saville7fabfa22010-08-13 16:11:42 -0700434 mNetTrackers[netType].startMonitoring(context, mHandler);
Robert Greenwaltec05b3c2009-10-30 14:17:42 -0700435 break;
Robert Greenwalteb123ac2010-12-06 13:56:24 -0800436 case ConnectivityManager.TYPE_DUMMY:
437 mNetTrackers[netType] = new DummyDataStateTracker(netType,
Wink Savillef2a62832011-04-07 14:23:45 -0700438 mNetConfigs[netType].name);
Robert Greenwalteb123ac2010-12-06 13:56:24 -0800439 mNetTrackers[netType].startMonitoring(context, mHandler);
440 break;
Jaikumar Ganesh0db51a02010-12-21 22:31:44 -0800441 case ConnectivityManager.TYPE_BLUETOOTH:
442 mNetTrackers[netType] = BluetoothTetheringDataTracker.getInstance();
443 mNetTrackers[netType].startMonitoring(context, mHandler);
444 break;
Benoit Goby6cec7f32010-12-22 14:29:40 -0800445 case ConnectivityManager.TYPE_ETHERNET:
446 mNetTrackers[netType] = EthernetDataTracker.getInstance();
447 mNetTrackers[netType].startMonitoring(context, mHandler);
448 break;
Robert Greenwaltec05b3c2009-10-30 14:17:42 -0700449 default:
Wink Savillee70c6f52010-12-03 12:01:38 -0800450 loge("Trying to create a DataStateTracker for an unknown radio type " +
Wink Savillef2a62832011-04-07 14:23:45 -0700451 mNetConfigs[netType].radio);
Robert Greenwaltec05b3c2009-10-30 14:17:42 -0700452 continue;
453 }
Wink Saville051a6642011-07-13 13:44:13 -0700454 mCurrentLinkProperties[netType] = mNetTrackers[netType].getLinkProperties();
Robert Greenwaltec05b3c2009-10-30 14:17:42 -0700455 }
Robert Greenwalt0c4828c2010-01-26 11:40:34 -0800456
Robert Greenwaltc0b6c602010-03-11 15:03:08 -0800457 mTethering = new Tethering(mContext, mHandler.getLooper());
Robert Greenwalt33cdcdf2011-06-02 17:30:47 -0700458 mTetheringConfigValid = ((mTethering.getTetherableUsbRegexs().length != 0 ||
Danica Chang96567052010-08-11 14:54:43 -0700459 mTethering.getTetherableWifiRegexs().length != 0 ||
460 mTethering.getTetherableBluetoothRegexs().length != 0) &&
Robert Greenwalt33cdcdf2011-06-02 17:30:47 -0700461 mTethering.getUpstreamIfaceTypes().length != 0);
Robert Greenwaltf1b66e12010-02-25 12:29:30 -0800462
Robert Greenwalt0e80be12010-09-20 14:35:25 -0700463 if (DBG) {
464 mInetLog = new ArrayList();
465 }
Robert Greenwaltc3c5f862010-10-11 16:00:27 -0700466
467 mSettingsObserver = new SettingsObserver(mHandler, EVENT_APPLY_GLOBAL_HTTP_PROXY);
468 mSettingsObserver.observe(mContext);
Robert Greenwalt6f7c6092010-12-02 11:31:00 -0800469
470 loadGlobalProxy();
Hung-ying Tyan4e723422011-01-19 16:48:38 +0800471
472 VpnManager.startVpnService(context);
The Android Open Source Project28527d22009-03-03 19:31:44 -0800473 }
474
Robert Greenwaltec05b3c2009-10-30 14:17:42 -0700475
The Android Open Source Project28527d22009-03-03 19:31:44 -0800476 /**
Robert Greenwalt0659da32009-07-16 17:21:39 -0700477 * Sets the preferred network.
The Android Open Source Project28527d22009-03-03 19:31:44 -0800478 * @param preference the new preference
479 */
Robert Greenwalt6a2db8a2010-09-23 10:05:56 -0700480 public void setNetworkPreference(int preference) {
The Android Open Source Project28527d22009-03-03 19:31:44 -0800481 enforceChangePermission();
Robert Greenwalt6a2db8a2010-09-23 10:05:56 -0700482
483 mHandler.sendMessage(mHandler.obtainMessage(EVENT_SET_NETWORK_PREFERENCE, preference, 0));
The Android Open Source Project28527d22009-03-03 19:31:44 -0800484 }
485
486 public int getNetworkPreference() {
487 enforceAccessPermission();
Robert Greenwalt6a2db8a2010-09-23 10:05:56 -0700488 int preference;
489 synchronized(this) {
490 preference = mNetworkPreference;
491 }
492 return preference;
The Android Open Source Project28527d22009-03-03 19:31:44 -0800493 }
494
Robert Greenwalt6a2db8a2010-09-23 10:05:56 -0700495 private void handleSetNetworkPreference(int preference) {
496 if (ConnectivityManager.isNetworkTypeValid(preference) &&
Robert Greenwalt34848c02011-03-25 13:09:25 -0700497 mNetConfigs[preference] != null &&
498 mNetConfigs[preference].isDefault()) {
Robert Greenwalt6a2db8a2010-09-23 10:05:56 -0700499 if (mNetworkPreference != preference) {
500 final ContentResolver cr = mContext.getContentResolver();
501 Settings.Secure.putInt(cr, Settings.Secure.NETWORK_PREFERENCE, preference);
502 synchronized(this) {
503 mNetworkPreference = preference;
504 }
505 enforcePreference();
506 }
507 }
The Android Open Source Project28527d22009-03-03 19:31:44 -0800508 }
Robert Greenwalt0659da32009-07-16 17:21:39 -0700509
The Android Open Source Project28527d22009-03-03 19:31:44 -0800510 private int getPersistedNetworkPreference() {
511 final ContentResolver cr = mContext.getContentResolver();
512
513 final int networkPrefSetting = Settings.Secure
514 .getInt(cr, Settings.Secure.NETWORK_PREFERENCE, -1);
515 if (networkPrefSetting != -1) {
516 return networkPrefSetting;
517 }
518
519 return ConnectivityManager.DEFAULT_NETWORK_PREFERENCE;
520 }
Robert Greenwalt0659da32009-07-16 17:21:39 -0700521
The Android Open Source Project28527d22009-03-03 19:31:44 -0800522 /**
Robert Greenwalt0659da32009-07-16 17:21:39 -0700523 * Make the state of network connectivity conform to the preference settings
The Android Open Source Project28527d22009-03-03 19:31:44 -0800524 * In this method, we only tear down a non-preferred network. Establishing
525 * a connection to the preferred network is taken care of when we handle
526 * the disconnect event from the non-preferred network
527 * (see {@link #handleDisconnect(NetworkInfo)}).
528 */
529 private void enforcePreference() {
Robert Greenwalt2034b912009-08-12 16:08:25 -0700530 if (mNetTrackers[mNetworkPreference].getNetworkInfo().isConnected())
The Android Open Source Project28527d22009-03-03 19:31:44 -0800531 return;
532
Robert Greenwalt2034b912009-08-12 16:08:25 -0700533 if (!mNetTrackers[mNetworkPreference].isAvailable())
534 return;
The Android Open Source Project28527d22009-03-03 19:31:44 -0800535
Robert Greenwalt2034b912009-08-12 16:08:25 -0700536 for (int t=0; t <= ConnectivityManager.MAX_RADIO_TYPE; t++) {
Robert Greenwaltec05b3c2009-10-30 14:17:42 -0700537 if (t != mNetworkPreference && mNetTrackers[t] != null &&
Robert Greenwalt2034b912009-08-12 16:08:25 -0700538 mNetTrackers[t].getNetworkInfo().isConnected()) {
Robert Greenwaltf3f045b2009-08-20 15:25:14 -0700539 if (DBG) {
Wink Savillee70c6f52010-12-03 12:01:38 -0800540 log("tearing down " + mNetTrackers[t].getNetworkInfo() +
Robert Greenwaltf3f045b2009-08-20 15:25:14 -0700541 " in enforcePreference");
542 }
Robert Greenwalt2034b912009-08-12 16:08:25 -0700543 teardown(mNetTrackers[t]);
The Android Open Source Project28527d22009-03-03 19:31:44 -0800544 }
545 }
546 }
547
548 private boolean teardown(NetworkStateTracker netTracker) {
549 if (netTracker.teardown()) {
550 netTracker.setTeardownRequested(true);
551 return true;
552 } else {
553 return false;
554 }
555 }
556
557 /**
558 * Return NetworkInfo for the active (i.e., connected) network interface.
559 * It is assumed that at most one network is active at a time. If more
560 * than one is active, it is indeterminate which will be returned.
Robert Greenwalt0659da32009-07-16 17:21:39 -0700561 * @return the info for the active network, or {@code null} if none is
562 * active
The Android Open Source Project28527d22009-03-03 19:31:44 -0800563 */
564 public NetworkInfo getActiveNetworkInfo() {
Robert Greenwalte1544bb2011-05-20 12:23:41 -0700565 return getNetworkInfo(mActiveDefaultNetwork);
The Android Open Source Project28527d22009-03-03 19:31:44 -0800566 }
567
568 public NetworkInfo getNetworkInfo(int networkType) {
569 enforceAccessPermission();
570 if (ConnectivityManager.isNetworkTypeValid(networkType)) {
571 NetworkStateTracker t = mNetTrackers[networkType];
572 if (t != null)
573 return t.getNetworkInfo();
574 }
575 return null;
576 }
577
578 public NetworkInfo[] getAllNetworkInfo() {
579 enforceAccessPermission();
Robert Greenwaltec05b3c2009-10-30 14:17:42 -0700580 NetworkInfo[] result = new NetworkInfo[mNetworksDefined];
The Android Open Source Project28527d22009-03-03 19:31:44 -0800581 int i = 0;
582 for (NetworkStateTracker t : mNetTrackers) {
Robert Greenwaltec05b3c2009-10-30 14:17:42 -0700583 if(t != null) result[i++] = t.getNetworkInfo();
The Android Open Source Project28527d22009-03-03 19:31:44 -0800584 }
585 return result;
586 }
587
Robert Greenwalt9f0ee4f2010-09-14 09:18:02 -0700588 /**
589 * Return LinkProperties for the active (i.e., connected) default
590 * network interface. It is assumed that at most one default network
591 * is active at a time. If more than one is active, it is indeterminate
592 * which will be returned.
593 * @return the ip properties for the active network, or {@code null} if
594 * none is active
595 */
596 public LinkProperties getActiveLinkProperties() {
Robert Greenwalte1544bb2011-05-20 12:23:41 -0700597 return getLinkProperties(mActiveDefaultNetwork);
Robert Greenwalt9f0ee4f2010-09-14 09:18:02 -0700598 }
599
600 public LinkProperties getLinkProperties(int networkType) {
601 enforceAccessPermission();
602 if (ConnectivityManager.isNetworkTypeValid(networkType)) {
603 NetworkStateTracker t = mNetTrackers[networkType];
604 if (t != null) return t.getLinkProperties();
605 }
606 return null;
607 }
608
The Android Open Source Project28527d22009-03-03 19:31:44 -0800609 public boolean setRadios(boolean turnOn) {
610 boolean result = true;
611 enforceChangePermission();
612 for (NetworkStateTracker t : mNetTrackers) {
Robert Greenwaltec05b3c2009-10-30 14:17:42 -0700613 if (t != null) result = t.setRadio(turnOn) && result;
The Android Open Source Project28527d22009-03-03 19:31:44 -0800614 }
615 return result;
616 }
617
618 public boolean setRadio(int netType, boolean turnOn) {
619 enforceChangePermission();
620 if (!ConnectivityManager.isNetworkTypeValid(netType)) {
621 return false;
622 }
623 NetworkStateTracker tracker = mNetTrackers[netType];
624 return tracker != null && tracker.setRadio(turnOn);
625 }
626
Robert Greenwaltaffc3a12009-09-27 17:27:04 -0700627 /**
628 * Used to notice when the calling process dies so we can self-expire
629 *
630 * Also used to know if the process has cleaned up after itself when
631 * our auto-expire timer goes off. The timer has a link to an object.
632 *
633 */
Robert Greenwalt2034b912009-08-12 16:08:25 -0700634 private class FeatureUser implements IBinder.DeathRecipient {
635 int mNetworkType;
636 String mFeature;
637 IBinder mBinder;
638 int mPid;
639 int mUid;
Robert Greenwalt3eeb6032009-12-21 18:24:07 -0800640 long mCreateTime;
Robert Greenwalt2034b912009-08-12 16:08:25 -0700641
642 FeatureUser(int type, String feature, IBinder binder) {
643 super();
644 mNetworkType = type;
645 mFeature = feature;
646 mBinder = binder;
647 mPid = getCallingPid();
648 mUid = getCallingUid();
Robert Greenwalt3eeb6032009-12-21 18:24:07 -0800649 mCreateTime = System.currentTimeMillis();
Robert Greenwaltaffc3a12009-09-27 17:27:04 -0700650
Robert Greenwalt2034b912009-08-12 16:08:25 -0700651 try {
652 mBinder.linkToDeath(this, 0);
653 } catch (RemoteException e) {
654 binderDied();
655 }
656 }
657
658 void unlinkDeathRecipient() {
659 mBinder.unlinkToDeath(this, 0);
660 }
661
662 public void binderDied() {
Wink Savillee70c6f52010-12-03 12:01:38 -0800663 log("ConnectivityService FeatureUser binderDied(" +
Robert Greenwalt3eeb6032009-12-21 18:24:07 -0800664 mNetworkType + ", " + mFeature + ", " + mBinder + "), created " +
665 (System.currentTimeMillis() - mCreateTime) + " mSec ago");
Robert Greenwaltaffc3a12009-09-27 17:27:04 -0700666 stopUsingNetworkFeature(this, false);
Robert Greenwalt2034b912009-08-12 16:08:25 -0700667 }
668
Robert Greenwaltaffc3a12009-09-27 17:27:04 -0700669 public void expire() {
Wink Savillee70c6f52010-12-03 12:01:38 -0800670 log("ConnectivityService FeatureUser expire(" +
Robert Greenwalt3eeb6032009-12-21 18:24:07 -0800671 mNetworkType + ", " + mFeature + ", " + mBinder +"), created " +
672 (System.currentTimeMillis() - mCreateTime) + " mSec ago");
Robert Greenwaltaffc3a12009-09-27 17:27:04 -0700673 stopUsingNetworkFeature(this, false);
674 }
Robert Greenwalt3eeb6032009-12-21 18:24:07 -0800675
676 public String toString() {
677 return "FeatureUser("+mNetworkType+","+mFeature+","+mPid+","+mUid+"), created " +
678 (System.currentTimeMillis() - mCreateTime) + " mSec ago";
679 }
Robert Greenwalt2034b912009-08-12 16:08:25 -0700680 }
681
Robert Greenwaltaffc3a12009-09-27 17:27:04 -0700682 // javadoc from interface
Robert Greenwalt2034b912009-08-12 16:08:25 -0700683 public int startUsingNetworkFeature(int networkType, String feature,
684 IBinder binder) {
685 if (DBG) {
Wink Savillee70c6f52010-12-03 12:01:38 -0800686 log("startUsingNetworkFeature for net " + networkType + ": " + feature);
Robert Greenwalt2034b912009-08-12 16:08:25 -0700687 }
The Android Open Source Project28527d22009-03-03 19:31:44 -0800688 enforceChangePermission();
Robert Greenwaltec05b3c2009-10-30 14:17:42 -0700689 if (!ConnectivityManager.isNetworkTypeValid(networkType) ||
Robert Greenwalt34848c02011-03-25 13:09:25 -0700690 mNetConfigs[networkType] == null) {
Robert Greenwalt2034b912009-08-12 16:08:25 -0700691 return Phone.APN_REQUEST_FAILED;
The Android Open Source Project28527d22009-03-03 19:31:44 -0800692 }
Robert Greenwalt2034b912009-08-12 16:08:25 -0700693
Robert Greenwaltaffc3a12009-09-27 17:27:04 -0700694 FeatureUser f = new FeatureUser(networkType, feature, binder);
Robert Greenwalt2034b912009-08-12 16:08:25 -0700695
696 // TODO - move this into the MobileDataStateTracker
697 int usedNetworkType = networkType;
698 if(networkType == ConnectivityManager.TYPE_MOBILE) {
Wink Savilleb7c92c72011-03-12 14:52:01 -0800699 usedNetworkType = convertFeatureToNetworkType(feature);
700 if (usedNetworkType < 0) {
701 Slog.e(TAG, "Can't match any netTracker!");
702 usedNetworkType = networkType;
Robert Greenwalt2034b912009-08-12 16:08:25 -0700703 }
704 }
Robert Greenwalt6cac0742011-06-21 17:26:14 -0700705
706 if (mProtectedNetworks.contains(usedNetworkType)) {
707 enforceConnectivityInternalPermission();
708 }
709
Robert Greenwalt2034b912009-08-12 16:08:25 -0700710 NetworkStateTracker network = mNetTrackers[usedNetworkType];
711 if (network != null) {
Robert Greenwalt5364d752010-12-15 13:26:33 -0800712 Integer currentPid = new Integer(getCallingPid());
Robert Greenwalt2034b912009-08-12 16:08:25 -0700713 if (usedNetworkType != networkType) {
Robert Greenwalt2034b912009-08-12 16:08:25 -0700714 NetworkStateTracker radio = mNetTrackers[networkType];
715 NetworkInfo ni = network.getNetworkInfo();
716
717 if (ni.isAvailable() == false) {
Wink Savillee70c6f52010-12-03 12:01:38 -0800718 if (DBG) log("special network not available");
Robert Greenwalt2cc87442010-12-29 14:35:21 -0800719 if (!TextUtils.equals(feature,Phone.FEATURE_ENABLE_DUN_ALWAYS)) {
720 return Phone.APN_TYPE_NOT_AVAILABLE;
721 } else {
722 // else make the attempt anyway - probably giving REQUEST_STARTED below
723 }
Robert Greenwalt2034b912009-08-12 16:08:25 -0700724 }
725
Robert Greenwaltaffc3a12009-09-27 17:27:04 -0700726 synchronized(this) {
727 mFeatureUsers.add(f);
728 if (!mNetRequestersPids[usedNetworkType].contains(currentPid)) {
729 // this gets used for per-pid dns when connected
730 mNetRequestersPids[usedNetworkType].add(currentPid);
731 }
Robert Greenwalt2034b912009-08-12 16:08:25 -0700732 }
Robert Greenwaltaffc3a12009-09-27 17:27:04 -0700733
Robert Greenwalt20f819c2011-05-03 19:02:44 -0700734 int restoreTimer = getRestoreDefaultNetworkDelay(usedNetworkType);
735
736 if (restoreTimer >= 0) {
737 mHandler.sendMessageDelayed(
738 mHandler.obtainMessage(EVENT_RESTORE_DEFAULT_NETWORK, f), restoreTimer);
739 }
Robert Greenwalt2034b912009-08-12 16:08:25 -0700740
Robert Greenwalta52c75a2009-08-19 20:19:33 -0700741 if ((ni.isConnectedOrConnecting() == true) &&
742 !network.isTeardownRequested()) {
Robert Greenwalt2034b912009-08-12 16:08:25 -0700743 if (ni.isConnected() == true) {
744 // add the pid-specific dns
Robert Greenwalt3afbead2010-07-23 15:46:26 -0700745 handleDnsConfigurationChange(networkType);
Wink Savillee70c6f52010-12-03 12:01:38 -0800746 if (DBG) log("special network already active");
Robert Greenwalt2034b912009-08-12 16:08:25 -0700747 return Phone.APN_ALREADY_ACTIVE;
748 }
Wink Savillee70c6f52010-12-03 12:01:38 -0800749 if (DBG) log("special network already connecting");
Robert Greenwalt2034b912009-08-12 16:08:25 -0700750 return Phone.APN_REQUEST_STARTED;
751 }
752
753 // check if the radio in play can make another contact
754 // assume if cannot for now
755
Wink Savillee70c6f52010-12-03 12:01:38 -0800756 if (DBG) log("reconnecting to special network");
Robert Greenwalt2034b912009-08-12 16:08:25 -0700757 network.reconnect();
758 return Phone.APN_REQUEST_STARTED;
759 } else {
Robert Greenwalt5364d752010-12-15 13:26:33 -0800760 // need to remember this unsupported request so we respond appropriately on stop
761 synchronized(this) {
762 mFeatureUsers.add(f);
763 if (!mNetRequestersPids[usedNetworkType].contains(currentPid)) {
764 // this gets used for per-pid dns when connected
765 mNetRequestersPids[usedNetworkType].add(currentPid);
766 }
767 }
Robert Greenwaltd391e892010-05-18 10:52:51 -0700768 return -1;
Robert Greenwalt2034b912009-08-12 16:08:25 -0700769 }
770 }
771 return Phone.APN_TYPE_NOT_AVAILABLE;
The Android Open Source Project28527d22009-03-03 19:31:44 -0800772 }
773
Robert Greenwaltaffc3a12009-09-27 17:27:04 -0700774 // javadoc from interface
The Android Open Source Project28527d22009-03-03 19:31:44 -0800775 public int stopUsingNetworkFeature(int networkType, String feature) {
Robert Greenwalt28f43012009-10-06 17:52:40 -0700776 enforceChangePermission();
777
Robert Greenwaltaffc3a12009-09-27 17:27:04 -0700778 int pid = getCallingPid();
779 int uid = getCallingUid();
780
781 FeatureUser u = null;
782 boolean found = false;
783
784 synchronized(this) {
785 for (int i = 0; i < mFeatureUsers.size() ; i++) {
786 u = (FeatureUser)mFeatureUsers.get(i);
787 if (uid == u.mUid && pid == u.mPid &&
788 networkType == u.mNetworkType &&
789 TextUtils.equals(feature, u.mFeature)) {
790 found = true;
791 break;
792 }
793 }
794 }
795 if (found && u != null) {
796 // stop regardless of how many other time this proc had called start
797 return stopUsingNetworkFeature(u, true);
798 } else {
799 // none found!
Wink Savillee70c6f52010-12-03 12:01:38 -0800800 if (DBG) log("ignoring stopUsingNetworkFeature - not a live request");
Robert Greenwaltaffc3a12009-09-27 17:27:04 -0700801 return 1;
802 }
Robert Greenwalt2034b912009-08-12 16:08:25 -0700803 }
804
Robert Greenwaltaffc3a12009-09-27 17:27:04 -0700805 private int stopUsingNetworkFeature(FeatureUser u, boolean ignoreDups) {
806 int networkType = u.mNetworkType;
807 String feature = u.mFeature;
808 int pid = u.mPid;
809 int uid = u.mUid;
810
811 NetworkStateTracker tracker = null;
812 boolean callTeardown = false; // used to carry our decision outside of sync block
813
Robert Greenwalt2034b912009-08-12 16:08:25 -0700814 if (DBG) {
Wink Savillee70c6f52010-12-03 12:01:38 -0800815 log("stopUsingNetworkFeature for net " + networkType +
Robert Greenwalt2034b912009-08-12 16:08:25 -0700816 ": " + feature);
817 }
Robert Greenwalt28f43012009-10-06 17:52:40 -0700818
The Android Open Source Project28527d22009-03-03 19:31:44 -0800819 if (!ConnectivityManager.isNetworkTypeValid(networkType)) {
820 return -1;
821 }
Robert Greenwalt2034b912009-08-12 16:08:25 -0700822
Robert Greenwaltaffc3a12009-09-27 17:27:04 -0700823 // need to link the mFeatureUsers list with the mNetRequestersPids state in this
824 // sync block
825 synchronized(this) {
826 // check if this process still has an outstanding start request
827 if (!mFeatureUsers.contains(u)) {
Wink Savillee70c6f52010-12-03 12:01:38 -0800828 if (DBG) log("ignoring - this process has no outstanding requests");
Robert Greenwalt2034b912009-08-12 16:08:25 -0700829 return 1;
830 }
Robert Greenwaltaffc3a12009-09-27 17:27:04 -0700831 u.unlinkDeathRecipient();
832 mFeatureUsers.remove(mFeatureUsers.indexOf(u));
833 // If we care about duplicate requests, check for that here.
834 //
835 // This is done to support the extension of a request - the app
836 // can request we start the network feature again and renew the
837 // auto-shutoff delay. Normal "stop" calls from the app though
838 // do not pay attention to duplicate requests - in effect the
839 // API does not refcount and a single stop will counter multiple starts.
840 if (ignoreDups == false) {
841 for (int i = 0; i < mFeatureUsers.size() ; i++) {
842 FeatureUser x = (FeatureUser)mFeatureUsers.get(i);
843 if (x.mUid == u.mUid && x.mPid == u.mPid &&
844 x.mNetworkType == u.mNetworkType &&
845 TextUtils.equals(x.mFeature, u.mFeature)) {
Wink Savillee70c6f52010-12-03 12:01:38 -0800846 if (DBG) log("ignoring stopUsingNetworkFeature as dup is found");
Robert Greenwaltaffc3a12009-09-27 17:27:04 -0700847 return 1;
848 }
849 }
Robert Greenwalt2034b912009-08-12 16:08:25 -0700850 }
Robert Greenwaltaffc3a12009-09-27 17:27:04 -0700851
852 // TODO - move to MobileDataStateTracker
853 int usedNetworkType = networkType;
854 if (networkType == ConnectivityManager.TYPE_MOBILE) {
Wink Savilleb7c92c72011-03-12 14:52:01 -0800855 usedNetworkType = convertFeatureToNetworkType(feature);
856 if (usedNetworkType < 0) {
857 usedNetworkType = networkType;
Robert Greenwaltaffc3a12009-09-27 17:27:04 -0700858 }
859 }
860 tracker = mNetTrackers[usedNetworkType];
Robert Greenwaltec05b3c2009-10-30 14:17:42 -0700861 if (tracker == null) {
Wink Savillee70c6f52010-12-03 12:01:38 -0800862 if (DBG) log("ignoring - no known tracker for net type " + usedNetworkType);
Robert Greenwaltec05b3c2009-10-30 14:17:42 -0700863 return -1;
864 }
865 if (usedNetworkType != networkType) {
Robert Greenwaltaffc3a12009-09-27 17:27:04 -0700866 Integer currentPid = new Integer(pid);
Robert Greenwaltaffc3a12009-09-27 17:27:04 -0700867 mNetRequestersPids[usedNetworkType].remove(currentPid);
Robert Greenwalt0ca68a02009-12-17 14:54:59 -0800868 reassessPidDns(pid, true);
Robert Greenwaltaffc3a12009-09-27 17:27:04 -0700869 if (mNetRequestersPids[usedNetworkType].size() != 0) {
Wink Savillee70c6f52010-12-03 12:01:38 -0800870 if (DBG) log("not tearing down special network - " +
Robert Greenwaltaffc3a12009-09-27 17:27:04 -0700871 "others still using it");
872 return 1;
873 }
874 callTeardown = true;
Robert Greenwalt9f3be4c2011-01-10 11:58:31 -0800875 } else {
876 if (DBG) log("not a known feature - dropping");
Robert Greenwaltaffc3a12009-09-27 17:27:04 -0700877 }
878 }
Wink Savillee70c6f52010-12-03 12:01:38 -0800879 if (DBG) log("Doing network teardown");
Robert Greenwaltaffc3a12009-09-27 17:27:04 -0700880 if (callTeardown) {
881 tracker.teardown();
Robert Greenwalt2034b912009-08-12 16:08:25 -0700882 return 1;
883 } else {
Robert Greenwaltd391e892010-05-18 10:52:51 -0700884 return -1;
Robert Greenwalt2034b912009-08-12 16:08:25 -0700885 }
The Android Open Source Project28527d22009-03-03 19:31:44 -0800886 }
887
888 /**
Robert Greenwalt7fe44cb2010-08-27 09:24:29 -0700889 * @deprecated use requestRouteToHostAddress instead
890 *
The Android Open Source Project28527d22009-03-03 19:31:44 -0800891 * Ensure that a network route exists to deliver traffic to the specified
892 * host via the specified network interface.
Robert Greenwalt0659da32009-07-16 17:21:39 -0700893 * @param networkType the type of the network over which traffic to the
894 * specified host is to be routed
895 * @param hostAddress the IP address of the host to which the route is
896 * desired
The Android Open Source Project28527d22009-03-03 19:31:44 -0800897 * @return {@code true} on success, {@code false} on failure
898 */
899 public boolean requestRouteToHost(int networkType, int hostAddress) {
Robert Greenwalt7fe44cb2010-08-27 09:24:29 -0700900 InetAddress inetAddress = NetworkUtils.intToInetAddress(hostAddress);
901
902 if (inetAddress == null) {
903 return false;
904 }
905
906 return requestRouteToHostAddress(networkType, inetAddress.getAddress());
907 }
908
909 /**
910 * Ensure that a network route exists to deliver traffic to the specified
911 * host via the specified network interface.
912 * @param networkType the type of the network over which traffic to the
913 * specified host is to be routed
914 * @param hostAddress the IP address of the host to which the route is
915 * desired
916 * @return {@code true} on success, {@code false} on failure
917 */
918 public boolean requestRouteToHostAddress(int networkType, byte[] hostAddress) {
The Android Open Source Project28527d22009-03-03 19:31:44 -0800919 enforceChangePermission();
Robert Greenwalt6cac0742011-06-21 17:26:14 -0700920 if (mProtectedNetworks.contains(networkType)) {
921 enforceConnectivityInternalPermission();
922 }
923
The Android Open Source Project28527d22009-03-03 19:31:44 -0800924 if (!ConnectivityManager.isNetworkTypeValid(networkType)) {
925 return false;
926 }
927 NetworkStateTracker tracker = mNetTrackers[networkType];
Robert Greenwalt4666ed02009-09-10 15:06:20 -0700928
Robert Greenwaltec05b3c2009-10-30 14:17:42 -0700929 if (tracker == null || !tracker.getNetworkInfo().isConnected() ||
930 tracker.isTeardownRequested()) {
Robert Greenwalt4666ed02009-09-10 15:06:20 -0700931 if (DBG) {
Wink Savillee70c6f52010-12-03 12:01:38 -0800932 log("requestRouteToHostAddress on down network " +
Robert Greenwalt7fe44cb2010-08-27 09:24:29 -0700933 "(" + networkType + ") - dropped");
Robert Greenwalt4666ed02009-09-10 15:06:20 -0700934 }
935 return false;
The Android Open Source Project28527d22009-03-03 19:31:44 -0800936 }
Robert Greenwalta7dfbd32010-06-15 15:43:39 -0700937 try {
Robert Greenwalt7fe44cb2010-08-27 09:24:29 -0700938 InetAddress addr = InetAddress.getByAddress(hostAddress);
Robert Greenwaltbd492212011-05-06 17:10:53 -0700939 return addHostRoute(tracker, addr, 0);
Robert Greenwalta7dfbd32010-06-15 15:43:39 -0700940 } catch (UnknownHostException e) {}
941 return false;
Irfan Sheriff7f132d92010-06-09 15:39:36 -0700942 }
943
944 /**
945 * Ensure that a network route exists to deliver traffic to the specified
946 * host via the mobile data network.
947 * @param hostAddress the IP address of the host to which the route is desired,
948 * in network byte order.
Robert Greenwalta7dfbd32010-06-15 15:43:39 -0700949 * TODO - deprecate
Irfan Sheriff7f132d92010-06-09 15:39:36 -0700950 * @return {@code true} on success, {@code false} on failure
951 */
Robert Greenwaltbd492212011-05-06 17:10:53 -0700952 private boolean addHostRoute(NetworkStateTracker nt, InetAddress hostAddress, int cycleCount) {
Robert Greenwaltbd492212011-05-06 17:10:53 -0700953 LinkProperties lp = nt.getLinkProperties();
954 if ((lp == null) || (hostAddress == null)) return false;
Irfan Sheriff7f132d92010-06-09 15:39:36 -0700955
Robert Greenwaltbd492212011-05-06 17:10:53 -0700956 String interfaceName = lp.getInterfaceName();
Irfan Sheriff7f132d92010-06-09 15:39:36 -0700957 if (DBG) {
Robert Greenwaltbd492212011-05-06 17:10:53 -0700958 log("Requested host route to " + hostAddress + "(" + interfaceName + "), cycleCount=" +
959 cycleCount);
Irfan Sheriff7f132d92010-06-09 15:39:36 -0700960 }
Robert Greenwaltbd492212011-05-06 17:10:53 -0700961 if (interfaceName == null) {
Wink Savillee70c6f52010-12-03 12:01:38 -0800962 if (DBG) loge("addHostRoute failed due to null interface name");
Irfan Sheriff7f132d92010-06-09 15:39:36 -0700963 return false;
964 }
Robert Greenwaltbd492212011-05-06 17:10:53 -0700965
966 RouteInfo bestRoute = RouteInfo.selectBestRoute(lp.getRoutes(), hostAddress);
Robert Greenwalt355205c2011-05-10 15:05:02 -0700967 InetAddress gatewayAddress = null;
Robert Greenwaltbd492212011-05-06 17:10:53 -0700968 if (bestRoute != null) {
Robert Greenwalt355205c2011-05-10 15:05:02 -0700969 gatewayAddress = bestRoute.getGateway();
Robert Greenwaltbd492212011-05-06 17:10:53 -0700970 // if the best route is ourself, don't relf-reference, just add the host route
Robert Greenwalt355205c2011-05-10 15:05:02 -0700971 if (hostAddress.equals(gatewayAddress)) gatewayAddress = null;
Robert Greenwaltbd492212011-05-06 17:10:53 -0700972 }
Robert Greenwalt355205c2011-05-10 15:05:02 -0700973 if (gatewayAddress != null) {
Robert Greenwaltbd492212011-05-06 17:10:53 -0700974 if (cycleCount > MAX_HOSTROUTE_CYCLE_COUNT) {
975 loge("Error adding hostroute - too much recursion");
976 return false;
977 }
Robert Greenwalt355205c2011-05-10 15:05:02 -0700978 if (!addHostRoute(nt, gatewayAddress, cycleCount+1)) return false;
Robert Greenwaltbd492212011-05-06 17:10:53 -0700979 }
Robert Greenwalt355205c2011-05-10 15:05:02 -0700980
981 RouteInfo route = RouteInfo.makeHostRoute(hostAddress, gatewayAddress);
982
983 try {
984 mNetd.addRoute(interfaceName, route);
985 return true;
986 } catch (Exception ex) {
987 return false;
988 }
Robert Greenwaltbd492212011-05-06 17:10:53 -0700989 }
990
991 // TODO support the removal of single host routes. Keep a ref count of them so we
992 // aren't over-zealous
993 private boolean removeHostRoute(NetworkStateTracker nt, InetAddress hostAddress) {
994 return false;
The Android Open Source Project28527d22009-03-03 19:31:44 -0800995 }
996
997 /**
998 * @see ConnectivityManager#getBackgroundDataSetting()
999 */
1000 public boolean getBackgroundDataSetting() {
Robert Greenwaltd62c7002010-12-29 16:15:02 -08001001 return mBackgroundDataEnabled.get();
The Android Open Source Project28527d22009-03-03 19:31:44 -08001002 }
Robert Greenwalt0659da32009-07-16 17:21:39 -07001003
The Android Open Source Project28527d22009-03-03 19:31:44 -08001004 /**
1005 * @see ConnectivityManager#setBackgroundDataSetting(boolean)
1006 */
1007 public void setBackgroundDataSetting(boolean allowBackgroundDataUsage) {
1008 mContext.enforceCallingOrSelfPermission(
1009 android.Manifest.permission.CHANGE_BACKGROUND_DATA_SETTING,
1010 "ConnectivityService");
Robert Greenwalt0659da32009-07-16 17:21:39 -07001011
Robert Greenwaltd62c7002010-12-29 16:15:02 -08001012 mBackgroundDataEnabled.set(allowBackgroundDataUsage);
1013
Robert Greenwalt6a2db8a2010-09-23 10:05:56 -07001014 mHandler.sendMessage(mHandler.obtainMessage(EVENT_SET_BACKGROUND_DATA,
1015 (allowBackgroundDataUsage ? ENABLED : DISABLED), 0));
1016 }
The Android Open Source Project28527d22009-03-03 19:31:44 -08001017
Robert Greenwalt6a2db8a2010-09-23 10:05:56 -07001018 private void handleSetBackgroundData(boolean enabled) {
Robert Greenwalt0ffdef12011-02-25 13:44:09 -08001019 Settings.Secure.putInt(mContext.getContentResolver(),
1020 Settings.Secure.BACKGROUND_DATA, enabled ? 1 : 0);
1021 Intent broadcast = new Intent(
1022 ConnectivityManager.ACTION_BACKGROUND_DATA_SETTING_CHANGED);
1023 mContext.sendBroadcast(broadcast);
Robert Greenwalt0659da32009-07-16 17:21:39 -07001024 }
1025
Robert Greenwalt1b21f6c2010-02-23 18:58:05 -08001026 /**
1027 * @see ConnectivityManager#getMobileDataEnabled()
1028 */
1029 public boolean getMobileDataEnabled() {
Wink Savilleb9024c62010-12-07 10:31:02 -08001030 // TODO: This detail should probably be in DataConnectionTracker's
1031 // which is where we store the value and maybe make this
1032 // asynchronous.
Robert Greenwalt1b21f6c2010-02-23 18:58:05 -08001033 enforceAccessPermission();
1034 boolean retVal = Settings.Secure.getInt(mContext.getContentResolver(),
1035 Settings.Secure.MOBILE_DATA, 1) == 1;
Wink Savillee70c6f52010-12-03 12:01:38 -08001036 if (DBG) log("getMobileDataEnabled returning " + retVal);
Robert Greenwalt1b21f6c2010-02-23 18:58:05 -08001037 return retVal;
1038 }
1039
Robert Greenwalt34848c02011-03-25 13:09:25 -07001040 public void setDataDependency(int networkType, boolean met) {
Robert Greenwalt6cac0742011-06-21 17:26:14 -07001041 enforceConnectivityInternalPermission();
1042
Robert Greenwalt34848c02011-03-25 13:09:25 -07001043 if (DBG) {
1044 log("setDataDependency(" + networkType + ", " + met + ")");
1045 }
1046 mHandler.sendMessage(mHandler.obtainMessage(EVENT_SET_DEPENDENCY_MET,
1047 (met ? ENABLED : DISABLED), networkType));
1048 }
1049
1050 private void handleSetDependencyMet(int networkType, boolean met) {
1051 if (mNetTrackers[networkType] != null) {
1052 if (DBG) {
1053 log("handleSetDependencyMet(" + networkType + ", " + met + ")");
1054 }
1055 mNetTrackers[networkType].setDependencyMet(met);
1056 }
1057 }
1058
Robert Greenwalt1b21f6c2010-02-23 18:58:05 -08001059 /**
1060 * @see ConnectivityManager#setMobileDataEnabled(boolean)
1061 */
Robert Greenwalt6a2db8a2010-09-23 10:05:56 -07001062 public void setMobileDataEnabled(boolean enabled) {
Robert Greenwalt1b21f6c2010-02-23 18:58:05 -08001063 enforceChangePermission();
Wink Savillee70c6f52010-12-03 12:01:38 -08001064 if (DBG) log("setMobileDataEnabled(" + enabled + ")");
Robert Greenwalt1b21f6c2010-02-23 18:58:05 -08001065
Robert Greenwalt6a2db8a2010-09-23 10:05:56 -07001066 mHandler.sendMessage(mHandler.obtainMessage(EVENT_SET_MOBILE_DATA,
Robert Greenwalt34848c02011-03-25 13:09:25 -07001067 (enabled ? ENABLED : DISABLED), 0));
Robert Greenwalt6a2db8a2010-09-23 10:05:56 -07001068 }
1069
1070 private void handleSetMobileData(boolean enabled) {
Wink Savilleb9024c62010-12-07 10:31:02 -08001071 if (mNetTrackers[ConnectivityManager.TYPE_MOBILE] != null) {
1072 if (DBG) {
1073 Slog.d(TAG, mNetTrackers[ConnectivityManager.TYPE_MOBILE].toString() + enabled);
Robert Greenwalt1b21f6c2010-02-23 18:58:05 -08001074 }
Wink Savilleb9024c62010-12-07 10:31:02 -08001075 mNetTrackers[ConnectivityManager.TYPE_MOBILE].setDataEnable(enabled);
Robert Greenwalt1b21f6c2010-02-23 18:58:05 -08001076 }
1077 }
1078
The Android Open Source Project28527d22009-03-03 19:31:44 -08001079 private void enforceAccessPermission() {
Robert Greenwalt0659da32009-07-16 17:21:39 -07001080 mContext.enforceCallingOrSelfPermission(
1081 android.Manifest.permission.ACCESS_NETWORK_STATE,
1082 "ConnectivityService");
The Android Open Source Project28527d22009-03-03 19:31:44 -08001083 }
1084
1085 private void enforceChangePermission() {
Robert Greenwalt0659da32009-07-16 17:21:39 -07001086 mContext.enforceCallingOrSelfPermission(
1087 android.Manifest.permission.CHANGE_NETWORK_STATE,
1088 "ConnectivityService");
The Android Open Source Project28527d22009-03-03 19:31:44 -08001089 }
1090
Robert Greenwalt0c4828c2010-01-26 11:40:34 -08001091 // TODO Make this a special check when it goes public
1092 private void enforceTetherChangePermission() {
1093 mContext.enforceCallingOrSelfPermission(
1094 android.Manifest.permission.CHANGE_NETWORK_STATE,
1095 "ConnectivityService");
1096 }
1097
Robert Greenwalt8e87f122010-02-11 18:18:40 -08001098 private void enforceTetherAccessPermission() {
1099 mContext.enforceCallingOrSelfPermission(
1100 android.Manifest.permission.ACCESS_NETWORK_STATE,
1101 "ConnectivityService");
1102 }
1103
Robert Greenwalt93dc1042010-06-15 12:19:37 -07001104 private void enforceConnectivityInternalPermission() {
1105 mContext.enforceCallingOrSelfPermission(
1106 android.Manifest.permission.CONNECTIVITY_INTERNAL,
1107 "ConnectivityService");
1108 }
1109
The Android Open Source Project28527d22009-03-03 19:31:44 -08001110 /**
Robert Greenwalt0659da32009-07-16 17:21:39 -07001111 * Handle a {@code DISCONNECTED} event. If this pertains to the non-active
1112 * network, we ignore it. If it is for the active network, we send out a
1113 * broadcast. But first, we check whether it might be possible to connect
1114 * to a different network.
The Android Open Source Project28527d22009-03-03 19:31:44 -08001115 * @param info the {@code NetworkInfo} for the network
1116 */
1117 private void handleDisconnect(NetworkInfo info) {
1118
Robert Greenwalt2034b912009-08-12 16:08:25 -07001119 int prevNetType = info.getType();
The Android Open Source Project28527d22009-03-03 19:31:44 -08001120
Robert Greenwalt2034b912009-08-12 16:08:25 -07001121 mNetTrackers[prevNetType].setTeardownRequested(false);
The Android Open Source Project28527d22009-03-03 19:31:44 -08001122 /*
1123 * If the disconnected network is not the active one, then don't report
1124 * this as a loss of connectivity. What probably happened is that we're
1125 * getting the disconnect for a network that we explicitly disabled
1126 * in accordance with network preference policies.
1127 */
Robert Greenwalt34848c02011-03-25 13:09:25 -07001128 if (!mNetConfigs[prevNetType].isDefault()) {
Robert Greenwalt2034b912009-08-12 16:08:25 -07001129 List pids = mNetRequestersPids[prevNetType];
1130 for (int i = 0; i<pids.size(); i++) {
1131 Integer pid = (Integer)pids.get(i);
1132 // will remove them because the net's no longer connected
1133 // need to do this now as only now do we know the pids and
1134 // can properly null things that are no longer referenced.
1135 reassessPidDns(pid.intValue(), false);
The Android Open Source Project28527d22009-03-03 19:31:44 -08001136 }
The Android Open Source Project28527d22009-03-03 19:31:44 -08001137 }
1138
The Android Open Source Project28527d22009-03-03 19:31:44 -08001139 Intent intent = new Intent(ConnectivityManager.CONNECTIVITY_ACTION);
1140 intent.putExtra(ConnectivityManager.EXTRA_NETWORK_INFO, info);
1141 if (info.isFailover()) {
1142 intent.putExtra(ConnectivityManager.EXTRA_IS_FAILOVER, true);
1143 info.setFailover(false);
1144 }
1145 if (info.getReason() != null) {
1146 intent.putExtra(ConnectivityManager.EXTRA_REASON, info.getReason());
1147 }
1148 if (info.getExtraInfo() != null) {
Robert Greenwalt0659da32009-07-16 17:21:39 -07001149 intent.putExtra(ConnectivityManager.EXTRA_EXTRA_INFO,
1150 info.getExtraInfo());
The Android Open Source Project28527d22009-03-03 19:31:44 -08001151 }
Robert Greenwalt2034b912009-08-12 16:08:25 -07001152
Robert Greenwalt34848c02011-03-25 13:09:25 -07001153 if (mNetConfigs[prevNetType].isDefault()) {
Robert Greenwalt4b7c55e2011-01-11 13:56:33 -08001154 tryFailover(prevNetType);
1155 if (mActiveDefaultNetwork != -1) {
1156 NetworkInfo switchTo = mNetTrackers[mActiveDefaultNetwork].getNetworkInfo();
Robert Greenwalt3cc68d32010-01-25 17:54:29 -08001157 intent.putExtra(ConnectivityManager.EXTRA_OTHER_NETWORK_INFO, switchTo);
1158 } else {
Robert Greenwaltb2a9e492010-09-20 18:01:43 -07001159 mDefaultInetConditionPublished = 0; // we're not connected anymore
Robert Greenwalt3cc68d32010-01-25 17:54:29 -08001160 intent.putExtra(ConnectivityManager.EXTRA_NO_CONNECTIVITY, true);
1161 }
Robert Greenwaltf55ced92010-01-20 19:29:41 -08001162 }
Robert Greenwaltb2a9e492010-09-20 18:01:43 -07001163 intent.putExtra(ConnectivityManager.EXTRA_INET_CONDITION, mDefaultInetConditionPublished);
Robert Greenwalt36ea8692011-06-15 12:22:07 -07001164
1165 // Reset interface if no other connections are using the same interface
1166 boolean doReset = true;
1167 LinkProperties linkProperties = mNetTrackers[prevNetType].getLinkProperties();
1168 if (linkProperties != null) {
1169 String oldIface = linkProperties.getInterfaceName();
1170 if (TextUtils.isEmpty(oldIface) == false) {
1171 for (NetworkStateTracker networkStateTracker : mNetTrackers) {
1172 if (networkStateTracker == null) continue;
1173 NetworkInfo networkInfo = networkStateTracker.getNetworkInfo();
1174 if (networkInfo.isConnected() && networkInfo.getType() != prevNetType) {
1175 LinkProperties l = networkStateTracker.getLinkProperties();
1176 if (l == null) continue;
1177 if (oldIface.equals(l.getInterfaceName())) {
1178 doReset = false;
1179 break;
1180 }
1181 }
1182 }
1183 }
1184 }
1185
Robert Greenwaltf55ced92010-01-20 19:29:41 -08001186 // do this before we broadcast the change
Robert Greenwalt36ea8692011-06-15 12:22:07 -07001187 handleConnectivityChange(prevNetType, doReset);
Robert Greenwaltf55ced92010-01-20 19:29:41 -08001188
1189 sendStickyBroadcast(intent);
1190 /*
1191 * If the failover network is already connected, then immediately send
1192 * out a followup broadcast indicating successful failover
1193 */
Robert Greenwalt4b7c55e2011-01-11 13:56:33 -08001194 if (mActiveDefaultNetwork != -1) {
1195 sendConnectedBroadcast(mNetTrackers[mActiveDefaultNetwork].getNetworkInfo());
Robert Greenwaltf55ced92010-01-20 19:29:41 -08001196 }
1197 }
1198
Robert Greenwalt4b7c55e2011-01-11 13:56:33 -08001199 private void tryFailover(int prevNetType) {
Robert Greenwalt2034b912009-08-12 16:08:25 -07001200 /*
Robert Greenwalt92564852011-01-06 15:41:07 -08001201 * If this is a default network, check if other defaults are available.
1202 * Try to reconnect on all available and let them hash it out when
1203 * more than one connects.
Robert Greenwalt2034b912009-08-12 16:08:25 -07001204 */
Robert Greenwalt34848c02011-03-25 13:09:25 -07001205 if (mNetConfigs[prevNetType].isDefault()) {
Robert Greenwalt2034b912009-08-12 16:08:25 -07001206 if (mActiveDefaultNetwork == prevNetType) {
1207 mActiveDefaultNetwork = -1;
1208 }
1209
Robert Greenwalt4b7c55e2011-01-11 13:56:33 -08001210 // don't signal a reconnect for anything lower or equal priority than our
1211 // current connected default
1212 // TODO - don't filter by priority now - nice optimization but risky
1213// int currentPriority = -1;
1214// if (mActiveDefaultNetwork != -1) {
Robert Greenwalt34848c02011-03-25 13:09:25 -07001215// currentPriority = mNetConfigs[mActiveDefaultNetwork].mPriority;
Robert Greenwalt4b7c55e2011-01-11 13:56:33 -08001216// }
Robert Greenwaltf55ced92010-01-20 19:29:41 -08001217 for (int checkType=0; checkType <= ConnectivityManager.MAX_NETWORK_TYPE; checkType++) {
Robert Greenwaltec05b3c2009-10-30 14:17:42 -07001218 if (checkType == prevNetType) continue;
Robert Greenwalt34848c02011-03-25 13:09:25 -07001219 if (mNetConfigs[checkType] == null) continue;
1220 if (!mNetConfigs[checkType].isDefault()) continue;
Wink Saville72a95b92011-01-26 15:43:49 -08001221
1222// Enabling the isAvailable() optimization caused mobile to not get
1223// selected if it was in the middle of error handling. Specifically
1224// a moble connection that took 30 seconds to complete the DEACTIVATE_DATA_CALL
1225// would not be available and we wouldn't get connected to anything.
1226// So removing the isAvailable() optimization below for now. TODO: This
1227// optimization should work and we need to investigate why it doesn't work.
1228// This could be related to how DEACTIVATE_DATA_CALL is reporting its
1229// complete before it is really complete.
1230// if (!mNetTrackers[checkType].isAvailable()) continue;
1231
Robert Greenwalt34848c02011-03-25 13:09:25 -07001232// if (currentPriority >= mNetConfigs[checkType].mPriority) continue;
Robert Greenwalt2034b912009-08-12 16:08:25 -07001233
Robert Greenwalt4b7c55e2011-01-11 13:56:33 -08001234 NetworkStateTracker checkTracker = mNetTrackers[checkType];
1235 NetworkInfo checkInfo = checkTracker.getNetworkInfo();
1236 if (!checkInfo.isConnectedOrConnecting() || checkTracker.isTeardownRequested()) {
1237 checkInfo.setFailover(true);
1238 checkTracker.reconnect();
Robert Greenwalt2034b912009-08-12 16:08:25 -07001239 }
Robert Greenwalt4b7c55e2011-01-11 13:56:33 -08001240 if (DBG) log("Attempting to switch to " + checkInfo.getTypeName());
Robert Greenwalt2034b912009-08-12 16:08:25 -07001241 }
The Android Open Source Project28527d22009-03-03 19:31:44 -08001242 }
The Android Open Source Project28527d22009-03-03 19:31:44 -08001243 }
1244
1245 private void sendConnectedBroadcast(NetworkInfo info) {
Robert Greenwaltd3401f92010-09-15 17:36:33 -07001246 sendGeneralBroadcast(info, ConnectivityManager.CONNECTIVITY_ACTION);
1247 }
1248
1249 private void sendInetConditionBroadcast(NetworkInfo info) {
1250 sendGeneralBroadcast(info, ConnectivityManager.INET_CONDITION_ACTION);
1251 }
1252
1253 private void sendGeneralBroadcast(NetworkInfo info, String bcastType) {
1254 Intent intent = new Intent(bcastType);
The Android Open Source Project28527d22009-03-03 19:31:44 -08001255 intent.putExtra(ConnectivityManager.EXTRA_NETWORK_INFO, info);
1256 if (info.isFailover()) {
1257 intent.putExtra(ConnectivityManager.EXTRA_IS_FAILOVER, true);
1258 info.setFailover(false);
1259 }
1260 if (info.getReason() != null) {
1261 intent.putExtra(ConnectivityManager.EXTRA_REASON, info.getReason());
1262 }
1263 if (info.getExtraInfo() != null) {
Robert Greenwalt0659da32009-07-16 17:21:39 -07001264 intent.putExtra(ConnectivityManager.EXTRA_EXTRA_INFO,
1265 info.getExtraInfo());
The Android Open Source Project28527d22009-03-03 19:31:44 -08001266 }
Robert Greenwalt986c7412010-09-08 15:24:47 -07001267 intent.putExtra(ConnectivityManager.EXTRA_INET_CONDITION, mDefaultInetConditionPublished);
Mike Lockwoodfde2b762009-08-14 14:18:49 -04001268 sendStickyBroadcast(intent);
The Android Open Source Project28527d22009-03-03 19:31:44 -08001269 }
1270
1271 /**
1272 * Called when an attempt to fail over to another network has failed.
1273 * @param info the {@link NetworkInfo} for the failed network
1274 */
1275 private void handleConnectionFailure(NetworkInfo info) {
1276 mNetTrackers[info.getType()].setTeardownRequested(false);
The Android Open Source Project28527d22009-03-03 19:31:44 -08001277
Robert Greenwalt2034b912009-08-12 16:08:25 -07001278 String reason = info.getReason();
1279 String extraInfo = info.getExtraInfo();
Robert Greenwalt0659da32009-07-16 17:21:39 -07001280
Robert Greenwalte981bc52010-10-08 16:35:52 -07001281 String reasonText;
1282 if (reason == null) {
1283 reasonText = ".";
1284 } else {
1285 reasonText = " (" + reason + ").";
The Android Open Source Project28527d22009-03-03 19:31:44 -08001286 }
Wink Savillee70c6f52010-12-03 12:01:38 -08001287 loge("Attempt to connect to " + info.getTypeName() + " failed" + reasonText);
Robert Greenwalt2034b912009-08-12 16:08:25 -07001288
1289 Intent intent = new Intent(ConnectivityManager.CONNECTIVITY_ACTION);
1290 intent.putExtra(ConnectivityManager.EXTRA_NETWORK_INFO, info);
1291 if (getActiveNetworkInfo() == null) {
1292 intent.putExtra(ConnectivityManager.EXTRA_NO_CONNECTIVITY, true);
1293 }
1294 if (reason != null) {
1295 intent.putExtra(ConnectivityManager.EXTRA_REASON, reason);
1296 }
1297 if (extraInfo != null) {
1298 intent.putExtra(ConnectivityManager.EXTRA_EXTRA_INFO, extraInfo);
1299 }
1300 if (info.isFailover()) {
1301 intent.putExtra(ConnectivityManager.EXTRA_IS_FAILOVER, true);
1302 info.setFailover(false);
1303 }
Robert Greenwaltf55ced92010-01-20 19:29:41 -08001304
Robert Greenwalt34848c02011-03-25 13:09:25 -07001305 if (mNetConfigs[info.getType()].isDefault()) {
Robert Greenwalt4b7c55e2011-01-11 13:56:33 -08001306 tryFailover(info.getType());
1307 if (mActiveDefaultNetwork != -1) {
1308 NetworkInfo switchTo = mNetTrackers[mActiveDefaultNetwork].getNetworkInfo();
Robert Greenwalt3cc68d32010-01-25 17:54:29 -08001309 intent.putExtra(ConnectivityManager.EXTRA_OTHER_NETWORK_INFO, switchTo);
1310 } else {
Robert Greenwaltb2a9e492010-09-20 18:01:43 -07001311 mDefaultInetConditionPublished = 0;
Robert Greenwalt3cc68d32010-01-25 17:54:29 -08001312 intent.putExtra(ConnectivityManager.EXTRA_NO_CONNECTIVITY, true);
1313 }
Robert Greenwaltf55ced92010-01-20 19:29:41 -08001314 }
Robert Greenwalt3cc68d32010-01-25 17:54:29 -08001315
Robert Greenwaltb2a9e492010-09-20 18:01:43 -07001316 intent.putExtra(ConnectivityManager.EXTRA_INET_CONDITION, mDefaultInetConditionPublished);
Mike Lockwoodfde2b762009-08-14 14:18:49 -04001317 sendStickyBroadcast(intent);
Robert Greenwaltf55ced92010-01-20 19:29:41 -08001318 /*
1319 * If the failover network is already connected, then immediately send
1320 * out a followup broadcast indicating successful failover
1321 */
Robert Greenwalt4b7c55e2011-01-11 13:56:33 -08001322 if (mActiveDefaultNetwork != -1) {
1323 sendConnectedBroadcast(mNetTrackers[mActiveDefaultNetwork].getNetworkInfo());
Robert Greenwaltf55ced92010-01-20 19:29:41 -08001324 }
Mike Lockwoodfde2b762009-08-14 14:18:49 -04001325 }
1326
1327 private void sendStickyBroadcast(Intent intent) {
1328 synchronized(this) {
Dianne Hackborna417ff82009-12-08 19:45:14 -08001329 if (!mSystemReady) {
1330 mInitialBroadcast = new Intent(intent);
Mike Lockwoodfde2b762009-08-14 14:18:49 -04001331 }
Dianne Hackborna417ff82009-12-08 19:45:14 -08001332 intent.addFlags(Intent.FLAG_RECEIVER_REGISTERED_ONLY_BEFORE_BOOT);
1333 mContext.sendStickyBroadcast(intent);
Mike Lockwoodfde2b762009-08-14 14:18:49 -04001334 }
1335 }
1336
1337 void systemReady() {
Robert Greenwalt355205c2011-05-10 15:05:02 -07001338 IBinder b = ServiceManager.getService(Context.NETWORKMANAGEMENT_SERVICE);
1339 mNetd = INetworkManagementService.Stub.asInterface(b);
1340
Mike Lockwoodfde2b762009-08-14 14:18:49 -04001341 synchronized(this) {
1342 mSystemReady = true;
Dianne Hackborna417ff82009-12-08 19:45:14 -08001343 if (mInitialBroadcast != null) {
1344 mContext.sendStickyBroadcast(mInitialBroadcast);
1345 mInitialBroadcast = null;
Mike Lockwoodfde2b762009-08-14 14:18:49 -04001346 }
1347 }
Robert Greenwaltc3c5f862010-10-11 16:00:27 -07001348 // load the global proxy at startup
1349 mHandler.sendMessage(mHandler.obtainMessage(EVENT_APPLY_GLOBAL_HTTP_PROXY));
The Android Open Source Project28527d22009-03-03 19:31:44 -08001350 }
1351
1352 private void handleConnect(NetworkInfo info) {
Robert Greenwalt2034b912009-08-12 16:08:25 -07001353 int type = info.getType();
The Android Open Source Project28527d22009-03-03 19:31:44 -08001354
1355 // snapshot isFailover, because sendConnectedBroadcast() resets it
1356 boolean isFailover = info.isFailover();
Robert Greenwalt2034b912009-08-12 16:08:25 -07001357 NetworkStateTracker thisNet = mNetTrackers[type];
The Android Open Source Project28527d22009-03-03 19:31:44 -08001358
Robert Greenwalt2034b912009-08-12 16:08:25 -07001359 // if this is a default net and other default is running
1360 // kill the one not preferred
Robert Greenwalt34848c02011-03-25 13:09:25 -07001361 if (mNetConfigs[type].isDefault()) {
Robert Greenwalt2034b912009-08-12 16:08:25 -07001362 if (mActiveDefaultNetwork != -1 && mActiveDefaultNetwork != type) {
1363 if ((type != mNetworkPreference &&
Wink Savillef2a62832011-04-07 14:23:45 -07001364 mNetConfigs[mActiveDefaultNetwork].priority >
1365 mNetConfigs[type].priority) ||
Robert Greenwalt2034b912009-08-12 16:08:25 -07001366 mNetworkPreference == mActiveDefaultNetwork) {
1367 // don't accept this one
Wink Savillee70c6f52010-12-03 12:01:38 -08001368 if (DBG) {
1369 log("Not broadcasting CONNECT_ACTION " +
Robert Greenwalt2034b912009-08-12 16:08:25 -07001370 "to torn down network " + info.getTypeName());
Wink Savillee70c6f52010-12-03 12:01:38 -08001371 }
Robert Greenwalt2034b912009-08-12 16:08:25 -07001372 teardown(thisNet);
1373 return;
1374 } else {
1375 // tear down the other
1376 NetworkStateTracker otherNet =
1377 mNetTrackers[mActiveDefaultNetwork];
Wink Savillee70c6f52010-12-03 12:01:38 -08001378 if (DBG) {
1379 log("Policy requires " + otherNet.getNetworkInfo().getTypeName() +
Robert Greenwalt2034b912009-08-12 16:08:25 -07001380 " teardown");
Wink Savillee70c6f52010-12-03 12:01:38 -08001381 }
Robert Greenwalt2034b912009-08-12 16:08:25 -07001382 if (!teardown(otherNet)) {
Wink Savillee70c6f52010-12-03 12:01:38 -08001383 loge("Network declined teardown request");
Robert Greenwalt99910172011-03-29 11:36:28 -07001384 teardown(thisNet);
Robert Greenwalt2034b912009-08-12 16:08:25 -07001385 return;
1386 }
Robert Greenwalt93dc1042010-06-15 12:19:37 -07001387 }
1388 }
1389 synchronized (ConnectivityService.this) {
1390 // have a new default network, release the transition wakelock in a second
1391 // if it's held. The second pause is to allow apps to reconnect over the
1392 // new network
1393 if (mNetTransitionWakeLock.isHeld()) {
1394 mHandler.sendMessageDelayed(mHandler.obtainMessage(
Robert Greenwaltccb36f92010-09-24 14:32:21 -07001395 EVENT_CLEAR_NET_TRANSITION_WAKELOCK,
Robert Greenwalt93dc1042010-06-15 12:19:37 -07001396 mNetTransitionWakeLockSerialNumber, 0),
1397 1000);
Robert Greenwalt2034b912009-08-12 16:08:25 -07001398 }
1399 }
1400 mActiveDefaultNetwork = type;
Robert Greenwalt986c7412010-09-08 15:24:47 -07001401 // this will cause us to come up initially as unconnected and switching
1402 // to connected after our normal pause unless somebody reports us as reall
1403 // disconnected
1404 mDefaultInetConditionPublished = 0;
1405 mDefaultConnectionSequence++;
1406 mInetConditionChangeInFlight = false;
1407 // Don't do this - if we never sign in stay, grey
1408 //reportNetworkCondition(mActiveDefaultNetwork, 100);
Robert Greenwalt2034b912009-08-12 16:08:25 -07001409 }
The Android Open Source Project28527d22009-03-03 19:31:44 -08001410 thisNet.setTeardownRequested(false);
Irfan Sheriff7f132d92010-06-09 15:39:36 -07001411 updateNetworkSettings(thisNet);
Robert Greenwalt36ea8692011-06-15 12:22:07 -07001412 handleConnectivityChange(type, false);
Robert Greenwalt2034b912009-08-12 16:08:25 -07001413 sendConnectedBroadcast(info);
The Android Open Source Project28527d22009-03-03 19:31:44 -08001414 }
1415
The Android Open Source Project28527d22009-03-03 19:31:44 -08001416 /**
Robert Greenwalt3afbead2010-07-23 15:46:26 -07001417 * After a change in the connectivity state of a network. We're mainly
1418 * concerned with making sure that the list of DNS servers is set up
1419 * according to which networks are connected, and ensuring that the
1420 * right routing table entries exist.
The Android Open Source Project28527d22009-03-03 19:31:44 -08001421 */
Robert Greenwalt36ea8692011-06-15 12:22:07 -07001422 private void handleConnectivityChange(int netType, boolean doReset) {
Wink Saville051a6642011-07-13 13:44:13 -07001423 int resetMask = doReset ? NetworkUtils.RESET_ALL_ADDRESSES : 0;
1424
The Android Open Source Project28527d22009-03-03 19:31:44 -08001425 /*
Robert Greenwalt2034b912009-08-12 16:08:25 -07001426 * If a non-default network is enabled, add the host routes that
Robert Greenwalt3afbead2010-07-23 15:46:26 -07001427 * will allow it's DNS servers to be accessed.
The Android Open Source Project28527d22009-03-03 19:31:44 -08001428 */
Robert Greenwalt3afbead2010-07-23 15:46:26 -07001429 handleDnsConfigurationChange(netType);
Robert Greenwalt2034b912009-08-12 16:08:25 -07001430
Robert Greenwalt3afbead2010-07-23 15:46:26 -07001431 if (mNetTrackers[netType].getNetworkInfo().isConnected()) {
Wink Saville051a6642011-07-13 13:44:13 -07001432 LinkProperties newLp = mNetTrackers[netType].getLinkProperties();
1433 LinkProperties curLp = mCurrentLinkProperties[netType];
1434 mCurrentLinkProperties[netType] = newLp;
1435 if (VDBG) {
1436 log("handleConnectivityChange: changed linkProperty[" + netType + "]:" +
1437 " doReset=" + doReset + " resetMask=" + resetMask +
1438 "\n curLp=" + curLp +
1439 "\n newLp=" + newLp);
1440 }
1441
1442 if (curLp.isIdenticalInterfaceName(newLp)) {
1443 CompareAddressesResult car = curLp.compareAddresses(newLp);
1444 if ((car.removed.size() != 0) || (car.added.size() != 0)) {
1445 for (LinkAddress linkAddr : car.removed) {
1446 if (linkAddr.getAddress() instanceof Inet4Address) {
1447 resetMask |= NetworkUtils.RESET_IPV4_ADDRESSES;
1448 }
1449 if (linkAddr.getAddress() instanceof Inet6Address) {
1450 resetMask |= NetworkUtils.RESET_IPV6_ADDRESSES;
1451 }
1452 }
1453 if (DBG) {
1454 log("handleConnectivityChange: addresses changed" +
1455 " linkProperty[" + netType + "]:" + " resetMask=" + resetMask +
1456 "\n car=" + car);
1457 }
1458 } else {
1459 if (DBG) {
1460 log("handleConnectivityChange: address are the same reset per doReset" +
1461 " linkProperty[" + netType + "]:" +
1462 " resetMask=" + resetMask);
1463 }
1464 }
1465 } else {
1466 resetMask = NetworkUtils.RESET_ALL_ADDRESSES;
1467 log("handleConnectivityChange: interface not not equivalent reset both" +
1468 " linkProperty[" + netType + "]:" +
1469 " resetMask=" + resetMask);
1470 }
Robert Greenwalt34848c02011-03-25 13:09:25 -07001471 if (mNetConfigs[netType].isDefault()) {
Robert Greenwaltc3c5f862010-10-11 16:00:27 -07001472 handleApplyDefaultProxy(netType);
Robert Greenwalt3afbead2010-07-23 15:46:26 -07001473 addDefaultRoute(mNetTrackers[netType]);
Robert Greenwalt2034b912009-08-12 16:08:25 -07001474 } else {
Robert Greenwalta71471b2011-03-24 21:41:41 -07001475 // many radios add a default route even when we don't want one.
1476 // remove the default interface unless we need it for our active network
1477 if (mActiveDefaultNetwork != -1) {
Robert Greenwalt3a378b92011-03-30 15:03:13 -07001478 LinkProperties linkProperties =
1479 mNetTrackers[mActiveDefaultNetwork].getLinkProperties();
1480 LinkProperties newLinkProperties =
1481 mNetTrackers[netType].getLinkProperties();
1482 String defaultIface = linkProperties.getInterfaceName();
Robert Greenwalta71471b2011-03-24 21:41:41 -07001483 if (defaultIface != null &&
Robert Greenwalt3a378b92011-03-30 15:03:13 -07001484 !defaultIface.equals(newLinkProperties.getInterfaceName())) {
Robert Greenwaltf7dae302011-03-31 10:44:47 -07001485 removeDefaultRoute(mNetTrackers[netType]);
Robert Greenwalta71471b2011-03-24 21:41:41 -07001486 }
1487 }
Robert Greenwaltf7dae302011-03-31 10:44:47 -07001488 addPrivateDnsRoutes(mNetTrackers[netType]);
Robert Greenwalt3afbead2010-07-23 15:46:26 -07001489 }
1490 } else {
Robert Greenwalt34848c02011-03-25 13:09:25 -07001491 if (mNetConfigs[netType].isDefault()) {
Robert Greenwalt3afbead2010-07-23 15:46:26 -07001492 removeDefaultRoute(mNetTrackers[netType]);
1493 } else {
1494 removePrivateDnsRoutes(mNetTrackers[netType]);
Robert Greenwalt2034b912009-08-12 16:08:25 -07001495 }
The Android Open Source Project28527d22009-03-03 19:31:44 -08001496 }
Robert Greenwalt36ea8692011-06-15 12:22:07 -07001497
Wink Saville051a6642011-07-13 13:44:13 -07001498 if (doReset || resetMask != 0) {
Robert Greenwalt36ea8692011-06-15 12:22:07 -07001499 LinkProperties linkProperties = mNetTrackers[netType].getLinkProperties();
1500 if (linkProperties != null) {
1501 String iface = linkProperties.getInterfaceName();
1502 if (TextUtils.isEmpty(iface) == false) {
Wink Saville051a6642011-07-13 13:44:13 -07001503 if (DBG) log("resetConnections(" + iface + ", " + resetMask + ")");
1504 NetworkUtils.resetConnections(iface, resetMask);
Robert Greenwalt36ea8692011-06-15 12:22:07 -07001505 }
1506 }
1507 }
Kazuhiro Ondo07680062011-06-22 21:10:34 -05001508
1509 // TODO: Temporary notifying upstread change to Tethering.
1510 // @see bug/4455071
1511 /** Notify TetheringService if interface name has been changed. */
1512 if (TextUtils.equals(mNetTrackers[netType].getNetworkInfo().getReason(),
1513 Phone.REASON_LINK_PROPERTIES_CHANGED)) {
1514 if (isTetheringSupported()) {
1515 mTethering.handleTetherIfaceChange();
1516 }
1517 }
The Android Open Source Project28527d22009-03-03 19:31:44 -08001518 }
1519
Irfan Sheriff7f132d92010-06-09 15:39:36 -07001520 private void addPrivateDnsRoutes(NetworkStateTracker nt) {
Irfan Sheriff7f132d92010-06-09 15:39:36 -07001521 boolean privateDnsRouteSet = nt.isPrivateDnsRouteSet();
Robert Greenwalt1f1bcfe2010-08-30 10:56:47 -07001522 LinkProperties p = nt.getLinkProperties();
Robert Greenwalta7dfbd32010-06-15 15:43:39 -07001523 if (p == null) return;
1524 String interfaceName = p.getInterfaceName();
Irfan Sheriff7f132d92010-06-09 15:39:36 -07001525
1526 if (DBG) {
Wink Savillee70c6f52010-12-03 12:01:38 -08001527 log("addPrivateDnsRoutes for " + nt +
Irfan Sheriff7f132d92010-06-09 15:39:36 -07001528 "(" + interfaceName + ") - mPrivateDnsRouteSet = " + privateDnsRouteSet);
1529 }
Irfan Sheriff7f132d92010-06-09 15:39:36 -07001530 if (interfaceName != null && !privateDnsRouteSet) {
Robert Greenwalta7dfbd32010-06-15 15:43:39 -07001531 Collection<InetAddress> dnsList = p.getDnses();
1532 for (InetAddress dns : dnsList) {
Robert Greenwaltbd492212011-05-06 17:10:53 -07001533 addHostRoute(nt, dns, 0);
Irfan Sheriff7f132d92010-06-09 15:39:36 -07001534 }
1535 nt.privateDnsRouteSet(true);
1536 }
1537 }
1538
1539 private void removePrivateDnsRoutes(NetworkStateTracker nt) {
Robert Greenwalt1f1bcfe2010-08-30 10:56:47 -07001540 LinkProperties p = nt.getLinkProperties();
Robert Greenwalta7dfbd32010-06-15 15:43:39 -07001541 if (p == null) return;
1542 String interfaceName = p.getInterfaceName();
Irfan Sheriff7f132d92010-06-09 15:39:36 -07001543 boolean privateDnsRouteSet = nt.isPrivateDnsRouteSet();
1544 if (interfaceName != null && privateDnsRouteSet) {
1545 if (DBG) {
Wink Savillee70c6f52010-12-03 12:01:38 -08001546 log("removePrivateDnsRoutes for " + nt.getNetworkInfo().getTypeName() +
Irfan Sheriff7f132d92010-06-09 15:39:36 -07001547 " (" + interfaceName + ")");
1548 }
Robert Greenwalt355205c2011-05-10 15:05:02 -07001549
1550 Collection<InetAddress> dnsList = p.getDnses();
1551 for (InetAddress dns : dnsList) {
1552 if (DBG) log(" removing " + dns);
1553 RouteInfo route = RouteInfo.makeHostRoute(dns);
1554 try {
1555 mNetd.removeRoute(interfaceName, route);
1556 } catch (Exception ex) {
1557 loge("error (" + ex + ") removing dns route " + route);
1558 }
1559 }
Irfan Sheriff7f132d92010-06-09 15:39:36 -07001560 nt.privateDnsRouteSet(false);
1561 }
1562 }
1563
Irfan Sheriff7f132d92010-06-09 15:39:36 -07001564
1565 private void addDefaultRoute(NetworkStateTracker nt) {
Robert Greenwalt1f1bcfe2010-08-30 10:56:47 -07001566 LinkProperties p = nt.getLinkProperties();
Robert Greenwalta7dfbd32010-06-15 15:43:39 -07001567 if (p == null) return;
1568 String interfaceName = p.getInterfaceName();
Robert Greenwalt5c733972011-02-09 13:56:06 -08001569 if (TextUtils.isEmpty(interfaceName)) return;
Irfan Sheriff7f132d92010-06-09 15:39:36 -07001570
Robert Greenwalt355205c2011-05-10 15:05:02 -07001571 for (RouteInfo route : p.getRoutes()) {
Robert Greenwalt5a901292011-04-28 14:28:50 -07001572 //TODO - handle non-default routes
1573 if (route.isDefaultRoute()) {
Robert Greenwalt355205c2011-05-10 15:05:02 -07001574 if (DBG) log("adding default route " + route);
Robert Greenwalt5a901292011-04-28 14:28:50 -07001575 InetAddress gateway = route.getGateway();
Robert Greenwalt355205c2011-05-10 15:05:02 -07001576 if (addHostRoute(nt, gateway, 0)) {
1577 try {
1578 mNetd.addRoute(interfaceName, route);
1579 } catch (Exception e) {
1580 loge("error adding default route " + route);
1581 continue;
1582 }
Robert Greenwalt5a901292011-04-28 14:28:50 -07001583 if (DBG) {
1584 NetworkInfo networkInfo = nt.getNetworkInfo();
1585 log("addDefaultRoute for " + networkInfo.getTypeName() +
1586 " (" + interfaceName + "), GatewayAddr=" +
1587 gateway.getHostAddress());
1588 }
Robert Greenwalt355205c2011-05-10 15:05:02 -07001589 } else {
1590 loge("error adding host route for default route " + route);
Robert Greenwalt03d53da2011-03-22 18:47:42 -07001591 }
Irfan Sheriff7f132d92010-06-09 15:39:36 -07001592 }
Irfan Sheriff7f132d92010-06-09 15:39:36 -07001593 }
1594 }
1595
1596
1597 public void removeDefaultRoute(NetworkStateTracker nt) {
Robert Greenwalt1f1bcfe2010-08-30 10:56:47 -07001598 LinkProperties p = nt.getLinkProperties();
Robert Greenwalta7dfbd32010-06-15 15:43:39 -07001599 if (p == null) return;
1600 String interfaceName = p.getInterfaceName();
Irfan Sheriff7f132d92010-06-09 15:39:36 -07001601
Robert Greenwalt355205c2011-05-10 15:05:02 -07001602 if (interfaceName == null) return;
1603
1604 for (RouteInfo route : p.getRoutes()) {
1605 //TODO - handle non-default routes
1606 if (route.isDefaultRoute()) {
1607 try {
1608 mNetd.removeRoute(interfaceName, route);
1609 } catch (Exception ex) {
1610 loge("error (" + ex + ") removing default route " + route);
1611 continue;
1612 }
Robert Greenwalt03d53da2011-03-22 18:47:42 -07001613 if (DBG) {
1614 NetworkInfo networkInfo = nt.getNetworkInfo();
1615 log("removeDefaultRoute for " + networkInfo.getTypeName() + " (" +
1616 interfaceName + ")");
1617 }
Irfan Sheriff7f132d92010-06-09 15:39:36 -07001618 }
Irfan Sheriff7f132d92010-06-09 15:39:36 -07001619 }
1620 }
1621
1622 /**
1623 * Reads the network specific TCP buffer sizes from SystemProperties
1624 * net.tcp.buffersize.[default|wifi|umts|edge|gprs] and set them for system
1625 * wide use
1626 */
1627 public void updateNetworkSettings(NetworkStateTracker nt) {
1628 String key = nt.getTcpBufferSizesPropName();
1629 String bufferSizes = SystemProperties.get(key);
1630
1631 if (bufferSizes.length() == 0) {
Wink Savillee70c6f52010-12-03 12:01:38 -08001632 loge(key + " not found in system properties. Using defaults");
Irfan Sheriff7f132d92010-06-09 15:39:36 -07001633
1634 // Setting to default values so we won't be stuck to previous values
1635 key = "net.tcp.buffersize.default";
1636 bufferSizes = SystemProperties.get(key);
1637 }
1638
1639 // Set values in kernel
1640 if (bufferSizes.length() != 0) {
1641 if (DBG) {
Wink Savillee70c6f52010-12-03 12:01:38 -08001642 log("Setting TCP values: [" + bufferSizes
Irfan Sheriff7f132d92010-06-09 15:39:36 -07001643 + "] which comes from [" + key + "]");
1644 }
1645 setBufferSize(bufferSizes);
1646 }
1647 }
1648
1649 /**
1650 * Writes TCP buffer sizes to /sys/kernel/ipv4/tcp_[r/w]mem_[min/def/max]
1651 * which maps to /proc/sys/net/ipv4/tcp_rmem and tcpwmem
1652 *
1653 * @param bufferSizes in the format of "readMin, readInitial, readMax,
1654 * writeMin, writeInitial, writeMax"
1655 */
1656 private void setBufferSize(String bufferSizes) {
1657 try {
1658 String[] values = bufferSizes.split(",");
1659
1660 if (values.length == 6) {
1661 final String prefix = "/sys/kernel/ipv4/tcp_";
1662 stringToFile(prefix + "rmem_min", values[0]);
1663 stringToFile(prefix + "rmem_def", values[1]);
1664 stringToFile(prefix + "rmem_max", values[2]);
1665 stringToFile(prefix + "wmem_min", values[3]);
1666 stringToFile(prefix + "wmem_def", values[4]);
1667 stringToFile(prefix + "wmem_max", values[5]);
1668 } else {
Wink Savillee70c6f52010-12-03 12:01:38 -08001669 loge("Invalid buffersize string: " + bufferSizes);
Irfan Sheriff7f132d92010-06-09 15:39:36 -07001670 }
1671 } catch (IOException e) {
Wink Savillee70c6f52010-12-03 12:01:38 -08001672 loge("Can't set tcp buffer sizes:" + e);
Irfan Sheriff7f132d92010-06-09 15:39:36 -07001673 }
1674 }
1675
1676 /**
1677 * Writes string to file. Basically same as "echo -n $string > $filename"
1678 *
1679 * @param filename
1680 * @param string
1681 * @throws IOException
1682 */
1683 private void stringToFile(String filename, String string) throws IOException {
1684 FileWriter out = new FileWriter(filename);
1685 try {
1686 out.write(string);
1687 } finally {
1688 out.close();
1689 }
1690 }
1691
1692
Robert Greenwalt2034b912009-08-12 16:08:25 -07001693 /**
1694 * Adjust the per-process dns entries (net.dns<x>.<pid>) based
1695 * on the highest priority active net which this process requested.
1696 * If there aren't any, clear it out
1697 */
1698 private void reassessPidDns(int myPid, boolean doBump)
1699 {
Wink Savillee70c6f52010-12-03 12:01:38 -08001700 if (DBG) log("reassessPidDns for pid " + myPid);
Robert Greenwalt2034b912009-08-12 16:08:25 -07001701 for(int i : mPriorityList) {
Robert Greenwalt34848c02011-03-25 13:09:25 -07001702 if (mNetConfigs[i].isDefault()) {
Robert Greenwalt2034b912009-08-12 16:08:25 -07001703 continue;
1704 }
1705 NetworkStateTracker nt = mNetTrackers[i];
Robert Greenwalt0659da32009-07-16 17:21:39 -07001706 if (nt.getNetworkInfo().isConnected() &&
1707 !nt.isTeardownRequested()) {
Robert Greenwalt1f1bcfe2010-08-30 10:56:47 -07001708 LinkProperties p = nt.getLinkProperties();
Robert Greenwalta7dfbd32010-06-15 15:43:39 -07001709 if (p == null) continue;
Robert Greenwalt2034b912009-08-12 16:08:25 -07001710 List pids = mNetRequestersPids[i];
1711 for (int j=0; j<pids.size(); j++) {
1712 Integer pid = (Integer)pids.get(j);
1713 if (pid.intValue() == myPid) {
Robert Greenwalta7dfbd32010-06-15 15:43:39 -07001714 Collection<InetAddress> dnses = p.getDnses();
1715 writePidDns(dnses, myPid);
Robert Greenwalt2034b912009-08-12 16:08:25 -07001716 if (doBump) {
1717 bumpDns();
1718 }
1719 return;
1720 }
1721 }
1722 }
1723 }
1724 // nothing found - delete
1725 for (int i = 1; ; i++) {
1726 String prop = "net.dns" + i + "." + myPid;
1727 if (SystemProperties.get(prop).length() == 0) {
1728 if (doBump) {
1729 bumpDns();
1730 }
1731 return;
1732 }
1733 SystemProperties.set(prop, "");
1734 }
1735 }
1736
Robert Greenwalt8ca88762010-12-17 15:20:36 -08001737 // return true if results in a change
1738 private boolean writePidDns(Collection <InetAddress> dnses, int pid) {
Robert Greenwalt2034b912009-08-12 16:08:25 -07001739 int j = 1;
Robert Greenwalt8ca88762010-12-17 15:20:36 -08001740 boolean changed = false;
Robert Greenwalta7dfbd32010-06-15 15:43:39 -07001741 for (InetAddress dns : dnses) {
Robert Greenwalt8ca88762010-12-17 15:20:36 -08001742 String dnsString = dns.getHostAddress();
1743 if (changed || !dnsString.equals(SystemProperties.get("net.dns" + j + "." + pid))) {
1744 changed = true;
1745 SystemProperties.set("net.dns" + j++ + "." + pid, dns.getHostAddress());
1746 }
Robert Greenwalt2034b912009-08-12 16:08:25 -07001747 }
Robert Greenwalt8ca88762010-12-17 15:20:36 -08001748 return changed;
Robert Greenwalt2034b912009-08-12 16:08:25 -07001749 }
1750
1751 private void bumpDns() {
1752 /*
1753 * Bump the property that tells the name resolver library to reread
1754 * the DNS server list from the properties.
1755 */
1756 String propVal = SystemProperties.get("net.dnschange");
1757 int n = 0;
1758 if (propVal.length() != 0) {
1759 try {
1760 n = Integer.parseInt(propVal);
1761 } catch (NumberFormatException e) {}
1762 }
1763 SystemProperties.set("net.dnschange", "" + (n+1));
Robert Greenwalt051642b2010-11-02 14:08:23 -07001764 /*
1765 * Tell the VMs to toss their DNS caches
1766 */
1767 Intent intent = new Intent(Intent.ACTION_CLEAR_DNS_CACHE);
1768 intent.addFlags(Intent.FLAG_RECEIVER_REPLACE_PENDING);
Stan Chesnuttf444f502011-01-05 17:14:03 -08001769 /*
1770 * Connectivity events can happen before boot has completed ...
1771 */
1772 intent.addFlags(Intent.FLAG_RECEIVER_REGISTERED_ONLY_BEFORE_BOOT);
Robert Greenwalt051642b2010-11-02 14:08:23 -07001773 mContext.sendBroadcast(intent);
Robert Greenwalt2034b912009-08-12 16:08:25 -07001774 }
1775
Robert Greenwalt3afbead2010-07-23 15:46:26 -07001776 private void handleDnsConfigurationChange(int netType) {
Robert Greenwalt2034b912009-08-12 16:08:25 -07001777 // add default net's dns entries
Robert Greenwalt3afbead2010-07-23 15:46:26 -07001778 NetworkStateTracker nt = mNetTrackers[netType];
1779 if (nt != null && nt.getNetworkInfo().isConnected() && !nt.isTeardownRequested()) {
Robert Greenwalt1f1bcfe2010-08-30 10:56:47 -07001780 LinkProperties p = nt.getLinkProperties();
Robert Greenwalt3afbead2010-07-23 15:46:26 -07001781 if (p == null) return;
1782 Collection<InetAddress> dnses = p.getDnses();
Robert Greenwalt8ca88762010-12-17 15:20:36 -08001783 boolean changed = false;
Robert Greenwalt34848c02011-03-25 13:09:25 -07001784 if (mNetConfigs[netType].isDefault()) {
Robert Greenwalt3afbead2010-07-23 15:46:26 -07001785 int j = 1;
Robert Greenwalt94daa182010-09-01 11:34:05 -07001786 if (dnses.size() == 0 && mDefaultDns != null) {
Robert Greenwalt8ca88762010-12-17 15:20:36 -08001787 String dnsString = mDefaultDns.getHostAddress();
1788 if (!dnsString.equals(SystemProperties.get("net.dns1"))) {
1789 if (DBG) {
1790 log("no dns provided - using " + dnsString);
1791 }
1792 changed = true;
1793 SystemProperties.set("net.dns1", dnsString);
Robert Greenwalt2034b912009-08-12 16:08:25 -07001794 }
Robert Greenwalt94daa182010-09-01 11:34:05 -07001795 j++;
1796 } else {
1797 for (InetAddress dns : dnses) {
Robert Greenwalt8ca88762010-12-17 15:20:36 -08001798 String dnsString = dns.getHostAddress();
1799 if (!changed && dnsString.equals(SystemProperties.get("net.dns" + j))) {
1800 j++;
1801 continue;
1802 }
Robert Greenwalt94daa182010-09-01 11:34:05 -07001803 if (DBG) {
Wink Savillee70c6f52010-12-03 12:01:38 -08001804 log("adding dns " + dns + " for " +
Robert Greenwalt94daa182010-09-01 11:34:05 -07001805 nt.getNetworkInfo().getTypeName());
1806 }
Robert Greenwalt8ca88762010-12-17 15:20:36 -08001807 changed = true;
1808 SystemProperties.set("net.dns" + j++, dnsString);
Robert Greenwalt94daa182010-09-01 11:34:05 -07001809 }
Robert Greenwalt3afbead2010-07-23 15:46:26 -07001810 }
1811 for (int k=j ; k<mNumDnsEntries; k++) {
Robert Greenwalt8ca88762010-12-17 15:20:36 -08001812 if (changed || !TextUtils.isEmpty(SystemProperties.get("net.dns" + k))) {
1813 if (DBG) log("erasing net.dns" + k);
1814 changed = true;
1815 SystemProperties.set("net.dns" + k, "");
1816 }
Robert Greenwalt3afbead2010-07-23 15:46:26 -07001817 }
1818 mNumDnsEntries = j;
1819 } else {
1820 // set per-pid dns for attached secondary nets
1821 List pids = mNetRequestersPids[netType];
1822 for (int y=0; y< pids.size(); y++) {
1823 Integer pid = (Integer)pids.get(y);
Robert Greenwalt8ca88762010-12-17 15:20:36 -08001824 changed = writePidDns(dnses, pid.intValue());
The Android Open Source Project28527d22009-03-03 19:31:44 -08001825 }
1826 }
Robert Greenwalt8ca88762010-12-17 15:20:36 -08001827 if (changed) bumpDns();
The Android Open Source Project28527d22009-03-03 19:31:44 -08001828 }
Robert Greenwalt2034b912009-08-12 16:08:25 -07001829 }
1830
Robert Greenwalt20f819c2011-05-03 19:02:44 -07001831 private int getRestoreDefaultNetworkDelay(int networkType) {
Robert Greenwalt2034b912009-08-12 16:08:25 -07001832 String restoreDefaultNetworkDelayStr = SystemProperties.get(
1833 NETWORK_RESTORE_DELAY_PROP_NAME);
1834 if(restoreDefaultNetworkDelayStr != null &&
1835 restoreDefaultNetworkDelayStr.length() != 0) {
1836 try {
1837 return Integer.valueOf(restoreDefaultNetworkDelayStr);
1838 } catch (NumberFormatException e) {
1839 }
The Android Open Source Project28527d22009-03-03 19:31:44 -08001840 }
Robert Greenwalt20f819c2011-05-03 19:02:44 -07001841 // if the system property isn't set, use the value for the apn type
1842 int ret = RESTORE_DEFAULT_NETWORK_DELAY;
1843
1844 if ((networkType <= ConnectivityManager.MAX_NETWORK_TYPE) &&
1845 (mNetConfigs[networkType] != null)) {
1846 ret = mNetConfigs[networkType].restoreTime;
1847 }
1848 return ret;
The Android Open Source Project28527d22009-03-03 19:31:44 -08001849 }
1850
1851 @Override
1852 protected void dump(FileDescriptor fd, PrintWriter pw, String[] args) {
Robert Greenwalt0659da32009-07-16 17:21:39 -07001853 if (mContext.checkCallingOrSelfPermission(
1854 android.Manifest.permission.DUMP)
The Android Open Source Project28527d22009-03-03 19:31:44 -08001855 != PackageManager.PERMISSION_GRANTED) {
Robert Greenwalt0659da32009-07-16 17:21:39 -07001856 pw.println("Permission Denial: can't dump ConnectivityService " +
1857 "from from pid=" + Binder.getCallingPid() + ", uid=" +
1858 Binder.getCallingUid());
The Android Open Source Project28527d22009-03-03 19:31:44 -08001859 return;
1860 }
The Android Open Source Project28527d22009-03-03 19:31:44 -08001861 pw.println();
1862 for (NetworkStateTracker nst : mNetTrackers) {
Robert Greenwalt3eeb6032009-12-21 18:24:07 -08001863 if (nst != null) {
1864 if (nst.getNetworkInfo().isConnected()) {
1865 pw.println("Active network: " + nst.getNetworkInfo().
1866 getTypeName());
1867 }
1868 pw.println(nst.getNetworkInfo());
1869 pw.println(nst);
1870 pw.println();
Robert Greenwalt2034b912009-08-12 16:08:25 -07001871 }
The Android Open Source Project28527d22009-03-03 19:31:44 -08001872 }
Robert Greenwalt3eeb6032009-12-21 18:24:07 -08001873
1874 pw.println("Network Requester Pids:");
1875 for (int net : mPriorityList) {
1876 String pidString = net + ": ";
1877 for (Object pid : mNetRequestersPids[net]) {
1878 pidString = pidString + pid.toString() + ", ";
1879 }
1880 pw.println(pidString);
1881 }
1882 pw.println();
1883
1884 pw.println("FeatureUsers:");
1885 for (Object requester : mFeatureUsers) {
1886 pw.println(requester.toString());
1887 }
1888 pw.println();
Robert Greenwalt8e87f122010-02-11 18:18:40 -08001889
Robert Greenwalt93dc1042010-06-15 12:19:37 -07001890 synchronized (this) {
1891 pw.println("NetworkTranstionWakeLock is currently " +
1892 (mNetTransitionWakeLock.isHeld() ? "" : "not ") + "held.");
1893 pw.println("It was last requested for "+mNetTransitionWakeLockCausedBy);
1894 }
1895 pw.println();
1896
Robert Greenwalt8e87f122010-02-11 18:18:40 -08001897 mTethering.dump(fd, pw, args);
Robert Greenwalt0e80be12010-09-20 14:35:25 -07001898
1899 if (mInetLog != null) {
1900 pw.println();
1901 pw.println("Inet condition reports:");
1902 for(int i = 0; i < mInetLog.size(); i++) {
1903 pw.println(mInetLog.get(i));
1904 }
1905 }
The Android Open Source Project28527d22009-03-03 19:31:44 -08001906 }
1907
Robert Greenwalt2034b912009-08-12 16:08:25 -07001908 // must be stateless - things change under us.
The Android Open Source Project28527d22009-03-03 19:31:44 -08001909 private class MyHandler extends Handler {
Wink Saville775aad62010-09-02 19:23:52 -07001910 public MyHandler(Looper looper) {
1911 super(looper);
1912 }
1913
The Android Open Source Project28527d22009-03-03 19:31:44 -08001914 @Override
1915 public void handleMessage(Message msg) {
1916 NetworkInfo info;
1917 switch (msg.what) {
1918 case NetworkStateTracker.EVENT_STATE_CHANGED:
1919 info = (NetworkInfo) msg.obj;
Robert Greenwalt12c44552009-12-07 11:33:18 -08001920 int type = info.getType();
1921 NetworkInfo.State state = info.getState();
Robert Greenwalt12c44552009-12-07 11:33:18 -08001922
Wink Savillee70c6f52010-12-03 12:01:38 -08001923 if (DBG) log("ConnectivityChange for " +
Robert Greenwalt0659da32009-07-16 17:21:39 -07001924 info.getTypeName() + ": " +
Robert Greenwalt12c44552009-12-07 11:33:18 -08001925 state + "/" + info.getDetailedState());
The Android Open Source Project28527d22009-03-03 19:31:44 -08001926
1927 // Connectivity state changed:
1928 // [31-13] Reserved for future use
Robert Greenwalt0659da32009-07-16 17:21:39 -07001929 // [12-9] Network subtype (for mobile network, as defined
1930 // by TelephonyManager)
1931 // [8-3] Detailed state ordinal (as defined by
1932 // NetworkInfo.DetailedState)
The Android Open Source Project28527d22009-03-03 19:31:44 -08001933 // [2-0] Network type (as defined by ConnectivityManager)
1934 int eventLogParam = (info.getType() & 0x7) |
1935 ((info.getDetailedState().ordinal() & 0x3f) << 3) |
1936 (info.getSubtype() << 9);
Doug Zongker2fc96232009-12-04 10:31:43 -08001937 EventLog.writeEvent(EventLogTags.CONNECTIVITY_STATE_CHANGED,
Robert Greenwalt0659da32009-07-16 17:21:39 -07001938 eventLogParam);
1939
1940 if (info.getDetailedState() ==
1941 NetworkInfo.DetailedState.FAILED) {
The Android Open Source Project28527d22009-03-03 19:31:44 -08001942 handleConnectionFailure(info);
Robert Greenwalt12c44552009-12-07 11:33:18 -08001943 } else if (state == NetworkInfo.State.DISCONNECTED) {
The Android Open Source Project28527d22009-03-03 19:31:44 -08001944 handleDisconnect(info);
Robert Greenwalt12c44552009-12-07 11:33:18 -08001945 } else if (state == NetworkInfo.State.SUSPENDED) {
The Android Open Source Project28527d22009-03-03 19:31:44 -08001946 // TODO: need to think this over.
Robert Greenwalt0659da32009-07-16 17:21:39 -07001947 // the logic here is, handle SUSPENDED the same as
1948 // DISCONNECTED. The only difference being we are
1949 // broadcasting an intent with NetworkInfo that's
1950 // suspended. This allows the applications an
1951 // opportunity to handle DISCONNECTED and SUSPENDED
1952 // differently, or not.
The Android Open Source Project28527d22009-03-03 19:31:44 -08001953 handleDisconnect(info);
Robert Greenwalt12c44552009-12-07 11:33:18 -08001954 } else if (state == NetworkInfo.State.CONNECTED) {
The Android Open Source Project28527d22009-03-03 19:31:44 -08001955 handleConnect(info);
1956 }
The Android Open Source Project28527d22009-03-03 19:31:44 -08001957 break;
The Android Open Source Project28527d22009-03-03 19:31:44 -08001958 case NetworkStateTracker.EVENT_CONFIGURATION_CHANGED:
Robert Greenwalt3afbead2010-07-23 15:46:26 -07001959 info = (NetworkInfo) msg.obj;
Kazuhiro Ondo07680062011-06-22 21:10:34 -05001960 // TODO: Temporary allowing network configuration
1961 // change not resetting sockets.
1962 // @see bug/4455071
1963 handleConnectivityChange(info.getType(), false);
The Android Open Source Project28527d22009-03-03 19:31:44 -08001964 break;
Robert Greenwaltccb36f92010-09-24 14:32:21 -07001965 case EVENT_CLEAR_NET_TRANSITION_WAKELOCK:
Robert Greenwalt93dc1042010-06-15 12:19:37 -07001966 String causedBy = null;
1967 synchronized (ConnectivityService.this) {
1968 if (msg.arg1 == mNetTransitionWakeLockSerialNumber &&
1969 mNetTransitionWakeLock.isHeld()) {
1970 mNetTransitionWakeLock.release();
1971 causedBy = mNetTransitionWakeLockCausedBy;
1972 }
1973 }
1974 if (causedBy != null) {
Wink Savillee70c6f52010-12-03 12:01:38 -08001975 log("NetTransition Wakelock for " + causedBy + " released by timeout");
Robert Greenwalt93dc1042010-06-15 12:19:37 -07001976 }
Robert Greenwaltcf1a56c2010-09-09 14:05:10 -07001977 break;
Robert Greenwalt6a2db8a2010-09-23 10:05:56 -07001978 case EVENT_RESTORE_DEFAULT_NETWORK:
Robert Greenwaltaffc3a12009-09-27 17:27:04 -07001979 FeatureUser u = (FeatureUser)msg.obj;
1980 u.expire();
Robert Greenwalt986c7412010-09-08 15:24:47 -07001981 break;
Robert Greenwalt6a2db8a2010-09-23 10:05:56 -07001982 case EVENT_INET_CONDITION_CHANGE:
1983 {
1984 int netType = msg.arg1;
1985 int condition = msg.arg2;
1986 handleInetConditionChange(netType, condition);
Robert Greenwalt93dc1042010-06-15 12:19:37 -07001987 break;
Robert Greenwalt6a2db8a2010-09-23 10:05:56 -07001988 }
1989 case EVENT_INET_CONDITION_HOLD_END:
1990 {
1991 int netType = msg.arg1;
1992 int sequence = msg.arg2;
1993 handleInetConditionHoldEnd(netType, sequence);
Robert Greenwalt986c7412010-09-08 15:24:47 -07001994 break;
Robert Greenwalt6a2db8a2010-09-23 10:05:56 -07001995 }
1996 case EVENT_SET_NETWORK_PREFERENCE:
1997 {
1998 int preference = msg.arg1;
1999 handleSetNetworkPreference(preference);
2000 break;
2001 }
2002 case EVENT_SET_BACKGROUND_DATA:
2003 {
2004 boolean enabled = (msg.arg1 == ENABLED);
2005 handleSetBackgroundData(enabled);
2006 break;
2007 }
2008 case EVENT_SET_MOBILE_DATA:
2009 {
2010 boolean enabled = (msg.arg1 == ENABLED);
2011 handleSetMobileData(enabled);
2012 break;
2013 }
Robert Greenwaltc3c5f862010-10-11 16:00:27 -07002014 case EVENT_APPLY_GLOBAL_HTTP_PROXY:
2015 {
2016 handleDeprecatedGlobalHttpProxy();
Robert Greenwalt34848c02011-03-25 13:09:25 -07002017 break;
2018 }
2019 case EVENT_SET_DEPENDENCY_MET:
2020 {
2021 boolean met = (msg.arg1 == ENABLED);
2022 handleSetDependencyMet(msg.arg2, met);
2023 break;
Robert Greenwaltc3c5f862010-10-11 16:00:27 -07002024 }
The Android Open Source Project28527d22009-03-03 19:31:44 -08002025 }
2026 }
2027 }
Robert Greenwalt0c4828c2010-01-26 11:40:34 -08002028
2029 // javadoc from interface
Robert Greenwalt4283ded2010-03-02 17:25:02 -08002030 public int tether(String iface) {
Robert Greenwalt0c4828c2010-01-26 11:40:34 -08002031 enforceTetherChangePermission();
Robert Greenwalt4283ded2010-03-02 17:25:02 -08002032
2033 if (isTetheringSupported()) {
2034 return mTethering.tether(iface);
2035 } else {
2036 return ConnectivityManager.TETHER_ERROR_UNSUPPORTED;
2037 }
Robert Greenwalt0c4828c2010-01-26 11:40:34 -08002038 }
2039
2040 // javadoc from interface
Robert Greenwalt4283ded2010-03-02 17:25:02 -08002041 public int untether(String iface) {
Robert Greenwalt0c4828c2010-01-26 11:40:34 -08002042 enforceTetherChangePermission();
Robert Greenwalt4283ded2010-03-02 17:25:02 -08002043
2044 if (isTetheringSupported()) {
2045 return mTethering.untether(iface);
2046 } else {
2047 return ConnectivityManager.TETHER_ERROR_UNSUPPORTED;
2048 }
2049 }
2050
2051 // javadoc from interface
2052 public int getLastTetherError(String iface) {
2053 enforceTetherAccessPermission();
2054
2055 if (isTetheringSupported()) {
2056 return mTethering.getLastTetherError(iface);
2057 } else {
2058 return ConnectivityManager.TETHER_ERROR_UNSUPPORTED;
2059 }
Robert Greenwalt8e87f122010-02-11 18:18:40 -08002060 }
2061
2062 // TODO - proper iface API for selection by property, inspection, etc
2063 public String[] getTetherableUsbRegexs() {
2064 enforceTetherAccessPermission();
2065 if (isTetheringSupported()) {
2066 return mTethering.getTetherableUsbRegexs();
2067 } else {
2068 return new String[0];
2069 }
2070 }
2071
2072 public String[] getTetherableWifiRegexs() {
2073 enforceTetherAccessPermission();
2074 if (isTetheringSupported()) {
2075 return mTethering.getTetherableWifiRegexs();
2076 } else {
2077 return new String[0];
2078 }
Robert Greenwalt0c4828c2010-01-26 11:40:34 -08002079 }
2080
Danica Chang96567052010-08-11 14:54:43 -07002081 public String[] getTetherableBluetoothRegexs() {
2082 enforceTetherAccessPermission();
2083 if (isTetheringSupported()) {
2084 return mTethering.getTetherableBluetoothRegexs();
2085 } else {
2086 return new String[0];
2087 }
2088 }
2089
Robert Greenwalt0c4828c2010-01-26 11:40:34 -08002090 // TODO - move iface listing, queries, etc to new module
2091 // javadoc from interface
2092 public String[] getTetherableIfaces() {
Robert Greenwalt8e87f122010-02-11 18:18:40 -08002093 enforceTetherAccessPermission();
Robert Greenwalt0c4828c2010-01-26 11:40:34 -08002094 return mTethering.getTetherableIfaces();
2095 }
2096
2097 public String[] getTetheredIfaces() {
Robert Greenwalt8e87f122010-02-11 18:18:40 -08002098 enforceTetherAccessPermission();
Robert Greenwalt0c4828c2010-01-26 11:40:34 -08002099 return mTethering.getTetheredIfaces();
2100 }
Robert Greenwalt8e87f122010-02-11 18:18:40 -08002101
Robert Greenwalt4283ded2010-03-02 17:25:02 -08002102 public String[] getTetheringErroredIfaces() {
2103 enforceTetherAccessPermission();
2104 return mTethering.getErroredIfaces();
2105 }
2106
Robert Greenwalt8e87f122010-02-11 18:18:40 -08002107 // if ro.tether.denied = true we default to no tethering
2108 // gservices could set the secure setting to 1 though to enable it on a build where it
2109 // had previously been turned off.
2110 public boolean isTetheringSupported() {
2111 enforceTetherAccessPermission();
2112 int defaultVal = (SystemProperties.get("ro.tether.denied").equals("true") ? 0 : 1);
Robert Greenwaltf1b66e12010-02-25 12:29:30 -08002113 boolean tetherEnabledInSettings = (Settings.Secure.getInt(mContext.getContentResolver(),
2114 Settings.Secure.TETHER_SUPPORTED, defaultVal) != 0);
2115 return tetherEnabledInSettings && mTetheringConfigValid;
Robert Greenwalt8e87f122010-02-11 18:18:40 -08002116 }
Robert Greenwalt93dc1042010-06-15 12:19:37 -07002117
2118 // An API NetworkStateTrackers can call when they lose their network.
2119 // This will automatically be cleared after X seconds or a network becomes CONNECTED,
2120 // whichever happens first. The timer is started by the first caller and not
2121 // restarted by subsequent callers.
2122 public void requestNetworkTransitionWakelock(String forWhom) {
2123 enforceConnectivityInternalPermission();
2124 synchronized (this) {
2125 if (mNetTransitionWakeLock.isHeld()) return;
2126 mNetTransitionWakeLockSerialNumber++;
2127 mNetTransitionWakeLock.acquire();
2128 mNetTransitionWakeLockCausedBy = forWhom;
2129 }
2130 mHandler.sendMessageDelayed(mHandler.obtainMessage(
Robert Greenwaltccb36f92010-09-24 14:32:21 -07002131 EVENT_CLEAR_NET_TRANSITION_WAKELOCK,
Robert Greenwalt93dc1042010-06-15 12:19:37 -07002132 mNetTransitionWakeLockSerialNumber, 0),
2133 mNetTransitionWakeLockTimeout);
2134 return;
2135 }
Robert Greenwalt24118e82010-09-09 13:15:32 -07002136
Robert Greenwalt986c7412010-09-08 15:24:47 -07002137 // 100 percent is full good, 0 is full bad.
2138 public void reportInetCondition(int networkType, int percentage) {
Wink Savillee70c6f52010-12-03 12:01:38 -08002139 if (DBG) log("reportNetworkCondition(" + networkType + ", " + percentage + ")");
Robert Greenwalt986c7412010-09-08 15:24:47 -07002140 mContext.enforceCallingOrSelfPermission(
2141 android.Manifest.permission.STATUS_BAR,
2142 "ConnectivityService");
2143
Robert Greenwalt0e80be12010-09-20 14:35:25 -07002144 if (DBG) {
2145 int pid = getCallingPid();
2146 int uid = getCallingUid();
2147 String s = pid + "(" + uid + ") reports inet is " +
2148 (percentage > 50 ? "connected" : "disconnected") + " (" + percentage + ") on " +
2149 "network Type " + networkType + " at " + GregorianCalendar.getInstance().getTime();
2150 mInetLog.add(s);
2151 while(mInetLog.size() > INET_CONDITION_LOG_MAX_SIZE) {
2152 mInetLog.remove(0);
2153 }
2154 }
Robert Greenwalt986c7412010-09-08 15:24:47 -07002155 mHandler.sendMessage(mHandler.obtainMessage(
Robert Greenwalt6a2db8a2010-09-23 10:05:56 -07002156 EVENT_INET_CONDITION_CHANGE, networkType, percentage));
2157 }
2158
2159 private void handleInetConditionChange(int netType, int condition) {
2160 if (DBG) {
Wink Savillee70c6f52010-12-03 12:01:38 -08002161 log("Inet connectivity change, net=" +
Robert Greenwalt6a2db8a2010-09-23 10:05:56 -07002162 netType + ", condition=" + condition +
2163 ",mActiveDefaultNetwork=" + mActiveDefaultNetwork);
2164 }
2165 if (mActiveDefaultNetwork == -1) {
Wink Savillee70c6f52010-12-03 12:01:38 -08002166 if (DBG) log("no active default network - aborting");
Robert Greenwalt6a2db8a2010-09-23 10:05:56 -07002167 return;
2168 }
2169 if (mActiveDefaultNetwork != netType) {
Wink Savillee70c6f52010-12-03 12:01:38 -08002170 if (DBG) log("given net not default - aborting");
Robert Greenwalt6a2db8a2010-09-23 10:05:56 -07002171 return;
2172 }
2173 mDefaultInetCondition = condition;
2174 int delay;
2175 if (mInetConditionChangeInFlight == false) {
Wink Savillee70c6f52010-12-03 12:01:38 -08002176 if (DBG) log("starting a change hold");
Robert Greenwalt6a2db8a2010-09-23 10:05:56 -07002177 // setup a new hold to debounce this
2178 if (mDefaultInetCondition > 50) {
2179 delay = Settings.Secure.getInt(mContext.getContentResolver(),
2180 Settings.Secure.INET_CONDITION_DEBOUNCE_UP_DELAY, 500);
2181 } else {
2182 delay = Settings.Secure.getInt(mContext.getContentResolver(),
2183 Settings.Secure.INET_CONDITION_DEBOUNCE_DOWN_DELAY, 3000);
2184 }
2185 mInetConditionChangeInFlight = true;
2186 mHandler.sendMessageDelayed(mHandler.obtainMessage(EVENT_INET_CONDITION_HOLD_END,
2187 mActiveDefaultNetwork, mDefaultConnectionSequence), delay);
2188 } else {
2189 // we've set the new condition, when this hold ends that will get
2190 // picked up
Wink Savillee70c6f52010-12-03 12:01:38 -08002191 if (DBG) log("currently in hold - not setting new end evt");
Robert Greenwalt6a2db8a2010-09-23 10:05:56 -07002192 }
2193 }
2194
2195 private void handleInetConditionHoldEnd(int netType, int sequence) {
2196 if (DBG) {
Wink Savillee70c6f52010-12-03 12:01:38 -08002197 log("Inet hold end, net=" + netType +
Robert Greenwalt6a2db8a2010-09-23 10:05:56 -07002198 ", condition =" + mDefaultInetCondition +
2199 ", published condition =" + mDefaultInetConditionPublished);
2200 }
2201 mInetConditionChangeInFlight = false;
2202
2203 if (mActiveDefaultNetwork == -1) {
Wink Savillee70c6f52010-12-03 12:01:38 -08002204 if (DBG) log("no active default network - aborting");
Robert Greenwalt6a2db8a2010-09-23 10:05:56 -07002205 return;
2206 }
2207 if (mDefaultConnectionSequence != sequence) {
Wink Savillee70c6f52010-12-03 12:01:38 -08002208 if (DBG) log("event hold for obsolete network - aborting");
Robert Greenwalt6a2db8a2010-09-23 10:05:56 -07002209 return;
2210 }
2211 if (mDefaultInetConditionPublished == mDefaultInetCondition) {
Wink Savillee70c6f52010-12-03 12:01:38 -08002212 if (DBG) log("no change in condition - aborting");
Robert Greenwalt6a2db8a2010-09-23 10:05:56 -07002213 return;
2214 }
2215 NetworkInfo networkInfo = mNetTrackers[mActiveDefaultNetwork].getNetworkInfo();
2216 if (networkInfo.isConnected() == false) {
Wink Savillee70c6f52010-12-03 12:01:38 -08002217 if (DBG) log("default network not connected - aborting");
Robert Greenwalt6a2db8a2010-09-23 10:05:56 -07002218 return;
2219 }
2220 mDefaultInetConditionPublished = mDefaultInetCondition;
2221 sendInetConditionBroadcast(networkInfo);
2222 return;
Robert Greenwalt986c7412010-09-08 15:24:47 -07002223 }
Robert Greenwaltc3c5f862010-10-11 16:00:27 -07002224
2225 public synchronized ProxyProperties getProxy() {
2226 if (mGlobalProxy != null) return mGlobalProxy;
2227 if (mDefaultProxy != null) return mDefaultProxy;
2228 return null;
2229 }
2230
2231 public void setGlobalProxy(ProxyProperties proxyProperties) {
2232 enforceChangePermission();
2233 synchronized (mGlobalProxyLock) {
2234 if (proxyProperties == mGlobalProxy) return;
2235 if (proxyProperties != null && proxyProperties.equals(mGlobalProxy)) return;
2236 if (mGlobalProxy != null && mGlobalProxy.equals(proxyProperties)) return;
2237
2238 String host = "";
2239 int port = 0;
2240 String exclList = "";
2241 if (proxyProperties != null && !TextUtils.isEmpty(proxyProperties.getHost())) {
2242 mGlobalProxy = new ProxyProperties(proxyProperties);
2243 host = mGlobalProxy.getHost();
2244 port = mGlobalProxy.getPort();
2245 exclList = mGlobalProxy.getExclusionList();
2246 } else {
2247 mGlobalProxy = null;
2248 }
2249 ContentResolver res = mContext.getContentResolver();
2250 Settings.Secure.putString(res, Settings.Secure.GLOBAL_HTTP_PROXY_HOST, host);
2251 Settings.Secure.putInt(res, Settings.Secure.GLOBAL_HTTP_PROXY_PORT, port);
Robert Greenwalt6f7c6092010-12-02 11:31:00 -08002252 Settings.Secure.putString(res, Settings.Secure.GLOBAL_HTTP_PROXY_EXCLUSION_LIST,
Robert Greenwaltc3c5f862010-10-11 16:00:27 -07002253 exclList);
2254 }
2255
2256 if (mGlobalProxy == null) {
2257 proxyProperties = mDefaultProxy;
2258 }
2259 sendProxyBroadcast(proxyProperties);
2260 }
2261
Robert Greenwalt6f7c6092010-12-02 11:31:00 -08002262 private void loadGlobalProxy() {
2263 ContentResolver res = mContext.getContentResolver();
2264 String host = Settings.Secure.getString(res, Settings.Secure.GLOBAL_HTTP_PROXY_HOST);
2265 int port = Settings.Secure.getInt(res, Settings.Secure.GLOBAL_HTTP_PROXY_PORT, 0);
2266 String exclList = Settings.Secure.getString(res,
2267 Settings.Secure.GLOBAL_HTTP_PROXY_EXCLUSION_LIST);
2268 if (!TextUtils.isEmpty(host)) {
2269 ProxyProperties proxyProperties = new ProxyProperties(host, port, exclList);
2270 synchronized (mGlobalProxyLock) {
2271 mGlobalProxy = proxyProperties;
2272 }
2273 }
2274 }
2275
Robert Greenwaltc3c5f862010-10-11 16:00:27 -07002276 public ProxyProperties getGlobalProxy() {
2277 synchronized (mGlobalProxyLock) {
2278 return mGlobalProxy;
2279 }
2280 }
2281
2282 private void handleApplyDefaultProxy(int type) {
2283 // check if new default - push it out to all VM if so
2284 ProxyProperties proxy = mNetTrackers[type].getLinkProperties().getHttpProxy();
2285 synchronized (this) {
2286 if (mDefaultProxy != null && mDefaultProxy.equals(proxy)) return;
2287 if (mDefaultProxy == proxy) return;
2288 if (!TextUtils.isEmpty(proxy.getHost())) {
2289 mDefaultProxy = proxy;
2290 } else {
2291 mDefaultProxy = null;
2292 }
2293 }
Wink Savillee70c6f52010-12-03 12:01:38 -08002294 if (DBG) log("changing default proxy to " + proxy);
Robert Greenwaltc3c5f862010-10-11 16:00:27 -07002295 if ((proxy == null && mGlobalProxy == null) || proxy.equals(mGlobalProxy)) return;
2296 if (mGlobalProxy != null) return;
2297 sendProxyBroadcast(proxy);
2298 }
2299
2300 private void handleDeprecatedGlobalHttpProxy() {
2301 String proxy = Settings.Secure.getString(mContext.getContentResolver(),
2302 Settings.Secure.HTTP_PROXY);
2303 if (!TextUtils.isEmpty(proxy)) {
2304 String data[] = proxy.split(":");
2305 String proxyHost = data[0];
2306 int proxyPort = 8080;
2307 if (data.length > 1) {
2308 try {
2309 proxyPort = Integer.parseInt(data[1]);
2310 } catch (NumberFormatException e) {
2311 return;
2312 }
2313 }
2314 ProxyProperties p = new ProxyProperties(data[0], proxyPort, "");
2315 setGlobalProxy(p);
2316 }
2317 }
2318
2319 private void sendProxyBroadcast(ProxyProperties proxy) {
Robert Greenwalt611291c2010-12-23 15:51:10 -08002320 if (proxy == null) proxy = new ProxyProperties("", 0, "");
Wink Savillee70c6f52010-12-03 12:01:38 -08002321 log("sending Proxy Broadcast for " + proxy);
Robert Greenwaltc3c5f862010-10-11 16:00:27 -07002322 Intent intent = new Intent(Proxy.PROXY_CHANGE_ACTION);
Stan Chesnutt1f2a2ac2011-01-06 11:00:19 -08002323 intent.addFlags(Intent.FLAG_RECEIVER_REPLACE_PENDING |
2324 Intent.FLAG_RECEIVER_REGISTERED_ONLY_BEFORE_BOOT);
Robert Greenwaltc3c5f862010-10-11 16:00:27 -07002325 intent.putExtra(Proxy.EXTRA_PROXY_INFO, proxy);
Robert Greenwaltd93dc8f2010-12-06 11:29:17 -08002326 mContext.sendStickyBroadcast(intent);
Robert Greenwaltc3c5f862010-10-11 16:00:27 -07002327 }
2328
2329 private static class SettingsObserver extends ContentObserver {
2330 private int mWhat;
2331 private Handler mHandler;
2332 SettingsObserver(Handler handler, int what) {
2333 super(handler);
2334 mHandler = handler;
2335 mWhat = what;
2336 }
2337
2338 void observe(Context context) {
2339 ContentResolver resolver = context.getContentResolver();
2340 resolver.registerContentObserver(Settings.Secure.getUriFor(
2341 Settings.Secure.HTTP_PROXY), false, this);
2342 }
2343
2344 @Override
2345 public void onChange(boolean selfChange) {
2346 mHandler.obtainMessage(mWhat).sendToTarget();
2347 }
2348 }
Wink Savillee70c6f52010-12-03 12:01:38 -08002349
2350 private void log(String s) {
2351 Slog.d(TAG, s);
2352 }
2353
2354 private void loge(String s) {
2355 Slog.e(TAG, s);
2356 }
Wink Savilleb7c92c72011-03-12 14:52:01 -08002357 int convertFeatureToNetworkType(String feature){
2358 int networkType = -1;
2359 if (TextUtils.equals(feature, Phone.FEATURE_ENABLE_MMS)) {
2360 networkType = ConnectivityManager.TYPE_MOBILE_MMS;
2361 } else if (TextUtils.equals(feature, Phone.FEATURE_ENABLE_SUPL)) {
2362 networkType = ConnectivityManager.TYPE_MOBILE_SUPL;
2363 } else if (TextUtils.equals(feature, Phone.FEATURE_ENABLE_DUN) ||
2364 TextUtils.equals(feature, Phone.FEATURE_ENABLE_DUN_ALWAYS)) {
2365 networkType = ConnectivityManager.TYPE_MOBILE_DUN;
2366 } else if (TextUtils.equals(feature, Phone.FEATURE_ENABLE_HIPRI)) {
2367 networkType = ConnectivityManager.TYPE_MOBILE_HIPRI;
2368 } else if (TextUtils.equals(feature, Phone.FEATURE_ENABLE_FOTA)) {
2369 networkType = ConnectivityManager.TYPE_MOBILE_FOTA;
2370 } else if (TextUtils.equals(feature, Phone.FEATURE_ENABLE_IMS)) {
2371 networkType = ConnectivityManager.TYPE_MOBILE_IMS;
2372 } else if (TextUtils.equals(feature, Phone.FEATURE_ENABLE_CBS)) {
2373 networkType = ConnectivityManager.TYPE_MOBILE_CBS;
2374 }
2375 return networkType;
2376 }
The Android Open Source Project28527d22009-03-03 19:31:44 -08002377}