blob: 090909e0542b1edff9c3517727ca76ee0c3c5ebf [file] [log] [blame]
The Android Open Source Project28527d22009-03-03 19:31:44 -08001/*
2 * Copyright (C) 2008 The Android Open Source Project
3 *
4 * Licensed under the Apache License, Version 2.0 (the "License");
5 * you may not use this file except in compliance with the License.
6 * You may obtain a copy of the License at
7 *
8 * http://www.apache.org/licenses/LICENSE-2.0
9 *
10 * Unless required by applicable law or agreed to in writing, software
11 * distributed under the License is distributed on an "AS IS" BASIS,
12 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13 * See the License for the specific language governing permissions and
14 * limitations under the License.
15 */
16
17package com.android.server;
18
Jaikumar Ganesh0db51a02010-12-21 22:31:44 -080019import android.bluetooth.BluetoothTetheringDataTracker;
The Android Open Source Project28527d22009-03-03 19:31:44 -080020import android.content.ContentResolver;
21import android.content.Context;
22import android.content.Intent;
23import android.content.pm.PackageManager;
Robert Greenwaltc3c5f862010-10-11 16:00:27 -070024import android.database.ContentObserver;
The Android Open Source Project28527d22009-03-03 19:31:44 -080025import android.net.ConnectivityManager;
Robert Greenwalteb123ac2010-12-06 13:56:24 -080026import android.net.DummyDataStateTracker;
Benoit Goby211b5692010-12-22 14:29:40 -080027import android.net.EthernetDataTracker;
The Android Open Source Project28527d22009-03-03 19:31:44 -080028import android.net.IConnectivityManager;
Jaikumar Ganesh0db51a02010-12-21 22:31:44 -080029import android.net.LinkProperties;
The Android Open Source Project28527d22009-03-03 19:31:44 -080030import android.net.MobileDataStateTracker;
Robert Greenwalt34848c02011-03-25 13:09:25 -070031import android.net.NetworkConfig;
The Android Open Source Project28527d22009-03-03 19:31:44 -080032import android.net.NetworkInfo;
33import android.net.NetworkStateTracker;
Robert Greenwalt7fe44cb2010-08-27 09:24:29 -070034import android.net.NetworkUtils;
Robert Greenwaltc3c5f862010-10-11 16:00:27 -070035import android.net.Proxy;
36import android.net.ProxyProperties;
Robert Greenwalt5a901292011-04-28 14:28:50 -070037import android.net.RouteInfo;
Hung-ying Tyan4e723422011-01-19 16:48:38 +080038import android.net.vpn.VpnManager;
The Android Open Source Project28527d22009-03-03 19:31:44 -080039import android.net.wifi.WifiStateTracker;
40import android.os.Binder;
41import android.os.Handler;
Wink Saville775aad62010-09-02 19:23:52 -070042import android.os.HandlerThread;
Robert Greenwalt2034b912009-08-12 16:08:25 -070043import android.os.IBinder;
Chia-chi Yeh4df51322011-05-11 16:35:13 -070044import android.os.INetworkManagementService;
The Android Open Source Project28527d22009-03-03 19:31:44 -080045import android.os.Looper;
46import android.os.Message;
Robert Greenwalt93dc1042010-06-15 12:19:37 -070047import android.os.PowerManager;
Robert Greenwalt2034b912009-08-12 16:08:25 -070048import android.os.RemoteException;
The Android Open Source Project28527d22009-03-03 19:31:44 -080049import android.os.ServiceManager;
50import android.os.SystemProperties;
51import android.provider.Settings;
Robert Greenwalt2034b912009-08-12 16:08:25 -070052import android.text.TextUtils;
The Android Open Source Project28527d22009-03-03 19:31:44 -080053import android.util.EventLog;
Joe Onoratoc2386bb2010-02-26 18:56:32 -080054import android.util.Slog;
The Android Open Source Project28527d22009-03-03 19:31:44 -080055
Robert Greenwalt2034b912009-08-12 16:08:25 -070056import com.android.internal.telephony.Phone;
Robert Greenwalt0c4828c2010-01-26 11:40:34 -080057import com.android.server.connectivity.Tethering;
58
The Android Open Source Project28527d22009-03-03 19:31:44 -080059import java.io.FileDescriptor;
Irfan Sheriff7f132d92010-06-09 15:39:36 -070060import java.io.FileWriter;
61import java.io.IOException;
The Android Open Source Project28527d22009-03-03 19:31:44 -080062import java.io.PrintWriter;
Robert Greenwalta7dfbd32010-06-15 15:43:39 -070063import java.net.InetAddress;
64import java.net.UnknownHostException;
Robert Greenwalt2034b912009-08-12 16:08:25 -070065import java.util.ArrayList;
Robert Greenwalta7dfbd32010-06-15 15:43:39 -070066import java.util.Collection;
Robert Greenwaltd62c7002010-12-29 16:15:02 -080067import java.util.concurrent.atomic.AtomicBoolean;
Robert Greenwalt0e80be12010-09-20 14:35:25 -070068import java.util.GregorianCalendar;
Robert Greenwalt2034b912009-08-12 16:08:25 -070069import java.util.List;
The Android Open Source Project28527d22009-03-03 19:31:44 -080070
71/**
72 * @hide
73 */
74public class ConnectivityService extends IConnectivityManager.Stub {
75
Robert Greenwalt063dc7d2010-10-05 19:12:26 -070076 private static final boolean DBG = true;
The Android Open Source Project28527d22009-03-03 19:31:44 -080077 private static final String TAG = "ConnectivityService";
78
Robert Greenwalt2034b912009-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 Greenwaltbd492212011-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 Greenwalt0c4828c2010-01-26 11:40:34 -080089 private Tethering mTethering;
Robert Greenwaltf1b66e12010-02-25 12:29:30 -080090 private boolean mTetheringConfigValid = false;
Robert Greenwalt0c4828c2010-01-26 11:40:34 -080091
The Android Open Source Project28527d22009-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 Greenwalt2034b912009-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 Sheriff653e2a22010-06-07 09:03:04 -0700105 private WifiWatchdogService mWifiWatchdogService;
106
Robert Greenwalt2034b912009-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 Project28527d22009-03-03 19:31:44 -0800112 private Context mContext;
113 private int mNetworkPreference;
Robert Greenwalt2034b912009-08-12 16:08:25 -0700114 private int mActiveDefaultNetwork = -1;
Robert Greenwalt986c7412010-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 Project28527d22009-03-03 19:31:44 -0800120
121 private int mNumDnsEntries;
The Android Open Source Project28527d22009-03-03 19:31:44 -0800122
123 private boolean mTestMode;
Joe Onorato56023ad2010-09-01 21:18:22 -0700124 private static ConnectivityService sServiceInstance;
The Android Open Source Project28527d22009-03-03 19:31:44 -0800125
Robert Greenwaltd62c7002010-12-29 16:15:02 -0800126 private AtomicBoolean mBackgroundDataEnabled = new AtomicBoolean(true);
127
Robert Greenwalt6a2db8a2010-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 Greenwaltccb36f92010-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 Greenwaltc3c5f862010-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 Greenwalt34848c02011-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 Greenwalt2034b912009-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 Lockwoodfde2b762009-08-14 14:18:49 -0400213 private boolean mSystemReady;
Dianne Hackborna417ff82009-12-08 19:45:14 -0800214 private Intent mInitialBroadcast;
Mike Lockwoodfde2b762009-08-14 14:18:49 -0400215
Robert Greenwalt93dc1042010-06-15 12:19:37 -0700216 private PowerManager.WakeLock mNetTransitionWakeLock;
217 private String mNetTransitionWakeLockCausedBy = "";
218 private int mNetTransitionWakeLockSerialNumber;
219 private int mNetTransitionWakeLockTimeout;
220
Robert Greenwalt94daa182010-09-01 11:34:05 -0700221 private InetAddress mDefaultDns;
222
Robert Greenwalt0e80be12010-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 Greenwaltc3c5f862010-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 Greenwalt34848c02011-03-25 13:09:25 -0700235 NetworkConfig[] mNetConfigs;
Robert Greenwaltec05b3c2009-10-30 14:17:42 -0700236 int mNetworksDefined;
Robert Greenwalt2034b912009-08-12 16:08:25 -0700237
Robert Greenwalt12c44552009-12-07 11:33:18 -0800238 private static class RadioAttributes {
Robert Greenwalt2034b912009-08-12 16:08:25 -0700239 public int mSimultaneity;
240 public int mType;
241 public RadioAttributes(String init) {
242 String fragments[] = init.split(",");
Robert Greenwaltec05b3c2009-10-30 14:17:42 -0700243 mType = Integer.parseInt(fragments[0]);
244 mSimultaneity = Integer.parseInt(fragments[1]);
Robert Greenwalt2034b912009-08-12 16:08:25 -0700245 }
246 }
247 RadioAttributes[] mRadioAttributes;
248
Wink Saville775aad62010-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 Project28527d22009-03-03 19:31:44 -0800252 }
Wink Saville775aad62010-09-02 19:23:52 -0700253 return sServiceInstance;
The Android Open Source Project28527d22009-03-03 19:31:44 -0800254 }
Robert Greenwalt0659da32009-07-16 17:21:39 -0700255
The Android Open Source Project28527d22009-03-03 19:31:44 -0800256 private ConnectivityService(Context context) {
Wink Savillee70c6f52010-12-03 12:01:38 -0800257 if (DBG) log("ConnectivityService starting up");
Robert Greenwaltd48f8ee2010-01-14 17:47:58 -0800258
Wink Saville775aad62010-09-02 19:23:52 -0700259 HandlerThread handlerThread = new HandlerThread("ConnectivityServiceThread");
260 handlerThread.start();
261 mHandler = new MyHandler(handlerThread.getLooper());
262
Robert Greenwaltd62c7002010-12-29 16:15:02 -0800263 mBackgroundDataEnabled.set(Settings.Secure.getInt(context.getContentResolver(),
264 Settings.Secure.BACKGROUND_DATA, 1) == 1);
265
Robert Greenwaltd48f8ee2010-01-14 17:47:58 -0800266 // setup our unique device name
Robert Greenwalt82cde132010-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 Greenwaltd48f8ee2010-01-14 17:47:58 -0800274 }
275
Robert Greenwalt94daa182010-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 Greenwalt35e34d12011-02-22 16:00:42 -0800284 mDefaultDns = NetworkUtils.numericToInetAddress(dns);
285 } catch (IllegalArgumentException e) {
Wink Savillee70c6f52010-12-03 12:01:38 -0800286 loge("Error setting defaultDns using " + dns);
Robert Greenwalt94daa182010-09-01 11:34:05 -0700287 }
288
The Android Open Source Project28527d22009-03-03 19:31:44 -0800289 mContext = context;
Robert Greenwalt93dc1042010-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 Greenwalt2034b912009-08-12 16:08:25 -0700296 mNetTrackers = new NetworkStateTracker[
297 ConnectivityManager.MAX_NETWORK_TYPE+1];
Robert Greenwalt0659da32009-07-16 17:21:39 -0700298
The Android Open Source Project28527d22009-03-03 19:31:44 -0800299 mNetworkPreference = getPersistedNetworkPreference();
Robert Greenwalt0659da32009-07-16 17:21:39 -0700300
Robert Greenwaltec05b3c2009-10-30 14:17:42 -0700301 mRadioAttributes = new RadioAttributes[ConnectivityManager.MAX_RADIO_TYPE+1];
Robert Greenwalt34848c02011-03-25 13:09:25 -0700302 mNetConfigs = new NetworkConfig[ConnectivityManager.MAX_NETWORK_TYPE+1];
Robert Greenwaltec05b3c2009-10-30 14:17:42 -0700303
Robert Greenwalt2034b912009-08-12 16:08:25 -0700304 // Load device network attributes from resources
Robert Greenwalt2034b912009-08-12 16:08:25 -0700305 String[] raStrings = context.getResources().getStringArray(
306 com.android.internal.R.array.radioAttributes);
Robert Greenwaltec05b3c2009-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 Savillee70c6f52010-12-03 12:01:38 -0800310 loge("Error in radioAttributes - ignoring attempt to define type " + r.mType);
Robert Greenwaltec05b3c2009-10-30 14:17:42 -0700311 continue;
312 }
313 if (mRadioAttributes[r.mType] != null) {
Wink Savillee70c6f52010-12-03 12:01:38 -0800314 loge("Error in radioAttributes - ignoring attempt to redefine type " +
Robert Greenwaltec05b3c2009-10-30 14:17:42 -0700315 r.mType);
316 continue;
317 }
Robert Greenwalt2034b912009-08-12 16:08:25 -0700318 mRadioAttributes[r.mType] = r;
319 }
320
Robert Greenwaltec05b3c2009-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 Greenwalt34848c02011-03-25 13:09:25 -0700325 NetworkConfig n = new NetworkConfig(naString);
Wink Savillef2a62832011-04-07 14:23:45 -0700326 if (n.type > ConnectivityManager.MAX_NETWORK_TYPE) {
Wink Savillee70c6f52010-12-03 12:01:38 -0800327 loge("Error in networkAttributes - ignoring attempt to define type " +
Wink Savillef2a62832011-04-07 14:23:45 -0700328 n.type);
Robert Greenwaltec05b3c2009-10-30 14:17:42 -0700329 continue;
Robert Greenwalt2034b912009-08-12 16:08:25 -0700330 }
Wink Savillef2a62832011-04-07 14:23:45 -0700331 if (mNetConfigs[n.type] != null) {
Wink Savillee70c6f52010-12-03 12:01:38 -0800332 loge("Error in networkAttributes - ignoring attempt to redefine type " +
Wink Savillef2a62832011-04-07 14:23:45 -0700333 n.type);
Robert Greenwaltec05b3c2009-10-30 14:17:42 -0700334 continue;
335 }
Wink Savillef2a62832011-04-07 14:23:45 -0700336 if (mRadioAttributes[n.radio] == null) {
Wink Savillee70c6f52010-12-03 12:01:38 -0800337 loge("Error in networkAttributes - ignoring attempt to use undefined " +
Wink Savillef2a62832011-04-07 14:23:45 -0700338 "radio " + n.radio + " in network type " + n.type);
Robert Greenwaltec05b3c2009-10-30 14:17:42 -0700339 continue;
340 }
Wink Savillef2a62832011-04-07 14:23:45 -0700341 mNetConfigs[n.type] = n;
Robert Greenwaltec05b3c2009-10-30 14:17:42 -0700342 mNetworksDefined++;
343 } catch(Exception e) {
344 // ignore it - leave the entry null
Robert Greenwalt2034b912009-08-12 16:08:25 -0700345 }
346 }
347
Robert Greenwaltec05b3c2009-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 Greenwalt34848c02011-03-25 13:09:25 -0700355 for (NetworkConfig na : mNetConfigs) {
Robert Greenwaltec05b3c2009-10-30 14:17:42 -0700356 if (na == null) continue;
Wink Savillef2a62832011-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 Greenwaltec05b3c2009-10-30 14:17:42 -0700361 }
362 continue;
363 }
Wink Savillef2a62832011-04-07 14:23:45 -0700364 mPriorityList[insertionPoint--] = na.type;
Robert Greenwaltec05b3c2009-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 Greenwalt2034b912009-08-12 16:08:25 -0700373 mNetRequestersPids[i] = new ArrayList();
374 }
375
376 mFeatureUsers = new ArrayList();
377
Robert Greenwaltec05b3c2009-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 Project28527d22009-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 Greenwaltec05b3c2009-10-30 14:17:42 -0700389 for (int netType : mPriorityList) {
Wink Savillef2a62832011-04-07 14:23:45 -0700390 switch (mNetConfigs[netType].radio) {
Robert Greenwaltec05b3c2009-10-30 14:17:42 -0700391 case ConnectivityManager.TYPE_WIFI:
Wink Savillee70c6f52010-12-03 12:01:38 -0800392 if (DBG) log("Starting Wifi Service.");
Wink Saville7fabfa22010-08-13 16:11:42 -0700393 WifiStateTracker wst = new WifiStateTracker();
Irfan Sheriff25be0762010-07-28 09:35:20 -0700394 WifiService wifiService = new WifiService(context);
Robert Greenwaltec05b3c2009-10-30 14:17:42 -0700395 ServiceManager.addService(Context.WIFI_SERVICE, wifiService);
Irfan Sheriff25be0762010-07-28 09:35:20 -0700396 wifiService.checkAndStartWifi();
Robert Greenwaltec05b3c2009-10-30 14:17:42 -0700397 mNetTrackers[ConnectivityManager.TYPE_WIFI] = wst;
Wink Saville7fabfa22010-08-13 16:11:42 -0700398 wst.startMonitoring(context, mHandler);
The Android Open Source Project28527d22009-03-03 19:31:44 -0800399
Irfan Sheriff653e2a22010-06-07 09:03:04 -0700400 //TODO: as part of WWS refactor, create only when needed
Irfan Sheriff25be0762010-07-28 09:35:20 -0700401 mWifiWatchdogService = new WifiWatchdogService(context);
Irfan Sheriff653e2a22010-06-07 09:03:04 -0700402
Robert Greenwaltec05b3c2009-10-30 14:17:42 -0700403 break;
404 case ConnectivityManager.TYPE_MOBILE:
Wink Saville7fabfa22010-08-13 16:11:42 -0700405 mNetTrackers[netType] = new MobileDataStateTracker(netType,
Wink Savillef2a62832011-04-07 14:23:45 -0700406 mNetConfigs[netType].name);
Wink Saville7fabfa22010-08-13 16:11:42 -0700407 mNetTrackers[netType].startMonitoring(context, mHandler);
Robert Greenwaltec05b3c2009-10-30 14:17:42 -0700408 break;
Robert Greenwalteb123ac2010-12-06 13:56:24 -0800409 case ConnectivityManager.TYPE_DUMMY:
410 mNetTrackers[netType] = new DummyDataStateTracker(netType,
Wink Savillef2a62832011-04-07 14:23:45 -0700411 mNetConfigs[netType].name);
Robert Greenwalteb123ac2010-12-06 13:56:24 -0800412 mNetTrackers[netType].startMonitoring(context, mHandler);
413 break;
Jaikumar Ganesh0db51a02010-12-21 22:31:44 -0800414 case ConnectivityManager.TYPE_BLUETOOTH:
415 mNetTrackers[netType] = BluetoothTetheringDataTracker.getInstance();
416 mNetTrackers[netType].startMonitoring(context, mHandler);
417 break;
Benoit Goby211b5692010-12-22 14:29:40 -0800418 case ConnectivityManager.TYPE_ETHERNET:
419 mNetTrackers[netType] = EthernetDataTracker.getInstance();
420 mNetTrackers[netType].startMonitoring(context, mHandler);
421 break;
Robert Greenwaltec05b3c2009-10-30 14:17:42 -0700422 default:
Wink Savillee70c6f52010-12-03 12:01:38 -0800423 loge("Trying to create a DataStateTracker for an unknown radio type " +
Wink Savillef2a62832011-04-07 14:23:45 -0700424 mNetConfigs[netType].radio);
Robert Greenwaltec05b3c2009-10-30 14:17:42 -0700425 continue;
426 }
427 }
Robert Greenwalt0c4828c2010-01-26 11:40:34 -0800428
Chia-chi Yeh4df51322011-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 Greenwaltf1b66e12010-02-25 12:29:30 -0800433 mTetheringConfigValid = (((mNetTrackers[ConnectivityManager.TYPE_MOBILE_DUN] != null) ||
434 !mTethering.isDunRequired()) &&
435 (mTethering.getTetherableUsbRegexs().length != 0 ||
Danica Chang96567052010-08-11 14:54:43 -0700436 mTethering.getTetherableWifiRegexs().length != 0 ||
437 mTethering.getTetherableBluetoothRegexs().length != 0) &&
Robert Greenwaltf1b66e12010-02-25 12:29:30 -0800438 mTethering.getUpstreamIfaceRegexs().length != 0);
439
Robert Greenwalt0e80be12010-09-20 14:35:25 -0700440 if (DBG) {
441 mInetLog = new ArrayList();
442 }
Robert Greenwaltc3c5f862010-10-11 16:00:27 -0700443
444 mSettingsObserver = new SettingsObserver(mHandler, EVENT_APPLY_GLOBAL_HTTP_PROXY);
445 mSettingsObserver.observe(mContext);
Robert Greenwalt6f7c6092010-12-02 11:31:00 -0800446
447 loadGlobalProxy();
Hung-ying Tyan4e723422011-01-19 16:48:38 +0800448
449 VpnManager.startVpnService(context);
The Android Open Source Project28527d22009-03-03 19:31:44 -0800450 }
451
Robert Greenwaltec05b3c2009-10-30 14:17:42 -0700452
The Android Open Source Project28527d22009-03-03 19:31:44 -0800453 /**
Robert Greenwalt0659da32009-07-16 17:21:39 -0700454 * Sets the preferred network.
The Android Open Source Project28527d22009-03-03 19:31:44 -0800455 * @param preference the new preference
456 */
Robert Greenwalt6a2db8a2010-09-23 10:05:56 -0700457 public void setNetworkPreference(int preference) {
The Android Open Source Project28527d22009-03-03 19:31:44 -0800458 enforceChangePermission();
Robert Greenwalt6a2db8a2010-09-23 10:05:56 -0700459
460 mHandler.sendMessage(mHandler.obtainMessage(EVENT_SET_NETWORK_PREFERENCE, preference, 0));
The Android Open Source Project28527d22009-03-03 19:31:44 -0800461 }
462
463 public int getNetworkPreference() {
464 enforceAccessPermission();
Robert Greenwalt6a2db8a2010-09-23 10:05:56 -0700465 int preference;
466 synchronized(this) {
467 preference = mNetworkPreference;
468 }
469 return preference;
The Android Open Source Project28527d22009-03-03 19:31:44 -0800470 }
471
Robert Greenwalt6a2db8a2010-09-23 10:05:56 -0700472 private void handleSetNetworkPreference(int preference) {
473 if (ConnectivityManager.isNetworkTypeValid(preference) &&
Robert Greenwalt34848c02011-03-25 13:09:25 -0700474 mNetConfigs[preference] != null &&
475 mNetConfigs[preference].isDefault()) {
Robert Greenwalt6a2db8a2010-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 Project28527d22009-03-03 19:31:44 -0800485 }
Robert Greenwalt0659da32009-07-16 17:21:39 -0700486
The Android Open Source Project28527d22009-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 Greenwalt0659da32009-07-16 17:21:39 -0700498
The Android Open Source Project28527d22009-03-03 19:31:44 -0800499 /**
Robert Greenwalt0659da32009-07-16 17:21:39 -0700500 * Make the state of network connectivity conform to the preference settings
The Android Open Source Project28527d22009-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 Greenwalt2034b912009-08-12 16:08:25 -0700507 if (mNetTrackers[mNetworkPreference].getNetworkInfo().isConnected())
The Android Open Source Project28527d22009-03-03 19:31:44 -0800508 return;
509
Robert Greenwalt2034b912009-08-12 16:08:25 -0700510 if (!mNetTrackers[mNetworkPreference].isAvailable())
511 return;
The Android Open Source Project28527d22009-03-03 19:31:44 -0800512
Robert Greenwalt2034b912009-08-12 16:08:25 -0700513 for (int t=0; t <= ConnectivityManager.MAX_RADIO_TYPE; t++) {
Robert Greenwaltec05b3c2009-10-30 14:17:42 -0700514 if (t != mNetworkPreference && mNetTrackers[t] != null &&
Robert Greenwalt2034b912009-08-12 16:08:25 -0700515 mNetTrackers[t].getNetworkInfo().isConnected()) {
Robert Greenwaltf3f045b2009-08-20 15:25:14 -0700516 if (DBG) {
Wink Savillee70c6f52010-12-03 12:01:38 -0800517 log("tearing down " + mNetTrackers[t].getNetworkInfo() +
Robert Greenwaltf3f045b2009-08-20 15:25:14 -0700518 " in enforcePreference");
519 }
Robert Greenwalt2034b912009-08-12 16:08:25 -0700520 teardown(mNetTrackers[t]);
The Android Open Source Project28527d22009-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 Greenwalt0659da32009-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 Project28527d22009-03-03 19:31:44 -0800540 */
541 public NetworkInfo getActiveNetworkInfo() {
542 enforceAccessPermission();
Robert Greenwalt1e2f2862011-04-01 10:51:22 -0700543 if (mActiveDefaultNetwork != -1) {
544 return mNetTrackers[mActiveDefaultNetwork].getNetworkInfo();
The Android Open Source Project28527d22009-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 Greenwaltec05b3c2009-10-30 14:17:42 -0700561 NetworkInfo[] result = new NetworkInfo[mNetworksDefined];
The Android Open Source Project28527d22009-03-03 19:31:44 -0800562 int i = 0;
563 for (NetworkStateTracker t : mNetTrackers) {
Robert Greenwaltec05b3c2009-10-30 14:17:42 -0700564 if(t != null) result[i++] = t.getNetworkInfo();
The Android Open Source Project28527d22009-03-03 19:31:44 -0800565 }
566 return result;
567 }
568
Robert Greenwalt9f0ee4f2010-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 Greenwalt34848c02011-03-25 13:09:25 -0700580 if (mNetConfigs[type] == null || !mNetConfigs[type].isDefault()) {
Robert Greenwalt9f0ee4f2010-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 Project28527d22009-03-03 19:31:44 -0800601 public boolean setRadios(boolean turnOn) {
602 boolean result = true;
603 enforceChangePermission();
604 for (NetworkStateTracker t : mNetTrackers) {
Robert Greenwaltec05b3c2009-10-30 14:17:42 -0700605 if (t != null) result = t.setRadio(turnOn) && result;
The Android Open Source Project28527d22009-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 Greenwaltaffc3a12009-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 Greenwalt2034b912009-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 Greenwalt3eeb6032009-12-21 18:24:07 -0800632 long mCreateTime;
Robert Greenwalt2034b912009-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 Greenwalt3eeb6032009-12-21 18:24:07 -0800641 mCreateTime = System.currentTimeMillis();
Robert Greenwaltaffc3a12009-09-27 17:27:04 -0700642
Robert Greenwalt2034b912009-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 Savillee70c6f52010-12-03 12:01:38 -0800655 log("ConnectivityService FeatureUser binderDied(" +
Robert Greenwalt3eeb6032009-12-21 18:24:07 -0800656 mNetworkType + ", " + mFeature + ", " + mBinder + "), created " +
657 (System.currentTimeMillis() - mCreateTime) + " mSec ago");
Robert Greenwaltaffc3a12009-09-27 17:27:04 -0700658 stopUsingNetworkFeature(this, false);
Robert Greenwalt2034b912009-08-12 16:08:25 -0700659 }
660
Robert Greenwaltaffc3a12009-09-27 17:27:04 -0700661 public void expire() {
Wink Savillee70c6f52010-12-03 12:01:38 -0800662 log("ConnectivityService FeatureUser expire(" +
Robert Greenwalt3eeb6032009-12-21 18:24:07 -0800663 mNetworkType + ", " + mFeature + ", " + mBinder +"), created " +
664 (System.currentTimeMillis() - mCreateTime) + " mSec ago");
Robert Greenwaltaffc3a12009-09-27 17:27:04 -0700665 stopUsingNetworkFeature(this, false);
666 }
Robert Greenwalt3eeb6032009-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 Greenwalt2034b912009-08-12 16:08:25 -0700672 }
673
Robert Greenwaltaffc3a12009-09-27 17:27:04 -0700674 // javadoc from interface
Robert Greenwalt2034b912009-08-12 16:08:25 -0700675 public int startUsingNetworkFeature(int networkType, String feature,
676 IBinder binder) {
677 if (DBG) {
Wink Savillee70c6f52010-12-03 12:01:38 -0800678 log("startUsingNetworkFeature for net " + networkType + ": " + feature);
Robert Greenwalt2034b912009-08-12 16:08:25 -0700679 }
The Android Open Source Project28527d22009-03-03 19:31:44 -0800680 enforceChangePermission();
Robert Greenwaltec05b3c2009-10-30 14:17:42 -0700681 if (!ConnectivityManager.isNetworkTypeValid(networkType) ||
Robert Greenwalt34848c02011-03-25 13:09:25 -0700682 mNetConfigs[networkType] == null) {
Robert Greenwalt2034b912009-08-12 16:08:25 -0700683 return Phone.APN_REQUEST_FAILED;
The Android Open Source Project28527d22009-03-03 19:31:44 -0800684 }
Robert Greenwalt2034b912009-08-12 16:08:25 -0700685
Robert Greenwaltaffc3a12009-09-27 17:27:04 -0700686 FeatureUser f = new FeatureUser(networkType, feature, binder);
Robert Greenwalt2034b912009-08-12 16:08:25 -0700687
688 // TODO - move this into the MobileDataStateTracker
689 int usedNetworkType = networkType;
690 if(networkType == ConnectivityManager.TYPE_MOBILE) {
Wink Savillef6b76692011-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 Greenwalt2034b912009-08-12 16:08:25 -0700695 }
696 }
697 NetworkStateTracker network = mNetTrackers[usedNetworkType];
698 if (network != null) {
Robert Greenwalt5364d752010-12-15 13:26:33 -0800699 Integer currentPid = new Integer(getCallingPid());
Robert Greenwalt2034b912009-08-12 16:08:25 -0700700 if (usedNetworkType != networkType) {
Robert Greenwalt2034b912009-08-12 16:08:25 -0700701 NetworkStateTracker radio = mNetTrackers[networkType];
702 NetworkInfo ni = network.getNetworkInfo();
703
704 if (ni.isAvailable() == false) {
Wink Savillee70c6f52010-12-03 12:01:38 -0800705 if (DBG) log("special network not available");
Robert Greenwalt2cc87442010-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 Greenwalt2034b912009-08-12 16:08:25 -0700711 }
712
Robert Greenwaltaffc3a12009-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 Greenwalt2034b912009-08-12 16:08:25 -0700719 }
Robert Greenwaltaffc3a12009-09-27 17:27:04 -0700720
Robert Greenwalt20f819c2011-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 Greenwalt2034b912009-08-12 16:08:25 -0700727
Robert Greenwalta52c75a2009-08-19 20:19:33 -0700728 if ((ni.isConnectedOrConnecting() == true) &&
729 !network.isTeardownRequested()) {
Robert Greenwalt2034b912009-08-12 16:08:25 -0700730 if (ni.isConnected() == true) {
731 // add the pid-specific dns
Robert Greenwalt3afbead2010-07-23 15:46:26 -0700732 handleDnsConfigurationChange(networkType);
Wink Savillee70c6f52010-12-03 12:01:38 -0800733 if (DBG) log("special network already active");
Robert Greenwalt2034b912009-08-12 16:08:25 -0700734 return Phone.APN_ALREADY_ACTIVE;
735 }
Wink Savillee70c6f52010-12-03 12:01:38 -0800736 if (DBG) log("special network already connecting");
Robert Greenwalt2034b912009-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 Savillee70c6f52010-12-03 12:01:38 -0800743 if (DBG) log("reconnecting to special network");
Robert Greenwalt2034b912009-08-12 16:08:25 -0700744 network.reconnect();
745 return Phone.APN_REQUEST_STARTED;
746 } else {
Robert Greenwalt5364d752010-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 Greenwaltd391e892010-05-18 10:52:51 -0700755 return -1;
Robert Greenwalt2034b912009-08-12 16:08:25 -0700756 }
757 }
758 return Phone.APN_TYPE_NOT_AVAILABLE;
The Android Open Source Project28527d22009-03-03 19:31:44 -0800759 }
760
Robert Greenwaltaffc3a12009-09-27 17:27:04 -0700761 // javadoc from interface
The Android Open Source Project28527d22009-03-03 19:31:44 -0800762 public int stopUsingNetworkFeature(int networkType, String feature) {
Robert Greenwalt28f43012009-10-06 17:52:40 -0700763 enforceChangePermission();
764
Robert Greenwaltaffc3a12009-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 Savillee70c6f52010-12-03 12:01:38 -0800787 if (DBG) log("ignoring stopUsingNetworkFeature - not a live request");
Robert Greenwaltaffc3a12009-09-27 17:27:04 -0700788 return 1;
789 }
Robert Greenwalt2034b912009-08-12 16:08:25 -0700790 }
791
Robert Greenwaltaffc3a12009-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 Greenwalt2034b912009-08-12 16:08:25 -0700801 if (DBG) {
Wink Savillee70c6f52010-12-03 12:01:38 -0800802 log("stopUsingNetworkFeature for net " + networkType +
Robert Greenwalt2034b912009-08-12 16:08:25 -0700803 ": " + feature);
804 }
Robert Greenwalt28f43012009-10-06 17:52:40 -0700805
The Android Open Source Project28527d22009-03-03 19:31:44 -0800806 if (!ConnectivityManager.isNetworkTypeValid(networkType)) {
807 return -1;
808 }
Robert Greenwalt2034b912009-08-12 16:08:25 -0700809
Robert Greenwaltaffc3a12009-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 Savillee70c6f52010-12-03 12:01:38 -0800815 if (DBG) log("ignoring - this process has no outstanding requests");
Robert Greenwalt2034b912009-08-12 16:08:25 -0700816 return 1;
817 }
Robert Greenwaltaffc3a12009-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 Savillee70c6f52010-12-03 12:01:38 -0800833 if (DBG) log("ignoring stopUsingNetworkFeature as dup is found");
Robert Greenwaltaffc3a12009-09-27 17:27:04 -0700834 return 1;
835 }
836 }
Robert Greenwalt2034b912009-08-12 16:08:25 -0700837 }
Robert Greenwaltaffc3a12009-09-27 17:27:04 -0700838
839 // TODO - move to MobileDataStateTracker
840 int usedNetworkType = networkType;
841 if (networkType == ConnectivityManager.TYPE_MOBILE) {
Wink Savillef6b76692011-02-24 17:58:51 -0800842 usedNetworkType = convertFeatureToNetworkType(feature);
843 if (usedNetworkType < 0) {
844 usedNetworkType = networkType;
Robert Greenwaltaffc3a12009-09-27 17:27:04 -0700845 }
846 }
847 tracker = mNetTrackers[usedNetworkType];
Robert Greenwaltec05b3c2009-10-30 14:17:42 -0700848 if (tracker == null) {
Wink Savillee70c6f52010-12-03 12:01:38 -0800849 if (DBG) log("ignoring - no known tracker for net type " + usedNetworkType);
Robert Greenwaltec05b3c2009-10-30 14:17:42 -0700850 return -1;
851 }
852 if (usedNetworkType != networkType) {
Robert Greenwaltaffc3a12009-09-27 17:27:04 -0700853 Integer currentPid = new Integer(pid);
Robert Greenwaltaffc3a12009-09-27 17:27:04 -0700854 mNetRequestersPids[usedNetworkType].remove(currentPid);
Robert Greenwalt0ca68a02009-12-17 14:54:59 -0800855 reassessPidDns(pid, true);
Robert Greenwaltaffc3a12009-09-27 17:27:04 -0700856 if (mNetRequestersPids[usedNetworkType].size() != 0) {
Wink Savillee70c6f52010-12-03 12:01:38 -0800857 if (DBG) log("not tearing down special network - " +
Robert Greenwaltaffc3a12009-09-27 17:27:04 -0700858 "others still using it");
859 return 1;
860 }
861 callTeardown = true;
Robert Greenwalt9f3be4c2011-01-10 11:58:31 -0800862 } else {
863 if (DBG) log("not a known feature - dropping");
Robert Greenwaltaffc3a12009-09-27 17:27:04 -0700864 }
865 }
Wink Savillee70c6f52010-12-03 12:01:38 -0800866 if (DBG) log("Doing network teardown");
Robert Greenwaltaffc3a12009-09-27 17:27:04 -0700867 if (callTeardown) {
868 tracker.teardown();
Robert Greenwalt2034b912009-08-12 16:08:25 -0700869 return 1;
870 } else {
Robert Greenwaltd391e892010-05-18 10:52:51 -0700871 return -1;
Robert Greenwalt2034b912009-08-12 16:08:25 -0700872 }
The Android Open Source Project28527d22009-03-03 19:31:44 -0800873 }
874
875 /**
Robert Greenwalt7fe44cb2010-08-27 09:24:29 -0700876 * @deprecated use requestRouteToHostAddress instead
877 *
The Android Open Source Project28527d22009-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 Greenwalt0659da32009-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 Project28527d22009-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 Greenwalt7fe44cb2010-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 Project28527d22009-03-03 19:31:44 -0800906 enforceChangePermission();
907 if (!ConnectivityManager.isNetworkTypeValid(networkType)) {
908 return false;
909 }
910 NetworkStateTracker tracker = mNetTrackers[networkType];
Robert Greenwalt4666ed02009-09-10 15:06:20 -0700911
Robert Greenwaltec05b3c2009-10-30 14:17:42 -0700912 if (tracker == null || !tracker.getNetworkInfo().isConnected() ||
913 tracker.isTeardownRequested()) {
Robert Greenwalt4666ed02009-09-10 15:06:20 -0700914 if (DBG) {
Wink Savillee70c6f52010-12-03 12:01:38 -0800915 log("requestRouteToHostAddress on down network " +
Robert Greenwalt7fe44cb2010-08-27 09:24:29 -0700916 "(" + networkType + ") - dropped");
Robert Greenwalt4666ed02009-09-10 15:06:20 -0700917 }
918 return false;
The Android Open Source Project28527d22009-03-03 19:31:44 -0800919 }
Robert Greenwalta7dfbd32010-06-15 15:43:39 -0700920 try {
Robert Greenwalt7fe44cb2010-08-27 09:24:29 -0700921 InetAddress addr = InetAddress.getByAddress(hostAddress);
Robert Greenwaltbd492212011-05-06 17:10:53 -0700922 return addHostRoute(tracker, addr, 0);
Robert Greenwalta7dfbd32010-06-15 15:43:39 -0700923 } catch (UnknownHostException e) {}
924 return false;
Irfan Sheriff7f132d92010-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 Greenwalta7dfbd32010-06-15 15:43:39 -0700932 * TODO - deprecate
Irfan Sheriff7f132d92010-06-09 15:39:36 -0700933 * @return {@code true} on success, {@code false} on failure
934 */
Robert Greenwaltbd492212011-05-06 17:10:53 -0700935 private boolean addHostRoute(NetworkStateTracker nt, InetAddress hostAddress, int cycleCount) {
Irfan Sheriff7f132d92010-06-09 15:39:36 -0700936 if (nt.getNetworkInfo().getType() == ConnectivityManager.TYPE_WIFI) {
937 return false;
938 }
939
Robert Greenwaltbd492212011-05-06 17:10:53 -0700940 LinkProperties lp = nt.getLinkProperties();
941 if ((lp == null) || (hostAddress == null)) return false;
Irfan Sheriff7f132d92010-06-09 15:39:36 -0700942
Robert Greenwaltbd492212011-05-06 17:10:53 -0700943 String interfaceName = lp.getInterfaceName();
Irfan Sheriff7f132d92010-06-09 15:39:36 -0700944 if (DBG) {
Robert Greenwaltbd492212011-05-06 17:10:53 -0700945 log("Requested host route to " + hostAddress + "(" + interfaceName + "), cycleCount=" +
946 cycleCount);
Irfan Sheriff7f132d92010-06-09 15:39:36 -0700947 }
Robert Greenwaltbd492212011-05-06 17:10:53 -0700948 if (interfaceName == null) {
Wink Savillee70c6f52010-12-03 12:01:38 -0800949 if (DBG) loge("addHostRoute failed due to null interface name");
Irfan Sheriff7f132d92010-06-09 15:39:36 -0700950 return false;
951 }
Robert Greenwaltbd492212011-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 Project28527d22009-03-03 19:31:44 -0800974 }
975
976 /**
977 * @see ConnectivityManager#getBackgroundDataSetting()
978 */
979 public boolean getBackgroundDataSetting() {
Robert Greenwaltd62c7002010-12-29 16:15:02 -0800980 return mBackgroundDataEnabled.get();
The Android Open Source Project28527d22009-03-03 19:31:44 -0800981 }
Robert Greenwalt0659da32009-07-16 17:21:39 -0700982
The Android Open Source Project28527d22009-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 Greenwalt0659da32009-07-16 17:21:39 -0700990
Robert Greenwaltd62c7002010-12-29 16:15:02 -0800991 mBackgroundDataEnabled.set(allowBackgroundDataUsage);
992
Robert Greenwalt6a2db8a2010-09-23 10:05:56 -0700993 mHandler.sendMessage(mHandler.obtainMessage(EVENT_SET_BACKGROUND_DATA,
994 (allowBackgroundDataUsage ? ENABLED : DISABLED), 0));
995 }
The Android Open Source Project28527d22009-03-03 19:31:44 -0800996
Robert Greenwalt6a2db8a2010-09-23 10:05:56 -0700997 private void handleSetBackgroundData(boolean enabled) {
Robert Greenwalt0ffdef12011-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 Greenwalt0659da32009-07-16 17:21:39 -07001003 }
1004
Robert Greenwalt1b21f6c2010-02-23 18:58:05 -08001005 /**
1006 * @see ConnectivityManager#getMobileDataEnabled()
1007 */
1008 public boolean getMobileDataEnabled() {
Wink Savilleb9024c62010-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 Greenwalt1b21f6c2010-02-23 18:58:05 -08001012 enforceAccessPermission();
1013 boolean retVal = Settings.Secure.getInt(mContext.getContentResolver(),
1014 Settings.Secure.MOBILE_DATA, 1) == 1;
Wink Savillee70c6f52010-12-03 12:01:38 -08001015 if (DBG) log("getMobileDataEnabled returning " + retVal);
Robert Greenwalt1b21f6c2010-02-23 18:58:05 -08001016 return retVal;
1017 }
1018
Robert Greenwalt34848c02011-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 Greenwalt1b21f6c2010-02-23 18:58:05 -08001037 /**
1038 * @see ConnectivityManager#setMobileDataEnabled(boolean)
1039 */
Robert Greenwalt6a2db8a2010-09-23 10:05:56 -07001040 public void setMobileDataEnabled(boolean enabled) {
Robert Greenwalt1b21f6c2010-02-23 18:58:05 -08001041 enforceChangePermission();
Wink Savillee70c6f52010-12-03 12:01:38 -08001042 if (DBG) log("setMobileDataEnabled(" + enabled + ")");
Robert Greenwalt1b21f6c2010-02-23 18:58:05 -08001043
Robert Greenwalt6a2db8a2010-09-23 10:05:56 -07001044 mHandler.sendMessage(mHandler.obtainMessage(EVENT_SET_MOBILE_DATA,
Robert Greenwalt34848c02011-03-25 13:09:25 -07001045 (enabled ? ENABLED : DISABLED), 0));
Robert Greenwalt6a2db8a2010-09-23 10:05:56 -07001046 }
1047
1048 private void handleSetMobileData(boolean enabled) {
Wink Savilleb9024c62010-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 Greenwalt1b21f6c2010-02-23 18:58:05 -08001052 }
Wink Savilleb9024c62010-12-07 10:31:02 -08001053 mNetTrackers[ConnectivityManager.TYPE_MOBILE].setDataEnable(enabled);
Robert Greenwalt1b21f6c2010-02-23 18:58:05 -08001054 }
1055 }
1056
The Android Open Source Project28527d22009-03-03 19:31:44 -08001057 private void enforceAccessPermission() {
Robert Greenwalt0659da32009-07-16 17:21:39 -07001058 mContext.enforceCallingOrSelfPermission(
1059 android.Manifest.permission.ACCESS_NETWORK_STATE,
1060 "ConnectivityService");
The Android Open Source Project28527d22009-03-03 19:31:44 -08001061 }
1062
1063 private void enforceChangePermission() {
Robert Greenwalt0659da32009-07-16 17:21:39 -07001064 mContext.enforceCallingOrSelfPermission(
1065 android.Manifest.permission.CHANGE_NETWORK_STATE,
1066 "ConnectivityService");
The Android Open Source Project28527d22009-03-03 19:31:44 -08001067 }
1068
Robert Greenwalt0c4828c2010-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 Greenwalt8e87f122010-02-11 18:18:40 -08001076 private void enforceTetherAccessPermission() {
1077 mContext.enforceCallingOrSelfPermission(
1078 android.Manifest.permission.ACCESS_NETWORK_STATE,
1079 "ConnectivityService");
1080 }
1081
Robert Greenwalt93dc1042010-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 Project28527d22009-03-03 19:31:44 -08001088 /**
Robert Greenwalt0659da32009-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 Project28527d22009-03-03 19:31:44 -08001093 * @param info the {@code NetworkInfo} for the network
1094 */
1095 private void handleDisconnect(NetworkInfo info) {
1096
Robert Greenwalt2034b912009-08-12 16:08:25 -07001097 int prevNetType = info.getType();
The Android Open Source Project28527d22009-03-03 19:31:44 -08001098
Robert Greenwalt2034b912009-08-12 16:08:25 -07001099 mNetTrackers[prevNetType].setTeardownRequested(false);
The Android Open Source Project28527d22009-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 Greenwalt34848c02011-03-25 13:09:25 -07001106 if (!mNetConfigs[prevNetType].isDefault()) {
Robert Greenwalt2034b912009-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 Project28527d22009-03-03 19:31:44 -08001114 }
The Android Open Source Project28527d22009-03-03 19:31:44 -08001115 }
1116
The Android Open Source Project28527d22009-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 Greenwalt0659da32009-07-16 17:21:39 -07001127 intent.putExtra(ConnectivityManager.EXTRA_EXTRA_INFO,
1128 info.getExtraInfo());
The Android Open Source Project28527d22009-03-03 19:31:44 -08001129 }
Robert Greenwalt2034b912009-08-12 16:08:25 -07001130
Robert Greenwalt34848c02011-03-25 13:09:25 -07001131 if (mNetConfigs[prevNetType].isDefault()) {
Robert Greenwalt4b7c55e2011-01-11 13:56:33 -08001132 tryFailover(prevNetType);
1133 if (mActiveDefaultNetwork != -1) {
1134 NetworkInfo switchTo = mNetTrackers[mActiveDefaultNetwork].getNetworkInfo();
Robert Greenwalt3cc68d32010-01-25 17:54:29 -08001135 intent.putExtra(ConnectivityManager.EXTRA_OTHER_NETWORK_INFO, switchTo);
1136 } else {
Robert Greenwaltb2a9e492010-09-20 18:01:43 -07001137 mDefaultInetConditionPublished = 0; // we're not connected anymore
Robert Greenwalt3cc68d32010-01-25 17:54:29 -08001138 intent.putExtra(ConnectivityManager.EXTRA_NO_CONNECTIVITY, true);
1139 }
Robert Greenwaltf55ced92010-01-20 19:29:41 -08001140 }
Robert Greenwaltb2a9e492010-09-20 18:01:43 -07001141 intent.putExtra(ConnectivityManager.EXTRA_INET_CONDITION, mDefaultInetConditionPublished);
Robert Greenwaltf55ced92010-01-20 19:29:41 -08001142 // do this before we broadcast the change
Robert Greenwalt3afbead2010-07-23 15:46:26 -07001143 handleConnectivityChange(prevNetType);
Robert Greenwaltf55ced92010-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 Greenwalt4b7c55e2011-01-11 13:56:33 -08001150 if (mActiveDefaultNetwork != -1) {
1151 sendConnectedBroadcast(mNetTrackers[mActiveDefaultNetwork].getNetworkInfo());
Robert Greenwaltf55ced92010-01-20 19:29:41 -08001152 }
1153 }
1154
Robert Greenwalt4b7c55e2011-01-11 13:56:33 -08001155 private void tryFailover(int prevNetType) {
Robert Greenwalt2034b912009-08-12 16:08:25 -07001156 /*
Robert Greenwalt92564852011-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 Greenwalt2034b912009-08-12 16:08:25 -07001160 */
Robert Greenwalt34848c02011-03-25 13:09:25 -07001161 if (mNetConfigs[prevNetType].isDefault()) {
Robert Greenwalt2034b912009-08-12 16:08:25 -07001162 if (mActiveDefaultNetwork == prevNetType) {
1163 mActiveDefaultNetwork = -1;
1164 }
1165
Robert Greenwalt4b7c55e2011-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 Greenwalt34848c02011-03-25 13:09:25 -07001171// currentPriority = mNetConfigs[mActiveDefaultNetwork].mPriority;
Robert Greenwalt4b7c55e2011-01-11 13:56:33 -08001172// }
Robert Greenwaltf55ced92010-01-20 19:29:41 -08001173 for (int checkType=0; checkType <= ConnectivityManager.MAX_NETWORK_TYPE; checkType++) {
Robert Greenwaltec05b3c2009-10-30 14:17:42 -07001174 if (checkType == prevNetType) continue;
Robert Greenwalt34848c02011-03-25 13:09:25 -07001175 if (mNetConfigs[checkType] == null) continue;
1176 if (!mNetConfigs[checkType].isDefault()) continue;
Wink Saville72a95b92011-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 Greenwalt34848c02011-03-25 13:09:25 -07001188// if (currentPriority >= mNetConfigs[checkType].mPriority) continue;
Robert Greenwalt2034b912009-08-12 16:08:25 -07001189
Robert Greenwalt4b7c55e2011-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 Greenwalt2034b912009-08-12 16:08:25 -07001195 }
Robert Greenwalt4b7c55e2011-01-11 13:56:33 -08001196 if (DBG) log("Attempting to switch to " + checkInfo.getTypeName());
Robert Greenwalt2034b912009-08-12 16:08:25 -07001197 }
The Android Open Source Project28527d22009-03-03 19:31:44 -08001198 }
The Android Open Source Project28527d22009-03-03 19:31:44 -08001199 }
1200
1201 private void sendConnectedBroadcast(NetworkInfo info) {
Robert Greenwaltd3401f92010-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 Project28527d22009-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 Greenwalt0659da32009-07-16 17:21:39 -07001220 intent.putExtra(ConnectivityManager.EXTRA_EXTRA_INFO,
1221 info.getExtraInfo());
The Android Open Source Project28527d22009-03-03 19:31:44 -08001222 }
Robert Greenwalt986c7412010-09-08 15:24:47 -07001223 intent.putExtra(ConnectivityManager.EXTRA_INET_CONDITION, mDefaultInetConditionPublished);
Mike Lockwoodfde2b762009-08-14 14:18:49 -04001224 sendStickyBroadcast(intent);
The Android Open Source Project28527d22009-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 Project28527d22009-03-03 19:31:44 -08001233
Robert Greenwalt2034b912009-08-12 16:08:25 -07001234 String reason = info.getReason();
1235 String extraInfo = info.getExtraInfo();
Robert Greenwalt0659da32009-07-16 17:21:39 -07001236
Robert Greenwalte981bc52010-10-08 16:35:52 -07001237 String reasonText;
1238 if (reason == null) {
1239 reasonText = ".";
1240 } else {
1241 reasonText = " (" + reason + ").";
The Android Open Source Project28527d22009-03-03 19:31:44 -08001242 }
Wink Savillee70c6f52010-12-03 12:01:38 -08001243 loge("Attempt to connect to " + info.getTypeName() + " failed" + reasonText);
Robert Greenwalt2034b912009-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 Greenwaltf55ced92010-01-20 19:29:41 -08001260
Robert Greenwalt34848c02011-03-25 13:09:25 -07001261 if (mNetConfigs[info.getType()].isDefault()) {
Robert Greenwalt4b7c55e2011-01-11 13:56:33 -08001262 tryFailover(info.getType());
1263 if (mActiveDefaultNetwork != -1) {
1264 NetworkInfo switchTo = mNetTrackers[mActiveDefaultNetwork].getNetworkInfo();
Robert Greenwalt3cc68d32010-01-25 17:54:29 -08001265 intent.putExtra(ConnectivityManager.EXTRA_OTHER_NETWORK_INFO, switchTo);
1266 } else {
Robert Greenwaltb2a9e492010-09-20 18:01:43 -07001267 mDefaultInetConditionPublished = 0;
Robert Greenwalt3cc68d32010-01-25 17:54:29 -08001268 intent.putExtra(ConnectivityManager.EXTRA_NO_CONNECTIVITY, true);
1269 }
Robert Greenwaltf55ced92010-01-20 19:29:41 -08001270 }
Robert Greenwalt3cc68d32010-01-25 17:54:29 -08001271
Robert Greenwaltb2a9e492010-09-20 18:01:43 -07001272 intent.putExtra(ConnectivityManager.EXTRA_INET_CONDITION, mDefaultInetConditionPublished);
Mike Lockwoodfde2b762009-08-14 14:18:49 -04001273 sendStickyBroadcast(intent);
Robert Greenwaltf55ced92010-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 Greenwalt4b7c55e2011-01-11 13:56:33 -08001278 if (mActiveDefaultNetwork != -1) {
1279 sendConnectedBroadcast(mNetTrackers[mActiveDefaultNetwork].getNetworkInfo());
Robert Greenwaltf55ced92010-01-20 19:29:41 -08001280 }
Mike Lockwoodfde2b762009-08-14 14:18:49 -04001281 }
1282
1283 private void sendStickyBroadcast(Intent intent) {
1284 synchronized(this) {
Dianne Hackborna417ff82009-12-08 19:45:14 -08001285 if (!mSystemReady) {
1286 mInitialBroadcast = new Intent(intent);
Mike Lockwoodfde2b762009-08-14 14:18:49 -04001287 }
Dianne Hackborna417ff82009-12-08 19:45:14 -08001288 intent.addFlags(Intent.FLAG_RECEIVER_REGISTERED_ONLY_BEFORE_BOOT);
1289 mContext.sendStickyBroadcast(intent);
Mike Lockwoodfde2b762009-08-14 14:18:49 -04001290 }
1291 }
1292
1293 void systemReady() {
1294 synchronized(this) {
1295 mSystemReady = true;
Dianne Hackborna417ff82009-12-08 19:45:14 -08001296 if (mInitialBroadcast != null) {
1297 mContext.sendStickyBroadcast(mInitialBroadcast);
1298 mInitialBroadcast = null;
Mike Lockwoodfde2b762009-08-14 14:18:49 -04001299 }
1300 }
Robert Greenwaltc3c5f862010-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 Project28527d22009-03-03 19:31:44 -08001303 }
1304
1305 private void handleConnect(NetworkInfo info) {
Robert Greenwalt2034b912009-08-12 16:08:25 -07001306 int type = info.getType();
The Android Open Source Project28527d22009-03-03 19:31:44 -08001307
1308 // snapshot isFailover, because sendConnectedBroadcast() resets it
1309 boolean isFailover = info.isFailover();
Robert Greenwalt2034b912009-08-12 16:08:25 -07001310 NetworkStateTracker thisNet = mNetTrackers[type];
The Android Open Source Project28527d22009-03-03 19:31:44 -08001311
Robert Greenwalt2034b912009-08-12 16:08:25 -07001312 // if this is a default net and other default is running
1313 // kill the one not preferred
Robert Greenwalt34848c02011-03-25 13:09:25 -07001314 if (mNetConfigs[type].isDefault()) {
Robert Greenwalt2034b912009-08-12 16:08:25 -07001315 if (mActiveDefaultNetwork != -1 && mActiveDefaultNetwork != type) {
1316 if ((type != mNetworkPreference &&
Wink Savillef2a62832011-04-07 14:23:45 -07001317 mNetConfigs[mActiveDefaultNetwork].priority >
1318 mNetConfigs[type].priority) ||
Robert Greenwalt2034b912009-08-12 16:08:25 -07001319 mNetworkPreference == mActiveDefaultNetwork) {
1320 // don't accept this one
Wink Savillee70c6f52010-12-03 12:01:38 -08001321 if (DBG) {
1322 log("Not broadcasting CONNECT_ACTION " +
Robert Greenwalt2034b912009-08-12 16:08:25 -07001323 "to torn down network " + info.getTypeName());
Wink Savillee70c6f52010-12-03 12:01:38 -08001324 }
Robert Greenwalt2034b912009-08-12 16:08:25 -07001325 teardown(thisNet);
1326 return;
1327 } else {
1328 // tear down the other
1329 NetworkStateTracker otherNet =
1330 mNetTrackers[mActiveDefaultNetwork];
Wink Savillee70c6f52010-12-03 12:01:38 -08001331 if (DBG) {
1332 log("Policy requires " + otherNet.getNetworkInfo().getTypeName() +
Robert Greenwalt2034b912009-08-12 16:08:25 -07001333 " teardown");
Wink Savillee70c6f52010-12-03 12:01:38 -08001334 }
Robert Greenwalt2034b912009-08-12 16:08:25 -07001335 if (!teardown(otherNet)) {
Wink Savillee70c6f52010-12-03 12:01:38 -08001336 loge("Network declined teardown request");
Robert Greenwalt99910172011-03-29 11:36:28 -07001337 teardown(thisNet);
Robert Greenwalt2034b912009-08-12 16:08:25 -07001338 return;
1339 }
Robert Greenwalt93dc1042010-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 Greenwaltccb36f92010-09-24 14:32:21 -07001348 EVENT_CLEAR_NET_TRANSITION_WAKELOCK,
Robert Greenwalt93dc1042010-06-15 12:19:37 -07001349 mNetTransitionWakeLockSerialNumber, 0),
1350 1000);
Robert Greenwalt2034b912009-08-12 16:08:25 -07001351 }
1352 }
1353 mActiveDefaultNetwork = type;
Robert Greenwalt986c7412010-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 Greenwalt2034b912009-08-12 16:08:25 -07001362 }
The Android Open Source Project28527d22009-03-03 19:31:44 -08001363 thisNet.setTeardownRequested(false);
Irfan Sheriff7f132d92010-06-09 15:39:36 -07001364 updateNetworkSettings(thisNet);
Robert Greenwalt3afbead2010-07-23 15:46:26 -07001365 handleConnectivityChange(type);
Robert Greenwalt2034b912009-08-12 16:08:25 -07001366 sendConnectedBroadcast(info);
The Android Open Source Project28527d22009-03-03 19:31:44 -08001367 }
1368
The Android Open Source Project28527d22009-03-03 19:31:44 -08001369 /**
Robert Greenwalt3afbead2010-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 Project28527d22009-03-03 19:31:44 -08001374 */
Robert Greenwalt3afbead2010-07-23 15:46:26 -07001375 private void handleConnectivityChange(int netType) {
The Android Open Source Project28527d22009-03-03 19:31:44 -08001376 /*
Robert Greenwalt2034b912009-08-12 16:08:25 -07001377 * If a non-default network is enabled, add the host routes that
Robert Greenwalt3afbead2010-07-23 15:46:26 -07001378 * will allow it's DNS servers to be accessed.
The Android Open Source Project28527d22009-03-03 19:31:44 -08001379 */
Robert Greenwalt3afbead2010-07-23 15:46:26 -07001380 handleDnsConfigurationChange(netType);
Robert Greenwalt2034b912009-08-12 16:08:25 -07001381
Robert Greenwalt3afbead2010-07-23 15:46:26 -07001382 if (mNetTrackers[netType].getNetworkInfo().isConnected()) {
Robert Greenwalt34848c02011-03-25 13:09:25 -07001383 if (mNetConfigs[netType].isDefault()) {
Robert Greenwaltc3c5f862010-10-11 16:00:27 -07001384 handleApplyDefaultProxy(netType);
Robert Greenwalt3afbead2010-07-23 15:46:26 -07001385 addDefaultRoute(mNetTrackers[netType]);
Robert Greenwalt2034b912009-08-12 16:08:25 -07001386 } else {
Robert Greenwalt1e2f2862011-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 Jurka837e3642011-03-30 19:54:08 -07001400 addPrivateDnsRoutes(mNetTrackers[netType]);
Robert Greenwalt3afbead2010-07-23 15:46:26 -07001401 }
Kazuhiro Ondo3a340412011-04-30 20:10:57 -05001402
1403 /** Notify TetheringService if interface name has been changed. */
1404 if (TextUtils.equals(mNetTrackers[netType].getNetworkInfo().getReason(),
1405 Phone.REASON_LINK_PROPERTIES_CHANGED)) {
1406 handleTetherIfaceChange(netType);
1407 }
Robert Greenwalt3afbead2010-07-23 15:46:26 -07001408 } else {
Robert Greenwalt34848c02011-03-25 13:09:25 -07001409 if (mNetConfigs[netType].isDefault()) {
Robert Greenwalt3afbead2010-07-23 15:46:26 -07001410 removeDefaultRoute(mNetTrackers[netType]);
1411 } else {
1412 removePrivateDnsRoutes(mNetTrackers[netType]);
Robert Greenwalt2034b912009-08-12 16:08:25 -07001413 }
The Android Open Source Project28527d22009-03-03 19:31:44 -08001414 }
1415 }
1416
Irfan Sheriff7f132d92010-06-09 15:39:36 -07001417 private void addPrivateDnsRoutes(NetworkStateTracker nt) {
Irfan Sheriff7f132d92010-06-09 15:39:36 -07001418 boolean privateDnsRouteSet = nt.isPrivateDnsRouteSet();
Robert Greenwalt1f1bcfe2010-08-30 10:56:47 -07001419 LinkProperties p = nt.getLinkProperties();
Robert Greenwalta7dfbd32010-06-15 15:43:39 -07001420 if (p == null) return;
1421 String interfaceName = p.getInterfaceName();
Irfan Sheriff7f132d92010-06-09 15:39:36 -07001422
1423 if (DBG) {
Wink Savillee70c6f52010-12-03 12:01:38 -08001424 log("addPrivateDnsRoutes for " + nt +
Irfan Sheriff7f132d92010-06-09 15:39:36 -07001425 "(" + interfaceName + ") - mPrivateDnsRouteSet = " + privateDnsRouteSet);
1426 }
Irfan Sheriff7f132d92010-06-09 15:39:36 -07001427 if (interfaceName != null && !privateDnsRouteSet) {
Robert Greenwalta7dfbd32010-06-15 15:43:39 -07001428 Collection<InetAddress> dnsList = p.getDnses();
1429 for (InetAddress dns : dnsList) {
Wink Savillee70c6f52010-12-03 12:01:38 -08001430 if (DBG) log(" adding " + dns);
Robert Greenwaltbd492212011-05-06 17:10:53 -07001431 addHostRoute(nt, dns, 0);
Irfan Sheriff7f132d92010-06-09 15:39:36 -07001432 }
1433 nt.privateDnsRouteSet(true);
1434 }
1435 }
1436
1437 private void removePrivateDnsRoutes(NetworkStateTracker nt) {
1438 // TODO - we should do this explicitly but the NetUtils api doesnt
1439 // support this yet - must remove all. No worse than before
Robert Greenwalt1f1bcfe2010-08-30 10:56:47 -07001440 LinkProperties p = nt.getLinkProperties();
Robert Greenwalta7dfbd32010-06-15 15:43:39 -07001441 if (p == null) return;
1442 String interfaceName = p.getInterfaceName();
Irfan Sheriff7f132d92010-06-09 15:39:36 -07001443 boolean privateDnsRouteSet = nt.isPrivateDnsRouteSet();
1444 if (interfaceName != null && privateDnsRouteSet) {
1445 if (DBG) {
Wink Savillee70c6f52010-12-03 12:01:38 -08001446 log("removePrivateDnsRoutes for " + nt.getNetworkInfo().getTypeName() +
Irfan Sheriff7f132d92010-06-09 15:39:36 -07001447 " (" + interfaceName + ")");
1448 }
1449 NetworkUtils.removeHostRoutes(interfaceName);
1450 nt.privateDnsRouteSet(false);
1451 }
1452 }
1453
Irfan Sheriff7f132d92010-06-09 15:39:36 -07001454
1455 private void addDefaultRoute(NetworkStateTracker nt) {
Robert Greenwalt1f1bcfe2010-08-30 10:56:47 -07001456 LinkProperties p = nt.getLinkProperties();
Robert Greenwalta7dfbd32010-06-15 15:43:39 -07001457 if (p == null) return;
1458 String interfaceName = p.getInterfaceName();
Robert Greenwalt5c733972011-02-09 13:56:06 -08001459 if (TextUtils.isEmpty(interfaceName)) return;
Robert Greenwalt5a901292011-04-28 14:28:50 -07001460 for (RouteInfo route : p.getRoutes()) {
Irfan Sheriff7f132d92010-06-09 15:39:36 -07001461
Robert Greenwalt5a901292011-04-28 14:28:50 -07001462 //TODO - handle non-default routes
1463 if (route.isDefaultRoute()) {
1464 InetAddress gateway = route.getGateway();
Robert Greenwaltbd492212011-05-06 17:10:53 -07001465 if (addHostRoute(nt, gateway, 0) &&
Robert Greenwalt5a901292011-04-28 14:28:50 -07001466 NetworkUtils.addDefaultRoute(interfaceName, gateway)) {
1467 if (DBG) {
1468 NetworkInfo networkInfo = nt.getNetworkInfo();
1469 log("addDefaultRoute for " + networkInfo.getTypeName() +
1470 " (" + interfaceName + "), GatewayAddr=" +
1471 gateway.getHostAddress());
1472 }
Robert Greenwalt03d53da2011-03-22 18:47:42 -07001473 }
Irfan Sheriff7f132d92010-06-09 15:39:36 -07001474 }
Irfan Sheriff7f132d92010-06-09 15:39:36 -07001475 }
1476 }
1477
1478
1479 public void removeDefaultRoute(NetworkStateTracker nt) {
Robert Greenwalt1f1bcfe2010-08-30 10:56:47 -07001480 LinkProperties p = nt.getLinkProperties();
Robert Greenwalta7dfbd32010-06-15 15:43:39 -07001481 if (p == null) return;
1482 String interfaceName = p.getInterfaceName();
Irfan Sheriff7f132d92010-06-09 15:39:36 -07001483
Robert Greenwalt3afbead2010-07-23 15:46:26 -07001484 if (interfaceName != null) {
Robert Greenwalt03d53da2011-03-22 18:47:42 -07001485 if (NetworkUtils.removeDefaultRoute(interfaceName) >= 0) {
1486 if (DBG) {
1487 NetworkInfo networkInfo = nt.getNetworkInfo();
1488 log("removeDefaultRoute for " + networkInfo.getTypeName() + " (" +
1489 interfaceName + ")");
1490 }
Irfan Sheriff7f132d92010-06-09 15:39:36 -07001491 }
Irfan Sheriff7f132d92010-06-09 15:39:36 -07001492 }
1493 }
1494
1495 /**
1496 * Reads the network specific TCP buffer sizes from SystemProperties
1497 * net.tcp.buffersize.[default|wifi|umts|edge|gprs] and set them for system
1498 * wide use
1499 */
1500 public void updateNetworkSettings(NetworkStateTracker nt) {
1501 String key = nt.getTcpBufferSizesPropName();
1502 String bufferSizes = SystemProperties.get(key);
1503
1504 if (bufferSizes.length() == 0) {
Wink Savillee70c6f52010-12-03 12:01:38 -08001505 loge(key + " not found in system properties. Using defaults");
Irfan Sheriff7f132d92010-06-09 15:39:36 -07001506
1507 // Setting to default values so we won't be stuck to previous values
1508 key = "net.tcp.buffersize.default";
1509 bufferSizes = SystemProperties.get(key);
1510 }
1511
1512 // Set values in kernel
1513 if (bufferSizes.length() != 0) {
1514 if (DBG) {
Wink Savillee70c6f52010-12-03 12:01:38 -08001515 log("Setting TCP values: [" + bufferSizes
Irfan Sheriff7f132d92010-06-09 15:39:36 -07001516 + "] which comes from [" + key + "]");
1517 }
1518 setBufferSize(bufferSizes);
1519 }
1520 }
1521
1522 /**
1523 * Writes TCP buffer sizes to /sys/kernel/ipv4/tcp_[r/w]mem_[min/def/max]
1524 * which maps to /proc/sys/net/ipv4/tcp_rmem and tcpwmem
1525 *
1526 * @param bufferSizes in the format of "readMin, readInitial, readMax,
1527 * writeMin, writeInitial, writeMax"
1528 */
1529 private void setBufferSize(String bufferSizes) {
1530 try {
1531 String[] values = bufferSizes.split(",");
1532
1533 if (values.length == 6) {
1534 final String prefix = "/sys/kernel/ipv4/tcp_";
1535 stringToFile(prefix + "rmem_min", values[0]);
1536 stringToFile(prefix + "rmem_def", values[1]);
1537 stringToFile(prefix + "rmem_max", values[2]);
1538 stringToFile(prefix + "wmem_min", values[3]);
1539 stringToFile(prefix + "wmem_def", values[4]);
1540 stringToFile(prefix + "wmem_max", values[5]);
1541 } else {
Wink Savillee70c6f52010-12-03 12:01:38 -08001542 loge("Invalid buffersize string: " + bufferSizes);
Irfan Sheriff7f132d92010-06-09 15:39:36 -07001543 }
1544 } catch (IOException e) {
Wink Savillee70c6f52010-12-03 12:01:38 -08001545 loge("Can't set tcp buffer sizes:" + e);
Irfan Sheriff7f132d92010-06-09 15:39:36 -07001546 }
1547 }
1548
1549 /**
1550 * Writes string to file. Basically same as "echo -n $string > $filename"
1551 *
1552 * @param filename
1553 * @param string
1554 * @throws IOException
1555 */
1556 private void stringToFile(String filename, String string) throws IOException {
1557 FileWriter out = new FileWriter(filename);
1558 try {
1559 out.write(string);
1560 } finally {
1561 out.close();
1562 }
1563 }
1564
1565
Robert Greenwalt2034b912009-08-12 16:08:25 -07001566 /**
1567 * Adjust the per-process dns entries (net.dns<x>.<pid>) based
1568 * on the highest priority active net which this process requested.
1569 * If there aren't any, clear it out
1570 */
1571 private void reassessPidDns(int myPid, boolean doBump)
1572 {
Wink Savillee70c6f52010-12-03 12:01:38 -08001573 if (DBG) log("reassessPidDns for pid " + myPid);
Robert Greenwalt2034b912009-08-12 16:08:25 -07001574 for(int i : mPriorityList) {
Robert Greenwalt34848c02011-03-25 13:09:25 -07001575 if (mNetConfigs[i].isDefault()) {
Robert Greenwalt2034b912009-08-12 16:08:25 -07001576 continue;
1577 }
1578 NetworkStateTracker nt = mNetTrackers[i];
Robert Greenwalt0659da32009-07-16 17:21:39 -07001579 if (nt.getNetworkInfo().isConnected() &&
1580 !nt.isTeardownRequested()) {
Robert Greenwalt1f1bcfe2010-08-30 10:56:47 -07001581 LinkProperties p = nt.getLinkProperties();
Robert Greenwalta7dfbd32010-06-15 15:43:39 -07001582 if (p == null) continue;
Robert Greenwalt2034b912009-08-12 16:08:25 -07001583 List pids = mNetRequestersPids[i];
1584 for (int j=0; j<pids.size(); j++) {
1585 Integer pid = (Integer)pids.get(j);
1586 if (pid.intValue() == myPid) {
Robert Greenwalta7dfbd32010-06-15 15:43:39 -07001587 Collection<InetAddress> dnses = p.getDnses();
1588 writePidDns(dnses, myPid);
Robert Greenwalt2034b912009-08-12 16:08:25 -07001589 if (doBump) {
1590 bumpDns();
1591 }
1592 return;
1593 }
1594 }
1595 }
1596 }
1597 // nothing found - delete
1598 for (int i = 1; ; i++) {
1599 String prop = "net.dns" + i + "." + myPid;
1600 if (SystemProperties.get(prop).length() == 0) {
1601 if (doBump) {
1602 bumpDns();
1603 }
1604 return;
1605 }
1606 SystemProperties.set(prop, "");
1607 }
1608 }
1609
Robert Greenwalt8ca88762010-12-17 15:20:36 -08001610 // return true if results in a change
1611 private boolean writePidDns(Collection <InetAddress> dnses, int pid) {
Robert Greenwalt2034b912009-08-12 16:08:25 -07001612 int j = 1;
Robert Greenwalt8ca88762010-12-17 15:20:36 -08001613 boolean changed = false;
Robert Greenwalta7dfbd32010-06-15 15:43:39 -07001614 for (InetAddress dns : dnses) {
Robert Greenwalt8ca88762010-12-17 15:20:36 -08001615 String dnsString = dns.getHostAddress();
1616 if (changed || !dnsString.equals(SystemProperties.get("net.dns" + j + "." + pid))) {
1617 changed = true;
1618 SystemProperties.set("net.dns" + j++ + "." + pid, dns.getHostAddress());
1619 }
Robert Greenwalt2034b912009-08-12 16:08:25 -07001620 }
Robert Greenwalt8ca88762010-12-17 15:20:36 -08001621 return changed;
Robert Greenwalt2034b912009-08-12 16:08:25 -07001622 }
1623
1624 private void bumpDns() {
1625 /*
1626 * Bump the property that tells the name resolver library to reread
1627 * the DNS server list from the properties.
1628 */
1629 String propVal = SystemProperties.get("net.dnschange");
1630 int n = 0;
1631 if (propVal.length() != 0) {
1632 try {
1633 n = Integer.parseInt(propVal);
1634 } catch (NumberFormatException e) {}
1635 }
1636 SystemProperties.set("net.dnschange", "" + (n+1));
Robert Greenwalt051642b2010-11-02 14:08:23 -07001637 /*
1638 * Tell the VMs to toss their DNS caches
1639 */
1640 Intent intent = new Intent(Intent.ACTION_CLEAR_DNS_CACHE);
1641 intent.addFlags(Intent.FLAG_RECEIVER_REPLACE_PENDING);
Stan Chesnuttf444f502011-01-05 17:14:03 -08001642 /*
1643 * Connectivity events can happen before boot has completed ...
1644 */
1645 intent.addFlags(Intent.FLAG_RECEIVER_REGISTERED_ONLY_BEFORE_BOOT);
Robert Greenwalt051642b2010-11-02 14:08:23 -07001646 mContext.sendBroadcast(intent);
Robert Greenwalt2034b912009-08-12 16:08:25 -07001647 }
1648
Robert Greenwalt3afbead2010-07-23 15:46:26 -07001649 private void handleDnsConfigurationChange(int netType) {
Robert Greenwalt2034b912009-08-12 16:08:25 -07001650 // add default net's dns entries
Robert Greenwalt3afbead2010-07-23 15:46:26 -07001651 NetworkStateTracker nt = mNetTrackers[netType];
1652 if (nt != null && nt.getNetworkInfo().isConnected() && !nt.isTeardownRequested()) {
Robert Greenwalt1f1bcfe2010-08-30 10:56:47 -07001653 LinkProperties p = nt.getLinkProperties();
Robert Greenwalt3afbead2010-07-23 15:46:26 -07001654 if (p == null) return;
1655 Collection<InetAddress> dnses = p.getDnses();
Robert Greenwalt8ca88762010-12-17 15:20:36 -08001656 boolean changed = false;
Robert Greenwalt34848c02011-03-25 13:09:25 -07001657 if (mNetConfigs[netType].isDefault()) {
Robert Greenwalt3afbead2010-07-23 15:46:26 -07001658 int j = 1;
Robert Greenwalt94daa182010-09-01 11:34:05 -07001659 if (dnses.size() == 0 && mDefaultDns != null) {
Robert Greenwalt8ca88762010-12-17 15:20:36 -08001660 String dnsString = mDefaultDns.getHostAddress();
1661 if (!dnsString.equals(SystemProperties.get("net.dns1"))) {
1662 if (DBG) {
1663 log("no dns provided - using " + dnsString);
1664 }
1665 changed = true;
1666 SystemProperties.set("net.dns1", dnsString);
Robert Greenwalt2034b912009-08-12 16:08:25 -07001667 }
Robert Greenwalt94daa182010-09-01 11:34:05 -07001668 j++;
1669 } else {
1670 for (InetAddress dns : dnses) {
Robert Greenwalt8ca88762010-12-17 15:20:36 -08001671 String dnsString = dns.getHostAddress();
1672 if (!changed && dnsString.equals(SystemProperties.get("net.dns" + j))) {
1673 j++;
1674 continue;
1675 }
Robert Greenwalt94daa182010-09-01 11:34:05 -07001676 if (DBG) {
Wink Savillee70c6f52010-12-03 12:01:38 -08001677 log("adding dns " + dns + " for " +
Robert Greenwalt94daa182010-09-01 11:34:05 -07001678 nt.getNetworkInfo().getTypeName());
1679 }
Robert Greenwalt8ca88762010-12-17 15:20:36 -08001680 changed = true;
1681 SystemProperties.set("net.dns" + j++, dnsString);
Robert Greenwalt94daa182010-09-01 11:34:05 -07001682 }
Robert Greenwalt3afbead2010-07-23 15:46:26 -07001683 }
1684 for (int k=j ; k<mNumDnsEntries; k++) {
Robert Greenwalt8ca88762010-12-17 15:20:36 -08001685 if (changed || !TextUtils.isEmpty(SystemProperties.get("net.dns" + k))) {
1686 if (DBG) log("erasing net.dns" + k);
1687 changed = true;
1688 SystemProperties.set("net.dns" + k, "");
1689 }
Robert Greenwalt3afbead2010-07-23 15:46:26 -07001690 }
1691 mNumDnsEntries = j;
1692 } else {
1693 // set per-pid dns for attached secondary nets
1694 List pids = mNetRequestersPids[netType];
1695 for (int y=0; y< pids.size(); y++) {
1696 Integer pid = (Integer)pids.get(y);
Robert Greenwalt8ca88762010-12-17 15:20:36 -08001697 changed = writePidDns(dnses, pid.intValue());
The Android Open Source Project28527d22009-03-03 19:31:44 -08001698 }
1699 }
Robert Greenwalt8ca88762010-12-17 15:20:36 -08001700 if (changed) bumpDns();
The Android Open Source Project28527d22009-03-03 19:31:44 -08001701 }
Robert Greenwalt2034b912009-08-12 16:08:25 -07001702 }
1703
Robert Greenwalt20f819c2011-05-03 19:02:44 -07001704 private int getRestoreDefaultNetworkDelay(int networkType) {
Robert Greenwalt2034b912009-08-12 16:08:25 -07001705 String restoreDefaultNetworkDelayStr = SystemProperties.get(
1706 NETWORK_RESTORE_DELAY_PROP_NAME);
1707 if(restoreDefaultNetworkDelayStr != null &&
1708 restoreDefaultNetworkDelayStr.length() != 0) {
1709 try {
1710 return Integer.valueOf(restoreDefaultNetworkDelayStr);
1711 } catch (NumberFormatException e) {
1712 }
The Android Open Source Project28527d22009-03-03 19:31:44 -08001713 }
Robert Greenwalt20f819c2011-05-03 19:02:44 -07001714 // if the system property isn't set, use the value for the apn type
1715 int ret = RESTORE_DEFAULT_NETWORK_DELAY;
1716
1717 if ((networkType <= ConnectivityManager.MAX_NETWORK_TYPE) &&
1718 (mNetConfigs[networkType] != null)) {
1719 ret = mNetConfigs[networkType].restoreTime;
1720 }
1721 return ret;
The Android Open Source Project28527d22009-03-03 19:31:44 -08001722 }
1723
1724 @Override
1725 protected void dump(FileDescriptor fd, PrintWriter pw, String[] args) {
Robert Greenwalt0659da32009-07-16 17:21:39 -07001726 if (mContext.checkCallingOrSelfPermission(
1727 android.Manifest.permission.DUMP)
The Android Open Source Project28527d22009-03-03 19:31:44 -08001728 != PackageManager.PERMISSION_GRANTED) {
Robert Greenwalt0659da32009-07-16 17:21:39 -07001729 pw.println("Permission Denial: can't dump ConnectivityService " +
1730 "from from pid=" + Binder.getCallingPid() + ", uid=" +
1731 Binder.getCallingUid());
The Android Open Source Project28527d22009-03-03 19:31:44 -08001732 return;
1733 }
The Android Open Source Project28527d22009-03-03 19:31:44 -08001734 pw.println();
1735 for (NetworkStateTracker nst : mNetTrackers) {
Robert Greenwalt3eeb6032009-12-21 18:24:07 -08001736 if (nst != null) {
1737 if (nst.getNetworkInfo().isConnected()) {
1738 pw.println("Active network: " + nst.getNetworkInfo().
1739 getTypeName());
1740 }
1741 pw.println(nst.getNetworkInfo());
1742 pw.println(nst);
1743 pw.println();
Robert Greenwalt2034b912009-08-12 16:08:25 -07001744 }
The Android Open Source Project28527d22009-03-03 19:31:44 -08001745 }
Robert Greenwalt3eeb6032009-12-21 18:24:07 -08001746
1747 pw.println("Network Requester Pids:");
1748 for (int net : mPriorityList) {
1749 String pidString = net + ": ";
1750 for (Object pid : mNetRequestersPids[net]) {
1751 pidString = pidString + pid.toString() + ", ";
1752 }
1753 pw.println(pidString);
1754 }
1755 pw.println();
1756
1757 pw.println("FeatureUsers:");
1758 for (Object requester : mFeatureUsers) {
1759 pw.println(requester.toString());
1760 }
1761 pw.println();
Robert Greenwalt8e87f122010-02-11 18:18:40 -08001762
Robert Greenwalt93dc1042010-06-15 12:19:37 -07001763 synchronized (this) {
1764 pw.println("NetworkTranstionWakeLock is currently " +
1765 (mNetTransitionWakeLock.isHeld() ? "" : "not ") + "held.");
1766 pw.println("It was last requested for "+mNetTransitionWakeLockCausedBy);
1767 }
1768 pw.println();
1769
Robert Greenwalt8e87f122010-02-11 18:18:40 -08001770 mTethering.dump(fd, pw, args);
Robert Greenwalt0e80be12010-09-20 14:35:25 -07001771
1772 if (mInetLog != null) {
1773 pw.println();
1774 pw.println("Inet condition reports:");
1775 for(int i = 0; i < mInetLog.size(); i++) {
1776 pw.println(mInetLog.get(i));
1777 }
1778 }
The Android Open Source Project28527d22009-03-03 19:31:44 -08001779 }
1780
Robert Greenwalt2034b912009-08-12 16:08:25 -07001781 // must be stateless - things change under us.
The Android Open Source Project28527d22009-03-03 19:31:44 -08001782 private class MyHandler extends Handler {
Wink Saville775aad62010-09-02 19:23:52 -07001783 public MyHandler(Looper looper) {
1784 super(looper);
1785 }
1786
The Android Open Source Project28527d22009-03-03 19:31:44 -08001787 @Override
1788 public void handleMessage(Message msg) {
1789 NetworkInfo info;
1790 switch (msg.what) {
1791 case NetworkStateTracker.EVENT_STATE_CHANGED:
1792 info = (NetworkInfo) msg.obj;
Robert Greenwalt12c44552009-12-07 11:33:18 -08001793 int type = info.getType();
1794 NetworkInfo.State state = info.getState();
Robert Greenwalt12c44552009-12-07 11:33:18 -08001795
Wink Savillee70c6f52010-12-03 12:01:38 -08001796 if (DBG) log("ConnectivityChange for " +
Robert Greenwalt0659da32009-07-16 17:21:39 -07001797 info.getTypeName() + ": " +
Robert Greenwalt12c44552009-12-07 11:33:18 -08001798 state + "/" + info.getDetailedState());
The Android Open Source Project28527d22009-03-03 19:31:44 -08001799
1800 // Connectivity state changed:
1801 // [31-13] Reserved for future use
Robert Greenwalt0659da32009-07-16 17:21:39 -07001802 // [12-9] Network subtype (for mobile network, as defined
1803 // by TelephonyManager)
1804 // [8-3] Detailed state ordinal (as defined by
1805 // NetworkInfo.DetailedState)
The Android Open Source Project28527d22009-03-03 19:31:44 -08001806 // [2-0] Network type (as defined by ConnectivityManager)
1807 int eventLogParam = (info.getType() & 0x7) |
1808 ((info.getDetailedState().ordinal() & 0x3f) << 3) |
1809 (info.getSubtype() << 9);
Doug Zongker2fc96232009-12-04 10:31:43 -08001810 EventLog.writeEvent(EventLogTags.CONNECTIVITY_STATE_CHANGED,
Robert Greenwalt0659da32009-07-16 17:21:39 -07001811 eventLogParam);
1812
1813 if (info.getDetailedState() ==
1814 NetworkInfo.DetailedState.FAILED) {
The Android Open Source Project28527d22009-03-03 19:31:44 -08001815 handleConnectionFailure(info);
Robert Greenwalt12c44552009-12-07 11:33:18 -08001816 } else if (state == NetworkInfo.State.DISCONNECTED) {
The Android Open Source Project28527d22009-03-03 19:31:44 -08001817 handleDisconnect(info);
Robert Greenwalt12c44552009-12-07 11:33:18 -08001818 } else if (state == NetworkInfo.State.SUSPENDED) {
The Android Open Source Project28527d22009-03-03 19:31:44 -08001819 // TODO: need to think this over.
Robert Greenwalt0659da32009-07-16 17:21:39 -07001820 // the logic here is, handle SUSPENDED the same as
1821 // DISCONNECTED. The only difference being we are
1822 // broadcasting an intent with NetworkInfo that's
1823 // suspended. This allows the applications an
1824 // opportunity to handle DISCONNECTED and SUSPENDED
1825 // differently, or not.
The Android Open Source Project28527d22009-03-03 19:31:44 -08001826 handleDisconnect(info);
Robert Greenwalt12c44552009-12-07 11:33:18 -08001827 } else if (state == NetworkInfo.State.CONNECTED) {
The Android Open Source Project28527d22009-03-03 19:31:44 -08001828 handleConnect(info);
1829 }
The Android Open Source Project28527d22009-03-03 19:31:44 -08001830 break;
The Android Open Source Project28527d22009-03-03 19:31:44 -08001831 case NetworkStateTracker.EVENT_CONFIGURATION_CHANGED:
Robert Greenwalt3afbead2010-07-23 15:46:26 -07001832 info = (NetworkInfo) msg.obj;
Robert Greenwalt34848c02011-03-25 13:09:25 -07001833 handleConnectivityChange(info.getType());
The Android Open Source Project28527d22009-03-03 19:31:44 -08001834 break;
Robert Greenwaltccb36f92010-09-24 14:32:21 -07001835 case EVENT_CLEAR_NET_TRANSITION_WAKELOCK:
Robert Greenwalt93dc1042010-06-15 12:19:37 -07001836 String causedBy = null;
1837 synchronized (ConnectivityService.this) {
1838 if (msg.arg1 == mNetTransitionWakeLockSerialNumber &&
1839 mNetTransitionWakeLock.isHeld()) {
1840 mNetTransitionWakeLock.release();
1841 causedBy = mNetTransitionWakeLockCausedBy;
1842 }
1843 }
1844 if (causedBy != null) {
Wink Savillee70c6f52010-12-03 12:01:38 -08001845 log("NetTransition Wakelock for " + causedBy + " released by timeout");
Robert Greenwalt93dc1042010-06-15 12:19:37 -07001846 }
Robert Greenwaltcf1a56c2010-09-09 14:05:10 -07001847 break;
Robert Greenwalt6a2db8a2010-09-23 10:05:56 -07001848 case EVENT_RESTORE_DEFAULT_NETWORK:
Robert Greenwaltaffc3a12009-09-27 17:27:04 -07001849 FeatureUser u = (FeatureUser)msg.obj;
1850 u.expire();
Robert Greenwalt986c7412010-09-08 15:24:47 -07001851 break;
Robert Greenwalt6a2db8a2010-09-23 10:05:56 -07001852 case EVENT_INET_CONDITION_CHANGE:
1853 {
1854 int netType = msg.arg1;
1855 int condition = msg.arg2;
1856 handleInetConditionChange(netType, condition);
Robert Greenwalt93dc1042010-06-15 12:19:37 -07001857 break;
Robert Greenwalt6a2db8a2010-09-23 10:05:56 -07001858 }
1859 case EVENT_INET_CONDITION_HOLD_END:
1860 {
1861 int netType = msg.arg1;
1862 int sequence = msg.arg2;
1863 handleInetConditionHoldEnd(netType, sequence);
Robert Greenwalt986c7412010-09-08 15:24:47 -07001864 break;
Robert Greenwalt6a2db8a2010-09-23 10:05:56 -07001865 }
1866 case EVENT_SET_NETWORK_PREFERENCE:
1867 {
1868 int preference = msg.arg1;
1869 handleSetNetworkPreference(preference);
1870 break;
1871 }
1872 case EVENT_SET_BACKGROUND_DATA:
1873 {
1874 boolean enabled = (msg.arg1 == ENABLED);
1875 handleSetBackgroundData(enabled);
1876 break;
1877 }
1878 case EVENT_SET_MOBILE_DATA:
1879 {
1880 boolean enabled = (msg.arg1 == ENABLED);
1881 handleSetMobileData(enabled);
1882 break;
1883 }
Robert Greenwaltc3c5f862010-10-11 16:00:27 -07001884 case EVENT_APPLY_GLOBAL_HTTP_PROXY:
1885 {
1886 handleDeprecatedGlobalHttpProxy();
Robert Greenwalt34848c02011-03-25 13:09:25 -07001887 break;
1888 }
1889 case EVENT_SET_DEPENDENCY_MET:
1890 {
1891 boolean met = (msg.arg1 == ENABLED);
1892 handleSetDependencyMet(msg.arg2, met);
1893 break;
Robert Greenwaltc3c5f862010-10-11 16:00:27 -07001894 }
The Android Open Source Project28527d22009-03-03 19:31:44 -08001895 }
1896 }
1897 }
Robert Greenwalt0c4828c2010-01-26 11:40:34 -08001898
1899 // javadoc from interface
Robert Greenwalt4283ded2010-03-02 17:25:02 -08001900 public int tether(String iface) {
Robert Greenwalt0c4828c2010-01-26 11:40:34 -08001901 enforceTetherChangePermission();
Robert Greenwalt4283ded2010-03-02 17:25:02 -08001902
1903 if (isTetheringSupported()) {
1904 return mTethering.tether(iface);
1905 } else {
1906 return ConnectivityManager.TETHER_ERROR_UNSUPPORTED;
1907 }
Robert Greenwalt0c4828c2010-01-26 11:40:34 -08001908 }
1909
1910 // javadoc from interface
Robert Greenwalt4283ded2010-03-02 17:25:02 -08001911 public int untether(String iface) {
Robert Greenwalt0c4828c2010-01-26 11:40:34 -08001912 enforceTetherChangePermission();
Robert Greenwalt4283ded2010-03-02 17:25:02 -08001913
1914 if (isTetheringSupported()) {
1915 return mTethering.untether(iface);
1916 } else {
1917 return ConnectivityManager.TETHER_ERROR_UNSUPPORTED;
1918 }
1919 }
1920
1921 // javadoc from interface
1922 public int getLastTetherError(String iface) {
1923 enforceTetherAccessPermission();
1924
1925 if (isTetheringSupported()) {
1926 return mTethering.getLastTetherError(iface);
1927 } else {
1928 return ConnectivityManager.TETHER_ERROR_UNSUPPORTED;
1929 }
Robert Greenwalt8e87f122010-02-11 18:18:40 -08001930 }
1931
1932 // TODO - proper iface API for selection by property, inspection, etc
1933 public String[] getTetherableUsbRegexs() {
1934 enforceTetherAccessPermission();
1935 if (isTetheringSupported()) {
1936 return mTethering.getTetherableUsbRegexs();
1937 } else {
1938 return new String[0];
1939 }
1940 }
1941
1942 public String[] getTetherableWifiRegexs() {
1943 enforceTetherAccessPermission();
1944 if (isTetheringSupported()) {
1945 return mTethering.getTetherableWifiRegexs();
1946 } else {
1947 return new String[0];
1948 }
Robert Greenwalt0c4828c2010-01-26 11:40:34 -08001949 }
1950
Danica Chang96567052010-08-11 14:54:43 -07001951 public String[] getTetherableBluetoothRegexs() {
1952 enforceTetherAccessPermission();
1953 if (isTetheringSupported()) {
1954 return mTethering.getTetherableBluetoothRegexs();
1955 } else {
1956 return new String[0];
1957 }
1958 }
1959
Robert Greenwalt0c4828c2010-01-26 11:40:34 -08001960 // TODO - move iface listing, queries, etc to new module
1961 // javadoc from interface
1962 public String[] getTetherableIfaces() {
Robert Greenwalt8e87f122010-02-11 18:18:40 -08001963 enforceTetherAccessPermission();
Robert Greenwalt0c4828c2010-01-26 11:40:34 -08001964 return mTethering.getTetherableIfaces();
1965 }
1966
1967 public String[] getTetheredIfaces() {
Robert Greenwalt8e87f122010-02-11 18:18:40 -08001968 enforceTetherAccessPermission();
Robert Greenwalt0c4828c2010-01-26 11:40:34 -08001969 return mTethering.getTetheredIfaces();
1970 }
Robert Greenwalt8e87f122010-02-11 18:18:40 -08001971
Robert Greenwalt4283ded2010-03-02 17:25:02 -08001972 public String[] getTetheringErroredIfaces() {
1973 enforceTetherAccessPermission();
1974 return mTethering.getErroredIfaces();
1975 }
1976
Robert Greenwalt8e87f122010-02-11 18:18:40 -08001977 // if ro.tether.denied = true we default to no tethering
1978 // gservices could set the secure setting to 1 though to enable it on a build where it
1979 // had previously been turned off.
1980 public boolean isTetheringSupported() {
1981 enforceTetherAccessPermission();
1982 int defaultVal = (SystemProperties.get("ro.tether.denied").equals("true") ? 0 : 1);
Robert Greenwaltf1b66e12010-02-25 12:29:30 -08001983 boolean tetherEnabledInSettings = (Settings.Secure.getInt(mContext.getContentResolver(),
1984 Settings.Secure.TETHER_SUPPORTED, defaultVal) != 0);
1985 return tetherEnabledInSettings && mTetheringConfigValid;
Robert Greenwalt8e87f122010-02-11 18:18:40 -08001986 }
Robert Greenwalt93dc1042010-06-15 12:19:37 -07001987
1988 // An API NetworkStateTrackers can call when they lose their network.
1989 // This will automatically be cleared after X seconds or a network becomes CONNECTED,
1990 // whichever happens first. The timer is started by the first caller and not
1991 // restarted by subsequent callers.
1992 public void requestNetworkTransitionWakelock(String forWhom) {
1993 enforceConnectivityInternalPermission();
1994 synchronized (this) {
1995 if (mNetTransitionWakeLock.isHeld()) return;
1996 mNetTransitionWakeLockSerialNumber++;
1997 mNetTransitionWakeLock.acquire();
1998 mNetTransitionWakeLockCausedBy = forWhom;
1999 }
2000 mHandler.sendMessageDelayed(mHandler.obtainMessage(
Robert Greenwaltccb36f92010-09-24 14:32:21 -07002001 EVENT_CLEAR_NET_TRANSITION_WAKELOCK,
Robert Greenwalt93dc1042010-06-15 12:19:37 -07002002 mNetTransitionWakeLockSerialNumber, 0),
2003 mNetTransitionWakeLockTimeout);
2004 return;
2005 }
Robert Greenwalt24118e82010-09-09 13:15:32 -07002006
Robert Greenwalt986c7412010-09-08 15:24:47 -07002007 // 100 percent is full good, 0 is full bad.
2008 public void reportInetCondition(int networkType, int percentage) {
Wink Savillee70c6f52010-12-03 12:01:38 -08002009 if (DBG) log("reportNetworkCondition(" + networkType + ", " + percentage + ")");
Robert Greenwalt986c7412010-09-08 15:24:47 -07002010 mContext.enforceCallingOrSelfPermission(
2011 android.Manifest.permission.STATUS_BAR,
2012 "ConnectivityService");
2013
Robert Greenwalt0e80be12010-09-20 14:35:25 -07002014 if (DBG) {
2015 int pid = getCallingPid();
2016 int uid = getCallingUid();
2017 String s = pid + "(" + uid + ") reports inet is " +
2018 (percentage > 50 ? "connected" : "disconnected") + " (" + percentage + ") on " +
2019 "network Type " + networkType + " at " + GregorianCalendar.getInstance().getTime();
2020 mInetLog.add(s);
2021 while(mInetLog.size() > INET_CONDITION_LOG_MAX_SIZE) {
2022 mInetLog.remove(0);
2023 }
2024 }
Robert Greenwalt986c7412010-09-08 15:24:47 -07002025 mHandler.sendMessage(mHandler.obtainMessage(
Robert Greenwalt6a2db8a2010-09-23 10:05:56 -07002026 EVENT_INET_CONDITION_CHANGE, networkType, percentage));
2027 }
2028
2029 private void handleInetConditionChange(int netType, int condition) {
2030 if (DBG) {
Wink Savillee70c6f52010-12-03 12:01:38 -08002031 log("Inet connectivity change, net=" +
Robert Greenwalt6a2db8a2010-09-23 10:05:56 -07002032 netType + ", condition=" + condition +
2033 ",mActiveDefaultNetwork=" + mActiveDefaultNetwork);
2034 }
2035 if (mActiveDefaultNetwork == -1) {
Wink Savillee70c6f52010-12-03 12:01:38 -08002036 if (DBG) log("no active default network - aborting");
Robert Greenwalt6a2db8a2010-09-23 10:05:56 -07002037 return;
2038 }
2039 if (mActiveDefaultNetwork != netType) {
Wink Savillee70c6f52010-12-03 12:01:38 -08002040 if (DBG) log("given net not default - aborting");
Robert Greenwalt6a2db8a2010-09-23 10:05:56 -07002041 return;
2042 }
2043 mDefaultInetCondition = condition;
2044 int delay;
2045 if (mInetConditionChangeInFlight == false) {
Wink Savillee70c6f52010-12-03 12:01:38 -08002046 if (DBG) log("starting a change hold");
Robert Greenwalt6a2db8a2010-09-23 10:05:56 -07002047 // setup a new hold to debounce this
2048 if (mDefaultInetCondition > 50) {
2049 delay = Settings.Secure.getInt(mContext.getContentResolver(),
2050 Settings.Secure.INET_CONDITION_DEBOUNCE_UP_DELAY, 500);
2051 } else {
2052 delay = Settings.Secure.getInt(mContext.getContentResolver(),
2053 Settings.Secure.INET_CONDITION_DEBOUNCE_DOWN_DELAY, 3000);
2054 }
2055 mInetConditionChangeInFlight = true;
2056 mHandler.sendMessageDelayed(mHandler.obtainMessage(EVENT_INET_CONDITION_HOLD_END,
2057 mActiveDefaultNetwork, mDefaultConnectionSequence), delay);
2058 } else {
2059 // we've set the new condition, when this hold ends that will get
2060 // picked up
Wink Savillee70c6f52010-12-03 12:01:38 -08002061 if (DBG) log("currently in hold - not setting new end evt");
Robert Greenwalt6a2db8a2010-09-23 10:05:56 -07002062 }
2063 }
2064
2065 private void handleInetConditionHoldEnd(int netType, int sequence) {
2066 if (DBG) {
Wink Savillee70c6f52010-12-03 12:01:38 -08002067 log("Inet hold end, net=" + netType +
Robert Greenwalt6a2db8a2010-09-23 10:05:56 -07002068 ", condition =" + mDefaultInetCondition +
2069 ", published condition =" + mDefaultInetConditionPublished);
2070 }
2071 mInetConditionChangeInFlight = false;
2072
2073 if (mActiveDefaultNetwork == -1) {
Wink Savillee70c6f52010-12-03 12:01:38 -08002074 if (DBG) log("no active default network - aborting");
Robert Greenwalt6a2db8a2010-09-23 10:05:56 -07002075 return;
2076 }
2077 if (mDefaultConnectionSequence != sequence) {
Wink Savillee70c6f52010-12-03 12:01:38 -08002078 if (DBG) log("event hold for obsolete network - aborting");
Robert Greenwalt6a2db8a2010-09-23 10:05:56 -07002079 return;
2080 }
2081 if (mDefaultInetConditionPublished == mDefaultInetCondition) {
Wink Savillee70c6f52010-12-03 12:01:38 -08002082 if (DBG) log("no change in condition - aborting");
Robert Greenwalt6a2db8a2010-09-23 10:05:56 -07002083 return;
2084 }
2085 NetworkInfo networkInfo = mNetTrackers[mActiveDefaultNetwork].getNetworkInfo();
2086 if (networkInfo.isConnected() == false) {
Wink Savillee70c6f52010-12-03 12:01:38 -08002087 if (DBG) log("default network not connected - aborting");
Robert Greenwalt6a2db8a2010-09-23 10:05:56 -07002088 return;
2089 }
2090 mDefaultInetConditionPublished = mDefaultInetCondition;
2091 sendInetConditionBroadcast(networkInfo);
2092 return;
Robert Greenwalt986c7412010-09-08 15:24:47 -07002093 }
Robert Greenwaltc3c5f862010-10-11 16:00:27 -07002094
2095 public synchronized ProxyProperties getProxy() {
2096 if (mGlobalProxy != null) return mGlobalProxy;
2097 if (mDefaultProxy != null) return mDefaultProxy;
2098 return null;
2099 }
2100
2101 public void setGlobalProxy(ProxyProperties proxyProperties) {
2102 enforceChangePermission();
2103 synchronized (mGlobalProxyLock) {
2104 if (proxyProperties == mGlobalProxy) return;
2105 if (proxyProperties != null && proxyProperties.equals(mGlobalProxy)) return;
2106 if (mGlobalProxy != null && mGlobalProxy.equals(proxyProperties)) return;
2107
2108 String host = "";
2109 int port = 0;
2110 String exclList = "";
2111 if (proxyProperties != null && !TextUtils.isEmpty(proxyProperties.getHost())) {
2112 mGlobalProxy = new ProxyProperties(proxyProperties);
2113 host = mGlobalProxy.getHost();
2114 port = mGlobalProxy.getPort();
2115 exclList = mGlobalProxy.getExclusionList();
2116 } else {
2117 mGlobalProxy = null;
2118 }
2119 ContentResolver res = mContext.getContentResolver();
2120 Settings.Secure.putString(res, Settings.Secure.GLOBAL_HTTP_PROXY_HOST, host);
2121 Settings.Secure.putInt(res, Settings.Secure.GLOBAL_HTTP_PROXY_PORT, port);
Robert Greenwalt6f7c6092010-12-02 11:31:00 -08002122 Settings.Secure.putString(res, Settings.Secure.GLOBAL_HTTP_PROXY_EXCLUSION_LIST,
Robert Greenwaltc3c5f862010-10-11 16:00:27 -07002123 exclList);
2124 }
2125
2126 if (mGlobalProxy == null) {
2127 proxyProperties = mDefaultProxy;
2128 }
2129 sendProxyBroadcast(proxyProperties);
2130 }
2131
Robert Greenwalt6f7c6092010-12-02 11:31:00 -08002132 private void loadGlobalProxy() {
2133 ContentResolver res = mContext.getContentResolver();
2134 String host = Settings.Secure.getString(res, Settings.Secure.GLOBAL_HTTP_PROXY_HOST);
2135 int port = Settings.Secure.getInt(res, Settings.Secure.GLOBAL_HTTP_PROXY_PORT, 0);
2136 String exclList = Settings.Secure.getString(res,
2137 Settings.Secure.GLOBAL_HTTP_PROXY_EXCLUSION_LIST);
2138 if (!TextUtils.isEmpty(host)) {
2139 ProxyProperties proxyProperties = new ProxyProperties(host, port, exclList);
2140 synchronized (mGlobalProxyLock) {
2141 mGlobalProxy = proxyProperties;
2142 }
2143 }
2144 }
2145
Robert Greenwaltc3c5f862010-10-11 16:00:27 -07002146 public ProxyProperties getGlobalProxy() {
2147 synchronized (mGlobalProxyLock) {
2148 return mGlobalProxy;
2149 }
2150 }
2151
2152 private void handleApplyDefaultProxy(int type) {
2153 // check if new default - push it out to all VM if so
2154 ProxyProperties proxy = mNetTrackers[type].getLinkProperties().getHttpProxy();
2155 synchronized (this) {
2156 if (mDefaultProxy != null && mDefaultProxy.equals(proxy)) return;
2157 if (mDefaultProxy == proxy) return;
2158 if (!TextUtils.isEmpty(proxy.getHost())) {
2159 mDefaultProxy = proxy;
2160 } else {
2161 mDefaultProxy = null;
2162 }
2163 }
Wink Savillee70c6f52010-12-03 12:01:38 -08002164 if (DBG) log("changing default proxy to " + proxy);
Robert Greenwaltc3c5f862010-10-11 16:00:27 -07002165 if ((proxy == null && mGlobalProxy == null) || proxy.equals(mGlobalProxy)) return;
2166 if (mGlobalProxy != null) return;
2167 sendProxyBroadcast(proxy);
2168 }
2169
2170 private void handleDeprecatedGlobalHttpProxy() {
2171 String proxy = Settings.Secure.getString(mContext.getContentResolver(),
2172 Settings.Secure.HTTP_PROXY);
2173 if (!TextUtils.isEmpty(proxy)) {
2174 String data[] = proxy.split(":");
2175 String proxyHost = data[0];
2176 int proxyPort = 8080;
2177 if (data.length > 1) {
2178 try {
2179 proxyPort = Integer.parseInt(data[1]);
2180 } catch (NumberFormatException e) {
2181 return;
2182 }
2183 }
2184 ProxyProperties p = new ProxyProperties(data[0], proxyPort, "");
2185 setGlobalProxy(p);
2186 }
2187 }
2188
2189 private void sendProxyBroadcast(ProxyProperties proxy) {
Robert Greenwalt611291c2010-12-23 15:51:10 -08002190 if (proxy == null) proxy = new ProxyProperties("", 0, "");
Wink Savillee70c6f52010-12-03 12:01:38 -08002191 log("sending Proxy Broadcast for " + proxy);
Robert Greenwaltc3c5f862010-10-11 16:00:27 -07002192 Intent intent = new Intent(Proxy.PROXY_CHANGE_ACTION);
Stan Chesnutt1f2a2ac2011-01-06 11:00:19 -08002193 intent.addFlags(Intent.FLAG_RECEIVER_REPLACE_PENDING |
2194 Intent.FLAG_RECEIVER_REGISTERED_ONLY_BEFORE_BOOT);
Robert Greenwaltc3c5f862010-10-11 16:00:27 -07002195 intent.putExtra(Proxy.EXTRA_PROXY_INFO, proxy);
Robert Greenwaltd93dc8f2010-12-06 11:29:17 -08002196 mContext.sendStickyBroadcast(intent);
Robert Greenwaltc3c5f862010-10-11 16:00:27 -07002197 }
2198
2199 private static class SettingsObserver extends ContentObserver {
2200 private int mWhat;
2201 private Handler mHandler;
2202 SettingsObserver(Handler handler, int what) {
2203 super(handler);
2204 mHandler = handler;
2205 mWhat = what;
2206 }
2207
2208 void observe(Context context) {
2209 ContentResolver resolver = context.getContentResolver();
2210 resolver.registerContentObserver(Settings.Secure.getUriFor(
2211 Settings.Secure.HTTP_PROXY), false, this);
2212 }
2213
2214 @Override
2215 public void onChange(boolean selfChange) {
2216 mHandler.obtainMessage(mWhat).sendToTarget();
2217 }
2218 }
Wink Savillee70c6f52010-12-03 12:01:38 -08002219
Kazuhiro Ondo3a340412011-04-30 20:10:57 -05002220 private void handleTetherIfaceChange(int type) {
2221 String iface = mNetTrackers[type].getLinkProperties().getInterfaceName();
2222
2223 if (isTetheringSupported()) {
2224 mTethering.handleTetherIfaceChange(iface);
2225 }
2226 }
2227
Wink Savillee70c6f52010-12-03 12:01:38 -08002228 private void log(String s) {
2229 Slog.d(TAG, s);
2230 }
2231
2232 private void loge(String s) {
2233 Slog.e(TAG, s);
2234 }
Wink Savillef6b76692011-02-24 17:58:51 -08002235 int convertFeatureToNetworkType(String feature){
2236 int networkType = -1;
2237 if (TextUtils.equals(feature, Phone.FEATURE_ENABLE_MMS)) {
2238 networkType = ConnectivityManager.TYPE_MOBILE_MMS;
2239 } else if (TextUtils.equals(feature, Phone.FEATURE_ENABLE_SUPL)) {
2240 networkType = ConnectivityManager.TYPE_MOBILE_SUPL;
2241 } else if (TextUtils.equals(feature, Phone.FEATURE_ENABLE_DUN) ||
2242 TextUtils.equals(feature, Phone.FEATURE_ENABLE_DUN_ALWAYS)) {
2243 networkType = ConnectivityManager.TYPE_MOBILE_DUN;
2244 } else if (TextUtils.equals(feature, Phone.FEATURE_ENABLE_HIPRI)) {
2245 networkType = ConnectivityManager.TYPE_MOBILE_HIPRI;
2246 } else if (TextUtils.equals(feature, Phone.FEATURE_ENABLE_FOTA)) {
2247 networkType = ConnectivityManager.TYPE_MOBILE_FOTA;
2248 } else if (TextUtils.equals(feature, Phone.FEATURE_ENABLE_IMS)) {
2249 networkType = ConnectivityManager.TYPE_MOBILE_IMS;
2250 } else if (TextUtils.equals(feature, Phone.FEATURE_ENABLE_CBS)) {
2251 networkType = ConnectivityManager.TYPE_MOBILE_CBS;
2252 }
2253 return networkType;
2254 }
The Android Open Source Project28527d22009-03-03 19:31:44 -08002255}