blob: a564c2d4bac113b51dd71e309cce4299e968e4ad [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 Goby211b5692010-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;
Mike Lockwood0d5916c2011-05-28 13:24:04 -040042import android.os.FileUtils;
The Android Open Source Project28527d22009-03-03 19:31:44 -080043import android.os.Handler;
Wink Saville775aad62010-09-02 19:23:52 -070044import android.os.HandlerThread;
Robert Greenwalt2034b912009-08-12 16:08:25 -070045import android.os.IBinder;
Chia-chi Yeh4df51322011-05-11 16:35:13 -070046import android.os.INetworkManagementService;
The Android Open Source Project28527d22009-03-03 19:31:44 -080047import android.os.Looper;
48import android.os.Message;
Robert Greenwalt93dc1042010-06-15 12:19:37 -070049import android.os.PowerManager;
Robert Greenwalt2034b912009-08-12 16:08:25 -070050import android.os.RemoteException;
The Android Open Source Project28527d22009-03-03 19:31:44 -080051import android.os.ServiceManager;
52import android.os.SystemProperties;
53import android.provider.Settings;
Robert Greenwalt2034b912009-08-12 16:08:25 -070054import android.text.TextUtils;
The Android Open Source Project28527d22009-03-03 19:31:44 -080055import android.util.EventLog;
Joe Onoratoc2386bb2010-02-26 18:56:32 -080056import android.util.Slog;
The Android Open Source Project28527d22009-03-03 19:31:44 -080057
Robert Greenwalt2034b912009-08-12 16:08:25 -070058import com.android.internal.telephony.Phone;
Robert Greenwalt0c4828c2010-01-26 11:40:34 -080059import com.android.server.connectivity.Tethering;
60
The Android Open Source Project28527d22009-03-03 19:31:44 -080061import java.io.FileDescriptor;
Irfan Sheriff7f132d92010-06-09 15:39:36 -070062import java.io.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
Wink Saville775aad62010-09-02 19:23:52 -0700253 public static synchronized ConnectivityService getInstance(Context context) {
254 if (sServiceInstance == null) {
255 sServiceInstance = new ConnectivityService(context);
The Android Open Source Project28527d22009-03-03 19:31:44 -0800256 }
Wink Saville775aad62010-09-02 19:23:52 -0700257 return sServiceInstance;
The Android Open Source Project28527d22009-03-03 19:31:44 -0800258 }
Robert Greenwalt0659da32009-07-16 17:21:39 -0700259
The Android Open Source Project28527d22009-03-03 19:31:44 -0800260 private ConnectivityService(Context context) {
Wink Savillee70c6f52010-12-03 12:01:38 -0800261 if (DBG) log("ConnectivityService starting up");
Robert Greenwaltd48f8ee2010-01-14 17:47:58 -0800262
Wink Saville775aad62010-09-02 19:23:52 -0700263 HandlerThread handlerThread = new HandlerThread("ConnectivityServiceThread");
264 handlerThread.start();
265 mHandler = new MyHandler(handlerThread.getLooper());
266
Robert Greenwaltd62c7002010-12-29 16:15:02 -0800267 mBackgroundDataEnabled.set(Settings.Secure.getInt(context.getContentResolver(),
268 Settings.Secure.BACKGROUND_DATA, 1) == 1);
269
Robert Greenwaltd48f8ee2010-01-14 17:47:58 -0800270 // setup our unique device name
Robert Greenwalt82cde132010-12-06 09:30:17 -0800271 if (TextUtils.isEmpty(SystemProperties.get("net.hostname"))) {
272 String id = Settings.Secure.getString(context.getContentResolver(),
273 Settings.Secure.ANDROID_ID);
274 if (id != null && id.length() > 0) {
275 String name = new String("android_").concat(id);
276 SystemProperties.set("net.hostname", name);
277 }
Robert Greenwaltd48f8ee2010-01-14 17:47:58 -0800278 }
279
Robert Greenwalt94daa182010-09-01 11:34:05 -0700280 // read our default dns server ip
281 String dns = Settings.Secure.getString(context.getContentResolver(),
282 Settings.Secure.DEFAULT_DNS_SERVER);
283 if (dns == null || dns.length() == 0) {
284 dns = context.getResources().getString(
285 com.android.internal.R.string.config_default_dns_server);
286 }
287 try {
Robert Greenwalt35e34d12011-02-22 16:00:42 -0800288 mDefaultDns = NetworkUtils.numericToInetAddress(dns);
289 } catch (IllegalArgumentException e) {
Wink Savillee70c6f52010-12-03 12:01:38 -0800290 loge("Error setting defaultDns using " + dns);
Robert Greenwalt94daa182010-09-01 11:34:05 -0700291 }
292
The Android Open Source Project28527d22009-03-03 19:31:44 -0800293 mContext = context;
Robert Greenwalt93dc1042010-06-15 12:19:37 -0700294
295 PowerManager powerManager = (PowerManager)mContext.getSystemService(Context.POWER_SERVICE);
296 mNetTransitionWakeLock = powerManager.newWakeLock(PowerManager.PARTIAL_WAKE_LOCK, TAG);
297 mNetTransitionWakeLockTimeout = mContext.getResources().getInteger(
298 com.android.internal.R.integer.config_networkTransitionTimeout);
299
Robert Greenwalt2034b912009-08-12 16:08:25 -0700300 mNetTrackers = new NetworkStateTracker[
301 ConnectivityManager.MAX_NETWORK_TYPE+1];
Robert Greenwalt0659da32009-07-16 17:21:39 -0700302
The Android Open Source Project28527d22009-03-03 19:31:44 -0800303 mNetworkPreference = getPersistedNetworkPreference();
Robert Greenwalt0659da32009-07-16 17:21:39 -0700304
Robert Greenwaltec05b3c2009-10-30 14:17:42 -0700305 mRadioAttributes = new RadioAttributes[ConnectivityManager.MAX_RADIO_TYPE+1];
Robert Greenwalt34848c02011-03-25 13:09:25 -0700306 mNetConfigs = new NetworkConfig[ConnectivityManager.MAX_NETWORK_TYPE+1];
Robert Greenwaltec05b3c2009-10-30 14:17:42 -0700307
Robert Greenwalt2034b912009-08-12 16:08:25 -0700308 // Load device network attributes from resources
Robert Greenwalt2034b912009-08-12 16:08:25 -0700309 String[] raStrings = context.getResources().getStringArray(
310 com.android.internal.R.array.radioAttributes);
Robert Greenwaltec05b3c2009-10-30 14:17:42 -0700311 for (String raString : raStrings) {
312 RadioAttributes r = new RadioAttributes(raString);
313 if (r.mType > ConnectivityManager.MAX_RADIO_TYPE) {
Wink Savillee70c6f52010-12-03 12:01:38 -0800314 loge("Error in radioAttributes - ignoring attempt to define type " + r.mType);
Robert Greenwaltec05b3c2009-10-30 14:17:42 -0700315 continue;
316 }
317 if (mRadioAttributes[r.mType] != null) {
Wink Savillee70c6f52010-12-03 12:01:38 -0800318 loge("Error in radioAttributes - ignoring attempt to redefine type " +
Robert Greenwaltec05b3c2009-10-30 14:17:42 -0700319 r.mType);
320 continue;
321 }
Robert Greenwalt2034b912009-08-12 16:08:25 -0700322 mRadioAttributes[r.mType] = r;
323 }
324
Robert Greenwaltec05b3c2009-10-30 14:17:42 -0700325 String[] naStrings = context.getResources().getStringArray(
326 com.android.internal.R.array.networkAttributes);
327 for (String naString : naStrings) {
328 try {
Robert Greenwalt34848c02011-03-25 13:09:25 -0700329 NetworkConfig n = new NetworkConfig(naString);
Wink Savillef2a62832011-04-07 14:23:45 -0700330 if (n.type > ConnectivityManager.MAX_NETWORK_TYPE) {
Wink Savillee70c6f52010-12-03 12:01:38 -0800331 loge("Error in networkAttributes - ignoring attempt to define type " +
Wink Savillef2a62832011-04-07 14:23:45 -0700332 n.type);
Robert Greenwaltec05b3c2009-10-30 14:17:42 -0700333 continue;
Robert Greenwalt2034b912009-08-12 16:08:25 -0700334 }
Wink Savillef2a62832011-04-07 14:23:45 -0700335 if (mNetConfigs[n.type] != null) {
Wink Savillee70c6f52010-12-03 12:01:38 -0800336 loge("Error in networkAttributes - ignoring attempt to redefine type " +
Wink Savillef2a62832011-04-07 14:23:45 -0700337 n.type);
Robert Greenwaltec05b3c2009-10-30 14:17:42 -0700338 continue;
339 }
Wink Savillef2a62832011-04-07 14:23:45 -0700340 if (mRadioAttributes[n.radio] == null) {
Wink Savillee70c6f52010-12-03 12:01:38 -0800341 loge("Error in networkAttributes - ignoring attempt to use undefined " +
Wink Savillef2a62832011-04-07 14:23:45 -0700342 "radio " + n.radio + " in network type " + n.type);
Robert Greenwaltec05b3c2009-10-30 14:17:42 -0700343 continue;
344 }
Wink Savillef2a62832011-04-07 14:23:45 -0700345 mNetConfigs[n.type] = n;
Robert Greenwaltec05b3c2009-10-30 14:17:42 -0700346 mNetworksDefined++;
347 } catch(Exception e) {
348 // ignore it - leave the entry null
Robert Greenwalt2034b912009-08-12 16:08:25 -0700349 }
350 }
351
Robert Greenwaltec05b3c2009-10-30 14:17:42 -0700352 // high priority first
353 mPriorityList = new int[mNetworksDefined];
354 {
355 int insertionPoint = mNetworksDefined-1;
356 int currentLowest = 0;
357 int nextLowest = 0;
358 while (insertionPoint > -1) {
Robert Greenwalt34848c02011-03-25 13:09:25 -0700359 for (NetworkConfig na : mNetConfigs) {
Robert Greenwaltec05b3c2009-10-30 14:17:42 -0700360 if (na == null) continue;
Wink Savillef2a62832011-04-07 14:23:45 -0700361 if (na.priority < currentLowest) continue;
362 if (na.priority > currentLowest) {
363 if (na.priority < nextLowest || nextLowest == 0) {
364 nextLowest = na.priority;
Robert Greenwaltec05b3c2009-10-30 14:17:42 -0700365 }
366 continue;
367 }
Wink Savillef2a62832011-04-07 14:23:45 -0700368 mPriorityList[insertionPoint--] = na.type;
Robert Greenwaltec05b3c2009-10-30 14:17:42 -0700369 }
370 currentLowest = nextLowest;
371 nextLowest = 0;
372 }
373 }
374
375 mNetRequestersPids = new ArrayList[ConnectivityManager.MAX_NETWORK_TYPE+1];
376 for (int i : mPriorityList) {
Robert Greenwalt2034b912009-08-12 16:08:25 -0700377 mNetRequestersPids[i] = new ArrayList();
378 }
379
380 mFeatureUsers = new ArrayList();
381
Robert Greenwaltec05b3c2009-10-30 14:17:42 -0700382 mNumDnsEntries = 0;
383
384 mTestMode = SystemProperties.get("cm.test.mode").equals("true")
385 && SystemProperties.get("ro.build.type").equals("eng");
The Android Open Source Project28527d22009-03-03 19:31:44 -0800386 /*
387 * Create the network state trackers for Wi-Fi and mobile
388 * data. Maybe this could be done with a factory class,
389 * but it's not clear that it's worth it, given that
390 * the number of different network types is not going
391 * to change very often.
392 */
Robert Greenwaltec05b3c2009-10-30 14:17:42 -0700393 for (int netType : mPriorityList) {
Wink Savillef2a62832011-04-07 14:23:45 -0700394 switch (mNetConfigs[netType].radio) {
Robert Greenwaltec05b3c2009-10-30 14:17:42 -0700395 case ConnectivityManager.TYPE_WIFI:
Wink Savillee70c6f52010-12-03 12:01:38 -0800396 if (DBG) log("Starting Wifi Service.");
Wink Saville7fabfa22010-08-13 16:11:42 -0700397 WifiStateTracker wst = new WifiStateTracker();
Irfan Sheriff25be0762010-07-28 09:35:20 -0700398 WifiService wifiService = new WifiService(context);
Robert Greenwaltec05b3c2009-10-30 14:17:42 -0700399 ServiceManager.addService(Context.WIFI_SERVICE, wifiService);
Irfan Sheriff25be0762010-07-28 09:35:20 -0700400 wifiService.checkAndStartWifi();
Robert Greenwaltec05b3c2009-10-30 14:17:42 -0700401 mNetTrackers[ConnectivityManager.TYPE_WIFI] = wst;
Wink Saville7fabfa22010-08-13 16:11:42 -0700402 wst.startMonitoring(context, mHandler);
The Android Open Source Project28527d22009-03-03 19:31:44 -0800403
Irfan Sheriff653e2a22010-06-07 09:03:04 -0700404 //TODO: as part of WWS refactor, create only when needed
Irfan Sheriff25be0762010-07-28 09:35:20 -0700405 mWifiWatchdogService = new WifiWatchdogService(context);
Irfan Sheriff653e2a22010-06-07 09:03:04 -0700406
Robert Greenwaltec05b3c2009-10-30 14:17:42 -0700407 break;
408 case ConnectivityManager.TYPE_MOBILE:
Wink Saville7fabfa22010-08-13 16:11:42 -0700409 mNetTrackers[netType] = new MobileDataStateTracker(netType,
Wink Savillef2a62832011-04-07 14:23:45 -0700410 mNetConfigs[netType].name);
Wink Saville7fabfa22010-08-13 16:11:42 -0700411 mNetTrackers[netType].startMonitoring(context, mHandler);
Robert Greenwaltec05b3c2009-10-30 14:17:42 -0700412 break;
Robert Greenwalteb123ac2010-12-06 13:56:24 -0800413 case ConnectivityManager.TYPE_DUMMY:
414 mNetTrackers[netType] = new DummyDataStateTracker(netType,
Wink Savillef2a62832011-04-07 14:23:45 -0700415 mNetConfigs[netType].name);
Robert Greenwalteb123ac2010-12-06 13:56:24 -0800416 mNetTrackers[netType].startMonitoring(context, mHandler);
417 break;
Jaikumar Ganesh0db51a02010-12-21 22:31:44 -0800418 case ConnectivityManager.TYPE_BLUETOOTH:
419 mNetTrackers[netType] = BluetoothTetheringDataTracker.getInstance();
420 mNetTrackers[netType].startMonitoring(context, mHandler);
421 break;
Benoit Goby211b5692010-12-22 14:29:40 -0800422 case ConnectivityManager.TYPE_ETHERNET:
423 mNetTrackers[netType] = EthernetDataTracker.getInstance();
424 mNetTrackers[netType].startMonitoring(context, mHandler);
425 break;
Robert Greenwaltec05b3c2009-10-30 14:17:42 -0700426 default:
Wink Savillee70c6f52010-12-03 12:01:38 -0800427 loge("Trying to create a DataStateTracker for an unknown radio type " +
Wink Savillef2a62832011-04-07 14:23:45 -0700428 mNetConfigs[netType].radio);
Robert Greenwaltec05b3c2009-10-30 14:17:42 -0700429 continue;
430 }
431 }
Robert Greenwalt0c4828c2010-01-26 11:40:34 -0800432
Chia-chi Yeh4df51322011-05-11 16:35:13 -0700433 IBinder b = ServiceManager.getService(Context.NETWORKMANAGEMENT_SERVICE);
434 INetworkManagementService nmService = INetworkManagementService.Stub.asInterface(b);
435
436 mTethering = new Tethering(mContext, nmService, mHandler.getLooper());
Robert Greenwaltf1b66e12010-02-25 12:29:30 -0800437 mTetheringConfigValid = (((mNetTrackers[ConnectivityManager.TYPE_MOBILE_DUN] != null) ||
438 !mTethering.isDunRequired()) &&
439 (mTethering.getTetherableUsbRegexs().length != 0 ||
Danica Chang96567052010-08-11 14:54:43 -0700440 mTethering.getTetherableWifiRegexs().length != 0 ||
441 mTethering.getTetherableBluetoothRegexs().length != 0) &&
Robert Greenwaltf1b66e12010-02-25 12:29:30 -0800442 mTethering.getUpstreamIfaceRegexs().length != 0);
443
Robert Greenwalt0e80be12010-09-20 14:35:25 -0700444 if (DBG) {
445 mInetLog = new ArrayList();
446 }
Robert Greenwaltc3c5f862010-10-11 16:00:27 -0700447
448 mSettingsObserver = new SettingsObserver(mHandler, EVENT_APPLY_GLOBAL_HTTP_PROXY);
449 mSettingsObserver.observe(mContext);
Robert Greenwalt6f7c6092010-12-02 11:31:00 -0800450
451 loadGlobalProxy();
Hung-ying Tyan4e723422011-01-19 16:48:38 +0800452
453 VpnManager.startVpnService(context);
The Android Open Source Project28527d22009-03-03 19:31:44 -0800454 }
455
Robert Greenwaltec05b3c2009-10-30 14:17:42 -0700456
The Android Open Source Project28527d22009-03-03 19:31:44 -0800457 /**
Robert Greenwalt0659da32009-07-16 17:21:39 -0700458 * Sets the preferred network.
The Android Open Source Project28527d22009-03-03 19:31:44 -0800459 * @param preference the new preference
460 */
Robert Greenwalt6a2db8a2010-09-23 10:05:56 -0700461 public void setNetworkPreference(int preference) {
The Android Open Source Project28527d22009-03-03 19:31:44 -0800462 enforceChangePermission();
Robert Greenwalt6a2db8a2010-09-23 10:05:56 -0700463
464 mHandler.sendMessage(mHandler.obtainMessage(EVENT_SET_NETWORK_PREFERENCE, preference, 0));
The Android Open Source Project28527d22009-03-03 19:31:44 -0800465 }
466
467 public int getNetworkPreference() {
468 enforceAccessPermission();
Robert Greenwalt6a2db8a2010-09-23 10:05:56 -0700469 int preference;
470 synchronized(this) {
471 preference = mNetworkPreference;
472 }
473 return preference;
The Android Open Source Project28527d22009-03-03 19:31:44 -0800474 }
475
Robert Greenwalt6a2db8a2010-09-23 10:05:56 -0700476 private void handleSetNetworkPreference(int preference) {
477 if (ConnectivityManager.isNetworkTypeValid(preference) &&
Robert Greenwalt34848c02011-03-25 13:09:25 -0700478 mNetConfigs[preference] != null &&
479 mNetConfigs[preference].isDefault()) {
Robert Greenwalt6a2db8a2010-09-23 10:05:56 -0700480 if (mNetworkPreference != preference) {
481 final ContentResolver cr = mContext.getContentResolver();
482 Settings.Secure.putInt(cr, Settings.Secure.NETWORK_PREFERENCE, preference);
483 synchronized(this) {
484 mNetworkPreference = preference;
485 }
486 enforcePreference();
487 }
488 }
The Android Open Source Project28527d22009-03-03 19:31:44 -0800489 }
Robert Greenwalt0659da32009-07-16 17:21:39 -0700490
The Android Open Source Project28527d22009-03-03 19:31:44 -0800491 private int getPersistedNetworkPreference() {
492 final ContentResolver cr = mContext.getContentResolver();
493
494 final int networkPrefSetting = Settings.Secure
495 .getInt(cr, Settings.Secure.NETWORK_PREFERENCE, -1);
496 if (networkPrefSetting != -1) {
497 return networkPrefSetting;
498 }
499
500 return ConnectivityManager.DEFAULT_NETWORK_PREFERENCE;
501 }
Robert Greenwalt0659da32009-07-16 17:21:39 -0700502
The Android Open Source Project28527d22009-03-03 19:31:44 -0800503 /**
Robert Greenwalt0659da32009-07-16 17:21:39 -0700504 * Make the state of network connectivity conform to the preference settings
The Android Open Source Project28527d22009-03-03 19:31:44 -0800505 * In this method, we only tear down a non-preferred network. Establishing
506 * a connection to the preferred network is taken care of when we handle
507 * the disconnect event from the non-preferred network
508 * (see {@link #handleDisconnect(NetworkInfo)}).
509 */
510 private void enforcePreference() {
Robert Greenwalt2034b912009-08-12 16:08:25 -0700511 if (mNetTrackers[mNetworkPreference].getNetworkInfo().isConnected())
The Android Open Source Project28527d22009-03-03 19:31:44 -0800512 return;
513
Robert Greenwalt2034b912009-08-12 16:08:25 -0700514 if (!mNetTrackers[mNetworkPreference].isAvailable())
515 return;
The Android Open Source Project28527d22009-03-03 19:31:44 -0800516
Robert Greenwalt2034b912009-08-12 16:08:25 -0700517 for (int t=0; t <= ConnectivityManager.MAX_RADIO_TYPE; t++) {
Robert Greenwaltec05b3c2009-10-30 14:17:42 -0700518 if (t != mNetworkPreference && mNetTrackers[t] != null &&
Robert Greenwalt2034b912009-08-12 16:08:25 -0700519 mNetTrackers[t].getNetworkInfo().isConnected()) {
Robert Greenwaltf3f045b2009-08-20 15:25:14 -0700520 if (DBG) {
Wink Savillee70c6f52010-12-03 12:01:38 -0800521 log("tearing down " + mNetTrackers[t].getNetworkInfo() +
Robert Greenwaltf3f045b2009-08-20 15:25:14 -0700522 " in enforcePreference");
523 }
Robert Greenwalt2034b912009-08-12 16:08:25 -0700524 teardown(mNetTrackers[t]);
The Android Open Source Project28527d22009-03-03 19:31:44 -0800525 }
526 }
527 }
528
529 private boolean teardown(NetworkStateTracker netTracker) {
530 if (netTracker.teardown()) {
531 netTracker.setTeardownRequested(true);
532 return true;
533 } else {
534 return false;
535 }
536 }
537
538 /**
539 * Return NetworkInfo for the active (i.e., connected) network interface.
540 * It is assumed that at most one network is active at a time. If more
541 * than one is active, it is indeterminate which will be returned.
Robert Greenwalt0659da32009-07-16 17:21:39 -0700542 * @return the info for the active network, or {@code null} if none is
543 * active
The Android Open Source Project28527d22009-03-03 19:31:44 -0800544 */
545 public NetworkInfo getActiveNetworkInfo() {
Robert Greenwalte1544bb2011-05-20 12:23:41 -0700546 return getNetworkInfo(mActiveDefaultNetwork);
The Android Open Source Project28527d22009-03-03 19:31:44 -0800547 }
548
549 public NetworkInfo getNetworkInfo(int networkType) {
550 enforceAccessPermission();
551 if (ConnectivityManager.isNetworkTypeValid(networkType)) {
552 NetworkStateTracker t = mNetTrackers[networkType];
553 if (t != null)
554 return t.getNetworkInfo();
555 }
556 return null;
557 }
558
559 public NetworkInfo[] getAllNetworkInfo() {
560 enforceAccessPermission();
Robert Greenwaltec05b3c2009-10-30 14:17:42 -0700561 NetworkInfo[] result = new NetworkInfo[mNetworksDefined];
The Android Open Source Project28527d22009-03-03 19:31:44 -0800562 int i = 0;
563 for (NetworkStateTracker t : mNetTrackers) {
Robert Greenwaltec05b3c2009-10-30 14:17:42 -0700564 if(t != null) result[i++] = t.getNetworkInfo();
The Android Open Source Project28527d22009-03-03 19:31:44 -0800565 }
566 return result;
567 }
568
Robert Greenwalt9f0ee4f2010-09-14 09:18:02 -0700569 /**
570 * Return LinkProperties for the active (i.e., connected) default
571 * network interface. It is assumed that at most one default network
572 * is active at a time. If more than one is active, it is indeterminate
573 * which will be returned.
574 * @return the ip properties for the active network, or {@code null} if
575 * none is active
576 */
577 public LinkProperties getActiveLinkProperties() {
Robert Greenwalte1544bb2011-05-20 12:23:41 -0700578 return getLinkProperties(mActiveDefaultNetwork);
Robert Greenwalt9f0ee4f2010-09-14 09:18:02 -0700579 }
580
581 public LinkProperties getLinkProperties(int networkType) {
582 enforceAccessPermission();
583 if (ConnectivityManager.isNetworkTypeValid(networkType)) {
584 NetworkStateTracker t = mNetTrackers[networkType];
585 if (t != null) return t.getLinkProperties();
586 }
587 return null;
588 }
589
The Android Open Source Project28527d22009-03-03 19:31:44 -0800590 public boolean setRadios(boolean turnOn) {
591 boolean result = true;
592 enforceChangePermission();
593 for (NetworkStateTracker t : mNetTrackers) {
Robert Greenwaltec05b3c2009-10-30 14:17:42 -0700594 if (t != null) result = t.setRadio(turnOn) && result;
The Android Open Source Project28527d22009-03-03 19:31:44 -0800595 }
596 return result;
597 }
598
599 public boolean setRadio(int netType, boolean turnOn) {
600 enforceChangePermission();
601 if (!ConnectivityManager.isNetworkTypeValid(netType)) {
602 return false;
603 }
604 NetworkStateTracker tracker = mNetTrackers[netType];
605 return tracker != null && tracker.setRadio(turnOn);
606 }
607
Robert Greenwaltaffc3a12009-09-27 17:27:04 -0700608 /**
609 * Used to notice when the calling process dies so we can self-expire
610 *
611 * Also used to know if the process has cleaned up after itself when
612 * our auto-expire timer goes off. The timer has a link to an object.
613 *
614 */
Robert Greenwalt2034b912009-08-12 16:08:25 -0700615 private class FeatureUser implements IBinder.DeathRecipient {
616 int mNetworkType;
617 String mFeature;
618 IBinder mBinder;
619 int mPid;
620 int mUid;
Robert Greenwalt3eeb6032009-12-21 18:24:07 -0800621 long mCreateTime;
Robert Greenwalt2034b912009-08-12 16:08:25 -0700622
623 FeatureUser(int type, String feature, IBinder binder) {
624 super();
625 mNetworkType = type;
626 mFeature = feature;
627 mBinder = binder;
628 mPid = getCallingPid();
629 mUid = getCallingUid();
Robert Greenwalt3eeb6032009-12-21 18:24:07 -0800630 mCreateTime = System.currentTimeMillis();
Robert Greenwaltaffc3a12009-09-27 17:27:04 -0700631
Robert Greenwalt2034b912009-08-12 16:08:25 -0700632 try {
633 mBinder.linkToDeath(this, 0);
634 } catch (RemoteException e) {
635 binderDied();
636 }
637 }
638
639 void unlinkDeathRecipient() {
640 mBinder.unlinkToDeath(this, 0);
641 }
642
643 public void binderDied() {
Wink Savillee70c6f52010-12-03 12:01:38 -0800644 log("ConnectivityService FeatureUser binderDied(" +
Robert Greenwalt3eeb6032009-12-21 18:24:07 -0800645 mNetworkType + ", " + mFeature + ", " + mBinder + "), created " +
646 (System.currentTimeMillis() - mCreateTime) + " mSec ago");
Robert Greenwaltaffc3a12009-09-27 17:27:04 -0700647 stopUsingNetworkFeature(this, false);
Robert Greenwalt2034b912009-08-12 16:08:25 -0700648 }
649
Robert Greenwaltaffc3a12009-09-27 17:27:04 -0700650 public void expire() {
Wink Savillee70c6f52010-12-03 12:01:38 -0800651 log("ConnectivityService FeatureUser expire(" +
Robert Greenwalt3eeb6032009-12-21 18:24:07 -0800652 mNetworkType + ", " + mFeature + ", " + mBinder +"), created " +
653 (System.currentTimeMillis() - mCreateTime) + " mSec ago");
Robert Greenwaltaffc3a12009-09-27 17:27:04 -0700654 stopUsingNetworkFeature(this, false);
655 }
Robert Greenwalt3eeb6032009-12-21 18:24:07 -0800656
657 public String toString() {
658 return "FeatureUser("+mNetworkType+","+mFeature+","+mPid+","+mUid+"), created " +
659 (System.currentTimeMillis() - mCreateTime) + " mSec ago";
660 }
Robert Greenwalt2034b912009-08-12 16:08:25 -0700661 }
662
Robert Greenwaltaffc3a12009-09-27 17:27:04 -0700663 // javadoc from interface
Robert Greenwalt2034b912009-08-12 16:08:25 -0700664 public int startUsingNetworkFeature(int networkType, String feature,
665 IBinder binder) {
666 if (DBG) {
Wink Savillee70c6f52010-12-03 12:01:38 -0800667 log("startUsingNetworkFeature for net " + networkType + ": " + feature);
Robert Greenwalt2034b912009-08-12 16:08:25 -0700668 }
The Android Open Source Project28527d22009-03-03 19:31:44 -0800669 enforceChangePermission();
Robert Greenwaltec05b3c2009-10-30 14:17:42 -0700670 if (!ConnectivityManager.isNetworkTypeValid(networkType) ||
Robert Greenwalt34848c02011-03-25 13:09:25 -0700671 mNetConfigs[networkType] == null) {
Robert Greenwalt2034b912009-08-12 16:08:25 -0700672 return Phone.APN_REQUEST_FAILED;
The Android Open Source Project28527d22009-03-03 19:31:44 -0800673 }
Robert Greenwalt2034b912009-08-12 16:08:25 -0700674
Robert Greenwaltaffc3a12009-09-27 17:27:04 -0700675 FeatureUser f = new FeatureUser(networkType, feature, binder);
Robert Greenwalt2034b912009-08-12 16:08:25 -0700676
677 // TODO - move this into the MobileDataStateTracker
678 int usedNetworkType = networkType;
679 if(networkType == ConnectivityManager.TYPE_MOBILE) {
Wink Savillef6b76692011-02-24 17:58:51 -0800680 usedNetworkType = convertFeatureToNetworkType(feature);
681 if (usedNetworkType < 0) {
682 Slog.e(TAG, "Can't match any netTracker!");
683 usedNetworkType = networkType;
Robert Greenwalt2034b912009-08-12 16:08:25 -0700684 }
685 }
686 NetworkStateTracker network = mNetTrackers[usedNetworkType];
687 if (network != null) {
Robert Greenwalt5364d752010-12-15 13:26:33 -0800688 Integer currentPid = new Integer(getCallingPid());
Robert Greenwalt2034b912009-08-12 16:08:25 -0700689 if (usedNetworkType != networkType) {
Robert Greenwalt2034b912009-08-12 16:08:25 -0700690 NetworkStateTracker radio = mNetTrackers[networkType];
691 NetworkInfo ni = network.getNetworkInfo();
692
693 if (ni.isAvailable() == false) {
Wink Savillee70c6f52010-12-03 12:01:38 -0800694 if (DBG) log("special network not available");
Robert Greenwalt2cc87442010-12-29 14:35:21 -0800695 if (!TextUtils.equals(feature,Phone.FEATURE_ENABLE_DUN_ALWAYS)) {
696 return Phone.APN_TYPE_NOT_AVAILABLE;
697 } else {
698 // else make the attempt anyway - probably giving REQUEST_STARTED below
699 }
Robert Greenwalt2034b912009-08-12 16:08:25 -0700700 }
701
Robert Greenwaltaffc3a12009-09-27 17:27:04 -0700702 synchronized(this) {
703 mFeatureUsers.add(f);
704 if (!mNetRequestersPids[usedNetworkType].contains(currentPid)) {
705 // this gets used for per-pid dns when connected
706 mNetRequestersPids[usedNetworkType].add(currentPid);
707 }
Robert Greenwalt2034b912009-08-12 16:08:25 -0700708 }
Robert Greenwaltaffc3a12009-09-27 17:27:04 -0700709
Robert Greenwalt20f819c2011-05-03 19:02:44 -0700710 int restoreTimer = getRestoreDefaultNetworkDelay(usedNetworkType);
711
712 if (restoreTimer >= 0) {
713 mHandler.sendMessageDelayed(
714 mHandler.obtainMessage(EVENT_RESTORE_DEFAULT_NETWORK, f), restoreTimer);
715 }
Robert Greenwalt2034b912009-08-12 16:08:25 -0700716
Robert Greenwalta52c75a2009-08-19 20:19:33 -0700717 if ((ni.isConnectedOrConnecting() == true) &&
718 !network.isTeardownRequested()) {
Robert Greenwalt2034b912009-08-12 16:08:25 -0700719 if (ni.isConnected() == true) {
720 // add the pid-specific dns
Robert Greenwalt3afbead2010-07-23 15:46:26 -0700721 handleDnsConfigurationChange(networkType);
Wink Savillee70c6f52010-12-03 12:01:38 -0800722 if (DBG) log("special network already active");
Robert Greenwalt2034b912009-08-12 16:08:25 -0700723 return Phone.APN_ALREADY_ACTIVE;
724 }
Wink Savillee70c6f52010-12-03 12:01:38 -0800725 if (DBG) log("special network already connecting");
Robert Greenwalt2034b912009-08-12 16:08:25 -0700726 return Phone.APN_REQUEST_STARTED;
727 }
728
729 // check if the radio in play can make another contact
730 // assume if cannot for now
731
Wink Savillee70c6f52010-12-03 12:01:38 -0800732 if (DBG) log("reconnecting to special network");
Robert Greenwalt2034b912009-08-12 16:08:25 -0700733 network.reconnect();
734 return Phone.APN_REQUEST_STARTED;
735 } else {
Robert Greenwalt5364d752010-12-15 13:26:33 -0800736 // need to remember this unsupported request so we respond appropriately on stop
737 synchronized(this) {
738 mFeatureUsers.add(f);
739 if (!mNetRequestersPids[usedNetworkType].contains(currentPid)) {
740 // this gets used for per-pid dns when connected
741 mNetRequestersPids[usedNetworkType].add(currentPid);
742 }
743 }
Robert Greenwaltd391e892010-05-18 10:52:51 -0700744 return -1;
Robert Greenwalt2034b912009-08-12 16:08:25 -0700745 }
746 }
747 return Phone.APN_TYPE_NOT_AVAILABLE;
The Android Open Source Project28527d22009-03-03 19:31:44 -0800748 }
749
Robert Greenwaltaffc3a12009-09-27 17:27:04 -0700750 // javadoc from interface
The Android Open Source Project28527d22009-03-03 19:31:44 -0800751 public int stopUsingNetworkFeature(int networkType, String feature) {
Robert Greenwalt28f43012009-10-06 17:52:40 -0700752 enforceChangePermission();
753
Robert Greenwaltaffc3a12009-09-27 17:27:04 -0700754 int pid = getCallingPid();
755 int uid = getCallingUid();
756
757 FeatureUser u = null;
758 boolean found = false;
759
760 synchronized(this) {
761 for (int i = 0; i < mFeatureUsers.size() ; i++) {
762 u = (FeatureUser)mFeatureUsers.get(i);
763 if (uid == u.mUid && pid == u.mPid &&
764 networkType == u.mNetworkType &&
765 TextUtils.equals(feature, u.mFeature)) {
766 found = true;
767 break;
768 }
769 }
770 }
771 if (found && u != null) {
772 // stop regardless of how many other time this proc had called start
773 return stopUsingNetworkFeature(u, true);
774 } else {
775 // none found!
Wink Savillee70c6f52010-12-03 12:01:38 -0800776 if (DBG) log("ignoring stopUsingNetworkFeature - not a live request");
Robert Greenwaltaffc3a12009-09-27 17:27:04 -0700777 return 1;
778 }
Robert Greenwalt2034b912009-08-12 16:08:25 -0700779 }
780
Robert Greenwaltaffc3a12009-09-27 17:27:04 -0700781 private int stopUsingNetworkFeature(FeatureUser u, boolean ignoreDups) {
782 int networkType = u.mNetworkType;
783 String feature = u.mFeature;
784 int pid = u.mPid;
785 int uid = u.mUid;
786
787 NetworkStateTracker tracker = null;
788 boolean callTeardown = false; // used to carry our decision outside of sync block
789
Robert Greenwalt2034b912009-08-12 16:08:25 -0700790 if (DBG) {
Wink Savillee70c6f52010-12-03 12:01:38 -0800791 log("stopUsingNetworkFeature for net " + networkType +
Robert Greenwalt2034b912009-08-12 16:08:25 -0700792 ": " + feature);
793 }
Robert Greenwalt28f43012009-10-06 17:52:40 -0700794
The Android Open Source Project28527d22009-03-03 19:31:44 -0800795 if (!ConnectivityManager.isNetworkTypeValid(networkType)) {
796 return -1;
797 }
Robert Greenwalt2034b912009-08-12 16:08:25 -0700798
Robert Greenwaltaffc3a12009-09-27 17:27:04 -0700799 // need to link the mFeatureUsers list with the mNetRequestersPids state in this
800 // sync block
801 synchronized(this) {
802 // check if this process still has an outstanding start request
803 if (!mFeatureUsers.contains(u)) {
Wink Savillee70c6f52010-12-03 12:01:38 -0800804 if (DBG) log("ignoring - this process has no outstanding requests");
Robert Greenwalt2034b912009-08-12 16:08:25 -0700805 return 1;
806 }
Robert Greenwaltaffc3a12009-09-27 17:27:04 -0700807 u.unlinkDeathRecipient();
808 mFeatureUsers.remove(mFeatureUsers.indexOf(u));
809 // If we care about duplicate requests, check for that here.
810 //
811 // This is done to support the extension of a request - the app
812 // can request we start the network feature again and renew the
813 // auto-shutoff delay. Normal "stop" calls from the app though
814 // do not pay attention to duplicate requests - in effect the
815 // API does not refcount and a single stop will counter multiple starts.
816 if (ignoreDups == false) {
817 for (int i = 0; i < mFeatureUsers.size() ; i++) {
818 FeatureUser x = (FeatureUser)mFeatureUsers.get(i);
819 if (x.mUid == u.mUid && x.mPid == u.mPid &&
820 x.mNetworkType == u.mNetworkType &&
821 TextUtils.equals(x.mFeature, u.mFeature)) {
Wink Savillee70c6f52010-12-03 12:01:38 -0800822 if (DBG) log("ignoring stopUsingNetworkFeature as dup is found");
Robert Greenwaltaffc3a12009-09-27 17:27:04 -0700823 return 1;
824 }
825 }
Robert Greenwalt2034b912009-08-12 16:08:25 -0700826 }
Robert Greenwaltaffc3a12009-09-27 17:27:04 -0700827
828 // TODO - move to MobileDataStateTracker
829 int usedNetworkType = networkType;
830 if (networkType == ConnectivityManager.TYPE_MOBILE) {
Wink Savillef6b76692011-02-24 17:58:51 -0800831 usedNetworkType = convertFeatureToNetworkType(feature);
832 if (usedNetworkType < 0) {
833 usedNetworkType = networkType;
Robert Greenwaltaffc3a12009-09-27 17:27:04 -0700834 }
835 }
836 tracker = mNetTrackers[usedNetworkType];
Robert Greenwaltec05b3c2009-10-30 14:17:42 -0700837 if (tracker == null) {
Wink Savillee70c6f52010-12-03 12:01:38 -0800838 if (DBG) log("ignoring - no known tracker for net type " + usedNetworkType);
Robert Greenwaltec05b3c2009-10-30 14:17:42 -0700839 return -1;
840 }
841 if (usedNetworkType != networkType) {
Robert Greenwaltaffc3a12009-09-27 17:27:04 -0700842 Integer currentPid = new Integer(pid);
Robert Greenwaltaffc3a12009-09-27 17:27:04 -0700843 mNetRequestersPids[usedNetworkType].remove(currentPid);
Robert Greenwalt0ca68a02009-12-17 14:54:59 -0800844 reassessPidDns(pid, true);
Robert Greenwaltaffc3a12009-09-27 17:27:04 -0700845 if (mNetRequestersPids[usedNetworkType].size() != 0) {
Wink Savillee70c6f52010-12-03 12:01:38 -0800846 if (DBG) log("not tearing down special network - " +
Robert Greenwaltaffc3a12009-09-27 17:27:04 -0700847 "others still using it");
848 return 1;
849 }
850 callTeardown = true;
Robert Greenwalt9f3be4c2011-01-10 11:58:31 -0800851 } else {
852 if (DBG) log("not a known feature - dropping");
Robert Greenwaltaffc3a12009-09-27 17:27:04 -0700853 }
854 }
Wink Savillee70c6f52010-12-03 12:01:38 -0800855 if (DBG) log("Doing network teardown");
Robert Greenwaltaffc3a12009-09-27 17:27:04 -0700856 if (callTeardown) {
857 tracker.teardown();
Robert Greenwalt2034b912009-08-12 16:08:25 -0700858 return 1;
859 } else {
Robert Greenwaltd391e892010-05-18 10:52:51 -0700860 return -1;
Robert Greenwalt2034b912009-08-12 16:08:25 -0700861 }
The Android Open Source Project28527d22009-03-03 19:31:44 -0800862 }
863
864 /**
Robert Greenwalt7fe44cb2010-08-27 09:24:29 -0700865 * @deprecated use requestRouteToHostAddress instead
866 *
The Android Open Source Project28527d22009-03-03 19:31:44 -0800867 * Ensure that a network route exists to deliver traffic to the specified
868 * host via the specified network interface.
Robert Greenwalt0659da32009-07-16 17:21:39 -0700869 * @param networkType the type of the network over which traffic to the
870 * specified host is to be routed
871 * @param hostAddress the IP address of the host to which the route is
872 * desired
The Android Open Source Project28527d22009-03-03 19:31:44 -0800873 * @return {@code true} on success, {@code false} on failure
874 */
875 public boolean requestRouteToHost(int networkType, int hostAddress) {
Robert Greenwalt7fe44cb2010-08-27 09:24:29 -0700876 InetAddress inetAddress = NetworkUtils.intToInetAddress(hostAddress);
877
878 if (inetAddress == null) {
879 return false;
880 }
881
882 return requestRouteToHostAddress(networkType, inetAddress.getAddress());
883 }
884
885 /**
886 * Ensure that a network route exists to deliver traffic to the specified
887 * host via the specified network interface.
888 * @param networkType the type of the network over which traffic to the
889 * specified host is to be routed
890 * @param hostAddress the IP address of the host to which the route is
891 * desired
892 * @return {@code true} on success, {@code false} on failure
893 */
894 public boolean requestRouteToHostAddress(int networkType, byte[] hostAddress) {
The Android Open Source Project28527d22009-03-03 19:31:44 -0800895 enforceChangePermission();
896 if (!ConnectivityManager.isNetworkTypeValid(networkType)) {
897 return false;
898 }
899 NetworkStateTracker tracker = mNetTrackers[networkType];
Robert Greenwalt4666ed02009-09-10 15:06:20 -0700900
Robert Greenwaltec05b3c2009-10-30 14:17:42 -0700901 if (tracker == null || !tracker.getNetworkInfo().isConnected() ||
902 tracker.isTeardownRequested()) {
Robert Greenwalt4666ed02009-09-10 15:06:20 -0700903 if (DBG) {
Wink Savillee70c6f52010-12-03 12:01:38 -0800904 log("requestRouteToHostAddress on down network " +
Robert Greenwalt7fe44cb2010-08-27 09:24:29 -0700905 "(" + networkType + ") - dropped");
Robert Greenwalt4666ed02009-09-10 15:06:20 -0700906 }
907 return false;
The Android Open Source Project28527d22009-03-03 19:31:44 -0800908 }
Robert Greenwalta7dfbd32010-06-15 15:43:39 -0700909 try {
Robert Greenwalt7fe44cb2010-08-27 09:24:29 -0700910 InetAddress addr = InetAddress.getByAddress(hostAddress);
Robert Greenwaltbd492212011-05-06 17:10:53 -0700911 return addHostRoute(tracker, addr, 0);
Robert Greenwalta7dfbd32010-06-15 15:43:39 -0700912 } catch (UnknownHostException e) {}
913 return false;
Irfan Sheriff7f132d92010-06-09 15:39:36 -0700914 }
915
916 /**
917 * Ensure that a network route exists to deliver traffic to the specified
918 * host via the mobile data network.
919 * @param hostAddress the IP address of the host to which the route is desired,
920 * in network byte order.
Robert Greenwalta7dfbd32010-06-15 15:43:39 -0700921 * TODO - deprecate
Irfan Sheriff7f132d92010-06-09 15:39:36 -0700922 * @return {@code true} on success, {@code false} on failure
923 */
Robert Greenwaltbd492212011-05-06 17:10:53 -0700924 private boolean addHostRoute(NetworkStateTracker nt, InetAddress hostAddress, int cycleCount) {
Robert Greenwaltbd492212011-05-06 17:10:53 -0700925 LinkProperties lp = nt.getLinkProperties();
926 if ((lp == null) || (hostAddress == null)) return false;
Irfan Sheriff7f132d92010-06-09 15:39:36 -0700927
Robert Greenwaltbd492212011-05-06 17:10:53 -0700928 String interfaceName = lp.getInterfaceName();
Irfan Sheriff7f132d92010-06-09 15:39:36 -0700929 if (DBG) {
Robert Greenwaltbd492212011-05-06 17:10:53 -0700930 log("Requested host route to " + hostAddress + "(" + interfaceName + "), cycleCount=" +
931 cycleCount);
Irfan Sheriff7f132d92010-06-09 15:39:36 -0700932 }
Robert Greenwaltbd492212011-05-06 17:10:53 -0700933 if (interfaceName == null) {
Wink Savillee70c6f52010-12-03 12:01:38 -0800934 if (DBG) loge("addHostRoute failed due to null interface name");
Irfan Sheriff7f132d92010-06-09 15:39:36 -0700935 return false;
936 }
Robert Greenwaltbd492212011-05-06 17:10:53 -0700937
938 RouteInfo bestRoute = RouteInfo.selectBestRoute(lp.getRoutes(), hostAddress);
Robert Greenwalt355205c2011-05-10 15:05:02 -0700939 InetAddress gatewayAddress = null;
Robert Greenwaltbd492212011-05-06 17:10:53 -0700940 if (bestRoute != null) {
Robert Greenwalt355205c2011-05-10 15:05:02 -0700941 gatewayAddress = bestRoute.getGateway();
Robert Greenwaltbd492212011-05-06 17:10:53 -0700942 // if the best route is ourself, don't relf-reference, just add the host route
Robert Greenwalt355205c2011-05-10 15:05:02 -0700943 if (hostAddress.equals(gatewayAddress)) gatewayAddress = null;
Robert Greenwaltbd492212011-05-06 17:10:53 -0700944 }
Robert Greenwalt355205c2011-05-10 15:05:02 -0700945 if (gatewayAddress != null) {
Robert Greenwaltbd492212011-05-06 17:10:53 -0700946 if (cycleCount > MAX_HOSTROUTE_CYCLE_COUNT) {
947 loge("Error adding hostroute - too much recursion");
948 return false;
949 }
Robert Greenwalt355205c2011-05-10 15:05:02 -0700950 if (!addHostRoute(nt, gatewayAddress, cycleCount+1)) return false;
Robert Greenwaltbd492212011-05-06 17:10:53 -0700951 }
Robert Greenwalt355205c2011-05-10 15:05:02 -0700952
953 RouteInfo route = RouteInfo.makeHostRoute(hostAddress, gatewayAddress);
954
955 try {
956 mNetd.addRoute(interfaceName, route);
957 return true;
958 } catch (Exception ex) {
959 return false;
960 }
Robert Greenwaltbd492212011-05-06 17:10:53 -0700961 }
962
963 // TODO support the removal of single host routes. Keep a ref count of them so we
964 // aren't over-zealous
965 private boolean removeHostRoute(NetworkStateTracker nt, InetAddress hostAddress) {
966 return false;
The Android Open Source Project28527d22009-03-03 19:31:44 -0800967 }
968
969 /**
970 * @see ConnectivityManager#getBackgroundDataSetting()
971 */
972 public boolean getBackgroundDataSetting() {
Robert Greenwaltd62c7002010-12-29 16:15:02 -0800973 return mBackgroundDataEnabled.get();
The Android Open Source Project28527d22009-03-03 19:31:44 -0800974 }
Robert Greenwalt0659da32009-07-16 17:21:39 -0700975
The Android Open Source Project28527d22009-03-03 19:31:44 -0800976 /**
977 * @see ConnectivityManager#setBackgroundDataSetting(boolean)
978 */
979 public void setBackgroundDataSetting(boolean allowBackgroundDataUsage) {
980 mContext.enforceCallingOrSelfPermission(
981 android.Manifest.permission.CHANGE_BACKGROUND_DATA_SETTING,
982 "ConnectivityService");
Robert Greenwalt0659da32009-07-16 17:21:39 -0700983
Robert Greenwaltd62c7002010-12-29 16:15:02 -0800984 mBackgroundDataEnabled.set(allowBackgroundDataUsage);
985
Robert Greenwalt6a2db8a2010-09-23 10:05:56 -0700986 mHandler.sendMessage(mHandler.obtainMessage(EVENT_SET_BACKGROUND_DATA,
987 (allowBackgroundDataUsage ? ENABLED : DISABLED), 0));
988 }
The Android Open Source Project28527d22009-03-03 19:31:44 -0800989
Robert Greenwalt6a2db8a2010-09-23 10:05:56 -0700990 private void handleSetBackgroundData(boolean enabled) {
Robert Greenwalt0ffdef12011-02-25 13:44:09 -0800991 Settings.Secure.putInt(mContext.getContentResolver(),
992 Settings.Secure.BACKGROUND_DATA, enabled ? 1 : 0);
993 Intent broadcast = new Intent(
994 ConnectivityManager.ACTION_BACKGROUND_DATA_SETTING_CHANGED);
995 mContext.sendBroadcast(broadcast);
Robert Greenwalt0659da32009-07-16 17:21:39 -0700996 }
997
Robert Greenwalt1b21f6c2010-02-23 18:58:05 -0800998 /**
999 * @see ConnectivityManager#getMobileDataEnabled()
1000 */
1001 public boolean getMobileDataEnabled() {
Wink Savilleb9024c62010-12-07 10:31:02 -08001002 // TODO: This detail should probably be in DataConnectionTracker's
1003 // which is where we store the value and maybe make this
1004 // asynchronous.
Robert Greenwalt1b21f6c2010-02-23 18:58:05 -08001005 enforceAccessPermission();
1006 boolean retVal = Settings.Secure.getInt(mContext.getContentResolver(),
1007 Settings.Secure.MOBILE_DATA, 1) == 1;
Wink Savillee70c6f52010-12-03 12:01:38 -08001008 if (DBG) log("getMobileDataEnabled returning " + retVal);
Robert Greenwalt1b21f6c2010-02-23 18:58:05 -08001009 return retVal;
1010 }
1011
Robert Greenwalt34848c02011-03-25 13:09:25 -07001012 public void setDataDependency(int networkType, boolean met) {
1013 enforceChangePermission();
1014 if (DBG) {
1015 log("setDataDependency(" + networkType + ", " + met + ")");
1016 }
1017 mHandler.sendMessage(mHandler.obtainMessage(EVENT_SET_DEPENDENCY_MET,
1018 (met ? ENABLED : DISABLED), networkType));
1019 }
1020
1021 private void handleSetDependencyMet(int networkType, boolean met) {
1022 if (mNetTrackers[networkType] != null) {
1023 if (DBG) {
1024 log("handleSetDependencyMet(" + networkType + ", " + met + ")");
1025 }
1026 mNetTrackers[networkType].setDependencyMet(met);
1027 }
1028 }
1029
Robert Greenwalt1b21f6c2010-02-23 18:58:05 -08001030 /**
1031 * @see ConnectivityManager#setMobileDataEnabled(boolean)
1032 */
Robert Greenwalt6a2db8a2010-09-23 10:05:56 -07001033 public void setMobileDataEnabled(boolean enabled) {
Robert Greenwalt1b21f6c2010-02-23 18:58:05 -08001034 enforceChangePermission();
Wink Savillee70c6f52010-12-03 12:01:38 -08001035 if (DBG) log("setMobileDataEnabled(" + enabled + ")");
Robert Greenwalt1b21f6c2010-02-23 18:58:05 -08001036
Robert Greenwalt6a2db8a2010-09-23 10:05:56 -07001037 mHandler.sendMessage(mHandler.obtainMessage(EVENT_SET_MOBILE_DATA,
Robert Greenwalt34848c02011-03-25 13:09:25 -07001038 (enabled ? ENABLED : DISABLED), 0));
Robert Greenwalt6a2db8a2010-09-23 10:05:56 -07001039 }
1040
1041 private void handleSetMobileData(boolean enabled) {
Wink Savilleb9024c62010-12-07 10:31:02 -08001042 if (mNetTrackers[ConnectivityManager.TYPE_MOBILE] != null) {
1043 if (DBG) {
1044 Slog.d(TAG, mNetTrackers[ConnectivityManager.TYPE_MOBILE].toString() + enabled);
Robert Greenwalt1b21f6c2010-02-23 18:58:05 -08001045 }
Wink Savilleb9024c62010-12-07 10:31:02 -08001046 mNetTrackers[ConnectivityManager.TYPE_MOBILE].setDataEnable(enabled);
Robert Greenwalt1b21f6c2010-02-23 18:58:05 -08001047 }
1048 }
1049
The Android Open Source Project28527d22009-03-03 19:31:44 -08001050 private void enforceAccessPermission() {
Robert Greenwalt0659da32009-07-16 17:21:39 -07001051 mContext.enforceCallingOrSelfPermission(
1052 android.Manifest.permission.ACCESS_NETWORK_STATE,
1053 "ConnectivityService");
The Android Open Source Project28527d22009-03-03 19:31:44 -08001054 }
1055
1056 private void enforceChangePermission() {
Robert Greenwalt0659da32009-07-16 17:21:39 -07001057 mContext.enforceCallingOrSelfPermission(
1058 android.Manifest.permission.CHANGE_NETWORK_STATE,
1059 "ConnectivityService");
The Android Open Source Project28527d22009-03-03 19:31:44 -08001060 }
1061
Robert Greenwalt0c4828c2010-01-26 11:40:34 -08001062 // TODO Make this a special check when it goes public
1063 private void enforceTetherChangePermission() {
1064 mContext.enforceCallingOrSelfPermission(
1065 android.Manifest.permission.CHANGE_NETWORK_STATE,
1066 "ConnectivityService");
1067 }
1068
Robert Greenwalt8e87f122010-02-11 18:18:40 -08001069 private void enforceTetherAccessPermission() {
1070 mContext.enforceCallingOrSelfPermission(
1071 android.Manifest.permission.ACCESS_NETWORK_STATE,
1072 "ConnectivityService");
1073 }
1074
Robert Greenwalt93dc1042010-06-15 12:19:37 -07001075 private void enforceConnectivityInternalPermission() {
1076 mContext.enforceCallingOrSelfPermission(
1077 android.Manifest.permission.CONNECTIVITY_INTERNAL,
1078 "ConnectivityService");
1079 }
1080
The Android Open Source Project28527d22009-03-03 19:31:44 -08001081 /**
Robert Greenwalt0659da32009-07-16 17:21:39 -07001082 * Handle a {@code DISCONNECTED} event. If this pertains to the non-active
1083 * network, we ignore it. If it is for the active network, we send out a
1084 * broadcast. But first, we check whether it might be possible to connect
1085 * to a different network.
The Android Open Source Project28527d22009-03-03 19:31:44 -08001086 * @param info the {@code NetworkInfo} for the network
1087 */
1088 private void handleDisconnect(NetworkInfo info) {
1089
Robert Greenwalt2034b912009-08-12 16:08:25 -07001090 int prevNetType = info.getType();
The Android Open Source Project28527d22009-03-03 19:31:44 -08001091
Robert Greenwalt2034b912009-08-12 16:08:25 -07001092 mNetTrackers[prevNetType].setTeardownRequested(false);
The Android Open Source Project28527d22009-03-03 19:31:44 -08001093 /*
1094 * If the disconnected network is not the active one, then don't report
1095 * this as a loss of connectivity. What probably happened is that we're
1096 * getting the disconnect for a network that we explicitly disabled
1097 * in accordance with network preference policies.
1098 */
Robert Greenwalt34848c02011-03-25 13:09:25 -07001099 if (!mNetConfigs[prevNetType].isDefault()) {
Robert Greenwalt2034b912009-08-12 16:08:25 -07001100 List pids = mNetRequestersPids[prevNetType];
1101 for (int i = 0; i<pids.size(); i++) {
1102 Integer pid = (Integer)pids.get(i);
1103 // will remove them because the net's no longer connected
1104 // need to do this now as only now do we know the pids and
1105 // can properly null things that are no longer referenced.
1106 reassessPidDns(pid.intValue(), false);
The Android Open Source Project28527d22009-03-03 19:31:44 -08001107 }
The Android Open Source Project28527d22009-03-03 19:31:44 -08001108 }
1109
The Android Open Source Project28527d22009-03-03 19:31:44 -08001110 Intent intent = new Intent(ConnectivityManager.CONNECTIVITY_ACTION);
1111 intent.putExtra(ConnectivityManager.EXTRA_NETWORK_INFO, info);
1112 if (info.isFailover()) {
1113 intent.putExtra(ConnectivityManager.EXTRA_IS_FAILOVER, true);
1114 info.setFailover(false);
1115 }
1116 if (info.getReason() != null) {
1117 intent.putExtra(ConnectivityManager.EXTRA_REASON, info.getReason());
1118 }
1119 if (info.getExtraInfo() != null) {
Robert Greenwalt0659da32009-07-16 17:21:39 -07001120 intent.putExtra(ConnectivityManager.EXTRA_EXTRA_INFO,
1121 info.getExtraInfo());
The Android Open Source Project28527d22009-03-03 19:31:44 -08001122 }
Robert Greenwalt2034b912009-08-12 16:08:25 -07001123
Robert Greenwalt34848c02011-03-25 13:09:25 -07001124 if (mNetConfigs[prevNetType].isDefault()) {
Robert Greenwalt4b7c55e2011-01-11 13:56:33 -08001125 tryFailover(prevNetType);
1126 if (mActiveDefaultNetwork != -1) {
1127 NetworkInfo switchTo = mNetTrackers[mActiveDefaultNetwork].getNetworkInfo();
Robert Greenwalt3cc68d32010-01-25 17:54:29 -08001128 intent.putExtra(ConnectivityManager.EXTRA_OTHER_NETWORK_INFO, switchTo);
1129 } else {
Robert Greenwaltb2a9e492010-09-20 18:01:43 -07001130 mDefaultInetConditionPublished = 0; // we're not connected anymore
Robert Greenwalt3cc68d32010-01-25 17:54:29 -08001131 intent.putExtra(ConnectivityManager.EXTRA_NO_CONNECTIVITY, true);
1132 }
Robert Greenwaltf55ced92010-01-20 19:29:41 -08001133 }
Robert Greenwaltb2a9e492010-09-20 18:01:43 -07001134 intent.putExtra(ConnectivityManager.EXTRA_INET_CONDITION, mDefaultInetConditionPublished);
Robert Greenwaltf55ced92010-01-20 19:29:41 -08001135 // do this before we broadcast the change
Robert Greenwalt3afbead2010-07-23 15:46:26 -07001136 handleConnectivityChange(prevNetType);
Robert Greenwaltf55ced92010-01-20 19:29:41 -08001137
1138 sendStickyBroadcast(intent);
1139 /*
1140 * If the failover network is already connected, then immediately send
1141 * out a followup broadcast indicating successful failover
1142 */
Robert Greenwalt4b7c55e2011-01-11 13:56:33 -08001143 if (mActiveDefaultNetwork != -1) {
1144 sendConnectedBroadcast(mNetTrackers[mActiveDefaultNetwork].getNetworkInfo());
Robert Greenwaltf55ced92010-01-20 19:29:41 -08001145 }
1146 }
1147
Robert Greenwalt4b7c55e2011-01-11 13:56:33 -08001148 private void tryFailover(int prevNetType) {
Robert Greenwalt2034b912009-08-12 16:08:25 -07001149 /*
Robert Greenwalt92564852011-01-06 15:41:07 -08001150 * If this is a default network, check if other defaults are available.
1151 * Try to reconnect on all available and let them hash it out when
1152 * more than one connects.
Robert Greenwalt2034b912009-08-12 16:08:25 -07001153 */
Robert Greenwalt34848c02011-03-25 13:09:25 -07001154 if (mNetConfigs[prevNetType].isDefault()) {
Robert Greenwalt2034b912009-08-12 16:08:25 -07001155 if (mActiveDefaultNetwork == prevNetType) {
1156 mActiveDefaultNetwork = -1;
1157 }
1158
Robert Greenwalt4b7c55e2011-01-11 13:56:33 -08001159 // don't signal a reconnect for anything lower or equal priority than our
1160 // current connected default
1161 // TODO - don't filter by priority now - nice optimization but risky
1162// int currentPriority = -1;
1163// if (mActiveDefaultNetwork != -1) {
Robert Greenwalt34848c02011-03-25 13:09:25 -07001164// currentPriority = mNetConfigs[mActiveDefaultNetwork].mPriority;
Robert Greenwalt4b7c55e2011-01-11 13:56:33 -08001165// }
Robert Greenwaltf55ced92010-01-20 19:29:41 -08001166 for (int checkType=0; checkType <= ConnectivityManager.MAX_NETWORK_TYPE; checkType++) {
Robert Greenwaltec05b3c2009-10-30 14:17:42 -07001167 if (checkType == prevNetType) continue;
Robert Greenwalt34848c02011-03-25 13:09:25 -07001168 if (mNetConfigs[checkType] == null) continue;
1169 if (!mNetConfigs[checkType].isDefault()) continue;
Wink Saville72a95b92011-01-26 15:43:49 -08001170
1171// Enabling the isAvailable() optimization caused mobile to not get
1172// selected if it was in the middle of error handling. Specifically
1173// a moble connection that took 30 seconds to complete the DEACTIVATE_DATA_CALL
1174// would not be available and we wouldn't get connected to anything.
1175// So removing the isAvailable() optimization below for now. TODO: This
1176// optimization should work and we need to investigate why it doesn't work.
1177// This could be related to how DEACTIVATE_DATA_CALL is reporting its
1178// complete before it is really complete.
1179// if (!mNetTrackers[checkType].isAvailable()) continue;
1180
Robert Greenwalt34848c02011-03-25 13:09:25 -07001181// if (currentPriority >= mNetConfigs[checkType].mPriority) continue;
Robert Greenwalt2034b912009-08-12 16:08:25 -07001182
Robert Greenwalt4b7c55e2011-01-11 13:56:33 -08001183 NetworkStateTracker checkTracker = mNetTrackers[checkType];
1184 NetworkInfo checkInfo = checkTracker.getNetworkInfo();
1185 if (!checkInfo.isConnectedOrConnecting() || checkTracker.isTeardownRequested()) {
1186 checkInfo.setFailover(true);
1187 checkTracker.reconnect();
Robert Greenwalt2034b912009-08-12 16:08:25 -07001188 }
Robert Greenwalt4b7c55e2011-01-11 13:56:33 -08001189 if (DBG) log("Attempting to switch to " + checkInfo.getTypeName());
Robert Greenwalt2034b912009-08-12 16:08:25 -07001190 }
The Android Open Source Project28527d22009-03-03 19:31:44 -08001191 }
The Android Open Source Project28527d22009-03-03 19:31:44 -08001192 }
1193
1194 private void sendConnectedBroadcast(NetworkInfo info) {
Robert Greenwaltd3401f92010-09-15 17:36:33 -07001195 sendGeneralBroadcast(info, ConnectivityManager.CONNECTIVITY_ACTION);
1196 }
1197
1198 private void sendInetConditionBroadcast(NetworkInfo info) {
1199 sendGeneralBroadcast(info, ConnectivityManager.INET_CONDITION_ACTION);
1200 }
1201
1202 private void sendGeneralBroadcast(NetworkInfo info, String bcastType) {
1203 Intent intent = new Intent(bcastType);
The Android Open Source Project28527d22009-03-03 19:31:44 -08001204 intent.putExtra(ConnectivityManager.EXTRA_NETWORK_INFO, info);
1205 if (info.isFailover()) {
1206 intent.putExtra(ConnectivityManager.EXTRA_IS_FAILOVER, true);
1207 info.setFailover(false);
1208 }
1209 if (info.getReason() != null) {
1210 intent.putExtra(ConnectivityManager.EXTRA_REASON, info.getReason());
1211 }
1212 if (info.getExtraInfo() != null) {
Robert Greenwalt0659da32009-07-16 17:21:39 -07001213 intent.putExtra(ConnectivityManager.EXTRA_EXTRA_INFO,
1214 info.getExtraInfo());
The Android Open Source Project28527d22009-03-03 19:31:44 -08001215 }
Robert Greenwalt986c7412010-09-08 15:24:47 -07001216 intent.putExtra(ConnectivityManager.EXTRA_INET_CONDITION, mDefaultInetConditionPublished);
Mike Lockwoodfde2b762009-08-14 14:18:49 -04001217 sendStickyBroadcast(intent);
The Android Open Source Project28527d22009-03-03 19:31:44 -08001218 }
1219
1220 /**
1221 * Called when an attempt to fail over to another network has failed.
1222 * @param info the {@link NetworkInfo} for the failed network
1223 */
1224 private void handleConnectionFailure(NetworkInfo info) {
1225 mNetTrackers[info.getType()].setTeardownRequested(false);
The Android Open Source Project28527d22009-03-03 19:31:44 -08001226
Robert Greenwalt2034b912009-08-12 16:08:25 -07001227 String reason = info.getReason();
1228 String extraInfo = info.getExtraInfo();
Robert Greenwalt0659da32009-07-16 17:21:39 -07001229
Robert Greenwalte981bc52010-10-08 16:35:52 -07001230 String reasonText;
1231 if (reason == null) {
1232 reasonText = ".";
1233 } else {
1234 reasonText = " (" + reason + ").";
The Android Open Source Project28527d22009-03-03 19:31:44 -08001235 }
Wink Savillee70c6f52010-12-03 12:01:38 -08001236 loge("Attempt to connect to " + info.getTypeName() + " failed" + reasonText);
Robert Greenwalt2034b912009-08-12 16:08:25 -07001237
1238 Intent intent = new Intent(ConnectivityManager.CONNECTIVITY_ACTION);
1239 intent.putExtra(ConnectivityManager.EXTRA_NETWORK_INFO, info);
1240 if (getActiveNetworkInfo() == null) {
1241 intent.putExtra(ConnectivityManager.EXTRA_NO_CONNECTIVITY, true);
1242 }
1243 if (reason != null) {
1244 intent.putExtra(ConnectivityManager.EXTRA_REASON, reason);
1245 }
1246 if (extraInfo != null) {
1247 intent.putExtra(ConnectivityManager.EXTRA_EXTRA_INFO, extraInfo);
1248 }
1249 if (info.isFailover()) {
1250 intent.putExtra(ConnectivityManager.EXTRA_IS_FAILOVER, true);
1251 info.setFailover(false);
1252 }
Robert Greenwaltf55ced92010-01-20 19:29:41 -08001253
Robert Greenwalt34848c02011-03-25 13:09:25 -07001254 if (mNetConfigs[info.getType()].isDefault()) {
Robert Greenwalt4b7c55e2011-01-11 13:56:33 -08001255 tryFailover(info.getType());
1256 if (mActiveDefaultNetwork != -1) {
1257 NetworkInfo switchTo = mNetTrackers[mActiveDefaultNetwork].getNetworkInfo();
Robert Greenwalt3cc68d32010-01-25 17:54:29 -08001258 intent.putExtra(ConnectivityManager.EXTRA_OTHER_NETWORK_INFO, switchTo);
1259 } else {
Robert Greenwaltb2a9e492010-09-20 18:01:43 -07001260 mDefaultInetConditionPublished = 0;
Robert Greenwalt3cc68d32010-01-25 17:54:29 -08001261 intent.putExtra(ConnectivityManager.EXTRA_NO_CONNECTIVITY, true);
1262 }
Robert Greenwaltf55ced92010-01-20 19:29:41 -08001263 }
Robert Greenwalt3cc68d32010-01-25 17:54:29 -08001264
Robert Greenwaltb2a9e492010-09-20 18:01:43 -07001265 intent.putExtra(ConnectivityManager.EXTRA_INET_CONDITION, mDefaultInetConditionPublished);
Mike Lockwoodfde2b762009-08-14 14:18:49 -04001266 sendStickyBroadcast(intent);
Robert Greenwaltf55ced92010-01-20 19:29:41 -08001267 /*
1268 * If the failover network is already connected, then immediately send
1269 * out a followup broadcast indicating successful failover
1270 */
Robert Greenwalt4b7c55e2011-01-11 13:56:33 -08001271 if (mActiveDefaultNetwork != -1) {
1272 sendConnectedBroadcast(mNetTrackers[mActiveDefaultNetwork].getNetworkInfo());
Robert Greenwaltf55ced92010-01-20 19:29:41 -08001273 }
Mike Lockwoodfde2b762009-08-14 14:18:49 -04001274 }
1275
1276 private void sendStickyBroadcast(Intent intent) {
1277 synchronized(this) {
Dianne Hackborna417ff82009-12-08 19:45:14 -08001278 if (!mSystemReady) {
1279 mInitialBroadcast = new Intent(intent);
Mike Lockwoodfde2b762009-08-14 14:18:49 -04001280 }
Dianne Hackborna417ff82009-12-08 19:45:14 -08001281 intent.addFlags(Intent.FLAG_RECEIVER_REGISTERED_ONLY_BEFORE_BOOT);
1282 mContext.sendStickyBroadcast(intent);
Mike Lockwoodfde2b762009-08-14 14:18:49 -04001283 }
1284 }
1285
1286 void systemReady() {
Robert Greenwalt355205c2011-05-10 15:05:02 -07001287 IBinder b = ServiceManager.getService(Context.NETWORKMANAGEMENT_SERVICE);
1288 mNetd = INetworkManagementService.Stub.asInterface(b);
1289
Mike Lockwoodfde2b762009-08-14 14:18:49 -04001290 synchronized(this) {
1291 mSystemReady = true;
Dianne Hackborna417ff82009-12-08 19:45:14 -08001292 if (mInitialBroadcast != null) {
1293 mContext.sendStickyBroadcast(mInitialBroadcast);
1294 mInitialBroadcast = null;
Mike Lockwoodfde2b762009-08-14 14:18:49 -04001295 }
1296 }
Robert Greenwaltc3c5f862010-10-11 16:00:27 -07001297 // load the global proxy at startup
1298 mHandler.sendMessage(mHandler.obtainMessage(EVENT_APPLY_GLOBAL_HTTP_PROXY));
The Android Open Source Project28527d22009-03-03 19:31:44 -08001299 }
1300
1301 private void handleConnect(NetworkInfo info) {
Robert Greenwalt2034b912009-08-12 16:08:25 -07001302 int type = info.getType();
The Android Open Source Project28527d22009-03-03 19:31:44 -08001303
1304 // snapshot isFailover, because sendConnectedBroadcast() resets it
1305 boolean isFailover = info.isFailover();
Robert Greenwalt2034b912009-08-12 16:08:25 -07001306 NetworkStateTracker thisNet = mNetTrackers[type];
The Android Open Source Project28527d22009-03-03 19:31:44 -08001307
Robert Greenwalt2034b912009-08-12 16:08:25 -07001308 // if this is a default net and other default is running
1309 // kill the one not preferred
Robert Greenwalt34848c02011-03-25 13:09:25 -07001310 if (mNetConfigs[type].isDefault()) {
Robert Greenwalt2034b912009-08-12 16:08:25 -07001311 if (mActiveDefaultNetwork != -1 && mActiveDefaultNetwork != type) {
1312 if ((type != mNetworkPreference &&
Wink Savillef2a62832011-04-07 14:23:45 -07001313 mNetConfigs[mActiveDefaultNetwork].priority >
1314 mNetConfigs[type].priority) ||
Robert Greenwalt2034b912009-08-12 16:08:25 -07001315 mNetworkPreference == mActiveDefaultNetwork) {
1316 // don't accept this one
Wink Savillee70c6f52010-12-03 12:01:38 -08001317 if (DBG) {
1318 log("Not broadcasting CONNECT_ACTION " +
Robert Greenwalt2034b912009-08-12 16:08:25 -07001319 "to torn down network " + info.getTypeName());
Wink Savillee70c6f52010-12-03 12:01:38 -08001320 }
Robert Greenwalt2034b912009-08-12 16:08:25 -07001321 teardown(thisNet);
1322 return;
1323 } else {
1324 // tear down the other
1325 NetworkStateTracker otherNet =
1326 mNetTrackers[mActiveDefaultNetwork];
Wink Savillee70c6f52010-12-03 12:01:38 -08001327 if (DBG) {
1328 log("Policy requires " + otherNet.getNetworkInfo().getTypeName() +
Robert Greenwalt2034b912009-08-12 16:08:25 -07001329 " teardown");
Wink Savillee70c6f52010-12-03 12:01:38 -08001330 }
Robert Greenwalt2034b912009-08-12 16:08:25 -07001331 if (!teardown(otherNet)) {
Wink Savillee70c6f52010-12-03 12:01:38 -08001332 loge("Network declined teardown request");
Robert Greenwalt99910172011-03-29 11:36:28 -07001333 teardown(thisNet);
Robert Greenwalt2034b912009-08-12 16:08:25 -07001334 return;
1335 }
Robert Greenwalt93dc1042010-06-15 12:19:37 -07001336 }
1337 }
1338 synchronized (ConnectivityService.this) {
1339 // have a new default network, release the transition wakelock in a second
1340 // if it's held. The second pause is to allow apps to reconnect over the
1341 // new network
1342 if (mNetTransitionWakeLock.isHeld()) {
1343 mHandler.sendMessageDelayed(mHandler.obtainMessage(
Robert Greenwaltccb36f92010-09-24 14:32:21 -07001344 EVENT_CLEAR_NET_TRANSITION_WAKELOCK,
Robert Greenwalt93dc1042010-06-15 12:19:37 -07001345 mNetTransitionWakeLockSerialNumber, 0),
1346 1000);
Robert Greenwalt2034b912009-08-12 16:08:25 -07001347 }
1348 }
1349 mActiveDefaultNetwork = type;
Robert Greenwalt986c7412010-09-08 15:24:47 -07001350 // this will cause us to come up initially as unconnected and switching
1351 // to connected after our normal pause unless somebody reports us as reall
1352 // disconnected
1353 mDefaultInetConditionPublished = 0;
1354 mDefaultConnectionSequence++;
1355 mInetConditionChangeInFlight = false;
1356 // Don't do this - if we never sign in stay, grey
1357 //reportNetworkCondition(mActiveDefaultNetwork, 100);
Robert Greenwalt2034b912009-08-12 16:08:25 -07001358 }
The Android Open Source Project28527d22009-03-03 19:31:44 -08001359 thisNet.setTeardownRequested(false);
Irfan Sheriff7f132d92010-06-09 15:39:36 -07001360 updateNetworkSettings(thisNet);
Robert Greenwalt3afbead2010-07-23 15:46:26 -07001361 handleConnectivityChange(type);
Robert Greenwalt2034b912009-08-12 16:08:25 -07001362 sendConnectedBroadcast(info);
The Android Open Source Project28527d22009-03-03 19:31:44 -08001363 }
1364
The Android Open Source Project28527d22009-03-03 19:31:44 -08001365 /**
Robert Greenwalt3afbead2010-07-23 15:46:26 -07001366 * After a change in the connectivity state of a network. We're mainly
1367 * concerned with making sure that the list of DNS servers is set up
1368 * according to which networks are connected, and ensuring that the
1369 * right routing table entries exist.
The Android Open Source Project28527d22009-03-03 19:31:44 -08001370 */
Robert Greenwalt3afbead2010-07-23 15:46:26 -07001371 private void handleConnectivityChange(int netType) {
The Android Open Source Project28527d22009-03-03 19:31:44 -08001372 /*
Robert Greenwalt2034b912009-08-12 16:08:25 -07001373 * If a non-default network is enabled, add the host routes that
Robert Greenwalt3afbead2010-07-23 15:46:26 -07001374 * will allow it's DNS servers to be accessed.
The Android Open Source Project28527d22009-03-03 19:31:44 -08001375 */
Robert Greenwalt3afbead2010-07-23 15:46:26 -07001376 handleDnsConfigurationChange(netType);
Robert Greenwalt2034b912009-08-12 16:08:25 -07001377
Robert Greenwalt3afbead2010-07-23 15:46:26 -07001378 if (mNetTrackers[netType].getNetworkInfo().isConnected()) {
Robert Greenwalt34848c02011-03-25 13:09:25 -07001379 if (mNetConfigs[netType].isDefault()) {
Robert Greenwaltc3c5f862010-10-11 16:00:27 -07001380 handleApplyDefaultProxy(netType);
Robert Greenwalt3afbead2010-07-23 15:46:26 -07001381 addDefaultRoute(mNetTrackers[netType]);
Robert Greenwalt2034b912009-08-12 16:08:25 -07001382 } else {
Robert Greenwalt1e2f2862011-04-01 10:51:22 -07001383 // many radios add a default route even when we don't want one.
1384 // remove the default route unless we need it for our active network
1385 if (mActiveDefaultNetwork != -1) {
1386 LinkProperties defaultLinkProperties =
1387 mNetTrackers[mActiveDefaultNetwork].getLinkProperties();
1388 LinkProperties newLinkProperties =
1389 mNetTrackers[netType].getLinkProperties();
1390 String defaultIface = defaultLinkProperties.getInterfaceName();
1391 if (defaultIface != null &&
1392 !defaultIface.equals(newLinkProperties.getInterfaceName())) {
1393 removeDefaultRoute(mNetTrackers[netType]);
1394 }
1395 }
Michael Jurka837e3642011-03-30 19:54:08 -07001396 addPrivateDnsRoutes(mNetTrackers[netType]);
Robert Greenwalt3afbead2010-07-23 15:46:26 -07001397 }
Kazuhiro Ondo3a340412011-04-30 20:10:57 -05001398
1399 /** Notify TetheringService if interface name has been changed. */
1400 if (TextUtils.equals(mNetTrackers[netType].getNetworkInfo().getReason(),
1401 Phone.REASON_LINK_PROPERTIES_CHANGED)) {
1402 handleTetherIfaceChange(netType);
1403 }
Robert Greenwalt3afbead2010-07-23 15:46:26 -07001404 } else {
Robert Greenwalt34848c02011-03-25 13:09:25 -07001405 if (mNetConfigs[netType].isDefault()) {
Robert Greenwalt3afbead2010-07-23 15:46:26 -07001406 removeDefaultRoute(mNetTrackers[netType]);
1407 } else {
1408 removePrivateDnsRoutes(mNetTrackers[netType]);
Robert Greenwalt2034b912009-08-12 16:08:25 -07001409 }
The Android Open Source Project28527d22009-03-03 19:31:44 -08001410 }
1411 }
1412
Irfan Sheriff7f132d92010-06-09 15:39:36 -07001413 private void addPrivateDnsRoutes(NetworkStateTracker nt) {
Irfan Sheriff7f132d92010-06-09 15:39:36 -07001414 boolean privateDnsRouteSet = nt.isPrivateDnsRouteSet();
Robert Greenwalt1f1bcfe2010-08-30 10:56:47 -07001415 LinkProperties p = nt.getLinkProperties();
Robert Greenwalta7dfbd32010-06-15 15:43:39 -07001416 if (p == null) return;
1417 String interfaceName = p.getInterfaceName();
Irfan Sheriff7f132d92010-06-09 15:39:36 -07001418
1419 if (DBG) {
Wink Savillee70c6f52010-12-03 12:01:38 -08001420 log("addPrivateDnsRoutes for " + nt +
Irfan Sheriff7f132d92010-06-09 15:39:36 -07001421 "(" + interfaceName + ") - mPrivateDnsRouteSet = " + privateDnsRouteSet);
1422 }
Irfan Sheriff7f132d92010-06-09 15:39:36 -07001423 if (interfaceName != null && !privateDnsRouteSet) {
Robert Greenwalta7dfbd32010-06-15 15:43:39 -07001424 Collection<InetAddress> dnsList = p.getDnses();
1425 for (InetAddress dns : dnsList) {
Robert Greenwaltbd492212011-05-06 17:10:53 -07001426 addHostRoute(nt, dns, 0);
Irfan Sheriff7f132d92010-06-09 15:39:36 -07001427 }
1428 nt.privateDnsRouteSet(true);
1429 }
1430 }
1431
1432 private void removePrivateDnsRoutes(NetworkStateTracker nt) {
Robert Greenwalt1f1bcfe2010-08-30 10:56:47 -07001433 LinkProperties p = nt.getLinkProperties();
Robert Greenwalta7dfbd32010-06-15 15:43:39 -07001434 if (p == null) return;
1435 String interfaceName = p.getInterfaceName();
Irfan Sheriff7f132d92010-06-09 15:39:36 -07001436 boolean privateDnsRouteSet = nt.isPrivateDnsRouteSet();
1437 if (interfaceName != null && privateDnsRouteSet) {
1438 if (DBG) {
Wink Savillee70c6f52010-12-03 12:01:38 -08001439 log("removePrivateDnsRoutes for " + nt.getNetworkInfo().getTypeName() +
Irfan Sheriff7f132d92010-06-09 15:39:36 -07001440 " (" + interfaceName + ")");
1441 }
Robert Greenwalt355205c2011-05-10 15:05:02 -07001442
1443 Collection<InetAddress> dnsList = p.getDnses();
1444 for (InetAddress dns : dnsList) {
1445 if (DBG) log(" removing " + dns);
1446 RouteInfo route = RouteInfo.makeHostRoute(dns);
1447 try {
1448 mNetd.removeRoute(interfaceName, route);
1449 } catch (Exception ex) {
1450 loge("error (" + ex + ") removing dns route " + route);
1451 }
1452 }
Irfan Sheriff7f132d92010-06-09 15:39:36 -07001453 nt.privateDnsRouteSet(false);
1454 }
1455 }
1456
Irfan Sheriff7f132d92010-06-09 15:39:36 -07001457
1458 private void addDefaultRoute(NetworkStateTracker nt) {
Robert Greenwalt1f1bcfe2010-08-30 10:56:47 -07001459 LinkProperties p = nt.getLinkProperties();
Robert Greenwalta7dfbd32010-06-15 15:43:39 -07001460 if (p == null) return;
1461 String interfaceName = p.getInterfaceName();
Robert Greenwalt5c733972011-02-09 13:56:06 -08001462 if (TextUtils.isEmpty(interfaceName)) return;
Irfan Sheriff7f132d92010-06-09 15:39:36 -07001463
Robert Greenwalt355205c2011-05-10 15:05:02 -07001464 for (RouteInfo route : p.getRoutes()) {
Robert Greenwalt5a901292011-04-28 14:28:50 -07001465 //TODO - handle non-default routes
1466 if (route.isDefaultRoute()) {
Robert Greenwalt355205c2011-05-10 15:05:02 -07001467 if (DBG) log("adding default route " + route);
Robert Greenwalt5a901292011-04-28 14:28:50 -07001468 InetAddress gateway = route.getGateway();
Robert Greenwalt355205c2011-05-10 15:05:02 -07001469 if (addHostRoute(nt, gateway, 0)) {
1470 try {
1471 mNetd.addRoute(interfaceName, route);
1472 } catch (Exception e) {
1473 loge("error adding default route " + route);
1474 continue;
1475 }
Robert Greenwalt5a901292011-04-28 14:28:50 -07001476 if (DBG) {
1477 NetworkInfo networkInfo = nt.getNetworkInfo();
1478 log("addDefaultRoute for " + networkInfo.getTypeName() +
1479 " (" + interfaceName + "), GatewayAddr=" +
1480 gateway.getHostAddress());
1481 }
Robert Greenwalt355205c2011-05-10 15:05:02 -07001482 } else {
1483 loge("error adding host route for default route " + route);
Robert Greenwalt03d53da2011-03-22 18:47:42 -07001484 }
Irfan Sheriff7f132d92010-06-09 15:39:36 -07001485 }
Irfan Sheriff7f132d92010-06-09 15:39:36 -07001486 }
1487 }
1488
1489
1490 public void removeDefaultRoute(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();
Irfan Sheriff7f132d92010-06-09 15:39:36 -07001494
Robert Greenwalt355205c2011-05-10 15:05:02 -07001495 if (interfaceName == null) return;
1496
1497 for (RouteInfo route : p.getRoutes()) {
1498 //TODO - handle non-default routes
1499 if (route.isDefaultRoute()) {
1500 try {
1501 mNetd.removeRoute(interfaceName, route);
1502 } catch (Exception ex) {
1503 loge("error (" + ex + ") removing default route " + route);
1504 continue;
1505 }
Robert Greenwalt03d53da2011-03-22 18:47:42 -07001506 if (DBG) {
1507 NetworkInfo networkInfo = nt.getNetworkInfo();
1508 log("removeDefaultRoute for " + networkInfo.getTypeName() + " (" +
1509 interfaceName + ")");
1510 }
Irfan Sheriff7f132d92010-06-09 15:39:36 -07001511 }
Irfan Sheriff7f132d92010-06-09 15:39:36 -07001512 }
1513 }
1514
1515 /**
1516 * Reads the network specific TCP buffer sizes from SystemProperties
1517 * net.tcp.buffersize.[default|wifi|umts|edge|gprs] and set them for system
1518 * wide use
1519 */
1520 public void updateNetworkSettings(NetworkStateTracker nt) {
1521 String key = nt.getTcpBufferSizesPropName();
1522 String bufferSizes = SystemProperties.get(key);
1523
1524 if (bufferSizes.length() == 0) {
Wink Savillee70c6f52010-12-03 12:01:38 -08001525 loge(key + " not found in system properties. Using defaults");
Irfan Sheriff7f132d92010-06-09 15:39:36 -07001526
1527 // Setting to default values so we won't be stuck to previous values
1528 key = "net.tcp.buffersize.default";
1529 bufferSizes = SystemProperties.get(key);
1530 }
1531
1532 // Set values in kernel
1533 if (bufferSizes.length() != 0) {
1534 if (DBG) {
Wink Savillee70c6f52010-12-03 12:01:38 -08001535 log("Setting TCP values: [" + bufferSizes
Irfan Sheriff7f132d92010-06-09 15:39:36 -07001536 + "] which comes from [" + key + "]");
1537 }
1538 setBufferSize(bufferSizes);
1539 }
1540 }
1541
1542 /**
1543 * Writes TCP buffer sizes to /sys/kernel/ipv4/tcp_[r/w]mem_[min/def/max]
1544 * which maps to /proc/sys/net/ipv4/tcp_rmem and tcpwmem
1545 *
1546 * @param bufferSizes in the format of "readMin, readInitial, readMax,
1547 * writeMin, writeInitial, writeMax"
1548 */
1549 private void setBufferSize(String bufferSizes) {
1550 try {
1551 String[] values = bufferSizes.split(",");
1552
1553 if (values.length == 6) {
1554 final String prefix = "/sys/kernel/ipv4/tcp_";
Mike Lockwood0d5916c2011-05-28 13:24:04 -04001555 FileUtils.stringToFile(prefix + "rmem_min", values[0]);
1556 FileUtils.stringToFile(prefix + "rmem_def", values[1]);
1557 FileUtils.stringToFile(prefix + "rmem_max", values[2]);
1558 FileUtils.stringToFile(prefix + "wmem_min", values[3]);
1559 FileUtils.stringToFile(prefix + "wmem_def", values[4]);
1560 FileUtils.stringToFile(prefix + "wmem_max", values[5]);
Irfan Sheriff7f132d92010-06-09 15:39:36 -07001561 } else {
Wink Savillee70c6f52010-12-03 12:01:38 -08001562 loge("Invalid buffersize string: " + bufferSizes);
Irfan Sheriff7f132d92010-06-09 15:39:36 -07001563 }
1564 } catch (IOException e) {
Wink Savillee70c6f52010-12-03 12:01:38 -08001565 loge("Can't set tcp buffer sizes:" + e);
Irfan Sheriff7f132d92010-06-09 15:39:36 -07001566 }
1567 }
1568
Robert Greenwalt2034b912009-08-12 16:08:25 -07001569 /**
1570 * Adjust the per-process dns entries (net.dns<x>.<pid>) based
1571 * on the highest priority active net which this process requested.
1572 * If there aren't any, clear it out
1573 */
1574 private void reassessPidDns(int myPid, boolean doBump)
1575 {
Wink Savillee70c6f52010-12-03 12:01:38 -08001576 if (DBG) log("reassessPidDns for pid " + myPid);
Robert Greenwalt2034b912009-08-12 16:08:25 -07001577 for(int i : mPriorityList) {
Robert Greenwalt34848c02011-03-25 13:09:25 -07001578 if (mNetConfigs[i].isDefault()) {
Robert Greenwalt2034b912009-08-12 16:08:25 -07001579 continue;
1580 }
1581 NetworkStateTracker nt = mNetTrackers[i];
Robert Greenwalt0659da32009-07-16 17:21:39 -07001582 if (nt.getNetworkInfo().isConnected() &&
1583 !nt.isTeardownRequested()) {
Robert Greenwalt1f1bcfe2010-08-30 10:56:47 -07001584 LinkProperties p = nt.getLinkProperties();
Robert Greenwalta7dfbd32010-06-15 15:43:39 -07001585 if (p == null) continue;
Robert Greenwalt2034b912009-08-12 16:08:25 -07001586 List pids = mNetRequestersPids[i];
1587 for (int j=0; j<pids.size(); j++) {
1588 Integer pid = (Integer)pids.get(j);
1589 if (pid.intValue() == myPid) {
Robert Greenwalta7dfbd32010-06-15 15:43:39 -07001590 Collection<InetAddress> dnses = p.getDnses();
1591 writePidDns(dnses, myPid);
Robert Greenwalt2034b912009-08-12 16:08:25 -07001592 if (doBump) {
1593 bumpDns();
1594 }
1595 return;
1596 }
1597 }
1598 }
1599 }
1600 // nothing found - delete
1601 for (int i = 1; ; i++) {
1602 String prop = "net.dns" + i + "." + myPid;
1603 if (SystemProperties.get(prop).length() == 0) {
1604 if (doBump) {
1605 bumpDns();
1606 }
1607 return;
1608 }
1609 SystemProperties.set(prop, "");
1610 }
1611 }
1612
Robert Greenwalt8ca88762010-12-17 15:20:36 -08001613 // return true if results in a change
1614 private boolean writePidDns(Collection <InetAddress> dnses, int pid) {
Robert Greenwalt2034b912009-08-12 16:08:25 -07001615 int j = 1;
Robert Greenwalt8ca88762010-12-17 15:20:36 -08001616 boolean changed = false;
Robert Greenwalta7dfbd32010-06-15 15:43:39 -07001617 for (InetAddress dns : dnses) {
Robert Greenwalt8ca88762010-12-17 15:20:36 -08001618 String dnsString = dns.getHostAddress();
1619 if (changed || !dnsString.equals(SystemProperties.get("net.dns" + j + "." + pid))) {
1620 changed = true;
1621 SystemProperties.set("net.dns" + j++ + "." + pid, dns.getHostAddress());
1622 }
Robert Greenwalt2034b912009-08-12 16:08:25 -07001623 }
Robert Greenwalt8ca88762010-12-17 15:20:36 -08001624 return changed;
Robert Greenwalt2034b912009-08-12 16:08:25 -07001625 }
1626
1627 private void bumpDns() {
1628 /*
1629 * Bump the property that tells the name resolver library to reread
1630 * the DNS server list from the properties.
1631 */
1632 String propVal = SystemProperties.get("net.dnschange");
1633 int n = 0;
1634 if (propVal.length() != 0) {
1635 try {
1636 n = Integer.parseInt(propVal);
1637 } catch (NumberFormatException e) {}
1638 }
1639 SystemProperties.set("net.dnschange", "" + (n+1));
Robert Greenwalt051642b2010-11-02 14:08:23 -07001640 /*
1641 * Tell the VMs to toss their DNS caches
1642 */
1643 Intent intent = new Intent(Intent.ACTION_CLEAR_DNS_CACHE);
1644 intent.addFlags(Intent.FLAG_RECEIVER_REPLACE_PENDING);
Stan Chesnuttf444f502011-01-05 17:14:03 -08001645 /*
1646 * Connectivity events can happen before boot has completed ...
1647 */
1648 intent.addFlags(Intent.FLAG_RECEIVER_REGISTERED_ONLY_BEFORE_BOOT);
Robert Greenwalt051642b2010-11-02 14:08:23 -07001649 mContext.sendBroadcast(intent);
Robert Greenwalt2034b912009-08-12 16:08:25 -07001650 }
1651
Robert Greenwalt3afbead2010-07-23 15:46:26 -07001652 private void handleDnsConfigurationChange(int netType) {
Robert Greenwalt2034b912009-08-12 16:08:25 -07001653 // add default net's dns entries
Robert Greenwalt3afbead2010-07-23 15:46:26 -07001654 NetworkStateTracker nt = mNetTrackers[netType];
1655 if (nt != null && nt.getNetworkInfo().isConnected() && !nt.isTeardownRequested()) {
Robert Greenwalt1f1bcfe2010-08-30 10:56:47 -07001656 LinkProperties p = nt.getLinkProperties();
Robert Greenwalt3afbead2010-07-23 15:46:26 -07001657 if (p == null) return;
1658 Collection<InetAddress> dnses = p.getDnses();
Robert Greenwalt8ca88762010-12-17 15:20:36 -08001659 boolean changed = false;
Robert Greenwalt34848c02011-03-25 13:09:25 -07001660 if (mNetConfigs[netType].isDefault()) {
Robert Greenwalt3afbead2010-07-23 15:46:26 -07001661 int j = 1;
Robert Greenwalt94daa182010-09-01 11:34:05 -07001662 if (dnses.size() == 0 && mDefaultDns != null) {
Robert Greenwalt8ca88762010-12-17 15:20:36 -08001663 String dnsString = mDefaultDns.getHostAddress();
1664 if (!dnsString.equals(SystemProperties.get("net.dns1"))) {
1665 if (DBG) {
1666 log("no dns provided - using " + dnsString);
1667 }
1668 changed = true;
1669 SystemProperties.set("net.dns1", dnsString);
Robert Greenwalt2034b912009-08-12 16:08:25 -07001670 }
Robert Greenwalt94daa182010-09-01 11:34:05 -07001671 j++;
1672 } else {
1673 for (InetAddress dns : dnses) {
Robert Greenwalt8ca88762010-12-17 15:20:36 -08001674 String dnsString = dns.getHostAddress();
1675 if (!changed && dnsString.equals(SystemProperties.get("net.dns" + j))) {
1676 j++;
1677 continue;
1678 }
Robert Greenwalt94daa182010-09-01 11:34:05 -07001679 if (DBG) {
Wink Savillee70c6f52010-12-03 12:01:38 -08001680 log("adding dns " + dns + " for " +
Robert Greenwalt94daa182010-09-01 11:34:05 -07001681 nt.getNetworkInfo().getTypeName());
1682 }
Robert Greenwalt8ca88762010-12-17 15:20:36 -08001683 changed = true;
1684 SystemProperties.set("net.dns" + j++, dnsString);
Robert Greenwalt94daa182010-09-01 11:34:05 -07001685 }
Robert Greenwalt3afbead2010-07-23 15:46:26 -07001686 }
1687 for (int k=j ; k<mNumDnsEntries; k++) {
Robert Greenwalt8ca88762010-12-17 15:20:36 -08001688 if (changed || !TextUtils.isEmpty(SystemProperties.get("net.dns" + k))) {
1689 if (DBG) log("erasing net.dns" + k);
1690 changed = true;
1691 SystemProperties.set("net.dns" + k, "");
1692 }
Robert Greenwalt3afbead2010-07-23 15:46:26 -07001693 }
1694 mNumDnsEntries = j;
1695 } else {
1696 // set per-pid dns for attached secondary nets
1697 List pids = mNetRequestersPids[netType];
1698 for (int y=0; y< pids.size(); y++) {
1699 Integer pid = (Integer)pids.get(y);
Robert Greenwalt8ca88762010-12-17 15:20:36 -08001700 changed = writePidDns(dnses, pid.intValue());
The Android Open Source Project28527d22009-03-03 19:31:44 -08001701 }
1702 }
Robert Greenwalt8ca88762010-12-17 15:20:36 -08001703 if (changed) bumpDns();
The Android Open Source Project28527d22009-03-03 19:31:44 -08001704 }
Robert Greenwalt2034b912009-08-12 16:08:25 -07001705 }
1706
Robert Greenwalt20f819c2011-05-03 19:02:44 -07001707 private int getRestoreDefaultNetworkDelay(int networkType) {
Robert Greenwalt2034b912009-08-12 16:08:25 -07001708 String restoreDefaultNetworkDelayStr = SystemProperties.get(
1709 NETWORK_RESTORE_DELAY_PROP_NAME);
1710 if(restoreDefaultNetworkDelayStr != null &&
1711 restoreDefaultNetworkDelayStr.length() != 0) {
1712 try {
1713 return Integer.valueOf(restoreDefaultNetworkDelayStr);
1714 } catch (NumberFormatException e) {
1715 }
The Android Open Source Project28527d22009-03-03 19:31:44 -08001716 }
Robert Greenwalt20f819c2011-05-03 19:02:44 -07001717 // if the system property isn't set, use the value for the apn type
1718 int ret = RESTORE_DEFAULT_NETWORK_DELAY;
1719
1720 if ((networkType <= ConnectivityManager.MAX_NETWORK_TYPE) &&
1721 (mNetConfigs[networkType] != null)) {
1722 ret = mNetConfigs[networkType].restoreTime;
1723 }
1724 return ret;
The Android Open Source Project28527d22009-03-03 19:31:44 -08001725 }
1726
1727 @Override
1728 protected void dump(FileDescriptor fd, PrintWriter pw, String[] args) {
Robert Greenwalt0659da32009-07-16 17:21:39 -07001729 if (mContext.checkCallingOrSelfPermission(
1730 android.Manifest.permission.DUMP)
The Android Open Source Project28527d22009-03-03 19:31:44 -08001731 != PackageManager.PERMISSION_GRANTED) {
Robert Greenwalt0659da32009-07-16 17:21:39 -07001732 pw.println("Permission Denial: can't dump ConnectivityService " +
1733 "from from pid=" + Binder.getCallingPid() + ", uid=" +
1734 Binder.getCallingUid());
The Android Open Source Project28527d22009-03-03 19:31:44 -08001735 return;
1736 }
The Android Open Source Project28527d22009-03-03 19:31:44 -08001737 pw.println();
1738 for (NetworkStateTracker nst : mNetTrackers) {
Robert Greenwalt3eeb6032009-12-21 18:24:07 -08001739 if (nst != null) {
1740 if (nst.getNetworkInfo().isConnected()) {
1741 pw.println("Active network: " + nst.getNetworkInfo().
1742 getTypeName());
1743 }
1744 pw.println(nst.getNetworkInfo());
1745 pw.println(nst);
1746 pw.println();
Robert Greenwalt2034b912009-08-12 16:08:25 -07001747 }
The Android Open Source Project28527d22009-03-03 19:31:44 -08001748 }
Robert Greenwalt3eeb6032009-12-21 18:24:07 -08001749
1750 pw.println("Network Requester Pids:");
1751 for (int net : mPriorityList) {
1752 String pidString = net + ": ";
1753 for (Object pid : mNetRequestersPids[net]) {
1754 pidString = pidString + pid.toString() + ", ";
1755 }
1756 pw.println(pidString);
1757 }
1758 pw.println();
1759
1760 pw.println("FeatureUsers:");
1761 for (Object requester : mFeatureUsers) {
1762 pw.println(requester.toString());
1763 }
1764 pw.println();
Robert Greenwalt8e87f122010-02-11 18:18:40 -08001765
Robert Greenwalt93dc1042010-06-15 12:19:37 -07001766 synchronized (this) {
1767 pw.println("NetworkTranstionWakeLock is currently " +
1768 (mNetTransitionWakeLock.isHeld() ? "" : "not ") + "held.");
1769 pw.println("It was last requested for "+mNetTransitionWakeLockCausedBy);
1770 }
1771 pw.println();
1772
Robert Greenwalt8e87f122010-02-11 18:18:40 -08001773 mTethering.dump(fd, pw, args);
Robert Greenwalt0e80be12010-09-20 14:35:25 -07001774
1775 if (mInetLog != null) {
1776 pw.println();
1777 pw.println("Inet condition reports:");
1778 for(int i = 0; i < mInetLog.size(); i++) {
1779 pw.println(mInetLog.get(i));
1780 }
1781 }
The Android Open Source Project28527d22009-03-03 19:31:44 -08001782 }
1783
Robert Greenwalt2034b912009-08-12 16:08:25 -07001784 // must be stateless - things change under us.
The Android Open Source Project28527d22009-03-03 19:31:44 -08001785 private class MyHandler extends Handler {
Wink Saville775aad62010-09-02 19:23:52 -07001786 public MyHandler(Looper looper) {
1787 super(looper);
1788 }
1789
The Android Open Source Project28527d22009-03-03 19:31:44 -08001790 @Override
1791 public void handleMessage(Message msg) {
1792 NetworkInfo info;
1793 switch (msg.what) {
1794 case NetworkStateTracker.EVENT_STATE_CHANGED:
1795 info = (NetworkInfo) msg.obj;
Robert Greenwalt12c44552009-12-07 11:33:18 -08001796 int type = info.getType();
1797 NetworkInfo.State state = info.getState();
Robert Greenwalt12c44552009-12-07 11:33:18 -08001798
Wink Savillee70c6f52010-12-03 12:01:38 -08001799 if (DBG) log("ConnectivityChange for " +
Robert Greenwalt0659da32009-07-16 17:21:39 -07001800 info.getTypeName() + ": " +
Robert Greenwalt12c44552009-12-07 11:33:18 -08001801 state + "/" + info.getDetailedState());
The Android Open Source Project28527d22009-03-03 19:31:44 -08001802
1803 // Connectivity state changed:
1804 // [31-13] Reserved for future use
Robert Greenwalt0659da32009-07-16 17:21:39 -07001805 // [12-9] Network subtype (for mobile network, as defined
1806 // by TelephonyManager)
1807 // [8-3] Detailed state ordinal (as defined by
1808 // NetworkInfo.DetailedState)
The Android Open Source Project28527d22009-03-03 19:31:44 -08001809 // [2-0] Network type (as defined by ConnectivityManager)
1810 int eventLogParam = (info.getType() & 0x7) |
1811 ((info.getDetailedState().ordinal() & 0x3f) << 3) |
1812 (info.getSubtype() << 9);
Doug Zongker2fc96232009-12-04 10:31:43 -08001813 EventLog.writeEvent(EventLogTags.CONNECTIVITY_STATE_CHANGED,
Robert Greenwalt0659da32009-07-16 17:21:39 -07001814 eventLogParam);
1815
1816 if (info.getDetailedState() ==
1817 NetworkInfo.DetailedState.FAILED) {
The Android Open Source Project28527d22009-03-03 19:31:44 -08001818 handleConnectionFailure(info);
Robert Greenwalt12c44552009-12-07 11:33:18 -08001819 } else if (state == NetworkInfo.State.DISCONNECTED) {
The Android Open Source Project28527d22009-03-03 19:31:44 -08001820 handleDisconnect(info);
Robert Greenwalt12c44552009-12-07 11:33:18 -08001821 } else if (state == NetworkInfo.State.SUSPENDED) {
The Android Open Source Project28527d22009-03-03 19:31:44 -08001822 // TODO: need to think this over.
Robert Greenwalt0659da32009-07-16 17:21:39 -07001823 // the logic here is, handle SUSPENDED the same as
1824 // DISCONNECTED. The only difference being we are
1825 // broadcasting an intent with NetworkInfo that's
1826 // suspended. This allows the applications an
1827 // opportunity to handle DISCONNECTED and SUSPENDED
1828 // differently, or not.
The Android Open Source Project28527d22009-03-03 19:31:44 -08001829 handleDisconnect(info);
Robert Greenwalt12c44552009-12-07 11:33:18 -08001830 } else if (state == NetworkInfo.State.CONNECTED) {
The Android Open Source Project28527d22009-03-03 19:31:44 -08001831 handleConnect(info);
1832 }
The Android Open Source Project28527d22009-03-03 19:31:44 -08001833 break;
The Android Open Source Project28527d22009-03-03 19:31:44 -08001834 case NetworkStateTracker.EVENT_CONFIGURATION_CHANGED:
Robert Greenwalt3afbead2010-07-23 15:46:26 -07001835 info = (NetworkInfo) msg.obj;
Robert Greenwalt34848c02011-03-25 13:09:25 -07001836 handleConnectivityChange(info.getType());
The Android Open Source Project28527d22009-03-03 19:31:44 -08001837 break;
Robert Greenwaltccb36f92010-09-24 14:32:21 -07001838 case EVENT_CLEAR_NET_TRANSITION_WAKELOCK:
Robert Greenwalt93dc1042010-06-15 12:19:37 -07001839 String causedBy = null;
1840 synchronized (ConnectivityService.this) {
1841 if (msg.arg1 == mNetTransitionWakeLockSerialNumber &&
1842 mNetTransitionWakeLock.isHeld()) {
1843 mNetTransitionWakeLock.release();
1844 causedBy = mNetTransitionWakeLockCausedBy;
1845 }
1846 }
1847 if (causedBy != null) {
Wink Savillee70c6f52010-12-03 12:01:38 -08001848 log("NetTransition Wakelock for " + causedBy + " released by timeout");
Robert Greenwalt93dc1042010-06-15 12:19:37 -07001849 }
Robert Greenwaltcf1a56c2010-09-09 14:05:10 -07001850 break;
Robert Greenwalt6a2db8a2010-09-23 10:05:56 -07001851 case EVENT_RESTORE_DEFAULT_NETWORK:
Robert Greenwaltaffc3a12009-09-27 17:27:04 -07001852 FeatureUser u = (FeatureUser)msg.obj;
1853 u.expire();
Robert Greenwalt986c7412010-09-08 15:24:47 -07001854 break;
Robert Greenwalt6a2db8a2010-09-23 10:05:56 -07001855 case EVENT_INET_CONDITION_CHANGE:
1856 {
1857 int netType = msg.arg1;
1858 int condition = msg.arg2;
1859 handleInetConditionChange(netType, condition);
Robert Greenwalt93dc1042010-06-15 12:19:37 -07001860 break;
Robert Greenwalt6a2db8a2010-09-23 10:05:56 -07001861 }
1862 case EVENT_INET_CONDITION_HOLD_END:
1863 {
1864 int netType = msg.arg1;
1865 int sequence = msg.arg2;
1866 handleInetConditionHoldEnd(netType, sequence);
Robert Greenwalt986c7412010-09-08 15:24:47 -07001867 break;
Robert Greenwalt6a2db8a2010-09-23 10:05:56 -07001868 }
1869 case EVENT_SET_NETWORK_PREFERENCE:
1870 {
1871 int preference = msg.arg1;
1872 handleSetNetworkPreference(preference);
1873 break;
1874 }
1875 case EVENT_SET_BACKGROUND_DATA:
1876 {
1877 boolean enabled = (msg.arg1 == ENABLED);
1878 handleSetBackgroundData(enabled);
1879 break;
1880 }
1881 case EVENT_SET_MOBILE_DATA:
1882 {
1883 boolean enabled = (msg.arg1 == ENABLED);
1884 handleSetMobileData(enabled);
1885 break;
1886 }
Robert Greenwaltc3c5f862010-10-11 16:00:27 -07001887 case EVENT_APPLY_GLOBAL_HTTP_PROXY:
1888 {
1889 handleDeprecatedGlobalHttpProxy();
Robert Greenwalt34848c02011-03-25 13:09:25 -07001890 break;
1891 }
1892 case EVENT_SET_DEPENDENCY_MET:
1893 {
1894 boolean met = (msg.arg1 == ENABLED);
1895 handleSetDependencyMet(msg.arg2, met);
1896 break;
Robert Greenwaltc3c5f862010-10-11 16:00:27 -07001897 }
The Android Open Source Project28527d22009-03-03 19:31:44 -08001898 }
1899 }
1900 }
Robert Greenwalt0c4828c2010-01-26 11:40:34 -08001901
1902 // javadoc from interface
Robert Greenwalt4283ded2010-03-02 17:25:02 -08001903 public int tether(String iface) {
Robert Greenwalt0c4828c2010-01-26 11:40:34 -08001904 enforceTetherChangePermission();
Robert Greenwalt4283ded2010-03-02 17:25:02 -08001905
1906 if (isTetheringSupported()) {
1907 return mTethering.tether(iface);
1908 } else {
1909 return ConnectivityManager.TETHER_ERROR_UNSUPPORTED;
1910 }
Robert Greenwalt0c4828c2010-01-26 11:40:34 -08001911 }
1912
1913 // javadoc from interface
Robert Greenwalt4283ded2010-03-02 17:25:02 -08001914 public int untether(String iface) {
Robert Greenwalt0c4828c2010-01-26 11:40:34 -08001915 enforceTetherChangePermission();
Robert Greenwalt4283ded2010-03-02 17:25:02 -08001916
1917 if (isTetheringSupported()) {
1918 return mTethering.untether(iface);
1919 } else {
1920 return ConnectivityManager.TETHER_ERROR_UNSUPPORTED;
1921 }
1922 }
1923
1924 // javadoc from interface
1925 public int getLastTetherError(String iface) {
1926 enforceTetherAccessPermission();
1927
1928 if (isTetheringSupported()) {
1929 return mTethering.getLastTetherError(iface);
1930 } else {
1931 return ConnectivityManager.TETHER_ERROR_UNSUPPORTED;
1932 }
Robert Greenwalt8e87f122010-02-11 18:18:40 -08001933 }
1934
1935 // TODO - proper iface API for selection by property, inspection, etc
1936 public String[] getTetherableUsbRegexs() {
1937 enforceTetherAccessPermission();
1938 if (isTetheringSupported()) {
1939 return mTethering.getTetherableUsbRegexs();
1940 } else {
1941 return new String[0];
1942 }
1943 }
1944
1945 public String[] getTetherableWifiRegexs() {
1946 enforceTetherAccessPermission();
1947 if (isTetheringSupported()) {
1948 return mTethering.getTetherableWifiRegexs();
1949 } else {
1950 return new String[0];
1951 }
Robert Greenwalt0c4828c2010-01-26 11:40:34 -08001952 }
1953
Danica Chang96567052010-08-11 14:54:43 -07001954 public String[] getTetherableBluetoothRegexs() {
1955 enforceTetherAccessPermission();
1956 if (isTetheringSupported()) {
1957 return mTethering.getTetherableBluetoothRegexs();
1958 } else {
1959 return new String[0];
1960 }
1961 }
1962
Robert Greenwalt0c4828c2010-01-26 11:40:34 -08001963 // TODO - move iface listing, queries, etc to new module
1964 // javadoc from interface
1965 public String[] getTetherableIfaces() {
Robert Greenwalt8e87f122010-02-11 18:18:40 -08001966 enforceTetherAccessPermission();
Robert Greenwalt0c4828c2010-01-26 11:40:34 -08001967 return mTethering.getTetherableIfaces();
1968 }
1969
1970 public String[] getTetheredIfaces() {
Robert Greenwalt8e87f122010-02-11 18:18:40 -08001971 enforceTetherAccessPermission();
Robert Greenwalt0c4828c2010-01-26 11:40:34 -08001972 return mTethering.getTetheredIfaces();
1973 }
Robert Greenwalt8e87f122010-02-11 18:18:40 -08001974
Robert Greenwalt4283ded2010-03-02 17:25:02 -08001975 public String[] getTetheringErroredIfaces() {
1976 enforceTetherAccessPermission();
1977 return mTethering.getErroredIfaces();
1978 }
1979
Robert Greenwalt8e87f122010-02-11 18:18:40 -08001980 // if ro.tether.denied = true we default to no tethering
1981 // gservices could set the secure setting to 1 though to enable it on a build where it
1982 // had previously been turned off.
1983 public boolean isTetheringSupported() {
1984 enforceTetherAccessPermission();
1985 int defaultVal = (SystemProperties.get("ro.tether.denied").equals("true") ? 0 : 1);
Robert Greenwaltf1b66e12010-02-25 12:29:30 -08001986 boolean tetherEnabledInSettings = (Settings.Secure.getInt(mContext.getContentResolver(),
1987 Settings.Secure.TETHER_SUPPORTED, defaultVal) != 0);
1988 return tetherEnabledInSettings && mTetheringConfigValid;
Robert Greenwalt8e87f122010-02-11 18:18:40 -08001989 }
Robert Greenwalt93dc1042010-06-15 12:19:37 -07001990
1991 // An API NetworkStateTrackers can call when they lose their network.
1992 // This will automatically be cleared after X seconds or a network becomes CONNECTED,
1993 // whichever happens first. The timer is started by the first caller and not
1994 // restarted by subsequent callers.
1995 public void requestNetworkTransitionWakelock(String forWhom) {
1996 enforceConnectivityInternalPermission();
1997 synchronized (this) {
1998 if (mNetTransitionWakeLock.isHeld()) return;
1999 mNetTransitionWakeLockSerialNumber++;
2000 mNetTransitionWakeLock.acquire();
2001 mNetTransitionWakeLockCausedBy = forWhom;
2002 }
2003 mHandler.sendMessageDelayed(mHandler.obtainMessage(
Robert Greenwaltccb36f92010-09-24 14:32:21 -07002004 EVENT_CLEAR_NET_TRANSITION_WAKELOCK,
Robert Greenwalt93dc1042010-06-15 12:19:37 -07002005 mNetTransitionWakeLockSerialNumber, 0),
2006 mNetTransitionWakeLockTimeout);
2007 return;
2008 }
Robert Greenwalt24118e82010-09-09 13:15:32 -07002009
Robert Greenwalt986c7412010-09-08 15:24:47 -07002010 // 100 percent is full good, 0 is full bad.
2011 public void reportInetCondition(int networkType, int percentage) {
Wink Savillee70c6f52010-12-03 12:01:38 -08002012 if (DBG) log("reportNetworkCondition(" + networkType + ", " + percentage + ")");
Robert Greenwalt986c7412010-09-08 15:24:47 -07002013 mContext.enforceCallingOrSelfPermission(
2014 android.Manifest.permission.STATUS_BAR,
2015 "ConnectivityService");
2016
Robert Greenwalt0e80be12010-09-20 14:35:25 -07002017 if (DBG) {
2018 int pid = getCallingPid();
2019 int uid = getCallingUid();
2020 String s = pid + "(" + uid + ") reports inet is " +
2021 (percentage > 50 ? "connected" : "disconnected") + " (" + percentage + ") on " +
2022 "network Type " + networkType + " at " + GregorianCalendar.getInstance().getTime();
2023 mInetLog.add(s);
2024 while(mInetLog.size() > INET_CONDITION_LOG_MAX_SIZE) {
2025 mInetLog.remove(0);
2026 }
2027 }
Robert Greenwalt986c7412010-09-08 15:24:47 -07002028 mHandler.sendMessage(mHandler.obtainMessage(
Robert Greenwalt6a2db8a2010-09-23 10:05:56 -07002029 EVENT_INET_CONDITION_CHANGE, networkType, percentage));
2030 }
2031
2032 private void handleInetConditionChange(int netType, int condition) {
2033 if (DBG) {
Wink Savillee70c6f52010-12-03 12:01:38 -08002034 log("Inet connectivity change, net=" +
Robert Greenwalt6a2db8a2010-09-23 10:05:56 -07002035 netType + ", condition=" + condition +
2036 ",mActiveDefaultNetwork=" + mActiveDefaultNetwork);
2037 }
2038 if (mActiveDefaultNetwork == -1) {
Wink Savillee70c6f52010-12-03 12:01:38 -08002039 if (DBG) log("no active default network - aborting");
Robert Greenwalt6a2db8a2010-09-23 10:05:56 -07002040 return;
2041 }
2042 if (mActiveDefaultNetwork != netType) {
Wink Savillee70c6f52010-12-03 12:01:38 -08002043 if (DBG) log("given net not default - aborting");
Robert Greenwalt6a2db8a2010-09-23 10:05:56 -07002044 return;
2045 }
2046 mDefaultInetCondition = condition;
2047 int delay;
2048 if (mInetConditionChangeInFlight == false) {
Wink Savillee70c6f52010-12-03 12:01:38 -08002049 if (DBG) log("starting a change hold");
Robert Greenwalt6a2db8a2010-09-23 10:05:56 -07002050 // setup a new hold to debounce this
2051 if (mDefaultInetCondition > 50) {
2052 delay = Settings.Secure.getInt(mContext.getContentResolver(),
2053 Settings.Secure.INET_CONDITION_DEBOUNCE_UP_DELAY, 500);
2054 } else {
2055 delay = Settings.Secure.getInt(mContext.getContentResolver(),
2056 Settings.Secure.INET_CONDITION_DEBOUNCE_DOWN_DELAY, 3000);
2057 }
2058 mInetConditionChangeInFlight = true;
2059 mHandler.sendMessageDelayed(mHandler.obtainMessage(EVENT_INET_CONDITION_HOLD_END,
2060 mActiveDefaultNetwork, mDefaultConnectionSequence), delay);
2061 } else {
2062 // we've set the new condition, when this hold ends that will get
2063 // picked up
Wink Savillee70c6f52010-12-03 12:01:38 -08002064 if (DBG) log("currently in hold - not setting new end evt");
Robert Greenwalt6a2db8a2010-09-23 10:05:56 -07002065 }
2066 }
2067
2068 private void handleInetConditionHoldEnd(int netType, int sequence) {
2069 if (DBG) {
Wink Savillee70c6f52010-12-03 12:01:38 -08002070 log("Inet hold end, net=" + netType +
Robert Greenwalt6a2db8a2010-09-23 10:05:56 -07002071 ", condition =" + mDefaultInetCondition +
2072 ", published condition =" + mDefaultInetConditionPublished);
2073 }
2074 mInetConditionChangeInFlight = false;
2075
2076 if (mActiveDefaultNetwork == -1) {
Wink Savillee70c6f52010-12-03 12:01:38 -08002077 if (DBG) log("no active default network - aborting");
Robert Greenwalt6a2db8a2010-09-23 10:05:56 -07002078 return;
2079 }
2080 if (mDefaultConnectionSequence != sequence) {
Wink Savillee70c6f52010-12-03 12:01:38 -08002081 if (DBG) log("event hold for obsolete network - aborting");
Robert Greenwalt6a2db8a2010-09-23 10:05:56 -07002082 return;
2083 }
2084 if (mDefaultInetConditionPublished == mDefaultInetCondition) {
Wink Savillee70c6f52010-12-03 12:01:38 -08002085 if (DBG) log("no change in condition - aborting");
Robert Greenwalt6a2db8a2010-09-23 10:05:56 -07002086 return;
2087 }
2088 NetworkInfo networkInfo = mNetTrackers[mActiveDefaultNetwork].getNetworkInfo();
2089 if (networkInfo.isConnected() == false) {
Wink Savillee70c6f52010-12-03 12:01:38 -08002090 if (DBG) log("default network not connected - aborting");
Robert Greenwalt6a2db8a2010-09-23 10:05:56 -07002091 return;
2092 }
2093 mDefaultInetConditionPublished = mDefaultInetCondition;
2094 sendInetConditionBroadcast(networkInfo);
2095 return;
Robert Greenwalt986c7412010-09-08 15:24:47 -07002096 }
Robert Greenwaltc3c5f862010-10-11 16:00:27 -07002097
2098 public synchronized ProxyProperties getProxy() {
2099 if (mGlobalProxy != null) return mGlobalProxy;
2100 if (mDefaultProxy != null) return mDefaultProxy;
2101 return null;
2102 }
2103
2104 public void setGlobalProxy(ProxyProperties proxyProperties) {
2105 enforceChangePermission();
2106 synchronized (mGlobalProxyLock) {
2107 if (proxyProperties == mGlobalProxy) return;
2108 if (proxyProperties != null && proxyProperties.equals(mGlobalProxy)) return;
2109 if (mGlobalProxy != null && mGlobalProxy.equals(proxyProperties)) return;
2110
2111 String host = "";
2112 int port = 0;
2113 String exclList = "";
2114 if (proxyProperties != null && !TextUtils.isEmpty(proxyProperties.getHost())) {
2115 mGlobalProxy = new ProxyProperties(proxyProperties);
2116 host = mGlobalProxy.getHost();
2117 port = mGlobalProxy.getPort();
2118 exclList = mGlobalProxy.getExclusionList();
2119 } else {
2120 mGlobalProxy = null;
2121 }
2122 ContentResolver res = mContext.getContentResolver();
2123 Settings.Secure.putString(res, Settings.Secure.GLOBAL_HTTP_PROXY_HOST, host);
2124 Settings.Secure.putInt(res, Settings.Secure.GLOBAL_HTTP_PROXY_PORT, port);
Robert Greenwalt6f7c6092010-12-02 11:31:00 -08002125 Settings.Secure.putString(res, Settings.Secure.GLOBAL_HTTP_PROXY_EXCLUSION_LIST,
Robert Greenwaltc3c5f862010-10-11 16:00:27 -07002126 exclList);
2127 }
2128
2129 if (mGlobalProxy == null) {
2130 proxyProperties = mDefaultProxy;
2131 }
2132 sendProxyBroadcast(proxyProperties);
2133 }
2134
Robert Greenwalt6f7c6092010-12-02 11:31:00 -08002135 private void loadGlobalProxy() {
2136 ContentResolver res = mContext.getContentResolver();
2137 String host = Settings.Secure.getString(res, Settings.Secure.GLOBAL_HTTP_PROXY_HOST);
2138 int port = Settings.Secure.getInt(res, Settings.Secure.GLOBAL_HTTP_PROXY_PORT, 0);
2139 String exclList = Settings.Secure.getString(res,
2140 Settings.Secure.GLOBAL_HTTP_PROXY_EXCLUSION_LIST);
2141 if (!TextUtils.isEmpty(host)) {
2142 ProxyProperties proxyProperties = new ProxyProperties(host, port, exclList);
2143 synchronized (mGlobalProxyLock) {
2144 mGlobalProxy = proxyProperties;
2145 }
2146 }
2147 }
2148
Robert Greenwaltc3c5f862010-10-11 16:00:27 -07002149 public ProxyProperties getGlobalProxy() {
2150 synchronized (mGlobalProxyLock) {
2151 return mGlobalProxy;
2152 }
2153 }
2154
2155 private void handleApplyDefaultProxy(int type) {
2156 // check if new default - push it out to all VM if so
2157 ProxyProperties proxy = mNetTrackers[type].getLinkProperties().getHttpProxy();
2158 synchronized (this) {
2159 if (mDefaultProxy != null && mDefaultProxy.equals(proxy)) return;
2160 if (mDefaultProxy == proxy) return;
2161 if (!TextUtils.isEmpty(proxy.getHost())) {
2162 mDefaultProxy = proxy;
2163 } else {
2164 mDefaultProxy = null;
2165 }
2166 }
Wink Savillee70c6f52010-12-03 12:01:38 -08002167 if (DBG) log("changing default proxy to " + proxy);
Robert Greenwaltc3c5f862010-10-11 16:00:27 -07002168 if ((proxy == null && mGlobalProxy == null) || proxy.equals(mGlobalProxy)) return;
2169 if (mGlobalProxy != null) return;
2170 sendProxyBroadcast(proxy);
2171 }
2172
2173 private void handleDeprecatedGlobalHttpProxy() {
2174 String proxy = Settings.Secure.getString(mContext.getContentResolver(),
2175 Settings.Secure.HTTP_PROXY);
2176 if (!TextUtils.isEmpty(proxy)) {
2177 String data[] = proxy.split(":");
2178 String proxyHost = data[0];
2179 int proxyPort = 8080;
2180 if (data.length > 1) {
2181 try {
2182 proxyPort = Integer.parseInt(data[1]);
2183 } catch (NumberFormatException e) {
2184 return;
2185 }
2186 }
2187 ProxyProperties p = new ProxyProperties(data[0], proxyPort, "");
2188 setGlobalProxy(p);
2189 }
2190 }
2191
2192 private void sendProxyBroadcast(ProxyProperties proxy) {
Robert Greenwalt611291c2010-12-23 15:51:10 -08002193 if (proxy == null) proxy = new ProxyProperties("", 0, "");
Wink Savillee70c6f52010-12-03 12:01:38 -08002194 log("sending Proxy Broadcast for " + proxy);
Robert Greenwaltc3c5f862010-10-11 16:00:27 -07002195 Intent intent = new Intent(Proxy.PROXY_CHANGE_ACTION);
Stan Chesnutt1f2a2ac2011-01-06 11:00:19 -08002196 intent.addFlags(Intent.FLAG_RECEIVER_REPLACE_PENDING |
2197 Intent.FLAG_RECEIVER_REGISTERED_ONLY_BEFORE_BOOT);
Robert Greenwaltc3c5f862010-10-11 16:00:27 -07002198 intent.putExtra(Proxy.EXTRA_PROXY_INFO, proxy);
Robert Greenwaltd93dc8f2010-12-06 11:29:17 -08002199 mContext.sendStickyBroadcast(intent);
Robert Greenwaltc3c5f862010-10-11 16:00:27 -07002200 }
2201
2202 private static class SettingsObserver extends ContentObserver {
2203 private int mWhat;
2204 private Handler mHandler;
2205 SettingsObserver(Handler handler, int what) {
2206 super(handler);
2207 mHandler = handler;
2208 mWhat = what;
2209 }
2210
2211 void observe(Context context) {
2212 ContentResolver resolver = context.getContentResolver();
2213 resolver.registerContentObserver(Settings.Secure.getUriFor(
2214 Settings.Secure.HTTP_PROXY), false, this);
2215 }
2216
2217 @Override
2218 public void onChange(boolean selfChange) {
2219 mHandler.obtainMessage(mWhat).sendToTarget();
2220 }
2221 }
Wink Savillee70c6f52010-12-03 12:01:38 -08002222
Kazuhiro Ondo3a340412011-04-30 20:10:57 -05002223 private void handleTetherIfaceChange(int type) {
2224 String iface = mNetTrackers[type].getLinkProperties().getInterfaceName();
2225
2226 if (isTetheringSupported()) {
2227 mTethering.handleTetherIfaceChange(iface);
2228 }
2229 }
2230
Wink Savillee70c6f52010-12-03 12:01:38 -08002231 private void log(String s) {
2232 Slog.d(TAG, s);
2233 }
2234
2235 private void loge(String s) {
2236 Slog.e(TAG, s);
2237 }
Wink Savillef6b76692011-02-24 17:58:51 -08002238 int convertFeatureToNetworkType(String feature){
2239 int networkType = -1;
2240 if (TextUtils.equals(feature, Phone.FEATURE_ENABLE_MMS)) {
2241 networkType = ConnectivityManager.TYPE_MOBILE_MMS;
2242 } else if (TextUtils.equals(feature, Phone.FEATURE_ENABLE_SUPL)) {
2243 networkType = ConnectivityManager.TYPE_MOBILE_SUPL;
2244 } else if (TextUtils.equals(feature, Phone.FEATURE_ENABLE_DUN) ||
2245 TextUtils.equals(feature, Phone.FEATURE_ENABLE_DUN_ALWAYS)) {
2246 networkType = ConnectivityManager.TYPE_MOBILE_DUN;
2247 } else if (TextUtils.equals(feature, Phone.FEATURE_ENABLE_HIPRI)) {
2248 networkType = ConnectivityManager.TYPE_MOBILE_HIPRI;
2249 } else if (TextUtils.equals(feature, Phone.FEATURE_ENABLE_FOTA)) {
2250 networkType = ConnectivityManager.TYPE_MOBILE_FOTA;
2251 } else if (TextUtils.equals(feature, Phone.FEATURE_ENABLE_IMS)) {
2252 networkType = ConnectivityManager.TYPE_MOBILE_IMS;
2253 } else if (TextUtils.equals(feature, Phone.FEATURE_ENABLE_CBS)) {
2254 networkType = ConnectivityManager.TYPE_MOBILE_CBS;
2255 }
2256 return networkType;
2257 }
The Android Open Source Project28527d22009-03-03 19:31:44 -08002258}