blob: b923afcbc12ffeeaaf7aa8c1430e2522da4e1269 [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() {
543 enforceAccessPermission();
Robert Greenwalt42acef32009-08-12 16:08:25 -0700544 for (int type=0; type <= ConnectivityManager.MAX_NETWORK_TYPE; type++) {
Robert Greenwaltd55a6b42011-03-25 13:09:25 -0700545 if (mNetConfigs[type] == null || !mNetConfigs[type].isDefault()) {
Robert Greenwalt42acef32009-08-12 16:08:25 -0700546 continue;
547 }
548 NetworkStateTracker t = mNetTrackers[type];
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800549 NetworkInfo info = t.getNetworkInfo();
550 if (info.isConnected()) {
Wink Savilleed9c02b2010-12-03 12:01:38 -0800551 if (DBG && type != mActiveDefaultNetwork) {
552 loge("connected default network is not mActiveDefaultNetwork!");
553 }
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800554 return info;
555 }
556 }
557 return null;
558 }
559
560 public NetworkInfo getNetworkInfo(int networkType) {
561 enforceAccessPermission();
562 if (ConnectivityManager.isNetworkTypeValid(networkType)) {
563 NetworkStateTracker t = mNetTrackers[networkType];
564 if (t != null)
565 return t.getNetworkInfo();
566 }
567 return null;
568 }
569
570 public NetworkInfo[] getAllNetworkInfo() {
571 enforceAccessPermission();
Robert Greenwalt5154ae762009-10-30 14:17:42 -0700572 NetworkInfo[] result = new NetworkInfo[mNetworksDefined];
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800573 int i = 0;
574 for (NetworkStateTracker t : mNetTrackers) {
Robert Greenwalt5154ae762009-10-30 14:17:42 -0700575 if(t != null) result[i++] = t.getNetworkInfo();
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800576 }
577 return result;
578 }
579
Robert Greenwaltd192dad2010-09-14 09:18:02 -0700580 /**
581 * Return LinkProperties for the active (i.e., connected) default
582 * network interface. It is assumed that at most one default network
583 * is active at a time. If more than one is active, it is indeterminate
584 * which will be returned.
585 * @return the ip properties for the active network, or {@code null} if
586 * none is active
587 */
588 public LinkProperties getActiveLinkProperties() {
589 enforceAccessPermission();
590 for (int type=0; type <= ConnectivityManager.MAX_NETWORK_TYPE; type++) {
Robert Greenwaltd55a6b42011-03-25 13:09:25 -0700591 if (mNetConfigs[type] == null || !mNetConfigs[type].isDefault()) {
Robert Greenwaltd192dad2010-09-14 09:18:02 -0700592 continue;
593 }
594 NetworkStateTracker t = mNetTrackers[type];
595 NetworkInfo info = t.getNetworkInfo();
596 if (info.isConnected()) {
597 return t.getLinkProperties();
598 }
599 }
600 return null;
601 }
602
603 public LinkProperties getLinkProperties(int networkType) {
604 enforceAccessPermission();
605 if (ConnectivityManager.isNetworkTypeValid(networkType)) {
606 NetworkStateTracker t = mNetTrackers[networkType];
607 if (t != null) return t.getLinkProperties();
608 }
609 return null;
610 }
611
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800612 public boolean setRadios(boolean turnOn) {
613 boolean result = true;
614 enforceChangePermission();
615 for (NetworkStateTracker t : mNetTrackers) {
Robert Greenwalt5154ae762009-10-30 14:17:42 -0700616 if (t != null) result = t.setRadio(turnOn) && result;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800617 }
618 return result;
619 }
620
621 public boolean setRadio(int netType, boolean turnOn) {
622 enforceChangePermission();
623 if (!ConnectivityManager.isNetworkTypeValid(netType)) {
624 return false;
625 }
626 NetworkStateTracker tracker = mNetTrackers[netType];
627 return tracker != null && tracker.setRadio(turnOn);
628 }
629
Robert Greenwalt9c75d4a2009-09-27 17:27:04 -0700630 /**
631 * Used to notice when the calling process dies so we can self-expire
632 *
633 * Also used to know if the process has cleaned up after itself when
634 * our auto-expire timer goes off. The timer has a link to an object.
635 *
636 */
Robert Greenwalt42acef32009-08-12 16:08:25 -0700637 private class FeatureUser implements IBinder.DeathRecipient {
638 int mNetworkType;
639 String mFeature;
640 IBinder mBinder;
641 int mPid;
642 int mUid;
Robert Greenwaltb9285352009-12-21 18:24:07 -0800643 long mCreateTime;
Robert Greenwalt42acef32009-08-12 16:08:25 -0700644
645 FeatureUser(int type, String feature, IBinder binder) {
646 super();
647 mNetworkType = type;
648 mFeature = feature;
649 mBinder = binder;
650 mPid = getCallingPid();
651 mUid = getCallingUid();
Robert Greenwaltb9285352009-12-21 18:24:07 -0800652 mCreateTime = System.currentTimeMillis();
Robert Greenwalt9c75d4a2009-09-27 17:27:04 -0700653
Robert Greenwalt42acef32009-08-12 16:08:25 -0700654 try {
655 mBinder.linkToDeath(this, 0);
656 } catch (RemoteException e) {
657 binderDied();
658 }
659 }
660
661 void unlinkDeathRecipient() {
662 mBinder.unlinkToDeath(this, 0);
663 }
664
665 public void binderDied() {
Wink Savilleed9c02b2010-12-03 12:01:38 -0800666 log("ConnectivityService FeatureUser binderDied(" +
Robert Greenwaltb9285352009-12-21 18:24:07 -0800667 mNetworkType + ", " + mFeature + ", " + mBinder + "), created " +
668 (System.currentTimeMillis() - mCreateTime) + " mSec ago");
Robert Greenwalt9c75d4a2009-09-27 17:27:04 -0700669 stopUsingNetworkFeature(this, false);
Robert Greenwalt42acef32009-08-12 16:08:25 -0700670 }
671
Robert Greenwalt9c75d4a2009-09-27 17:27:04 -0700672 public void expire() {
Wink Savilleed9c02b2010-12-03 12:01:38 -0800673 log("ConnectivityService FeatureUser expire(" +
Robert Greenwaltb9285352009-12-21 18:24:07 -0800674 mNetworkType + ", " + mFeature + ", " + mBinder +"), created " +
675 (System.currentTimeMillis() - mCreateTime) + " mSec ago");
Robert Greenwalt9c75d4a2009-09-27 17:27:04 -0700676 stopUsingNetworkFeature(this, false);
677 }
Robert Greenwaltb9285352009-12-21 18:24:07 -0800678
679 public String toString() {
680 return "FeatureUser("+mNetworkType+","+mFeature+","+mPid+","+mUid+"), created " +
681 (System.currentTimeMillis() - mCreateTime) + " mSec ago";
682 }
Robert Greenwalt42acef32009-08-12 16:08:25 -0700683 }
684
Robert Greenwalt9c75d4a2009-09-27 17:27:04 -0700685 // javadoc from interface
Robert Greenwalt42acef32009-08-12 16:08:25 -0700686 public int startUsingNetworkFeature(int networkType, String feature,
687 IBinder binder) {
688 if (DBG) {
Wink Savilleed9c02b2010-12-03 12:01:38 -0800689 log("startUsingNetworkFeature for net " + networkType + ": " + feature);
Robert Greenwalt42acef32009-08-12 16:08:25 -0700690 }
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800691 enforceChangePermission();
Robert Greenwalt5154ae762009-10-30 14:17:42 -0700692 if (!ConnectivityManager.isNetworkTypeValid(networkType) ||
Robert Greenwaltd55a6b42011-03-25 13:09:25 -0700693 mNetConfigs[networkType] == null) {
Robert Greenwalt42acef32009-08-12 16:08:25 -0700694 return Phone.APN_REQUEST_FAILED;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800695 }
Robert Greenwalt42acef32009-08-12 16:08:25 -0700696
Robert Greenwalt9c75d4a2009-09-27 17:27:04 -0700697 FeatureUser f = new FeatureUser(networkType, feature, binder);
Robert Greenwalt42acef32009-08-12 16:08:25 -0700698
699 // TODO - move this into the MobileDataStateTracker
700 int usedNetworkType = networkType;
701 if(networkType == ConnectivityManager.TYPE_MOBILE) {
Wink Saville9d7d6282011-03-12 14:52:01 -0800702 usedNetworkType = convertFeatureToNetworkType(feature);
703 if (usedNetworkType < 0) {
704 Slog.e(TAG, "Can't match any netTracker!");
705 usedNetworkType = networkType;
Robert Greenwalt42acef32009-08-12 16:08:25 -0700706 }
707 }
708 NetworkStateTracker network = mNetTrackers[usedNetworkType];
709 if (network != null) {
Robert Greenwalt0be1e982010-12-15 13:26:33 -0800710 Integer currentPid = new Integer(getCallingPid());
Robert Greenwalt42acef32009-08-12 16:08:25 -0700711 if (usedNetworkType != networkType) {
Robert Greenwalt42acef32009-08-12 16:08:25 -0700712 NetworkStateTracker radio = mNetTrackers[networkType];
713 NetworkInfo ni = network.getNetworkInfo();
714
715 if (ni.isAvailable() == false) {
Wink Savilleed9c02b2010-12-03 12:01:38 -0800716 if (DBG) log("special network not available");
Robert Greenwalte32e8122010-12-29 14:35:21 -0800717 if (!TextUtils.equals(feature,Phone.FEATURE_ENABLE_DUN_ALWAYS)) {
718 return Phone.APN_TYPE_NOT_AVAILABLE;
719 } else {
720 // else make the attempt anyway - probably giving REQUEST_STARTED below
721 }
Robert Greenwalt42acef32009-08-12 16:08:25 -0700722 }
723
Robert Greenwalt9c75d4a2009-09-27 17:27:04 -0700724 synchronized(this) {
725 mFeatureUsers.add(f);
726 if (!mNetRequestersPids[usedNetworkType].contains(currentPid)) {
727 // this gets used for per-pid dns when connected
728 mNetRequestersPids[usedNetworkType].add(currentPid);
729 }
Robert Greenwalt42acef32009-08-12 16:08:25 -0700730 }
Robert Greenwalt9c75d4a2009-09-27 17:27:04 -0700731
Robert Greenwaltf2102f72011-05-03 19:02:44 -0700732 int restoreTimer = getRestoreDefaultNetworkDelay(usedNetworkType);
733
734 if (restoreTimer >= 0) {
735 mHandler.sendMessageDelayed(
736 mHandler.obtainMessage(EVENT_RESTORE_DEFAULT_NETWORK, f), restoreTimer);
737 }
Robert Greenwalt42acef32009-08-12 16:08:25 -0700738
Robert Greenwalta64bf832009-08-19 20:19:33 -0700739 if ((ni.isConnectedOrConnecting() == true) &&
740 !network.isTeardownRequested()) {
Robert Greenwalt42acef32009-08-12 16:08:25 -0700741 if (ni.isConnected() == true) {
742 // add the pid-specific dns
Robert Greenwaltc76b8fa2010-07-23 15:46:26 -0700743 handleDnsConfigurationChange(networkType);
Wink Savilleed9c02b2010-12-03 12:01:38 -0800744 if (DBG) log("special network already active");
Robert Greenwalt42acef32009-08-12 16:08:25 -0700745 return Phone.APN_ALREADY_ACTIVE;
746 }
Wink Savilleed9c02b2010-12-03 12:01:38 -0800747 if (DBG) log("special network already connecting");
Robert Greenwalt42acef32009-08-12 16:08:25 -0700748 return Phone.APN_REQUEST_STARTED;
749 }
750
751 // check if the radio in play can make another contact
752 // assume if cannot for now
753
Wink Savilleed9c02b2010-12-03 12:01:38 -0800754 if (DBG) log("reconnecting to special network");
Robert Greenwalt42acef32009-08-12 16:08:25 -0700755 network.reconnect();
756 return Phone.APN_REQUEST_STARTED;
757 } else {
Robert Greenwalt0be1e982010-12-15 13:26:33 -0800758 // need to remember this unsupported request so we respond appropriately on stop
759 synchronized(this) {
760 mFeatureUsers.add(f);
761 if (!mNetRequestersPids[usedNetworkType].contains(currentPid)) {
762 // this gets used for per-pid dns when connected
763 mNetRequestersPids[usedNetworkType].add(currentPid);
764 }
765 }
Robert Greenwalt02648a42010-05-18 10:52:51 -0700766 return -1;
Robert Greenwalt42acef32009-08-12 16:08:25 -0700767 }
768 }
769 return Phone.APN_TYPE_NOT_AVAILABLE;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800770 }
771
Robert Greenwalt9c75d4a2009-09-27 17:27:04 -0700772 // javadoc from interface
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800773 public int stopUsingNetworkFeature(int networkType, String feature) {
Robert Greenwaltb8f16342009-10-06 17:52:40 -0700774 enforceChangePermission();
775
Robert Greenwalt9c75d4a2009-09-27 17:27:04 -0700776 int pid = getCallingPid();
777 int uid = getCallingUid();
778
779 FeatureUser u = null;
780 boolean found = false;
781
782 synchronized(this) {
783 for (int i = 0; i < mFeatureUsers.size() ; i++) {
784 u = (FeatureUser)mFeatureUsers.get(i);
785 if (uid == u.mUid && pid == u.mPid &&
786 networkType == u.mNetworkType &&
787 TextUtils.equals(feature, u.mFeature)) {
788 found = true;
789 break;
790 }
791 }
792 }
793 if (found && u != null) {
794 // stop regardless of how many other time this proc had called start
795 return stopUsingNetworkFeature(u, true);
796 } else {
797 // none found!
Wink Savilleed9c02b2010-12-03 12:01:38 -0800798 if (DBG) log("ignoring stopUsingNetworkFeature - not a live request");
Robert Greenwalt9c75d4a2009-09-27 17:27:04 -0700799 return 1;
800 }
Robert Greenwalt42acef32009-08-12 16:08:25 -0700801 }
802
Robert Greenwalt9c75d4a2009-09-27 17:27:04 -0700803 private int stopUsingNetworkFeature(FeatureUser u, boolean ignoreDups) {
804 int networkType = u.mNetworkType;
805 String feature = u.mFeature;
806 int pid = u.mPid;
807 int uid = u.mUid;
808
809 NetworkStateTracker tracker = null;
810 boolean callTeardown = false; // used to carry our decision outside of sync block
811
Robert Greenwalt42acef32009-08-12 16:08:25 -0700812 if (DBG) {
Wink Savilleed9c02b2010-12-03 12:01:38 -0800813 log("stopUsingNetworkFeature for net " + networkType +
Robert Greenwalt42acef32009-08-12 16:08:25 -0700814 ": " + feature);
815 }
Robert Greenwaltb8f16342009-10-06 17:52:40 -0700816
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800817 if (!ConnectivityManager.isNetworkTypeValid(networkType)) {
818 return -1;
819 }
Robert Greenwalt42acef32009-08-12 16:08:25 -0700820
Robert Greenwalt9c75d4a2009-09-27 17:27:04 -0700821 // need to link the mFeatureUsers list with the mNetRequestersPids state in this
822 // sync block
823 synchronized(this) {
824 // check if this process still has an outstanding start request
825 if (!mFeatureUsers.contains(u)) {
Wink Savilleed9c02b2010-12-03 12:01:38 -0800826 if (DBG) log("ignoring - this process has no outstanding requests");
Robert Greenwalt42acef32009-08-12 16:08:25 -0700827 return 1;
828 }
Robert Greenwalt9c75d4a2009-09-27 17:27:04 -0700829 u.unlinkDeathRecipient();
830 mFeatureUsers.remove(mFeatureUsers.indexOf(u));
831 // If we care about duplicate requests, check for that here.
832 //
833 // This is done to support the extension of a request - the app
834 // can request we start the network feature again and renew the
835 // auto-shutoff delay. Normal "stop" calls from the app though
836 // do not pay attention to duplicate requests - in effect the
837 // API does not refcount and a single stop will counter multiple starts.
838 if (ignoreDups == false) {
839 for (int i = 0; i < mFeatureUsers.size() ; i++) {
840 FeatureUser x = (FeatureUser)mFeatureUsers.get(i);
841 if (x.mUid == u.mUid && x.mPid == u.mPid &&
842 x.mNetworkType == u.mNetworkType &&
843 TextUtils.equals(x.mFeature, u.mFeature)) {
Wink Savilleed9c02b2010-12-03 12:01:38 -0800844 if (DBG) log("ignoring stopUsingNetworkFeature as dup is found");
Robert Greenwalt9c75d4a2009-09-27 17:27:04 -0700845 return 1;
846 }
847 }
Robert Greenwalt42acef32009-08-12 16:08:25 -0700848 }
Robert Greenwalt9c75d4a2009-09-27 17:27:04 -0700849
850 // TODO - move to MobileDataStateTracker
851 int usedNetworkType = networkType;
852 if (networkType == ConnectivityManager.TYPE_MOBILE) {
Wink Saville9d7d6282011-03-12 14:52:01 -0800853 usedNetworkType = convertFeatureToNetworkType(feature);
854 if (usedNetworkType < 0) {
855 usedNetworkType = networkType;
Robert Greenwalt9c75d4a2009-09-27 17:27:04 -0700856 }
857 }
858 tracker = mNetTrackers[usedNetworkType];
Robert Greenwalt5154ae762009-10-30 14:17:42 -0700859 if (tracker == null) {
Wink Savilleed9c02b2010-12-03 12:01:38 -0800860 if (DBG) log("ignoring - no known tracker for net type " + usedNetworkType);
Robert Greenwalt5154ae762009-10-30 14:17:42 -0700861 return -1;
862 }
863 if (usedNetworkType != networkType) {
Robert Greenwalt9c75d4a2009-09-27 17:27:04 -0700864 Integer currentPid = new Integer(pid);
Robert Greenwalt9c75d4a2009-09-27 17:27:04 -0700865 mNetRequestersPids[usedNetworkType].remove(currentPid);
Robert Greenwalt421c72b2009-12-17 14:54:59 -0800866 reassessPidDns(pid, true);
Robert Greenwalt9c75d4a2009-09-27 17:27:04 -0700867 if (mNetRequestersPids[usedNetworkType].size() != 0) {
Wink Savilleed9c02b2010-12-03 12:01:38 -0800868 if (DBG) log("not tearing down special network - " +
Robert Greenwalt9c75d4a2009-09-27 17:27:04 -0700869 "others still using it");
870 return 1;
871 }
872 callTeardown = true;
Robert Greenwalt19b9ab42011-01-10 11:58:31 -0800873 } else {
874 if (DBG) log("not a known feature - dropping");
Robert Greenwalt9c75d4a2009-09-27 17:27:04 -0700875 }
876 }
Wink Savilleed9c02b2010-12-03 12:01:38 -0800877 if (DBG) log("Doing network teardown");
Robert Greenwalt9c75d4a2009-09-27 17:27:04 -0700878 if (callTeardown) {
879 tracker.teardown();
Robert Greenwalt42acef32009-08-12 16:08:25 -0700880 return 1;
881 } else {
Robert Greenwalt02648a42010-05-18 10:52:51 -0700882 return -1;
Robert Greenwalt42acef32009-08-12 16:08:25 -0700883 }
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800884 }
885
886 /**
Robert Greenwalt585ac0f2010-08-27 09:24:29 -0700887 * @deprecated use requestRouteToHostAddress instead
888 *
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800889 * Ensure that a network route exists to deliver traffic to the specified
890 * host via the specified network interface.
Robert Greenwalt86e9e552009-07-16 17:21:39 -0700891 * @param networkType the type of the network over which traffic to the
892 * specified host is to be routed
893 * @param hostAddress the IP address of the host to which the route is
894 * desired
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800895 * @return {@code true} on success, {@code false} on failure
896 */
897 public boolean requestRouteToHost(int networkType, int hostAddress) {
Robert Greenwalt585ac0f2010-08-27 09:24:29 -0700898 InetAddress inetAddress = NetworkUtils.intToInetAddress(hostAddress);
899
900 if (inetAddress == null) {
901 return false;
902 }
903
904 return requestRouteToHostAddress(networkType, inetAddress.getAddress());
905 }
906
907 /**
908 * Ensure that a network route exists to deliver traffic to the specified
909 * host via the specified network interface.
910 * @param networkType the type of the network over which traffic to the
911 * specified host is to be routed
912 * @param hostAddress the IP address of the host to which the route is
913 * desired
914 * @return {@code true} on success, {@code false} on failure
915 */
916 public boolean requestRouteToHostAddress(int networkType, byte[] hostAddress) {
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800917 enforceChangePermission();
918 if (!ConnectivityManager.isNetworkTypeValid(networkType)) {
919 return false;
920 }
921 NetworkStateTracker tracker = mNetTrackers[networkType];
Robert Greenwalt8206ff32009-09-10 15:06:20 -0700922
Robert Greenwalt5154ae762009-10-30 14:17:42 -0700923 if (tracker == null || !tracker.getNetworkInfo().isConnected() ||
924 tracker.isTeardownRequested()) {
Robert Greenwalt8206ff32009-09-10 15:06:20 -0700925 if (DBG) {
Wink Savilleed9c02b2010-12-03 12:01:38 -0800926 log("requestRouteToHostAddress on down network " +
Robert Greenwalt585ac0f2010-08-27 09:24:29 -0700927 "(" + networkType + ") - dropped");
Robert Greenwalt8206ff32009-09-10 15:06:20 -0700928 }
929 return false;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800930 }
Robert Greenwalt47f69fe2010-06-15 15:43:39 -0700931 try {
Robert Greenwalt585ac0f2010-08-27 09:24:29 -0700932 InetAddress addr = InetAddress.getByAddress(hostAddress);
Robert Greenwaltf43396c2011-05-06 17:10:53 -0700933 return addHostRoute(tracker, addr, 0);
Robert Greenwalt47f69fe2010-06-15 15:43:39 -0700934 } catch (UnknownHostException e) {}
935 return false;
Irfan Sheriffd649c122010-06-09 15:39:36 -0700936 }
937
938 /**
939 * Ensure that a network route exists to deliver traffic to the specified
940 * host via the mobile data network.
941 * @param hostAddress the IP address of the host to which the route is desired,
942 * in network byte order.
Robert Greenwalt47f69fe2010-06-15 15:43:39 -0700943 * TODO - deprecate
Irfan Sheriffd649c122010-06-09 15:39:36 -0700944 * @return {@code true} on success, {@code false} on failure
945 */
Robert Greenwaltf43396c2011-05-06 17:10:53 -0700946 private boolean addHostRoute(NetworkStateTracker nt, InetAddress hostAddress, int cycleCount) {
Robert Greenwaltf43396c2011-05-06 17:10:53 -0700947 LinkProperties lp = nt.getLinkProperties();
948 if ((lp == null) || (hostAddress == null)) return false;
Irfan Sheriffd649c122010-06-09 15:39:36 -0700949
Robert Greenwaltf43396c2011-05-06 17:10:53 -0700950 String interfaceName = lp.getInterfaceName();
Irfan Sheriffd649c122010-06-09 15:39:36 -0700951 if (DBG) {
Robert Greenwaltf43396c2011-05-06 17:10:53 -0700952 log("Requested host route to " + hostAddress + "(" + interfaceName + "), cycleCount=" +
953 cycleCount);
Irfan Sheriffd649c122010-06-09 15:39:36 -0700954 }
Robert Greenwaltf43396c2011-05-06 17:10:53 -0700955 if (interfaceName == null) {
Wink Savilleed9c02b2010-12-03 12:01:38 -0800956 if (DBG) loge("addHostRoute failed due to null interface name");
Irfan Sheriffd649c122010-06-09 15:39:36 -0700957 return false;
958 }
Robert Greenwaltf43396c2011-05-06 17:10:53 -0700959
960 RouteInfo bestRoute = RouteInfo.selectBestRoute(lp.getRoutes(), hostAddress);
Robert Greenwalt59b1a4e2011-05-10 15:05:02 -0700961 InetAddress gatewayAddress = null;
Robert Greenwaltf43396c2011-05-06 17:10:53 -0700962 if (bestRoute != null) {
Robert Greenwalt59b1a4e2011-05-10 15:05:02 -0700963 gatewayAddress = bestRoute.getGateway();
Robert Greenwaltf43396c2011-05-06 17:10:53 -0700964 // if the best route is ourself, don't relf-reference, just add the host route
Robert Greenwalt59b1a4e2011-05-10 15:05:02 -0700965 if (hostAddress.equals(gatewayAddress)) gatewayAddress = null;
Robert Greenwaltf43396c2011-05-06 17:10:53 -0700966 }
Robert Greenwalt59b1a4e2011-05-10 15:05:02 -0700967 if (gatewayAddress != null) {
Robert Greenwaltf43396c2011-05-06 17:10:53 -0700968 if (cycleCount > MAX_HOSTROUTE_CYCLE_COUNT) {
969 loge("Error adding hostroute - too much recursion");
970 return false;
971 }
Robert Greenwalt59b1a4e2011-05-10 15:05:02 -0700972 if (!addHostRoute(nt, gatewayAddress, cycleCount+1)) return false;
Robert Greenwaltf43396c2011-05-06 17:10:53 -0700973 }
Robert Greenwalt59b1a4e2011-05-10 15:05:02 -0700974
975 RouteInfo route = RouteInfo.makeHostRoute(hostAddress, gatewayAddress);
976
977 try {
978 mNetd.addRoute(interfaceName, route);
979 return true;
980 } catch (Exception ex) {
981 return false;
982 }
Robert Greenwaltf43396c2011-05-06 17:10:53 -0700983 }
984
985 // TODO support the removal of single host routes. Keep a ref count of them so we
986 // aren't over-zealous
987 private boolean removeHostRoute(NetworkStateTracker nt, InetAddress hostAddress) {
988 return false;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800989 }
990
991 /**
992 * @see ConnectivityManager#getBackgroundDataSetting()
993 */
994 public boolean getBackgroundDataSetting() {
Robert Greenwaltd825ea42010-12-29 16:15:02 -0800995 return mBackgroundDataEnabled.get();
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800996 }
Robert Greenwalt86e9e552009-07-16 17:21:39 -0700997
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800998 /**
999 * @see ConnectivityManager#setBackgroundDataSetting(boolean)
1000 */
1001 public void setBackgroundDataSetting(boolean allowBackgroundDataUsage) {
1002 mContext.enforceCallingOrSelfPermission(
1003 android.Manifest.permission.CHANGE_BACKGROUND_DATA_SETTING,
1004 "ConnectivityService");
Robert Greenwalt86e9e552009-07-16 17:21:39 -07001005
Robert Greenwaltd825ea42010-12-29 16:15:02 -08001006 mBackgroundDataEnabled.set(allowBackgroundDataUsage);
1007
Robert Greenwalt8dcc28b2010-09-23 10:05:56 -07001008 mHandler.sendMessage(mHandler.obtainMessage(EVENT_SET_BACKGROUND_DATA,
1009 (allowBackgroundDataUsage ? ENABLED : DISABLED), 0));
1010 }
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001011
Robert Greenwalt8dcc28b2010-09-23 10:05:56 -07001012 private void handleSetBackgroundData(boolean enabled) {
Robert Greenwaltdb4afae2011-02-25 13:44:09 -08001013 Settings.Secure.putInt(mContext.getContentResolver(),
1014 Settings.Secure.BACKGROUND_DATA, enabled ? 1 : 0);
1015 Intent broadcast = new Intent(
1016 ConnectivityManager.ACTION_BACKGROUND_DATA_SETTING_CHANGED);
1017 mContext.sendBroadcast(broadcast);
Robert Greenwalt86e9e552009-07-16 17:21:39 -07001018 }
1019
Robert Greenwaltc03fa502010-02-23 18:58:05 -08001020 /**
1021 * @see ConnectivityManager#getMobileDataEnabled()
1022 */
1023 public boolean getMobileDataEnabled() {
Wink Savillee7982682010-12-07 10:31:02 -08001024 // TODO: This detail should probably be in DataConnectionTracker's
1025 // which is where we store the value and maybe make this
1026 // asynchronous.
Robert Greenwaltc03fa502010-02-23 18:58:05 -08001027 enforceAccessPermission();
1028 boolean retVal = Settings.Secure.getInt(mContext.getContentResolver(),
1029 Settings.Secure.MOBILE_DATA, 1) == 1;
Wink Savilleed9c02b2010-12-03 12:01:38 -08001030 if (DBG) log("getMobileDataEnabled returning " + retVal);
Robert Greenwaltc03fa502010-02-23 18:58:05 -08001031 return retVal;
1032 }
1033
Robert Greenwaltd55a6b42011-03-25 13:09:25 -07001034 public void setDataDependency(int networkType, boolean met) {
1035 enforceChangePermission();
1036 if (DBG) {
1037 log("setDataDependency(" + networkType + ", " + met + ")");
1038 }
1039 mHandler.sendMessage(mHandler.obtainMessage(EVENT_SET_DEPENDENCY_MET,
1040 (met ? ENABLED : DISABLED), networkType));
1041 }
1042
1043 private void handleSetDependencyMet(int networkType, boolean met) {
1044 if (mNetTrackers[networkType] != null) {
1045 if (DBG) {
1046 log("handleSetDependencyMet(" + networkType + ", " + met + ")");
1047 }
1048 mNetTrackers[networkType].setDependencyMet(met);
1049 }
1050 }
1051
Robert Greenwaltc03fa502010-02-23 18:58:05 -08001052 /**
1053 * @see ConnectivityManager#setMobileDataEnabled(boolean)
1054 */
Robert Greenwalt8dcc28b2010-09-23 10:05:56 -07001055 public void setMobileDataEnabled(boolean enabled) {
Robert Greenwaltc03fa502010-02-23 18:58:05 -08001056 enforceChangePermission();
Wink Savilleed9c02b2010-12-03 12:01:38 -08001057 if (DBG) log("setMobileDataEnabled(" + enabled + ")");
Robert Greenwaltc03fa502010-02-23 18:58:05 -08001058
Robert Greenwalt8dcc28b2010-09-23 10:05:56 -07001059 mHandler.sendMessage(mHandler.obtainMessage(EVENT_SET_MOBILE_DATA,
Robert Greenwaltd55a6b42011-03-25 13:09:25 -07001060 (enabled ? ENABLED : DISABLED), 0));
Robert Greenwalt8dcc28b2010-09-23 10:05:56 -07001061 }
1062
1063 private void handleSetMobileData(boolean enabled) {
Wink Savillee7982682010-12-07 10:31:02 -08001064 if (mNetTrackers[ConnectivityManager.TYPE_MOBILE] != null) {
1065 if (DBG) {
1066 Slog.d(TAG, mNetTrackers[ConnectivityManager.TYPE_MOBILE].toString() + enabled);
Robert Greenwaltc03fa502010-02-23 18:58:05 -08001067 }
Wink Savillee7982682010-12-07 10:31:02 -08001068 mNetTrackers[ConnectivityManager.TYPE_MOBILE].setDataEnable(enabled);
Robert Greenwaltc03fa502010-02-23 18:58:05 -08001069 }
1070 }
1071
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001072 private void enforceAccessPermission() {
Robert Greenwalt86e9e552009-07-16 17:21:39 -07001073 mContext.enforceCallingOrSelfPermission(
1074 android.Manifest.permission.ACCESS_NETWORK_STATE,
1075 "ConnectivityService");
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001076 }
1077
1078 private void enforceChangePermission() {
Robert Greenwalt86e9e552009-07-16 17:21:39 -07001079 mContext.enforceCallingOrSelfPermission(
1080 android.Manifest.permission.CHANGE_NETWORK_STATE,
1081 "ConnectivityService");
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001082 }
1083
Robert Greenwaltd0e18ff2010-01-26 11:40:34 -08001084 // TODO Make this a special check when it goes public
1085 private void enforceTetherChangePermission() {
1086 mContext.enforceCallingOrSelfPermission(
1087 android.Manifest.permission.CHANGE_NETWORK_STATE,
1088 "ConnectivityService");
1089 }
1090
Robert Greenwalt2a091d72010-02-11 18:18:40 -08001091 private void enforceTetherAccessPermission() {
1092 mContext.enforceCallingOrSelfPermission(
1093 android.Manifest.permission.ACCESS_NETWORK_STATE,
1094 "ConnectivityService");
1095 }
1096
Robert Greenwalt14f2ef42010-06-15 12:19:37 -07001097 private void enforceConnectivityInternalPermission() {
1098 mContext.enforceCallingOrSelfPermission(
1099 android.Manifest.permission.CONNECTIVITY_INTERNAL,
1100 "ConnectivityService");
1101 }
1102
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001103 /**
Robert Greenwalt86e9e552009-07-16 17:21:39 -07001104 * Handle a {@code DISCONNECTED} event. If this pertains to the non-active
1105 * network, we ignore it. If it is for the active network, we send out a
1106 * broadcast. But first, we check whether it might be possible to connect
1107 * to a different network.
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001108 * @param info the {@code NetworkInfo} for the network
1109 */
1110 private void handleDisconnect(NetworkInfo info) {
1111
Robert Greenwalt42acef32009-08-12 16:08:25 -07001112 int prevNetType = info.getType();
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001113
Robert Greenwalt42acef32009-08-12 16:08:25 -07001114 mNetTrackers[prevNetType].setTeardownRequested(false);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001115 /*
1116 * If the disconnected network is not the active one, then don't report
1117 * this as a loss of connectivity. What probably happened is that we're
1118 * getting the disconnect for a network that we explicitly disabled
1119 * in accordance with network preference policies.
1120 */
Robert Greenwaltd55a6b42011-03-25 13:09:25 -07001121 if (!mNetConfigs[prevNetType].isDefault()) {
Robert Greenwalt42acef32009-08-12 16:08:25 -07001122 List pids = mNetRequestersPids[prevNetType];
1123 for (int i = 0; i<pids.size(); i++) {
1124 Integer pid = (Integer)pids.get(i);
1125 // will remove them because the net's no longer connected
1126 // need to do this now as only now do we know the pids and
1127 // can properly null things that are no longer referenced.
1128 reassessPidDns(pid.intValue(), false);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001129 }
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001130 }
1131
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001132 Intent intent = new Intent(ConnectivityManager.CONNECTIVITY_ACTION);
1133 intent.putExtra(ConnectivityManager.EXTRA_NETWORK_INFO, info);
1134 if (info.isFailover()) {
1135 intent.putExtra(ConnectivityManager.EXTRA_IS_FAILOVER, true);
1136 info.setFailover(false);
1137 }
1138 if (info.getReason() != null) {
1139 intent.putExtra(ConnectivityManager.EXTRA_REASON, info.getReason());
1140 }
1141 if (info.getExtraInfo() != null) {
Robert Greenwalt86e9e552009-07-16 17:21:39 -07001142 intent.putExtra(ConnectivityManager.EXTRA_EXTRA_INFO,
1143 info.getExtraInfo());
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001144 }
Robert Greenwalt42acef32009-08-12 16:08:25 -07001145
Robert Greenwaltd55a6b42011-03-25 13:09:25 -07001146 if (mNetConfigs[prevNetType].isDefault()) {
Robert Greenwaltf21ef7d2011-01-11 13:56:33 -08001147 tryFailover(prevNetType);
1148 if (mActiveDefaultNetwork != -1) {
1149 NetworkInfo switchTo = mNetTrackers[mActiveDefaultNetwork].getNetworkInfo();
Robert Greenwaltcc4b4012010-01-25 17:54:29 -08001150 intent.putExtra(ConnectivityManager.EXTRA_OTHER_NETWORK_INFO, switchTo);
1151 } else {
Robert Greenwalt029be812010-09-20 18:01:43 -07001152 mDefaultInetConditionPublished = 0; // we're not connected anymore
Robert Greenwaltcc4b4012010-01-25 17:54:29 -08001153 intent.putExtra(ConnectivityManager.EXTRA_NO_CONNECTIVITY, true);
1154 }
Robert Greenwaltda03c4e2010-01-20 19:29:41 -08001155 }
Robert Greenwalt029be812010-09-20 18:01:43 -07001156 intent.putExtra(ConnectivityManager.EXTRA_INET_CONDITION, mDefaultInetConditionPublished);
Robert Greenwaltda03c4e2010-01-20 19:29:41 -08001157 // do this before we broadcast the change
Robert Greenwaltc76b8fa2010-07-23 15:46:26 -07001158 handleConnectivityChange(prevNetType);
Robert Greenwaltda03c4e2010-01-20 19:29:41 -08001159
1160 sendStickyBroadcast(intent);
1161 /*
1162 * If the failover network is already connected, then immediately send
1163 * out a followup broadcast indicating successful failover
1164 */
Robert Greenwaltf21ef7d2011-01-11 13:56:33 -08001165 if (mActiveDefaultNetwork != -1) {
1166 sendConnectedBroadcast(mNetTrackers[mActiveDefaultNetwork].getNetworkInfo());
Robert Greenwaltda03c4e2010-01-20 19:29:41 -08001167 }
1168 }
1169
Robert Greenwaltf21ef7d2011-01-11 13:56:33 -08001170 private void tryFailover(int prevNetType) {
Robert Greenwalt42acef32009-08-12 16:08:25 -07001171 /*
Robert Greenwaltbff90182011-01-06 15:41:07 -08001172 * If this is a default network, check if other defaults are available.
1173 * Try to reconnect on all available and let them hash it out when
1174 * more than one connects.
Robert Greenwalt42acef32009-08-12 16:08:25 -07001175 */
Robert Greenwaltd55a6b42011-03-25 13:09:25 -07001176 if (mNetConfigs[prevNetType].isDefault()) {
Robert Greenwalt42acef32009-08-12 16:08:25 -07001177 if (mActiveDefaultNetwork == prevNetType) {
1178 mActiveDefaultNetwork = -1;
1179 }
1180
Robert Greenwaltf21ef7d2011-01-11 13:56:33 -08001181 // don't signal a reconnect for anything lower or equal priority than our
1182 // current connected default
1183 // TODO - don't filter by priority now - nice optimization but risky
1184// int currentPriority = -1;
1185// if (mActiveDefaultNetwork != -1) {
Robert Greenwaltd55a6b42011-03-25 13:09:25 -07001186// currentPriority = mNetConfigs[mActiveDefaultNetwork].mPriority;
Robert Greenwaltf21ef7d2011-01-11 13:56:33 -08001187// }
Robert Greenwaltda03c4e2010-01-20 19:29:41 -08001188 for (int checkType=0; checkType <= ConnectivityManager.MAX_NETWORK_TYPE; checkType++) {
Robert Greenwalt5154ae762009-10-30 14:17:42 -07001189 if (checkType == prevNetType) continue;
Robert Greenwaltd55a6b42011-03-25 13:09:25 -07001190 if (mNetConfigs[checkType] == null) continue;
1191 if (!mNetConfigs[checkType].isDefault()) continue;
Wink Saville9f7a0b22011-01-26 15:43:49 -08001192
1193// Enabling the isAvailable() optimization caused mobile to not get
1194// selected if it was in the middle of error handling. Specifically
1195// a moble connection that took 30 seconds to complete the DEACTIVATE_DATA_CALL
1196// would not be available and we wouldn't get connected to anything.
1197// So removing the isAvailable() optimization below for now. TODO: This
1198// optimization should work and we need to investigate why it doesn't work.
1199// This could be related to how DEACTIVATE_DATA_CALL is reporting its
1200// complete before it is really complete.
1201// if (!mNetTrackers[checkType].isAvailable()) continue;
1202
Robert Greenwaltd55a6b42011-03-25 13:09:25 -07001203// if (currentPriority >= mNetConfigs[checkType].mPriority) continue;
Robert Greenwalt42acef32009-08-12 16:08:25 -07001204
Robert Greenwaltf21ef7d2011-01-11 13:56:33 -08001205 NetworkStateTracker checkTracker = mNetTrackers[checkType];
1206 NetworkInfo checkInfo = checkTracker.getNetworkInfo();
1207 if (!checkInfo.isConnectedOrConnecting() || checkTracker.isTeardownRequested()) {
1208 checkInfo.setFailover(true);
1209 checkTracker.reconnect();
Robert Greenwalt42acef32009-08-12 16:08:25 -07001210 }
Robert Greenwaltf21ef7d2011-01-11 13:56:33 -08001211 if (DBG) log("Attempting to switch to " + checkInfo.getTypeName());
Robert Greenwalt42acef32009-08-12 16:08:25 -07001212 }
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001213 }
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001214 }
1215
1216 private void sendConnectedBroadcast(NetworkInfo info) {
Robert Greenwalt1e9aac22010-09-15 17:36:33 -07001217 sendGeneralBroadcast(info, ConnectivityManager.CONNECTIVITY_ACTION);
1218 }
1219
1220 private void sendInetConditionBroadcast(NetworkInfo info) {
1221 sendGeneralBroadcast(info, ConnectivityManager.INET_CONDITION_ACTION);
1222 }
1223
1224 private void sendGeneralBroadcast(NetworkInfo info, String bcastType) {
1225 Intent intent = new Intent(bcastType);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001226 intent.putExtra(ConnectivityManager.EXTRA_NETWORK_INFO, info);
1227 if (info.isFailover()) {
1228 intent.putExtra(ConnectivityManager.EXTRA_IS_FAILOVER, true);
1229 info.setFailover(false);
1230 }
1231 if (info.getReason() != null) {
1232 intent.putExtra(ConnectivityManager.EXTRA_REASON, info.getReason());
1233 }
1234 if (info.getExtraInfo() != null) {
Robert Greenwalt86e9e552009-07-16 17:21:39 -07001235 intent.putExtra(ConnectivityManager.EXTRA_EXTRA_INFO,
1236 info.getExtraInfo());
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001237 }
Robert Greenwaltd7085fc2010-09-08 15:24:47 -07001238 intent.putExtra(ConnectivityManager.EXTRA_INET_CONDITION, mDefaultInetConditionPublished);
Mike Lockwood0f79b542009-08-14 14:18:49 -04001239 sendStickyBroadcast(intent);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001240 }
1241
1242 /**
1243 * Called when an attempt to fail over to another network has failed.
1244 * @param info the {@link NetworkInfo} for the failed network
1245 */
1246 private void handleConnectionFailure(NetworkInfo info) {
1247 mNetTrackers[info.getType()].setTeardownRequested(false);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001248
Robert Greenwalt42acef32009-08-12 16:08:25 -07001249 String reason = info.getReason();
1250 String extraInfo = info.getExtraInfo();
Robert Greenwalt86e9e552009-07-16 17:21:39 -07001251
Robert Greenwalt572172b2010-10-08 16:35:52 -07001252 String reasonText;
1253 if (reason == null) {
1254 reasonText = ".";
1255 } else {
1256 reasonText = " (" + reason + ").";
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001257 }
Wink Savilleed9c02b2010-12-03 12:01:38 -08001258 loge("Attempt to connect to " + info.getTypeName() + " failed" + reasonText);
Robert Greenwalt42acef32009-08-12 16:08:25 -07001259
1260 Intent intent = new Intent(ConnectivityManager.CONNECTIVITY_ACTION);
1261 intent.putExtra(ConnectivityManager.EXTRA_NETWORK_INFO, info);
1262 if (getActiveNetworkInfo() == null) {
1263 intent.putExtra(ConnectivityManager.EXTRA_NO_CONNECTIVITY, true);
1264 }
1265 if (reason != null) {
1266 intent.putExtra(ConnectivityManager.EXTRA_REASON, reason);
1267 }
1268 if (extraInfo != null) {
1269 intent.putExtra(ConnectivityManager.EXTRA_EXTRA_INFO, extraInfo);
1270 }
1271 if (info.isFailover()) {
1272 intent.putExtra(ConnectivityManager.EXTRA_IS_FAILOVER, true);
1273 info.setFailover(false);
1274 }
Robert Greenwaltda03c4e2010-01-20 19:29:41 -08001275
Robert Greenwaltd55a6b42011-03-25 13:09:25 -07001276 if (mNetConfigs[info.getType()].isDefault()) {
Robert Greenwaltf21ef7d2011-01-11 13:56:33 -08001277 tryFailover(info.getType());
1278 if (mActiveDefaultNetwork != -1) {
1279 NetworkInfo switchTo = mNetTrackers[mActiveDefaultNetwork].getNetworkInfo();
Robert Greenwaltcc4b4012010-01-25 17:54:29 -08001280 intent.putExtra(ConnectivityManager.EXTRA_OTHER_NETWORK_INFO, switchTo);
1281 } else {
Robert Greenwalt029be812010-09-20 18:01:43 -07001282 mDefaultInetConditionPublished = 0;
Robert Greenwaltcc4b4012010-01-25 17:54:29 -08001283 intent.putExtra(ConnectivityManager.EXTRA_NO_CONNECTIVITY, true);
1284 }
Robert Greenwaltda03c4e2010-01-20 19:29:41 -08001285 }
Robert Greenwaltcc4b4012010-01-25 17:54:29 -08001286
Robert Greenwalt029be812010-09-20 18:01:43 -07001287 intent.putExtra(ConnectivityManager.EXTRA_INET_CONDITION, mDefaultInetConditionPublished);
Mike Lockwood0f79b542009-08-14 14:18:49 -04001288 sendStickyBroadcast(intent);
Robert Greenwaltda03c4e2010-01-20 19:29:41 -08001289 /*
1290 * If the failover network is already connected, then immediately send
1291 * out a followup broadcast indicating successful failover
1292 */
Robert Greenwaltf21ef7d2011-01-11 13:56:33 -08001293 if (mActiveDefaultNetwork != -1) {
1294 sendConnectedBroadcast(mNetTrackers[mActiveDefaultNetwork].getNetworkInfo());
Robert Greenwaltda03c4e2010-01-20 19:29:41 -08001295 }
Mike Lockwood0f79b542009-08-14 14:18:49 -04001296 }
1297
1298 private void sendStickyBroadcast(Intent intent) {
1299 synchronized(this) {
Dianne Hackborn1c633fc2009-12-08 19:45:14 -08001300 if (!mSystemReady) {
1301 mInitialBroadcast = new Intent(intent);
Mike Lockwood0f79b542009-08-14 14:18:49 -04001302 }
Dianne Hackborn1c633fc2009-12-08 19:45:14 -08001303 intent.addFlags(Intent.FLAG_RECEIVER_REGISTERED_ONLY_BEFORE_BOOT);
1304 mContext.sendStickyBroadcast(intent);
Mike Lockwood0f79b542009-08-14 14:18:49 -04001305 }
1306 }
1307
1308 void systemReady() {
Robert Greenwalt59b1a4e2011-05-10 15:05:02 -07001309 IBinder b = ServiceManager.getService(Context.NETWORKMANAGEMENT_SERVICE);
1310 mNetd = INetworkManagementService.Stub.asInterface(b);
1311
Mike Lockwood0f79b542009-08-14 14:18:49 -04001312 synchronized(this) {
1313 mSystemReady = true;
Dianne Hackborn1c633fc2009-12-08 19:45:14 -08001314 if (mInitialBroadcast != null) {
1315 mContext.sendStickyBroadcast(mInitialBroadcast);
1316 mInitialBroadcast = null;
Mike Lockwood0f79b542009-08-14 14:18:49 -04001317 }
1318 }
Robert Greenwalt434203a2010-10-11 16:00:27 -07001319 // load the global proxy at startup
1320 mHandler.sendMessage(mHandler.obtainMessage(EVENT_APPLY_GLOBAL_HTTP_PROXY));
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001321 }
1322
1323 private void handleConnect(NetworkInfo info) {
Robert Greenwalt42acef32009-08-12 16:08:25 -07001324 int type = info.getType();
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001325
1326 // snapshot isFailover, because sendConnectedBroadcast() resets it
1327 boolean isFailover = info.isFailover();
Robert Greenwalt42acef32009-08-12 16:08:25 -07001328 NetworkStateTracker thisNet = mNetTrackers[type];
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001329
Robert Greenwalt42acef32009-08-12 16:08:25 -07001330 // if this is a default net and other default is running
1331 // kill the one not preferred
Robert Greenwaltd55a6b42011-03-25 13:09:25 -07001332 if (mNetConfigs[type].isDefault()) {
Robert Greenwalt42acef32009-08-12 16:08:25 -07001333 if (mActiveDefaultNetwork != -1 && mActiveDefaultNetwork != type) {
1334 if ((type != mNetworkPreference &&
Wink Saville975c8482011-04-07 14:23:45 -07001335 mNetConfigs[mActiveDefaultNetwork].priority >
1336 mNetConfigs[type].priority) ||
Robert Greenwalt42acef32009-08-12 16:08:25 -07001337 mNetworkPreference == mActiveDefaultNetwork) {
1338 // don't accept this one
Wink Savilleed9c02b2010-12-03 12:01:38 -08001339 if (DBG) {
1340 log("Not broadcasting CONNECT_ACTION " +
Robert Greenwalt42acef32009-08-12 16:08:25 -07001341 "to torn down network " + info.getTypeName());
Wink Savilleed9c02b2010-12-03 12:01:38 -08001342 }
Robert Greenwalt42acef32009-08-12 16:08:25 -07001343 teardown(thisNet);
1344 return;
1345 } else {
1346 // tear down the other
1347 NetworkStateTracker otherNet =
1348 mNetTrackers[mActiveDefaultNetwork];
Wink Savilleed9c02b2010-12-03 12:01:38 -08001349 if (DBG) {
1350 log("Policy requires " + otherNet.getNetworkInfo().getTypeName() +
Robert Greenwalt42acef32009-08-12 16:08:25 -07001351 " teardown");
Wink Savilleed9c02b2010-12-03 12:01:38 -08001352 }
Robert Greenwalt42acef32009-08-12 16:08:25 -07001353 if (!teardown(otherNet)) {
Wink Savilleed9c02b2010-12-03 12:01:38 -08001354 loge("Network declined teardown request");
Robert Greenwalt42acef32009-08-12 16:08:25 -07001355 return;
1356 }
Robert Greenwalt14f2ef42010-06-15 12:19:37 -07001357 }
1358 }
1359 synchronized (ConnectivityService.this) {
1360 // have a new default network, release the transition wakelock in a second
1361 // if it's held. The second pause is to allow apps to reconnect over the
1362 // new network
1363 if (mNetTransitionWakeLock.isHeld()) {
1364 mHandler.sendMessageDelayed(mHandler.obtainMessage(
Robert Greenwaltf3331232010-09-24 14:32:21 -07001365 EVENT_CLEAR_NET_TRANSITION_WAKELOCK,
Robert Greenwalt14f2ef42010-06-15 12:19:37 -07001366 mNetTransitionWakeLockSerialNumber, 0),
1367 1000);
Robert Greenwalt42acef32009-08-12 16:08:25 -07001368 }
1369 }
1370 mActiveDefaultNetwork = type;
Robert Greenwaltd7085fc2010-09-08 15:24:47 -07001371 // this will cause us to come up initially as unconnected and switching
1372 // to connected after our normal pause unless somebody reports us as reall
1373 // disconnected
1374 mDefaultInetConditionPublished = 0;
1375 mDefaultConnectionSequence++;
1376 mInetConditionChangeInFlight = false;
1377 // Don't do this - if we never sign in stay, grey
1378 //reportNetworkCondition(mActiveDefaultNetwork, 100);
Robert Greenwalt42acef32009-08-12 16:08:25 -07001379 }
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001380 thisNet.setTeardownRequested(false);
Irfan Sheriffd649c122010-06-09 15:39:36 -07001381 updateNetworkSettings(thisNet);
Robert Greenwaltc76b8fa2010-07-23 15:46:26 -07001382 handleConnectivityChange(type);
Robert Greenwalt42acef32009-08-12 16:08:25 -07001383 sendConnectedBroadcast(info);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001384 }
1385
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001386 /**
Robert Greenwaltc76b8fa2010-07-23 15:46:26 -07001387 * After a change in the connectivity state of a network. We're mainly
1388 * concerned with making sure that the list of DNS servers is set up
1389 * according to which networks are connected, and ensuring that the
1390 * right routing table entries exist.
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001391 */
Robert Greenwaltc76b8fa2010-07-23 15:46:26 -07001392 private void handleConnectivityChange(int netType) {
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001393 /*
Robert Greenwalt42acef32009-08-12 16:08:25 -07001394 * If a non-default network is enabled, add the host routes that
Robert Greenwaltc76b8fa2010-07-23 15:46:26 -07001395 * will allow it's DNS servers to be accessed.
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001396 */
Robert Greenwaltc76b8fa2010-07-23 15:46:26 -07001397 handleDnsConfigurationChange(netType);
Robert Greenwalt42acef32009-08-12 16:08:25 -07001398
Robert Greenwaltc76b8fa2010-07-23 15:46:26 -07001399 if (mNetTrackers[netType].getNetworkInfo().isConnected()) {
Robert Greenwaltd55a6b42011-03-25 13:09:25 -07001400 if (mNetConfigs[netType].isDefault()) {
Robert Greenwalt434203a2010-10-11 16:00:27 -07001401 handleApplyDefaultProxy(netType);
Robert Greenwaltc76b8fa2010-07-23 15:46:26 -07001402 addDefaultRoute(mNetTrackers[netType]);
Robert Greenwalt42acef32009-08-12 16:08:25 -07001403 } else {
Robert Greenwaltc76b8fa2010-07-23 15:46:26 -07001404 addPrivateDnsRoutes(mNetTrackers[netType]);
1405 }
Kazuhiro Ondo01758e82011-04-30 20:10:57 -05001406
1407 /** Notify TetheringService if interface name has been changed. */
1408 if (TextUtils.equals(mNetTrackers[netType].getNetworkInfo().getReason(),
1409 Phone.REASON_LINK_PROPERTIES_CHANGED)) {
1410 handleTetherIfaceChange(netType);
1411 }
Robert Greenwaltc76b8fa2010-07-23 15:46:26 -07001412 } else {
Robert Greenwaltd55a6b42011-03-25 13:09:25 -07001413 if (mNetConfigs[netType].isDefault()) {
Robert Greenwaltc76b8fa2010-07-23 15:46:26 -07001414 removeDefaultRoute(mNetTrackers[netType]);
1415 } else {
1416 removePrivateDnsRoutes(mNetTrackers[netType]);
Robert Greenwalt42acef32009-08-12 16:08:25 -07001417 }
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001418 }
1419 }
1420
Irfan Sheriffd649c122010-06-09 15:39:36 -07001421 private void addPrivateDnsRoutes(NetworkStateTracker nt) {
Irfan Sheriffd649c122010-06-09 15:39:36 -07001422 boolean privateDnsRouteSet = nt.isPrivateDnsRouteSet();
Robert Greenwalt37e65eb2010-08-30 10:56:47 -07001423 LinkProperties p = nt.getLinkProperties();
Robert Greenwalt47f69fe2010-06-15 15:43:39 -07001424 if (p == null) return;
1425 String interfaceName = p.getInterfaceName();
Irfan Sheriffd649c122010-06-09 15:39:36 -07001426
1427 if (DBG) {
Wink Savilleed9c02b2010-12-03 12:01:38 -08001428 log("addPrivateDnsRoutes for " + nt +
Irfan Sheriffd649c122010-06-09 15:39:36 -07001429 "(" + interfaceName + ") - mPrivateDnsRouteSet = " + privateDnsRouteSet);
1430 }
Irfan Sheriffd649c122010-06-09 15:39:36 -07001431 if (interfaceName != null && !privateDnsRouteSet) {
Robert Greenwalt47f69fe2010-06-15 15:43:39 -07001432 Collection<InetAddress> dnsList = p.getDnses();
1433 for (InetAddress dns : dnsList) {
Robert Greenwaltf43396c2011-05-06 17:10:53 -07001434 addHostRoute(nt, dns, 0);
Irfan Sheriffd649c122010-06-09 15:39:36 -07001435 }
1436 nt.privateDnsRouteSet(true);
1437 }
1438 }
1439
1440 private void removePrivateDnsRoutes(NetworkStateTracker nt) {
Robert Greenwalt37e65eb2010-08-30 10:56:47 -07001441 LinkProperties p = nt.getLinkProperties();
Robert Greenwalt47f69fe2010-06-15 15:43:39 -07001442 if (p == null) return;
1443 String interfaceName = p.getInterfaceName();
Irfan Sheriffd649c122010-06-09 15:39:36 -07001444 boolean privateDnsRouteSet = nt.isPrivateDnsRouteSet();
1445 if (interfaceName != null && privateDnsRouteSet) {
1446 if (DBG) {
Wink Savilleed9c02b2010-12-03 12:01:38 -08001447 log("removePrivateDnsRoutes for " + nt.getNetworkInfo().getTypeName() +
Irfan Sheriffd649c122010-06-09 15:39:36 -07001448 " (" + interfaceName + ")");
1449 }
Robert Greenwalt59b1a4e2011-05-10 15:05:02 -07001450
1451 Collection<InetAddress> dnsList = p.getDnses();
1452 for (InetAddress dns : dnsList) {
1453 if (DBG) log(" removing " + dns);
1454 RouteInfo route = RouteInfo.makeHostRoute(dns);
1455 try {
1456 mNetd.removeRoute(interfaceName, route);
1457 } catch (Exception ex) {
1458 loge("error (" + ex + ") removing dns route " + route);
1459 }
1460 }
Irfan Sheriffd649c122010-06-09 15:39:36 -07001461 nt.privateDnsRouteSet(false);
1462 }
1463 }
1464
Irfan Sheriffd649c122010-06-09 15:39:36 -07001465
1466 private void addDefaultRoute(NetworkStateTracker nt) {
Robert Greenwalt37e65eb2010-08-30 10:56:47 -07001467 LinkProperties p = nt.getLinkProperties();
Robert Greenwalt47f69fe2010-06-15 15:43:39 -07001468 if (p == null) return;
1469 String interfaceName = p.getInterfaceName();
Robert Greenwalt992564e2011-02-09 13:56:06 -08001470 if (TextUtils.isEmpty(interfaceName)) return;
Irfan Sheriffd649c122010-06-09 15:39:36 -07001471
Robert Greenwalt59b1a4e2011-05-10 15:05:02 -07001472 for (RouteInfo route : p.getRoutes()) {
Robert Greenwaltaa70f102011-04-28 14:28:50 -07001473 //TODO - handle non-default routes
1474 if (route.isDefaultRoute()) {
Robert Greenwalt59b1a4e2011-05-10 15:05:02 -07001475 if (DBG) log("adding default route " + route);
Robert Greenwaltaa70f102011-04-28 14:28:50 -07001476 InetAddress gateway = route.getGateway();
Robert Greenwalt59b1a4e2011-05-10 15:05:02 -07001477 if (addHostRoute(nt, gateway, 0)) {
1478 try {
1479 mNetd.addRoute(interfaceName, route);
1480 } catch (Exception e) {
1481 loge("error adding default route " + route);
1482 continue;
1483 }
Robert Greenwaltaa70f102011-04-28 14:28:50 -07001484 if (DBG) {
1485 NetworkInfo networkInfo = nt.getNetworkInfo();
1486 log("addDefaultRoute for " + networkInfo.getTypeName() +
1487 " (" + interfaceName + "), GatewayAddr=" +
1488 gateway.getHostAddress());
1489 }
Robert Greenwalt59b1a4e2011-05-10 15:05:02 -07001490 } else {
1491 loge("error adding host route for default route " + route);
Robert Greenwaltedcb4f92011-03-22 18:47:42 -07001492 }
Irfan Sheriffd649c122010-06-09 15:39:36 -07001493 }
Irfan Sheriffd649c122010-06-09 15:39:36 -07001494 }
1495 }
1496
1497
1498 public void removeDefaultRoute(NetworkStateTracker nt) {
Robert Greenwalt37e65eb2010-08-30 10:56:47 -07001499 LinkProperties p = nt.getLinkProperties();
Robert Greenwalt47f69fe2010-06-15 15:43:39 -07001500 if (p == null) return;
1501 String interfaceName = p.getInterfaceName();
Irfan Sheriffd649c122010-06-09 15:39:36 -07001502
Robert Greenwalt59b1a4e2011-05-10 15:05:02 -07001503 if (interfaceName == null) return;
1504
1505 for (RouteInfo route : p.getRoutes()) {
1506 //TODO - handle non-default routes
1507 if (route.isDefaultRoute()) {
1508 try {
1509 mNetd.removeRoute(interfaceName, route);
1510 } catch (Exception ex) {
1511 loge("error (" + ex + ") removing default route " + route);
1512 continue;
1513 }
Robert Greenwaltedcb4f92011-03-22 18:47:42 -07001514 if (DBG) {
1515 NetworkInfo networkInfo = nt.getNetworkInfo();
1516 log("removeDefaultRoute for " + networkInfo.getTypeName() + " (" +
1517 interfaceName + ")");
1518 }
Irfan Sheriffd649c122010-06-09 15:39:36 -07001519 }
Irfan Sheriffd649c122010-06-09 15:39:36 -07001520 }
1521 }
1522
1523 /**
1524 * Reads the network specific TCP buffer sizes from SystemProperties
1525 * net.tcp.buffersize.[default|wifi|umts|edge|gprs] and set them for system
1526 * wide use
1527 */
1528 public void updateNetworkSettings(NetworkStateTracker nt) {
1529 String key = nt.getTcpBufferSizesPropName();
1530 String bufferSizes = SystemProperties.get(key);
1531
1532 if (bufferSizes.length() == 0) {
Wink Savilleed9c02b2010-12-03 12:01:38 -08001533 loge(key + " not found in system properties. Using defaults");
Irfan Sheriffd649c122010-06-09 15:39:36 -07001534
1535 // Setting to default values so we won't be stuck to previous values
1536 key = "net.tcp.buffersize.default";
1537 bufferSizes = SystemProperties.get(key);
1538 }
1539
1540 // Set values in kernel
1541 if (bufferSizes.length() != 0) {
1542 if (DBG) {
Wink Savilleed9c02b2010-12-03 12:01:38 -08001543 log("Setting TCP values: [" + bufferSizes
Irfan Sheriffd649c122010-06-09 15:39:36 -07001544 + "] which comes from [" + key + "]");
1545 }
1546 setBufferSize(bufferSizes);
1547 }
1548 }
1549
1550 /**
1551 * Writes TCP buffer sizes to /sys/kernel/ipv4/tcp_[r/w]mem_[min/def/max]
1552 * which maps to /proc/sys/net/ipv4/tcp_rmem and tcpwmem
1553 *
1554 * @param bufferSizes in the format of "readMin, readInitial, readMax,
1555 * writeMin, writeInitial, writeMax"
1556 */
1557 private void setBufferSize(String bufferSizes) {
1558 try {
1559 String[] values = bufferSizes.split(",");
1560
1561 if (values.length == 6) {
1562 final String prefix = "/sys/kernel/ipv4/tcp_";
1563 stringToFile(prefix + "rmem_min", values[0]);
1564 stringToFile(prefix + "rmem_def", values[1]);
1565 stringToFile(prefix + "rmem_max", values[2]);
1566 stringToFile(prefix + "wmem_min", values[3]);
1567 stringToFile(prefix + "wmem_def", values[4]);
1568 stringToFile(prefix + "wmem_max", values[5]);
1569 } else {
Wink Savilleed9c02b2010-12-03 12:01:38 -08001570 loge("Invalid buffersize string: " + bufferSizes);
Irfan Sheriffd649c122010-06-09 15:39:36 -07001571 }
1572 } catch (IOException e) {
Wink Savilleed9c02b2010-12-03 12:01:38 -08001573 loge("Can't set tcp buffer sizes:" + e);
Irfan Sheriffd649c122010-06-09 15:39:36 -07001574 }
1575 }
1576
1577 /**
1578 * Writes string to file. Basically same as "echo -n $string > $filename"
1579 *
1580 * @param filename
1581 * @param string
1582 * @throws IOException
1583 */
1584 private void stringToFile(String filename, String string) throws IOException {
1585 FileWriter out = new FileWriter(filename);
1586 try {
1587 out.write(string);
1588 } finally {
1589 out.close();
1590 }
1591 }
1592
1593
Robert Greenwalt42acef32009-08-12 16:08:25 -07001594 /**
1595 * Adjust the per-process dns entries (net.dns<x>.<pid>) based
1596 * on the highest priority active net which this process requested.
1597 * If there aren't any, clear it out
1598 */
1599 private void reassessPidDns(int myPid, boolean doBump)
1600 {
Wink Savilleed9c02b2010-12-03 12:01:38 -08001601 if (DBG) log("reassessPidDns for pid " + myPid);
Robert Greenwalt42acef32009-08-12 16:08:25 -07001602 for(int i : mPriorityList) {
Robert Greenwaltd55a6b42011-03-25 13:09:25 -07001603 if (mNetConfigs[i].isDefault()) {
Robert Greenwalt42acef32009-08-12 16:08:25 -07001604 continue;
1605 }
1606 NetworkStateTracker nt = mNetTrackers[i];
Robert Greenwalt86e9e552009-07-16 17:21:39 -07001607 if (nt.getNetworkInfo().isConnected() &&
1608 !nt.isTeardownRequested()) {
Robert Greenwalt37e65eb2010-08-30 10:56:47 -07001609 LinkProperties p = nt.getLinkProperties();
Robert Greenwalt47f69fe2010-06-15 15:43:39 -07001610 if (p == null) continue;
Robert Greenwalt42acef32009-08-12 16:08:25 -07001611 List pids = mNetRequestersPids[i];
1612 for (int j=0; j<pids.size(); j++) {
1613 Integer pid = (Integer)pids.get(j);
1614 if (pid.intValue() == myPid) {
Robert Greenwalt47f69fe2010-06-15 15:43:39 -07001615 Collection<InetAddress> dnses = p.getDnses();
1616 writePidDns(dnses, myPid);
Robert Greenwalt42acef32009-08-12 16:08:25 -07001617 if (doBump) {
1618 bumpDns();
1619 }
1620 return;
1621 }
1622 }
1623 }
1624 }
1625 // nothing found - delete
1626 for (int i = 1; ; i++) {
1627 String prop = "net.dns" + i + "." + myPid;
1628 if (SystemProperties.get(prop).length() == 0) {
1629 if (doBump) {
1630 bumpDns();
1631 }
1632 return;
1633 }
1634 SystemProperties.set(prop, "");
1635 }
1636 }
1637
Robert Greenwalt10398722010-12-17 15:20:36 -08001638 // return true if results in a change
1639 private boolean writePidDns(Collection <InetAddress> dnses, int pid) {
Robert Greenwalt42acef32009-08-12 16:08:25 -07001640 int j = 1;
Robert Greenwalt10398722010-12-17 15:20:36 -08001641 boolean changed = false;
Robert Greenwalt47f69fe2010-06-15 15:43:39 -07001642 for (InetAddress dns : dnses) {
Robert Greenwalt10398722010-12-17 15:20:36 -08001643 String dnsString = dns.getHostAddress();
1644 if (changed || !dnsString.equals(SystemProperties.get("net.dns" + j + "." + pid))) {
1645 changed = true;
1646 SystemProperties.set("net.dns" + j++ + "." + pid, dns.getHostAddress());
1647 }
Robert Greenwalt42acef32009-08-12 16:08:25 -07001648 }
Robert Greenwalt10398722010-12-17 15:20:36 -08001649 return changed;
Robert Greenwalt42acef32009-08-12 16:08:25 -07001650 }
1651
1652 private void bumpDns() {
1653 /*
1654 * Bump the property that tells the name resolver library to reread
1655 * the DNS server list from the properties.
1656 */
1657 String propVal = SystemProperties.get("net.dnschange");
1658 int n = 0;
1659 if (propVal.length() != 0) {
1660 try {
1661 n = Integer.parseInt(propVal);
1662 } catch (NumberFormatException e) {}
1663 }
1664 SystemProperties.set("net.dnschange", "" + (n+1));
Robert Greenwalt03595d02010-11-02 14:08:23 -07001665 /*
1666 * Tell the VMs to toss their DNS caches
1667 */
1668 Intent intent = new Intent(Intent.ACTION_CLEAR_DNS_CACHE);
1669 intent.addFlags(Intent.FLAG_RECEIVER_REPLACE_PENDING);
Stan Chesnutt3d1db862011-01-05 17:14:03 -08001670 /*
1671 * Connectivity events can happen before boot has completed ...
1672 */
1673 intent.addFlags(Intent.FLAG_RECEIVER_REGISTERED_ONLY_BEFORE_BOOT);
Robert Greenwalt03595d02010-11-02 14:08:23 -07001674 mContext.sendBroadcast(intent);
Robert Greenwalt42acef32009-08-12 16:08:25 -07001675 }
1676
Robert Greenwaltc76b8fa2010-07-23 15:46:26 -07001677 private void handleDnsConfigurationChange(int netType) {
Robert Greenwalt42acef32009-08-12 16:08:25 -07001678 // add default net's dns entries
Robert Greenwaltc76b8fa2010-07-23 15:46:26 -07001679 NetworkStateTracker nt = mNetTrackers[netType];
1680 if (nt != null && nt.getNetworkInfo().isConnected() && !nt.isTeardownRequested()) {
Robert Greenwalt37e65eb2010-08-30 10:56:47 -07001681 LinkProperties p = nt.getLinkProperties();
Robert Greenwaltc76b8fa2010-07-23 15:46:26 -07001682 if (p == null) return;
1683 Collection<InetAddress> dnses = p.getDnses();
Robert Greenwalt10398722010-12-17 15:20:36 -08001684 boolean changed = false;
Robert Greenwaltd55a6b42011-03-25 13:09:25 -07001685 if (mNetConfigs[netType].isDefault()) {
Robert Greenwaltc76b8fa2010-07-23 15:46:26 -07001686 int j = 1;
Robert Greenwalte90aa5e2010-09-01 11:34:05 -07001687 if (dnses.size() == 0 && mDefaultDns != null) {
Robert Greenwalt10398722010-12-17 15:20:36 -08001688 String dnsString = mDefaultDns.getHostAddress();
1689 if (!dnsString.equals(SystemProperties.get("net.dns1"))) {
1690 if (DBG) {
1691 log("no dns provided - using " + dnsString);
1692 }
1693 changed = true;
1694 SystemProperties.set("net.dns1", dnsString);
Robert Greenwalt42acef32009-08-12 16:08:25 -07001695 }
Robert Greenwalte90aa5e2010-09-01 11:34:05 -07001696 j++;
1697 } else {
1698 for (InetAddress dns : dnses) {
Robert Greenwalt10398722010-12-17 15:20:36 -08001699 String dnsString = dns.getHostAddress();
1700 if (!changed && dnsString.equals(SystemProperties.get("net.dns" + j))) {
1701 j++;
1702 continue;
1703 }
Robert Greenwalte90aa5e2010-09-01 11:34:05 -07001704 if (DBG) {
Wink Savilleed9c02b2010-12-03 12:01:38 -08001705 log("adding dns " + dns + " for " +
Robert Greenwalte90aa5e2010-09-01 11:34:05 -07001706 nt.getNetworkInfo().getTypeName());
1707 }
Robert Greenwalt10398722010-12-17 15:20:36 -08001708 changed = true;
1709 SystemProperties.set("net.dns" + j++, dnsString);
Robert Greenwalte90aa5e2010-09-01 11:34:05 -07001710 }
Robert Greenwaltc76b8fa2010-07-23 15:46:26 -07001711 }
1712 for (int k=j ; k<mNumDnsEntries; k++) {
Robert Greenwalt10398722010-12-17 15:20:36 -08001713 if (changed || !TextUtils.isEmpty(SystemProperties.get("net.dns" + k))) {
1714 if (DBG) log("erasing net.dns" + k);
1715 changed = true;
1716 SystemProperties.set("net.dns" + k, "");
1717 }
Robert Greenwaltc76b8fa2010-07-23 15:46:26 -07001718 }
1719 mNumDnsEntries = j;
1720 } else {
1721 // set per-pid dns for attached secondary nets
1722 List pids = mNetRequestersPids[netType];
1723 for (int y=0; y< pids.size(); y++) {
1724 Integer pid = (Integer)pids.get(y);
Robert Greenwalt10398722010-12-17 15:20:36 -08001725 changed = writePidDns(dnses, pid.intValue());
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001726 }
1727 }
Robert Greenwalt10398722010-12-17 15:20:36 -08001728 if (changed) bumpDns();
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001729 }
Robert Greenwalt42acef32009-08-12 16:08:25 -07001730 }
1731
Robert Greenwaltf2102f72011-05-03 19:02:44 -07001732 private int getRestoreDefaultNetworkDelay(int networkType) {
Robert Greenwalt42acef32009-08-12 16:08:25 -07001733 String restoreDefaultNetworkDelayStr = SystemProperties.get(
1734 NETWORK_RESTORE_DELAY_PROP_NAME);
1735 if(restoreDefaultNetworkDelayStr != null &&
1736 restoreDefaultNetworkDelayStr.length() != 0) {
1737 try {
1738 return Integer.valueOf(restoreDefaultNetworkDelayStr);
1739 } catch (NumberFormatException e) {
1740 }
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001741 }
Robert Greenwaltf2102f72011-05-03 19:02:44 -07001742 // if the system property isn't set, use the value for the apn type
1743 int ret = RESTORE_DEFAULT_NETWORK_DELAY;
1744
1745 if ((networkType <= ConnectivityManager.MAX_NETWORK_TYPE) &&
1746 (mNetConfigs[networkType] != null)) {
1747 ret = mNetConfigs[networkType].restoreTime;
1748 }
1749 return ret;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001750 }
1751
1752 @Override
1753 protected void dump(FileDescriptor fd, PrintWriter pw, String[] args) {
Robert Greenwalt86e9e552009-07-16 17:21:39 -07001754 if (mContext.checkCallingOrSelfPermission(
1755 android.Manifest.permission.DUMP)
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001756 != PackageManager.PERMISSION_GRANTED) {
Robert Greenwalt86e9e552009-07-16 17:21:39 -07001757 pw.println("Permission Denial: can't dump ConnectivityService " +
1758 "from from pid=" + Binder.getCallingPid() + ", uid=" +
1759 Binder.getCallingUid());
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001760 return;
1761 }
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001762 pw.println();
1763 for (NetworkStateTracker nst : mNetTrackers) {
Robert Greenwaltb9285352009-12-21 18:24:07 -08001764 if (nst != null) {
1765 if (nst.getNetworkInfo().isConnected()) {
1766 pw.println("Active network: " + nst.getNetworkInfo().
1767 getTypeName());
1768 }
1769 pw.println(nst.getNetworkInfo());
1770 pw.println(nst);
1771 pw.println();
Robert Greenwalt42acef32009-08-12 16:08:25 -07001772 }
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001773 }
Robert Greenwaltb9285352009-12-21 18:24:07 -08001774
1775 pw.println("Network Requester Pids:");
1776 for (int net : mPriorityList) {
1777 String pidString = net + ": ";
1778 for (Object pid : mNetRequestersPids[net]) {
1779 pidString = pidString + pid.toString() + ", ";
1780 }
1781 pw.println(pidString);
1782 }
1783 pw.println();
1784
1785 pw.println("FeatureUsers:");
1786 for (Object requester : mFeatureUsers) {
1787 pw.println(requester.toString());
1788 }
1789 pw.println();
Robert Greenwalt2a091d72010-02-11 18:18:40 -08001790
Robert Greenwalt14f2ef42010-06-15 12:19:37 -07001791 synchronized (this) {
1792 pw.println("NetworkTranstionWakeLock is currently " +
1793 (mNetTransitionWakeLock.isHeld() ? "" : "not ") + "held.");
1794 pw.println("It was last requested for "+mNetTransitionWakeLockCausedBy);
1795 }
1796 pw.println();
1797
Robert Greenwalt2a091d72010-02-11 18:18:40 -08001798 mTethering.dump(fd, pw, args);
Robert Greenwalt4e8dfef2010-09-20 14:35:25 -07001799
1800 if (mInetLog != null) {
1801 pw.println();
1802 pw.println("Inet condition reports:");
1803 for(int i = 0; i < mInetLog.size(); i++) {
1804 pw.println(mInetLog.get(i));
1805 }
1806 }
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001807 }
1808
Robert Greenwalt42acef32009-08-12 16:08:25 -07001809 // must be stateless - things change under us.
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001810 private class MyHandler extends Handler {
Wink Savillebb08caf2010-09-02 19:23:52 -07001811 public MyHandler(Looper looper) {
1812 super(looper);
1813 }
1814
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001815 @Override
1816 public void handleMessage(Message msg) {
1817 NetworkInfo info;
1818 switch (msg.what) {
1819 case NetworkStateTracker.EVENT_STATE_CHANGED:
1820 info = (NetworkInfo) msg.obj;
Robert Greenwalt511288a2009-12-07 11:33:18 -08001821 int type = info.getType();
1822 NetworkInfo.State state = info.getState();
Robert Greenwalt511288a2009-12-07 11:33:18 -08001823
Wink Savilleed9c02b2010-12-03 12:01:38 -08001824 if (DBG) log("ConnectivityChange for " +
Robert Greenwalt86e9e552009-07-16 17:21:39 -07001825 info.getTypeName() + ": " +
Robert Greenwalt511288a2009-12-07 11:33:18 -08001826 state + "/" + info.getDetailedState());
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001827
1828 // Connectivity state changed:
1829 // [31-13] Reserved for future use
Robert Greenwalt86e9e552009-07-16 17:21:39 -07001830 // [12-9] Network subtype (for mobile network, as defined
1831 // by TelephonyManager)
1832 // [8-3] Detailed state ordinal (as defined by
1833 // NetworkInfo.DetailedState)
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001834 // [2-0] Network type (as defined by ConnectivityManager)
1835 int eventLogParam = (info.getType() & 0x7) |
1836 ((info.getDetailedState().ordinal() & 0x3f) << 3) |
1837 (info.getSubtype() << 9);
Doug Zongkerab5c49c2009-12-04 10:31:43 -08001838 EventLog.writeEvent(EventLogTags.CONNECTIVITY_STATE_CHANGED,
Robert Greenwalt86e9e552009-07-16 17:21:39 -07001839 eventLogParam);
1840
1841 if (info.getDetailedState() ==
1842 NetworkInfo.DetailedState.FAILED) {
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001843 handleConnectionFailure(info);
Robert Greenwalt511288a2009-12-07 11:33:18 -08001844 } else if (state == NetworkInfo.State.DISCONNECTED) {
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001845 handleDisconnect(info);
Robert Greenwalt511288a2009-12-07 11:33:18 -08001846 } else if (state == NetworkInfo.State.SUSPENDED) {
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001847 // TODO: need to think this over.
Robert Greenwalt86e9e552009-07-16 17:21:39 -07001848 // the logic here is, handle SUSPENDED the same as
1849 // DISCONNECTED. The only difference being we are
1850 // broadcasting an intent with NetworkInfo that's
1851 // suspended. This allows the applications an
1852 // opportunity to handle DISCONNECTED and SUSPENDED
1853 // differently, or not.
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001854 handleDisconnect(info);
Robert Greenwalt511288a2009-12-07 11:33:18 -08001855 } else if (state == NetworkInfo.State.CONNECTED) {
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001856 handleConnect(info);
1857 }
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001858 break;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001859 case NetworkStateTracker.EVENT_CONFIGURATION_CHANGED:
Robert Greenwaltc76b8fa2010-07-23 15:46:26 -07001860 info = (NetworkInfo) msg.obj;
Robert Greenwaltd55a6b42011-03-25 13:09:25 -07001861 handleConnectivityChange(info.getType());
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001862 break;
Robert Greenwaltf3331232010-09-24 14:32:21 -07001863 case EVENT_CLEAR_NET_TRANSITION_WAKELOCK:
Robert Greenwalt14f2ef42010-06-15 12:19:37 -07001864 String causedBy = null;
1865 synchronized (ConnectivityService.this) {
1866 if (msg.arg1 == mNetTransitionWakeLockSerialNumber &&
1867 mNetTransitionWakeLock.isHeld()) {
1868 mNetTransitionWakeLock.release();
1869 causedBy = mNetTransitionWakeLockCausedBy;
1870 }
1871 }
1872 if (causedBy != null) {
Wink Savilleed9c02b2010-12-03 12:01:38 -08001873 log("NetTransition Wakelock for " + causedBy + " released by timeout");
Robert Greenwalt14f2ef42010-06-15 12:19:37 -07001874 }
Robert Greenwalt057d5e92010-09-09 14:05:10 -07001875 break;
Robert Greenwalt8dcc28b2010-09-23 10:05:56 -07001876 case EVENT_RESTORE_DEFAULT_NETWORK:
Robert Greenwalt9c75d4a2009-09-27 17:27:04 -07001877 FeatureUser u = (FeatureUser)msg.obj;
1878 u.expire();
Robert Greenwaltd7085fc2010-09-08 15:24:47 -07001879 break;
Robert Greenwalt8dcc28b2010-09-23 10:05:56 -07001880 case EVENT_INET_CONDITION_CHANGE:
1881 {
1882 int netType = msg.arg1;
1883 int condition = msg.arg2;
1884 handleInetConditionChange(netType, condition);
Robert Greenwalt14f2ef42010-06-15 12:19:37 -07001885 break;
Robert Greenwalt8dcc28b2010-09-23 10:05:56 -07001886 }
1887 case EVENT_INET_CONDITION_HOLD_END:
1888 {
1889 int netType = msg.arg1;
1890 int sequence = msg.arg2;
1891 handleInetConditionHoldEnd(netType, sequence);
Robert Greenwaltd7085fc2010-09-08 15:24:47 -07001892 break;
Robert Greenwalt8dcc28b2010-09-23 10:05:56 -07001893 }
1894 case EVENT_SET_NETWORK_PREFERENCE:
1895 {
1896 int preference = msg.arg1;
1897 handleSetNetworkPreference(preference);
1898 break;
1899 }
1900 case EVENT_SET_BACKGROUND_DATA:
1901 {
1902 boolean enabled = (msg.arg1 == ENABLED);
1903 handleSetBackgroundData(enabled);
1904 break;
1905 }
1906 case EVENT_SET_MOBILE_DATA:
1907 {
1908 boolean enabled = (msg.arg1 == ENABLED);
1909 handleSetMobileData(enabled);
1910 break;
1911 }
Robert Greenwalt434203a2010-10-11 16:00:27 -07001912 case EVENT_APPLY_GLOBAL_HTTP_PROXY:
1913 {
1914 handleDeprecatedGlobalHttpProxy();
Robert Greenwaltd55a6b42011-03-25 13:09:25 -07001915 break;
1916 }
1917 case EVENT_SET_DEPENDENCY_MET:
1918 {
1919 boolean met = (msg.arg1 == ENABLED);
1920 handleSetDependencyMet(msg.arg2, met);
1921 break;
Robert Greenwalt434203a2010-10-11 16:00:27 -07001922 }
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001923 }
1924 }
1925 }
Robert Greenwaltd0e18ff2010-01-26 11:40:34 -08001926
1927 // javadoc from interface
Robert Greenwalt5a735062010-03-02 17:25:02 -08001928 public int tether(String iface) {
Robert Greenwaltd0e18ff2010-01-26 11:40:34 -08001929 enforceTetherChangePermission();
Robert Greenwalt5a735062010-03-02 17:25:02 -08001930
1931 if (isTetheringSupported()) {
1932 return mTethering.tether(iface);
1933 } else {
1934 return ConnectivityManager.TETHER_ERROR_UNSUPPORTED;
1935 }
Robert Greenwaltd0e18ff2010-01-26 11:40:34 -08001936 }
1937
1938 // javadoc from interface
Robert Greenwalt5a735062010-03-02 17:25:02 -08001939 public int untether(String iface) {
Robert Greenwaltd0e18ff2010-01-26 11:40:34 -08001940 enforceTetherChangePermission();
Robert Greenwalt5a735062010-03-02 17:25:02 -08001941
1942 if (isTetheringSupported()) {
1943 return mTethering.untether(iface);
1944 } else {
1945 return ConnectivityManager.TETHER_ERROR_UNSUPPORTED;
1946 }
1947 }
1948
1949 // javadoc from interface
1950 public int getLastTetherError(String iface) {
1951 enforceTetherAccessPermission();
1952
1953 if (isTetheringSupported()) {
1954 return mTethering.getLastTetherError(iface);
1955 } else {
1956 return ConnectivityManager.TETHER_ERROR_UNSUPPORTED;
1957 }
Robert Greenwalt2a091d72010-02-11 18:18:40 -08001958 }
1959
1960 // TODO - proper iface API for selection by property, inspection, etc
1961 public String[] getTetherableUsbRegexs() {
1962 enforceTetherAccessPermission();
1963 if (isTetheringSupported()) {
1964 return mTethering.getTetherableUsbRegexs();
1965 } else {
1966 return new String[0];
1967 }
1968 }
1969
1970 public String[] getTetherableWifiRegexs() {
1971 enforceTetherAccessPermission();
1972 if (isTetheringSupported()) {
1973 return mTethering.getTetherableWifiRegexs();
1974 } else {
1975 return new String[0];
1976 }
Robert Greenwaltd0e18ff2010-01-26 11:40:34 -08001977 }
1978
Danica Chang6fdd0c62010-08-11 14:54:43 -07001979 public String[] getTetherableBluetoothRegexs() {
1980 enforceTetherAccessPermission();
1981 if (isTetheringSupported()) {
1982 return mTethering.getTetherableBluetoothRegexs();
1983 } else {
1984 return new String[0];
1985 }
1986 }
1987
Robert Greenwaltd0e18ff2010-01-26 11:40:34 -08001988 // TODO - move iface listing, queries, etc to new module
1989 // javadoc from interface
1990 public String[] getTetherableIfaces() {
Robert Greenwalt2a091d72010-02-11 18:18:40 -08001991 enforceTetherAccessPermission();
Robert Greenwaltd0e18ff2010-01-26 11:40:34 -08001992 return mTethering.getTetherableIfaces();
1993 }
1994
1995 public String[] getTetheredIfaces() {
Robert Greenwalt2a091d72010-02-11 18:18:40 -08001996 enforceTetherAccessPermission();
Robert Greenwaltd0e18ff2010-01-26 11:40:34 -08001997 return mTethering.getTetheredIfaces();
1998 }
Robert Greenwalt2a091d72010-02-11 18:18:40 -08001999
Robert Greenwalt5a735062010-03-02 17:25:02 -08002000 public String[] getTetheringErroredIfaces() {
2001 enforceTetherAccessPermission();
2002 return mTethering.getErroredIfaces();
2003 }
2004
Robert Greenwalt2a091d72010-02-11 18:18:40 -08002005 // if ro.tether.denied = true we default to no tethering
2006 // gservices could set the secure setting to 1 though to enable it on a build where it
2007 // had previously been turned off.
2008 public boolean isTetheringSupported() {
2009 enforceTetherAccessPermission();
2010 int defaultVal = (SystemProperties.get("ro.tether.denied").equals("true") ? 0 : 1);
Robert Greenwaltc9d5fb72010-02-25 12:29:30 -08002011 boolean tetherEnabledInSettings = (Settings.Secure.getInt(mContext.getContentResolver(),
2012 Settings.Secure.TETHER_SUPPORTED, defaultVal) != 0);
2013 return tetherEnabledInSettings && mTetheringConfigValid;
Robert Greenwalt2a091d72010-02-11 18:18:40 -08002014 }
Robert Greenwalt14f2ef42010-06-15 12:19:37 -07002015
2016 // An API NetworkStateTrackers can call when they lose their network.
2017 // This will automatically be cleared after X seconds or a network becomes CONNECTED,
2018 // whichever happens first. The timer is started by the first caller and not
2019 // restarted by subsequent callers.
2020 public void requestNetworkTransitionWakelock(String forWhom) {
2021 enforceConnectivityInternalPermission();
2022 synchronized (this) {
2023 if (mNetTransitionWakeLock.isHeld()) return;
2024 mNetTransitionWakeLockSerialNumber++;
2025 mNetTransitionWakeLock.acquire();
2026 mNetTransitionWakeLockCausedBy = forWhom;
2027 }
2028 mHandler.sendMessageDelayed(mHandler.obtainMessage(
Robert Greenwaltf3331232010-09-24 14:32:21 -07002029 EVENT_CLEAR_NET_TRANSITION_WAKELOCK,
Robert Greenwalt14f2ef42010-06-15 12:19:37 -07002030 mNetTransitionWakeLockSerialNumber, 0),
2031 mNetTransitionWakeLockTimeout);
2032 return;
2033 }
Robert Greenwaltca4306c2010-09-09 13:15:32 -07002034
Robert Greenwaltd7085fc2010-09-08 15:24:47 -07002035 // 100 percent is full good, 0 is full bad.
2036 public void reportInetCondition(int networkType, int percentage) {
Wink Savilleed9c02b2010-12-03 12:01:38 -08002037 if (DBG) log("reportNetworkCondition(" + networkType + ", " + percentage + ")");
Robert Greenwaltd7085fc2010-09-08 15:24:47 -07002038 mContext.enforceCallingOrSelfPermission(
2039 android.Manifest.permission.STATUS_BAR,
2040 "ConnectivityService");
2041
Robert Greenwalt4e8dfef2010-09-20 14:35:25 -07002042 if (DBG) {
2043 int pid = getCallingPid();
2044 int uid = getCallingUid();
2045 String s = pid + "(" + uid + ") reports inet is " +
2046 (percentage > 50 ? "connected" : "disconnected") + " (" + percentage + ") on " +
2047 "network Type " + networkType + " at " + GregorianCalendar.getInstance().getTime();
2048 mInetLog.add(s);
2049 while(mInetLog.size() > INET_CONDITION_LOG_MAX_SIZE) {
2050 mInetLog.remove(0);
2051 }
2052 }
Robert Greenwaltd7085fc2010-09-08 15:24:47 -07002053 mHandler.sendMessage(mHandler.obtainMessage(
Robert Greenwalt8dcc28b2010-09-23 10:05:56 -07002054 EVENT_INET_CONDITION_CHANGE, networkType, percentage));
2055 }
2056
2057 private void handleInetConditionChange(int netType, int condition) {
2058 if (DBG) {
Wink Savilleed9c02b2010-12-03 12:01:38 -08002059 log("Inet connectivity change, net=" +
Robert Greenwalt8dcc28b2010-09-23 10:05:56 -07002060 netType + ", condition=" + condition +
2061 ",mActiveDefaultNetwork=" + mActiveDefaultNetwork);
2062 }
2063 if (mActiveDefaultNetwork == -1) {
Wink Savilleed9c02b2010-12-03 12:01:38 -08002064 if (DBG) log("no active default network - aborting");
Robert Greenwalt8dcc28b2010-09-23 10:05:56 -07002065 return;
2066 }
2067 if (mActiveDefaultNetwork != netType) {
Wink Savilleed9c02b2010-12-03 12:01:38 -08002068 if (DBG) log("given net not default - aborting");
Robert Greenwalt8dcc28b2010-09-23 10:05:56 -07002069 return;
2070 }
2071 mDefaultInetCondition = condition;
2072 int delay;
2073 if (mInetConditionChangeInFlight == false) {
Wink Savilleed9c02b2010-12-03 12:01:38 -08002074 if (DBG) log("starting a change hold");
Robert Greenwalt8dcc28b2010-09-23 10:05:56 -07002075 // setup a new hold to debounce this
2076 if (mDefaultInetCondition > 50) {
2077 delay = Settings.Secure.getInt(mContext.getContentResolver(),
2078 Settings.Secure.INET_CONDITION_DEBOUNCE_UP_DELAY, 500);
2079 } else {
2080 delay = Settings.Secure.getInt(mContext.getContentResolver(),
2081 Settings.Secure.INET_CONDITION_DEBOUNCE_DOWN_DELAY, 3000);
2082 }
2083 mInetConditionChangeInFlight = true;
2084 mHandler.sendMessageDelayed(mHandler.obtainMessage(EVENT_INET_CONDITION_HOLD_END,
2085 mActiveDefaultNetwork, mDefaultConnectionSequence), delay);
2086 } else {
2087 // we've set the new condition, when this hold ends that will get
2088 // picked up
Wink Savilleed9c02b2010-12-03 12:01:38 -08002089 if (DBG) log("currently in hold - not setting new end evt");
Robert Greenwalt8dcc28b2010-09-23 10:05:56 -07002090 }
2091 }
2092
2093 private void handleInetConditionHoldEnd(int netType, int sequence) {
2094 if (DBG) {
Wink Savilleed9c02b2010-12-03 12:01:38 -08002095 log("Inet hold end, net=" + netType +
Robert Greenwalt8dcc28b2010-09-23 10:05:56 -07002096 ", condition =" + mDefaultInetCondition +
2097 ", published condition =" + mDefaultInetConditionPublished);
2098 }
2099 mInetConditionChangeInFlight = false;
2100
2101 if (mActiveDefaultNetwork == -1) {
Wink Savilleed9c02b2010-12-03 12:01:38 -08002102 if (DBG) log("no active default network - aborting");
Robert Greenwalt8dcc28b2010-09-23 10:05:56 -07002103 return;
2104 }
2105 if (mDefaultConnectionSequence != sequence) {
Wink Savilleed9c02b2010-12-03 12:01:38 -08002106 if (DBG) log("event hold for obsolete network - aborting");
Robert Greenwalt8dcc28b2010-09-23 10:05:56 -07002107 return;
2108 }
2109 if (mDefaultInetConditionPublished == mDefaultInetCondition) {
Wink Savilleed9c02b2010-12-03 12:01:38 -08002110 if (DBG) log("no change in condition - aborting");
Robert Greenwalt8dcc28b2010-09-23 10:05:56 -07002111 return;
2112 }
2113 NetworkInfo networkInfo = mNetTrackers[mActiveDefaultNetwork].getNetworkInfo();
2114 if (networkInfo.isConnected() == false) {
Wink Savilleed9c02b2010-12-03 12:01:38 -08002115 if (DBG) log("default network not connected - aborting");
Robert Greenwalt8dcc28b2010-09-23 10:05:56 -07002116 return;
2117 }
2118 mDefaultInetConditionPublished = mDefaultInetCondition;
2119 sendInetConditionBroadcast(networkInfo);
2120 return;
Robert Greenwaltd7085fc2010-09-08 15:24:47 -07002121 }
Robert Greenwalt434203a2010-10-11 16:00:27 -07002122
2123 public synchronized ProxyProperties getProxy() {
2124 if (mGlobalProxy != null) return mGlobalProxy;
2125 if (mDefaultProxy != null) return mDefaultProxy;
2126 return null;
2127 }
2128
2129 public void setGlobalProxy(ProxyProperties proxyProperties) {
2130 enforceChangePermission();
2131 synchronized (mGlobalProxyLock) {
2132 if (proxyProperties == mGlobalProxy) return;
2133 if (proxyProperties != null && proxyProperties.equals(mGlobalProxy)) return;
2134 if (mGlobalProxy != null && mGlobalProxy.equals(proxyProperties)) return;
2135
2136 String host = "";
2137 int port = 0;
2138 String exclList = "";
2139 if (proxyProperties != null && !TextUtils.isEmpty(proxyProperties.getHost())) {
2140 mGlobalProxy = new ProxyProperties(proxyProperties);
2141 host = mGlobalProxy.getHost();
2142 port = mGlobalProxy.getPort();
2143 exclList = mGlobalProxy.getExclusionList();
2144 } else {
2145 mGlobalProxy = null;
2146 }
2147 ContentResolver res = mContext.getContentResolver();
2148 Settings.Secure.putString(res, Settings.Secure.GLOBAL_HTTP_PROXY_HOST, host);
2149 Settings.Secure.putInt(res, Settings.Secure.GLOBAL_HTTP_PROXY_PORT, port);
Robert Greenwaltb7090d62010-12-02 11:31:00 -08002150 Settings.Secure.putString(res, Settings.Secure.GLOBAL_HTTP_PROXY_EXCLUSION_LIST,
Robert Greenwalt434203a2010-10-11 16:00:27 -07002151 exclList);
2152 }
2153
2154 if (mGlobalProxy == null) {
2155 proxyProperties = mDefaultProxy;
2156 }
2157 sendProxyBroadcast(proxyProperties);
2158 }
2159
Robert Greenwaltb7090d62010-12-02 11:31:00 -08002160 private void loadGlobalProxy() {
2161 ContentResolver res = mContext.getContentResolver();
2162 String host = Settings.Secure.getString(res, Settings.Secure.GLOBAL_HTTP_PROXY_HOST);
2163 int port = Settings.Secure.getInt(res, Settings.Secure.GLOBAL_HTTP_PROXY_PORT, 0);
2164 String exclList = Settings.Secure.getString(res,
2165 Settings.Secure.GLOBAL_HTTP_PROXY_EXCLUSION_LIST);
2166 if (!TextUtils.isEmpty(host)) {
2167 ProxyProperties proxyProperties = new ProxyProperties(host, port, exclList);
2168 synchronized (mGlobalProxyLock) {
2169 mGlobalProxy = proxyProperties;
2170 }
2171 }
2172 }
2173
Robert Greenwalt434203a2010-10-11 16:00:27 -07002174 public ProxyProperties getGlobalProxy() {
2175 synchronized (mGlobalProxyLock) {
2176 return mGlobalProxy;
2177 }
2178 }
2179
2180 private void handleApplyDefaultProxy(int type) {
2181 // check if new default - push it out to all VM if so
2182 ProxyProperties proxy = mNetTrackers[type].getLinkProperties().getHttpProxy();
2183 synchronized (this) {
2184 if (mDefaultProxy != null && mDefaultProxy.equals(proxy)) return;
2185 if (mDefaultProxy == proxy) return;
2186 if (!TextUtils.isEmpty(proxy.getHost())) {
2187 mDefaultProxy = proxy;
2188 } else {
2189 mDefaultProxy = null;
2190 }
2191 }
Wink Savilleed9c02b2010-12-03 12:01:38 -08002192 if (DBG) log("changing default proxy to " + proxy);
Robert Greenwalt434203a2010-10-11 16:00:27 -07002193 if ((proxy == null && mGlobalProxy == null) || proxy.equals(mGlobalProxy)) return;
2194 if (mGlobalProxy != null) return;
2195 sendProxyBroadcast(proxy);
2196 }
2197
2198 private void handleDeprecatedGlobalHttpProxy() {
2199 String proxy = Settings.Secure.getString(mContext.getContentResolver(),
2200 Settings.Secure.HTTP_PROXY);
2201 if (!TextUtils.isEmpty(proxy)) {
2202 String data[] = proxy.split(":");
2203 String proxyHost = data[0];
2204 int proxyPort = 8080;
2205 if (data.length > 1) {
2206 try {
2207 proxyPort = Integer.parseInt(data[1]);
2208 } catch (NumberFormatException e) {
2209 return;
2210 }
2211 }
2212 ProxyProperties p = new ProxyProperties(data[0], proxyPort, "");
2213 setGlobalProxy(p);
2214 }
2215 }
2216
2217 private void sendProxyBroadcast(ProxyProperties proxy) {
Robert Greenwalt55985be2010-12-23 15:51:10 -08002218 if (proxy == null) proxy = new ProxyProperties("", 0, "");
Wink Savilleed9c02b2010-12-03 12:01:38 -08002219 log("sending Proxy Broadcast for " + proxy);
Robert Greenwalt434203a2010-10-11 16:00:27 -07002220 Intent intent = new Intent(Proxy.PROXY_CHANGE_ACTION);
Stan Chesnuttb35d67a2011-01-06 11:00:19 -08002221 intent.addFlags(Intent.FLAG_RECEIVER_REPLACE_PENDING |
2222 Intent.FLAG_RECEIVER_REGISTERED_ONLY_BEFORE_BOOT);
Robert Greenwalt434203a2010-10-11 16:00:27 -07002223 intent.putExtra(Proxy.EXTRA_PROXY_INFO, proxy);
Robert Greenwalta2e13392010-12-06 11:29:17 -08002224 mContext.sendStickyBroadcast(intent);
Robert Greenwalt434203a2010-10-11 16:00:27 -07002225 }
2226
2227 private static class SettingsObserver extends ContentObserver {
2228 private int mWhat;
2229 private Handler mHandler;
2230 SettingsObserver(Handler handler, int what) {
2231 super(handler);
2232 mHandler = handler;
2233 mWhat = what;
2234 }
2235
2236 void observe(Context context) {
2237 ContentResolver resolver = context.getContentResolver();
2238 resolver.registerContentObserver(Settings.Secure.getUriFor(
2239 Settings.Secure.HTTP_PROXY), false, this);
2240 }
2241
2242 @Override
2243 public void onChange(boolean selfChange) {
2244 mHandler.obtainMessage(mWhat).sendToTarget();
2245 }
2246 }
Wink Savilleed9c02b2010-12-03 12:01:38 -08002247
Kazuhiro Ondo01758e82011-04-30 20:10:57 -05002248 private void handleTetherIfaceChange(int type) {
2249 String iface = mNetTrackers[type].getLinkProperties().getInterfaceName();
2250
2251 if (isTetheringSupported()) {
2252 mTethering.handleTetherIfaceChange(iface);
2253 }
2254 }
2255
Wink Savilleed9c02b2010-12-03 12:01:38 -08002256 private void log(String s) {
2257 Slog.d(TAG, s);
2258 }
2259
2260 private void loge(String s) {
2261 Slog.e(TAG, s);
2262 }
Wink Saville9d7d6282011-03-12 14:52:01 -08002263 int convertFeatureToNetworkType(String feature){
2264 int networkType = -1;
2265 if (TextUtils.equals(feature, Phone.FEATURE_ENABLE_MMS)) {
2266 networkType = ConnectivityManager.TYPE_MOBILE_MMS;
2267 } else if (TextUtils.equals(feature, Phone.FEATURE_ENABLE_SUPL)) {
2268 networkType = ConnectivityManager.TYPE_MOBILE_SUPL;
2269 } else if (TextUtils.equals(feature, Phone.FEATURE_ENABLE_DUN) ||
2270 TextUtils.equals(feature, Phone.FEATURE_ENABLE_DUN_ALWAYS)) {
2271 networkType = ConnectivityManager.TYPE_MOBILE_DUN;
2272 } else if (TextUtils.equals(feature, Phone.FEATURE_ENABLE_HIPRI)) {
2273 networkType = ConnectivityManager.TYPE_MOBILE_HIPRI;
2274 } else if (TextUtils.equals(feature, Phone.FEATURE_ENABLE_FOTA)) {
2275 networkType = ConnectivityManager.TYPE_MOBILE_FOTA;
2276 } else if (TextUtils.equals(feature, Phone.FEATURE_ENABLE_IMS)) {
2277 networkType = ConnectivityManager.TYPE_MOBILE_IMS;
2278 } else if (TextUtils.equals(feature, Phone.FEATURE_ENABLE_CBS)) {
2279 networkType = ConnectivityManager.TYPE_MOBILE_CBS;
2280 }
2281 return networkType;
2282 }
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002283}