blob: 812c1eb1d547e9f451a9274345a01e31d5c00ede [file] [log] [blame]
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001/*
2 * Copyright (C) 2008 The Android Open Source Project
3 *
4 * Licensed under the Apache License, Version 2.0 (the "License");
5 * you may not use this file except in compliance with the License.
6 * You may obtain a copy of the License at
7 *
8 * http://www.apache.org/licenses/LICENSE-2.0
9 *
10 * Unless required by applicable law or agreed to in writing, software
11 * distributed under the License is distributed on an "AS IS" BASIS,
12 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13 * See the License for the specific language governing permissions and
14 * limitations under the License.
15 */
16
17package com.android.server;
18
Jaikumar Ganesh15c74392010-12-21 22:31:44 -080019import android.bluetooth.BluetoothTetheringDataTracker;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080020import android.content.ContentResolver;
21import android.content.Context;
22import android.content.Intent;
23import android.content.pm.PackageManager;
Robert Greenwalt434203a2010-10-11 16:00:27 -070024import android.database.ContentObserver;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080025import android.net.ConnectivityManager;
Robert Greenwaltda3d5e62010-12-06 13:56:24 -080026import android.net.DummyDataStateTracker;
Benoit Goby08c39c62010-12-22 14:29:40 -080027import android.net.EthernetDataTracker;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080028import android.net.IConnectivityManager;
Robert Greenwalt59b1a4e2011-05-10 15:05:02 -070029import android.net.LinkAddress;
Jaikumar Ganesh15c74392010-12-21 22:31:44 -080030import android.net.LinkProperties;
Wink Savillee8222252011-07-13 13:44:13 -070031import android.net.LinkProperties.CompareAddressesResult;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080032import android.net.MobileDataStateTracker;
Robert Greenwaltd55a6b42011-03-25 13:09:25 -070033import android.net.NetworkConfig;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080034import android.net.NetworkInfo;
35import android.net.NetworkStateTracker;
Robert Greenwalt585ac0f2010-08-27 09:24:29 -070036import android.net.NetworkUtils;
Robert Greenwalt434203a2010-10-11 16:00:27 -070037import android.net.Proxy;
38import android.net.ProxyProperties;
Robert Greenwaltaa70f102011-04-28 14:28:50 -070039import android.net.RouteInfo;
Hung-ying Tyan6b818de2011-01-19 16:48:38 +080040import android.net.vpn.VpnManager;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080041import android.net.wifi.WifiStateTracker;
42import android.os.Binder;
43import android.os.Handler;
Wink Savillebb08caf2010-09-02 19:23:52 -070044import android.os.HandlerThread;
Robert Greenwalt42acef32009-08-12 16:08:25 -070045import android.os.IBinder;
Robert Greenwalt59b1a4e2011-05-10 15:05:02 -070046import android.os.INetworkManagementService;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080047import android.os.Looper;
48import android.os.Message;
Robert Greenwalt14f2ef42010-06-15 12:19:37 -070049import android.os.PowerManager;
Robert Greenwalt42acef32009-08-12 16:08:25 -070050import android.os.RemoteException;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080051import android.os.ServiceManager;
52import android.os.SystemProperties;
53import android.provider.Settings;
Robert Greenwalt42acef32009-08-12 16:08:25 -070054import android.text.TextUtils;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080055import android.util.EventLog;
Joe Onorato8a9b2202010-02-26 18:56:32 -080056import android.util.Slog;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080057
Robert Greenwalt42acef32009-08-12 16:08:25 -070058import com.android.internal.telephony.Phone;
Robert Greenwaltd0e18ff2010-01-26 11:40:34 -080059import com.android.server.connectivity.Tethering;
60
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080061import java.io.FileDescriptor;
Irfan Sheriffd649c122010-06-09 15:39:36 -070062import java.io.FileWriter;
63import java.io.IOException;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080064import java.io.PrintWriter;
Wink Savillee8222252011-07-13 13:44:13 -070065import java.net.Inet6Address;
Robert Greenwalt47f69fe2010-06-15 15:43:39 -070066import java.net.InetAddress;
Robert Greenwalt59b1a4e2011-05-10 15:05:02 -070067import java.net.Inet4Address;
Robert Greenwalt47f69fe2010-06-15 15:43:39 -070068import java.net.UnknownHostException;
Robert Greenwalt42acef32009-08-12 16:08:25 -070069import java.util.ArrayList;
Robert Greenwalt47f69fe2010-06-15 15:43:39 -070070import java.util.Collection;
Robert Greenwaltd825ea42010-12-29 16:15:02 -080071import java.util.concurrent.atomic.AtomicBoolean;
Robert Greenwalt4e8dfef2010-09-20 14:35:25 -070072import java.util.GregorianCalendar;
Robert Greenwalt42acef32009-08-12 16:08:25 -070073import java.util.List;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080074
75/**
76 * @hide
77 */
78public class ConnectivityService extends IConnectivityManager.Stub {
79
Robert Greenwaltba175a52010-10-05 19:12:26 -070080 private static final boolean DBG = true;
Wink Savillee8222252011-07-13 13:44:13 -070081 private static final boolean VDBG = true;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080082 private static final String TAG = "ConnectivityService";
83
Robert Greenwalt42acef32009-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 Greenwaltf43396c2011-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 Greenwaltd0e18ff2010-01-26 11:40:34 -080094 private Tethering mTethering;
Robert Greenwaltc9d5fb72010-02-25 12:29:30 -080095 private boolean mTetheringConfigValid = false;
Robert Greenwaltd0e18ff2010-01-26 11:40:34 -080096
The Android Open Source Project9066cfe2009-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 Greenwalt42acef32009-08-12 16:08:25 -0700103
104 /**
Wink Savillee8222252011-07-13 13:44:13 -0700105 * The link properties that define the current links
106 */
107 private LinkProperties mCurrentLinkProperties[];
108
109 /**
Robert Greenwalt42acef32009-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 Sheriffa2a1b912010-06-07 09:03:04 -0700115 private WifiWatchdogService mWifiWatchdogService;
116
Robert Greenwalt42acef32009-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 Project9066cfe2009-03-03 19:31:44 -0800122 private Context mContext;
123 private int mNetworkPreference;
Robert Greenwalt42acef32009-08-12 16:08:25 -0700124 private int mActiveDefaultNetwork = -1;
Robert Greenwaltd7085fc2010-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 Project9066cfe2009-03-03 19:31:44 -0800130
131 private int mNumDnsEntries;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800132
133 private boolean mTestMode;
Joe Onorato00092872010-09-01 21:18:22 -0700134 private static ConnectivityService sServiceInstance;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800135
Robert Greenwaltd825ea42010-12-29 16:15:02 -0800136 private AtomicBoolean mBackgroundDataEnabled = new AtomicBoolean(true);
137
Robert Greenwalt59b1a4e2011-05-10 15:05:02 -0700138 private INetworkManagementService mNetd;
139
Robert Greenwalt8dcc28b2010-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 Greenwaltf3331232010-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 Greenwalt434203a2010-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 Greenwaltd55a6b42011-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 Greenwalt42acef32009-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 Lockwood0f79b542009-08-14 14:18:49 -0400225 private boolean mSystemReady;
Dianne Hackborn1c633fc2009-12-08 19:45:14 -0800226 private Intent mInitialBroadcast;
Mike Lockwood0f79b542009-08-14 14:18:49 -0400227
Robert Greenwalt14f2ef42010-06-15 12:19:37 -0700228 private PowerManager.WakeLock mNetTransitionWakeLock;
229 private String mNetTransitionWakeLockCausedBy = "";
230 private int mNetTransitionWakeLockSerialNumber;
231 private int mNetTransitionWakeLockTimeout;
232
Robert Greenwalte90aa5e2010-09-01 11:34:05 -0700233 private InetAddress mDefaultDns;
234
Robert Greenwalt4e8dfef2010-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 Greenwalt434203a2010-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 Greenwaltd55a6b42011-03-25 13:09:25 -0700247 NetworkConfig[] mNetConfigs;
Robert Greenwalt5154ae762009-10-30 14:17:42 -0700248 int mNetworksDefined;
Robert Greenwalt42acef32009-08-12 16:08:25 -0700249
Robert Greenwalt511288a2009-12-07 11:33:18 -0800250 private static class RadioAttributes {
Robert Greenwalt42acef32009-08-12 16:08:25 -0700251 public int mSimultaneity;
252 public int mType;
253 public RadioAttributes(String init) {
254 String fragments[] = init.split(",");
Robert Greenwalt5154ae762009-10-30 14:17:42 -0700255 mType = Integer.parseInt(fragments[0]);
256 mSimultaneity = Integer.parseInt(fragments[1]);
Robert Greenwalt42acef32009-08-12 16:08:25 -0700257 }
258 }
259 RadioAttributes[] mRadioAttributes;
260
Robert Greenwalt50393202011-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 Savillebb08caf2010-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 Project9066cfe2009-03-03 19:31:44 -0800267 }
Wink Savillebb08caf2010-09-02 19:23:52 -0700268 return sServiceInstance;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800269 }
Robert Greenwalt86e9e552009-07-16 17:21:39 -0700270
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800271 private ConnectivityService(Context context) {
Wink Savilleed9c02b2010-12-03 12:01:38 -0800272 if (DBG) log("ConnectivityService starting up");
Robert Greenwaltde8383c2010-01-14 17:47:58 -0800273
Wink Savillebb08caf2010-09-02 19:23:52 -0700274 HandlerThread handlerThread = new HandlerThread("ConnectivityServiceThread");
275 handlerThread.start();
276 mHandler = new MyHandler(handlerThread.getLooper());
277
Robert Greenwaltd825ea42010-12-29 16:15:02 -0800278 mBackgroundDataEnabled.set(Settings.Secure.getInt(context.getContentResolver(),
279 Settings.Secure.BACKGROUND_DATA, 1) == 1);
280
Robert Greenwaltde8383c2010-01-14 17:47:58 -0800281 // setup our unique device name
Robert Greenwalt733c6292010-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 Greenwaltde8383c2010-01-14 17:47:58 -0800289 }
290
Robert Greenwalte90aa5e2010-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 Greenwalte5903732011-02-22 16:00:42 -0800299 mDefaultDns = NetworkUtils.numericToInetAddress(dns);
300 } catch (IllegalArgumentException e) {
Wink Savilleed9c02b2010-12-03 12:01:38 -0800301 loge("Error setting defaultDns using " + dns);
Robert Greenwalte90aa5e2010-09-01 11:34:05 -0700302 }
303
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800304 mContext = context;
Robert Greenwalt14f2ef42010-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 Greenwalt42acef32009-08-12 16:08:25 -0700311 mNetTrackers = new NetworkStateTracker[
312 ConnectivityManager.MAX_NETWORK_TYPE+1];
Wink Savillee8222252011-07-13 13:44:13 -0700313 mCurrentLinkProperties = new LinkProperties[ConnectivityManager.MAX_NETWORK_TYPE+1];
Robert Greenwalt86e9e552009-07-16 17:21:39 -0700314
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800315 mNetworkPreference = getPersistedNetworkPreference();
Robert Greenwalt86e9e552009-07-16 17:21:39 -0700316
Robert Greenwalt5154ae762009-10-30 14:17:42 -0700317 mRadioAttributes = new RadioAttributes[ConnectivityManager.MAX_RADIO_TYPE+1];
Robert Greenwaltd55a6b42011-03-25 13:09:25 -0700318 mNetConfigs = new NetworkConfig[ConnectivityManager.MAX_NETWORK_TYPE+1];
Robert Greenwalt5154ae762009-10-30 14:17:42 -0700319
Robert Greenwalt42acef32009-08-12 16:08:25 -0700320 // Load device network attributes from resources
Robert Greenwalt42acef32009-08-12 16:08:25 -0700321 String[] raStrings = context.getResources().getStringArray(
322 com.android.internal.R.array.radioAttributes);
Robert Greenwalt5154ae762009-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 Savilleed9c02b2010-12-03 12:01:38 -0800326 loge("Error in radioAttributes - ignoring attempt to define type " + r.mType);
Robert Greenwalt5154ae762009-10-30 14:17:42 -0700327 continue;
328 }
329 if (mRadioAttributes[r.mType] != null) {
Wink Savilleed9c02b2010-12-03 12:01:38 -0800330 loge("Error in radioAttributes - ignoring attempt to redefine type " +
Robert Greenwalt5154ae762009-10-30 14:17:42 -0700331 r.mType);
332 continue;
333 }
Robert Greenwalt42acef32009-08-12 16:08:25 -0700334 mRadioAttributes[r.mType] = r;
335 }
336
Robert Greenwalt5154ae762009-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 Greenwaltd55a6b42011-03-25 13:09:25 -0700341 NetworkConfig n = new NetworkConfig(naString);
Wink Saville975c8482011-04-07 14:23:45 -0700342 if (n.type > ConnectivityManager.MAX_NETWORK_TYPE) {
Wink Savilleed9c02b2010-12-03 12:01:38 -0800343 loge("Error in networkAttributes - ignoring attempt to define type " +
Wink Saville975c8482011-04-07 14:23:45 -0700344 n.type);
Robert Greenwalt5154ae762009-10-30 14:17:42 -0700345 continue;
Robert Greenwalt42acef32009-08-12 16:08:25 -0700346 }
Wink Saville975c8482011-04-07 14:23:45 -0700347 if (mNetConfigs[n.type] != null) {
Wink Savilleed9c02b2010-12-03 12:01:38 -0800348 loge("Error in networkAttributes - ignoring attempt to redefine type " +
Wink Saville975c8482011-04-07 14:23:45 -0700349 n.type);
Robert Greenwalt5154ae762009-10-30 14:17:42 -0700350 continue;
351 }
Wink Saville975c8482011-04-07 14:23:45 -0700352 if (mRadioAttributes[n.radio] == null) {
Wink Savilleed9c02b2010-12-03 12:01:38 -0800353 loge("Error in networkAttributes - ignoring attempt to use undefined " +
Wink Saville975c8482011-04-07 14:23:45 -0700354 "radio " + n.radio + " in network type " + n.type);
Robert Greenwalt5154ae762009-10-30 14:17:42 -0700355 continue;
356 }
Wink Saville975c8482011-04-07 14:23:45 -0700357 mNetConfigs[n.type] = n;
Robert Greenwalt5154ae762009-10-30 14:17:42 -0700358 mNetworksDefined++;
359 } catch(Exception e) {
360 // ignore it - leave the entry null
Robert Greenwalt42acef32009-08-12 16:08:25 -0700361 }
362 }
363
Robert Greenwalt50393202011-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 Greenwalt5154ae762009-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 Greenwaltd55a6b42011-03-25 13:09:25 -0700382 for (NetworkConfig na : mNetConfigs) {
Robert Greenwalt5154ae762009-10-30 14:17:42 -0700383 if (na == null) continue;
Wink Saville975c8482011-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 Greenwalt5154ae762009-10-30 14:17:42 -0700388 }
389 continue;
390 }
Wink Saville975c8482011-04-07 14:23:45 -0700391 mPriorityList[insertionPoint--] = na.type;
Robert Greenwalt5154ae762009-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 Greenwalt42acef32009-08-12 16:08:25 -0700400 mNetRequestersPids[i] = new ArrayList();
401 }
402
403 mFeatureUsers = new ArrayList();
404
Robert Greenwalt5154ae762009-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 Project9066cfe2009-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 Greenwalt5154ae762009-10-30 14:17:42 -0700416 for (int netType : mPriorityList) {
Wink Saville975c8482011-04-07 14:23:45 -0700417 switch (mNetConfigs[netType].radio) {
Robert Greenwalt5154ae762009-10-30 14:17:42 -0700418 case ConnectivityManager.TYPE_WIFI:
Wink Savilleed9c02b2010-12-03 12:01:38 -0800419 if (DBG) log("Starting Wifi Service.");
Wink Savillec7a98342010-08-13 16:11:42 -0700420 WifiStateTracker wst = new WifiStateTracker();
Irfan Sheriff0d255342010-07-28 09:35:20 -0700421 WifiService wifiService = new WifiService(context);
Robert Greenwalt5154ae762009-10-30 14:17:42 -0700422 ServiceManager.addService(Context.WIFI_SERVICE, wifiService);
Irfan Sheriff0d255342010-07-28 09:35:20 -0700423 wifiService.checkAndStartWifi();
Robert Greenwalt5154ae762009-10-30 14:17:42 -0700424 mNetTrackers[ConnectivityManager.TYPE_WIFI] = wst;
Wink Savillec7a98342010-08-13 16:11:42 -0700425 wst.startMonitoring(context, mHandler);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800426
Irfan Sheriffa2a1b912010-06-07 09:03:04 -0700427 //TODO: as part of WWS refactor, create only when needed
Irfan Sheriff0d255342010-07-28 09:35:20 -0700428 mWifiWatchdogService = new WifiWatchdogService(context);
Irfan Sheriffa2a1b912010-06-07 09:03:04 -0700429
Robert Greenwalt5154ae762009-10-30 14:17:42 -0700430 break;
431 case ConnectivityManager.TYPE_MOBILE:
Wink Savillec7a98342010-08-13 16:11:42 -0700432 mNetTrackers[netType] = new MobileDataStateTracker(netType,
Wink Saville975c8482011-04-07 14:23:45 -0700433 mNetConfigs[netType].name);
Wink Savillec7a98342010-08-13 16:11:42 -0700434 mNetTrackers[netType].startMonitoring(context, mHandler);
Robert Greenwalt5154ae762009-10-30 14:17:42 -0700435 break;
Robert Greenwaltda3d5e62010-12-06 13:56:24 -0800436 case ConnectivityManager.TYPE_DUMMY:
437 mNetTrackers[netType] = new DummyDataStateTracker(netType,
Wink Saville975c8482011-04-07 14:23:45 -0700438 mNetConfigs[netType].name);
Robert Greenwaltda3d5e62010-12-06 13:56:24 -0800439 mNetTrackers[netType].startMonitoring(context, mHandler);
440 break;
Jaikumar Ganesh15c74392010-12-21 22:31:44 -0800441 case ConnectivityManager.TYPE_BLUETOOTH:
442 mNetTrackers[netType] = BluetoothTetheringDataTracker.getInstance();
443 mNetTrackers[netType].startMonitoring(context, mHandler);
444 break;
Benoit Goby08c39c62010-12-22 14:29:40 -0800445 case ConnectivityManager.TYPE_ETHERNET:
446 mNetTrackers[netType] = EthernetDataTracker.getInstance();
447 mNetTrackers[netType].startMonitoring(context, mHandler);
448 break;
Robert Greenwalt5154ae762009-10-30 14:17:42 -0700449 default:
Wink Savilleed9c02b2010-12-03 12:01:38 -0800450 loge("Trying to create a DataStateTracker for an unknown radio type " +
Wink Saville975c8482011-04-07 14:23:45 -0700451 mNetConfigs[netType].radio);
Robert Greenwalt5154ae762009-10-30 14:17:42 -0700452 continue;
453 }
Wink Savillee8222252011-07-13 13:44:13 -0700454 mCurrentLinkProperties[netType] = mNetTrackers[netType].getLinkProperties();
Robert Greenwalt5154ae762009-10-30 14:17:42 -0700455 }
Robert Greenwaltd0e18ff2010-01-26 11:40:34 -0800456
Robert Greenwaltdfadaea2010-03-11 15:03:08 -0800457 mTethering = new Tethering(mContext, mHandler.getLooper());
Robert Greenwaltccf83af12011-06-02 17:30:47 -0700458 mTetheringConfigValid = ((mTethering.getTetherableUsbRegexs().length != 0 ||
Danica Chang6fdd0c62010-08-11 14:54:43 -0700459 mTethering.getTetherableWifiRegexs().length != 0 ||
460 mTethering.getTetherableBluetoothRegexs().length != 0) &&
Robert Greenwaltccf83af12011-06-02 17:30:47 -0700461 mTethering.getUpstreamIfaceTypes().length != 0);
Robert Greenwaltc9d5fb72010-02-25 12:29:30 -0800462
Robert Greenwalt4e8dfef2010-09-20 14:35:25 -0700463 if (DBG) {
464 mInetLog = new ArrayList();
465 }
Robert Greenwalt434203a2010-10-11 16:00:27 -0700466
467 mSettingsObserver = new SettingsObserver(mHandler, EVENT_APPLY_GLOBAL_HTTP_PROXY);
468 mSettingsObserver.observe(mContext);
Robert Greenwaltb7090d62010-12-02 11:31:00 -0800469
470 loadGlobalProxy();
Hung-ying Tyan6b818de2011-01-19 16:48:38 +0800471
472 VpnManager.startVpnService(context);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800473 }
474
Robert Greenwalt5154ae762009-10-30 14:17:42 -0700475
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800476 /**
Robert Greenwalt86e9e552009-07-16 17:21:39 -0700477 * Sets the preferred network.
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800478 * @param preference the new preference
479 */
Robert Greenwalt8dcc28b2010-09-23 10:05:56 -0700480 public void setNetworkPreference(int preference) {
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800481 enforceChangePermission();
Robert Greenwalt8dcc28b2010-09-23 10:05:56 -0700482
483 mHandler.sendMessage(mHandler.obtainMessage(EVENT_SET_NETWORK_PREFERENCE, preference, 0));
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800484 }
485
486 public int getNetworkPreference() {
487 enforceAccessPermission();
Robert Greenwalt8dcc28b2010-09-23 10:05:56 -0700488 int preference;
489 synchronized(this) {
490 preference = mNetworkPreference;
491 }
492 return preference;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800493 }
494
Robert Greenwalt8dcc28b2010-09-23 10:05:56 -0700495 private void handleSetNetworkPreference(int preference) {
496 if (ConnectivityManager.isNetworkTypeValid(preference) &&
Robert Greenwaltd55a6b42011-03-25 13:09:25 -0700497 mNetConfigs[preference] != null &&
498 mNetConfigs[preference].isDefault()) {
Robert Greenwalt8dcc28b2010-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 Project9066cfe2009-03-03 19:31:44 -0800508 }
Robert Greenwalt86e9e552009-07-16 17:21:39 -0700509
The Android Open Source Project9066cfe2009-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 Greenwalt86e9e552009-07-16 17:21:39 -0700521
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800522 /**
Robert Greenwalt86e9e552009-07-16 17:21:39 -0700523 * Make the state of network connectivity conform to the preference settings
The Android Open Source Project9066cfe2009-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 Greenwalt42acef32009-08-12 16:08:25 -0700530 if (mNetTrackers[mNetworkPreference].getNetworkInfo().isConnected())
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800531 return;
532
Robert Greenwalt42acef32009-08-12 16:08:25 -0700533 if (!mNetTrackers[mNetworkPreference].isAvailable())
534 return;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800535
Robert Greenwalt42acef32009-08-12 16:08:25 -0700536 for (int t=0; t <= ConnectivityManager.MAX_RADIO_TYPE; t++) {
Robert Greenwalt5154ae762009-10-30 14:17:42 -0700537 if (t != mNetworkPreference && mNetTrackers[t] != null &&
Robert Greenwalt42acef32009-08-12 16:08:25 -0700538 mNetTrackers[t].getNetworkInfo().isConnected()) {
Robert Greenwaltec9fe462009-08-20 15:25:14 -0700539 if (DBG) {
Wink Savilleed9c02b2010-12-03 12:01:38 -0800540 log("tearing down " + mNetTrackers[t].getNetworkInfo() +
Robert Greenwaltec9fe462009-08-20 15:25:14 -0700541 " in enforcePreference");
542 }
Robert Greenwalt42acef32009-08-12 16:08:25 -0700543 teardown(mNetTrackers[t]);
The Android Open Source Project9066cfe2009-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 Greenwalt86e9e552009-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 Project9066cfe2009-03-03 19:31:44 -0800563 */
564 public NetworkInfo getActiveNetworkInfo() {
Robert Greenwalt59911582011-05-20 12:23:41 -0700565 return getNetworkInfo(mActiveDefaultNetwork);
The Android Open Source Project9066cfe2009-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 Greenwalt5154ae762009-10-30 14:17:42 -0700580 NetworkInfo[] result = new NetworkInfo[mNetworksDefined];
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800581 int i = 0;
582 for (NetworkStateTracker t : mNetTrackers) {
Robert Greenwalt5154ae762009-10-30 14:17:42 -0700583 if(t != null) result[i++] = t.getNetworkInfo();
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800584 }
585 return result;
586 }
587
Robert Greenwaltd192dad2010-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 Greenwalt59911582011-05-20 12:23:41 -0700597 return getLinkProperties(mActiveDefaultNetwork);
Robert Greenwaltd192dad2010-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 Project9066cfe2009-03-03 19:31:44 -0800609 public boolean setRadios(boolean turnOn) {
610 boolean result = true;
611 enforceChangePermission();
612 for (NetworkStateTracker t : mNetTrackers) {
Robert Greenwalt5154ae762009-10-30 14:17:42 -0700613 if (t != null) result = t.setRadio(turnOn) && result;
The Android Open Source Project9066cfe2009-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 Greenwalt9c75d4a2009-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 Greenwalt42acef32009-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 Greenwaltb9285352009-12-21 18:24:07 -0800640 long mCreateTime;
Robert Greenwalt42acef32009-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 Greenwaltb9285352009-12-21 18:24:07 -0800649 mCreateTime = System.currentTimeMillis();
Robert Greenwalt9c75d4a2009-09-27 17:27:04 -0700650
Robert Greenwalt42acef32009-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 Savilleed9c02b2010-12-03 12:01:38 -0800663 log("ConnectivityService FeatureUser binderDied(" +
Robert Greenwaltb9285352009-12-21 18:24:07 -0800664 mNetworkType + ", " + mFeature + ", " + mBinder + "), created " +
665 (System.currentTimeMillis() - mCreateTime) + " mSec ago");
Robert Greenwalt9c75d4a2009-09-27 17:27:04 -0700666 stopUsingNetworkFeature(this, false);
Robert Greenwalt42acef32009-08-12 16:08:25 -0700667 }
668
Robert Greenwalt9c75d4a2009-09-27 17:27:04 -0700669 public void expire() {
Wink Savilleed9c02b2010-12-03 12:01:38 -0800670 log("ConnectivityService FeatureUser expire(" +
Robert Greenwaltb9285352009-12-21 18:24:07 -0800671 mNetworkType + ", " + mFeature + ", " + mBinder +"), created " +
672 (System.currentTimeMillis() - mCreateTime) + " mSec ago");
Robert Greenwalt9c75d4a2009-09-27 17:27:04 -0700673 stopUsingNetworkFeature(this, false);
674 }
Robert Greenwaltb9285352009-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 Greenwalt42acef32009-08-12 16:08:25 -0700680 }
681
Robert Greenwalt9c75d4a2009-09-27 17:27:04 -0700682 // javadoc from interface
Robert Greenwalt42acef32009-08-12 16:08:25 -0700683 public int startUsingNetworkFeature(int networkType, String feature,
684 IBinder binder) {
685 if (DBG) {
Wink Savilleed9c02b2010-12-03 12:01:38 -0800686 log("startUsingNetworkFeature for net " + networkType + ": " + feature);
Robert Greenwalt42acef32009-08-12 16:08:25 -0700687 }
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800688 enforceChangePermission();
Robert Greenwalt5154ae762009-10-30 14:17:42 -0700689 if (!ConnectivityManager.isNetworkTypeValid(networkType) ||
Robert Greenwaltd55a6b42011-03-25 13:09:25 -0700690 mNetConfigs[networkType] == null) {
Robert Greenwalt42acef32009-08-12 16:08:25 -0700691 return Phone.APN_REQUEST_FAILED;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800692 }
Robert Greenwalt42acef32009-08-12 16:08:25 -0700693
Robert Greenwalt9c75d4a2009-09-27 17:27:04 -0700694 FeatureUser f = new FeatureUser(networkType, feature, binder);
Robert Greenwalt42acef32009-08-12 16:08:25 -0700695
696 // TODO - move this into the MobileDataStateTracker
697 int usedNetworkType = networkType;
698 if(networkType == ConnectivityManager.TYPE_MOBILE) {
Wink Saville9d7d6282011-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 Greenwalt42acef32009-08-12 16:08:25 -0700703 }
704 }
Robert Greenwalt50393202011-06-21 17:26:14 -0700705
706 if (mProtectedNetworks.contains(usedNetworkType)) {
707 enforceConnectivityInternalPermission();
708 }
709
Robert Greenwalt42acef32009-08-12 16:08:25 -0700710 NetworkStateTracker network = mNetTrackers[usedNetworkType];
711 if (network != null) {
Robert Greenwalt0be1e982010-12-15 13:26:33 -0800712 Integer currentPid = new Integer(getCallingPid());
Robert Greenwalt42acef32009-08-12 16:08:25 -0700713 if (usedNetworkType != networkType) {
Robert Greenwalt42acef32009-08-12 16:08:25 -0700714 NetworkStateTracker radio = mNetTrackers[networkType];
715 NetworkInfo ni = network.getNetworkInfo();
716
717 if (ni.isAvailable() == false) {
Wink Savilleed9c02b2010-12-03 12:01:38 -0800718 if (DBG) log("special network not available");
Robert Greenwalte32e8122010-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 Greenwalt42acef32009-08-12 16:08:25 -0700724 }
725
Robert Greenwalt9c75d4a2009-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 Greenwalt42acef32009-08-12 16:08:25 -0700732 }
Robert Greenwalt9c75d4a2009-09-27 17:27:04 -0700733
Robert Greenwaltf2102f72011-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 Greenwalt42acef32009-08-12 16:08:25 -0700740
Robert Greenwalta64bf832009-08-19 20:19:33 -0700741 if ((ni.isConnectedOrConnecting() == true) &&
742 !network.isTeardownRequested()) {
Robert Greenwalt42acef32009-08-12 16:08:25 -0700743 if (ni.isConnected() == true) {
744 // add the pid-specific dns
Robert Greenwaltc76b8fa2010-07-23 15:46:26 -0700745 handleDnsConfigurationChange(networkType);
Wink Savilleed9c02b2010-12-03 12:01:38 -0800746 if (DBG) log("special network already active");
Robert Greenwalt42acef32009-08-12 16:08:25 -0700747 return Phone.APN_ALREADY_ACTIVE;
748 }
Wink Savilleed9c02b2010-12-03 12:01:38 -0800749 if (DBG) log("special network already connecting");
Robert Greenwalt42acef32009-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 Savilleed9c02b2010-12-03 12:01:38 -0800756 if (DBG) log("reconnecting to special network");
Robert Greenwalt42acef32009-08-12 16:08:25 -0700757 network.reconnect();
758 return Phone.APN_REQUEST_STARTED;
759 } else {
Robert Greenwalt0be1e982010-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 Greenwalt02648a42010-05-18 10:52:51 -0700768 return -1;
Robert Greenwalt42acef32009-08-12 16:08:25 -0700769 }
770 }
771 return Phone.APN_TYPE_NOT_AVAILABLE;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800772 }
773
Robert Greenwalt9c75d4a2009-09-27 17:27:04 -0700774 // javadoc from interface
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800775 public int stopUsingNetworkFeature(int networkType, String feature) {
Robert Greenwaltb8f16342009-10-06 17:52:40 -0700776 enforceChangePermission();
777
Robert Greenwalt9c75d4a2009-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 Savilleed9c02b2010-12-03 12:01:38 -0800800 if (DBG) log("ignoring stopUsingNetworkFeature - not a live request");
Robert Greenwalt9c75d4a2009-09-27 17:27:04 -0700801 return 1;
802 }
Robert Greenwalt42acef32009-08-12 16:08:25 -0700803 }
804
Robert Greenwalt9c75d4a2009-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 Greenwalt42acef32009-08-12 16:08:25 -0700814 if (DBG) {
Wink Savilleed9c02b2010-12-03 12:01:38 -0800815 log("stopUsingNetworkFeature for net " + networkType +
Robert Greenwalt42acef32009-08-12 16:08:25 -0700816 ": " + feature);
817 }
Robert Greenwaltb8f16342009-10-06 17:52:40 -0700818
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800819 if (!ConnectivityManager.isNetworkTypeValid(networkType)) {
820 return -1;
821 }
Robert Greenwalt42acef32009-08-12 16:08:25 -0700822
Robert Greenwalt9c75d4a2009-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 Savilleed9c02b2010-12-03 12:01:38 -0800828 if (DBG) log("ignoring - this process has no outstanding requests");
Robert Greenwalt42acef32009-08-12 16:08:25 -0700829 return 1;
830 }
Robert Greenwalt9c75d4a2009-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 Savilleed9c02b2010-12-03 12:01:38 -0800846 if (DBG) log("ignoring stopUsingNetworkFeature as dup is found");
Robert Greenwalt9c75d4a2009-09-27 17:27:04 -0700847 return 1;
848 }
849 }
Robert Greenwalt42acef32009-08-12 16:08:25 -0700850 }
Robert Greenwalt9c75d4a2009-09-27 17:27:04 -0700851
852 // TODO - move to MobileDataStateTracker
853 int usedNetworkType = networkType;
854 if (networkType == ConnectivityManager.TYPE_MOBILE) {
Wink Saville9d7d6282011-03-12 14:52:01 -0800855 usedNetworkType = convertFeatureToNetworkType(feature);
856 if (usedNetworkType < 0) {
857 usedNetworkType = networkType;
Robert Greenwalt9c75d4a2009-09-27 17:27:04 -0700858 }
859 }
860 tracker = mNetTrackers[usedNetworkType];
Robert Greenwalt5154ae762009-10-30 14:17:42 -0700861 if (tracker == null) {
Wink Savilleed9c02b2010-12-03 12:01:38 -0800862 if (DBG) log("ignoring - no known tracker for net type " + usedNetworkType);
Robert Greenwalt5154ae762009-10-30 14:17:42 -0700863 return -1;
864 }
865 if (usedNetworkType != networkType) {
Robert Greenwalt9c75d4a2009-09-27 17:27:04 -0700866 Integer currentPid = new Integer(pid);
Robert Greenwalt9c75d4a2009-09-27 17:27:04 -0700867 mNetRequestersPids[usedNetworkType].remove(currentPid);
Robert Greenwalt421c72b2009-12-17 14:54:59 -0800868 reassessPidDns(pid, true);
Robert Greenwalt9c75d4a2009-09-27 17:27:04 -0700869 if (mNetRequestersPids[usedNetworkType].size() != 0) {
Wink Savilleed9c02b2010-12-03 12:01:38 -0800870 if (DBG) log("not tearing down special network - " +
Robert Greenwalt9c75d4a2009-09-27 17:27:04 -0700871 "others still using it");
872 return 1;
873 }
874 callTeardown = true;
Robert Greenwalt19b9ab42011-01-10 11:58:31 -0800875 } else {
876 if (DBG) log("not a known feature - dropping");
Robert Greenwalt9c75d4a2009-09-27 17:27:04 -0700877 }
878 }
Wink Savilleed9c02b2010-12-03 12:01:38 -0800879 if (DBG) log("Doing network teardown");
Robert Greenwalt9c75d4a2009-09-27 17:27:04 -0700880 if (callTeardown) {
881 tracker.teardown();
Robert Greenwalt42acef32009-08-12 16:08:25 -0700882 return 1;
883 } else {
Robert Greenwalt02648a42010-05-18 10:52:51 -0700884 return -1;
Robert Greenwalt42acef32009-08-12 16:08:25 -0700885 }
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800886 }
887
888 /**
Robert Greenwalt585ac0f2010-08-27 09:24:29 -0700889 * @deprecated use requestRouteToHostAddress instead
890 *
The Android Open Source Project9066cfe2009-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 Greenwalt86e9e552009-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 Project9066cfe2009-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 Greenwalt585ac0f2010-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 Project9066cfe2009-03-03 19:31:44 -0800919 enforceChangePermission();
Robert Greenwalt50393202011-06-21 17:26:14 -0700920 if (mProtectedNetworks.contains(networkType)) {
921 enforceConnectivityInternalPermission();
922 }
923
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800924 if (!ConnectivityManager.isNetworkTypeValid(networkType)) {
925 return false;
926 }
927 NetworkStateTracker tracker = mNetTrackers[networkType];
Robert Greenwalt8206ff32009-09-10 15:06:20 -0700928
Robert Greenwalt5154ae762009-10-30 14:17:42 -0700929 if (tracker == null || !tracker.getNetworkInfo().isConnected() ||
930 tracker.isTeardownRequested()) {
Robert Greenwalt8206ff32009-09-10 15:06:20 -0700931 if (DBG) {
Wink Savilleed9c02b2010-12-03 12:01:38 -0800932 log("requestRouteToHostAddress on down network " +
Robert Greenwalt585ac0f2010-08-27 09:24:29 -0700933 "(" + networkType + ") - dropped");
Robert Greenwalt8206ff32009-09-10 15:06:20 -0700934 }
935 return false;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800936 }
Robert Greenwalt47f69fe2010-06-15 15:43:39 -0700937 try {
Robert Greenwalt585ac0f2010-08-27 09:24:29 -0700938 InetAddress addr = InetAddress.getByAddress(hostAddress);
Robert Greenwaltf43396c2011-05-06 17:10:53 -0700939 return addHostRoute(tracker, addr, 0);
Robert Greenwalt47f69fe2010-06-15 15:43:39 -0700940 } catch (UnknownHostException e) {}
941 return false;
Irfan Sheriffd649c122010-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 Greenwalt47f69fe2010-06-15 15:43:39 -0700949 * TODO - deprecate
Irfan Sheriffd649c122010-06-09 15:39:36 -0700950 * @return {@code true} on success, {@code false} on failure
951 */
Robert Greenwaltf43396c2011-05-06 17:10:53 -0700952 private boolean addHostRoute(NetworkStateTracker nt, InetAddress hostAddress, int cycleCount) {
Robert Greenwaltf43396c2011-05-06 17:10:53 -0700953 LinkProperties lp = nt.getLinkProperties();
954 if ((lp == null) || (hostAddress == null)) return false;
Irfan Sheriffd649c122010-06-09 15:39:36 -0700955
Robert Greenwaltf43396c2011-05-06 17:10:53 -0700956 String interfaceName = lp.getInterfaceName();
Irfan Sheriffd649c122010-06-09 15:39:36 -0700957 if (DBG) {
Robert Greenwaltf43396c2011-05-06 17:10:53 -0700958 log("Requested host route to " + hostAddress + "(" + interfaceName + "), cycleCount=" +
959 cycleCount);
Irfan Sheriffd649c122010-06-09 15:39:36 -0700960 }
Robert Greenwaltf43396c2011-05-06 17:10:53 -0700961 if (interfaceName == null) {
Wink Savilleed9c02b2010-12-03 12:01:38 -0800962 if (DBG) loge("addHostRoute failed due to null interface name");
Irfan Sheriffd649c122010-06-09 15:39:36 -0700963 return false;
964 }
Robert Greenwaltf43396c2011-05-06 17:10:53 -0700965
966 RouteInfo bestRoute = RouteInfo.selectBestRoute(lp.getRoutes(), hostAddress);
Robert Greenwalt59b1a4e2011-05-10 15:05:02 -0700967 InetAddress gatewayAddress = null;
Robert Greenwaltf43396c2011-05-06 17:10:53 -0700968 if (bestRoute != null) {
Robert Greenwalt59b1a4e2011-05-10 15:05:02 -0700969 gatewayAddress = bestRoute.getGateway();
Robert Greenwaltf43396c2011-05-06 17:10:53 -0700970 // if the best route is ourself, don't relf-reference, just add the host route
Robert Greenwalt59b1a4e2011-05-10 15:05:02 -0700971 if (hostAddress.equals(gatewayAddress)) gatewayAddress = null;
Robert Greenwaltf43396c2011-05-06 17:10:53 -0700972 }
Robert Greenwalt59b1a4e2011-05-10 15:05:02 -0700973 if (gatewayAddress != null) {
Robert Greenwaltf43396c2011-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 Greenwalt59b1a4e2011-05-10 15:05:02 -0700978 if (!addHostRoute(nt, gatewayAddress, cycleCount+1)) return false;
Robert Greenwaltf43396c2011-05-06 17:10:53 -0700979 }
Robert Greenwalt59b1a4e2011-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 Greenwaltf43396c2011-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 Project9066cfe2009-03-03 19:31:44 -0800995 }
996
997 /**
998 * @see ConnectivityManager#getBackgroundDataSetting()
999 */
1000 public boolean getBackgroundDataSetting() {
Robert Greenwaltd825ea42010-12-29 16:15:02 -08001001 return mBackgroundDataEnabled.get();
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001002 }
Robert Greenwalt86e9e552009-07-16 17:21:39 -07001003
The Android Open Source Project9066cfe2009-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 Greenwalt86e9e552009-07-16 17:21:39 -07001011
Robert Greenwaltd825ea42010-12-29 16:15:02 -08001012 mBackgroundDataEnabled.set(allowBackgroundDataUsage);
1013
Robert Greenwalt8dcc28b2010-09-23 10:05:56 -07001014 mHandler.sendMessage(mHandler.obtainMessage(EVENT_SET_BACKGROUND_DATA,
1015 (allowBackgroundDataUsage ? ENABLED : DISABLED), 0));
1016 }
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001017
Robert Greenwalt8dcc28b2010-09-23 10:05:56 -07001018 private void handleSetBackgroundData(boolean enabled) {
Robert Greenwaltdb4afae2011-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 Greenwalt86e9e552009-07-16 17:21:39 -07001024 }
1025
Robert Greenwaltc03fa502010-02-23 18:58:05 -08001026 /**
1027 * @see ConnectivityManager#getMobileDataEnabled()
1028 */
1029 public boolean getMobileDataEnabled() {
Wink Savillee7982682010-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 Greenwaltc03fa502010-02-23 18:58:05 -08001033 enforceAccessPermission();
1034 boolean retVal = Settings.Secure.getInt(mContext.getContentResolver(),
1035 Settings.Secure.MOBILE_DATA, 1) == 1;
Wink Savilleed9c02b2010-12-03 12:01:38 -08001036 if (DBG) log("getMobileDataEnabled returning " + retVal);
Robert Greenwaltc03fa502010-02-23 18:58:05 -08001037 return retVal;
1038 }
1039
Robert Greenwaltd55a6b42011-03-25 13:09:25 -07001040 public void setDataDependency(int networkType, boolean met) {
Robert Greenwalt50393202011-06-21 17:26:14 -07001041 enforceConnectivityInternalPermission();
1042
Robert Greenwaltd55a6b42011-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 Greenwaltc03fa502010-02-23 18:58:05 -08001059 /**
1060 * @see ConnectivityManager#setMobileDataEnabled(boolean)
1061 */
Robert Greenwalt8dcc28b2010-09-23 10:05:56 -07001062 public void setMobileDataEnabled(boolean enabled) {
Robert Greenwaltc03fa502010-02-23 18:58:05 -08001063 enforceChangePermission();
Wink Savilleed9c02b2010-12-03 12:01:38 -08001064 if (DBG) log("setMobileDataEnabled(" + enabled + ")");
Robert Greenwaltc03fa502010-02-23 18:58:05 -08001065
Robert Greenwalt8dcc28b2010-09-23 10:05:56 -07001066 mHandler.sendMessage(mHandler.obtainMessage(EVENT_SET_MOBILE_DATA,
Robert Greenwaltd55a6b42011-03-25 13:09:25 -07001067 (enabled ? ENABLED : DISABLED), 0));
Robert Greenwalt8dcc28b2010-09-23 10:05:56 -07001068 }
1069
1070 private void handleSetMobileData(boolean enabled) {
Wink Savillee7982682010-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 Greenwaltc03fa502010-02-23 18:58:05 -08001074 }
Wink Savillee7982682010-12-07 10:31:02 -08001075 mNetTrackers[ConnectivityManager.TYPE_MOBILE].setDataEnable(enabled);
Robert Greenwaltc03fa502010-02-23 18:58:05 -08001076 }
1077 }
1078
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001079 private void enforceAccessPermission() {
Robert Greenwalt86e9e552009-07-16 17:21:39 -07001080 mContext.enforceCallingOrSelfPermission(
1081 android.Manifest.permission.ACCESS_NETWORK_STATE,
1082 "ConnectivityService");
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001083 }
1084
1085 private void enforceChangePermission() {
Robert Greenwalt86e9e552009-07-16 17:21:39 -07001086 mContext.enforceCallingOrSelfPermission(
1087 android.Manifest.permission.CHANGE_NETWORK_STATE,
1088 "ConnectivityService");
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001089 }
1090
Robert Greenwaltd0e18ff2010-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 Greenwalt2a091d72010-02-11 18:18:40 -08001098 private void enforceTetherAccessPermission() {
1099 mContext.enforceCallingOrSelfPermission(
1100 android.Manifest.permission.ACCESS_NETWORK_STATE,
1101 "ConnectivityService");
1102 }
1103
Robert Greenwalt14f2ef42010-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 Project9066cfe2009-03-03 19:31:44 -08001110 /**
Robert Greenwalt86e9e552009-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 Project9066cfe2009-03-03 19:31:44 -08001115 * @param info the {@code NetworkInfo} for the network
1116 */
1117 private void handleDisconnect(NetworkInfo info) {
1118
Robert Greenwalt42acef32009-08-12 16:08:25 -07001119 int prevNetType = info.getType();
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001120
Robert Greenwalt42acef32009-08-12 16:08:25 -07001121 mNetTrackers[prevNetType].setTeardownRequested(false);
The Android Open Source Project9066cfe2009-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 Greenwaltd55a6b42011-03-25 13:09:25 -07001128 if (!mNetConfigs[prevNetType].isDefault()) {
Robert Greenwalt42acef32009-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 Project9066cfe2009-03-03 19:31:44 -08001136 }
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001137 }
1138
The Android Open Source Project9066cfe2009-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 Greenwalt86e9e552009-07-16 17:21:39 -07001149 intent.putExtra(ConnectivityManager.EXTRA_EXTRA_INFO,
1150 info.getExtraInfo());
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001151 }
Robert Greenwalt42acef32009-08-12 16:08:25 -07001152
Robert Greenwaltd55a6b42011-03-25 13:09:25 -07001153 if (mNetConfigs[prevNetType].isDefault()) {
Robert Greenwaltf21ef7d2011-01-11 13:56:33 -08001154 tryFailover(prevNetType);
1155 if (mActiveDefaultNetwork != -1) {
1156 NetworkInfo switchTo = mNetTrackers[mActiveDefaultNetwork].getNetworkInfo();
Robert Greenwaltcc4b4012010-01-25 17:54:29 -08001157 intent.putExtra(ConnectivityManager.EXTRA_OTHER_NETWORK_INFO, switchTo);
1158 } else {
Robert Greenwalt029be812010-09-20 18:01:43 -07001159 mDefaultInetConditionPublished = 0; // we're not connected anymore
Robert Greenwaltcc4b4012010-01-25 17:54:29 -08001160 intent.putExtra(ConnectivityManager.EXTRA_NO_CONNECTIVITY, true);
1161 }
Robert Greenwaltda03c4e2010-01-20 19:29:41 -08001162 }
Robert Greenwalt029be812010-09-20 18:01:43 -07001163 intent.putExtra(ConnectivityManager.EXTRA_INET_CONDITION, mDefaultInetConditionPublished);
Robert Greenwaltec896c62011-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 Greenwaltda03c4e2010-01-20 19:29:41 -08001186 // do this before we broadcast the change
Robert Greenwaltec896c62011-06-15 12:22:07 -07001187 handleConnectivityChange(prevNetType, doReset);
Robert Greenwaltda03c4e2010-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 Greenwaltf21ef7d2011-01-11 13:56:33 -08001194 if (mActiveDefaultNetwork != -1) {
1195 sendConnectedBroadcast(mNetTrackers[mActiveDefaultNetwork].getNetworkInfo());
Robert Greenwaltda03c4e2010-01-20 19:29:41 -08001196 }
1197 }
1198
Robert Greenwaltf21ef7d2011-01-11 13:56:33 -08001199 private void tryFailover(int prevNetType) {
Robert Greenwalt42acef32009-08-12 16:08:25 -07001200 /*
Robert Greenwaltbff90182011-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 Greenwalt42acef32009-08-12 16:08:25 -07001204 */
Robert Greenwaltd55a6b42011-03-25 13:09:25 -07001205 if (mNetConfigs[prevNetType].isDefault()) {
Robert Greenwalt42acef32009-08-12 16:08:25 -07001206 if (mActiveDefaultNetwork == prevNetType) {
1207 mActiveDefaultNetwork = -1;
1208 }
1209
Robert Greenwaltf21ef7d2011-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 Greenwaltd55a6b42011-03-25 13:09:25 -07001215// currentPriority = mNetConfigs[mActiveDefaultNetwork].mPriority;
Robert Greenwaltf21ef7d2011-01-11 13:56:33 -08001216// }
Robert Greenwaltda03c4e2010-01-20 19:29:41 -08001217 for (int checkType=0; checkType <= ConnectivityManager.MAX_NETWORK_TYPE; checkType++) {
Robert Greenwalt5154ae762009-10-30 14:17:42 -07001218 if (checkType == prevNetType) continue;
Robert Greenwaltd55a6b42011-03-25 13:09:25 -07001219 if (mNetConfigs[checkType] == null) continue;
1220 if (!mNetConfigs[checkType].isDefault()) continue;
Wink Saville9f7a0b22011-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 Greenwaltd55a6b42011-03-25 13:09:25 -07001232// if (currentPriority >= mNetConfigs[checkType].mPriority) continue;
Robert Greenwalt42acef32009-08-12 16:08:25 -07001233
Robert Greenwaltf21ef7d2011-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 Greenwalt42acef32009-08-12 16:08:25 -07001239 }
Robert Greenwaltf21ef7d2011-01-11 13:56:33 -08001240 if (DBG) log("Attempting to switch to " + checkInfo.getTypeName());
Robert Greenwalt42acef32009-08-12 16:08:25 -07001241 }
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001242 }
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001243 }
1244
1245 private void sendConnectedBroadcast(NetworkInfo info) {
Robert Greenwalt1e9aac22010-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 Project9066cfe2009-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 Greenwalt86e9e552009-07-16 17:21:39 -07001264 intent.putExtra(ConnectivityManager.EXTRA_EXTRA_INFO,
1265 info.getExtraInfo());
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001266 }
Robert Greenwaltd7085fc2010-09-08 15:24:47 -07001267 intent.putExtra(ConnectivityManager.EXTRA_INET_CONDITION, mDefaultInetConditionPublished);
Mike Lockwood0f79b542009-08-14 14:18:49 -04001268 sendStickyBroadcast(intent);
The Android Open Source Project9066cfe2009-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 Project9066cfe2009-03-03 19:31:44 -08001277
Robert Greenwalt42acef32009-08-12 16:08:25 -07001278 String reason = info.getReason();
1279 String extraInfo = info.getExtraInfo();
Robert Greenwalt86e9e552009-07-16 17:21:39 -07001280
Robert Greenwalt572172b2010-10-08 16:35:52 -07001281 String reasonText;
1282 if (reason == null) {
1283 reasonText = ".";
1284 } else {
1285 reasonText = " (" + reason + ").";
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001286 }
Wink Savilleed9c02b2010-12-03 12:01:38 -08001287 loge("Attempt to connect to " + info.getTypeName() + " failed" + reasonText);
Robert Greenwalt42acef32009-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 Greenwaltda03c4e2010-01-20 19:29:41 -08001304
Robert Greenwaltd55a6b42011-03-25 13:09:25 -07001305 if (mNetConfigs[info.getType()].isDefault()) {
Robert Greenwaltf21ef7d2011-01-11 13:56:33 -08001306 tryFailover(info.getType());
1307 if (mActiveDefaultNetwork != -1) {
1308 NetworkInfo switchTo = mNetTrackers[mActiveDefaultNetwork].getNetworkInfo();
Robert Greenwaltcc4b4012010-01-25 17:54:29 -08001309 intent.putExtra(ConnectivityManager.EXTRA_OTHER_NETWORK_INFO, switchTo);
1310 } else {
Robert Greenwalt029be812010-09-20 18:01:43 -07001311 mDefaultInetConditionPublished = 0;
Robert Greenwaltcc4b4012010-01-25 17:54:29 -08001312 intent.putExtra(ConnectivityManager.EXTRA_NO_CONNECTIVITY, true);
1313 }
Robert Greenwaltda03c4e2010-01-20 19:29:41 -08001314 }
Robert Greenwaltcc4b4012010-01-25 17:54:29 -08001315
Robert Greenwalt029be812010-09-20 18:01:43 -07001316 intent.putExtra(ConnectivityManager.EXTRA_INET_CONDITION, mDefaultInetConditionPublished);
Mike Lockwood0f79b542009-08-14 14:18:49 -04001317 sendStickyBroadcast(intent);
Robert Greenwaltda03c4e2010-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 Greenwaltf21ef7d2011-01-11 13:56:33 -08001322 if (mActiveDefaultNetwork != -1) {
1323 sendConnectedBroadcast(mNetTrackers[mActiveDefaultNetwork].getNetworkInfo());
Robert Greenwaltda03c4e2010-01-20 19:29:41 -08001324 }
Mike Lockwood0f79b542009-08-14 14:18:49 -04001325 }
1326
1327 private void sendStickyBroadcast(Intent intent) {
1328 synchronized(this) {
Dianne Hackborn1c633fc2009-12-08 19:45:14 -08001329 if (!mSystemReady) {
1330 mInitialBroadcast = new Intent(intent);
Mike Lockwood0f79b542009-08-14 14:18:49 -04001331 }
Dianne Hackborn1c633fc2009-12-08 19:45:14 -08001332 intent.addFlags(Intent.FLAG_RECEIVER_REGISTERED_ONLY_BEFORE_BOOT);
1333 mContext.sendStickyBroadcast(intent);
Mike Lockwood0f79b542009-08-14 14:18:49 -04001334 }
1335 }
1336
1337 void systemReady() {
Robert Greenwalt59b1a4e2011-05-10 15:05:02 -07001338 IBinder b = ServiceManager.getService(Context.NETWORKMANAGEMENT_SERVICE);
1339 mNetd = INetworkManagementService.Stub.asInterface(b);
1340
Mike Lockwood0f79b542009-08-14 14:18:49 -04001341 synchronized(this) {
1342 mSystemReady = true;
Dianne Hackborn1c633fc2009-12-08 19:45:14 -08001343 if (mInitialBroadcast != null) {
1344 mContext.sendStickyBroadcast(mInitialBroadcast);
1345 mInitialBroadcast = null;
Mike Lockwood0f79b542009-08-14 14:18:49 -04001346 }
1347 }
Robert Greenwalt434203a2010-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 Project9066cfe2009-03-03 19:31:44 -08001350 }
1351
1352 private void handleConnect(NetworkInfo info) {
Robert Greenwalt42acef32009-08-12 16:08:25 -07001353 int type = info.getType();
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001354
1355 // snapshot isFailover, because sendConnectedBroadcast() resets it
1356 boolean isFailover = info.isFailover();
Robert Greenwalt42acef32009-08-12 16:08:25 -07001357 NetworkStateTracker thisNet = mNetTrackers[type];
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001358
Robert Greenwalt42acef32009-08-12 16:08:25 -07001359 // if this is a default net and other default is running
1360 // kill the one not preferred
Robert Greenwaltd55a6b42011-03-25 13:09:25 -07001361 if (mNetConfigs[type].isDefault()) {
Robert Greenwalt42acef32009-08-12 16:08:25 -07001362 if (mActiveDefaultNetwork != -1 && mActiveDefaultNetwork != type) {
1363 if ((type != mNetworkPreference &&
Wink Saville975c8482011-04-07 14:23:45 -07001364 mNetConfigs[mActiveDefaultNetwork].priority >
1365 mNetConfigs[type].priority) ||
Robert Greenwalt42acef32009-08-12 16:08:25 -07001366 mNetworkPreference == mActiveDefaultNetwork) {
1367 // don't accept this one
Wink Savilleed9c02b2010-12-03 12:01:38 -08001368 if (DBG) {
1369 log("Not broadcasting CONNECT_ACTION " +
Robert Greenwalt42acef32009-08-12 16:08:25 -07001370 "to torn down network " + info.getTypeName());
Wink Savilleed9c02b2010-12-03 12:01:38 -08001371 }
Robert Greenwalt42acef32009-08-12 16:08:25 -07001372 teardown(thisNet);
1373 return;
1374 } else {
1375 // tear down the other
1376 NetworkStateTracker otherNet =
1377 mNetTrackers[mActiveDefaultNetwork];
Wink Savilleed9c02b2010-12-03 12:01:38 -08001378 if (DBG) {
1379 log("Policy requires " + otherNet.getNetworkInfo().getTypeName() +
Robert Greenwalt42acef32009-08-12 16:08:25 -07001380 " teardown");
Wink Savilleed9c02b2010-12-03 12:01:38 -08001381 }
Robert Greenwalt42acef32009-08-12 16:08:25 -07001382 if (!teardown(otherNet)) {
Wink Savilleed9c02b2010-12-03 12:01:38 -08001383 loge("Network declined teardown request");
Robert Greenwalt42acef32009-08-12 16:08:25 -07001384 return;
1385 }
Robert Greenwalt14f2ef42010-06-15 12:19:37 -07001386 }
1387 }
1388 synchronized (ConnectivityService.this) {
1389 // have a new default network, release the transition wakelock in a second
1390 // if it's held. The second pause is to allow apps to reconnect over the
1391 // new network
1392 if (mNetTransitionWakeLock.isHeld()) {
1393 mHandler.sendMessageDelayed(mHandler.obtainMessage(
Robert Greenwaltf3331232010-09-24 14:32:21 -07001394 EVENT_CLEAR_NET_TRANSITION_WAKELOCK,
Robert Greenwalt14f2ef42010-06-15 12:19:37 -07001395 mNetTransitionWakeLockSerialNumber, 0),
1396 1000);
Robert Greenwalt42acef32009-08-12 16:08:25 -07001397 }
1398 }
1399 mActiveDefaultNetwork = type;
Robert Greenwaltd7085fc2010-09-08 15:24:47 -07001400 // this will cause us to come up initially as unconnected and switching
1401 // to connected after our normal pause unless somebody reports us as reall
1402 // disconnected
1403 mDefaultInetConditionPublished = 0;
1404 mDefaultConnectionSequence++;
1405 mInetConditionChangeInFlight = false;
1406 // Don't do this - if we never sign in stay, grey
1407 //reportNetworkCondition(mActiveDefaultNetwork, 100);
Robert Greenwalt42acef32009-08-12 16:08:25 -07001408 }
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001409 thisNet.setTeardownRequested(false);
Irfan Sheriffd649c122010-06-09 15:39:36 -07001410 updateNetworkSettings(thisNet);
Robert Greenwaltec896c62011-06-15 12:22:07 -07001411 handleConnectivityChange(type, false);
Robert Greenwalt42acef32009-08-12 16:08:25 -07001412 sendConnectedBroadcast(info);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001413 }
1414
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001415 /**
Robert Greenwaltc76b8fa2010-07-23 15:46:26 -07001416 * After a change in the connectivity state of a network. We're mainly
1417 * concerned with making sure that the list of DNS servers is set up
1418 * according to which networks are connected, and ensuring that the
1419 * right routing table entries exist.
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001420 */
Robert Greenwaltec896c62011-06-15 12:22:07 -07001421 private void handleConnectivityChange(int netType, boolean doReset) {
Wink Savillee8222252011-07-13 13:44:13 -07001422 int resetMask = doReset ? NetworkUtils.RESET_ALL_ADDRESSES : 0;
1423
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001424 /*
Robert Greenwalt42acef32009-08-12 16:08:25 -07001425 * If a non-default network is enabled, add the host routes that
Robert Greenwaltc76b8fa2010-07-23 15:46:26 -07001426 * will allow it's DNS servers to be accessed.
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001427 */
Robert Greenwaltc76b8fa2010-07-23 15:46:26 -07001428 handleDnsConfigurationChange(netType);
Robert Greenwalt42acef32009-08-12 16:08:25 -07001429
Robert Greenwaltc76b8fa2010-07-23 15:46:26 -07001430 if (mNetTrackers[netType].getNetworkInfo().isConnected()) {
Wink Savillee8222252011-07-13 13:44:13 -07001431 LinkProperties newLp = mNetTrackers[netType].getLinkProperties();
1432 LinkProperties curLp = mCurrentLinkProperties[netType];
1433 mCurrentLinkProperties[netType] = newLp;
1434 if (VDBG) {
1435 log("handleConnectivityChange: changed linkProperty[" + netType + "]:" +
1436 " doReset=" + doReset + " resetMask=" + resetMask +
1437 "\n curLp=" + curLp +
1438 "\n newLp=" + newLp);
1439 }
1440
1441 if (curLp.isIdenticalInterfaceName(newLp)) {
1442 CompareAddressesResult car = curLp.compareAddresses(newLp);
1443 if ((car.removed.size() != 0) || (car.added.size() != 0)) {
1444 for (LinkAddress linkAddr : car.removed) {
1445 if (linkAddr.getAddress() instanceof Inet4Address) {
1446 resetMask |= NetworkUtils.RESET_IPV4_ADDRESSES;
1447 }
1448 if (linkAddr.getAddress() instanceof Inet6Address) {
1449 resetMask |= NetworkUtils.RESET_IPV6_ADDRESSES;
1450 }
1451 }
1452 if (DBG) {
1453 log("handleConnectivityChange: addresses changed" +
1454 " linkProperty[" + netType + "]:" + " resetMask=" + resetMask +
1455 "\n car=" + car);
1456 }
1457 } else {
1458 if (DBG) {
1459 log("handleConnectivityChange: address are the same reset per doReset" +
1460 " linkProperty[" + netType + "]:" +
1461 " resetMask=" + resetMask);
1462 }
1463 }
1464 } else {
1465 resetMask = NetworkUtils.RESET_ALL_ADDRESSES;
1466 log("handleConnectivityChange: interface not not equivalent reset both" +
1467 " linkProperty[" + netType + "]:" +
1468 " resetMask=" + resetMask);
1469 }
Robert Greenwaltd55a6b42011-03-25 13:09:25 -07001470 if (mNetConfigs[netType].isDefault()) {
Robert Greenwalt434203a2010-10-11 16:00:27 -07001471 handleApplyDefaultProxy(netType);
Robert Greenwaltc76b8fa2010-07-23 15:46:26 -07001472 addDefaultRoute(mNetTrackers[netType]);
Robert Greenwalt42acef32009-08-12 16:08:25 -07001473 } else {
Robert Greenwaltc76b8fa2010-07-23 15:46:26 -07001474 addPrivateDnsRoutes(mNetTrackers[netType]);
1475 }
1476 } else {
Robert Greenwaltd55a6b42011-03-25 13:09:25 -07001477 if (mNetConfigs[netType].isDefault()) {
Robert Greenwaltc76b8fa2010-07-23 15:46:26 -07001478 removeDefaultRoute(mNetTrackers[netType]);
1479 } else {
1480 removePrivateDnsRoutes(mNetTrackers[netType]);
Robert Greenwalt42acef32009-08-12 16:08:25 -07001481 }
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001482 }
Robert Greenwaltec896c62011-06-15 12:22:07 -07001483
Wink Savillee8222252011-07-13 13:44:13 -07001484 if (doReset || resetMask != 0) {
Robert Greenwaltec896c62011-06-15 12:22:07 -07001485 LinkProperties linkProperties = mNetTrackers[netType].getLinkProperties();
1486 if (linkProperties != null) {
1487 String iface = linkProperties.getInterfaceName();
1488 if (TextUtils.isEmpty(iface) == false) {
Wink Savillee8222252011-07-13 13:44:13 -07001489 if (DBG) log("resetConnections(" + iface + ", " + resetMask + ")");
1490 NetworkUtils.resetConnections(iface, resetMask);
Robert Greenwaltec896c62011-06-15 12:22:07 -07001491 }
1492 }
1493 }
Kazuhiro Ondo1ccb10e2011-06-22 21:10:34 -05001494
1495 // TODO: Temporary notifying upstread change to Tethering.
1496 // @see bug/4455071
1497 /** Notify TetheringService if interface name has been changed. */
1498 if (TextUtils.equals(mNetTrackers[netType].getNetworkInfo().getReason(),
1499 Phone.REASON_LINK_PROPERTIES_CHANGED)) {
1500 if (isTetheringSupported()) {
1501 mTethering.handleTetherIfaceChange();
1502 }
1503 }
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001504 }
1505
Irfan Sheriffd649c122010-06-09 15:39:36 -07001506 private void addPrivateDnsRoutes(NetworkStateTracker nt) {
Irfan Sheriffd649c122010-06-09 15:39:36 -07001507 boolean privateDnsRouteSet = nt.isPrivateDnsRouteSet();
Robert Greenwalt37e65eb2010-08-30 10:56:47 -07001508 LinkProperties p = nt.getLinkProperties();
Robert Greenwalt47f69fe2010-06-15 15:43:39 -07001509 if (p == null) return;
1510 String interfaceName = p.getInterfaceName();
Irfan Sheriffd649c122010-06-09 15:39:36 -07001511
1512 if (DBG) {
Wink Savilleed9c02b2010-12-03 12:01:38 -08001513 log("addPrivateDnsRoutes for " + nt +
Irfan Sheriffd649c122010-06-09 15:39:36 -07001514 "(" + interfaceName + ") - mPrivateDnsRouteSet = " + privateDnsRouteSet);
1515 }
Irfan Sheriffd649c122010-06-09 15:39:36 -07001516 if (interfaceName != null && !privateDnsRouteSet) {
Robert Greenwalt47f69fe2010-06-15 15:43:39 -07001517 Collection<InetAddress> dnsList = p.getDnses();
1518 for (InetAddress dns : dnsList) {
Robert Greenwaltf43396c2011-05-06 17:10:53 -07001519 addHostRoute(nt, dns, 0);
Irfan Sheriffd649c122010-06-09 15:39:36 -07001520 }
1521 nt.privateDnsRouteSet(true);
1522 }
1523 }
1524
1525 private void removePrivateDnsRoutes(NetworkStateTracker nt) {
Robert Greenwalt37e65eb2010-08-30 10:56:47 -07001526 LinkProperties p = nt.getLinkProperties();
Robert Greenwalt47f69fe2010-06-15 15:43:39 -07001527 if (p == null) return;
1528 String interfaceName = p.getInterfaceName();
Irfan Sheriffd649c122010-06-09 15:39:36 -07001529 boolean privateDnsRouteSet = nt.isPrivateDnsRouteSet();
1530 if (interfaceName != null && privateDnsRouteSet) {
1531 if (DBG) {
Wink Savilleed9c02b2010-12-03 12:01:38 -08001532 log("removePrivateDnsRoutes for " + nt.getNetworkInfo().getTypeName() +
Irfan Sheriffd649c122010-06-09 15:39:36 -07001533 " (" + interfaceName + ")");
1534 }
Robert Greenwalt59b1a4e2011-05-10 15:05:02 -07001535
1536 Collection<InetAddress> dnsList = p.getDnses();
1537 for (InetAddress dns : dnsList) {
1538 if (DBG) log(" removing " + dns);
1539 RouteInfo route = RouteInfo.makeHostRoute(dns);
1540 try {
1541 mNetd.removeRoute(interfaceName, route);
1542 } catch (Exception ex) {
1543 loge("error (" + ex + ") removing dns route " + route);
1544 }
1545 }
Irfan Sheriffd649c122010-06-09 15:39:36 -07001546 nt.privateDnsRouteSet(false);
1547 }
1548 }
1549
Irfan Sheriffd649c122010-06-09 15:39:36 -07001550
1551 private void addDefaultRoute(NetworkStateTracker nt) {
Robert Greenwalt37e65eb2010-08-30 10:56:47 -07001552 LinkProperties p = nt.getLinkProperties();
Robert Greenwalt47f69fe2010-06-15 15:43:39 -07001553 if (p == null) return;
1554 String interfaceName = p.getInterfaceName();
Robert Greenwalt992564e2011-02-09 13:56:06 -08001555 if (TextUtils.isEmpty(interfaceName)) return;
Irfan Sheriffd649c122010-06-09 15:39:36 -07001556
Robert Greenwalt59b1a4e2011-05-10 15:05:02 -07001557 for (RouteInfo route : p.getRoutes()) {
Robert Greenwaltaa70f102011-04-28 14:28:50 -07001558 //TODO - handle non-default routes
1559 if (route.isDefaultRoute()) {
Robert Greenwalt59b1a4e2011-05-10 15:05:02 -07001560 if (DBG) log("adding default route " + route);
Robert Greenwaltaa70f102011-04-28 14:28:50 -07001561 InetAddress gateway = route.getGateway();
Robert Greenwalt59b1a4e2011-05-10 15:05:02 -07001562 if (addHostRoute(nt, gateway, 0)) {
1563 try {
1564 mNetd.addRoute(interfaceName, route);
1565 } catch (Exception e) {
1566 loge("error adding default route " + route);
1567 continue;
1568 }
Robert Greenwaltaa70f102011-04-28 14:28:50 -07001569 if (DBG) {
1570 NetworkInfo networkInfo = nt.getNetworkInfo();
1571 log("addDefaultRoute for " + networkInfo.getTypeName() +
1572 " (" + interfaceName + "), GatewayAddr=" +
1573 gateway.getHostAddress());
1574 }
Robert Greenwalt59b1a4e2011-05-10 15:05:02 -07001575 } else {
1576 loge("error adding host route for default route " + route);
Robert Greenwaltedcb4f92011-03-22 18:47:42 -07001577 }
Irfan Sheriffd649c122010-06-09 15:39:36 -07001578 }
Irfan Sheriffd649c122010-06-09 15:39:36 -07001579 }
1580 }
1581
1582
1583 public void removeDefaultRoute(NetworkStateTracker nt) {
Robert Greenwalt37e65eb2010-08-30 10:56:47 -07001584 LinkProperties p = nt.getLinkProperties();
Robert Greenwalt47f69fe2010-06-15 15:43:39 -07001585 if (p == null) return;
1586 String interfaceName = p.getInterfaceName();
Irfan Sheriffd649c122010-06-09 15:39:36 -07001587
Robert Greenwalt59b1a4e2011-05-10 15:05:02 -07001588 if (interfaceName == null) return;
1589
1590 for (RouteInfo route : p.getRoutes()) {
1591 //TODO - handle non-default routes
1592 if (route.isDefaultRoute()) {
1593 try {
1594 mNetd.removeRoute(interfaceName, route);
1595 } catch (Exception ex) {
1596 loge("error (" + ex + ") removing default route " + route);
1597 continue;
1598 }
Robert Greenwaltedcb4f92011-03-22 18:47:42 -07001599 if (DBG) {
1600 NetworkInfo networkInfo = nt.getNetworkInfo();
1601 log("removeDefaultRoute for " + networkInfo.getTypeName() + " (" +
1602 interfaceName + ")");
1603 }
Irfan Sheriffd649c122010-06-09 15:39:36 -07001604 }
Irfan Sheriffd649c122010-06-09 15:39:36 -07001605 }
1606 }
1607
1608 /**
1609 * Reads the network specific TCP buffer sizes from SystemProperties
1610 * net.tcp.buffersize.[default|wifi|umts|edge|gprs] and set them for system
1611 * wide use
1612 */
1613 public void updateNetworkSettings(NetworkStateTracker nt) {
1614 String key = nt.getTcpBufferSizesPropName();
1615 String bufferSizes = SystemProperties.get(key);
1616
1617 if (bufferSizes.length() == 0) {
Wink Savilleed9c02b2010-12-03 12:01:38 -08001618 loge(key + " not found in system properties. Using defaults");
Irfan Sheriffd649c122010-06-09 15:39:36 -07001619
1620 // Setting to default values so we won't be stuck to previous values
1621 key = "net.tcp.buffersize.default";
1622 bufferSizes = SystemProperties.get(key);
1623 }
1624
1625 // Set values in kernel
1626 if (bufferSizes.length() != 0) {
1627 if (DBG) {
Wink Savilleed9c02b2010-12-03 12:01:38 -08001628 log("Setting TCP values: [" + bufferSizes
Irfan Sheriffd649c122010-06-09 15:39:36 -07001629 + "] which comes from [" + key + "]");
1630 }
1631 setBufferSize(bufferSizes);
1632 }
1633 }
1634
1635 /**
1636 * Writes TCP buffer sizes to /sys/kernel/ipv4/tcp_[r/w]mem_[min/def/max]
1637 * which maps to /proc/sys/net/ipv4/tcp_rmem and tcpwmem
1638 *
1639 * @param bufferSizes in the format of "readMin, readInitial, readMax,
1640 * writeMin, writeInitial, writeMax"
1641 */
1642 private void setBufferSize(String bufferSizes) {
1643 try {
1644 String[] values = bufferSizes.split(",");
1645
1646 if (values.length == 6) {
1647 final String prefix = "/sys/kernel/ipv4/tcp_";
1648 stringToFile(prefix + "rmem_min", values[0]);
1649 stringToFile(prefix + "rmem_def", values[1]);
1650 stringToFile(prefix + "rmem_max", values[2]);
1651 stringToFile(prefix + "wmem_min", values[3]);
1652 stringToFile(prefix + "wmem_def", values[4]);
1653 stringToFile(prefix + "wmem_max", values[5]);
1654 } else {
Wink Savilleed9c02b2010-12-03 12:01:38 -08001655 loge("Invalid buffersize string: " + bufferSizes);
Irfan Sheriffd649c122010-06-09 15:39:36 -07001656 }
1657 } catch (IOException e) {
Wink Savilleed9c02b2010-12-03 12:01:38 -08001658 loge("Can't set tcp buffer sizes:" + e);
Irfan Sheriffd649c122010-06-09 15:39:36 -07001659 }
1660 }
1661
1662 /**
1663 * Writes string to file. Basically same as "echo -n $string > $filename"
1664 *
1665 * @param filename
1666 * @param string
1667 * @throws IOException
1668 */
1669 private void stringToFile(String filename, String string) throws IOException {
1670 FileWriter out = new FileWriter(filename);
1671 try {
1672 out.write(string);
1673 } finally {
1674 out.close();
1675 }
1676 }
1677
1678
Robert Greenwalt42acef32009-08-12 16:08:25 -07001679 /**
1680 * Adjust the per-process dns entries (net.dns<x>.<pid>) based
1681 * on the highest priority active net which this process requested.
1682 * If there aren't any, clear it out
1683 */
1684 private void reassessPidDns(int myPid, boolean doBump)
1685 {
Wink Savilleed9c02b2010-12-03 12:01:38 -08001686 if (DBG) log("reassessPidDns for pid " + myPid);
Robert Greenwalt42acef32009-08-12 16:08:25 -07001687 for(int i : mPriorityList) {
Robert Greenwaltd55a6b42011-03-25 13:09:25 -07001688 if (mNetConfigs[i].isDefault()) {
Robert Greenwalt42acef32009-08-12 16:08:25 -07001689 continue;
1690 }
1691 NetworkStateTracker nt = mNetTrackers[i];
Robert Greenwalt86e9e552009-07-16 17:21:39 -07001692 if (nt.getNetworkInfo().isConnected() &&
1693 !nt.isTeardownRequested()) {
Robert Greenwalt37e65eb2010-08-30 10:56:47 -07001694 LinkProperties p = nt.getLinkProperties();
Robert Greenwalt47f69fe2010-06-15 15:43:39 -07001695 if (p == null) continue;
Robert Greenwalt42acef32009-08-12 16:08:25 -07001696 List pids = mNetRequestersPids[i];
1697 for (int j=0; j<pids.size(); j++) {
1698 Integer pid = (Integer)pids.get(j);
1699 if (pid.intValue() == myPid) {
Robert Greenwalt47f69fe2010-06-15 15:43:39 -07001700 Collection<InetAddress> dnses = p.getDnses();
1701 writePidDns(dnses, myPid);
Robert Greenwalt42acef32009-08-12 16:08:25 -07001702 if (doBump) {
1703 bumpDns();
1704 }
1705 return;
1706 }
1707 }
1708 }
1709 }
1710 // nothing found - delete
1711 for (int i = 1; ; i++) {
1712 String prop = "net.dns" + i + "." + myPid;
1713 if (SystemProperties.get(prop).length() == 0) {
1714 if (doBump) {
1715 bumpDns();
1716 }
1717 return;
1718 }
1719 SystemProperties.set(prop, "");
1720 }
1721 }
1722
Robert Greenwalt10398722010-12-17 15:20:36 -08001723 // return true if results in a change
1724 private boolean writePidDns(Collection <InetAddress> dnses, int pid) {
Robert Greenwalt42acef32009-08-12 16:08:25 -07001725 int j = 1;
Robert Greenwalt10398722010-12-17 15:20:36 -08001726 boolean changed = false;
Robert Greenwalt47f69fe2010-06-15 15:43:39 -07001727 for (InetAddress dns : dnses) {
Robert Greenwalt10398722010-12-17 15:20:36 -08001728 String dnsString = dns.getHostAddress();
1729 if (changed || !dnsString.equals(SystemProperties.get("net.dns" + j + "." + pid))) {
1730 changed = true;
1731 SystemProperties.set("net.dns" + j++ + "." + pid, dns.getHostAddress());
1732 }
Robert Greenwalt42acef32009-08-12 16:08:25 -07001733 }
Robert Greenwalt10398722010-12-17 15:20:36 -08001734 return changed;
Robert Greenwalt42acef32009-08-12 16:08:25 -07001735 }
1736
1737 private void bumpDns() {
1738 /*
1739 * Bump the property that tells the name resolver library to reread
1740 * the DNS server list from the properties.
1741 */
1742 String propVal = SystemProperties.get("net.dnschange");
1743 int n = 0;
1744 if (propVal.length() != 0) {
1745 try {
1746 n = Integer.parseInt(propVal);
1747 } catch (NumberFormatException e) {}
1748 }
1749 SystemProperties.set("net.dnschange", "" + (n+1));
Robert Greenwalt03595d02010-11-02 14:08:23 -07001750 /*
1751 * Tell the VMs to toss their DNS caches
1752 */
1753 Intent intent = new Intent(Intent.ACTION_CLEAR_DNS_CACHE);
1754 intent.addFlags(Intent.FLAG_RECEIVER_REPLACE_PENDING);
Stan Chesnutt3d1db862011-01-05 17:14:03 -08001755 /*
1756 * Connectivity events can happen before boot has completed ...
1757 */
1758 intent.addFlags(Intent.FLAG_RECEIVER_REGISTERED_ONLY_BEFORE_BOOT);
Robert Greenwalt03595d02010-11-02 14:08:23 -07001759 mContext.sendBroadcast(intent);
Robert Greenwalt42acef32009-08-12 16:08:25 -07001760 }
1761
Robert Greenwaltc76b8fa2010-07-23 15:46:26 -07001762 private void handleDnsConfigurationChange(int netType) {
Robert Greenwalt42acef32009-08-12 16:08:25 -07001763 // add default net's dns entries
Robert Greenwaltc76b8fa2010-07-23 15:46:26 -07001764 NetworkStateTracker nt = mNetTrackers[netType];
1765 if (nt != null && nt.getNetworkInfo().isConnected() && !nt.isTeardownRequested()) {
Robert Greenwalt37e65eb2010-08-30 10:56:47 -07001766 LinkProperties p = nt.getLinkProperties();
Robert Greenwaltc76b8fa2010-07-23 15:46:26 -07001767 if (p == null) return;
1768 Collection<InetAddress> dnses = p.getDnses();
Robert Greenwalt10398722010-12-17 15:20:36 -08001769 boolean changed = false;
Robert Greenwaltd55a6b42011-03-25 13:09:25 -07001770 if (mNetConfigs[netType].isDefault()) {
Robert Greenwaltc76b8fa2010-07-23 15:46:26 -07001771 int j = 1;
Robert Greenwalte90aa5e2010-09-01 11:34:05 -07001772 if (dnses.size() == 0 && mDefaultDns != null) {
Robert Greenwalt10398722010-12-17 15:20:36 -08001773 String dnsString = mDefaultDns.getHostAddress();
1774 if (!dnsString.equals(SystemProperties.get("net.dns1"))) {
1775 if (DBG) {
1776 log("no dns provided - using " + dnsString);
1777 }
1778 changed = true;
1779 SystemProperties.set("net.dns1", dnsString);
Robert Greenwalt42acef32009-08-12 16:08:25 -07001780 }
Robert Greenwalte90aa5e2010-09-01 11:34:05 -07001781 j++;
1782 } else {
1783 for (InetAddress dns : dnses) {
Robert Greenwalt10398722010-12-17 15:20:36 -08001784 String dnsString = dns.getHostAddress();
1785 if (!changed && dnsString.equals(SystemProperties.get("net.dns" + j))) {
1786 j++;
1787 continue;
1788 }
Robert Greenwalte90aa5e2010-09-01 11:34:05 -07001789 if (DBG) {
Wink Savilleed9c02b2010-12-03 12:01:38 -08001790 log("adding dns " + dns + " for " +
Robert Greenwalte90aa5e2010-09-01 11:34:05 -07001791 nt.getNetworkInfo().getTypeName());
1792 }
Robert Greenwalt10398722010-12-17 15:20:36 -08001793 changed = true;
1794 SystemProperties.set("net.dns" + j++, dnsString);
Robert Greenwalte90aa5e2010-09-01 11:34:05 -07001795 }
Robert Greenwaltc76b8fa2010-07-23 15:46:26 -07001796 }
1797 for (int k=j ; k<mNumDnsEntries; k++) {
Robert Greenwalt10398722010-12-17 15:20:36 -08001798 if (changed || !TextUtils.isEmpty(SystemProperties.get("net.dns" + k))) {
1799 if (DBG) log("erasing net.dns" + k);
1800 changed = true;
1801 SystemProperties.set("net.dns" + k, "");
1802 }
Robert Greenwaltc76b8fa2010-07-23 15:46:26 -07001803 }
1804 mNumDnsEntries = j;
1805 } else {
1806 // set per-pid dns for attached secondary nets
1807 List pids = mNetRequestersPids[netType];
1808 for (int y=0; y< pids.size(); y++) {
1809 Integer pid = (Integer)pids.get(y);
Robert Greenwalt10398722010-12-17 15:20:36 -08001810 changed = writePidDns(dnses, pid.intValue());
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001811 }
1812 }
Robert Greenwalt10398722010-12-17 15:20:36 -08001813 if (changed) bumpDns();
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001814 }
Robert Greenwalt42acef32009-08-12 16:08:25 -07001815 }
1816
Robert Greenwaltf2102f72011-05-03 19:02:44 -07001817 private int getRestoreDefaultNetworkDelay(int networkType) {
Robert Greenwalt42acef32009-08-12 16:08:25 -07001818 String restoreDefaultNetworkDelayStr = SystemProperties.get(
1819 NETWORK_RESTORE_DELAY_PROP_NAME);
1820 if(restoreDefaultNetworkDelayStr != null &&
1821 restoreDefaultNetworkDelayStr.length() != 0) {
1822 try {
1823 return Integer.valueOf(restoreDefaultNetworkDelayStr);
1824 } catch (NumberFormatException e) {
1825 }
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001826 }
Robert Greenwaltf2102f72011-05-03 19:02:44 -07001827 // if the system property isn't set, use the value for the apn type
1828 int ret = RESTORE_DEFAULT_NETWORK_DELAY;
1829
1830 if ((networkType <= ConnectivityManager.MAX_NETWORK_TYPE) &&
1831 (mNetConfigs[networkType] != null)) {
1832 ret = mNetConfigs[networkType].restoreTime;
1833 }
1834 return ret;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001835 }
1836
1837 @Override
1838 protected void dump(FileDescriptor fd, PrintWriter pw, String[] args) {
Robert Greenwalt86e9e552009-07-16 17:21:39 -07001839 if (mContext.checkCallingOrSelfPermission(
1840 android.Manifest.permission.DUMP)
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001841 != PackageManager.PERMISSION_GRANTED) {
Robert Greenwalt86e9e552009-07-16 17:21:39 -07001842 pw.println("Permission Denial: can't dump ConnectivityService " +
1843 "from from pid=" + Binder.getCallingPid() + ", uid=" +
1844 Binder.getCallingUid());
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001845 return;
1846 }
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001847 pw.println();
1848 for (NetworkStateTracker nst : mNetTrackers) {
Robert Greenwaltb9285352009-12-21 18:24:07 -08001849 if (nst != null) {
1850 if (nst.getNetworkInfo().isConnected()) {
1851 pw.println("Active network: " + nst.getNetworkInfo().
1852 getTypeName());
1853 }
1854 pw.println(nst.getNetworkInfo());
1855 pw.println(nst);
1856 pw.println();
Robert Greenwalt42acef32009-08-12 16:08:25 -07001857 }
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001858 }
Robert Greenwaltb9285352009-12-21 18:24:07 -08001859
1860 pw.println("Network Requester Pids:");
1861 for (int net : mPriorityList) {
1862 String pidString = net + ": ";
1863 for (Object pid : mNetRequestersPids[net]) {
1864 pidString = pidString + pid.toString() + ", ";
1865 }
1866 pw.println(pidString);
1867 }
1868 pw.println();
1869
1870 pw.println("FeatureUsers:");
1871 for (Object requester : mFeatureUsers) {
1872 pw.println(requester.toString());
1873 }
1874 pw.println();
Robert Greenwalt2a091d72010-02-11 18:18:40 -08001875
Robert Greenwalt14f2ef42010-06-15 12:19:37 -07001876 synchronized (this) {
1877 pw.println("NetworkTranstionWakeLock is currently " +
1878 (mNetTransitionWakeLock.isHeld() ? "" : "not ") + "held.");
1879 pw.println("It was last requested for "+mNetTransitionWakeLockCausedBy);
1880 }
1881 pw.println();
1882
Robert Greenwalt2a091d72010-02-11 18:18:40 -08001883 mTethering.dump(fd, pw, args);
Robert Greenwalt4e8dfef2010-09-20 14:35:25 -07001884
1885 if (mInetLog != null) {
1886 pw.println();
1887 pw.println("Inet condition reports:");
1888 for(int i = 0; i < mInetLog.size(); i++) {
1889 pw.println(mInetLog.get(i));
1890 }
1891 }
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001892 }
1893
Robert Greenwalt42acef32009-08-12 16:08:25 -07001894 // must be stateless - things change under us.
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001895 private class MyHandler extends Handler {
Wink Savillebb08caf2010-09-02 19:23:52 -07001896 public MyHandler(Looper looper) {
1897 super(looper);
1898 }
1899
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001900 @Override
1901 public void handleMessage(Message msg) {
1902 NetworkInfo info;
1903 switch (msg.what) {
1904 case NetworkStateTracker.EVENT_STATE_CHANGED:
1905 info = (NetworkInfo) msg.obj;
Robert Greenwalt511288a2009-12-07 11:33:18 -08001906 int type = info.getType();
1907 NetworkInfo.State state = info.getState();
Robert Greenwalt511288a2009-12-07 11:33:18 -08001908
Wink Savilleed9c02b2010-12-03 12:01:38 -08001909 if (DBG) log("ConnectivityChange for " +
Robert Greenwalt86e9e552009-07-16 17:21:39 -07001910 info.getTypeName() + ": " +
Robert Greenwalt511288a2009-12-07 11:33:18 -08001911 state + "/" + info.getDetailedState());
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001912
1913 // Connectivity state changed:
1914 // [31-13] Reserved for future use
Robert Greenwalt86e9e552009-07-16 17:21:39 -07001915 // [12-9] Network subtype (for mobile network, as defined
1916 // by TelephonyManager)
1917 // [8-3] Detailed state ordinal (as defined by
1918 // NetworkInfo.DetailedState)
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001919 // [2-0] Network type (as defined by ConnectivityManager)
1920 int eventLogParam = (info.getType() & 0x7) |
1921 ((info.getDetailedState().ordinal() & 0x3f) << 3) |
1922 (info.getSubtype() << 9);
Doug Zongkerab5c49c2009-12-04 10:31:43 -08001923 EventLog.writeEvent(EventLogTags.CONNECTIVITY_STATE_CHANGED,
Robert Greenwalt86e9e552009-07-16 17:21:39 -07001924 eventLogParam);
1925
1926 if (info.getDetailedState() ==
1927 NetworkInfo.DetailedState.FAILED) {
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001928 handleConnectionFailure(info);
Robert Greenwalt511288a2009-12-07 11:33:18 -08001929 } else if (state == NetworkInfo.State.DISCONNECTED) {
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001930 handleDisconnect(info);
Robert Greenwalt511288a2009-12-07 11:33:18 -08001931 } else if (state == NetworkInfo.State.SUSPENDED) {
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001932 // TODO: need to think this over.
Robert Greenwalt86e9e552009-07-16 17:21:39 -07001933 // the logic here is, handle SUSPENDED the same as
1934 // DISCONNECTED. The only difference being we are
1935 // broadcasting an intent with NetworkInfo that's
1936 // suspended. This allows the applications an
1937 // opportunity to handle DISCONNECTED and SUSPENDED
1938 // differently, or not.
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001939 handleDisconnect(info);
Robert Greenwalt511288a2009-12-07 11:33:18 -08001940 } else if (state == NetworkInfo.State.CONNECTED) {
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001941 handleConnect(info);
1942 }
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001943 break;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001944 case NetworkStateTracker.EVENT_CONFIGURATION_CHANGED:
Robert Greenwaltc76b8fa2010-07-23 15:46:26 -07001945 info = (NetworkInfo) msg.obj;
Kazuhiro Ondo1ccb10e2011-06-22 21:10:34 -05001946 // TODO: Temporary allowing network configuration
1947 // change not resetting sockets.
1948 // @see bug/4455071
1949 handleConnectivityChange(info.getType(), false);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001950 break;
Robert Greenwaltf3331232010-09-24 14:32:21 -07001951 case EVENT_CLEAR_NET_TRANSITION_WAKELOCK:
Robert Greenwalt14f2ef42010-06-15 12:19:37 -07001952 String causedBy = null;
1953 synchronized (ConnectivityService.this) {
1954 if (msg.arg1 == mNetTransitionWakeLockSerialNumber &&
1955 mNetTransitionWakeLock.isHeld()) {
1956 mNetTransitionWakeLock.release();
1957 causedBy = mNetTransitionWakeLockCausedBy;
1958 }
1959 }
1960 if (causedBy != null) {
Wink Savilleed9c02b2010-12-03 12:01:38 -08001961 log("NetTransition Wakelock for " + causedBy + " released by timeout");
Robert Greenwalt14f2ef42010-06-15 12:19:37 -07001962 }
Robert Greenwalt057d5e92010-09-09 14:05:10 -07001963 break;
Robert Greenwalt8dcc28b2010-09-23 10:05:56 -07001964 case EVENT_RESTORE_DEFAULT_NETWORK:
Robert Greenwalt9c75d4a2009-09-27 17:27:04 -07001965 FeatureUser u = (FeatureUser)msg.obj;
1966 u.expire();
Robert Greenwaltd7085fc2010-09-08 15:24:47 -07001967 break;
Robert Greenwalt8dcc28b2010-09-23 10:05:56 -07001968 case EVENT_INET_CONDITION_CHANGE:
1969 {
1970 int netType = msg.arg1;
1971 int condition = msg.arg2;
1972 handleInetConditionChange(netType, condition);
Robert Greenwalt14f2ef42010-06-15 12:19:37 -07001973 break;
Robert Greenwalt8dcc28b2010-09-23 10:05:56 -07001974 }
1975 case EVENT_INET_CONDITION_HOLD_END:
1976 {
1977 int netType = msg.arg1;
1978 int sequence = msg.arg2;
1979 handleInetConditionHoldEnd(netType, sequence);
Robert Greenwaltd7085fc2010-09-08 15:24:47 -07001980 break;
Robert Greenwalt8dcc28b2010-09-23 10:05:56 -07001981 }
1982 case EVENT_SET_NETWORK_PREFERENCE:
1983 {
1984 int preference = msg.arg1;
1985 handleSetNetworkPreference(preference);
1986 break;
1987 }
1988 case EVENT_SET_BACKGROUND_DATA:
1989 {
1990 boolean enabled = (msg.arg1 == ENABLED);
1991 handleSetBackgroundData(enabled);
1992 break;
1993 }
1994 case EVENT_SET_MOBILE_DATA:
1995 {
1996 boolean enabled = (msg.arg1 == ENABLED);
1997 handleSetMobileData(enabled);
1998 break;
1999 }
Robert Greenwalt434203a2010-10-11 16:00:27 -07002000 case EVENT_APPLY_GLOBAL_HTTP_PROXY:
2001 {
2002 handleDeprecatedGlobalHttpProxy();
Robert Greenwaltd55a6b42011-03-25 13:09:25 -07002003 break;
2004 }
2005 case EVENT_SET_DEPENDENCY_MET:
2006 {
2007 boolean met = (msg.arg1 == ENABLED);
2008 handleSetDependencyMet(msg.arg2, met);
2009 break;
Robert Greenwalt434203a2010-10-11 16:00:27 -07002010 }
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002011 }
2012 }
2013 }
Robert Greenwaltd0e18ff2010-01-26 11:40:34 -08002014
2015 // javadoc from interface
Robert Greenwalt5a735062010-03-02 17:25:02 -08002016 public int tether(String iface) {
Robert Greenwaltd0e18ff2010-01-26 11:40:34 -08002017 enforceTetherChangePermission();
Robert Greenwalt5a735062010-03-02 17:25:02 -08002018
2019 if (isTetheringSupported()) {
2020 return mTethering.tether(iface);
2021 } else {
2022 return ConnectivityManager.TETHER_ERROR_UNSUPPORTED;
2023 }
Robert Greenwaltd0e18ff2010-01-26 11:40:34 -08002024 }
2025
2026 // javadoc from interface
Robert Greenwalt5a735062010-03-02 17:25:02 -08002027 public int untether(String iface) {
Robert Greenwaltd0e18ff2010-01-26 11:40:34 -08002028 enforceTetherChangePermission();
Robert Greenwalt5a735062010-03-02 17:25:02 -08002029
2030 if (isTetheringSupported()) {
2031 return mTethering.untether(iface);
2032 } else {
2033 return ConnectivityManager.TETHER_ERROR_UNSUPPORTED;
2034 }
2035 }
2036
2037 // javadoc from interface
2038 public int getLastTetherError(String iface) {
2039 enforceTetherAccessPermission();
2040
2041 if (isTetheringSupported()) {
2042 return mTethering.getLastTetherError(iface);
2043 } else {
2044 return ConnectivityManager.TETHER_ERROR_UNSUPPORTED;
2045 }
Robert Greenwalt2a091d72010-02-11 18:18:40 -08002046 }
2047
2048 // TODO - proper iface API for selection by property, inspection, etc
2049 public String[] getTetherableUsbRegexs() {
2050 enforceTetherAccessPermission();
2051 if (isTetheringSupported()) {
2052 return mTethering.getTetherableUsbRegexs();
2053 } else {
2054 return new String[0];
2055 }
2056 }
2057
2058 public String[] getTetherableWifiRegexs() {
2059 enforceTetherAccessPermission();
2060 if (isTetheringSupported()) {
2061 return mTethering.getTetherableWifiRegexs();
2062 } else {
2063 return new String[0];
2064 }
Robert Greenwaltd0e18ff2010-01-26 11:40:34 -08002065 }
2066
Danica Chang6fdd0c62010-08-11 14:54:43 -07002067 public String[] getTetherableBluetoothRegexs() {
2068 enforceTetherAccessPermission();
2069 if (isTetheringSupported()) {
2070 return mTethering.getTetherableBluetoothRegexs();
2071 } else {
2072 return new String[0];
2073 }
2074 }
2075
Robert Greenwaltd0e18ff2010-01-26 11:40:34 -08002076 // TODO - move iface listing, queries, etc to new module
2077 // javadoc from interface
2078 public String[] getTetherableIfaces() {
Robert Greenwalt2a091d72010-02-11 18:18:40 -08002079 enforceTetherAccessPermission();
Robert Greenwaltd0e18ff2010-01-26 11:40:34 -08002080 return mTethering.getTetherableIfaces();
2081 }
2082
2083 public String[] getTetheredIfaces() {
Robert Greenwalt2a091d72010-02-11 18:18:40 -08002084 enforceTetherAccessPermission();
Robert Greenwaltd0e18ff2010-01-26 11:40:34 -08002085 return mTethering.getTetheredIfaces();
2086 }
Robert Greenwalt2a091d72010-02-11 18:18:40 -08002087
Robert Greenwalt5a735062010-03-02 17:25:02 -08002088 public String[] getTetheringErroredIfaces() {
2089 enforceTetherAccessPermission();
2090 return mTethering.getErroredIfaces();
2091 }
2092
Robert Greenwalt2a091d72010-02-11 18:18:40 -08002093 // if ro.tether.denied = true we default to no tethering
2094 // gservices could set the secure setting to 1 though to enable it on a build where it
2095 // had previously been turned off.
2096 public boolean isTetheringSupported() {
2097 enforceTetherAccessPermission();
2098 int defaultVal = (SystemProperties.get("ro.tether.denied").equals("true") ? 0 : 1);
Robert Greenwaltc9d5fb72010-02-25 12:29:30 -08002099 boolean tetherEnabledInSettings = (Settings.Secure.getInt(mContext.getContentResolver(),
2100 Settings.Secure.TETHER_SUPPORTED, defaultVal) != 0);
2101 return tetherEnabledInSettings && mTetheringConfigValid;
Robert Greenwalt2a091d72010-02-11 18:18:40 -08002102 }
Robert Greenwalt14f2ef42010-06-15 12:19:37 -07002103
2104 // An API NetworkStateTrackers can call when they lose their network.
2105 // This will automatically be cleared after X seconds or a network becomes CONNECTED,
2106 // whichever happens first. The timer is started by the first caller and not
2107 // restarted by subsequent callers.
2108 public void requestNetworkTransitionWakelock(String forWhom) {
2109 enforceConnectivityInternalPermission();
2110 synchronized (this) {
2111 if (mNetTransitionWakeLock.isHeld()) return;
2112 mNetTransitionWakeLockSerialNumber++;
2113 mNetTransitionWakeLock.acquire();
2114 mNetTransitionWakeLockCausedBy = forWhom;
2115 }
2116 mHandler.sendMessageDelayed(mHandler.obtainMessage(
Robert Greenwaltf3331232010-09-24 14:32:21 -07002117 EVENT_CLEAR_NET_TRANSITION_WAKELOCK,
Robert Greenwalt14f2ef42010-06-15 12:19:37 -07002118 mNetTransitionWakeLockSerialNumber, 0),
2119 mNetTransitionWakeLockTimeout);
2120 return;
2121 }
Robert Greenwaltca4306c2010-09-09 13:15:32 -07002122
Robert Greenwaltd7085fc2010-09-08 15:24:47 -07002123 // 100 percent is full good, 0 is full bad.
2124 public void reportInetCondition(int networkType, int percentage) {
Wink Savilleed9c02b2010-12-03 12:01:38 -08002125 if (DBG) log("reportNetworkCondition(" + networkType + ", " + percentage + ")");
Robert Greenwaltd7085fc2010-09-08 15:24:47 -07002126 mContext.enforceCallingOrSelfPermission(
2127 android.Manifest.permission.STATUS_BAR,
2128 "ConnectivityService");
2129
Robert Greenwalt4e8dfef2010-09-20 14:35:25 -07002130 if (DBG) {
2131 int pid = getCallingPid();
2132 int uid = getCallingUid();
2133 String s = pid + "(" + uid + ") reports inet is " +
2134 (percentage > 50 ? "connected" : "disconnected") + " (" + percentage + ") on " +
2135 "network Type " + networkType + " at " + GregorianCalendar.getInstance().getTime();
2136 mInetLog.add(s);
2137 while(mInetLog.size() > INET_CONDITION_LOG_MAX_SIZE) {
2138 mInetLog.remove(0);
2139 }
2140 }
Robert Greenwaltd7085fc2010-09-08 15:24:47 -07002141 mHandler.sendMessage(mHandler.obtainMessage(
Robert Greenwalt8dcc28b2010-09-23 10:05:56 -07002142 EVENT_INET_CONDITION_CHANGE, networkType, percentage));
2143 }
2144
2145 private void handleInetConditionChange(int netType, int condition) {
2146 if (DBG) {
Wink Savilleed9c02b2010-12-03 12:01:38 -08002147 log("Inet connectivity change, net=" +
Robert Greenwalt8dcc28b2010-09-23 10:05:56 -07002148 netType + ", condition=" + condition +
2149 ",mActiveDefaultNetwork=" + mActiveDefaultNetwork);
2150 }
2151 if (mActiveDefaultNetwork == -1) {
Wink Savilleed9c02b2010-12-03 12:01:38 -08002152 if (DBG) log("no active default network - aborting");
Robert Greenwalt8dcc28b2010-09-23 10:05:56 -07002153 return;
2154 }
2155 if (mActiveDefaultNetwork != netType) {
Wink Savilleed9c02b2010-12-03 12:01:38 -08002156 if (DBG) log("given net not default - aborting");
Robert Greenwalt8dcc28b2010-09-23 10:05:56 -07002157 return;
2158 }
2159 mDefaultInetCondition = condition;
2160 int delay;
2161 if (mInetConditionChangeInFlight == false) {
Wink Savilleed9c02b2010-12-03 12:01:38 -08002162 if (DBG) log("starting a change hold");
Robert Greenwalt8dcc28b2010-09-23 10:05:56 -07002163 // setup a new hold to debounce this
2164 if (mDefaultInetCondition > 50) {
2165 delay = Settings.Secure.getInt(mContext.getContentResolver(),
2166 Settings.Secure.INET_CONDITION_DEBOUNCE_UP_DELAY, 500);
2167 } else {
2168 delay = Settings.Secure.getInt(mContext.getContentResolver(),
2169 Settings.Secure.INET_CONDITION_DEBOUNCE_DOWN_DELAY, 3000);
2170 }
2171 mInetConditionChangeInFlight = true;
2172 mHandler.sendMessageDelayed(mHandler.obtainMessage(EVENT_INET_CONDITION_HOLD_END,
2173 mActiveDefaultNetwork, mDefaultConnectionSequence), delay);
2174 } else {
2175 // we've set the new condition, when this hold ends that will get
2176 // picked up
Wink Savilleed9c02b2010-12-03 12:01:38 -08002177 if (DBG) log("currently in hold - not setting new end evt");
Robert Greenwalt8dcc28b2010-09-23 10:05:56 -07002178 }
2179 }
2180
2181 private void handleInetConditionHoldEnd(int netType, int sequence) {
2182 if (DBG) {
Wink Savilleed9c02b2010-12-03 12:01:38 -08002183 log("Inet hold end, net=" + netType +
Robert Greenwalt8dcc28b2010-09-23 10:05:56 -07002184 ", condition =" + mDefaultInetCondition +
2185 ", published condition =" + mDefaultInetConditionPublished);
2186 }
2187 mInetConditionChangeInFlight = false;
2188
2189 if (mActiveDefaultNetwork == -1) {
Wink Savilleed9c02b2010-12-03 12:01:38 -08002190 if (DBG) log("no active default network - aborting");
Robert Greenwalt8dcc28b2010-09-23 10:05:56 -07002191 return;
2192 }
2193 if (mDefaultConnectionSequence != sequence) {
Wink Savilleed9c02b2010-12-03 12:01:38 -08002194 if (DBG) log("event hold for obsolete network - aborting");
Robert Greenwalt8dcc28b2010-09-23 10:05:56 -07002195 return;
2196 }
2197 if (mDefaultInetConditionPublished == mDefaultInetCondition) {
Wink Savilleed9c02b2010-12-03 12:01:38 -08002198 if (DBG) log("no change in condition - aborting");
Robert Greenwalt8dcc28b2010-09-23 10:05:56 -07002199 return;
2200 }
2201 NetworkInfo networkInfo = mNetTrackers[mActiveDefaultNetwork].getNetworkInfo();
2202 if (networkInfo.isConnected() == false) {
Wink Savilleed9c02b2010-12-03 12:01:38 -08002203 if (DBG) log("default network not connected - aborting");
Robert Greenwalt8dcc28b2010-09-23 10:05:56 -07002204 return;
2205 }
2206 mDefaultInetConditionPublished = mDefaultInetCondition;
2207 sendInetConditionBroadcast(networkInfo);
2208 return;
Robert Greenwaltd7085fc2010-09-08 15:24:47 -07002209 }
Robert Greenwalt434203a2010-10-11 16:00:27 -07002210
2211 public synchronized ProxyProperties getProxy() {
2212 if (mGlobalProxy != null) return mGlobalProxy;
2213 if (mDefaultProxy != null) return mDefaultProxy;
2214 return null;
2215 }
2216
2217 public void setGlobalProxy(ProxyProperties proxyProperties) {
2218 enforceChangePermission();
2219 synchronized (mGlobalProxyLock) {
2220 if (proxyProperties == mGlobalProxy) return;
2221 if (proxyProperties != null && proxyProperties.equals(mGlobalProxy)) return;
2222 if (mGlobalProxy != null && mGlobalProxy.equals(proxyProperties)) return;
2223
2224 String host = "";
2225 int port = 0;
2226 String exclList = "";
2227 if (proxyProperties != null && !TextUtils.isEmpty(proxyProperties.getHost())) {
2228 mGlobalProxy = new ProxyProperties(proxyProperties);
2229 host = mGlobalProxy.getHost();
2230 port = mGlobalProxy.getPort();
2231 exclList = mGlobalProxy.getExclusionList();
2232 } else {
2233 mGlobalProxy = null;
2234 }
2235 ContentResolver res = mContext.getContentResolver();
2236 Settings.Secure.putString(res, Settings.Secure.GLOBAL_HTTP_PROXY_HOST, host);
2237 Settings.Secure.putInt(res, Settings.Secure.GLOBAL_HTTP_PROXY_PORT, port);
Robert Greenwaltb7090d62010-12-02 11:31:00 -08002238 Settings.Secure.putString(res, Settings.Secure.GLOBAL_HTTP_PROXY_EXCLUSION_LIST,
Robert Greenwalt434203a2010-10-11 16:00:27 -07002239 exclList);
2240 }
2241
2242 if (mGlobalProxy == null) {
2243 proxyProperties = mDefaultProxy;
2244 }
2245 sendProxyBroadcast(proxyProperties);
2246 }
2247
Robert Greenwaltb7090d62010-12-02 11:31:00 -08002248 private void loadGlobalProxy() {
2249 ContentResolver res = mContext.getContentResolver();
2250 String host = Settings.Secure.getString(res, Settings.Secure.GLOBAL_HTTP_PROXY_HOST);
2251 int port = Settings.Secure.getInt(res, Settings.Secure.GLOBAL_HTTP_PROXY_PORT, 0);
2252 String exclList = Settings.Secure.getString(res,
2253 Settings.Secure.GLOBAL_HTTP_PROXY_EXCLUSION_LIST);
2254 if (!TextUtils.isEmpty(host)) {
2255 ProxyProperties proxyProperties = new ProxyProperties(host, port, exclList);
2256 synchronized (mGlobalProxyLock) {
2257 mGlobalProxy = proxyProperties;
2258 }
2259 }
2260 }
2261
Robert Greenwalt434203a2010-10-11 16:00:27 -07002262 public ProxyProperties getGlobalProxy() {
2263 synchronized (mGlobalProxyLock) {
2264 return mGlobalProxy;
2265 }
2266 }
2267
2268 private void handleApplyDefaultProxy(int type) {
2269 // check if new default - push it out to all VM if so
2270 ProxyProperties proxy = mNetTrackers[type].getLinkProperties().getHttpProxy();
2271 synchronized (this) {
2272 if (mDefaultProxy != null && mDefaultProxy.equals(proxy)) return;
2273 if (mDefaultProxy == proxy) return;
2274 if (!TextUtils.isEmpty(proxy.getHost())) {
2275 mDefaultProxy = proxy;
2276 } else {
2277 mDefaultProxy = null;
2278 }
2279 }
Wink Savilleed9c02b2010-12-03 12:01:38 -08002280 if (DBG) log("changing default proxy to " + proxy);
Robert Greenwalt434203a2010-10-11 16:00:27 -07002281 if ((proxy == null && mGlobalProxy == null) || proxy.equals(mGlobalProxy)) return;
2282 if (mGlobalProxy != null) return;
2283 sendProxyBroadcast(proxy);
2284 }
2285
2286 private void handleDeprecatedGlobalHttpProxy() {
2287 String proxy = Settings.Secure.getString(mContext.getContentResolver(),
2288 Settings.Secure.HTTP_PROXY);
2289 if (!TextUtils.isEmpty(proxy)) {
2290 String data[] = proxy.split(":");
2291 String proxyHost = data[0];
2292 int proxyPort = 8080;
2293 if (data.length > 1) {
2294 try {
2295 proxyPort = Integer.parseInt(data[1]);
2296 } catch (NumberFormatException e) {
2297 return;
2298 }
2299 }
2300 ProxyProperties p = new ProxyProperties(data[0], proxyPort, "");
2301 setGlobalProxy(p);
2302 }
2303 }
2304
2305 private void sendProxyBroadcast(ProxyProperties proxy) {
Robert Greenwalt55985be2010-12-23 15:51:10 -08002306 if (proxy == null) proxy = new ProxyProperties("", 0, "");
Wink Savilleed9c02b2010-12-03 12:01:38 -08002307 log("sending Proxy Broadcast for " + proxy);
Robert Greenwalt434203a2010-10-11 16:00:27 -07002308 Intent intent = new Intent(Proxy.PROXY_CHANGE_ACTION);
Stan Chesnuttb35d67a2011-01-06 11:00:19 -08002309 intent.addFlags(Intent.FLAG_RECEIVER_REPLACE_PENDING |
2310 Intent.FLAG_RECEIVER_REGISTERED_ONLY_BEFORE_BOOT);
Robert Greenwalt434203a2010-10-11 16:00:27 -07002311 intent.putExtra(Proxy.EXTRA_PROXY_INFO, proxy);
Robert Greenwalta2e13392010-12-06 11:29:17 -08002312 mContext.sendStickyBroadcast(intent);
Robert Greenwalt434203a2010-10-11 16:00:27 -07002313 }
2314
2315 private static class SettingsObserver extends ContentObserver {
2316 private int mWhat;
2317 private Handler mHandler;
2318 SettingsObserver(Handler handler, int what) {
2319 super(handler);
2320 mHandler = handler;
2321 mWhat = what;
2322 }
2323
2324 void observe(Context context) {
2325 ContentResolver resolver = context.getContentResolver();
2326 resolver.registerContentObserver(Settings.Secure.getUriFor(
2327 Settings.Secure.HTTP_PROXY), false, this);
2328 }
2329
2330 @Override
2331 public void onChange(boolean selfChange) {
2332 mHandler.obtainMessage(mWhat).sendToTarget();
2333 }
2334 }
Wink Savilleed9c02b2010-12-03 12:01:38 -08002335
2336 private void log(String s) {
2337 Slog.d(TAG, s);
2338 }
2339
2340 private void loge(String s) {
2341 Slog.e(TAG, s);
2342 }
Wink Saville9d7d6282011-03-12 14:52:01 -08002343 int convertFeatureToNetworkType(String feature){
2344 int networkType = -1;
2345 if (TextUtils.equals(feature, Phone.FEATURE_ENABLE_MMS)) {
2346 networkType = ConnectivityManager.TYPE_MOBILE_MMS;
2347 } else if (TextUtils.equals(feature, Phone.FEATURE_ENABLE_SUPL)) {
2348 networkType = ConnectivityManager.TYPE_MOBILE_SUPL;
2349 } else if (TextUtils.equals(feature, Phone.FEATURE_ENABLE_DUN) ||
2350 TextUtils.equals(feature, Phone.FEATURE_ENABLE_DUN_ALWAYS)) {
2351 networkType = ConnectivityManager.TYPE_MOBILE_DUN;
2352 } else if (TextUtils.equals(feature, Phone.FEATURE_ENABLE_HIPRI)) {
2353 networkType = ConnectivityManager.TYPE_MOBILE_HIPRI;
2354 } else if (TextUtils.equals(feature, Phone.FEATURE_ENABLE_FOTA)) {
2355 networkType = ConnectivityManager.TYPE_MOBILE_FOTA;
2356 } else if (TextUtils.equals(feature, Phone.FEATURE_ENABLE_IMS)) {
2357 networkType = ConnectivityManager.TYPE_MOBILE_IMS;
2358 } else if (TextUtils.equals(feature, Phone.FEATURE_ENABLE_CBS)) {
2359 networkType = ConnectivityManager.TYPE_MOBILE_CBS;
2360 }
2361 return networkType;
2362 }
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002363}