blob: de19d3173a172d0c895a9ada787211c8a86d9a28 [file] [log] [blame]
The Android Open Source Project28527d22009-03-03 19:31:44 -08001/*
2 * Copyright (C) 2008 The Android Open Source Project
3 *
4 * Licensed under the Apache License, Version 2.0 (the "License");
5 * you may not use this file except in compliance with the License.
6 * You may obtain a copy of the License at
7 *
8 * http://www.apache.org/licenses/LICENSE-2.0
9 *
10 * Unless required by applicable law or agreed to in writing, software
11 * distributed under the License is distributed on an "AS IS" BASIS,
12 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13 * See the License for the specific language governing permissions and
14 * limitations under the License.
15 */
16
17package com.android.server;
18
Jaikumar Ganesh0db51a02010-12-21 22:31:44 -080019import android.bluetooth.BluetoothTetheringDataTracker;
The Android Open Source Project28527d22009-03-03 19:31:44 -080020import android.content.ContentResolver;
21import android.content.Context;
22import android.content.Intent;
23import android.content.pm.PackageManager;
Robert Greenwaltc3c5f862010-10-11 16:00:27 -070024import android.database.ContentObserver;
The Android Open Source Project28527d22009-03-03 19:31:44 -080025import android.net.ConnectivityManager;
Robert Greenwalteb123ac2010-12-06 13:56:24 -080026import android.net.DummyDataStateTracker;
Benoit Goby6cec7f32010-12-22 14:29:40 -080027import android.net.EthernetDataTracker;
The Android Open Source Project28527d22009-03-03 19:31:44 -080028import android.net.IConnectivityManager;
Robert Greenwalt355205c2011-05-10 15:05:02 -070029import android.net.LinkAddress;
Jaikumar Ganesh0db51a02010-12-21 22:31:44 -080030import android.net.LinkProperties;
The Android Open Source Project28527d22009-03-03 19:31:44 -080031import android.net.MobileDataStateTracker;
Robert Greenwalt34848c02011-03-25 13:09:25 -070032import android.net.NetworkConfig;
The Android Open Source Project28527d22009-03-03 19:31:44 -080033import android.net.NetworkInfo;
34import android.net.NetworkStateTracker;
Robert Greenwalt7fe44cb2010-08-27 09:24:29 -070035import android.net.NetworkUtils;
Robert Greenwaltc3c5f862010-10-11 16:00:27 -070036import android.net.Proxy;
37import android.net.ProxyProperties;
Robert Greenwalt5a901292011-04-28 14:28:50 -070038import android.net.RouteInfo;
Hung-ying Tyan4e723422011-01-19 16:48:38 +080039import android.net.vpn.VpnManager;
The Android Open Source Project28527d22009-03-03 19:31:44 -080040import android.net.wifi.WifiStateTracker;
41import android.os.Binder;
42import android.os.Handler;
Wink Saville775aad62010-09-02 19:23:52 -070043import android.os.HandlerThread;
Robert Greenwalt2034b912009-08-12 16:08:25 -070044import android.os.IBinder;
Robert Greenwalt355205c2011-05-10 15:05:02 -070045import android.os.INetworkManagementService;
The Android Open Source Project28527d22009-03-03 19:31:44 -080046import android.os.Looper;
47import android.os.Message;
Robert Greenwalt93dc1042010-06-15 12:19:37 -070048import android.os.PowerManager;
Robert Greenwalt2034b912009-08-12 16:08:25 -070049import android.os.RemoteException;
The Android Open Source Project28527d22009-03-03 19:31:44 -080050import android.os.ServiceManager;
51import android.os.SystemProperties;
52import android.provider.Settings;
Robert Greenwalt2034b912009-08-12 16:08:25 -070053import android.text.TextUtils;
The Android Open Source Project28527d22009-03-03 19:31:44 -080054import android.util.EventLog;
Joe Onoratoc2386bb2010-02-26 18:56:32 -080055import android.util.Slog;
The Android Open Source Project28527d22009-03-03 19:31:44 -080056
Robert Greenwalt2034b912009-08-12 16:08:25 -070057import com.android.internal.telephony.Phone;
Robert Greenwalt0c4828c2010-01-26 11:40:34 -080058import com.android.server.connectivity.Tethering;
59
The Android Open Source Project28527d22009-03-03 19:31:44 -080060import java.io.FileDescriptor;
Irfan Sheriff7f132d92010-06-09 15:39:36 -070061import java.io.FileWriter;
62import java.io.IOException;
The Android Open Source Project28527d22009-03-03 19:31:44 -080063import java.io.PrintWriter;
Robert Greenwalta7dfbd32010-06-15 15:43:39 -070064import java.net.InetAddress;
Robert Greenwalt355205c2011-05-10 15:05:02 -070065import java.net.Inet4Address;
Robert Greenwalta7dfbd32010-06-15 15:43:39 -070066import java.net.UnknownHostException;
Robert Greenwalt2034b912009-08-12 16:08:25 -070067import java.util.ArrayList;
Robert Greenwalta7dfbd32010-06-15 15:43:39 -070068import java.util.Collection;
Robert Greenwaltd62c7002010-12-29 16:15:02 -080069import java.util.concurrent.atomic.AtomicBoolean;
Robert Greenwalt0e80be12010-09-20 14:35:25 -070070import java.util.GregorianCalendar;
Robert Greenwalt2034b912009-08-12 16:08:25 -070071import java.util.List;
The Android Open Source Project28527d22009-03-03 19:31:44 -080072
73/**
74 * @hide
75 */
76public class ConnectivityService extends IConnectivityManager.Stub {
77
Robert Greenwalt063dc7d2010-10-05 19:12:26 -070078 private static final boolean DBG = true;
The Android Open Source Project28527d22009-03-03 19:31:44 -080079 private static final String TAG = "ConnectivityService";
80
Robert Greenwalt2034b912009-08-12 16:08:25 -070081 // how long to wait before switching back to a radio's default network
82 private static final int RESTORE_DEFAULT_NETWORK_DELAY = 1 * 60 * 1000;
83 // system property that can override the above value
84 private static final String NETWORK_RESTORE_DELAY_PROP_NAME =
85 "android.telephony.apn-restore";
86
Robert Greenwaltbd492212011-05-06 17:10:53 -070087 // used in recursive route setting to add gateways for the host for which
88 // a host route was requested.
89 private static final int MAX_HOSTROUTE_CYCLE_COUNT = 10;
90
Robert Greenwalt0c4828c2010-01-26 11:40:34 -080091 private Tethering mTethering;
Robert Greenwaltf1b66e12010-02-25 12:29:30 -080092 private boolean mTetheringConfigValid = false;
Robert Greenwalt0c4828c2010-01-26 11:40:34 -080093
The Android Open Source Project28527d22009-03-03 19:31:44 -080094 /**
95 * Sometimes we want to refer to the individual network state
96 * trackers separately, and sometimes we just want to treat them
97 * abstractly.
98 */
99 private NetworkStateTracker mNetTrackers[];
Robert Greenwalt2034b912009-08-12 16:08:25 -0700100
101 /**
102 * A per Net list of the PID's that requested access to the net
103 * used both as a refcount and for per-PID DNS selection
104 */
105 private List mNetRequestersPids[];
106
Irfan Sheriff653e2a22010-06-07 09:03:04 -0700107 private WifiWatchdogService mWifiWatchdogService;
108
Robert Greenwalt2034b912009-08-12 16:08:25 -0700109 // priority order of the nettrackers
110 // (excluding dynamically set mNetworkPreference)
111 // TODO - move mNetworkTypePreference into this
112 private int[] mPriorityList;
113
The Android Open Source Project28527d22009-03-03 19:31:44 -0800114 private Context mContext;
115 private int mNetworkPreference;
Robert Greenwalt2034b912009-08-12 16:08:25 -0700116 private int mActiveDefaultNetwork = -1;
Robert Greenwalt986c7412010-09-08 15:24:47 -0700117 // 0 is full bad, 100 is full good
118 private int mDefaultInetCondition = 0;
119 private int mDefaultInetConditionPublished = 0;
120 private boolean mInetConditionChangeInFlight = false;
121 private int mDefaultConnectionSequence = 0;
The Android Open Source Project28527d22009-03-03 19:31:44 -0800122
123 private int mNumDnsEntries;
The Android Open Source Project28527d22009-03-03 19:31:44 -0800124
125 private boolean mTestMode;
Joe Onorato56023ad2010-09-01 21:18:22 -0700126 private static ConnectivityService sServiceInstance;
The Android Open Source Project28527d22009-03-03 19:31:44 -0800127
Robert Greenwaltd62c7002010-12-29 16:15:02 -0800128 private AtomicBoolean mBackgroundDataEnabled = new AtomicBoolean(true);
129
Robert Greenwalt355205c2011-05-10 15:05:02 -0700130 private INetworkManagementService mNetd;
131
Robert Greenwalt6a2db8a2010-09-23 10:05:56 -0700132 private static final int ENABLED = 1;
133 private static final int DISABLED = 0;
134
135 // Share the event space with NetworkStateTracker (which can't see this
136 // internal class but sends us events). If you change these, change
137 // NetworkStateTracker.java too.
138 private static final int MIN_NETWORK_STATE_TRACKER_EVENT = 1;
139 private static final int MAX_NETWORK_STATE_TRACKER_EVENT = 100;
140
141 /**
142 * used internally as a delayed event to make us switch back to the
143 * default network
144 */
145 private static final int EVENT_RESTORE_DEFAULT_NETWORK =
146 MAX_NETWORK_STATE_TRACKER_EVENT + 1;
147
148 /**
149 * used internally to change our mobile data enabled flag
150 */
151 private static final int EVENT_CHANGE_MOBILE_DATA_ENABLED =
152 MAX_NETWORK_STATE_TRACKER_EVENT + 2;
153
154 /**
155 * used internally to change our network preference setting
156 * arg1 = networkType to prefer
157 */
158 private static final int EVENT_SET_NETWORK_PREFERENCE =
159 MAX_NETWORK_STATE_TRACKER_EVENT + 3;
160
161 /**
162 * used internally to synchronize inet condition reports
163 * arg1 = networkType
164 * arg2 = condition (0 bad, 100 good)
165 */
166 private static final int EVENT_INET_CONDITION_CHANGE =
167 MAX_NETWORK_STATE_TRACKER_EVENT + 4;
168
169 /**
170 * used internally to mark the end of inet condition hold periods
171 * arg1 = networkType
172 */
173 private static final int EVENT_INET_CONDITION_HOLD_END =
174 MAX_NETWORK_STATE_TRACKER_EVENT + 5;
175
176 /**
177 * used internally to set the background data preference
178 * arg1 = TRUE for enabled, FALSE for disabled
179 */
180 private static final int EVENT_SET_BACKGROUND_DATA =
181 MAX_NETWORK_STATE_TRACKER_EVENT + 6;
182
183 /**
184 * used internally to set enable/disable cellular data
185 * arg1 = ENBALED or DISABLED
186 */
187 private static final int EVENT_SET_MOBILE_DATA =
188 MAX_NETWORK_STATE_TRACKER_EVENT + 7;
189
Robert Greenwaltccb36f92010-09-24 14:32:21 -0700190 /**
191 * used internally to clear a wakelock when transitioning
192 * from one net to another
193 */
194 private static final int EVENT_CLEAR_NET_TRANSITION_WAKELOCK =
195 MAX_NETWORK_STATE_TRACKER_EVENT + 8;
196
Robert Greenwaltc3c5f862010-10-11 16:00:27 -0700197 /**
198 * used internally to reload global proxy settings
199 */
200 private static final int EVENT_APPLY_GLOBAL_HTTP_PROXY =
201 MAX_NETWORK_STATE_TRACKER_EVENT + 9;
202
Robert Greenwalt34848c02011-03-25 13:09:25 -0700203 /**
204 * used internally to set external dependency met/unmet
205 * arg1 = ENABLED (met) or DISABLED (unmet)
206 * arg2 = NetworkType
207 */
208 private static final int EVENT_SET_DEPENDENCY_MET =
209 MAX_NETWORK_STATE_TRACKER_EVENT + 10;
210
Robert Greenwalt2034b912009-08-12 16:08:25 -0700211 private Handler mHandler;
212
213 // list of DeathRecipients used to make sure features are turned off when
214 // a process dies
215 private List mFeatureUsers;
216
Mike Lockwoodfde2b762009-08-14 14:18:49 -0400217 private boolean mSystemReady;
Dianne Hackborna417ff82009-12-08 19:45:14 -0800218 private Intent mInitialBroadcast;
Mike Lockwoodfde2b762009-08-14 14:18:49 -0400219
Robert Greenwalt93dc1042010-06-15 12:19:37 -0700220 private PowerManager.WakeLock mNetTransitionWakeLock;
221 private String mNetTransitionWakeLockCausedBy = "";
222 private int mNetTransitionWakeLockSerialNumber;
223 private int mNetTransitionWakeLockTimeout;
224
Robert Greenwalt94daa182010-09-01 11:34:05 -0700225 private InetAddress mDefaultDns;
226
Robert Greenwalt0e80be12010-09-20 14:35:25 -0700227 // used in DBG mode to track inet condition reports
228 private static final int INET_CONDITION_LOG_MAX_SIZE = 15;
229 private ArrayList mInetLog;
230
Robert Greenwaltc3c5f862010-10-11 16:00:27 -0700231 // track the current default http proxy - tell the world if we get a new one (real change)
232 private ProxyProperties mDefaultProxy = null;
233 // track the global proxy.
234 private ProxyProperties mGlobalProxy = null;
235 private final Object mGlobalProxyLock = new Object();
236
237 private SettingsObserver mSettingsObserver;
238
Robert Greenwalt34848c02011-03-25 13:09:25 -0700239 NetworkConfig[] mNetConfigs;
Robert Greenwaltec05b3c2009-10-30 14:17:42 -0700240 int mNetworksDefined;
Robert Greenwalt2034b912009-08-12 16:08:25 -0700241
Robert Greenwalt12c44552009-12-07 11:33:18 -0800242 private static class RadioAttributes {
Robert Greenwalt2034b912009-08-12 16:08:25 -0700243 public int mSimultaneity;
244 public int mType;
245 public RadioAttributes(String init) {
246 String fragments[] = init.split(",");
Robert Greenwaltec05b3c2009-10-30 14:17:42 -0700247 mType = Integer.parseInt(fragments[0]);
248 mSimultaneity = Integer.parseInt(fragments[1]);
Robert Greenwalt2034b912009-08-12 16:08:25 -0700249 }
250 }
251 RadioAttributes[] mRadioAttributes;
252
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 Goby6cec7f32010-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
Robert Greenwaltc0b6c602010-03-11 15:03:08 -0800433 mTethering = new Tethering(mContext, mHandler.getLooper());
Robert Greenwaltf1b66e12010-02-25 12:29:30 -0800434 mTetheringConfigValid = (((mNetTrackers[ConnectivityManager.TYPE_MOBILE_DUN] != null) ||
435 !mTethering.isDunRequired()) &&
436 (mTethering.getTetherableUsbRegexs().length != 0 ||
Danica Chang96567052010-08-11 14:54:43 -0700437 mTethering.getTetherableWifiRegexs().length != 0 ||
438 mTethering.getTetherableBluetoothRegexs().length != 0) &&
Robert Greenwaltf1b66e12010-02-25 12:29:30 -0800439 mTethering.getUpstreamIfaceRegexs().length != 0);
440
Robert Greenwalt0e80be12010-09-20 14:35:25 -0700441 if (DBG) {
442 mInetLog = new ArrayList();
443 }
Robert Greenwaltc3c5f862010-10-11 16:00:27 -0700444
445 mSettingsObserver = new SettingsObserver(mHandler, EVENT_APPLY_GLOBAL_HTTP_PROXY);
446 mSettingsObserver.observe(mContext);
Robert Greenwalt6f7c6092010-12-02 11:31:00 -0800447
448 loadGlobalProxy();
Hung-ying Tyan4e723422011-01-19 16:48:38 +0800449
450 VpnManager.startVpnService(context);
The Android Open Source Project28527d22009-03-03 19:31:44 -0800451 }
452
Robert Greenwaltec05b3c2009-10-30 14:17:42 -0700453
The Android Open Source Project28527d22009-03-03 19:31:44 -0800454 /**
Robert Greenwalt0659da32009-07-16 17:21:39 -0700455 * Sets the preferred network.
The Android Open Source Project28527d22009-03-03 19:31:44 -0800456 * @param preference the new preference
457 */
Robert Greenwalt6a2db8a2010-09-23 10:05:56 -0700458 public void setNetworkPreference(int preference) {
The Android Open Source Project28527d22009-03-03 19:31:44 -0800459 enforceChangePermission();
Robert Greenwalt6a2db8a2010-09-23 10:05:56 -0700460
461 mHandler.sendMessage(mHandler.obtainMessage(EVENT_SET_NETWORK_PREFERENCE, preference, 0));
The Android Open Source Project28527d22009-03-03 19:31:44 -0800462 }
463
464 public int getNetworkPreference() {
465 enforceAccessPermission();
Robert Greenwalt6a2db8a2010-09-23 10:05:56 -0700466 int preference;
467 synchronized(this) {
468 preference = mNetworkPreference;
469 }
470 return preference;
The Android Open Source Project28527d22009-03-03 19:31:44 -0800471 }
472
Robert Greenwalt6a2db8a2010-09-23 10:05:56 -0700473 private void handleSetNetworkPreference(int preference) {
474 if (ConnectivityManager.isNetworkTypeValid(preference) &&
Robert Greenwalt34848c02011-03-25 13:09:25 -0700475 mNetConfigs[preference] != null &&
476 mNetConfigs[preference].isDefault()) {
Robert Greenwalt6a2db8a2010-09-23 10:05:56 -0700477 if (mNetworkPreference != preference) {
478 final ContentResolver cr = mContext.getContentResolver();
479 Settings.Secure.putInt(cr, Settings.Secure.NETWORK_PREFERENCE, preference);
480 synchronized(this) {
481 mNetworkPreference = preference;
482 }
483 enforcePreference();
484 }
485 }
The Android Open Source Project28527d22009-03-03 19:31:44 -0800486 }
Robert Greenwalt0659da32009-07-16 17:21:39 -0700487
The Android Open Source Project28527d22009-03-03 19:31:44 -0800488 private int getPersistedNetworkPreference() {
489 final ContentResolver cr = mContext.getContentResolver();
490
491 final int networkPrefSetting = Settings.Secure
492 .getInt(cr, Settings.Secure.NETWORK_PREFERENCE, -1);
493 if (networkPrefSetting != -1) {
494 return networkPrefSetting;
495 }
496
497 return ConnectivityManager.DEFAULT_NETWORK_PREFERENCE;
498 }
Robert Greenwalt0659da32009-07-16 17:21:39 -0700499
The Android Open Source Project28527d22009-03-03 19:31:44 -0800500 /**
Robert Greenwalt0659da32009-07-16 17:21:39 -0700501 * Make the state of network connectivity conform to the preference settings
The Android Open Source Project28527d22009-03-03 19:31:44 -0800502 * In this method, we only tear down a non-preferred network. Establishing
503 * a connection to the preferred network is taken care of when we handle
504 * the disconnect event from the non-preferred network
505 * (see {@link #handleDisconnect(NetworkInfo)}).
506 */
507 private void enforcePreference() {
Robert Greenwalt2034b912009-08-12 16:08:25 -0700508 if (mNetTrackers[mNetworkPreference].getNetworkInfo().isConnected())
The Android Open Source Project28527d22009-03-03 19:31:44 -0800509 return;
510
Robert Greenwalt2034b912009-08-12 16:08:25 -0700511 if (!mNetTrackers[mNetworkPreference].isAvailable())
512 return;
The Android Open Source Project28527d22009-03-03 19:31:44 -0800513
Robert Greenwalt2034b912009-08-12 16:08:25 -0700514 for (int t=0; t <= ConnectivityManager.MAX_RADIO_TYPE; t++) {
Robert Greenwaltec05b3c2009-10-30 14:17:42 -0700515 if (t != mNetworkPreference && mNetTrackers[t] != null &&
Robert Greenwalt2034b912009-08-12 16:08:25 -0700516 mNetTrackers[t].getNetworkInfo().isConnected()) {
Robert Greenwaltf3f045b2009-08-20 15:25:14 -0700517 if (DBG) {
Wink Savillee70c6f52010-12-03 12:01:38 -0800518 log("tearing down " + mNetTrackers[t].getNetworkInfo() +
Robert Greenwaltf3f045b2009-08-20 15:25:14 -0700519 " in enforcePreference");
520 }
Robert Greenwalt2034b912009-08-12 16:08:25 -0700521 teardown(mNetTrackers[t]);
The Android Open Source Project28527d22009-03-03 19:31:44 -0800522 }
523 }
524 }
525
526 private boolean teardown(NetworkStateTracker netTracker) {
527 if (netTracker.teardown()) {
528 netTracker.setTeardownRequested(true);
529 return true;
530 } else {
531 return false;
532 }
533 }
534
535 /**
536 * Return NetworkInfo for the active (i.e., connected) network interface.
537 * It is assumed that at most one network is active at a time. If more
538 * than one is active, it is indeterminate which will be returned.
Robert Greenwalt0659da32009-07-16 17:21:39 -0700539 * @return the info for the active network, or {@code null} if none is
540 * active
The Android Open Source Project28527d22009-03-03 19:31:44 -0800541 */
542 public NetworkInfo getActiveNetworkInfo() {
543 enforceAccessPermission();
Robert Greenwalta71471b2011-03-24 21:41:41 -0700544 if (mActiveDefaultNetwork != -1) {
545 return mNetTrackers[mActiveDefaultNetwork].getNetworkInfo();
The Android Open Source Project28527d22009-03-03 19:31:44 -0800546 }
547 return null;
548 }
549
550 public NetworkInfo getNetworkInfo(int networkType) {
551 enforceAccessPermission();
552 if (ConnectivityManager.isNetworkTypeValid(networkType)) {
553 NetworkStateTracker t = mNetTrackers[networkType];
554 if (t != null)
555 return t.getNetworkInfo();
556 }
557 return null;
558 }
559
560 public NetworkInfo[] getAllNetworkInfo() {
561 enforceAccessPermission();
Robert Greenwaltec05b3c2009-10-30 14:17:42 -0700562 NetworkInfo[] result = new NetworkInfo[mNetworksDefined];
The Android Open Source Project28527d22009-03-03 19:31:44 -0800563 int i = 0;
564 for (NetworkStateTracker t : mNetTrackers) {
Robert Greenwaltec05b3c2009-10-30 14:17:42 -0700565 if(t != null) result[i++] = t.getNetworkInfo();
The Android Open Source Project28527d22009-03-03 19:31:44 -0800566 }
567 return result;
568 }
569
Robert Greenwalt9f0ee4f2010-09-14 09:18:02 -0700570 /**
571 * Return LinkProperties for the active (i.e., connected) default
572 * network interface. It is assumed that at most one default network
573 * is active at a time. If more than one is active, it is indeterminate
574 * which will be returned.
575 * @return the ip properties for the active network, or {@code null} if
576 * none is active
577 */
578 public LinkProperties getActiveLinkProperties() {
579 enforceAccessPermission();
580 for (int type=0; type <= ConnectivityManager.MAX_NETWORK_TYPE; type++) {
Robert Greenwalt34848c02011-03-25 13:09:25 -0700581 if (mNetConfigs[type] == null || !mNetConfigs[type].isDefault()) {
Robert Greenwalt9f0ee4f2010-09-14 09:18:02 -0700582 continue;
583 }
584 NetworkStateTracker t = mNetTrackers[type];
585 NetworkInfo info = t.getNetworkInfo();
586 if (info.isConnected()) {
587 return t.getLinkProperties();
588 }
589 }
590 return null;
591 }
592
593 public LinkProperties getLinkProperties(int networkType) {
594 enforceAccessPermission();
595 if (ConnectivityManager.isNetworkTypeValid(networkType)) {
596 NetworkStateTracker t = mNetTrackers[networkType];
597 if (t != null) return t.getLinkProperties();
598 }
599 return null;
600 }
601
The Android Open Source Project28527d22009-03-03 19:31:44 -0800602 public boolean setRadios(boolean turnOn) {
603 boolean result = true;
604 enforceChangePermission();
605 for (NetworkStateTracker t : mNetTrackers) {
Robert Greenwaltec05b3c2009-10-30 14:17:42 -0700606 if (t != null) result = t.setRadio(turnOn) && result;
The Android Open Source Project28527d22009-03-03 19:31:44 -0800607 }
608 return result;
609 }
610
611 public boolean setRadio(int netType, boolean turnOn) {
612 enforceChangePermission();
613 if (!ConnectivityManager.isNetworkTypeValid(netType)) {
614 return false;
615 }
616 NetworkStateTracker tracker = mNetTrackers[netType];
617 return tracker != null && tracker.setRadio(turnOn);
618 }
619
Robert Greenwaltaffc3a12009-09-27 17:27:04 -0700620 /**
621 * Used to notice when the calling process dies so we can self-expire
622 *
623 * Also used to know if the process has cleaned up after itself when
624 * our auto-expire timer goes off. The timer has a link to an object.
625 *
626 */
Robert Greenwalt2034b912009-08-12 16:08:25 -0700627 private class FeatureUser implements IBinder.DeathRecipient {
628 int mNetworkType;
629 String mFeature;
630 IBinder mBinder;
631 int mPid;
632 int mUid;
Robert Greenwalt3eeb6032009-12-21 18:24:07 -0800633 long mCreateTime;
Robert Greenwalt2034b912009-08-12 16:08:25 -0700634
635 FeatureUser(int type, String feature, IBinder binder) {
636 super();
637 mNetworkType = type;
638 mFeature = feature;
639 mBinder = binder;
640 mPid = getCallingPid();
641 mUid = getCallingUid();
Robert Greenwalt3eeb6032009-12-21 18:24:07 -0800642 mCreateTime = System.currentTimeMillis();
Robert Greenwaltaffc3a12009-09-27 17:27:04 -0700643
Robert Greenwalt2034b912009-08-12 16:08:25 -0700644 try {
645 mBinder.linkToDeath(this, 0);
646 } catch (RemoteException e) {
647 binderDied();
648 }
649 }
650
651 void unlinkDeathRecipient() {
652 mBinder.unlinkToDeath(this, 0);
653 }
654
655 public void binderDied() {
Wink Savillee70c6f52010-12-03 12:01:38 -0800656 log("ConnectivityService FeatureUser binderDied(" +
Robert Greenwalt3eeb6032009-12-21 18:24:07 -0800657 mNetworkType + ", " + mFeature + ", " + mBinder + "), created " +
658 (System.currentTimeMillis() - mCreateTime) + " mSec ago");
Robert Greenwaltaffc3a12009-09-27 17:27:04 -0700659 stopUsingNetworkFeature(this, false);
Robert Greenwalt2034b912009-08-12 16:08:25 -0700660 }
661
Robert Greenwaltaffc3a12009-09-27 17:27:04 -0700662 public void expire() {
Wink Savillee70c6f52010-12-03 12:01:38 -0800663 log("ConnectivityService FeatureUser expire(" +
Robert Greenwalt3eeb6032009-12-21 18:24:07 -0800664 mNetworkType + ", " + mFeature + ", " + mBinder +"), created " +
665 (System.currentTimeMillis() - mCreateTime) + " mSec ago");
Robert Greenwaltaffc3a12009-09-27 17:27:04 -0700666 stopUsingNetworkFeature(this, false);
667 }
Robert Greenwalt3eeb6032009-12-21 18:24:07 -0800668
669 public String toString() {
670 return "FeatureUser("+mNetworkType+","+mFeature+","+mPid+","+mUid+"), created " +
671 (System.currentTimeMillis() - mCreateTime) + " mSec ago";
672 }
Robert Greenwalt2034b912009-08-12 16:08:25 -0700673 }
674
Robert Greenwaltaffc3a12009-09-27 17:27:04 -0700675 // javadoc from interface
Robert Greenwalt2034b912009-08-12 16:08:25 -0700676 public int startUsingNetworkFeature(int networkType, String feature,
677 IBinder binder) {
678 if (DBG) {
Wink Savillee70c6f52010-12-03 12:01:38 -0800679 log("startUsingNetworkFeature for net " + networkType + ": " + feature);
Robert Greenwalt2034b912009-08-12 16:08:25 -0700680 }
The Android Open Source Project28527d22009-03-03 19:31:44 -0800681 enforceChangePermission();
Robert Greenwaltec05b3c2009-10-30 14:17:42 -0700682 if (!ConnectivityManager.isNetworkTypeValid(networkType) ||
Robert Greenwalt34848c02011-03-25 13:09:25 -0700683 mNetConfigs[networkType] == null) {
Robert Greenwalt2034b912009-08-12 16:08:25 -0700684 return Phone.APN_REQUEST_FAILED;
The Android Open Source Project28527d22009-03-03 19:31:44 -0800685 }
Robert Greenwalt2034b912009-08-12 16:08:25 -0700686
Robert Greenwaltaffc3a12009-09-27 17:27:04 -0700687 FeatureUser f = new FeatureUser(networkType, feature, binder);
Robert Greenwalt2034b912009-08-12 16:08:25 -0700688
689 // TODO - move this into the MobileDataStateTracker
690 int usedNetworkType = networkType;
691 if(networkType == ConnectivityManager.TYPE_MOBILE) {
Wink Savilleb7c92c72011-03-12 14:52:01 -0800692 usedNetworkType = convertFeatureToNetworkType(feature);
693 if (usedNetworkType < 0) {
694 Slog.e(TAG, "Can't match any netTracker!");
695 usedNetworkType = networkType;
Robert Greenwalt2034b912009-08-12 16:08:25 -0700696 }
697 }
698 NetworkStateTracker network = mNetTrackers[usedNetworkType];
699 if (network != null) {
Robert Greenwalt5364d752010-12-15 13:26:33 -0800700 Integer currentPid = new Integer(getCallingPid());
Robert Greenwalt2034b912009-08-12 16:08:25 -0700701 if (usedNetworkType != networkType) {
Robert Greenwalt2034b912009-08-12 16:08:25 -0700702 NetworkStateTracker radio = mNetTrackers[networkType];
703 NetworkInfo ni = network.getNetworkInfo();
704
705 if (ni.isAvailable() == false) {
Wink Savillee70c6f52010-12-03 12:01:38 -0800706 if (DBG) log("special network not available");
Robert Greenwalt2cc87442010-12-29 14:35:21 -0800707 if (!TextUtils.equals(feature,Phone.FEATURE_ENABLE_DUN_ALWAYS)) {
708 return Phone.APN_TYPE_NOT_AVAILABLE;
709 } else {
710 // else make the attempt anyway - probably giving REQUEST_STARTED below
711 }
Robert Greenwalt2034b912009-08-12 16:08:25 -0700712 }
713
Robert Greenwaltaffc3a12009-09-27 17:27:04 -0700714 synchronized(this) {
715 mFeatureUsers.add(f);
716 if (!mNetRequestersPids[usedNetworkType].contains(currentPid)) {
717 // this gets used for per-pid dns when connected
718 mNetRequestersPids[usedNetworkType].add(currentPid);
719 }
Robert Greenwalt2034b912009-08-12 16:08:25 -0700720 }
Robert Greenwaltaffc3a12009-09-27 17:27:04 -0700721
Robert Greenwalt20f819c2011-05-03 19:02:44 -0700722 int restoreTimer = getRestoreDefaultNetworkDelay(usedNetworkType);
723
724 if (restoreTimer >= 0) {
725 mHandler.sendMessageDelayed(
726 mHandler.obtainMessage(EVENT_RESTORE_DEFAULT_NETWORK, f), restoreTimer);
727 }
Robert Greenwalt2034b912009-08-12 16:08:25 -0700728
Robert Greenwalta52c75a2009-08-19 20:19:33 -0700729 if ((ni.isConnectedOrConnecting() == true) &&
730 !network.isTeardownRequested()) {
Robert Greenwalt2034b912009-08-12 16:08:25 -0700731 if (ni.isConnected() == true) {
732 // add the pid-specific dns
Robert Greenwalt3afbead2010-07-23 15:46:26 -0700733 handleDnsConfigurationChange(networkType);
Wink Savillee70c6f52010-12-03 12:01:38 -0800734 if (DBG) log("special network already active");
Robert Greenwalt2034b912009-08-12 16:08:25 -0700735 return Phone.APN_ALREADY_ACTIVE;
736 }
Wink Savillee70c6f52010-12-03 12:01:38 -0800737 if (DBG) log("special network already connecting");
Robert Greenwalt2034b912009-08-12 16:08:25 -0700738 return Phone.APN_REQUEST_STARTED;
739 }
740
741 // check if the radio in play can make another contact
742 // assume if cannot for now
743
Wink Savillee70c6f52010-12-03 12:01:38 -0800744 if (DBG) log("reconnecting to special network");
Robert Greenwalt2034b912009-08-12 16:08:25 -0700745 network.reconnect();
746 return Phone.APN_REQUEST_STARTED;
747 } else {
Robert Greenwalt5364d752010-12-15 13:26:33 -0800748 // need to remember this unsupported request so we respond appropriately on stop
749 synchronized(this) {
750 mFeatureUsers.add(f);
751 if (!mNetRequestersPids[usedNetworkType].contains(currentPid)) {
752 // this gets used for per-pid dns when connected
753 mNetRequestersPids[usedNetworkType].add(currentPid);
754 }
755 }
Robert Greenwaltd391e892010-05-18 10:52:51 -0700756 return -1;
Robert Greenwalt2034b912009-08-12 16:08:25 -0700757 }
758 }
759 return Phone.APN_TYPE_NOT_AVAILABLE;
The Android Open Source Project28527d22009-03-03 19:31:44 -0800760 }
761
Robert Greenwaltaffc3a12009-09-27 17:27:04 -0700762 // javadoc from interface
The Android Open Source Project28527d22009-03-03 19:31:44 -0800763 public int stopUsingNetworkFeature(int networkType, String feature) {
Robert Greenwalt28f43012009-10-06 17:52:40 -0700764 enforceChangePermission();
765
Robert Greenwaltaffc3a12009-09-27 17:27:04 -0700766 int pid = getCallingPid();
767 int uid = getCallingUid();
768
769 FeatureUser u = null;
770 boolean found = false;
771
772 synchronized(this) {
773 for (int i = 0; i < mFeatureUsers.size() ; i++) {
774 u = (FeatureUser)mFeatureUsers.get(i);
775 if (uid == u.mUid && pid == u.mPid &&
776 networkType == u.mNetworkType &&
777 TextUtils.equals(feature, u.mFeature)) {
778 found = true;
779 break;
780 }
781 }
782 }
783 if (found && u != null) {
784 // stop regardless of how many other time this proc had called start
785 return stopUsingNetworkFeature(u, true);
786 } else {
787 // none found!
Wink Savillee70c6f52010-12-03 12:01:38 -0800788 if (DBG) log("ignoring stopUsingNetworkFeature - not a live request");
Robert Greenwaltaffc3a12009-09-27 17:27:04 -0700789 return 1;
790 }
Robert Greenwalt2034b912009-08-12 16:08:25 -0700791 }
792
Robert Greenwaltaffc3a12009-09-27 17:27:04 -0700793 private int stopUsingNetworkFeature(FeatureUser u, boolean ignoreDups) {
794 int networkType = u.mNetworkType;
795 String feature = u.mFeature;
796 int pid = u.mPid;
797 int uid = u.mUid;
798
799 NetworkStateTracker tracker = null;
800 boolean callTeardown = false; // used to carry our decision outside of sync block
801
Robert Greenwalt2034b912009-08-12 16:08:25 -0700802 if (DBG) {
Wink Savillee70c6f52010-12-03 12:01:38 -0800803 log("stopUsingNetworkFeature for net " + networkType +
Robert Greenwalt2034b912009-08-12 16:08:25 -0700804 ": " + feature);
805 }
Robert Greenwalt28f43012009-10-06 17:52:40 -0700806
The Android Open Source Project28527d22009-03-03 19:31:44 -0800807 if (!ConnectivityManager.isNetworkTypeValid(networkType)) {
808 return -1;
809 }
Robert Greenwalt2034b912009-08-12 16:08:25 -0700810
Robert Greenwaltaffc3a12009-09-27 17:27:04 -0700811 // need to link the mFeatureUsers list with the mNetRequestersPids state in this
812 // sync block
813 synchronized(this) {
814 // check if this process still has an outstanding start request
815 if (!mFeatureUsers.contains(u)) {
Wink Savillee70c6f52010-12-03 12:01:38 -0800816 if (DBG) log("ignoring - this process has no outstanding requests");
Robert Greenwalt2034b912009-08-12 16:08:25 -0700817 return 1;
818 }
Robert Greenwaltaffc3a12009-09-27 17:27:04 -0700819 u.unlinkDeathRecipient();
820 mFeatureUsers.remove(mFeatureUsers.indexOf(u));
821 // If we care about duplicate requests, check for that here.
822 //
823 // This is done to support the extension of a request - the app
824 // can request we start the network feature again and renew the
825 // auto-shutoff delay. Normal "stop" calls from the app though
826 // do not pay attention to duplicate requests - in effect the
827 // API does not refcount and a single stop will counter multiple starts.
828 if (ignoreDups == false) {
829 for (int i = 0; i < mFeatureUsers.size() ; i++) {
830 FeatureUser x = (FeatureUser)mFeatureUsers.get(i);
831 if (x.mUid == u.mUid && x.mPid == u.mPid &&
832 x.mNetworkType == u.mNetworkType &&
833 TextUtils.equals(x.mFeature, u.mFeature)) {
Wink Savillee70c6f52010-12-03 12:01:38 -0800834 if (DBG) log("ignoring stopUsingNetworkFeature as dup is found");
Robert Greenwaltaffc3a12009-09-27 17:27:04 -0700835 return 1;
836 }
837 }
Robert Greenwalt2034b912009-08-12 16:08:25 -0700838 }
Robert Greenwaltaffc3a12009-09-27 17:27:04 -0700839
840 // TODO - move to MobileDataStateTracker
841 int usedNetworkType = networkType;
842 if (networkType == ConnectivityManager.TYPE_MOBILE) {
Wink Savilleb7c92c72011-03-12 14:52:01 -0800843 usedNetworkType = convertFeatureToNetworkType(feature);
844 if (usedNetworkType < 0) {
845 usedNetworkType = networkType;
Robert Greenwaltaffc3a12009-09-27 17:27:04 -0700846 }
847 }
848 tracker = mNetTrackers[usedNetworkType];
Robert Greenwaltec05b3c2009-10-30 14:17:42 -0700849 if (tracker == null) {
Wink Savillee70c6f52010-12-03 12:01:38 -0800850 if (DBG) log("ignoring - no known tracker for net type " + usedNetworkType);
Robert Greenwaltec05b3c2009-10-30 14:17:42 -0700851 return -1;
852 }
853 if (usedNetworkType != networkType) {
Robert Greenwaltaffc3a12009-09-27 17:27:04 -0700854 Integer currentPid = new Integer(pid);
Robert Greenwaltaffc3a12009-09-27 17:27:04 -0700855 mNetRequestersPids[usedNetworkType].remove(currentPid);
Robert Greenwalt0ca68a02009-12-17 14:54:59 -0800856 reassessPidDns(pid, true);
Robert Greenwaltaffc3a12009-09-27 17:27:04 -0700857 if (mNetRequestersPids[usedNetworkType].size() != 0) {
Wink Savillee70c6f52010-12-03 12:01:38 -0800858 if (DBG) log("not tearing down special network - " +
Robert Greenwaltaffc3a12009-09-27 17:27:04 -0700859 "others still using it");
860 return 1;
861 }
862 callTeardown = true;
Robert Greenwalt9f3be4c2011-01-10 11:58:31 -0800863 } else {
864 if (DBG) log("not a known feature - dropping");
Robert Greenwaltaffc3a12009-09-27 17:27:04 -0700865 }
866 }
Wink Savillee70c6f52010-12-03 12:01:38 -0800867 if (DBG) log("Doing network teardown");
Robert Greenwaltaffc3a12009-09-27 17:27:04 -0700868 if (callTeardown) {
869 tracker.teardown();
Robert Greenwalt2034b912009-08-12 16:08:25 -0700870 return 1;
871 } else {
Robert Greenwaltd391e892010-05-18 10:52:51 -0700872 return -1;
Robert Greenwalt2034b912009-08-12 16:08:25 -0700873 }
The Android Open Source Project28527d22009-03-03 19:31:44 -0800874 }
875
876 /**
Robert Greenwalt7fe44cb2010-08-27 09:24:29 -0700877 * @deprecated use requestRouteToHostAddress instead
878 *
The Android Open Source Project28527d22009-03-03 19:31:44 -0800879 * Ensure that a network route exists to deliver traffic to the specified
880 * host via the specified network interface.
Robert Greenwalt0659da32009-07-16 17:21:39 -0700881 * @param networkType the type of the network over which traffic to the
882 * specified host is to be routed
883 * @param hostAddress the IP address of the host to which the route is
884 * desired
The Android Open Source Project28527d22009-03-03 19:31:44 -0800885 * @return {@code true} on success, {@code false} on failure
886 */
887 public boolean requestRouteToHost(int networkType, int hostAddress) {
Robert Greenwalt7fe44cb2010-08-27 09:24:29 -0700888 InetAddress inetAddress = NetworkUtils.intToInetAddress(hostAddress);
889
890 if (inetAddress == null) {
891 return false;
892 }
893
894 return requestRouteToHostAddress(networkType, inetAddress.getAddress());
895 }
896
897 /**
898 * Ensure that a network route exists to deliver traffic to the specified
899 * host via the specified network interface.
900 * @param networkType the type of the network over which traffic to the
901 * specified host is to be routed
902 * @param hostAddress the IP address of the host to which the route is
903 * desired
904 * @return {@code true} on success, {@code false} on failure
905 */
906 public boolean requestRouteToHostAddress(int networkType, byte[] hostAddress) {
The Android Open Source Project28527d22009-03-03 19:31:44 -0800907 enforceChangePermission();
908 if (!ConnectivityManager.isNetworkTypeValid(networkType)) {
909 return false;
910 }
911 NetworkStateTracker tracker = mNetTrackers[networkType];
Robert Greenwalt4666ed02009-09-10 15:06:20 -0700912
Robert Greenwaltec05b3c2009-10-30 14:17:42 -0700913 if (tracker == null || !tracker.getNetworkInfo().isConnected() ||
914 tracker.isTeardownRequested()) {
Robert Greenwalt4666ed02009-09-10 15:06:20 -0700915 if (DBG) {
Wink Savillee70c6f52010-12-03 12:01:38 -0800916 log("requestRouteToHostAddress on down network " +
Robert Greenwalt7fe44cb2010-08-27 09:24:29 -0700917 "(" + networkType + ") - dropped");
Robert Greenwalt4666ed02009-09-10 15:06:20 -0700918 }
919 return false;
The Android Open Source Project28527d22009-03-03 19:31:44 -0800920 }
Robert Greenwalta7dfbd32010-06-15 15:43:39 -0700921 try {
Robert Greenwalt7fe44cb2010-08-27 09:24:29 -0700922 InetAddress addr = InetAddress.getByAddress(hostAddress);
Robert Greenwaltbd492212011-05-06 17:10:53 -0700923 return addHostRoute(tracker, addr, 0);
Robert Greenwalta7dfbd32010-06-15 15:43:39 -0700924 } catch (UnknownHostException e) {}
925 return false;
Irfan Sheriff7f132d92010-06-09 15:39:36 -0700926 }
927
928 /**
929 * Ensure that a network route exists to deliver traffic to the specified
930 * host via the mobile data network.
931 * @param hostAddress the IP address of the host to which the route is desired,
932 * in network byte order.
Robert Greenwalta7dfbd32010-06-15 15:43:39 -0700933 * TODO - deprecate
Irfan Sheriff7f132d92010-06-09 15:39:36 -0700934 * @return {@code true} on success, {@code false} on failure
935 */
Robert Greenwaltbd492212011-05-06 17:10:53 -0700936 private boolean addHostRoute(NetworkStateTracker nt, InetAddress hostAddress, int cycleCount) {
Robert Greenwaltbd492212011-05-06 17:10:53 -0700937 LinkProperties lp = nt.getLinkProperties();
938 if ((lp == null) || (hostAddress == null)) return false;
Irfan Sheriff7f132d92010-06-09 15:39:36 -0700939
Robert Greenwaltbd492212011-05-06 17:10:53 -0700940 String interfaceName = lp.getInterfaceName();
Irfan Sheriff7f132d92010-06-09 15:39:36 -0700941 if (DBG) {
Robert Greenwaltbd492212011-05-06 17:10:53 -0700942 log("Requested host route to " + hostAddress + "(" + interfaceName + "), cycleCount=" +
943 cycleCount);
Irfan Sheriff7f132d92010-06-09 15:39:36 -0700944 }
Robert Greenwaltbd492212011-05-06 17:10:53 -0700945 if (interfaceName == null) {
Wink Savillee70c6f52010-12-03 12:01:38 -0800946 if (DBG) loge("addHostRoute failed due to null interface name");
Irfan Sheriff7f132d92010-06-09 15:39:36 -0700947 return false;
948 }
Robert Greenwaltbd492212011-05-06 17:10:53 -0700949
950 RouteInfo bestRoute = RouteInfo.selectBestRoute(lp.getRoutes(), hostAddress);
Robert Greenwalt355205c2011-05-10 15:05:02 -0700951 InetAddress gatewayAddress = null;
Robert Greenwaltbd492212011-05-06 17:10:53 -0700952 if (bestRoute != null) {
Robert Greenwalt355205c2011-05-10 15:05:02 -0700953 gatewayAddress = bestRoute.getGateway();
Robert Greenwaltbd492212011-05-06 17:10:53 -0700954 // if the best route is ourself, don't relf-reference, just add the host route
Robert Greenwalt355205c2011-05-10 15:05:02 -0700955 if (hostAddress.equals(gatewayAddress)) gatewayAddress = null;
Robert Greenwaltbd492212011-05-06 17:10:53 -0700956 }
Robert Greenwalt355205c2011-05-10 15:05:02 -0700957 if (gatewayAddress != null) {
Robert Greenwaltbd492212011-05-06 17:10:53 -0700958 if (cycleCount > MAX_HOSTROUTE_CYCLE_COUNT) {
959 loge("Error adding hostroute - too much recursion");
960 return false;
961 }
Robert Greenwalt355205c2011-05-10 15:05:02 -0700962 if (!addHostRoute(nt, gatewayAddress, cycleCount+1)) return false;
Robert Greenwaltbd492212011-05-06 17:10:53 -0700963 }
Robert Greenwalt355205c2011-05-10 15:05:02 -0700964
965 RouteInfo route = RouteInfo.makeHostRoute(hostAddress, gatewayAddress);
966
967 try {
968 mNetd.addRoute(interfaceName, route);
969 return true;
970 } catch (Exception ex) {
971 return false;
972 }
Robert Greenwaltbd492212011-05-06 17:10:53 -0700973 }
974
975 // TODO support the removal of single host routes. Keep a ref count of them so we
976 // aren't over-zealous
977 private boolean removeHostRoute(NetworkStateTracker nt, InetAddress hostAddress) {
978 return false;
The Android Open Source Project28527d22009-03-03 19:31:44 -0800979 }
980
981 /**
982 * @see ConnectivityManager#getBackgroundDataSetting()
983 */
984 public boolean getBackgroundDataSetting() {
Robert Greenwaltd62c7002010-12-29 16:15:02 -0800985 return mBackgroundDataEnabled.get();
The Android Open Source Project28527d22009-03-03 19:31:44 -0800986 }
Robert Greenwalt0659da32009-07-16 17:21:39 -0700987
The Android Open Source Project28527d22009-03-03 19:31:44 -0800988 /**
989 * @see ConnectivityManager#setBackgroundDataSetting(boolean)
990 */
991 public void setBackgroundDataSetting(boolean allowBackgroundDataUsage) {
992 mContext.enforceCallingOrSelfPermission(
993 android.Manifest.permission.CHANGE_BACKGROUND_DATA_SETTING,
994 "ConnectivityService");
Robert Greenwalt0659da32009-07-16 17:21:39 -0700995
Robert Greenwaltd62c7002010-12-29 16:15:02 -0800996 mBackgroundDataEnabled.set(allowBackgroundDataUsage);
997
Robert Greenwalt6a2db8a2010-09-23 10:05:56 -0700998 mHandler.sendMessage(mHandler.obtainMessage(EVENT_SET_BACKGROUND_DATA,
999 (allowBackgroundDataUsage ? ENABLED : DISABLED), 0));
1000 }
The Android Open Source Project28527d22009-03-03 19:31:44 -08001001
Robert Greenwalt6a2db8a2010-09-23 10:05:56 -07001002 private void handleSetBackgroundData(boolean enabled) {
Robert Greenwalt0ffdef12011-02-25 13:44:09 -08001003 Settings.Secure.putInt(mContext.getContentResolver(),
1004 Settings.Secure.BACKGROUND_DATA, enabled ? 1 : 0);
1005 Intent broadcast = new Intent(
1006 ConnectivityManager.ACTION_BACKGROUND_DATA_SETTING_CHANGED);
1007 mContext.sendBroadcast(broadcast);
Robert Greenwalt0659da32009-07-16 17:21:39 -07001008 }
1009
Robert Greenwalt1b21f6c2010-02-23 18:58:05 -08001010 /**
1011 * @see ConnectivityManager#getMobileDataEnabled()
1012 */
1013 public boolean getMobileDataEnabled() {
Wink Savilleb9024c62010-12-07 10:31:02 -08001014 // TODO: This detail should probably be in DataConnectionTracker's
1015 // which is where we store the value and maybe make this
1016 // asynchronous.
Robert Greenwalt1b21f6c2010-02-23 18:58:05 -08001017 enforceAccessPermission();
1018 boolean retVal = Settings.Secure.getInt(mContext.getContentResolver(),
1019 Settings.Secure.MOBILE_DATA, 1) == 1;
Wink Savillee70c6f52010-12-03 12:01:38 -08001020 if (DBG) log("getMobileDataEnabled returning " + retVal);
Robert Greenwalt1b21f6c2010-02-23 18:58:05 -08001021 return retVal;
1022 }
1023
Robert Greenwalt34848c02011-03-25 13:09:25 -07001024 public void setDataDependency(int networkType, boolean met) {
1025 enforceChangePermission();
1026 if (DBG) {
1027 log("setDataDependency(" + networkType + ", " + met + ")");
1028 }
1029 mHandler.sendMessage(mHandler.obtainMessage(EVENT_SET_DEPENDENCY_MET,
1030 (met ? ENABLED : DISABLED), networkType));
1031 }
1032
1033 private void handleSetDependencyMet(int networkType, boolean met) {
1034 if (mNetTrackers[networkType] != null) {
1035 if (DBG) {
1036 log("handleSetDependencyMet(" + networkType + ", " + met + ")");
1037 }
1038 mNetTrackers[networkType].setDependencyMet(met);
1039 }
1040 }
1041
Robert Greenwalt1b21f6c2010-02-23 18:58:05 -08001042 /**
1043 * @see ConnectivityManager#setMobileDataEnabled(boolean)
1044 */
Robert Greenwalt6a2db8a2010-09-23 10:05:56 -07001045 public void setMobileDataEnabled(boolean enabled) {
Robert Greenwalt1b21f6c2010-02-23 18:58:05 -08001046 enforceChangePermission();
Wink Savillee70c6f52010-12-03 12:01:38 -08001047 if (DBG) log("setMobileDataEnabled(" + enabled + ")");
Robert Greenwalt1b21f6c2010-02-23 18:58:05 -08001048
Robert Greenwalt6a2db8a2010-09-23 10:05:56 -07001049 mHandler.sendMessage(mHandler.obtainMessage(EVENT_SET_MOBILE_DATA,
Robert Greenwalt34848c02011-03-25 13:09:25 -07001050 (enabled ? ENABLED : DISABLED), 0));
Robert Greenwalt6a2db8a2010-09-23 10:05:56 -07001051 }
1052
1053 private void handleSetMobileData(boolean enabled) {
Wink Savilleb9024c62010-12-07 10:31:02 -08001054 if (mNetTrackers[ConnectivityManager.TYPE_MOBILE] != null) {
1055 if (DBG) {
1056 Slog.d(TAG, mNetTrackers[ConnectivityManager.TYPE_MOBILE].toString() + enabled);
Robert Greenwalt1b21f6c2010-02-23 18:58:05 -08001057 }
Wink Savilleb9024c62010-12-07 10:31:02 -08001058 mNetTrackers[ConnectivityManager.TYPE_MOBILE].setDataEnable(enabled);
Robert Greenwalt1b21f6c2010-02-23 18:58:05 -08001059 }
1060 }
1061
The Android Open Source Project28527d22009-03-03 19:31:44 -08001062 private void enforceAccessPermission() {
Robert Greenwalt0659da32009-07-16 17:21:39 -07001063 mContext.enforceCallingOrSelfPermission(
1064 android.Manifest.permission.ACCESS_NETWORK_STATE,
1065 "ConnectivityService");
The Android Open Source Project28527d22009-03-03 19:31:44 -08001066 }
1067
1068 private void enforceChangePermission() {
Robert Greenwalt0659da32009-07-16 17:21:39 -07001069 mContext.enforceCallingOrSelfPermission(
1070 android.Manifest.permission.CHANGE_NETWORK_STATE,
1071 "ConnectivityService");
The Android Open Source Project28527d22009-03-03 19:31:44 -08001072 }
1073
Robert Greenwalt0c4828c2010-01-26 11:40:34 -08001074 // TODO Make this a special check when it goes public
1075 private void enforceTetherChangePermission() {
1076 mContext.enforceCallingOrSelfPermission(
1077 android.Manifest.permission.CHANGE_NETWORK_STATE,
1078 "ConnectivityService");
1079 }
1080
Robert Greenwalt8e87f122010-02-11 18:18:40 -08001081 private void enforceTetherAccessPermission() {
1082 mContext.enforceCallingOrSelfPermission(
1083 android.Manifest.permission.ACCESS_NETWORK_STATE,
1084 "ConnectivityService");
1085 }
1086
Robert Greenwalt93dc1042010-06-15 12:19:37 -07001087 private void enforceConnectivityInternalPermission() {
1088 mContext.enforceCallingOrSelfPermission(
1089 android.Manifest.permission.CONNECTIVITY_INTERNAL,
1090 "ConnectivityService");
1091 }
1092
The Android Open Source Project28527d22009-03-03 19:31:44 -08001093 /**
Robert Greenwalt0659da32009-07-16 17:21:39 -07001094 * Handle a {@code DISCONNECTED} event. If this pertains to the non-active
1095 * network, we ignore it. If it is for the active network, we send out a
1096 * broadcast. But first, we check whether it might be possible to connect
1097 * to a different network.
The Android Open Source Project28527d22009-03-03 19:31:44 -08001098 * @param info the {@code NetworkInfo} for the network
1099 */
1100 private void handleDisconnect(NetworkInfo info) {
1101
Robert Greenwalt2034b912009-08-12 16:08:25 -07001102 int prevNetType = info.getType();
The Android Open Source Project28527d22009-03-03 19:31:44 -08001103
Robert Greenwalt2034b912009-08-12 16:08:25 -07001104 mNetTrackers[prevNetType].setTeardownRequested(false);
The Android Open Source Project28527d22009-03-03 19:31:44 -08001105 /*
1106 * If the disconnected network is not the active one, then don't report
1107 * this as a loss of connectivity. What probably happened is that we're
1108 * getting the disconnect for a network that we explicitly disabled
1109 * in accordance with network preference policies.
1110 */
Robert Greenwalt34848c02011-03-25 13:09:25 -07001111 if (!mNetConfigs[prevNetType].isDefault()) {
Robert Greenwalt2034b912009-08-12 16:08:25 -07001112 List pids = mNetRequestersPids[prevNetType];
1113 for (int i = 0; i<pids.size(); i++) {
1114 Integer pid = (Integer)pids.get(i);
1115 // will remove them because the net's no longer connected
1116 // need to do this now as only now do we know the pids and
1117 // can properly null things that are no longer referenced.
1118 reassessPidDns(pid.intValue(), false);
The Android Open Source Project28527d22009-03-03 19:31:44 -08001119 }
The Android Open Source Project28527d22009-03-03 19:31:44 -08001120 }
1121
The Android Open Source Project28527d22009-03-03 19:31:44 -08001122 Intent intent = new Intent(ConnectivityManager.CONNECTIVITY_ACTION);
1123 intent.putExtra(ConnectivityManager.EXTRA_NETWORK_INFO, info);
1124 if (info.isFailover()) {
1125 intent.putExtra(ConnectivityManager.EXTRA_IS_FAILOVER, true);
1126 info.setFailover(false);
1127 }
1128 if (info.getReason() != null) {
1129 intent.putExtra(ConnectivityManager.EXTRA_REASON, info.getReason());
1130 }
1131 if (info.getExtraInfo() != null) {
Robert Greenwalt0659da32009-07-16 17:21:39 -07001132 intent.putExtra(ConnectivityManager.EXTRA_EXTRA_INFO,
1133 info.getExtraInfo());
The Android Open Source Project28527d22009-03-03 19:31:44 -08001134 }
Robert Greenwalt2034b912009-08-12 16:08:25 -07001135
Robert Greenwalt34848c02011-03-25 13:09:25 -07001136 if (mNetConfigs[prevNetType].isDefault()) {
Robert Greenwalt4b7c55e2011-01-11 13:56:33 -08001137 tryFailover(prevNetType);
1138 if (mActiveDefaultNetwork != -1) {
1139 NetworkInfo switchTo = mNetTrackers[mActiveDefaultNetwork].getNetworkInfo();
Robert Greenwalt3cc68d32010-01-25 17:54:29 -08001140 intent.putExtra(ConnectivityManager.EXTRA_OTHER_NETWORK_INFO, switchTo);
1141 } else {
Robert Greenwaltb2a9e492010-09-20 18:01:43 -07001142 mDefaultInetConditionPublished = 0; // we're not connected anymore
Robert Greenwalt3cc68d32010-01-25 17:54:29 -08001143 intent.putExtra(ConnectivityManager.EXTRA_NO_CONNECTIVITY, true);
1144 }
Robert Greenwaltf55ced92010-01-20 19:29:41 -08001145 }
Robert Greenwaltb2a9e492010-09-20 18:01:43 -07001146 intent.putExtra(ConnectivityManager.EXTRA_INET_CONDITION, mDefaultInetConditionPublished);
Robert Greenwaltf55ced92010-01-20 19:29:41 -08001147 // do this before we broadcast the change
Robert Greenwalt3afbead2010-07-23 15:46:26 -07001148 handleConnectivityChange(prevNetType);
Robert Greenwaltf55ced92010-01-20 19:29:41 -08001149
1150 sendStickyBroadcast(intent);
1151 /*
1152 * If the failover network is already connected, then immediately send
1153 * out a followup broadcast indicating successful failover
1154 */
Robert Greenwalt4b7c55e2011-01-11 13:56:33 -08001155 if (mActiveDefaultNetwork != -1) {
1156 sendConnectedBroadcast(mNetTrackers[mActiveDefaultNetwork].getNetworkInfo());
Robert Greenwaltf55ced92010-01-20 19:29:41 -08001157 }
1158 }
1159
Robert Greenwalt4b7c55e2011-01-11 13:56:33 -08001160 private void tryFailover(int prevNetType) {
Robert Greenwalt2034b912009-08-12 16:08:25 -07001161 /*
Robert Greenwalt92564852011-01-06 15:41:07 -08001162 * If this is a default network, check if other defaults are available.
1163 * Try to reconnect on all available and let them hash it out when
1164 * more than one connects.
Robert Greenwalt2034b912009-08-12 16:08:25 -07001165 */
Robert Greenwalt34848c02011-03-25 13:09:25 -07001166 if (mNetConfigs[prevNetType].isDefault()) {
Robert Greenwalt2034b912009-08-12 16:08:25 -07001167 if (mActiveDefaultNetwork == prevNetType) {
1168 mActiveDefaultNetwork = -1;
1169 }
1170
Robert Greenwalt4b7c55e2011-01-11 13:56:33 -08001171 // don't signal a reconnect for anything lower or equal priority than our
1172 // current connected default
1173 // TODO - don't filter by priority now - nice optimization but risky
1174// int currentPriority = -1;
1175// if (mActiveDefaultNetwork != -1) {
Robert Greenwalt34848c02011-03-25 13:09:25 -07001176// currentPriority = mNetConfigs[mActiveDefaultNetwork].mPriority;
Robert Greenwalt4b7c55e2011-01-11 13:56:33 -08001177// }
Robert Greenwaltf55ced92010-01-20 19:29:41 -08001178 for (int checkType=0; checkType <= ConnectivityManager.MAX_NETWORK_TYPE; checkType++) {
Robert Greenwaltec05b3c2009-10-30 14:17:42 -07001179 if (checkType == prevNetType) continue;
Robert Greenwalt34848c02011-03-25 13:09:25 -07001180 if (mNetConfigs[checkType] == null) continue;
1181 if (!mNetConfigs[checkType].isDefault()) continue;
Wink Saville72a95b92011-01-26 15:43:49 -08001182
1183// Enabling the isAvailable() optimization caused mobile to not get
1184// selected if it was in the middle of error handling. Specifically
1185// a moble connection that took 30 seconds to complete the DEACTIVATE_DATA_CALL
1186// would not be available and we wouldn't get connected to anything.
1187// So removing the isAvailable() optimization below for now. TODO: This
1188// optimization should work and we need to investigate why it doesn't work.
1189// This could be related to how DEACTIVATE_DATA_CALL is reporting its
1190// complete before it is really complete.
1191// if (!mNetTrackers[checkType].isAvailable()) continue;
1192
Robert Greenwalt34848c02011-03-25 13:09:25 -07001193// if (currentPriority >= mNetConfigs[checkType].mPriority) continue;
Robert Greenwalt2034b912009-08-12 16:08:25 -07001194
Robert Greenwalt4b7c55e2011-01-11 13:56:33 -08001195 NetworkStateTracker checkTracker = mNetTrackers[checkType];
1196 NetworkInfo checkInfo = checkTracker.getNetworkInfo();
1197 if (!checkInfo.isConnectedOrConnecting() || checkTracker.isTeardownRequested()) {
1198 checkInfo.setFailover(true);
1199 checkTracker.reconnect();
Robert Greenwalt2034b912009-08-12 16:08:25 -07001200 }
Robert Greenwalt4b7c55e2011-01-11 13:56:33 -08001201 if (DBG) log("Attempting to switch to " + checkInfo.getTypeName());
Robert Greenwalt2034b912009-08-12 16:08:25 -07001202 }
The Android Open Source Project28527d22009-03-03 19:31:44 -08001203 }
The Android Open Source Project28527d22009-03-03 19:31:44 -08001204 }
1205
1206 private void sendConnectedBroadcast(NetworkInfo info) {
Robert Greenwaltd3401f92010-09-15 17:36:33 -07001207 sendGeneralBroadcast(info, ConnectivityManager.CONNECTIVITY_ACTION);
1208 }
1209
1210 private void sendInetConditionBroadcast(NetworkInfo info) {
1211 sendGeneralBroadcast(info, ConnectivityManager.INET_CONDITION_ACTION);
1212 }
1213
1214 private void sendGeneralBroadcast(NetworkInfo info, String bcastType) {
1215 Intent intent = new Intent(bcastType);
The Android Open Source Project28527d22009-03-03 19:31:44 -08001216 intent.putExtra(ConnectivityManager.EXTRA_NETWORK_INFO, info);
1217 if (info.isFailover()) {
1218 intent.putExtra(ConnectivityManager.EXTRA_IS_FAILOVER, true);
1219 info.setFailover(false);
1220 }
1221 if (info.getReason() != null) {
1222 intent.putExtra(ConnectivityManager.EXTRA_REASON, info.getReason());
1223 }
1224 if (info.getExtraInfo() != null) {
Robert Greenwalt0659da32009-07-16 17:21:39 -07001225 intent.putExtra(ConnectivityManager.EXTRA_EXTRA_INFO,
1226 info.getExtraInfo());
The Android Open Source Project28527d22009-03-03 19:31:44 -08001227 }
Robert Greenwalt986c7412010-09-08 15:24:47 -07001228 intent.putExtra(ConnectivityManager.EXTRA_INET_CONDITION, mDefaultInetConditionPublished);
Mike Lockwoodfde2b762009-08-14 14:18:49 -04001229 sendStickyBroadcast(intent);
The Android Open Source Project28527d22009-03-03 19:31:44 -08001230 }
1231
1232 /**
1233 * Called when an attempt to fail over to another network has failed.
1234 * @param info the {@link NetworkInfo} for the failed network
1235 */
1236 private void handleConnectionFailure(NetworkInfo info) {
1237 mNetTrackers[info.getType()].setTeardownRequested(false);
The Android Open Source Project28527d22009-03-03 19:31:44 -08001238
Robert Greenwalt2034b912009-08-12 16:08:25 -07001239 String reason = info.getReason();
1240 String extraInfo = info.getExtraInfo();
Robert Greenwalt0659da32009-07-16 17:21:39 -07001241
Robert Greenwalte981bc52010-10-08 16:35:52 -07001242 String reasonText;
1243 if (reason == null) {
1244 reasonText = ".";
1245 } else {
1246 reasonText = " (" + reason + ").";
The Android Open Source Project28527d22009-03-03 19:31:44 -08001247 }
Wink Savillee70c6f52010-12-03 12:01:38 -08001248 loge("Attempt to connect to " + info.getTypeName() + " failed" + reasonText);
Robert Greenwalt2034b912009-08-12 16:08:25 -07001249
1250 Intent intent = new Intent(ConnectivityManager.CONNECTIVITY_ACTION);
1251 intent.putExtra(ConnectivityManager.EXTRA_NETWORK_INFO, info);
1252 if (getActiveNetworkInfo() == null) {
1253 intent.putExtra(ConnectivityManager.EXTRA_NO_CONNECTIVITY, true);
1254 }
1255 if (reason != null) {
1256 intent.putExtra(ConnectivityManager.EXTRA_REASON, reason);
1257 }
1258 if (extraInfo != null) {
1259 intent.putExtra(ConnectivityManager.EXTRA_EXTRA_INFO, extraInfo);
1260 }
1261 if (info.isFailover()) {
1262 intent.putExtra(ConnectivityManager.EXTRA_IS_FAILOVER, true);
1263 info.setFailover(false);
1264 }
Robert Greenwaltf55ced92010-01-20 19:29:41 -08001265
Robert Greenwalt34848c02011-03-25 13:09:25 -07001266 if (mNetConfigs[info.getType()].isDefault()) {
Robert Greenwalt4b7c55e2011-01-11 13:56:33 -08001267 tryFailover(info.getType());
1268 if (mActiveDefaultNetwork != -1) {
1269 NetworkInfo switchTo = mNetTrackers[mActiveDefaultNetwork].getNetworkInfo();
Robert Greenwalt3cc68d32010-01-25 17:54:29 -08001270 intent.putExtra(ConnectivityManager.EXTRA_OTHER_NETWORK_INFO, switchTo);
1271 } else {
Robert Greenwaltb2a9e492010-09-20 18:01:43 -07001272 mDefaultInetConditionPublished = 0;
Robert Greenwalt3cc68d32010-01-25 17:54:29 -08001273 intent.putExtra(ConnectivityManager.EXTRA_NO_CONNECTIVITY, true);
1274 }
Robert Greenwaltf55ced92010-01-20 19:29:41 -08001275 }
Robert Greenwalt3cc68d32010-01-25 17:54:29 -08001276
Robert Greenwaltb2a9e492010-09-20 18:01:43 -07001277 intent.putExtra(ConnectivityManager.EXTRA_INET_CONDITION, mDefaultInetConditionPublished);
Mike Lockwoodfde2b762009-08-14 14:18:49 -04001278 sendStickyBroadcast(intent);
Robert Greenwaltf55ced92010-01-20 19:29:41 -08001279 /*
1280 * If the failover network is already connected, then immediately send
1281 * out a followup broadcast indicating successful failover
1282 */
Robert Greenwalt4b7c55e2011-01-11 13:56:33 -08001283 if (mActiveDefaultNetwork != -1) {
1284 sendConnectedBroadcast(mNetTrackers[mActiveDefaultNetwork].getNetworkInfo());
Robert Greenwaltf55ced92010-01-20 19:29:41 -08001285 }
Mike Lockwoodfde2b762009-08-14 14:18:49 -04001286 }
1287
1288 private void sendStickyBroadcast(Intent intent) {
1289 synchronized(this) {
Dianne Hackborna417ff82009-12-08 19:45:14 -08001290 if (!mSystemReady) {
1291 mInitialBroadcast = new Intent(intent);
Mike Lockwoodfde2b762009-08-14 14:18:49 -04001292 }
Dianne Hackborna417ff82009-12-08 19:45:14 -08001293 intent.addFlags(Intent.FLAG_RECEIVER_REGISTERED_ONLY_BEFORE_BOOT);
1294 mContext.sendStickyBroadcast(intent);
Mike Lockwoodfde2b762009-08-14 14:18:49 -04001295 }
1296 }
1297
1298 void systemReady() {
Robert Greenwalt355205c2011-05-10 15:05:02 -07001299 IBinder b = ServiceManager.getService(Context.NETWORKMANAGEMENT_SERVICE);
1300 mNetd = INetworkManagementService.Stub.asInterface(b);
1301
Mike Lockwoodfde2b762009-08-14 14:18:49 -04001302 synchronized(this) {
1303 mSystemReady = true;
Dianne Hackborna417ff82009-12-08 19:45:14 -08001304 if (mInitialBroadcast != null) {
1305 mContext.sendStickyBroadcast(mInitialBroadcast);
1306 mInitialBroadcast = null;
Mike Lockwoodfde2b762009-08-14 14:18:49 -04001307 }
1308 }
Robert Greenwaltc3c5f862010-10-11 16:00:27 -07001309 // load the global proxy at startup
1310 mHandler.sendMessage(mHandler.obtainMessage(EVENT_APPLY_GLOBAL_HTTP_PROXY));
The Android Open Source Project28527d22009-03-03 19:31:44 -08001311 }
1312
1313 private void handleConnect(NetworkInfo info) {
Robert Greenwalt2034b912009-08-12 16:08:25 -07001314 int type = info.getType();
The Android Open Source Project28527d22009-03-03 19:31:44 -08001315
1316 // snapshot isFailover, because sendConnectedBroadcast() resets it
1317 boolean isFailover = info.isFailover();
Robert Greenwalt2034b912009-08-12 16:08:25 -07001318 NetworkStateTracker thisNet = mNetTrackers[type];
The Android Open Source Project28527d22009-03-03 19:31:44 -08001319
Robert Greenwalt2034b912009-08-12 16:08:25 -07001320 // if this is a default net and other default is running
1321 // kill the one not preferred
Robert Greenwalt34848c02011-03-25 13:09:25 -07001322 if (mNetConfigs[type].isDefault()) {
Robert Greenwalt2034b912009-08-12 16:08:25 -07001323 if (mActiveDefaultNetwork != -1 && mActiveDefaultNetwork != type) {
1324 if ((type != mNetworkPreference &&
Wink Savillef2a62832011-04-07 14:23:45 -07001325 mNetConfigs[mActiveDefaultNetwork].priority >
1326 mNetConfigs[type].priority) ||
Robert Greenwalt2034b912009-08-12 16:08:25 -07001327 mNetworkPreference == mActiveDefaultNetwork) {
1328 // don't accept this one
Wink Savillee70c6f52010-12-03 12:01:38 -08001329 if (DBG) {
1330 log("Not broadcasting CONNECT_ACTION " +
Robert Greenwalt2034b912009-08-12 16:08:25 -07001331 "to torn down network " + info.getTypeName());
Wink Savillee70c6f52010-12-03 12:01:38 -08001332 }
Robert Greenwalt2034b912009-08-12 16:08:25 -07001333 teardown(thisNet);
1334 return;
1335 } else {
1336 // tear down the other
1337 NetworkStateTracker otherNet =
1338 mNetTrackers[mActiveDefaultNetwork];
Wink Savillee70c6f52010-12-03 12:01:38 -08001339 if (DBG) {
1340 log("Policy requires " + otherNet.getNetworkInfo().getTypeName() +
Robert Greenwalt2034b912009-08-12 16:08:25 -07001341 " teardown");
Wink Savillee70c6f52010-12-03 12:01:38 -08001342 }
Robert Greenwalt2034b912009-08-12 16:08:25 -07001343 if (!teardown(otherNet)) {
Wink Savillee70c6f52010-12-03 12:01:38 -08001344 loge("Network declined teardown request");
Robert Greenwalt99910172011-03-29 11:36:28 -07001345 teardown(thisNet);
Robert Greenwalt2034b912009-08-12 16:08:25 -07001346 return;
1347 }
Robert Greenwalt93dc1042010-06-15 12:19:37 -07001348 }
1349 }
1350 synchronized (ConnectivityService.this) {
1351 // have a new default network, release the transition wakelock in a second
1352 // if it's held. The second pause is to allow apps to reconnect over the
1353 // new network
1354 if (mNetTransitionWakeLock.isHeld()) {
1355 mHandler.sendMessageDelayed(mHandler.obtainMessage(
Robert Greenwaltccb36f92010-09-24 14:32:21 -07001356 EVENT_CLEAR_NET_TRANSITION_WAKELOCK,
Robert Greenwalt93dc1042010-06-15 12:19:37 -07001357 mNetTransitionWakeLockSerialNumber, 0),
1358 1000);
Robert Greenwalt2034b912009-08-12 16:08:25 -07001359 }
1360 }
1361 mActiveDefaultNetwork = type;
Robert Greenwalt986c7412010-09-08 15:24:47 -07001362 // this will cause us to come up initially as unconnected and switching
1363 // to connected after our normal pause unless somebody reports us as reall
1364 // disconnected
1365 mDefaultInetConditionPublished = 0;
1366 mDefaultConnectionSequence++;
1367 mInetConditionChangeInFlight = false;
1368 // Don't do this - if we never sign in stay, grey
1369 //reportNetworkCondition(mActiveDefaultNetwork, 100);
Robert Greenwalt2034b912009-08-12 16:08:25 -07001370 }
The Android Open Source Project28527d22009-03-03 19:31:44 -08001371 thisNet.setTeardownRequested(false);
Irfan Sheriff7f132d92010-06-09 15:39:36 -07001372 updateNetworkSettings(thisNet);
Robert Greenwalt3afbead2010-07-23 15:46:26 -07001373 handleConnectivityChange(type);
Robert Greenwalt2034b912009-08-12 16:08:25 -07001374 sendConnectedBroadcast(info);
The Android Open Source Project28527d22009-03-03 19:31:44 -08001375 }
1376
The Android Open Source Project28527d22009-03-03 19:31:44 -08001377 /**
Robert Greenwalt3afbead2010-07-23 15:46:26 -07001378 * After a change in the connectivity state of a network. We're mainly
1379 * concerned with making sure that the list of DNS servers is set up
1380 * according to which networks are connected, and ensuring that the
1381 * right routing table entries exist.
The Android Open Source Project28527d22009-03-03 19:31:44 -08001382 */
Robert Greenwalt3afbead2010-07-23 15:46:26 -07001383 private void handleConnectivityChange(int netType) {
The Android Open Source Project28527d22009-03-03 19:31:44 -08001384 /*
Robert Greenwalt2034b912009-08-12 16:08:25 -07001385 * If a non-default network is enabled, add the host routes that
Robert Greenwalt3afbead2010-07-23 15:46:26 -07001386 * will allow it's DNS servers to be accessed.
The Android Open Source Project28527d22009-03-03 19:31:44 -08001387 */
Robert Greenwalt3afbead2010-07-23 15:46:26 -07001388 handleDnsConfigurationChange(netType);
Robert Greenwalt2034b912009-08-12 16:08:25 -07001389
Robert Greenwalt3afbead2010-07-23 15:46:26 -07001390 if (mNetTrackers[netType].getNetworkInfo().isConnected()) {
Robert Greenwalt34848c02011-03-25 13:09:25 -07001391 if (mNetConfigs[netType].isDefault()) {
Robert Greenwaltc3c5f862010-10-11 16:00:27 -07001392 handleApplyDefaultProxy(netType);
Robert Greenwalt3afbead2010-07-23 15:46:26 -07001393 addDefaultRoute(mNetTrackers[netType]);
Robert Greenwalt2034b912009-08-12 16:08:25 -07001394 } else {
Robert Greenwalta71471b2011-03-24 21:41:41 -07001395 // many radios add a default route even when we don't want one.
1396 // remove the default interface unless we need it for our active network
1397 if (mActiveDefaultNetwork != -1) {
Robert Greenwalt3a378b92011-03-30 15:03:13 -07001398 LinkProperties linkProperties =
1399 mNetTrackers[mActiveDefaultNetwork].getLinkProperties();
1400 LinkProperties newLinkProperties =
1401 mNetTrackers[netType].getLinkProperties();
1402 String defaultIface = linkProperties.getInterfaceName();
Robert Greenwalta71471b2011-03-24 21:41:41 -07001403 if (defaultIface != null &&
Robert Greenwalt3a378b92011-03-30 15:03:13 -07001404 !defaultIface.equals(newLinkProperties.getInterfaceName())) {
Robert Greenwaltf7dae302011-03-31 10:44:47 -07001405 removeDefaultRoute(mNetTrackers[netType]);
Robert Greenwalta71471b2011-03-24 21:41:41 -07001406 }
1407 }
Robert Greenwaltf7dae302011-03-31 10:44:47 -07001408 addPrivateDnsRoutes(mNetTrackers[netType]);
Robert Greenwalt3afbead2010-07-23 15:46:26 -07001409 }
Kazuhiro Ondo3a340412011-04-30 20:10:57 -05001410
1411 /** Notify TetheringService if interface name has been changed. */
1412 if (TextUtils.equals(mNetTrackers[netType].getNetworkInfo().getReason(),
1413 Phone.REASON_LINK_PROPERTIES_CHANGED)) {
1414 handleTetherIfaceChange(netType);
1415 }
Robert Greenwalt3afbead2010-07-23 15:46:26 -07001416 } else {
Robert Greenwalt34848c02011-03-25 13:09:25 -07001417 if (mNetConfigs[netType].isDefault()) {
Robert Greenwalt3afbead2010-07-23 15:46:26 -07001418 removeDefaultRoute(mNetTrackers[netType]);
1419 } else {
1420 removePrivateDnsRoutes(mNetTrackers[netType]);
Robert Greenwalt2034b912009-08-12 16:08:25 -07001421 }
The Android Open Source Project28527d22009-03-03 19:31:44 -08001422 }
1423 }
1424
Irfan Sheriff7f132d92010-06-09 15:39:36 -07001425 private void addPrivateDnsRoutes(NetworkStateTracker nt) {
Irfan Sheriff7f132d92010-06-09 15:39:36 -07001426 boolean privateDnsRouteSet = nt.isPrivateDnsRouteSet();
Robert Greenwalt1f1bcfe2010-08-30 10:56:47 -07001427 LinkProperties p = nt.getLinkProperties();
Robert Greenwalta7dfbd32010-06-15 15:43:39 -07001428 if (p == null) return;
1429 String interfaceName = p.getInterfaceName();
Irfan Sheriff7f132d92010-06-09 15:39:36 -07001430
1431 if (DBG) {
Wink Savillee70c6f52010-12-03 12:01:38 -08001432 log("addPrivateDnsRoutes for " + nt +
Irfan Sheriff7f132d92010-06-09 15:39:36 -07001433 "(" + interfaceName + ") - mPrivateDnsRouteSet = " + privateDnsRouteSet);
1434 }
Irfan Sheriff7f132d92010-06-09 15:39:36 -07001435 if (interfaceName != null && !privateDnsRouteSet) {
Robert Greenwalta7dfbd32010-06-15 15:43:39 -07001436 Collection<InetAddress> dnsList = p.getDnses();
1437 for (InetAddress dns : dnsList) {
Robert Greenwaltbd492212011-05-06 17:10:53 -07001438 addHostRoute(nt, dns, 0);
Irfan Sheriff7f132d92010-06-09 15:39:36 -07001439 }
1440 nt.privateDnsRouteSet(true);
1441 }
1442 }
1443
1444 private void removePrivateDnsRoutes(NetworkStateTracker nt) {
Robert Greenwalt1f1bcfe2010-08-30 10:56:47 -07001445 LinkProperties p = nt.getLinkProperties();
Robert Greenwalta7dfbd32010-06-15 15:43:39 -07001446 if (p == null) return;
1447 String interfaceName = p.getInterfaceName();
Irfan Sheriff7f132d92010-06-09 15:39:36 -07001448 boolean privateDnsRouteSet = nt.isPrivateDnsRouteSet();
1449 if (interfaceName != null && privateDnsRouteSet) {
1450 if (DBG) {
Wink Savillee70c6f52010-12-03 12:01:38 -08001451 log("removePrivateDnsRoutes for " + nt.getNetworkInfo().getTypeName() +
Irfan Sheriff7f132d92010-06-09 15:39:36 -07001452 " (" + interfaceName + ")");
1453 }
Robert Greenwalt355205c2011-05-10 15:05:02 -07001454
1455 Collection<InetAddress> dnsList = p.getDnses();
1456 for (InetAddress dns : dnsList) {
1457 if (DBG) log(" removing " + dns);
1458 RouteInfo route = RouteInfo.makeHostRoute(dns);
1459 try {
1460 mNetd.removeRoute(interfaceName, route);
1461 } catch (Exception ex) {
1462 loge("error (" + ex + ") removing dns route " + route);
1463 }
1464 }
Irfan Sheriff7f132d92010-06-09 15:39:36 -07001465 nt.privateDnsRouteSet(false);
1466 }
1467 }
1468
Irfan Sheriff7f132d92010-06-09 15:39:36 -07001469
1470 private void addDefaultRoute(NetworkStateTracker nt) {
Robert Greenwalt1f1bcfe2010-08-30 10:56:47 -07001471 LinkProperties p = nt.getLinkProperties();
Robert Greenwalta7dfbd32010-06-15 15:43:39 -07001472 if (p == null) return;
1473 String interfaceName = p.getInterfaceName();
Robert Greenwalt5c733972011-02-09 13:56:06 -08001474 if (TextUtils.isEmpty(interfaceName)) return;
Irfan Sheriff7f132d92010-06-09 15:39:36 -07001475
Robert Greenwalt355205c2011-05-10 15:05:02 -07001476 for (RouteInfo route : p.getRoutes()) {
Robert Greenwalt5a901292011-04-28 14:28:50 -07001477 //TODO - handle non-default routes
1478 if (route.isDefaultRoute()) {
Robert Greenwalt355205c2011-05-10 15:05:02 -07001479 if (DBG) log("adding default route " + route);
Robert Greenwalt5a901292011-04-28 14:28:50 -07001480 InetAddress gateway = route.getGateway();
Robert Greenwalt355205c2011-05-10 15:05:02 -07001481 if (addHostRoute(nt, gateway, 0)) {
1482 try {
1483 mNetd.addRoute(interfaceName, route);
1484 } catch (Exception e) {
1485 loge("error adding default route " + route);
1486 continue;
1487 }
Robert Greenwalt5a901292011-04-28 14:28:50 -07001488 if (DBG) {
1489 NetworkInfo networkInfo = nt.getNetworkInfo();
1490 log("addDefaultRoute for " + networkInfo.getTypeName() +
1491 " (" + interfaceName + "), GatewayAddr=" +
1492 gateway.getHostAddress());
1493 }
Robert Greenwalt355205c2011-05-10 15:05:02 -07001494 } else {
1495 loge("error adding host route for default route " + route);
Robert Greenwalt03d53da2011-03-22 18:47:42 -07001496 }
Irfan Sheriff7f132d92010-06-09 15:39:36 -07001497 }
Irfan Sheriff7f132d92010-06-09 15:39:36 -07001498 }
1499 }
1500
1501
1502 public void removeDefaultRoute(NetworkStateTracker nt) {
Robert Greenwalt1f1bcfe2010-08-30 10:56:47 -07001503 LinkProperties p = nt.getLinkProperties();
Robert Greenwalta7dfbd32010-06-15 15:43:39 -07001504 if (p == null) return;
1505 String interfaceName = p.getInterfaceName();
Irfan Sheriff7f132d92010-06-09 15:39:36 -07001506
Robert Greenwalt355205c2011-05-10 15:05:02 -07001507 if (interfaceName == null) return;
1508
1509 for (RouteInfo route : p.getRoutes()) {
1510 //TODO - handle non-default routes
1511 if (route.isDefaultRoute()) {
1512 try {
1513 mNetd.removeRoute(interfaceName, route);
1514 } catch (Exception ex) {
1515 loge("error (" + ex + ") removing default route " + route);
1516 continue;
1517 }
Robert Greenwalt03d53da2011-03-22 18:47:42 -07001518 if (DBG) {
1519 NetworkInfo networkInfo = nt.getNetworkInfo();
1520 log("removeDefaultRoute for " + networkInfo.getTypeName() + " (" +
1521 interfaceName + ")");
1522 }
Irfan Sheriff7f132d92010-06-09 15:39:36 -07001523 }
Irfan Sheriff7f132d92010-06-09 15:39:36 -07001524 }
1525 }
1526
1527 /**
1528 * Reads the network specific TCP buffer sizes from SystemProperties
1529 * net.tcp.buffersize.[default|wifi|umts|edge|gprs] and set them for system
1530 * wide use
1531 */
1532 public void updateNetworkSettings(NetworkStateTracker nt) {
1533 String key = nt.getTcpBufferSizesPropName();
1534 String bufferSizes = SystemProperties.get(key);
1535
1536 if (bufferSizes.length() == 0) {
Wink Savillee70c6f52010-12-03 12:01:38 -08001537 loge(key + " not found in system properties. Using defaults");
Irfan Sheriff7f132d92010-06-09 15:39:36 -07001538
1539 // Setting to default values so we won't be stuck to previous values
1540 key = "net.tcp.buffersize.default";
1541 bufferSizes = SystemProperties.get(key);
1542 }
1543
1544 // Set values in kernel
1545 if (bufferSizes.length() != 0) {
1546 if (DBG) {
Wink Savillee70c6f52010-12-03 12:01:38 -08001547 log("Setting TCP values: [" + bufferSizes
Irfan Sheriff7f132d92010-06-09 15:39:36 -07001548 + "] which comes from [" + key + "]");
1549 }
1550 setBufferSize(bufferSizes);
1551 }
1552 }
1553
1554 /**
1555 * Writes TCP buffer sizes to /sys/kernel/ipv4/tcp_[r/w]mem_[min/def/max]
1556 * which maps to /proc/sys/net/ipv4/tcp_rmem and tcpwmem
1557 *
1558 * @param bufferSizes in the format of "readMin, readInitial, readMax,
1559 * writeMin, writeInitial, writeMax"
1560 */
1561 private void setBufferSize(String bufferSizes) {
1562 try {
1563 String[] values = bufferSizes.split(",");
1564
1565 if (values.length == 6) {
1566 final String prefix = "/sys/kernel/ipv4/tcp_";
1567 stringToFile(prefix + "rmem_min", values[0]);
1568 stringToFile(prefix + "rmem_def", values[1]);
1569 stringToFile(prefix + "rmem_max", values[2]);
1570 stringToFile(prefix + "wmem_min", values[3]);
1571 stringToFile(prefix + "wmem_def", values[4]);
1572 stringToFile(prefix + "wmem_max", values[5]);
1573 } else {
Wink Savillee70c6f52010-12-03 12:01:38 -08001574 loge("Invalid buffersize string: " + bufferSizes);
Irfan Sheriff7f132d92010-06-09 15:39:36 -07001575 }
1576 } catch (IOException e) {
Wink Savillee70c6f52010-12-03 12:01:38 -08001577 loge("Can't set tcp buffer sizes:" + e);
Irfan Sheriff7f132d92010-06-09 15:39:36 -07001578 }
1579 }
1580
1581 /**
1582 * Writes string to file. Basically same as "echo -n $string > $filename"
1583 *
1584 * @param filename
1585 * @param string
1586 * @throws IOException
1587 */
1588 private void stringToFile(String filename, String string) throws IOException {
1589 FileWriter out = new FileWriter(filename);
1590 try {
1591 out.write(string);
1592 } finally {
1593 out.close();
1594 }
1595 }
1596
1597
Robert Greenwalt2034b912009-08-12 16:08:25 -07001598 /**
1599 * Adjust the per-process dns entries (net.dns<x>.<pid>) based
1600 * on the highest priority active net which this process requested.
1601 * If there aren't any, clear it out
1602 */
1603 private void reassessPidDns(int myPid, boolean doBump)
1604 {
Wink Savillee70c6f52010-12-03 12:01:38 -08001605 if (DBG) log("reassessPidDns for pid " + myPid);
Robert Greenwalt2034b912009-08-12 16:08:25 -07001606 for(int i : mPriorityList) {
Robert Greenwalt34848c02011-03-25 13:09:25 -07001607 if (mNetConfigs[i].isDefault()) {
Robert Greenwalt2034b912009-08-12 16:08:25 -07001608 continue;
1609 }
1610 NetworkStateTracker nt = mNetTrackers[i];
Robert Greenwalt0659da32009-07-16 17:21:39 -07001611 if (nt.getNetworkInfo().isConnected() &&
1612 !nt.isTeardownRequested()) {
Robert Greenwalt1f1bcfe2010-08-30 10:56:47 -07001613 LinkProperties p = nt.getLinkProperties();
Robert Greenwalta7dfbd32010-06-15 15:43:39 -07001614 if (p == null) continue;
Robert Greenwalt2034b912009-08-12 16:08:25 -07001615 List pids = mNetRequestersPids[i];
1616 for (int j=0; j<pids.size(); j++) {
1617 Integer pid = (Integer)pids.get(j);
1618 if (pid.intValue() == myPid) {
Robert Greenwalta7dfbd32010-06-15 15:43:39 -07001619 Collection<InetAddress> dnses = p.getDnses();
1620 writePidDns(dnses, myPid);
Robert Greenwalt2034b912009-08-12 16:08:25 -07001621 if (doBump) {
1622 bumpDns();
1623 }
1624 return;
1625 }
1626 }
1627 }
1628 }
1629 // nothing found - delete
1630 for (int i = 1; ; i++) {
1631 String prop = "net.dns" + i + "." + myPid;
1632 if (SystemProperties.get(prop).length() == 0) {
1633 if (doBump) {
1634 bumpDns();
1635 }
1636 return;
1637 }
1638 SystemProperties.set(prop, "");
1639 }
1640 }
1641
Robert Greenwalt8ca88762010-12-17 15:20:36 -08001642 // return true if results in a change
1643 private boolean writePidDns(Collection <InetAddress> dnses, int pid) {
Robert Greenwalt2034b912009-08-12 16:08:25 -07001644 int j = 1;
Robert Greenwalt8ca88762010-12-17 15:20:36 -08001645 boolean changed = false;
Robert Greenwalta7dfbd32010-06-15 15:43:39 -07001646 for (InetAddress dns : dnses) {
Robert Greenwalt8ca88762010-12-17 15:20:36 -08001647 String dnsString = dns.getHostAddress();
1648 if (changed || !dnsString.equals(SystemProperties.get("net.dns" + j + "." + pid))) {
1649 changed = true;
1650 SystemProperties.set("net.dns" + j++ + "." + pid, dns.getHostAddress());
1651 }
Robert Greenwalt2034b912009-08-12 16:08:25 -07001652 }
Robert Greenwalt8ca88762010-12-17 15:20:36 -08001653 return changed;
Robert Greenwalt2034b912009-08-12 16:08:25 -07001654 }
1655
1656 private void bumpDns() {
1657 /*
1658 * Bump the property that tells the name resolver library to reread
1659 * the DNS server list from the properties.
1660 */
1661 String propVal = SystemProperties.get("net.dnschange");
1662 int n = 0;
1663 if (propVal.length() != 0) {
1664 try {
1665 n = Integer.parseInt(propVal);
1666 } catch (NumberFormatException e) {}
1667 }
1668 SystemProperties.set("net.dnschange", "" + (n+1));
Robert Greenwalt051642b2010-11-02 14:08:23 -07001669 /*
1670 * Tell the VMs to toss their DNS caches
1671 */
1672 Intent intent = new Intent(Intent.ACTION_CLEAR_DNS_CACHE);
1673 intent.addFlags(Intent.FLAG_RECEIVER_REPLACE_PENDING);
Stan Chesnuttf444f502011-01-05 17:14:03 -08001674 /*
1675 * Connectivity events can happen before boot has completed ...
1676 */
1677 intent.addFlags(Intent.FLAG_RECEIVER_REGISTERED_ONLY_BEFORE_BOOT);
Robert Greenwalt051642b2010-11-02 14:08:23 -07001678 mContext.sendBroadcast(intent);
Robert Greenwalt2034b912009-08-12 16:08:25 -07001679 }
1680
Robert Greenwalt3afbead2010-07-23 15:46:26 -07001681 private void handleDnsConfigurationChange(int netType) {
Robert Greenwalt2034b912009-08-12 16:08:25 -07001682 // add default net's dns entries
Robert Greenwalt3afbead2010-07-23 15:46:26 -07001683 NetworkStateTracker nt = mNetTrackers[netType];
1684 if (nt != null && nt.getNetworkInfo().isConnected() && !nt.isTeardownRequested()) {
Robert Greenwalt1f1bcfe2010-08-30 10:56:47 -07001685 LinkProperties p = nt.getLinkProperties();
Robert Greenwalt3afbead2010-07-23 15:46:26 -07001686 if (p == null) return;
1687 Collection<InetAddress> dnses = p.getDnses();
Robert Greenwalt8ca88762010-12-17 15:20:36 -08001688 boolean changed = false;
Robert Greenwalt34848c02011-03-25 13:09:25 -07001689 if (mNetConfigs[netType].isDefault()) {
Robert Greenwalt3afbead2010-07-23 15:46:26 -07001690 int j = 1;
Robert Greenwalt94daa182010-09-01 11:34:05 -07001691 if (dnses.size() == 0 && mDefaultDns != null) {
Robert Greenwalt8ca88762010-12-17 15:20:36 -08001692 String dnsString = mDefaultDns.getHostAddress();
1693 if (!dnsString.equals(SystemProperties.get("net.dns1"))) {
1694 if (DBG) {
1695 log("no dns provided - using " + dnsString);
1696 }
1697 changed = true;
1698 SystemProperties.set("net.dns1", dnsString);
Robert Greenwalt2034b912009-08-12 16:08:25 -07001699 }
Robert Greenwalt94daa182010-09-01 11:34:05 -07001700 j++;
1701 } else {
1702 for (InetAddress dns : dnses) {
Robert Greenwalt8ca88762010-12-17 15:20:36 -08001703 String dnsString = dns.getHostAddress();
1704 if (!changed && dnsString.equals(SystemProperties.get("net.dns" + j))) {
1705 j++;
1706 continue;
1707 }
Robert Greenwalt94daa182010-09-01 11:34:05 -07001708 if (DBG) {
Wink Savillee70c6f52010-12-03 12:01:38 -08001709 log("adding dns " + dns + " for " +
Robert Greenwalt94daa182010-09-01 11:34:05 -07001710 nt.getNetworkInfo().getTypeName());
1711 }
Robert Greenwalt8ca88762010-12-17 15:20:36 -08001712 changed = true;
1713 SystemProperties.set("net.dns" + j++, dnsString);
Robert Greenwalt94daa182010-09-01 11:34:05 -07001714 }
Robert Greenwalt3afbead2010-07-23 15:46:26 -07001715 }
1716 for (int k=j ; k<mNumDnsEntries; k++) {
Robert Greenwalt8ca88762010-12-17 15:20:36 -08001717 if (changed || !TextUtils.isEmpty(SystemProperties.get("net.dns" + k))) {
1718 if (DBG) log("erasing net.dns" + k);
1719 changed = true;
1720 SystemProperties.set("net.dns" + k, "");
1721 }
Robert Greenwalt3afbead2010-07-23 15:46:26 -07001722 }
1723 mNumDnsEntries = j;
1724 } else {
1725 // set per-pid dns for attached secondary nets
1726 List pids = mNetRequestersPids[netType];
1727 for (int y=0; y< pids.size(); y++) {
1728 Integer pid = (Integer)pids.get(y);
Robert Greenwalt8ca88762010-12-17 15:20:36 -08001729 changed = writePidDns(dnses, pid.intValue());
The Android Open Source Project28527d22009-03-03 19:31:44 -08001730 }
1731 }
Robert Greenwalt8ca88762010-12-17 15:20:36 -08001732 if (changed) bumpDns();
The Android Open Source Project28527d22009-03-03 19:31:44 -08001733 }
Robert Greenwalt2034b912009-08-12 16:08:25 -07001734 }
1735
Robert Greenwalt20f819c2011-05-03 19:02:44 -07001736 private int getRestoreDefaultNetworkDelay(int networkType) {
Robert Greenwalt2034b912009-08-12 16:08:25 -07001737 String restoreDefaultNetworkDelayStr = SystemProperties.get(
1738 NETWORK_RESTORE_DELAY_PROP_NAME);
1739 if(restoreDefaultNetworkDelayStr != null &&
1740 restoreDefaultNetworkDelayStr.length() != 0) {
1741 try {
1742 return Integer.valueOf(restoreDefaultNetworkDelayStr);
1743 } catch (NumberFormatException e) {
1744 }
The Android Open Source Project28527d22009-03-03 19:31:44 -08001745 }
Robert Greenwalt20f819c2011-05-03 19:02:44 -07001746 // if the system property isn't set, use the value for the apn type
1747 int ret = RESTORE_DEFAULT_NETWORK_DELAY;
1748
1749 if ((networkType <= ConnectivityManager.MAX_NETWORK_TYPE) &&
1750 (mNetConfigs[networkType] != null)) {
1751 ret = mNetConfigs[networkType].restoreTime;
1752 }
1753 return ret;
The Android Open Source Project28527d22009-03-03 19:31:44 -08001754 }
1755
1756 @Override
1757 protected void dump(FileDescriptor fd, PrintWriter pw, String[] args) {
Robert Greenwalt0659da32009-07-16 17:21:39 -07001758 if (mContext.checkCallingOrSelfPermission(
1759 android.Manifest.permission.DUMP)
The Android Open Source Project28527d22009-03-03 19:31:44 -08001760 != PackageManager.PERMISSION_GRANTED) {
Robert Greenwalt0659da32009-07-16 17:21:39 -07001761 pw.println("Permission Denial: can't dump ConnectivityService " +
1762 "from from pid=" + Binder.getCallingPid() + ", uid=" +
1763 Binder.getCallingUid());
The Android Open Source Project28527d22009-03-03 19:31:44 -08001764 return;
1765 }
The Android Open Source Project28527d22009-03-03 19:31:44 -08001766 pw.println();
1767 for (NetworkStateTracker nst : mNetTrackers) {
Robert Greenwalt3eeb6032009-12-21 18:24:07 -08001768 if (nst != null) {
1769 if (nst.getNetworkInfo().isConnected()) {
1770 pw.println("Active network: " + nst.getNetworkInfo().
1771 getTypeName());
1772 }
1773 pw.println(nst.getNetworkInfo());
1774 pw.println(nst);
1775 pw.println();
Robert Greenwalt2034b912009-08-12 16:08:25 -07001776 }
The Android Open Source Project28527d22009-03-03 19:31:44 -08001777 }
Robert Greenwalt3eeb6032009-12-21 18:24:07 -08001778
1779 pw.println("Network Requester Pids:");
1780 for (int net : mPriorityList) {
1781 String pidString = net + ": ";
1782 for (Object pid : mNetRequestersPids[net]) {
1783 pidString = pidString + pid.toString() + ", ";
1784 }
1785 pw.println(pidString);
1786 }
1787 pw.println();
1788
1789 pw.println("FeatureUsers:");
1790 for (Object requester : mFeatureUsers) {
1791 pw.println(requester.toString());
1792 }
1793 pw.println();
Robert Greenwalt8e87f122010-02-11 18:18:40 -08001794
Robert Greenwalt93dc1042010-06-15 12:19:37 -07001795 synchronized (this) {
1796 pw.println("NetworkTranstionWakeLock is currently " +
1797 (mNetTransitionWakeLock.isHeld() ? "" : "not ") + "held.");
1798 pw.println("It was last requested for "+mNetTransitionWakeLockCausedBy);
1799 }
1800 pw.println();
1801
Robert Greenwalt8e87f122010-02-11 18:18:40 -08001802 mTethering.dump(fd, pw, args);
Robert Greenwalt0e80be12010-09-20 14:35:25 -07001803
1804 if (mInetLog != null) {
1805 pw.println();
1806 pw.println("Inet condition reports:");
1807 for(int i = 0; i < mInetLog.size(); i++) {
1808 pw.println(mInetLog.get(i));
1809 }
1810 }
The Android Open Source Project28527d22009-03-03 19:31:44 -08001811 }
1812
Robert Greenwalt2034b912009-08-12 16:08:25 -07001813 // must be stateless - things change under us.
The Android Open Source Project28527d22009-03-03 19:31:44 -08001814 private class MyHandler extends Handler {
Wink Saville775aad62010-09-02 19:23:52 -07001815 public MyHandler(Looper looper) {
1816 super(looper);
1817 }
1818
The Android Open Source Project28527d22009-03-03 19:31:44 -08001819 @Override
1820 public void handleMessage(Message msg) {
1821 NetworkInfo info;
1822 switch (msg.what) {
1823 case NetworkStateTracker.EVENT_STATE_CHANGED:
1824 info = (NetworkInfo) msg.obj;
Robert Greenwalt12c44552009-12-07 11:33:18 -08001825 int type = info.getType();
1826 NetworkInfo.State state = info.getState();
Robert Greenwalt12c44552009-12-07 11:33:18 -08001827
Wink Savillee70c6f52010-12-03 12:01:38 -08001828 if (DBG) log("ConnectivityChange for " +
Robert Greenwalt0659da32009-07-16 17:21:39 -07001829 info.getTypeName() + ": " +
Robert Greenwalt12c44552009-12-07 11:33:18 -08001830 state + "/" + info.getDetailedState());
The Android Open Source Project28527d22009-03-03 19:31:44 -08001831
1832 // Connectivity state changed:
1833 // [31-13] Reserved for future use
Robert Greenwalt0659da32009-07-16 17:21:39 -07001834 // [12-9] Network subtype (for mobile network, as defined
1835 // by TelephonyManager)
1836 // [8-3] Detailed state ordinal (as defined by
1837 // NetworkInfo.DetailedState)
The Android Open Source Project28527d22009-03-03 19:31:44 -08001838 // [2-0] Network type (as defined by ConnectivityManager)
1839 int eventLogParam = (info.getType() & 0x7) |
1840 ((info.getDetailedState().ordinal() & 0x3f) << 3) |
1841 (info.getSubtype() << 9);
Doug Zongker2fc96232009-12-04 10:31:43 -08001842 EventLog.writeEvent(EventLogTags.CONNECTIVITY_STATE_CHANGED,
Robert Greenwalt0659da32009-07-16 17:21:39 -07001843 eventLogParam);
1844
1845 if (info.getDetailedState() ==
1846 NetworkInfo.DetailedState.FAILED) {
The Android Open Source Project28527d22009-03-03 19:31:44 -08001847 handleConnectionFailure(info);
Robert Greenwalt12c44552009-12-07 11:33:18 -08001848 } else if (state == NetworkInfo.State.DISCONNECTED) {
The Android Open Source Project28527d22009-03-03 19:31:44 -08001849 handleDisconnect(info);
Robert Greenwalt12c44552009-12-07 11:33:18 -08001850 } else if (state == NetworkInfo.State.SUSPENDED) {
The Android Open Source Project28527d22009-03-03 19:31:44 -08001851 // TODO: need to think this over.
Robert Greenwalt0659da32009-07-16 17:21:39 -07001852 // the logic here is, handle SUSPENDED the same as
1853 // DISCONNECTED. The only difference being we are
1854 // broadcasting an intent with NetworkInfo that's
1855 // suspended. This allows the applications an
1856 // opportunity to handle DISCONNECTED and SUSPENDED
1857 // differently, or not.
The Android Open Source Project28527d22009-03-03 19:31:44 -08001858 handleDisconnect(info);
Robert Greenwalt12c44552009-12-07 11:33:18 -08001859 } else if (state == NetworkInfo.State.CONNECTED) {
The Android Open Source Project28527d22009-03-03 19:31:44 -08001860 handleConnect(info);
1861 }
The Android Open Source Project28527d22009-03-03 19:31:44 -08001862 break;
The Android Open Source Project28527d22009-03-03 19:31:44 -08001863 case NetworkStateTracker.EVENT_CONFIGURATION_CHANGED:
Robert Greenwalt3afbead2010-07-23 15:46:26 -07001864 info = (NetworkInfo) msg.obj;
Robert Greenwalt34848c02011-03-25 13:09:25 -07001865 handleConnectivityChange(info.getType());
The Android Open Source Project28527d22009-03-03 19:31:44 -08001866 break;
Robert Greenwaltccb36f92010-09-24 14:32:21 -07001867 case EVENT_CLEAR_NET_TRANSITION_WAKELOCK:
Robert Greenwalt93dc1042010-06-15 12:19:37 -07001868 String causedBy = null;
1869 synchronized (ConnectivityService.this) {
1870 if (msg.arg1 == mNetTransitionWakeLockSerialNumber &&
1871 mNetTransitionWakeLock.isHeld()) {
1872 mNetTransitionWakeLock.release();
1873 causedBy = mNetTransitionWakeLockCausedBy;
1874 }
1875 }
1876 if (causedBy != null) {
Wink Savillee70c6f52010-12-03 12:01:38 -08001877 log("NetTransition Wakelock for " + causedBy + " released by timeout");
Robert Greenwalt93dc1042010-06-15 12:19:37 -07001878 }
Robert Greenwaltcf1a56c2010-09-09 14:05:10 -07001879 break;
Robert Greenwalt6a2db8a2010-09-23 10:05:56 -07001880 case EVENT_RESTORE_DEFAULT_NETWORK:
Robert Greenwaltaffc3a12009-09-27 17:27:04 -07001881 FeatureUser u = (FeatureUser)msg.obj;
1882 u.expire();
Robert Greenwalt986c7412010-09-08 15:24:47 -07001883 break;
Robert Greenwalt6a2db8a2010-09-23 10:05:56 -07001884 case EVENT_INET_CONDITION_CHANGE:
1885 {
1886 int netType = msg.arg1;
1887 int condition = msg.arg2;
1888 handleInetConditionChange(netType, condition);
Robert Greenwalt93dc1042010-06-15 12:19:37 -07001889 break;
Robert Greenwalt6a2db8a2010-09-23 10:05:56 -07001890 }
1891 case EVENT_INET_CONDITION_HOLD_END:
1892 {
1893 int netType = msg.arg1;
1894 int sequence = msg.arg2;
1895 handleInetConditionHoldEnd(netType, sequence);
Robert Greenwalt986c7412010-09-08 15:24:47 -07001896 break;
Robert Greenwalt6a2db8a2010-09-23 10:05:56 -07001897 }
1898 case EVENT_SET_NETWORK_PREFERENCE:
1899 {
1900 int preference = msg.arg1;
1901 handleSetNetworkPreference(preference);
1902 break;
1903 }
1904 case EVENT_SET_BACKGROUND_DATA:
1905 {
1906 boolean enabled = (msg.arg1 == ENABLED);
1907 handleSetBackgroundData(enabled);
1908 break;
1909 }
1910 case EVENT_SET_MOBILE_DATA:
1911 {
1912 boolean enabled = (msg.arg1 == ENABLED);
1913 handleSetMobileData(enabled);
1914 break;
1915 }
Robert Greenwaltc3c5f862010-10-11 16:00:27 -07001916 case EVENT_APPLY_GLOBAL_HTTP_PROXY:
1917 {
1918 handleDeprecatedGlobalHttpProxy();
Robert Greenwalt34848c02011-03-25 13:09:25 -07001919 break;
1920 }
1921 case EVENT_SET_DEPENDENCY_MET:
1922 {
1923 boolean met = (msg.arg1 == ENABLED);
1924 handleSetDependencyMet(msg.arg2, met);
1925 break;
Robert Greenwaltc3c5f862010-10-11 16:00:27 -07001926 }
The Android Open Source Project28527d22009-03-03 19:31:44 -08001927 }
1928 }
1929 }
Robert Greenwalt0c4828c2010-01-26 11:40:34 -08001930
1931 // javadoc from interface
Robert Greenwalt4283ded2010-03-02 17:25:02 -08001932 public int tether(String iface) {
Robert Greenwalt0c4828c2010-01-26 11:40:34 -08001933 enforceTetherChangePermission();
Robert Greenwalt4283ded2010-03-02 17:25:02 -08001934
1935 if (isTetheringSupported()) {
1936 return mTethering.tether(iface);
1937 } else {
1938 return ConnectivityManager.TETHER_ERROR_UNSUPPORTED;
1939 }
Robert Greenwalt0c4828c2010-01-26 11:40:34 -08001940 }
1941
1942 // javadoc from interface
Robert Greenwalt4283ded2010-03-02 17:25:02 -08001943 public int untether(String iface) {
Robert Greenwalt0c4828c2010-01-26 11:40:34 -08001944 enforceTetherChangePermission();
Robert Greenwalt4283ded2010-03-02 17:25:02 -08001945
1946 if (isTetheringSupported()) {
1947 return mTethering.untether(iface);
1948 } else {
1949 return ConnectivityManager.TETHER_ERROR_UNSUPPORTED;
1950 }
1951 }
1952
1953 // javadoc from interface
1954 public int getLastTetherError(String iface) {
1955 enforceTetherAccessPermission();
1956
1957 if (isTetheringSupported()) {
1958 return mTethering.getLastTetherError(iface);
1959 } else {
1960 return ConnectivityManager.TETHER_ERROR_UNSUPPORTED;
1961 }
Robert Greenwalt8e87f122010-02-11 18:18:40 -08001962 }
1963
1964 // TODO - proper iface API for selection by property, inspection, etc
1965 public String[] getTetherableUsbRegexs() {
1966 enforceTetherAccessPermission();
1967 if (isTetheringSupported()) {
1968 return mTethering.getTetherableUsbRegexs();
1969 } else {
1970 return new String[0];
1971 }
1972 }
1973
1974 public String[] getTetherableWifiRegexs() {
1975 enforceTetherAccessPermission();
1976 if (isTetheringSupported()) {
1977 return mTethering.getTetherableWifiRegexs();
1978 } else {
1979 return new String[0];
1980 }
Robert Greenwalt0c4828c2010-01-26 11:40:34 -08001981 }
1982
Danica Chang96567052010-08-11 14:54:43 -07001983 public String[] getTetherableBluetoothRegexs() {
1984 enforceTetherAccessPermission();
1985 if (isTetheringSupported()) {
1986 return mTethering.getTetherableBluetoothRegexs();
1987 } else {
1988 return new String[0];
1989 }
1990 }
1991
Robert Greenwalt0c4828c2010-01-26 11:40:34 -08001992 // TODO - move iface listing, queries, etc to new module
1993 // javadoc from interface
1994 public String[] getTetherableIfaces() {
Robert Greenwalt8e87f122010-02-11 18:18:40 -08001995 enforceTetherAccessPermission();
Robert Greenwalt0c4828c2010-01-26 11:40:34 -08001996 return mTethering.getTetherableIfaces();
1997 }
1998
1999 public String[] getTetheredIfaces() {
Robert Greenwalt8e87f122010-02-11 18:18:40 -08002000 enforceTetherAccessPermission();
Robert Greenwalt0c4828c2010-01-26 11:40:34 -08002001 return mTethering.getTetheredIfaces();
2002 }
Robert Greenwalt8e87f122010-02-11 18:18:40 -08002003
Robert Greenwalt4283ded2010-03-02 17:25:02 -08002004 public String[] getTetheringErroredIfaces() {
2005 enforceTetherAccessPermission();
2006 return mTethering.getErroredIfaces();
2007 }
2008
Robert Greenwalt8e87f122010-02-11 18:18:40 -08002009 // if ro.tether.denied = true we default to no tethering
2010 // gservices could set the secure setting to 1 though to enable it on a build where it
2011 // had previously been turned off.
2012 public boolean isTetheringSupported() {
2013 enforceTetherAccessPermission();
2014 int defaultVal = (SystemProperties.get("ro.tether.denied").equals("true") ? 0 : 1);
Robert Greenwaltf1b66e12010-02-25 12:29:30 -08002015 boolean tetherEnabledInSettings = (Settings.Secure.getInt(mContext.getContentResolver(),
2016 Settings.Secure.TETHER_SUPPORTED, defaultVal) != 0);
2017 return tetherEnabledInSettings && mTetheringConfigValid;
Robert Greenwalt8e87f122010-02-11 18:18:40 -08002018 }
Robert Greenwalt93dc1042010-06-15 12:19:37 -07002019
2020 // An API NetworkStateTrackers can call when they lose their network.
2021 // This will automatically be cleared after X seconds or a network becomes CONNECTED,
2022 // whichever happens first. The timer is started by the first caller and not
2023 // restarted by subsequent callers.
2024 public void requestNetworkTransitionWakelock(String forWhom) {
2025 enforceConnectivityInternalPermission();
2026 synchronized (this) {
2027 if (mNetTransitionWakeLock.isHeld()) return;
2028 mNetTransitionWakeLockSerialNumber++;
2029 mNetTransitionWakeLock.acquire();
2030 mNetTransitionWakeLockCausedBy = forWhom;
2031 }
2032 mHandler.sendMessageDelayed(mHandler.obtainMessage(
Robert Greenwaltccb36f92010-09-24 14:32:21 -07002033 EVENT_CLEAR_NET_TRANSITION_WAKELOCK,
Robert Greenwalt93dc1042010-06-15 12:19:37 -07002034 mNetTransitionWakeLockSerialNumber, 0),
2035 mNetTransitionWakeLockTimeout);
2036 return;
2037 }
Robert Greenwalt24118e82010-09-09 13:15:32 -07002038
Robert Greenwalt986c7412010-09-08 15:24:47 -07002039 // 100 percent is full good, 0 is full bad.
2040 public void reportInetCondition(int networkType, int percentage) {
Wink Savillee70c6f52010-12-03 12:01:38 -08002041 if (DBG) log("reportNetworkCondition(" + networkType + ", " + percentage + ")");
Robert Greenwalt986c7412010-09-08 15:24:47 -07002042 mContext.enforceCallingOrSelfPermission(
2043 android.Manifest.permission.STATUS_BAR,
2044 "ConnectivityService");
2045
Robert Greenwalt0e80be12010-09-20 14:35:25 -07002046 if (DBG) {
2047 int pid = getCallingPid();
2048 int uid = getCallingUid();
2049 String s = pid + "(" + uid + ") reports inet is " +
2050 (percentage > 50 ? "connected" : "disconnected") + " (" + percentage + ") on " +
2051 "network Type " + networkType + " at " + GregorianCalendar.getInstance().getTime();
2052 mInetLog.add(s);
2053 while(mInetLog.size() > INET_CONDITION_LOG_MAX_SIZE) {
2054 mInetLog.remove(0);
2055 }
2056 }
Robert Greenwalt986c7412010-09-08 15:24:47 -07002057 mHandler.sendMessage(mHandler.obtainMessage(
Robert Greenwalt6a2db8a2010-09-23 10:05:56 -07002058 EVENT_INET_CONDITION_CHANGE, networkType, percentage));
2059 }
2060
2061 private void handleInetConditionChange(int netType, int condition) {
2062 if (DBG) {
Wink Savillee70c6f52010-12-03 12:01:38 -08002063 log("Inet connectivity change, net=" +
Robert Greenwalt6a2db8a2010-09-23 10:05:56 -07002064 netType + ", condition=" + condition +
2065 ",mActiveDefaultNetwork=" + mActiveDefaultNetwork);
2066 }
2067 if (mActiveDefaultNetwork == -1) {
Wink Savillee70c6f52010-12-03 12:01:38 -08002068 if (DBG) log("no active default network - aborting");
Robert Greenwalt6a2db8a2010-09-23 10:05:56 -07002069 return;
2070 }
2071 if (mActiveDefaultNetwork != netType) {
Wink Savillee70c6f52010-12-03 12:01:38 -08002072 if (DBG) log("given net not default - aborting");
Robert Greenwalt6a2db8a2010-09-23 10:05:56 -07002073 return;
2074 }
2075 mDefaultInetCondition = condition;
2076 int delay;
2077 if (mInetConditionChangeInFlight == false) {
Wink Savillee70c6f52010-12-03 12:01:38 -08002078 if (DBG) log("starting a change hold");
Robert Greenwalt6a2db8a2010-09-23 10:05:56 -07002079 // setup a new hold to debounce this
2080 if (mDefaultInetCondition > 50) {
2081 delay = Settings.Secure.getInt(mContext.getContentResolver(),
2082 Settings.Secure.INET_CONDITION_DEBOUNCE_UP_DELAY, 500);
2083 } else {
2084 delay = Settings.Secure.getInt(mContext.getContentResolver(),
2085 Settings.Secure.INET_CONDITION_DEBOUNCE_DOWN_DELAY, 3000);
2086 }
2087 mInetConditionChangeInFlight = true;
2088 mHandler.sendMessageDelayed(mHandler.obtainMessage(EVENT_INET_CONDITION_HOLD_END,
2089 mActiveDefaultNetwork, mDefaultConnectionSequence), delay);
2090 } else {
2091 // we've set the new condition, when this hold ends that will get
2092 // picked up
Wink Savillee70c6f52010-12-03 12:01:38 -08002093 if (DBG) log("currently in hold - not setting new end evt");
Robert Greenwalt6a2db8a2010-09-23 10:05:56 -07002094 }
2095 }
2096
2097 private void handleInetConditionHoldEnd(int netType, int sequence) {
2098 if (DBG) {
Wink Savillee70c6f52010-12-03 12:01:38 -08002099 log("Inet hold end, net=" + netType +
Robert Greenwalt6a2db8a2010-09-23 10:05:56 -07002100 ", condition =" + mDefaultInetCondition +
2101 ", published condition =" + mDefaultInetConditionPublished);
2102 }
2103 mInetConditionChangeInFlight = false;
2104
2105 if (mActiveDefaultNetwork == -1) {
Wink Savillee70c6f52010-12-03 12:01:38 -08002106 if (DBG) log("no active default network - aborting");
Robert Greenwalt6a2db8a2010-09-23 10:05:56 -07002107 return;
2108 }
2109 if (mDefaultConnectionSequence != sequence) {
Wink Savillee70c6f52010-12-03 12:01:38 -08002110 if (DBG) log("event hold for obsolete network - aborting");
Robert Greenwalt6a2db8a2010-09-23 10:05:56 -07002111 return;
2112 }
2113 if (mDefaultInetConditionPublished == mDefaultInetCondition) {
Wink Savillee70c6f52010-12-03 12:01:38 -08002114 if (DBG) log("no change in condition - aborting");
Robert Greenwalt6a2db8a2010-09-23 10:05:56 -07002115 return;
2116 }
2117 NetworkInfo networkInfo = mNetTrackers[mActiveDefaultNetwork].getNetworkInfo();
2118 if (networkInfo.isConnected() == false) {
Wink Savillee70c6f52010-12-03 12:01:38 -08002119 if (DBG) log("default network not connected - aborting");
Robert Greenwalt6a2db8a2010-09-23 10:05:56 -07002120 return;
2121 }
2122 mDefaultInetConditionPublished = mDefaultInetCondition;
2123 sendInetConditionBroadcast(networkInfo);
2124 return;
Robert Greenwalt986c7412010-09-08 15:24:47 -07002125 }
Robert Greenwaltc3c5f862010-10-11 16:00:27 -07002126
2127 public synchronized ProxyProperties getProxy() {
2128 if (mGlobalProxy != null) return mGlobalProxy;
2129 if (mDefaultProxy != null) return mDefaultProxy;
2130 return null;
2131 }
2132
2133 public void setGlobalProxy(ProxyProperties proxyProperties) {
2134 enforceChangePermission();
2135 synchronized (mGlobalProxyLock) {
2136 if (proxyProperties == mGlobalProxy) return;
2137 if (proxyProperties != null && proxyProperties.equals(mGlobalProxy)) return;
2138 if (mGlobalProxy != null && mGlobalProxy.equals(proxyProperties)) return;
2139
2140 String host = "";
2141 int port = 0;
2142 String exclList = "";
2143 if (proxyProperties != null && !TextUtils.isEmpty(proxyProperties.getHost())) {
2144 mGlobalProxy = new ProxyProperties(proxyProperties);
2145 host = mGlobalProxy.getHost();
2146 port = mGlobalProxy.getPort();
2147 exclList = mGlobalProxy.getExclusionList();
2148 } else {
2149 mGlobalProxy = null;
2150 }
2151 ContentResolver res = mContext.getContentResolver();
2152 Settings.Secure.putString(res, Settings.Secure.GLOBAL_HTTP_PROXY_HOST, host);
2153 Settings.Secure.putInt(res, Settings.Secure.GLOBAL_HTTP_PROXY_PORT, port);
Robert Greenwalt6f7c6092010-12-02 11:31:00 -08002154 Settings.Secure.putString(res, Settings.Secure.GLOBAL_HTTP_PROXY_EXCLUSION_LIST,
Robert Greenwaltc3c5f862010-10-11 16:00:27 -07002155 exclList);
2156 }
2157
2158 if (mGlobalProxy == null) {
2159 proxyProperties = mDefaultProxy;
2160 }
2161 sendProxyBroadcast(proxyProperties);
2162 }
2163
Robert Greenwalt6f7c6092010-12-02 11:31:00 -08002164 private void loadGlobalProxy() {
2165 ContentResolver res = mContext.getContentResolver();
2166 String host = Settings.Secure.getString(res, Settings.Secure.GLOBAL_HTTP_PROXY_HOST);
2167 int port = Settings.Secure.getInt(res, Settings.Secure.GLOBAL_HTTP_PROXY_PORT, 0);
2168 String exclList = Settings.Secure.getString(res,
2169 Settings.Secure.GLOBAL_HTTP_PROXY_EXCLUSION_LIST);
2170 if (!TextUtils.isEmpty(host)) {
2171 ProxyProperties proxyProperties = new ProxyProperties(host, port, exclList);
2172 synchronized (mGlobalProxyLock) {
2173 mGlobalProxy = proxyProperties;
2174 }
2175 }
2176 }
2177
Robert Greenwaltc3c5f862010-10-11 16:00:27 -07002178 public ProxyProperties getGlobalProxy() {
2179 synchronized (mGlobalProxyLock) {
2180 return mGlobalProxy;
2181 }
2182 }
2183
2184 private void handleApplyDefaultProxy(int type) {
2185 // check if new default - push it out to all VM if so
2186 ProxyProperties proxy = mNetTrackers[type].getLinkProperties().getHttpProxy();
2187 synchronized (this) {
2188 if (mDefaultProxy != null && mDefaultProxy.equals(proxy)) return;
2189 if (mDefaultProxy == proxy) return;
2190 if (!TextUtils.isEmpty(proxy.getHost())) {
2191 mDefaultProxy = proxy;
2192 } else {
2193 mDefaultProxy = null;
2194 }
2195 }
Wink Savillee70c6f52010-12-03 12:01:38 -08002196 if (DBG) log("changing default proxy to " + proxy);
Robert Greenwaltc3c5f862010-10-11 16:00:27 -07002197 if ((proxy == null && mGlobalProxy == null) || proxy.equals(mGlobalProxy)) return;
2198 if (mGlobalProxy != null) return;
2199 sendProxyBroadcast(proxy);
2200 }
2201
2202 private void handleDeprecatedGlobalHttpProxy() {
2203 String proxy = Settings.Secure.getString(mContext.getContentResolver(),
2204 Settings.Secure.HTTP_PROXY);
2205 if (!TextUtils.isEmpty(proxy)) {
2206 String data[] = proxy.split(":");
2207 String proxyHost = data[0];
2208 int proxyPort = 8080;
2209 if (data.length > 1) {
2210 try {
2211 proxyPort = Integer.parseInt(data[1]);
2212 } catch (NumberFormatException e) {
2213 return;
2214 }
2215 }
2216 ProxyProperties p = new ProxyProperties(data[0], proxyPort, "");
2217 setGlobalProxy(p);
2218 }
2219 }
2220
2221 private void sendProxyBroadcast(ProxyProperties proxy) {
Robert Greenwalt611291c2010-12-23 15:51:10 -08002222 if (proxy == null) proxy = new ProxyProperties("", 0, "");
Wink Savillee70c6f52010-12-03 12:01:38 -08002223 log("sending Proxy Broadcast for " + proxy);
Robert Greenwaltc3c5f862010-10-11 16:00:27 -07002224 Intent intent = new Intent(Proxy.PROXY_CHANGE_ACTION);
Stan Chesnutt1f2a2ac2011-01-06 11:00:19 -08002225 intent.addFlags(Intent.FLAG_RECEIVER_REPLACE_PENDING |
2226 Intent.FLAG_RECEIVER_REGISTERED_ONLY_BEFORE_BOOT);
Robert Greenwaltc3c5f862010-10-11 16:00:27 -07002227 intent.putExtra(Proxy.EXTRA_PROXY_INFO, proxy);
Robert Greenwaltd93dc8f2010-12-06 11:29:17 -08002228 mContext.sendStickyBroadcast(intent);
Robert Greenwaltc3c5f862010-10-11 16:00:27 -07002229 }
2230
2231 private static class SettingsObserver extends ContentObserver {
2232 private int mWhat;
2233 private Handler mHandler;
2234 SettingsObserver(Handler handler, int what) {
2235 super(handler);
2236 mHandler = handler;
2237 mWhat = what;
2238 }
2239
2240 void observe(Context context) {
2241 ContentResolver resolver = context.getContentResolver();
2242 resolver.registerContentObserver(Settings.Secure.getUriFor(
2243 Settings.Secure.HTTP_PROXY), false, this);
2244 }
2245
2246 @Override
2247 public void onChange(boolean selfChange) {
2248 mHandler.obtainMessage(mWhat).sendToTarget();
2249 }
2250 }
Wink Savillee70c6f52010-12-03 12:01:38 -08002251
Kazuhiro Ondo3a340412011-04-30 20:10:57 -05002252 private void handleTetherIfaceChange(int type) {
2253 String iface = mNetTrackers[type].getLinkProperties().getInterfaceName();
2254
2255 if (isTetheringSupported()) {
2256 mTethering.handleTetherIfaceChange(iface);
2257 }
2258 }
2259
Wink Savillee70c6f52010-12-03 12:01:38 -08002260 private void log(String s) {
2261 Slog.d(TAG, s);
2262 }
2263
2264 private void loge(String s) {
2265 Slog.e(TAG, s);
2266 }
Wink Savilleb7c92c72011-03-12 14:52:01 -08002267 int convertFeatureToNetworkType(String feature){
2268 int networkType = -1;
2269 if (TextUtils.equals(feature, Phone.FEATURE_ENABLE_MMS)) {
2270 networkType = ConnectivityManager.TYPE_MOBILE_MMS;
2271 } else if (TextUtils.equals(feature, Phone.FEATURE_ENABLE_SUPL)) {
2272 networkType = ConnectivityManager.TYPE_MOBILE_SUPL;
2273 } else if (TextUtils.equals(feature, Phone.FEATURE_ENABLE_DUN) ||
2274 TextUtils.equals(feature, Phone.FEATURE_ENABLE_DUN_ALWAYS)) {
2275 networkType = ConnectivityManager.TYPE_MOBILE_DUN;
2276 } else if (TextUtils.equals(feature, Phone.FEATURE_ENABLE_HIPRI)) {
2277 networkType = ConnectivityManager.TYPE_MOBILE_HIPRI;
2278 } else if (TextUtils.equals(feature, Phone.FEATURE_ENABLE_FOTA)) {
2279 networkType = ConnectivityManager.TYPE_MOBILE_FOTA;
2280 } else if (TextUtils.equals(feature, Phone.FEATURE_ENABLE_IMS)) {
2281 networkType = ConnectivityManager.TYPE_MOBILE_IMS;
2282 } else if (TextUtils.equals(feature, Phone.FEATURE_ENABLE_CBS)) {
2283 networkType = ConnectivityManager.TYPE_MOBILE_CBS;
2284 }
2285 return networkType;
2286 }
The Android Open Source Project28527d22009-03-03 19:31:44 -08002287}