blob: cc65b56f37b14cde6ac9f772dc461b3991bf165b [file] [log] [blame]
The Android Open Source Project9066cfe2009-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 Ganesh15c74392010-12-21 22:31:44 -080019import android.bluetooth.BluetoothTetheringDataTracker;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080020import android.content.ContentResolver;
21import android.content.Context;
22import android.content.Intent;
23import android.content.pm.PackageManager;
Robert Greenwalt434203a2010-10-11 16:00:27 -070024import android.database.ContentObserver;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080025import android.net.ConnectivityManager;
Robert Greenwaltda3d5e62010-12-06 13:56:24 -080026import android.net.DummyDataStateTracker;
Benoit Goby08c39c62010-12-22 14:29:40 -080027import android.net.EthernetDataTracker;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080028import android.net.IConnectivityManager;
Robert Greenwalt59b1a4e2011-05-10 15:05:02 -070029import android.net.LinkAddress;
Jaikumar Ganesh15c74392010-12-21 22:31:44 -080030import android.net.LinkProperties;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080031import android.net.MobileDataStateTracker;
Robert Greenwaltd55a6b42011-03-25 13:09:25 -070032import android.net.NetworkConfig;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080033import android.net.NetworkInfo;
34import android.net.NetworkStateTracker;
Robert Greenwalt585ac0f2010-08-27 09:24:29 -070035import android.net.NetworkUtils;
Robert Greenwalt434203a2010-10-11 16:00:27 -070036import android.net.Proxy;
37import android.net.ProxyProperties;
Robert Greenwaltaa70f102011-04-28 14:28:50 -070038import android.net.RouteInfo;
Hung-ying Tyan6b818de2011-01-19 16:48:38 +080039import android.net.vpn.VpnManager;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080040import android.net.wifi.WifiStateTracker;
41import android.os.Binder;
42import android.os.Handler;
Wink Savillebb08caf2010-09-02 19:23:52 -070043import android.os.HandlerThread;
Robert Greenwalt42acef32009-08-12 16:08:25 -070044import android.os.IBinder;
Robert Greenwalt59b1a4e2011-05-10 15:05:02 -070045import android.os.INetworkManagementService;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080046import android.os.Looper;
47import android.os.Message;
Robert Greenwalt14f2ef42010-06-15 12:19:37 -070048import android.os.PowerManager;
Robert Greenwalt42acef32009-08-12 16:08:25 -070049import android.os.RemoteException;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080050import android.os.ServiceManager;
51import android.os.SystemProperties;
52import android.provider.Settings;
Robert Greenwalt42acef32009-08-12 16:08:25 -070053import android.text.TextUtils;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080054import android.util.EventLog;
Joe Onorato8a9b2202010-02-26 18:56:32 -080055import android.util.Slog;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080056
Robert Greenwalt42acef32009-08-12 16:08:25 -070057import com.android.internal.telephony.Phone;
Robert Greenwaltd0e18ff2010-01-26 11:40:34 -080058import com.android.server.connectivity.Tethering;
59
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080060import java.io.FileDescriptor;
Irfan Sheriffd649c122010-06-09 15:39:36 -070061import java.io.FileWriter;
62import java.io.IOException;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080063import java.io.PrintWriter;
Robert Greenwalt47f69fe2010-06-15 15:43:39 -070064import java.net.InetAddress;
Robert Greenwalt59b1a4e2011-05-10 15:05:02 -070065import java.net.Inet4Address;
Robert Greenwalt47f69fe2010-06-15 15:43:39 -070066import java.net.UnknownHostException;
Robert Greenwalt42acef32009-08-12 16:08:25 -070067import java.util.ArrayList;
Robert Greenwalt47f69fe2010-06-15 15:43:39 -070068import java.util.Collection;
Robert Greenwaltd825ea42010-12-29 16:15:02 -080069import java.util.concurrent.atomic.AtomicBoolean;
Robert Greenwalt4e8dfef2010-09-20 14:35:25 -070070import java.util.GregorianCalendar;
Robert Greenwalt42acef32009-08-12 16:08:25 -070071import java.util.List;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080072
73/**
74 * @hide
75 */
76public class ConnectivityService extends IConnectivityManager.Stub {
77
Robert Greenwaltba175a52010-10-05 19:12:26 -070078 private static final boolean DBG = true;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080079 private static final String TAG = "ConnectivityService";
80
Robert Greenwalt42acef32009-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 Greenwaltf43396c2011-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 Greenwaltd0e18ff2010-01-26 11:40:34 -080091 private Tethering mTethering;
Robert Greenwaltc9d5fb72010-02-25 12:29:30 -080092 private boolean mTetheringConfigValid = false;
Robert Greenwaltd0e18ff2010-01-26 11:40:34 -080093
The Android Open Source Project9066cfe2009-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 Greenwalt42acef32009-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 Sheriffa2a1b912010-06-07 09:03:04 -0700107 private WifiWatchdogService mWifiWatchdogService;
108
Robert Greenwalt42acef32009-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 Project9066cfe2009-03-03 19:31:44 -0800114 private Context mContext;
115 private int mNetworkPreference;
Robert Greenwalt42acef32009-08-12 16:08:25 -0700116 private int mActiveDefaultNetwork = -1;
Robert Greenwaltd7085fc2010-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 Project9066cfe2009-03-03 19:31:44 -0800122
123 private int mNumDnsEntries;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800124
125 private boolean mTestMode;
Joe Onorato00092872010-09-01 21:18:22 -0700126 private static ConnectivityService sServiceInstance;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800127
Robert Greenwaltd825ea42010-12-29 16:15:02 -0800128 private AtomicBoolean mBackgroundDataEnabled = new AtomicBoolean(true);
129
Robert Greenwalt59b1a4e2011-05-10 15:05:02 -0700130 private INetworkManagementService mNetd;
131
Robert Greenwalt8dcc28b2010-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 Greenwaltf3331232010-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 Greenwalt434203a2010-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 Greenwaltd55a6b42011-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 Greenwalt42acef32009-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 Lockwood0f79b542009-08-14 14:18:49 -0400217 private boolean mSystemReady;
Dianne Hackborn1c633fc2009-12-08 19:45:14 -0800218 private Intent mInitialBroadcast;
Mike Lockwood0f79b542009-08-14 14:18:49 -0400219
Robert Greenwalt14f2ef42010-06-15 12:19:37 -0700220 private PowerManager.WakeLock mNetTransitionWakeLock;
221 private String mNetTransitionWakeLockCausedBy = "";
222 private int mNetTransitionWakeLockSerialNumber;
223 private int mNetTransitionWakeLockTimeout;
224
Robert Greenwalte90aa5e2010-09-01 11:34:05 -0700225 private InetAddress mDefaultDns;
226
Robert Greenwalt4e8dfef2010-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 Greenwalt434203a2010-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 Greenwaltd55a6b42011-03-25 13:09:25 -0700239 NetworkConfig[] mNetConfigs;
Robert Greenwalt5154ae762009-10-30 14:17:42 -0700240 int mNetworksDefined;
Robert Greenwalt42acef32009-08-12 16:08:25 -0700241
Robert Greenwalt511288a2009-12-07 11:33:18 -0800242 private static class RadioAttributes {
Robert Greenwalt42acef32009-08-12 16:08:25 -0700243 public int mSimultaneity;
244 public int mType;
245 public RadioAttributes(String init) {
246 String fragments[] = init.split(",");
Robert Greenwalt5154ae762009-10-30 14:17:42 -0700247 mType = Integer.parseInt(fragments[0]);
248 mSimultaneity = Integer.parseInt(fragments[1]);
Robert Greenwalt42acef32009-08-12 16:08:25 -0700249 }
250 }
251 RadioAttributes[] mRadioAttributes;
252
Wink Savillebb08caf2010-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 Project9066cfe2009-03-03 19:31:44 -0800256 }
Wink Savillebb08caf2010-09-02 19:23:52 -0700257 return sServiceInstance;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800258 }
Robert Greenwalt86e9e552009-07-16 17:21:39 -0700259
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800260 private ConnectivityService(Context context) {
Wink Savilleed9c02b2010-12-03 12:01:38 -0800261 if (DBG) log("ConnectivityService starting up");
Robert Greenwaltde8383c2010-01-14 17:47:58 -0800262
Wink Savillebb08caf2010-09-02 19:23:52 -0700263 HandlerThread handlerThread = new HandlerThread("ConnectivityServiceThread");
264 handlerThread.start();
265 mHandler = new MyHandler(handlerThread.getLooper());
266
Robert Greenwaltd825ea42010-12-29 16:15:02 -0800267 mBackgroundDataEnabled.set(Settings.Secure.getInt(context.getContentResolver(),
268 Settings.Secure.BACKGROUND_DATA, 1) == 1);
269
Robert Greenwaltde8383c2010-01-14 17:47:58 -0800270 // setup our unique device name
Robert Greenwalt733c6292010-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 Greenwaltde8383c2010-01-14 17:47:58 -0800278 }
279
Robert Greenwalte90aa5e2010-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 Greenwalte5903732011-02-22 16:00:42 -0800288 mDefaultDns = NetworkUtils.numericToInetAddress(dns);
289 } catch (IllegalArgumentException e) {
Wink Savilleed9c02b2010-12-03 12:01:38 -0800290 loge("Error setting defaultDns using " + dns);
Robert Greenwalte90aa5e2010-09-01 11:34:05 -0700291 }
292
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800293 mContext = context;
Robert Greenwalt14f2ef42010-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 Greenwalt42acef32009-08-12 16:08:25 -0700300 mNetTrackers = new NetworkStateTracker[
301 ConnectivityManager.MAX_NETWORK_TYPE+1];
Robert Greenwalt86e9e552009-07-16 17:21:39 -0700302
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800303 mNetworkPreference = getPersistedNetworkPreference();
Robert Greenwalt86e9e552009-07-16 17:21:39 -0700304
Robert Greenwalt5154ae762009-10-30 14:17:42 -0700305 mRadioAttributes = new RadioAttributes[ConnectivityManager.MAX_RADIO_TYPE+1];
Robert Greenwaltd55a6b42011-03-25 13:09:25 -0700306 mNetConfigs = new NetworkConfig[ConnectivityManager.MAX_NETWORK_TYPE+1];
Robert Greenwalt5154ae762009-10-30 14:17:42 -0700307
Robert Greenwalt42acef32009-08-12 16:08:25 -0700308 // Load device network attributes from resources
Robert Greenwalt42acef32009-08-12 16:08:25 -0700309 String[] raStrings = context.getResources().getStringArray(
310 com.android.internal.R.array.radioAttributes);
Robert Greenwalt5154ae762009-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 Savilleed9c02b2010-12-03 12:01:38 -0800314 loge("Error in radioAttributes - ignoring attempt to define type " + r.mType);
Robert Greenwalt5154ae762009-10-30 14:17:42 -0700315 continue;
316 }
317 if (mRadioAttributes[r.mType] != null) {
Wink Savilleed9c02b2010-12-03 12:01:38 -0800318 loge("Error in radioAttributes - ignoring attempt to redefine type " +
Robert Greenwalt5154ae762009-10-30 14:17:42 -0700319 r.mType);
320 continue;
321 }
Robert Greenwalt42acef32009-08-12 16:08:25 -0700322 mRadioAttributes[r.mType] = r;
323 }
324
Robert Greenwalt5154ae762009-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 Greenwaltd55a6b42011-03-25 13:09:25 -0700329 NetworkConfig n = new NetworkConfig(naString);
Wink Saville975c8482011-04-07 14:23:45 -0700330 if (n.type > ConnectivityManager.MAX_NETWORK_TYPE) {
Wink Savilleed9c02b2010-12-03 12:01:38 -0800331 loge("Error in networkAttributes - ignoring attempt to define type " +
Wink Saville975c8482011-04-07 14:23:45 -0700332 n.type);
Robert Greenwalt5154ae762009-10-30 14:17:42 -0700333 continue;
Robert Greenwalt42acef32009-08-12 16:08:25 -0700334 }
Wink Saville975c8482011-04-07 14:23:45 -0700335 if (mNetConfigs[n.type] != null) {
Wink Savilleed9c02b2010-12-03 12:01:38 -0800336 loge("Error in networkAttributes - ignoring attempt to redefine type " +
Wink Saville975c8482011-04-07 14:23:45 -0700337 n.type);
Robert Greenwalt5154ae762009-10-30 14:17:42 -0700338 continue;
339 }
Wink Saville975c8482011-04-07 14:23:45 -0700340 if (mRadioAttributes[n.radio] == null) {
Wink Savilleed9c02b2010-12-03 12:01:38 -0800341 loge("Error in networkAttributes - ignoring attempt to use undefined " +
Wink Saville975c8482011-04-07 14:23:45 -0700342 "radio " + n.radio + " in network type " + n.type);
Robert Greenwalt5154ae762009-10-30 14:17:42 -0700343 continue;
344 }
Wink Saville975c8482011-04-07 14:23:45 -0700345 mNetConfigs[n.type] = n;
Robert Greenwalt5154ae762009-10-30 14:17:42 -0700346 mNetworksDefined++;
347 } catch(Exception e) {
348 // ignore it - leave the entry null
Robert Greenwalt42acef32009-08-12 16:08:25 -0700349 }
350 }
351
Robert Greenwalt5154ae762009-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 Greenwaltd55a6b42011-03-25 13:09:25 -0700359 for (NetworkConfig na : mNetConfigs) {
Robert Greenwalt5154ae762009-10-30 14:17:42 -0700360 if (na == null) continue;
Wink Saville975c8482011-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 Greenwalt5154ae762009-10-30 14:17:42 -0700365 }
366 continue;
367 }
Wink Saville975c8482011-04-07 14:23:45 -0700368 mPriorityList[insertionPoint--] = na.type;
Robert Greenwalt5154ae762009-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 Greenwalt42acef32009-08-12 16:08:25 -0700377 mNetRequestersPids[i] = new ArrayList();
378 }
379
380 mFeatureUsers = new ArrayList();
381
Robert Greenwalt5154ae762009-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 Project9066cfe2009-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 Greenwalt5154ae762009-10-30 14:17:42 -0700393 for (int netType : mPriorityList) {
Wink Saville975c8482011-04-07 14:23:45 -0700394 switch (mNetConfigs[netType].radio) {
Robert Greenwalt5154ae762009-10-30 14:17:42 -0700395 case ConnectivityManager.TYPE_WIFI:
Wink Savilleed9c02b2010-12-03 12:01:38 -0800396 if (DBG) log("Starting Wifi Service.");
Wink Savillec7a98342010-08-13 16:11:42 -0700397 WifiStateTracker wst = new WifiStateTracker();
Irfan Sheriff0d255342010-07-28 09:35:20 -0700398 WifiService wifiService = new WifiService(context);
Robert Greenwalt5154ae762009-10-30 14:17:42 -0700399 ServiceManager.addService(Context.WIFI_SERVICE, wifiService);
Irfan Sheriff0d255342010-07-28 09:35:20 -0700400 wifiService.checkAndStartWifi();
Robert Greenwalt5154ae762009-10-30 14:17:42 -0700401 mNetTrackers[ConnectivityManager.TYPE_WIFI] = wst;
Wink Savillec7a98342010-08-13 16:11:42 -0700402 wst.startMonitoring(context, mHandler);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800403
Irfan Sheriffa2a1b912010-06-07 09:03:04 -0700404 //TODO: as part of WWS refactor, create only when needed
Irfan Sheriff0d255342010-07-28 09:35:20 -0700405 mWifiWatchdogService = new WifiWatchdogService(context);
Irfan Sheriffa2a1b912010-06-07 09:03:04 -0700406
Robert Greenwalt5154ae762009-10-30 14:17:42 -0700407 break;
408 case ConnectivityManager.TYPE_MOBILE:
Wink Savillec7a98342010-08-13 16:11:42 -0700409 mNetTrackers[netType] = new MobileDataStateTracker(netType,
Wink Saville975c8482011-04-07 14:23:45 -0700410 mNetConfigs[netType].name);
Wink Savillec7a98342010-08-13 16:11:42 -0700411 mNetTrackers[netType].startMonitoring(context, mHandler);
Robert Greenwalt5154ae762009-10-30 14:17:42 -0700412 break;
Robert Greenwaltda3d5e62010-12-06 13:56:24 -0800413 case ConnectivityManager.TYPE_DUMMY:
414 mNetTrackers[netType] = new DummyDataStateTracker(netType,
Wink Saville975c8482011-04-07 14:23:45 -0700415 mNetConfigs[netType].name);
Robert Greenwaltda3d5e62010-12-06 13:56:24 -0800416 mNetTrackers[netType].startMonitoring(context, mHandler);
417 break;
Jaikumar Ganesh15c74392010-12-21 22:31:44 -0800418 case ConnectivityManager.TYPE_BLUETOOTH:
419 mNetTrackers[netType] = BluetoothTetheringDataTracker.getInstance();
420 mNetTrackers[netType].startMonitoring(context, mHandler);
421 break;
Benoit Goby08c39c62010-12-22 14:29:40 -0800422 case ConnectivityManager.TYPE_ETHERNET:
423 mNetTrackers[netType] = EthernetDataTracker.getInstance();
424 mNetTrackers[netType].startMonitoring(context, mHandler);
425 break;
Robert Greenwalt5154ae762009-10-30 14:17:42 -0700426 default:
Wink Savilleed9c02b2010-12-03 12:01:38 -0800427 loge("Trying to create a DataStateTracker for an unknown radio type " +
Wink Saville975c8482011-04-07 14:23:45 -0700428 mNetConfigs[netType].radio);
Robert Greenwalt5154ae762009-10-30 14:17:42 -0700429 continue;
430 }
431 }
Robert Greenwaltd0e18ff2010-01-26 11:40:34 -0800432
Robert Greenwaltdfadaea2010-03-11 15:03:08 -0800433 mTethering = new Tethering(mContext, mHandler.getLooper());
Robert Greenwaltc9d5fb72010-02-25 12:29:30 -0800434 mTetheringConfigValid = (((mNetTrackers[ConnectivityManager.TYPE_MOBILE_DUN] != null) ||
435 !mTethering.isDunRequired()) &&
436 (mTethering.getTetherableUsbRegexs().length != 0 ||
Danica Chang6fdd0c62010-08-11 14:54:43 -0700437 mTethering.getTetherableWifiRegexs().length != 0 ||
438 mTethering.getTetherableBluetoothRegexs().length != 0) &&
Robert Greenwaltc9d5fb72010-02-25 12:29:30 -0800439 mTethering.getUpstreamIfaceRegexs().length != 0);
440
Robert Greenwalt4e8dfef2010-09-20 14:35:25 -0700441 if (DBG) {
442 mInetLog = new ArrayList();
443 }
Robert Greenwalt434203a2010-10-11 16:00:27 -0700444
445 mSettingsObserver = new SettingsObserver(mHandler, EVENT_APPLY_GLOBAL_HTTP_PROXY);
446 mSettingsObserver.observe(mContext);
Robert Greenwaltb7090d62010-12-02 11:31:00 -0800447
448 loadGlobalProxy();
Hung-ying Tyan6b818de2011-01-19 16:48:38 +0800449
450 VpnManager.startVpnService(context);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800451 }
452
Robert Greenwalt5154ae762009-10-30 14:17:42 -0700453
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800454 /**
Robert Greenwalt86e9e552009-07-16 17:21:39 -0700455 * Sets the preferred network.
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800456 * @param preference the new preference
457 */
Robert Greenwalt8dcc28b2010-09-23 10:05:56 -0700458 public void setNetworkPreference(int preference) {
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800459 enforceChangePermission();
Robert Greenwalt8dcc28b2010-09-23 10:05:56 -0700460
461 mHandler.sendMessage(mHandler.obtainMessage(EVENT_SET_NETWORK_PREFERENCE, preference, 0));
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800462 }
463
464 public int getNetworkPreference() {
465 enforceAccessPermission();
Robert Greenwalt8dcc28b2010-09-23 10:05:56 -0700466 int preference;
467 synchronized(this) {
468 preference = mNetworkPreference;
469 }
470 return preference;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800471 }
472
Robert Greenwalt8dcc28b2010-09-23 10:05:56 -0700473 private void handleSetNetworkPreference(int preference) {
474 if (ConnectivityManager.isNetworkTypeValid(preference) &&
Robert Greenwaltd55a6b42011-03-25 13:09:25 -0700475 mNetConfigs[preference] != null &&
476 mNetConfigs[preference].isDefault()) {
Robert Greenwalt8dcc28b2010-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 Project9066cfe2009-03-03 19:31:44 -0800486 }
Robert Greenwalt86e9e552009-07-16 17:21:39 -0700487
The Android Open Source Project9066cfe2009-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 Greenwalt86e9e552009-07-16 17:21:39 -0700499
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800500 /**
Robert Greenwalt86e9e552009-07-16 17:21:39 -0700501 * Make the state of network connectivity conform to the preference settings
The Android Open Source Project9066cfe2009-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 Greenwalt42acef32009-08-12 16:08:25 -0700508 if (mNetTrackers[mNetworkPreference].getNetworkInfo().isConnected())
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800509 return;
510
Robert Greenwalt42acef32009-08-12 16:08:25 -0700511 if (!mNetTrackers[mNetworkPreference].isAvailable())
512 return;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800513
Robert Greenwalt42acef32009-08-12 16:08:25 -0700514 for (int t=0; t <= ConnectivityManager.MAX_RADIO_TYPE; t++) {
Robert Greenwalt5154ae762009-10-30 14:17:42 -0700515 if (t != mNetworkPreference && mNetTrackers[t] != null &&
Robert Greenwalt42acef32009-08-12 16:08:25 -0700516 mNetTrackers[t].getNetworkInfo().isConnected()) {
Robert Greenwaltec9fe462009-08-20 15:25:14 -0700517 if (DBG) {
Wink Savilleed9c02b2010-12-03 12:01:38 -0800518 log("tearing down " + mNetTrackers[t].getNetworkInfo() +
Robert Greenwaltec9fe462009-08-20 15:25:14 -0700519 " in enforcePreference");
520 }
Robert Greenwalt42acef32009-08-12 16:08:25 -0700521 teardown(mNetTrackers[t]);
The Android Open Source Project9066cfe2009-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 Greenwalt86e9e552009-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 Project9066cfe2009-03-03 19:31:44 -0800541 */
542 public NetworkInfo getActiveNetworkInfo() {
Robert Greenwalt59911582011-05-20 12:23:41 -0700543 return getNetworkInfo(mActiveDefaultNetwork);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800544 }
545
546 public NetworkInfo getNetworkInfo(int networkType) {
547 enforceAccessPermission();
548 if (ConnectivityManager.isNetworkTypeValid(networkType)) {
549 NetworkStateTracker t = mNetTrackers[networkType];
550 if (t != null)
551 return t.getNetworkInfo();
552 }
553 return null;
554 }
555
556 public NetworkInfo[] getAllNetworkInfo() {
557 enforceAccessPermission();
Robert Greenwalt5154ae762009-10-30 14:17:42 -0700558 NetworkInfo[] result = new NetworkInfo[mNetworksDefined];
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800559 int i = 0;
560 for (NetworkStateTracker t : mNetTrackers) {
Robert Greenwalt5154ae762009-10-30 14:17:42 -0700561 if(t != null) result[i++] = t.getNetworkInfo();
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800562 }
563 return result;
564 }
565
Robert Greenwaltd192dad2010-09-14 09:18:02 -0700566 /**
567 * Return LinkProperties for the active (i.e., connected) default
568 * network interface. It is assumed that at most one default network
569 * is active at a time. If more than one is active, it is indeterminate
570 * which will be returned.
571 * @return the ip properties for the active network, or {@code null} if
572 * none is active
573 */
574 public LinkProperties getActiveLinkProperties() {
Robert Greenwalt59911582011-05-20 12:23:41 -0700575 return getLinkProperties(mActiveDefaultNetwork);
Robert Greenwaltd192dad2010-09-14 09:18:02 -0700576 }
577
578 public LinkProperties getLinkProperties(int networkType) {
579 enforceAccessPermission();
580 if (ConnectivityManager.isNetworkTypeValid(networkType)) {
581 NetworkStateTracker t = mNetTrackers[networkType];
582 if (t != null) return t.getLinkProperties();
583 }
584 return null;
585 }
586
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800587 public boolean setRadios(boolean turnOn) {
588 boolean result = true;
589 enforceChangePermission();
590 for (NetworkStateTracker t : mNetTrackers) {
Robert Greenwalt5154ae762009-10-30 14:17:42 -0700591 if (t != null) result = t.setRadio(turnOn) && result;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800592 }
593 return result;
594 }
595
596 public boolean setRadio(int netType, boolean turnOn) {
597 enforceChangePermission();
598 if (!ConnectivityManager.isNetworkTypeValid(netType)) {
599 return false;
600 }
601 NetworkStateTracker tracker = mNetTrackers[netType];
602 return tracker != null && tracker.setRadio(turnOn);
603 }
604
Robert Greenwalt9c75d4a2009-09-27 17:27:04 -0700605 /**
606 * Used to notice when the calling process dies so we can self-expire
607 *
608 * Also used to know if the process has cleaned up after itself when
609 * our auto-expire timer goes off. The timer has a link to an object.
610 *
611 */
Robert Greenwalt42acef32009-08-12 16:08:25 -0700612 private class FeatureUser implements IBinder.DeathRecipient {
613 int mNetworkType;
614 String mFeature;
615 IBinder mBinder;
616 int mPid;
617 int mUid;
Robert Greenwaltb9285352009-12-21 18:24:07 -0800618 long mCreateTime;
Robert Greenwalt42acef32009-08-12 16:08:25 -0700619
620 FeatureUser(int type, String feature, IBinder binder) {
621 super();
622 mNetworkType = type;
623 mFeature = feature;
624 mBinder = binder;
625 mPid = getCallingPid();
626 mUid = getCallingUid();
Robert Greenwaltb9285352009-12-21 18:24:07 -0800627 mCreateTime = System.currentTimeMillis();
Robert Greenwalt9c75d4a2009-09-27 17:27:04 -0700628
Robert Greenwalt42acef32009-08-12 16:08:25 -0700629 try {
630 mBinder.linkToDeath(this, 0);
631 } catch (RemoteException e) {
632 binderDied();
633 }
634 }
635
636 void unlinkDeathRecipient() {
637 mBinder.unlinkToDeath(this, 0);
638 }
639
640 public void binderDied() {
Wink Savilleed9c02b2010-12-03 12:01:38 -0800641 log("ConnectivityService FeatureUser binderDied(" +
Robert Greenwaltb9285352009-12-21 18:24:07 -0800642 mNetworkType + ", " + mFeature + ", " + mBinder + "), created " +
643 (System.currentTimeMillis() - mCreateTime) + " mSec ago");
Robert Greenwalt9c75d4a2009-09-27 17:27:04 -0700644 stopUsingNetworkFeature(this, false);
Robert Greenwalt42acef32009-08-12 16:08:25 -0700645 }
646
Robert Greenwalt9c75d4a2009-09-27 17:27:04 -0700647 public void expire() {
Wink Savilleed9c02b2010-12-03 12:01:38 -0800648 log("ConnectivityService FeatureUser expire(" +
Robert Greenwaltb9285352009-12-21 18:24:07 -0800649 mNetworkType + ", " + mFeature + ", " + mBinder +"), created " +
650 (System.currentTimeMillis() - mCreateTime) + " mSec ago");
Robert Greenwalt9c75d4a2009-09-27 17:27:04 -0700651 stopUsingNetworkFeature(this, false);
652 }
Robert Greenwaltb9285352009-12-21 18:24:07 -0800653
654 public String toString() {
655 return "FeatureUser("+mNetworkType+","+mFeature+","+mPid+","+mUid+"), created " +
656 (System.currentTimeMillis() - mCreateTime) + " mSec ago";
657 }
Robert Greenwalt42acef32009-08-12 16:08:25 -0700658 }
659
Robert Greenwalt9c75d4a2009-09-27 17:27:04 -0700660 // javadoc from interface
Robert Greenwalt42acef32009-08-12 16:08:25 -0700661 public int startUsingNetworkFeature(int networkType, String feature,
662 IBinder binder) {
663 if (DBG) {
Wink Savilleed9c02b2010-12-03 12:01:38 -0800664 log("startUsingNetworkFeature for net " + networkType + ": " + feature);
Robert Greenwalt42acef32009-08-12 16:08:25 -0700665 }
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800666 enforceChangePermission();
Robert Greenwalt5154ae762009-10-30 14:17:42 -0700667 if (!ConnectivityManager.isNetworkTypeValid(networkType) ||
Robert Greenwaltd55a6b42011-03-25 13:09:25 -0700668 mNetConfigs[networkType] == null) {
Robert Greenwalt42acef32009-08-12 16:08:25 -0700669 return Phone.APN_REQUEST_FAILED;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800670 }
Robert Greenwalt42acef32009-08-12 16:08:25 -0700671
Robert Greenwalt9c75d4a2009-09-27 17:27:04 -0700672 FeatureUser f = new FeatureUser(networkType, feature, binder);
Robert Greenwalt42acef32009-08-12 16:08:25 -0700673
674 // TODO - move this into the MobileDataStateTracker
675 int usedNetworkType = networkType;
676 if(networkType == ConnectivityManager.TYPE_MOBILE) {
Wink Saville9d7d6282011-03-12 14:52:01 -0800677 usedNetworkType = convertFeatureToNetworkType(feature);
678 if (usedNetworkType < 0) {
679 Slog.e(TAG, "Can't match any netTracker!");
680 usedNetworkType = networkType;
Robert Greenwalt42acef32009-08-12 16:08:25 -0700681 }
682 }
683 NetworkStateTracker network = mNetTrackers[usedNetworkType];
684 if (network != null) {
Robert Greenwalt0be1e982010-12-15 13:26:33 -0800685 Integer currentPid = new Integer(getCallingPid());
Robert Greenwalt42acef32009-08-12 16:08:25 -0700686 if (usedNetworkType != networkType) {
Robert Greenwalt42acef32009-08-12 16:08:25 -0700687 NetworkStateTracker radio = mNetTrackers[networkType];
688 NetworkInfo ni = network.getNetworkInfo();
689
690 if (ni.isAvailable() == false) {
Wink Savilleed9c02b2010-12-03 12:01:38 -0800691 if (DBG) log("special network not available");
Robert Greenwalte32e8122010-12-29 14:35:21 -0800692 if (!TextUtils.equals(feature,Phone.FEATURE_ENABLE_DUN_ALWAYS)) {
693 return Phone.APN_TYPE_NOT_AVAILABLE;
694 } else {
695 // else make the attempt anyway - probably giving REQUEST_STARTED below
696 }
Robert Greenwalt42acef32009-08-12 16:08:25 -0700697 }
698
Robert Greenwalt9c75d4a2009-09-27 17:27:04 -0700699 synchronized(this) {
700 mFeatureUsers.add(f);
701 if (!mNetRequestersPids[usedNetworkType].contains(currentPid)) {
702 // this gets used for per-pid dns when connected
703 mNetRequestersPids[usedNetworkType].add(currentPid);
704 }
Robert Greenwalt42acef32009-08-12 16:08:25 -0700705 }
Robert Greenwalt9c75d4a2009-09-27 17:27:04 -0700706
Robert Greenwaltf2102f72011-05-03 19:02:44 -0700707 int restoreTimer = getRestoreDefaultNetworkDelay(usedNetworkType);
708
709 if (restoreTimer >= 0) {
710 mHandler.sendMessageDelayed(
711 mHandler.obtainMessage(EVENT_RESTORE_DEFAULT_NETWORK, f), restoreTimer);
712 }
Robert Greenwalt42acef32009-08-12 16:08:25 -0700713
Robert Greenwalta64bf832009-08-19 20:19:33 -0700714 if ((ni.isConnectedOrConnecting() == true) &&
715 !network.isTeardownRequested()) {
Robert Greenwalt42acef32009-08-12 16:08:25 -0700716 if (ni.isConnected() == true) {
717 // add the pid-specific dns
Robert Greenwaltc76b8fa2010-07-23 15:46:26 -0700718 handleDnsConfigurationChange(networkType);
Wink Savilleed9c02b2010-12-03 12:01:38 -0800719 if (DBG) log("special network already active");
Robert Greenwalt42acef32009-08-12 16:08:25 -0700720 return Phone.APN_ALREADY_ACTIVE;
721 }
Wink Savilleed9c02b2010-12-03 12:01:38 -0800722 if (DBG) log("special network already connecting");
Robert Greenwalt42acef32009-08-12 16:08:25 -0700723 return Phone.APN_REQUEST_STARTED;
724 }
725
726 // check if the radio in play can make another contact
727 // assume if cannot for now
728
Wink Savilleed9c02b2010-12-03 12:01:38 -0800729 if (DBG) log("reconnecting to special network");
Robert Greenwalt42acef32009-08-12 16:08:25 -0700730 network.reconnect();
731 return Phone.APN_REQUEST_STARTED;
732 } else {
Robert Greenwalt0be1e982010-12-15 13:26:33 -0800733 // need to remember this unsupported request so we respond appropriately on stop
734 synchronized(this) {
735 mFeatureUsers.add(f);
736 if (!mNetRequestersPids[usedNetworkType].contains(currentPid)) {
737 // this gets used for per-pid dns when connected
738 mNetRequestersPids[usedNetworkType].add(currentPid);
739 }
740 }
Robert Greenwalt02648a42010-05-18 10:52:51 -0700741 return -1;
Robert Greenwalt42acef32009-08-12 16:08:25 -0700742 }
743 }
744 return Phone.APN_TYPE_NOT_AVAILABLE;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800745 }
746
Robert Greenwalt9c75d4a2009-09-27 17:27:04 -0700747 // javadoc from interface
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800748 public int stopUsingNetworkFeature(int networkType, String feature) {
Robert Greenwaltb8f16342009-10-06 17:52:40 -0700749 enforceChangePermission();
750
Robert Greenwalt9c75d4a2009-09-27 17:27:04 -0700751 int pid = getCallingPid();
752 int uid = getCallingUid();
753
754 FeatureUser u = null;
755 boolean found = false;
756
757 synchronized(this) {
758 for (int i = 0; i < mFeatureUsers.size() ; i++) {
759 u = (FeatureUser)mFeatureUsers.get(i);
760 if (uid == u.mUid && pid == u.mPid &&
761 networkType == u.mNetworkType &&
762 TextUtils.equals(feature, u.mFeature)) {
763 found = true;
764 break;
765 }
766 }
767 }
768 if (found && u != null) {
769 // stop regardless of how many other time this proc had called start
770 return stopUsingNetworkFeature(u, true);
771 } else {
772 // none found!
Wink Savilleed9c02b2010-12-03 12:01:38 -0800773 if (DBG) log("ignoring stopUsingNetworkFeature - not a live request");
Robert Greenwalt9c75d4a2009-09-27 17:27:04 -0700774 return 1;
775 }
Robert Greenwalt42acef32009-08-12 16:08:25 -0700776 }
777
Robert Greenwalt9c75d4a2009-09-27 17:27:04 -0700778 private int stopUsingNetworkFeature(FeatureUser u, boolean ignoreDups) {
779 int networkType = u.mNetworkType;
780 String feature = u.mFeature;
781 int pid = u.mPid;
782 int uid = u.mUid;
783
784 NetworkStateTracker tracker = null;
785 boolean callTeardown = false; // used to carry our decision outside of sync block
786
Robert Greenwalt42acef32009-08-12 16:08:25 -0700787 if (DBG) {
Wink Savilleed9c02b2010-12-03 12:01:38 -0800788 log("stopUsingNetworkFeature for net " + networkType +
Robert Greenwalt42acef32009-08-12 16:08:25 -0700789 ": " + feature);
790 }
Robert Greenwaltb8f16342009-10-06 17:52:40 -0700791
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800792 if (!ConnectivityManager.isNetworkTypeValid(networkType)) {
793 return -1;
794 }
Robert Greenwalt42acef32009-08-12 16:08:25 -0700795
Robert Greenwalt9c75d4a2009-09-27 17:27:04 -0700796 // need to link the mFeatureUsers list with the mNetRequestersPids state in this
797 // sync block
798 synchronized(this) {
799 // check if this process still has an outstanding start request
800 if (!mFeatureUsers.contains(u)) {
Wink Savilleed9c02b2010-12-03 12:01:38 -0800801 if (DBG) log("ignoring - this process has no outstanding requests");
Robert Greenwalt42acef32009-08-12 16:08:25 -0700802 return 1;
803 }
Robert Greenwalt9c75d4a2009-09-27 17:27:04 -0700804 u.unlinkDeathRecipient();
805 mFeatureUsers.remove(mFeatureUsers.indexOf(u));
806 // If we care about duplicate requests, check for that here.
807 //
808 // This is done to support the extension of a request - the app
809 // can request we start the network feature again and renew the
810 // auto-shutoff delay. Normal "stop" calls from the app though
811 // do not pay attention to duplicate requests - in effect the
812 // API does not refcount and a single stop will counter multiple starts.
813 if (ignoreDups == false) {
814 for (int i = 0; i < mFeatureUsers.size() ; i++) {
815 FeatureUser x = (FeatureUser)mFeatureUsers.get(i);
816 if (x.mUid == u.mUid && x.mPid == u.mPid &&
817 x.mNetworkType == u.mNetworkType &&
818 TextUtils.equals(x.mFeature, u.mFeature)) {
Wink Savilleed9c02b2010-12-03 12:01:38 -0800819 if (DBG) log("ignoring stopUsingNetworkFeature as dup is found");
Robert Greenwalt9c75d4a2009-09-27 17:27:04 -0700820 return 1;
821 }
822 }
Robert Greenwalt42acef32009-08-12 16:08:25 -0700823 }
Robert Greenwalt9c75d4a2009-09-27 17:27:04 -0700824
825 // TODO - move to MobileDataStateTracker
826 int usedNetworkType = networkType;
827 if (networkType == ConnectivityManager.TYPE_MOBILE) {
Wink Saville9d7d6282011-03-12 14:52:01 -0800828 usedNetworkType = convertFeatureToNetworkType(feature);
829 if (usedNetworkType < 0) {
830 usedNetworkType = networkType;
Robert Greenwalt9c75d4a2009-09-27 17:27:04 -0700831 }
832 }
833 tracker = mNetTrackers[usedNetworkType];
Robert Greenwalt5154ae762009-10-30 14:17:42 -0700834 if (tracker == null) {
Wink Savilleed9c02b2010-12-03 12:01:38 -0800835 if (DBG) log("ignoring - no known tracker for net type " + usedNetworkType);
Robert Greenwalt5154ae762009-10-30 14:17:42 -0700836 return -1;
837 }
838 if (usedNetworkType != networkType) {
Robert Greenwalt9c75d4a2009-09-27 17:27:04 -0700839 Integer currentPid = new Integer(pid);
Robert Greenwalt9c75d4a2009-09-27 17:27:04 -0700840 mNetRequestersPids[usedNetworkType].remove(currentPid);
Robert Greenwalt421c72b2009-12-17 14:54:59 -0800841 reassessPidDns(pid, true);
Robert Greenwalt9c75d4a2009-09-27 17:27:04 -0700842 if (mNetRequestersPids[usedNetworkType].size() != 0) {
Wink Savilleed9c02b2010-12-03 12:01:38 -0800843 if (DBG) log("not tearing down special network - " +
Robert Greenwalt9c75d4a2009-09-27 17:27:04 -0700844 "others still using it");
845 return 1;
846 }
847 callTeardown = true;
Robert Greenwalt19b9ab42011-01-10 11:58:31 -0800848 } else {
849 if (DBG) log("not a known feature - dropping");
Robert Greenwalt9c75d4a2009-09-27 17:27:04 -0700850 }
851 }
Wink Savilleed9c02b2010-12-03 12:01:38 -0800852 if (DBG) log("Doing network teardown");
Robert Greenwalt9c75d4a2009-09-27 17:27:04 -0700853 if (callTeardown) {
854 tracker.teardown();
Robert Greenwalt42acef32009-08-12 16:08:25 -0700855 return 1;
856 } else {
Robert Greenwalt02648a42010-05-18 10:52:51 -0700857 return -1;
Robert Greenwalt42acef32009-08-12 16:08:25 -0700858 }
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800859 }
860
861 /**
Robert Greenwalt585ac0f2010-08-27 09:24:29 -0700862 * @deprecated use requestRouteToHostAddress instead
863 *
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800864 * Ensure that a network route exists to deliver traffic to the specified
865 * host via the specified network interface.
Robert Greenwalt86e9e552009-07-16 17:21:39 -0700866 * @param networkType the type of the network over which traffic to the
867 * specified host is to be routed
868 * @param hostAddress the IP address of the host to which the route is
869 * desired
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800870 * @return {@code true} on success, {@code false} on failure
871 */
872 public boolean requestRouteToHost(int networkType, int hostAddress) {
Robert Greenwalt585ac0f2010-08-27 09:24:29 -0700873 InetAddress inetAddress = NetworkUtils.intToInetAddress(hostAddress);
874
875 if (inetAddress == null) {
876 return false;
877 }
878
879 return requestRouteToHostAddress(networkType, inetAddress.getAddress());
880 }
881
882 /**
883 * Ensure that a network route exists to deliver traffic to the specified
884 * host via the specified network interface.
885 * @param networkType the type of the network over which traffic to the
886 * specified host is to be routed
887 * @param hostAddress the IP address of the host to which the route is
888 * desired
889 * @return {@code true} on success, {@code false} on failure
890 */
891 public boolean requestRouteToHostAddress(int networkType, byte[] hostAddress) {
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800892 enforceChangePermission();
893 if (!ConnectivityManager.isNetworkTypeValid(networkType)) {
894 return false;
895 }
896 NetworkStateTracker tracker = mNetTrackers[networkType];
Robert Greenwalt8206ff32009-09-10 15:06:20 -0700897
Robert Greenwalt5154ae762009-10-30 14:17:42 -0700898 if (tracker == null || !tracker.getNetworkInfo().isConnected() ||
899 tracker.isTeardownRequested()) {
Robert Greenwalt8206ff32009-09-10 15:06:20 -0700900 if (DBG) {
Wink Savilleed9c02b2010-12-03 12:01:38 -0800901 log("requestRouteToHostAddress on down network " +
Robert Greenwalt585ac0f2010-08-27 09:24:29 -0700902 "(" + networkType + ") - dropped");
Robert Greenwalt8206ff32009-09-10 15:06:20 -0700903 }
904 return false;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800905 }
Robert Greenwalt47f69fe2010-06-15 15:43:39 -0700906 try {
Robert Greenwalt585ac0f2010-08-27 09:24:29 -0700907 InetAddress addr = InetAddress.getByAddress(hostAddress);
Robert Greenwaltf43396c2011-05-06 17:10:53 -0700908 return addHostRoute(tracker, addr, 0);
Robert Greenwalt47f69fe2010-06-15 15:43:39 -0700909 } catch (UnknownHostException e) {}
910 return false;
Irfan Sheriffd649c122010-06-09 15:39:36 -0700911 }
912
913 /**
914 * Ensure that a network route exists to deliver traffic to the specified
915 * host via the mobile data network.
916 * @param hostAddress the IP address of the host to which the route is desired,
917 * in network byte order.
Robert Greenwalt47f69fe2010-06-15 15:43:39 -0700918 * TODO - deprecate
Irfan Sheriffd649c122010-06-09 15:39:36 -0700919 * @return {@code true} on success, {@code false} on failure
920 */
Robert Greenwaltf43396c2011-05-06 17:10:53 -0700921 private boolean addHostRoute(NetworkStateTracker nt, InetAddress hostAddress, int cycleCount) {
Robert Greenwaltf43396c2011-05-06 17:10:53 -0700922 LinkProperties lp = nt.getLinkProperties();
923 if ((lp == null) || (hostAddress == null)) return false;
Irfan Sheriffd649c122010-06-09 15:39:36 -0700924
Robert Greenwaltf43396c2011-05-06 17:10:53 -0700925 String interfaceName = lp.getInterfaceName();
Irfan Sheriffd649c122010-06-09 15:39:36 -0700926 if (DBG) {
Robert Greenwaltf43396c2011-05-06 17:10:53 -0700927 log("Requested host route to " + hostAddress + "(" + interfaceName + "), cycleCount=" +
928 cycleCount);
Irfan Sheriffd649c122010-06-09 15:39:36 -0700929 }
Robert Greenwaltf43396c2011-05-06 17:10:53 -0700930 if (interfaceName == null) {
Wink Savilleed9c02b2010-12-03 12:01:38 -0800931 if (DBG) loge("addHostRoute failed due to null interface name");
Irfan Sheriffd649c122010-06-09 15:39:36 -0700932 return false;
933 }
Robert Greenwaltf43396c2011-05-06 17:10:53 -0700934
935 RouteInfo bestRoute = RouteInfo.selectBestRoute(lp.getRoutes(), hostAddress);
Robert Greenwalt59b1a4e2011-05-10 15:05:02 -0700936 InetAddress gatewayAddress = null;
Robert Greenwaltf43396c2011-05-06 17:10:53 -0700937 if (bestRoute != null) {
Robert Greenwalt59b1a4e2011-05-10 15:05:02 -0700938 gatewayAddress = bestRoute.getGateway();
Robert Greenwaltf43396c2011-05-06 17:10:53 -0700939 // if the best route is ourself, don't relf-reference, just add the host route
Robert Greenwalt59b1a4e2011-05-10 15:05:02 -0700940 if (hostAddress.equals(gatewayAddress)) gatewayAddress = null;
Robert Greenwaltf43396c2011-05-06 17:10:53 -0700941 }
Robert Greenwalt59b1a4e2011-05-10 15:05:02 -0700942 if (gatewayAddress != null) {
Robert Greenwaltf43396c2011-05-06 17:10:53 -0700943 if (cycleCount > MAX_HOSTROUTE_CYCLE_COUNT) {
944 loge("Error adding hostroute - too much recursion");
945 return false;
946 }
Robert Greenwalt59b1a4e2011-05-10 15:05:02 -0700947 if (!addHostRoute(nt, gatewayAddress, cycleCount+1)) return false;
Robert Greenwaltf43396c2011-05-06 17:10:53 -0700948 }
Robert Greenwalt59b1a4e2011-05-10 15:05:02 -0700949
950 RouteInfo route = RouteInfo.makeHostRoute(hostAddress, gatewayAddress);
951
952 try {
953 mNetd.addRoute(interfaceName, route);
954 return true;
955 } catch (Exception ex) {
956 return false;
957 }
Robert Greenwaltf43396c2011-05-06 17:10:53 -0700958 }
959
960 // TODO support the removal of single host routes. Keep a ref count of them so we
961 // aren't over-zealous
962 private boolean removeHostRoute(NetworkStateTracker nt, InetAddress hostAddress) {
963 return false;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800964 }
965
966 /**
967 * @see ConnectivityManager#getBackgroundDataSetting()
968 */
969 public boolean getBackgroundDataSetting() {
Robert Greenwaltd825ea42010-12-29 16:15:02 -0800970 return mBackgroundDataEnabled.get();
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800971 }
Robert Greenwalt86e9e552009-07-16 17:21:39 -0700972
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800973 /**
974 * @see ConnectivityManager#setBackgroundDataSetting(boolean)
975 */
976 public void setBackgroundDataSetting(boolean allowBackgroundDataUsage) {
977 mContext.enforceCallingOrSelfPermission(
978 android.Manifest.permission.CHANGE_BACKGROUND_DATA_SETTING,
979 "ConnectivityService");
Robert Greenwalt86e9e552009-07-16 17:21:39 -0700980
Robert Greenwaltd825ea42010-12-29 16:15:02 -0800981 mBackgroundDataEnabled.set(allowBackgroundDataUsage);
982
Robert Greenwalt8dcc28b2010-09-23 10:05:56 -0700983 mHandler.sendMessage(mHandler.obtainMessage(EVENT_SET_BACKGROUND_DATA,
984 (allowBackgroundDataUsage ? ENABLED : DISABLED), 0));
985 }
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800986
Robert Greenwalt8dcc28b2010-09-23 10:05:56 -0700987 private void handleSetBackgroundData(boolean enabled) {
Robert Greenwaltdb4afae2011-02-25 13:44:09 -0800988 Settings.Secure.putInt(mContext.getContentResolver(),
989 Settings.Secure.BACKGROUND_DATA, enabled ? 1 : 0);
990 Intent broadcast = new Intent(
991 ConnectivityManager.ACTION_BACKGROUND_DATA_SETTING_CHANGED);
992 mContext.sendBroadcast(broadcast);
Robert Greenwalt86e9e552009-07-16 17:21:39 -0700993 }
994
Robert Greenwaltc03fa502010-02-23 18:58:05 -0800995 /**
996 * @see ConnectivityManager#getMobileDataEnabled()
997 */
998 public boolean getMobileDataEnabled() {
Wink Savillee7982682010-12-07 10:31:02 -0800999 // TODO: This detail should probably be in DataConnectionTracker's
1000 // which is where we store the value and maybe make this
1001 // asynchronous.
Robert Greenwaltc03fa502010-02-23 18:58:05 -08001002 enforceAccessPermission();
1003 boolean retVal = Settings.Secure.getInt(mContext.getContentResolver(),
1004 Settings.Secure.MOBILE_DATA, 1) == 1;
Wink Savilleed9c02b2010-12-03 12:01:38 -08001005 if (DBG) log("getMobileDataEnabled returning " + retVal);
Robert Greenwaltc03fa502010-02-23 18:58:05 -08001006 return retVal;
1007 }
1008
Robert Greenwaltd55a6b42011-03-25 13:09:25 -07001009 public void setDataDependency(int networkType, boolean met) {
1010 enforceChangePermission();
1011 if (DBG) {
1012 log("setDataDependency(" + networkType + ", " + met + ")");
1013 }
1014 mHandler.sendMessage(mHandler.obtainMessage(EVENT_SET_DEPENDENCY_MET,
1015 (met ? ENABLED : DISABLED), networkType));
1016 }
1017
1018 private void handleSetDependencyMet(int networkType, boolean met) {
1019 if (mNetTrackers[networkType] != null) {
1020 if (DBG) {
1021 log("handleSetDependencyMet(" + networkType + ", " + met + ")");
1022 }
1023 mNetTrackers[networkType].setDependencyMet(met);
1024 }
1025 }
1026
Robert Greenwaltc03fa502010-02-23 18:58:05 -08001027 /**
1028 * @see ConnectivityManager#setMobileDataEnabled(boolean)
1029 */
Robert Greenwalt8dcc28b2010-09-23 10:05:56 -07001030 public void setMobileDataEnabled(boolean enabled) {
Robert Greenwaltc03fa502010-02-23 18:58:05 -08001031 enforceChangePermission();
Wink Savilleed9c02b2010-12-03 12:01:38 -08001032 if (DBG) log("setMobileDataEnabled(" + enabled + ")");
Robert Greenwaltc03fa502010-02-23 18:58:05 -08001033
Robert Greenwalt8dcc28b2010-09-23 10:05:56 -07001034 mHandler.sendMessage(mHandler.obtainMessage(EVENT_SET_MOBILE_DATA,
Robert Greenwaltd55a6b42011-03-25 13:09:25 -07001035 (enabled ? ENABLED : DISABLED), 0));
Robert Greenwalt8dcc28b2010-09-23 10:05:56 -07001036 }
1037
1038 private void handleSetMobileData(boolean enabled) {
Wink Savillee7982682010-12-07 10:31:02 -08001039 if (mNetTrackers[ConnectivityManager.TYPE_MOBILE] != null) {
1040 if (DBG) {
1041 Slog.d(TAG, mNetTrackers[ConnectivityManager.TYPE_MOBILE].toString() + enabled);
Robert Greenwaltc03fa502010-02-23 18:58:05 -08001042 }
Wink Savillee7982682010-12-07 10:31:02 -08001043 mNetTrackers[ConnectivityManager.TYPE_MOBILE].setDataEnable(enabled);
Robert Greenwaltc03fa502010-02-23 18:58:05 -08001044 }
1045 }
1046
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001047 private void enforceAccessPermission() {
Robert Greenwalt86e9e552009-07-16 17:21:39 -07001048 mContext.enforceCallingOrSelfPermission(
1049 android.Manifest.permission.ACCESS_NETWORK_STATE,
1050 "ConnectivityService");
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001051 }
1052
1053 private void enforceChangePermission() {
Robert Greenwalt86e9e552009-07-16 17:21:39 -07001054 mContext.enforceCallingOrSelfPermission(
1055 android.Manifest.permission.CHANGE_NETWORK_STATE,
1056 "ConnectivityService");
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001057 }
1058
Robert Greenwaltd0e18ff2010-01-26 11:40:34 -08001059 // TODO Make this a special check when it goes public
1060 private void enforceTetherChangePermission() {
1061 mContext.enforceCallingOrSelfPermission(
1062 android.Manifest.permission.CHANGE_NETWORK_STATE,
1063 "ConnectivityService");
1064 }
1065
Robert Greenwalt2a091d72010-02-11 18:18:40 -08001066 private void enforceTetherAccessPermission() {
1067 mContext.enforceCallingOrSelfPermission(
1068 android.Manifest.permission.ACCESS_NETWORK_STATE,
1069 "ConnectivityService");
1070 }
1071
Robert Greenwalt14f2ef42010-06-15 12:19:37 -07001072 private void enforceConnectivityInternalPermission() {
1073 mContext.enforceCallingOrSelfPermission(
1074 android.Manifest.permission.CONNECTIVITY_INTERNAL,
1075 "ConnectivityService");
1076 }
1077
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001078 /**
Robert Greenwalt86e9e552009-07-16 17:21:39 -07001079 * Handle a {@code DISCONNECTED} event. If this pertains to the non-active
1080 * network, we ignore it. If it is for the active network, we send out a
1081 * broadcast. But first, we check whether it might be possible to connect
1082 * to a different network.
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001083 * @param info the {@code NetworkInfo} for the network
1084 */
1085 private void handleDisconnect(NetworkInfo info) {
1086
Robert Greenwalt42acef32009-08-12 16:08:25 -07001087 int prevNetType = info.getType();
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001088
Robert Greenwalt42acef32009-08-12 16:08:25 -07001089 mNetTrackers[prevNetType].setTeardownRequested(false);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001090 /*
1091 * If the disconnected network is not the active one, then don't report
1092 * this as a loss of connectivity. What probably happened is that we're
1093 * getting the disconnect for a network that we explicitly disabled
1094 * in accordance with network preference policies.
1095 */
Robert Greenwaltd55a6b42011-03-25 13:09:25 -07001096 if (!mNetConfigs[prevNetType].isDefault()) {
Robert Greenwalt42acef32009-08-12 16:08:25 -07001097 List pids = mNetRequestersPids[prevNetType];
1098 for (int i = 0; i<pids.size(); i++) {
1099 Integer pid = (Integer)pids.get(i);
1100 // will remove them because the net's no longer connected
1101 // need to do this now as only now do we know the pids and
1102 // can properly null things that are no longer referenced.
1103 reassessPidDns(pid.intValue(), false);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001104 }
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001105 }
1106
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001107 Intent intent = new Intent(ConnectivityManager.CONNECTIVITY_ACTION);
1108 intent.putExtra(ConnectivityManager.EXTRA_NETWORK_INFO, info);
1109 if (info.isFailover()) {
1110 intent.putExtra(ConnectivityManager.EXTRA_IS_FAILOVER, true);
1111 info.setFailover(false);
1112 }
1113 if (info.getReason() != null) {
1114 intent.putExtra(ConnectivityManager.EXTRA_REASON, info.getReason());
1115 }
1116 if (info.getExtraInfo() != null) {
Robert Greenwalt86e9e552009-07-16 17:21:39 -07001117 intent.putExtra(ConnectivityManager.EXTRA_EXTRA_INFO,
1118 info.getExtraInfo());
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001119 }
Robert Greenwalt42acef32009-08-12 16:08:25 -07001120
Robert Greenwaltd55a6b42011-03-25 13:09:25 -07001121 if (mNetConfigs[prevNetType].isDefault()) {
Robert Greenwaltf21ef7d2011-01-11 13:56:33 -08001122 tryFailover(prevNetType);
1123 if (mActiveDefaultNetwork != -1) {
1124 NetworkInfo switchTo = mNetTrackers[mActiveDefaultNetwork].getNetworkInfo();
Robert Greenwaltcc4b4012010-01-25 17:54:29 -08001125 intent.putExtra(ConnectivityManager.EXTRA_OTHER_NETWORK_INFO, switchTo);
1126 } else {
Robert Greenwalt029be812010-09-20 18:01:43 -07001127 mDefaultInetConditionPublished = 0; // we're not connected anymore
Robert Greenwaltcc4b4012010-01-25 17:54:29 -08001128 intent.putExtra(ConnectivityManager.EXTRA_NO_CONNECTIVITY, true);
1129 }
Robert Greenwaltda03c4e2010-01-20 19:29:41 -08001130 }
Robert Greenwalt029be812010-09-20 18:01:43 -07001131 intent.putExtra(ConnectivityManager.EXTRA_INET_CONDITION, mDefaultInetConditionPublished);
Robert Greenwaltec896c62011-06-15 12:22:07 -07001132
1133 // Reset interface if no other connections are using the same interface
1134 boolean doReset = true;
1135 LinkProperties linkProperties = mNetTrackers[prevNetType].getLinkProperties();
1136 if (linkProperties != null) {
1137 String oldIface = linkProperties.getInterfaceName();
1138 if (TextUtils.isEmpty(oldIface) == false) {
1139 for (NetworkStateTracker networkStateTracker : mNetTrackers) {
1140 if (networkStateTracker == null) continue;
1141 NetworkInfo networkInfo = networkStateTracker.getNetworkInfo();
1142 if (networkInfo.isConnected() && networkInfo.getType() != prevNetType) {
1143 LinkProperties l = networkStateTracker.getLinkProperties();
1144 if (l == null) continue;
1145 if (oldIface.equals(l.getInterfaceName())) {
1146 doReset = false;
1147 break;
1148 }
1149 }
1150 }
1151 }
1152 }
1153
Robert Greenwaltda03c4e2010-01-20 19:29:41 -08001154 // do this before we broadcast the change
Robert Greenwaltec896c62011-06-15 12:22:07 -07001155 handleConnectivityChange(prevNetType, doReset);
Robert Greenwaltda03c4e2010-01-20 19:29:41 -08001156
1157 sendStickyBroadcast(intent);
1158 /*
1159 * If the failover network is already connected, then immediately send
1160 * out a followup broadcast indicating successful failover
1161 */
Robert Greenwaltf21ef7d2011-01-11 13:56:33 -08001162 if (mActiveDefaultNetwork != -1) {
1163 sendConnectedBroadcast(mNetTrackers[mActiveDefaultNetwork].getNetworkInfo());
Robert Greenwaltda03c4e2010-01-20 19:29:41 -08001164 }
1165 }
1166
Robert Greenwaltf21ef7d2011-01-11 13:56:33 -08001167 private void tryFailover(int prevNetType) {
Robert Greenwalt42acef32009-08-12 16:08:25 -07001168 /*
Robert Greenwaltbff90182011-01-06 15:41:07 -08001169 * If this is a default network, check if other defaults are available.
1170 * Try to reconnect on all available and let them hash it out when
1171 * more than one connects.
Robert Greenwalt42acef32009-08-12 16:08:25 -07001172 */
Robert Greenwaltd55a6b42011-03-25 13:09:25 -07001173 if (mNetConfigs[prevNetType].isDefault()) {
Robert Greenwalt42acef32009-08-12 16:08:25 -07001174 if (mActiveDefaultNetwork == prevNetType) {
1175 mActiveDefaultNetwork = -1;
1176 }
1177
Robert Greenwaltf21ef7d2011-01-11 13:56:33 -08001178 // don't signal a reconnect for anything lower or equal priority than our
1179 // current connected default
1180 // TODO - don't filter by priority now - nice optimization but risky
1181// int currentPriority = -1;
1182// if (mActiveDefaultNetwork != -1) {
Robert Greenwaltd55a6b42011-03-25 13:09:25 -07001183// currentPriority = mNetConfigs[mActiveDefaultNetwork].mPriority;
Robert Greenwaltf21ef7d2011-01-11 13:56:33 -08001184// }
Robert Greenwaltda03c4e2010-01-20 19:29:41 -08001185 for (int checkType=0; checkType <= ConnectivityManager.MAX_NETWORK_TYPE; checkType++) {
Robert Greenwalt5154ae762009-10-30 14:17:42 -07001186 if (checkType == prevNetType) continue;
Robert Greenwaltd55a6b42011-03-25 13:09:25 -07001187 if (mNetConfigs[checkType] == null) continue;
1188 if (!mNetConfigs[checkType].isDefault()) continue;
Wink Saville9f7a0b22011-01-26 15:43:49 -08001189
1190// Enabling the isAvailable() optimization caused mobile to not get
1191// selected if it was in the middle of error handling. Specifically
1192// a moble connection that took 30 seconds to complete the DEACTIVATE_DATA_CALL
1193// would not be available and we wouldn't get connected to anything.
1194// So removing the isAvailable() optimization below for now. TODO: This
1195// optimization should work and we need to investigate why it doesn't work.
1196// This could be related to how DEACTIVATE_DATA_CALL is reporting its
1197// complete before it is really complete.
1198// if (!mNetTrackers[checkType].isAvailable()) continue;
1199
Robert Greenwaltd55a6b42011-03-25 13:09:25 -07001200// if (currentPriority >= mNetConfigs[checkType].mPriority) continue;
Robert Greenwalt42acef32009-08-12 16:08:25 -07001201
Robert Greenwaltf21ef7d2011-01-11 13:56:33 -08001202 NetworkStateTracker checkTracker = mNetTrackers[checkType];
1203 NetworkInfo checkInfo = checkTracker.getNetworkInfo();
1204 if (!checkInfo.isConnectedOrConnecting() || checkTracker.isTeardownRequested()) {
1205 checkInfo.setFailover(true);
1206 checkTracker.reconnect();
Robert Greenwalt42acef32009-08-12 16:08:25 -07001207 }
Robert Greenwaltf21ef7d2011-01-11 13:56:33 -08001208 if (DBG) log("Attempting to switch to " + checkInfo.getTypeName());
Robert Greenwalt42acef32009-08-12 16:08:25 -07001209 }
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001210 }
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001211 }
1212
1213 private void sendConnectedBroadcast(NetworkInfo info) {
Robert Greenwalt1e9aac22010-09-15 17:36:33 -07001214 sendGeneralBroadcast(info, ConnectivityManager.CONNECTIVITY_ACTION);
1215 }
1216
1217 private void sendInetConditionBroadcast(NetworkInfo info) {
1218 sendGeneralBroadcast(info, ConnectivityManager.INET_CONDITION_ACTION);
1219 }
1220
1221 private void sendGeneralBroadcast(NetworkInfo info, String bcastType) {
1222 Intent intent = new Intent(bcastType);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001223 intent.putExtra(ConnectivityManager.EXTRA_NETWORK_INFO, info);
1224 if (info.isFailover()) {
1225 intent.putExtra(ConnectivityManager.EXTRA_IS_FAILOVER, true);
1226 info.setFailover(false);
1227 }
1228 if (info.getReason() != null) {
1229 intent.putExtra(ConnectivityManager.EXTRA_REASON, info.getReason());
1230 }
1231 if (info.getExtraInfo() != null) {
Robert Greenwalt86e9e552009-07-16 17:21:39 -07001232 intent.putExtra(ConnectivityManager.EXTRA_EXTRA_INFO,
1233 info.getExtraInfo());
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001234 }
Robert Greenwaltd7085fc2010-09-08 15:24:47 -07001235 intent.putExtra(ConnectivityManager.EXTRA_INET_CONDITION, mDefaultInetConditionPublished);
Mike Lockwood0f79b542009-08-14 14:18:49 -04001236 sendStickyBroadcast(intent);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001237 }
1238
1239 /**
1240 * Called when an attempt to fail over to another network has failed.
1241 * @param info the {@link NetworkInfo} for the failed network
1242 */
1243 private void handleConnectionFailure(NetworkInfo info) {
1244 mNetTrackers[info.getType()].setTeardownRequested(false);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001245
Robert Greenwalt42acef32009-08-12 16:08:25 -07001246 String reason = info.getReason();
1247 String extraInfo = info.getExtraInfo();
Robert Greenwalt86e9e552009-07-16 17:21:39 -07001248
Robert Greenwalt572172b2010-10-08 16:35:52 -07001249 String reasonText;
1250 if (reason == null) {
1251 reasonText = ".";
1252 } else {
1253 reasonText = " (" + reason + ").";
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001254 }
Wink Savilleed9c02b2010-12-03 12:01:38 -08001255 loge("Attempt to connect to " + info.getTypeName() + " failed" + reasonText);
Robert Greenwalt42acef32009-08-12 16:08:25 -07001256
1257 Intent intent = new Intent(ConnectivityManager.CONNECTIVITY_ACTION);
1258 intent.putExtra(ConnectivityManager.EXTRA_NETWORK_INFO, info);
1259 if (getActiveNetworkInfo() == null) {
1260 intent.putExtra(ConnectivityManager.EXTRA_NO_CONNECTIVITY, true);
1261 }
1262 if (reason != null) {
1263 intent.putExtra(ConnectivityManager.EXTRA_REASON, reason);
1264 }
1265 if (extraInfo != null) {
1266 intent.putExtra(ConnectivityManager.EXTRA_EXTRA_INFO, extraInfo);
1267 }
1268 if (info.isFailover()) {
1269 intent.putExtra(ConnectivityManager.EXTRA_IS_FAILOVER, true);
1270 info.setFailover(false);
1271 }
Robert Greenwaltda03c4e2010-01-20 19:29:41 -08001272
Robert Greenwaltd55a6b42011-03-25 13:09:25 -07001273 if (mNetConfigs[info.getType()].isDefault()) {
Robert Greenwaltf21ef7d2011-01-11 13:56:33 -08001274 tryFailover(info.getType());
1275 if (mActiveDefaultNetwork != -1) {
1276 NetworkInfo switchTo = mNetTrackers[mActiveDefaultNetwork].getNetworkInfo();
Robert Greenwaltcc4b4012010-01-25 17:54:29 -08001277 intent.putExtra(ConnectivityManager.EXTRA_OTHER_NETWORK_INFO, switchTo);
1278 } else {
Robert Greenwalt029be812010-09-20 18:01:43 -07001279 mDefaultInetConditionPublished = 0;
Robert Greenwaltcc4b4012010-01-25 17:54:29 -08001280 intent.putExtra(ConnectivityManager.EXTRA_NO_CONNECTIVITY, true);
1281 }
Robert Greenwaltda03c4e2010-01-20 19:29:41 -08001282 }
Robert Greenwaltcc4b4012010-01-25 17:54:29 -08001283
Robert Greenwalt029be812010-09-20 18:01:43 -07001284 intent.putExtra(ConnectivityManager.EXTRA_INET_CONDITION, mDefaultInetConditionPublished);
Mike Lockwood0f79b542009-08-14 14:18:49 -04001285 sendStickyBroadcast(intent);
Robert Greenwaltda03c4e2010-01-20 19:29:41 -08001286 /*
1287 * If the failover network is already connected, then immediately send
1288 * out a followup broadcast indicating successful failover
1289 */
Robert Greenwaltf21ef7d2011-01-11 13:56:33 -08001290 if (mActiveDefaultNetwork != -1) {
1291 sendConnectedBroadcast(mNetTrackers[mActiveDefaultNetwork].getNetworkInfo());
Robert Greenwaltda03c4e2010-01-20 19:29:41 -08001292 }
Mike Lockwood0f79b542009-08-14 14:18:49 -04001293 }
1294
1295 private void sendStickyBroadcast(Intent intent) {
1296 synchronized(this) {
Dianne Hackborn1c633fc2009-12-08 19:45:14 -08001297 if (!mSystemReady) {
1298 mInitialBroadcast = new Intent(intent);
Mike Lockwood0f79b542009-08-14 14:18:49 -04001299 }
Dianne Hackborn1c633fc2009-12-08 19:45:14 -08001300 intent.addFlags(Intent.FLAG_RECEIVER_REGISTERED_ONLY_BEFORE_BOOT);
1301 mContext.sendStickyBroadcast(intent);
Mike Lockwood0f79b542009-08-14 14:18:49 -04001302 }
1303 }
1304
1305 void systemReady() {
Robert Greenwalt59b1a4e2011-05-10 15:05:02 -07001306 IBinder b = ServiceManager.getService(Context.NETWORKMANAGEMENT_SERVICE);
1307 mNetd = INetworkManagementService.Stub.asInterface(b);
1308
Mike Lockwood0f79b542009-08-14 14:18:49 -04001309 synchronized(this) {
1310 mSystemReady = true;
Dianne Hackborn1c633fc2009-12-08 19:45:14 -08001311 if (mInitialBroadcast != null) {
1312 mContext.sendStickyBroadcast(mInitialBroadcast);
1313 mInitialBroadcast = null;
Mike Lockwood0f79b542009-08-14 14:18:49 -04001314 }
1315 }
Robert Greenwalt434203a2010-10-11 16:00:27 -07001316 // load the global proxy at startup
1317 mHandler.sendMessage(mHandler.obtainMessage(EVENT_APPLY_GLOBAL_HTTP_PROXY));
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001318 }
1319
1320 private void handleConnect(NetworkInfo info) {
Robert Greenwalt42acef32009-08-12 16:08:25 -07001321 int type = info.getType();
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001322
1323 // snapshot isFailover, because sendConnectedBroadcast() resets it
1324 boolean isFailover = info.isFailover();
Robert Greenwalt42acef32009-08-12 16:08:25 -07001325 NetworkStateTracker thisNet = mNetTrackers[type];
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001326
Robert Greenwalt42acef32009-08-12 16:08:25 -07001327 // if this is a default net and other default is running
1328 // kill the one not preferred
Robert Greenwaltd55a6b42011-03-25 13:09:25 -07001329 if (mNetConfigs[type].isDefault()) {
Robert Greenwalt42acef32009-08-12 16:08:25 -07001330 if (mActiveDefaultNetwork != -1 && mActiveDefaultNetwork != type) {
1331 if ((type != mNetworkPreference &&
Wink Saville975c8482011-04-07 14:23:45 -07001332 mNetConfigs[mActiveDefaultNetwork].priority >
1333 mNetConfigs[type].priority) ||
Robert Greenwalt42acef32009-08-12 16:08:25 -07001334 mNetworkPreference == mActiveDefaultNetwork) {
1335 // don't accept this one
Wink Savilleed9c02b2010-12-03 12:01:38 -08001336 if (DBG) {
1337 log("Not broadcasting CONNECT_ACTION " +
Robert Greenwalt42acef32009-08-12 16:08:25 -07001338 "to torn down network " + info.getTypeName());
Wink Savilleed9c02b2010-12-03 12:01:38 -08001339 }
Robert Greenwalt42acef32009-08-12 16:08:25 -07001340 teardown(thisNet);
1341 return;
1342 } else {
1343 // tear down the other
1344 NetworkStateTracker otherNet =
1345 mNetTrackers[mActiveDefaultNetwork];
Wink Savilleed9c02b2010-12-03 12:01:38 -08001346 if (DBG) {
1347 log("Policy requires " + otherNet.getNetworkInfo().getTypeName() +
Robert Greenwalt42acef32009-08-12 16:08:25 -07001348 " teardown");
Wink Savilleed9c02b2010-12-03 12:01:38 -08001349 }
Robert Greenwalt42acef32009-08-12 16:08:25 -07001350 if (!teardown(otherNet)) {
Wink Savilleed9c02b2010-12-03 12:01:38 -08001351 loge("Network declined teardown request");
Robert Greenwalt42acef32009-08-12 16:08:25 -07001352 return;
1353 }
Robert Greenwalt14f2ef42010-06-15 12:19:37 -07001354 }
1355 }
1356 synchronized (ConnectivityService.this) {
1357 // have a new default network, release the transition wakelock in a second
1358 // if it's held. The second pause is to allow apps to reconnect over the
1359 // new network
1360 if (mNetTransitionWakeLock.isHeld()) {
1361 mHandler.sendMessageDelayed(mHandler.obtainMessage(
Robert Greenwaltf3331232010-09-24 14:32:21 -07001362 EVENT_CLEAR_NET_TRANSITION_WAKELOCK,
Robert Greenwalt14f2ef42010-06-15 12:19:37 -07001363 mNetTransitionWakeLockSerialNumber, 0),
1364 1000);
Robert Greenwalt42acef32009-08-12 16:08:25 -07001365 }
1366 }
1367 mActiveDefaultNetwork = type;
Robert Greenwaltd7085fc2010-09-08 15:24:47 -07001368 // this will cause us to come up initially as unconnected and switching
1369 // to connected after our normal pause unless somebody reports us as reall
1370 // disconnected
1371 mDefaultInetConditionPublished = 0;
1372 mDefaultConnectionSequence++;
1373 mInetConditionChangeInFlight = false;
1374 // Don't do this - if we never sign in stay, grey
1375 //reportNetworkCondition(mActiveDefaultNetwork, 100);
Robert Greenwalt42acef32009-08-12 16:08:25 -07001376 }
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001377 thisNet.setTeardownRequested(false);
Irfan Sheriffd649c122010-06-09 15:39:36 -07001378 updateNetworkSettings(thisNet);
Robert Greenwaltec896c62011-06-15 12:22:07 -07001379 handleConnectivityChange(type, false);
Robert Greenwalt42acef32009-08-12 16:08:25 -07001380 sendConnectedBroadcast(info);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001381 }
1382
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001383 /**
Robert Greenwaltc76b8fa2010-07-23 15:46:26 -07001384 * After a change in the connectivity state of a network. We're mainly
1385 * concerned with making sure that the list of DNS servers is set up
1386 * according to which networks are connected, and ensuring that the
1387 * right routing table entries exist.
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001388 */
Robert Greenwaltec896c62011-06-15 12:22:07 -07001389 private void handleConnectivityChange(int netType, boolean doReset) {
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001390 /*
Robert Greenwalt42acef32009-08-12 16:08:25 -07001391 * If a non-default network is enabled, add the host routes that
Robert Greenwaltc76b8fa2010-07-23 15:46:26 -07001392 * will allow it's DNS servers to be accessed.
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001393 */
Robert Greenwaltc76b8fa2010-07-23 15:46:26 -07001394 handleDnsConfigurationChange(netType);
Robert Greenwalt42acef32009-08-12 16:08:25 -07001395
Robert Greenwaltc76b8fa2010-07-23 15:46:26 -07001396 if (mNetTrackers[netType].getNetworkInfo().isConnected()) {
Robert Greenwaltd55a6b42011-03-25 13:09:25 -07001397 if (mNetConfigs[netType].isDefault()) {
Robert Greenwalt434203a2010-10-11 16:00:27 -07001398 handleApplyDefaultProxy(netType);
Robert Greenwaltc76b8fa2010-07-23 15:46:26 -07001399 addDefaultRoute(mNetTrackers[netType]);
Robert Greenwalt42acef32009-08-12 16:08:25 -07001400 } else {
Robert Greenwaltc76b8fa2010-07-23 15:46:26 -07001401 addPrivateDnsRoutes(mNetTrackers[netType]);
1402 }
Kazuhiro Ondo01758e82011-04-30 20:10:57 -05001403
1404 /** Notify TetheringService if interface name has been changed. */
1405 if (TextUtils.equals(mNetTrackers[netType].getNetworkInfo().getReason(),
1406 Phone.REASON_LINK_PROPERTIES_CHANGED)) {
1407 handleTetherIfaceChange(netType);
1408 }
Robert Greenwaltc76b8fa2010-07-23 15:46:26 -07001409 } else {
Robert Greenwaltd55a6b42011-03-25 13:09:25 -07001410 if (mNetConfigs[netType].isDefault()) {
Robert Greenwaltc76b8fa2010-07-23 15:46:26 -07001411 removeDefaultRoute(mNetTrackers[netType]);
1412 } else {
1413 removePrivateDnsRoutes(mNetTrackers[netType]);
Robert Greenwalt42acef32009-08-12 16:08:25 -07001414 }
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001415 }
Robert Greenwaltec896c62011-06-15 12:22:07 -07001416
1417 if (doReset) {
1418 LinkProperties linkProperties = mNetTrackers[netType].getLinkProperties();
1419 if (linkProperties != null) {
1420 String iface = linkProperties.getInterfaceName();
1421 if (TextUtils.isEmpty(iface) == false) {
1422 if (DBG) log("resetConnections(" + iface + ")");
1423 NetworkUtils.resetConnections(iface);
1424 }
1425 }
1426 }
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001427 }
1428
Irfan Sheriffd649c122010-06-09 15:39:36 -07001429 private void addPrivateDnsRoutes(NetworkStateTracker nt) {
Irfan Sheriffd649c122010-06-09 15:39:36 -07001430 boolean privateDnsRouteSet = nt.isPrivateDnsRouteSet();
Robert Greenwalt37e65eb2010-08-30 10:56:47 -07001431 LinkProperties p = nt.getLinkProperties();
Robert Greenwalt47f69fe2010-06-15 15:43:39 -07001432 if (p == null) return;
1433 String interfaceName = p.getInterfaceName();
Irfan Sheriffd649c122010-06-09 15:39:36 -07001434
1435 if (DBG) {
Wink Savilleed9c02b2010-12-03 12:01:38 -08001436 log("addPrivateDnsRoutes for " + nt +
Irfan Sheriffd649c122010-06-09 15:39:36 -07001437 "(" + interfaceName + ") - mPrivateDnsRouteSet = " + privateDnsRouteSet);
1438 }
Irfan Sheriffd649c122010-06-09 15:39:36 -07001439 if (interfaceName != null && !privateDnsRouteSet) {
Robert Greenwalt47f69fe2010-06-15 15:43:39 -07001440 Collection<InetAddress> dnsList = p.getDnses();
1441 for (InetAddress dns : dnsList) {
Robert Greenwaltf43396c2011-05-06 17:10:53 -07001442 addHostRoute(nt, dns, 0);
Irfan Sheriffd649c122010-06-09 15:39:36 -07001443 }
1444 nt.privateDnsRouteSet(true);
1445 }
1446 }
1447
1448 private void removePrivateDnsRoutes(NetworkStateTracker nt) {
Robert Greenwalt37e65eb2010-08-30 10:56:47 -07001449 LinkProperties p = nt.getLinkProperties();
Robert Greenwalt47f69fe2010-06-15 15:43:39 -07001450 if (p == null) return;
1451 String interfaceName = p.getInterfaceName();
Irfan Sheriffd649c122010-06-09 15:39:36 -07001452 boolean privateDnsRouteSet = nt.isPrivateDnsRouteSet();
1453 if (interfaceName != null && privateDnsRouteSet) {
1454 if (DBG) {
Wink Savilleed9c02b2010-12-03 12:01:38 -08001455 log("removePrivateDnsRoutes for " + nt.getNetworkInfo().getTypeName() +
Irfan Sheriffd649c122010-06-09 15:39:36 -07001456 " (" + interfaceName + ")");
1457 }
Robert Greenwalt59b1a4e2011-05-10 15:05:02 -07001458
1459 Collection<InetAddress> dnsList = p.getDnses();
1460 for (InetAddress dns : dnsList) {
1461 if (DBG) log(" removing " + dns);
1462 RouteInfo route = RouteInfo.makeHostRoute(dns);
1463 try {
1464 mNetd.removeRoute(interfaceName, route);
1465 } catch (Exception ex) {
1466 loge("error (" + ex + ") removing dns route " + route);
1467 }
1468 }
Irfan Sheriffd649c122010-06-09 15:39:36 -07001469 nt.privateDnsRouteSet(false);
1470 }
1471 }
1472
Irfan Sheriffd649c122010-06-09 15:39:36 -07001473
1474 private void addDefaultRoute(NetworkStateTracker nt) {
Robert Greenwalt37e65eb2010-08-30 10:56:47 -07001475 LinkProperties p = nt.getLinkProperties();
Robert Greenwalt47f69fe2010-06-15 15:43:39 -07001476 if (p == null) return;
1477 String interfaceName = p.getInterfaceName();
Robert Greenwalt992564e2011-02-09 13:56:06 -08001478 if (TextUtils.isEmpty(interfaceName)) return;
Irfan Sheriffd649c122010-06-09 15:39:36 -07001479
Robert Greenwalt59b1a4e2011-05-10 15:05:02 -07001480 for (RouteInfo route : p.getRoutes()) {
Robert Greenwaltaa70f102011-04-28 14:28:50 -07001481 //TODO - handle non-default routes
1482 if (route.isDefaultRoute()) {
Robert Greenwalt59b1a4e2011-05-10 15:05:02 -07001483 if (DBG) log("adding default route " + route);
Robert Greenwaltaa70f102011-04-28 14:28:50 -07001484 InetAddress gateway = route.getGateway();
Robert Greenwalt59b1a4e2011-05-10 15:05:02 -07001485 if (addHostRoute(nt, gateway, 0)) {
1486 try {
1487 mNetd.addRoute(interfaceName, route);
1488 } catch (Exception e) {
1489 loge("error adding default route " + route);
1490 continue;
1491 }
Robert Greenwaltaa70f102011-04-28 14:28:50 -07001492 if (DBG) {
1493 NetworkInfo networkInfo = nt.getNetworkInfo();
1494 log("addDefaultRoute for " + networkInfo.getTypeName() +
1495 " (" + interfaceName + "), GatewayAddr=" +
1496 gateway.getHostAddress());
1497 }
Robert Greenwalt59b1a4e2011-05-10 15:05:02 -07001498 } else {
1499 loge("error adding host route for default route " + route);
Robert Greenwaltedcb4f92011-03-22 18:47:42 -07001500 }
Irfan Sheriffd649c122010-06-09 15:39:36 -07001501 }
Irfan Sheriffd649c122010-06-09 15:39:36 -07001502 }
1503 }
1504
1505
1506 public void removeDefaultRoute(NetworkStateTracker nt) {
Robert Greenwalt37e65eb2010-08-30 10:56:47 -07001507 LinkProperties p = nt.getLinkProperties();
Robert Greenwalt47f69fe2010-06-15 15:43:39 -07001508 if (p == null) return;
1509 String interfaceName = p.getInterfaceName();
Irfan Sheriffd649c122010-06-09 15:39:36 -07001510
Robert Greenwalt59b1a4e2011-05-10 15:05:02 -07001511 if (interfaceName == null) return;
1512
1513 for (RouteInfo route : p.getRoutes()) {
1514 //TODO - handle non-default routes
1515 if (route.isDefaultRoute()) {
1516 try {
1517 mNetd.removeRoute(interfaceName, route);
1518 } catch (Exception ex) {
1519 loge("error (" + ex + ") removing default route " + route);
1520 continue;
1521 }
Robert Greenwaltedcb4f92011-03-22 18:47:42 -07001522 if (DBG) {
1523 NetworkInfo networkInfo = nt.getNetworkInfo();
1524 log("removeDefaultRoute for " + networkInfo.getTypeName() + " (" +
1525 interfaceName + ")");
1526 }
Irfan Sheriffd649c122010-06-09 15:39:36 -07001527 }
Irfan Sheriffd649c122010-06-09 15:39:36 -07001528 }
1529 }
1530
1531 /**
1532 * Reads the network specific TCP buffer sizes from SystemProperties
1533 * net.tcp.buffersize.[default|wifi|umts|edge|gprs] and set them for system
1534 * wide use
1535 */
1536 public void updateNetworkSettings(NetworkStateTracker nt) {
1537 String key = nt.getTcpBufferSizesPropName();
1538 String bufferSizes = SystemProperties.get(key);
1539
1540 if (bufferSizes.length() == 0) {
Wink Savilleed9c02b2010-12-03 12:01:38 -08001541 loge(key + " not found in system properties. Using defaults");
Irfan Sheriffd649c122010-06-09 15:39:36 -07001542
1543 // Setting to default values so we won't be stuck to previous values
1544 key = "net.tcp.buffersize.default";
1545 bufferSizes = SystemProperties.get(key);
1546 }
1547
1548 // Set values in kernel
1549 if (bufferSizes.length() != 0) {
1550 if (DBG) {
Wink Savilleed9c02b2010-12-03 12:01:38 -08001551 log("Setting TCP values: [" + bufferSizes
Irfan Sheriffd649c122010-06-09 15:39:36 -07001552 + "] which comes from [" + key + "]");
1553 }
1554 setBufferSize(bufferSizes);
1555 }
1556 }
1557
1558 /**
1559 * Writes TCP buffer sizes to /sys/kernel/ipv4/tcp_[r/w]mem_[min/def/max]
1560 * which maps to /proc/sys/net/ipv4/tcp_rmem and tcpwmem
1561 *
1562 * @param bufferSizes in the format of "readMin, readInitial, readMax,
1563 * writeMin, writeInitial, writeMax"
1564 */
1565 private void setBufferSize(String bufferSizes) {
1566 try {
1567 String[] values = bufferSizes.split(",");
1568
1569 if (values.length == 6) {
1570 final String prefix = "/sys/kernel/ipv4/tcp_";
1571 stringToFile(prefix + "rmem_min", values[0]);
1572 stringToFile(prefix + "rmem_def", values[1]);
1573 stringToFile(prefix + "rmem_max", values[2]);
1574 stringToFile(prefix + "wmem_min", values[3]);
1575 stringToFile(prefix + "wmem_def", values[4]);
1576 stringToFile(prefix + "wmem_max", values[5]);
1577 } else {
Wink Savilleed9c02b2010-12-03 12:01:38 -08001578 loge("Invalid buffersize string: " + bufferSizes);
Irfan Sheriffd649c122010-06-09 15:39:36 -07001579 }
1580 } catch (IOException e) {
Wink Savilleed9c02b2010-12-03 12:01:38 -08001581 loge("Can't set tcp buffer sizes:" + e);
Irfan Sheriffd649c122010-06-09 15:39:36 -07001582 }
1583 }
1584
1585 /**
1586 * Writes string to file. Basically same as "echo -n $string > $filename"
1587 *
1588 * @param filename
1589 * @param string
1590 * @throws IOException
1591 */
1592 private void stringToFile(String filename, String string) throws IOException {
1593 FileWriter out = new FileWriter(filename);
1594 try {
1595 out.write(string);
1596 } finally {
1597 out.close();
1598 }
1599 }
1600
1601
Robert Greenwalt42acef32009-08-12 16:08:25 -07001602 /**
1603 * Adjust the per-process dns entries (net.dns<x>.<pid>) based
1604 * on the highest priority active net which this process requested.
1605 * If there aren't any, clear it out
1606 */
1607 private void reassessPidDns(int myPid, boolean doBump)
1608 {
Wink Savilleed9c02b2010-12-03 12:01:38 -08001609 if (DBG) log("reassessPidDns for pid " + myPid);
Robert Greenwalt42acef32009-08-12 16:08:25 -07001610 for(int i : mPriorityList) {
Robert Greenwaltd55a6b42011-03-25 13:09:25 -07001611 if (mNetConfigs[i].isDefault()) {
Robert Greenwalt42acef32009-08-12 16:08:25 -07001612 continue;
1613 }
1614 NetworkStateTracker nt = mNetTrackers[i];
Robert Greenwalt86e9e552009-07-16 17:21:39 -07001615 if (nt.getNetworkInfo().isConnected() &&
1616 !nt.isTeardownRequested()) {
Robert Greenwalt37e65eb2010-08-30 10:56:47 -07001617 LinkProperties p = nt.getLinkProperties();
Robert Greenwalt47f69fe2010-06-15 15:43:39 -07001618 if (p == null) continue;
Robert Greenwalt42acef32009-08-12 16:08:25 -07001619 List pids = mNetRequestersPids[i];
1620 for (int j=0; j<pids.size(); j++) {
1621 Integer pid = (Integer)pids.get(j);
1622 if (pid.intValue() == myPid) {
Robert Greenwalt47f69fe2010-06-15 15:43:39 -07001623 Collection<InetAddress> dnses = p.getDnses();
1624 writePidDns(dnses, myPid);
Robert Greenwalt42acef32009-08-12 16:08:25 -07001625 if (doBump) {
1626 bumpDns();
1627 }
1628 return;
1629 }
1630 }
1631 }
1632 }
1633 // nothing found - delete
1634 for (int i = 1; ; i++) {
1635 String prop = "net.dns" + i + "." + myPid;
1636 if (SystemProperties.get(prop).length() == 0) {
1637 if (doBump) {
1638 bumpDns();
1639 }
1640 return;
1641 }
1642 SystemProperties.set(prop, "");
1643 }
1644 }
1645
Robert Greenwalt10398722010-12-17 15:20:36 -08001646 // return true if results in a change
1647 private boolean writePidDns(Collection <InetAddress> dnses, int pid) {
Robert Greenwalt42acef32009-08-12 16:08:25 -07001648 int j = 1;
Robert Greenwalt10398722010-12-17 15:20:36 -08001649 boolean changed = false;
Robert Greenwalt47f69fe2010-06-15 15:43:39 -07001650 for (InetAddress dns : dnses) {
Robert Greenwalt10398722010-12-17 15:20:36 -08001651 String dnsString = dns.getHostAddress();
1652 if (changed || !dnsString.equals(SystemProperties.get("net.dns" + j + "." + pid))) {
1653 changed = true;
1654 SystemProperties.set("net.dns" + j++ + "." + pid, dns.getHostAddress());
1655 }
Robert Greenwalt42acef32009-08-12 16:08:25 -07001656 }
Robert Greenwalt10398722010-12-17 15:20:36 -08001657 return changed;
Robert Greenwalt42acef32009-08-12 16:08:25 -07001658 }
1659
1660 private void bumpDns() {
1661 /*
1662 * Bump the property that tells the name resolver library to reread
1663 * the DNS server list from the properties.
1664 */
1665 String propVal = SystemProperties.get("net.dnschange");
1666 int n = 0;
1667 if (propVal.length() != 0) {
1668 try {
1669 n = Integer.parseInt(propVal);
1670 } catch (NumberFormatException e) {}
1671 }
1672 SystemProperties.set("net.dnschange", "" + (n+1));
Robert Greenwalt03595d02010-11-02 14:08:23 -07001673 /*
1674 * Tell the VMs to toss their DNS caches
1675 */
1676 Intent intent = new Intent(Intent.ACTION_CLEAR_DNS_CACHE);
1677 intent.addFlags(Intent.FLAG_RECEIVER_REPLACE_PENDING);
Stan Chesnutt3d1db862011-01-05 17:14:03 -08001678 /*
1679 * Connectivity events can happen before boot has completed ...
1680 */
1681 intent.addFlags(Intent.FLAG_RECEIVER_REGISTERED_ONLY_BEFORE_BOOT);
Robert Greenwalt03595d02010-11-02 14:08:23 -07001682 mContext.sendBroadcast(intent);
Robert Greenwalt42acef32009-08-12 16:08:25 -07001683 }
1684
Robert Greenwaltc76b8fa2010-07-23 15:46:26 -07001685 private void handleDnsConfigurationChange(int netType) {
Robert Greenwalt42acef32009-08-12 16:08:25 -07001686 // add default net's dns entries
Robert Greenwaltc76b8fa2010-07-23 15:46:26 -07001687 NetworkStateTracker nt = mNetTrackers[netType];
1688 if (nt != null && nt.getNetworkInfo().isConnected() && !nt.isTeardownRequested()) {
Robert Greenwalt37e65eb2010-08-30 10:56:47 -07001689 LinkProperties p = nt.getLinkProperties();
Robert Greenwaltc76b8fa2010-07-23 15:46:26 -07001690 if (p == null) return;
1691 Collection<InetAddress> dnses = p.getDnses();
Robert Greenwalt10398722010-12-17 15:20:36 -08001692 boolean changed = false;
Robert Greenwaltd55a6b42011-03-25 13:09:25 -07001693 if (mNetConfigs[netType].isDefault()) {
Robert Greenwaltc76b8fa2010-07-23 15:46:26 -07001694 int j = 1;
Robert Greenwalte90aa5e2010-09-01 11:34:05 -07001695 if (dnses.size() == 0 && mDefaultDns != null) {
Robert Greenwalt10398722010-12-17 15:20:36 -08001696 String dnsString = mDefaultDns.getHostAddress();
1697 if (!dnsString.equals(SystemProperties.get("net.dns1"))) {
1698 if (DBG) {
1699 log("no dns provided - using " + dnsString);
1700 }
1701 changed = true;
1702 SystemProperties.set("net.dns1", dnsString);
Robert Greenwalt42acef32009-08-12 16:08:25 -07001703 }
Robert Greenwalte90aa5e2010-09-01 11:34:05 -07001704 j++;
1705 } else {
1706 for (InetAddress dns : dnses) {
Robert Greenwalt10398722010-12-17 15:20:36 -08001707 String dnsString = dns.getHostAddress();
1708 if (!changed && dnsString.equals(SystemProperties.get("net.dns" + j))) {
1709 j++;
1710 continue;
1711 }
Robert Greenwalte90aa5e2010-09-01 11:34:05 -07001712 if (DBG) {
Wink Savilleed9c02b2010-12-03 12:01:38 -08001713 log("adding dns " + dns + " for " +
Robert Greenwalte90aa5e2010-09-01 11:34:05 -07001714 nt.getNetworkInfo().getTypeName());
1715 }
Robert Greenwalt10398722010-12-17 15:20:36 -08001716 changed = true;
1717 SystemProperties.set("net.dns" + j++, dnsString);
Robert Greenwalte90aa5e2010-09-01 11:34:05 -07001718 }
Robert Greenwaltc76b8fa2010-07-23 15:46:26 -07001719 }
1720 for (int k=j ; k<mNumDnsEntries; k++) {
Robert Greenwalt10398722010-12-17 15:20:36 -08001721 if (changed || !TextUtils.isEmpty(SystemProperties.get("net.dns" + k))) {
1722 if (DBG) log("erasing net.dns" + k);
1723 changed = true;
1724 SystemProperties.set("net.dns" + k, "");
1725 }
Robert Greenwaltc76b8fa2010-07-23 15:46:26 -07001726 }
1727 mNumDnsEntries = j;
1728 } else {
1729 // set per-pid dns for attached secondary nets
1730 List pids = mNetRequestersPids[netType];
1731 for (int y=0; y< pids.size(); y++) {
1732 Integer pid = (Integer)pids.get(y);
Robert Greenwalt10398722010-12-17 15:20:36 -08001733 changed = writePidDns(dnses, pid.intValue());
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001734 }
1735 }
Robert Greenwalt10398722010-12-17 15:20:36 -08001736 if (changed) bumpDns();
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001737 }
Robert Greenwalt42acef32009-08-12 16:08:25 -07001738 }
1739
Robert Greenwaltf2102f72011-05-03 19:02:44 -07001740 private int getRestoreDefaultNetworkDelay(int networkType) {
Robert Greenwalt42acef32009-08-12 16:08:25 -07001741 String restoreDefaultNetworkDelayStr = SystemProperties.get(
1742 NETWORK_RESTORE_DELAY_PROP_NAME);
1743 if(restoreDefaultNetworkDelayStr != null &&
1744 restoreDefaultNetworkDelayStr.length() != 0) {
1745 try {
1746 return Integer.valueOf(restoreDefaultNetworkDelayStr);
1747 } catch (NumberFormatException e) {
1748 }
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001749 }
Robert Greenwaltf2102f72011-05-03 19:02:44 -07001750 // if the system property isn't set, use the value for the apn type
1751 int ret = RESTORE_DEFAULT_NETWORK_DELAY;
1752
1753 if ((networkType <= ConnectivityManager.MAX_NETWORK_TYPE) &&
1754 (mNetConfigs[networkType] != null)) {
1755 ret = mNetConfigs[networkType].restoreTime;
1756 }
1757 return ret;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001758 }
1759
1760 @Override
1761 protected void dump(FileDescriptor fd, PrintWriter pw, String[] args) {
Robert Greenwalt86e9e552009-07-16 17:21:39 -07001762 if (mContext.checkCallingOrSelfPermission(
1763 android.Manifest.permission.DUMP)
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001764 != PackageManager.PERMISSION_GRANTED) {
Robert Greenwalt86e9e552009-07-16 17:21:39 -07001765 pw.println("Permission Denial: can't dump ConnectivityService " +
1766 "from from pid=" + Binder.getCallingPid() + ", uid=" +
1767 Binder.getCallingUid());
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001768 return;
1769 }
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001770 pw.println();
1771 for (NetworkStateTracker nst : mNetTrackers) {
Robert Greenwaltb9285352009-12-21 18:24:07 -08001772 if (nst != null) {
1773 if (nst.getNetworkInfo().isConnected()) {
1774 pw.println("Active network: " + nst.getNetworkInfo().
1775 getTypeName());
1776 }
1777 pw.println(nst.getNetworkInfo());
1778 pw.println(nst);
1779 pw.println();
Robert Greenwalt42acef32009-08-12 16:08:25 -07001780 }
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001781 }
Robert Greenwaltb9285352009-12-21 18:24:07 -08001782
1783 pw.println("Network Requester Pids:");
1784 for (int net : mPriorityList) {
1785 String pidString = net + ": ";
1786 for (Object pid : mNetRequestersPids[net]) {
1787 pidString = pidString + pid.toString() + ", ";
1788 }
1789 pw.println(pidString);
1790 }
1791 pw.println();
1792
1793 pw.println("FeatureUsers:");
1794 for (Object requester : mFeatureUsers) {
1795 pw.println(requester.toString());
1796 }
1797 pw.println();
Robert Greenwalt2a091d72010-02-11 18:18:40 -08001798
Robert Greenwalt14f2ef42010-06-15 12:19:37 -07001799 synchronized (this) {
1800 pw.println("NetworkTranstionWakeLock is currently " +
1801 (mNetTransitionWakeLock.isHeld() ? "" : "not ") + "held.");
1802 pw.println("It was last requested for "+mNetTransitionWakeLockCausedBy);
1803 }
1804 pw.println();
1805
Robert Greenwalt2a091d72010-02-11 18:18:40 -08001806 mTethering.dump(fd, pw, args);
Robert Greenwalt4e8dfef2010-09-20 14:35:25 -07001807
1808 if (mInetLog != null) {
1809 pw.println();
1810 pw.println("Inet condition reports:");
1811 for(int i = 0; i < mInetLog.size(); i++) {
1812 pw.println(mInetLog.get(i));
1813 }
1814 }
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001815 }
1816
Robert Greenwalt42acef32009-08-12 16:08:25 -07001817 // must be stateless - things change under us.
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001818 private class MyHandler extends Handler {
Wink Savillebb08caf2010-09-02 19:23:52 -07001819 public MyHandler(Looper looper) {
1820 super(looper);
1821 }
1822
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001823 @Override
1824 public void handleMessage(Message msg) {
1825 NetworkInfo info;
1826 switch (msg.what) {
1827 case NetworkStateTracker.EVENT_STATE_CHANGED:
1828 info = (NetworkInfo) msg.obj;
Robert Greenwalt511288a2009-12-07 11:33:18 -08001829 int type = info.getType();
1830 NetworkInfo.State state = info.getState();
Robert Greenwalt511288a2009-12-07 11:33:18 -08001831
Wink Savilleed9c02b2010-12-03 12:01:38 -08001832 if (DBG) log("ConnectivityChange for " +
Robert Greenwalt86e9e552009-07-16 17:21:39 -07001833 info.getTypeName() + ": " +
Robert Greenwalt511288a2009-12-07 11:33:18 -08001834 state + "/" + info.getDetailedState());
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001835
1836 // Connectivity state changed:
1837 // [31-13] Reserved for future use
Robert Greenwalt86e9e552009-07-16 17:21:39 -07001838 // [12-9] Network subtype (for mobile network, as defined
1839 // by TelephonyManager)
1840 // [8-3] Detailed state ordinal (as defined by
1841 // NetworkInfo.DetailedState)
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001842 // [2-0] Network type (as defined by ConnectivityManager)
1843 int eventLogParam = (info.getType() & 0x7) |
1844 ((info.getDetailedState().ordinal() & 0x3f) << 3) |
1845 (info.getSubtype() << 9);
Doug Zongkerab5c49c2009-12-04 10:31:43 -08001846 EventLog.writeEvent(EventLogTags.CONNECTIVITY_STATE_CHANGED,
Robert Greenwalt86e9e552009-07-16 17:21:39 -07001847 eventLogParam);
1848
1849 if (info.getDetailedState() ==
1850 NetworkInfo.DetailedState.FAILED) {
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001851 handleConnectionFailure(info);
Robert Greenwalt511288a2009-12-07 11:33:18 -08001852 } else if (state == NetworkInfo.State.DISCONNECTED) {
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001853 handleDisconnect(info);
Robert Greenwalt511288a2009-12-07 11:33:18 -08001854 } else if (state == NetworkInfo.State.SUSPENDED) {
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001855 // TODO: need to think this over.
Robert Greenwalt86e9e552009-07-16 17:21:39 -07001856 // the logic here is, handle SUSPENDED the same as
1857 // DISCONNECTED. The only difference being we are
1858 // broadcasting an intent with NetworkInfo that's
1859 // suspended. This allows the applications an
1860 // opportunity to handle DISCONNECTED and SUSPENDED
1861 // differently, or not.
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001862 handleDisconnect(info);
Robert Greenwalt511288a2009-12-07 11:33:18 -08001863 } else if (state == NetworkInfo.State.CONNECTED) {
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001864 handleConnect(info);
1865 }
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001866 break;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001867 case NetworkStateTracker.EVENT_CONFIGURATION_CHANGED:
Robert Greenwaltc76b8fa2010-07-23 15:46:26 -07001868 info = (NetworkInfo) msg.obj;
Robert Greenwaltec896c62011-06-15 12:22:07 -07001869 handleConnectivityChange(info.getType(), true);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001870 break;
Robert Greenwaltf3331232010-09-24 14:32:21 -07001871 case EVENT_CLEAR_NET_TRANSITION_WAKELOCK:
Robert Greenwalt14f2ef42010-06-15 12:19:37 -07001872 String causedBy = null;
1873 synchronized (ConnectivityService.this) {
1874 if (msg.arg1 == mNetTransitionWakeLockSerialNumber &&
1875 mNetTransitionWakeLock.isHeld()) {
1876 mNetTransitionWakeLock.release();
1877 causedBy = mNetTransitionWakeLockCausedBy;
1878 }
1879 }
1880 if (causedBy != null) {
Wink Savilleed9c02b2010-12-03 12:01:38 -08001881 log("NetTransition Wakelock for " + causedBy + " released by timeout");
Robert Greenwalt14f2ef42010-06-15 12:19:37 -07001882 }
Robert Greenwalt057d5e92010-09-09 14:05:10 -07001883 break;
Robert Greenwalt8dcc28b2010-09-23 10:05:56 -07001884 case EVENT_RESTORE_DEFAULT_NETWORK:
Robert Greenwalt9c75d4a2009-09-27 17:27:04 -07001885 FeatureUser u = (FeatureUser)msg.obj;
1886 u.expire();
Robert Greenwaltd7085fc2010-09-08 15:24:47 -07001887 break;
Robert Greenwalt8dcc28b2010-09-23 10:05:56 -07001888 case EVENT_INET_CONDITION_CHANGE:
1889 {
1890 int netType = msg.arg1;
1891 int condition = msg.arg2;
1892 handleInetConditionChange(netType, condition);
Robert Greenwalt14f2ef42010-06-15 12:19:37 -07001893 break;
Robert Greenwalt8dcc28b2010-09-23 10:05:56 -07001894 }
1895 case EVENT_INET_CONDITION_HOLD_END:
1896 {
1897 int netType = msg.arg1;
1898 int sequence = msg.arg2;
1899 handleInetConditionHoldEnd(netType, sequence);
Robert Greenwaltd7085fc2010-09-08 15:24:47 -07001900 break;
Robert Greenwalt8dcc28b2010-09-23 10:05:56 -07001901 }
1902 case EVENT_SET_NETWORK_PREFERENCE:
1903 {
1904 int preference = msg.arg1;
1905 handleSetNetworkPreference(preference);
1906 break;
1907 }
1908 case EVENT_SET_BACKGROUND_DATA:
1909 {
1910 boolean enabled = (msg.arg1 == ENABLED);
1911 handleSetBackgroundData(enabled);
1912 break;
1913 }
1914 case EVENT_SET_MOBILE_DATA:
1915 {
1916 boolean enabled = (msg.arg1 == ENABLED);
1917 handleSetMobileData(enabled);
1918 break;
1919 }
Robert Greenwalt434203a2010-10-11 16:00:27 -07001920 case EVENT_APPLY_GLOBAL_HTTP_PROXY:
1921 {
1922 handleDeprecatedGlobalHttpProxy();
Robert Greenwaltd55a6b42011-03-25 13:09:25 -07001923 break;
1924 }
1925 case EVENT_SET_DEPENDENCY_MET:
1926 {
1927 boolean met = (msg.arg1 == ENABLED);
1928 handleSetDependencyMet(msg.arg2, met);
1929 break;
Robert Greenwalt434203a2010-10-11 16:00:27 -07001930 }
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001931 }
1932 }
1933 }
Robert Greenwaltd0e18ff2010-01-26 11:40:34 -08001934
1935 // javadoc from interface
Robert Greenwalt5a735062010-03-02 17:25:02 -08001936 public int tether(String iface) {
Robert Greenwaltd0e18ff2010-01-26 11:40:34 -08001937 enforceTetherChangePermission();
Robert Greenwalt5a735062010-03-02 17:25:02 -08001938
1939 if (isTetheringSupported()) {
1940 return mTethering.tether(iface);
1941 } else {
1942 return ConnectivityManager.TETHER_ERROR_UNSUPPORTED;
1943 }
Robert Greenwaltd0e18ff2010-01-26 11:40:34 -08001944 }
1945
1946 // javadoc from interface
Robert Greenwalt5a735062010-03-02 17:25:02 -08001947 public int untether(String iface) {
Robert Greenwaltd0e18ff2010-01-26 11:40:34 -08001948 enforceTetherChangePermission();
Robert Greenwalt5a735062010-03-02 17:25:02 -08001949
1950 if (isTetheringSupported()) {
1951 return mTethering.untether(iface);
1952 } else {
1953 return ConnectivityManager.TETHER_ERROR_UNSUPPORTED;
1954 }
1955 }
1956
1957 // javadoc from interface
1958 public int getLastTetherError(String iface) {
1959 enforceTetherAccessPermission();
1960
1961 if (isTetheringSupported()) {
1962 return mTethering.getLastTetherError(iface);
1963 } else {
1964 return ConnectivityManager.TETHER_ERROR_UNSUPPORTED;
1965 }
Robert Greenwalt2a091d72010-02-11 18:18:40 -08001966 }
1967
1968 // TODO - proper iface API for selection by property, inspection, etc
1969 public String[] getTetherableUsbRegexs() {
1970 enforceTetherAccessPermission();
1971 if (isTetheringSupported()) {
1972 return mTethering.getTetherableUsbRegexs();
1973 } else {
1974 return new String[0];
1975 }
1976 }
1977
1978 public String[] getTetherableWifiRegexs() {
1979 enforceTetherAccessPermission();
1980 if (isTetheringSupported()) {
1981 return mTethering.getTetherableWifiRegexs();
1982 } else {
1983 return new String[0];
1984 }
Robert Greenwaltd0e18ff2010-01-26 11:40:34 -08001985 }
1986
Danica Chang6fdd0c62010-08-11 14:54:43 -07001987 public String[] getTetherableBluetoothRegexs() {
1988 enforceTetherAccessPermission();
1989 if (isTetheringSupported()) {
1990 return mTethering.getTetherableBluetoothRegexs();
1991 } else {
1992 return new String[0];
1993 }
1994 }
1995
Robert Greenwaltd0e18ff2010-01-26 11:40:34 -08001996 // TODO - move iface listing, queries, etc to new module
1997 // javadoc from interface
1998 public String[] getTetherableIfaces() {
Robert Greenwalt2a091d72010-02-11 18:18:40 -08001999 enforceTetherAccessPermission();
Robert Greenwaltd0e18ff2010-01-26 11:40:34 -08002000 return mTethering.getTetherableIfaces();
2001 }
2002
2003 public String[] getTetheredIfaces() {
Robert Greenwalt2a091d72010-02-11 18:18:40 -08002004 enforceTetherAccessPermission();
Robert Greenwaltd0e18ff2010-01-26 11:40:34 -08002005 return mTethering.getTetheredIfaces();
2006 }
Robert Greenwalt2a091d72010-02-11 18:18:40 -08002007
Robert Greenwalt5a735062010-03-02 17:25:02 -08002008 public String[] getTetheringErroredIfaces() {
2009 enforceTetherAccessPermission();
2010 return mTethering.getErroredIfaces();
2011 }
2012
Robert Greenwalt2a091d72010-02-11 18:18:40 -08002013 // if ro.tether.denied = true we default to no tethering
2014 // gservices could set the secure setting to 1 though to enable it on a build where it
2015 // had previously been turned off.
2016 public boolean isTetheringSupported() {
2017 enforceTetherAccessPermission();
2018 int defaultVal = (SystemProperties.get("ro.tether.denied").equals("true") ? 0 : 1);
Robert Greenwaltc9d5fb72010-02-25 12:29:30 -08002019 boolean tetherEnabledInSettings = (Settings.Secure.getInt(mContext.getContentResolver(),
2020 Settings.Secure.TETHER_SUPPORTED, defaultVal) != 0);
2021 return tetherEnabledInSettings && mTetheringConfigValid;
Robert Greenwalt2a091d72010-02-11 18:18:40 -08002022 }
Robert Greenwalt14f2ef42010-06-15 12:19:37 -07002023
2024 // An API NetworkStateTrackers can call when they lose their network.
2025 // This will automatically be cleared after X seconds or a network becomes CONNECTED,
2026 // whichever happens first. The timer is started by the first caller and not
2027 // restarted by subsequent callers.
2028 public void requestNetworkTransitionWakelock(String forWhom) {
2029 enforceConnectivityInternalPermission();
2030 synchronized (this) {
2031 if (mNetTransitionWakeLock.isHeld()) return;
2032 mNetTransitionWakeLockSerialNumber++;
2033 mNetTransitionWakeLock.acquire();
2034 mNetTransitionWakeLockCausedBy = forWhom;
2035 }
2036 mHandler.sendMessageDelayed(mHandler.obtainMessage(
Robert Greenwaltf3331232010-09-24 14:32:21 -07002037 EVENT_CLEAR_NET_TRANSITION_WAKELOCK,
Robert Greenwalt14f2ef42010-06-15 12:19:37 -07002038 mNetTransitionWakeLockSerialNumber, 0),
2039 mNetTransitionWakeLockTimeout);
2040 return;
2041 }
Robert Greenwaltca4306c2010-09-09 13:15:32 -07002042
Robert Greenwaltd7085fc2010-09-08 15:24:47 -07002043 // 100 percent is full good, 0 is full bad.
2044 public void reportInetCondition(int networkType, int percentage) {
Wink Savilleed9c02b2010-12-03 12:01:38 -08002045 if (DBG) log("reportNetworkCondition(" + networkType + ", " + percentage + ")");
Robert Greenwaltd7085fc2010-09-08 15:24:47 -07002046 mContext.enforceCallingOrSelfPermission(
2047 android.Manifest.permission.STATUS_BAR,
2048 "ConnectivityService");
2049
Robert Greenwalt4e8dfef2010-09-20 14:35:25 -07002050 if (DBG) {
2051 int pid = getCallingPid();
2052 int uid = getCallingUid();
2053 String s = pid + "(" + uid + ") reports inet is " +
2054 (percentage > 50 ? "connected" : "disconnected") + " (" + percentage + ") on " +
2055 "network Type " + networkType + " at " + GregorianCalendar.getInstance().getTime();
2056 mInetLog.add(s);
2057 while(mInetLog.size() > INET_CONDITION_LOG_MAX_SIZE) {
2058 mInetLog.remove(0);
2059 }
2060 }
Robert Greenwaltd7085fc2010-09-08 15:24:47 -07002061 mHandler.sendMessage(mHandler.obtainMessage(
Robert Greenwalt8dcc28b2010-09-23 10:05:56 -07002062 EVENT_INET_CONDITION_CHANGE, networkType, percentage));
2063 }
2064
2065 private void handleInetConditionChange(int netType, int condition) {
2066 if (DBG) {
Wink Savilleed9c02b2010-12-03 12:01:38 -08002067 log("Inet connectivity change, net=" +
Robert Greenwalt8dcc28b2010-09-23 10:05:56 -07002068 netType + ", condition=" + condition +
2069 ",mActiveDefaultNetwork=" + mActiveDefaultNetwork);
2070 }
2071 if (mActiveDefaultNetwork == -1) {
Wink Savilleed9c02b2010-12-03 12:01:38 -08002072 if (DBG) log("no active default network - aborting");
Robert Greenwalt8dcc28b2010-09-23 10:05:56 -07002073 return;
2074 }
2075 if (mActiveDefaultNetwork != netType) {
Wink Savilleed9c02b2010-12-03 12:01:38 -08002076 if (DBG) log("given net not default - aborting");
Robert Greenwalt8dcc28b2010-09-23 10:05:56 -07002077 return;
2078 }
2079 mDefaultInetCondition = condition;
2080 int delay;
2081 if (mInetConditionChangeInFlight == false) {
Wink Savilleed9c02b2010-12-03 12:01:38 -08002082 if (DBG) log("starting a change hold");
Robert Greenwalt8dcc28b2010-09-23 10:05:56 -07002083 // setup a new hold to debounce this
2084 if (mDefaultInetCondition > 50) {
2085 delay = Settings.Secure.getInt(mContext.getContentResolver(),
2086 Settings.Secure.INET_CONDITION_DEBOUNCE_UP_DELAY, 500);
2087 } else {
2088 delay = Settings.Secure.getInt(mContext.getContentResolver(),
2089 Settings.Secure.INET_CONDITION_DEBOUNCE_DOWN_DELAY, 3000);
2090 }
2091 mInetConditionChangeInFlight = true;
2092 mHandler.sendMessageDelayed(mHandler.obtainMessage(EVENT_INET_CONDITION_HOLD_END,
2093 mActiveDefaultNetwork, mDefaultConnectionSequence), delay);
2094 } else {
2095 // we've set the new condition, when this hold ends that will get
2096 // picked up
Wink Savilleed9c02b2010-12-03 12:01:38 -08002097 if (DBG) log("currently in hold - not setting new end evt");
Robert Greenwalt8dcc28b2010-09-23 10:05:56 -07002098 }
2099 }
2100
2101 private void handleInetConditionHoldEnd(int netType, int sequence) {
2102 if (DBG) {
Wink Savilleed9c02b2010-12-03 12:01:38 -08002103 log("Inet hold end, net=" + netType +
Robert Greenwalt8dcc28b2010-09-23 10:05:56 -07002104 ", condition =" + mDefaultInetCondition +
2105 ", published condition =" + mDefaultInetConditionPublished);
2106 }
2107 mInetConditionChangeInFlight = false;
2108
2109 if (mActiveDefaultNetwork == -1) {
Wink Savilleed9c02b2010-12-03 12:01:38 -08002110 if (DBG) log("no active default network - aborting");
Robert Greenwalt8dcc28b2010-09-23 10:05:56 -07002111 return;
2112 }
2113 if (mDefaultConnectionSequence != sequence) {
Wink Savilleed9c02b2010-12-03 12:01:38 -08002114 if (DBG) log("event hold for obsolete network - aborting");
Robert Greenwalt8dcc28b2010-09-23 10:05:56 -07002115 return;
2116 }
2117 if (mDefaultInetConditionPublished == mDefaultInetCondition) {
Wink Savilleed9c02b2010-12-03 12:01:38 -08002118 if (DBG) log("no change in condition - aborting");
Robert Greenwalt8dcc28b2010-09-23 10:05:56 -07002119 return;
2120 }
2121 NetworkInfo networkInfo = mNetTrackers[mActiveDefaultNetwork].getNetworkInfo();
2122 if (networkInfo.isConnected() == false) {
Wink Savilleed9c02b2010-12-03 12:01:38 -08002123 if (DBG) log("default network not connected - aborting");
Robert Greenwalt8dcc28b2010-09-23 10:05:56 -07002124 return;
2125 }
2126 mDefaultInetConditionPublished = mDefaultInetCondition;
2127 sendInetConditionBroadcast(networkInfo);
2128 return;
Robert Greenwaltd7085fc2010-09-08 15:24:47 -07002129 }
Robert Greenwalt434203a2010-10-11 16:00:27 -07002130
2131 public synchronized ProxyProperties getProxy() {
2132 if (mGlobalProxy != null) return mGlobalProxy;
2133 if (mDefaultProxy != null) return mDefaultProxy;
2134 return null;
2135 }
2136
2137 public void setGlobalProxy(ProxyProperties proxyProperties) {
2138 enforceChangePermission();
2139 synchronized (mGlobalProxyLock) {
2140 if (proxyProperties == mGlobalProxy) return;
2141 if (proxyProperties != null && proxyProperties.equals(mGlobalProxy)) return;
2142 if (mGlobalProxy != null && mGlobalProxy.equals(proxyProperties)) return;
2143
2144 String host = "";
2145 int port = 0;
2146 String exclList = "";
2147 if (proxyProperties != null && !TextUtils.isEmpty(proxyProperties.getHost())) {
2148 mGlobalProxy = new ProxyProperties(proxyProperties);
2149 host = mGlobalProxy.getHost();
2150 port = mGlobalProxy.getPort();
2151 exclList = mGlobalProxy.getExclusionList();
2152 } else {
2153 mGlobalProxy = null;
2154 }
2155 ContentResolver res = mContext.getContentResolver();
2156 Settings.Secure.putString(res, Settings.Secure.GLOBAL_HTTP_PROXY_HOST, host);
2157 Settings.Secure.putInt(res, Settings.Secure.GLOBAL_HTTP_PROXY_PORT, port);
Robert Greenwaltb7090d62010-12-02 11:31:00 -08002158 Settings.Secure.putString(res, Settings.Secure.GLOBAL_HTTP_PROXY_EXCLUSION_LIST,
Robert Greenwalt434203a2010-10-11 16:00:27 -07002159 exclList);
2160 }
2161
2162 if (mGlobalProxy == null) {
2163 proxyProperties = mDefaultProxy;
2164 }
2165 sendProxyBroadcast(proxyProperties);
2166 }
2167
Robert Greenwaltb7090d62010-12-02 11:31:00 -08002168 private void loadGlobalProxy() {
2169 ContentResolver res = mContext.getContentResolver();
2170 String host = Settings.Secure.getString(res, Settings.Secure.GLOBAL_HTTP_PROXY_HOST);
2171 int port = Settings.Secure.getInt(res, Settings.Secure.GLOBAL_HTTP_PROXY_PORT, 0);
2172 String exclList = Settings.Secure.getString(res,
2173 Settings.Secure.GLOBAL_HTTP_PROXY_EXCLUSION_LIST);
2174 if (!TextUtils.isEmpty(host)) {
2175 ProxyProperties proxyProperties = new ProxyProperties(host, port, exclList);
2176 synchronized (mGlobalProxyLock) {
2177 mGlobalProxy = proxyProperties;
2178 }
2179 }
2180 }
2181
Robert Greenwalt434203a2010-10-11 16:00:27 -07002182 public ProxyProperties getGlobalProxy() {
2183 synchronized (mGlobalProxyLock) {
2184 return mGlobalProxy;
2185 }
2186 }
2187
2188 private void handleApplyDefaultProxy(int type) {
2189 // check if new default - push it out to all VM if so
2190 ProxyProperties proxy = mNetTrackers[type].getLinkProperties().getHttpProxy();
2191 synchronized (this) {
2192 if (mDefaultProxy != null && mDefaultProxy.equals(proxy)) return;
2193 if (mDefaultProxy == proxy) return;
2194 if (!TextUtils.isEmpty(proxy.getHost())) {
2195 mDefaultProxy = proxy;
2196 } else {
2197 mDefaultProxy = null;
2198 }
2199 }
Wink Savilleed9c02b2010-12-03 12:01:38 -08002200 if (DBG) log("changing default proxy to " + proxy);
Robert Greenwalt434203a2010-10-11 16:00:27 -07002201 if ((proxy == null && mGlobalProxy == null) || proxy.equals(mGlobalProxy)) return;
2202 if (mGlobalProxy != null) return;
2203 sendProxyBroadcast(proxy);
2204 }
2205
2206 private void handleDeprecatedGlobalHttpProxy() {
2207 String proxy = Settings.Secure.getString(mContext.getContentResolver(),
2208 Settings.Secure.HTTP_PROXY);
2209 if (!TextUtils.isEmpty(proxy)) {
2210 String data[] = proxy.split(":");
2211 String proxyHost = data[0];
2212 int proxyPort = 8080;
2213 if (data.length > 1) {
2214 try {
2215 proxyPort = Integer.parseInt(data[1]);
2216 } catch (NumberFormatException e) {
2217 return;
2218 }
2219 }
2220 ProxyProperties p = new ProxyProperties(data[0], proxyPort, "");
2221 setGlobalProxy(p);
2222 }
2223 }
2224
2225 private void sendProxyBroadcast(ProxyProperties proxy) {
Robert Greenwalt55985be2010-12-23 15:51:10 -08002226 if (proxy == null) proxy = new ProxyProperties("", 0, "");
Wink Savilleed9c02b2010-12-03 12:01:38 -08002227 log("sending Proxy Broadcast for " + proxy);
Robert Greenwalt434203a2010-10-11 16:00:27 -07002228 Intent intent = new Intent(Proxy.PROXY_CHANGE_ACTION);
Stan Chesnuttb35d67a2011-01-06 11:00:19 -08002229 intent.addFlags(Intent.FLAG_RECEIVER_REPLACE_PENDING |
2230 Intent.FLAG_RECEIVER_REGISTERED_ONLY_BEFORE_BOOT);
Robert Greenwalt434203a2010-10-11 16:00:27 -07002231 intent.putExtra(Proxy.EXTRA_PROXY_INFO, proxy);
Robert Greenwalta2e13392010-12-06 11:29:17 -08002232 mContext.sendStickyBroadcast(intent);
Robert Greenwalt434203a2010-10-11 16:00:27 -07002233 }
2234
2235 private static class SettingsObserver extends ContentObserver {
2236 private int mWhat;
2237 private Handler mHandler;
2238 SettingsObserver(Handler handler, int what) {
2239 super(handler);
2240 mHandler = handler;
2241 mWhat = what;
2242 }
2243
2244 void observe(Context context) {
2245 ContentResolver resolver = context.getContentResolver();
2246 resolver.registerContentObserver(Settings.Secure.getUriFor(
2247 Settings.Secure.HTTP_PROXY), false, this);
2248 }
2249
2250 @Override
2251 public void onChange(boolean selfChange) {
2252 mHandler.obtainMessage(mWhat).sendToTarget();
2253 }
2254 }
Wink Savilleed9c02b2010-12-03 12:01:38 -08002255
Kazuhiro Ondo01758e82011-04-30 20:10:57 -05002256 private void handleTetherIfaceChange(int type) {
2257 String iface = mNetTrackers[type].getLinkProperties().getInterfaceName();
2258
2259 if (isTetheringSupported()) {
2260 mTethering.handleTetherIfaceChange(iface);
2261 }
2262 }
2263
Wink Savilleed9c02b2010-12-03 12:01:38 -08002264 private void log(String s) {
2265 Slog.d(TAG, s);
2266 }
2267
2268 private void loge(String s) {
2269 Slog.e(TAG, s);
2270 }
Wink Saville9d7d6282011-03-12 14:52:01 -08002271 int convertFeatureToNetworkType(String feature){
2272 int networkType = -1;
2273 if (TextUtils.equals(feature, Phone.FEATURE_ENABLE_MMS)) {
2274 networkType = ConnectivityManager.TYPE_MOBILE_MMS;
2275 } else if (TextUtils.equals(feature, Phone.FEATURE_ENABLE_SUPL)) {
2276 networkType = ConnectivityManager.TYPE_MOBILE_SUPL;
2277 } else if (TextUtils.equals(feature, Phone.FEATURE_ENABLE_DUN) ||
2278 TextUtils.equals(feature, Phone.FEATURE_ENABLE_DUN_ALWAYS)) {
2279 networkType = ConnectivityManager.TYPE_MOBILE_DUN;
2280 } else if (TextUtils.equals(feature, Phone.FEATURE_ENABLE_HIPRI)) {
2281 networkType = ConnectivityManager.TYPE_MOBILE_HIPRI;
2282 } else if (TextUtils.equals(feature, Phone.FEATURE_ENABLE_FOTA)) {
2283 networkType = ConnectivityManager.TYPE_MOBILE_FOTA;
2284 } else if (TextUtils.equals(feature, Phone.FEATURE_ENABLE_IMS)) {
2285 networkType = ConnectivityManager.TYPE_MOBILE_IMS;
2286 } else if (TextUtils.equals(feature, Phone.FEATURE_ENABLE_CBS)) {
2287 networkType = ConnectivityManager.TYPE_MOBILE_CBS;
2288 }
2289 return networkType;
2290 }
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002291}