blob: a8ddc152240a311f7f7d1a8d9dbfa23bdb3d5de5 [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 Greenwaltccf83af12011-06-02 17:30:47 -0700434 mTetheringConfigValid = ((mTethering.getTetherableUsbRegexs().length != 0 ||
Danica Chang6fdd0c62010-08-11 14:54:43 -0700435 mTethering.getTetherableWifiRegexs().length != 0 ||
436 mTethering.getTetherableBluetoothRegexs().length != 0) &&
Robert Greenwaltccf83af12011-06-02 17:30:47 -0700437 mTethering.getUpstreamIfaceTypes().length != 0);
Robert Greenwaltc9d5fb72010-02-25 12:29:30 -0800438
Robert Greenwalt4e8dfef2010-09-20 14:35:25 -0700439 if (DBG) {
440 mInetLog = new ArrayList();
441 }
Robert Greenwalt434203a2010-10-11 16:00:27 -0700442
443 mSettingsObserver = new SettingsObserver(mHandler, EVENT_APPLY_GLOBAL_HTTP_PROXY);
444 mSettingsObserver.observe(mContext);
Robert Greenwaltb7090d62010-12-02 11:31:00 -0800445
446 loadGlobalProxy();
Hung-ying Tyan6b818de2011-01-19 16:48:38 +0800447
448 VpnManager.startVpnService(context);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800449 }
450
Robert Greenwalt5154ae762009-10-30 14:17:42 -0700451
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800452 /**
Robert Greenwalt86e9e552009-07-16 17:21:39 -0700453 * Sets the preferred network.
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800454 * @param preference the new preference
455 */
Robert Greenwalt8dcc28b2010-09-23 10:05:56 -0700456 public void setNetworkPreference(int preference) {
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800457 enforceChangePermission();
Robert Greenwalt8dcc28b2010-09-23 10:05:56 -0700458
459 mHandler.sendMessage(mHandler.obtainMessage(EVENT_SET_NETWORK_PREFERENCE, preference, 0));
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800460 }
461
462 public int getNetworkPreference() {
463 enforceAccessPermission();
Robert Greenwalt8dcc28b2010-09-23 10:05:56 -0700464 int preference;
465 synchronized(this) {
466 preference = mNetworkPreference;
467 }
468 return preference;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800469 }
470
Robert Greenwalt8dcc28b2010-09-23 10:05:56 -0700471 private void handleSetNetworkPreference(int preference) {
472 if (ConnectivityManager.isNetworkTypeValid(preference) &&
Robert Greenwaltd55a6b42011-03-25 13:09:25 -0700473 mNetConfigs[preference] != null &&
474 mNetConfigs[preference].isDefault()) {
Robert Greenwalt8dcc28b2010-09-23 10:05:56 -0700475 if (mNetworkPreference != preference) {
476 final ContentResolver cr = mContext.getContentResolver();
477 Settings.Secure.putInt(cr, Settings.Secure.NETWORK_PREFERENCE, preference);
478 synchronized(this) {
479 mNetworkPreference = preference;
480 }
481 enforcePreference();
482 }
483 }
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800484 }
Robert Greenwalt86e9e552009-07-16 17:21:39 -0700485
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800486 private int getPersistedNetworkPreference() {
487 final ContentResolver cr = mContext.getContentResolver();
488
489 final int networkPrefSetting = Settings.Secure
490 .getInt(cr, Settings.Secure.NETWORK_PREFERENCE, -1);
491 if (networkPrefSetting != -1) {
492 return networkPrefSetting;
493 }
494
495 return ConnectivityManager.DEFAULT_NETWORK_PREFERENCE;
496 }
Robert Greenwalt86e9e552009-07-16 17:21:39 -0700497
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800498 /**
Robert Greenwalt86e9e552009-07-16 17:21:39 -0700499 * Make the state of network connectivity conform to the preference settings
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800500 * In this method, we only tear down a non-preferred network. Establishing
501 * a connection to the preferred network is taken care of when we handle
502 * the disconnect event from the non-preferred network
503 * (see {@link #handleDisconnect(NetworkInfo)}).
504 */
505 private void enforcePreference() {
Robert Greenwalt42acef32009-08-12 16:08:25 -0700506 if (mNetTrackers[mNetworkPreference].getNetworkInfo().isConnected())
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800507 return;
508
Robert Greenwalt42acef32009-08-12 16:08:25 -0700509 if (!mNetTrackers[mNetworkPreference].isAvailable())
510 return;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800511
Robert Greenwalt42acef32009-08-12 16:08:25 -0700512 for (int t=0; t <= ConnectivityManager.MAX_RADIO_TYPE; t++) {
Robert Greenwalt5154ae762009-10-30 14:17:42 -0700513 if (t != mNetworkPreference && mNetTrackers[t] != null &&
Robert Greenwalt42acef32009-08-12 16:08:25 -0700514 mNetTrackers[t].getNetworkInfo().isConnected()) {
Robert Greenwaltec9fe462009-08-20 15:25:14 -0700515 if (DBG) {
Wink Savilleed9c02b2010-12-03 12:01:38 -0800516 log("tearing down " + mNetTrackers[t].getNetworkInfo() +
Robert Greenwaltec9fe462009-08-20 15:25:14 -0700517 " in enforcePreference");
518 }
Robert Greenwalt42acef32009-08-12 16:08:25 -0700519 teardown(mNetTrackers[t]);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800520 }
521 }
522 }
523
524 private boolean teardown(NetworkStateTracker netTracker) {
525 if (netTracker.teardown()) {
526 netTracker.setTeardownRequested(true);
527 return true;
528 } else {
529 return false;
530 }
531 }
532
533 /**
534 * Return NetworkInfo for the active (i.e., connected) network interface.
535 * It is assumed that at most one network is active at a time. If more
536 * than one is active, it is indeterminate which will be returned.
Robert Greenwalt86e9e552009-07-16 17:21:39 -0700537 * @return the info for the active network, or {@code null} if none is
538 * active
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800539 */
540 public NetworkInfo getActiveNetworkInfo() {
Robert Greenwalt59911582011-05-20 12:23:41 -0700541 return getNetworkInfo(mActiveDefaultNetwork);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800542 }
543
544 public NetworkInfo getNetworkInfo(int networkType) {
545 enforceAccessPermission();
546 if (ConnectivityManager.isNetworkTypeValid(networkType)) {
547 NetworkStateTracker t = mNetTrackers[networkType];
548 if (t != null)
549 return t.getNetworkInfo();
550 }
551 return null;
552 }
553
554 public NetworkInfo[] getAllNetworkInfo() {
555 enforceAccessPermission();
Robert Greenwalt5154ae762009-10-30 14:17:42 -0700556 NetworkInfo[] result = new NetworkInfo[mNetworksDefined];
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800557 int i = 0;
558 for (NetworkStateTracker t : mNetTrackers) {
Robert Greenwalt5154ae762009-10-30 14:17:42 -0700559 if(t != null) result[i++] = t.getNetworkInfo();
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800560 }
561 return result;
562 }
563
Robert Greenwaltd192dad2010-09-14 09:18:02 -0700564 /**
565 * Return LinkProperties for the active (i.e., connected) default
566 * network interface. It is assumed that at most one default network
567 * is active at a time. If more than one is active, it is indeterminate
568 * which will be returned.
569 * @return the ip properties for the active network, or {@code null} if
570 * none is active
571 */
572 public LinkProperties getActiveLinkProperties() {
Robert Greenwalt59911582011-05-20 12:23:41 -0700573 return getLinkProperties(mActiveDefaultNetwork);
Robert Greenwaltd192dad2010-09-14 09:18:02 -0700574 }
575
576 public LinkProperties getLinkProperties(int networkType) {
577 enforceAccessPermission();
578 if (ConnectivityManager.isNetworkTypeValid(networkType)) {
579 NetworkStateTracker t = mNetTrackers[networkType];
580 if (t != null) return t.getLinkProperties();
581 }
582 return null;
583 }
584
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800585 public boolean setRadios(boolean turnOn) {
586 boolean result = true;
587 enforceChangePermission();
588 for (NetworkStateTracker t : mNetTrackers) {
Robert Greenwalt5154ae762009-10-30 14:17:42 -0700589 if (t != null) result = t.setRadio(turnOn) && result;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800590 }
591 return result;
592 }
593
594 public boolean setRadio(int netType, boolean turnOn) {
595 enforceChangePermission();
596 if (!ConnectivityManager.isNetworkTypeValid(netType)) {
597 return false;
598 }
599 NetworkStateTracker tracker = mNetTrackers[netType];
600 return tracker != null && tracker.setRadio(turnOn);
601 }
602
Robert Greenwalt9c75d4a2009-09-27 17:27:04 -0700603 /**
604 * Used to notice when the calling process dies so we can self-expire
605 *
606 * Also used to know if the process has cleaned up after itself when
607 * our auto-expire timer goes off. The timer has a link to an object.
608 *
609 */
Robert Greenwalt42acef32009-08-12 16:08:25 -0700610 private class FeatureUser implements IBinder.DeathRecipient {
611 int mNetworkType;
612 String mFeature;
613 IBinder mBinder;
614 int mPid;
615 int mUid;
Robert Greenwaltb9285352009-12-21 18:24:07 -0800616 long mCreateTime;
Robert Greenwalt42acef32009-08-12 16:08:25 -0700617
618 FeatureUser(int type, String feature, IBinder binder) {
619 super();
620 mNetworkType = type;
621 mFeature = feature;
622 mBinder = binder;
623 mPid = getCallingPid();
624 mUid = getCallingUid();
Robert Greenwaltb9285352009-12-21 18:24:07 -0800625 mCreateTime = System.currentTimeMillis();
Robert Greenwalt9c75d4a2009-09-27 17:27:04 -0700626
Robert Greenwalt42acef32009-08-12 16:08:25 -0700627 try {
628 mBinder.linkToDeath(this, 0);
629 } catch (RemoteException e) {
630 binderDied();
631 }
632 }
633
634 void unlinkDeathRecipient() {
635 mBinder.unlinkToDeath(this, 0);
636 }
637
638 public void binderDied() {
Wink Savilleed9c02b2010-12-03 12:01:38 -0800639 log("ConnectivityService FeatureUser binderDied(" +
Robert Greenwaltb9285352009-12-21 18:24:07 -0800640 mNetworkType + ", " + mFeature + ", " + mBinder + "), created " +
641 (System.currentTimeMillis() - mCreateTime) + " mSec ago");
Robert Greenwalt9c75d4a2009-09-27 17:27:04 -0700642 stopUsingNetworkFeature(this, false);
Robert Greenwalt42acef32009-08-12 16:08:25 -0700643 }
644
Robert Greenwalt9c75d4a2009-09-27 17:27:04 -0700645 public void expire() {
Wink Savilleed9c02b2010-12-03 12:01:38 -0800646 log("ConnectivityService FeatureUser expire(" +
Robert Greenwaltb9285352009-12-21 18:24:07 -0800647 mNetworkType + ", " + mFeature + ", " + mBinder +"), created " +
648 (System.currentTimeMillis() - mCreateTime) + " mSec ago");
Robert Greenwalt9c75d4a2009-09-27 17:27:04 -0700649 stopUsingNetworkFeature(this, false);
650 }
Robert Greenwaltb9285352009-12-21 18:24:07 -0800651
652 public String toString() {
653 return "FeatureUser("+mNetworkType+","+mFeature+","+mPid+","+mUid+"), created " +
654 (System.currentTimeMillis() - mCreateTime) + " mSec ago";
655 }
Robert Greenwalt42acef32009-08-12 16:08:25 -0700656 }
657
Robert Greenwalt9c75d4a2009-09-27 17:27:04 -0700658 // javadoc from interface
Robert Greenwalt42acef32009-08-12 16:08:25 -0700659 public int startUsingNetworkFeature(int networkType, String feature,
660 IBinder binder) {
661 if (DBG) {
Wink Savilleed9c02b2010-12-03 12:01:38 -0800662 log("startUsingNetworkFeature for net " + networkType + ": " + feature);
Robert Greenwalt42acef32009-08-12 16:08:25 -0700663 }
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800664 enforceChangePermission();
Robert Greenwalt5154ae762009-10-30 14:17:42 -0700665 if (!ConnectivityManager.isNetworkTypeValid(networkType) ||
Robert Greenwaltd55a6b42011-03-25 13:09:25 -0700666 mNetConfigs[networkType] == null) {
Robert Greenwalt42acef32009-08-12 16:08:25 -0700667 return Phone.APN_REQUEST_FAILED;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800668 }
Robert Greenwalt42acef32009-08-12 16:08:25 -0700669
Robert Greenwalt9c75d4a2009-09-27 17:27:04 -0700670 FeatureUser f = new FeatureUser(networkType, feature, binder);
Robert Greenwalt42acef32009-08-12 16:08:25 -0700671
672 // TODO - move this into the MobileDataStateTracker
673 int usedNetworkType = networkType;
674 if(networkType == ConnectivityManager.TYPE_MOBILE) {
Wink Saville9d7d6282011-03-12 14:52:01 -0800675 usedNetworkType = convertFeatureToNetworkType(feature);
676 if (usedNetworkType < 0) {
677 Slog.e(TAG, "Can't match any netTracker!");
678 usedNetworkType = networkType;
Robert Greenwalt42acef32009-08-12 16:08:25 -0700679 }
680 }
681 NetworkStateTracker network = mNetTrackers[usedNetworkType];
682 if (network != null) {
Robert Greenwalt0be1e982010-12-15 13:26:33 -0800683 Integer currentPid = new Integer(getCallingPid());
Robert Greenwalt42acef32009-08-12 16:08:25 -0700684 if (usedNetworkType != networkType) {
Robert Greenwalt42acef32009-08-12 16:08:25 -0700685 NetworkStateTracker radio = mNetTrackers[networkType];
686 NetworkInfo ni = network.getNetworkInfo();
687
688 if (ni.isAvailable() == false) {
Wink Savilleed9c02b2010-12-03 12:01:38 -0800689 if (DBG) log("special network not available");
Robert Greenwalte32e8122010-12-29 14:35:21 -0800690 if (!TextUtils.equals(feature,Phone.FEATURE_ENABLE_DUN_ALWAYS)) {
691 return Phone.APN_TYPE_NOT_AVAILABLE;
692 } else {
693 // else make the attempt anyway - probably giving REQUEST_STARTED below
694 }
Robert Greenwalt42acef32009-08-12 16:08:25 -0700695 }
696
Robert Greenwalt9c75d4a2009-09-27 17:27:04 -0700697 synchronized(this) {
698 mFeatureUsers.add(f);
699 if (!mNetRequestersPids[usedNetworkType].contains(currentPid)) {
700 // this gets used for per-pid dns when connected
701 mNetRequestersPids[usedNetworkType].add(currentPid);
702 }
Robert Greenwalt42acef32009-08-12 16:08:25 -0700703 }
Robert Greenwalt9c75d4a2009-09-27 17:27:04 -0700704
Robert Greenwaltf2102f72011-05-03 19:02:44 -0700705 int restoreTimer = getRestoreDefaultNetworkDelay(usedNetworkType);
706
707 if (restoreTimer >= 0) {
708 mHandler.sendMessageDelayed(
709 mHandler.obtainMessage(EVENT_RESTORE_DEFAULT_NETWORK, f), restoreTimer);
710 }
Robert Greenwalt42acef32009-08-12 16:08:25 -0700711
Robert Greenwalta64bf832009-08-19 20:19:33 -0700712 if ((ni.isConnectedOrConnecting() == true) &&
713 !network.isTeardownRequested()) {
Robert Greenwalt42acef32009-08-12 16:08:25 -0700714 if (ni.isConnected() == true) {
715 // add the pid-specific dns
Robert Greenwaltc76b8fa2010-07-23 15:46:26 -0700716 handleDnsConfigurationChange(networkType);
Wink Savilleed9c02b2010-12-03 12:01:38 -0800717 if (DBG) log("special network already active");
Robert Greenwalt42acef32009-08-12 16:08:25 -0700718 return Phone.APN_ALREADY_ACTIVE;
719 }
Wink Savilleed9c02b2010-12-03 12:01:38 -0800720 if (DBG) log("special network already connecting");
Robert Greenwalt42acef32009-08-12 16:08:25 -0700721 return Phone.APN_REQUEST_STARTED;
722 }
723
724 // check if the radio in play can make another contact
725 // assume if cannot for now
726
Wink Savilleed9c02b2010-12-03 12:01:38 -0800727 if (DBG) log("reconnecting to special network");
Robert Greenwalt42acef32009-08-12 16:08:25 -0700728 network.reconnect();
729 return Phone.APN_REQUEST_STARTED;
730 } else {
Robert Greenwalt0be1e982010-12-15 13:26:33 -0800731 // need to remember this unsupported request so we respond appropriately on stop
732 synchronized(this) {
733 mFeatureUsers.add(f);
734 if (!mNetRequestersPids[usedNetworkType].contains(currentPid)) {
735 // this gets used for per-pid dns when connected
736 mNetRequestersPids[usedNetworkType].add(currentPid);
737 }
738 }
Robert Greenwalt02648a42010-05-18 10:52:51 -0700739 return -1;
Robert Greenwalt42acef32009-08-12 16:08:25 -0700740 }
741 }
742 return Phone.APN_TYPE_NOT_AVAILABLE;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800743 }
744
Robert Greenwalt9c75d4a2009-09-27 17:27:04 -0700745 // javadoc from interface
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800746 public int stopUsingNetworkFeature(int networkType, String feature) {
Robert Greenwaltb8f16342009-10-06 17:52:40 -0700747 enforceChangePermission();
748
Robert Greenwalt9c75d4a2009-09-27 17:27:04 -0700749 int pid = getCallingPid();
750 int uid = getCallingUid();
751
752 FeatureUser u = null;
753 boolean found = false;
754
755 synchronized(this) {
756 for (int i = 0; i < mFeatureUsers.size() ; i++) {
757 u = (FeatureUser)mFeatureUsers.get(i);
758 if (uid == u.mUid && pid == u.mPid &&
759 networkType == u.mNetworkType &&
760 TextUtils.equals(feature, u.mFeature)) {
761 found = true;
762 break;
763 }
764 }
765 }
766 if (found && u != null) {
767 // stop regardless of how many other time this proc had called start
768 return stopUsingNetworkFeature(u, true);
769 } else {
770 // none found!
Wink Savilleed9c02b2010-12-03 12:01:38 -0800771 if (DBG) log("ignoring stopUsingNetworkFeature - not a live request");
Robert Greenwalt9c75d4a2009-09-27 17:27:04 -0700772 return 1;
773 }
Robert Greenwalt42acef32009-08-12 16:08:25 -0700774 }
775
Robert Greenwalt9c75d4a2009-09-27 17:27:04 -0700776 private int stopUsingNetworkFeature(FeatureUser u, boolean ignoreDups) {
777 int networkType = u.mNetworkType;
778 String feature = u.mFeature;
779 int pid = u.mPid;
780 int uid = u.mUid;
781
782 NetworkStateTracker tracker = null;
783 boolean callTeardown = false; // used to carry our decision outside of sync block
784
Robert Greenwalt42acef32009-08-12 16:08:25 -0700785 if (DBG) {
Wink Savilleed9c02b2010-12-03 12:01:38 -0800786 log("stopUsingNetworkFeature for net " + networkType +
Robert Greenwalt42acef32009-08-12 16:08:25 -0700787 ": " + feature);
788 }
Robert Greenwaltb8f16342009-10-06 17:52:40 -0700789
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800790 if (!ConnectivityManager.isNetworkTypeValid(networkType)) {
791 return -1;
792 }
Robert Greenwalt42acef32009-08-12 16:08:25 -0700793
Robert Greenwalt9c75d4a2009-09-27 17:27:04 -0700794 // need to link the mFeatureUsers list with the mNetRequestersPids state in this
795 // sync block
796 synchronized(this) {
797 // check if this process still has an outstanding start request
798 if (!mFeatureUsers.contains(u)) {
Wink Savilleed9c02b2010-12-03 12:01:38 -0800799 if (DBG) log("ignoring - this process has no outstanding requests");
Robert Greenwalt42acef32009-08-12 16:08:25 -0700800 return 1;
801 }
Robert Greenwalt9c75d4a2009-09-27 17:27:04 -0700802 u.unlinkDeathRecipient();
803 mFeatureUsers.remove(mFeatureUsers.indexOf(u));
804 // If we care about duplicate requests, check for that here.
805 //
806 // This is done to support the extension of a request - the app
807 // can request we start the network feature again and renew the
808 // auto-shutoff delay. Normal "stop" calls from the app though
809 // do not pay attention to duplicate requests - in effect the
810 // API does not refcount and a single stop will counter multiple starts.
811 if (ignoreDups == false) {
812 for (int i = 0; i < mFeatureUsers.size() ; i++) {
813 FeatureUser x = (FeatureUser)mFeatureUsers.get(i);
814 if (x.mUid == u.mUid && x.mPid == u.mPid &&
815 x.mNetworkType == u.mNetworkType &&
816 TextUtils.equals(x.mFeature, u.mFeature)) {
Wink Savilleed9c02b2010-12-03 12:01:38 -0800817 if (DBG) log("ignoring stopUsingNetworkFeature as dup is found");
Robert Greenwalt9c75d4a2009-09-27 17:27:04 -0700818 return 1;
819 }
820 }
Robert Greenwalt42acef32009-08-12 16:08:25 -0700821 }
Robert Greenwalt9c75d4a2009-09-27 17:27:04 -0700822
823 // TODO - move to MobileDataStateTracker
824 int usedNetworkType = networkType;
825 if (networkType == ConnectivityManager.TYPE_MOBILE) {
Wink Saville9d7d6282011-03-12 14:52:01 -0800826 usedNetworkType = convertFeatureToNetworkType(feature);
827 if (usedNetworkType < 0) {
828 usedNetworkType = networkType;
Robert Greenwalt9c75d4a2009-09-27 17:27:04 -0700829 }
830 }
831 tracker = mNetTrackers[usedNetworkType];
Robert Greenwalt5154ae762009-10-30 14:17:42 -0700832 if (tracker == null) {
Wink Savilleed9c02b2010-12-03 12:01:38 -0800833 if (DBG) log("ignoring - no known tracker for net type " + usedNetworkType);
Robert Greenwalt5154ae762009-10-30 14:17:42 -0700834 return -1;
835 }
836 if (usedNetworkType != networkType) {
Robert Greenwalt9c75d4a2009-09-27 17:27:04 -0700837 Integer currentPid = new Integer(pid);
Robert Greenwalt9c75d4a2009-09-27 17:27:04 -0700838 mNetRequestersPids[usedNetworkType].remove(currentPid);
Robert Greenwalt421c72b2009-12-17 14:54:59 -0800839 reassessPidDns(pid, true);
Robert Greenwalt9c75d4a2009-09-27 17:27:04 -0700840 if (mNetRequestersPids[usedNetworkType].size() != 0) {
Wink Savilleed9c02b2010-12-03 12:01:38 -0800841 if (DBG) log("not tearing down special network - " +
Robert Greenwalt9c75d4a2009-09-27 17:27:04 -0700842 "others still using it");
843 return 1;
844 }
845 callTeardown = true;
Robert Greenwalt19b9ab42011-01-10 11:58:31 -0800846 } else {
847 if (DBG) log("not a known feature - dropping");
Robert Greenwalt9c75d4a2009-09-27 17:27:04 -0700848 }
849 }
Wink Savilleed9c02b2010-12-03 12:01:38 -0800850 if (DBG) log("Doing network teardown");
Robert Greenwalt9c75d4a2009-09-27 17:27:04 -0700851 if (callTeardown) {
852 tracker.teardown();
Robert Greenwalt42acef32009-08-12 16:08:25 -0700853 return 1;
854 } else {
Robert Greenwalt02648a42010-05-18 10:52:51 -0700855 return -1;
Robert Greenwalt42acef32009-08-12 16:08:25 -0700856 }
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800857 }
858
859 /**
Robert Greenwalt585ac0f2010-08-27 09:24:29 -0700860 * @deprecated use requestRouteToHostAddress instead
861 *
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800862 * Ensure that a network route exists to deliver traffic to the specified
863 * host via the specified network interface.
Robert Greenwalt86e9e552009-07-16 17:21:39 -0700864 * @param networkType the type of the network over which traffic to the
865 * specified host is to be routed
866 * @param hostAddress the IP address of the host to which the route is
867 * desired
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800868 * @return {@code true} on success, {@code false} on failure
869 */
870 public boolean requestRouteToHost(int networkType, int hostAddress) {
Robert Greenwalt585ac0f2010-08-27 09:24:29 -0700871 InetAddress inetAddress = NetworkUtils.intToInetAddress(hostAddress);
872
873 if (inetAddress == null) {
874 return false;
875 }
876
877 return requestRouteToHostAddress(networkType, inetAddress.getAddress());
878 }
879
880 /**
881 * Ensure that a network route exists to deliver traffic to the specified
882 * host via the specified network interface.
883 * @param networkType the type of the network over which traffic to the
884 * specified host is to be routed
885 * @param hostAddress the IP address of the host to which the route is
886 * desired
887 * @return {@code true} on success, {@code false} on failure
888 */
889 public boolean requestRouteToHostAddress(int networkType, byte[] hostAddress) {
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800890 enforceChangePermission();
891 if (!ConnectivityManager.isNetworkTypeValid(networkType)) {
892 return false;
893 }
894 NetworkStateTracker tracker = mNetTrackers[networkType];
Robert Greenwalt8206ff32009-09-10 15:06:20 -0700895
Robert Greenwalt5154ae762009-10-30 14:17:42 -0700896 if (tracker == null || !tracker.getNetworkInfo().isConnected() ||
897 tracker.isTeardownRequested()) {
Robert Greenwalt8206ff32009-09-10 15:06:20 -0700898 if (DBG) {
Wink Savilleed9c02b2010-12-03 12:01:38 -0800899 log("requestRouteToHostAddress on down network " +
Robert Greenwalt585ac0f2010-08-27 09:24:29 -0700900 "(" + networkType + ") - dropped");
Robert Greenwalt8206ff32009-09-10 15:06:20 -0700901 }
902 return false;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800903 }
Robert Greenwalt47f69fe2010-06-15 15:43:39 -0700904 try {
Robert Greenwalt585ac0f2010-08-27 09:24:29 -0700905 InetAddress addr = InetAddress.getByAddress(hostAddress);
Robert Greenwaltf43396c2011-05-06 17:10:53 -0700906 return addHostRoute(tracker, addr, 0);
Robert Greenwalt47f69fe2010-06-15 15:43:39 -0700907 } catch (UnknownHostException e) {}
908 return false;
Irfan Sheriffd649c122010-06-09 15:39:36 -0700909 }
910
911 /**
912 * Ensure that a network route exists to deliver traffic to the specified
913 * host via the mobile data network.
914 * @param hostAddress the IP address of the host to which the route is desired,
915 * in network byte order.
Robert Greenwalt47f69fe2010-06-15 15:43:39 -0700916 * TODO - deprecate
Irfan Sheriffd649c122010-06-09 15:39:36 -0700917 * @return {@code true} on success, {@code false} on failure
918 */
Robert Greenwaltf43396c2011-05-06 17:10:53 -0700919 private boolean addHostRoute(NetworkStateTracker nt, InetAddress hostAddress, int cycleCount) {
Robert Greenwaltf43396c2011-05-06 17:10:53 -0700920 LinkProperties lp = nt.getLinkProperties();
921 if ((lp == null) || (hostAddress == null)) return false;
Irfan Sheriffd649c122010-06-09 15:39:36 -0700922
Robert Greenwaltf43396c2011-05-06 17:10:53 -0700923 String interfaceName = lp.getInterfaceName();
Irfan Sheriffd649c122010-06-09 15:39:36 -0700924 if (DBG) {
Robert Greenwaltf43396c2011-05-06 17:10:53 -0700925 log("Requested host route to " + hostAddress + "(" + interfaceName + "), cycleCount=" +
926 cycleCount);
Irfan Sheriffd649c122010-06-09 15:39:36 -0700927 }
Robert Greenwaltf43396c2011-05-06 17:10:53 -0700928 if (interfaceName == null) {
Wink Savilleed9c02b2010-12-03 12:01:38 -0800929 if (DBG) loge("addHostRoute failed due to null interface name");
Irfan Sheriffd649c122010-06-09 15:39:36 -0700930 return false;
931 }
Robert Greenwaltf43396c2011-05-06 17:10:53 -0700932
933 RouteInfo bestRoute = RouteInfo.selectBestRoute(lp.getRoutes(), hostAddress);
Robert Greenwalt59b1a4e2011-05-10 15:05:02 -0700934 InetAddress gatewayAddress = null;
Robert Greenwaltf43396c2011-05-06 17:10:53 -0700935 if (bestRoute != null) {
Robert Greenwalt59b1a4e2011-05-10 15:05:02 -0700936 gatewayAddress = bestRoute.getGateway();
Robert Greenwaltf43396c2011-05-06 17:10:53 -0700937 // if the best route is ourself, don't relf-reference, just add the host route
Robert Greenwalt59b1a4e2011-05-10 15:05:02 -0700938 if (hostAddress.equals(gatewayAddress)) gatewayAddress = null;
Robert Greenwaltf43396c2011-05-06 17:10:53 -0700939 }
Robert Greenwalt59b1a4e2011-05-10 15:05:02 -0700940 if (gatewayAddress != null) {
Robert Greenwaltf43396c2011-05-06 17:10:53 -0700941 if (cycleCount > MAX_HOSTROUTE_CYCLE_COUNT) {
942 loge("Error adding hostroute - too much recursion");
943 return false;
944 }
Robert Greenwalt59b1a4e2011-05-10 15:05:02 -0700945 if (!addHostRoute(nt, gatewayAddress, cycleCount+1)) return false;
Robert Greenwaltf43396c2011-05-06 17:10:53 -0700946 }
Robert Greenwalt59b1a4e2011-05-10 15:05:02 -0700947
948 RouteInfo route = RouteInfo.makeHostRoute(hostAddress, gatewayAddress);
949
950 try {
951 mNetd.addRoute(interfaceName, route);
952 return true;
953 } catch (Exception ex) {
954 return false;
955 }
Robert Greenwaltf43396c2011-05-06 17:10:53 -0700956 }
957
958 // TODO support the removal of single host routes. Keep a ref count of them so we
959 // aren't over-zealous
960 private boolean removeHostRoute(NetworkStateTracker nt, InetAddress hostAddress) {
961 return false;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800962 }
963
964 /**
965 * @see ConnectivityManager#getBackgroundDataSetting()
966 */
967 public boolean getBackgroundDataSetting() {
Robert Greenwaltd825ea42010-12-29 16:15:02 -0800968 return mBackgroundDataEnabled.get();
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800969 }
Robert Greenwalt86e9e552009-07-16 17:21:39 -0700970
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800971 /**
972 * @see ConnectivityManager#setBackgroundDataSetting(boolean)
973 */
974 public void setBackgroundDataSetting(boolean allowBackgroundDataUsage) {
975 mContext.enforceCallingOrSelfPermission(
976 android.Manifest.permission.CHANGE_BACKGROUND_DATA_SETTING,
977 "ConnectivityService");
Robert Greenwalt86e9e552009-07-16 17:21:39 -0700978
Robert Greenwaltd825ea42010-12-29 16:15:02 -0800979 mBackgroundDataEnabled.set(allowBackgroundDataUsage);
980
Robert Greenwalt8dcc28b2010-09-23 10:05:56 -0700981 mHandler.sendMessage(mHandler.obtainMessage(EVENT_SET_BACKGROUND_DATA,
982 (allowBackgroundDataUsage ? ENABLED : DISABLED), 0));
983 }
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800984
Robert Greenwalt8dcc28b2010-09-23 10:05:56 -0700985 private void handleSetBackgroundData(boolean enabled) {
Robert Greenwaltdb4afae2011-02-25 13:44:09 -0800986 Settings.Secure.putInt(mContext.getContentResolver(),
987 Settings.Secure.BACKGROUND_DATA, enabled ? 1 : 0);
988 Intent broadcast = new Intent(
989 ConnectivityManager.ACTION_BACKGROUND_DATA_SETTING_CHANGED);
990 mContext.sendBroadcast(broadcast);
Robert Greenwalt86e9e552009-07-16 17:21:39 -0700991 }
992
Robert Greenwaltc03fa502010-02-23 18:58:05 -0800993 /**
994 * @see ConnectivityManager#getMobileDataEnabled()
995 */
996 public boolean getMobileDataEnabled() {
Wink Savillee7982682010-12-07 10:31:02 -0800997 // TODO: This detail should probably be in DataConnectionTracker's
998 // which is where we store the value and maybe make this
999 // asynchronous.
Robert Greenwaltc03fa502010-02-23 18:58:05 -08001000 enforceAccessPermission();
1001 boolean retVal = Settings.Secure.getInt(mContext.getContentResolver(),
1002 Settings.Secure.MOBILE_DATA, 1) == 1;
Wink Savilleed9c02b2010-12-03 12:01:38 -08001003 if (DBG) log("getMobileDataEnabled returning " + retVal);
Robert Greenwaltc03fa502010-02-23 18:58:05 -08001004 return retVal;
1005 }
1006
Robert Greenwaltd55a6b42011-03-25 13:09:25 -07001007 public void setDataDependency(int networkType, boolean met) {
1008 enforceChangePermission();
1009 if (DBG) {
1010 log("setDataDependency(" + networkType + ", " + met + ")");
1011 }
1012 mHandler.sendMessage(mHandler.obtainMessage(EVENT_SET_DEPENDENCY_MET,
1013 (met ? ENABLED : DISABLED), networkType));
1014 }
1015
1016 private void handleSetDependencyMet(int networkType, boolean met) {
1017 if (mNetTrackers[networkType] != null) {
1018 if (DBG) {
1019 log("handleSetDependencyMet(" + networkType + ", " + met + ")");
1020 }
1021 mNetTrackers[networkType].setDependencyMet(met);
1022 }
1023 }
1024
Robert Greenwaltc03fa502010-02-23 18:58:05 -08001025 /**
1026 * @see ConnectivityManager#setMobileDataEnabled(boolean)
1027 */
Robert Greenwalt8dcc28b2010-09-23 10:05:56 -07001028 public void setMobileDataEnabled(boolean enabled) {
Robert Greenwaltc03fa502010-02-23 18:58:05 -08001029 enforceChangePermission();
Wink Savilleed9c02b2010-12-03 12:01:38 -08001030 if (DBG) log("setMobileDataEnabled(" + enabled + ")");
Robert Greenwaltc03fa502010-02-23 18:58:05 -08001031
Robert Greenwalt8dcc28b2010-09-23 10:05:56 -07001032 mHandler.sendMessage(mHandler.obtainMessage(EVENT_SET_MOBILE_DATA,
Robert Greenwaltd55a6b42011-03-25 13:09:25 -07001033 (enabled ? ENABLED : DISABLED), 0));
Robert Greenwalt8dcc28b2010-09-23 10:05:56 -07001034 }
1035
1036 private void handleSetMobileData(boolean enabled) {
Wink Savillee7982682010-12-07 10:31:02 -08001037 if (mNetTrackers[ConnectivityManager.TYPE_MOBILE] != null) {
1038 if (DBG) {
1039 Slog.d(TAG, mNetTrackers[ConnectivityManager.TYPE_MOBILE].toString() + enabled);
Robert Greenwaltc03fa502010-02-23 18:58:05 -08001040 }
Wink Savillee7982682010-12-07 10:31:02 -08001041 mNetTrackers[ConnectivityManager.TYPE_MOBILE].setDataEnable(enabled);
Robert Greenwaltc03fa502010-02-23 18:58:05 -08001042 }
1043 }
1044
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001045 private void enforceAccessPermission() {
Robert Greenwalt86e9e552009-07-16 17:21:39 -07001046 mContext.enforceCallingOrSelfPermission(
1047 android.Manifest.permission.ACCESS_NETWORK_STATE,
1048 "ConnectivityService");
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001049 }
1050
1051 private void enforceChangePermission() {
Robert Greenwalt86e9e552009-07-16 17:21:39 -07001052 mContext.enforceCallingOrSelfPermission(
1053 android.Manifest.permission.CHANGE_NETWORK_STATE,
1054 "ConnectivityService");
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001055 }
1056
Robert Greenwaltd0e18ff2010-01-26 11:40:34 -08001057 // TODO Make this a special check when it goes public
1058 private void enforceTetherChangePermission() {
1059 mContext.enforceCallingOrSelfPermission(
1060 android.Manifest.permission.CHANGE_NETWORK_STATE,
1061 "ConnectivityService");
1062 }
1063
Robert Greenwalt2a091d72010-02-11 18:18:40 -08001064 private void enforceTetherAccessPermission() {
1065 mContext.enforceCallingOrSelfPermission(
1066 android.Manifest.permission.ACCESS_NETWORK_STATE,
1067 "ConnectivityService");
1068 }
1069
Robert Greenwalt14f2ef42010-06-15 12:19:37 -07001070 private void enforceConnectivityInternalPermission() {
1071 mContext.enforceCallingOrSelfPermission(
1072 android.Manifest.permission.CONNECTIVITY_INTERNAL,
1073 "ConnectivityService");
1074 }
1075
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001076 /**
Robert Greenwalt86e9e552009-07-16 17:21:39 -07001077 * Handle a {@code DISCONNECTED} event. If this pertains to the non-active
1078 * network, we ignore it. If it is for the active network, we send out a
1079 * broadcast. But first, we check whether it might be possible to connect
1080 * to a different network.
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001081 * @param info the {@code NetworkInfo} for the network
1082 */
1083 private void handleDisconnect(NetworkInfo info) {
1084
Robert Greenwalt42acef32009-08-12 16:08:25 -07001085 int prevNetType = info.getType();
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001086
Robert Greenwalt42acef32009-08-12 16:08:25 -07001087 mNetTrackers[prevNetType].setTeardownRequested(false);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001088 /*
1089 * If the disconnected network is not the active one, then don't report
1090 * this as a loss of connectivity. What probably happened is that we're
1091 * getting the disconnect for a network that we explicitly disabled
1092 * in accordance with network preference policies.
1093 */
Robert Greenwaltd55a6b42011-03-25 13:09:25 -07001094 if (!mNetConfigs[prevNetType].isDefault()) {
Robert Greenwalt42acef32009-08-12 16:08:25 -07001095 List pids = mNetRequestersPids[prevNetType];
1096 for (int i = 0; i<pids.size(); i++) {
1097 Integer pid = (Integer)pids.get(i);
1098 // will remove them because the net's no longer connected
1099 // need to do this now as only now do we know the pids and
1100 // can properly null things that are no longer referenced.
1101 reassessPidDns(pid.intValue(), false);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001102 }
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001103 }
1104
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001105 Intent intent = new Intent(ConnectivityManager.CONNECTIVITY_ACTION);
1106 intent.putExtra(ConnectivityManager.EXTRA_NETWORK_INFO, info);
1107 if (info.isFailover()) {
1108 intent.putExtra(ConnectivityManager.EXTRA_IS_FAILOVER, true);
1109 info.setFailover(false);
1110 }
1111 if (info.getReason() != null) {
1112 intent.putExtra(ConnectivityManager.EXTRA_REASON, info.getReason());
1113 }
1114 if (info.getExtraInfo() != null) {
Robert Greenwalt86e9e552009-07-16 17:21:39 -07001115 intent.putExtra(ConnectivityManager.EXTRA_EXTRA_INFO,
1116 info.getExtraInfo());
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001117 }
Robert Greenwalt42acef32009-08-12 16:08:25 -07001118
Robert Greenwaltd55a6b42011-03-25 13:09:25 -07001119 if (mNetConfigs[prevNetType].isDefault()) {
Robert Greenwaltf21ef7d2011-01-11 13:56:33 -08001120 tryFailover(prevNetType);
1121 if (mActiveDefaultNetwork != -1) {
1122 NetworkInfo switchTo = mNetTrackers[mActiveDefaultNetwork].getNetworkInfo();
Robert Greenwaltcc4b4012010-01-25 17:54:29 -08001123 intent.putExtra(ConnectivityManager.EXTRA_OTHER_NETWORK_INFO, switchTo);
1124 } else {
Robert Greenwalt029be812010-09-20 18:01:43 -07001125 mDefaultInetConditionPublished = 0; // we're not connected anymore
Robert Greenwaltcc4b4012010-01-25 17:54:29 -08001126 intent.putExtra(ConnectivityManager.EXTRA_NO_CONNECTIVITY, true);
1127 }
Robert Greenwaltda03c4e2010-01-20 19:29:41 -08001128 }
Robert Greenwalt029be812010-09-20 18:01:43 -07001129 intent.putExtra(ConnectivityManager.EXTRA_INET_CONDITION, mDefaultInetConditionPublished);
Robert Greenwaltec896c62011-06-15 12:22:07 -07001130
1131 // Reset interface if no other connections are using the same interface
1132 boolean doReset = true;
1133 LinkProperties linkProperties = mNetTrackers[prevNetType].getLinkProperties();
1134 if (linkProperties != null) {
1135 String oldIface = linkProperties.getInterfaceName();
1136 if (TextUtils.isEmpty(oldIface) == false) {
1137 for (NetworkStateTracker networkStateTracker : mNetTrackers) {
1138 if (networkStateTracker == null) continue;
1139 NetworkInfo networkInfo = networkStateTracker.getNetworkInfo();
1140 if (networkInfo.isConnected() && networkInfo.getType() != prevNetType) {
1141 LinkProperties l = networkStateTracker.getLinkProperties();
1142 if (l == null) continue;
1143 if (oldIface.equals(l.getInterfaceName())) {
1144 doReset = false;
1145 break;
1146 }
1147 }
1148 }
1149 }
1150 }
1151
Robert Greenwaltda03c4e2010-01-20 19:29:41 -08001152 // do this before we broadcast the change
Robert Greenwaltec896c62011-06-15 12:22:07 -07001153 handleConnectivityChange(prevNetType, doReset);
Robert Greenwaltda03c4e2010-01-20 19:29:41 -08001154
1155 sendStickyBroadcast(intent);
1156 /*
1157 * If the failover network is already connected, then immediately send
1158 * out a followup broadcast indicating successful failover
1159 */
Robert Greenwaltf21ef7d2011-01-11 13:56:33 -08001160 if (mActiveDefaultNetwork != -1) {
1161 sendConnectedBroadcast(mNetTrackers[mActiveDefaultNetwork].getNetworkInfo());
Robert Greenwaltda03c4e2010-01-20 19:29:41 -08001162 }
1163 }
1164
Robert Greenwaltf21ef7d2011-01-11 13:56:33 -08001165 private void tryFailover(int prevNetType) {
Robert Greenwalt42acef32009-08-12 16:08:25 -07001166 /*
Robert Greenwaltbff90182011-01-06 15:41:07 -08001167 * If this is a default network, check if other defaults are available.
1168 * Try to reconnect on all available and let them hash it out when
1169 * more than one connects.
Robert Greenwalt42acef32009-08-12 16:08:25 -07001170 */
Robert Greenwaltd55a6b42011-03-25 13:09:25 -07001171 if (mNetConfigs[prevNetType].isDefault()) {
Robert Greenwalt42acef32009-08-12 16:08:25 -07001172 if (mActiveDefaultNetwork == prevNetType) {
1173 mActiveDefaultNetwork = -1;
1174 }
1175
Robert Greenwaltf21ef7d2011-01-11 13:56:33 -08001176 // don't signal a reconnect for anything lower or equal priority than our
1177 // current connected default
1178 // TODO - don't filter by priority now - nice optimization but risky
1179// int currentPriority = -1;
1180// if (mActiveDefaultNetwork != -1) {
Robert Greenwaltd55a6b42011-03-25 13:09:25 -07001181// currentPriority = mNetConfigs[mActiveDefaultNetwork].mPriority;
Robert Greenwaltf21ef7d2011-01-11 13:56:33 -08001182// }
Robert Greenwaltda03c4e2010-01-20 19:29:41 -08001183 for (int checkType=0; checkType <= ConnectivityManager.MAX_NETWORK_TYPE; checkType++) {
Robert Greenwalt5154ae762009-10-30 14:17:42 -07001184 if (checkType == prevNetType) continue;
Robert Greenwaltd55a6b42011-03-25 13:09:25 -07001185 if (mNetConfigs[checkType] == null) continue;
1186 if (!mNetConfigs[checkType].isDefault()) continue;
Wink Saville9f7a0b22011-01-26 15:43:49 -08001187
1188// Enabling the isAvailable() optimization caused mobile to not get
1189// selected if it was in the middle of error handling. Specifically
1190// a moble connection that took 30 seconds to complete the DEACTIVATE_DATA_CALL
1191// would not be available and we wouldn't get connected to anything.
1192// So removing the isAvailable() optimization below for now. TODO: This
1193// optimization should work and we need to investigate why it doesn't work.
1194// This could be related to how DEACTIVATE_DATA_CALL is reporting its
1195// complete before it is really complete.
1196// if (!mNetTrackers[checkType].isAvailable()) continue;
1197
Robert Greenwaltd55a6b42011-03-25 13:09:25 -07001198// if (currentPriority >= mNetConfigs[checkType].mPriority) continue;
Robert Greenwalt42acef32009-08-12 16:08:25 -07001199
Robert Greenwaltf21ef7d2011-01-11 13:56:33 -08001200 NetworkStateTracker checkTracker = mNetTrackers[checkType];
1201 NetworkInfo checkInfo = checkTracker.getNetworkInfo();
1202 if (!checkInfo.isConnectedOrConnecting() || checkTracker.isTeardownRequested()) {
1203 checkInfo.setFailover(true);
1204 checkTracker.reconnect();
Robert Greenwalt42acef32009-08-12 16:08:25 -07001205 }
Robert Greenwaltf21ef7d2011-01-11 13:56:33 -08001206 if (DBG) log("Attempting to switch to " + checkInfo.getTypeName());
Robert Greenwalt42acef32009-08-12 16:08:25 -07001207 }
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001208 }
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001209 }
1210
1211 private void sendConnectedBroadcast(NetworkInfo info) {
Robert Greenwalt1e9aac22010-09-15 17:36:33 -07001212 sendGeneralBroadcast(info, ConnectivityManager.CONNECTIVITY_ACTION);
1213 }
1214
1215 private void sendInetConditionBroadcast(NetworkInfo info) {
1216 sendGeneralBroadcast(info, ConnectivityManager.INET_CONDITION_ACTION);
1217 }
1218
1219 private void sendGeneralBroadcast(NetworkInfo info, String bcastType) {
1220 Intent intent = new Intent(bcastType);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001221 intent.putExtra(ConnectivityManager.EXTRA_NETWORK_INFO, info);
1222 if (info.isFailover()) {
1223 intent.putExtra(ConnectivityManager.EXTRA_IS_FAILOVER, true);
1224 info.setFailover(false);
1225 }
1226 if (info.getReason() != null) {
1227 intent.putExtra(ConnectivityManager.EXTRA_REASON, info.getReason());
1228 }
1229 if (info.getExtraInfo() != null) {
Robert Greenwalt86e9e552009-07-16 17:21:39 -07001230 intent.putExtra(ConnectivityManager.EXTRA_EXTRA_INFO,
1231 info.getExtraInfo());
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001232 }
Robert Greenwaltd7085fc2010-09-08 15:24:47 -07001233 intent.putExtra(ConnectivityManager.EXTRA_INET_CONDITION, mDefaultInetConditionPublished);
Mike Lockwood0f79b542009-08-14 14:18:49 -04001234 sendStickyBroadcast(intent);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001235 }
1236
1237 /**
1238 * Called when an attempt to fail over to another network has failed.
1239 * @param info the {@link NetworkInfo} for the failed network
1240 */
1241 private void handleConnectionFailure(NetworkInfo info) {
1242 mNetTrackers[info.getType()].setTeardownRequested(false);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001243
Robert Greenwalt42acef32009-08-12 16:08:25 -07001244 String reason = info.getReason();
1245 String extraInfo = info.getExtraInfo();
Robert Greenwalt86e9e552009-07-16 17:21:39 -07001246
Robert Greenwalt572172b2010-10-08 16:35:52 -07001247 String reasonText;
1248 if (reason == null) {
1249 reasonText = ".";
1250 } else {
1251 reasonText = " (" + reason + ").";
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001252 }
Wink Savilleed9c02b2010-12-03 12:01:38 -08001253 loge("Attempt to connect to " + info.getTypeName() + " failed" + reasonText);
Robert Greenwalt42acef32009-08-12 16:08:25 -07001254
1255 Intent intent = new Intent(ConnectivityManager.CONNECTIVITY_ACTION);
1256 intent.putExtra(ConnectivityManager.EXTRA_NETWORK_INFO, info);
1257 if (getActiveNetworkInfo() == null) {
1258 intent.putExtra(ConnectivityManager.EXTRA_NO_CONNECTIVITY, true);
1259 }
1260 if (reason != null) {
1261 intent.putExtra(ConnectivityManager.EXTRA_REASON, reason);
1262 }
1263 if (extraInfo != null) {
1264 intent.putExtra(ConnectivityManager.EXTRA_EXTRA_INFO, extraInfo);
1265 }
1266 if (info.isFailover()) {
1267 intent.putExtra(ConnectivityManager.EXTRA_IS_FAILOVER, true);
1268 info.setFailover(false);
1269 }
Robert Greenwaltda03c4e2010-01-20 19:29:41 -08001270
Robert Greenwaltd55a6b42011-03-25 13:09:25 -07001271 if (mNetConfigs[info.getType()].isDefault()) {
Robert Greenwaltf21ef7d2011-01-11 13:56:33 -08001272 tryFailover(info.getType());
1273 if (mActiveDefaultNetwork != -1) {
1274 NetworkInfo switchTo = mNetTrackers[mActiveDefaultNetwork].getNetworkInfo();
Robert Greenwaltcc4b4012010-01-25 17:54:29 -08001275 intent.putExtra(ConnectivityManager.EXTRA_OTHER_NETWORK_INFO, switchTo);
1276 } else {
Robert Greenwalt029be812010-09-20 18:01:43 -07001277 mDefaultInetConditionPublished = 0;
Robert Greenwaltcc4b4012010-01-25 17:54:29 -08001278 intent.putExtra(ConnectivityManager.EXTRA_NO_CONNECTIVITY, true);
1279 }
Robert Greenwaltda03c4e2010-01-20 19:29:41 -08001280 }
Robert Greenwaltcc4b4012010-01-25 17:54:29 -08001281
Robert Greenwalt029be812010-09-20 18:01:43 -07001282 intent.putExtra(ConnectivityManager.EXTRA_INET_CONDITION, mDefaultInetConditionPublished);
Mike Lockwood0f79b542009-08-14 14:18:49 -04001283 sendStickyBroadcast(intent);
Robert Greenwaltda03c4e2010-01-20 19:29:41 -08001284 /*
1285 * If the failover network is already connected, then immediately send
1286 * out a followup broadcast indicating successful failover
1287 */
Robert Greenwaltf21ef7d2011-01-11 13:56:33 -08001288 if (mActiveDefaultNetwork != -1) {
1289 sendConnectedBroadcast(mNetTrackers[mActiveDefaultNetwork].getNetworkInfo());
Robert Greenwaltda03c4e2010-01-20 19:29:41 -08001290 }
Mike Lockwood0f79b542009-08-14 14:18:49 -04001291 }
1292
1293 private void sendStickyBroadcast(Intent intent) {
1294 synchronized(this) {
Dianne Hackborn1c633fc2009-12-08 19:45:14 -08001295 if (!mSystemReady) {
1296 mInitialBroadcast = new Intent(intent);
Mike Lockwood0f79b542009-08-14 14:18:49 -04001297 }
Dianne Hackborn1c633fc2009-12-08 19:45:14 -08001298 intent.addFlags(Intent.FLAG_RECEIVER_REGISTERED_ONLY_BEFORE_BOOT);
1299 mContext.sendStickyBroadcast(intent);
Mike Lockwood0f79b542009-08-14 14:18:49 -04001300 }
1301 }
1302
1303 void systemReady() {
Robert Greenwalt59b1a4e2011-05-10 15:05:02 -07001304 IBinder b = ServiceManager.getService(Context.NETWORKMANAGEMENT_SERVICE);
1305 mNetd = INetworkManagementService.Stub.asInterface(b);
1306
Mike Lockwood0f79b542009-08-14 14:18:49 -04001307 synchronized(this) {
1308 mSystemReady = true;
Dianne Hackborn1c633fc2009-12-08 19:45:14 -08001309 if (mInitialBroadcast != null) {
1310 mContext.sendStickyBroadcast(mInitialBroadcast);
1311 mInitialBroadcast = null;
Mike Lockwood0f79b542009-08-14 14:18:49 -04001312 }
1313 }
Robert Greenwalt434203a2010-10-11 16:00:27 -07001314 // load the global proxy at startup
1315 mHandler.sendMessage(mHandler.obtainMessage(EVENT_APPLY_GLOBAL_HTTP_PROXY));
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001316 }
1317
1318 private void handleConnect(NetworkInfo info) {
Robert Greenwalt42acef32009-08-12 16:08:25 -07001319 int type = info.getType();
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001320
1321 // snapshot isFailover, because sendConnectedBroadcast() resets it
1322 boolean isFailover = info.isFailover();
Robert Greenwalt42acef32009-08-12 16:08:25 -07001323 NetworkStateTracker thisNet = mNetTrackers[type];
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001324
Robert Greenwalt42acef32009-08-12 16:08:25 -07001325 // if this is a default net and other default is running
1326 // kill the one not preferred
Robert Greenwaltd55a6b42011-03-25 13:09:25 -07001327 if (mNetConfigs[type].isDefault()) {
Robert Greenwalt42acef32009-08-12 16:08:25 -07001328 if (mActiveDefaultNetwork != -1 && mActiveDefaultNetwork != type) {
1329 if ((type != mNetworkPreference &&
Wink Saville975c8482011-04-07 14:23:45 -07001330 mNetConfigs[mActiveDefaultNetwork].priority >
1331 mNetConfigs[type].priority) ||
Robert Greenwalt42acef32009-08-12 16:08:25 -07001332 mNetworkPreference == mActiveDefaultNetwork) {
1333 // don't accept this one
Wink Savilleed9c02b2010-12-03 12:01:38 -08001334 if (DBG) {
1335 log("Not broadcasting CONNECT_ACTION " +
Robert Greenwalt42acef32009-08-12 16:08:25 -07001336 "to torn down network " + info.getTypeName());
Wink Savilleed9c02b2010-12-03 12:01:38 -08001337 }
Robert Greenwalt42acef32009-08-12 16:08:25 -07001338 teardown(thisNet);
1339 return;
1340 } else {
1341 // tear down the other
1342 NetworkStateTracker otherNet =
1343 mNetTrackers[mActiveDefaultNetwork];
Wink Savilleed9c02b2010-12-03 12:01:38 -08001344 if (DBG) {
1345 log("Policy requires " + otherNet.getNetworkInfo().getTypeName() +
Robert Greenwalt42acef32009-08-12 16:08:25 -07001346 " teardown");
Wink Savilleed9c02b2010-12-03 12:01:38 -08001347 }
Robert Greenwalt42acef32009-08-12 16:08:25 -07001348 if (!teardown(otherNet)) {
Wink Savilleed9c02b2010-12-03 12:01:38 -08001349 loge("Network declined teardown request");
Robert Greenwalt42acef32009-08-12 16:08:25 -07001350 return;
1351 }
Robert Greenwalt14f2ef42010-06-15 12:19:37 -07001352 }
1353 }
1354 synchronized (ConnectivityService.this) {
1355 // have a new default network, release the transition wakelock in a second
1356 // if it's held. The second pause is to allow apps to reconnect over the
1357 // new network
1358 if (mNetTransitionWakeLock.isHeld()) {
1359 mHandler.sendMessageDelayed(mHandler.obtainMessage(
Robert Greenwaltf3331232010-09-24 14:32:21 -07001360 EVENT_CLEAR_NET_TRANSITION_WAKELOCK,
Robert Greenwalt14f2ef42010-06-15 12:19:37 -07001361 mNetTransitionWakeLockSerialNumber, 0),
1362 1000);
Robert Greenwalt42acef32009-08-12 16:08:25 -07001363 }
1364 }
1365 mActiveDefaultNetwork = type;
Robert Greenwaltd7085fc2010-09-08 15:24:47 -07001366 // this will cause us to come up initially as unconnected and switching
1367 // to connected after our normal pause unless somebody reports us as reall
1368 // disconnected
1369 mDefaultInetConditionPublished = 0;
1370 mDefaultConnectionSequence++;
1371 mInetConditionChangeInFlight = false;
1372 // Don't do this - if we never sign in stay, grey
1373 //reportNetworkCondition(mActiveDefaultNetwork, 100);
Robert Greenwalt42acef32009-08-12 16:08:25 -07001374 }
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001375 thisNet.setTeardownRequested(false);
Irfan Sheriffd649c122010-06-09 15:39:36 -07001376 updateNetworkSettings(thisNet);
Robert Greenwaltec896c62011-06-15 12:22:07 -07001377 handleConnectivityChange(type, false);
Robert Greenwalt42acef32009-08-12 16:08:25 -07001378 sendConnectedBroadcast(info);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001379 }
1380
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001381 /**
Robert Greenwaltc76b8fa2010-07-23 15:46:26 -07001382 * After a change in the connectivity state of a network. We're mainly
1383 * concerned with making sure that the list of DNS servers is set up
1384 * according to which networks are connected, and ensuring that the
1385 * right routing table entries exist.
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001386 */
Robert Greenwaltec896c62011-06-15 12:22:07 -07001387 private void handleConnectivityChange(int netType, boolean doReset) {
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001388 /*
Robert Greenwalt42acef32009-08-12 16:08:25 -07001389 * If a non-default network is enabled, add the host routes that
Robert Greenwaltc76b8fa2010-07-23 15:46:26 -07001390 * will allow it's DNS servers to be accessed.
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001391 */
Robert Greenwaltc76b8fa2010-07-23 15:46:26 -07001392 handleDnsConfigurationChange(netType);
Robert Greenwalt42acef32009-08-12 16:08:25 -07001393
Robert Greenwaltc76b8fa2010-07-23 15:46:26 -07001394 if (mNetTrackers[netType].getNetworkInfo().isConnected()) {
Robert Greenwaltd55a6b42011-03-25 13:09:25 -07001395 if (mNetConfigs[netType].isDefault()) {
Robert Greenwalt434203a2010-10-11 16:00:27 -07001396 handleApplyDefaultProxy(netType);
Robert Greenwaltc76b8fa2010-07-23 15:46:26 -07001397 addDefaultRoute(mNetTrackers[netType]);
Robert Greenwalt42acef32009-08-12 16:08:25 -07001398 } else {
Robert Greenwaltc76b8fa2010-07-23 15:46:26 -07001399 addPrivateDnsRoutes(mNetTrackers[netType]);
1400 }
1401 } else {
Robert Greenwaltd55a6b42011-03-25 13:09:25 -07001402 if (mNetConfigs[netType].isDefault()) {
Robert Greenwaltc76b8fa2010-07-23 15:46:26 -07001403 removeDefaultRoute(mNetTrackers[netType]);
1404 } else {
1405 removePrivateDnsRoutes(mNetTrackers[netType]);
Robert Greenwalt42acef32009-08-12 16:08:25 -07001406 }
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001407 }
Robert Greenwaltec896c62011-06-15 12:22:07 -07001408
1409 if (doReset) {
1410 LinkProperties linkProperties = mNetTrackers[netType].getLinkProperties();
1411 if (linkProperties != null) {
1412 String iface = linkProperties.getInterfaceName();
1413 if (TextUtils.isEmpty(iface) == false) {
1414 if (DBG) log("resetConnections(" + iface + ")");
1415 NetworkUtils.resetConnections(iface);
1416 }
1417 }
1418 }
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001419 }
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 Greenwaltec896c62011-06-15 12:22:07 -07001861 handleConnectivityChange(info.getType(), true);
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
2248 private void log(String s) {
2249 Slog.d(TAG, s);
2250 }
2251
2252 private void loge(String s) {
2253 Slog.e(TAG, s);
2254 }
Wink Saville9d7d6282011-03-12 14:52:01 -08002255 int convertFeatureToNetworkType(String feature){
2256 int networkType = -1;
2257 if (TextUtils.equals(feature, Phone.FEATURE_ENABLE_MMS)) {
2258 networkType = ConnectivityManager.TYPE_MOBILE_MMS;
2259 } else if (TextUtils.equals(feature, Phone.FEATURE_ENABLE_SUPL)) {
2260 networkType = ConnectivityManager.TYPE_MOBILE_SUPL;
2261 } else if (TextUtils.equals(feature, Phone.FEATURE_ENABLE_DUN) ||
2262 TextUtils.equals(feature, Phone.FEATURE_ENABLE_DUN_ALWAYS)) {
2263 networkType = ConnectivityManager.TYPE_MOBILE_DUN;
2264 } else if (TextUtils.equals(feature, Phone.FEATURE_ENABLE_HIPRI)) {
2265 networkType = ConnectivityManager.TYPE_MOBILE_HIPRI;
2266 } else if (TextUtils.equals(feature, Phone.FEATURE_ENABLE_FOTA)) {
2267 networkType = ConnectivityManager.TYPE_MOBILE_FOTA;
2268 } else if (TextUtils.equals(feature, Phone.FEATURE_ENABLE_IMS)) {
2269 networkType = ConnectivityManager.TYPE_MOBILE_IMS;
2270 } else if (TextUtils.equals(feature, Phone.FEATURE_ENABLE_CBS)) {
2271 networkType = ConnectivityManager.TYPE_MOBILE_CBS;
2272 }
2273 return networkType;
2274 }
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002275}