blob: db831c79b29e4a6ebce65f11af2752e40e79989b [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 Goby19970692010-12-22 14:29:40 -080027import android.net.EthernetDataTracker;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080028import android.net.IConnectivityManager;
Jaikumar Ganesh15c74392010-12-21 22:31:44 -080029import android.net.LinkProperties;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080030import android.net.MobileDataStateTracker;
Robert Greenwaltd55a6b42011-03-25 13:09:25 -070031import android.net.NetworkConfig;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080032import android.net.NetworkInfo;
33import android.net.NetworkStateTracker;
Robert Greenwalt585ac0f2010-08-27 09:24:29 -070034import android.net.NetworkUtils;
Robert Greenwalt434203a2010-10-11 16:00:27 -070035import android.net.Proxy;
36import android.net.ProxyProperties;
Robert Greenwaltaa70f102011-04-28 14:28:50 -070037import android.net.RouteInfo;
Hung-ying Tyan6b818de2011-01-19 16:48:38 +080038import android.net.vpn.VpnManager;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080039import android.net.wifi.WifiStateTracker;
40import android.os.Binder;
41import android.os.Handler;
Wink Savillebb08caf2010-09-02 19:23:52 -070042import android.os.HandlerThread;
Robert Greenwalt42acef32009-08-12 16:08:25 -070043import android.os.IBinder;
Chia-chi Yehc9338302011-05-11 16:35:13 -070044import android.os.INetworkManagementService;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080045import android.os.Looper;
46import android.os.Message;
Robert Greenwalt14f2ef42010-06-15 12:19:37 -070047import android.os.PowerManager;
Robert Greenwalt42acef32009-08-12 16:08:25 -070048import android.os.RemoteException;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080049import android.os.ServiceManager;
50import android.os.SystemProperties;
51import android.provider.Settings;
Robert Greenwalt42acef32009-08-12 16:08:25 -070052import android.text.TextUtils;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080053import android.util.EventLog;
Joe Onorato8a9b2202010-02-26 18:56:32 -080054import android.util.Slog;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080055
Robert Greenwalt42acef32009-08-12 16:08:25 -070056import com.android.internal.telephony.Phone;
Robert Greenwaltd0e18ff2010-01-26 11:40:34 -080057import com.android.server.connectivity.Tethering;
58
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080059import java.io.FileDescriptor;
Irfan Sheriffd649c122010-06-09 15:39:36 -070060import java.io.FileWriter;
61import java.io.IOException;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080062import java.io.PrintWriter;
Robert Greenwalt47f69fe2010-06-15 15:43:39 -070063import java.net.InetAddress;
64import java.net.UnknownHostException;
Robert Greenwalt42acef32009-08-12 16:08:25 -070065import java.util.ArrayList;
Robert Greenwalt47f69fe2010-06-15 15:43:39 -070066import java.util.Collection;
Robert Greenwaltd825ea42010-12-29 16:15:02 -080067import java.util.concurrent.atomic.AtomicBoolean;
Robert Greenwalt4e8dfef2010-09-20 14:35:25 -070068import java.util.GregorianCalendar;
Robert Greenwalt42acef32009-08-12 16:08:25 -070069import java.util.List;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080070
71/**
72 * @hide
73 */
74public class ConnectivityService extends IConnectivityManager.Stub {
75
Robert Greenwaltba175a52010-10-05 19:12:26 -070076 private static final boolean DBG = true;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080077 private static final String TAG = "ConnectivityService";
78
Robert Greenwalt42acef32009-08-12 16:08:25 -070079 // how long to wait before switching back to a radio's default network
80 private static final int RESTORE_DEFAULT_NETWORK_DELAY = 1 * 60 * 1000;
81 // system property that can override the above value
82 private static final String NETWORK_RESTORE_DELAY_PROP_NAME =
83 "android.telephony.apn-restore";
84
Robert Greenwaltf43396c2011-05-06 17:10:53 -070085 // used in recursive route setting to add gateways for the host for which
86 // a host route was requested.
87 private static final int MAX_HOSTROUTE_CYCLE_COUNT = 10;
88
Robert Greenwaltd0e18ff2010-01-26 11:40:34 -080089 private Tethering mTethering;
Robert Greenwaltc9d5fb72010-02-25 12:29:30 -080090 private boolean mTetheringConfigValid = false;
Robert Greenwaltd0e18ff2010-01-26 11:40:34 -080091
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080092 /**
93 * Sometimes we want to refer to the individual network state
94 * trackers separately, and sometimes we just want to treat them
95 * abstractly.
96 */
97 private NetworkStateTracker mNetTrackers[];
Robert Greenwalt42acef32009-08-12 16:08:25 -070098
99 /**
100 * A per Net list of the PID's that requested access to the net
101 * used both as a refcount and for per-PID DNS selection
102 */
103 private List mNetRequestersPids[];
104
Irfan Sheriffa2a1b912010-06-07 09:03:04 -0700105 private WifiWatchdogService mWifiWatchdogService;
106
Robert Greenwalt42acef32009-08-12 16:08:25 -0700107 // priority order of the nettrackers
108 // (excluding dynamically set mNetworkPreference)
109 // TODO - move mNetworkTypePreference into this
110 private int[] mPriorityList;
111
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800112 private Context mContext;
113 private int mNetworkPreference;
Robert Greenwalt42acef32009-08-12 16:08:25 -0700114 private int mActiveDefaultNetwork = -1;
Robert Greenwaltd7085fc2010-09-08 15:24:47 -0700115 // 0 is full bad, 100 is full good
116 private int mDefaultInetCondition = 0;
117 private int mDefaultInetConditionPublished = 0;
118 private boolean mInetConditionChangeInFlight = false;
119 private int mDefaultConnectionSequence = 0;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800120
121 private int mNumDnsEntries;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800122
123 private boolean mTestMode;
Joe Onorato00092872010-09-01 21:18:22 -0700124 private static ConnectivityService sServiceInstance;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800125
Robert Greenwaltd825ea42010-12-29 16:15:02 -0800126 private AtomicBoolean mBackgroundDataEnabled = new AtomicBoolean(true);
127
Robert Greenwalt8dcc28b2010-09-23 10:05:56 -0700128 private static final int ENABLED = 1;
129 private static final int DISABLED = 0;
130
131 // Share the event space with NetworkStateTracker (which can't see this
132 // internal class but sends us events). If you change these, change
133 // NetworkStateTracker.java too.
134 private static final int MIN_NETWORK_STATE_TRACKER_EVENT = 1;
135 private static final int MAX_NETWORK_STATE_TRACKER_EVENT = 100;
136
137 /**
138 * used internally as a delayed event to make us switch back to the
139 * default network
140 */
141 private static final int EVENT_RESTORE_DEFAULT_NETWORK =
142 MAX_NETWORK_STATE_TRACKER_EVENT + 1;
143
144 /**
145 * used internally to change our mobile data enabled flag
146 */
147 private static final int EVENT_CHANGE_MOBILE_DATA_ENABLED =
148 MAX_NETWORK_STATE_TRACKER_EVENT + 2;
149
150 /**
151 * used internally to change our network preference setting
152 * arg1 = networkType to prefer
153 */
154 private static final int EVENT_SET_NETWORK_PREFERENCE =
155 MAX_NETWORK_STATE_TRACKER_EVENT + 3;
156
157 /**
158 * used internally to synchronize inet condition reports
159 * arg1 = networkType
160 * arg2 = condition (0 bad, 100 good)
161 */
162 private static final int EVENT_INET_CONDITION_CHANGE =
163 MAX_NETWORK_STATE_TRACKER_EVENT + 4;
164
165 /**
166 * used internally to mark the end of inet condition hold periods
167 * arg1 = networkType
168 */
169 private static final int EVENT_INET_CONDITION_HOLD_END =
170 MAX_NETWORK_STATE_TRACKER_EVENT + 5;
171
172 /**
173 * used internally to set the background data preference
174 * arg1 = TRUE for enabled, FALSE for disabled
175 */
176 private static final int EVENT_SET_BACKGROUND_DATA =
177 MAX_NETWORK_STATE_TRACKER_EVENT + 6;
178
179 /**
180 * used internally to set enable/disable cellular data
181 * arg1 = ENBALED or DISABLED
182 */
183 private static final int EVENT_SET_MOBILE_DATA =
184 MAX_NETWORK_STATE_TRACKER_EVENT + 7;
185
Robert Greenwaltf3331232010-09-24 14:32:21 -0700186 /**
187 * used internally to clear a wakelock when transitioning
188 * from one net to another
189 */
190 private static final int EVENT_CLEAR_NET_TRANSITION_WAKELOCK =
191 MAX_NETWORK_STATE_TRACKER_EVENT + 8;
192
Robert Greenwalt434203a2010-10-11 16:00:27 -0700193 /**
194 * used internally to reload global proxy settings
195 */
196 private static final int EVENT_APPLY_GLOBAL_HTTP_PROXY =
197 MAX_NETWORK_STATE_TRACKER_EVENT + 9;
198
Robert Greenwaltd55a6b42011-03-25 13:09:25 -0700199 /**
200 * used internally to set external dependency met/unmet
201 * arg1 = ENABLED (met) or DISABLED (unmet)
202 * arg2 = NetworkType
203 */
204 private static final int EVENT_SET_DEPENDENCY_MET =
205 MAX_NETWORK_STATE_TRACKER_EVENT + 10;
206
Robert Greenwalt42acef32009-08-12 16:08:25 -0700207 private Handler mHandler;
208
209 // list of DeathRecipients used to make sure features are turned off when
210 // a process dies
211 private List mFeatureUsers;
212
Mike Lockwood0f79b542009-08-14 14:18:49 -0400213 private boolean mSystemReady;
Dianne Hackborn1c633fc2009-12-08 19:45:14 -0800214 private Intent mInitialBroadcast;
Mike Lockwood0f79b542009-08-14 14:18:49 -0400215
Robert Greenwalt14f2ef42010-06-15 12:19:37 -0700216 private PowerManager.WakeLock mNetTransitionWakeLock;
217 private String mNetTransitionWakeLockCausedBy = "";
218 private int mNetTransitionWakeLockSerialNumber;
219 private int mNetTransitionWakeLockTimeout;
220
Robert Greenwalte90aa5e2010-09-01 11:34:05 -0700221 private InetAddress mDefaultDns;
222
Robert Greenwalt4e8dfef2010-09-20 14:35:25 -0700223 // used in DBG mode to track inet condition reports
224 private static final int INET_CONDITION_LOG_MAX_SIZE = 15;
225 private ArrayList mInetLog;
226
Robert Greenwalt434203a2010-10-11 16:00:27 -0700227 // track the current default http proxy - tell the world if we get a new one (real change)
228 private ProxyProperties mDefaultProxy = null;
229 // track the global proxy.
230 private ProxyProperties mGlobalProxy = null;
231 private final Object mGlobalProxyLock = new Object();
232
233 private SettingsObserver mSettingsObserver;
234
Robert Greenwaltd55a6b42011-03-25 13:09:25 -0700235 NetworkConfig[] mNetConfigs;
Robert Greenwalt5154ae762009-10-30 14:17:42 -0700236 int mNetworksDefined;
Robert Greenwalt42acef32009-08-12 16:08:25 -0700237
Robert Greenwalt511288a2009-12-07 11:33:18 -0800238 private static class RadioAttributes {
Robert Greenwalt42acef32009-08-12 16:08:25 -0700239 public int mSimultaneity;
240 public int mType;
241 public RadioAttributes(String init) {
242 String fragments[] = init.split(",");
Robert Greenwalt5154ae762009-10-30 14:17:42 -0700243 mType = Integer.parseInt(fragments[0]);
244 mSimultaneity = Integer.parseInt(fragments[1]);
Robert Greenwalt42acef32009-08-12 16:08:25 -0700245 }
246 }
247 RadioAttributes[] mRadioAttributes;
248
Wink Savillebb08caf2010-09-02 19:23:52 -0700249 public static synchronized ConnectivityService getInstance(Context context) {
250 if (sServiceInstance == null) {
251 sServiceInstance = new ConnectivityService(context);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800252 }
Wink Savillebb08caf2010-09-02 19:23:52 -0700253 return sServiceInstance;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800254 }
Robert Greenwalt86e9e552009-07-16 17:21:39 -0700255
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800256 private ConnectivityService(Context context) {
Wink Savilleed9c02b2010-12-03 12:01:38 -0800257 if (DBG) log("ConnectivityService starting up");
Robert Greenwaltde8383c2010-01-14 17:47:58 -0800258
Wink Savillebb08caf2010-09-02 19:23:52 -0700259 HandlerThread handlerThread = new HandlerThread("ConnectivityServiceThread");
260 handlerThread.start();
261 mHandler = new MyHandler(handlerThread.getLooper());
262
Robert Greenwaltd825ea42010-12-29 16:15:02 -0800263 mBackgroundDataEnabled.set(Settings.Secure.getInt(context.getContentResolver(),
264 Settings.Secure.BACKGROUND_DATA, 1) == 1);
265
Robert Greenwaltde8383c2010-01-14 17:47:58 -0800266 // setup our unique device name
Robert Greenwalt733c6292010-12-06 09:30:17 -0800267 if (TextUtils.isEmpty(SystemProperties.get("net.hostname"))) {
268 String id = Settings.Secure.getString(context.getContentResolver(),
269 Settings.Secure.ANDROID_ID);
270 if (id != null && id.length() > 0) {
271 String name = new String("android_").concat(id);
272 SystemProperties.set("net.hostname", name);
273 }
Robert Greenwaltde8383c2010-01-14 17:47:58 -0800274 }
275
Robert Greenwalte90aa5e2010-09-01 11:34:05 -0700276 // read our default dns server ip
277 String dns = Settings.Secure.getString(context.getContentResolver(),
278 Settings.Secure.DEFAULT_DNS_SERVER);
279 if (dns == null || dns.length() == 0) {
280 dns = context.getResources().getString(
281 com.android.internal.R.string.config_default_dns_server);
282 }
283 try {
Robert Greenwalte5903732011-02-22 16:00:42 -0800284 mDefaultDns = NetworkUtils.numericToInetAddress(dns);
285 } catch (IllegalArgumentException e) {
Wink Savilleed9c02b2010-12-03 12:01:38 -0800286 loge("Error setting defaultDns using " + dns);
Robert Greenwalte90aa5e2010-09-01 11:34:05 -0700287 }
288
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800289 mContext = context;
Robert Greenwalt14f2ef42010-06-15 12:19:37 -0700290
291 PowerManager powerManager = (PowerManager)mContext.getSystemService(Context.POWER_SERVICE);
292 mNetTransitionWakeLock = powerManager.newWakeLock(PowerManager.PARTIAL_WAKE_LOCK, TAG);
293 mNetTransitionWakeLockTimeout = mContext.getResources().getInteger(
294 com.android.internal.R.integer.config_networkTransitionTimeout);
295
Robert Greenwalt42acef32009-08-12 16:08:25 -0700296 mNetTrackers = new NetworkStateTracker[
297 ConnectivityManager.MAX_NETWORK_TYPE+1];
Robert Greenwalt86e9e552009-07-16 17:21:39 -0700298
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800299 mNetworkPreference = getPersistedNetworkPreference();
Robert Greenwalt86e9e552009-07-16 17:21:39 -0700300
Robert Greenwalt5154ae762009-10-30 14:17:42 -0700301 mRadioAttributes = new RadioAttributes[ConnectivityManager.MAX_RADIO_TYPE+1];
Robert Greenwaltd55a6b42011-03-25 13:09:25 -0700302 mNetConfigs = new NetworkConfig[ConnectivityManager.MAX_NETWORK_TYPE+1];
Robert Greenwalt5154ae762009-10-30 14:17:42 -0700303
Robert Greenwalt42acef32009-08-12 16:08:25 -0700304 // Load device network attributes from resources
Robert Greenwalt42acef32009-08-12 16:08:25 -0700305 String[] raStrings = context.getResources().getStringArray(
306 com.android.internal.R.array.radioAttributes);
Robert Greenwalt5154ae762009-10-30 14:17:42 -0700307 for (String raString : raStrings) {
308 RadioAttributes r = new RadioAttributes(raString);
309 if (r.mType > ConnectivityManager.MAX_RADIO_TYPE) {
Wink Savilleed9c02b2010-12-03 12:01:38 -0800310 loge("Error in radioAttributes - ignoring attempt to define type " + r.mType);
Robert Greenwalt5154ae762009-10-30 14:17:42 -0700311 continue;
312 }
313 if (mRadioAttributes[r.mType] != null) {
Wink Savilleed9c02b2010-12-03 12:01:38 -0800314 loge("Error in radioAttributes - ignoring attempt to redefine type " +
Robert Greenwalt5154ae762009-10-30 14:17:42 -0700315 r.mType);
316 continue;
317 }
Robert Greenwalt42acef32009-08-12 16:08:25 -0700318 mRadioAttributes[r.mType] = r;
319 }
320
Robert Greenwalt5154ae762009-10-30 14:17:42 -0700321 String[] naStrings = context.getResources().getStringArray(
322 com.android.internal.R.array.networkAttributes);
323 for (String naString : naStrings) {
324 try {
Robert Greenwaltd55a6b42011-03-25 13:09:25 -0700325 NetworkConfig n = new NetworkConfig(naString);
Wink Saville975c8482011-04-07 14:23:45 -0700326 if (n.type > ConnectivityManager.MAX_NETWORK_TYPE) {
Wink Savilleed9c02b2010-12-03 12:01:38 -0800327 loge("Error in networkAttributes - ignoring attempt to define type " +
Wink Saville975c8482011-04-07 14:23:45 -0700328 n.type);
Robert Greenwalt5154ae762009-10-30 14:17:42 -0700329 continue;
Robert Greenwalt42acef32009-08-12 16:08:25 -0700330 }
Wink Saville975c8482011-04-07 14:23:45 -0700331 if (mNetConfigs[n.type] != null) {
Wink Savilleed9c02b2010-12-03 12:01:38 -0800332 loge("Error in networkAttributes - ignoring attempt to redefine type " +
Wink Saville975c8482011-04-07 14:23:45 -0700333 n.type);
Robert Greenwalt5154ae762009-10-30 14:17:42 -0700334 continue;
335 }
Wink Saville975c8482011-04-07 14:23:45 -0700336 if (mRadioAttributes[n.radio] == null) {
Wink Savilleed9c02b2010-12-03 12:01:38 -0800337 loge("Error in networkAttributes - ignoring attempt to use undefined " +
Wink Saville975c8482011-04-07 14:23:45 -0700338 "radio " + n.radio + " in network type " + n.type);
Robert Greenwalt5154ae762009-10-30 14:17:42 -0700339 continue;
340 }
Wink Saville975c8482011-04-07 14:23:45 -0700341 mNetConfigs[n.type] = n;
Robert Greenwalt5154ae762009-10-30 14:17:42 -0700342 mNetworksDefined++;
343 } catch(Exception e) {
344 // ignore it - leave the entry null
Robert Greenwalt42acef32009-08-12 16:08:25 -0700345 }
346 }
347
Robert Greenwalt5154ae762009-10-30 14:17:42 -0700348 // high priority first
349 mPriorityList = new int[mNetworksDefined];
350 {
351 int insertionPoint = mNetworksDefined-1;
352 int currentLowest = 0;
353 int nextLowest = 0;
354 while (insertionPoint > -1) {
Robert Greenwaltd55a6b42011-03-25 13:09:25 -0700355 for (NetworkConfig na : mNetConfigs) {
Robert Greenwalt5154ae762009-10-30 14:17:42 -0700356 if (na == null) continue;
Wink Saville975c8482011-04-07 14:23:45 -0700357 if (na.priority < currentLowest) continue;
358 if (na.priority > currentLowest) {
359 if (na.priority < nextLowest || nextLowest == 0) {
360 nextLowest = na.priority;
Robert Greenwalt5154ae762009-10-30 14:17:42 -0700361 }
362 continue;
363 }
Wink Saville975c8482011-04-07 14:23:45 -0700364 mPriorityList[insertionPoint--] = na.type;
Robert Greenwalt5154ae762009-10-30 14:17:42 -0700365 }
366 currentLowest = nextLowest;
367 nextLowest = 0;
368 }
369 }
370
371 mNetRequestersPids = new ArrayList[ConnectivityManager.MAX_NETWORK_TYPE+1];
372 for (int i : mPriorityList) {
Robert Greenwalt42acef32009-08-12 16:08:25 -0700373 mNetRequestersPids[i] = new ArrayList();
374 }
375
376 mFeatureUsers = new ArrayList();
377
Robert Greenwalt5154ae762009-10-30 14:17:42 -0700378 mNumDnsEntries = 0;
379
380 mTestMode = SystemProperties.get("cm.test.mode").equals("true")
381 && SystemProperties.get("ro.build.type").equals("eng");
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800382 /*
383 * Create the network state trackers for Wi-Fi and mobile
384 * data. Maybe this could be done with a factory class,
385 * but it's not clear that it's worth it, given that
386 * the number of different network types is not going
387 * to change very often.
388 */
Robert Greenwalt5154ae762009-10-30 14:17:42 -0700389 for (int netType : mPriorityList) {
Wink Saville975c8482011-04-07 14:23:45 -0700390 switch (mNetConfigs[netType].radio) {
Robert Greenwalt5154ae762009-10-30 14:17:42 -0700391 case ConnectivityManager.TYPE_WIFI:
Wink Savilleed9c02b2010-12-03 12:01:38 -0800392 if (DBG) log("Starting Wifi Service.");
Wink Savillec7a98342010-08-13 16:11:42 -0700393 WifiStateTracker wst = new WifiStateTracker();
Irfan Sheriff0d255342010-07-28 09:35:20 -0700394 WifiService wifiService = new WifiService(context);
Robert Greenwalt5154ae762009-10-30 14:17:42 -0700395 ServiceManager.addService(Context.WIFI_SERVICE, wifiService);
Irfan Sheriff0d255342010-07-28 09:35:20 -0700396 wifiService.checkAndStartWifi();
Robert Greenwalt5154ae762009-10-30 14:17:42 -0700397 mNetTrackers[ConnectivityManager.TYPE_WIFI] = wst;
Wink Savillec7a98342010-08-13 16:11:42 -0700398 wst.startMonitoring(context, mHandler);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800399
Irfan Sheriffa2a1b912010-06-07 09:03:04 -0700400 //TODO: as part of WWS refactor, create only when needed
Irfan Sheriff0d255342010-07-28 09:35:20 -0700401 mWifiWatchdogService = new WifiWatchdogService(context);
Irfan Sheriffa2a1b912010-06-07 09:03:04 -0700402
Robert Greenwalt5154ae762009-10-30 14:17:42 -0700403 break;
404 case ConnectivityManager.TYPE_MOBILE:
Wink Savillec7a98342010-08-13 16:11:42 -0700405 mNetTrackers[netType] = new MobileDataStateTracker(netType,
Wink Saville975c8482011-04-07 14:23:45 -0700406 mNetConfigs[netType].name);
Wink Savillec7a98342010-08-13 16:11:42 -0700407 mNetTrackers[netType].startMonitoring(context, mHandler);
Robert Greenwalt5154ae762009-10-30 14:17:42 -0700408 break;
Robert Greenwaltda3d5e62010-12-06 13:56:24 -0800409 case ConnectivityManager.TYPE_DUMMY:
410 mNetTrackers[netType] = new DummyDataStateTracker(netType,
Wink Saville975c8482011-04-07 14:23:45 -0700411 mNetConfigs[netType].name);
Robert Greenwaltda3d5e62010-12-06 13:56:24 -0800412 mNetTrackers[netType].startMonitoring(context, mHandler);
413 break;
Jaikumar Ganesh15c74392010-12-21 22:31:44 -0800414 case ConnectivityManager.TYPE_BLUETOOTH:
415 mNetTrackers[netType] = BluetoothTetheringDataTracker.getInstance();
416 mNetTrackers[netType].startMonitoring(context, mHandler);
417 break;
Benoit Goby19970692010-12-22 14:29:40 -0800418 case ConnectivityManager.TYPE_ETHERNET:
419 mNetTrackers[netType] = EthernetDataTracker.getInstance();
420 mNetTrackers[netType].startMonitoring(context, mHandler);
421 break;
Robert Greenwalt5154ae762009-10-30 14:17:42 -0700422 default:
Wink Savilleed9c02b2010-12-03 12:01:38 -0800423 loge("Trying to create a DataStateTracker for an unknown radio type " +
Wink Saville975c8482011-04-07 14:23:45 -0700424 mNetConfigs[netType].radio);
Robert Greenwalt5154ae762009-10-30 14:17:42 -0700425 continue;
426 }
427 }
Robert Greenwaltd0e18ff2010-01-26 11:40:34 -0800428
Chia-chi Yehc9338302011-05-11 16:35:13 -0700429 IBinder b = ServiceManager.getService(Context.NETWORKMANAGEMENT_SERVICE);
430 INetworkManagementService nmService = INetworkManagementService.Stub.asInterface(b);
431
432 mTethering = new Tethering(mContext, nmService, mHandler.getLooper());
Robert Greenwaltc9d5fb72010-02-25 12:29:30 -0800433 mTetheringConfigValid = (((mNetTrackers[ConnectivityManager.TYPE_MOBILE_DUN] != null) ||
434 !mTethering.isDunRequired()) &&
435 (mTethering.getTetherableUsbRegexs().length != 0 ||
Danica Chang6fdd0c62010-08-11 14:54:43 -0700436 mTethering.getTetherableWifiRegexs().length != 0 ||
437 mTethering.getTetherableBluetoothRegexs().length != 0) &&
Robert Greenwaltc9d5fb72010-02-25 12:29:30 -0800438 mTethering.getUpstreamIfaceRegexs().length != 0);
439
Robert Greenwalt4e8dfef2010-09-20 14:35:25 -0700440 if (DBG) {
441 mInetLog = new ArrayList();
442 }
Robert Greenwalt434203a2010-10-11 16:00:27 -0700443
444 mSettingsObserver = new SettingsObserver(mHandler, EVENT_APPLY_GLOBAL_HTTP_PROXY);
445 mSettingsObserver.observe(mContext);
Robert Greenwaltb7090d62010-12-02 11:31:00 -0800446
447 loadGlobalProxy();
Hung-ying Tyan6b818de2011-01-19 16:48:38 +0800448
449 VpnManager.startVpnService(context);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800450 }
451
Robert Greenwalt5154ae762009-10-30 14:17:42 -0700452
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800453 /**
Robert Greenwalt86e9e552009-07-16 17:21:39 -0700454 * Sets the preferred network.
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800455 * @param preference the new preference
456 */
Robert Greenwalt8dcc28b2010-09-23 10:05:56 -0700457 public void setNetworkPreference(int preference) {
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800458 enforceChangePermission();
Robert Greenwalt8dcc28b2010-09-23 10:05:56 -0700459
460 mHandler.sendMessage(mHandler.obtainMessage(EVENT_SET_NETWORK_PREFERENCE, preference, 0));
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800461 }
462
463 public int getNetworkPreference() {
464 enforceAccessPermission();
Robert Greenwalt8dcc28b2010-09-23 10:05:56 -0700465 int preference;
466 synchronized(this) {
467 preference = mNetworkPreference;
468 }
469 return preference;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800470 }
471
Robert Greenwalt8dcc28b2010-09-23 10:05:56 -0700472 private void handleSetNetworkPreference(int preference) {
473 if (ConnectivityManager.isNetworkTypeValid(preference) &&
Robert Greenwaltd55a6b42011-03-25 13:09:25 -0700474 mNetConfigs[preference] != null &&
475 mNetConfigs[preference].isDefault()) {
Robert Greenwalt8dcc28b2010-09-23 10:05:56 -0700476 if (mNetworkPreference != preference) {
477 final ContentResolver cr = mContext.getContentResolver();
478 Settings.Secure.putInt(cr, Settings.Secure.NETWORK_PREFERENCE, preference);
479 synchronized(this) {
480 mNetworkPreference = preference;
481 }
482 enforcePreference();
483 }
484 }
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800485 }
Robert Greenwalt86e9e552009-07-16 17:21:39 -0700486
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800487 private int getPersistedNetworkPreference() {
488 final ContentResolver cr = mContext.getContentResolver();
489
490 final int networkPrefSetting = Settings.Secure
491 .getInt(cr, Settings.Secure.NETWORK_PREFERENCE, -1);
492 if (networkPrefSetting != -1) {
493 return networkPrefSetting;
494 }
495
496 return ConnectivityManager.DEFAULT_NETWORK_PREFERENCE;
497 }
Robert Greenwalt86e9e552009-07-16 17:21:39 -0700498
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800499 /**
Robert Greenwalt86e9e552009-07-16 17:21:39 -0700500 * Make the state of network connectivity conform to the preference settings
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800501 * In this method, we only tear down a non-preferred network. Establishing
502 * a connection to the preferred network is taken care of when we handle
503 * the disconnect event from the non-preferred network
504 * (see {@link #handleDisconnect(NetworkInfo)}).
505 */
506 private void enforcePreference() {
Robert Greenwalt42acef32009-08-12 16:08:25 -0700507 if (mNetTrackers[mNetworkPreference].getNetworkInfo().isConnected())
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800508 return;
509
Robert Greenwalt42acef32009-08-12 16:08:25 -0700510 if (!mNetTrackers[mNetworkPreference].isAvailable())
511 return;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800512
Robert Greenwalt42acef32009-08-12 16:08:25 -0700513 for (int t=0; t <= ConnectivityManager.MAX_RADIO_TYPE; t++) {
Robert Greenwalt5154ae762009-10-30 14:17:42 -0700514 if (t != mNetworkPreference && mNetTrackers[t] != null &&
Robert Greenwalt42acef32009-08-12 16:08:25 -0700515 mNetTrackers[t].getNetworkInfo().isConnected()) {
Robert Greenwaltec9fe462009-08-20 15:25:14 -0700516 if (DBG) {
Wink Savilleed9c02b2010-12-03 12:01:38 -0800517 log("tearing down " + mNetTrackers[t].getNetworkInfo() +
Robert Greenwaltec9fe462009-08-20 15:25:14 -0700518 " in enforcePreference");
519 }
Robert Greenwalt42acef32009-08-12 16:08:25 -0700520 teardown(mNetTrackers[t]);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800521 }
522 }
523 }
524
525 private boolean teardown(NetworkStateTracker netTracker) {
526 if (netTracker.teardown()) {
527 netTracker.setTeardownRequested(true);
528 return true;
529 } else {
530 return false;
531 }
532 }
533
534 /**
535 * Return NetworkInfo for the active (i.e., connected) network interface.
536 * It is assumed that at most one network is active at a time. If more
537 * than one is active, it is indeterminate which will be returned.
Robert Greenwalt86e9e552009-07-16 17:21:39 -0700538 * @return the info for the active network, or {@code null} if none is
539 * active
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800540 */
541 public NetworkInfo getActiveNetworkInfo() {
542 enforceAccessPermission();
Robert Greenwalt13ec4062011-04-01 10:51:22 -0700543 if (mActiveDefaultNetwork != -1) {
544 return mNetTrackers[mActiveDefaultNetwork].getNetworkInfo();
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800545 }
546 return null;
547 }
548
549 public NetworkInfo getNetworkInfo(int networkType) {
550 enforceAccessPermission();
551 if (ConnectivityManager.isNetworkTypeValid(networkType)) {
552 NetworkStateTracker t = mNetTrackers[networkType];
553 if (t != null)
554 return t.getNetworkInfo();
555 }
556 return null;
557 }
558
559 public NetworkInfo[] getAllNetworkInfo() {
560 enforceAccessPermission();
Robert Greenwalt5154ae762009-10-30 14:17:42 -0700561 NetworkInfo[] result = new NetworkInfo[mNetworksDefined];
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800562 int i = 0;
563 for (NetworkStateTracker t : mNetTrackers) {
Robert Greenwalt5154ae762009-10-30 14:17:42 -0700564 if(t != null) result[i++] = t.getNetworkInfo();
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800565 }
566 return result;
567 }
568
Robert Greenwaltd192dad2010-09-14 09:18:02 -0700569 /**
570 * Return LinkProperties for the active (i.e., connected) default
571 * network interface. It is assumed that at most one default network
572 * is active at a time. If more than one is active, it is indeterminate
573 * which will be returned.
574 * @return the ip properties for the active network, or {@code null} if
575 * none is active
576 */
577 public LinkProperties getActiveLinkProperties() {
578 enforceAccessPermission();
579 for (int type=0; type <= ConnectivityManager.MAX_NETWORK_TYPE; type++) {
Robert Greenwaltd55a6b42011-03-25 13:09:25 -0700580 if (mNetConfigs[type] == null || !mNetConfigs[type].isDefault()) {
Robert Greenwaltd192dad2010-09-14 09:18:02 -0700581 continue;
582 }
583 NetworkStateTracker t = mNetTrackers[type];
584 NetworkInfo info = t.getNetworkInfo();
585 if (info.isConnected()) {
586 return t.getLinkProperties();
587 }
588 }
589 return null;
590 }
591
592 public LinkProperties getLinkProperties(int networkType) {
593 enforceAccessPermission();
594 if (ConnectivityManager.isNetworkTypeValid(networkType)) {
595 NetworkStateTracker t = mNetTrackers[networkType];
596 if (t != null) return t.getLinkProperties();
597 }
598 return null;
599 }
600
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800601 public boolean setRadios(boolean turnOn) {
602 boolean result = true;
603 enforceChangePermission();
604 for (NetworkStateTracker t : mNetTrackers) {
Robert Greenwalt5154ae762009-10-30 14:17:42 -0700605 if (t != null) result = t.setRadio(turnOn) && result;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800606 }
607 return result;
608 }
609
610 public boolean setRadio(int netType, boolean turnOn) {
611 enforceChangePermission();
612 if (!ConnectivityManager.isNetworkTypeValid(netType)) {
613 return false;
614 }
615 NetworkStateTracker tracker = mNetTrackers[netType];
616 return tracker != null && tracker.setRadio(turnOn);
617 }
618
Robert Greenwalt9c75d4a2009-09-27 17:27:04 -0700619 /**
620 * Used to notice when the calling process dies so we can self-expire
621 *
622 * Also used to know if the process has cleaned up after itself when
623 * our auto-expire timer goes off. The timer has a link to an object.
624 *
625 */
Robert Greenwalt42acef32009-08-12 16:08:25 -0700626 private class FeatureUser implements IBinder.DeathRecipient {
627 int mNetworkType;
628 String mFeature;
629 IBinder mBinder;
630 int mPid;
631 int mUid;
Robert Greenwaltb9285352009-12-21 18:24:07 -0800632 long mCreateTime;
Robert Greenwalt42acef32009-08-12 16:08:25 -0700633
634 FeatureUser(int type, String feature, IBinder binder) {
635 super();
636 mNetworkType = type;
637 mFeature = feature;
638 mBinder = binder;
639 mPid = getCallingPid();
640 mUid = getCallingUid();
Robert Greenwaltb9285352009-12-21 18:24:07 -0800641 mCreateTime = System.currentTimeMillis();
Robert Greenwalt9c75d4a2009-09-27 17:27:04 -0700642
Robert Greenwalt42acef32009-08-12 16:08:25 -0700643 try {
644 mBinder.linkToDeath(this, 0);
645 } catch (RemoteException e) {
646 binderDied();
647 }
648 }
649
650 void unlinkDeathRecipient() {
651 mBinder.unlinkToDeath(this, 0);
652 }
653
654 public void binderDied() {
Wink Savilleed9c02b2010-12-03 12:01:38 -0800655 log("ConnectivityService FeatureUser binderDied(" +
Robert Greenwaltb9285352009-12-21 18:24:07 -0800656 mNetworkType + ", " + mFeature + ", " + mBinder + "), created " +
657 (System.currentTimeMillis() - mCreateTime) + " mSec ago");
Robert Greenwalt9c75d4a2009-09-27 17:27:04 -0700658 stopUsingNetworkFeature(this, false);
Robert Greenwalt42acef32009-08-12 16:08:25 -0700659 }
660
Robert Greenwalt9c75d4a2009-09-27 17:27:04 -0700661 public void expire() {
Wink Savilleed9c02b2010-12-03 12:01:38 -0800662 log("ConnectivityService FeatureUser expire(" +
Robert Greenwaltb9285352009-12-21 18:24:07 -0800663 mNetworkType + ", " + mFeature + ", " + mBinder +"), created " +
664 (System.currentTimeMillis() - mCreateTime) + " mSec ago");
Robert Greenwalt9c75d4a2009-09-27 17:27:04 -0700665 stopUsingNetworkFeature(this, false);
666 }
Robert Greenwaltb9285352009-12-21 18:24:07 -0800667
668 public String toString() {
669 return "FeatureUser("+mNetworkType+","+mFeature+","+mPid+","+mUid+"), created " +
670 (System.currentTimeMillis() - mCreateTime) + " mSec ago";
671 }
Robert Greenwalt42acef32009-08-12 16:08:25 -0700672 }
673
Robert Greenwalt9c75d4a2009-09-27 17:27:04 -0700674 // javadoc from interface
Robert Greenwalt42acef32009-08-12 16:08:25 -0700675 public int startUsingNetworkFeature(int networkType, String feature,
676 IBinder binder) {
677 if (DBG) {
Wink Savilleed9c02b2010-12-03 12:01:38 -0800678 log("startUsingNetworkFeature for net " + networkType + ": " + feature);
Robert Greenwalt42acef32009-08-12 16:08:25 -0700679 }
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800680 enforceChangePermission();
Robert Greenwalt5154ae762009-10-30 14:17:42 -0700681 if (!ConnectivityManager.isNetworkTypeValid(networkType) ||
Robert Greenwaltd55a6b42011-03-25 13:09:25 -0700682 mNetConfigs[networkType] == null) {
Robert Greenwalt42acef32009-08-12 16:08:25 -0700683 return Phone.APN_REQUEST_FAILED;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800684 }
Robert Greenwalt42acef32009-08-12 16:08:25 -0700685
Robert Greenwalt9c75d4a2009-09-27 17:27:04 -0700686 FeatureUser f = new FeatureUser(networkType, feature, binder);
Robert Greenwalt42acef32009-08-12 16:08:25 -0700687
688 // TODO - move this into the MobileDataStateTracker
689 int usedNetworkType = networkType;
690 if(networkType == ConnectivityManager.TYPE_MOBILE) {
Wink Saville2b8bcfe2011-02-24 17:58:51 -0800691 usedNetworkType = convertFeatureToNetworkType(feature);
692 if (usedNetworkType < 0) {
693 Slog.e(TAG, "Can't match any netTracker!");
694 usedNetworkType = networkType;
Robert Greenwalt42acef32009-08-12 16:08:25 -0700695 }
696 }
697 NetworkStateTracker network = mNetTrackers[usedNetworkType];
698 if (network != null) {
Robert Greenwalt0be1e982010-12-15 13:26:33 -0800699 Integer currentPid = new Integer(getCallingPid());
Robert Greenwalt42acef32009-08-12 16:08:25 -0700700 if (usedNetworkType != networkType) {
Robert Greenwalt42acef32009-08-12 16:08:25 -0700701 NetworkStateTracker radio = mNetTrackers[networkType];
702 NetworkInfo ni = network.getNetworkInfo();
703
704 if (ni.isAvailable() == false) {
Wink Savilleed9c02b2010-12-03 12:01:38 -0800705 if (DBG) log("special network not available");
Robert Greenwalte32e8122010-12-29 14:35:21 -0800706 if (!TextUtils.equals(feature,Phone.FEATURE_ENABLE_DUN_ALWAYS)) {
707 return Phone.APN_TYPE_NOT_AVAILABLE;
708 } else {
709 // else make the attempt anyway - probably giving REQUEST_STARTED below
710 }
Robert Greenwalt42acef32009-08-12 16:08:25 -0700711 }
712
Robert Greenwalt9c75d4a2009-09-27 17:27:04 -0700713 synchronized(this) {
714 mFeatureUsers.add(f);
715 if (!mNetRequestersPids[usedNetworkType].contains(currentPid)) {
716 // this gets used for per-pid dns when connected
717 mNetRequestersPids[usedNetworkType].add(currentPid);
718 }
Robert Greenwalt42acef32009-08-12 16:08:25 -0700719 }
Robert Greenwalt9c75d4a2009-09-27 17:27:04 -0700720
Robert Greenwaltf2102f72011-05-03 19:02:44 -0700721 int restoreTimer = getRestoreDefaultNetworkDelay(usedNetworkType);
722
723 if (restoreTimer >= 0) {
724 mHandler.sendMessageDelayed(
725 mHandler.obtainMessage(EVENT_RESTORE_DEFAULT_NETWORK, f), restoreTimer);
726 }
Robert Greenwalt42acef32009-08-12 16:08:25 -0700727
Robert Greenwalta64bf832009-08-19 20:19:33 -0700728 if ((ni.isConnectedOrConnecting() == true) &&
729 !network.isTeardownRequested()) {
Robert Greenwalt42acef32009-08-12 16:08:25 -0700730 if (ni.isConnected() == true) {
731 // add the pid-specific dns
Robert Greenwaltc76b8fa2010-07-23 15:46:26 -0700732 handleDnsConfigurationChange(networkType);
Wink Savilleed9c02b2010-12-03 12:01:38 -0800733 if (DBG) log("special network already active");
Robert Greenwalt42acef32009-08-12 16:08:25 -0700734 return Phone.APN_ALREADY_ACTIVE;
735 }
Wink Savilleed9c02b2010-12-03 12:01:38 -0800736 if (DBG) log("special network already connecting");
Robert Greenwalt42acef32009-08-12 16:08:25 -0700737 return Phone.APN_REQUEST_STARTED;
738 }
739
740 // check if the radio in play can make another contact
741 // assume if cannot for now
742
Wink Savilleed9c02b2010-12-03 12:01:38 -0800743 if (DBG) log("reconnecting to special network");
Robert Greenwalt42acef32009-08-12 16:08:25 -0700744 network.reconnect();
745 return Phone.APN_REQUEST_STARTED;
746 } else {
Robert Greenwalt0be1e982010-12-15 13:26:33 -0800747 // need to remember this unsupported request so we respond appropriately on stop
748 synchronized(this) {
749 mFeatureUsers.add(f);
750 if (!mNetRequestersPids[usedNetworkType].contains(currentPid)) {
751 // this gets used for per-pid dns when connected
752 mNetRequestersPids[usedNetworkType].add(currentPid);
753 }
754 }
Robert Greenwalt02648a42010-05-18 10:52:51 -0700755 return -1;
Robert Greenwalt42acef32009-08-12 16:08:25 -0700756 }
757 }
758 return Phone.APN_TYPE_NOT_AVAILABLE;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800759 }
760
Robert Greenwalt9c75d4a2009-09-27 17:27:04 -0700761 // javadoc from interface
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800762 public int stopUsingNetworkFeature(int networkType, String feature) {
Robert Greenwaltb8f16342009-10-06 17:52:40 -0700763 enforceChangePermission();
764
Robert Greenwalt9c75d4a2009-09-27 17:27:04 -0700765 int pid = getCallingPid();
766 int uid = getCallingUid();
767
768 FeatureUser u = null;
769 boolean found = false;
770
771 synchronized(this) {
772 for (int i = 0; i < mFeatureUsers.size() ; i++) {
773 u = (FeatureUser)mFeatureUsers.get(i);
774 if (uid == u.mUid && pid == u.mPid &&
775 networkType == u.mNetworkType &&
776 TextUtils.equals(feature, u.mFeature)) {
777 found = true;
778 break;
779 }
780 }
781 }
782 if (found && u != null) {
783 // stop regardless of how many other time this proc had called start
784 return stopUsingNetworkFeature(u, true);
785 } else {
786 // none found!
Wink Savilleed9c02b2010-12-03 12:01:38 -0800787 if (DBG) log("ignoring stopUsingNetworkFeature - not a live request");
Robert Greenwalt9c75d4a2009-09-27 17:27:04 -0700788 return 1;
789 }
Robert Greenwalt42acef32009-08-12 16:08:25 -0700790 }
791
Robert Greenwalt9c75d4a2009-09-27 17:27:04 -0700792 private int stopUsingNetworkFeature(FeatureUser u, boolean ignoreDups) {
793 int networkType = u.mNetworkType;
794 String feature = u.mFeature;
795 int pid = u.mPid;
796 int uid = u.mUid;
797
798 NetworkStateTracker tracker = null;
799 boolean callTeardown = false; // used to carry our decision outside of sync block
800
Robert Greenwalt42acef32009-08-12 16:08:25 -0700801 if (DBG) {
Wink Savilleed9c02b2010-12-03 12:01:38 -0800802 log("stopUsingNetworkFeature for net " + networkType +
Robert Greenwalt42acef32009-08-12 16:08:25 -0700803 ": " + feature);
804 }
Robert Greenwaltb8f16342009-10-06 17:52:40 -0700805
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800806 if (!ConnectivityManager.isNetworkTypeValid(networkType)) {
807 return -1;
808 }
Robert Greenwalt42acef32009-08-12 16:08:25 -0700809
Robert Greenwalt9c75d4a2009-09-27 17:27:04 -0700810 // need to link the mFeatureUsers list with the mNetRequestersPids state in this
811 // sync block
812 synchronized(this) {
813 // check if this process still has an outstanding start request
814 if (!mFeatureUsers.contains(u)) {
Wink Savilleed9c02b2010-12-03 12:01:38 -0800815 if (DBG) log("ignoring - this process has no outstanding requests");
Robert Greenwalt42acef32009-08-12 16:08:25 -0700816 return 1;
817 }
Robert Greenwalt9c75d4a2009-09-27 17:27:04 -0700818 u.unlinkDeathRecipient();
819 mFeatureUsers.remove(mFeatureUsers.indexOf(u));
820 // If we care about duplicate requests, check for that here.
821 //
822 // This is done to support the extension of a request - the app
823 // can request we start the network feature again and renew the
824 // auto-shutoff delay. Normal "stop" calls from the app though
825 // do not pay attention to duplicate requests - in effect the
826 // API does not refcount and a single stop will counter multiple starts.
827 if (ignoreDups == false) {
828 for (int i = 0; i < mFeatureUsers.size() ; i++) {
829 FeatureUser x = (FeatureUser)mFeatureUsers.get(i);
830 if (x.mUid == u.mUid && x.mPid == u.mPid &&
831 x.mNetworkType == u.mNetworkType &&
832 TextUtils.equals(x.mFeature, u.mFeature)) {
Wink Savilleed9c02b2010-12-03 12:01:38 -0800833 if (DBG) log("ignoring stopUsingNetworkFeature as dup is found");
Robert Greenwalt9c75d4a2009-09-27 17:27:04 -0700834 return 1;
835 }
836 }
Robert Greenwalt42acef32009-08-12 16:08:25 -0700837 }
Robert Greenwalt9c75d4a2009-09-27 17:27:04 -0700838
839 // TODO - move to MobileDataStateTracker
840 int usedNetworkType = networkType;
841 if (networkType == ConnectivityManager.TYPE_MOBILE) {
Wink Saville2b8bcfe2011-02-24 17:58:51 -0800842 usedNetworkType = convertFeatureToNetworkType(feature);
843 if (usedNetworkType < 0) {
844 usedNetworkType = networkType;
Robert Greenwalt9c75d4a2009-09-27 17:27:04 -0700845 }
846 }
847 tracker = mNetTrackers[usedNetworkType];
Robert Greenwalt5154ae762009-10-30 14:17:42 -0700848 if (tracker == null) {
Wink Savilleed9c02b2010-12-03 12:01:38 -0800849 if (DBG) log("ignoring - no known tracker for net type " + usedNetworkType);
Robert Greenwalt5154ae762009-10-30 14:17:42 -0700850 return -1;
851 }
852 if (usedNetworkType != networkType) {
Robert Greenwalt9c75d4a2009-09-27 17:27:04 -0700853 Integer currentPid = new Integer(pid);
Robert Greenwalt9c75d4a2009-09-27 17:27:04 -0700854 mNetRequestersPids[usedNetworkType].remove(currentPid);
Robert Greenwalt421c72b2009-12-17 14:54:59 -0800855 reassessPidDns(pid, true);
Robert Greenwalt9c75d4a2009-09-27 17:27:04 -0700856 if (mNetRequestersPids[usedNetworkType].size() != 0) {
Wink Savilleed9c02b2010-12-03 12:01:38 -0800857 if (DBG) log("not tearing down special network - " +
Robert Greenwalt9c75d4a2009-09-27 17:27:04 -0700858 "others still using it");
859 return 1;
860 }
861 callTeardown = true;
Robert Greenwalt19b9ab42011-01-10 11:58:31 -0800862 } else {
863 if (DBG) log("not a known feature - dropping");
Robert Greenwalt9c75d4a2009-09-27 17:27:04 -0700864 }
865 }
Wink Savilleed9c02b2010-12-03 12:01:38 -0800866 if (DBG) log("Doing network teardown");
Robert Greenwalt9c75d4a2009-09-27 17:27:04 -0700867 if (callTeardown) {
868 tracker.teardown();
Robert Greenwalt42acef32009-08-12 16:08:25 -0700869 return 1;
870 } else {
Robert Greenwalt02648a42010-05-18 10:52:51 -0700871 return -1;
Robert Greenwalt42acef32009-08-12 16:08:25 -0700872 }
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800873 }
874
875 /**
Robert Greenwalt585ac0f2010-08-27 09:24:29 -0700876 * @deprecated use requestRouteToHostAddress instead
877 *
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800878 * Ensure that a network route exists to deliver traffic to the specified
879 * host via the specified network interface.
Robert Greenwalt86e9e552009-07-16 17:21:39 -0700880 * @param networkType the type of the network over which traffic to the
881 * specified host is to be routed
882 * @param hostAddress the IP address of the host to which the route is
883 * desired
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800884 * @return {@code true} on success, {@code false} on failure
885 */
886 public boolean requestRouteToHost(int networkType, int hostAddress) {
Robert Greenwalt585ac0f2010-08-27 09:24:29 -0700887 InetAddress inetAddress = NetworkUtils.intToInetAddress(hostAddress);
888
889 if (inetAddress == null) {
890 return false;
891 }
892
893 return requestRouteToHostAddress(networkType, inetAddress.getAddress());
894 }
895
896 /**
897 * Ensure that a network route exists to deliver traffic to the specified
898 * host via the specified network interface.
899 * @param networkType the type of the network over which traffic to the
900 * specified host is to be routed
901 * @param hostAddress the IP address of the host to which the route is
902 * desired
903 * @return {@code true} on success, {@code false} on failure
904 */
905 public boolean requestRouteToHostAddress(int networkType, byte[] hostAddress) {
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800906 enforceChangePermission();
907 if (!ConnectivityManager.isNetworkTypeValid(networkType)) {
908 return false;
909 }
910 NetworkStateTracker tracker = mNetTrackers[networkType];
Robert Greenwalt8206ff32009-09-10 15:06:20 -0700911
Robert Greenwalt5154ae762009-10-30 14:17:42 -0700912 if (tracker == null || !tracker.getNetworkInfo().isConnected() ||
913 tracker.isTeardownRequested()) {
Robert Greenwalt8206ff32009-09-10 15:06:20 -0700914 if (DBG) {
Wink Savilleed9c02b2010-12-03 12:01:38 -0800915 log("requestRouteToHostAddress on down network " +
Robert Greenwalt585ac0f2010-08-27 09:24:29 -0700916 "(" + networkType + ") - dropped");
Robert Greenwalt8206ff32009-09-10 15:06:20 -0700917 }
918 return false;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800919 }
Robert Greenwalt47f69fe2010-06-15 15:43:39 -0700920 try {
Robert Greenwalt585ac0f2010-08-27 09:24:29 -0700921 InetAddress addr = InetAddress.getByAddress(hostAddress);
Robert Greenwaltf43396c2011-05-06 17:10:53 -0700922 return addHostRoute(tracker, addr, 0);
Robert Greenwalt47f69fe2010-06-15 15:43:39 -0700923 } catch (UnknownHostException e) {}
924 return false;
Irfan Sheriffd649c122010-06-09 15:39:36 -0700925 }
926
927 /**
928 * Ensure that a network route exists to deliver traffic to the specified
929 * host via the mobile data network.
930 * @param hostAddress the IP address of the host to which the route is desired,
931 * in network byte order.
Robert Greenwalt47f69fe2010-06-15 15:43:39 -0700932 * TODO - deprecate
Irfan Sheriffd649c122010-06-09 15:39:36 -0700933 * @return {@code true} on success, {@code false} on failure
934 */
Robert Greenwaltf43396c2011-05-06 17:10:53 -0700935 private boolean addHostRoute(NetworkStateTracker nt, InetAddress hostAddress, int cycleCount) {
Irfan Sheriffd649c122010-06-09 15:39:36 -0700936 if (nt.getNetworkInfo().getType() == ConnectivityManager.TYPE_WIFI) {
937 return false;
938 }
939
Robert Greenwaltf43396c2011-05-06 17:10:53 -0700940 LinkProperties lp = nt.getLinkProperties();
941 if ((lp == null) || (hostAddress == null)) return false;
Irfan Sheriffd649c122010-06-09 15:39:36 -0700942
Robert Greenwaltf43396c2011-05-06 17:10:53 -0700943 String interfaceName = lp.getInterfaceName();
Irfan Sheriffd649c122010-06-09 15:39:36 -0700944 if (DBG) {
Robert Greenwaltf43396c2011-05-06 17:10:53 -0700945 log("Requested host route to " + hostAddress + "(" + interfaceName + "), cycleCount=" +
946 cycleCount);
Irfan Sheriffd649c122010-06-09 15:39:36 -0700947 }
Robert Greenwaltf43396c2011-05-06 17:10:53 -0700948 if (interfaceName == null) {
Wink Savilleed9c02b2010-12-03 12:01:38 -0800949 if (DBG) loge("addHostRoute failed due to null interface name");
Irfan Sheriffd649c122010-06-09 15:39:36 -0700950 return false;
951 }
Robert Greenwaltf43396c2011-05-06 17:10:53 -0700952
953 RouteInfo bestRoute = RouteInfo.selectBestRoute(lp.getRoutes(), hostAddress);
954 InetAddress gateway = null;
955 if (bestRoute != null) {
956 gateway = bestRoute.getGateway();
957 // if the best route is ourself, don't relf-reference, just add the host route
958 if (hostAddress.equals(gateway)) gateway = null;
959 }
960 if (gateway != null) {
961 if (cycleCount > MAX_HOSTROUTE_CYCLE_COUNT) {
962 loge("Error adding hostroute - too much recursion");
963 return false;
964 }
965 if (!addHostRoute(nt, gateway, cycleCount+1)) return false;
966 }
967 return NetworkUtils.addHostRoute(interfaceName, hostAddress, gateway);
968 }
969
970 // TODO support the removal of single host routes. Keep a ref count of them so we
971 // aren't over-zealous
972 private boolean removeHostRoute(NetworkStateTracker nt, InetAddress hostAddress) {
973 return false;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800974 }
975
976 /**
977 * @see ConnectivityManager#getBackgroundDataSetting()
978 */
979 public boolean getBackgroundDataSetting() {
Robert Greenwaltd825ea42010-12-29 16:15:02 -0800980 return mBackgroundDataEnabled.get();
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800981 }
Robert Greenwalt86e9e552009-07-16 17:21:39 -0700982
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800983 /**
984 * @see ConnectivityManager#setBackgroundDataSetting(boolean)
985 */
986 public void setBackgroundDataSetting(boolean allowBackgroundDataUsage) {
987 mContext.enforceCallingOrSelfPermission(
988 android.Manifest.permission.CHANGE_BACKGROUND_DATA_SETTING,
989 "ConnectivityService");
Robert Greenwalt86e9e552009-07-16 17:21:39 -0700990
Robert Greenwaltd825ea42010-12-29 16:15:02 -0800991 mBackgroundDataEnabled.set(allowBackgroundDataUsage);
992
Robert Greenwalt8dcc28b2010-09-23 10:05:56 -0700993 mHandler.sendMessage(mHandler.obtainMessage(EVENT_SET_BACKGROUND_DATA,
994 (allowBackgroundDataUsage ? ENABLED : DISABLED), 0));
995 }
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800996
Robert Greenwalt8dcc28b2010-09-23 10:05:56 -0700997 private void handleSetBackgroundData(boolean enabled) {
Robert Greenwaltdb4afae2011-02-25 13:44:09 -0800998 Settings.Secure.putInt(mContext.getContentResolver(),
999 Settings.Secure.BACKGROUND_DATA, enabled ? 1 : 0);
1000 Intent broadcast = new Intent(
1001 ConnectivityManager.ACTION_BACKGROUND_DATA_SETTING_CHANGED);
1002 mContext.sendBroadcast(broadcast);
Robert Greenwalt86e9e552009-07-16 17:21:39 -07001003 }
1004
Robert Greenwaltc03fa502010-02-23 18:58:05 -08001005 /**
1006 * @see ConnectivityManager#getMobileDataEnabled()
1007 */
1008 public boolean getMobileDataEnabled() {
Wink Savillee7982682010-12-07 10:31:02 -08001009 // TODO: This detail should probably be in DataConnectionTracker's
1010 // which is where we store the value and maybe make this
1011 // asynchronous.
Robert Greenwaltc03fa502010-02-23 18:58:05 -08001012 enforceAccessPermission();
1013 boolean retVal = Settings.Secure.getInt(mContext.getContentResolver(),
1014 Settings.Secure.MOBILE_DATA, 1) == 1;
Wink Savilleed9c02b2010-12-03 12:01:38 -08001015 if (DBG) log("getMobileDataEnabled returning " + retVal);
Robert Greenwaltc03fa502010-02-23 18:58:05 -08001016 return retVal;
1017 }
1018
Robert Greenwaltd55a6b42011-03-25 13:09:25 -07001019 public void setDataDependency(int networkType, boolean met) {
1020 enforceChangePermission();
1021 if (DBG) {
1022 log("setDataDependency(" + networkType + ", " + met + ")");
1023 }
1024 mHandler.sendMessage(mHandler.obtainMessage(EVENT_SET_DEPENDENCY_MET,
1025 (met ? ENABLED : DISABLED), networkType));
1026 }
1027
1028 private void handleSetDependencyMet(int networkType, boolean met) {
1029 if (mNetTrackers[networkType] != null) {
1030 if (DBG) {
1031 log("handleSetDependencyMet(" + networkType + ", " + met + ")");
1032 }
1033 mNetTrackers[networkType].setDependencyMet(met);
1034 }
1035 }
1036
Robert Greenwaltc03fa502010-02-23 18:58:05 -08001037 /**
1038 * @see ConnectivityManager#setMobileDataEnabled(boolean)
1039 */
Robert Greenwalt8dcc28b2010-09-23 10:05:56 -07001040 public void setMobileDataEnabled(boolean enabled) {
Robert Greenwaltc03fa502010-02-23 18:58:05 -08001041 enforceChangePermission();
Wink Savilleed9c02b2010-12-03 12:01:38 -08001042 if (DBG) log("setMobileDataEnabled(" + enabled + ")");
Robert Greenwaltc03fa502010-02-23 18:58:05 -08001043
Robert Greenwalt8dcc28b2010-09-23 10:05:56 -07001044 mHandler.sendMessage(mHandler.obtainMessage(EVENT_SET_MOBILE_DATA,
Robert Greenwaltd55a6b42011-03-25 13:09:25 -07001045 (enabled ? ENABLED : DISABLED), 0));
Robert Greenwalt8dcc28b2010-09-23 10:05:56 -07001046 }
1047
1048 private void handleSetMobileData(boolean enabled) {
Wink Savillee7982682010-12-07 10:31:02 -08001049 if (mNetTrackers[ConnectivityManager.TYPE_MOBILE] != null) {
1050 if (DBG) {
1051 Slog.d(TAG, mNetTrackers[ConnectivityManager.TYPE_MOBILE].toString() + enabled);
Robert Greenwaltc03fa502010-02-23 18:58:05 -08001052 }
Wink Savillee7982682010-12-07 10:31:02 -08001053 mNetTrackers[ConnectivityManager.TYPE_MOBILE].setDataEnable(enabled);
Robert Greenwaltc03fa502010-02-23 18:58:05 -08001054 }
1055 }
1056
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001057 private void enforceAccessPermission() {
Robert Greenwalt86e9e552009-07-16 17:21:39 -07001058 mContext.enforceCallingOrSelfPermission(
1059 android.Manifest.permission.ACCESS_NETWORK_STATE,
1060 "ConnectivityService");
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001061 }
1062
1063 private void enforceChangePermission() {
Robert Greenwalt86e9e552009-07-16 17:21:39 -07001064 mContext.enforceCallingOrSelfPermission(
1065 android.Manifest.permission.CHANGE_NETWORK_STATE,
1066 "ConnectivityService");
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001067 }
1068
Robert Greenwaltd0e18ff2010-01-26 11:40:34 -08001069 // TODO Make this a special check when it goes public
1070 private void enforceTetherChangePermission() {
1071 mContext.enforceCallingOrSelfPermission(
1072 android.Manifest.permission.CHANGE_NETWORK_STATE,
1073 "ConnectivityService");
1074 }
1075
Robert Greenwalt2a091d72010-02-11 18:18:40 -08001076 private void enforceTetherAccessPermission() {
1077 mContext.enforceCallingOrSelfPermission(
1078 android.Manifest.permission.ACCESS_NETWORK_STATE,
1079 "ConnectivityService");
1080 }
1081
Robert Greenwalt14f2ef42010-06-15 12:19:37 -07001082 private void enforceConnectivityInternalPermission() {
1083 mContext.enforceCallingOrSelfPermission(
1084 android.Manifest.permission.CONNECTIVITY_INTERNAL,
1085 "ConnectivityService");
1086 }
1087
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001088 /**
Robert Greenwalt86e9e552009-07-16 17:21:39 -07001089 * Handle a {@code DISCONNECTED} event. If this pertains to the non-active
1090 * network, we ignore it. If it is for the active network, we send out a
1091 * broadcast. But first, we check whether it might be possible to connect
1092 * to a different network.
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001093 * @param info the {@code NetworkInfo} for the network
1094 */
1095 private void handleDisconnect(NetworkInfo info) {
1096
Robert Greenwalt42acef32009-08-12 16:08:25 -07001097 int prevNetType = info.getType();
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001098
Robert Greenwalt42acef32009-08-12 16:08:25 -07001099 mNetTrackers[prevNetType].setTeardownRequested(false);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001100 /*
1101 * If the disconnected network is not the active one, then don't report
1102 * this as a loss of connectivity. What probably happened is that we're
1103 * getting the disconnect for a network that we explicitly disabled
1104 * in accordance with network preference policies.
1105 */
Robert Greenwaltd55a6b42011-03-25 13:09:25 -07001106 if (!mNetConfigs[prevNetType].isDefault()) {
Robert Greenwalt42acef32009-08-12 16:08:25 -07001107 List pids = mNetRequestersPids[prevNetType];
1108 for (int i = 0; i<pids.size(); i++) {
1109 Integer pid = (Integer)pids.get(i);
1110 // will remove them because the net's no longer connected
1111 // need to do this now as only now do we know the pids and
1112 // can properly null things that are no longer referenced.
1113 reassessPidDns(pid.intValue(), false);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001114 }
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001115 }
1116
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001117 Intent intent = new Intent(ConnectivityManager.CONNECTIVITY_ACTION);
1118 intent.putExtra(ConnectivityManager.EXTRA_NETWORK_INFO, info);
1119 if (info.isFailover()) {
1120 intent.putExtra(ConnectivityManager.EXTRA_IS_FAILOVER, true);
1121 info.setFailover(false);
1122 }
1123 if (info.getReason() != null) {
1124 intent.putExtra(ConnectivityManager.EXTRA_REASON, info.getReason());
1125 }
1126 if (info.getExtraInfo() != null) {
Robert Greenwalt86e9e552009-07-16 17:21:39 -07001127 intent.putExtra(ConnectivityManager.EXTRA_EXTRA_INFO,
1128 info.getExtraInfo());
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001129 }
Robert Greenwalt42acef32009-08-12 16:08:25 -07001130
Robert Greenwaltd55a6b42011-03-25 13:09:25 -07001131 if (mNetConfigs[prevNetType].isDefault()) {
Robert Greenwaltf21ef7d2011-01-11 13:56:33 -08001132 tryFailover(prevNetType);
1133 if (mActiveDefaultNetwork != -1) {
1134 NetworkInfo switchTo = mNetTrackers[mActiveDefaultNetwork].getNetworkInfo();
Robert Greenwaltcc4b4012010-01-25 17:54:29 -08001135 intent.putExtra(ConnectivityManager.EXTRA_OTHER_NETWORK_INFO, switchTo);
1136 } else {
Robert Greenwalt029be812010-09-20 18:01:43 -07001137 mDefaultInetConditionPublished = 0; // we're not connected anymore
Robert Greenwaltcc4b4012010-01-25 17:54:29 -08001138 intent.putExtra(ConnectivityManager.EXTRA_NO_CONNECTIVITY, true);
1139 }
Robert Greenwaltda03c4e2010-01-20 19:29:41 -08001140 }
Robert Greenwalt029be812010-09-20 18:01:43 -07001141 intent.putExtra(ConnectivityManager.EXTRA_INET_CONDITION, mDefaultInetConditionPublished);
Robert Greenwaltda03c4e2010-01-20 19:29:41 -08001142 // do this before we broadcast the change
Robert Greenwaltc76b8fa2010-07-23 15:46:26 -07001143 handleConnectivityChange(prevNetType);
Robert Greenwaltda03c4e2010-01-20 19:29:41 -08001144
1145 sendStickyBroadcast(intent);
1146 /*
1147 * If the failover network is already connected, then immediately send
1148 * out a followup broadcast indicating successful failover
1149 */
Robert Greenwaltf21ef7d2011-01-11 13:56:33 -08001150 if (mActiveDefaultNetwork != -1) {
1151 sendConnectedBroadcast(mNetTrackers[mActiveDefaultNetwork].getNetworkInfo());
Robert Greenwaltda03c4e2010-01-20 19:29:41 -08001152 }
1153 }
1154
Robert Greenwaltf21ef7d2011-01-11 13:56:33 -08001155 private void tryFailover(int prevNetType) {
Robert Greenwalt42acef32009-08-12 16:08:25 -07001156 /*
Robert Greenwaltbff90182011-01-06 15:41:07 -08001157 * If this is a default network, check if other defaults are available.
1158 * Try to reconnect on all available and let them hash it out when
1159 * more than one connects.
Robert Greenwalt42acef32009-08-12 16:08:25 -07001160 */
Robert Greenwaltd55a6b42011-03-25 13:09:25 -07001161 if (mNetConfigs[prevNetType].isDefault()) {
Robert Greenwalt42acef32009-08-12 16:08:25 -07001162 if (mActiveDefaultNetwork == prevNetType) {
1163 mActiveDefaultNetwork = -1;
1164 }
1165
Robert Greenwaltf21ef7d2011-01-11 13:56:33 -08001166 // don't signal a reconnect for anything lower or equal priority than our
1167 // current connected default
1168 // TODO - don't filter by priority now - nice optimization but risky
1169// int currentPriority = -1;
1170// if (mActiveDefaultNetwork != -1) {
Robert Greenwaltd55a6b42011-03-25 13:09:25 -07001171// currentPriority = mNetConfigs[mActiveDefaultNetwork].mPriority;
Robert Greenwaltf21ef7d2011-01-11 13:56:33 -08001172// }
Robert Greenwaltda03c4e2010-01-20 19:29:41 -08001173 for (int checkType=0; checkType <= ConnectivityManager.MAX_NETWORK_TYPE; checkType++) {
Robert Greenwalt5154ae762009-10-30 14:17:42 -07001174 if (checkType == prevNetType) continue;
Robert Greenwaltd55a6b42011-03-25 13:09:25 -07001175 if (mNetConfigs[checkType] == null) continue;
1176 if (!mNetConfigs[checkType].isDefault()) continue;
Wink Saville9f7a0b22011-01-26 15:43:49 -08001177
1178// Enabling the isAvailable() optimization caused mobile to not get
1179// selected if it was in the middle of error handling. Specifically
1180// a moble connection that took 30 seconds to complete the DEACTIVATE_DATA_CALL
1181// would not be available and we wouldn't get connected to anything.
1182// So removing the isAvailable() optimization below for now. TODO: This
1183// optimization should work and we need to investigate why it doesn't work.
1184// This could be related to how DEACTIVATE_DATA_CALL is reporting its
1185// complete before it is really complete.
1186// if (!mNetTrackers[checkType].isAvailable()) continue;
1187
Robert Greenwaltd55a6b42011-03-25 13:09:25 -07001188// if (currentPriority >= mNetConfigs[checkType].mPriority) continue;
Robert Greenwalt42acef32009-08-12 16:08:25 -07001189
Robert Greenwaltf21ef7d2011-01-11 13:56:33 -08001190 NetworkStateTracker checkTracker = mNetTrackers[checkType];
1191 NetworkInfo checkInfo = checkTracker.getNetworkInfo();
1192 if (!checkInfo.isConnectedOrConnecting() || checkTracker.isTeardownRequested()) {
1193 checkInfo.setFailover(true);
1194 checkTracker.reconnect();
Robert Greenwalt42acef32009-08-12 16:08:25 -07001195 }
Robert Greenwaltf21ef7d2011-01-11 13:56:33 -08001196 if (DBG) log("Attempting to switch to " + checkInfo.getTypeName());
Robert Greenwalt42acef32009-08-12 16:08:25 -07001197 }
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001198 }
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001199 }
1200
1201 private void sendConnectedBroadcast(NetworkInfo info) {
Robert Greenwalt1e9aac22010-09-15 17:36:33 -07001202 sendGeneralBroadcast(info, ConnectivityManager.CONNECTIVITY_ACTION);
1203 }
1204
1205 private void sendInetConditionBroadcast(NetworkInfo info) {
1206 sendGeneralBroadcast(info, ConnectivityManager.INET_CONDITION_ACTION);
1207 }
1208
1209 private void sendGeneralBroadcast(NetworkInfo info, String bcastType) {
1210 Intent intent = new Intent(bcastType);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001211 intent.putExtra(ConnectivityManager.EXTRA_NETWORK_INFO, info);
1212 if (info.isFailover()) {
1213 intent.putExtra(ConnectivityManager.EXTRA_IS_FAILOVER, true);
1214 info.setFailover(false);
1215 }
1216 if (info.getReason() != null) {
1217 intent.putExtra(ConnectivityManager.EXTRA_REASON, info.getReason());
1218 }
1219 if (info.getExtraInfo() != null) {
Robert Greenwalt86e9e552009-07-16 17:21:39 -07001220 intent.putExtra(ConnectivityManager.EXTRA_EXTRA_INFO,
1221 info.getExtraInfo());
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001222 }
Robert Greenwaltd7085fc2010-09-08 15:24:47 -07001223 intent.putExtra(ConnectivityManager.EXTRA_INET_CONDITION, mDefaultInetConditionPublished);
Mike Lockwood0f79b542009-08-14 14:18:49 -04001224 sendStickyBroadcast(intent);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001225 }
1226
1227 /**
1228 * Called when an attempt to fail over to another network has failed.
1229 * @param info the {@link NetworkInfo} for the failed network
1230 */
1231 private void handleConnectionFailure(NetworkInfo info) {
1232 mNetTrackers[info.getType()].setTeardownRequested(false);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001233
Robert Greenwalt42acef32009-08-12 16:08:25 -07001234 String reason = info.getReason();
1235 String extraInfo = info.getExtraInfo();
Robert Greenwalt86e9e552009-07-16 17:21:39 -07001236
Robert Greenwalt572172b2010-10-08 16:35:52 -07001237 String reasonText;
1238 if (reason == null) {
1239 reasonText = ".";
1240 } else {
1241 reasonText = " (" + reason + ").";
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001242 }
Wink Savilleed9c02b2010-12-03 12:01:38 -08001243 loge("Attempt to connect to " + info.getTypeName() + " failed" + reasonText);
Robert Greenwalt42acef32009-08-12 16:08:25 -07001244
1245 Intent intent = new Intent(ConnectivityManager.CONNECTIVITY_ACTION);
1246 intent.putExtra(ConnectivityManager.EXTRA_NETWORK_INFO, info);
1247 if (getActiveNetworkInfo() == null) {
1248 intent.putExtra(ConnectivityManager.EXTRA_NO_CONNECTIVITY, true);
1249 }
1250 if (reason != null) {
1251 intent.putExtra(ConnectivityManager.EXTRA_REASON, reason);
1252 }
1253 if (extraInfo != null) {
1254 intent.putExtra(ConnectivityManager.EXTRA_EXTRA_INFO, extraInfo);
1255 }
1256 if (info.isFailover()) {
1257 intent.putExtra(ConnectivityManager.EXTRA_IS_FAILOVER, true);
1258 info.setFailover(false);
1259 }
Robert Greenwaltda03c4e2010-01-20 19:29:41 -08001260
Robert Greenwaltd55a6b42011-03-25 13:09:25 -07001261 if (mNetConfigs[info.getType()].isDefault()) {
Robert Greenwaltf21ef7d2011-01-11 13:56:33 -08001262 tryFailover(info.getType());
1263 if (mActiveDefaultNetwork != -1) {
1264 NetworkInfo switchTo = mNetTrackers[mActiveDefaultNetwork].getNetworkInfo();
Robert Greenwaltcc4b4012010-01-25 17:54:29 -08001265 intent.putExtra(ConnectivityManager.EXTRA_OTHER_NETWORK_INFO, switchTo);
1266 } else {
Robert Greenwalt029be812010-09-20 18:01:43 -07001267 mDefaultInetConditionPublished = 0;
Robert Greenwaltcc4b4012010-01-25 17:54:29 -08001268 intent.putExtra(ConnectivityManager.EXTRA_NO_CONNECTIVITY, true);
1269 }
Robert Greenwaltda03c4e2010-01-20 19:29:41 -08001270 }
Robert Greenwaltcc4b4012010-01-25 17:54:29 -08001271
Robert Greenwalt029be812010-09-20 18:01:43 -07001272 intent.putExtra(ConnectivityManager.EXTRA_INET_CONDITION, mDefaultInetConditionPublished);
Mike Lockwood0f79b542009-08-14 14:18:49 -04001273 sendStickyBroadcast(intent);
Robert Greenwaltda03c4e2010-01-20 19:29:41 -08001274 /*
1275 * If the failover network is already connected, then immediately send
1276 * out a followup broadcast indicating successful failover
1277 */
Robert Greenwaltf21ef7d2011-01-11 13:56:33 -08001278 if (mActiveDefaultNetwork != -1) {
1279 sendConnectedBroadcast(mNetTrackers[mActiveDefaultNetwork].getNetworkInfo());
Robert Greenwaltda03c4e2010-01-20 19:29:41 -08001280 }
Mike Lockwood0f79b542009-08-14 14:18:49 -04001281 }
1282
1283 private void sendStickyBroadcast(Intent intent) {
1284 synchronized(this) {
Dianne Hackborn1c633fc2009-12-08 19:45:14 -08001285 if (!mSystemReady) {
1286 mInitialBroadcast = new Intent(intent);
Mike Lockwood0f79b542009-08-14 14:18:49 -04001287 }
Dianne Hackborn1c633fc2009-12-08 19:45:14 -08001288 intent.addFlags(Intent.FLAG_RECEIVER_REGISTERED_ONLY_BEFORE_BOOT);
1289 mContext.sendStickyBroadcast(intent);
Mike Lockwood0f79b542009-08-14 14:18:49 -04001290 }
1291 }
1292
1293 void systemReady() {
1294 synchronized(this) {
1295 mSystemReady = true;
Dianne Hackborn1c633fc2009-12-08 19:45:14 -08001296 if (mInitialBroadcast != null) {
1297 mContext.sendStickyBroadcast(mInitialBroadcast);
1298 mInitialBroadcast = null;
Mike Lockwood0f79b542009-08-14 14:18:49 -04001299 }
1300 }
Robert Greenwalt434203a2010-10-11 16:00:27 -07001301 // load the global proxy at startup
1302 mHandler.sendMessage(mHandler.obtainMessage(EVENT_APPLY_GLOBAL_HTTP_PROXY));
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001303 }
1304
1305 private void handleConnect(NetworkInfo info) {
Robert Greenwalt42acef32009-08-12 16:08:25 -07001306 int type = info.getType();
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001307
1308 // snapshot isFailover, because sendConnectedBroadcast() resets it
1309 boolean isFailover = info.isFailover();
Robert Greenwalt42acef32009-08-12 16:08:25 -07001310 NetworkStateTracker thisNet = mNetTrackers[type];
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001311
Robert Greenwalt42acef32009-08-12 16:08:25 -07001312 // if this is a default net and other default is running
1313 // kill the one not preferred
Robert Greenwaltd55a6b42011-03-25 13:09:25 -07001314 if (mNetConfigs[type].isDefault()) {
Robert Greenwalt42acef32009-08-12 16:08:25 -07001315 if (mActiveDefaultNetwork != -1 && mActiveDefaultNetwork != type) {
1316 if ((type != mNetworkPreference &&
Wink Saville975c8482011-04-07 14:23:45 -07001317 mNetConfigs[mActiveDefaultNetwork].priority >
1318 mNetConfigs[type].priority) ||
Robert Greenwalt42acef32009-08-12 16:08:25 -07001319 mNetworkPreference == mActiveDefaultNetwork) {
1320 // don't accept this one
Wink Savilleed9c02b2010-12-03 12:01:38 -08001321 if (DBG) {
1322 log("Not broadcasting CONNECT_ACTION " +
Robert Greenwalt42acef32009-08-12 16:08:25 -07001323 "to torn down network " + info.getTypeName());
Wink Savilleed9c02b2010-12-03 12:01:38 -08001324 }
Robert Greenwalt42acef32009-08-12 16:08:25 -07001325 teardown(thisNet);
1326 return;
1327 } else {
1328 // tear down the other
1329 NetworkStateTracker otherNet =
1330 mNetTrackers[mActiveDefaultNetwork];
Wink Savilleed9c02b2010-12-03 12:01:38 -08001331 if (DBG) {
1332 log("Policy requires " + otherNet.getNetworkInfo().getTypeName() +
Robert Greenwalt42acef32009-08-12 16:08:25 -07001333 " teardown");
Wink Savilleed9c02b2010-12-03 12:01:38 -08001334 }
Robert Greenwalt42acef32009-08-12 16:08:25 -07001335 if (!teardown(otherNet)) {
Wink Savilleed9c02b2010-12-03 12:01:38 -08001336 loge("Network declined teardown request");
Robert Greenwalt27725e82011-03-29 11:36:28 -07001337 teardown(thisNet);
Robert Greenwalt42acef32009-08-12 16:08:25 -07001338 return;
1339 }
Robert Greenwalt14f2ef42010-06-15 12:19:37 -07001340 }
1341 }
1342 synchronized (ConnectivityService.this) {
1343 // have a new default network, release the transition wakelock in a second
1344 // if it's held. The second pause is to allow apps to reconnect over the
1345 // new network
1346 if (mNetTransitionWakeLock.isHeld()) {
1347 mHandler.sendMessageDelayed(mHandler.obtainMessage(
Robert Greenwaltf3331232010-09-24 14:32:21 -07001348 EVENT_CLEAR_NET_TRANSITION_WAKELOCK,
Robert Greenwalt14f2ef42010-06-15 12:19:37 -07001349 mNetTransitionWakeLockSerialNumber, 0),
1350 1000);
Robert Greenwalt42acef32009-08-12 16:08:25 -07001351 }
1352 }
1353 mActiveDefaultNetwork = type;
Robert Greenwaltd7085fc2010-09-08 15:24:47 -07001354 // this will cause us to come up initially as unconnected and switching
1355 // to connected after our normal pause unless somebody reports us as reall
1356 // disconnected
1357 mDefaultInetConditionPublished = 0;
1358 mDefaultConnectionSequence++;
1359 mInetConditionChangeInFlight = false;
1360 // Don't do this - if we never sign in stay, grey
1361 //reportNetworkCondition(mActiveDefaultNetwork, 100);
Robert Greenwalt42acef32009-08-12 16:08:25 -07001362 }
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001363 thisNet.setTeardownRequested(false);
Irfan Sheriffd649c122010-06-09 15:39:36 -07001364 updateNetworkSettings(thisNet);
Robert Greenwaltc76b8fa2010-07-23 15:46:26 -07001365 handleConnectivityChange(type);
Robert Greenwalt42acef32009-08-12 16:08:25 -07001366 sendConnectedBroadcast(info);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001367 }
1368
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001369 /**
Robert Greenwaltc76b8fa2010-07-23 15:46:26 -07001370 * After a change in the connectivity state of a network. We're mainly
1371 * concerned with making sure that the list of DNS servers is set up
1372 * according to which networks are connected, and ensuring that the
1373 * right routing table entries exist.
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001374 */
Robert Greenwaltc76b8fa2010-07-23 15:46:26 -07001375 private void handleConnectivityChange(int netType) {
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001376 /*
Robert Greenwalt42acef32009-08-12 16:08:25 -07001377 * If a non-default network is enabled, add the host routes that
Robert Greenwaltc76b8fa2010-07-23 15:46:26 -07001378 * will allow it's DNS servers to be accessed.
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001379 */
Robert Greenwaltc76b8fa2010-07-23 15:46:26 -07001380 handleDnsConfigurationChange(netType);
Robert Greenwalt42acef32009-08-12 16:08:25 -07001381
Robert Greenwaltc76b8fa2010-07-23 15:46:26 -07001382 if (mNetTrackers[netType].getNetworkInfo().isConnected()) {
Robert Greenwaltd55a6b42011-03-25 13:09:25 -07001383 if (mNetConfigs[netType].isDefault()) {
Robert Greenwalt434203a2010-10-11 16:00:27 -07001384 handleApplyDefaultProxy(netType);
Robert Greenwaltc76b8fa2010-07-23 15:46:26 -07001385 addDefaultRoute(mNetTrackers[netType]);
Robert Greenwalt42acef32009-08-12 16:08:25 -07001386 } else {
Robert Greenwalt13ec4062011-04-01 10:51:22 -07001387 // many radios add a default route even when we don't want one.
1388 // remove the default route unless we need it for our active network
1389 if (mActiveDefaultNetwork != -1) {
1390 LinkProperties defaultLinkProperties =
1391 mNetTrackers[mActiveDefaultNetwork].getLinkProperties();
1392 LinkProperties newLinkProperties =
1393 mNetTrackers[netType].getLinkProperties();
1394 String defaultIface = defaultLinkProperties.getInterfaceName();
1395 if (defaultIface != null &&
1396 !defaultIface.equals(newLinkProperties.getInterfaceName())) {
1397 removeDefaultRoute(mNetTrackers[netType]);
1398 }
1399 }
Michael Jurka8fad7ee2011-03-30 19:54:08 -07001400 addPrivateDnsRoutes(mNetTrackers[netType]);
Robert Greenwaltc76b8fa2010-07-23 15:46:26 -07001401 }
1402 } else {
Robert Greenwaltd55a6b42011-03-25 13:09:25 -07001403 if (mNetConfigs[netType].isDefault()) {
Robert Greenwaltc76b8fa2010-07-23 15:46:26 -07001404 removeDefaultRoute(mNetTrackers[netType]);
1405 } else {
1406 removePrivateDnsRoutes(mNetTrackers[netType]);
Robert Greenwalt42acef32009-08-12 16:08:25 -07001407 }
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001408 }
1409 }
1410
Irfan Sheriffd649c122010-06-09 15:39:36 -07001411 private void addPrivateDnsRoutes(NetworkStateTracker nt) {
Irfan Sheriffd649c122010-06-09 15:39:36 -07001412 boolean privateDnsRouteSet = nt.isPrivateDnsRouteSet();
Robert Greenwalt37e65eb2010-08-30 10:56:47 -07001413 LinkProperties p = nt.getLinkProperties();
Robert Greenwalt47f69fe2010-06-15 15:43:39 -07001414 if (p == null) return;
1415 String interfaceName = p.getInterfaceName();
Irfan Sheriffd649c122010-06-09 15:39:36 -07001416
1417 if (DBG) {
Wink Savilleed9c02b2010-12-03 12:01:38 -08001418 log("addPrivateDnsRoutes for " + nt +
Irfan Sheriffd649c122010-06-09 15:39:36 -07001419 "(" + interfaceName + ") - mPrivateDnsRouteSet = " + privateDnsRouteSet);
1420 }
Irfan Sheriffd649c122010-06-09 15:39:36 -07001421 if (interfaceName != null && !privateDnsRouteSet) {
Robert Greenwalt47f69fe2010-06-15 15:43:39 -07001422 Collection<InetAddress> dnsList = p.getDnses();
1423 for (InetAddress dns : dnsList) {
Wink Savilleed9c02b2010-12-03 12:01:38 -08001424 if (DBG) log(" adding " + dns);
Robert Greenwaltf43396c2011-05-06 17:10:53 -07001425 addHostRoute(nt, dns, 0);
Irfan Sheriffd649c122010-06-09 15:39:36 -07001426 }
1427 nt.privateDnsRouteSet(true);
1428 }
1429 }
1430
1431 private void removePrivateDnsRoutes(NetworkStateTracker nt) {
1432 // TODO - we should do this explicitly but the NetUtils api doesnt
1433 // support this yet - must remove all. No worse than before
Robert Greenwalt37e65eb2010-08-30 10:56:47 -07001434 LinkProperties p = nt.getLinkProperties();
Robert Greenwalt47f69fe2010-06-15 15:43:39 -07001435 if (p == null) return;
1436 String interfaceName = p.getInterfaceName();
Irfan Sheriffd649c122010-06-09 15:39:36 -07001437 boolean privateDnsRouteSet = nt.isPrivateDnsRouteSet();
1438 if (interfaceName != null && privateDnsRouteSet) {
1439 if (DBG) {
Wink Savilleed9c02b2010-12-03 12:01:38 -08001440 log("removePrivateDnsRoutes for " + nt.getNetworkInfo().getTypeName() +
Irfan Sheriffd649c122010-06-09 15:39:36 -07001441 " (" + interfaceName + ")");
1442 }
1443 NetworkUtils.removeHostRoutes(interfaceName);
1444 nt.privateDnsRouteSet(false);
1445 }
1446 }
1447
Irfan Sheriffd649c122010-06-09 15:39:36 -07001448
1449 private void addDefaultRoute(NetworkStateTracker nt) {
Robert Greenwalt37e65eb2010-08-30 10:56:47 -07001450 LinkProperties p = nt.getLinkProperties();
Robert Greenwalt47f69fe2010-06-15 15:43:39 -07001451 if (p == null) return;
1452 String interfaceName = p.getInterfaceName();
Robert Greenwalt992564e2011-02-09 13:56:06 -08001453 if (TextUtils.isEmpty(interfaceName)) return;
Robert Greenwaltaa70f102011-04-28 14:28:50 -07001454 for (RouteInfo route : p.getRoutes()) {
Irfan Sheriffd649c122010-06-09 15:39:36 -07001455
Robert Greenwaltaa70f102011-04-28 14:28:50 -07001456 //TODO - handle non-default routes
1457 if (route.isDefaultRoute()) {
1458 InetAddress gateway = route.getGateway();
Robert Greenwaltf43396c2011-05-06 17:10:53 -07001459 if (addHostRoute(nt, gateway, 0) &&
Robert Greenwaltaa70f102011-04-28 14:28:50 -07001460 NetworkUtils.addDefaultRoute(interfaceName, gateway)) {
1461 if (DBG) {
1462 NetworkInfo networkInfo = nt.getNetworkInfo();
1463 log("addDefaultRoute for " + networkInfo.getTypeName() +
1464 " (" + interfaceName + "), GatewayAddr=" +
1465 gateway.getHostAddress());
1466 }
Robert Greenwaltedcb4f92011-03-22 18:47:42 -07001467 }
Irfan Sheriffd649c122010-06-09 15:39:36 -07001468 }
Irfan Sheriffd649c122010-06-09 15:39:36 -07001469 }
1470 }
1471
1472
1473 public void removeDefaultRoute(NetworkStateTracker nt) {
Robert Greenwalt37e65eb2010-08-30 10:56:47 -07001474 LinkProperties p = nt.getLinkProperties();
Robert Greenwalt47f69fe2010-06-15 15:43:39 -07001475 if (p == null) return;
1476 String interfaceName = p.getInterfaceName();
Irfan Sheriffd649c122010-06-09 15:39:36 -07001477
Robert Greenwaltc76b8fa2010-07-23 15:46:26 -07001478 if (interfaceName != null) {
Robert Greenwaltedcb4f92011-03-22 18:47:42 -07001479 if (NetworkUtils.removeDefaultRoute(interfaceName) >= 0) {
1480 if (DBG) {
1481 NetworkInfo networkInfo = nt.getNetworkInfo();
1482 log("removeDefaultRoute for " + networkInfo.getTypeName() + " (" +
1483 interfaceName + ")");
1484 }
Irfan Sheriffd649c122010-06-09 15:39:36 -07001485 }
Irfan Sheriffd649c122010-06-09 15:39:36 -07001486 }
1487 }
1488
1489 /**
1490 * Reads the network specific TCP buffer sizes from SystemProperties
1491 * net.tcp.buffersize.[default|wifi|umts|edge|gprs] and set them for system
1492 * wide use
1493 */
1494 public void updateNetworkSettings(NetworkStateTracker nt) {
1495 String key = nt.getTcpBufferSizesPropName();
1496 String bufferSizes = SystemProperties.get(key);
1497
1498 if (bufferSizes.length() == 0) {
Wink Savilleed9c02b2010-12-03 12:01:38 -08001499 loge(key + " not found in system properties. Using defaults");
Irfan Sheriffd649c122010-06-09 15:39:36 -07001500
1501 // Setting to default values so we won't be stuck to previous values
1502 key = "net.tcp.buffersize.default";
1503 bufferSizes = SystemProperties.get(key);
1504 }
1505
1506 // Set values in kernel
1507 if (bufferSizes.length() != 0) {
1508 if (DBG) {
Wink Savilleed9c02b2010-12-03 12:01:38 -08001509 log("Setting TCP values: [" + bufferSizes
Irfan Sheriffd649c122010-06-09 15:39:36 -07001510 + "] which comes from [" + key + "]");
1511 }
1512 setBufferSize(bufferSizes);
1513 }
1514 }
1515
1516 /**
1517 * Writes TCP buffer sizes to /sys/kernel/ipv4/tcp_[r/w]mem_[min/def/max]
1518 * which maps to /proc/sys/net/ipv4/tcp_rmem and tcpwmem
1519 *
1520 * @param bufferSizes in the format of "readMin, readInitial, readMax,
1521 * writeMin, writeInitial, writeMax"
1522 */
1523 private void setBufferSize(String bufferSizes) {
1524 try {
1525 String[] values = bufferSizes.split(",");
1526
1527 if (values.length == 6) {
1528 final String prefix = "/sys/kernel/ipv4/tcp_";
1529 stringToFile(prefix + "rmem_min", values[0]);
1530 stringToFile(prefix + "rmem_def", values[1]);
1531 stringToFile(prefix + "rmem_max", values[2]);
1532 stringToFile(prefix + "wmem_min", values[3]);
1533 stringToFile(prefix + "wmem_def", values[4]);
1534 stringToFile(prefix + "wmem_max", values[5]);
1535 } else {
Wink Savilleed9c02b2010-12-03 12:01:38 -08001536 loge("Invalid buffersize string: " + bufferSizes);
Irfan Sheriffd649c122010-06-09 15:39:36 -07001537 }
1538 } catch (IOException e) {
Wink Savilleed9c02b2010-12-03 12:01:38 -08001539 loge("Can't set tcp buffer sizes:" + e);
Irfan Sheriffd649c122010-06-09 15:39:36 -07001540 }
1541 }
1542
1543 /**
1544 * Writes string to file. Basically same as "echo -n $string > $filename"
1545 *
1546 * @param filename
1547 * @param string
1548 * @throws IOException
1549 */
1550 private void stringToFile(String filename, String string) throws IOException {
1551 FileWriter out = new FileWriter(filename);
1552 try {
1553 out.write(string);
1554 } finally {
1555 out.close();
1556 }
1557 }
1558
1559
Robert Greenwalt42acef32009-08-12 16:08:25 -07001560 /**
1561 * Adjust the per-process dns entries (net.dns<x>.<pid>) based
1562 * on the highest priority active net which this process requested.
1563 * If there aren't any, clear it out
1564 */
1565 private void reassessPidDns(int myPid, boolean doBump)
1566 {
Wink Savilleed9c02b2010-12-03 12:01:38 -08001567 if (DBG) log("reassessPidDns for pid " + myPid);
Robert Greenwalt42acef32009-08-12 16:08:25 -07001568 for(int i : mPriorityList) {
Robert Greenwaltd55a6b42011-03-25 13:09:25 -07001569 if (mNetConfigs[i].isDefault()) {
Robert Greenwalt42acef32009-08-12 16:08:25 -07001570 continue;
1571 }
1572 NetworkStateTracker nt = mNetTrackers[i];
Robert Greenwalt86e9e552009-07-16 17:21:39 -07001573 if (nt.getNetworkInfo().isConnected() &&
1574 !nt.isTeardownRequested()) {
Robert Greenwalt37e65eb2010-08-30 10:56:47 -07001575 LinkProperties p = nt.getLinkProperties();
Robert Greenwalt47f69fe2010-06-15 15:43:39 -07001576 if (p == null) continue;
Robert Greenwalt42acef32009-08-12 16:08:25 -07001577 List pids = mNetRequestersPids[i];
1578 for (int j=0; j<pids.size(); j++) {
1579 Integer pid = (Integer)pids.get(j);
1580 if (pid.intValue() == myPid) {
Robert Greenwalt47f69fe2010-06-15 15:43:39 -07001581 Collection<InetAddress> dnses = p.getDnses();
1582 writePidDns(dnses, myPid);
Robert Greenwalt42acef32009-08-12 16:08:25 -07001583 if (doBump) {
1584 bumpDns();
1585 }
1586 return;
1587 }
1588 }
1589 }
1590 }
1591 // nothing found - delete
1592 for (int i = 1; ; i++) {
1593 String prop = "net.dns" + i + "." + myPid;
1594 if (SystemProperties.get(prop).length() == 0) {
1595 if (doBump) {
1596 bumpDns();
1597 }
1598 return;
1599 }
1600 SystemProperties.set(prop, "");
1601 }
1602 }
1603
Robert Greenwalt10398722010-12-17 15:20:36 -08001604 // return true if results in a change
1605 private boolean writePidDns(Collection <InetAddress> dnses, int pid) {
Robert Greenwalt42acef32009-08-12 16:08:25 -07001606 int j = 1;
Robert Greenwalt10398722010-12-17 15:20:36 -08001607 boolean changed = false;
Robert Greenwalt47f69fe2010-06-15 15:43:39 -07001608 for (InetAddress dns : dnses) {
Robert Greenwalt10398722010-12-17 15:20:36 -08001609 String dnsString = dns.getHostAddress();
1610 if (changed || !dnsString.equals(SystemProperties.get("net.dns" + j + "." + pid))) {
1611 changed = true;
1612 SystemProperties.set("net.dns" + j++ + "." + pid, dns.getHostAddress());
1613 }
Robert Greenwalt42acef32009-08-12 16:08:25 -07001614 }
Robert Greenwalt10398722010-12-17 15:20:36 -08001615 return changed;
Robert Greenwalt42acef32009-08-12 16:08:25 -07001616 }
1617
1618 private void bumpDns() {
1619 /*
1620 * Bump the property that tells the name resolver library to reread
1621 * the DNS server list from the properties.
1622 */
1623 String propVal = SystemProperties.get("net.dnschange");
1624 int n = 0;
1625 if (propVal.length() != 0) {
1626 try {
1627 n = Integer.parseInt(propVal);
1628 } catch (NumberFormatException e) {}
1629 }
1630 SystemProperties.set("net.dnschange", "" + (n+1));
Robert Greenwalt03595d02010-11-02 14:08:23 -07001631 /*
1632 * Tell the VMs to toss their DNS caches
1633 */
1634 Intent intent = new Intent(Intent.ACTION_CLEAR_DNS_CACHE);
1635 intent.addFlags(Intent.FLAG_RECEIVER_REPLACE_PENDING);
Stan Chesnutt3d1db862011-01-05 17:14:03 -08001636 /*
1637 * Connectivity events can happen before boot has completed ...
1638 */
1639 intent.addFlags(Intent.FLAG_RECEIVER_REGISTERED_ONLY_BEFORE_BOOT);
Robert Greenwalt03595d02010-11-02 14:08:23 -07001640 mContext.sendBroadcast(intent);
Robert Greenwalt42acef32009-08-12 16:08:25 -07001641 }
1642
Robert Greenwaltc76b8fa2010-07-23 15:46:26 -07001643 private void handleDnsConfigurationChange(int netType) {
Robert Greenwalt42acef32009-08-12 16:08:25 -07001644 // add default net's dns entries
Robert Greenwaltc76b8fa2010-07-23 15:46:26 -07001645 NetworkStateTracker nt = mNetTrackers[netType];
1646 if (nt != null && nt.getNetworkInfo().isConnected() && !nt.isTeardownRequested()) {
Robert Greenwalt37e65eb2010-08-30 10:56:47 -07001647 LinkProperties p = nt.getLinkProperties();
Robert Greenwaltc76b8fa2010-07-23 15:46:26 -07001648 if (p == null) return;
1649 Collection<InetAddress> dnses = p.getDnses();
Robert Greenwalt10398722010-12-17 15:20:36 -08001650 boolean changed = false;
Robert Greenwaltd55a6b42011-03-25 13:09:25 -07001651 if (mNetConfigs[netType].isDefault()) {
Robert Greenwaltc76b8fa2010-07-23 15:46:26 -07001652 int j = 1;
Robert Greenwalte90aa5e2010-09-01 11:34:05 -07001653 if (dnses.size() == 0 && mDefaultDns != null) {
Robert Greenwalt10398722010-12-17 15:20:36 -08001654 String dnsString = mDefaultDns.getHostAddress();
1655 if (!dnsString.equals(SystemProperties.get("net.dns1"))) {
1656 if (DBG) {
1657 log("no dns provided - using " + dnsString);
1658 }
1659 changed = true;
1660 SystemProperties.set("net.dns1", dnsString);
Robert Greenwalt42acef32009-08-12 16:08:25 -07001661 }
Robert Greenwalte90aa5e2010-09-01 11:34:05 -07001662 j++;
1663 } else {
1664 for (InetAddress dns : dnses) {
Robert Greenwalt10398722010-12-17 15:20:36 -08001665 String dnsString = dns.getHostAddress();
1666 if (!changed && dnsString.equals(SystemProperties.get("net.dns" + j))) {
1667 j++;
1668 continue;
1669 }
Robert Greenwalte90aa5e2010-09-01 11:34:05 -07001670 if (DBG) {
Wink Savilleed9c02b2010-12-03 12:01:38 -08001671 log("adding dns " + dns + " for " +
Robert Greenwalte90aa5e2010-09-01 11:34:05 -07001672 nt.getNetworkInfo().getTypeName());
1673 }
Robert Greenwalt10398722010-12-17 15:20:36 -08001674 changed = true;
1675 SystemProperties.set("net.dns" + j++, dnsString);
Robert Greenwalte90aa5e2010-09-01 11:34:05 -07001676 }
Robert Greenwaltc76b8fa2010-07-23 15:46:26 -07001677 }
1678 for (int k=j ; k<mNumDnsEntries; k++) {
Robert Greenwalt10398722010-12-17 15:20:36 -08001679 if (changed || !TextUtils.isEmpty(SystemProperties.get("net.dns" + k))) {
1680 if (DBG) log("erasing net.dns" + k);
1681 changed = true;
1682 SystemProperties.set("net.dns" + k, "");
1683 }
Robert Greenwaltc76b8fa2010-07-23 15:46:26 -07001684 }
1685 mNumDnsEntries = j;
1686 } else {
1687 // set per-pid dns for attached secondary nets
1688 List pids = mNetRequestersPids[netType];
1689 for (int y=0; y< pids.size(); y++) {
1690 Integer pid = (Integer)pids.get(y);
Robert Greenwalt10398722010-12-17 15:20:36 -08001691 changed = writePidDns(dnses, pid.intValue());
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001692 }
1693 }
Robert Greenwalt10398722010-12-17 15:20:36 -08001694 if (changed) bumpDns();
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001695 }
Robert Greenwalt42acef32009-08-12 16:08:25 -07001696 }
1697
Robert Greenwaltf2102f72011-05-03 19:02:44 -07001698 private int getRestoreDefaultNetworkDelay(int networkType) {
Robert Greenwalt42acef32009-08-12 16:08:25 -07001699 String restoreDefaultNetworkDelayStr = SystemProperties.get(
1700 NETWORK_RESTORE_DELAY_PROP_NAME);
1701 if(restoreDefaultNetworkDelayStr != null &&
1702 restoreDefaultNetworkDelayStr.length() != 0) {
1703 try {
1704 return Integer.valueOf(restoreDefaultNetworkDelayStr);
1705 } catch (NumberFormatException e) {
1706 }
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001707 }
Robert Greenwaltf2102f72011-05-03 19:02:44 -07001708 // if the system property isn't set, use the value for the apn type
1709 int ret = RESTORE_DEFAULT_NETWORK_DELAY;
1710
1711 if ((networkType <= ConnectivityManager.MAX_NETWORK_TYPE) &&
1712 (mNetConfigs[networkType] != null)) {
1713 ret = mNetConfigs[networkType].restoreTime;
1714 }
1715 return ret;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001716 }
1717
1718 @Override
1719 protected void dump(FileDescriptor fd, PrintWriter pw, String[] args) {
Robert Greenwalt86e9e552009-07-16 17:21:39 -07001720 if (mContext.checkCallingOrSelfPermission(
1721 android.Manifest.permission.DUMP)
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001722 != PackageManager.PERMISSION_GRANTED) {
Robert Greenwalt86e9e552009-07-16 17:21:39 -07001723 pw.println("Permission Denial: can't dump ConnectivityService " +
1724 "from from pid=" + Binder.getCallingPid() + ", uid=" +
1725 Binder.getCallingUid());
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001726 return;
1727 }
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001728 pw.println();
1729 for (NetworkStateTracker nst : mNetTrackers) {
Robert Greenwaltb9285352009-12-21 18:24:07 -08001730 if (nst != null) {
1731 if (nst.getNetworkInfo().isConnected()) {
1732 pw.println("Active network: " + nst.getNetworkInfo().
1733 getTypeName());
1734 }
1735 pw.println(nst.getNetworkInfo());
1736 pw.println(nst);
1737 pw.println();
Robert Greenwalt42acef32009-08-12 16:08:25 -07001738 }
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001739 }
Robert Greenwaltb9285352009-12-21 18:24:07 -08001740
1741 pw.println("Network Requester Pids:");
1742 for (int net : mPriorityList) {
1743 String pidString = net + ": ";
1744 for (Object pid : mNetRequestersPids[net]) {
1745 pidString = pidString + pid.toString() + ", ";
1746 }
1747 pw.println(pidString);
1748 }
1749 pw.println();
1750
1751 pw.println("FeatureUsers:");
1752 for (Object requester : mFeatureUsers) {
1753 pw.println(requester.toString());
1754 }
1755 pw.println();
Robert Greenwalt2a091d72010-02-11 18:18:40 -08001756
Robert Greenwalt14f2ef42010-06-15 12:19:37 -07001757 synchronized (this) {
1758 pw.println("NetworkTranstionWakeLock is currently " +
1759 (mNetTransitionWakeLock.isHeld() ? "" : "not ") + "held.");
1760 pw.println("It was last requested for "+mNetTransitionWakeLockCausedBy);
1761 }
1762 pw.println();
1763
Robert Greenwalt2a091d72010-02-11 18:18:40 -08001764 mTethering.dump(fd, pw, args);
Robert Greenwalt4e8dfef2010-09-20 14:35:25 -07001765
1766 if (mInetLog != null) {
1767 pw.println();
1768 pw.println("Inet condition reports:");
1769 for(int i = 0; i < mInetLog.size(); i++) {
1770 pw.println(mInetLog.get(i));
1771 }
1772 }
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001773 }
1774
Robert Greenwalt42acef32009-08-12 16:08:25 -07001775 // must be stateless - things change under us.
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001776 private class MyHandler extends Handler {
Wink Savillebb08caf2010-09-02 19:23:52 -07001777 public MyHandler(Looper looper) {
1778 super(looper);
1779 }
1780
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001781 @Override
1782 public void handleMessage(Message msg) {
1783 NetworkInfo info;
1784 switch (msg.what) {
1785 case NetworkStateTracker.EVENT_STATE_CHANGED:
1786 info = (NetworkInfo) msg.obj;
Robert Greenwalt511288a2009-12-07 11:33:18 -08001787 int type = info.getType();
1788 NetworkInfo.State state = info.getState();
Robert Greenwalt511288a2009-12-07 11:33:18 -08001789
Wink Savilleed9c02b2010-12-03 12:01:38 -08001790 if (DBG) log("ConnectivityChange for " +
Robert Greenwalt86e9e552009-07-16 17:21:39 -07001791 info.getTypeName() + ": " +
Robert Greenwalt511288a2009-12-07 11:33:18 -08001792 state + "/" + info.getDetailedState());
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001793
1794 // Connectivity state changed:
1795 // [31-13] Reserved for future use
Robert Greenwalt86e9e552009-07-16 17:21:39 -07001796 // [12-9] Network subtype (for mobile network, as defined
1797 // by TelephonyManager)
1798 // [8-3] Detailed state ordinal (as defined by
1799 // NetworkInfo.DetailedState)
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001800 // [2-0] Network type (as defined by ConnectivityManager)
1801 int eventLogParam = (info.getType() & 0x7) |
1802 ((info.getDetailedState().ordinal() & 0x3f) << 3) |
1803 (info.getSubtype() << 9);
Doug Zongkerab5c49c2009-12-04 10:31:43 -08001804 EventLog.writeEvent(EventLogTags.CONNECTIVITY_STATE_CHANGED,
Robert Greenwalt86e9e552009-07-16 17:21:39 -07001805 eventLogParam);
1806
1807 if (info.getDetailedState() ==
1808 NetworkInfo.DetailedState.FAILED) {
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001809 handleConnectionFailure(info);
Robert Greenwalt511288a2009-12-07 11:33:18 -08001810 } else if (state == NetworkInfo.State.DISCONNECTED) {
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001811 handleDisconnect(info);
Robert Greenwalt511288a2009-12-07 11:33:18 -08001812 } else if (state == NetworkInfo.State.SUSPENDED) {
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001813 // TODO: need to think this over.
Robert Greenwalt86e9e552009-07-16 17:21:39 -07001814 // the logic here is, handle SUSPENDED the same as
1815 // DISCONNECTED. The only difference being we are
1816 // broadcasting an intent with NetworkInfo that's
1817 // suspended. This allows the applications an
1818 // opportunity to handle DISCONNECTED and SUSPENDED
1819 // differently, or not.
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001820 handleDisconnect(info);
Robert Greenwalt511288a2009-12-07 11:33:18 -08001821 } else if (state == NetworkInfo.State.CONNECTED) {
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001822 handleConnect(info);
1823 }
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001824 break;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001825 case NetworkStateTracker.EVENT_CONFIGURATION_CHANGED:
Robert Greenwaltc76b8fa2010-07-23 15:46:26 -07001826 info = (NetworkInfo) msg.obj;
Robert Greenwaltd55a6b42011-03-25 13:09:25 -07001827 handleConnectivityChange(info.getType());
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001828 break;
Robert Greenwaltf3331232010-09-24 14:32:21 -07001829 case EVENT_CLEAR_NET_TRANSITION_WAKELOCK:
Robert Greenwalt14f2ef42010-06-15 12:19:37 -07001830 String causedBy = null;
1831 synchronized (ConnectivityService.this) {
1832 if (msg.arg1 == mNetTransitionWakeLockSerialNumber &&
1833 mNetTransitionWakeLock.isHeld()) {
1834 mNetTransitionWakeLock.release();
1835 causedBy = mNetTransitionWakeLockCausedBy;
1836 }
1837 }
1838 if (causedBy != null) {
Wink Savilleed9c02b2010-12-03 12:01:38 -08001839 log("NetTransition Wakelock for " + causedBy + " released by timeout");
Robert Greenwalt14f2ef42010-06-15 12:19:37 -07001840 }
Robert Greenwalt057d5e92010-09-09 14:05:10 -07001841 break;
Robert Greenwalt8dcc28b2010-09-23 10:05:56 -07001842 case EVENT_RESTORE_DEFAULT_NETWORK:
Robert Greenwalt9c75d4a2009-09-27 17:27:04 -07001843 FeatureUser u = (FeatureUser)msg.obj;
1844 u.expire();
Robert Greenwaltd7085fc2010-09-08 15:24:47 -07001845 break;
Robert Greenwalt8dcc28b2010-09-23 10:05:56 -07001846 case EVENT_INET_CONDITION_CHANGE:
1847 {
1848 int netType = msg.arg1;
1849 int condition = msg.arg2;
1850 handleInetConditionChange(netType, condition);
Robert Greenwalt14f2ef42010-06-15 12:19:37 -07001851 break;
Robert Greenwalt8dcc28b2010-09-23 10:05:56 -07001852 }
1853 case EVENT_INET_CONDITION_HOLD_END:
1854 {
1855 int netType = msg.arg1;
1856 int sequence = msg.arg2;
1857 handleInetConditionHoldEnd(netType, sequence);
Robert Greenwaltd7085fc2010-09-08 15:24:47 -07001858 break;
Robert Greenwalt8dcc28b2010-09-23 10:05:56 -07001859 }
1860 case EVENT_SET_NETWORK_PREFERENCE:
1861 {
1862 int preference = msg.arg1;
1863 handleSetNetworkPreference(preference);
1864 break;
1865 }
1866 case EVENT_SET_BACKGROUND_DATA:
1867 {
1868 boolean enabled = (msg.arg1 == ENABLED);
1869 handleSetBackgroundData(enabled);
1870 break;
1871 }
1872 case EVENT_SET_MOBILE_DATA:
1873 {
1874 boolean enabled = (msg.arg1 == ENABLED);
1875 handleSetMobileData(enabled);
1876 break;
1877 }
Robert Greenwalt434203a2010-10-11 16:00:27 -07001878 case EVENT_APPLY_GLOBAL_HTTP_PROXY:
1879 {
1880 handleDeprecatedGlobalHttpProxy();
Robert Greenwaltd55a6b42011-03-25 13:09:25 -07001881 break;
1882 }
1883 case EVENT_SET_DEPENDENCY_MET:
1884 {
1885 boolean met = (msg.arg1 == ENABLED);
1886 handleSetDependencyMet(msg.arg2, met);
1887 break;
Robert Greenwalt434203a2010-10-11 16:00:27 -07001888 }
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001889 }
1890 }
1891 }
Robert Greenwaltd0e18ff2010-01-26 11:40:34 -08001892
1893 // javadoc from interface
Robert Greenwalt5a735062010-03-02 17:25:02 -08001894 public int tether(String iface) {
Robert Greenwaltd0e18ff2010-01-26 11:40:34 -08001895 enforceTetherChangePermission();
Robert Greenwalt5a735062010-03-02 17:25:02 -08001896
1897 if (isTetheringSupported()) {
1898 return mTethering.tether(iface);
1899 } else {
1900 return ConnectivityManager.TETHER_ERROR_UNSUPPORTED;
1901 }
Robert Greenwaltd0e18ff2010-01-26 11:40:34 -08001902 }
1903
1904 // javadoc from interface
Robert Greenwalt5a735062010-03-02 17:25:02 -08001905 public int untether(String iface) {
Robert Greenwaltd0e18ff2010-01-26 11:40:34 -08001906 enforceTetherChangePermission();
Robert Greenwalt5a735062010-03-02 17:25:02 -08001907
1908 if (isTetheringSupported()) {
1909 return mTethering.untether(iface);
1910 } else {
1911 return ConnectivityManager.TETHER_ERROR_UNSUPPORTED;
1912 }
1913 }
1914
1915 // javadoc from interface
1916 public int getLastTetherError(String iface) {
1917 enforceTetherAccessPermission();
1918
1919 if (isTetheringSupported()) {
1920 return mTethering.getLastTetherError(iface);
1921 } else {
1922 return ConnectivityManager.TETHER_ERROR_UNSUPPORTED;
1923 }
Robert Greenwalt2a091d72010-02-11 18:18:40 -08001924 }
1925
1926 // TODO - proper iface API for selection by property, inspection, etc
1927 public String[] getTetherableUsbRegexs() {
1928 enforceTetherAccessPermission();
1929 if (isTetheringSupported()) {
1930 return mTethering.getTetherableUsbRegexs();
1931 } else {
1932 return new String[0];
1933 }
1934 }
1935
1936 public String[] getTetherableWifiRegexs() {
1937 enforceTetherAccessPermission();
1938 if (isTetheringSupported()) {
1939 return mTethering.getTetherableWifiRegexs();
1940 } else {
1941 return new String[0];
1942 }
Robert Greenwaltd0e18ff2010-01-26 11:40:34 -08001943 }
1944
Danica Chang6fdd0c62010-08-11 14:54:43 -07001945 public String[] getTetherableBluetoothRegexs() {
1946 enforceTetherAccessPermission();
1947 if (isTetheringSupported()) {
1948 return mTethering.getTetherableBluetoothRegexs();
1949 } else {
1950 return new String[0];
1951 }
1952 }
1953
Robert Greenwaltd0e18ff2010-01-26 11:40:34 -08001954 // TODO - move iface listing, queries, etc to new module
1955 // javadoc from interface
1956 public String[] getTetherableIfaces() {
Robert Greenwalt2a091d72010-02-11 18:18:40 -08001957 enforceTetherAccessPermission();
Robert Greenwaltd0e18ff2010-01-26 11:40:34 -08001958 return mTethering.getTetherableIfaces();
1959 }
1960
1961 public String[] getTetheredIfaces() {
Robert Greenwalt2a091d72010-02-11 18:18:40 -08001962 enforceTetherAccessPermission();
Robert Greenwaltd0e18ff2010-01-26 11:40:34 -08001963 return mTethering.getTetheredIfaces();
1964 }
Robert Greenwalt2a091d72010-02-11 18:18:40 -08001965
Robert Greenwalt5a735062010-03-02 17:25:02 -08001966 public String[] getTetheringErroredIfaces() {
1967 enforceTetherAccessPermission();
1968 return mTethering.getErroredIfaces();
1969 }
1970
Robert Greenwalt2a091d72010-02-11 18:18:40 -08001971 // if ro.tether.denied = true we default to no tethering
1972 // gservices could set the secure setting to 1 though to enable it on a build where it
1973 // had previously been turned off.
1974 public boolean isTetheringSupported() {
1975 enforceTetherAccessPermission();
1976 int defaultVal = (SystemProperties.get("ro.tether.denied").equals("true") ? 0 : 1);
Robert Greenwaltc9d5fb72010-02-25 12:29:30 -08001977 boolean tetherEnabledInSettings = (Settings.Secure.getInt(mContext.getContentResolver(),
1978 Settings.Secure.TETHER_SUPPORTED, defaultVal) != 0);
1979 return tetherEnabledInSettings && mTetheringConfigValid;
Robert Greenwalt2a091d72010-02-11 18:18:40 -08001980 }
Robert Greenwalt14f2ef42010-06-15 12:19:37 -07001981
1982 // An API NetworkStateTrackers can call when they lose their network.
1983 // This will automatically be cleared after X seconds or a network becomes CONNECTED,
1984 // whichever happens first. The timer is started by the first caller and not
1985 // restarted by subsequent callers.
1986 public void requestNetworkTransitionWakelock(String forWhom) {
1987 enforceConnectivityInternalPermission();
1988 synchronized (this) {
1989 if (mNetTransitionWakeLock.isHeld()) return;
1990 mNetTransitionWakeLockSerialNumber++;
1991 mNetTransitionWakeLock.acquire();
1992 mNetTransitionWakeLockCausedBy = forWhom;
1993 }
1994 mHandler.sendMessageDelayed(mHandler.obtainMessage(
Robert Greenwaltf3331232010-09-24 14:32:21 -07001995 EVENT_CLEAR_NET_TRANSITION_WAKELOCK,
Robert Greenwalt14f2ef42010-06-15 12:19:37 -07001996 mNetTransitionWakeLockSerialNumber, 0),
1997 mNetTransitionWakeLockTimeout);
1998 return;
1999 }
Robert Greenwaltca4306c2010-09-09 13:15:32 -07002000
Robert Greenwaltd7085fc2010-09-08 15:24:47 -07002001 // 100 percent is full good, 0 is full bad.
2002 public void reportInetCondition(int networkType, int percentage) {
Wink Savilleed9c02b2010-12-03 12:01:38 -08002003 if (DBG) log("reportNetworkCondition(" + networkType + ", " + percentage + ")");
Robert Greenwaltd7085fc2010-09-08 15:24:47 -07002004 mContext.enforceCallingOrSelfPermission(
2005 android.Manifest.permission.STATUS_BAR,
2006 "ConnectivityService");
2007
Robert Greenwalt4e8dfef2010-09-20 14:35:25 -07002008 if (DBG) {
2009 int pid = getCallingPid();
2010 int uid = getCallingUid();
2011 String s = pid + "(" + uid + ") reports inet is " +
2012 (percentage > 50 ? "connected" : "disconnected") + " (" + percentage + ") on " +
2013 "network Type " + networkType + " at " + GregorianCalendar.getInstance().getTime();
2014 mInetLog.add(s);
2015 while(mInetLog.size() > INET_CONDITION_LOG_MAX_SIZE) {
2016 mInetLog.remove(0);
2017 }
2018 }
Robert Greenwaltd7085fc2010-09-08 15:24:47 -07002019 mHandler.sendMessage(mHandler.obtainMessage(
Robert Greenwalt8dcc28b2010-09-23 10:05:56 -07002020 EVENT_INET_CONDITION_CHANGE, networkType, percentage));
2021 }
2022
2023 private void handleInetConditionChange(int netType, int condition) {
2024 if (DBG) {
Wink Savilleed9c02b2010-12-03 12:01:38 -08002025 log("Inet connectivity change, net=" +
Robert Greenwalt8dcc28b2010-09-23 10:05:56 -07002026 netType + ", condition=" + condition +
2027 ",mActiveDefaultNetwork=" + mActiveDefaultNetwork);
2028 }
2029 if (mActiveDefaultNetwork == -1) {
Wink Savilleed9c02b2010-12-03 12:01:38 -08002030 if (DBG) log("no active default network - aborting");
Robert Greenwalt8dcc28b2010-09-23 10:05:56 -07002031 return;
2032 }
2033 if (mActiveDefaultNetwork != netType) {
Wink Savilleed9c02b2010-12-03 12:01:38 -08002034 if (DBG) log("given net not default - aborting");
Robert Greenwalt8dcc28b2010-09-23 10:05:56 -07002035 return;
2036 }
2037 mDefaultInetCondition = condition;
2038 int delay;
2039 if (mInetConditionChangeInFlight == false) {
Wink Savilleed9c02b2010-12-03 12:01:38 -08002040 if (DBG) log("starting a change hold");
Robert Greenwalt8dcc28b2010-09-23 10:05:56 -07002041 // setup a new hold to debounce this
2042 if (mDefaultInetCondition > 50) {
2043 delay = Settings.Secure.getInt(mContext.getContentResolver(),
2044 Settings.Secure.INET_CONDITION_DEBOUNCE_UP_DELAY, 500);
2045 } else {
2046 delay = Settings.Secure.getInt(mContext.getContentResolver(),
2047 Settings.Secure.INET_CONDITION_DEBOUNCE_DOWN_DELAY, 3000);
2048 }
2049 mInetConditionChangeInFlight = true;
2050 mHandler.sendMessageDelayed(mHandler.obtainMessage(EVENT_INET_CONDITION_HOLD_END,
2051 mActiveDefaultNetwork, mDefaultConnectionSequence), delay);
2052 } else {
2053 // we've set the new condition, when this hold ends that will get
2054 // picked up
Wink Savilleed9c02b2010-12-03 12:01:38 -08002055 if (DBG) log("currently in hold - not setting new end evt");
Robert Greenwalt8dcc28b2010-09-23 10:05:56 -07002056 }
2057 }
2058
2059 private void handleInetConditionHoldEnd(int netType, int sequence) {
2060 if (DBG) {
Wink Savilleed9c02b2010-12-03 12:01:38 -08002061 log("Inet hold end, net=" + netType +
Robert Greenwalt8dcc28b2010-09-23 10:05:56 -07002062 ", condition =" + mDefaultInetCondition +
2063 ", published condition =" + mDefaultInetConditionPublished);
2064 }
2065 mInetConditionChangeInFlight = false;
2066
2067 if (mActiveDefaultNetwork == -1) {
Wink Savilleed9c02b2010-12-03 12:01:38 -08002068 if (DBG) log("no active default network - aborting");
Robert Greenwalt8dcc28b2010-09-23 10:05:56 -07002069 return;
2070 }
2071 if (mDefaultConnectionSequence != sequence) {
Wink Savilleed9c02b2010-12-03 12:01:38 -08002072 if (DBG) log("event hold for obsolete network - aborting");
Robert Greenwalt8dcc28b2010-09-23 10:05:56 -07002073 return;
2074 }
2075 if (mDefaultInetConditionPublished == mDefaultInetCondition) {
Wink Savilleed9c02b2010-12-03 12:01:38 -08002076 if (DBG) log("no change in condition - aborting");
Robert Greenwalt8dcc28b2010-09-23 10:05:56 -07002077 return;
2078 }
2079 NetworkInfo networkInfo = mNetTrackers[mActiveDefaultNetwork].getNetworkInfo();
2080 if (networkInfo.isConnected() == false) {
Wink Savilleed9c02b2010-12-03 12:01:38 -08002081 if (DBG) log("default network not connected - aborting");
Robert Greenwalt8dcc28b2010-09-23 10:05:56 -07002082 return;
2083 }
2084 mDefaultInetConditionPublished = mDefaultInetCondition;
2085 sendInetConditionBroadcast(networkInfo);
2086 return;
Robert Greenwaltd7085fc2010-09-08 15:24:47 -07002087 }
Robert Greenwalt434203a2010-10-11 16:00:27 -07002088
2089 public synchronized ProxyProperties getProxy() {
2090 if (mGlobalProxy != null) return mGlobalProxy;
2091 if (mDefaultProxy != null) return mDefaultProxy;
2092 return null;
2093 }
2094
2095 public void setGlobalProxy(ProxyProperties proxyProperties) {
2096 enforceChangePermission();
2097 synchronized (mGlobalProxyLock) {
2098 if (proxyProperties == mGlobalProxy) return;
2099 if (proxyProperties != null && proxyProperties.equals(mGlobalProxy)) return;
2100 if (mGlobalProxy != null && mGlobalProxy.equals(proxyProperties)) return;
2101
2102 String host = "";
2103 int port = 0;
2104 String exclList = "";
2105 if (proxyProperties != null && !TextUtils.isEmpty(proxyProperties.getHost())) {
2106 mGlobalProxy = new ProxyProperties(proxyProperties);
2107 host = mGlobalProxy.getHost();
2108 port = mGlobalProxy.getPort();
2109 exclList = mGlobalProxy.getExclusionList();
2110 } else {
2111 mGlobalProxy = null;
2112 }
2113 ContentResolver res = mContext.getContentResolver();
2114 Settings.Secure.putString(res, Settings.Secure.GLOBAL_HTTP_PROXY_HOST, host);
2115 Settings.Secure.putInt(res, Settings.Secure.GLOBAL_HTTP_PROXY_PORT, port);
Robert Greenwaltb7090d62010-12-02 11:31:00 -08002116 Settings.Secure.putString(res, Settings.Secure.GLOBAL_HTTP_PROXY_EXCLUSION_LIST,
Robert Greenwalt434203a2010-10-11 16:00:27 -07002117 exclList);
2118 }
2119
2120 if (mGlobalProxy == null) {
2121 proxyProperties = mDefaultProxy;
2122 }
2123 sendProxyBroadcast(proxyProperties);
2124 }
2125
Robert Greenwaltb7090d62010-12-02 11:31:00 -08002126 private void loadGlobalProxy() {
2127 ContentResolver res = mContext.getContentResolver();
2128 String host = Settings.Secure.getString(res, Settings.Secure.GLOBAL_HTTP_PROXY_HOST);
2129 int port = Settings.Secure.getInt(res, Settings.Secure.GLOBAL_HTTP_PROXY_PORT, 0);
2130 String exclList = Settings.Secure.getString(res,
2131 Settings.Secure.GLOBAL_HTTP_PROXY_EXCLUSION_LIST);
2132 if (!TextUtils.isEmpty(host)) {
2133 ProxyProperties proxyProperties = new ProxyProperties(host, port, exclList);
2134 synchronized (mGlobalProxyLock) {
2135 mGlobalProxy = proxyProperties;
2136 }
2137 }
2138 }
2139
Robert Greenwalt434203a2010-10-11 16:00:27 -07002140 public ProxyProperties getGlobalProxy() {
2141 synchronized (mGlobalProxyLock) {
2142 return mGlobalProxy;
2143 }
2144 }
2145
2146 private void handleApplyDefaultProxy(int type) {
2147 // check if new default - push it out to all VM if so
2148 ProxyProperties proxy = mNetTrackers[type].getLinkProperties().getHttpProxy();
2149 synchronized (this) {
2150 if (mDefaultProxy != null && mDefaultProxy.equals(proxy)) return;
2151 if (mDefaultProxy == proxy) return;
2152 if (!TextUtils.isEmpty(proxy.getHost())) {
2153 mDefaultProxy = proxy;
2154 } else {
2155 mDefaultProxy = null;
2156 }
2157 }
Wink Savilleed9c02b2010-12-03 12:01:38 -08002158 if (DBG) log("changing default proxy to " + proxy);
Robert Greenwalt434203a2010-10-11 16:00:27 -07002159 if ((proxy == null && mGlobalProxy == null) || proxy.equals(mGlobalProxy)) return;
2160 if (mGlobalProxy != null) return;
2161 sendProxyBroadcast(proxy);
2162 }
2163
2164 private void handleDeprecatedGlobalHttpProxy() {
2165 String proxy = Settings.Secure.getString(mContext.getContentResolver(),
2166 Settings.Secure.HTTP_PROXY);
2167 if (!TextUtils.isEmpty(proxy)) {
2168 String data[] = proxy.split(":");
2169 String proxyHost = data[0];
2170 int proxyPort = 8080;
2171 if (data.length > 1) {
2172 try {
2173 proxyPort = Integer.parseInt(data[1]);
2174 } catch (NumberFormatException e) {
2175 return;
2176 }
2177 }
2178 ProxyProperties p = new ProxyProperties(data[0], proxyPort, "");
2179 setGlobalProxy(p);
2180 }
2181 }
2182
2183 private void sendProxyBroadcast(ProxyProperties proxy) {
Robert Greenwalt55985be2010-12-23 15:51:10 -08002184 if (proxy == null) proxy = new ProxyProperties("", 0, "");
Wink Savilleed9c02b2010-12-03 12:01:38 -08002185 log("sending Proxy Broadcast for " + proxy);
Robert Greenwalt434203a2010-10-11 16:00:27 -07002186 Intent intent = new Intent(Proxy.PROXY_CHANGE_ACTION);
Stan Chesnuttb35d67a2011-01-06 11:00:19 -08002187 intent.addFlags(Intent.FLAG_RECEIVER_REPLACE_PENDING |
2188 Intent.FLAG_RECEIVER_REGISTERED_ONLY_BEFORE_BOOT);
Robert Greenwalt434203a2010-10-11 16:00:27 -07002189 intent.putExtra(Proxy.EXTRA_PROXY_INFO, proxy);
Robert Greenwalta2e13392010-12-06 11:29:17 -08002190 mContext.sendStickyBroadcast(intent);
Robert Greenwalt434203a2010-10-11 16:00:27 -07002191 }
2192
2193 private static class SettingsObserver extends ContentObserver {
2194 private int mWhat;
2195 private Handler mHandler;
2196 SettingsObserver(Handler handler, int what) {
2197 super(handler);
2198 mHandler = handler;
2199 mWhat = what;
2200 }
2201
2202 void observe(Context context) {
2203 ContentResolver resolver = context.getContentResolver();
2204 resolver.registerContentObserver(Settings.Secure.getUriFor(
2205 Settings.Secure.HTTP_PROXY), false, this);
2206 }
2207
2208 @Override
2209 public void onChange(boolean selfChange) {
2210 mHandler.obtainMessage(mWhat).sendToTarget();
2211 }
2212 }
Wink Savilleed9c02b2010-12-03 12:01:38 -08002213
2214 private void log(String s) {
2215 Slog.d(TAG, s);
2216 }
2217
2218 private void loge(String s) {
2219 Slog.e(TAG, s);
2220 }
Wink Saville2b8bcfe2011-02-24 17:58:51 -08002221 int convertFeatureToNetworkType(String feature){
2222 int networkType = -1;
2223 if (TextUtils.equals(feature, Phone.FEATURE_ENABLE_MMS)) {
2224 networkType = ConnectivityManager.TYPE_MOBILE_MMS;
2225 } else if (TextUtils.equals(feature, Phone.FEATURE_ENABLE_SUPL)) {
2226 networkType = ConnectivityManager.TYPE_MOBILE_SUPL;
2227 } else if (TextUtils.equals(feature, Phone.FEATURE_ENABLE_DUN) ||
2228 TextUtils.equals(feature, Phone.FEATURE_ENABLE_DUN_ALWAYS)) {
2229 networkType = ConnectivityManager.TYPE_MOBILE_DUN;
2230 } else if (TextUtils.equals(feature, Phone.FEATURE_ENABLE_HIPRI)) {
2231 networkType = ConnectivityManager.TYPE_MOBILE_HIPRI;
2232 } else if (TextUtils.equals(feature, Phone.FEATURE_ENABLE_FOTA)) {
2233 networkType = ConnectivityManager.TYPE_MOBILE_FOTA;
2234 } else if (TextUtils.equals(feature, Phone.FEATURE_ENABLE_IMS)) {
2235 networkType = ConnectivityManager.TYPE_MOBILE_IMS;
2236 } else if (TextUtils.equals(feature, Phone.FEATURE_ENABLE_CBS)) {
2237 networkType = ConnectivityManager.TYPE_MOBILE_CBS;
2238 }
2239 return networkType;
2240 }
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002241}