blob: e11190f99c9027ae4123cfe92975f2b011241960 [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 }
The Android Open Source Project28527d22009-03-03 19:31:44 -08001443 }
1444
Irfan Sheriff7f132d92010-06-09 15:39:36 -07001445 private void addPrivateDnsRoutes(NetworkStateTracker nt) {
Irfan Sheriff7f132d92010-06-09 15:39:36 -07001446 boolean privateDnsRouteSet = nt.isPrivateDnsRouteSet();
Robert Greenwalt1f1bcfe2010-08-30 10:56:47 -07001447 LinkProperties p = nt.getLinkProperties();
Robert Greenwalta7dfbd32010-06-15 15:43:39 -07001448 if (p == null) return;
1449 String interfaceName = p.getInterfaceName();
Irfan Sheriff7f132d92010-06-09 15:39:36 -07001450
1451 if (DBG) {
Wink Savillee70c6f52010-12-03 12:01:38 -08001452 log("addPrivateDnsRoutes for " + nt +
Irfan Sheriff7f132d92010-06-09 15:39:36 -07001453 "(" + interfaceName + ") - mPrivateDnsRouteSet = " + privateDnsRouteSet);
1454 }
Irfan Sheriff7f132d92010-06-09 15:39:36 -07001455 if (interfaceName != null && !privateDnsRouteSet) {
Robert Greenwalta7dfbd32010-06-15 15:43:39 -07001456 Collection<InetAddress> dnsList = p.getDnses();
1457 for (InetAddress dns : dnsList) {
Robert Greenwaltbd492212011-05-06 17:10:53 -07001458 addHostRoute(nt, dns, 0);
Irfan Sheriff7f132d92010-06-09 15:39:36 -07001459 }
1460 nt.privateDnsRouteSet(true);
1461 }
1462 }
1463
1464 private void removePrivateDnsRoutes(NetworkStateTracker nt) {
Robert Greenwalt1f1bcfe2010-08-30 10:56:47 -07001465 LinkProperties p = nt.getLinkProperties();
Robert Greenwalta7dfbd32010-06-15 15:43:39 -07001466 if (p == null) return;
1467 String interfaceName = p.getInterfaceName();
Irfan Sheriff7f132d92010-06-09 15:39:36 -07001468 boolean privateDnsRouteSet = nt.isPrivateDnsRouteSet();
1469 if (interfaceName != null && privateDnsRouteSet) {
1470 if (DBG) {
Wink Savillee70c6f52010-12-03 12:01:38 -08001471 log("removePrivateDnsRoutes for " + nt.getNetworkInfo().getTypeName() +
Irfan Sheriff7f132d92010-06-09 15:39:36 -07001472 " (" + interfaceName + ")");
1473 }
Robert Greenwalt355205c2011-05-10 15:05:02 -07001474
1475 Collection<InetAddress> dnsList = p.getDnses();
1476 for (InetAddress dns : dnsList) {
1477 if (DBG) log(" removing " + dns);
1478 RouteInfo route = RouteInfo.makeHostRoute(dns);
1479 try {
1480 mNetd.removeRoute(interfaceName, route);
1481 } catch (Exception ex) {
1482 loge("error (" + ex + ") removing dns route " + route);
1483 }
1484 }
Irfan Sheriff7f132d92010-06-09 15:39:36 -07001485 nt.privateDnsRouteSet(false);
1486 }
1487 }
1488
Irfan Sheriff7f132d92010-06-09 15:39:36 -07001489
1490 private void addDefaultRoute(NetworkStateTracker nt) {
Robert Greenwalt1f1bcfe2010-08-30 10:56:47 -07001491 LinkProperties p = nt.getLinkProperties();
Robert Greenwalta7dfbd32010-06-15 15:43:39 -07001492 if (p == null) return;
1493 String interfaceName = p.getInterfaceName();
Robert Greenwalt5c733972011-02-09 13:56:06 -08001494 if (TextUtils.isEmpty(interfaceName)) return;
Irfan Sheriff7f132d92010-06-09 15:39:36 -07001495
Robert Greenwalt355205c2011-05-10 15:05:02 -07001496 for (RouteInfo route : p.getRoutes()) {
Robert Greenwalt5a901292011-04-28 14:28:50 -07001497 //TODO - handle non-default routes
1498 if (route.isDefaultRoute()) {
Robert Greenwalt355205c2011-05-10 15:05:02 -07001499 if (DBG) log("adding default route " + route);
Robert Greenwalt5a901292011-04-28 14:28:50 -07001500 InetAddress gateway = route.getGateway();
Robert Greenwalt355205c2011-05-10 15:05:02 -07001501 if (addHostRoute(nt, gateway, 0)) {
1502 try {
1503 mNetd.addRoute(interfaceName, route);
1504 } catch (Exception e) {
1505 loge("error adding default route " + route);
1506 continue;
1507 }
Robert Greenwalt5a901292011-04-28 14:28:50 -07001508 if (DBG) {
1509 NetworkInfo networkInfo = nt.getNetworkInfo();
1510 log("addDefaultRoute for " + networkInfo.getTypeName() +
1511 " (" + interfaceName + "), GatewayAddr=" +
1512 gateway.getHostAddress());
1513 }
Robert Greenwalt355205c2011-05-10 15:05:02 -07001514 } else {
1515 loge("error adding host route for default route " + route);
Robert Greenwalt03d53da2011-03-22 18:47:42 -07001516 }
Irfan Sheriff7f132d92010-06-09 15:39:36 -07001517 }
Irfan Sheriff7f132d92010-06-09 15:39:36 -07001518 }
1519 }
1520
1521
1522 public void removeDefaultRoute(NetworkStateTracker nt) {
Robert Greenwalt1f1bcfe2010-08-30 10:56:47 -07001523 LinkProperties p = nt.getLinkProperties();
Robert Greenwalta7dfbd32010-06-15 15:43:39 -07001524 if (p == null) return;
1525 String interfaceName = p.getInterfaceName();
Irfan Sheriff7f132d92010-06-09 15:39:36 -07001526
Robert Greenwalt355205c2011-05-10 15:05:02 -07001527 if (interfaceName == null) return;
1528
1529 for (RouteInfo route : p.getRoutes()) {
1530 //TODO - handle non-default routes
1531 if (route.isDefaultRoute()) {
1532 try {
1533 mNetd.removeRoute(interfaceName, route);
1534 } catch (Exception ex) {
1535 loge("error (" + ex + ") removing default route " + route);
1536 continue;
1537 }
Robert Greenwalt03d53da2011-03-22 18:47:42 -07001538 if (DBG) {
1539 NetworkInfo networkInfo = nt.getNetworkInfo();
1540 log("removeDefaultRoute for " + networkInfo.getTypeName() + " (" +
1541 interfaceName + ")");
1542 }
Irfan Sheriff7f132d92010-06-09 15:39:36 -07001543 }
Irfan Sheriff7f132d92010-06-09 15:39:36 -07001544 }
1545 }
1546
1547 /**
1548 * Reads the network specific TCP buffer sizes from SystemProperties
1549 * net.tcp.buffersize.[default|wifi|umts|edge|gprs] and set them for system
1550 * wide use
1551 */
1552 public void updateNetworkSettings(NetworkStateTracker nt) {
1553 String key = nt.getTcpBufferSizesPropName();
1554 String bufferSizes = SystemProperties.get(key);
1555
1556 if (bufferSizes.length() == 0) {
Wink Savillee70c6f52010-12-03 12:01:38 -08001557 loge(key + " not found in system properties. Using defaults");
Irfan Sheriff7f132d92010-06-09 15:39:36 -07001558
1559 // Setting to default values so we won't be stuck to previous values
1560 key = "net.tcp.buffersize.default";
1561 bufferSizes = SystemProperties.get(key);
1562 }
1563
1564 // Set values in kernel
1565 if (bufferSizes.length() != 0) {
1566 if (DBG) {
Wink Savillee70c6f52010-12-03 12:01:38 -08001567 log("Setting TCP values: [" + bufferSizes
Irfan Sheriff7f132d92010-06-09 15:39:36 -07001568 + "] which comes from [" + key + "]");
1569 }
1570 setBufferSize(bufferSizes);
1571 }
1572 }
1573
1574 /**
1575 * Writes TCP buffer sizes to /sys/kernel/ipv4/tcp_[r/w]mem_[min/def/max]
1576 * which maps to /proc/sys/net/ipv4/tcp_rmem and tcpwmem
1577 *
1578 * @param bufferSizes in the format of "readMin, readInitial, readMax,
1579 * writeMin, writeInitial, writeMax"
1580 */
1581 private void setBufferSize(String bufferSizes) {
1582 try {
1583 String[] values = bufferSizes.split(",");
1584
1585 if (values.length == 6) {
1586 final String prefix = "/sys/kernel/ipv4/tcp_";
1587 stringToFile(prefix + "rmem_min", values[0]);
1588 stringToFile(prefix + "rmem_def", values[1]);
1589 stringToFile(prefix + "rmem_max", values[2]);
1590 stringToFile(prefix + "wmem_min", values[3]);
1591 stringToFile(prefix + "wmem_def", values[4]);
1592 stringToFile(prefix + "wmem_max", values[5]);
1593 } else {
Wink Savillee70c6f52010-12-03 12:01:38 -08001594 loge("Invalid buffersize string: " + bufferSizes);
Irfan Sheriff7f132d92010-06-09 15:39:36 -07001595 }
1596 } catch (IOException e) {
Wink Savillee70c6f52010-12-03 12:01:38 -08001597 loge("Can't set tcp buffer sizes:" + e);
Irfan Sheriff7f132d92010-06-09 15:39:36 -07001598 }
1599 }
1600
1601 /**
1602 * Writes string to file. Basically same as "echo -n $string > $filename"
1603 *
1604 * @param filename
1605 * @param string
1606 * @throws IOException
1607 */
1608 private void stringToFile(String filename, String string) throws IOException {
1609 FileWriter out = new FileWriter(filename);
1610 try {
1611 out.write(string);
1612 } finally {
1613 out.close();
1614 }
1615 }
1616
1617
Robert Greenwalt2034b912009-08-12 16:08:25 -07001618 /**
1619 * Adjust the per-process dns entries (net.dns<x>.<pid>) based
1620 * on the highest priority active net which this process requested.
1621 * If there aren't any, clear it out
1622 */
1623 private void reassessPidDns(int myPid, boolean doBump)
1624 {
Wink Savillee70c6f52010-12-03 12:01:38 -08001625 if (DBG) log("reassessPidDns for pid " + myPid);
Robert Greenwalt2034b912009-08-12 16:08:25 -07001626 for(int i : mPriorityList) {
Robert Greenwalt34848c02011-03-25 13:09:25 -07001627 if (mNetConfigs[i].isDefault()) {
Robert Greenwalt2034b912009-08-12 16:08:25 -07001628 continue;
1629 }
1630 NetworkStateTracker nt = mNetTrackers[i];
Robert Greenwalt0659da32009-07-16 17:21:39 -07001631 if (nt.getNetworkInfo().isConnected() &&
1632 !nt.isTeardownRequested()) {
Robert Greenwalt1f1bcfe2010-08-30 10:56:47 -07001633 LinkProperties p = nt.getLinkProperties();
Robert Greenwalta7dfbd32010-06-15 15:43:39 -07001634 if (p == null) continue;
Robert Greenwalt2034b912009-08-12 16:08:25 -07001635 List pids = mNetRequestersPids[i];
1636 for (int j=0; j<pids.size(); j++) {
1637 Integer pid = (Integer)pids.get(j);
1638 if (pid.intValue() == myPid) {
Robert Greenwalta7dfbd32010-06-15 15:43:39 -07001639 Collection<InetAddress> dnses = p.getDnses();
1640 writePidDns(dnses, myPid);
Robert Greenwalt2034b912009-08-12 16:08:25 -07001641 if (doBump) {
1642 bumpDns();
1643 }
1644 return;
1645 }
1646 }
1647 }
1648 }
1649 // nothing found - delete
1650 for (int i = 1; ; i++) {
1651 String prop = "net.dns" + i + "." + myPid;
1652 if (SystemProperties.get(prop).length() == 0) {
1653 if (doBump) {
1654 bumpDns();
1655 }
1656 return;
1657 }
1658 SystemProperties.set(prop, "");
1659 }
1660 }
1661
Robert Greenwalt8ca88762010-12-17 15:20:36 -08001662 // return true if results in a change
1663 private boolean writePidDns(Collection <InetAddress> dnses, int pid) {
Robert Greenwalt2034b912009-08-12 16:08:25 -07001664 int j = 1;
Robert Greenwalt8ca88762010-12-17 15:20:36 -08001665 boolean changed = false;
Robert Greenwalta7dfbd32010-06-15 15:43:39 -07001666 for (InetAddress dns : dnses) {
Robert Greenwalt8ca88762010-12-17 15:20:36 -08001667 String dnsString = dns.getHostAddress();
1668 if (changed || !dnsString.equals(SystemProperties.get("net.dns" + j + "." + pid))) {
1669 changed = true;
1670 SystemProperties.set("net.dns" + j++ + "." + pid, dns.getHostAddress());
1671 }
Robert Greenwalt2034b912009-08-12 16:08:25 -07001672 }
Robert Greenwalt8ca88762010-12-17 15:20:36 -08001673 return changed;
Robert Greenwalt2034b912009-08-12 16:08:25 -07001674 }
1675
1676 private void bumpDns() {
1677 /*
1678 * Bump the property that tells the name resolver library to reread
1679 * the DNS server list from the properties.
1680 */
1681 String propVal = SystemProperties.get("net.dnschange");
1682 int n = 0;
1683 if (propVal.length() != 0) {
1684 try {
1685 n = Integer.parseInt(propVal);
1686 } catch (NumberFormatException e) {}
1687 }
1688 SystemProperties.set("net.dnschange", "" + (n+1));
Robert Greenwalt051642b2010-11-02 14:08:23 -07001689 /*
1690 * Tell the VMs to toss their DNS caches
1691 */
1692 Intent intent = new Intent(Intent.ACTION_CLEAR_DNS_CACHE);
1693 intent.addFlags(Intent.FLAG_RECEIVER_REPLACE_PENDING);
Stan Chesnuttf444f502011-01-05 17:14:03 -08001694 /*
1695 * Connectivity events can happen before boot has completed ...
1696 */
1697 intent.addFlags(Intent.FLAG_RECEIVER_REGISTERED_ONLY_BEFORE_BOOT);
Robert Greenwalt051642b2010-11-02 14:08:23 -07001698 mContext.sendBroadcast(intent);
Robert Greenwalt2034b912009-08-12 16:08:25 -07001699 }
1700
Robert Greenwalt3afbead2010-07-23 15:46:26 -07001701 private void handleDnsConfigurationChange(int netType) {
Robert Greenwalt2034b912009-08-12 16:08:25 -07001702 // add default net's dns entries
Robert Greenwalt3afbead2010-07-23 15:46:26 -07001703 NetworkStateTracker nt = mNetTrackers[netType];
1704 if (nt != null && nt.getNetworkInfo().isConnected() && !nt.isTeardownRequested()) {
Robert Greenwalt1f1bcfe2010-08-30 10:56:47 -07001705 LinkProperties p = nt.getLinkProperties();
Robert Greenwalt3afbead2010-07-23 15:46:26 -07001706 if (p == null) return;
1707 Collection<InetAddress> dnses = p.getDnses();
Robert Greenwalt8ca88762010-12-17 15:20:36 -08001708 boolean changed = false;
Robert Greenwalt34848c02011-03-25 13:09:25 -07001709 if (mNetConfigs[netType].isDefault()) {
Robert Greenwalt3afbead2010-07-23 15:46:26 -07001710 int j = 1;
Robert Greenwalt94daa182010-09-01 11:34:05 -07001711 if (dnses.size() == 0 && mDefaultDns != null) {
Robert Greenwalt8ca88762010-12-17 15:20:36 -08001712 String dnsString = mDefaultDns.getHostAddress();
1713 if (!dnsString.equals(SystemProperties.get("net.dns1"))) {
1714 if (DBG) {
1715 log("no dns provided - using " + dnsString);
1716 }
1717 changed = true;
1718 SystemProperties.set("net.dns1", dnsString);
Robert Greenwalt2034b912009-08-12 16:08:25 -07001719 }
Robert Greenwalt94daa182010-09-01 11:34:05 -07001720 j++;
1721 } else {
1722 for (InetAddress dns : dnses) {
Robert Greenwalt8ca88762010-12-17 15:20:36 -08001723 String dnsString = dns.getHostAddress();
1724 if (!changed && dnsString.equals(SystemProperties.get("net.dns" + j))) {
1725 j++;
1726 continue;
1727 }
Robert Greenwalt94daa182010-09-01 11:34:05 -07001728 if (DBG) {
Wink Savillee70c6f52010-12-03 12:01:38 -08001729 log("adding dns " + dns + " for " +
Robert Greenwalt94daa182010-09-01 11:34:05 -07001730 nt.getNetworkInfo().getTypeName());
1731 }
Robert Greenwalt8ca88762010-12-17 15:20:36 -08001732 changed = true;
1733 SystemProperties.set("net.dns" + j++, dnsString);
Robert Greenwalt94daa182010-09-01 11:34:05 -07001734 }
Robert Greenwalt3afbead2010-07-23 15:46:26 -07001735 }
1736 for (int k=j ; k<mNumDnsEntries; k++) {
Robert Greenwalt8ca88762010-12-17 15:20:36 -08001737 if (changed || !TextUtils.isEmpty(SystemProperties.get("net.dns" + k))) {
1738 if (DBG) log("erasing net.dns" + k);
1739 changed = true;
1740 SystemProperties.set("net.dns" + k, "");
1741 }
Robert Greenwalt3afbead2010-07-23 15:46:26 -07001742 }
1743 mNumDnsEntries = j;
1744 } else {
1745 // set per-pid dns for attached secondary nets
1746 List pids = mNetRequestersPids[netType];
1747 for (int y=0; y< pids.size(); y++) {
1748 Integer pid = (Integer)pids.get(y);
Robert Greenwalt8ca88762010-12-17 15:20:36 -08001749 changed = writePidDns(dnses, pid.intValue());
The Android Open Source Project28527d22009-03-03 19:31:44 -08001750 }
1751 }
Robert Greenwalt8ca88762010-12-17 15:20:36 -08001752 if (changed) bumpDns();
The Android Open Source Project28527d22009-03-03 19:31:44 -08001753 }
Robert Greenwalt2034b912009-08-12 16:08:25 -07001754 }
1755
Robert Greenwalt20f819c2011-05-03 19:02:44 -07001756 private int getRestoreDefaultNetworkDelay(int networkType) {
Robert Greenwalt2034b912009-08-12 16:08:25 -07001757 String restoreDefaultNetworkDelayStr = SystemProperties.get(
1758 NETWORK_RESTORE_DELAY_PROP_NAME);
1759 if(restoreDefaultNetworkDelayStr != null &&
1760 restoreDefaultNetworkDelayStr.length() != 0) {
1761 try {
1762 return Integer.valueOf(restoreDefaultNetworkDelayStr);
1763 } catch (NumberFormatException e) {
1764 }
The Android Open Source Project28527d22009-03-03 19:31:44 -08001765 }
Robert Greenwalt20f819c2011-05-03 19:02:44 -07001766 // if the system property isn't set, use the value for the apn type
1767 int ret = RESTORE_DEFAULT_NETWORK_DELAY;
1768
1769 if ((networkType <= ConnectivityManager.MAX_NETWORK_TYPE) &&
1770 (mNetConfigs[networkType] != null)) {
1771 ret = mNetConfigs[networkType].restoreTime;
1772 }
1773 return ret;
The Android Open Source Project28527d22009-03-03 19:31:44 -08001774 }
1775
1776 @Override
1777 protected void dump(FileDescriptor fd, PrintWriter pw, String[] args) {
Robert Greenwalt0659da32009-07-16 17:21:39 -07001778 if (mContext.checkCallingOrSelfPermission(
1779 android.Manifest.permission.DUMP)
The Android Open Source Project28527d22009-03-03 19:31:44 -08001780 != PackageManager.PERMISSION_GRANTED) {
Robert Greenwalt0659da32009-07-16 17:21:39 -07001781 pw.println("Permission Denial: can't dump ConnectivityService " +
1782 "from from pid=" + Binder.getCallingPid() + ", uid=" +
1783 Binder.getCallingUid());
The Android Open Source Project28527d22009-03-03 19:31:44 -08001784 return;
1785 }
The Android Open Source Project28527d22009-03-03 19:31:44 -08001786 pw.println();
1787 for (NetworkStateTracker nst : mNetTrackers) {
Robert Greenwalt3eeb6032009-12-21 18:24:07 -08001788 if (nst != null) {
1789 if (nst.getNetworkInfo().isConnected()) {
1790 pw.println("Active network: " + nst.getNetworkInfo().
1791 getTypeName());
1792 }
1793 pw.println(nst.getNetworkInfo());
1794 pw.println(nst);
1795 pw.println();
Robert Greenwalt2034b912009-08-12 16:08:25 -07001796 }
The Android Open Source Project28527d22009-03-03 19:31:44 -08001797 }
Robert Greenwalt3eeb6032009-12-21 18:24:07 -08001798
1799 pw.println("Network Requester Pids:");
1800 for (int net : mPriorityList) {
1801 String pidString = net + ": ";
1802 for (Object pid : mNetRequestersPids[net]) {
1803 pidString = pidString + pid.toString() + ", ";
1804 }
1805 pw.println(pidString);
1806 }
1807 pw.println();
1808
1809 pw.println("FeatureUsers:");
1810 for (Object requester : mFeatureUsers) {
1811 pw.println(requester.toString());
1812 }
1813 pw.println();
Robert Greenwalt8e87f122010-02-11 18:18:40 -08001814
Robert Greenwalt93dc1042010-06-15 12:19:37 -07001815 synchronized (this) {
1816 pw.println("NetworkTranstionWakeLock is currently " +
1817 (mNetTransitionWakeLock.isHeld() ? "" : "not ") + "held.");
1818 pw.println("It was last requested for "+mNetTransitionWakeLockCausedBy);
1819 }
1820 pw.println();
1821
Robert Greenwalt8e87f122010-02-11 18:18:40 -08001822 mTethering.dump(fd, pw, args);
Robert Greenwalt0e80be12010-09-20 14:35:25 -07001823
1824 if (mInetLog != null) {
1825 pw.println();
1826 pw.println("Inet condition reports:");
1827 for(int i = 0; i < mInetLog.size(); i++) {
1828 pw.println(mInetLog.get(i));
1829 }
1830 }
The Android Open Source Project28527d22009-03-03 19:31:44 -08001831 }
1832
Robert Greenwalt2034b912009-08-12 16:08:25 -07001833 // must be stateless - things change under us.
The Android Open Source Project28527d22009-03-03 19:31:44 -08001834 private class MyHandler extends Handler {
Wink Saville775aad62010-09-02 19:23:52 -07001835 public MyHandler(Looper looper) {
1836 super(looper);
1837 }
1838
The Android Open Source Project28527d22009-03-03 19:31:44 -08001839 @Override
1840 public void handleMessage(Message msg) {
1841 NetworkInfo info;
1842 switch (msg.what) {
1843 case NetworkStateTracker.EVENT_STATE_CHANGED:
1844 info = (NetworkInfo) msg.obj;
Robert Greenwalt12c44552009-12-07 11:33:18 -08001845 int type = info.getType();
1846 NetworkInfo.State state = info.getState();
Robert Greenwalt12c44552009-12-07 11:33:18 -08001847
Wink Savillee70c6f52010-12-03 12:01:38 -08001848 if (DBG) log("ConnectivityChange for " +
Robert Greenwalt0659da32009-07-16 17:21:39 -07001849 info.getTypeName() + ": " +
Robert Greenwalt12c44552009-12-07 11:33:18 -08001850 state + "/" + info.getDetailedState());
The Android Open Source Project28527d22009-03-03 19:31:44 -08001851
1852 // Connectivity state changed:
1853 // [31-13] Reserved for future use
Robert Greenwalt0659da32009-07-16 17:21:39 -07001854 // [12-9] Network subtype (for mobile network, as defined
1855 // by TelephonyManager)
1856 // [8-3] Detailed state ordinal (as defined by
1857 // NetworkInfo.DetailedState)
The Android Open Source Project28527d22009-03-03 19:31:44 -08001858 // [2-0] Network type (as defined by ConnectivityManager)
1859 int eventLogParam = (info.getType() & 0x7) |
1860 ((info.getDetailedState().ordinal() & 0x3f) << 3) |
1861 (info.getSubtype() << 9);
Doug Zongker2fc96232009-12-04 10:31:43 -08001862 EventLog.writeEvent(EventLogTags.CONNECTIVITY_STATE_CHANGED,
Robert Greenwalt0659da32009-07-16 17:21:39 -07001863 eventLogParam);
1864
1865 if (info.getDetailedState() ==
1866 NetworkInfo.DetailedState.FAILED) {
The Android Open Source Project28527d22009-03-03 19:31:44 -08001867 handleConnectionFailure(info);
Robert Greenwalt12c44552009-12-07 11:33:18 -08001868 } else if (state == NetworkInfo.State.DISCONNECTED) {
The Android Open Source Project28527d22009-03-03 19:31:44 -08001869 handleDisconnect(info);
Robert Greenwalt12c44552009-12-07 11:33:18 -08001870 } else if (state == NetworkInfo.State.SUSPENDED) {
The Android Open Source Project28527d22009-03-03 19:31:44 -08001871 // TODO: need to think this over.
Robert Greenwalt0659da32009-07-16 17:21:39 -07001872 // the logic here is, handle SUSPENDED the same as
1873 // DISCONNECTED. The only difference being we are
1874 // broadcasting an intent with NetworkInfo that's
1875 // suspended. This allows the applications an
1876 // opportunity to handle DISCONNECTED and SUSPENDED
1877 // differently, or not.
The Android Open Source Project28527d22009-03-03 19:31:44 -08001878 handleDisconnect(info);
Robert Greenwalt12c44552009-12-07 11:33:18 -08001879 } else if (state == NetworkInfo.State.CONNECTED) {
The Android Open Source Project28527d22009-03-03 19:31:44 -08001880 handleConnect(info);
1881 }
The Android Open Source Project28527d22009-03-03 19:31:44 -08001882 break;
The Android Open Source Project28527d22009-03-03 19:31:44 -08001883 case NetworkStateTracker.EVENT_CONFIGURATION_CHANGED:
Robert Greenwalt3afbead2010-07-23 15:46:26 -07001884 info = (NetworkInfo) msg.obj;
Robert Greenwalt36ea8692011-06-15 12:22:07 -07001885 handleConnectivityChange(info.getType(), true);
The Android Open Source Project28527d22009-03-03 19:31:44 -08001886 break;
Robert Greenwaltccb36f92010-09-24 14:32:21 -07001887 case EVENT_CLEAR_NET_TRANSITION_WAKELOCK:
Robert Greenwalt93dc1042010-06-15 12:19:37 -07001888 String causedBy = null;
1889 synchronized (ConnectivityService.this) {
1890 if (msg.arg1 == mNetTransitionWakeLockSerialNumber &&
1891 mNetTransitionWakeLock.isHeld()) {
1892 mNetTransitionWakeLock.release();
1893 causedBy = mNetTransitionWakeLockCausedBy;
1894 }
1895 }
1896 if (causedBy != null) {
Wink Savillee70c6f52010-12-03 12:01:38 -08001897 log("NetTransition Wakelock for " + causedBy + " released by timeout");
Robert Greenwalt93dc1042010-06-15 12:19:37 -07001898 }
Robert Greenwaltcf1a56c2010-09-09 14:05:10 -07001899 break;
Robert Greenwalt6a2db8a2010-09-23 10:05:56 -07001900 case EVENT_RESTORE_DEFAULT_NETWORK:
Robert Greenwaltaffc3a12009-09-27 17:27:04 -07001901 FeatureUser u = (FeatureUser)msg.obj;
1902 u.expire();
Robert Greenwalt986c7412010-09-08 15:24:47 -07001903 break;
Robert Greenwalt6a2db8a2010-09-23 10:05:56 -07001904 case EVENT_INET_CONDITION_CHANGE:
1905 {
1906 int netType = msg.arg1;
1907 int condition = msg.arg2;
1908 handleInetConditionChange(netType, condition);
Robert Greenwalt93dc1042010-06-15 12:19:37 -07001909 break;
Robert Greenwalt6a2db8a2010-09-23 10:05:56 -07001910 }
1911 case EVENT_INET_CONDITION_HOLD_END:
1912 {
1913 int netType = msg.arg1;
1914 int sequence = msg.arg2;
1915 handleInetConditionHoldEnd(netType, sequence);
Robert Greenwalt986c7412010-09-08 15:24:47 -07001916 break;
Robert Greenwalt6a2db8a2010-09-23 10:05:56 -07001917 }
1918 case EVENT_SET_NETWORK_PREFERENCE:
1919 {
1920 int preference = msg.arg1;
1921 handleSetNetworkPreference(preference);
1922 break;
1923 }
1924 case EVENT_SET_BACKGROUND_DATA:
1925 {
1926 boolean enabled = (msg.arg1 == ENABLED);
1927 handleSetBackgroundData(enabled);
1928 break;
1929 }
1930 case EVENT_SET_MOBILE_DATA:
1931 {
1932 boolean enabled = (msg.arg1 == ENABLED);
1933 handleSetMobileData(enabled);
1934 break;
1935 }
Robert Greenwaltc3c5f862010-10-11 16:00:27 -07001936 case EVENT_APPLY_GLOBAL_HTTP_PROXY:
1937 {
1938 handleDeprecatedGlobalHttpProxy();
Robert Greenwalt34848c02011-03-25 13:09:25 -07001939 break;
1940 }
1941 case EVENT_SET_DEPENDENCY_MET:
1942 {
1943 boolean met = (msg.arg1 == ENABLED);
1944 handleSetDependencyMet(msg.arg2, met);
1945 break;
Robert Greenwaltc3c5f862010-10-11 16:00:27 -07001946 }
The Android Open Source Project28527d22009-03-03 19:31:44 -08001947 }
1948 }
1949 }
Robert Greenwalt0c4828c2010-01-26 11:40:34 -08001950
1951 // javadoc from interface
Robert Greenwalt4283ded2010-03-02 17:25:02 -08001952 public int tether(String iface) {
Robert Greenwalt0c4828c2010-01-26 11:40:34 -08001953 enforceTetherChangePermission();
Robert Greenwalt4283ded2010-03-02 17:25:02 -08001954
1955 if (isTetheringSupported()) {
1956 return mTethering.tether(iface);
1957 } else {
1958 return ConnectivityManager.TETHER_ERROR_UNSUPPORTED;
1959 }
Robert Greenwalt0c4828c2010-01-26 11:40:34 -08001960 }
1961
1962 // javadoc from interface
Robert Greenwalt4283ded2010-03-02 17:25:02 -08001963 public int untether(String iface) {
Robert Greenwalt0c4828c2010-01-26 11:40:34 -08001964 enforceTetherChangePermission();
Robert Greenwalt4283ded2010-03-02 17:25:02 -08001965
1966 if (isTetheringSupported()) {
1967 return mTethering.untether(iface);
1968 } else {
1969 return ConnectivityManager.TETHER_ERROR_UNSUPPORTED;
1970 }
1971 }
1972
1973 // javadoc from interface
1974 public int getLastTetherError(String iface) {
1975 enforceTetherAccessPermission();
1976
1977 if (isTetheringSupported()) {
1978 return mTethering.getLastTetherError(iface);
1979 } else {
1980 return ConnectivityManager.TETHER_ERROR_UNSUPPORTED;
1981 }
Robert Greenwalt8e87f122010-02-11 18:18:40 -08001982 }
1983
1984 // TODO - proper iface API for selection by property, inspection, etc
1985 public String[] getTetherableUsbRegexs() {
1986 enforceTetherAccessPermission();
1987 if (isTetheringSupported()) {
1988 return mTethering.getTetherableUsbRegexs();
1989 } else {
1990 return new String[0];
1991 }
1992 }
1993
1994 public String[] getTetherableWifiRegexs() {
1995 enforceTetherAccessPermission();
1996 if (isTetheringSupported()) {
1997 return mTethering.getTetherableWifiRegexs();
1998 } else {
1999 return new String[0];
2000 }
Robert Greenwalt0c4828c2010-01-26 11:40:34 -08002001 }
2002
Danica Chang96567052010-08-11 14:54:43 -07002003 public String[] getTetherableBluetoothRegexs() {
2004 enforceTetherAccessPermission();
2005 if (isTetheringSupported()) {
2006 return mTethering.getTetherableBluetoothRegexs();
2007 } else {
2008 return new String[0];
2009 }
2010 }
2011
Robert Greenwalt0c4828c2010-01-26 11:40:34 -08002012 // TODO - move iface listing, queries, etc to new module
2013 // javadoc from interface
2014 public String[] getTetherableIfaces() {
Robert Greenwalt8e87f122010-02-11 18:18:40 -08002015 enforceTetherAccessPermission();
Robert Greenwalt0c4828c2010-01-26 11:40:34 -08002016 return mTethering.getTetherableIfaces();
2017 }
2018
2019 public String[] getTetheredIfaces() {
Robert Greenwalt8e87f122010-02-11 18:18:40 -08002020 enforceTetherAccessPermission();
Robert Greenwalt0c4828c2010-01-26 11:40:34 -08002021 return mTethering.getTetheredIfaces();
2022 }
Robert Greenwalt8e87f122010-02-11 18:18:40 -08002023
Robert Greenwalt4283ded2010-03-02 17:25:02 -08002024 public String[] getTetheringErroredIfaces() {
2025 enforceTetherAccessPermission();
2026 return mTethering.getErroredIfaces();
2027 }
2028
Robert Greenwalt8e87f122010-02-11 18:18:40 -08002029 // if ro.tether.denied = true we default to no tethering
2030 // gservices could set the secure setting to 1 though to enable it on a build where it
2031 // had previously been turned off.
2032 public boolean isTetheringSupported() {
2033 enforceTetherAccessPermission();
2034 int defaultVal = (SystemProperties.get("ro.tether.denied").equals("true") ? 0 : 1);
Robert Greenwaltf1b66e12010-02-25 12:29:30 -08002035 boolean tetherEnabledInSettings = (Settings.Secure.getInt(mContext.getContentResolver(),
2036 Settings.Secure.TETHER_SUPPORTED, defaultVal) != 0);
2037 return tetherEnabledInSettings && mTetheringConfigValid;
Robert Greenwalt8e87f122010-02-11 18:18:40 -08002038 }
Robert Greenwalt93dc1042010-06-15 12:19:37 -07002039
2040 // An API NetworkStateTrackers can call when they lose their network.
2041 // This will automatically be cleared after X seconds or a network becomes CONNECTED,
2042 // whichever happens first. The timer is started by the first caller and not
2043 // restarted by subsequent callers.
2044 public void requestNetworkTransitionWakelock(String forWhom) {
2045 enforceConnectivityInternalPermission();
2046 synchronized (this) {
2047 if (mNetTransitionWakeLock.isHeld()) return;
2048 mNetTransitionWakeLockSerialNumber++;
2049 mNetTransitionWakeLock.acquire();
2050 mNetTransitionWakeLockCausedBy = forWhom;
2051 }
2052 mHandler.sendMessageDelayed(mHandler.obtainMessage(
Robert Greenwaltccb36f92010-09-24 14:32:21 -07002053 EVENT_CLEAR_NET_TRANSITION_WAKELOCK,
Robert Greenwalt93dc1042010-06-15 12:19:37 -07002054 mNetTransitionWakeLockSerialNumber, 0),
2055 mNetTransitionWakeLockTimeout);
2056 return;
2057 }
Robert Greenwalt24118e82010-09-09 13:15:32 -07002058
Robert Greenwalt986c7412010-09-08 15:24:47 -07002059 // 100 percent is full good, 0 is full bad.
2060 public void reportInetCondition(int networkType, int percentage) {
Wink Savillee70c6f52010-12-03 12:01:38 -08002061 if (DBG) log("reportNetworkCondition(" + networkType + ", " + percentage + ")");
Robert Greenwalt986c7412010-09-08 15:24:47 -07002062 mContext.enforceCallingOrSelfPermission(
2063 android.Manifest.permission.STATUS_BAR,
2064 "ConnectivityService");
2065
Robert Greenwalt0e80be12010-09-20 14:35:25 -07002066 if (DBG) {
2067 int pid = getCallingPid();
2068 int uid = getCallingUid();
2069 String s = pid + "(" + uid + ") reports inet is " +
2070 (percentage > 50 ? "connected" : "disconnected") + " (" + percentage + ") on " +
2071 "network Type " + networkType + " at " + GregorianCalendar.getInstance().getTime();
2072 mInetLog.add(s);
2073 while(mInetLog.size() > INET_CONDITION_LOG_MAX_SIZE) {
2074 mInetLog.remove(0);
2075 }
2076 }
Robert Greenwalt986c7412010-09-08 15:24:47 -07002077 mHandler.sendMessage(mHandler.obtainMessage(
Robert Greenwalt6a2db8a2010-09-23 10:05:56 -07002078 EVENT_INET_CONDITION_CHANGE, networkType, percentage));
2079 }
2080
2081 private void handleInetConditionChange(int netType, int condition) {
2082 if (DBG) {
Wink Savillee70c6f52010-12-03 12:01:38 -08002083 log("Inet connectivity change, net=" +
Robert Greenwalt6a2db8a2010-09-23 10:05:56 -07002084 netType + ", condition=" + condition +
2085 ",mActiveDefaultNetwork=" + mActiveDefaultNetwork);
2086 }
2087 if (mActiveDefaultNetwork == -1) {
Wink Savillee70c6f52010-12-03 12:01:38 -08002088 if (DBG) log("no active default network - aborting");
Robert Greenwalt6a2db8a2010-09-23 10:05:56 -07002089 return;
2090 }
2091 if (mActiveDefaultNetwork != netType) {
Wink Savillee70c6f52010-12-03 12:01:38 -08002092 if (DBG) log("given net not default - aborting");
Robert Greenwalt6a2db8a2010-09-23 10:05:56 -07002093 return;
2094 }
2095 mDefaultInetCondition = condition;
2096 int delay;
2097 if (mInetConditionChangeInFlight == false) {
Wink Savillee70c6f52010-12-03 12:01:38 -08002098 if (DBG) log("starting a change hold");
Robert Greenwalt6a2db8a2010-09-23 10:05:56 -07002099 // setup a new hold to debounce this
2100 if (mDefaultInetCondition > 50) {
2101 delay = Settings.Secure.getInt(mContext.getContentResolver(),
2102 Settings.Secure.INET_CONDITION_DEBOUNCE_UP_DELAY, 500);
2103 } else {
2104 delay = Settings.Secure.getInt(mContext.getContentResolver(),
2105 Settings.Secure.INET_CONDITION_DEBOUNCE_DOWN_DELAY, 3000);
2106 }
2107 mInetConditionChangeInFlight = true;
2108 mHandler.sendMessageDelayed(mHandler.obtainMessage(EVENT_INET_CONDITION_HOLD_END,
2109 mActiveDefaultNetwork, mDefaultConnectionSequence), delay);
2110 } else {
2111 // we've set the new condition, when this hold ends that will get
2112 // picked up
Wink Savillee70c6f52010-12-03 12:01:38 -08002113 if (DBG) log("currently in hold - not setting new end evt");
Robert Greenwalt6a2db8a2010-09-23 10:05:56 -07002114 }
2115 }
2116
2117 private void handleInetConditionHoldEnd(int netType, int sequence) {
2118 if (DBG) {
Wink Savillee70c6f52010-12-03 12:01:38 -08002119 log("Inet hold end, net=" + netType +
Robert Greenwalt6a2db8a2010-09-23 10:05:56 -07002120 ", condition =" + mDefaultInetCondition +
2121 ", published condition =" + mDefaultInetConditionPublished);
2122 }
2123 mInetConditionChangeInFlight = false;
2124
2125 if (mActiveDefaultNetwork == -1) {
Wink Savillee70c6f52010-12-03 12:01:38 -08002126 if (DBG) log("no active default network - aborting");
Robert Greenwalt6a2db8a2010-09-23 10:05:56 -07002127 return;
2128 }
2129 if (mDefaultConnectionSequence != sequence) {
Wink Savillee70c6f52010-12-03 12:01:38 -08002130 if (DBG) log("event hold for obsolete network - aborting");
Robert Greenwalt6a2db8a2010-09-23 10:05:56 -07002131 return;
2132 }
2133 if (mDefaultInetConditionPublished == mDefaultInetCondition) {
Wink Savillee70c6f52010-12-03 12:01:38 -08002134 if (DBG) log("no change in condition - aborting");
Robert Greenwalt6a2db8a2010-09-23 10:05:56 -07002135 return;
2136 }
2137 NetworkInfo networkInfo = mNetTrackers[mActiveDefaultNetwork].getNetworkInfo();
2138 if (networkInfo.isConnected() == false) {
Wink Savillee70c6f52010-12-03 12:01:38 -08002139 if (DBG) log("default network not connected - aborting");
Robert Greenwalt6a2db8a2010-09-23 10:05:56 -07002140 return;
2141 }
2142 mDefaultInetConditionPublished = mDefaultInetCondition;
2143 sendInetConditionBroadcast(networkInfo);
2144 return;
Robert Greenwalt986c7412010-09-08 15:24:47 -07002145 }
Robert Greenwaltc3c5f862010-10-11 16:00:27 -07002146
2147 public synchronized ProxyProperties getProxy() {
2148 if (mGlobalProxy != null) return mGlobalProxy;
2149 if (mDefaultProxy != null) return mDefaultProxy;
2150 return null;
2151 }
2152
2153 public void setGlobalProxy(ProxyProperties proxyProperties) {
2154 enforceChangePermission();
2155 synchronized (mGlobalProxyLock) {
2156 if (proxyProperties == mGlobalProxy) return;
2157 if (proxyProperties != null && proxyProperties.equals(mGlobalProxy)) return;
2158 if (mGlobalProxy != null && mGlobalProxy.equals(proxyProperties)) return;
2159
2160 String host = "";
2161 int port = 0;
2162 String exclList = "";
2163 if (proxyProperties != null && !TextUtils.isEmpty(proxyProperties.getHost())) {
2164 mGlobalProxy = new ProxyProperties(proxyProperties);
2165 host = mGlobalProxy.getHost();
2166 port = mGlobalProxy.getPort();
2167 exclList = mGlobalProxy.getExclusionList();
2168 } else {
2169 mGlobalProxy = null;
2170 }
2171 ContentResolver res = mContext.getContentResolver();
2172 Settings.Secure.putString(res, Settings.Secure.GLOBAL_HTTP_PROXY_HOST, host);
2173 Settings.Secure.putInt(res, Settings.Secure.GLOBAL_HTTP_PROXY_PORT, port);
Robert Greenwalt6f7c6092010-12-02 11:31:00 -08002174 Settings.Secure.putString(res, Settings.Secure.GLOBAL_HTTP_PROXY_EXCLUSION_LIST,
Robert Greenwaltc3c5f862010-10-11 16:00:27 -07002175 exclList);
2176 }
2177
2178 if (mGlobalProxy == null) {
2179 proxyProperties = mDefaultProxy;
2180 }
2181 sendProxyBroadcast(proxyProperties);
2182 }
2183
Robert Greenwalt6f7c6092010-12-02 11:31:00 -08002184 private void loadGlobalProxy() {
2185 ContentResolver res = mContext.getContentResolver();
2186 String host = Settings.Secure.getString(res, Settings.Secure.GLOBAL_HTTP_PROXY_HOST);
2187 int port = Settings.Secure.getInt(res, Settings.Secure.GLOBAL_HTTP_PROXY_PORT, 0);
2188 String exclList = Settings.Secure.getString(res,
2189 Settings.Secure.GLOBAL_HTTP_PROXY_EXCLUSION_LIST);
2190 if (!TextUtils.isEmpty(host)) {
2191 ProxyProperties proxyProperties = new ProxyProperties(host, port, exclList);
2192 synchronized (mGlobalProxyLock) {
2193 mGlobalProxy = proxyProperties;
2194 }
2195 }
2196 }
2197
Robert Greenwaltc3c5f862010-10-11 16:00:27 -07002198 public ProxyProperties getGlobalProxy() {
2199 synchronized (mGlobalProxyLock) {
2200 return mGlobalProxy;
2201 }
2202 }
2203
2204 private void handleApplyDefaultProxy(int type) {
2205 // check if new default - push it out to all VM if so
2206 ProxyProperties proxy = mNetTrackers[type].getLinkProperties().getHttpProxy();
2207 synchronized (this) {
2208 if (mDefaultProxy != null && mDefaultProxy.equals(proxy)) return;
2209 if (mDefaultProxy == proxy) return;
2210 if (!TextUtils.isEmpty(proxy.getHost())) {
2211 mDefaultProxy = proxy;
2212 } else {
2213 mDefaultProxy = null;
2214 }
2215 }
Wink Savillee70c6f52010-12-03 12:01:38 -08002216 if (DBG) log("changing default proxy to " + proxy);
Robert Greenwaltc3c5f862010-10-11 16:00:27 -07002217 if ((proxy == null && mGlobalProxy == null) || proxy.equals(mGlobalProxy)) return;
2218 if (mGlobalProxy != null) return;
2219 sendProxyBroadcast(proxy);
2220 }
2221
2222 private void handleDeprecatedGlobalHttpProxy() {
2223 String proxy = Settings.Secure.getString(mContext.getContentResolver(),
2224 Settings.Secure.HTTP_PROXY);
2225 if (!TextUtils.isEmpty(proxy)) {
2226 String data[] = proxy.split(":");
2227 String proxyHost = data[0];
2228 int proxyPort = 8080;
2229 if (data.length > 1) {
2230 try {
2231 proxyPort = Integer.parseInt(data[1]);
2232 } catch (NumberFormatException e) {
2233 return;
2234 }
2235 }
2236 ProxyProperties p = new ProxyProperties(data[0], proxyPort, "");
2237 setGlobalProxy(p);
2238 }
2239 }
2240
2241 private void sendProxyBroadcast(ProxyProperties proxy) {
Robert Greenwalt611291c2010-12-23 15:51:10 -08002242 if (proxy == null) proxy = new ProxyProperties("", 0, "");
Wink Savillee70c6f52010-12-03 12:01:38 -08002243 log("sending Proxy Broadcast for " + proxy);
Robert Greenwaltc3c5f862010-10-11 16:00:27 -07002244 Intent intent = new Intent(Proxy.PROXY_CHANGE_ACTION);
Stan Chesnutt1f2a2ac2011-01-06 11:00:19 -08002245 intent.addFlags(Intent.FLAG_RECEIVER_REPLACE_PENDING |
2246 Intent.FLAG_RECEIVER_REGISTERED_ONLY_BEFORE_BOOT);
Robert Greenwaltc3c5f862010-10-11 16:00:27 -07002247 intent.putExtra(Proxy.EXTRA_PROXY_INFO, proxy);
Robert Greenwaltd93dc8f2010-12-06 11:29:17 -08002248 mContext.sendStickyBroadcast(intent);
Robert Greenwaltc3c5f862010-10-11 16:00:27 -07002249 }
2250
2251 private static class SettingsObserver extends ContentObserver {
2252 private int mWhat;
2253 private Handler mHandler;
2254 SettingsObserver(Handler handler, int what) {
2255 super(handler);
2256 mHandler = handler;
2257 mWhat = what;
2258 }
2259
2260 void observe(Context context) {
2261 ContentResolver resolver = context.getContentResolver();
2262 resolver.registerContentObserver(Settings.Secure.getUriFor(
2263 Settings.Secure.HTTP_PROXY), false, this);
2264 }
2265
2266 @Override
2267 public void onChange(boolean selfChange) {
2268 mHandler.obtainMessage(mWhat).sendToTarget();
2269 }
2270 }
Wink Savillee70c6f52010-12-03 12:01:38 -08002271
2272 private void log(String s) {
2273 Slog.d(TAG, s);
2274 }
2275
2276 private void loge(String s) {
2277 Slog.e(TAG, s);
2278 }
Wink Savilleb7c92c72011-03-12 14:52:01 -08002279 int convertFeatureToNetworkType(String feature){
2280 int networkType = -1;
2281 if (TextUtils.equals(feature, Phone.FEATURE_ENABLE_MMS)) {
2282 networkType = ConnectivityManager.TYPE_MOBILE_MMS;
2283 } else if (TextUtils.equals(feature, Phone.FEATURE_ENABLE_SUPL)) {
2284 networkType = ConnectivityManager.TYPE_MOBILE_SUPL;
2285 } else if (TextUtils.equals(feature, Phone.FEATURE_ENABLE_DUN) ||
2286 TextUtils.equals(feature, Phone.FEATURE_ENABLE_DUN_ALWAYS)) {
2287 networkType = ConnectivityManager.TYPE_MOBILE_DUN;
2288 } else if (TextUtils.equals(feature, Phone.FEATURE_ENABLE_HIPRI)) {
2289 networkType = ConnectivityManager.TYPE_MOBILE_HIPRI;
2290 } else if (TextUtils.equals(feature, Phone.FEATURE_ENABLE_FOTA)) {
2291 networkType = ConnectivityManager.TYPE_MOBILE_FOTA;
2292 } else if (TextUtils.equals(feature, Phone.FEATURE_ENABLE_IMS)) {
2293 networkType = ConnectivityManager.TYPE_MOBILE_IMS;
2294 } else if (TextUtils.equals(feature, Phone.FEATURE_ENABLE_CBS)) {
2295 networkType = ConnectivityManager.TYPE_MOBILE_CBS;
2296 }
2297 return networkType;
2298 }
The Android Open Source Project28527d22009-03-03 19:31:44 -08002299}