blob: 9158d9e1f4fad87282bac609e9be5462fbe8d519 [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;
42import android.os.Handler;
Wink Saville775aad62010-09-02 19:23:52 -070043import android.os.HandlerThread;
Robert Greenwalt2034b912009-08-12 16:08:25 -070044import android.os.IBinder;
Chia-chi Yeh4df51322011-05-11 16:35:13 -070045import android.os.INetworkManagementService;
The Android Open Source Project28527d22009-03-03 19:31:44 -080046import android.os.Looper;
47import android.os.Message;
Robert Greenwalt93dc1042010-06-15 12:19:37 -070048import android.os.PowerManager;
Robert Greenwalt2034b912009-08-12 16:08:25 -070049import android.os.RemoteException;
The Android Open Source Project28527d22009-03-03 19:31:44 -080050import android.os.ServiceManager;
51import android.os.SystemProperties;
52import android.provider.Settings;
Robert Greenwalt2034b912009-08-12 16:08:25 -070053import android.text.TextUtils;
The Android Open Source Project28527d22009-03-03 19:31:44 -080054import android.util.EventLog;
Joe Onoratoc2386bb2010-02-26 18:56:32 -080055import android.util.Slog;
The Android Open Source Project28527d22009-03-03 19:31:44 -080056
Robert Greenwalt2034b912009-08-12 16:08:25 -070057import com.android.internal.telephony.Phone;
Robert Greenwalt0c4828c2010-01-26 11:40:34 -080058import com.android.server.connectivity.Tethering;
59
The Android Open Source Project28527d22009-03-03 19:31:44 -080060import java.io.FileDescriptor;
Irfan Sheriff7f132d92010-06-09 15:39:36 -070061import java.io.FileWriter;
62import java.io.IOException;
The Android Open Source Project28527d22009-03-03 19:31:44 -080063import java.io.PrintWriter;
Robert Greenwalta7dfbd32010-06-15 15:43:39 -070064import java.net.InetAddress;
Robert Greenwalt355205c2011-05-10 15:05:02 -070065import java.net.Inet4Address;
Robert Greenwalta7dfbd32010-06-15 15:43:39 -070066import java.net.UnknownHostException;
Robert Greenwalt2034b912009-08-12 16:08:25 -070067import java.util.ArrayList;
Robert Greenwalta7dfbd32010-06-15 15:43:39 -070068import java.util.Collection;
Robert Greenwaltd62c7002010-12-29 16:15:02 -080069import java.util.concurrent.atomic.AtomicBoolean;
Robert Greenwalt0e80be12010-09-20 14:35:25 -070070import java.util.GregorianCalendar;
Robert Greenwalt2034b912009-08-12 16:08:25 -070071import java.util.List;
The Android Open Source Project28527d22009-03-03 19:31:44 -080072
73/**
74 * @hide
75 */
76public class ConnectivityService extends IConnectivityManager.Stub {
77
Robert Greenwalt063dc7d2010-10-05 19:12:26 -070078 private static final boolean DBG = true;
The Android Open Source Project28527d22009-03-03 19:31:44 -080079 private static final String TAG = "ConnectivityService";
80
Robert Greenwalt2034b912009-08-12 16:08:25 -070081 // how long to wait before switching back to a radio's default network
82 private static final int RESTORE_DEFAULT_NETWORK_DELAY = 1 * 60 * 1000;
83 // system property that can override the above value
84 private static final String NETWORK_RESTORE_DELAY_PROP_NAME =
85 "android.telephony.apn-restore";
86
Robert Greenwaltbd492212011-05-06 17:10:53 -070087 // used in recursive route setting to add gateways for the host for which
88 // a host route was requested.
89 private static final int MAX_HOSTROUTE_CYCLE_COUNT = 10;
90
Robert Greenwalt0c4828c2010-01-26 11:40:34 -080091 private Tethering mTethering;
Robert Greenwaltf1b66e12010-02-25 12:29:30 -080092 private boolean mTetheringConfigValid = false;
Robert Greenwalt0c4828c2010-01-26 11:40:34 -080093
The Android Open Source Project28527d22009-03-03 19:31:44 -080094 /**
95 * Sometimes we want to refer to the individual network state
96 * trackers separately, and sometimes we just want to treat them
97 * abstractly.
98 */
99 private NetworkStateTracker mNetTrackers[];
Robert Greenwalt2034b912009-08-12 16:08:25 -0700100
101 /**
102 * A per Net list of the PID's that requested access to the net
103 * used both as a refcount and for per-PID DNS selection
104 */
105 private List mNetRequestersPids[];
106
Irfan Sheriff653e2a22010-06-07 09:03:04 -0700107 private WifiWatchdogService mWifiWatchdogService;
108
Robert Greenwalt2034b912009-08-12 16:08:25 -0700109 // priority order of the nettrackers
110 // (excluding dynamically set mNetworkPreference)
111 // TODO - move mNetworkTypePreference into this
112 private int[] mPriorityList;
113
The Android Open Source Project28527d22009-03-03 19:31:44 -0800114 private Context mContext;
115 private int mNetworkPreference;
Robert Greenwalt2034b912009-08-12 16:08:25 -0700116 private int mActiveDefaultNetwork = -1;
Robert Greenwalt986c7412010-09-08 15:24:47 -0700117 // 0 is full bad, 100 is full good
118 private int mDefaultInetCondition = 0;
119 private int mDefaultInetConditionPublished = 0;
120 private boolean mInetConditionChangeInFlight = false;
121 private int mDefaultConnectionSequence = 0;
The Android Open Source Project28527d22009-03-03 19:31:44 -0800122
123 private int mNumDnsEntries;
The Android Open Source Project28527d22009-03-03 19:31:44 -0800124
125 private boolean mTestMode;
Joe Onorato56023ad2010-09-01 21:18:22 -0700126 private static ConnectivityService sServiceInstance;
The Android Open Source Project28527d22009-03-03 19:31:44 -0800127
Robert Greenwaltd62c7002010-12-29 16:15:02 -0800128 private AtomicBoolean mBackgroundDataEnabled = new AtomicBoolean(true);
129
Robert Greenwalt355205c2011-05-10 15:05:02 -0700130 private INetworkManagementService mNetd;
131
Robert Greenwalt6a2db8a2010-09-23 10:05:56 -0700132 private static final int ENABLED = 1;
133 private static final int DISABLED = 0;
134
135 // Share the event space with NetworkStateTracker (which can't see this
136 // internal class but sends us events). If you change these, change
137 // NetworkStateTracker.java too.
138 private static final int MIN_NETWORK_STATE_TRACKER_EVENT = 1;
139 private static final int MAX_NETWORK_STATE_TRACKER_EVENT = 100;
140
141 /**
142 * used internally as a delayed event to make us switch back to the
143 * default network
144 */
145 private static final int EVENT_RESTORE_DEFAULT_NETWORK =
146 MAX_NETWORK_STATE_TRACKER_EVENT + 1;
147
148 /**
149 * used internally to change our mobile data enabled flag
150 */
151 private static final int EVENT_CHANGE_MOBILE_DATA_ENABLED =
152 MAX_NETWORK_STATE_TRACKER_EVENT + 2;
153
154 /**
155 * used internally to change our network preference setting
156 * arg1 = networkType to prefer
157 */
158 private static final int EVENT_SET_NETWORK_PREFERENCE =
159 MAX_NETWORK_STATE_TRACKER_EVENT + 3;
160
161 /**
162 * used internally to synchronize inet condition reports
163 * arg1 = networkType
164 * arg2 = condition (0 bad, 100 good)
165 */
166 private static final int EVENT_INET_CONDITION_CHANGE =
167 MAX_NETWORK_STATE_TRACKER_EVENT + 4;
168
169 /**
170 * used internally to mark the end of inet condition hold periods
171 * arg1 = networkType
172 */
173 private static final int EVENT_INET_CONDITION_HOLD_END =
174 MAX_NETWORK_STATE_TRACKER_EVENT + 5;
175
176 /**
177 * used internally to set the background data preference
178 * arg1 = TRUE for enabled, FALSE for disabled
179 */
180 private static final int EVENT_SET_BACKGROUND_DATA =
181 MAX_NETWORK_STATE_TRACKER_EVENT + 6;
182
183 /**
184 * used internally to set enable/disable cellular data
185 * arg1 = ENBALED or DISABLED
186 */
187 private static final int EVENT_SET_MOBILE_DATA =
188 MAX_NETWORK_STATE_TRACKER_EVENT + 7;
189
Robert Greenwaltccb36f92010-09-24 14:32:21 -0700190 /**
191 * used internally to clear a wakelock when transitioning
192 * from one net to another
193 */
194 private static final int EVENT_CLEAR_NET_TRANSITION_WAKELOCK =
195 MAX_NETWORK_STATE_TRACKER_EVENT + 8;
196
Robert Greenwaltc3c5f862010-10-11 16:00:27 -0700197 /**
198 * used internally to reload global proxy settings
199 */
200 private static final int EVENT_APPLY_GLOBAL_HTTP_PROXY =
201 MAX_NETWORK_STATE_TRACKER_EVENT + 9;
202
Robert Greenwalt34848c02011-03-25 13:09:25 -0700203 /**
204 * used internally to set external dependency met/unmet
205 * arg1 = ENABLED (met) or DISABLED (unmet)
206 * arg2 = NetworkType
207 */
208 private static final int EVENT_SET_DEPENDENCY_MET =
209 MAX_NETWORK_STATE_TRACKER_EVENT + 10;
210
Robert Greenwalt2034b912009-08-12 16:08:25 -0700211 private Handler mHandler;
212
213 // list of DeathRecipients used to make sure features are turned off when
214 // a process dies
215 private List mFeatureUsers;
216
Mike Lockwoodfde2b762009-08-14 14:18:49 -0400217 private boolean mSystemReady;
Dianne Hackborna417ff82009-12-08 19:45:14 -0800218 private Intent mInitialBroadcast;
Mike Lockwoodfde2b762009-08-14 14:18:49 -0400219
Robert Greenwalt93dc1042010-06-15 12:19:37 -0700220 private PowerManager.WakeLock mNetTransitionWakeLock;
221 private String mNetTransitionWakeLockCausedBy = "";
222 private int mNetTransitionWakeLockSerialNumber;
223 private int mNetTransitionWakeLockTimeout;
224
Robert Greenwalt94daa182010-09-01 11:34:05 -0700225 private InetAddress mDefaultDns;
226
Robert Greenwalt0e80be12010-09-20 14:35:25 -0700227 // used in DBG mode to track inet condition reports
228 private static final int INET_CONDITION_LOG_MAX_SIZE = 15;
229 private ArrayList mInetLog;
230
Robert Greenwaltc3c5f862010-10-11 16:00:27 -0700231 // track the current default http proxy - tell the world if we get a new one (real change)
232 private ProxyProperties mDefaultProxy = null;
233 // track the global proxy.
234 private ProxyProperties mGlobalProxy = null;
235 private final Object mGlobalProxyLock = new Object();
236
237 private SettingsObserver mSettingsObserver;
238
Robert Greenwalt34848c02011-03-25 13:09:25 -0700239 NetworkConfig[] mNetConfigs;
Robert Greenwaltec05b3c2009-10-30 14:17:42 -0700240 int mNetworksDefined;
Robert Greenwalt2034b912009-08-12 16:08:25 -0700241
Robert Greenwalt12c44552009-12-07 11:33:18 -0800242 private static class RadioAttributes {
Robert Greenwalt2034b912009-08-12 16:08:25 -0700243 public int mSimultaneity;
244 public int mType;
245 public RadioAttributes(String init) {
246 String fragments[] = init.split(",");
Robert Greenwaltec05b3c2009-10-30 14:17:42 -0700247 mType = Integer.parseInt(fragments[0]);
248 mSimultaneity = Integer.parseInt(fragments[1]);
Robert Greenwalt2034b912009-08-12 16:08:25 -0700249 }
250 }
251 RadioAttributes[] mRadioAttributes;
252
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
Chia-chi Yehf3204aa2011-05-23 15:08:29 -0700444 try {
445 nmService.registerObserver(mTethering);
446 } catch (RemoteException e) {
447 loge("Error registering observer :" + e);
448 }
449
Robert Greenwalt0e80be12010-09-20 14:35:25 -0700450 if (DBG) {
451 mInetLog = new ArrayList();
452 }
Robert Greenwaltc3c5f862010-10-11 16:00:27 -0700453
454 mSettingsObserver = new SettingsObserver(mHandler, EVENT_APPLY_GLOBAL_HTTP_PROXY);
455 mSettingsObserver.observe(mContext);
Robert Greenwalt6f7c6092010-12-02 11:31:00 -0800456
457 loadGlobalProxy();
Hung-ying Tyan4e723422011-01-19 16:48:38 +0800458
459 VpnManager.startVpnService(context);
The Android Open Source Project28527d22009-03-03 19:31:44 -0800460 }
461
Robert Greenwaltec05b3c2009-10-30 14:17:42 -0700462
The Android Open Source Project28527d22009-03-03 19:31:44 -0800463 /**
Robert Greenwalt0659da32009-07-16 17:21:39 -0700464 * Sets the preferred network.
The Android Open Source Project28527d22009-03-03 19:31:44 -0800465 * @param preference the new preference
466 */
Robert Greenwalt6a2db8a2010-09-23 10:05:56 -0700467 public void setNetworkPreference(int preference) {
The Android Open Source Project28527d22009-03-03 19:31:44 -0800468 enforceChangePermission();
Robert Greenwalt6a2db8a2010-09-23 10:05:56 -0700469
470 mHandler.sendMessage(mHandler.obtainMessage(EVENT_SET_NETWORK_PREFERENCE, preference, 0));
The Android Open Source Project28527d22009-03-03 19:31:44 -0800471 }
472
473 public int getNetworkPreference() {
474 enforceAccessPermission();
Robert Greenwalt6a2db8a2010-09-23 10:05:56 -0700475 int preference;
476 synchronized(this) {
477 preference = mNetworkPreference;
478 }
479 return preference;
The Android Open Source Project28527d22009-03-03 19:31:44 -0800480 }
481
Robert Greenwalt6a2db8a2010-09-23 10:05:56 -0700482 private void handleSetNetworkPreference(int preference) {
483 if (ConnectivityManager.isNetworkTypeValid(preference) &&
Robert Greenwalt34848c02011-03-25 13:09:25 -0700484 mNetConfigs[preference] != null &&
485 mNetConfigs[preference].isDefault()) {
Robert Greenwalt6a2db8a2010-09-23 10:05:56 -0700486 if (mNetworkPreference != preference) {
487 final ContentResolver cr = mContext.getContentResolver();
488 Settings.Secure.putInt(cr, Settings.Secure.NETWORK_PREFERENCE, preference);
489 synchronized(this) {
490 mNetworkPreference = preference;
491 }
492 enforcePreference();
493 }
494 }
The Android Open Source Project28527d22009-03-03 19:31:44 -0800495 }
Robert Greenwalt0659da32009-07-16 17:21:39 -0700496
The Android Open Source Project28527d22009-03-03 19:31:44 -0800497 private int getPersistedNetworkPreference() {
498 final ContentResolver cr = mContext.getContentResolver();
499
500 final int networkPrefSetting = Settings.Secure
501 .getInt(cr, Settings.Secure.NETWORK_PREFERENCE, -1);
502 if (networkPrefSetting != -1) {
503 return networkPrefSetting;
504 }
505
506 return ConnectivityManager.DEFAULT_NETWORK_PREFERENCE;
507 }
Robert Greenwalt0659da32009-07-16 17:21:39 -0700508
The Android Open Source Project28527d22009-03-03 19:31:44 -0800509 /**
Robert Greenwalt0659da32009-07-16 17:21:39 -0700510 * Make the state of network connectivity conform to the preference settings
The Android Open Source Project28527d22009-03-03 19:31:44 -0800511 * In this method, we only tear down a non-preferred network. Establishing
512 * a connection to the preferred network is taken care of when we handle
513 * the disconnect event from the non-preferred network
514 * (see {@link #handleDisconnect(NetworkInfo)}).
515 */
516 private void enforcePreference() {
Robert Greenwalt2034b912009-08-12 16:08:25 -0700517 if (mNetTrackers[mNetworkPreference].getNetworkInfo().isConnected())
The Android Open Source Project28527d22009-03-03 19:31:44 -0800518 return;
519
Robert Greenwalt2034b912009-08-12 16:08:25 -0700520 if (!mNetTrackers[mNetworkPreference].isAvailable())
521 return;
The Android Open Source Project28527d22009-03-03 19:31:44 -0800522
Robert Greenwalt2034b912009-08-12 16:08:25 -0700523 for (int t=0; t <= ConnectivityManager.MAX_RADIO_TYPE; t++) {
Robert Greenwaltec05b3c2009-10-30 14:17:42 -0700524 if (t != mNetworkPreference && mNetTrackers[t] != null &&
Robert Greenwalt2034b912009-08-12 16:08:25 -0700525 mNetTrackers[t].getNetworkInfo().isConnected()) {
Robert Greenwaltf3f045b2009-08-20 15:25:14 -0700526 if (DBG) {
Wink Savillee70c6f52010-12-03 12:01:38 -0800527 log("tearing down " + mNetTrackers[t].getNetworkInfo() +
Robert Greenwaltf3f045b2009-08-20 15:25:14 -0700528 " in enforcePreference");
529 }
Robert Greenwalt2034b912009-08-12 16:08:25 -0700530 teardown(mNetTrackers[t]);
The Android Open Source Project28527d22009-03-03 19:31:44 -0800531 }
532 }
533 }
534
535 private boolean teardown(NetworkStateTracker netTracker) {
536 if (netTracker.teardown()) {
537 netTracker.setTeardownRequested(true);
538 return true;
539 } else {
540 return false;
541 }
542 }
543
544 /**
545 * Return NetworkInfo for the active (i.e., connected) network interface.
546 * It is assumed that at most one network is active at a time. If more
547 * than one is active, it is indeterminate which will be returned.
Robert Greenwalt0659da32009-07-16 17:21:39 -0700548 * @return the info for the active network, or {@code null} if none is
549 * active
The Android Open Source Project28527d22009-03-03 19:31:44 -0800550 */
551 public NetworkInfo getActiveNetworkInfo() {
Robert Greenwalte1544bb2011-05-20 12:23:41 -0700552 return getNetworkInfo(mActiveDefaultNetwork);
The Android Open Source Project28527d22009-03-03 19:31:44 -0800553 }
554
555 public NetworkInfo getNetworkInfo(int networkType) {
556 enforceAccessPermission();
557 if (ConnectivityManager.isNetworkTypeValid(networkType)) {
558 NetworkStateTracker t = mNetTrackers[networkType];
559 if (t != null)
560 return t.getNetworkInfo();
561 }
562 return null;
563 }
564
565 public NetworkInfo[] getAllNetworkInfo() {
566 enforceAccessPermission();
Robert Greenwaltec05b3c2009-10-30 14:17:42 -0700567 NetworkInfo[] result = new NetworkInfo[mNetworksDefined];
The Android Open Source Project28527d22009-03-03 19:31:44 -0800568 int i = 0;
569 for (NetworkStateTracker t : mNetTrackers) {
Robert Greenwaltec05b3c2009-10-30 14:17:42 -0700570 if(t != null) result[i++] = t.getNetworkInfo();
The Android Open Source Project28527d22009-03-03 19:31:44 -0800571 }
572 return result;
573 }
574
Robert Greenwalt9f0ee4f2010-09-14 09:18:02 -0700575 /**
576 * Return LinkProperties for the active (i.e., connected) default
577 * network interface. It is assumed that at most one default network
578 * is active at a time. If more than one is active, it is indeterminate
579 * which will be returned.
580 * @return the ip properties for the active network, or {@code null} if
581 * none is active
582 */
583 public LinkProperties getActiveLinkProperties() {
Robert Greenwalte1544bb2011-05-20 12:23:41 -0700584 return getLinkProperties(mActiveDefaultNetwork);
Robert Greenwalt9f0ee4f2010-09-14 09:18:02 -0700585 }
586
587 public LinkProperties getLinkProperties(int networkType) {
588 enforceAccessPermission();
589 if (ConnectivityManager.isNetworkTypeValid(networkType)) {
590 NetworkStateTracker t = mNetTrackers[networkType];
591 if (t != null) return t.getLinkProperties();
592 }
593 return null;
594 }
595
The Android Open Source Project28527d22009-03-03 19:31:44 -0800596 public boolean setRadios(boolean turnOn) {
597 boolean result = true;
598 enforceChangePermission();
599 for (NetworkStateTracker t : mNetTrackers) {
Robert Greenwaltec05b3c2009-10-30 14:17:42 -0700600 if (t != null) result = t.setRadio(turnOn) && result;
The Android Open Source Project28527d22009-03-03 19:31:44 -0800601 }
602 return result;
603 }
604
605 public boolean setRadio(int netType, boolean turnOn) {
606 enforceChangePermission();
607 if (!ConnectivityManager.isNetworkTypeValid(netType)) {
608 return false;
609 }
610 NetworkStateTracker tracker = mNetTrackers[netType];
611 return tracker != null && tracker.setRadio(turnOn);
612 }
613
Robert Greenwaltaffc3a12009-09-27 17:27:04 -0700614 /**
615 * Used to notice when the calling process dies so we can self-expire
616 *
617 * Also used to know if the process has cleaned up after itself when
618 * our auto-expire timer goes off. The timer has a link to an object.
619 *
620 */
Robert Greenwalt2034b912009-08-12 16:08:25 -0700621 private class FeatureUser implements IBinder.DeathRecipient {
622 int mNetworkType;
623 String mFeature;
624 IBinder mBinder;
625 int mPid;
626 int mUid;
Robert Greenwalt3eeb6032009-12-21 18:24:07 -0800627 long mCreateTime;
Robert Greenwalt2034b912009-08-12 16:08:25 -0700628
629 FeatureUser(int type, String feature, IBinder binder) {
630 super();
631 mNetworkType = type;
632 mFeature = feature;
633 mBinder = binder;
634 mPid = getCallingPid();
635 mUid = getCallingUid();
Robert Greenwalt3eeb6032009-12-21 18:24:07 -0800636 mCreateTime = System.currentTimeMillis();
Robert Greenwaltaffc3a12009-09-27 17:27:04 -0700637
Robert Greenwalt2034b912009-08-12 16:08:25 -0700638 try {
639 mBinder.linkToDeath(this, 0);
640 } catch (RemoteException e) {
641 binderDied();
642 }
643 }
644
645 void unlinkDeathRecipient() {
646 mBinder.unlinkToDeath(this, 0);
647 }
648
649 public void binderDied() {
Wink Savillee70c6f52010-12-03 12:01:38 -0800650 log("ConnectivityService FeatureUser binderDied(" +
Robert Greenwalt3eeb6032009-12-21 18:24:07 -0800651 mNetworkType + ", " + mFeature + ", " + mBinder + "), created " +
652 (System.currentTimeMillis() - mCreateTime) + " mSec ago");
Robert Greenwaltaffc3a12009-09-27 17:27:04 -0700653 stopUsingNetworkFeature(this, false);
Robert Greenwalt2034b912009-08-12 16:08:25 -0700654 }
655
Robert Greenwaltaffc3a12009-09-27 17:27:04 -0700656 public void expire() {
Wink Savillee70c6f52010-12-03 12:01:38 -0800657 log("ConnectivityService FeatureUser expire(" +
Robert Greenwalt3eeb6032009-12-21 18:24:07 -0800658 mNetworkType + ", " + mFeature + ", " + mBinder +"), created " +
659 (System.currentTimeMillis() - mCreateTime) + " mSec ago");
Robert Greenwaltaffc3a12009-09-27 17:27:04 -0700660 stopUsingNetworkFeature(this, false);
661 }
Robert Greenwalt3eeb6032009-12-21 18:24:07 -0800662
663 public String toString() {
664 return "FeatureUser("+mNetworkType+","+mFeature+","+mPid+","+mUid+"), created " +
665 (System.currentTimeMillis() - mCreateTime) + " mSec ago";
666 }
Robert Greenwalt2034b912009-08-12 16:08:25 -0700667 }
668
Robert Greenwaltaffc3a12009-09-27 17:27:04 -0700669 // javadoc from interface
Robert Greenwalt2034b912009-08-12 16:08:25 -0700670 public int startUsingNetworkFeature(int networkType, String feature,
671 IBinder binder) {
672 if (DBG) {
Wink Savillee70c6f52010-12-03 12:01:38 -0800673 log("startUsingNetworkFeature for net " + networkType + ": " + feature);
Robert Greenwalt2034b912009-08-12 16:08:25 -0700674 }
The Android Open Source Project28527d22009-03-03 19:31:44 -0800675 enforceChangePermission();
Robert Greenwaltec05b3c2009-10-30 14:17:42 -0700676 if (!ConnectivityManager.isNetworkTypeValid(networkType) ||
Robert Greenwalt34848c02011-03-25 13:09:25 -0700677 mNetConfigs[networkType] == null) {
Robert Greenwalt2034b912009-08-12 16:08:25 -0700678 return Phone.APN_REQUEST_FAILED;
The Android Open Source Project28527d22009-03-03 19:31:44 -0800679 }
Robert Greenwalt2034b912009-08-12 16:08:25 -0700680
Robert Greenwaltaffc3a12009-09-27 17:27:04 -0700681 FeatureUser f = new FeatureUser(networkType, feature, binder);
Robert Greenwalt2034b912009-08-12 16:08:25 -0700682
683 // TODO - move this into the MobileDataStateTracker
684 int usedNetworkType = networkType;
685 if(networkType == ConnectivityManager.TYPE_MOBILE) {
Wink Savillef6b76692011-02-24 17:58:51 -0800686 usedNetworkType = convertFeatureToNetworkType(feature);
687 if (usedNetworkType < 0) {
688 Slog.e(TAG, "Can't match any netTracker!");
689 usedNetworkType = networkType;
Robert Greenwalt2034b912009-08-12 16:08:25 -0700690 }
691 }
692 NetworkStateTracker network = mNetTrackers[usedNetworkType];
693 if (network != null) {
Robert Greenwalt5364d752010-12-15 13:26:33 -0800694 Integer currentPid = new Integer(getCallingPid());
Robert Greenwalt2034b912009-08-12 16:08:25 -0700695 if (usedNetworkType != networkType) {
Robert Greenwalt2034b912009-08-12 16:08:25 -0700696 NetworkStateTracker radio = mNetTrackers[networkType];
697 NetworkInfo ni = network.getNetworkInfo();
698
699 if (ni.isAvailable() == false) {
Wink Savillee70c6f52010-12-03 12:01:38 -0800700 if (DBG) log("special network not available");
Robert Greenwalt2cc87442010-12-29 14:35:21 -0800701 if (!TextUtils.equals(feature,Phone.FEATURE_ENABLE_DUN_ALWAYS)) {
702 return Phone.APN_TYPE_NOT_AVAILABLE;
703 } else {
704 // else make the attempt anyway - probably giving REQUEST_STARTED below
705 }
Robert Greenwalt2034b912009-08-12 16:08:25 -0700706 }
707
Robert Greenwaltaffc3a12009-09-27 17:27:04 -0700708 synchronized(this) {
709 mFeatureUsers.add(f);
710 if (!mNetRequestersPids[usedNetworkType].contains(currentPid)) {
711 // this gets used for per-pid dns when connected
712 mNetRequestersPids[usedNetworkType].add(currentPid);
713 }
Robert Greenwalt2034b912009-08-12 16:08:25 -0700714 }
Robert Greenwaltaffc3a12009-09-27 17:27:04 -0700715
Robert Greenwalt20f819c2011-05-03 19:02:44 -0700716 int restoreTimer = getRestoreDefaultNetworkDelay(usedNetworkType);
717
718 if (restoreTimer >= 0) {
719 mHandler.sendMessageDelayed(
720 mHandler.obtainMessage(EVENT_RESTORE_DEFAULT_NETWORK, f), restoreTimer);
721 }
Robert Greenwalt2034b912009-08-12 16:08:25 -0700722
Robert Greenwalta52c75a2009-08-19 20:19:33 -0700723 if ((ni.isConnectedOrConnecting() == true) &&
724 !network.isTeardownRequested()) {
Robert Greenwalt2034b912009-08-12 16:08:25 -0700725 if (ni.isConnected() == true) {
726 // add the pid-specific dns
Robert Greenwalt3afbead2010-07-23 15:46:26 -0700727 handleDnsConfigurationChange(networkType);
Wink Savillee70c6f52010-12-03 12:01:38 -0800728 if (DBG) log("special network already active");
Robert Greenwalt2034b912009-08-12 16:08:25 -0700729 return Phone.APN_ALREADY_ACTIVE;
730 }
Wink Savillee70c6f52010-12-03 12:01:38 -0800731 if (DBG) log("special network already connecting");
Robert Greenwalt2034b912009-08-12 16:08:25 -0700732 return Phone.APN_REQUEST_STARTED;
733 }
734
735 // check if the radio in play can make another contact
736 // assume if cannot for now
737
Wink Savillee70c6f52010-12-03 12:01:38 -0800738 if (DBG) log("reconnecting to special network");
Robert Greenwalt2034b912009-08-12 16:08:25 -0700739 network.reconnect();
740 return Phone.APN_REQUEST_STARTED;
741 } else {
Robert Greenwalt5364d752010-12-15 13:26:33 -0800742 // need to remember this unsupported request so we respond appropriately on stop
743 synchronized(this) {
744 mFeatureUsers.add(f);
745 if (!mNetRequestersPids[usedNetworkType].contains(currentPid)) {
746 // this gets used for per-pid dns when connected
747 mNetRequestersPids[usedNetworkType].add(currentPid);
748 }
749 }
Robert Greenwaltd391e892010-05-18 10:52:51 -0700750 return -1;
Robert Greenwalt2034b912009-08-12 16:08:25 -0700751 }
752 }
753 return Phone.APN_TYPE_NOT_AVAILABLE;
The Android Open Source Project28527d22009-03-03 19:31:44 -0800754 }
755
Robert Greenwaltaffc3a12009-09-27 17:27:04 -0700756 // javadoc from interface
The Android Open Source Project28527d22009-03-03 19:31:44 -0800757 public int stopUsingNetworkFeature(int networkType, String feature) {
Robert Greenwalt28f43012009-10-06 17:52:40 -0700758 enforceChangePermission();
759
Robert Greenwaltaffc3a12009-09-27 17:27:04 -0700760 int pid = getCallingPid();
761 int uid = getCallingUid();
762
763 FeatureUser u = null;
764 boolean found = false;
765
766 synchronized(this) {
767 for (int i = 0; i < mFeatureUsers.size() ; i++) {
768 u = (FeatureUser)mFeatureUsers.get(i);
769 if (uid == u.mUid && pid == u.mPid &&
770 networkType == u.mNetworkType &&
771 TextUtils.equals(feature, u.mFeature)) {
772 found = true;
773 break;
774 }
775 }
776 }
777 if (found && u != null) {
778 // stop regardless of how many other time this proc had called start
779 return stopUsingNetworkFeature(u, true);
780 } else {
781 // none found!
Wink Savillee70c6f52010-12-03 12:01:38 -0800782 if (DBG) log("ignoring stopUsingNetworkFeature - not a live request");
Robert Greenwaltaffc3a12009-09-27 17:27:04 -0700783 return 1;
784 }
Robert Greenwalt2034b912009-08-12 16:08:25 -0700785 }
786
Robert Greenwaltaffc3a12009-09-27 17:27:04 -0700787 private int stopUsingNetworkFeature(FeatureUser u, boolean ignoreDups) {
788 int networkType = u.mNetworkType;
789 String feature = u.mFeature;
790 int pid = u.mPid;
791 int uid = u.mUid;
792
793 NetworkStateTracker tracker = null;
794 boolean callTeardown = false; // used to carry our decision outside of sync block
795
Robert Greenwalt2034b912009-08-12 16:08:25 -0700796 if (DBG) {
Wink Savillee70c6f52010-12-03 12:01:38 -0800797 log("stopUsingNetworkFeature for net " + networkType +
Robert Greenwalt2034b912009-08-12 16:08:25 -0700798 ": " + feature);
799 }
Robert Greenwalt28f43012009-10-06 17:52:40 -0700800
The Android Open Source Project28527d22009-03-03 19:31:44 -0800801 if (!ConnectivityManager.isNetworkTypeValid(networkType)) {
802 return -1;
803 }
Robert Greenwalt2034b912009-08-12 16:08:25 -0700804
Robert Greenwaltaffc3a12009-09-27 17:27:04 -0700805 // need to link the mFeatureUsers list with the mNetRequestersPids state in this
806 // sync block
807 synchronized(this) {
808 // check if this process still has an outstanding start request
809 if (!mFeatureUsers.contains(u)) {
Wink Savillee70c6f52010-12-03 12:01:38 -0800810 if (DBG) log("ignoring - this process has no outstanding requests");
Robert Greenwalt2034b912009-08-12 16:08:25 -0700811 return 1;
812 }
Robert Greenwaltaffc3a12009-09-27 17:27:04 -0700813 u.unlinkDeathRecipient();
814 mFeatureUsers.remove(mFeatureUsers.indexOf(u));
815 // If we care about duplicate requests, check for that here.
816 //
817 // This is done to support the extension of a request - the app
818 // can request we start the network feature again and renew the
819 // auto-shutoff delay. Normal "stop" calls from the app though
820 // do not pay attention to duplicate requests - in effect the
821 // API does not refcount and a single stop will counter multiple starts.
822 if (ignoreDups == false) {
823 for (int i = 0; i < mFeatureUsers.size() ; i++) {
824 FeatureUser x = (FeatureUser)mFeatureUsers.get(i);
825 if (x.mUid == u.mUid && x.mPid == u.mPid &&
826 x.mNetworkType == u.mNetworkType &&
827 TextUtils.equals(x.mFeature, u.mFeature)) {
Wink Savillee70c6f52010-12-03 12:01:38 -0800828 if (DBG) log("ignoring stopUsingNetworkFeature as dup is found");
Robert Greenwaltaffc3a12009-09-27 17:27:04 -0700829 return 1;
830 }
831 }
Robert Greenwalt2034b912009-08-12 16:08:25 -0700832 }
Robert Greenwaltaffc3a12009-09-27 17:27:04 -0700833
834 // TODO - move to MobileDataStateTracker
835 int usedNetworkType = networkType;
836 if (networkType == ConnectivityManager.TYPE_MOBILE) {
Wink Savillef6b76692011-02-24 17:58:51 -0800837 usedNetworkType = convertFeatureToNetworkType(feature);
838 if (usedNetworkType < 0) {
839 usedNetworkType = networkType;
Robert Greenwaltaffc3a12009-09-27 17:27:04 -0700840 }
841 }
842 tracker = mNetTrackers[usedNetworkType];
Robert Greenwaltec05b3c2009-10-30 14:17:42 -0700843 if (tracker == null) {
Wink Savillee70c6f52010-12-03 12:01:38 -0800844 if (DBG) log("ignoring - no known tracker for net type " + usedNetworkType);
Robert Greenwaltec05b3c2009-10-30 14:17:42 -0700845 return -1;
846 }
847 if (usedNetworkType != networkType) {
Robert Greenwaltaffc3a12009-09-27 17:27:04 -0700848 Integer currentPid = new Integer(pid);
Robert Greenwaltaffc3a12009-09-27 17:27:04 -0700849 mNetRequestersPids[usedNetworkType].remove(currentPid);
Robert Greenwalt0ca68a02009-12-17 14:54:59 -0800850 reassessPidDns(pid, true);
Robert Greenwaltaffc3a12009-09-27 17:27:04 -0700851 if (mNetRequestersPids[usedNetworkType].size() != 0) {
Wink Savillee70c6f52010-12-03 12:01:38 -0800852 if (DBG) log("not tearing down special network - " +
Robert Greenwaltaffc3a12009-09-27 17:27:04 -0700853 "others still using it");
854 return 1;
855 }
856 callTeardown = true;
Robert Greenwalt9f3be4c2011-01-10 11:58:31 -0800857 } else {
858 if (DBG) log("not a known feature - dropping");
Robert Greenwaltaffc3a12009-09-27 17:27:04 -0700859 }
860 }
Wink Savillee70c6f52010-12-03 12:01:38 -0800861 if (DBG) log("Doing network teardown");
Robert Greenwaltaffc3a12009-09-27 17:27:04 -0700862 if (callTeardown) {
863 tracker.teardown();
Robert Greenwalt2034b912009-08-12 16:08:25 -0700864 return 1;
865 } else {
Robert Greenwaltd391e892010-05-18 10:52:51 -0700866 return -1;
Robert Greenwalt2034b912009-08-12 16:08:25 -0700867 }
The Android Open Source Project28527d22009-03-03 19:31:44 -0800868 }
869
870 /**
Robert Greenwalt7fe44cb2010-08-27 09:24:29 -0700871 * @deprecated use requestRouteToHostAddress instead
872 *
The Android Open Source Project28527d22009-03-03 19:31:44 -0800873 * Ensure that a network route exists to deliver traffic to the specified
874 * host via the specified network interface.
Robert Greenwalt0659da32009-07-16 17:21:39 -0700875 * @param networkType the type of the network over which traffic to the
876 * specified host is to be routed
877 * @param hostAddress the IP address of the host to which the route is
878 * desired
The Android Open Source Project28527d22009-03-03 19:31:44 -0800879 * @return {@code true} on success, {@code false} on failure
880 */
881 public boolean requestRouteToHost(int networkType, int hostAddress) {
Robert Greenwalt7fe44cb2010-08-27 09:24:29 -0700882 InetAddress inetAddress = NetworkUtils.intToInetAddress(hostAddress);
883
884 if (inetAddress == null) {
885 return false;
886 }
887
888 return requestRouteToHostAddress(networkType, inetAddress.getAddress());
889 }
890
891 /**
892 * Ensure that a network route exists to deliver traffic to the specified
893 * host via the specified network interface.
894 * @param networkType the type of the network over which traffic to the
895 * specified host is to be routed
896 * @param hostAddress the IP address of the host to which the route is
897 * desired
898 * @return {@code true} on success, {@code false} on failure
899 */
900 public boolean requestRouteToHostAddress(int networkType, byte[] hostAddress) {
The Android Open Source Project28527d22009-03-03 19:31:44 -0800901 enforceChangePermission();
902 if (!ConnectivityManager.isNetworkTypeValid(networkType)) {
903 return false;
904 }
905 NetworkStateTracker tracker = mNetTrackers[networkType];
Robert Greenwalt4666ed02009-09-10 15:06:20 -0700906
Robert Greenwaltec05b3c2009-10-30 14:17:42 -0700907 if (tracker == null || !tracker.getNetworkInfo().isConnected() ||
908 tracker.isTeardownRequested()) {
Robert Greenwalt4666ed02009-09-10 15:06:20 -0700909 if (DBG) {
Wink Savillee70c6f52010-12-03 12:01:38 -0800910 log("requestRouteToHostAddress on down network " +
Robert Greenwalt7fe44cb2010-08-27 09:24:29 -0700911 "(" + networkType + ") - dropped");
Robert Greenwalt4666ed02009-09-10 15:06:20 -0700912 }
913 return false;
The Android Open Source Project28527d22009-03-03 19:31:44 -0800914 }
Robert Greenwalta7dfbd32010-06-15 15:43:39 -0700915 try {
Robert Greenwalt7fe44cb2010-08-27 09:24:29 -0700916 InetAddress addr = InetAddress.getByAddress(hostAddress);
Robert Greenwaltbd492212011-05-06 17:10:53 -0700917 return addHostRoute(tracker, addr, 0);
Robert Greenwalta7dfbd32010-06-15 15:43:39 -0700918 } catch (UnknownHostException e) {}
919 return false;
Irfan Sheriff7f132d92010-06-09 15:39:36 -0700920 }
921
922 /**
923 * Ensure that a network route exists to deliver traffic to the specified
924 * host via the mobile data network.
925 * @param hostAddress the IP address of the host to which the route is desired,
926 * in network byte order.
Robert Greenwalta7dfbd32010-06-15 15:43:39 -0700927 * TODO - deprecate
Irfan Sheriff7f132d92010-06-09 15:39:36 -0700928 * @return {@code true} on success, {@code false} on failure
929 */
Robert Greenwaltbd492212011-05-06 17:10:53 -0700930 private boolean addHostRoute(NetworkStateTracker nt, InetAddress hostAddress, int cycleCount) {
Robert Greenwaltbd492212011-05-06 17:10:53 -0700931 LinkProperties lp = nt.getLinkProperties();
932 if ((lp == null) || (hostAddress == null)) return false;
Irfan Sheriff7f132d92010-06-09 15:39:36 -0700933
Robert Greenwaltbd492212011-05-06 17:10:53 -0700934 String interfaceName = lp.getInterfaceName();
Irfan Sheriff7f132d92010-06-09 15:39:36 -0700935 if (DBG) {
Robert Greenwaltbd492212011-05-06 17:10:53 -0700936 log("Requested host route to " + hostAddress + "(" + interfaceName + "), cycleCount=" +
937 cycleCount);
Irfan Sheriff7f132d92010-06-09 15:39:36 -0700938 }
Robert Greenwaltbd492212011-05-06 17:10:53 -0700939 if (interfaceName == null) {
Wink Savillee70c6f52010-12-03 12:01:38 -0800940 if (DBG) loge("addHostRoute failed due to null interface name");
Irfan Sheriff7f132d92010-06-09 15:39:36 -0700941 return false;
942 }
Robert Greenwaltbd492212011-05-06 17:10:53 -0700943
944 RouteInfo bestRoute = RouteInfo.selectBestRoute(lp.getRoutes(), hostAddress);
Robert Greenwalt355205c2011-05-10 15:05:02 -0700945 InetAddress gatewayAddress = null;
Robert Greenwaltbd492212011-05-06 17:10:53 -0700946 if (bestRoute != null) {
Robert Greenwalt355205c2011-05-10 15:05:02 -0700947 gatewayAddress = bestRoute.getGateway();
Robert Greenwaltbd492212011-05-06 17:10:53 -0700948 // if the best route is ourself, don't relf-reference, just add the host route
Robert Greenwalt355205c2011-05-10 15:05:02 -0700949 if (hostAddress.equals(gatewayAddress)) gatewayAddress = null;
Robert Greenwaltbd492212011-05-06 17:10:53 -0700950 }
Robert Greenwalt355205c2011-05-10 15:05:02 -0700951 if (gatewayAddress != null) {
Robert Greenwaltbd492212011-05-06 17:10:53 -0700952 if (cycleCount > MAX_HOSTROUTE_CYCLE_COUNT) {
953 loge("Error adding hostroute - too much recursion");
954 return false;
955 }
Robert Greenwalt355205c2011-05-10 15:05:02 -0700956 if (!addHostRoute(nt, gatewayAddress, cycleCount+1)) return false;
Robert Greenwaltbd492212011-05-06 17:10:53 -0700957 }
Robert Greenwalt355205c2011-05-10 15:05:02 -0700958
959 RouteInfo route = RouteInfo.makeHostRoute(hostAddress, gatewayAddress);
960
961 try {
962 mNetd.addRoute(interfaceName, route);
963 return true;
964 } catch (Exception ex) {
965 return false;
966 }
Robert Greenwaltbd492212011-05-06 17:10:53 -0700967 }
968
969 // TODO support the removal of single host routes. Keep a ref count of them so we
970 // aren't over-zealous
971 private boolean removeHostRoute(NetworkStateTracker nt, InetAddress hostAddress) {
972 return false;
The Android Open Source Project28527d22009-03-03 19:31:44 -0800973 }
974
975 /**
976 * @see ConnectivityManager#getBackgroundDataSetting()
977 */
978 public boolean getBackgroundDataSetting() {
Robert Greenwaltd62c7002010-12-29 16:15:02 -0800979 return mBackgroundDataEnabled.get();
The Android Open Source Project28527d22009-03-03 19:31:44 -0800980 }
Robert Greenwalt0659da32009-07-16 17:21:39 -0700981
The Android Open Source Project28527d22009-03-03 19:31:44 -0800982 /**
983 * @see ConnectivityManager#setBackgroundDataSetting(boolean)
984 */
985 public void setBackgroundDataSetting(boolean allowBackgroundDataUsage) {
986 mContext.enforceCallingOrSelfPermission(
987 android.Manifest.permission.CHANGE_BACKGROUND_DATA_SETTING,
988 "ConnectivityService");
Robert Greenwalt0659da32009-07-16 17:21:39 -0700989
Robert Greenwaltd62c7002010-12-29 16:15:02 -0800990 mBackgroundDataEnabled.set(allowBackgroundDataUsage);
991
Robert Greenwalt6a2db8a2010-09-23 10:05:56 -0700992 mHandler.sendMessage(mHandler.obtainMessage(EVENT_SET_BACKGROUND_DATA,
993 (allowBackgroundDataUsage ? ENABLED : DISABLED), 0));
994 }
The Android Open Source Project28527d22009-03-03 19:31:44 -0800995
Robert Greenwalt6a2db8a2010-09-23 10:05:56 -0700996 private void handleSetBackgroundData(boolean enabled) {
Robert Greenwalt0ffdef12011-02-25 13:44:09 -0800997 Settings.Secure.putInt(mContext.getContentResolver(),
998 Settings.Secure.BACKGROUND_DATA, enabled ? 1 : 0);
999 Intent broadcast = new Intent(
1000 ConnectivityManager.ACTION_BACKGROUND_DATA_SETTING_CHANGED);
1001 mContext.sendBroadcast(broadcast);
Robert Greenwalt0659da32009-07-16 17:21:39 -07001002 }
1003
Robert Greenwalt1b21f6c2010-02-23 18:58:05 -08001004 /**
1005 * @see ConnectivityManager#getMobileDataEnabled()
1006 */
1007 public boolean getMobileDataEnabled() {
Wink Savilleb9024c62010-12-07 10:31:02 -08001008 // TODO: This detail should probably be in DataConnectionTracker's
1009 // which is where we store the value and maybe make this
1010 // asynchronous.
Robert Greenwalt1b21f6c2010-02-23 18:58:05 -08001011 enforceAccessPermission();
1012 boolean retVal = Settings.Secure.getInt(mContext.getContentResolver(),
1013 Settings.Secure.MOBILE_DATA, 1) == 1;
Wink Savillee70c6f52010-12-03 12:01:38 -08001014 if (DBG) log("getMobileDataEnabled returning " + retVal);
Robert Greenwalt1b21f6c2010-02-23 18:58:05 -08001015 return retVal;
1016 }
1017
Robert Greenwalt34848c02011-03-25 13:09:25 -07001018 public void setDataDependency(int networkType, boolean met) {
1019 enforceChangePermission();
1020 if (DBG) {
1021 log("setDataDependency(" + networkType + ", " + met + ")");
1022 }
1023 mHandler.sendMessage(mHandler.obtainMessage(EVENT_SET_DEPENDENCY_MET,
1024 (met ? ENABLED : DISABLED), networkType));
1025 }
1026
1027 private void handleSetDependencyMet(int networkType, boolean met) {
1028 if (mNetTrackers[networkType] != null) {
1029 if (DBG) {
1030 log("handleSetDependencyMet(" + networkType + ", " + met + ")");
1031 }
1032 mNetTrackers[networkType].setDependencyMet(met);
1033 }
1034 }
1035
Robert Greenwalt1b21f6c2010-02-23 18:58:05 -08001036 /**
1037 * @see ConnectivityManager#setMobileDataEnabled(boolean)
1038 */
Robert Greenwalt6a2db8a2010-09-23 10:05:56 -07001039 public void setMobileDataEnabled(boolean enabled) {
Robert Greenwalt1b21f6c2010-02-23 18:58:05 -08001040 enforceChangePermission();
Wink Savillee70c6f52010-12-03 12:01:38 -08001041 if (DBG) log("setMobileDataEnabled(" + enabled + ")");
Robert Greenwalt1b21f6c2010-02-23 18:58:05 -08001042
Robert Greenwalt6a2db8a2010-09-23 10:05:56 -07001043 mHandler.sendMessage(mHandler.obtainMessage(EVENT_SET_MOBILE_DATA,
Robert Greenwalt34848c02011-03-25 13:09:25 -07001044 (enabled ? ENABLED : DISABLED), 0));
Robert Greenwalt6a2db8a2010-09-23 10:05:56 -07001045 }
1046
1047 private void handleSetMobileData(boolean enabled) {
Wink Savilleb9024c62010-12-07 10:31:02 -08001048 if (mNetTrackers[ConnectivityManager.TYPE_MOBILE] != null) {
1049 if (DBG) {
1050 Slog.d(TAG, mNetTrackers[ConnectivityManager.TYPE_MOBILE].toString() + enabled);
Robert Greenwalt1b21f6c2010-02-23 18:58:05 -08001051 }
Wink Savilleb9024c62010-12-07 10:31:02 -08001052 mNetTrackers[ConnectivityManager.TYPE_MOBILE].setDataEnable(enabled);
Robert Greenwalt1b21f6c2010-02-23 18:58:05 -08001053 }
1054 }
1055
The Android Open Source Project28527d22009-03-03 19:31:44 -08001056 private void enforceAccessPermission() {
Robert Greenwalt0659da32009-07-16 17:21:39 -07001057 mContext.enforceCallingOrSelfPermission(
1058 android.Manifest.permission.ACCESS_NETWORK_STATE,
1059 "ConnectivityService");
The Android Open Source Project28527d22009-03-03 19:31:44 -08001060 }
1061
1062 private void enforceChangePermission() {
Robert Greenwalt0659da32009-07-16 17:21:39 -07001063 mContext.enforceCallingOrSelfPermission(
1064 android.Manifest.permission.CHANGE_NETWORK_STATE,
1065 "ConnectivityService");
The Android Open Source Project28527d22009-03-03 19:31:44 -08001066 }
1067
Robert Greenwalt0c4828c2010-01-26 11:40:34 -08001068 // TODO Make this a special check when it goes public
1069 private void enforceTetherChangePermission() {
1070 mContext.enforceCallingOrSelfPermission(
1071 android.Manifest.permission.CHANGE_NETWORK_STATE,
1072 "ConnectivityService");
1073 }
1074
Robert Greenwalt8e87f122010-02-11 18:18:40 -08001075 private void enforceTetherAccessPermission() {
1076 mContext.enforceCallingOrSelfPermission(
1077 android.Manifest.permission.ACCESS_NETWORK_STATE,
1078 "ConnectivityService");
1079 }
1080
Robert Greenwalt93dc1042010-06-15 12:19:37 -07001081 private void enforceConnectivityInternalPermission() {
1082 mContext.enforceCallingOrSelfPermission(
1083 android.Manifest.permission.CONNECTIVITY_INTERNAL,
1084 "ConnectivityService");
1085 }
1086
The Android Open Source Project28527d22009-03-03 19:31:44 -08001087 /**
Robert Greenwalt0659da32009-07-16 17:21:39 -07001088 * Handle a {@code DISCONNECTED} event. If this pertains to the non-active
1089 * network, we ignore it. If it is for the active network, we send out a
1090 * broadcast. But first, we check whether it might be possible to connect
1091 * to a different network.
The Android Open Source Project28527d22009-03-03 19:31:44 -08001092 * @param info the {@code NetworkInfo} for the network
1093 */
1094 private void handleDisconnect(NetworkInfo info) {
1095
Robert Greenwalt2034b912009-08-12 16:08:25 -07001096 int prevNetType = info.getType();
The Android Open Source Project28527d22009-03-03 19:31:44 -08001097
Robert Greenwalt2034b912009-08-12 16:08:25 -07001098 mNetTrackers[prevNetType].setTeardownRequested(false);
The Android Open Source Project28527d22009-03-03 19:31:44 -08001099 /*
1100 * If the disconnected network is not the active one, then don't report
1101 * this as a loss of connectivity. What probably happened is that we're
1102 * getting the disconnect for a network that we explicitly disabled
1103 * in accordance with network preference policies.
1104 */
Robert Greenwalt34848c02011-03-25 13:09:25 -07001105 if (!mNetConfigs[prevNetType].isDefault()) {
Robert Greenwalt2034b912009-08-12 16:08:25 -07001106 List pids = mNetRequestersPids[prevNetType];
1107 for (int i = 0; i<pids.size(); i++) {
1108 Integer pid = (Integer)pids.get(i);
1109 // will remove them because the net's no longer connected
1110 // need to do this now as only now do we know the pids and
1111 // can properly null things that are no longer referenced.
1112 reassessPidDns(pid.intValue(), false);
The Android Open Source Project28527d22009-03-03 19:31:44 -08001113 }
The Android Open Source Project28527d22009-03-03 19:31:44 -08001114 }
1115
The Android Open Source Project28527d22009-03-03 19:31:44 -08001116 Intent intent = new Intent(ConnectivityManager.CONNECTIVITY_ACTION);
1117 intent.putExtra(ConnectivityManager.EXTRA_NETWORK_INFO, info);
1118 if (info.isFailover()) {
1119 intent.putExtra(ConnectivityManager.EXTRA_IS_FAILOVER, true);
1120 info.setFailover(false);
1121 }
1122 if (info.getReason() != null) {
1123 intent.putExtra(ConnectivityManager.EXTRA_REASON, info.getReason());
1124 }
1125 if (info.getExtraInfo() != null) {
Robert Greenwalt0659da32009-07-16 17:21:39 -07001126 intent.putExtra(ConnectivityManager.EXTRA_EXTRA_INFO,
1127 info.getExtraInfo());
The Android Open Source Project28527d22009-03-03 19:31:44 -08001128 }
Robert Greenwalt2034b912009-08-12 16:08:25 -07001129
Robert Greenwalt34848c02011-03-25 13:09:25 -07001130 if (mNetConfigs[prevNetType].isDefault()) {
Robert Greenwalt4b7c55e2011-01-11 13:56:33 -08001131 tryFailover(prevNetType);
1132 if (mActiveDefaultNetwork != -1) {
1133 NetworkInfo switchTo = mNetTrackers[mActiveDefaultNetwork].getNetworkInfo();
Robert Greenwalt3cc68d32010-01-25 17:54:29 -08001134 intent.putExtra(ConnectivityManager.EXTRA_OTHER_NETWORK_INFO, switchTo);
1135 } else {
Robert Greenwaltb2a9e492010-09-20 18:01:43 -07001136 mDefaultInetConditionPublished = 0; // we're not connected anymore
Robert Greenwalt3cc68d32010-01-25 17:54:29 -08001137 intent.putExtra(ConnectivityManager.EXTRA_NO_CONNECTIVITY, true);
1138 }
Robert Greenwaltf55ced92010-01-20 19:29:41 -08001139 }
Robert Greenwaltb2a9e492010-09-20 18:01:43 -07001140 intent.putExtra(ConnectivityManager.EXTRA_INET_CONDITION, mDefaultInetConditionPublished);
Robert Greenwaltf55ced92010-01-20 19:29:41 -08001141 // do this before we broadcast the change
Robert Greenwalt3afbead2010-07-23 15:46:26 -07001142 handleConnectivityChange(prevNetType);
Robert Greenwaltf55ced92010-01-20 19:29:41 -08001143
1144 sendStickyBroadcast(intent);
1145 /*
1146 * If the failover network is already connected, then immediately send
1147 * out a followup broadcast indicating successful failover
1148 */
Robert Greenwalt4b7c55e2011-01-11 13:56:33 -08001149 if (mActiveDefaultNetwork != -1) {
1150 sendConnectedBroadcast(mNetTrackers[mActiveDefaultNetwork].getNetworkInfo());
Robert Greenwaltf55ced92010-01-20 19:29:41 -08001151 }
1152 }
1153
Robert Greenwalt4b7c55e2011-01-11 13:56:33 -08001154 private void tryFailover(int prevNetType) {
Robert Greenwalt2034b912009-08-12 16:08:25 -07001155 /*
Robert Greenwalt92564852011-01-06 15:41:07 -08001156 * If this is a default network, check if other defaults are available.
1157 * Try to reconnect on all available and let them hash it out when
1158 * more than one connects.
Robert Greenwalt2034b912009-08-12 16:08:25 -07001159 */
Robert Greenwalt34848c02011-03-25 13:09:25 -07001160 if (mNetConfigs[prevNetType].isDefault()) {
Robert Greenwalt2034b912009-08-12 16:08:25 -07001161 if (mActiveDefaultNetwork == prevNetType) {
1162 mActiveDefaultNetwork = -1;
1163 }
1164
Robert Greenwalt4b7c55e2011-01-11 13:56:33 -08001165 // don't signal a reconnect for anything lower or equal priority than our
1166 // current connected default
1167 // TODO - don't filter by priority now - nice optimization but risky
1168// int currentPriority = -1;
1169// if (mActiveDefaultNetwork != -1) {
Robert Greenwalt34848c02011-03-25 13:09:25 -07001170// currentPriority = mNetConfigs[mActiveDefaultNetwork].mPriority;
Robert Greenwalt4b7c55e2011-01-11 13:56:33 -08001171// }
Robert Greenwaltf55ced92010-01-20 19:29:41 -08001172 for (int checkType=0; checkType <= ConnectivityManager.MAX_NETWORK_TYPE; checkType++) {
Robert Greenwaltec05b3c2009-10-30 14:17:42 -07001173 if (checkType == prevNetType) continue;
Robert Greenwalt34848c02011-03-25 13:09:25 -07001174 if (mNetConfigs[checkType] == null) continue;
1175 if (!mNetConfigs[checkType].isDefault()) continue;
Wink Saville72a95b92011-01-26 15:43:49 -08001176
1177// Enabling the isAvailable() optimization caused mobile to not get
1178// selected if it was in the middle of error handling. Specifically
1179// a moble connection that took 30 seconds to complete the DEACTIVATE_DATA_CALL
1180// would not be available and we wouldn't get connected to anything.
1181// So removing the isAvailable() optimization below for now. TODO: This
1182// optimization should work and we need to investigate why it doesn't work.
1183// This could be related to how DEACTIVATE_DATA_CALL is reporting its
1184// complete before it is really complete.
1185// if (!mNetTrackers[checkType].isAvailable()) continue;
1186
Robert Greenwalt34848c02011-03-25 13:09:25 -07001187// if (currentPriority >= mNetConfigs[checkType].mPriority) continue;
Robert Greenwalt2034b912009-08-12 16:08:25 -07001188
Robert Greenwalt4b7c55e2011-01-11 13:56:33 -08001189 NetworkStateTracker checkTracker = mNetTrackers[checkType];
1190 NetworkInfo checkInfo = checkTracker.getNetworkInfo();
1191 if (!checkInfo.isConnectedOrConnecting() || checkTracker.isTeardownRequested()) {
1192 checkInfo.setFailover(true);
1193 checkTracker.reconnect();
Robert Greenwalt2034b912009-08-12 16:08:25 -07001194 }
Robert Greenwalt4b7c55e2011-01-11 13:56:33 -08001195 if (DBG) log("Attempting to switch to " + checkInfo.getTypeName());
Robert Greenwalt2034b912009-08-12 16:08:25 -07001196 }
The Android Open Source Project28527d22009-03-03 19:31:44 -08001197 }
The Android Open Source Project28527d22009-03-03 19:31:44 -08001198 }
1199
1200 private void sendConnectedBroadcast(NetworkInfo info) {
Robert Greenwaltd3401f92010-09-15 17:36:33 -07001201 sendGeneralBroadcast(info, ConnectivityManager.CONNECTIVITY_ACTION);
1202 }
1203
1204 private void sendInetConditionBroadcast(NetworkInfo info) {
1205 sendGeneralBroadcast(info, ConnectivityManager.INET_CONDITION_ACTION);
1206 }
1207
1208 private void sendGeneralBroadcast(NetworkInfo info, String bcastType) {
1209 Intent intent = new Intent(bcastType);
The Android Open Source Project28527d22009-03-03 19:31:44 -08001210 intent.putExtra(ConnectivityManager.EXTRA_NETWORK_INFO, info);
1211 if (info.isFailover()) {
1212 intent.putExtra(ConnectivityManager.EXTRA_IS_FAILOVER, true);
1213 info.setFailover(false);
1214 }
1215 if (info.getReason() != null) {
1216 intent.putExtra(ConnectivityManager.EXTRA_REASON, info.getReason());
1217 }
1218 if (info.getExtraInfo() != null) {
Robert Greenwalt0659da32009-07-16 17:21:39 -07001219 intent.putExtra(ConnectivityManager.EXTRA_EXTRA_INFO,
1220 info.getExtraInfo());
The Android Open Source Project28527d22009-03-03 19:31:44 -08001221 }
Robert Greenwalt986c7412010-09-08 15:24:47 -07001222 intent.putExtra(ConnectivityManager.EXTRA_INET_CONDITION, mDefaultInetConditionPublished);
Mike Lockwoodfde2b762009-08-14 14:18:49 -04001223 sendStickyBroadcast(intent);
The Android Open Source Project28527d22009-03-03 19:31:44 -08001224 }
1225
1226 /**
1227 * Called when an attempt to fail over to another network has failed.
1228 * @param info the {@link NetworkInfo} for the failed network
1229 */
1230 private void handleConnectionFailure(NetworkInfo info) {
1231 mNetTrackers[info.getType()].setTeardownRequested(false);
The Android Open Source Project28527d22009-03-03 19:31:44 -08001232
Robert Greenwalt2034b912009-08-12 16:08:25 -07001233 String reason = info.getReason();
1234 String extraInfo = info.getExtraInfo();
Robert Greenwalt0659da32009-07-16 17:21:39 -07001235
Robert Greenwalte981bc52010-10-08 16:35:52 -07001236 String reasonText;
1237 if (reason == null) {
1238 reasonText = ".";
1239 } else {
1240 reasonText = " (" + reason + ").";
The Android Open Source Project28527d22009-03-03 19:31:44 -08001241 }
Wink Savillee70c6f52010-12-03 12:01:38 -08001242 loge("Attempt to connect to " + info.getTypeName() + " failed" + reasonText);
Robert Greenwalt2034b912009-08-12 16:08:25 -07001243
1244 Intent intent = new Intent(ConnectivityManager.CONNECTIVITY_ACTION);
1245 intent.putExtra(ConnectivityManager.EXTRA_NETWORK_INFO, info);
1246 if (getActiveNetworkInfo() == null) {
1247 intent.putExtra(ConnectivityManager.EXTRA_NO_CONNECTIVITY, true);
1248 }
1249 if (reason != null) {
1250 intent.putExtra(ConnectivityManager.EXTRA_REASON, reason);
1251 }
1252 if (extraInfo != null) {
1253 intent.putExtra(ConnectivityManager.EXTRA_EXTRA_INFO, extraInfo);
1254 }
1255 if (info.isFailover()) {
1256 intent.putExtra(ConnectivityManager.EXTRA_IS_FAILOVER, true);
1257 info.setFailover(false);
1258 }
Robert Greenwaltf55ced92010-01-20 19:29:41 -08001259
Robert Greenwalt34848c02011-03-25 13:09:25 -07001260 if (mNetConfigs[info.getType()].isDefault()) {
Robert Greenwalt4b7c55e2011-01-11 13:56:33 -08001261 tryFailover(info.getType());
1262 if (mActiveDefaultNetwork != -1) {
1263 NetworkInfo switchTo = mNetTrackers[mActiveDefaultNetwork].getNetworkInfo();
Robert Greenwalt3cc68d32010-01-25 17:54:29 -08001264 intent.putExtra(ConnectivityManager.EXTRA_OTHER_NETWORK_INFO, switchTo);
1265 } else {
Robert Greenwaltb2a9e492010-09-20 18:01:43 -07001266 mDefaultInetConditionPublished = 0;
Robert Greenwalt3cc68d32010-01-25 17:54:29 -08001267 intent.putExtra(ConnectivityManager.EXTRA_NO_CONNECTIVITY, true);
1268 }
Robert Greenwaltf55ced92010-01-20 19:29:41 -08001269 }
Robert Greenwalt3cc68d32010-01-25 17:54:29 -08001270
Robert Greenwaltb2a9e492010-09-20 18:01:43 -07001271 intent.putExtra(ConnectivityManager.EXTRA_INET_CONDITION, mDefaultInetConditionPublished);
Mike Lockwoodfde2b762009-08-14 14:18:49 -04001272 sendStickyBroadcast(intent);
Robert Greenwaltf55ced92010-01-20 19:29:41 -08001273 /*
1274 * If the failover network is already connected, then immediately send
1275 * out a followup broadcast indicating successful failover
1276 */
Robert Greenwalt4b7c55e2011-01-11 13:56:33 -08001277 if (mActiveDefaultNetwork != -1) {
1278 sendConnectedBroadcast(mNetTrackers[mActiveDefaultNetwork].getNetworkInfo());
Robert Greenwaltf55ced92010-01-20 19:29:41 -08001279 }
Mike Lockwoodfde2b762009-08-14 14:18:49 -04001280 }
1281
1282 private void sendStickyBroadcast(Intent intent) {
1283 synchronized(this) {
Dianne Hackborna417ff82009-12-08 19:45:14 -08001284 if (!mSystemReady) {
1285 mInitialBroadcast = new Intent(intent);
Mike Lockwoodfde2b762009-08-14 14:18:49 -04001286 }
Dianne Hackborna417ff82009-12-08 19:45:14 -08001287 intent.addFlags(Intent.FLAG_RECEIVER_REGISTERED_ONLY_BEFORE_BOOT);
1288 mContext.sendStickyBroadcast(intent);
Mike Lockwoodfde2b762009-08-14 14:18:49 -04001289 }
1290 }
1291
1292 void systemReady() {
Robert Greenwalt355205c2011-05-10 15:05:02 -07001293 IBinder b = ServiceManager.getService(Context.NETWORKMANAGEMENT_SERVICE);
1294 mNetd = INetworkManagementService.Stub.asInterface(b);
1295
Mike Lockwoodfde2b762009-08-14 14:18:49 -04001296 synchronized(this) {
1297 mSystemReady = true;
Dianne Hackborna417ff82009-12-08 19:45:14 -08001298 if (mInitialBroadcast != null) {
1299 mContext.sendStickyBroadcast(mInitialBroadcast);
1300 mInitialBroadcast = null;
Mike Lockwoodfde2b762009-08-14 14:18:49 -04001301 }
1302 }
Robert Greenwaltc3c5f862010-10-11 16:00:27 -07001303 // load the global proxy at startup
1304 mHandler.sendMessage(mHandler.obtainMessage(EVENT_APPLY_GLOBAL_HTTP_PROXY));
The Android Open Source Project28527d22009-03-03 19:31:44 -08001305 }
1306
1307 private void handleConnect(NetworkInfo info) {
Robert Greenwalt2034b912009-08-12 16:08:25 -07001308 int type = info.getType();
The Android Open Source Project28527d22009-03-03 19:31:44 -08001309
1310 // snapshot isFailover, because sendConnectedBroadcast() resets it
1311 boolean isFailover = info.isFailover();
Robert Greenwalt2034b912009-08-12 16:08:25 -07001312 NetworkStateTracker thisNet = mNetTrackers[type];
The Android Open Source Project28527d22009-03-03 19:31:44 -08001313
Robert Greenwalt2034b912009-08-12 16:08:25 -07001314 // if this is a default net and other default is running
1315 // kill the one not preferred
Robert Greenwalt34848c02011-03-25 13:09:25 -07001316 if (mNetConfigs[type].isDefault()) {
Robert Greenwalt2034b912009-08-12 16:08:25 -07001317 if (mActiveDefaultNetwork != -1 && mActiveDefaultNetwork != type) {
1318 if ((type != mNetworkPreference &&
Wink Savillef2a62832011-04-07 14:23:45 -07001319 mNetConfigs[mActiveDefaultNetwork].priority >
1320 mNetConfigs[type].priority) ||
Robert Greenwalt2034b912009-08-12 16:08:25 -07001321 mNetworkPreference == mActiveDefaultNetwork) {
1322 // don't accept this one
Wink Savillee70c6f52010-12-03 12:01:38 -08001323 if (DBG) {
1324 log("Not broadcasting CONNECT_ACTION " +
Robert Greenwalt2034b912009-08-12 16:08:25 -07001325 "to torn down network " + info.getTypeName());
Wink Savillee70c6f52010-12-03 12:01:38 -08001326 }
Robert Greenwalt2034b912009-08-12 16:08:25 -07001327 teardown(thisNet);
1328 return;
1329 } else {
1330 // tear down the other
1331 NetworkStateTracker otherNet =
1332 mNetTrackers[mActiveDefaultNetwork];
Wink Savillee70c6f52010-12-03 12:01:38 -08001333 if (DBG) {
1334 log("Policy requires " + otherNet.getNetworkInfo().getTypeName() +
Robert Greenwalt2034b912009-08-12 16:08:25 -07001335 " teardown");
Wink Savillee70c6f52010-12-03 12:01:38 -08001336 }
Robert Greenwalt2034b912009-08-12 16:08:25 -07001337 if (!teardown(otherNet)) {
Wink Savillee70c6f52010-12-03 12:01:38 -08001338 loge("Network declined teardown request");
Robert Greenwalt99910172011-03-29 11:36:28 -07001339 teardown(thisNet);
Robert Greenwalt2034b912009-08-12 16:08:25 -07001340 return;
1341 }
Robert Greenwalt93dc1042010-06-15 12:19:37 -07001342 }
1343 }
1344 synchronized (ConnectivityService.this) {
1345 // have a new default network, release the transition wakelock in a second
1346 // if it's held. The second pause is to allow apps to reconnect over the
1347 // new network
1348 if (mNetTransitionWakeLock.isHeld()) {
1349 mHandler.sendMessageDelayed(mHandler.obtainMessage(
Robert Greenwaltccb36f92010-09-24 14:32:21 -07001350 EVENT_CLEAR_NET_TRANSITION_WAKELOCK,
Robert Greenwalt93dc1042010-06-15 12:19:37 -07001351 mNetTransitionWakeLockSerialNumber, 0),
1352 1000);
Robert Greenwalt2034b912009-08-12 16:08:25 -07001353 }
1354 }
1355 mActiveDefaultNetwork = type;
Robert Greenwalt986c7412010-09-08 15:24:47 -07001356 // this will cause us to come up initially as unconnected and switching
1357 // to connected after our normal pause unless somebody reports us as reall
1358 // disconnected
1359 mDefaultInetConditionPublished = 0;
1360 mDefaultConnectionSequence++;
1361 mInetConditionChangeInFlight = false;
1362 // Don't do this - if we never sign in stay, grey
1363 //reportNetworkCondition(mActiveDefaultNetwork, 100);
Robert Greenwalt2034b912009-08-12 16:08:25 -07001364 }
The Android Open Source Project28527d22009-03-03 19:31:44 -08001365 thisNet.setTeardownRequested(false);
Irfan Sheriff7f132d92010-06-09 15:39:36 -07001366 updateNetworkSettings(thisNet);
Robert Greenwalt3afbead2010-07-23 15:46:26 -07001367 handleConnectivityChange(type);
Robert Greenwalt2034b912009-08-12 16:08:25 -07001368 sendConnectedBroadcast(info);
The Android Open Source Project28527d22009-03-03 19:31:44 -08001369 }
1370
The Android Open Source Project28527d22009-03-03 19:31:44 -08001371 /**
Robert Greenwalt3afbead2010-07-23 15:46:26 -07001372 * After a change in the connectivity state of a network. We're mainly
1373 * concerned with making sure that the list of DNS servers is set up
1374 * according to which networks are connected, and ensuring that the
1375 * right routing table entries exist.
The Android Open Source Project28527d22009-03-03 19:31:44 -08001376 */
Robert Greenwalt3afbead2010-07-23 15:46:26 -07001377 private void handleConnectivityChange(int netType) {
The Android Open Source Project28527d22009-03-03 19:31:44 -08001378 /*
Robert Greenwalt2034b912009-08-12 16:08:25 -07001379 * If a non-default network is enabled, add the host routes that
Robert Greenwalt3afbead2010-07-23 15:46:26 -07001380 * will allow it's DNS servers to be accessed.
The Android Open Source Project28527d22009-03-03 19:31:44 -08001381 */
Robert Greenwalt3afbead2010-07-23 15:46:26 -07001382 handleDnsConfigurationChange(netType);
Robert Greenwalt2034b912009-08-12 16:08:25 -07001383
Robert Greenwalt3afbead2010-07-23 15:46:26 -07001384 if (mNetTrackers[netType].getNetworkInfo().isConnected()) {
Robert Greenwalt34848c02011-03-25 13:09:25 -07001385 if (mNetConfigs[netType].isDefault()) {
Robert Greenwaltc3c5f862010-10-11 16:00:27 -07001386 handleApplyDefaultProxy(netType);
Robert Greenwalt3afbead2010-07-23 15:46:26 -07001387 addDefaultRoute(mNetTrackers[netType]);
Robert Greenwalt2034b912009-08-12 16:08:25 -07001388 } else {
Robert Greenwalt1e2f2862011-04-01 10:51:22 -07001389 // many radios add a default route even when we don't want one.
1390 // remove the default route unless we need it for our active network
1391 if (mActiveDefaultNetwork != -1) {
1392 LinkProperties defaultLinkProperties =
1393 mNetTrackers[mActiveDefaultNetwork].getLinkProperties();
1394 LinkProperties newLinkProperties =
1395 mNetTrackers[netType].getLinkProperties();
1396 String defaultIface = defaultLinkProperties.getInterfaceName();
1397 if (defaultIface != null &&
1398 !defaultIface.equals(newLinkProperties.getInterfaceName())) {
1399 removeDefaultRoute(mNetTrackers[netType]);
1400 }
1401 }
Michael Jurka837e3642011-03-30 19:54:08 -07001402 addPrivateDnsRoutes(mNetTrackers[netType]);
Robert Greenwalt3afbead2010-07-23 15:46:26 -07001403 }
Kazuhiro Ondo3a340412011-04-30 20:10:57 -05001404
1405 /** Notify TetheringService if interface name has been changed. */
1406 if (TextUtils.equals(mNetTrackers[netType].getNetworkInfo().getReason(),
1407 Phone.REASON_LINK_PROPERTIES_CHANGED)) {
1408 handleTetherIfaceChange(netType);
1409 }
Robert Greenwalt3afbead2010-07-23 15:46:26 -07001410 } else {
Robert Greenwalt34848c02011-03-25 13:09:25 -07001411 if (mNetConfigs[netType].isDefault()) {
Robert Greenwalt3afbead2010-07-23 15:46:26 -07001412 removeDefaultRoute(mNetTrackers[netType]);
1413 } else {
1414 removePrivateDnsRoutes(mNetTrackers[netType]);
Robert Greenwalt2034b912009-08-12 16:08:25 -07001415 }
The Android Open Source Project28527d22009-03-03 19:31:44 -08001416 }
1417 }
1418
Irfan Sheriff7f132d92010-06-09 15:39:36 -07001419 private void addPrivateDnsRoutes(NetworkStateTracker nt) {
Irfan Sheriff7f132d92010-06-09 15:39:36 -07001420 boolean privateDnsRouteSet = nt.isPrivateDnsRouteSet();
Robert Greenwalt1f1bcfe2010-08-30 10:56:47 -07001421 LinkProperties p = nt.getLinkProperties();
Robert Greenwalta7dfbd32010-06-15 15:43:39 -07001422 if (p == null) return;
1423 String interfaceName = p.getInterfaceName();
Irfan Sheriff7f132d92010-06-09 15:39:36 -07001424
1425 if (DBG) {
Wink Savillee70c6f52010-12-03 12:01:38 -08001426 log("addPrivateDnsRoutes for " + nt +
Irfan Sheriff7f132d92010-06-09 15:39:36 -07001427 "(" + interfaceName + ") - mPrivateDnsRouteSet = " + privateDnsRouteSet);
1428 }
Irfan Sheriff7f132d92010-06-09 15:39:36 -07001429 if (interfaceName != null && !privateDnsRouteSet) {
Robert Greenwalta7dfbd32010-06-15 15:43:39 -07001430 Collection<InetAddress> dnsList = p.getDnses();
1431 for (InetAddress dns : dnsList) {
Robert Greenwaltbd492212011-05-06 17:10:53 -07001432 addHostRoute(nt, dns, 0);
Irfan Sheriff7f132d92010-06-09 15:39:36 -07001433 }
1434 nt.privateDnsRouteSet(true);
1435 }
1436 }
1437
1438 private void removePrivateDnsRoutes(NetworkStateTracker nt) {
Robert Greenwalt1f1bcfe2010-08-30 10:56:47 -07001439 LinkProperties p = nt.getLinkProperties();
Robert Greenwalta7dfbd32010-06-15 15:43:39 -07001440 if (p == null) return;
1441 String interfaceName = p.getInterfaceName();
Irfan Sheriff7f132d92010-06-09 15:39:36 -07001442 boolean privateDnsRouteSet = nt.isPrivateDnsRouteSet();
1443 if (interfaceName != null && privateDnsRouteSet) {
1444 if (DBG) {
Wink Savillee70c6f52010-12-03 12:01:38 -08001445 log("removePrivateDnsRoutes for " + nt.getNetworkInfo().getTypeName() +
Irfan Sheriff7f132d92010-06-09 15:39:36 -07001446 " (" + interfaceName + ")");
1447 }
Robert Greenwalt355205c2011-05-10 15:05:02 -07001448
1449 Collection<InetAddress> dnsList = p.getDnses();
1450 for (InetAddress dns : dnsList) {
1451 if (DBG) log(" removing " + dns);
1452 RouteInfo route = RouteInfo.makeHostRoute(dns);
1453 try {
1454 mNetd.removeRoute(interfaceName, route);
1455 } catch (Exception ex) {
1456 loge("error (" + ex + ") removing dns route " + route);
1457 }
1458 }
Irfan Sheriff7f132d92010-06-09 15:39:36 -07001459 nt.privateDnsRouteSet(false);
1460 }
1461 }
1462
Irfan Sheriff7f132d92010-06-09 15:39:36 -07001463
1464 private void addDefaultRoute(NetworkStateTracker nt) {
Robert Greenwalt1f1bcfe2010-08-30 10:56:47 -07001465 LinkProperties p = nt.getLinkProperties();
Robert Greenwalta7dfbd32010-06-15 15:43:39 -07001466 if (p == null) return;
1467 String interfaceName = p.getInterfaceName();
Robert Greenwalt5c733972011-02-09 13:56:06 -08001468 if (TextUtils.isEmpty(interfaceName)) return;
Irfan Sheriff7f132d92010-06-09 15:39:36 -07001469
Robert Greenwalt355205c2011-05-10 15:05:02 -07001470 for (RouteInfo route : p.getRoutes()) {
Robert Greenwalt5a901292011-04-28 14:28:50 -07001471 //TODO - handle non-default routes
1472 if (route.isDefaultRoute()) {
Robert Greenwalt355205c2011-05-10 15:05:02 -07001473 if (DBG) log("adding default route " + route);
Robert Greenwalt5a901292011-04-28 14:28:50 -07001474 InetAddress gateway = route.getGateway();
Robert Greenwalt355205c2011-05-10 15:05:02 -07001475 if (addHostRoute(nt, gateway, 0)) {
1476 try {
1477 mNetd.addRoute(interfaceName, route);
1478 } catch (Exception e) {
1479 loge("error adding default route " + route);
1480 continue;
1481 }
Robert Greenwalt5a901292011-04-28 14:28:50 -07001482 if (DBG) {
1483 NetworkInfo networkInfo = nt.getNetworkInfo();
1484 log("addDefaultRoute for " + networkInfo.getTypeName() +
1485 " (" + interfaceName + "), GatewayAddr=" +
1486 gateway.getHostAddress());
1487 }
Robert Greenwalt355205c2011-05-10 15:05:02 -07001488 } else {
1489 loge("error adding host route for default route " + route);
Robert Greenwalt03d53da2011-03-22 18:47:42 -07001490 }
Irfan Sheriff7f132d92010-06-09 15:39:36 -07001491 }
Irfan Sheriff7f132d92010-06-09 15:39:36 -07001492 }
1493 }
1494
1495
1496 public void removeDefaultRoute(NetworkStateTracker nt) {
Robert Greenwalt1f1bcfe2010-08-30 10:56:47 -07001497 LinkProperties p = nt.getLinkProperties();
Robert Greenwalta7dfbd32010-06-15 15:43:39 -07001498 if (p == null) return;
1499 String interfaceName = p.getInterfaceName();
Irfan Sheriff7f132d92010-06-09 15:39:36 -07001500
Robert Greenwalt355205c2011-05-10 15:05:02 -07001501 if (interfaceName == null) return;
1502
1503 for (RouteInfo route : p.getRoutes()) {
1504 //TODO - handle non-default routes
1505 if (route.isDefaultRoute()) {
1506 try {
1507 mNetd.removeRoute(interfaceName, route);
1508 } catch (Exception ex) {
1509 loge("error (" + ex + ") removing default route " + route);
1510 continue;
1511 }
Robert Greenwalt03d53da2011-03-22 18:47:42 -07001512 if (DBG) {
1513 NetworkInfo networkInfo = nt.getNetworkInfo();
1514 log("removeDefaultRoute for " + networkInfo.getTypeName() + " (" +
1515 interfaceName + ")");
1516 }
Irfan Sheriff7f132d92010-06-09 15:39:36 -07001517 }
Irfan Sheriff7f132d92010-06-09 15:39:36 -07001518 }
1519 }
1520
1521 /**
1522 * Reads the network specific TCP buffer sizes from SystemProperties
1523 * net.tcp.buffersize.[default|wifi|umts|edge|gprs] and set them for system
1524 * wide use
1525 */
1526 public void updateNetworkSettings(NetworkStateTracker nt) {
1527 String key = nt.getTcpBufferSizesPropName();
1528 String bufferSizes = SystemProperties.get(key);
1529
1530 if (bufferSizes.length() == 0) {
Wink Savillee70c6f52010-12-03 12:01:38 -08001531 loge(key + " not found in system properties. Using defaults");
Irfan Sheriff7f132d92010-06-09 15:39:36 -07001532
1533 // Setting to default values so we won't be stuck to previous values
1534 key = "net.tcp.buffersize.default";
1535 bufferSizes = SystemProperties.get(key);
1536 }
1537
1538 // Set values in kernel
1539 if (bufferSizes.length() != 0) {
1540 if (DBG) {
Wink Savillee70c6f52010-12-03 12:01:38 -08001541 log("Setting TCP values: [" + bufferSizes
Irfan Sheriff7f132d92010-06-09 15:39:36 -07001542 + "] which comes from [" + key + "]");
1543 }
1544 setBufferSize(bufferSizes);
1545 }
1546 }
1547
1548 /**
1549 * Writes TCP buffer sizes to /sys/kernel/ipv4/tcp_[r/w]mem_[min/def/max]
1550 * which maps to /proc/sys/net/ipv4/tcp_rmem and tcpwmem
1551 *
1552 * @param bufferSizes in the format of "readMin, readInitial, readMax,
1553 * writeMin, writeInitial, writeMax"
1554 */
1555 private void setBufferSize(String bufferSizes) {
1556 try {
1557 String[] values = bufferSizes.split(",");
1558
1559 if (values.length == 6) {
1560 final String prefix = "/sys/kernel/ipv4/tcp_";
1561 stringToFile(prefix + "rmem_min", values[0]);
1562 stringToFile(prefix + "rmem_def", values[1]);
1563 stringToFile(prefix + "rmem_max", values[2]);
1564 stringToFile(prefix + "wmem_min", values[3]);
1565 stringToFile(prefix + "wmem_def", values[4]);
1566 stringToFile(prefix + "wmem_max", values[5]);
1567 } else {
Wink Savillee70c6f52010-12-03 12:01:38 -08001568 loge("Invalid buffersize string: " + bufferSizes);
Irfan Sheriff7f132d92010-06-09 15:39:36 -07001569 }
1570 } catch (IOException e) {
Wink Savillee70c6f52010-12-03 12:01:38 -08001571 loge("Can't set tcp buffer sizes:" + e);
Irfan Sheriff7f132d92010-06-09 15:39:36 -07001572 }
1573 }
1574
1575 /**
1576 * Writes string to file. Basically same as "echo -n $string > $filename"
1577 *
1578 * @param filename
1579 * @param string
1580 * @throws IOException
1581 */
1582 private void stringToFile(String filename, String string) throws IOException {
1583 FileWriter out = new FileWriter(filename);
1584 try {
1585 out.write(string);
1586 } finally {
1587 out.close();
1588 }
1589 }
1590
1591
Robert Greenwalt2034b912009-08-12 16:08:25 -07001592 /**
1593 * Adjust the per-process dns entries (net.dns<x>.<pid>) based
1594 * on the highest priority active net which this process requested.
1595 * If there aren't any, clear it out
1596 */
1597 private void reassessPidDns(int myPid, boolean doBump)
1598 {
Wink Savillee70c6f52010-12-03 12:01:38 -08001599 if (DBG) log("reassessPidDns for pid " + myPid);
Robert Greenwalt2034b912009-08-12 16:08:25 -07001600 for(int i : mPriorityList) {
Robert Greenwalt34848c02011-03-25 13:09:25 -07001601 if (mNetConfigs[i].isDefault()) {
Robert Greenwalt2034b912009-08-12 16:08:25 -07001602 continue;
1603 }
1604 NetworkStateTracker nt = mNetTrackers[i];
Robert Greenwalt0659da32009-07-16 17:21:39 -07001605 if (nt.getNetworkInfo().isConnected() &&
1606 !nt.isTeardownRequested()) {
Robert Greenwalt1f1bcfe2010-08-30 10:56:47 -07001607 LinkProperties p = nt.getLinkProperties();
Robert Greenwalta7dfbd32010-06-15 15:43:39 -07001608 if (p == null) continue;
Robert Greenwalt2034b912009-08-12 16:08:25 -07001609 List pids = mNetRequestersPids[i];
1610 for (int j=0; j<pids.size(); j++) {
1611 Integer pid = (Integer)pids.get(j);
1612 if (pid.intValue() == myPid) {
Robert Greenwalta7dfbd32010-06-15 15:43:39 -07001613 Collection<InetAddress> dnses = p.getDnses();
1614 writePidDns(dnses, myPid);
Robert Greenwalt2034b912009-08-12 16:08:25 -07001615 if (doBump) {
1616 bumpDns();
1617 }
1618 return;
1619 }
1620 }
1621 }
1622 }
1623 // nothing found - delete
1624 for (int i = 1; ; i++) {
1625 String prop = "net.dns" + i + "." + myPid;
1626 if (SystemProperties.get(prop).length() == 0) {
1627 if (doBump) {
1628 bumpDns();
1629 }
1630 return;
1631 }
1632 SystemProperties.set(prop, "");
1633 }
1634 }
1635
Robert Greenwalt8ca88762010-12-17 15:20:36 -08001636 // return true if results in a change
1637 private boolean writePidDns(Collection <InetAddress> dnses, int pid) {
Robert Greenwalt2034b912009-08-12 16:08:25 -07001638 int j = 1;
Robert Greenwalt8ca88762010-12-17 15:20:36 -08001639 boolean changed = false;
Robert Greenwalta7dfbd32010-06-15 15:43:39 -07001640 for (InetAddress dns : dnses) {
Robert Greenwalt8ca88762010-12-17 15:20:36 -08001641 String dnsString = dns.getHostAddress();
1642 if (changed || !dnsString.equals(SystemProperties.get("net.dns" + j + "." + pid))) {
1643 changed = true;
1644 SystemProperties.set("net.dns" + j++ + "." + pid, dns.getHostAddress());
1645 }
Robert Greenwalt2034b912009-08-12 16:08:25 -07001646 }
Robert Greenwalt8ca88762010-12-17 15:20:36 -08001647 return changed;
Robert Greenwalt2034b912009-08-12 16:08:25 -07001648 }
1649
1650 private void bumpDns() {
1651 /*
1652 * Bump the property that tells the name resolver library to reread
1653 * the DNS server list from the properties.
1654 */
1655 String propVal = SystemProperties.get("net.dnschange");
1656 int n = 0;
1657 if (propVal.length() != 0) {
1658 try {
1659 n = Integer.parseInt(propVal);
1660 } catch (NumberFormatException e) {}
1661 }
1662 SystemProperties.set("net.dnschange", "" + (n+1));
Robert Greenwalt051642b2010-11-02 14:08:23 -07001663 /*
1664 * Tell the VMs to toss their DNS caches
1665 */
1666 Intent intent = new Intent(Intent.ACTION_CLEAR_DNS_CACHE);
1667 intent.addFlags(Intent.FLAG_RECEIVER_REPLACE_PENDING);
Stan Chesnuttf444f502011-01-05 17:14:03 -08001668 /*
1669 * Connectivity events can happen before boot has completed ...
1670 */
1671 intent.addFlags(Intent.FLAG_RECEIVER_REGISTERED_ONLY_BEFORE_BOOT);
Robert Greenwalt051642b2010-11-02 14:08:23 -07001672 mContext.sendBroadcast(intent);
Robert Greenwalt2034b912009-08-12 16:08:25 -07001673 }
1674
Robert Greenwalt3afbead2010-07-23 15:46:26 -07001675 private void handleDnsConfigurationChange(int netType) {
Robert Greenwalt2034b912009-08-12 16:08:25 -07001676 // add default net's dns entries
Robert Greenwalt3afbead2010-07-23 15:46:26 -07001677 NetworkStateTracker nt = mNetTrackers[netType];
1678 if (nt != null && nt.getNetworkInfo().isConnected() && !nt.isTeardownRequested()) {
Robert Greenwalt1f1bcfe2010-08-30 10:56:47 -07001679 LinkProperties p = nt.getLinkProperties();
Robert Greenwalt3afbead2010-07-23 15:46:26 -07001680 if (p == null) return;
1681 Collection<InetAddress> dnses = p.getDnses();
Robert Greenwalt8ca88762010-12-17 15:20:36 -08001682 boolean changed = false;
Robert Greenwalt34848c02011-03-25 13:09:25 -07001683 if (mNetConfigs[netType].isDefault()) {
Robert Greenwalt3afbead2010-07-23 15:46:26 -07001684 int j = 1;
Robert Greenwalt94daa182010-09-01 11:34:05 -07001685 if (dnses.size() == 0 && mDefaultDns != null) {
Robert Greenwalt8ca88762010-12-17 15:20:36 -08001686 String dnsString = mDefaultDns.getHostAddress();
1687 if (!dnsString.equals(SystemProperties.get("net.dns1"))) {
1688 if (DBG) {
1689 log("no dns provided - using " + dnsString);
1690 }
1691 changed = true;
1692 SystemProperties.set("net.dns1", dnsString);
Robert Greenwalt2034b912009-08-12 16:08:25 -07001693 }
Robert Greenwalt94daa182010-09-01 11:34:05 -07001694 j++;
1695 } else {
1696 for (InetAddress dns : dnses) {
Robert Greenwalt8ca88762010-12-17 15:20:36 -08001697 String dnsString = dns.getHostAddress();
1698 if (!changed && dnsString.equals(SystemProperties.get("net.dns" + j))) {
1699 j++;
1700 continue;
1701 }
Robert Greenwalt94daa182010-09-01 11:34:05 -07001702 if (DBG) {
Wink Savillee70c6f52010-12-03 12:01:38 -08001703 log("adding dns " + dns + " for " +
Robert Greenwalt94daa182010-09-01 11:34:05 -07001704 nt.getNetworkInfo().getTypeName());
1705 }
Robert Greenwalt8ca88762010-12-17 15:20:36 -08001706 changed = true;
1707 SystemProperties.set("net.dns" + j++, dnsString);
Robert Greenwalt94daa182010-09-01 11:34:05 -07001708 }
Robert Greenwalt3afbead2010-07-23 15:46:26 -07001709 }
1710 for (int k=j ; k<mNumDnsEntries; k++) {
Robert Greenwalt8ca88762010-12-17 15:20:36 -08001711 if (changed || !TextUtils.isEmpty(SystemProperties.get("net.dns" + k))) {
1712 if (DBG) log("erasing net.dns" + k);
1713 changed = true;
1714 SystemProperties.set("net.dns" + k, "");
1715 }
Robert Greenwalt3afbead2010-07-23 15:46:26 -07001716 }
1717 mNumDnsEntries = j;
1718 } else {
1719 // set per-pid dns for attached secondary nets
1720 List pids = mNetRequestersPids[netType];
1721 for (int y=0; y< pids.size(); y++) {
1722 Integer pid = (Integer)pids.get(y);
Robert Greenwalt8ca88762010-12-17 15:20:36 -08001723 changed = writePidDns(dnses, pid.intValue());
The Android Open Source Project28527d22009-03-03 19:31:44 -08001724 }
1725 }
Robert Greenwalt8ca88762010-12-17 15:20:36 -08001726 if (changed) bumpDns();
The Android Open Source Project28527d22009-03-03 19:31:44 -08001727 }
Robert Greenwalt2034b912009-08-12 16:08:25 -07001728 }
1729
Robert Greenwalt20f819c2011-05-03 19:02:44 -07001730 private int getRestoreDefaultNetworkDelay(int networkType) {
Robert Greenwalt2034b912009-08-12 16:08:25 -07001731 String restoreDefaultNetworkDelayStr = SystemProperties.get(
1732 NETWORK_RESTORE_DELAY_PROP_NAME);
1733 if(restoreDefaultNetworkDelayStr != null &&
1734 restoreDefaultNetworkDelayStr.length() != 0) {
1735 try {
1736 return Integer.valueOf(restoreDefaultNetworkDelayStr);
1737 } catch (NumberFormatException e) {
1738 }
The Android Open Source Project28527d22009-03-03 19:31:44 -08001739 }
Robert Greenwalt20f819c2011-05-03 19:02:44 -07001740 // if the system property isn't set, use the value for the apn type
1741 int ret = RESTORE_DEFAULT_NETWORK_DELAY;
1742
1743 if ((networkType <= ConnectivityManager.MAX_NETWORK_TYPE) &&
1744 (mNetConfigs[networkType] != null)) {
1745 ret = mNetConfigs[networkType].restoreTime;
1746 }
1747 return ret;
The Android Open Source Project28527d22009-03-03 19:31:44 -08001748 }
1749
1750 @Override
1751 protected void dump(FileDescriptor fd, PrintWriter pw, String[] args) {
Robert Greenwalt0659da32009-07-16 17:21:39 -07001752 if (mContext.checkCallingOrSelfPermission(
1753 android.Manifest.permission.DUMP)
The Android Open Source Project28527d22009-03-03 19:31:44 -08001754 != PackageManager.PERMISSION_GRANTED) {
Robert Greenwalt0659da32009-07-16 17:21:39 -07001755 pw.println("Permission Denial: can't dump ConnectivityService " +
1756 "from from pid=" + Binder.getCallingPid() + ", uid=" +
1757 Binder.getCallingUid());
The Android Open Source Project28527d22009-03-03 19:31:44 -08001758 return;
1759 }
The Android Open Source Project28527d22009-03-03 19:31:44 -08001760 pw.println();
1761 for (NetworkStateTracker nst : mNetTrackers) {
Robert Greenwalt3eeb6032009-12-21 18:24:07 -08001762 if (nst != null) {
1763 if (nst.getNetworkInfo().isConnected()) {
1764 pw.println("Active network: " + nst.getNetworkInfo().
1765 getTypeName());
1766 }
1767 pw.println(nst.getNetworkInfo());
1768 pw.println(nst);
1769 pw.println();
Robert Greenwalt2034b912009-08-12 16:08:25 -07001770 }
The Android Open Source Project28527d22009-03-03 19:31:44 -08001771 }
Robert Greenwalt3eeb6032009-12-21 18:24:07 -08001772
1773 pw.println("Network Requester Pids:");
1774 for (int net : mPriorityList) {
1775 String pidString = net + ": ";
1776 for (Object pid : mNetRequestersPids[net]) {
1777 pidString = pidString + pid.toString() + ", ";
1778 }
1779 pw.println(pidString);
1780 }
1781 pw.println();
1782
1783 pw.println("FeatureUsers:");
1784 for (Object requester : mFeatureUsers) {
1785 pw.println(requester.toString());
1786 }
1787 pw.println();
Robert Greenwalt8e87f122010-02-11 18:18:40 -08001788
Robert Greenwalt93dc1042010-06-15 12:19:37 -07001789 synchronized (this) {
1790 pw.println("NetworkTranstionWakeLock is currently " +
1791 (mNetTransitionWakeLock.isHeld() ? "" : "not ") + "held.");
1792 pw.println("It was last requested for "+mNetTransitionWakeLockCausedBy);
1793 }
1794 pw.println();
1795
Robert Greenwalt8e87f122010-02-11 18:18:40 -08001796 mTethering.dump(fd, pw, args);
Robert Greenwalt0e80be12010-09-20 14:35:25 -07001797
1798 if (mInetLog != null) {
1799 pw.println();
1800 pw.println("Inet condition reports:");
1801 for(int i = 0; i < mInetLog.size(); i++) {
1802 pw.println(mInetLog.get(i));
1803 }
1804 }
The Android Open Source Project28527d22009-03-03 19:31:44 -08001805 }
1806
Robert Greenwalt2034b912009-08-12 16:08:25 -07001807 // must be stateless - things change under us.
The Android Open Source Project28527d22009-03-03 19:31:44 -08001808 private class MyHandler extends Handler {
Wink Saville775aad62010-09-02 19:23:52 -07001809 public MyHandler(Looper looper) {
1810 super(looper);
1811 }
1812
The Android Open Source Project28527d22009-03-03 19:31:44 -08001813 @Override
1814 public void handleMessage(Message msg) {
1815 NetworkInfo info;
1816 switch (msg.what) {
1817 case NetworkStateTracker.EVENT_STATE_CHANGED:
1818 info = (NetworkInfo) msg.obj;
Robert Greenwalt12c44552009-12-07 11:33:18 -08001819 int type = info.getType();
1820 NetworkInfo.State state = info.getState();
Robert Greenwalt12c44552009-12-07 11:33:18 -08001821
Wink Savillee70c6f52010-12-03 12:01:38 -08001822 if (DBG) log("ConnectivityChange for " +
Robert Greenwalt0659da32009-07-16 17:21:39 -07001823 info.getTypeName() + ": " +
Robert Greenwalt12c44552009-12-07 11:33:18 -08001824 state + "/" + info.getDetailedState());
The Android Open Source Project28527d22009-03-03 19:31:44 -08001825
1826 // Connectivity state changed:
1827 // [31-13] Reserved for future use
Robert Greenwalt0659da32009-07-16 17:21:39 -07001828 // [12-9] Network subtype (for mobile network, as defined
1829 // by TelephonyManager)
1830 // [8-3] Detailed state ordinal (as defined by
1831 // NetworkInfo.DetailedState)
The Android Open Source Project28527d22009-03-03 19:31:44 -08001832 // [2-0] Network type (as defined by ConnectivityManager)
1833 int eventLogParam = (info.getType() & 0x7) |
1834 ((info.getDetailedState().ordinal() & 0x3f) << 3) |
1835 (info.getSubtype() << 9);
Doug Zongker2fc96232009-12-04 10:31:43 -08001836 EventLog.writeEvent(EventLogTags.CONNECTIVITY_STATE_CHANGED,
Robert Greenwalt0659da32009-07-16 17:21:39 -07001837 eventLogParam);
1838
1839 if (info.getDetailedState() ==
1840 NetworkInfo.DetailedState.FAILED) {
The Android Open Source Project28527d22009-03-03 19:31:44 -08001841 handleConnectionFailure(info);
Robert Greenwalt12c44552009-12-07 11:33:18 -08001842 } else if (state == NetworkInfo.State.DISCONNECTED) {
The Android Open Source Project28527d22009-03-03 19:31:44 -08001843 handleDisconnect(info);
Robert Greenwalt12c44552009-12-07 11:33:18 -08001844 } else if (state == NetworkInfo.State.SUSPENDED) {
The Android Open Source Project28527d22009-03-03 19:31:44 -08001845 // TODO: need to think this over.
Robert Greenwalt0659da32009-07-16 17:21:39 -07001846 // the logic here is, handle SUSPENDED the same as
1847 // DISCONNECTED. The only difference being we are
1848 // broadcasting an intent with NetworkInfo that's
1849 // suspended. This allows the applications an
1850 // opportunity to handle DISCONNECTED and SUSPENDED
1851 // differently, or not.
The Android Open Source Project28527d22009-03-03 19:31:44 -08001852 handleDisconnect(info);
Robert Greenwalt12c44552009-12-07 11:33:18 -08001853 } else if (state == NetworkInfo.State.CONNECTED) {
The Android Open Source Project28527d22009-03-03 19:31:44 -08001854 handleConnect(info);
1855 }
The Android Open Source Project28527d22009-03-03 19:31:44 -08001856 break;
The Android Open Source Project28527d22009-03-03 19:31:44 -08001857 case NetworkStateTracker.EVENT_CONFIGURATION_CHANGED:
Robert Greenwalt3afbead2010-07-23 15:46:26 -07001858 info = (NetworkInfo) msg.obj;
Robert Greenwalt34848c02011-03-25 13:09:25 -07001859 handleConnectivityChange(info.getType());
The Android Open Source Project28527d22009-03-03 19:31:44 -08001860 break;
Robert Greenwaltccb36f92010-09-24 14:32:21 -07001861 case EVENT_CLEAR_NET_TRANSITION_WAKELOCK:
Robert Greenwalt93dc1042010-06-15 12:19:37 -07001862 String causedBy = null;
1863 synchronized (ConnectivityService.this) {
1864 if (msg.arg1 == mNetTransitionWakeLockSerialNumber &&
1865 mNetTransitionWakeLock.isHeld()) {
1866 mNetTransitionWakeLock.release();
1867 causedBy = mNetTransitionWakeLockCausedBy;
1868 }
1869 }
1870 if (causedBy != null) {
Wink Savillee70c6f52010-12-03 12:01:38 -08001871 log("NetTransition Wakelock for " + causedBy + " released by timeout");
Robert Greenwalt93dc1042010-06-15 12:19:37 -07001872 }
Robert Greenwaltcf1a56c2010-09-09 14:05:10 -07001873 break;
Robert Greenwalt6a2db8a2010-09-23 10:05:56 -07001874 case EVENT_RESTORE_DEFAULT_NETWORK:
Robert Greenwaltaffc3a12009-09-27 17:27:04 -07001875 FeatureUser u = (FeatureUser)msg.obj;
1876 u.expire();
Robert Greenwalt986c7412010-09-08 15:24:47 -07001877 break;
Robert Greenwalt6a2db8a2010-09-23 10:05:56 -07001878 case EVENT_INET_CONDITION_CHANGE:
1879 {
1880 int netType = msg.arg1;
1881 int condition = msg.arg2;
1882 handleInetConditionChange(netType, condition);
Robert Greenwalt93dc1042010-06-15 12:19:37 -07001883 break;
Robert Greenwalt6a2db8a2010-09-23 10:05:56 -07001884 }
1885 case EVENT_INET_CONDITION_HOLD_END:
1886 {
1887 int netType = msg.arg1;
1888 int sequence = msg.arg2;
1889 handleInetConditionHoldEnd(netType, sequence);
Robert Greenwalt986c7412010-09-08 15:24:47 -07001890 break;
Robert Greenwalt6a2db8a2010-09-23 10:05:56 -07001891 }
1892 case EVENT_SET_NETWORK_PREFERENCE:
1893 {
1894 int preference = msg.arg1;
1895 handleSetNetworkPreference(preference);
1896 break;
1897 }
1898 case EVENT_SET_BACKGROUND_DATA:
1899 {
1900 boolean enabled = (msg.arg1 == ENABLED);
1901 handleSetBackgroundData(enabled);
1902 break;
1903 }
1904 case EVENT_SET_MOBILE_DATA:
1905 {
1906 boolean enabled = (msg.arg1 == ENABLED);
1907 handleSetMobileData(enabled);
1908 break;
1909 }
Robert Greenwaltc3c5f862010-10-11 16:00:27 -07001910 case EVENT_APPLY_GLOBAL_HTTP_PROXY:
1911 {
1912 handleDeprecatedGlobalHttpProxy();
Robert Greenwalt34848c02011-03-25 13:09:25 -07001913 break;
1914 }
1915 case EVENT_SET_DEPENDENCY_MET:
1916 {
1917 boolean met = (msg.arg1 == ENABLED);
1918 handleSetDependencyMet(msg.arg2, met);
1919 break;
Robert Greenwaltc3c5f862010-10-11 16:00:27 -07001920 }
The Android Open Source Project28527d22009-03-03 19:31:44 -08001921 }
1922 }
1923 }
Robert Greenwalt0c4828c2010-01-26 11:40:34 -08001924
1925 // javadoc from interface
Robert Greenwalt4283ded2010-03-02 17:25:02 -08001926 public int tether(String iface) {
Robert Greenwalt0c4828c2010-01-26 11:40:34 -08001927 enforceTetherChangePermission();
Robert Greenwalt4283ded2010-03-02 17:25:02 -08001928
1929 if (isTetheringSupported()) {
1930 return mTethering.tether(iface);
1931 } else {
1932 return ConnectivityManager.TETHER_ERROR_UNSUPPORTED;
1933 }
Robert Greenwalt0c4828c2010-01-26 11:40:34 -08001934 }
1935
1936 // javadoc from interface
Robert Greenwalt4283ded2010-03-02 17:25:02 -08001937 public int untether(String iface) {
Robert Greenwalt0c4828c2010-01-26 11:40:34 -08001938 enforceTetherChangePermission();
Robert Greenwalt4283ded2010-03-02 17:25:02 -08001939
1940 if (isTetheringSupported()) {
1941 return mTethering.untether(iface);
1942 } else {
1943 return ConnectivityManager.TETHER_ERROR_UNSUPPORTED;
1944 }
1945 }
1946
1947 // javadoc from interface
1948 public int getLastTetherError(String iface) {
1949 enforceTetherAccessPermission();
1950
1951 if (isTetheringSupported()) {
1952 return mTethering.getLastTetherError(iface);
1953 } else {
1954 return ConnectivityManager.TETHER_ERROR_UNSUPPORTED;
1955 }
Robert Greenwalt8e87f122010-02-11 18:18:40 -08001956 }
1957
1958 // TODO - proper iface API for selection by property, inspection, etc
1959 public String[] getTetherableUsbRegexs() {
1960 enforceTetherAccessPermission();
1961 if (isTetheringSupported()) {
1962 return mTethering.getTetherableUsbRegexs();
1963 } else {
1964 return new String[0];
1965 }
1966 }
1967
1968 public String[] getTetherableWifiRegexs() {
1969 enforceTetherAccessPermission();
1970 if (isTetheringSupported()) {
1971 return mTethering.getTetherableWifiRegexs();
1972 } else {
1973 return new String[0];
1974 }
Robert Greenwalt0c4828c2010-01-26 11:40:34 -08001975 }
1976
Danica Chang96567052010-08-11 14:54:43 -07001977 public String[] getTetherableBluetoothRegexs() {
1978 enforceTetherAccessPermission();
1979 if (isTetheringSupported()) {
1980 return mTethering.getTetherableBluetoothRegexs();
1981 } else {
1982 return new String[0];
1983 }
1984 }
1985
Robert Greenwalt0c4828c2010-01-26 11:40:34 -08001986 // TODO - move iface listing, queries, etc to new module
1987 // javadoc from interface
1988 public String[] getTetherableIfaces() {
Robert Greenwalt8e87f122010-02-11 18:18:40 -08001989 enforceTetherAccessPermission();
Robert Greenwalt0c4828c2010-01-26 11:40:34 -08001990 return mTethering.getTetherableIfaces();
1991 }
1992
1993 public String[] getTetheredIfaces() {
Robert Greenwalt8e87f122010-02-11 18:18:40 -08001994 enforceTetherAccessPermission();
Robert Greenwalt0c4828c2010-01-26 11:40:34 -08001995 return mTethering.getTetheredIfaces();
1996 }
Robert Greenwalt8e87f122010-02-11 18:18:40 -08001997
Robert Greenwalt4283ded2010-03-02 17:25:02 -08001998 public String[] getTetheringErroredIfaces() {
1999 enforceTetherAccessPermission();
2000 return mTethering.getErroredIfaces();
2001 }
2002
Robert Greenwalt8e87f122010-02-11 18:18:40 -08002003 // if ro.tether.denied = true we default to no tethering
2004 // gservices could set the secure setting to 1 though to enable it on a build where it
2005 // had previously been turned off.
2006 public boolean isTetheringSupported() {
2007 enforceTetherAccessPermission();
2008 int defaultVal = (SystemProperties.get("ro.tether.denied").equals("true") ? 0 : 1);
Robert Greenwaltf1b66e12010-02-25 12:29:30 -08002009 boolean tetherEnabledInSettings = (Settings.Secure.getInt(mContext.getContentResolver(),
2010 Settings.Secure.TETHER_SUPPORTED, defaultVal) != 0);
2011 return tetherEnabledInSettings && mTetheringConfigValid;
Robert Greenwalt8e87f122010-02-11 18:18:40 -08002012 }
Robert Greenwalt93dc1042010-06-15 12:19:37 -07002013
2014 // An API NetworkStateTrackers can call when they lose their network.
2015 // This will automatically be cleared after X seconds or a network becomes CONNECTED,
2016 // whichever happens first. The timer is started by the first caller and not
2017 // restarted by subsequent callers.
2018 public void requestNetworkTransitionWakelock(String forWhom) {
2019 enforceConnectivityInternalPermission();
2020 synchronized (this) {
2021 if (mNetTransitionWakeLock.isHeld()) return;
2022 mNetTransitionWakeLockSerialNumber++;
2023 mNetTransitionWakeLock.acquire();
2024 mNetTransitionWakeLockCausedBy = forWhom;
2025 }
2026 mHandler.sendMessageDelayed(mHandler.obtainMessage(
Robert Greenwaltccb36f92010-09-24 14:32:21 -07002027 EVENT_CLEAR_NET_TRANSITION_WAKELOCK,
Robert Greenwalt93dc1042010-06-15 12:19:37 -07002028 mNetTransitionWakeLockSerialNumber, 0),
2029 mNetTransitionWakeLockTimeout);
2030 return;
2031 }
Robert Greenwalt24118e82010-09-09 13:15:32 -07002032
Robert Greenwalt986c7412010-09-08 15:24:47 -07002033 // 100 percent is full good, 0 is full bad.
2034 public void reportInetCondition(int networkType, int percentage) {
Wink Savillee70c6f52010-12-03 12:01:38 -08002035 if (DBG) log("reportNetworkCondition(" + networkType + ", " + percentage + ")");
Robert Greenwalt986c7412010-09-08 15:24:47 -07002036 mContext.enforceCallingOrSelfPermission(
2037 android.Manifest.permission.STATUS_BAR,
2038 "ConnectivityService");
2039
Robert Greenwalt0e80be12010-09-20 14:35:25 -07002040 if (DBG) {
2041 int pid = getCallingPid();
2042 int uid = getCallingUid();
2043 String s = pid + "(" + uid + ") reports inet is " +
2044 (percentage > 50 ? "connected" : "disconnected") + " (" + percentage + ") on " +
2045 "network Type " + networkType + " at " + GregorianCalendar.getInstance().getTime();
2046 mInetLog.add(s);
2047 while(mInetLog.size() > INET_CONDITION_LOG_MAX_SIZE) {
2048 mInetLog.remove(0);
2049 }
2050 }
Robert Greenwalt986c7412010-09-08 15:24:47 -07002051 mHandler.sendMessage(mHandler.obtainMessage(
Robert Greenwalt6a2db8a2010-09-23 10:05:56 -07002052 EVENT_INET_CONDITION_CHANGE, networkType, percentage));
2053 }
2054
2055 private void handleInetConditionChange(int netType, int condition) {
2056 if (DBG) {
Wink Savillee70c6f52010-12-03 12:01:38 -08002057 log("Inet connectivity change, net=" +
Robert Greenwalt6a2db8a2010-09-23 10:05:56 -07002058 netType + ", condition=" + condition +
2059 ",mActiveDefaultNetwork=" + mActiveDefaultNetwork);
2060 }
2061 if (mActiveDefaultNetwork == -1) {
Wink Savillee70c6f52010-12-03 12:01:38 -08002062 if (DBG) log("no active default network - aborting");
Robert Greenwalt6a2db8a2010-09-23 10:05:56 -07002063 return;
2064 }
2065 if (mActiveDefaultNetwork != netType) {
Wink Savillee70c6f52010-12-03 12:01:38 -08002066 if (DBG) log("given net not default - aborting");
Robert Greenwalt6a2db8a2010-09-23 10:05:56 -07002067 return;
2068 }
2069 mDefaultInetCondition = condition;
2070 int delay;
2071 if (mInetConditionChangeInFlight == false) {
Wink Savillee70c6f52010-12-03 12:01:38 -08002072 if (DBG) log("starting a change hold");
Robert Greenwalt6a2db8a2010-09-23 10:05:56 -07002073 // setup a new hold to debounce this
2074 if (mDefaultInetCondition > 50) {
2075 delay = Settings.Secure.getInt(mContext.getContentResolver(),
2076 Settings.Secure.INET_CONDITION_DEBOUNCE_UP_DELAY, 500);
2077 } else {
2078 delay = Settings.Secure.getInt(mContext.getContentResolver(),
2079 Settings.Secure.INET_CONDITION_DEBOUNCE_DOWN_DELAY, 3000);
2080 }
2081 mInetConditionChangeInFlight = true;
2082 mHandler.sendMessageDelayed(mHandler.obtainMessage(EVENT_INET_CONDITION_HOLD_END,
2083 mActiveDefaultNetwork, mDefaultConnectionSequence), delay);
2084 } else {
2085 // we've set the new condition, when this hold ends that will get
2086 // picked up
Wink Savillee70c6f52010-12-03 12:01:38 -08002087 if (DBG) log("currently in hold - not setting new end evt");
Robert Greenwalt6a2db8a2010-09-23 10:05:56 -07002088 }
2089 }
2090
2091 private void handleInetConditionHoldEnd(int netType, int sequence) {
2092 if (DBG) {
Wink Savillee70c6f52010-12-03 12:01:38 -08002093 log("Inet hold end, net=" + netType +
Robert Greenwalt6a2db8a2010-09-23 10:05:56 -07002094 ", condition =" + mDefaultInetCondition +
2095 ", published condition =" + mDefaultInetConditionPublished);
2096 }
2097 mInetConditionChangeInFlight = false;
2098
2099 if (mActiveDefaultNetwork == -1) {
Wink Savillee70c6f52010-12-03 12:01:38 -08002100 if (DBG) log("no active default network - aborting");
Robert Greenwalt6a2db8a2010-09-23 10:05:56 -07002101 return;
2102 }
2103 if (mDefaultConnectionSequence != sequence) {
Wink Savillee70c6f52010-12-03 12:01:38 -08002104 if (DBG) log("event hold for obsolete network - aborting");
Robert Greenwalt6a2db8a2010-09-23 10:05:56 -07002105 return;
2106 }
2107 if (mDefaultInetConditionPublished == mDefaultInetCondition) {
Wink Savillee70c6f52010-12-03 12:01:38 -08002108 if (DBG) log("no change in condition - aborting");
Robert Greenwalt6a2db8a2010-09-23 10:05:56 -07002109 return;
2110 }
2111 NetworkInfo networkInfo = mNetTrackers[mActiveDefaultNetwork].getNetworkInfo();
2112 if (networkInfo.isConnected() == false) {
Wink Savillee70c6f52010-12-03 12:01:38 -08002113 if (DBG) log("default network not connected - aborting");
Robert Greenwalt6a2db8a2010-09-23 10:05:56 -07002114 return;
2115 }
2116 mDefaultInetConditionPublished = mDefaultInetCondition;
2117 sendInetConditionBroadcast(networkInfo);
2118 return;
Robert Greenwalt986c7412010-09-08 15:24:47 -07002119 }
Robert Greenwaltc3c5f862010-10-11 16:00:27 -07002120
2121 public synchronized ProxyProperties getProxy() {
2122 if (mGlobalProxy != null) return mGlobalProxy;
2123 if (mDefaultProxy != null) return mDefaultProxy;
2124 return null;
2125 }
2126
2127 public void setGlobalProxy(ProxyProperties proxyProperties) {
2128 enforceChangePermission();
2129 synchronized (mGlobalProxyLock) {
2130 if (proxyProperties == mGlobalProxy) return;
2131 if (proxyProperties != null && proxyProperties.equals(mGlobalProxy)) return;
2132 if (mGlobalProxy != null && mGlobalProxy.equals(proxyProperties)) return;
2133
2134 String host = "";
2135 int port = 0;
2136 String exclList = "";
2137 if (proxyProperties != null && !TextUtils.isEmpty(proxyProperties.getHost())) {
2138 mGlobalProxy = new ProxyProperties(proxyProperties);
2139 host = mGlobalProxy.getHost();
2140 port = mGlobalProxy.getPort();
2141 exclList = mGlobalProxy.getExclusionList();
2142 } else {
2143 mGlobalProxy = null;
2144 }
2145 ContentResolver res = mContext.getContentResolver();
2146 Settings.Secure.putString(res, Settings.Secure.GLOBAL_HTTP_PROXY_HOST, host);
2147 Settings.Secure.putInt(res, Settings.Secure.GLOBAL_HTTP_PROXY_PORT, port);
Robert Greenwalt6f7c6092010-12-02 11:31:00 -08002148 Settings.Secure.putString(res, Settings.Secure.GLOBAL_HTTP_PROXY_EXCLUSION_LIST,
Robert Greenwaltc3c5f862010-10-11 16:00:27 -07002149 exclList);
2150 }
2151
2152 if (mGlobalProxy == null) {
2153 proxyProperties = mDefaultProxy;
2154 }
2155 sendProxyBroadcast(proxyProperties);
2156 }
2157
Robert Greenwalt6f7c6092010-12-02 11:31:00 -08002158 private void loadGlobalProxy() {
2159 ContentResolver res = mContext.getContentResolver();
2160 String host = Settings.Secure.getString(res, Settings.Secure.GLOBAL_HTTP_PROXY_HOST);
2161 int port = Settings.Secure.getInt(res, Settings.Secure.GLOBAL_HTTP_PROXY_PORT, 0);
2162 String exclList = Settings.Secure.getString(res,
2163 Settings.Secure.GLOBAL_HTTP_PROXY_EXCLUSION_LIST);
2164 if (!TextUtils.isEmpty(host)) {
2165 ProxyProperties proxyProperties = new ProxyProperties(host, port, exclList);
2166 synchronized (mGlobalProxyLock) {
2167 mGlobalProxy = proxyProperties;
2168 }
2169 }
2170 }
2171
Robert Greenwaltc3c5f862010-10-11 16:00:27 -07002172 public ProxyProperties getGlobalProxy() {
2173 synchronized (mGlobalProxyLock) {
2174 return mGlobalProxy;
2175 }
2176 }
2177
2178 private void handleApplyDefaultProxy(int type) {
2179 // check if new default - push it out to all VM if so
2180 ProxyProperties proxy = mNetTrackers[type].getLinkProperties().getHttpProxy();
2181 synchronized (this) {
2182 if (mDefaultProxy != null && mDefaultProxy.equals(proxy)) return;
2183 if (mDefaultProxy == proxy) return;
2184 if (!TextUtils.isEmpty(proxy.getHost())) {
2185 mDefaultProxy = proxy;
2186 } else {
2187 mDefaultProxy = null;
2188 }
2189 }
Wink Savillee70c6f52010-12-03 12:01:38 -08002190 if (DBG) log("changing default proxy to " + proxy);
Robert Greenwaltc3c5f862010-10-11 16:00:27 -07002191 if ((proxy == null && mGlobalProxy == null) || proxy.equals(mGlobalProxy)) return;
2192 if (mGlobalProxy != null) return;
2193 sendProxyBroadcast(proxy);
2194 }
2195
2196 private void handleDeprecatedGlobalHttpProxy() {
2197 String proxy = Settings.Secure.getString(mContext.getContentResolver(),
2198 Settings.Secure.HTTP_PROXY);
2199 if (!TextUtils.isEmpty(proxy)) {
2200 String data[] = proxy.split(":");
2201 String proxyHost = data[0];
2202 int proxyPort = 8080;
2203 if (data.length > 1) {
2204 try {
2205 proxyPort = Integer.parseInt(data[1]);
2206 } catch (NumberFormatException e) {
2207 return;
2208 }
2209 }
2210 ProxyProperties p = new ProxyProperties(data[0], proxyPort, "");
2211 setGlobalProxy(p);
2212 }
2213 }
2214
2215 private void sendProxyBroadcast(ProxyProperties proxy) {
Robert Greenwalt611291c2010-12-23 15:51:10 -08002216 if (proxy == null) proxy = new ProxyProperties("", 0, "");
Wink Savillee70c6f52010-12-03 12:01:38 -08002217 log("sending Proxy Broadcast for " + proxy);
Robert Greenwaltc3c5f862010-10-11 16:00:27 -07002218 Intent intent = new Intent(Proxy.PROXY_CHANGE_ACTION);
Stan Chesnutt1f2a2ac2011-01-06 11:00:19 -08002219 intent.addFlags(Intent.FLAG_RECEIVER_REPLACE_PENDING |
2220 Intent.FLAG_RECEIVER_REGISTERED_ONLY_BEFORE_BOOT);
Robert Greenwaltc3c5f862010-10-11 16:00:27 -07002221 intent.putExtra(Proxy.EXTRA_PROXY_INFO, proxy);
Robert Greenwaltd93dc8f2010-12-06 11:29:17 -08002222 mContext.sendStickyBroadcast(intent);
Robert Greenwaltc3c5f862010-10-11 16:00:27 -07002223 }
2224
2225 private static class SettingsObserver extends ContentObserver {
2226 private int mWhat;
2227 private Handler mHandler;
2228 SettingsObserver(Handler handler, int what) {
2229 super(handler);
2230 mHandler = handler;
2231 mWhat = what;
2232 }
2233
2234 void observe(Context context) {
2235 ContentResolver resolver = context.getContentResolver();
2236 resolver.registerContentObserver(Settings.Secure.getUriFor(
2237 Settings.Secure.HTTP_PROXY), false, this);
2238 }
2239
2240 @Override
2241 public void onChange(boolean selfChange) {
2242 mHandler.obtainMessage(mWhat).sendToTarget();
2243 }
2244 }
Wink Savillee70c6f52010-12-03 12:01:38 -08002245
Kazuhiro Ondo3a340412011-04-30 20:10:57 -05002246 private void handleTetherIfaceChange(int type) {
2247 String iface = mNetTrackers[type].getLinkProperties().getInterfaceName();
2248
2249 if (isTetheringSupported()) {
2250 mTethering.handleTetherIfaceChange(iface);
2251 }
2252 }
2253
Wink Savillee70c6f52010-12-03 12:01:38 -08002254 private void log(String s) {
2255 Slog.d(TAG, s);
2256 }
2257
2258 private void loge(String s) {
2259 Slog.e(TAG, s);
2260 }
Wink Savillef6b76692011-02-24 17:58:51 -08002261 int convertFeatureToNetworkType(String feature){
2262 int networkType = -1;
2263 if (TextUtils.equals(feature, Phone.FEATURE_ENABLE_MMS)) {
2264 networkType = ConnectivityManager.TYPE_MOBILE_MMS;
2265 } else if (TextUtils.equals(feature, Phone.FEATURE_ENABLE_SUPL)) {
2266 networkType = ConnectivityManager.TYPE_MOBILE_SUPL;
2267 } else if (TextUtils.equals(feature, Phone.FEATURE_ENABLE_DUN) ||
2268 TextUtils.equals(feature, Phone.FEATURE_ENABLE_DUN_ALWAYS)) {
2269 networkType = ConnectivityManager.TYPE_MOBILE_DUN;
2270 } else if (TextUtils.equals(feature, Phone.FEATURE_ENABLE_HIPRI)) {
2271 networkType = ConnectivityManager.TYPE_MOBILE_HIPRI;
2272 } else if (TextUtils.equals(feature, Phone.FEATURE_ENABLE_FOTA)) {
2273 networkType = ConnectivityManager.TYPE_MOBILE_FOTA;
2274 } else if (TextUtils.equals(feature, Phone.FEATURE_ENABLE_IMS)) {
2275 networkType = ConnectivityManager.TYPE_MOBILE_IMS;
2276 } else if (TextUtils.equals(feature, Phone.FEATURE_ENABLE_CBS)) {
2277 networkType = ConnectivityManager.TYPE_MOBILE_CBS;
2278 }
2279 return networkType;
2280 }
The Android Open Source Project28527d22009-03-03 19:31:44 -08002281}