blob: 02532653d648cbf6e26d9984a49167bf0e3a9a8f [file] [log] [blame]
The Android Open Source Project28527d22009-03-03 19:31:44 -08001/*
2 * Copyright (C) 2008 The Android Open Source Project
3 *
4 * Licensed under the Apache License, Version 2.0 (the "License");
5 * you may not use this file except in compliance with the License.
6 * You may obtain a copy of the License at
7 *
8 * http://www.apache.org/licenses/LICENSE-2.0
9 *
10 * Unless required by applicable law or agreed to in writing, software
11 * distributed under the License is distributed on an "AS IS" BASIS,
12 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13 * See the License for the specific language governing permissions and
14 * limitations under the License.
15 */
16
17package com.android.server;
18
Jaikumar Ganesh0db51a02010-12-21 22:31:44 -080019import android.bluetooth.BluetoothTetheringDataTracker;
The Android Open Source Project28527d22009-03-03 19:31:44 -080020import android.content.ContentResolver;
21import android.content.Context;
22import android.content.Intent;
23import android.content.pm.PackageManager;
Robert Greenwaltc3c5f862010-10-11 16:00:27 -070024import android.database.ContentObserver;
The Android Open Source Project28527d22009-03-03 19:31:44 -080025import android.net.ConnectivityManager;
Robert Greenwalteb123ac2010-12-06 13:56:24 -080026import android.net.DummyDataStateTracker;
Benoit Goby6cec7f32010-12-22 14:29:40 -080027import android.net.EthernetDataTracker;
The Android Open Source Project28527d22009-03-03 19:31:44 -080028import android.net.IConnectivityManager;
Robert Greenwalt355205c2011-05-10 15:05:02 -070029import android.net.LinkAddress;
Jaikumar Ganesh0db51a02010-12-21 22:31:44 -080030import android.net.LinkProperties;
The Android Open Source Project28527d22009-03-03 19:31:44 -080031import android.net.MobileDataStateTracker;
Robert Greenwalt34848c02011-03-25 13:09:25 -070032import android.net.NetworkConfig;
The Android Open Source Project28527d22009-03-03 19:31:44 -080033import android.net.NetworkInfo;
34import android.net.NetworkStateTracker;
Robert Greenwalt7fe44cb2010-08-27 09:24:29 -070035import android.net.NetworkUtils;
Robert Greenwaltc3c5f862010-10-11 16:00:27 -070036import android.net.Proxy;
37import android.net.ProxyProperties;
Robert Greenwalt5a901292011-04-28 14:28:50 -070038import android.net.RouteInfo;
Hung-ying Tyan4e723422011-01-19 16:48:38 +080039import android.net.vpn.VpnManager;
The Android Open Source Project28527d22009-03-03 19:31:44 -080040import android.net.wifi.WifiStateTracker;
41import android.os.Binder;
42import android.os.Handler;
Wink Saville775aad62010-09-02 19:23:52 -070043import android.os.HandlerThread;
Robert Greenwalt2034b912009-08-12 16:08:25 -070044import android.os.IBinder;
Robert Greenwalt355205c2011-05-10 15:05:02 -070045import android.os.INetworkManagementService;
The Android Open Source Project28527d22009-03-03 19:31:44 -080046import android.os.Looper;
47import android.os.Message;
Robert Greenwalt93dc1042010-06-15 12:19:37 -070048import android.os.PowerManager;
Robert Greenwalt2034b912009-08-12 16:08:25 -070049import android.os.RemoteException;
The Android Open Source Project28527d22009-03-03 19:31:44 -080050import android.os.ServiceManager;
51import android.os.SystemProperties;
52import android.provider.Settings;
Robert Greenwalt2034b912009-08-12 16:08:25 -070053import android.text.TextUtils;
The Android Open Source Project28527d22009-03-03 19:31:44 -080054import android.util.EventLog;
Joe Onoratoc2386bb2010-02-26 18:56:32 -080055import android.util.Slog;
The Android Open Source Project28527d22009-03-03 19:31:44 -080056
Robert Greenwalt2034b912009-08-12 16:08:25 -070057import com.android.internal.telephony.Phone;
Robert Greenwalt0c4828c2010-01-26 11:40:34 -080058import com.android.server.connectivity.Tethering;
59
The Android Open Source Project28527d22009-03-03 19:31:44 -080060import java.io.FileDescriptor;
Irfan Sheriff7f132d92010-06-09 15:39:36 -070061import java.io.FileWriter;
62import java.io.IOException;
The Android Open Source Project28527d22009-03-03 19:31:44 -080063import java.io.PrintWriter;
Robert Greenwalta7dfbd32010-06-15 15:43:39 -070064import java.net.InetAddress;
Robert Greenwalt355205c2011-05-10 15:05:02 -070065import java.net.Inet4Address;
Robert Greenwalta7dfbd32010-06-15 15:43:39 -070066import java.net.UnknownHostException;
Robert Greenwalt2034b912009-08-12 16:08:25 -070067import java.util.ArrayList;
Robert Greenwalta7dfbd32010-06-15 15:43:39 -070068import java.util.Collection;
Robert Greenwaltd62c7002010-12-29 16:15:02 -080069import java.util.concurrent.atomic.AtomicBoolean;
Robert Greenwalt0e80be12010-09-20 14:35:25 -070070import java.util.GregorianCalendar;
Robert Greenwalt2034b912009-08-12 16:08:25 -070071import java.util.List;
The Android Open Source Project28527d22009-03-03 19:31:44 -080072
73/**
74 * @hide
75 */
76public class ConnectivityService extends IConnectivityManager.Stub {
77
Robert Greenwalt063dc7d2010-10-05 19:12:26 -070078 private static final boolean DBG = true;
The Android Open Source Project28527d22009-03-03 19:31:44 -080079 private static final String TAG = "ConnectivityService";
80
Robert Greenwalt2034b912009-08-12 16:08:25 -070081 // how long to wait before switching back to a radio's default network
82 private static final int RESTORE_DEFAULT_NETWORK_DELAY = 1 * 60 * 1000;
83 // system property that can override the above value
84 private static final String NETWORK_RESTORE_DELAY_PROP_NAME =
85 "android.telephony.apn-restore";
86
Robert Greenwaltbd492212011-05-06 17:10:53 -070087 // used in recursive route setting to add gateways for the host for which
88 // a host route was requested.
89 private static final int MAX_HOSTROUTE_CYCLE_COUNT = 10;
90
Robert Greenwalt0c4828c2010-01-26 11:40:34 -080091 private Tethering mTethering;
Robert Greenwaltf1b66e12010-02-25 12:29:30 -080092 private boolean mTetheringConfigValid = false;
Robert Greenwalt0c4828c2010-01-26 11:40:34 -080093
The Android Open Source Project28527d22009-03-03 19:31:44 -080094 /**
95 * Sometimes we want to refer to the individual network state
96 * trackers separately, and sometimes we just want to treat them
97 * abstractly.
98 */
99 private NetworkStateTracker mNetTrackers[];
Robert Greenwalt2034b912009-08-12 16:08:25 -0700100
101 /**
102 * A per Net list of the PID's that requested access to the net
103 * used both as a refcount and for per-PID DNS selection
104 */
105 private List mNetRequestersPids[];
106
Irfan Sheriff653e2a22010-06-07 09:03:04 -0700107 private WifiWatchdogService mWifiWatchdogService;
108
Robert Greenwalt2034b912009-08-12 16:08:25 -0700109 // priority order of the nettrackers
110 // (excluding dynamically set mNetworkPreference)
111 // TODO - move mNetworkTypePreference into this
112 private int[] mPriorityList;
113
The Android Open Source Project28527d22009-03-03 19:31:44 -0800114 private Context mContext;
115 private int mNetworkPreference;
Robert Greenwalt2034b912009-08-12 16:08:25 -0700116 private int mActiveDefaultNetwork = -1;
Robert Greenwalt986c7412010-09-08 15:24:47 -0700117 // 0 is full bad, 100 is full good
118 private int mDefaultInetCondition = 0;
119 private int mDefaultInetConditionPublished = 0;
120 private boolean mInetConditionChangeInFlight = false;
121 private int mDefaultConnectionSequence = 0;
The Android Open Source Project28527d22009-03-03 19:31:44 -0800122
123 private int mNumDnsEntries;
The Android Open Source Project28527d22009-03-03 19:31:44 -0800124
125 private boolean mTestMode;
Joe Onorato56023ad2010-09-01 21:18:22 -0700126 private static ConnectivityService sServiceInstance;
The Android Open Source Project28527d22009-03-03 19:31:44 -0800127
Robert Greenwaltd62c7002010-12-29 16:15:02 -0800128 private AtomicBoolean mBackgroundDataEnabled = new AtomicBoolean(true);
129
Robert Greenwalt355205c2011-05-10 15:05:02 -0700130 private INetworkManagementService mNetd;
131
Robert Greenwalt6a2db8a2010-09-23 10:05:56 -0700132 private static final int ENABLED = 1;
133 private static final int DISABLED = 0;
134
135 // Share the event space with NetworkStateTracker (which can't see this
136 // internal class but sends us events). If you change these, change
137 // NetworkStateTracker.java too.
138 private static final int MIN_NETWORK_STATE_TRACKER_EVENT = 1;
139 private static final int MAX_NETWORK_STATE_TRACKER_EVENT = 100;
140
141 /**
142 * used internally as a delayed event to make us switch back to the
143 * default network
144 */
145 private static final int EVENT_RESTORE_DEFAULT_NETWORK =
146 MAX_NETWORK_STATE_TRACKER_EVENT + 1;
147
148 /**
149 * used internally to change our mobile data enabled flag
150 */
151 private static final int EVENT_CHANGE_MOBILE_DATA_ENABLED =
152 MAX_NETWORK_STATE_TRACKER_EVENT + 2;
153
154 /**
155 * used internally to change our network preference setting
156 * arg1 = networkType to prefer
157 */
158 private static final int EVENT_SET_NETWORK_PREFERENCE =
159 MAX_NETWORK_STATE_TRACKER_EVENT + 3;
160
161 /**
162 * used internally to synchronize inet condition reports
163 * arg1 = networkType
164 * arg2 = condition (0 bad, 100 good)
165 */
166 private static final int EVENT_INET_CONDITION_CHANGE =
167 MAX_NETWORK_STATE_TRACKER_EVENT + 4;
168
169 /**
170 * used internally to mark the end of inet condition hold periods
171 * arg1 = networkType
172 */
173 private static final int EVENT_INET_CONDITION_HOLD_END =
174 MAX_NETWORK_STATE_TRACKER_EVENT + 5;
175
176 /**
177 * used internally to set the background data preference
178 * arg1 = TRUE for enabled, FALSE for disabled
179 */
180 private static final int EVENT_SET_BACKGROUND_DATA =
181 MAX_NETWORK_STATE_TRACKER_EVENT + 6;
182
183 /**
184 * used internally to set enable/disable cellular data
185 * arg1 = ENBALED or DISABLED
186 */
187 private static final int EVENT_SET_MOBILE_DATA =
188 MAX_NETWORK_STATE_TRACKER_EVENT + 7;
189
Robert Greenwaltccb36f92010-09-24 14:32:21 -0700190 /**
191 * used internally to clear a wakelock when transitioning
192 * from one net to another
193 */
194 private static final int EVENT_CLEAR_NET_TRANSITION_WAKELOCK =
195 MAX_NETWORK_STATE_TRACKER_EVENT + 8;
196
Robert Greenwaltc3c5f862010-10-11 16:00:27 -0700197 /**
198 * used internally to reload global proxy settings
199 */
200 private static final int EVENT_APPLY_GLOBAL_HTTP_PROXY =
201 MAX_NETWORK_STATE_TRACKER_EVENT + 9;
202
Robert Greenwalt34848c02011-03-25 13:09:25 -0700203 /**
204 * used internally to set external dependency met/unmet
205 * arg1 = ENABLED (met) or DISABLED (unmet)
206 * arg2 = NetworkType
207 */
208 private static final int EVENT_SET_DEPENDENCY_MET =
209 MAX_NETWORK_STATE_TRACKER_EVENT + 10;
210
Robert Greenwalt2034b912009-08-12 16:08:25 -0700211 private Handler mHandler;
212
213 // list of DeathRecipients used to make sure features are turned off when
214 // a process dies
215 private List mFeatureUsers;
216
Mike Lockwoodfde2b762009-08-14 14:18:49 -0400217 private boolean mSystemReady;
Dianne Hackborna417ff82009-12-08 19:45:14 -0800218 private Intent mInitialBroadcast;
Mike Lockwoodfde2b762009-08-14 14:18:49 -0400219
Robert Greenwalt93dc1042010-06-15 12:19:37 -0700220 private PowerManager.WakeLock mNetTransitionWakeLock;
221 private String mNetTransitionWakeLockCausedBy = "";
222 private int mNetTransitionWakeLockSerialNumber;
223 private int mNetTransitionWakeLockTimeout;
224
Robert Greenwalt94daa182010-09-01 11:34:05 -0700225 private InetAddress mDefaultDns;
226
Robert Greenwalt0e80be12010-09-20 14:35:25 -0700227 // used in DBG mode to track inet condition reports
228 private static final int INET_CONDITION_LOG_MAX_SIZE = 15;
229 private ArrayList mInetLog;
230
Robert Greenwaltc3c5f862010-10-11 16:00:27 -0700231 // track the current default http proxy - tell the world if we get a new one (real change)
232 private ProxyProperties mDefaultProxy = null;
233 // track the global proxy.
234 private ProxyProperties mGlobalProxy = null;
235 private final Object mGlobalProxyLock = new Object();
236
237 private SettingsObserver mSettingsObserver;
238
Robert Greenwalt34848c02011-03-25 13:09:25 -0700239 NetworkConfig[] mNetConfigs;
Robert Greenwaltec05b3c2009-10-30 14:17:42 -0700240 int mNetworksDefined;
Robert Greenwalt2034b912009-08-12 16:08:25 -0700241
Robert Greenwalt12c44552009-12-07 11:33:18 -0800242 private static class RadioAttributes {
Robert Greenwalt2034b912009-08-12 16:08:25 -0700243 public int mSimultaneity;
244 public int mType;
245 public RadioAttributes(String init) {
246 String fragments[] = init.split(",");
Robert Greenwaltec05b3c2009-10-30 14:17:42 -0700247 mType = Integer.parseInt(fragments[0]);
248 mSimultaneity = Integer.parseInt(fragments[1]);
Robert Greenwalt2034b912009-08-12 16:08:25 -0700249 }
250 }
251 RadioAttributes[] mRadioAttributes;
252
Robert Greenwalt6cac0742011-06-21 17:26:14 -0700253 // the set of network types that can only be enabled by system/sig apps
254 List mProtectedNetworks;
255
Wink Saville775aad62010-09-02 19:23:52 -0700256 public static synchronized ConnectivityService getInstance(Context context) {
257 if (sServiceInstance == null) {
258 sServiceInstance = new ConnectivityService(context);
The Android Open Source Project28527d22009-03-03 19:31:44 -0800259 }
Wink Saville775aad62010-09-02 19:23:52 -0700260 return sServiceInstance;
The Android Open Source Project28527d22009-03-03 19:31:44 -0800261 }
Robert Greenwalt0659da32009-07-16 17:21:39 -0700262
The Android Open Source Project28527d22009-03-03 19:31:44 -0800263 private ConnectivityService(Context context) {
Wink Savillee70c6f52010-12-03 12:01:38 -0800264 if (DBG) log("ConnectivityService starting up");
Robert Greenwaltd48f8ee2010-01-14 17:47:58 -0800265
Wink Saville775aad62010-09-02 19:23:52 -0700266 HandlerThread handlerThread = new HandlerThread("ConnectivityServiceThread");
267 handlerThread.start();
268 mHandler = new MyHandler(handlerThread.getLooper());
269
Robert Greenwaltd62c7002010-12-29 16:15:02 -0800270 mBackgroundDataEnabled.set(Settings.Secure.getInt(context.getContentResolver(),
271 Settings.Secure.BACKGROUND_DATA, 1) == 1);
272
Robert Greenwaltd48f8ee2010-01-14 17:47:58 -0800273 // setup our unique device name
Robert Greenwalt82cde132010-12-06 09:30:17 -0800274 if (TextUtils.isEmpty(SystemProperties.get("net.hostname"))) {
275 String id = Settings.Secure.getString(context.getContentResolver(),
276 Settings.Secure.ANDROID_ID);
277 if (id != null && id.length() > 0) {
278 String name = new String("android_").concat(id);
279 SystemProperties.set("net.hostname", name);
280 }
Robert Greenwaltd48f8ee2010-01-14 17:47:58 -0800281 }
282
Robert Greenwalt94daa182010-09-01 11:34:05 -0700283 // read our default dns server ip
284 String dns = Settings.Secure.getString(context.getContentResolver(),
285 Settings.Secure.DEFAULT_DNS_SERVER);
286 if (dns == null || dns.length() == 0) {
287 dns = context.getResources().getString(
288 com.android.internal.R.string.config_default_dns_server);
289 }
290 try {
Robert Greenwalt35e34d12011-02-22 16:00:42 -0800291 mDefaultDns = NetworkUtils.numericToInetAddress(dns);
292 } catch (IllegalArgumentException e) {
Wink Savillee70c6f52010-12-03 12:01:38 -0800293 loge("Error setting defaultDns using " + dns);
Robert Greenwalt94daa182010-09-01 11:34:05 -0700294 }
295
The Android Open Source Project28527d22009-03-03 19:31:44 -0800296 mContext = context;
Robert Greenwalt93dc1042010-06-15 12:19:37 -0700297
298 PowerManager powerManager = (PowerManager)mContext.getSystemService(Context.POWER_SERVICE);
299 mNetTransitionWakeLock = powerManager.newWakeLock(PowerManager.PARTIAL_WAKE_LOCK, TAG);
300 mNetTransitionWakeLockTimeout = mContext.getResources().getInteger(
301 com.android.internal.R.integer.config_networkTransitionTimeout);
302
Robert Greenwalt2034b912009-08-12 16:08:25 -0700303 mNetTrackers = new NetworkStateTracker[
304 ConnectivityManager.MAX_NETWORK_TYPE+1];
Robert Greenwalt0659da32009-07-16 17:21:39 -0700305
The Android Open Source Project28527d22009-03-03 19:31:44 -0800306 mNetworkPreference = getPersistedNetworkPreference();
Robert Greenwalt0659da32009-07-16 17:21:39 -0700307
Robert Greenwaltec05b3c2009-10-30 14:17:42 -0700308 mRadioAttributes = new RadioAttributes[ConnectivityManager.MAX_RADIO_TYPE+1];
Robert Greenwalt34848c02011-03-25 13:09:25 -0700309 mNetConfigs = new NetworkConfig[ConnectivityManager.MAX_NETWORK_TYPE+1];
Robert Greenwaltec05b3c2009-10-30 14:17:42 -0700310
Robert Greenwalt2034b912009-08-12 16:08:25 -0700311 // Load device network attributes from resources
Robert Greenwalt2034b912009-08-12 16:08:25 -0700312 String[] raStrings = context.getResources().getStringArray(
313 com.android.internal.R.array.radioAttributes);
Robert Greenwaltec05b3c2009-10-30 14:17:42 -0700314 for (String raString : raStrings) {
315 RadioAttributes r = new RadioAttributes(raString);
316 if (r.mType > ConnectivityManager.MAX_RADIO_TYPE) {
Wink Savillee70c6f52010-12-03 12:01:38 -0800317 loge("Error in radioAttributes - ignoring attempt to define type " + r.mType);
Robert Greenwaltec05b3c2009-10-30 14:17:42 -0700318 continue;
319 }
320 if (mRadioAttributes[r.mType] != null) {
Wink Savillee70c6f52010-12-03 12:01:38 -0800321 loge("Error in radioAttributes - ignoring attempt to redefine type " +
Robert Greenwaltec05b3c2009-10-30 14:17:42 -0700322 r.mType);
323 continue;
324 }
Robert Greenwalt2034b912009-08-12 16:08:25 -0700325 mRadioAttributes[r.mType] = r;
326 }
327
Robert Greenwaltec05b3c2009-10-30 14:17:42 -0700328 String[] naStrings = context.getResources().getStringArray(
329 com.android.internal.R.array.networkAttributes);
330 for (String naString : naStrings) {
331 try {
Robert Greenwalt34848c02011-03-25 13:09:25 -0700332 NetworkConfig n = new NetworkConfig(naString);
Wink Savillef2a62832011-04-07 14:23:45 -0700333 if (n.type > ConnectivityManager.MAX_NETWORK_TYPE) {
Wink Savillee70c6f52010-12-03 12:01:38 -0800334 loge("Error in networkAttributes - ignoring attempt to define type " +
Wink Savillef2a62832011-04-07 14:23:45 -0700335 n.type);
Robert Greenwaltec05b3c2009-10-30 14:17:42 -0700336 continue;
Robert Greenwalt2034b912009-08-12 16:08:25 -0700337 }
Wink Savillef2a62832011-04-07 14:23:45 -0700338 if (mNetConfigs[n.type] != null) {
Wink Savillee70c6f52010-12-03 12:01:38 -0800339 loge("Error in networkAttributes - ignoring attempt to redefine type " +
Wink Savillef2a62832011-04-07 14:23:45 -0700340 n.type);
Robert Greenwaltec05b3c2009-10-30 14:17:42 -0700341 continue;
342 }
Wink Savillef2a62832011-04-07 14:23:45 -0700343 if (mRadioAttributes[n.radio] == null) {
Wink Savillee70c6f52010-12-03 12:01:38 -0800344 loge("Error in networkAttributes - ignoring attempt to use undefined " +
Wink Savillef2a62832011-04-07 14:23:45 -0700345 "radio " + n.radio + " in network type " + n.type);
Robert Greenwaltec05b3c2009-10-30 14:17:42 -0700346 continue;
347 }
Wink Savillef2a62832011-04-07 14:23:45 -0700348 mNetConfigs[n.type] = n;
Robert Greenwaltec05b3c2009-10-30 14:17:42 -0700349 mNetworksDefined++;
350 } catch(Exception e) {
351 // ignore it - leave the entry null
Robert Greenwalt2034b912009-08-12 16:08:25 -0700352 }
353 }
354
Robert Greenwalt6cac0742011-06-21 17:26:14 -0700355 mProtectedNetworks = new ArrayList<Integer>();
356 int[] protectedNetworks = context.getResources().getIntArray(
357 com.android.internal.R.array.config_protectedNetworks);
358 for (int p : protectedNetworks) {
359 if ((mNetConfigs[p] != null) && (mProtectedNetworks.contains(p) == false)) {
360 mProtectedNetworks.add(p);
361 } else {
362 if (DBG) loge("Ignoring protectedNetwork " + p);
363 }
364 }
365
Robert Greenwaltec05b3c2009-10-30 14:17:42 -0700366 // high priority first
367 mPriorityList = new int[mNetworksDefined];
368 {
369 int insertionPoint = mNetworksDefined-1;
370 int currentLowest = 0;
371 int nextLowest = 0;
372 while (insertionPoint > -1) {
Robert Greenwalt34848c02011-03-25 13:09:25 -0700373 for (NetworkConfig na : mNetConfigs) {
Robert Greenwaltec05b3c2009-10-30 14:17:42 -0700374 if (na == null) continue;
Wink Savillef2a62832011-04-07 14:23:45 -0700375 if (na.priority < currentLowest) continue;
376 if (na.priority > currentLowest) {
377 if (na.priority < nextLowest || nextLowest == 0) {
378 nextLowest = na.priority;
Robert Greenwaltec05b3c2009-10-30 14:17:42 -0700379 }
380 continue;
381 }
Wink Savillef2a62832011-04-07 14:23:45 -0700382 mPriorityList[insertionPoint--] = na.type;
Robert Greenwaltec05b3c2009-10-30 14:17:42 -0700383 }
384 currentLowest = nextLowest;
385 nextLowest = 0;
386 }
387 }
388
389 mNetRequestersPids = new ArrayList[ConnectivityManager.MAX_NETWORK_TYPE+1];
390 for (int i : mPriorityList) {
Robert Greenwalt2034b912009-08-12 16:08:25 -0700391 mNetRequestersPids[i] = new ArrayList();
392 }
393
394 mFeatureUsers = new ArrayList();
395
Robert Greenwaltec05b3c2009-10-30 14:17:42 -0700396 mNumDnsEntries = 0;
397
398 mTestMode = SystemProperties.get("cm.test.mode").equals("true")
399 && SystemProperties.get("ro.build.type").equals("eng");
The Android Open Source Project28527d22009-03-03 19:31:44 -0800400 /*
401 * Create the network state trackers for Wi-Fi and mobile
402 * data. Maybe this could be done with a factory class,
403 * but it's not clear that it's worth it, given that
404 * the number of different network types is not going
405 * to change very often.
406 */
Robert Greenwaltec05b3c2009-10-30 14:17:42 -0700407 for (int netType : mPriorityList) {
Wink Savillef2a62832011-04-07 14:23:45 -0700408 switch (mNetConfigs[netType].radio) {
Robert Greenwaltec05b3c2009-10-30 14:17:42 -0700409 case ConnectivityManager.TYPE_WIFI:
Wink Savillee70c6f52010-12-03 12:01:38 -0800410 if (DBG) log("Starting Wifi Service.");
Wink Saville7fabfa22010-08-13 16:11:42 -0700411 WifiStateTracker wst = new WifiStateTracker();
Irfan Sheriff25be0762010-07-28 09:35:20 -0700412 WifiService wifiService = new WifiService(context);
Robert Greenwaltec05b3c2009-10-30 14:17:42 -0700413 ServiceManager.addService(Context.WIFI_SERVICE, wifiService);
Irfan Sheriff25be0762010-07-28 09:35:20 -0700414 wifiService.checkAndStartWifi();
Robert Greenwaltec05b3c2009-10-30 14:17:42 -0700415 mNetTrackers[ConnectivityManager.TYPE_WIFI] = wst;
Wink Saville7fabfa22010-08-13 16:11:42 -0700416 wst.startMonitoring(context, mHandler);
The Android Open Source Project28527d22009-03-03 19:31:44 -0800417
Irfan Sheriff653e2a22010-06-07 09:03:04 -0700418 //TODO: as part of WWS refactor, create only when needed
Irfan Sheriff25be0762010-07-28 09:35:20 -0700419 mWifiWatchdogService = new WifiWatchdogService(context);
Irfan Sheriff653e2a22010-06-07 09:03:04 -0700420
Robert Greenwaltec05b3c2009-10-30 14:17:42 -0700421 break;
422 case ConnectivityManager.TYPE_MOBILE:
Wink Saville7fabfa22010-08-13 16:11:42 -0700423 mNetTrackers[netType] = new MobileDataStateTracker(netType,
Wink Savillef2a62832011-04-07 14:23:45 -0700424 mNetConfigs[netType].name);
Wink Saville7fabfa22010-08-13 16:11:42 -0700425 mNetTrackers[netType].startMonitoring(context, mHandler);
Robert Greenwaltec05b3c2009-10-30 14:17:42 -0700426 break;
Robert Greenwalteb123ac2010-12-06 13:56:24 -0800427 case ConnectivityManager.TYPE_DUMMY:
428 mNetTrackers[netType] = new DummyDataStateTracker(netType,
Wink Savillef2a62832011-04-07 14:23:45 -0700429 mNetConfigs[netType].name);
Robert Greenwalteb123ac2010-12-06 13:56:24 -0800430 mNetTrackers[netType].startMonitoring(context, mHandler);
431 break;
Jaikumar Ganesh0db51a02010-12-21 22:31:44 -0800432 case ConnectivityManager.TYPE_BLUETOOTH:
433 mNetTrackers[netType] = BluetoothTetheringDataTracker.getInstance();
434 mNetTrackers[netType].startMonitoring(context, mHandler);
435 break;
Benoit Goby6cec7f32010-12-22 14:29:40 -0800436 case ConnectivityManager.TYPE_ETHERNET:
437 mNetTrackers[netType] = EthernetDataTracker.getInstance();
438 mNetTrackers[netType].startMonitoring(context, mHandler);
439 break;
Robert Greenwaltec05b3c2009-10-30 14:17:42 -0700440 default:
Wink Savillee70c6f52010-12-03 12:01:38 -0800441 loge("Trying to create a DataStateTracker for an unknown radio type " +
Wink Savillef2a62832011-04-07 14:23:45 -0700442 mNetConfigs[netType].radio);
Robert Greenwaltec05b3c2009-10-30 14:17:42 -0700443 continue;
444 }
445 }
Robert Greenwalt0c4828c2010-01-26 11:40:34 -0800446
Robert Greenwaltc0b6c602010-03-11 15:03:08 -0800447 mTethering = new Tethering(mContext, mHandler.getLooper());
Robert Greenwalt33cdcdf2011-06-02 17:30:47 -0700448 mTetheringConfigValid = ((mTethering.getTetherableUsbRegexs().length != 0 ||
Danica Chang96567052010-08-11 14:54:43 -0700449 mTethering.getTetherableWifiRegexs().length != 0 ||
450 mTethering.getTetherableBluetoothRegexs().length != 0) &&
Robert Greenwalt33cdcdf2011-06-02 17:30:47 -0700451 mTethering.getUpstreamIfaceTypes().length != 0);
Robert Greenwaltf1b66e12010-02-25 12:29:30 -0800452
Robert Greenwalt0e80be12010-09-20 14:35:25 -0700453 if (DBG) {
454 mInetLog = new ArrayList();
455 }
Robert Greenwaltc3c5f862010-10-11 16:00:27 -0700456
457 mSettingsObserver = new SettingsObserver(mHandler, EVENT_APPLY_GLOBAL_HTTP_PROXY);
458 mSettingsObserver.observe(mContext);
Robert Greenwalt6f7c6092010-12-02 11:31:00 -0800459
460 loadGlobalProxy();
Hung-ying Tyan4e723422011-01-19 16:48:38 +0800461
462 VpnManager.startVpnService(context);
The Android Open Source Project28527d22009-03-03 19:31:44 -0800463 }
464
Robert Greenwaltec05b3c2009-10-30 14:17:42 -0700465
The Android Open Source Project28527d22009-03-03 19:31:44 -0800466 /**
Robert Greenwalt0659da32009-07-16 17:21:39 -0700467 * Sets the preferred network.
The Android Open Source Project28527d22009-03-03 19:31:44 -0800468 * @param preference the new preference
469 */
Robert Greenwalt6a2db8a2010-09-23 10:05:56 -0700470 public void setNetworkPreference(int preference) {
The Android Open Source Project28527d22009-03-03 19:31:44 -0800471 enforceChangePermission();
Robert Greenwalt6a2db8a2010-09-23 10:05:56 -0700472
473 mHandler.sendMessage(mHandler.obtainMessage(EVENT_SET_NETWORK_PREFERENCE, preference, 0));
The Android Open Source Project28527d22009-03-03 19:31:44 -0800474 }
475
476 public int getNetworkPreference() {
477 enforceAccessPermission();
Robert Greenwalt6a2db8a2010-09-23 10:05:56 -0700478 int preference;
479 synchronized(this) {
480 preference = mNetworkPreference;
481 }
482 return preference;
The Android Open Source Project28527d22009-03-03 19:31:44 -0800483 }
484
Robert Greenwalt6a2db8a2010-09-23 10:05:56 -0700485 private void handleSetNetworkPreference(int preference) {
486 if (ConnectivityManager.isNetworkTypeValid(preference) &&
Robert Greenwalt34848c02011-03-25 13:09:25 -0700487 mNetConfigs[preference] != null &&
488 mNetConfigs[preference].isDefault()) {
Robert Greenwalt6a2db8a2010-09-23 10:05:56 -0700489 if (mNetworkPreference != preference) {
490 final ContentResolver cr = mContext.getContentResolver();
491 Settings.Secure.putInt(cr, Settings.Secure.NETWORK_PREFERENCE, preference);
492 synchronized(this) {
493 mNetworkPreference = preference;
494 }
495 enforcePreference();
496 }
497 }
The Android Open Source Project28527d22009-03-03 19:31:44 -0800498 }
Robert Greenwalt0659da32009-07-16 17:21:39 -0700499
The Android Open Source Project28527d22009-03-03 19:31:44 -0800500 private int getPersistedNetworkPreference() {
501 final ContentResolver cr = mContext.getContentResolver();
502
503 final int networkPrefSetting = Settings.Secure
504 .getInt(cr, Settings.Secure.NETWORK_PREFERENCE, -1);
505 if (networkPrefSetting != -1) {
506 return networkPrefSetting;
507 }
508
509 return ConnectivityManager.DEFAULT_NETWORK_PREFERENCE;
510 }
Robert Greenwalt0659da32009-07-16 17:21:39 -0700511
The Android Open Source Project28527d22009-03-03 19:31:44 -0800512 /**
Robert Greenwalt0659da32009-07-16 17:21:39 -0700513 * Make the state of network connectivity conform to the preference settings
The Android Open Source Project28527d22009-03-03 19:31:44 -0800514 * In this method, we only tear down a non-preferred network. Establishing
515 * a connection to the preferred network is taken care of when we handle
516 * the disconnect event from the non-preferred network
517 * (see {@link #handleDisconnect(NetworkInfo)}).
518 */
519 private void enforcePreference() {
Robert Greenwalt2034b912009-08-12 16:08:25 -0700520 if (mNetTrackers[mNetworkPreference].getNetworkInfo().isConnected())
The Android Open Source Project28527d22009-03-03 19:31:44 -0800521 return;
522
Robert Greenwalt2034b912009-08-12 16:08:25 -0700523 if (!mNetTrackers[mNetworkPreference].isAvailable())
524 return;
The Android Open Source Project28527d22009-03-03 19:31:44 -0800525
Robert Greenwalt2034b912009-08-12 16:08:25 -0700526 for (int t=0; t <= ConnectivityManager.MAX_RADIO_TYPE; t++) {
Robert Greenwaltec05b3c2009-10-30 14:17:42 -0700527 if (t != mNetworkPreference && mNetTrackers[t] != null &&
Robert Greenwalt2034b912009-08-12 16:08:25 -0700528 mNetTrackers[t].getNetworkInfo().isConnected()) {
Robert Greenwaltf3f045b2009-08-20 15:25:14 -0700529 if (DBG) {
Wink Savillee70c6f52010-12-03 12:01:38 -0800530 log("tearing down " + mNetTrackers[t].getNetworkInfo() +
Robert Greenwaltf3f045b2009-08-20 15:25:14 -0700531 " in enforcePreference");
532 }
Robert Greenwalt2034b912009-08-12 16:08:25 -0700533 teardown(mNetTrackers[t]);
The Android Open Source Project28527d22009-03-03 19:31:44 -0800534 }
535 }
536 }
537
538 private boolean teardown(NetworkStateTracker netTracker) {
539 if (netTracker.teardown()) {
540 netTracker.setTeardownRequested(true);
541 return true;
542 } else {
543 return false;
544 }
545 }
546
547 /**
548 * Return NetworkInfo for the active (i.e., connected) network interface.
549 * It is assumed that at most one network is active at a time. If more
550 * than one is active, it is indeterminate which will be returned.
Robert Greenwalt0659da32009-07-16 17:21:39 -0700551 * @return the info for the active network, or {@code null} if none is
552 * active
The Android Open Source Project28527d22009-03-03 19:31:44 -0800553 */
554 public NetworkInfo getActiveNetworkInfo() {
Robert Greenwalte1544bb2011-05-20 12:23:41 -0700555 return getNetworkInfo(mActiveDefaultNetwork);
The Android Open Source Project28527d22009-03-03 19:31:44 -0800556 }
557
558 public NetworkInfo getNetworkInfo(int networkType) {
559 enforceAccessPermission();
560 if (ConnectivityManager.isNetworkTypeValid(networkType)) {
561 NetworkStateTracker t = mNetTrackers[networkType];
562 if (t != null)
563 return t.getNetworkInfo();
564 }
565 return null;
566 }
567
568 public NetworkInfo[] getAllNetworkInfo() {
569 enforceAccessPermission();
Robert Greenwaltec05b3c2009-10-30 14:17:42 -0700570 NetworkInfo[] result = new NetworkInfo[mNetworksDefined];
The Android Open Source Project28527d22009-03-03 19:31:44 -0800571 int i = 0;
572 for (NetworkStateTracker t : mNetTrackers) {
Robert Greenwaltec05b3c2009-10-30 14:17:42 -0700573 if(t != null) result[i++] = t.getNetworkInfo();
The Android Open Source Project28527d22009-03-03 19:31:44 -0800574 }
575 return result;
576 }
577
Robert Greenwalt9f0ee4f2010-09-14 09:18:02 -0700578 /**
579 * Return LinkProperties for the active (i.e., connected) default
580 * network interface. It is assumed that at most one default network
581 * is active at a time. If more than one is active, it is indeterminate
582 * which will be returned.
583 * @return the ip properties for the active network, or {@code null} if
584 * none is active
585 */
586 public LinkProperties getActiveLinkProperties() {
Robert Greenwalte1544bb2011-05-20 12:23:41 -0700587 return getLinkProperties(mActiveDefaultNetwork);
Robert Greenwalt9f0ee4f2010-09-14 09:18:02 -0700588 }
589
590 public LinkProperties getLinkProperties(int networkType) {
591 enforceAccessPermission();
592 if (ConnectivityManager.isNetworkTypeValid(networkType)) {
593 NetworkStateTracker t = mNetTrackers[networkType];
594 if (t != null) return t.getLinkProperties();
595 }
596 return null;
597 }
598
The Android Open Source Project28527d22009-03-03 19:31:44 -0800599 public boolean setRadios(boolean turnOn) {
600 boolean result = true;
601 enforceChangePermission();
602 for (NetworkStateTracker t : mNetTrackers) {
Robert Greenwaltec05b3c2009-10-30 14:17:42 -0700603 if (t != null) result = t.setRadio(turnOn) && result;
The Android Open Source Project28527d22009-03-03 19:31:44 -0800604 }
605 return result;
606 }
607
608 public boolean setRadio(int netType, boolean turnOn) {
609 enforceChangePermission();
610 if (!ConnectivityManager.isNetworkTypeValid(netType)) {
611 return false;
612 }
613 NetworkStateTracker tracker = mNetTrackers[netType];
614 return tracker != null && tracker.setRadio(turnOn);
615 }
616
Robert Greenwaltaffc3a12009-09-27 17:27:04 -0700617 /**
618 * Used to notice when the calling process dies so we can self-expire
619 *
620 * Also used to know if the process has cleaned up after itself when
621 * our auto-expire timer goes off. The timer has a link to an object.
622 *
623 */
Robert Greenwalt2034b912009-08-12 16:08:25 -0700624 private class FeatureUser implements IBinder.DeathRecipient {
625 int mNetworkType;
626 String mFeature;
627 IBinder mBinder;
628 int mPid;
629 int mUid;
Robert Greenwalt3eeb6032009-12-21 18:24:07 -0800630 long mCreateTime;
Robert Greenwalt2034b912009-08-12 16:08:25 -0700631
632 FeatureUser(int type, String feature, IBinder binder) {
633 super();
634 mNetworkType = type;
635 mFeature = feature;
636 mBinder = binder;
637 mPid = getCallingPid();
638 mUid = getCallingUid();
Robert Greenwalt3eeb6032009-12-21 18:24:07 -0800639 mCreateTime = System.currentTimeMillis();
Robert Greenwaltaffc3a12009-09-27 17:27:04 -0700640
Robert Greenwalt2034b912009-08-12 16:08:25 -0700641 try {
642 mBinder.linkToDeath(this, 0);
643 } catch (RemoteException e) {
644 binderDied();
645 }
646 }
647
648 void unlinkDeathRecipient() {
649 mBinder.unlinkToDeath(this, 0);
650 }
651
652 public void binderDied() {
Wink Savillee70c6f52010-12-03 12:01:38 -0800653 log("ConnectivityService FeatureUser binderDied(" +
Robert Greenwalt3eeb6032009-12-21 18:24:07 -0800654 mNetworkType + ", " + mFeature + ", " + mBinder + "), created " +
655 (System.currentTimeMillis() - mCreateTime) + " mSec ago");
Robert Greenwaltaffc3a12009-09-27 17:27:04 -0700656 stopUsingNetworkFeature(this, false);
Robert Greenwalt2034b912009-08-12 16:08:25 -0700657 }
658
Robert Greenwaltaffc3a12009-09-27 17:27:04 -0700659 public void expire() {
Wink Savillee70c6f52010-12-03 12:01:38 -0800660 log("ConnectivityService FeatureUser expire(" +
Robert Greenwalt3eeb6032009-12-21 18:24:07 -0800661 mNetworkType + ", " + mFeature + ", " + mBinder +"), created " +
662 (System.currentTimeMillis() - mCreateTime) + " mSec ago");
Robert Greenwaltaffc3a12009-09-27 17:27:04 -0700663 stopUsingNetworkFeature(this, false);
664 }
Robert Greenwalt3eeb6032009-12-21 18:24:07 -0800665
666 public String toString() {
667 return "FeatureUser("+mNetworkType+","+mFeature+","+mPid+","+mUid+"), created " +
668 (System.currentTimeMillis() - mCreateTime) + " mSec ago";
669 }
Robert Greenwalt2034b912009-08-12 16:08:25 -0700670 }
671
Robert Greenwaltaffc3a12009-09-27 17:27:04 -0700672 // javadoc from interface
Robert Greenwalt2034b912009-08-12 16:08:25 -0700673 public int startUsingNetworkFeature(int networkType, String feature,
674 IBinder binder) {
675 if (DBG) {
Wink Savillee70c6f52010-12-03 12:01:38 -0800676 log("startUsingNetworkFeature for net " + networkType + ": " + feature);
Robert Greenwalt2034b912009-08-12 16:08:25 -0700677 }
The Android Open Source Project28527d22009-03-03 19:31:44 -0800678 enforceChangePermission();
Robert Greenwaltec05b3c2009-10-30 14:17:42 -0700679 if (!ConnectivityManager.isNetworkTypeValid(networkType) ||
Robert Greenwalt34848c02011-03-25 13:09:25 -0700680 mNetConfigs[networkType] == null) {
Robert Greenwalt2034b912009-08-12 16:08:25 -0700681 return Phone.APN_REQUEST_FAILED;
The Android Open Source Project28527d22009-03-03 19:31:44 -0800682 }
Robert Greenwalt2034b912009-08-12 16:08:25 -0700683
Robert Greenwaltaffc3a12009-09-27 17:27:04 -0700684 FeatureUser f = new FeatureUser(networkType, feature, binder);
Robert Greenwalt2034b912009-08-12 16:08:25 -0700685
686 // TODO - move this into the MobileDataStateTracker
687 int usedNetworkType = networkType;
688 if(networkType == ConnectivityManager.TYPE_MOBILE) {
Wink Savilleb7c92c72011-03-12 14:52:01 -0800689 usedNetworkType = convertFeatureToNetworkType(feature);
690 if (usedNetworkType < 0) {
691 Slog.e(TAG, "Can't match any netTracker!");
692 usedNetworkType = networkType;
Robert Greenwalt2034b912009-08-12 16:08:25 -0700693 }
694 }
Robert Greenwalt6cac0742011-06-21 17:26:14 -0700695
696 if (mProtectedNetworks.contains(usedNetworkType)) {
697 enforceConnectivityInternalPermission();
698 }
699
Robert Greenwalt2034b912009-08-12 16:08:25 -0700700 NetworkStateTracker network = mNetTrackers[usedNetworkType];
701 if (network != null) {
Robert Greenwalt5364d752010-12-15 13:26:33 -0800702 Integer currentPid = new Integer(getCallingPid());
Robert Greenwalt2034b912009-08-12 16:08:25 -0700703 if (usedNetworkType != networkType) {
Robert Greenwalt2034b912009-08-12 16:08:25 -0700704 NetworkStateTracker radio = mNetTrackers[networkType];
705 NetworkInfo ni = network.getNetworkInfo();
706
707 if (ni.isAvailable() == false) {
Wink Savillee70c6f52010-12-03 12:01:38 -0800708 if (DBG) log("special network not available");
Robert Greenwalt2cc87442010-12-29 14:35:21 -0800709 if (!TextUtils.equals(feature,Phone.FEATURE_ENABLE_DUN_ALWAYS)) {
710 return Phone.APN_TYPE_NOT_AVAILABLE;
711 } else {
712 // else make the attempt anyway - probably giving REQUEST_STARTED below
713 }
Robert Greenwalt2034b912009-08-12 16:08:25 -0700714 }
715
Robert Greenwaltaffc3a12009-09-27 17:27:04 -0700716 synchronized(this) {
717 mFeatureUsers.add(f);
718 if (!mNetRequestersPids[usedNetworkType].contains(currentPid)) {
719 // this gets used for per-pid dns when connected
720 mNetRequestersPids[usedNetworkType].add(currentPid);
721 }
Robert Greenwalt2034b912009-08-12 16:08:25 -0700722 }
Robert Greenwaltaffc3a12009-09-27 17:27:04 -0700723
Robert Greenwalt20f819c2011-05-03 19:02:44 -0700724 int restoreTimer = getRestoreDefaultNetworkDelay(usedNetworkType);
725
726 if (restoreTimer >= 0) {
727 mHandler.sendMessageDelayed(
728 mHandler.obtainMessage(EVENT_RESTORE_DEFAULT_NETWORK, f), restoreTimer);
729 }
Robert Greenwalt2034b912009-08-12 16:08:25 -0700730
Robert Greenwalta52c75a2009-08-19 20:19:33 -0700731 if ((ni.isConnectedOrConnecting() == true) &&
732 !network.isTeardownRequested()) {
Robert Greenwalt2034b912009-08-12 16:08:25 -0700733 if (ni.isConnected() == true) {
734 // add the pid-specific dns
Robert Greenwalt3afbead2010-07-23 15:46:26 -0700735 handleDnsConfigurationChange(networkType);
Wink Savillee70c6f52010-12-03 12:01:38 -0800736 if (DBG) log("special network already active");
Robert Greenwalt2034b912009-08-12 16:08:25 -0700737 return Phone.APN_ALREADY_ACTIVE;
738 }
Wink Savillee70c6f52010-12-03 12:01:38 -0800739 if (DBG) log("special network already connecting");
Robert Greenwalt2034b912009-08-12 16:08:25 -0700740 return Phone.APN_REQUEST_STARTED;
741 }
742
743 // check if the radio in play can make another contact
744 // assume if cannot for now
745
Wink Savillee70c6f52010-12-03 12:01:38 -0800746 if (DBG) log("reconnecting to special network");
Robert Greenwalt2034b912009-08-12 16:08:25 -0700747 network.reconnect();
748 return Phone.APN_REQUEST_STARTED;
749 } else {
Robert Greenwalt5364d752010-12-15 13:26:33 -0800750 // need to remember this unsupported request so we respond appropriately on stop
751 synchronized(this) {
752 mFeatureUsers.add(f);
753 if (!mNetRequestersPids[usedNetworkType].contains(currentPid)) {
754 // this gets used for per-pid dns when connected
755 mNetRequestersPids[usedNetworkType].add(currentPid);
756 }
757 }
Robert Greenwaltd391e892010-05-18 10:52:51 -0700758 return -1;
Robert Greenwalt2034b912009-08-12 16:08:25 -0700759 }
760 }
761 return Phone.APN_TYPE_NOT_AVAILABLE;
The Android Open Source Project28527d22009-03-03 19:31:44 -0800762 }
763
Robert Greenwaltaffc3a12009-09-27 17:27:04 -0700764 // javadoc from interface
The Android Open Source Project28527d22009-03-03 19:31:44 -0800765 public int stopUsingNetworkFeature(int networkType, String feature) {
Robert Greenwalt28f43012009-10-06 17:52:40 -0700766 enforceChangePermission();
767
Robert Greenwaltaffc3a12009-09-27 17:27:04 -0700768 int pid = getCallingPid();
769 int uid = getCallingUid();
770
771 FeatureUser u = null;
772 boolean found = false;
773
774 synchronized(this) {
775 for (int i = 0; i < mFeatureUsers.size() ; i++) {
776 u = (FeatureUser)mFeatureUsers.get(i);
777 if (uid == u.mUid && pid == u.mPid &&
778 networkType == u.mNetworkType &&
779 TextUtils.equals(feature, u.mFeature)) {
780 found = true;
781 break;
782 }
783 }
784 }
785 if (found && u != null) {
786 // stop regardless of how many other time this proc had called start
787 return stopUsingNetworkFeature(u, true);
788 } else {
789 // none found!
Wink Savillee70c6f52010-12-03 12:01:38 -0800790 if (DBG) log("ignoring stopUsingNetworkFeature - not a live request");
Robert Greenwaltaffc3a12009-09-27 17:27:04 -0700791 return 1;
792 }
Robert Greenwalt2034b912009-08-12 16:08:25 -0700793 }
794
Robert Greenwaltaffc3a12009-09-27 17:27:04 -0700795 private int stopUsingNetworkFeature(FeatureUser u, boolean ignoreDups) {
796 int networkType = u.mNetworkType;
797 String feature = u.mFeature;
798 int pid = u.mPid;
799 int uid = u.mUid;
800
801 NetworkStateTracker tracker = null;
802 boolean callTeardown = false; // used to carry our decision outside of sync block
803
Robert Greenwalt2034b912009-08-12 16:08:25 -0700804 if (DBG) {
Wink Savillee70c6f52010-12-03 12:01:38 -0800805 log("stopUsingNetworkFeature for net " + networkType +
Robert Greenwalt2034b912009-08-12 16:08:25 -0700806 ": " + feature);
807 }
Robert Greenwalt28f43012009-10-06 17:52:40 -0700808
The Android Open Source Project28527d22009-03-03 19:31:44 -0800809 if (!ConnectivityManager.isNetworkTypeValid(networkType)) {
810 return -1;
811 }
Robert Greenwalt2034b912009-08-12 16:08:25 -0700812
Robert Greenwaltaffc3a12009-09-27 17:27:04 -0700813 // need to link the mFeatureUsers list with the mNetRequestersPids state in this
814 // sync block
815 synchronized(this) {
816 // check if this process still has an outstanding start request
817 if (!mFeatureUsers.contains(u)) {
Wink Savillee70c6f52010-12-03 12:01:38 -0800818 if (DBG) log("ignoring - this process has no outstanding requests");
Robert Greenwalt2034b912009-08-12 16:08:25 -0700819 return 1;
820 }
Robert Greenwaltaffc3a12009-09-27 17:27:04 -0700821 u.unlinkDeathRecipient();
822 mFeatureUsers.remove(mFeatureUsers.indexOf(u));
823 // If we care about duplicate requests, check for that here.
824 //
825 // This is done to support the extension of a request - the app
826 // can request we start the network feature again and renew the
827 // auto-shutoff delay. Normal "stop" calls from the app though
828 // do not pay attention to duplicate requests - in effect the
829 // API does not refcount and a single stop will counter multiple starts.
830 if (ignoreDups == false) {
831 for (int i = 0; i < mFeatureUsers.size() ; i++) {
832 FeatureUser x = (FeatureUser)mFeatureUsers.get(i);
833 if (x.mUid == u.mUid && x.mPid == u.mPid &&
834 x.mNetworkType == u.mNetworkType &&
835 TextUtils.equals(x.mFeature, u.mFeature)) {
Wink Savillee70c6f52010-12-03 12:01:38 -0800836 if (DBG) log("ignoring stopUsingNetworkFeature as dup is found");
Robert Greenwaltaffc3a12009-09-27 17:27:04 -0700837 return 1;
838 }
839 }
Robert Greenwalt2034b912009-08-12 16:08:25 -0700840 }
Robert Greenwaltaffc3a12009-09-27 17:27:04 -0700841
842 // TODO - move to MobileDataStateTracker
843 int usedNetworkType = networkType;
844 if (networkType == ConnectivityManager.TYPE_MOBILE) {
Wink Savilleb7c92c72011-03-12 14:52:01 -0800845 usedNetworkType = convertFeatureToNetworkType(feature);
846 if (usedNetworkType < 0) {
847 usedNetworkType = networkType;
Robert Greenwaltaffc3a12009-09-27 17:27:04 -0700848 }
849 }
850 tracker = mNetTrackers[usedNetworkType];
Robert Greenwaltec05b3c2009-10-30 14:17:42 -0700851 if (tracker == null) {
Wink Savillee70c6f52010-12-03 12:01:38 -0800852 if (DBG) log("ignoring - no known tracker for net type " + usedNetworkType);
Robert Greenwaltec05b3c2009-10-30 14:17:42 -0700853 return -1;
854 }
855 if (usedNetworkType != networkType) {
Robert Greenwaltaffc3a12009-09-27 17:27:04 -0700856 Integer currentPid = new Integer(pid);
Robert Greenwaltaffc3a12009-09-27 17:27:04 -0700857 mNetRequestersPids[usedNetworkType].remove(currentPid);
Robert Greenwalt0ca68a02009-12-17 14:54:59 -0800858 reassessPidDns(pid, true);
Robert Greenwaltaffc3a12009-09-27 17:27:04 -0700859 if (mNetRequestersPids[usedNetworkType].size() != 0) {
Wink Savillee70c6f52010-12-03 12:01:38 -0800860 if (DBG) log("not tearing down special network - " +
Robert Greenwaltaffc3a12009-09-27 17:27:04 -0700861 "others still using it");
862 return 1;
863 }
864 callTeardown = true;
Robert Greenwalt9f3be4c2011-01-10 11:58:31 -0800865 } else {
866 if (DBG) log("not a known feature - dropping");
Robert Greenwaltaffc3a12009-09-27 17:27:04 -0700867 }
868 }
Wink Savillee70c6f52010-12-03 12:01:38 -0800869 if (DBG) log("Doing network teardown");
Robert Greenwaltaffc3a12009-09-27 17:27:04 -0700870 if (callTeardown) {
871 tracker.teardown();
Robert Greenwalt2034b912009-08-12 16:08:25 -0700872 return 1;
873 } else {
Robert Greenwaltd391e892010-05-18 10:52:51 -0700874 return -1;
Robert Greenwalt2034b912009-08-12 16:08:25 -0700875 }
The Android Open Source Project28527d22009-03-03 19:31:44 -0800876 }
877
878 /**
Robert Greenwalt7fe44cb2010-08-27 09:24:29 -0700879 * @deprecated use requestRouteToHostAddress instead
880 *
The Android Open Source Project28527d22009-03-03 19:31:44 -0800881 * Ensure that a network route exists to deliver traffic to the specified
882 * host via the specified network interface.
Robert Greenwalt0659da32009-07-16 17:21:39 -0700883 * @param networkType the type of the network over which traffic to the
884 * specified host is to be routed
885 * @param hostAddress the IP address of the host to which the route is
886 * desired
The Android Open Source Project28527d22009-03-03 19:31:44 -0800887 * @return {@code true} on success, {@code false} on failure
888 */
889 public boolean requestRouteToHost(int networkType, int hostAddress) {
Robert Greenwalt7fe44cb2010-08-27 09:24:29 -0700890 InetAddress inetAddress = NetworkUtils.intToInetAddress(hostAddress);
891
892 if (inetAddress == null) {
893 return false;
894 }
895
896 return requestRouteToHostAddress(networkType, inetAddress.getAddress());
897 }
898
899 /**
900 * Ensure that a network route exists to deliver traffic to the specified
901 * host via the specified network interface.
902 * @param networkType the type of the network over which traffic to the
903 * specified host is to be routed
904 * @param hostAddress the IP address of the host to which the route is
905 * desired
906 * @return {@code true} on success, {@code false} on failure
907 */
908 public boolean requestRouteToHostAddress(int networkType, byte[] hostAddress) {
The Android Open Source Project28527d22009-03-03 19:31:44 -0800909 enforceChangePermission();
Robert Greenwalt6cac0742011-06-21 17:26:14 -0700910 if (mProtectedNetworks.contains(networkType)) {
911 enforceConnectivityInternalPermission();
912 }
913
The Android Open Source Project28527d22009-03-03 19:31:44 -0800914 if (!ConnectivityManager.isNetworkTypeValid(networkType)) {
915 return false;
916 }
917 NetworkStateTracker tracker = mNetTrackers[networkType];
Robert Greenwalt4666ed02009-09-10 15:06:20 -0700918
Robert Greenwaltec05b3c2009-10-30 14:17:42 -0700919 if (tracker == null || !tracker.getNetworkInfo().isConnected() ||
920 tracker.isTeardownRequested()) {
Robert Greenwalt4666ed02009-09-10 15:06:20 -0700921 if (DBG) {
Wink Savillee70c6f52010-12-03 12:01:38 -0800922 log("requestRouteToHostAddress on down network " +
Robert Greenwalt7fe44cb2010-08-27 09:24:29 -0700923 "(" + networkType + ") - dropped");
Robert Greenwalt4666ed02009-09-10 15:06:20 -0700924 }
925 return false;
The Android Open Source Project28527d22009-03-03 19:31:44 -0800926 }
Robert Greenwalta7dfbd32010-06-15 15:43:39 -0700927 try {
Robert Greenwalt7fe44cb2010-08-27 09:24:29 -0700928 InetAddress addr = InetAddress.getByAddress(hostAddress);
Robert Greenwaltbd492212011-05-06 17:10:53 -0700929 return addHostRoute(tracker, addr, 0);
Robert Greenwalta7dfbd32010-06-15 15:43:39 -0700930 } catch (UnknownHostException e) {}
931 return false;
Irfan Sheriff7f132d92010-06-09 15:39:36 -0700932 }
933
934 /**
935 * Ensure that a network route exists to deliver traffic to the specified
936 * host via the mobile data network.
937 * @param hostAddress the IP address of the host to which the route is desired,
938 * in network byte order.
Robert Greenwalta7dfbd32010-06-15 15:43:39 -0700939 * TODO - deprecate
Irfan Sheriff7f132d92010-06-09 15:39:36 -0700940 * @return {@code true} on success, {@code false} on failure
941 */
Robert Greenwaltbd492212011-05-06 17:10:53 -0700942 private boolean addHostRoute(NetworkStateTracker nt, InetAddress hostAddress, int cycleCount) {
Robert Greenwaltbd492212011-05-06 17:10:53 -0700943 LinkProperties lp = nt.getLinkProperties();
944 if ((lp == null) || (hostAddress == null)) return false;
Irfan Sheriff7f132d92010-06-09 15:39:36 -0700945
Robert Greenwaltbd492212011-05-06 17:10:53 -0700946 String interfaceName = lp.getInterfaceName();
Irfan Sheriff7f132d92010-06-09 15:39:36 -0700947 if (DBG) {
Robert Greenwaltbd492212011-05-06 17:10:53 -0700948 log("Requested host route to " + hostAddress + "(" + interfaceName + "), cycleCount=" +
949 cycleCount);
Irfan Sheriff7f132d92010-06-09 15:39:36 -0700950 }
Robert Greenwaltbd492212011-05-06 17:10:53 -0700951 if (interfaceName == null) {
Wink Savillee70c6f52010-12-03 12:01:38 -0800952 if (DBG) loge("addHostRoute failed due to null interface name");
Irfan Sheriff7f132d92010-06-09 15:39:36 -0700953 return false;
954 }
Robert Greenwaltbd492212011-05-06 17:10:53 -0700955
956 RouteInfo bestRoute = RouteInfo.selectBestRoute(lp.getRoutes(), hostAddress);
Robert Greenwalt355205c2011-05-10 15:05:02 -0700957 InetAddress gatewayAddress = null;
Robert Greenwaltbd492212011-05-06 17:10:53 -0700958 if (bestRoute != null) {
Robert Greenwalt355205c2011-05-10 15:05:02 -0700959 gatewayAddress = bestRoute.getGateway();
Robert Greenwaltbd492212011-05-06 17:10:53 -0700960 // if the best route is ourself, don't relf-reference, just add the host route
Robert Greenwalt355205c2011-05-10 15:05:02 -0700961 if (hostAddress.equals(gatewayAddress)) gatewayAddress = null;
Robert Greenwaltbd492212011-05-06 17:10:53 -0700962 }
Robert Greenwalt355205c2011-05-10 15:05:02 -0700963 if (gatewayAddress != null) {
Robert Greenwaltbd492212011-05-06 17:10:53 -0700964 if (cycleCount > MAX_HOSTROUTE_CYCLE_COUNT) {
965 loge("Error adding hostroute - too much recursion");
966 return false;
967 }
Robert Greenwalt355205c2011-05-10 15:05:02 -0700968 if (!addHostRoute(nt, gatewayAddress, cycleCount+1)) return false;
Robert Greenwaltbd492212011-05-06 17:10:53 -0700969 }
Robert Greenwalt355205c2011-05-10 15:05:02 -0700970
971 RouteInfo route = RouteInfo.makeHostRoute(hostAddress, gatewayAddress);
972
973 try {
974 mNetd.addRoute(interfaceName, route);
975 return true;
976 } catch (Exception ex) {
977 return false;
978 }
Robert Greenwaltbd492212011-05-06 17:10:53 -0700979 }
980
981 // TODO support the removal of single host routes. Keep a ref count of them so we
982 // aren't over-zealous
983 private boolean removeHostRoute(NetworkStateTracker nt, InetAddress hostAddress) {
984 return false;
The Android Open Source Project28527d22009-03-03 19:31:44 -0800985 }
986
987 /**
988 * @see ConnectivityManager#getBackgroundDataSetting()
989 */
990 public boolean getBackgroundDataSetting() {
Robert Greenwaltd62c7002010-12-29 16:15:02 -0800991 return mBackgroundDataEnabled.get();
The Android Open Source Project28527d22009-03-03 19:31:44 -0800992 }
Robert Greenwalt0659da32009-07-16 17:21:39 -0700993
The Android Open Source Project28527d22009-03-03 19:31:44 -0800994 /**
995 * @see ConnectivityManager#setBackgroundDataSetting(boolean)
996 */
997 public void setBackgroundDataSetting(boolean allowBackgroundDataUsage) {
998 mContext.enforceCallingOrSelfPermission(
999 android.Manifest.permission.CHANGE_BACKGROUND_DATA_SETTING,
1000 "ConnectivityService");
Robert Greenwalt0659da32009-07-16 17:21:39 -07001001
Robert Greenwaltd62c7002010-12-29 16:15:02 -08001002 mBackgroundDataEnabled.set(allowBackgroundDataUsage);
1003
Robert Greenwalt6a2db8a2010-09-23 10:05:56 -07001004 mHandler.sendMessage(mHandler.obtainMessage(EVENT_SET_BACKGROUND_DATA,
1005 (allowBackgroundDataUsage ? ENABLED : DISABLED), 0));
1006 }
The Android Open Source Project28527d22009-03-03 19:31:44 -08001007
Robert Greenwalt6a2db8a2010-09-23 10:05:56 -07001008 private void handleSetBackgroundData(boolean enabled) {
Robert Greenwalt0ffdef12011-02-25 13:44:09 -08001009 Settings.Secure.putInt(mContext.getContentResolver(),
1010 Settings.Secure.BACKGROUND_DATA, enabled ? 1 : 0);
1011 Intent broadcast = new Intent(
1012 ConnectivityManager.ACTION_BACKGROUND_DATA_SETTING_CHANGED);
1013 mContext.sendBroadcast(broadcast);
Robert Greenwalt0659da32009-07-16 17:21:39 -07001014 }
1015
Robert Greenwalt1b21f6c2010-02-23 18:58:05 -08001016 /**
1017 * @see ConnectivityManager#getMobileDataEnabled()
1018 */
1019 public boolean getMobileDataEnabled() {
Wink Savilleb9024c62010-12-07 10:31:02 -08001020 // TODO: This detail should probably be in DataConnectionTracker's
1021 // which is where we store the value and maybe make this
1022 // asynchronous.
Robert Greenwalt1b21f6c2010-02-23 18:58:05 -08001023 enforceAccessPermission();
1024 boolean retVal = Settings.Secure.getInt(mContext.getContentResolver(),
1025 Settings.Secure.MOBILE_DATA, 1) == 1;
Wink Savillee70c6f52010-12-03 12:01:38 -08001026 if (DBG) log("getMobileDataEnabled returning " + retVal);
Robert Greenwalt1b21f6c2010-02-23 18:58:05 -08001027 return retVal;
1028 }
1029
Robert Greenwalt34848c02011-03-25 13:09:25 -07001030 public void setDataDependency(int networkType, boolean met) {
Robert Greenwalt6cac0742011-06-21 17:26:14 -07001031 enforceConnectivityInternalPermission();
1032
Robert Greenwalt34848c02011-03-25 13:09:25 -07001033 if (DBG) {
1034 log("setDataDependency(" + networkType + ", " + met + ")");
1035 }
1036 mHandler.sendMessage(mHandler.obtainMessage(EVENT_SET_DEPENDENCY_MET,
1037 (met ? ENABLED : DISABLED), networkType));
1038 }
1039
1040 private void handleSetDependencyMet(int networkType, boolean met) {
1041 if (mNetTrackers[networkType] != null) {
1042 if (DBG) {
1043 log("handleSetDependencyMet(" + networkType + ", " + met + ")");
1044 }
1045 mNetTrackers[networkType].setDependencyMet(met);
1046 }
1047 }
1048
Robert Greenwalt1b21f6c2010-02-23 18:58:05 -08001049 /**
1050 * @see ConnectivityManager#setMobileDataEnabled(boolean)
1051 */
Robert Greenwalt6a2db8a2010-09-23 10:05:56 -07001052 public void setMobileDataEnabled(boolean enabled) {
Robert Greenwalt1b21f6c2010-02-23 18:58:05 -08001053 enforceChangePermission();
Wink Savillee70c6f52010-12-03 12:01:38 -08001054 if (DBG) log("setMobileDataEnabled(" + enabled + ")");
Robert Greenwalt1b21f6c2010-02-23 18:58:05 -08001055
Robert Greenwalt6a2db8a2010-09-23 10:05:56 -07001056 mHandler.sendMessage(mHandler.obtainMessage(EVENT_SET_MOBILE_DATA,
Robert Greenwalt34848c02011-03-25 13:09:25 -07001057 (enabled ? ENABLED : DISABLED), 0));
Robert Greenwalt6a2db8a2010-09-23 10:05:56 -07001058 }
1059
1060 private void handleSetMobileData(boolean enabled) {
Wink Savilleb9024c62010-12-07 10:31:02 -08001061 if (mNetTrackers[ConnectivityManager.TYPE_MOBILE] != null) {
1062 if (DBG) {
1063 Slog.d(TAG, mNetTrackers[ConnectivityManager.TYPE_MOBILE].toString() + enabled);
Robert Greenwalt1b21f6c2010-02-23 18:58:05 -08001064 }
Wink Savilleb9024c62010-12-07 10:31:02 -08001065 mNetTrackers[ConnectivityManager.TYPE_MOBILE].setDataEnable(enabled);
Robert Greenwalt1b21f6c2010-02-23 18:58:05 -08001066 }
1067 }
1068
The Android Open Source Project28527d22009-03-03 19:31:44 -08001069 private void enforceAccessPermission() {
Robert Greenwalt0659da32009-07-16 17:21:39 -07001070 mContext.enforceCallingOrSelfPermission(
1071 android.Manifest.permission.ACCESS_NETWORK_STATE,
1072 "ConnectivityService");
The Android Open Source Project28527d22009-03-03 19:31:44 -08001073 }
1074
1075 private void enforceChangePermission() {
Robert Greenwalt0659da32009-07-16 17:21:39 -07001076 mContext.enforceCallingOrSelfPermission(
1077 android.Manifest.permission.CHANGE_NETWORK_STATE,
1078 "ConnectivityService");
The Android Open Source Project28527d22009-03-03 19:31:44 -08001079 }
1080
Robert Greenwalt0c4828c2010-01-26 11:40:34 -08001081 // TODO Make this a special check when it goes public
1082 private void enforceTetherChangePermission() {
1083 mContext.enforceCallingOrSelfPermission(
1084 android.Manifest.permission.CHANGE_NETWORK_STATE,
1085 "ConnectivityService");
1086 }
1087
Robert Greenwalt8e87f122010-02-11 18:18:40 -08001088 private void enforceTetherAccessPermission() {
1089 mContext.enforceCallingOrSelfPermission(
1090 android.Manifest.permission.ACCESS_NETWORK_STATE,
1091 "ConnectivityService");
1092 }
1093
Robert Greenwalt93dc1042010-06-15 12:19:37 -07001094 private void enforceConnectivityInternalPermission() {
1095 mContext.enforceCallingOrSelfPermission(
1096 android.Manifest.permission.CONNECTIVITY_INTERNAL,
1097 "ConnectivityService");
1098 }
1099
The Android Open Source Project28527d22009-03-03 19:31:44 -08001100 /**
Robert Greenwalt0659da32009-07-16 17:21:39 -07001101 * Handle a {@code DISCONNECTED} event. If this pertains to the non-active
1102 * network, we ignore it. If it is for the active network, we send out a
1103 * broadcast. But first, we check whether it might be possible to connect
1104 * to a different network.
The Android Open Source Project28527d22009-03-03 19:31:44 -08001105 * @param info the {@code NetworkInfo} for the network
1106 */
1107 private void handleDisconnect(NetworkInfo info) {
1108
Robert Greenwalt2034b912009-08-12 16:08:25 -07001109 int prevNetType = info.getType();
The Android Open Source Project28527d22009-03-03 19:31:44 -08001110
Robert Greenwalt2034b912009-08-12 16:08:25 -07001111 mNetTrackers[prevNetType].setTeardownRequested(false);
The Android Open Source Project28527d22009-03-03 19:31:44 -08001112 /*
1113 * If the disconnected network is not the active one, then don't report
1114 * this as a loss of connectivity. What probably happened is that we're
1115 * getting the disconnect for a network that we explicitly disabled
1116 * in accordance with network preference policies.
1117 */
Robert Greenwalt34848c02011-03-25 13:09:25 -07001118 if (!mNetConfigs[prevNetType].isDefault()) {
Robert Greenwalt2034b912009-08-12 16:08:25 -07001119 List pids = mNetRequestersPids[prevNetType];
1120 for (int i = 0; i<pids.size(); i++) {
1121 Integer pid = (Integer)pids.get(i);
1122 // will remove them because the net's no longer connected
1123 // need to do this now as only now do we know the pids and
1124 // can properly null things that are no longer referenced.
1125 reassessPidDns(pid.intValue(), false);
The Android Open Source Project28527d22009-03-03 19:31:44 -08001126 }
The Android Open Source Project28527d22009-03-03 19:31:44 -08001127 }
1128
The Android Open Source Project28527d22009-03-03 19:31:44 -08001129 Intent intent = new Intent(ConnectivityManager.CONNECTIVITY_ACTION);
1130 intent.putExtra(ConnectivityManager.EXTRA_NETWORK_INFO, info);
1131 if (info.isFailover()) {
1132 intent.putExtra(ConnectivityManager.EXTRA_IS_FAILOVER, true);
1133 info.setFailover(false);
1134 }
1135 if (info.getReason() != null) {
1136 intent.putExtra(ConnectivityManager.EXTRA_REASON, info.getReason());
1137 }
1138 if (info.getExtraInfo() != null) {
Robert Greenwalt0659da32009-07-16 17:21:39 -07001139 intent.putExtra(ConnectivityManager.EXTRA_EXTRA_INFO,
1140 info.getExtraInfo());
The Android Open Source Project28527d22009-03-03 19:31:44 -08001141 }
Robert Greenwalt2034b912009-08-12 16:08:25 -07001142
Robert Greenwalt34848c02011-03-25 13:09:25 -07001143 if (mNetConfigs[prevNetType].isDefault()) {
Robert Greenwalt4b7c55e2011-01-11 13:56:33 -08001144 tryFailover(prevNetType);
1145 if (mActiveDefaultNetwork != -1) {
1146 NetworkInfo switchTo = mNetTrackers[mActiveDefaultNetwork].getNetworkInfo();
Robert Greenwalt3cc68d32010-01-25 17:54:29 -08001147 intent.putExtra(ConnectivityManager.EXTRA_OTHER_NETWORK_INFO, switchTo);
1148 } else {
Robert Greenwaltb2a9e492010-09-20 18:01:43 -07001149 mDefaultInetConditionPublished = 0; // we're not connected anymore
Robert Greenwalt3cc68d32010-01-25 17:54:29 -08001150 intent.putExtra(ConnectivityManager.EXTRA_NO_CONNECTIVITY, true);
1151 }
Robert Greenwaltf55ced92010-01-20 19:29:41 -08001152 }
Robert Greenwaltb2a9e492010-09-20 18:01:43 -07001153 intent.putExtra(ConnectivityManager.EXTRA_INET_CONDITION, mDefaultInetConditionPublished);
Robert Greenwalt36ea8692011-06-15 12:22:07 -07001154
1155 // Reset interface if no other connections are using the same interface
1156 boolean doReset = true;
1157 LinkProperties linkProperties = mNetTrackers[prevNetType].getLinkProperties();
1158 if (linkProperties != null) {
1159 String oldIface = linkProperties.getInterfaceName();
1160 if (TextUtils.isEmpty(oldIface) == false) {
1161 for (NetworkStateTracker networkStateTracker : mNetTrackers) {
1162 if (networkStateTracker == null) continue;
1163 NetworkInfo networkInfo = networkStateTracker.getNetworkInfo();
1164 if (networkInfo.isConnected() && networkInfo.getType() != prevNetType) {
1165 LinkProperties l = networkStateTracker.getLinkProperties();
1166 if (l == null) continue;
1167 if (oldIface.equals(l.getInterfaceName())) {
1168 doReset = false;
1169 break;
1170 }
1171 }
1172 }
1173 }
1174 }
1175
Robert Greenwaltf55ced92010-01-20 19:29:41 -08001176 // do this before we broadcast the change
Robert Greenwalt36ea8692011-06-15 12:22:07 -07001177 handleConnectivityChange(prevNetType, doReset);
Robert Greenwaltf55ced92010-01-20 19:29:41 -08001178
1179 sendStickyBroadcast(intent);
1180 /*
1181 * If the failover network is already connected, then immediately send
1182 * out a followup broadcast indicating successful failover
1183 */
Robert Greenwalt4b7c55e2011-01-11 13:56:33 -08001184 if (mActiveDefaultNetwork != -1) {
1185 sendConnectedBroadcast(mNetTrackers[mActiveDefaultNetwork].getNetworkInfo());
Robert Greenwaltf55ced92010-01-20 19:29:41 -08001186 }
1187 }
1188
Robert Greenwalt4b7c55e2011-01-11 13:56:33 -08001189 private void tryFailover(int prevNetType) {
Robert Greenwalt2034b912009-08-12 16:08:25 -07001190 /*
Robert Greenwalt92564852011-01-06 15:41:07 -08001191 * If this is a default network, check if other defaults are available.
1192 * Try to reconnect on all available and let them hash it out when
1193 * more than one connects.
Robert Greenwalt2034b912009-08-12 16:08:25 -07001194 */
Robert Greenwalt34848c02011-03-25 13:09:25 -07001195 if (mNetConfigs[prevNetType].isDefault()) {
Robert Greenwalt2034b912009-08-12 16:08:25 -07001196 if (mActiveDefaultNetwork == prevNetType) {
1197 mActiveDefaultNetwork = -1;
1198 }
1199
Robert Greenwalt4b7c55e2011-01-11 13:56:33 -08001200 // don't signal a reconnect for anything lower or equal priority than our
1201 // current connected default
1202 // TODO - don't filter by priority now - nice optimization but risky
1203// int currentPriority = -1;
1204// if (mActiveDefaultNetwork != -1) {
Robert Greenwalt34848c02011-03-25 13:09:25 -07001205// currentPriority = mNetConfigs[mActiveDefaultNetwork].mPriority;
Robert Greenwalt4b7c55e2011-01-11 13:56:33 -08001206// }
Robert Greenwaltf55ced92010-01-20 19:29:41 -08001207 for (int checkType=0; checkType <= ConnectivityManager.MAX_NETWORK_TYPE; checkType++) {
Robert Greenwaltec05b3c2009-10-30 14:17:42 -07001208 if (checkType == prevNetType) continue;
Robert Greenwalt34848c02011-03-25 13:09:25 -07001209 if (mNetConfigs[checkType] == null) continue;
1210 if (!mNetConfigs[checkType].isDefault()) continue;
Wink Saville72a95b92011-01-26 15:43:49 -08001211
1212// Enabling the isAvailable() optimization caused mobile to not get
1213// selected if it was in the middle of error handling. Specifically
1214// a moble connection that took 30 seconds to complete the DEACTIVATE_DATA_CALL
1215// would not be available and we wouldn't get connected to anything.
1216// So removing the isAvailable() optimization below for now. TODO: This
1217// optimization should work and we need to investigate why it doesn't work.
1218// This could be related to how DEACTIVATE_DATA_CALL is reporting its
1219// complete before it is really complete.
1220// if (!mNetTrackers[checkType].isAvailable()) continue;
1221
Robert Greenwalt34848c02011-03-25 13:09:25 -07001222// if (currentPriority >= mNetConfigs[checkType].mPriority) continue;
Robert Greenwalt2034b912009-08-12 16:08:25 -07001223
Robert Greenwalt4b7c55e2011-01-11 13:56:33 -08001224 NetworkStateTracker checkTracker = mNetTrackers[checkType];
1225 NetworkInfo checkInfo = checkTracker.getNetworkInfo();
1226 if (!checkInfo.isConnectedOrConnecting() || checkTracker.isTeardownRequested()) {
1227 checkInfo.setFailover(true);
1228 checkTracker.reconnect();
Robert Greenwalt2034b912009-08-12 16:08:25 -07001229 }
Robert Greenwalt4b7c55e2011-01-11 13:56:33 -08001230 if (DBG) log("Attempting to switch to " + checkInfo.getTypeName());
Robert Greenwalt2034b912009-08-12 16:08:25 -07001231 }
The Android Open Source Project28527d22009-03-03 19:31:44 -08001232 }
The Android Open Source Project28527d22009-03-03 19:31:44 -08001233 }
1234
1235 private void sendConnectedBroadcast(NetworkInfo info) {
Robert Greenwaltd3401f92010-09-15 17:36:33 -07001236 sendGeneralBroadcast(info, ConnectivityManager.CONNECTIVITY_ACTION);
1237 }
1238
1239 private void sendInetConditionBroadcast(NetworkInfo info) {
1240 sendGeneralBroadcast(info, ConnectivityManager.INET_CONDITION_ACTION);
1241 }
1242
1243 private void sendGeneralBroadcast(NetworkInfo info, String bcastType) {
1244 Intent intent = new Intent(bcastType);
The Android Open Source Project28527d22009-03-03 19:31:44 -08001245 intent.putExtra(ConnectivityManager.EXTRA_NETWORK_INFO, info);
1246 if (info.isFailover()) {
1247 intent.putExtra(ConnectivityManager.EXTRA_IS_FAILOVER, true);
1248 info.setFailover(false);
1249 }
1250 if (info.getReason() != null) {
1251 intent.putExtra(ConnectivityManager.EXTRA_REASON, info.getReason());
1252 }
1253 if (info.getExtraInfo() != null) {
Robert Greenwalt0659da32009-07-16 17:21:39 -07001254 intent.putExtra(ConnectivityManager.EXTRA_EXTRA_INFO,
1255 info.getExtraInfo());
The Android Open Source Project28527d22009-03-03 19:31:44 -08001256 }
Robert Greenwalt986c7412010-09-08 15:24:47 -07001257 intent.putExtra(ConnectivityManager.EXTRA_INET_CONDITION, mDefaultInetConditionPublished);
Mike Lockwoodfde2b762009-08-14 14:18:49 -04001258 sendStickyBroadcast(intent);
The Android Open Source Project28527d22009-03-03 19:31:44 -08001259 }
1260
1261 /**
1262 * Called when an attempt to fail over to another network has failed.
1263 * @param info the {@link NetworkInfo} for the failed network
1264 */
1265 private void handleConnectionFailure(NetworkInfo info) {
1266 mNetTrackers[info.getType()].setTeardownRequested(false);
The Android Open Source Project28527d22009-03-03 19:31:44 -08001267
Robert Greenwalt2034b912009-08-12 16:08:25 -07001268 String reason = info.getReason();
1269 String extraInfo = info.getExtraInfo();
Robert Greenwalt0659da32009-07-16 17:21:39 -07001270
Robert Greenwalte981bc52010-10-08 16:35:52 -07001271 String reasonText;
1272 if (reason == null) {
1273 reasonText = ".";
1274 } else {
1275 reasonText = " (" + reason + ").";
The Android Open Source Project28527d22009-03-03 19:31:44 -08001276 }
Wink Savillee70c6f52010-12-03 12:01:38 -08001277 loge("Attempt to connect to " + info.getTypeName() + " failed" + reasonText);
Robert Greenwalt2034b912009-08-12 16:08:25 -07001278
1279 Intent intent = new Intent(ConnectivityManager.CONNECTIVITY_ACTION);
1280 intent.putExtra(ConnectivityManager.EXTRA_NETWORK_INFO, info);
1281 if (getActiveNetworkInfo() == null) {
1282 intent.putExtra(ConnectivityManager.EXTRA_NO_CONNECTIVITY, true);
1283 }
1284 if (reason != null) {
1285 intent.putExtra(ConnectivityManager.EXTRA_REASON, reason);
1286 }
1287 if (extraInfo != null) {
1288 intent.putExtra(ConnectivityManager.EXTRA_EXTRA_INFO, extraInfo);
1289 }
1290 if (info.isFailover()) {
1291 intent.putExtra(ConnectivityManager.EXTRA_IS_FAILOVER, true);
1292 info.setFailover(false);
1293 }
Robert Greenwaltf55ced92010-01-20 19:29:41 -08001294
Robert Greenwalt34848c02011-03-25 13:09:25 -07001295 if (mNetConfigs[info.getType()].isDefault()) {
Robert Greenwalt4b7c55e2011-01-11 13:56:33 -08001296 tryFailover(info.getType());
1297 if (mActiveDefaultNetwork != -1) {
1298 NetworkInfo switchTo = mNetTrackers[mActiveDefaultNetwork].getNetworkInfo();
Robert Greenwalt3cc68d32010-01-25 17:54:29 -08001299 intent.putExtra(ConnectivityManager.EXTRA_OTHER_NETWORK_INFO, switchTo);
1300 } else {
Robert Greenwaltb2a9e492010-09-20 18:01:43 -07001301 mDefaultInetConditionPublished = 0;
Robert Greenwalt3cc68d32010-01-25 17:54:29 -08001302 intent.putExtra(ConnectivityManager.EXTRA_NO_CONNECTIVITY, true);
1303 }
Robert Greenwaltf55ced92010-01-20 19:29:41 -08001304 }
Robert Greenwalt3cc68d32010-01-25 17:54:29 -08001305
Robert Greenwaltb2a9e492010-09-20 18:01:43 -07001306 intent.putExtra(ConnectivityManager.EXTRA_INET_CONDITION, mDefaultInetConditionPublished);
Mike Lockwoodfde2b762009-08-14 14:18:49 -04001307 sendStickyBroadcast(intent);
Robert Greenwaltf55ced92010-01-20 19:29:41 -08001308 /*
1309 * If the failover network is already connected, then immediately send
1310 * out a followup broadcast indicating successful failover
1311 */
Robert Greenwalt4b7c55e2011-01-11 13:56:33 -08001312 if (mActiveDefaultNetwork != -1) {
1313 sendConnectedBroadcast(mNetTrackers[mActiveDefaultNetwork].getNetworkInfo());
Robert Greenwaltf55ced92010-01-20 19:29:41 -08001314 }
Mike Lockwoodfde2b762009-08-14 14:18:49 -04001315 }
1316
1317 private void sendStickyBroadcast(Intent intent) {
1318 synchronized(this) {
Dianne Hackborna417ff82009-12-08 19:45:14 -08001319 if (!mSystemReady) {
1320 mInitialBroadcast = new Intent(intent);
Mike Lockwoodfde2b762009-08-14 14:18:49 -04001321 }
Dianne Hackborna417ff82009-12-08 19:45:14 -08001322 intent.addFlags(Intent.FLAG_RECEIVER_REGISTERED_ONLY_BEFORE_BOOT);
1323 mContext.sendStickyBroadcast(intent);
Mike Lockwoodfde2b762009-08-14 14:18:49 -04001324 }
1325 }
1326
1327 void systemReady() {
Robert Greenwalt355205c2011-05-10 15:05:02 -07001328 IBinder b = ServiceManager.getService(Context.NETWORKMANAGEMENT_SERVICE);
1329 mNetd = INetworkManagementService.Stub.asInterface(b);
1330
Mike Lockwoodfde2b762009-08-14 14:18:49 -04001331 synchronized(this) {
1332 mSystemReady = true;
Dianne Hackborna417ff82009-12-08 19:45:14 -08001333 if (mInitialBroadcast != null) {
1334 mContext.sendStickyBroadcast(mInitialBroadcast);
1335 mInitialBroadcast = null;
Mike Lockwoodfde2b762009-08-14 14:18:49 -04001336 }
1337 }
Robert Greenwaltc3c5f862010-10-11 16:00:27 -07001338 // load the global proxy at startup
1339 mHandler.sendMessage(mHandler.obtainMessage(EVENT_APPLY_GLOBAL_HTTP_PROXY));
The Android Open Source Project28527d22009-03-03 19:31:44 -08001340 }
1341
1342 private void handleConnect(NetworkInfo info) {
Robert Greenwalt2034b912009-08-12 16:08:25 -07001343 int type = info.getType();
The Android Open Source Project28527d22009-03-03 19:31:44 -08001344
1345 // snapshot isFailover, because sendConnectedBroadcast() resets it
1346 boolean isFailover = info.isFailover();
Robert Greenwalt2034b912009-08-12 16:08:25 -07001347 NetworkStateTracker thisNet = mNetTrackers[type];
The Android Open Source Project28527d22009-03-03 19:31:44 -08001348
Robert Greenwalt2034b912009-08-12 16:08:25 -07001349 // if this is a default net and other default is running
1350 // kill the one not preferred
Robert Greenwalt34848c02011-03-25 13:09:25 -07001351 if (mNetConfigs[type].isDefault()) {
Robert Greenwalt2034b912009-08-12 16:08:25 -07001352 if (mActiveDefaultNetwork != -1 && mActiveDefaultNetwork != type) {
1353 if ((type != mNetworkPreference &&
Wink Savillef2a62832011-04-07 14:23:45 -07001354 mNetConfigs[mActiveDefaultNetwork].priority >
1355 mNetConfigs[type].priority) ||
Robert Greenwalt2034b912009-08-12 16:08:25 -07001356 mNetworkPreference == mActiveDefaultNetwork) {
1357 // don't accept this one
Wink Savillee70c6f52010-12-03 12:01:38 -08001358 if (DBG) {
1359 log("Not broadcasting CONNECT_ACTION " +
Robert Greenwalt2034b912009-08-12 16:08:25 -07001360 "to torn down network " + info.getTypeName());
Wink Savillee70c6f52010-12-03 12:01:38 -08001361 }
Robert Greenwalt2034b912009-08-12 16:08:25 -07001362 teardown(thisNet);
1363 return;
1364 } else {
1365 // tear down the other
1366 NetworkStateTracker otherNet =
1367 mNetTrackers[mActiveDefaultNetwork];
Wink Savillee70c6f52010-12-03 12:01:38 -08001368 if (DBG) {
1369 log("Policy requires " + otherNet.getNetworkInfo().getTypeName() +
Robert Greenwalt2034b912009-08-12 16:08:25 -07001370 " teardown");
Wink Savillee70c6f52010-12-03 12:01:38 -08001371 }
Robert Greenwalt2034b912009-08-12 16:08:25 -07001372 if (!teardown(otherNet)) {
Wink Savillee70c6f52010-12-03 12:01:38 -08001373 loge("Network declined teardown request");
Robert Greenwalt2034b912009-08-12 16:08:25 -07001374 return;
1375 }
Robert Greenwalt93dc1042010-06-15 12:19:37 -07001376 }
1377 }
1378 synchronized (ConnectivityService.this) {
1379 // have a new default network, release the transition wakelock in a second
1380 // if it's held. The second pause is to allow apps to reconnect over the
1381 // new network
1382 if (mNetTransitionWakeLock.isHeld()) {
1383 mHandler.sendMessageDelayed(mHandler.obtainMessage(
Robert Greenwaltccb36f92010-09-24 14:32:21 -07001384 EVENT_CLEAR_NET_TRANSITION_WAKELOCK,
Robert Greenwalt93dc1042010-06-15 12:19:37 -07001385 mNetTransitionWakeLockSerialNumber, 0),
1386 1000);
Robert Greenwalt2034b912009-08-12 16:08:25 -07001387 }
1388 }
1389 mActiveDefaultNetwork = type;
Robert Greenwalt986c7412010-09-08 15:24:47 -07001390 // this will cause us to come up initially as unconnected and switching
1391 // to connected after our normal pause unless somebody reports us as reall
1392 // disconnected
1393 mDefaultInetConditionPublished = 0;
1394 mDefaultConnectionSequence++;
1395 mInetConditionChangeInFlight = false;
1396 // Don't do this - if we never sign in stay, grey
1397 //reportNetworkCondition(mActiveDefaultNetwork, 100);
Robert Greenwalt2034b912009-08-12 16:08:25 -07001398 }
The Android Open Source Project28527d22009-03-03 19:31:44 -08001399 thisNet.setTeardownRequested(false);
Irfan Sheriff7f132d92010-06-09 15:39:36 -07001400 updateNetworkSettings(thisNet);
Robert Greenwalt36ea8692011-06-15 12:22:07 -07001401 handleConnectivityChange(type, false);
Robert Greenwalt2034b912009-08-12 16:08:25 -07001402 sendConnectedBroadcast(info);
The Android Open Source Project28527d22009-03-03 19:31:44 -08001403 }
1404
The Android Open Source Project28527d22009-03-03 19:31:44 -08001405 /**
Robert Greenwalt3afbead2010-07-23 15:46:26 -07001406 * After a change in the connectivity state of a network. We're mainly
1407 * concerned with making sure that the list of DNS servers is set up
1408 * according to which networks are connected, and ensuring that the
1409 * right routing table entries exist.
The Android Open Source Project28527d22009-03-03 19:31:44 -08001410 */
Robert Greenwalt36ea8692011-06-15 12:22:07 -07001411 private void handleConnectivityChange(int netType, boolean doReset) {
The Android Open Source Project28527d22009-03-03 19:31:44 -08001412 /*
Robert Greenwalt2034b912009-08-12 16:08:25 -07001413 * If a non-default network is enabled, add the host routes that
Robert Greenwalt3afbead2010-07-23 15:46:26 -07001414 * will allow it's DNS servers to be accessed.
The Android Open Source Project28527d22009-03-03 19:31:44 -08001415 */
Robert Greenwalt3afbead2010-07-23 15:46:26 -07001416 handleDnsConfigurationChange(netType);
Robert Greenwalt2034b912009-08-12 16:08:25 -07001417
Robert Greenwalt3afbead2010-07-23 15:46:26 -07001418 if (mNetTrackers[netType].getNetworkInfo().isConnected()) {
Robert Greenwalt34848c02011-03-25 13:09:25 -07001419 if (mNetConfigs[netType].isDefault()) {
Robert Greenwaltc3c5f862010-10-11 16:00:27 -07001420 handleApplyDefaultProxy(netType);
Robert Greenwalt3afbead2010-07-23 15:46:26 -07001421 addDefaultRoute(mNetTrackers[netType]);
Robert Greenwalt2034b912009-08-12 16:08:25 -07001422 } else {
Robert Greenwalt3afbead2010-07-23 15:46:26 -07001423 addPrivateDnsRoutes(mNetTrackers[netType]);
1424 }
1425 } else {
Robert Greenwalt34848c02011-03-25 13:09:25 -07001426 if (mNetConfigs[netType].isDefault()) {
Robert Greenwalt3afbead2010-07-23 15:46:26 -07001427 removeDefaultRoute(mNetTrackers[netType]);
1428 } else {
1429 removePrivateDnsRoutes(mNetTrackers[netType]);
Robert Greenwalt2034b912009-08-12 16:08:25 -07001430 }
The Android Open Source Project28527d22009-03-03 19:31:44 -08001431 }
Robert Greenwalt36ea8692011-06-15 12:22:07 -07001432
1433 if (doReset) {
1434 LinkProperties linkProperties = mNetTrackers[netType].getLinkProperties();
1435 if (linkProperties != null) {
1436 String iface = linkProperties.getInterfaceName();
1437 if (TextUtils.isEmpty(iface) == false) {
1438 if (DBG) log("resetConnections(" + iface + ")");
1439 NetworkUtils.resetConnections(iface);
1440 }
1441 }
1442 }
Kazuhiro Ondo07680062011-06-22 21:10:34 -05001443
1444 // TODO: Temporary notifying upstread change to Tethering.
1445 // @see bug/4455071
1446 /** Notify TetheringService if interface name has been changed. */
1447 if (TextUtils.equals(mNetTrackers[netType].getNetworkInfo().getReason(),
1448 Phone.REASON_LINK_PROPERTIES_CHANGED)) {
1449 if (isTetheringSupported()) {
1450 mTethering.handleTetherIfaceChange();
1451 }
1452 }
The Android Open Source Project28527d22009-03-03 19:31:44 -08001453 }
1454
Irfan Sheriff7f132d92010-06-09 15:39:36 -07001455 private void addPrivateDnsRoutes(NetworkStateTracker nt) {
Irfan Sheriff7f132d92010-06-09 15:39:36 -07001456 boolean privateDnsRouteSet = nt.isPrivateDnsRouteSet();
Robert Greenwalt1f1bcfe2010-08-30 10:56:47 -07001457 LinkProperties p = nt.getLinkProperties();
Robert Greenwalta7dfbd32010-06-15 15:43:39 -07001458 if (p == null) return;
1459 String interfaceName = p.getInterfaceName();
Irfan Sheriff7f132d92010-06-09 15:39:36 -07001460
1461 if (DBG) {
Wink Savillee70c6f52010-12-03 12:01:38 -08001462 log("addPrivateDnsRoutes for " + nt +
Irfan Sheriff7f132d92010-06-09 15:39:36 -07001463 "(" + interfaceName + ") - mPrivateDnsRouteSet = " + privateDnsRouteSet);
1464 }
Irfan Sheriff7f132d92010-06-09 15:39:36 -07001465 if (interfaceName != null && !privateDnsRouteSet) {
Robert Greenwalta7dfbd32010-06-15 15:43:39 -07001466 Collection<InetAddress> dnsList = p.getDnses();
1467 for (InetAddress dns : dnsList) {
Robert Greenwaltbd492212011-05-06 17:10:53 -07001468 addHostRoute(nt, dns, 0);
Irfan Sheriff7f132d92010-06-09 15:39:36 -07001469 }
1470 nt.privateDnsRouteSet(true);
1471 }
1472 }
1473
1474 private void removePrivateDnsRoutes(NetworkStateTracker nt) {
Robert Greenwalt1f1bcfe2010-08-30 10:56:47 -07001475 LinkProperties p = nt.getLinkProperties();
Robert Greenwalta7dfbd32010-06-15 15:43:39 -07001476 if (p == null) return;
1477 String interfaceName = p.getInterfaceName();
Irfan Sheriff7f132d92010-06-09 15:39:36 -07001478 boolean privateDnsRouteSet = nt.isPrivateDnsRouteSet();
1479 if (interfaceName != null && privateDnsRouteSet) {
1480 if (DBG) {
Wink Savillee70c6f52010-12-03 12:01:38 -08001481 log("removePrivateDnsRoutes for " + nt.getNetworkInfo().getTypeName() +
Irfan Sheriff7f132d92010-06-09 15:39:36 -07001482 " (" + interfaceName + ")");
1483 }
Robert Greenwalt355205c2011-05-10 15:05:02 -07001484
1485 Collection<InetAddress> dnsList = p.getDnses();
1486 for (InetAddress dns : dnsList) {
1487 if (DBG) log(" removing " + dns);
1488 RouteInfo route = RouteInfo.makeHostRoute(dns);
1489 try {
1490 mNetd.removeRoute(interfaceName, route);
1491 } catch (Exception ex) {
1492 loge("error (" + ex + ") removing dns route " + route);
1493 }
1494 }
Irfan Sheriff7f132d92010-06-09 15:39:36 -07001495 nt.privateDnsRouteSet(false);
1496 }
1497 }
1498
Irfan Sheriff7f132d92010-06-09 15:39:36 -07001499
1500 private void addDefaultRoute(NetworkStateTracker nt) {
Robert Greenwalt1f1bcfe2010-08-30 10:56:47 -07001501 LinkProperties p = nt.getLinkProperties();
Robert Greenwalta7dfbd32010-06-15 15:43:39 -07001502 if (p == null) return;
1503 String interfaceName = p.getInterfaceName();
Robert Greenwalt5c733972011-02-09 13:56:06 -08001504 if (TextUtils.isEmpty(interfaceName)) return;
Irfan Sheriff7f132d92010-06-09 15:39:36 -07001505
Robert Greenwalt355205c2011-05-10 15:05:02 -07001506 for (RouteInfo route : p.getRoutes()) {
Robert Greenwalt5a901292011-04-28 14:28:50 -07001507 //TODO - handle non-default routes
1508 if (route.isDefaultRoute()) {
Robert Greenwalt355205c2011-05-10 15:05:02 -07001509 if (DBG) log("adding default route " + route);
Robert Greenwalt5a901292011-04-28 14:28:50 -07001510 InetAddress gateway = route.getGateway();
Robert Greenwalt355205c2011-05-10 15:05:02 -07001511 if (addHostRoute(nt, gateway, 0)) {
1512 try {
1513 mNetd.addRoute(interfaceName, route);
1514 } catch (Exception e) {
1515 loge("error adding default route " + route);
1516 continue;
1517 }
Robert Greenwalt5a901292011-04-28 14:28:50 -07001518 if (DBG) {
1519 NetworkInfo networkInfo = nt.getNetworkInfo();
1520 log("addDefaultRoute for " + networkInfo.getTypeName() +
1521 " (" + interfaceName + "), GatewayAddr=" +
1522 gateway.getHostAddress());
1523 }
Robert Greenwalt355205c2011-05-10 15:05:02 -07001524 } else {
1525 loge("error adding host route for default route " + route);
Robert Greenwalt03d53da2011-03-22 18:47:42 -07001526 }
Irfan Sheriff7f132d92010-06-09 15:39:36 -07001527 }
Irfan Sheriff7f132d92010-06-09 15:39:36 -07001528 }
1529 }
1530
1531
1532 public void removeDefaultRoute(NetworkStateTracker nt) {
Robert Greenwalt1f1bcfe2010-08-30 10:56:47 -07001533 LinkProperties p = nt.getLinkProperties();
Robert Greenwalta7dfbd32010-06-15 15:43:39 -07001534 if (p == null) return;
1535 String interfaceName = p.getInterfaceName();
Irfan Sheriff7f132d92010-06-09 15:39:36 -07001536
Robert Greenwalt355205c2011-05-10 15:05:02 -07001537 if (interfaceName == null) return;
1538
1539 for (RouteInfo route : p.getRoutes()) {
1540 //TODO - handle non-default routes
1541 if (route.isDefaultRoute()) {
1542 try {
1543 mNetd.removeRoute(interfaceName, route);
1544 } catch (Exception ex) {
1545 loge("error (" + ex + ") removing default route " + route);
1546 continue;
1547 }
Robert Greenwalt03d53da2011-03-22 18:47:42 -07001548 if (DBG) {
1549 NetworkInfo networkInfo = nt.getNetworkInfo();
1550 log("removeDefaultRoute for " + networkInfo.getTypeName() + " (" +
1551 interfaceName + ")");
1552 }
Irfan Sheriff7f132d92010-06-09 15:39:36 -07001553 }
Irfan Sheriff7f132d92010-06-09 15:39:36 -07001554 }
1555 }
1556
1557 /**
1558 * Reads the network specific TCP buffer sizes from SystemProperties
1559 * net.tcp.buffersize.[default|wifi|umts|edge|gprs] and set them for system
1560 * wide use
1561 */
1562 public void updateNetworkSettings(NetworkStateTracker nt) {
1563 String key = nt.getTcpBufferSizesPropName();
1564 String bufferSizes = SystemProperties.get(key);
1565
1566 if (bufferSizes.length() == 0) {
Wink Savillee70c6f52010-12-03 12:01:38 -08001567 loge(key + " not found in system properties. Using defaults");
Irfan Sheriff7f132d92010-06-09 15:39:36 -07001568
1569 // Setting to default values so we won't be stuck to previous values
1570 key = "net.tcp.buffersize.default";
1571 bufferSizes = SystemProperties.get(key);
1572 }
1573
1574 // Set values in kernel
1575 if (bufferSizes.length() != 0) {
1576 if (DBG) {
Wink Savillee70c6f52010-12-03 12:01:38 -08001577 log("Setting TCP values: [" + bufferSizes
Irfan Sheriff7f132d92010-06-09 15:39:36 -07001578 + "] which comes from [" + key + "]");
1579 }
1580 setBufferSize(bufferSizes);
1581 }
1582 }
1583
1584 /**
1585 * Writes TCP buffer sizes to /sys/kernel/ipv4/tcp_[r/w]mem_[min/def/max]
1586 * which maps to /proc/sys/net/ipv4/tcp_rmem and tcpwmem
1587 *
1588 * @param bufferSizes in the format of "readMin, readInitial, readMax,
1589 * writeMin, writeInitial, writeMax"
1590 */
1591 private void setBufferSize(String bufferSizes) {
1592 try {
1593 String[] values = bufferSizes.split(",");
1594
1595 if (values.length == 6) {
1596 final String prefix = "/sys/kernel/ipv4/tcp_";
1597 stringToFile(prefix + "rmem_min", values[0]);
1598 stringToFile(prefix + "rmem_def", values[1]);
1599 stringToFile(prefix + "rmem_max", values[2]);
1600 stringToFile(prefix + "wmem_min", values[3]);
1601 stringToFile(prefix + "wmem_def", values[4]);
1602 stringToFile(prefix + "wmem_max", values[5]);
1603 } else {
Wink Savillee70c6f52010-12-03 12:01:38 -08001604 loge("Invalid buffersize string: " + bufferSizes);
Irfan Sheriff7f132d92010-06-09 15:39:36 -07001605 }
1606 } catch (IOException e) {
Wink Savillee70c6f52010-12-03 12:01:38 -08001607 loge("Can't set tcp buffer sizes:" + e);
Irfan Sheriff7f132d92010-06-09 15:39:36 -07001608 }
1609 }
1610
1611 /**
1612 * Writes string to file. Basically same as "echo -n $string > $filename"
1613 *
1614 * @param filename
1615 * @param string
1616 * @throws IOException
1617 */
1618 private void stringToFile(String filename, String string) throws IOException {
1619 FileWriter out = new FileWriter(filename);
1620 try {
1621 out.write(string);
1622 } finally {
1623 out.close();
1624 }
1625 }
1626
1627
Robert Greenwalt2034b912009-08-12 16:08:25 -07001628 /**
1629 * Adjust the per-process dns entries (net.dns<x>.<pid>) based
1630 * on the highest priority active net which this process requested.
1631 * If there aren't any, clear it out
1632 */
1633 private void reassessPidDns(int myPid, boolean doBump)
1634 {
Wink Savillee70c6f52010-12-03 12:01:38 -08001635 if (DBG) log("reassessPidDns for pid " + myPid);
Robert Greenwalt2034b912009-08-12 16:08:25 -07001636 for(int i : mPriorityList) {
Robert Greenwalt34848c02011-03-25 13:09:25 -07001637 if (mNetConfigs[i].isDefault()) {
Robert Greenwalt2034b912009-08-12 16:08:25 -07001638 continue;
1639 }
1640 NetworkStateTracker nt = mNetTrackers[i];
Robert Greenwalt0659da32009-07-16 17:21:39 -07001641 if (nt.getNetworkInfo().isConnected() &&
1642 !nt.isTeardownRequested()) {
Robert Greenwalt1f1bcfe2010-08-30 10:56:47 -07001643 LinkProperties p = nt.getLinkProperties();
Robert Greenwalta7dfbd32010-06-15 15:43:39 -07001644 if (p == null) continue;
Robert Greenwalt2034b912009-08-12 16:08:25 -07001645 List pids = mNetRequestersPids[i];
1646 for (int j=0; j<pids.size(); j++) {
1647 Integer pid = (Integer)pids.get(j);
1648 if (pid.intValue() == myPid) {
Robert Greenwalta7dfbd32010-06-15 15:43:39 -07001649 Collection<InetAddress> dnses = p.getDnses();
1650 writePidDns(dnses, myPid);
Robert Greenwalt2034b912009-08-12 16:08:25 -07001651 if (doBump) {
1652 bumpDns();
1653 }
1654 return;
1655 }
1656 }
1657 }
1658 }
1659 // nothing found - delete
1660 for (int i = 1; ; i++) {
1661 String prop = "net.dns" + i + "." + myPid;
1662 if (SystemProperties.get(prop).length() == 0) {
1663 if (doBump) {
1664 bumpDns();
1665 }
1666 return;
1667 }
1668 SystemProperties.set(prop, "");
1669 }
1670 }
1671
Robert Greenwalt8ca88762010-12-17 15:20:36 -08001672 // return true if results in a change
1673 private boolean writePidDns(Collection <InetAddress> dnses, int pid) {
Robert Greenwalt2034b912009-08-12 16:08:25 -07001674 int j = 1;
Robert Greenwalt8ca88762010-12-17 15:20:36 -08001675 boolean changed = false;
Robert Greenwalta7dfbd32010-06-15 15:43:39 -07001676 for (InetAddress dns : dnses) {
Robert Greenwalt8ca88762010-12-17 15:20:36 -08001677 String dnsString = dns.getHostAddress();
1678 if (changed || !dnsString.equals(SystemProperties.get("net.dns" + j + "." + pid))) {
1679 changed = true;
1680 SystemProperties.set("net.dns" + j++ + "." + pid, dns.getHostAddress());
1681 }
Robert Greenwalt2034b912009-08-12 16:08:25 -07001682 }
Robert Greenwalt8ca88762010-12-17 15:20:36 -08001683 return changed;
Robert Greenwalt2034b912009-08-12 16:08:25 -07001684 }
1685
1686 private void bumpDns() {
1687 /*
1688 * Bump the property that tells the name resolver library to reread
1689 * the DNS server list from the properties.
1690 */
1691 String propVal = SystemProperties.get("net.dnschange");
1692 int n = 0;
1693 if (propVal.length() != 0) {
1694 try {
1695 n = Integer.parseInt(propVal);
1696 } catch (NumberFormatException e) {}
1697 }
1698 SystemProperties.set("net.dnschange", "" + (n+1));
Robert Greenwalt051642b2010-11-02 14:08:23 -07001699 /*
1700 * Tell the VMs to toss their DNS caches
1701 */
1702 Intent intent = new Intent(Intent.ACTION_CLEAR_DNS_CACHE);
1703 intent.addFlags(Intent.FLAG_RECEIVER_REPLACE_PENDING);
Stan Chesnuttf444f502011-01-05 17:14:03 -08001704 /*
1705 * Connectivity events can happen before boot has completed ...
1706 */
1707 intent.addFlags(Intent.FLAG_RECEIVER_REGISTERED_ONLY_BEFORE_BOOT);
Robert Greenwalt051642b2010-11-02 14:08:23 -07001708 mContext.sendBroadcast(intent);
Robert Greenwalt2034b912009-08-12 16:08:25 -07001709 }
1710
Robert Greenwalt3afbead2010-07-23 15:46:26 -07001711 private void handleDnsConfigurationChange(int netType) {
Robert Greenwalt2034b912009-08-12 16:08:25 -07001712 // add default net's dns entries
Robert Greenwalt3afbead2010-07-23 15:46:26 -07001713 NetworkStateTracker nt = mNetTrackers[netType];
1714 if (nt != null && nt.getNetworkInfo().isConnected() && !nt.isTeardownRequested()) {
Robert Greenwalt1f1bcfe2010-08-30 10:56:47 -07001715 LinkProperties p = nt.getLinkProperties();
Robert Greenwalt3afbead2010-07-23 15:46:26 -07001716 if (p == null) return;
1717 Collection<InetAddress> dnses = p.getDnses();
Robert Greenwalt8ca88762010-12-17 15:20:36 -08001718 boolean changed = false;
Robert Greenwalt34848c02011-03-25 13:09:25 -07001719 if (mNetConfigs[netType].isDefault()) {
Robert Greenwalt3afbead2010-07-23 15:46:26 -07001720 int j = 1;
Robert Greenwalt94daa182010-09-01 11:34:05 -07001721 if (dnses.size() == 0 && mDefaultDns != null) {
Robert Greenwalt8ca88762010-12-17 15:20:36 -08001722 String dnsString = mDefaultDns.getHostAddress();
1723 if (!dnsString.equals(SystemProperties.get("net.dns1"))) {
1724 if (DBG) {
1725 log("no dns provided - using " + dnsString);
1726 }
1727 changed = true;
1728 SystemProperties.set("net.dns1", dnsString);
Robert Greenwalt2034b912009-08-12 16:08:25 -07001729 }
Robert Greenwalt94daa182010-09-01 11:34:05 -07001730 j++;
1731 } else {
1732 for (InetAddress dns : dnses) {
Robert Greenwalt8ca88762010-12-17 15:20:36 -08001733 String dnsString = dns.getHostAddress();
1734 if (!changed && dnsString.equals(SystemProperties.get("net.dns" + j))) {
1735 j++;
1736 continue;
1737 }
Robert Greenwalt94daa182010-09-01 11:34:05 -07001738 if (DBG) {
Wink Savillee70c6f52010-12-03 12:01:38 -08001739 log("adding dns " + dns + " for " +
Robert Greenwalt94daa182010-09-01 11:34:05 -07001740 nt.getNetworkInfo().getTypeName());
1741 }
Robert Greenwalt8ca88762010-12-17 15:20:36 -08001742 changed = true;
1743 SystemProperties.set("net.dns" + j++, dnsString);
Robert Greenwalt94daa182010-09-01 11:34:05 -07001744 }
Robert Greenwalt3afbead2010-07-23 15:46:26 -07001745 }
1746 for (int k=j ; k<mNumDnsEntries; k++) {
Robert Greenwalt8ca88762010-12-17 15:20:36 -08001747 if (changed || !TextUtils.isEmpty(SystemProperties.get("net.dns" + k))) {
1748 if (DBG) log("erasing net.dns" + k);
1749 changed = true;
1750 SystemProperties.set("net.dns" + k, "");
1751 }
Robert Greenwalt3afbead2010-07-23 15:46:26 -07001752 }
1753 mNumDnsEntries = j;
1754 } else {
1755 // set per-pid dns for attached secondary nets
1756 List pids = mNetRequestersPids[netType];
1757 for (int y=0; y< pids.size(); y++) {
1758 Integer pid = (Integer)pids.get(y);
Robert Greenwalt8ca88762010-12-17 15:20:36 -08001759 changed = writePidDns(dnses, pid.intValue());
The Android Open Source Project28527d22009-03-03 19:31:44 -08001760 }
1761 }
Robert Greenwalt8ca88762010-12-17 15:20:36 -08001762 if (changed) bumpDns();
The Android Open Source Project28527d22009-03-03 19:31:44 -08001763 }
Robert Greenwalt2034b912009-08-12 16:08:25 -07001764 }
1765
Robert Greenwalt20f819c2011-05-03 19:02:44 -07001766 private int getRestoreDefaultNetworkDelay(int networkType) {
Robert Greenwalt2034b912009-08-12 16:08:25 -07001767 String restoreDefaultNetworkDelayStr = SystemProperties.get(
1768 NETWORK_RESTORE_DELAY_PROP_NAME);
1769 if(restoreDefaultNetworkDelayStr != null &&
1770 restoreDefaultNetworkDelayStr.length() != 0) {
1771 try {
1772 return Integer.valueOf(restoreDefaultNetworkDelayStr);
1773 } catch (NumberFormatException e) {
1774 }
The Android Open Source Project28527d22009-03-03 19:31:44 -08001775 }
Robert Greenwalt20f819c2011-05-03 19:02:44 -07001776 // if the system property isn't set, use the value for the apn type
1777 int ret = RESTORE_DEFAULT_NETWORK_DELAY;
1778
1779 if ((networkType <= ConnectivityManager.MAX_NETWORK_TYPE) &&
1780 (mNetConfigs[networkType] != null)) {
1781 ret = mNetConfigs[networkType].restoreTime;
1782 }
1783 return ret;
The Android Open Source Project28527d22009-03-03 19:31:44 -08001784 }
1785
1786 @Override
1787 protected void dump(FileDescriptor fd, PrintWriter pw, String[] args) {
Robert Greenwalt0659da32009-07-16 17:21:39 -07001788 if (mContext.checkCallingOrSelfPermission(
1789 android.Manifest.permission.DUMP)
The Android Open Source Project28527d22009-03-03 19:31:44 -08001790 != PackageManager.PERMISSION_GRANTED) {
Robert Greenwalt0659da32009-07-16 17:21:39 -07001791 pw.println("Permission Denial: can't dump ConnectivityService " +
1792 "from from pid=" + Binder.getCallingPid() + ", uid=" +
1793 Binder.getCallingUid());
The Android Open Source Project28527d22009-03-03 19:31:44 -08001794 return;
1795 }
The Android Open Source Project28527d22009-03-03 19:31:44 -08001796 pw.println();
1797 for (NetworkStateTracker nst : mNetTrackers) {
Robert Greenwalt3eeb6032009-12-21 18:24:07 -08001798 if (nst != null) {
1799 if (nst.getNetworkInfo().isConnected()) {
1800 pw.println("Active network: " + nst.getNetworkInfo().
1801 getTypeName());
1802 }
1803 pw.println(nst.getNetworkInfo());
1804 pw.println(nst);
1805 pw.println();
Robert Greenwalt2034b912009-08-12 16:08:25 -07001806 }
The Android Open Source Project28527d22009-03-03 19:31:44 -08001807 }
Robert Greenwalt3eeb6032009-12-21 18:24:07 -08001808
1809 pw.println("Network Requester Pids:");
1810 for (int net : mPriorityList) {
1811 String pidString = net + ": ";
1812 for (Object pid : mNetRequestersPids[net]) {
1813 pidString = pidString + pid.toString() + ", ";
1814 }
1815 pw.println(pidString);
1816 }
1817 pw.println();
1818
1819 pw.println("FeatureUsers:");
1820 for (Object requester : mFeatureUsers) {
1821 pw.println(requester.toString());
1822 }
1823 pw.println();
Robert Greenwalt8e87f122010-02-11 18:18:40 -08001824
Robert Greenwalt93dc1042010-06-15 12:19:37 -07001825 synchronized (this) {
1826 pw.println("NetworkTranstionWakeLock is currently " +
1827 (mNetTransitionWakeLock.isHeld() ? "" : "not ") + "held.");
1828 pw.println("It was last requested for "+mNetTransitionWakeLockCausedBy);
1829 }
1830 pw.println();
1831
Robert Greenwalt8e87f122010-02-11 18:18:40 -08001832 mTethering.dump(fd, pw, args);
Robert Greenwalt0e80be12010-09-20 14:35:25 -07001833
1834 if (mInetLog != null) {
1835 pw.println();
1836 pw.println("Inet condition reports:");
1837 for(int i = 0; i < mInetLog.size(); i++) {
1838 pw.println(mInetLog.get(i));
1839 }
1840 }
The Android Open Source Project28527d22009-03-03 19:31:44 -08001841 }
1842
Robert Greenwalt2034b912009-08-12 16:08:25 -07001843 // must be stateless - things change under us.
The Android Open Source Project28527d22009-03-03 19:31:44 -08001844 private class MyHandler extends Handler {
Wink Saville775aad62010-09-02 19:23:52 -07001845 public MyHandler(Looper looper) {
1846 super(looper);
1847 }
1848
The Android Open Source Project28527d22009-03-03 19:31:44 -08001849 @Override
1850 public void handleMessage(Message msg) {
1851 NetworkInfo info;
1852 switch (msg.what) {
1853 case NetworkStateTracker.EVENT_STATE_CHANGED:
1854 info = (NetworkInfo) msg.obj;
Robert Greenwalt12c44552009-12-07 11:33:18 -08001855 int type = info.getType();
1856 NetworkInfo.State state = info.getState();
Robert Greenwalt12c44552009-12-07 11:33:18 -08001857
Wink Savillee70c6f52010-12-03 12:01:38 -08001858 if (DBG) log("ConnectivityChange for " +
Robert Greenwalt0659da32009-07-16 17:21:39 -07001859 info.getTypeName() + ": " +
Robert Greenwalt12c44552009-12-07 11:33:18 -08001860 state + "/" + info.getDetailedState());
The Android Open Source Project28527d22009-03-03 19:31:44 -08001861
1862 // Connectivity state changed:
1863 // [31-13] Reserved for future use
Robert Greenwalt0659da32009-07-16 17:21:39 -07001864 // [12-9] Network subtype (for mobile network, as defined
1865 // by TelephonyManager)
1866 // [8-3] Detailed state ordinal (as defined by
1867 // NetworkInfo.DetailedState)
The Android Open Source Project28527d22009-03-03 19:31:44 -08001868 // [2-0] Network type (as defined by ConnectivityManager)
1869 int eventLogParam = (info.getType() & 0x7) |
1870 ((info.getDetailedState().ordinal() & 0x3f) << 3) |
1871 (info.getSubtype() << 9);
Doug Zongker2fc96232009-12-04 10:31:43 -08001872 EventLog.writeEvent(EventLogTags.CONNECTIVITY_STATE_CHANGED,
Robert Greenwalt0659da32009-07-16 17:21:39 -07001873 eventLogParam);
1874
1875 if (info.getDetailedState() ==
1876 NetworkInfo.DetailedState.FAILED) {
The Android Open Source Project28527d22009-03-03 19:31:44 -08001877 handleConnectionFailure(info);
Robert Greenwalt12c44552009-12-07 11:33:18 -08001878 } else if (state == NetworkInfo.State.DISCONNECTED) {
The Android Open Source Project28527d22009-03-03 19:31:44 -08001879 handleDisconnect(info);
Robert Greenwalt12c44552009-12-07 11:33:18 -08001880 } else if (state == NetworkInfo.State.SUSPENDED) {
The Android Open Source Project28527d22009-03-03 19:31:44 -08001881 // TODO: need to think this over.
Robert Greenwalt0659da32009-07-16 17:21:39 -07001882 // the logic here is, handle SUSPENDED the same as
1883 // DISCONNECTED. The only difference being we are
1884 // broadcasting an intent with NetworkInfo that's
1885 // suspended. This allows the applications an
1886 // opportunity to handle DISCONNECTED and SUSPENDED
1887 // differently, or not.
The Android Open Source Project28527d22009-03-03 19:31:44 -08001888 handleDisconnect(info);
Robert Greenwalt12c44552009-12-07 11:33:18 -08001889 } else if (state == NetworkInfo.State.CONNECTED) {
The Android Open Source Project28527d22009-03-03 19:31:44 -08001890 handleConnect(info);
1891 }
The Android Open Source Project28527d22009-03-03 19:31:44 -08001892 break;
The Android Open Source Project28527d22009-03-03 19:31:44 -08001893 case NetworkStateTracker.EVENT_CONFIGURATION_CHANGED:
Robert Greenwalt3afbead2010-07-23 15:46:26 -07001894 info = (NetworkInfo) msg.obj;
Kazuhiro Ondo07680062011-06-22 21:10:34 -05001895 // TODO: Temporary allowing network configuration
1896 // change not resetting sockets.
1897 // @see bug/4455071
1898 handleConnectivityChange(info.getType(), false);
The Android Open Source Project28527d22009-03-03 19:31:44 -08001899 break;
Robert Greenwaltccb36f92010-09-24 14:32:21 -07001900 case EVENT_CLEAR_NET_TRANSITION_WAKELOCK:
Robert Greenwalt93dc1042010-06-15 12:19:37 -07001901 String causedBy = null;
1902 synchronized (ConnectivityService.this) {
1903 if (msg.arg1 == mNetTransitionWakeLockSerialNumber &&
1904 mNetTransitionWakeLock.isHeld()) {
1905 mNetTransitionWakeLock.release();
1906 causedBy = mNetTransitionWakeLockCausedBy;
1907 }
1908 }
1909 if (causedBy != null) {
Wink Savillee70c6f52010-12-03 12:01:38 -08001910 log("NetTransition Wakelock for " + causedBy + " released by timeout");
Robert Greenwalt93dc1042010-06-15 12:19:37 -07001911 }
Robert Greenwaltcf1a56c2010-09-09 14:05:10 -07001912 break;
Robert Greenwalt6a2db8a2010-09-23 10:05:56 -07001913 case EVENT_RESTORE_DEFAULT_NETWORK:
Robert Greenwaltaffc3a12009-09-27 17:27:04 -07001914 FeatureUser u = (FeatureUser)msg.obj;
1915 u.expire();
Robert Greenwalt986c7412010-09-08 15:24:47 -07001916 break;
Robert Greenwalt6a2db8a2010-09-23 10:05:56 -07001917 case EVENT_INET_CONDITION_CHANGE:
1918 {
1919 int netType = msg.arg1;
1920 int condition = msg.arg2;
1921 handleInetConditionChange(netType, condition);
Robert Greenwalt93dc1042010-06-15 12:19:37 -07001922 break;
Robert Greenwalt6a2db8a2010-09-23 10:05:56 -07001923 }
1924 case EVENT_INET_CONDITION_HOLD_END:
1925 {
1926 int netType = msg.arg1;
1927 int sequence = msg.arg2;
1928 handleInetConditionHoldEnd(netType, sequence);
Robert Greenwalt986c7412010-09-08 15:24:47 -07001929 break;
Robert Greenwalt6a2db8a2010-09-23 10:05:56 -07001930 }
1931 case EVENT_SET_NETWORK_PREFERENCE:
1932 {
1933 int preference = msg.arg1;
1934 handleSetNetworkPreference(preference);
1935 break;
1936 }
1937 case EVENT_SET_BACKGROUND_DATA:
1938 {
1939 boolean enabled = (msg.arg1 == ENABLED);
1940 handleSetBackgroundData(enabled);
1941 break;
1942 }
1943 case EVENT_SET_MOBILE_DATA:
1944 {
1945 boolean enabled = (msg.arg1 == ENABLED);
1946 handleSetMobileData(enabled);
1947 break;
1948 }
Robert Greenwaltc3c5f862010-10-11 16:00:27 -07001949 case EVENT_APPLY_GLOBAL_HTTP_PROXY:
1950 {
1951 handleDeprecatedGlobalHttpProxy();
Robert Greenwalt34848c02011-03-25 13:09:25 -07001952 break;
1953 }
1954 case EVENT_SET_DEPENDENCY_MET:
1955 {
1956 boolean met = (msg.arg1 == ENABLED);
1957 handleSetDependencyMet(msg.arg2, met);
1958 break;
Robert Greenwaltc3c5f862010-10-11 16:00:27 -07001959 }
The Android Open Source Project28527d22009-03-03 19:31:44 -08001960 }
1961 }
1962 }
Robert Greenwalt0c4828c2010-01-26 11:40:34 -08001963
1964 // javadoc from interface
Robert Greenwalt4283ded2010-03-02 17:25:02 -08001965 public int tether(String iface) {
Robert Greenwalt0c4828c2010-01-26 11:40:34 -08001966 enforceTetherChangePermission();
Robert Greenwalt4283ded2010-03-02 17:25:02 -08001967
1968 if (isTetheringSupported()) {
1969 return mTethering.tether(iface);
1970 } else {
1971 return ConnectivityManager.TETHER_ERROR_UNSUPPORTED;
1972 }
Robert Greenwalt0c4828c2010-01-26 11:40:34 -08001973 }
1974
1975 // javadoc from interface
Robert Greenwalt4283ded2010-03-02 17:25:02 -08001976 public int untether(String iface) {
Robert Greenwalt0c4828c2010-01-26 11:40:34 -08001977 enforceTetherChangePermission();
Robert Greenwalt4283ded2010-03-02 17:25:02 -08001978
1979 if (isTetheringSupported()) {
1980 return mTethering.untether(iface);
1981 } else {
1982 return ConnectivityManager.TETHER_ERROR_UNSUPPORTED;
1983 }
1984 }
1985
1986 // javadoc from interface
1987 public int getLastTetherError(String iface) {
1988 enforceTetherAccessPermission();
1989
1990 if (isTetheringSupported()) {
1991 return mTethering.getLastTetherError(iface);
1992 } else {
1993 return ConnectivityManager.TETHER_ERROR_UNSUPPORTED;
1994 }
Robert Greenwalt8e87f122010-02-11 18:18:40 -08001995 }
1996
1997 // TODO - proper iface API for selection by property, inspection, etc
1998 public String[] getTetherableUsbRegexs() {
1999 enforceTetherAccessPermission();
2000 if (isTetheringSupported()) {
2001 return mTethering.getTetherableUsbRegexs();
2002 } else {
2003 return new String[0];
2004 }
2005 }
2006
2007 public String[] getTetherableWifiRegexs() {
2008 enforceTetherAccessPermission();
2009 if (isTetheringSupported()) {
2010 return mTethering.getTetherableWifiRegexs();
2011 } else {
2012 return new String[0];
2013 }
Robert Greenwalt0c4828c2010-01-26 11:40:34 -08002014 }
2015
Danica Chang96567052010-08-11 14:54:43 -07002016 public String[] getTetherableBluetoothRegexs() {
2017 enforceTetherAccessPermission();
2018 if (isTetheringSupported()) {
2019 return mTethering.getTetherableBluetoothRegexs();
2020 } else {
2021 return new String[0];
2022 }
2023 }
2024
Robert Greenwalt0c4828c2010-01-26 11:40:34 -08002025 // TODO - move iface listing, queries, etc to new module
2026 // javadoc from interface
2027 public String[] getTetherableIfaces() {
Robert Greenwalt8e87f122010-02-11 18:18:40 -08002028 enforceTetherAccessPermission();
Robert Greenwalt0c4828c2010-01-26 11:40:34 -08002029 return mTethering.getTetherableIfaces();
2030 }
2031
2032 public String[] getTetheredIfaces() {
Robert Greenwalt8e87f122010-02-11 18:18:40 -08002033 enforceTetherAccessPermission();
Robert Greenwalt0c4828c2010-01-26 11:40:34 -08002034 return mTethering.getTetheredIfaces();
2035 }
Robert Greenwalt8e87f122010-02-11 18:18:40 -08002036
Robert Greenwalt4283ded2010-03-02 17:25:02 -08002037 public String[] getTetheringErroredIfaces() {
2038 enforceTetherAccessPermission();
2039 return mTethering.getErroredIfaces();
2040 }
2041
Robert Greenwalt8e87f122010-02-11 18:18:40 -08002042 // if ro.tether.denied = true we default to no tethering
2043 // gservices could set the secure setting to 1 though to enable it on a build where it
2044 // had previously been turned off.
2045 public boolean isTetheringSupported() {
2046 enforceTetherAccessPermission();
2047 int defaultVal = (SystemProperties.get("ro.tether.denied").equals("true") ? 0 : 1);
Robert Greenwaltf1b66e12010-02-25 12:29:30 -08002048 boolean tetherEnabledInSettings = (Settings.Secure.getInt(mContext.getContentResolver(),
2049 Settings.Secure.TETHER_SUPPORTED, defaultVal) != 0);
2050 return tetherEnabledInSettings && mTetheringConfigValid;
Robert Greenwalt8e87f122010-02-11 18:18:40 -08002051 }
Robert Greenwalt93dc1042010-06-15 12:19:37 -07002052
2053 // An API NetworkStateTrackers can call when they lose their network.
2054 // This will automatically be cleared after X seconds or a network becomes CONNECTED,
2055 // whichever happens first. The timer is started by the first caller and not
2056 // restarted by subsequent callers.
2057 public void requestNetworkTransitionWakelock(String forWhom) {
2058 enforceConnectivityInternalPermission();
2059 synchronized (this) {
2060 if (mNetTransitionWakeLock.isHeld()) return;
2061 mNetTransitionWakeLockSerialNumber++;
2062 mNetTransitionWakeLock.acquire();
2063 mNetTransitionWakeLockCausedBy = forWhom;
2064 }
2065 mHandler.sendMessageDelayed(mHandler.obtainMessage(
Robert Greenwaltccb36f92010-09-24 14:32:21 -07002066 EVENT_CLEAR_NET_TRANSITION_WAKELOCK,
Robert Greenwalt93dc1042010-06-15 12:19:37 -07002067 mNetTransitionWakeLockSerialNumber, 0),
2068 mNetTransitionWakeLockTimeout);
2069 return;
2070 }
Robert Greenwalt24118e82010-09-09 13:15:32 -07002071
Robert Greenwalt986c7412010-09-08 15:24:47 -07002072 // 100 percent is full good, 0 is full bad.
2073 public void reportInetCondition(int networkType, int percentage) {
Wink Savillee70c6f52010-12-03 12:01:38 -08002074 if (DBG) log("reportNetworkCondition(" + networkType + ", " + percentage + ")");
Robert Greenwalt986c7412010-09-08 15:24:47 -07002075 mContext.enforceCallingOrSelfPermission(
2076 android.Manifest.permission.STATUS_BAR,
2077 "ConnectivityService");
2078
Robert Greenwalt0e80be12010-09-20 14:35:25 -07002079 if (DBG) {
2080 int pid = getCallingPid();
2081 int uid = getCallingUid();
2082 String s = pid + "(" + uid + ") reports inet is " +
2083 (percentage > 50 ? "connected" : "disconnected") + " (" + percentage + ") on " +
2084 "network Type " + networkType + " at " + GregorianCalendar.getInstance().getTime();
2085 mInetLog.add(s);
2086 while(mInetLog.size() > INET_CONDITION_LOG_MAX_SIZE) {
2087 mInetLog.remove(0);
2088 }
2089 }
Robert Greenwalt986c7412010-09-08 15:24:47 -07002090 mHandler.sendMessage(mHandler.obtainMessage(
Robert Greenwalt6a2db8a2010-09-23 10:05:56 -07002091 EVENT_INET_CONDITION_CHANGE, networkType, percentage));
2092 }
2093
2094 private void handleInetConditionChange(int netType, int condition) {
2095 if (DBG) {
Wink Savillee70c6f52010-12-03 12:01:38 -08002096 log("Inet connectivity change, net=" +
Robert Greenwalt6a2db8a2010-09-23 10:05:56 -07002097 netType + ", condition=" + condition +
2098 ",mActiveDefaultNetwork=" + mActiveDefaultNetwork);
2099 }
2100 if (mActiveDefaultNetwork == -1) {
Wink Savillee70c6f52010-12-03 12:01:38 -08002101 if (DBG) log("no active default network - aborting");
Robert Greenwalt6a2db8a2010-09-23 10:05:56 -07002102 return;
2103 }
2104 if (mActiveDefaultNetwork != netType) {
Wink Savillee70c6f52010-12-03 12:01:38 -08002105 if (DBG) log("given net not default - aborting");
Robert Greenwalt6a2db8a2010-09-23 10:05:56 -07002106 return;
2107 }
2108 mDefaultInetCondition = condition;
2109 int delay;
2110 if (mInetConditionChangeInFlight == false) {
Wink Savillee70c6f52010-12-03 12:01:38 -08002111 if (DBG) log("starting a change hold");
Robert Greenwalt6a2db8a2010-09-23 10:05:56 -07002112 // setup a new hold to debounce this
2113 if (mDefaultInetCondition > 50) {
2114 delay = Settings.Secure.getInt(mContext.getContentResolver(),
2115 Settings.Secure.INET_CONDITION_DEBOUNCE_UP_DELAY, 500);
2116 } else {
2117 delay = Settings.Secure.getInt(mContext.getContentResolver(),
2118 Settings.Secure.INET_CONDITION_DEBOUNCE_DOWN_DELAY, 3000);
2119 }
2120 mInetConditionChangeInFlight = true;
2121 mHandler.sendMessageDelayed(mHandler.obtainMessage(EVENT_INET_CONDITION_HOLD_END,
2122 mActiveDefaultNetwork, mDefaultConnectionSequence), delay);
2123 } else {
2124 // we've set the new condition, when this hold ends that will get
2125 // picked up
Wink Savillee70c6f52010-12-03 12:01:38 -08002126 if (DBG) log("currently in hold - not setting new end evt");
Robert Greenwalt6a2db8a2010-09-23 10:05:56 -07002127 }
2128 }
2129
2130 private void handleInetConditionHoldEnd(int netType, int sequence) {
2131 if (DBG) {
Wink Savillee70c6f52010-12-03 12:01:38 -08002132 log("Inet hold end, net=" + netType +
Robert Greenwalt6a2db8a2010-09-23 10:05:56 -07002133 ", condition =" + mDefaultInetCondition +
2134 ", published condition =" + mDefaultInetConditionPublished);
2135 }
2136 mInetConditionChangeInFlight = false;
2137
2138 if (mActiveDefaultNetwork == -1) {
Wink Savillee70c6f52010-12-03 12:01:38 -08002139 if (DBG) log("no active default network - aborting");
Robert Greenwalt6a2db8a2010-09-23 10:05:56 -07002140 return;
2141 }
2142 if (mDefaultConnectionSequence != sequence) {
Wink Savillee70c6f52010-12-03 12:01:38 -08002143 if (DBG) log("event hold for obsolete network - aborting");
Robert Greenwalt6a2db8a2010-09-23 10:05:56 -07002144 return;
2145 }
2146 if (mDefaultInetConditionPublished == mDefaultInetCondition) {
Wink Savillee70c6f52010-12-03 12:01:38 -08002147 if (DBG) log("no change in condition - aborting");
Robert Greenwalt6a2db8a2010-09-23 10:05:56 -07002148 return;
2149 }
2150 NetworkInfo networkInfo = mNetTrackers[mActiveDefaultNetwork].getNetworkInfo();
2151 if (networkInfo.isConnected() == false) {
Wink Savillee70c6f52010-12-03 12:01:38 -08002152 if (DBG) log("default network not connected - aborting");
Robert Greenwalt6a2db8a2010-09-23 10:05:56 -07002153 return;
2154 }
2155 mDefaultInetConditionPublished = mDefaultInetCondition;
2156 sendInetConditionBroadcast(networkInfo);
2157 return;
Robert Greenwalt986c7412010-09-08 15:24:47 -07002158 }
Robert Greenwaltc3c5f862010-10-11 16:00:27 -07002159
2160 public synchronized ProxyProperties getProxy() {
2161 if (mGlobalProxy != null) return mGlobalProxy;
2162 if (mDefaultProxy != null) return mDefaultProxy;
2163 return null;
2164 }
2165
2166 public void setGlobalProxy(ProxyProperties proxyProperties) {
2167 enforceChangePermission();
2168 synchronized (mGlobalProxyLock) {
2169 if (proxyProperties == mGlobalProxy) return;
2170 if (proxyProperties != null && proxyProperties.equals(mGlobalProxy)) return;
2171 if (mGlobalProxy != null && mGlobalProxy.equals(proxyProperties)) return;
2172
2173 String host = "";
2174 int port = 0;
2175 String exclList = "";
2176 if (proxyProperties != null && !TextUtils.isEmpty(proxyProperties.getHost())) {
2177 mGlobalProxy = new ProxyProperties(proxyProperties);
2178 host = mGlobalProxy.getHost();
2179 port = mGlobalProxy.getPort();
2180 exclList = mGlobalProxy.getExclusionList();
2181 } else {
2182 mGlobalProxy = null;
2183 }
2184 ContentResolver res = mContext.getContentResolver();
2185 Settings.Secure.putString(res, Settings.Secure.GLOBAL_HTTP_PROXY_HOST, host);
2186 Settings.Secure.putInt(res, Settings.Secure.GLOBAL_HTTP_PROXY_PORT, port);
Robert Greenwalt6f7c6092010-12-02 11:31:00 -08002187 Settings.Secure.putString(res, Settings.Secure.GLOBAL_HTTP_PROXY_EXCLUSION_LIST,
Robert Greenwaltc3c5f862010-10-11 16:00:27 -07002188 exclList);
2189 }
2190
2191 if (mGlobalProxy == null) {
2192 proxyProperties = mDefaultProxy;
2193 }
2194 sendProxyBroadcast(proxyProperties);
2195 }
2196
Robert Greenwalt6f7c6092010-12-02 11:31:00 -08002197 private void loadGlobalProxy() {
2198 ContentResolver res = mContext.getContentResolver();
2199 String host = Settings.Secure.getString(res, Settings.Secure.GLOBAL_HTTP_PROXY_HOST);
2200 int port = Settings.Secure.getInt(res, Settings.Secure.GLOBAL_HTTP_PROXY_PORT, 0);
2201 String exclList = Settings.Secure.getString(res,
2202 Settings.Secure.GLOBAL_HTTP_PROXY_EXCLUSION_LIST);
2203 if (!TextUtils.isEmpty(host)) {
2204 ProxyProperties proxyProperties = new ProxyProperties(host, port, exclList);
2205 synchronized (mGlobalProxyLock) {
2206 mGlobalProxy = proxyProperties;
2207 }
2208 }
2209 }
2210
Robert Greenwaltc3c5f862010-10-11 16:00:27 -07002211 public ProxyProperties getGlobalProxy() {
2212 synchronized (mGlobalProxyLock) {
2213 return mGlobalProxy;
2214 }
2215 }
2216
2217 private void handleApplyDefaultProxy(int type) {
2218 // check if new default - push it out to all VM if so
2219 ProxyProperties proxy = mNetTrackers[type].getLinkProperties().getHttpProxy();
2220 synchronized (this) {
2221 if (mDefaultProxy != null && mDefaultProxy.equals(proxy)) return;
2222 if (mDefaultProxy == proxy) return;
2223 if (!TextUtils.isEmpty(proxy.getHost())) {
2224 mDefaultProxy = proxy;
2225 } else {
2226 mDefaultProxy = null;
2227 }
2228 }
Wink Savillee70c6f52010-12-03 12:01:38 -08002229 if (DBG) log("changing default proxy to " + proxy);
Robert Greenwaltc3c5f862010-10-11 16:00:27 -07002230 if ((proxy == null && mGlobalProxy == null) || proxy.equals(mGlobalProxy)) return;
2231 if (mGlobalProxy != null) return;
2232 sendProxyBroadcast(proxy);
2233 }
2234
2235 private void handleDeprecatedGlobalHttpProxy() {
2236 String proxy = Settings.Secure.getString(mContext.getContentResolver(),
2237 Settings.Secure.HTTP_PROXY);
2238 if (!TextUtils.isEmpty(proxy)) {
2239 String data[] = proxy.split(":");
2240 String proxyHost = data[0];
2241 int proxyPort = 8080;
2242 if (data.length > 1) {
2243 try {
2244 proxyPort = Integer.parseInt(data[1]);
2245 } catch (NumberFormatException e) {
2246 return;
2247 }
2248 }
2249 ProxyProperties p = new ProxyProperties(data[0], proxyPort, "");
2250 setGlobalProxy(p);
2251 }
2252 }
2253
2254 private void sendProxyBroadcast(ProxyProperties proxy) {
Robert Greenwalt611291c2010-12-23 15:51:10 -08002255 if (proxy == null) proxy = new ProxyProperties("", 0, "");
Wink Savillee70c6f52010-12-03 12:01:38 -08002256 log("sending Proxy Broadcast for " + proxy);
Robert Greenwaltc3c5f862010-10-11 16:00:27 -07002257 Intent intent = new Intent(Proxy.PROXY_CHANGE_ACTION);
Stan Chesnutt1f2a2ac2011-01-06 11:00:19 -08002258 intent.addFlags(Intent.FLAG_RECEIVER_REPLACE_PENDING |
2259 Intent.FLAG_RECEIVER_REGISTERED_ONLY_BEFORE_BOOT);
Robert Greenwaltc3c5f862010-10-11 16:00:27 -07002260 intent.putExtra(Proxy.EXTRA_PROXY_INFO, proxy);
Robert Greenwaltd93dc8f2010-12-06 11:29:17 -08002261 mContext.sendStickyBroadcast(intent);
Robert Greenwaltc3c5f862010-10-11 16:00:27 -07002262 }
2263
2264 private static class SettingsObserver extends ContentObserver {
2265 private int mWhat;
2266 private Handler mHandler;
2267 SettingsObserver(Handler handler, int what) {
2268 super(handler);
2269 mHandler = handler;
2270 mWhat = what;
2271 }
2272
2273 void observe(Context context) {
2274 ContentResolver resolver = context.getContentResolver();
2275 resolver.registerContentObserver(Settings.Secure.getUriFor(
2276 Settings.Secure.HTTP_PROXY), false, this);
2277 }
2278
2279 @Override
2280 public void onChange(boolean selfChange) {
2281 mHandler.obtainMessage(mWhat).sendToTarget();
2282 }
2283 }
Wink Savillee70c6f52010-12-03 12:01:38 -08002284
2285 private void log(String s) {
2286 Slog.d(TAG, s);
2287 }
2288
2289 private void loge(String s) {
2290 Slog.e(TAG, s);
2291 }
Wink Savilleb7c92c72011-03-12 14:52:01 -08002292 int convertFeatureToNetworkType(String feature){
2293 int networkType = -1;
2294 if (TextUtils.equals(feature, Phone.FEATURE_ENABLE_MMS)) {
2295 networkType = ConnectivityManager.TYPE_MOBILE_MMS;
2296 } else if (TextUtils.equals(feature, Phone.FEATURE_ENABLE_SUPL)) {
2297 networkType = ConnectivityManager.TYPE_MOBILE_SUPL;
2298 } else if (TextUtils.equals(feature, Phone.FEATURE_ENABLE_DUN) ||
2299 TextUtils.equals(feature, Phone.FEATURE_ENABLE_DUN_ALWAYS)) {
2300 networkType = ConnectivityManager.TYPE_MOBILE_DUN;
2301 } else if (TextUtils.equals(feature, Phone.FEATURE_ENABLE_HIPRI)) {
2302 networkType = ConnectivityManager.TYPE_MOBILE_HIPRI;
2303 } else if (TextUtils.equals(feature, Phone.FEATURE_ENABLE_FOTA)) {
2304 networkType = ConnectivityManager.TYPE_MOBILE_FOTA;
2305 } else if (TextUtils.equals(feature, Phone.FEATURE_ENABLE_IMS)) {
2306 networkType = ConnectivityManager.TYPE_MOBILE_IMS;
2307 } else if (TextUtils.equals(feature, Phone.FEATURE_ENABLE_CBS)) {
2308 networkType = ConnectivityManager.TYPE_MOBILE_CBS;
2309 }
2310 return networkType;
2311 }
The Android Open Source Project28527d22009-03-03 19:31:44 -08002312}