blob: 56b6c54197812367e8fcff59223a9e0b905ef8bd [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 Goby6cec7f32010-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;
The Android Open Source Project28527d22009-03-03 19:31:44 -080044import android.os.Looper;
45import android.os.Message;
Robert Greenwalt93dc1042010-06-15 12:19:37 -070046import android.os.PowerManager;
Robert Greenwalt2034b912009-08-12 16:08:25 -070047import android.os.RemoteException;
The Android Open Source Project28527d22009-03-03 19:31:44 -080048import android.os.ServiceManager;
49import android.os.SystemProperties;
50import android.provider.Settings;
Robert Greenwalt2034b912009-08-12 16:08:25 -070051import android.text.TextUtils;
The Android Open Source Project28527d22009-03-03 19:31:44 -080052import android.util.EventLog;
Joe Onoratoc2386bb2010-02-26 18:56:32 -080053import android.util.Slog;
The Android Open Source Project28527d22009-03-03 19:31:44 -080054
Robert Greenwalt2034b912009-08-12 16:08:25 -070055import com.android.internal.telephony.Phone;
Robert Greenwalt0c4828c2010-01-26 11:40:34 -080056import com.android.server.connectivity.Tethering;
57
The Android Open Source Project28527d22009-03-03 19:31:44 -080058import java.io.FileDescriptor;
Irfan Sheriff7f132d92010-06-09 15:39:36 -070059import java.io.FileWriter;
60import java.io.IOException;
The Android Open Source Project28527d22009-03-03 19:31:44 -080061import java.io.PrintWriter;
Robert Greenwalta7dfbd32010-06-15 15:43:39 -070062import java.net.InetAddress;
63import java.net.UnknownHostException;
Robert Greenwalt2034b912009-08-12 16:08:25 -070064import java.util.ArrayList;
Robert Greenwalta7dfbd32010-06-15 15:43:39 -070065import java.util.Collection;
Robert Greenwaltd62c7002010-12-29 16:15:02 -080066import java.util.concurrent.atomic.AtomicBoolean;
Robert Greenwalt0e80be12010-09-20 14:35:25 -070067import java.util.GregorianCalendar;
Robert Greenwalt2034b912009-08-12 16:08:25 -070068import java.util.List;
The Android Open Source Project28527d22009-03-03 19:31:44 -080069
70/**
71 * @hide
72 */
73public class ConnectivityService extends IConnectivityManager.Stub {
74
Robert Greenwalt063dc7d2010-10-05 19:12:26 -070075 private static final boolean DBG = true;
The Android Open Source Project28527d22009-03-03 19:31:44 -080076 private static final String TAG = "ConnectivityService";
77
Robert Greenwalt2034b912009-08-12 16:08:25 -070078 // how long to wait before switching back to a radio's default network
79 private static final int RESTORE_DEFAULT_NETWORK_DELAY = 1 * 60 * 1000;
80 // system property that can override the above value
81 private static final String NETWORK_RESTORE_DELAY_PROP_NAME =
82 "android.telephony.apn-restore";
83
Robert Greenwalt0c4828c2010-01-26 11:40:34 -080084 private Tethering mTethering;
Robert Greenwaltf1b66e12010-02-25 12:29:30 -080085 private boolean mTetheringConfigValid = false;
Robert Greenwalt0c4828c2010-01-26 11:40:34 -080086
The Android Open Source Project28527d22009-03-03 19:31:44 -080087 /**
88 * Sometimes we want to refer to the individual network state
89 * trackers separately, and sometimes we just want to treat them
90 * abstractly.
91 */
92 private NetworkStateTracker mNetTrackers[];
Robert Greenwalt2034b912009-08-12 16:08:25 -070093
94 /**
95 * A per Net list of the PID's that requested access to the net
96 * used both as a refcount and for per-PID DNS selection
97 */
98 private List mNetRequestersPids[];
99
Irfan Sheriff653e2a22010-06-07 09:03:04 -0700100 private WifiWatchdogService mWifiWatchdogService;
101
Robert Greenwalt2034b912009-08-12 16:08:25 -0700102 // priority order of the nettrackers
103 // (excluding dynamically set mNetworkPreference)
104 // TODO - move mNetworkTypePreference into this
105 private int[] mPriorityList;
106
The Android Open Source Project28527d22009-03-03 19:31:44 -0800107 private Context mContext;
108 private int mNetworkPreference;
Robert Greenwalt2034b912009-08-12 16:08:25 -0700109 private int mActiveDefaultNetwork = -1;
Robert Greenwalt986c7412010-09-08 15:24:47 -0700110 // 0 is full bad, 100 is full good
111 private int mDefaultInetCondition = 0;
112 private int mDefaultInetConditionPublished = 0;
113 private boolean mInetConditionChangeInFlight = false;
114 private int mDefaultConnectionSequence = 0;
The Android Open Source Project28527d22009-03-03 19:31:44 -0800115
116 private int mNumDnsEntries;
The Android Open Source Project28527d22009-03-03 19:31:44 -0800117
118 private boolean mTestMode;
Joe Onorato56023ad2010-09-01 21:18:22 -0700119 private static ConnectivityService sServiceInstance;
The Android Open Source Project28527d22009-03-03 19:31:44 -0800120
Robert Greenwaltd62c7002010-12-29 16:15:02 -0800121 private AtomicBoolean mBackgroundDataEnabled = new AtomicBoolean(true);
122
Robert Greenwalt6a2db8a2010-09-23 10:05:56 -0700123 private static final int ENABLED = 1;
124 private static final int DISABLED = 0;
125
126 // Share the event space with NetworkStateTracker (which can't see this
127 // internal class but sends us events). If you change these, change
128 // NetworkStateTracker.java too.
129 private static final int MIN_NETWORK_STATE_TRACKER_EVENT = 1;
130 private static final int MAX_NETWORK_STATE_TRACKER_EVENT = 100;
131
132 /**
133 * used internally as a delayed event to make us switch back to the
134 * default network
135 */
136 private static final int EVENT_RESTORE_DEFAULT_NETWORK =
137 MAX_NETWORK_STATE_TRACKER_EVENT + 1;
138
139 /**
140 * used internally to change our mobile data enabled flag
141 */
142 private static final int EVENT_CHANGE_MOBILE_DATA_ENABLED =
143 MAX_NETWORK_STATE_TRACKER_EVENT + 2;
144
145 /**
146 * used internally to change our network preference setting
147 * arg1 = networkType to prefer
148 */
149 private static final int EVENT_SET_NETWORK_PREFERENCE =
150 MAX_NETWORK_STATE_TRACKER_EVENT + 3;
151
152 /**
153 * used internally to synchronize inet condition reports
154 * arg1 = networkType
155 * arg2 = condition (0 bad, 100 good)
156 */
157 private static final int EVENT_INET_CONDITION_CHANGE =
158 MAX_NETWORK_STATE_TRACKER_EVENT + 4;
159
160 /**
161 * used internally to mark the end of inet condition hold periods
162 * arg1 = networkType
163 */
164 private static final int EVENT_INET_CONDITION_HOLD_END =
165 MAX_NETWORK_STATE_TRACKER_EVENT + 5;
166
167 /**
168 * used internally to set the background data preference
169 * arg1 = TRUE for enabled, FALSE for disabled
170 */
171 private static final int EVENT_SET_BACKGROUND_DATA =
172 MAX_NETWORK_STATE_TRACKER_EVENT + 6;
173
174 /**
175 * used internally to set enable/disable cellular data
176 * arg1 = ENBALED or DISABLED
177 */
178 private static final int EVENT_SET_MOBILE_DATA =
179 MAX_NETWORK_STATE_TRACKER_EVENT + 7;
180
Robert Greenwaltccb36f92010-09-24 14:32:21 -0700181 /**
182 * used internally to clear a wakelock when transitioning
183 * from one net to another
184 */
185 private static final int EVENT_CLEAR_NET_TRANSITION_WAKELOCK =
186 MAX_NETWORK_STATE_TRACKER_EVENT + 8;
187
Robert Greenwaltc3c5f862010-10-11 16:00:27 -0700188 /**
189 * used internally to reload global proxy settings
190 */
191 private static final int EVENT_APPLY_GLOBAL_HTTP_PROXY =
192 MAX_NETWORK_STATE_TRACKER_EVENT + 9;
193
Robert Greenwalt34848c02011-03-25 13:09:25 -0700194 /**
195 * used internally to set external dependency met/unmet
196 * arg1 = ENABLED (met) or DISABLED (unmet)
197 * arg2 = NetworkType
198 */
199 private static final int EVENT_SET_DEPENDENCY_MET =
200 MAX_NETWORK_STATE_TRACKER_EVENT + 10;
201
Robert Greenwalt2034b912009-08-12 16:08:25 -0700202 private Handler mHandler;
203
204 // list of DeathRecipients used to make sure features are turned off when
205 // a process dies
206 private List mFeatureUsers;
207
Mike Lockwoodfde2b762009-08-14 14:18:49 -0400208 private boolean mSystemReady;
Dianne Hackborna417ff82009-12-08 19:45:14 -0800209 private Intent mInitialBroadcast;
Mike Lockwoodfde2b762009-08-14 14:18:49 -0400210
Robert Greenwalt93dc1042010-06-15 12:19:37 -0700211 private PowerManager.WakeLock mNetTransitionWakeLock;
212 private String mNetTransitionWakeLockCausedBy = "";
213 private int mNetTransitionWakeLockSerialNumber;
214 private int mNetTransitionWakeLockTimeout;
215
Robert Greenwalt94daa182010-09-01 11:34:05 -0700216 private InetAddress mDefaultDns;
217
Robert Greenwalt0e80be12010-09-20 14:35:25 -0700218 // used in DBG mode to track inet condition reports
219 private static final int INET_CONDITION_LOG_MAX_SIZE = 15;
220 private ArrayList mInetLog;
221
Robert Greenwaltc3c5f862010-10-11 16:00:27 -0700222 // track the current default http proxy - tell the world if we get a new one (real change)
223 private ProxyProperties mDefaultProxy = null;
224 // track the global proxy.
225 private ProxyProperties mGlobalProxy = null;
226 private final Object mGlobalProxyLock = new Object();
227
228 private SettingsObserver mSettingsObserver;
229
Robert Greenwalt34848c02011-03-25 13:09:25 -0700230 NetworkConfig[] mNetConfigs;
Robert Greenwaltec05b3c2009-10-30 14:17:42 -0700231 int mNetworksDefined;
Robert Greenwalt2034b912009-08-12 16:08:25 -0700232
Robert Greenwalt12c44552009-12-07 11:33:18 -0800233 private static class RadioAttributes {
Robert Greenwalt2034b912009-08-12 16:08:25 -0700234 public int mSimultaneity;
235 public int mType;
236 public RadioAttributes(String init) {
237 String fragments[] = init.split(",");
Robert Greenwaltec05b3c2009-10-30 14:17:42 -0700238 mType = Integer.parseInt(fragments[0]);
239 mSimultaneity = Integer.parseInt(fragments[1]);
Robert Greenwalt2034b912009-08-12 16:08:25 -0700240 }
241 }
242 RadioAttributes[] mRadioAttributes;
243
Wink Saville775aad62010-09-02 19:23:52 -0700244 public static synchronized ConnectivityService getInstance(Context context) {
245 if (sServiceInstance == null) {
246 sServiceInstance = new ConnectivityService(context);
The Android Open Source Project28527d22009-03-03 19:31:44 -0800247 }
Wink Saville775aad62010-09-02 19:23:52 -0700248 return sServiceInstance;
The Android Open Source Project28527d22009-03-03 19:31:44 -0800249 }
Robert Greenwalt0659da32009-07-16 17:21:39 -0700250
The Android Open Source Project28527d22009-03-03 19:31:44 -0800251 private ConnectivityService(Context context) {
Wink Savillee70c6f52010-12-03 12:01:38 -0800252 if (DBG) log("ConnectivityService starting up");
Robert Greenwaltd48f8ee2010-01-14 17:47:58 -0800253
Wink Saville775aad62010-09-02 19:23:52 -0700254 HandlerThread handlerThread = new HandlerThread("ConnectivityServiceThread");
255 handlerThread.start();
256 mHandler = new MyHandler(handlerThread.getLooper());
257
Robert Greenwaltd62c7002010-12-29 16:15:02 -0800258 mBackgroundDataEnabled.set(Settings.Secure.getInt(context.getContentResolver(),
259 Settings.Secure.BACKGROUND_DATA, 1) == 1);
260
Robert Greenwaltd48f8ee2010-01-14 17:47:58 -0800261 // setup our unique device name
Robert Greenwalt82cde132010-12-06 09:30:17 -0800262 if (TextUtils.isEmpty(SystemProperties.get("net.hostname"))) {
263 String id = Settings.Secure.getString(context.getContentResolver(),
264 Settings.Secure.ANDROID_ID);
265 if (id != null && id.length() > 0) {
266 String name = new String("android_").concat(id);
267 SystemProperties.set("net.hostname", name);
268 }
Robert Greenwaltd48f8ee2010-01-14 17:47:58 -0800269 }
270
Robert Greenwalt94daa182010-09-01 11:34:05 -0700271 // read our default dns server ip
272 String dns = Settings.Secure.getString(context.getContentResolver(),
273 Settings.Secure.DEFAULT_DNS_SERVER);
274 if (dns == null || dns.length() == 0) {
275 dns = context.getResources().getString(
276 com.android.internal.R.string.config_default_dns_server);
277 }
278 try {
Robert Greenwalt35e34d12011-02-22 16:00:42 -0800279 mDefaultDns = NetworkUtils.numericToInetAddress(dns);
280 } catch (IllegalArgumentException e) {
Wink Savillee70c6f52010-12-03 12:01:38 -0800281 loge("Error setting defaultDns using " + dns);
Robert Greenwalt94daa182010-09-01 11:34:05 -0700282 }
283
The Android Open Source Project28527d22009-03-03 19:31:44 -0800284 mContext = context;
Robert Greenwalt93dc1042010-06-15 12:19:37 -0700285
286 PowerManager powerManager = (PowerManager)mContext.getSystemService(Context.POWER_SERVICE);
287 mNetTransitionWakeLock = powerManager.newWakeLock(PowerManager.PARTIAL_WAKE_LOCK, TAG);
288 mNetTransitionWakeLockTimeout = mContext.getResources().getInteger(
289 com.android.internal.R.integer.config_networkTransitionTimeout);
290
Robert Greenwalt2034b912009-08-12 16:08:25 -0700291 mNetTrackers = new NetworkStateTracker[
292 ConnectivityManager.MAX_NETWORK_TYPE+1];
Robert Greenwalt0659da32009-07-16 17:21:39 -0700293
The Android Open Source Project28527d22009-03-03 19:31:44 -0800294 mNetworkPreference = getPersistedNetworkPreference();
Robert Greenwalt0659da32009-07-16 17:21:39 -0700295
Robert Greenwaltec05b3c2009-10-30 14:17:42 -0700296 mRadioAttributes = new RadioAttributes[ConnectivityManager.MAX_RADIO_TYPE+1];
Robert Greenwalt34848c02011-03-25 13:09:25 -0700297 mNetConfigs = new NetworkConfig[ConnectivityManager.MAX_NETWORK_TYPE+1];
Robert Greenwaltec05b3c2009-10-30 14:17:42 -0700298
Robert Greenwalt2034b912009-08-12 16:08:25 -0700299 // Load device network attributes from resources
Robert Greenwalt2034b912009-08-12 16:08:25 -0700300 String[] raStrings = context.getResources().getStringArray(
301 com.android.internal.R.array.radioAttributes);
Robert Greenwaltec05b3c2009-10-30 14:17:42 -0700302 for (String raString : raStrings) {
303 RadioAttributes r = new RadioAttributes(raString);
304 if (r.mType > ConnectivityManager.MAX_RADIO_TYPE) {
Wink Savillee70c6f52010-12-03 12:01:38 -0800305 loge("Error in radioAttributes - ignoring attempt to define type " + r.mType);
Robert Greenwaltec05b3c2009-10-30 14:17:42 -0700306 continue;
307 }
308 if (mRadioAttributes[r.mType] != null) {
Wink Savillee70c6f52010-12-03 12:01:38 -0800309 loge("Error in radioAttributes - ignoring attempt to redefine type " +
Robert Greenwaltec05b3c2009-10-30 14:17:42 -0700310 r.mType);
311 continue;
312 }
Robert Greenwalt2034b912009-08-12 16:08:25 -0700313 mRadioAttributes[r.mType] = r;
314 }
315
Robert Greenwaltec05b3c2009-10-30 14:17:42 -0700316 String[] naStrings = context.getResources().getStringArray(
317 com.android.internal.R.array.networkAttributes);
318 for (String naString : naStrings) {
319 try {
Robert Greenwalt34848c02011-03-25 13:09:25 -0700320 NetworkConfig n = new NetworkConfig(naString);
Wink Savillef2a62832011-04-07 14:23:45 -0700321 if (n.type > ConnectivityManager.MAX_NETWORK_TYPE) {
Wink Savillee70c6f52010-12-03 12:01:38 -0800322 loge("Error in networkAttributes - ignoring attempt to define type " +
Wink Savillef2a62832011-04-07 14:23:45 -0700323 n.type);
Robert Greenwaltec05b3c2009-10-30 14:17:42 -0700324 continue;
Robert Greenwalt2034b912009-08-12 16:08:25 -0700325 }
Wink Savillef2a62832011-04-07 14:23:45 -0700326 if (mNetConfigs[n.type] != null) {
Wink Savillee70c6f52010-12-03 12:01:38 -0800327 loge("Error in networkAttributes - ignoring attempt to redefine type " +
Wink Savillef2a62832011-04-07 14:23:45 -0700328 n.type);
Robert Greenwaltec05b3c2009-10-30 14:17:42 -0700329 continue;
330 }
Wink Savillef2a62832011-04-07 14:23:45 -0700331 if (mRadioAttributes[n.radio] == null) {
Wink Savillee70c6f52010-12-03 12:01:38 -0800332 loge("Error in networkAttributes - ignoring attempt to use undefined " +
Wink Savillef2a62832011-04-07 14:23:45 -0700333 "radio " + n.radio + " in network type " + n.type);
Robert Greenwaltec05b3c2009-10-30 14:17:42 -0700334 continue;
335 }
Wink Savillef2a62832011-04-07 14:23:45 -0700336 mNetConfigs[n.type] = n;
Robert Greenwaltec05b3c2009-10-30 14:17:42 -0700337 mNetworksDefined++;
338 } catch(Exception e) {
339 // ignore it - leave the entry null
Robert Greenwalt2034b912009-08-12 16:08:25 -0700340 }
341 }
342
Robert Greenwaltec05b3c2009-10-30 14:17:42 -0700343 // high priority first
344 mPriorityList = new int[mNetworksDefined];
345 {
346 int insertionPoint = mNetworksDefined-1;
347 int currentLowest = 0;
348 int nextLowest = 0;
349 while (insertionPoint > -1) {
Robert Greenwalt34848c02011-03-25 13:09:25 -0700350 for (NetworkConfig na : mNetConfigs) {
Robert Greenwaltec05b3c2009-10-30 14:17:42 -0700351 if (na == null) continue;
Wink Savillef2a62832011-04-07 14:23:45 -0700352 if (na.priority < currentLowest) continue;
353 if (na.priority > currentLowest) {
354 if (na.priority < nextLowest || nextLowest == 0) {
355 nextLowest = na.priority;
Robert Greenwaltec05b3c2009-10-30 14:17:42 -0700356 }
357 continue;
358 }
Wink Savillef2a62832011-04-07 14:23:45 -0700359 mPriorityList[insertionPoint--] = na.type;
Robert Greenwaltec05b3c2009-10-30 14:17:42 -0700360 }
361 currentLowest = nextLowest;
362 nextLowest = 0;
363 }
364 }
365
366 mNetRequestersPids = new ArrayList[ConnectivityManager.MAX_NETWORK_TYPE+1];
367 for (int i : mPriorityList) {
Robert Greenwalt2034b912009-08-12 16:08:25 -0700368 mNetRequestersPids[i] = new ArrayList();
369 }
370
371 mFeatureUsers = new ArrayList();
372
Robert Greenwaltec05b3c2009-10-30 14:17:42 -0700373 mNumDnsEntries = 0;
374
375 mTestMode = SystemProperties.get("cm.test.mode").equals("true")
376 && SystemProperties.get("ro.build.type").equals("eng");
The Android Open Source Project28527d22009-03-03 19:31:44 -0800377 /*
378 * Create the network state trackers for Wi-Fi and mobile
379 * data. Maybe this could be done with a factory class,
380 * but it's not clear that it's worth it, given that
381 * the number of different network types is not going
382 * to change very often.
383 */
Robert Greenwaltec05b3c2009-10-30 14:17:42 -0700384 for (int netType : mPriorityList) {
Wink Savillef2a62832011-04-07 14:23:45 -0700385 switch (mNetConfigs[netType].radio) {
Robert Greenwaltec05b3c2009-10-30 14:17:42 -0700386 case ConnectivityManager.TYPE_WIFI:
Wink Savillee70c6f52010-12-03 12:01:38 -0800387 if (DBG) log("Starting Wifi Service.");
Wink Saville7fabfa22010-08-13 16:11:42 -0700388 WifiStateTracker wst = new WifiStateTracker();
Irfan Sheriff25be0762010-07-28 09:35:20 -0700389 WifiService wifiService = new WifiService(context);
Robert Greenwaltec05b3c2009-10-30 14:17:42 -0700390 ServiceManager.addService(Context.WIFI_SERVICE, wifiService);
Irfan Sheriff25be0762010-07-28 09:35:20 -0700391 wifiService.checkAndStartWifi();
Robert Greenwaltec05b3c2009-10-30 14:17:42 -0700392 mNetTrackers[ConnectivityManager.TYPE_WIFI] = wst;
Wink Saville7fabfa22010-08-13 16:11:42 -0700393 wst.startMonitoring(context, mHandler);
The Android Open Source Project28527d22009-03-03 19:31:44 -0800394
Irfan Sheriff653e2a22010-06-07 09:03:04 -0700395 //TODO: as part of WWS refactor, create only when needed
Irfan Sheriff25be0762010-07-28 09:35:20 -0700396 mWifiWatchdogService = new WifiWatchdogService(context);
Irfan Sheriff653e2a22010-06-07 09:03:04 -0700397
Robert Greenwaltec05b3c2009-10-30 14:17:42 -0700398 break;
399 case ConnectivityManager.TYPE_MOBILE:
Wink Saville7fabfa22010-08-13 16:11:42 -0700400 mNetTrackers[netType] = new MobileDataStateTracker(netType,
Wink Savillef2a62832011-04-07 14:23:45 -0700401 mNetConfigs[netType].name);
Wink Saville7fabfa22010-08-13 16:11:42 -0700402 mNetTrackers[netType].startMonitoring(context, mHandler);
Robert Greenwaltec05b3c2009-10-30 14:17:42 -0700403 break;
Robert Greenwalteb123ac2010-12-06 13:56:24 -0800404 case ConnectivityManager.TYPE_DUMMY:
405 mNetTrackers[netType] = new DummyDataStateTracker(netType,
Wink Savillef2a62832011-04-07 14:23:45 -0700406 mNetConfigs[netType].name);
Robert Greenwalteb123ac2010-12-06 13:56:24 -0800407 mNetTrackers[netType].startMonitoring(context, mHandler);
408 break;
Jaikumar Ganesh0db51a02010-12-21 22:31:44 -0800409 case ConnectivityManager.TYPE_BLUETOOTH:
410 mNetTrackers[netType] = BluetoothTetheringDataTracker.getInstance();
411 mNetTrackers[netType].startMonitoring(context, mHandler);
412 break;
Benoit Goby6cec7f32010-12-22 14:29:40 -0800413 case ConnectivityManager.TYPE_ETHERNET:
414 mNetTrackers[netType] = EthernetDataTracker.getInstance();
415 mNetTrackers[netType].startMonitoring(context, mHandler);
416 break;
Robert Greenwaltec05b3c2009-10-30 14:17:42 -0700417 default:
Wink Savillee70c6f52010-12-03 12:01:38 -0800418 loge("Trying to create a DataStateTracker for an unknown radio type " +
Wink Savillef2a62832011-04-07 14:23:45 -0700419 mNetConfigs[netType].radio);
Robert Greenwaltec05b3c2009-10-30 14:17:42 -0700420 continue;
421 }
422 }
Robert Greenwalt0c4828c2010-01-26 11:40:34 -0800423
Robert Greenwaltc0b6c602010-03-11 15:03:08 -0800424 mTethering = new Tethering(mContext, mHandler.getLooper());
Robert Greenwaltf1b66e12010-02-25 12:29:30 -0800425 mTetheringConfigValid = (((mNetTrackers[ConnectivityManager.TYPE_MOBILE_DUN] != null) ||
426 !mTethering.isDunRequired()) &&
427 (mTethering.getTetherableUsbRegexs().length != 0 ||
Danica Chang96567052010-08-11 14:54:43 -0700428 mTethering.getTetherableWifiRegexs().length != 0 ||
429 mTethering.getTetherableBluetoothRegexs().length != 0) &&
Robert Greenwaltf1b66e12010-02-25 12:29:30 -0800430 mTethering.getUpstreamIfaceRegexs().length != 0);
431
Robert Greenwalt0e80be12010-09-20 14:35:25 -0700432 if (DBG) {
433 mInetLog = new ArrayList();
434 }
Robert Greenwaltc3c5f862010-10-11 16:00:27 -0700435
436 mSettingsObserver = new SettingsObserver(mHandler, EVENT_APPLY_GLOBAL_HTTP_PROXY);
437 mSettingsObserver.observe(mContext);
Robert Greenwalt6f7c6092010-12-02 11:31:00 -0800438
439 loadGlobalProxy();
Hung-ying Tyan4e723422011-01-19 16:48:38 +0800440
441 VpnManager.startVpnService(context);
The Android Open Source Project28527d22009-03-03 19:31:44 -0800442 }
443
Robert Greenwaltec05b3c2009-10-30 14:17:42 -0700444
The Android Open Source Project28527d22009-03-03 19:31:44 -0800445 /**
Robert Greenwalt0659da32009-07-16 17:21:39 -0700446 * Sets the preferred network.
The Android Open Source Project28527d22009-03-03 19:31:44 -0800447 * @param preference the new preference
448 */
Robert Greenwalt6a2db8a2010-09-23 10:05:56 -0700449 public void setNetworkPreference(int preference) {
The Android Open Source Project28527d22009-03-03 19:31:44 -0800450 enforceChangePermission();
Robert Greenwalt6a2db8a2010-09-23 10:05:56 -0700451
452 mHandler.sendMessage(mHandler.obtainMessage(EVENT_SET_NETWORK_PREFERENCE, preference, 0));
The Android Open Source Project28527d22009-03-03 19:31:44 -0800453 }
454
455 public int getNetworkPreference() {
456 enforceAccessPermission();
Robert Greenwalt6a2db8a2010-09-23 10:05:56 -0700457 int preference;
458 synchronized(this) {
459 preference = mNetworkPreference;
460 }
461 return preference;
The Android Open Source Project28527d22009-03-03 19:31:44 -0800462 }
463
Robert Greenwalt6a2db8a2010-09-23 10:05:56 -0700464 private void handleSetNetworkPreference(int preference) {
465 if (ConnectivityManager.isNetworkTypeValid(preference) &&
Robert Greenwalt34848c02011-03-25 13:09:25 -0700466 mNetConfigs[preference] != null &&
467 mNetConfigs[preference].isDefault()) {
Robert Greenwalt6a2db8a2010-09-23 10:05:56 -0700468 if (mNetworkPreference != preference) {
469 final ContentResolver cr = mContext.getContentResolver();
470 Settings.Secure.putInt(cr, Settings.Secure.NETWORK_PREFERENCE, preference);
471 synchronized(this) {
472 mNetworkPreference = preference;
473 }
474 enforcePreference();
475 }
476 }
The Android Open Source Project28527d22009-03-03 19:31:44 -0800477 }
Robert Greenwalt0659da32009-07-16 17:21:39 -0700478
The Android Open Source Project28527d22009-03-03 19:31:44 -0800479 private int getPersistedNetworkPreference() {
480 final ContentResolver cr = mContext.getContentResolver();
481
482 final int networkPrefSetting = Settings.Secure
483 .getInt(cr, Settings.Secure.NETWORK_PREFERENCE, -1);
484 if (networkPrefSetting != -1) {
485 return networkPrefSetting;
486 }
487
488 return ConnectivityManager.DEFAULT_NETWORK_PREFERENCE;
489 }
Robert Greenwalt0659da32009-07-16 17:21:39 -0700490
The Android Open Source Project28527d22009-03-03 19:31:44 -0800491 /**
Robert Greenwalt0659da32009-07-16 17:21:39 -0700492 * Make the state of network connectivity conform to the preference settings
The Android Open Source Project28527d22009-03-03 19:31:44 -0800493 * In this method, we only tear down a non-preferred network. Establishing
494 * a connection to the preferred network is taken care of when we handle
495 * the disconnect event from the non-preferred network
496 * (see {@link #handleDisconnect(NetworkInfo)}).
497 */
498 private void enforcePreference() {
Robert Greenwalt2034b912009-08-12 16:08:25 -0700499 if (mNetTrackers[mNetworkPreference].getNetworkInfo().isConnected())
The Android Open Source Project28527d22009-03-03 19:31:44 -0800500 return;
501
Robert Greenwalt2034b912009-08-12 16:08:25 -0700502 if (!mNetTrackers[mNetworkPreference].isAvailable())
503 return;
The Android Open Source Project28527d22009-03-03 19:31:44 -0800504
Robert Greenwalt2034b912009-08-12 16:08:25 -0700505 for (int t=0; t <= ConnectivityManager.MAX_RADIO_TYPE; t++) {
Robert Greenwaltec05b3c2009-10-30 14:17:42 -0700506 if (t != mNetworkPreference && mNetTrackers[t] != null &&
Robert Greenwalt2034b912009-08-12 16:08:25 -0700507 mNetTrackers[t].getNetworkInfo().isConnected()) {
Robert Greenwaltf3f045b2009-08-20 15:25:14 -0700508 if (DBG) {
Wink Savillee70c6f52010-12-03 12:01:38 -0800509 log("tearing down " + mNetTrackers[t].getNetworkInfo() +
Robert Greenwaltf3f045b2009-08-20 15:25:14 -0700510 " in enforcePreference");
511 }
Robert Greenwalt2034b912009-08-12 16:08:25 -0700512 teardown(mNetTrackers[t]);
The Android Open Source Project28527d22009-03-03 19:31:44 -0800513 }
514 }
515 }
516
517 private boolean teardown(NetworkStateTracker netTracker) {
518 if (netTracker.teardown()) {
519 netTracker.setTeardownRequested(true);
520 return true;
521 } else {
522 return false;
523 }
524 }
525
526 /**
527 * Return NetworkInfo for the active (i.e., connected) network interface.
528 * It is assumed that at most one network is active at a time. If more
529 * than one is active, it is indeterminate which will be returned.
Robert Greenwalt0659da32009-07-16 17:21:39 -0700530 * @return the info for the active network, or {@code null} if none is
531 * active
The Android Open Source Project28527d22009-03-03 19:31:44 -0800532 */
533 public NetworkInfo getActiveNetworkInfo() {
534 enforceAccessPermission();
Robert Greenwalta71471b2011-03-24 21:41:41 -0700535 if (mActiveDefaultNetwork != -1) {
536 return mNetTrackers[mActiveDefaultNetwork].getNetworkInfo();
The Android Open Source Project28527d22009-03-03 19:31:44 -0800537 }
538 return null;
539 }
540
541 public NetworkInfo getNetworkInfo(int networkType) {
542 enforceAccessPermission();
543 if (ConnectivityManager.isNetworkTypeValid(networkType)) {
544 NetworkStateTracker t = mNetTrackers[networkType];
545 if (t != null)
546 return t.getNetworkInfo();
547 }
548 return null;
549 }
550
551 public NetworkInfo[] getAllNetworkInfo() {
552 enforceAccessPermission();
Robert Greenwaltec05b3c2009-10-30 14:17:42 -0700553 NetworkInfo[] result = new NetworkInfo[mNetworksDefined];
The Android Open Source Project28527d22009-03-03 19:31:44 -0800554 int i = 0;
555 for (NetworkStateTracker t : mNetTrackers) {
Robert Greenwaltec05b3c2009-10-30 14:17:42 -0700556 if(t != null) result[i++] = t.getNetworkInfo();
The Android Open Source Project28527d22009-03-03 19:31:44 -0800557 }
558 return result;
559 }
560
Robert Greenwalt9f0ee4f2010-09-14 09:18:02 -0700561 /**
562 * Return LinkProperties for the active (i.e., connected) default
563 * network interface. It is assumed that at most one default network
564 * is active at a time. If more than one is active, it is indeterminate
565 * which will be returned.
566 * @return the ip properties for the active network, or {@code null} if
567 * none is active
568 */
569 public LinkProperties getActiveLinkProperties() {
570 enforceAccessPermission();
571 for (int type=0; type <= ConnectivityManager.MAX_NETWORK_TYPE; type++) {
Robert Greenwalt34848c02011-03-25 13:09:25 -0700572 if (mNetConfigs[type] == null || !mNetConfigs[type].isDefault()) {
Robert Greenwalt9f0ee4f2010-09-14 09:18:02 -0700573 continue;
574 }
575 NetworkStateTracker t = mNetTrackers[type];
576 NetworkInfo info = t.getNetworkInfo();
577 if (info.isConnected()) {
578 return t.getLinkProperties();
579 }
580 }
581 return null;
582 }
583
584 public LinkProperties getLinkProperties(int networkType) {
585 enforceAccessPermission();
586 if (ConnectivityManager.isNetworkTypeValid(networkType)) {
587 NetworkStateTracker t = mNetTrackers[networkType];
588 if (t != null) return t.getLinkProperties();
589 }
590 return null;
591 }
592
The Android Open Source Project28527d22009-03-03 19:31:44 -0800593 public boolean setRadios(boolean turnOn) {
594 boolean result = true;
595 enforceChangePermission();
596 for (NetworkStateTracker t : mNetTrackers) {
Robert Greenwaltec05b3c2009-10-30 14:17:42 -0700597 if (t != null) result = t.setRadio(turnOn) && result;
The Android Open Source Project28527d22009-03-03 19:31:44 -0800598 }
599 return result;
600 }
601
602 public boolean setRadio(int netType, boolean turnOn) {
603 enforceChangePermission();
604 if (!ConnectivityManager.isNetworkTypeValid(netType)) {
605 return false;
606 }
607 NetworkStateTracker tracker = mNetTrackers[netType];
608 return tracker != null && tracker.setRadio(turnOn);
609 }
610
Robert Greenwaltaffc3a12009-09-27 17:27:04 -0700611 /**
612 * Used to notice when the calling process dies so we can self-expire
613 *
614 * Also used to know if the process has cleaned up after itself when
615 * our auto-expire timer goes off. The timer has a link to an object.
616 *
617 */
Robert Greenwalt2034b912009-08-12 16:08:25 -0700618 private class FeatureUser implements IBinder.DeathRecipient {
619 int mNetworkType;
620 String mFeature;
621 IBinder mBinder;
622 int mPid;
623 int mUid;
Robert Greenwalt3eeb6032009-12-21 18:24:07 -0800624 long mCreateTime;
Robert Greenwalt2034b912009-08-12 16:08:25 -0700625
626 FeatureUser(int type, String feature, IBinder binder) {
627 super();
628 mNetworkType = type;
629 mFeature = feature;
630 mBinder = binder;
631 mPid = getCallingPid();
632 mUid = getCallingUid();
Robert Greenwalt3eeb6032009-12-21 18:24:07 -0800633 mCreateTime = System.currentTimeMillis();
Robert Greenwaltaffc3a12009-09-27 17:27:04 -0700634
Robert Greenwalt2034b912009-08-12 16:08:25 -0700635 try {
636 mBinder.linkToDeath(this, 0);
637 } catch (RemoteException e) {
638 binderDied();
639 }
640 }
641
642 void unlinkDeathRecipient() {
643 mBinder.unlinkToDeath(this, 0);
644 }
645
646 public void binderDied() {
Wink Savillee70c6f52010-12-03 12:01:38 -0800647 log("ConnectivityService FeatureUser binderDied(" +
Robert Greenwalt3eeb6032009-12-21 18:24:07 -0800648 mNetworkType + ", " + mFeature + ", " + mBinder + "), created " +
649 (System.currentTimeMillis() - mCreateTime) + " mSec ago");
Robert Greenwaltaffc3a12009-09-27 17:27:04 -0700650 stopUsingNetworkFeature(this, false);
Robert Greenwalt2034b912009-08-12 16:08:25 -0700651 }
652
Robert Greenwaltaffc3a12009-09-27 17:27:04 -0700653 public void expire() {
Wink Savillee70c6f52010-12-03 12:01:38 -0800654 log("ConnectivityService FeatureUser expire(" +
Robert Greenwalt3eeb6032009-12-21 18:24:07 -0800655 mNetworkType + ", " + mFeature + ", " + mBinder +"), created " +
656 (System.currentTimeMillis() - mCreateTime) + " mSec ago");
Robert Greenwaltaffc3a12009-09-27 17:27:04 -0700657 stopUsingNetworkFeature(this, false);
658 }
Robert Greenwalt3eeb6032009-12-21 18:24:07 -0800659
660 public String toString() {
661 return "FeatureUser("+mNetworkType+","+mFeature+","+mPid+","+mUid+"), created " +
662 (System.currentTimeMillis() - mCreateTime) + " mSec ago";
663 }
Robert Greenwalt2034b912009-08-12 16:08:25 -0700664 }
665
Robert Greenwaltaffc3a12009-09-27 17:27:04 -0700666 // javadoc from interface
Robert Greenwalt2034b912009-08-12 16:08:25 -0700667 public int startUsingNetworkFeature(int networkType, String feature,
668 IBinder binder) {
669 if (DBG) {
Wink Savillee70c6f52010-12-03 12:01:38 -0800670 log("startUsingNetworkFeature for net " + networkType + ": " + feature);
Robert Greenwalt2034b912009-08-12 16:08:25 -0700671 }
The Android Open Source Project28527d22009-03-03 19:31:44 -0800672 enforceChangePermission();
Robert Greenwaltec05b3c2009-10-30 14:17:42 -0700673 if (!ConnectivityManager.isNetworkTypeValid(networkType) ||
Robert Greenwalt34848c02011-03-25 13:09:25 -0700674 mNetConfigs[networkType] == null) {
Robert Greenwalt2034b912009-08-12 16:08:25 -0700675 return Phone.APN_REQUEST_FAILED;
The Android Open Source Project28527d22009-03-03 19:31:44 -0800676 }
Robert Greenwalt2034b912009-08-12 16:08:25 -0700677
Robert Greenwaltaffc3a12009-09-27 17:27:04 -0700678 FeatureUser f = new FeatureUser(networkType, feature, binder);
Robert Greenwalt2034b912009-08-12 16:08:25 -0700679
680 // TODO - move this into the MobileDataStateTracker
681 int usedNetworkType = networkType;
682 if(networkType == ConnectivityManager.TYPE_MOBILE) {
Wink Savilleb7c92c72011-03-12 14:52:01 -0800683 usedNetworkType = convertFeatureToNetworkType(feature);
684 if (usedNetworkType < 0) {
685 Slog.e(TAG, "Can't match any netTracker!");
686 usedNetworkType = networkType;
Robert Greenwalt2034b912009-08-12 16:08:25 -0700687 }
688 }
689 NetworkStateTracker network = mNetTrackers[usedNetworkType];
690 if (network != null) {
Robert Greenwalt5364d752010-12-15 13:26:33 -0800691 Integer currentPid = new Integer(getCallingPid());
Robert Greenwalt2034b912009-08-12 16:08:25 -0700692 if (usedNetworkType != networkType) {
Robert Greenwalt2034b912009-08-12 16:08:25 -0700693 NetworkStateTracker radio = mNetTrackers[networkType];
694 NetworkInfo ni = network.getNetworkInfo();
695
696 if (ni.isAvailable() == false) {
Wink Savillee70c6f52010-12-03 12:01:38 -0800697 if (DBG) log("special network not available");
Robert Greenwalt2cc87442010-12-29 14:35:21 -0800698 if (!TextUtils.equals(feature,Phone.FEATURE_ENABLE_DUN_ALWAYS)) {
699 return Phone.APN_TYPE_NOT_AVAILABLE;
700 } else {
701 // else make the attempt anyway - probably giving REQUEST_STARTED below
702 }
Robert Greenwalt2034b912009-08-12 16:08:25 -0700703 }
704
Robert Greenwaltaffc3a12009-09-27 17:27:04 -0700705 synchronized(this) {
706 mFeatureUsers.add(f);
707 if (!mNetRequestersPids[usedNetworkType].contains(currentPid)) {
708 // this gets used for per-pid dns when connected
709 mNetRequestersPids[usedNetworkType].add(currentPid);
710 }
Robert Greenwalt2034b912009-08-12 16:08:25 -0700711 }
Robert Greenwalt6a2db8a2010-09-23 10:05:56 -0700712 mHandler.sendMessageDelayed(mHandler.obtainMessage(EVENT_RESTORE_DEFAULT_NETWORK,
Robert Greenwaltaffc3a12009-09-27 17:27:04 -0700713 f), getRestoreDefaultNetworkDelay());
714
Robert Greenwalt2034b912009-08-12 16:08:25 -0700715
Robert Greenwalta52c75a2009-08-19 20:19:33 -0700716 if ((ni.isConnectedOrConnecting() == true) &&
717 !network.isTeardownRequested()) {
Robert Greenwalt2034b912009-08-12 16:08:25 -0700718 if (ni.isConnected() == true) {
719 // add the pid-specific dns
Robert Greenwalt3afbead2010-07-23 15:46:26 -0700720 handleDnsConfigurationChange(networkType);
Wink Savillee70c6f52010-12-03 12:01:38 -0800721 if (DBG) log("special network already active");
Robert Greenwalt2034b912009-08-12 16:08:25 -0700722 return Phone.APN_ALREADY_ACTIVE;
723 }
Wink Savillee70c6f52010-12-03 12:01:38 -0800724 if (DBG) log("special network already connecting");
Robert Greenwalt2034b912009-08-12 16:08:25 -0700725 return Phone.APN_REQUEST_STARTED;
726 }
727
728 // check if the radio in play can make another contact
729 // assume if cannot for now
730
Wink Savillee70c6f52010-12-03 12:01:38 -0800731 if (DBG) log("reconnecting to special network");
Robert Greenwalt2034b912009-08-12 16:08:25 -0700732 network.reconnect();
733 return Phone.APN_REQUEST_STARTED;
734 } else {
Robert Greenwalt5364d752010-12-15 13:26:33 -0800735 // need to remember this unsupported request so we respond appropriately on stop
736 synchronized(this) {
737 mFeatureUsers.add(f);
738 if (!mNetRequestersPids[usedNetworkType].contains(currentPid)) {
739 // this gets used for per-pid dns when connected
740 mNetRequestersPids[usedNetworkType].add(currentPid);
741 }
742 }
Robert Greenwaltd391e892010-05-18 10:52:51 -0700743 return -1;
Robert Greenwalt2034b912009-08-12 16:08:25 -0700744 }
745 }
746 return Phone.APN_TYPE_NOT_AVAILABLE;
The Android Open Source Project28527d22009-03-03 19:31:44 -0800747 }
748
Robert Greenwaltaffc3a12009-09-27 17:27:04 -0700749 // javadoc from interface
The Android Open Source Project28527d22009-03-03 19:31:44 -0800750 public int stopUsingNetworkFeature(int networkType, String feature) {
Robert Greenwalt28f43012009-10-06 17:52:40 -0700751 enforceChangePermission();
752
Robert Greenwaltaffc3a12009-09-27 17:27:04 -0700753 int pid = getCallingPid();
754 int uid = getCallingUid();
755
756 FeatureUser u = null;
757 boolean found = false;
758
759 synchronized(this) {
760 for (int i = 0; i < mFeatureUsers.size() ; i++) {
761 u = (FeatureUser)mFeatureUsers.get(i);
762 if (uid == u.mUid && pid == u.mPid &&
763 networkType == u.mNetworkType &&
764 TextUtils.equals(feature, u.mFeature)) {
765 found = true;
766 break;
767 }
768 }
769 }
770 if (found && u != null) {
771 // stop regardless of how many other time this proc had called start
772 return stopUsingNetworkFeature(u, true);
773 } else {
774 // none found!
Wink Savillee70c6f52010-12-03 12:01:38 -0800775 if (DBG) log("ignoring stopUsingNetworkFeature - not a live request");
Robert Greenwaltaffc3a12009-09-27 17:27:04 -0700776 return 1;
777 }
Robert Greenwalt2034b912009-08-12 16:08:25 -0700778 }
779
Robert Greenwaltaffc3a12009-09-27 17:27:04 -0700780 private int stopUsingNetworkFeature(FeatureUser u, boolean ignoreDups) {
781 int networkType = u.mNetworkType;
782 String feature = u.mFeature;
783 int pid = u.mPid;
784 int uid = u.mUid;
785
786 NetworkStateTracker tracker = null;
787 boolean callTeardown = false; // used to carry our decision outside of sync block
788
Robert Greenwalt2034b912009-08-12 16:08:25 -0700789 if (DBG) {
Wink Savillee70c6f52010-12-03 12:01:38 -0800790 log("stopUsingNetworkFeature for net " + networkType +
Robert Greenwalt2034b912009-08-12 16:08:25 -0700791 ": " + feature);
792 }
Robert Greenwalt28f43012009-10-06 17:52:40 -0700793
The Android Open Source Project28527d22009-03-03 19:31:44 -0800794 if (!ConnectivityManager.isNetworkTypeValid(networkType)) {
795 return -1;
796 }
Robert Greenwalt2034b912009-08-12 16:08:25 -0700797
Robert Greenwaltaffc3a12009-09-27 17:27:04 -0700798 // need to link the mFeatureUsers list with the mNetRequestersPids state in this
799 // sync block
800 synchronized(this) {
801 // check if this process still has an outstanding start request
802 if (!mFeatureUsers.contains(u)) {
Wink Savillee70c6f52010-12-03 12:01:38 -0800803 if (DBG) log("ignoring - this process has no outstanding requests");
Robert Greenwalt2034b912009-08-12 16:08:25 -0700804 return 1;
805 }
Robert Greenwaltaffc3a12009-09-27 17:27:04 -0700806 u.unlinkDeathRecipient();
807 mFeatureUsers.remove(mFeatureUsers.indexOf(u));
808 // If we care about duplicate requests, check for that here.
809 //
810 // This is done to support the extension of a request - the app
811 // can request we start the network feature again and renew the
812 // auto-shutoff delay. Normal "stop" calls from the app though
813 // do not pay attention to duplicate requests - in effect the
814 // API does not refcount and a single stop will counter multiple starts.
815 if (ignoreDups == false) {
816 for (int i = 0; i < mFeatureUsers.size() ; i++) {
817 FeatureUser x = (FeatureUser)mFeatureUsers.get(i);
818 if (x.mUid == u.mUid && x.mPid == u.mPid &&
819 x.mNetworkType == u.mNetworkType &&
820 TextUtils.equals(x.mFeature, u.mFeature)) {
Wink Savillee70c6f52010-12-03 12:01:38 -0800821 if (DBG) log("ignoring stopUsingNetworkFeature as dup is found");
Robert Greenwaltaffc3a12009-09-27 17:27:04 -0700822 return 1;
823 }
824 }
Robert Greenwalt2034b912009-08-12 16:08:25 -0700825 }
Robert Greenwaltaffc3a12009-09-27 17:27:04 -0700826
827 // TODO - move to MobileDataStateTracker
828 int usedNetworkType = networkType;
829 if (networkType == ConnectivityManager.TYPE_MOBILE) {
Wink Savilleb7c92c72011-03-12 14:52:01 -0800830 usedNetworkType = convertFeatureToNetworkType(feature);
831 if (usedNetworkType < 0) {
832 usedNetworkType = networkType;
Robert Greenwaltaffc3a12009-09-27 17:27:04 -0700833 }
834 }
835 tracker = mNetTrackers[usedNetworkType];
Robert Greenwaltec05b3c2009-10-30 14:17:42 -0700836 if (tracker == null) {
Wink Savillee70c6f52010-12-03 12:01:38 -0800837 if (DBG) log("ignoring - no known tracker for net type " + usedNetworkType);
Robert Greenwaltec05b3c2009-10-30 14:17:42 -0700838 return -1;
839 }
840 if (usedNetworkType != networkType) {
Robert Greenwaltaffc3a12009-09-27 17:27:04 -0700841 Integer currentPid = new Integer(pid);
Robert Greenwaltaffc3a12009-09-27 17:27:04 -0700842 mNetRequestersPids[usedNetworkType].remove(currentPid);
Robert Greenwalt0ca68a02009-12-17 14:54:59 -0800843 reassessPidDns(pid, true);
Robert Greenwaltaffc3a12009-09-27 17:27:04 -0700844 if (mNetRequestersPids[usedNetworkType].size() != 0) {
Wink Savillee70c6f52010-12-03 12:01:38 -0800845 if (DBG) log("not tearing down special network - " +
Robert Greenwaltaffc3a12009-09-27 17:27:04 -0700846 "others still using it");
847 return 1;
848 }
849 callTeardown = true;
Robert Greenwalt9f3be4c2011-01-10 11:58:31 -0800850 } else {
851 if (DBG) log("not a known feature - dropping");
Robert Greenwaltaffc3a12009-09-27 17:27:04 -0700852 }
853 }
Wink Savillee70c6f52010-12-03 12:01:38 -0800854 if (DBG) log("Doing network teardown");
Robert Greenwaltaffc3a12009-09-27 17:27:04 -0700855 if (callTeardown) {
856 tracker.teardown();
Robert Greenwalt2034b912009-08-12 16:08:25 -0700857 return 1;
858 } else {
Robert Greenwaltd391e892010-05-18 10:52:51 -0700859 return -1;
Robert Greenwalt2034b912009-08-12 16:08:25 -0700860 }
The Android Open Source Project28527d22009-03-03 19:31:44 -0800861 }
862
863 /**
Robert Greenwalt7fe44cb2010-08-27 09:24:29 -0700864 * @deprecated use requestRouteToHostAddress instead
865 *
The Android Open Source Project28527d22009-03-03 19:31:44 -0800866 * Ensure that a network route exists to deliver traffic to the specified
867 * host via the specified network interface.
Robert Greenwalt0659da32009-07-16 17:21:39 -0700868 * @param networkType the type of the network over which traffic to the
869 * specified host is to be routed
870 * @param hostAddress the IP address of the host to which the route is
871 * desired
The Android Open Source Project28527d22009-03-03 19:31:44 -0800872 * @return {@code true} on success, {@code false} on failure
873 */
874 public boolean requestRouteToHost(int networkType, int hostAddress) {
Robert Greenwalt7fe44cb2010-08-27 09:24:29 -0700875 InetAddress inetAddress = NetworkUtils.intToInetAddress(hostAddress);
876
877 if (inetAddress == null) {
878 return false;
879 }
880
881 return requestRouteToHostAddress(networkType, inetAddress.getAddress());
882 }
883
884 /**
885 * Ensure that a network route exists to deliver traffic to the specified
886 * host via the specified network interface.
887 * @param networkType the type of the network over which traffic to the
888 * specified host is to be routed
889 * @param hostAddress the IP address of the host to which the route is
890 * desired
891 * @return {@code true} on success, {@code false} on failure
892 */
893 public boolean requestRouteToHostAddress(int networkType, byte[] hostAddress) {
The Android Open Source Project28527d22009-03-03 19:31:44 -0800894 enforceChangePermission();
895 if (!ConnectivityManager.isNetworkTypeValid(networkType)) {
896 return false;
897 }
898 NetworkStateTracker tracker = mNetTrackers[networkType];
Robert Greenwalt4666ed02009-09-10 15:06:20 -0700899
Robert Greenwaltec05b3c2009-10-30 14:17:42 -0700900 if (tracker == null || !tracker.getNetworkInfo().isConnected() ||
901 tracker.isTeardownRequested()) {
Robert Greenwalt4666ed02009-09-10 15:06:20 -0700902 if (DBG) {
Wink Savillee70c6f52010-12-03 12:01:38 -0800903 log("requestRouteToHostAddress on down network " +
Robert Greenwalt7fe44cb2010-08-27 09:24:29 -0700904 "(" + networkType + ") - dropped");
Robert Greenwalt4666ed02009-09-10 15:06:20 -0700905 }
906 return false;
The Android Open Source Project28527d22009-03-03 19:31:44 -0800907 }
Robert Greenwalta7dfbd32010-06-15 15:43:39 -0700908 try {
Robert Greenwalt7fe44cb2010-08-27 09:24:29 -0700909 InetAddress addr = InetAddress.getByAddress(hostAddress);
Robert Greenwalta7dfbd32010-06-15 15:43:39 -0700910 return addHostRoute(tracker, addr);
911 } catch (UnknownHostException e) {}
912 return false;
Irfan Sheriff7f132d92010-06-09 15:39:36 -0700913 }
914
915 /**
916 * Ensure that a network route exists to deliver traffic to the specified
917 * host via the mobile data network.
918 * @param hostAddress the IP address of the host to which the route is desired,
919 * in network byte order.
Robert Greenwalta7dfbd32010-06-15 15:43:39 -0700920 * TODO - deprecate
Irfan Sheriff7f132d92010-06-09 15:39:36 -0700921 * @return {@code true} on success, {@code false} on failure
922 */
Robert Greenwalta7dfbd32010-06-15 15:43:39 -0700923 private boolean addHostRoute(NetworkStateTracker nt, InetAddress hostAddress) {
Irfan Sheriff7f132d92010-06-09 15:39:36 -0700924 if (nt.getNetworkInfo().getType() == ConnectivityManager.TYPE_WIFI) {
925 return false;
926 }
927
Robert Greenwalt1f1bcfe2010-08-30 10:56:47 -0700928 LinkProperties p = nt.getLinkProperties();
Robert Greenwalta7dfbd32010-06-15 15:43:39 -0700929 if (p == null) return false;
930 String interfaceName = p.getInterfaceName();
Irfan Sheriff7f132d92010-06-09 15:39:36 -0700931
932 if (DBG) {
Wink Savillee70c6f52010-12-03 12:01:38 -0800933 log("Requested host route to " + hostAddress + "(" + interfaceName + ")");
Irfan Sheriff7f132d92010-06-09 15:39:36 -0700934 }
Robert Greenwalta7dfbd32010-06-15 15:43:39 -0700935 if (interfaceName != null) {
Robert Greenwalt7fe44cb2010-08-27 09:24:29 -0700936 return NetworkUtils.addHostRoute(interfaceName, hostAddress, null);
Irfan Sheriff7f132d92010-06-09 15:39:36 -0700937 } else {
Wink Savillee70c6f52010-12-03 12:01:38 -0800938 if (DBG) loge("addHostRoute failed due to null interface name");
Irfan Sheriff7f132d92010-06-09 15:39:36 -0700939 return false;
940 }
The Android Open Source Project28527d22009-03-03 19:31:44 -0800941 }
942
943 /**
944 * @see ConnectivityManager#getBackgroundDataSetting()
945 */
946 public boolean getBackgroundDataSetting() {
Robert Greenwaltd62c7002010-12-29 16:15:02 -0800947 return mBackgroundDataEnabled.get();
The Android Open Source Project28527d22009-03-03 19:31:44 -0800948 }
Robert Greenwalt0659da32009-07-16 17:21:39 -0700949
The Android Open Source Project28527d22009-03-03 19:31:44 -0800950 /**
951 * @see ConnectivityManager#setBackgroundDataSetting(boolean)
952 */
953 public void setBackgroundDataSetting(boolean allowBackgroundDataUsage) {
954 mContext.enforceCallingOrSelfPermission(
955 android.Manifest.permission.CHANGE_BACKGROUND_DATA_SETTING,
956 "ConnectivityService");
Robert Greenwalt0659da32009-07-16 17:21:39 -0700957
Robert Greenwaltd62c7002010-12-29 16:15:02 -0800958 mBackgroundDataEnabled.set(allowBackgroundDataUsage);
959
Robert Greenwalt6a2db8a2010-09-23 10:05:56 -0700960 mHandler.sendMessage(mHandler.obtainMessage(EVENT_SET_BACKGROUND_DATA,
961 (allowBackgroundDataUsage ? ENABLED : DISABLED), 0));
962 }
The Android Open Source Project28527d22009-03-03 19:31:44 -0800963
Robert Greenwalt6a2db8a2010-09-23 10:05:56 -0700964 private void handleSetBackgroundData(boolean enabled) {
Robert Greenwalt0ffdef12011-02-25 13:44:09 -0800965 Settings.Secure.putInt(mContext.getContentResolver(),
966 Settings.Secure.BACKGROUND_DATA, enabled ? 1 : 0);
967 Intent broadcast = new Intent(
968 ConnectivityManager.ACTION_BACKGROUND_DATA_SETTING_CHANGED);
969 mContext.sendBroadcast(broadcast);
Robert Greenwalt0659da32009-07-16 17:21:39 -0700970 }
971
Robert Greenwalt1b21f6c2010-02-23 18:58:05 -0800972 /**
973 * @see ConnectivityManager#getMobileDataEnabled()
974 */
975 public boolean getMobileDataEnabled() {
Wink Savilleb9024c62010-12-07 10:31:02 -0800976 // TODO: This detail should probably be in DataConnectionTracker's
977 // which is where we store the value and maybe make this
978 // asynchronous.
Robert Greenwalt1b21f6c2010-02-23 18:58:05 -0800979 enforceAccessPermission();
980 boolean retVal = Settings.Secure.getInt(mContext.getContentResolver(),
981 Settings.Secure.MOBILE_DATA, 1) == 1;
Wink Savillee70c6f52010-12-03 12:01:38 -0800982 if (DBG) log("getMobileDataEnabled returning " + retVal);
Robert Greenwalt1b21f6c2010-02-23 18:58:05 -0800983 return retVal;
984 }
985
Robert Greenwalt34848c02011-03-25 13:09:25 -0700986 public void setDataDependency(int networkType, boolean met) {
987 enforceChangePermission();
988 if (DBG) {
989 log("setDataDependency(" + networkType + ", " + met + ")");
990 }
991 mHandler.sendMessage(mHandler.obtainMessage(EVENT_SET_DEPENDENCY_MET,
992 (met ? ENABLED : DISABLED), networkType));
993 }
994
995 private void handleSetDependencyMet(int networkType, boolean met) {
996 if (mNetTrackers[networkType] != null) {
997 if (DBG) {
998 log("handleSetDependencyMet(" + networkType + ", " + met + ")");
999 }
1000 mNetTrackers[networkType].setDependencyMet(met);
1001 }
1002 }
1003
Robert Greenwalt1b21f6c2010-02-23 18:58:05 -08001004 /**
1005 * @see ConnectivityManager#setMobileDataEnabled(boolean)
1006 */
Robert Greenwalt6a2db8a2010-09-23 10:05:56 -07001007 public void setMobileDataEnabled(boolean enabled) {
Robert Greenwalt1b21f6c2010-02-23 18:58:05 -08001008 enforceChangePermission();
Wink Savillee70c6f52010-12-03 12:01:38 -08001009 if (DBG) log("setMobileDataEnabled(" + enabled + ")");
Robert Greenwalt1b21f6c2010-02-23 18:58:05 -08001010
Robert Greenwalt6a2db8a2010-09-23 10:05:56 -07001011 mHandler.sendMessage(mHandler.obtainMessage(EVENT_SET_MOBILE_DATA,
Robert Greenwalt34848c02011-03-25 13:09:25 -07001012 (enabled ? ENABLED : DISABLED), 0));
Robert Greenwalt6a2db8a2010-09-23 10:05:56 -07001013 }
1014
1015 private void handleSetMobileData(boolean enabled) {
Wink Savilleb9024c62010-12-07 10:31:02 -08001016 if (mNetTrackers[ConnectivityManager.TYPE_MOBILE] != null) {
1017 if (DBG) {
1018 Slog.d(TAG, mNetTrackers[ConnectivityManager.TYPE_MOBILE].toString() + enabled);
Robert Greenwalt1b21f6c2010-02-23 18:58:05 -08001019 }
Wink Savilleb9024c62010-12-07 10:31:02 -08001020 mNetTrackers[ConnectivityManager.TYPE_MOBILE].setDataEnable(enabled);
Robert Greenwalt1b21f6c2010-02-23 18:58:05 -08001021 }
1022 }
1023
The Android Open Source Project28527d22009-03-03 19:31:44 -08001024 private void enforceAccessPermission() {
Robert Greenwalt0659da32009-07-16 17:21:39 -07001025 mContext.enforceCallingOrSelfPermission(
1026 android.Manifest.permission.ACCESS_NETWORK_STATE,
1027 "ConnectivityService");
The Android Open Source Project28527d22009-03-03 19:31:44 -08001028 }
1029
1030 private void enforceChangePermission() {
Robert Greenwalt0659da32009-07-16 17:21:39 -07001031 mContext.enforceCallingOrSelfPermission(
1032 android.Manifest.permission.CHANGE_NETWORK_STATE,
1033 "ConnectivityService");
The Android Open Source Project28527d22009-03-03 19:31:44 -08001034 }
1035
Robert Greenwalt0c4828c2010-01-26 11:40:34 -08001036 // TODO Make this a special check when it goes public
1037 private void enforceTetherChangePermission() {
1038 mContext.enforceCallingOrSelfPermission(
1039 android.Manifest.permission.CHANGE_NETWORK_STATE,
1040 "ConnectivityService");
1041 }
1042
Robert Greenwalt8e87f122010-02-11 18:18:40 -08001043 private void enforceTetherAccessPermission() {
1044 mContext.enforceCallingOrSelfPermission(
1045 android.Manifest.permission.ACCESS_NETWORK_STATE,
1046 "ConnectivityService");
1047 }
1048
Robert Greenwalt93dc1042010-06-15 12:19:37 -07001049 private void enforceConnectivityInternalPermission() {
1050 mContext.enforceCallingOrSelfPermission(
1051 android.Manifest.permission.CONNECTIVITY_INTERNAL,
1052 "ConnectivityService");
1053 }
1054
The Android Open Source Project28527d22009-03-03 19:31:44 -08001055 /**
Robert Greenwalt0659da32009-07-16 17:21:39 -07001056 * Handle a {@code DISCONNECTED} event. If this pertains to the non-active
1057 * network, we ignore it. If it is for the active network, we send out a
1058 * broadcast. But first, we check whether it might be possible to connect
1059 * to a different network.
The Android Open Source Project28527d22009-03-03 19:31:44 -08001060 * @param info the {@code NetworkInfo} for the network
1061 */
1062 private void handleDisconnect(NetworkInfo info) {
1063
Robert Greenwalt2034b912009-08-12 16:08:25 -07001064 int prevNetType = info.getType();
The Android Open Source Project28527d22009-03-03 19:31:44 -08001065
Robert Greenwalt2034b912009-08-12 16:08:25 -07001066 mNetTrackers[prevNetType].setTeardownRequested(false);
The Android Open Source Project28527d22009-03-03 19:31:44 -08001067 /*
1068 * If the disconnected network is not the active one, then don't report
1069 * this as a loss of connectivity. What probably happened is that we're
1070 * getting the disconnect for a network that we explicitly disabled
1071 * in accordance with network preference policies.
1072 */
Robert Greenwalt34848c02011-03-25 13:09:25 -07001073 if (!mNetConfigs[prevNetType].isDefault()) {
Robert Greenwalt2034b912009-08-12 16:08:25 -07001074 List pids = mNetRequestersPids[prevNetType];
1075 for (int i = 0; i<pids.size(); i++) {
1076 Integer pid = (Integer)pids.get(i);
1077 // will remove them because the net's no longer connected
1078 // need to do this now as only now do we know the pids and
1079 // can properly null things that are no longer referenced.
1080 reassessPidDns(pid.intValue(), false);
The Android Open Source Project28527d22009-03-03 19:31:44 -08001081 }
The Android Open Source Project28527d22009-03-03 19:31:44 -08001082 }
1083
The Android Open Source Project28527d22009-03-03 19:31:44 -08001084 Intent intent = new Intent(ConnectivityManager.CONNECTIVITY_ACTION);
1085 intent.putExtra(ConnectivityManager.EXTRA_NETWORK_INFO, info);
1086 if (info.isFailover()) {
1087 intent.putExtra(ConnectivityManager.EXTRA_IS_FAILOVER, true);
1088 info.setFailover(false);
1089 }
1090 if (info.getReason() != null) {
1091 intent.putExtra(ConnectivityManager.EXTRA_REASON, info.getReason());
1092 }
1093 if (info.getExtraInfo() != null) {
Robert Greenwalt0659da32009-07-16 17:21:39 -07001094 intent.putExtra(ConnectivityManager.EXTRA_EXTRA_INFO,
1095 info.getExtraInfo());
The Android Open Source Project28527d22009-03-03 19:31:44 -08001096 }
Robert Greenwalt2034b912009-08-12 16:08:25 -07001097
Robert Greenwalt34848c02011-03-25 13:09:25 -07001098 if (mNetConfigs[prevNetType].isDefault()) {
Robert Greenwalt4b7c55e2011-01-11 13:56:33 -08001099 tryFailover(prevNetType);
1100 if (mActiveDefaultNetwork != -1) {
1101 NetworkInfo switchTo = mNetTrackers[mActiveDefaultNetwork].getNetworkInfo();
Robert Greenwalt3cc68d32010-01-25 17:54:29 -08001102 intent.putExtra(ConnectivityManager.EXTRA_OTHER_NETWORK_INFO, switchTo);
1103 } else {
Robert Greenwaltb2a9e492010-09-20 18:01:43 -07001104 mDefaultInetConditionPublished = 0; // we're not connected anymore
Robert Greenwalt3cc68d32010-01-25 17:54:29 -08001105 intent.putExtra(ConnectivityManager.EXTRA_NO_CONNECTIVITY, true);
1106 }
Robert Greenwaltf55ced92010-01-20 19:29:41 -08001107 }
Robert Greenwaltb2a9e492010-09-20 18:01:43 -07001108 intent.putExtra(ConnectivityManager.EXTRA_INET_CONDITION, mDefaultInetConditionPublished);
Robert Greenwaltf55ced92010-01-20 19:29:41 -08001109 // do this before we broadcast the change
Robert Greenwalt3afbead2010-07-23 15:46:26 -07001110 handleConnectivityChange(prevNetType);
Robert Greenwaltf55ced92010-01-20 19:29:41 -08001111
1112 sendStickyBroadcast(intent);
1113 /*
1114 * If the failover network is already connected, then immediately send
1115 * out a followup broadcast indicating successful failover
1116 */
Robert Greenwalt4b7c55e2011-01-11 13:56:33 -08001117 if (mActiveDefaultNetwork != -1) {
1118 sendConnectedBroadcast(mNetTrackers[mActiveDefaultNetwork].getNetworkInfo());
Robert Greenwaltf55ced92010-01-20 19:29:41 -08001119 }
1120 }
1121
Robert Greenwalt4b7c55e2011-01-11 13:56:33 -08001122 private void tryFailover(int prevNetType) {
Robert Greenwalt2034b912009-08-12 16:08:25 -07001123 /*
Robert Greenwalt92564852011-01-06 15:41:07 -08001124 * If this is a default network, check if other defaults are available.
1125 * Try to reconnect on all available and let them hash it out when
1126 * more than one connects.
Robert Greenwalt2034b912009-08-12 16:08:25 -07001127 */
Robert Greenwalt34848c02011-03-25 13:09:25 -07001128 if (mNetConfigs[prevNetType].isDefault()) {
Robert Greenwalt2034b912009-08-12 16:08:25 -07001129 if (mActiveDefaultNetwork == prevNetType) {
1130 mActiveDefaultNetwork = -1;
1131 }
1132
Robert Greenwalt4b7c55e2011-01-11 13:56:33 -08001133 // don't signal a reconnect for anything lower or equal priority than our
1134 // current connected default
1135 // TODO - don't filter by priority now - nice optimization but risky
1136// int currentPriority = -1;
1137// if (mActiveDefaultNetwork != -1) {
Robert Greenwalt34848c02011-03-25 13:09:25 -07001138// currentPriority = mNetConfigs[mActiveDefaultNetwork].mPriority;
Robert Greenwalt4b7c55e2011-01-11 13:56:33 -08001139// }
Robert Greenwaltf55ced92010-01-20 19:29:41 -08001140 for (int checkType=0; checkType <= ConnectivityManager.MAX_NETWORK_TYPE; checkType++) {
Robert Greenwaltec05b3c2009-10-30 14:17:42 -07001141 if (checkType == prevNetType) continue;
Robert Greenwalt34848c02011-03-25 13:09:25 -07001142 if (mNetConfigs[checkType] == null) continue;
1143 if (!mNetConfigs[checkType].isDefault()) continue;
Wink Saville72a95b92011-01-26 15:43:49 -08001144
1145// Enabling the isAvailable() optimization caused mobile to not get
1146// selected if it was in the middle of error handling. Specifically
1147// a moble connection that took 30 seconds to complete the DEACTIVATE_DATA_CALL
1148// would not be available and we wouldn't get connected to anything.
1149// So removing the isAvailable() optimization below for now. TODO: This
1150// optimization should work and we need to investigate why it doesn't work.
1151// This could be related to how DEACTIVATE_DATA_CALL is reporting its
1152// complete before it is really complete.
1153// if (!mNetTrackers[checkType].isAvailable()) continue;
1154
Robert Greenwalt34848c02011-03-25 13:09:25 -07001155// if (currentPriority >= mNetConfigs[checkType].mPriority) continue;
Robert Greenwalt2034b912009-08-12 16:08:25 -07001156
Robert Greenwalt4b7c55e2011-01-11 13:56:33 -08001157 NetworkStateTracker checkTracker = mNetTrackers[checkType];
1158 NetworkInfo checkInfo = checkTracker.getNetworkInfo();
1159 if (!checkInfo.isConnectedOrConnecting() || checkTracker.isTeardownRequested()) {
1160 checkInfo.setFailover(true);
1161 checkTracker.reconnect();
Robert Greenwalt2034b912009-08-12 16:08:25 -07001162 }
Robert Greenwalt4b7c55e2011-01-11 13:56:33 -08001163 if (DBG) log("Attempting to switch to " + checkInfo.getTypeName());
Robert Greenwalt2034b912009-08-12 16:08:25 -07001164 }
The Android Open Source Project28527d22009-03-03 19:31:44 -08001165 }
The Android Open Source Project28527d22009-03-03 19:31:44 -08001166 }
1167
1168 private void sendConnectedBroadcast(NetworkInfo info) {
Robert Greenwaltd3401f92010-09-15 17:36:33 -07001169 sendGeneralBroadcast(info, ConnectivityManager.CONNECTIVITY_ACTION);
1170 }
1171
1172 private void sendInetConditionBroadcast(NetworkInfo info) {
1173 sendGeneralBroadcast(info, ConnectivityManager.INET_CONDITION_ACTION);
1174 }
1175
1176 private void sendGeneralBroadcast(NetworkInfo info, String bcastType) {
1177 Intent intent = new Intent(bcastType);
The Android Open Source Project28527d22009-03-03 19:31:44 -08001178 intent.putExtra(ConnectivityManager.EXTRA_NETWORK_INFO, info);
1179 if (info.isFailover()) {
1180 intent.putExtra(ConnectivityManager.EXTRA_IS_FAILOVER, true);
1181 info.setFailover(false);
1182 }
1183 if (info.getReason() != null) {
1184 intent.putExtra(ConnectivityManager.EXTRA_REASON, info.getReason());
1185 }
1186 if (info.getExtraInfo() != null) {
Robert Greenwalt0659da32009-07-16 17:21:39 -07001187 intent.putExtra(ConnectivityManager.EXTRA_EXTRA_INFO,
1188 info.getExtraInfo());
The Android Open Source Project28527d22009-03-03 19:31:44 -08001189 }
Robert Greenwalt986c7412010-09-08 15:24:47 -07001190 intent.putExtra(ConnectivityManager.EXTRA_INET_CONDITION, mDefaultInetConditionPublished);
Mike Lockwoodfde2b762009-08-14 14:18:49 -04001191 sendStickyBroadcast(intent);
The Android Open Source Project28527d22009-03-03 19:31:44 -08001192 }
1193
1194 /**
1195 * Called when an attempt to fail over to another network has failed.
1196 * @param info the {@link NetworkInfo} for the failed network
1197 */
1198 private void handleConnectionFailure(NetworkInfo info) {
1199 mNetTrackers[info.getType()].setTeardownRequested(false);
The Android Open Source Project28527d22009-03-03 19:31:44 -08001200
Robert Greenwalt2034b912009-08-12 16:08:25 -07001201 String reason = info.getReason();
1202 String extraInfo = info.getExtraInfo();
Robert Greenwalt0659da32009-07-16 17:21:39 -07001203
Robert Greenwalte981bc52010-10-08 16:35:52 -07001204 String reasonText;
1205 if (reason == null) {
1206 reasonText = ".";
1207 } else {
1208 reasonText = " (" + reason + ").";
The Android Open Source Project28527d22009-03-03 19:31:44 -08001209 }
Wink Savillee70c6f52010-12-03 12:01:38 -08001210 loge("Attempt to connect to " + info.getTypeName() + " failed" + reasonText);
Robert Greenwalt2034b912009-08-12 16:08:25 -07001211
1212 Intent intent = new Intent(ConnectivityManager.CONNECTIVITY_ACTION);
1213 intent.putExtra(ConnectivityManager.EXTRA_NETWORK_INFO, info);
1214 if (getActiveNetworkInfo() == null) {
1215 intent.putExtra(ConnectivityManager.EXTRA_NO_CONNECTIVITY, true);
1216 }
1217 if (reason != null) {
1218 intent.putExtra(ConnectivityManager.EXTRA_REASON, reason);
1219 }
1220 if (extraInfo != null) {
1221 intent.putExtra(ConnectivityManager.EXTRA_EXTRA_INFO, extraInfo);
1222 }
1223 if (info.isFailover()) {
1224 intent.putExtra(ConnectivityManager.EXTRA_IS_FAILOVER, true);
1225 info.setFailover(false);
1226 }
Robert Greenwaltf55ced92010-01-20 19:29:41 -08001227
Robert Greenwalt34848c02011-03-25 13:09:25 -07001228 if (mNetConfigs[info.getType()].isDefault()) {
Robert Greenwalt4b7c55e2011-01-11 13:56:33 -08001229 tryFailover(info.getType());
1230 if (mActiveDefaultNetwork != -1) {
1231 NetworkInfo switchTo = mNetTrackers[mActiveDefaultNetwork].getNetworkInfo();
Robert Greenwalt3cc68d32010-01-25 17:54:29 -08001232 intent.putExtra(ConnectivityManager.EXTRA_OTHER_NETWORK_INFO, switchTo);
1233 } else {
Robert Greenwaltb2a9e492010-09-20 18:01:43 -07001234 mDefaultInetConditionPublished = 0;
Robert Greenwalt3cc68d32010-01-25 17:54:29 -08001235 intent.putExtra(ConnectivityManager.EXTRA_NO_CONNECTIVITY, true);
1236 }
Robert Greenwaltf55ced92010-01-20 19:29:41 -08001237 }
Robert Greenwalt3cc68d32010-01-25 17:54:29 -08001238
Robert Greenwaltb2a9e492010-09-20 18:01:43 -07001239 intent.putExtra(ConnectivityManager.EXTRA_INET_CONDITION, mDefaultInetConditionPublished);
Mike Lockwoodfde2b762009-08-14 14:18:49 -04001240 sendStickyBroadcast(intent);
Robert Greenwaltf55ced92010-01-20 19:29:41 -08001241 /*
1242 * If the failover network is already connected, then immediately send
1243 * out a followup broadcast indicating successful failover
1244 */
Robert Greenwalt4b7c55e2011-01-11 13:56:33 -08001245 if (mActiveDefaultNetwork != -1) {
1246 sendConnectedBroadcast(mNetTrackers[mActiveDefaultNetwork].getNetworkInfo());
Robert Greenwaltf55ced92010-01-20 19:29:41 -08001247 }
Mike Lockwoodfde2b762009-08-14 14:18:49 -04001248 }
1249
1250 private void sendStickyBroadcast(Intent intent) {
1251 synchronized(this) {
Dianne Hackborna417ff82009-12-08 19:45:14 -08001252 if (!mSystemReady) {
1253 mInitialBroadcast = new Intent(intent);
Mike Lockwoodfde2b762009-08-14 14:18:49 -04001254 }
Dianne Hackborna417ff82009-12-08 19:45:14 -08001255 intent.addFlags(Intent.FLAG_RECEIVER_REGISTERED_ONLY_BEFORE_BOOT);
1256 mContext.sendStickyBroadcast(intent);
Mike Lockwoodfde2b762009-08-14 14:18:49 -04001257 }
1258 }
1259
1260 void systemReady() {
1261 synchronized(this) {
1262 mSystemReady = true;
Dianne Hackborna417ff82009-12-08 19:45:14 -08001263 if (mInitialBroadcast != null) {
1264 mContext.sendStickyBroadcast(mInitialBroadcast);
1265 mInitialBroadcast = null;
Mike Lockwoodfde2b762009-08-14 14:18:49 -04001266 }
1267 }
Robert Greenwaltc3c5f862010-10-11 16:00:27 -07001268 // load the global proxy at startup
1269 mHandler.sendMessage(mHandler.obtainMessage(EVENT_APPLY_GLOBAL_HTTP_PROXY));
The Android Open Source Project28527d22009-03-03 19:31:44 -08001270 }
1271
1272 private void handleConnect(NetworkInfo info) {
Robert Greenwalt2034b912009-08-12 16:08:25 -07001273 int type = info.getType();
The Android Open Source Project28527d22009-03-03 19:31:44 -08001274
1275 // snapshot isFailover, because sendConnectedBroadcast() resets it
1276 boolean isFailover = info.isFailover();
Robert Greenwalt2034b912009-08-12 16:08:25 -07001277 NetworkStateTracker thisNet = mNetTrackers[type];
The Android Open Source Project28527d22009-03-03 19:31:44 -08001278
Robert Greenwalt2034b912009-08-12 16:08:25 -07001279 // if this is a default net and other default is running
1280 // kill the one not preferred
Robert Greenwalt34848c02011-03-25 13:09:25 -07001281 if (mNetConfigs[type].isDefault()) {
Robert Greenwalt2034b912009-08-12 16:08:25 -07001282 if (mActiveDefaultNetwork != -1 && mActiveDefaultNetwork != type) {
1283 if ((type != mNetworkPreference &&
Wink Savillef2a62832011-04-07 14:23:45 -07001284 mNetConfigs[mActiveDefaultNetwork].priority >
1285 mNetConfigs[type].priority) ||
Robert Greenwalt2034b912009-08-12 16:08:25 -07001286 mNetworkPreference == mActiveDefaultNetwork) {
1287 // don't accept this one
Wink Savillee70c6f52010-12-03 12:01:38 -08001288 if (DBG) {
1289 log("Not broadcasting CONNECT_ACTION " +
Robert Greenwalt2034b912009-08-12 16:08:25 -07001290 "to torn down network " + info.getTypeName());
Wink Savillee70c6f52010-12-03 12:01:38 -08001291 }
Robert Greenwalt2034b912009-08-12 16:08:25 -07001292 teardown(thisNet);
1293 return;
1294 } else {
1295 // tear down the other
1296 NetworkStateTracker otherNet =
1297 mNetTrackers[mActiveDefaultNetwork];
Wink Savillee70c6f52010-12-03 12:01:38 -08001298 if (DBG) {
1299 log("Policy requires " + otherNet.getNetworkInfo().getTypeName() +
Robert Greenwalt2034b912009-08-12 16:08:25 -07001300 " teardown");
Wink Savillee70c6f52010-12-03 12:01:38 -08001301 }
Robert Greenwalt2034b912009-08-12 16:08:25 -07001302 if (!teardown(otherNet)) {
Wink Savillee70c6f52010-12-03 12:01:38 -08001303 loge("Network declined teardown request");
Robert Greenwalt99910172011-03-29 11:36:28 -07001304 teardown(thisNet);
Robert Greenwalt2034b912009-08-12 16:08:25 -07001305 return;
1306 }
Robert Greenwalt93dc1042010-06-15 12:19:37 -07001307 }
1308 }
1309 synchronized (ConnectivityService.this) {
1310 // have a new default network, release the transition wakelock in a second
1311 // if it's held. The second pause is to allow apps to reconnect over the
1312 // new network
1313 if (mNetTransitionWakeLock.isHeld()) {
1314 mHandler.sendMessageDelayed(mHandler.obtainMessage(
Robert Greenwaltccb36f92010-09-24 14:32:21 -07001315 EVENT_CLEAR_NET_TRANSITION_WAKELOCK,
Robert Greenwalt93dc1042010-06-15 12:19:37 -07001316 mNetTransitionWakeLockSerialNumber, 0),
1317 1000);
Robert Greenwalt2034b912009-08-12 16:08:25 -07001318 }
1319 }
1320 mActiveDefaultNetwork = type;
Robert Greenwalt986c7412010-09-08 15:24:47 -07001321 // this will cause us to come up initially as unconnected and switching
1322 // to connected after our normal pause unless somebody reports us as reall
1323 // disconnected
1324 mDefaultInetConditionPublished = 0;
1325 mDefaultConnectionSequence++;
1326 mInetConditionChangeInFlight = false;
1327 // Don't do this - if we never sign in stay, grey
1328 //reportNetworkCondition(mActiveDefaultNetwork, 100);
Robert Greenwalt2034b912009-08-12 16:08:25 -07001329 }
The Android Open Source Project28527d22009-03-03 19:31:44 -08001330 thisNet.setTeardownRequested(false);
Irfan Sheriff7f132d92010-06-09 15:39:36 -07001331 updateNetworkSettings(thisNet);
Robert Greenwalt3afbead2010-07-23 15:46:26 -07001332 handleConnectivityChange(type);
Robert Greenwalt2034b912009-08-12 16:08:25 -07001333 sendConnectedBroadcast(info);
The Android Open Source Project28527d22009-03-03 19:31:44 -08001334 }
1335
The Android Open Source Project28527d22009-03-03 19:31:44 -08001336 /**
Robert Greenwalt3afbead2010-07-23 15:46:26 -07001337 * After a change in the connectivity state of a network. We're mainly
1338 * concerned with making sure that the list of DNS servers is set up
1339 * according to which networks are connected, and ensuring that the
1340 * right routing table entries exist.
The Android Open Source Project28527d22009-03-03 19:31:44 -08001341 */
Robert Greenwalt3afbead2010-07-23 15:46:26 -07001342 private void handleConnectivityChange(int netType) {
The Android Open Source Project28527d22009-03-03 19:31:44 -08001343 /*
Robert Greenwalt2034b912009-08-12 16:08:25 -07001344 * If a non-default network is enabled, add the host routes that
Robert Greenwalt3afbead2010-07-23 15:46:26 -07001345 * will allow it's DNS servers to be accessed.
The Android Open Source Project28527d22009-03-03 19:31:44 -08001346 */
Robert Greenwalt3afbead2010-07-23 15:46:26 -07001347 handleDnsConfigurationChange(netType);
Robert Greenwalt2034b912009-08-12 16:08:25 -07001348
Robert Greenwalt3afbead2010-07-23 15:46:26 -07001349 if (mNetTrackers[netType].getNetworkInfo().isConnected()) {
Robert Greenwalt34848c02011-03-25 13:09:25 -07001350 if (mNetConfigs[netType].isDefault()) {
Robert Greenwaltc3c5f862010-10-11 16:00:27 -07001351 handleApplyDefaultProxy(netType);
Robert Greenwalt3afbead2010-07-23 15:46:26 -07001352 addDefaultRoute(mNetTrackers[netType]);
Robert Greenwalt2034b912009-08-12 16:08:25 -07001353 } else {
Robert Greenwalta71471b2011-03-24 21:41:41 -07001354 // many radios add a default route even when we don't want one.
1355 // remove the default interface unless we need it for our active network
1356 if (mActiveDefaultNetwork != -1) {
Robert Greenwalt3a378b92011-03-30 15:03:13 -07001357 LinkProperties linkProperties =
1358 mNetTrackers[mActiveDefaultNetwork].getLinkProperties();
1359 LinkProperties newLinkProperties =
1360 mNetTrackers[netType].getLinkProperties();
1361 String defaultIface = linkProperties.getInterfaceName();
Robert Greenwalta71471b2011-03-24 21:41:41 -07001362 if (defaultIface != null &&
Robert Greenwalt3a378b92011-03-30 15:03:13 -07001363 !defaultIface.equals(newLinkProperties.getInterfaceName())) {
Robert Greenwaltf7dae302011-03-31 10:44:47 -07001364 removeDefaultRoute(mNetTrackers[netType]);
Robert Greenwalta71471b2011-03-24 21:41:41 -07001365 }
1366 }
Robert Greenwaltf7dae302011-03-31 10:44:47 -07001367 addPrivateDnsRoutes(mNetTrackers[netType]);
Robert Greenwalt3afbead2010-07-23 15:46:26 -07001368 }
1369 } else {
Robert Greenwalt34848c02011-03-25 13:09:25 -07001370 if (mNetConfigs[netType].isDefault()) {
Robert Greenwalt3afbead2010-07-23 15:46:26 -07001371 removeDefaultRoute(mNetTrackers[netType]);
1372 } else {
1373 removePrivateDnsRoutes(mNetTrackers[netType]);
Robert Greenwalt2034b912009-08-12 16:08:25 -07001374 }
The Android Open Source Project28527d22009-03-03 19:31:44 -08001375 }
1376 }
1377
Irfan Sheriff7f132d92010-06-09 15:39:36 -07001378 private void addPrivateDnsRoutes(NetworkStateTracker nt) {
Irfan Sheriff7f132d92010-06-09 15:39:36 -07001379 boolean privateDnsRouteSet = nt.isPrivateDnsRouteSet();
Robert Greenwalt1f1bcfe2010-08-30 10:56:47 -07001380 LinkProperties p = nt.getLinkProperties();
Robert Greenwalta7dfbd32010-06-15 15:43:39 -07001381 if (p == null) return;
1382 String interfaceName = p.getInterfaceName();
Irfan Sheriff7f132d92010-06-09 15:39:36 -07001383
1384 if (DBG) {
Wink Savillee70c6f52010-12-03 12:01:38 -08001385 log("addPrivateDnsRoutes for " + nt +
Irfan Sheriff7f132d92010-06-09 15:39:36 -07001386 "(" + interfaceName + ") - mPrivateDnsRouteSet = " + privateDnsRouteSet);
1387 }
Irfan Sheriff7f132d92010-06-09 15:39:36 -07001388 if (interfaceName != null && !privateDnsRouteSet) {
Robert Greenwalta7dfbd32010-06-15 15:43:39 -07001389 Collection<InetAddress> dnsList = p.getDnses();
1390 for (InetAddress dns : dnsList) {
Wink Savillee70c6f52010-12-03 12:01:38 -08001391 if (DBG) log(" adding " + dns);
Robert Greenwalt7fe44cb2010-08-27 09:24:29 -07001392 NetworkUtils.addHostRoute(interfaceName, dns, null);
Irfan Sheriff7f132d92010-06-09 15:39:36 -07001393 }
1394 nt.privateDnsRouteSet(true);
1395 }
1396 }
1397
1398 private void removePrivateDnsRoutes(NetworkStateTracker nt) {
1399 // TODO - we should do this explicitly but the NetUtils api doesnt
1400 // support this yet - must remove all. No worse than before
Robert Greenwalt1f1bcfe2010-08-30 10:56:47 -07001401 LinkProperties p = nt.getLinkProperties();
Robert Greenwalta7dfbd32010-06-15 15:43:39 -07001402 if (p == null) return;
1403 String interfaceName = p.getInterfaceName();
Irfan Sheriff7f132d92010-06-09 15:39:36 -07001404 boolean privateDnsRouteSet = nt.isPrivateDnsRouteSet();
1405 if (interfaceName != null && privateDnsRouteSet) {
1406 if (DBG) {
Wink Savillee70c6f52010-12-03 12:01:38 -08001407 log("removePrivateDnsRoutes for " + nt.getNetworkInfo().getTypeName() +
Irfan Sheriff7f132d92010-06-09 15:39:36 -07001408 " (" + interfaceName + ")");
1409 }
1410 NetworkUtils.removeHostRoutes(interfaceName);
1411 nt.privateDnsRouteSet(false);
1412 }
1413 }
1414
Irfan Sheriff7f132d92010-06-09 15:39:36 -07001415
1416 private void addDefaultRoute(NetworkStateTracker nt) {
Robert Greenwalt1f1bcfe2010-08-30 10:56:47 -07001417 LinkProperties p = nt.getLinkProperties();
Robert Greenwalta7dfbd32010-06-15 15:43:39 -07001418 if (p == null) return;
1419 String interfaceName = p.getInterfaceName();
Robert Greenwalt5c733972011-02-09 13:56:06 -08001420 if (TextUtils.isEmpty(interfaceName)) return;
Robert Greenwalt5a901292011-04-28 14:28:50 -07001421 for (RouteInfo route : p.getRoutes()) {
Irfan Sheriff7f132d92010-06-09 15:39:36 -07001422
Robert Greenwalt5a901292011-04-28 14:28:50 -07001423 //TODO - handle non-default routes
1424 if (route.isDefaultRoute()) {
1425 InetAddress gateway = route.getGateway();
1426 if (NetworkUtils.addHostRoute(interfaceName, gateway, null) &&
1427 NetworkUtils.addDefaultRoute(interfaceName, gateway)) {
1428 if (DBG) {
1429 NetworkInfo networkInfo = nt.getNetworkInfo();
1430 log("addDefaultRoute for " + networkInfo.getTypeName() +
1431 " (" + interfaceName + "), GatewayAddr=" +
1432 gateway.getHostAddress());
1433 }
Robert Greenwalt03d53da2011-03-22 18:47:42 -07001434 }
Irfan Sheriff7f132d92010-06-09 15:39:36 -07001435 }
Irfan Sheriff7f132d92010-06-09 15:39:36 -07001436 }
1437 }
1438
1439
1440 public void removeDefaultRoute(NetworkStateTracker nt) {
Robert Greenwalt1f1bcfe2010-08-30 10:56:47 -07001441 LinkProperties p = nt.getLinkProperties();
Robert Greenwalta7dfbd32010-06-15 15:43:39 -07001442 if (p == null) return;
1443 String interfaceName = p.getInterfaceName();
Irfan Sheriff7f132d92010-06-09 15:39:36 -07001444
Robert Greenwalt3afbead2010-07-23 15:46:26 -07001445 if (interfaceName != null) {
Robert Greenwalt03d53da2011-03-22 18:47:42 -07001446 if (NetworkUtils.removeDefaultRoute(interfaceName) >= 0) {
1447 if (DBG) {
1448 NetworkInfo networkInfo = nt.getNetworkInfo();
1449 log("removeDefaultRoute for " + networkInfo.getTypeName() + " (" +
1450 interfaceName + ")");
1451 }
Irfan Sheriff7f132d92010-06-09 15:39:36 -07001452 }
Irfan Sheriff7f132d92010-06-09 15:39:36 -07001453 }
1454 }
1455
1456 /**
1457 * Reads the network specific TCP buffer sizes from SystemProperties
1458 * net.tcp.buffersize.[default|wifi|umts|edge|gprs] and set them for system
1459 * wide use
1460 */
1461 public void updateNetworkSettings(NetworkStateTracker nt) {
1462 String key = nt.getTcpBufferSizesPropName();
1463 String bufferSizes = SystemProperties.get(key);
1464
1465 if (bufferSizes.length() == 0) {
Wink Savillee70c6f52010-12-03 12:01:38 -08001466 loge(key + " not found in system properties. Using defaults");
Irfan Sheriff7f132d92010-06-09 15:39:36 -07001467
1468 // Setting to default values so we won't be stuck to previous values
1469 key = "net.tcp.buffersize.default";
1470 bufferSizes = SystemProperties.get(key);
1471 }
1472
1473 // Set values in kernel
1474 if (bufferSizes.length() != 0) {
1475 if (DBG) {
Wink Savillee70c6f52010-12-03 12:01:38 -08001476 log("Setting TCP values: [" + bufferSizes
Irfan Sheriff7f132d92010-06-09 15:39:36 -07001477 + "] which comes from [" + key + "]");
1478 }
1479 setBufferSize(bufferSizes);
1480 }
1481 }
1482
1483 /**
1484 * Writes TCP buffer sizes to /sys/kernel/ipv4/tcp_[r/w]mem_[min/def/max]
1485 * which maps to /proc/sys/net/ipv4/tcp_rmem and tcpwmem
1486 *
1487 * @param bufferSizes in the format of "readMin, readInitial, readMax,
1488 * writeMin, writeInitial, writeMax"
1489 */
1490 private void setBufferSize(String bufferSizes) {
1491 try {
1492 String[] values = bufferSizes.split(",");
1493
1494 if (values.length == 6) {
1495 final String prefix = "/sys/kernel/ipv4/tcp_";
1496 stringToFile(prefix + "rmem_min", values[0]);
1497 stringToFile(prefix + "rmem_def", values[1]);
1498 stringToFile(prefix + "rmem_max", values[2]);
1499 stringToFile(prefix + "wmem_min", values[3]);
1500 stringToFile(prefix + "wmem_def", values[4]);
1501 stringToFile(prefix + "wmem_max", values[5]);
1502 } else {
Wink Savillee70c6f52010-12-03 12:01:38 -08001503 loge("Invalid buffersize string: " + bufferSizes);
Irfan Sheriff7f132d92010-06-09 15:39:36 -07001504 }
1505 } catch (IOException e) {
Wink Savillee70c6f52010-12-03 12:01:38 -08001506 loge("Can't set tcp buffer sizes:" + e);
Irfan Sheriff7f132d92010-06-09 15:39:36 -07001507 }
1508 }
1509
1510 /**
1511 * Writes string to file. Basically same as "echo -n $string > $filename"
1512 *
1513 * @param filename
1514 * @param string
1515 * @throws IOException
1516 */
1517 private void stringToFile(String filename, String string) throws IOException {
1518 FileWriter out = new FileWriter(filename);
1519 try {
1520 out.write(string);
1521 } finally {
1522 out.close();
1523 }
1524 }
1525
1526
Robert Greenwalt2034b912009-08-12 16:08:25 -07001527 /**
1528 * Adjust the per-process dns entries (net.dns<x>.<pid>) based
1529 * on the highest priority active net which this process requested.
1530 * If there aren't any, clear it out
1531 */
1532 private void reassessPidDns(int myPid, boolean doBump)
1533 {
Wink Savillee70c6f52010-12-03 12:01:38 -08001534 if (DBG) log("reassessPidDns for pid " + myPid);
Robert Greenwalt2034b912009-08-12 16:08:25 -07001535 for(int i : mPriorityList) {
Robert Greenwalt34848c02011-03-25 13:09:25 -07001536 if (mNetConfigs[i].isDefault()) {
Robert Greenwalt2034b912009-08-12 16:08:25 -07001537 continue;
1538 }
1539 NetworkStateTracker nt = mNetTrackers[i];
Robert Greenwalt0659da32009-07-16 17:21:39 -07001540 if (nt.getNetworkInfo().isConnected() &&
1541 !nt.isTeardownRequested()) {
Robert Greenwalt1f1bcfe2010-08-30 10:56:47 -07001542 LinkProperties p = nt.getLinkProperties();
Robert Greenwalta7dfbd32010-06-15 15:43:39 -07001543 if (p == null) continue;
Robert Greenwalt2034b912009-08-12 16:08:25 -07001544 List pids = mNetRequestersPids[i];
1545 for (int j=0; j<pids.size(); j++) {
1546 Integer pid = (Integer)pids.get(j);
1547 if (pid.intValue() == myPid) {
Robert Greenwalta7dfbd32010-06-15 15:43:39 -07001548 Collection<InetAddress> dnses = p.getDnses();
1549 writePidDns(dnses, myPid);
Robert Greenwalt2034b912009-08-12 16:08:25 -07001550 if (doBump) {
1551 bumpDns();
1552 }
1553 return;
1554 }
1555 }
1556 }
1557 }
1558 // nothing found - delete
1559 for (int i = 1; ; i++) {
1560 String prop = "net.dns" + i + "." + myPid;
1561 if (SystemProperties.get(prop).length() == 0) {
1562 if (doBump) {
1563 bumpDns();
1564 }
1565 return;
1566 }
1567 SystemProperties.set(prop, "");
1568 }
1569 }
1570
Robert Greenwalt8ca88762010-12-17 15:20:36 -08001571 // return true if results in a change
1572 private boolean writePidDns(Collection <InetAddress> dnses, int pid) {
Robert Greenwalt2034b912009-08-12 16:08:25 -07001573 int j = 1;
Robert Greenwalt8ca88762010-12-17 15:20:36 -08001574 boolean changed = false;
Robert Greenwalta7dfbd32010-06-15 15:43:39 -07001575 for (InetAddress dns : dnses) {
Robert Greenwalt8ca88762010-12-17 15:20:36 -08001576 String dnsString = dns.getHostAddress();
1577 if (changed || !dnsString.equals(SystemProperties.get("net.dns" + j + "." + pid))) {
1578 changed = true;
1579 SystemProperties.set("net.dns" + j++ + "." + pid, dns.getHostAddress());
1580 }
Robert Greenwalt2034b912009-08-12 16:08:25 -07001581 }
Robert Greenwalt8ca88762010-12-17 15:20:36 -08001582 return changed;
Robert Greenwalt2034b912009-08-12 16:08:25 -07001583 }
1584
1585 private void bumpDns() {
1586 /*
1587 * Bump the property that tells the name resolver library to reread
1588 * the DNS server list from the properties.
1589 */
1590 String propVal = SystemProperties.get("net.dnschange");
1591 int n = 0;
1592 if (propVal.length() != 0) {
1593 try {
1594 n = Integer.parseInt(propVal);
1595 } catch (NumberFormatException e) {}
1596 }
1597 SystemProperties.set("net.dnschange", "" + (n+1));
Robert Greenwalt051642b2010-11-02 14:08:23 -07001598 /*
1599 * Tell the VMs to toss their DNS caches
1600 */
1601 Intent intent = new Intent(Intent.ACTION_CLEAR_DNS_CACHE);
1602 intent.addFlags(Intent.FLAG_RECEIVER_REPLACE_PENDING);
Stan Chesnuttf444f502011-01-05 17:14:03 -08001603 /*
1604 * Connectivity events can happen before boot has completed ...
1605 */
1606 intent.addFlags(Intent.FLAG_RECEIVER_REGISTERED_ONLY_BEFORE_BOOT);
Robert Greenwalt051642b2010-11-02 14:08:23 -07001607 mContext.sendBroadcast(intent);
Robert Greenwalt2034b912009-08-12 16:08:25 -07001608 }
1609
Robert Greenwalt3afbead2010-07-23 15:46:26 -07001610 private void handleDnsConfigurationChange(int netType) {
Robert Greenwalt2034b912009-08-12 16:08:25 -07001611 // add default net's dns entries
Robert Greenwalt3afbead2010-07-23 15:46:26 -07001612 NetworkStateTracker nt = mNetTrackers[netType];
1613 if (nt != null && nt.getNetworkInfo().isConnected() && !nt.isTeardownRequested()) {
Robert Greenwalt1f1bcfe2010-08-30 10:56:47 -07001614 LinkProperties p = nt.getLinkProperties();
Robert Greenwalt3afbead2010-07-23 15:46:26 -07001615 if (p == null) return;
1616 Collection<InetAddress> dnses = p.getDnses();
Robert Greenwalt8ca88762010-12-17 15:20:36 -08001617 boolean changed = false;
Robert Greenwalt34848c02011-03-25 13:09:25 -07001618 if (mNetConfigs[netType].isDefault()) {
Robert Greenwalt3afbead2010-07-23 15:46:26 -07001619 int j = 1;
Robert Greenwalt94daa182010-09-01 11:34:05 -07001620 if (dnses.size() == 0 && mDefaultDns != null) {
Robert Greenwalt8ca88762010-12-17 15:20:36 -08001621 String dnsString = mDefaultDns.getHostAddress();
1622 if (!dnsString.equals(SystemProperties.get("net.dns1"))) {
1623 if (DBG) {
1624 log("no dns provided - using " + dnsString);
1625 }
1626 changed = true;
1627 SystemProperties.set("net.dns1", dnsString);
Robert Greenwalt2034b912009-08-12 16:08:25 -07001628 }
Robert Greenwalt94daa182010-09-01 11:34:05 -07001629 j++;
1630 } else {
1631 for (InetAddress dns : dnses) {
Robert Greenwalt8ca88762010-12-17 15:20:36 -08001632 String dnsString = dns.getHostAddress();
1633 if (!changed && dnsString.equals(SystemProperties.get("net.dns" + j))) {
1634 j++;
1635 continue;
1636 }
Robert Greenwalt94daa182010-09-01 11:34:05 -07001637 if (DBG) {
Wink Savillee70c6f52010-12-03 12:01:38 -08001638 log("adding dns " + dns + " for " +
Robert Greenwalt94daa182010-09-01 11:34:05 -07001639 nt.getNetworkInfo().getTypeName());
1640 }
Robert Greenwalt8ca88762010-12-17 15:20:36 -08001641 changed = true;
1642 SystemProperties.set("net.dns" + j++, dnsString);
Robert Greenwalt94daa182010-09-01 11:34:05 -07001643 }
Robert Greenwalt3afbead2010-07-23 15:46:26 -07001644 }
1645 for (int k=j ; k<mNumDnsEntries; k++) {
Robert Greenwalt8ca88762010-12-17 15:20:36 -08001646 if (changed || !TextUtils.isEmpty(SystemProperties.get("net.dns" + k))) {
1647 if (DBG) log("erasing net.dns" + k);
1648 changed = true;
1649 SystemProperties.set("net.dns" + k, "");
1650 }
Robert Greenwalt3afbead2010-07-23 15:46:26 -07001651 }
1652 mNumDnsEntries = j;
1653 } else {
1654 // set per-pid dns for attached secondary nets
1655 List pids = mNetRequestersPids[netType];
1656 for (int y=0; y< pids.size(); y++) {
1657 Integer pid = (Integer)pids.get(y);
Robert Greenwalt8ca88762010-12-17 15:20:36 -08001658 changed = writePidDns(dnses, pid.intValue());
The Android Open Source Project28527d22009-03-03 19:31:44 -08001659 }
1660 }
Robert Greenwalt8ca88762010-12-17 15:20:36 -08001661 if (changed) bumpDns();
The Android Open Source Project28527d22009-03-03 19:31:44 -08001662 }
Robert Greenwalt2034b912009-08-12 16:08:25 -07001663 }
1664
1665 private int getRestoreDefaultNetworkDelay() {
1666 String restoreDefaultNetworkDelayStr = SystemProperties.get(
1667 NETWORK_RESTORE_DELAY_PROP_NAME);
1668 if(restoreDefaultNetworkDelayStr != null &&
1669 restoreDefaultNetworkDelayStr.length() != 0) {
1670 try {
1671 return Integer.valueOf(restoreDefaultNetworkDelayStr);
1672 } catch (NumberFormatException e) {
1673 }
The Android Open Source Project28527d22009-03-03 19:31:44 -08001674 }
Robert Greenwalt2034b912009-08-12 16:08:25 -07001675 return RESTORE_DEFAULT_NETWORK_DELAY;
The Android Open Source Project28527d22009-03-03 19:31:44 -08001676 }
1677
1678 @Override
1679 protected void dump(FileDescriptor fd, PrintWriter pw, String[] args) {
Robert Greenwalt0659da32009-07-16 17:21:39 -07001680 if (mContext.checkCallingOrSelfPermission(
1681 android.Manifest.permission.DUMP)
The Android Open Source Project28527d22009-03-03 19:31:44 -08001682 != PackageManager.PERMISSION_GRANTED) {
Robert Greenwalt0659da32009-07-16 17:21:39 -07001683 pw.println("Permission Denial: can't dump ConnectivityService " +
1684 "from from pid=" + Binder.getCallingPid() + ", uid=" +
1685 Binder.getCallingUid());
The Android Open Source Project28527d22009-03-03 19:31:44 -08001686 return;
1687 }
The Android Open Source Project28527d22009-03-03 19:31:44 -08001688 pw.println();
1689 for (NetworkStateTracker nst : mNetTrackers) {
Robert Greenwalt3eeb6032009-12-21 18:24:07 -08001690 if (nst != null) {
1691 if (nst.getNetworkInfo().isConnected()) {
1692 pw.println("Active network: " + nst.getNetworkInfo().
1693 getTypeName());
1694 }
1695 pw.println(nst.getNetworkInfo());
1696 pw.println(nst);
1697 pw.println();
Robert Greenwalt2034b912009-08-12 16:08:25 -07001698 }
The Android Open Source Project28527d22009-03-03 19:31:44 -08001699 }
Robert Greenwalt3eeb6032009-12-21 18:24:07 -08001700
1701 pw.println("Network Requester Pids:");
1702 for (int net : mPriorityList) {
1703 String pidString = net + ": ";
1704 for (Object pid : mNetRequestersPids[net]) {
1705 pidString = pidString + pid.toString() + ", ";
1706 }
1707 pw.println(pidString);
1708 }
1709 pw.println();
1710
1711 pw.println("FeatureUsers:");
1712 for (Object requester : mFeatureUsers) {
1713 pw.println(requester.toString());
1714 }
1715 pw.println();
Robert Greenwalt8e87f122010-02-11 18:18:40 -08001716
Robert Greenwalt93dc1042010-06-15 12:19:37 -07001717 synchronized (this) {
1718 pw.println("NetworkTranstionWakeLock is currently " +
1719 (mNetTransitionWakeLock.isHeld() ? "" : "not ") + "held.");
1720 pw.println("It was last requested for "+mNetTransitionWakeLockCausedBy);
1721 }
1722 pw.println();
1723
Robert Greenwalt8e87f122010-02-11 18:18:40 -08001724 mTethering.dump(fd, pw, args);
Robert Greenwalt0e80be12010-09-20 14:35:25 -07001725
1726 if (mInetLog != null) {
1727 pw.println();
1728 pw.println("Inet condition reports:");
1729 for(int i = 0; i < mInetLog.size(); i++) {
1730 pw.println(mInetLog.get(i));
1731 }
1732 }
The Android Open Source Project28527d22009-03-03 19:31:44 -08001733 }
1734
Robert Greenwalt2034b912009-08-12 16:08:25 -07001735 // must be stateless - things change under us.
The Android Open Source Project28527d22009-03-03 19:31:44 -08001736 private class MyHandler extends Handler {
Wink Saville775aad62010-09-02 19:23:52 -07001737 public MyHandler(Looper looper) {
1738 super(looper);
1739 }
1740
The Android Open Source Project28527d22009-03-03 19:31:44 -08001741 @Override
1742 public void handleMessage(Message msg) {
1743 NetworkInfo info;
1744 switch (msg.what) {
1745 case NetworkStateTracker.EVENT_STATE_CHANGED:
1746 info = (NetworkInfo) msg.obj;
Robert Greenwalt12c44552009-12-07 11:33:18 -08001747 int type = info.getType();
1748 NetworkInfo.State state = info.getState();
Robert Greenwalt12c44552009-12-07 11:33:18 -08001749
Wink Savillee70c6f52010-12-03 12:01:38 -08001750 if (DBG) log("ConnectivityChange for " +
Robert Greenwalt0659da32009-07-16 17:21:39 -07001751 info.getTypeName() + ": " +
Robert Greenwalt12c44552009-12-07 11:33:18 -08001752 state + "/" + info.getDetailedState());
The Android Open Source Project28527d22009-03-03 19:31:44 -08001753
1754 // Connectivity state changed:
1755 // [31-13] Reserved for future use
Robert Greenwalt0659da32009-07-16 17:21:39 -07001756 // [12-9] Network subtype (for mobile network, as defined
1757 // by TelephonyManager)
1758 // [8-3] Detailed state ordinal (as defined by
1759 // NetworkInfo.DetailedState)
The Android Open Source Project28527d22009-03-03 19:31:44 -08001760 // [2-0] Network type (as defined by ConnectivityManager)
1761 int eventLogParam = (info.getType() & 0x7) |
1762 ((info.getDetailedState().ordinal() & 0x3f) << 3) |
1763 (info.getSubtype() << 9);
Doug Zongker2fc96232009-12-04 10:31:43 -08001764 EventLog.writeEvent(EventLogTags.CONNECTIVITY_STATE_CHANGED,
Robert Greenwalt0659da32009-07-16 17:21:39 -07001765 eventLogParam);
1766
1767 if (info.getDetailedState() ==
1768 NetworkInfo.DetailedState.FAILED) {
The Android Open Source Project28527d22009-03-03 19:31:44 -08001769 handleConnectionFailure(info);
Robert Greenwalt12c44552009-12-07 11:33:18 -08001770 } else if (state == NetworkInfo.State.DISCONNECTED) {
The Android Open Source Project28527d22009-03-03 19:31:44 -08001771 handleDisconnect(info);
Robert Greenwalt12c44552009-12-07 11:33:18 -08001772 } else if (state == NetworkInfo.State.SUSPENDED) {
The Android Open Source Project28527d22009-03-03 19:31:44 -08001773 // TODO: need to think this over.
Robert Greenwalt0659da32009-07-16 17:21:39 -07001774 // the logic here is, handle SUSPENDED the same as
1775 // DISCONNECTED. The only difference being we are
1776 // broadcasting an intent with NetworkInfo that's
1777 // suspended. This allows the applications an
1778 // opportunity to handle DISCONNECTED and SUSPENDED
1779 // differently, or not.
The Android Open Source Project28527d22009-03-03 19:31:44 -08001780 handleDisconnect(info);
Robert Greenwalt12c44552009-12-07 11:33:18 -08001781 } else if (state == NetworkInfo.State.CONNECTED) {
The Android Open Source Project28527d22009-03-03 19:31:44 -08001782 handleConnect(info);
1783 }
The Android Open Source Project28527d22009-03-03 19:31:44 -08001784 break;
The Android Open Source Project28527d22009-03-03 19:31:44 -08001785 case NetworkStateTracker.EVENT_CONFIGURATION_CHANGED:
Robert Greenwalt3afbead2010-07-23 15:46:26 -07001786 info = (NetworkInfo) msg.obj;
Robert Greenwalt34848c02011-03-25 13:09:25 -07001787 handleConnectivityChange(info.getType());
The Android Open Source Project28527d22009-03-03 19:31:44 -08001788 break;
Robert Greenwaltccb36f92010-09-24 14:32:21 -07001789 case EVENT_CLEAR_NET_TRANSITION_WAKELOCK:
Robert Greenwalt93dc1042010-06-15 12:19:37 -07001790 String causedBy = null;
1791 synchronized (ConnectivityService.this) {
1792 if (msg.arg1 == mNetTransitionWakeLockSerialNumber &&
1793 mNetTransitionWakeLock.isHeld()) {
1794 mNetTransitionWakeLock.release();
1795 causedBy = mNetTransitionWakeLockCausedBy;
1796 }
1797 }
1798 if (causedBy != null) {
Wink Savillee70c6f52010-12-03 12:01:38 -08001799 log("NetTransition Wakelock for " + causedBy + " released by timeout");
Robert Greenwalt93dc1042010-06-15 12:19:37 -07001800 }
Robert Greenwaltcf1a56c2010-09-09 14:05:10 -07001801 break;
Robert Greenwalt6a2db8a2010-09-23 10:05:56 -07001802 case EVENT_RESTORE_DEFAULT_NETWORK:
Robert Greenwaltaffc3a12009-09-27 17:27:04 -07001803 FeatureUser u = (FeatureUser)msg.obj;
1804 u.expire();
Robert Greenwalt986c7412010-09-08 15:24:47 -07001805 break;
Robert Greenwalt6a2db8a2010-09-23 10:05:56 -07001806 case EVENT_INET_CONDITION_CHANGE:
1807 {
1808 int netType = msg.arg1;
1809 int condition = msg.arg2;
1810 handleInetConditionChange(netType, condition);
Robert Greenwalt93dc1042010-06-15 12:19:37 -07001811 break;
Robert Greenwalt6a2db8a2010-09-23 10:05:56 -07001812 }
1813 case EVENT_INET_CONDITION_HOLD_END:
1814 {
1815 int netType = msg.arg1;
1816 int sequence = msg.arg2;
1817 handleInetConditionHoldEnd(netType, sequence);
Robert Greenwalt986c7412010-09-08 15:24:47 -07001818 break;
Robert Greenwalt6a2db8a2010-09-23 10:05:56 -07001819 }
1820 case EVENT_SET_NETWORK_PREFERENCE:
1821 {
1822 int preference = msg.arg1;
1823 handleSetNetworkPreference(preference);
1824 break;
1825 }
1826 case EVENT_SET_BACKGROUND_DATA:
1827 {
1828 boolean enabled = (msg.arg1 == ENABLED);
1829 handleSetBackgroundData(enabled);
1830 break;
1831 }
1832 case EVENT_SET_MOBILE_DATA:
1833 {
1834 boolean enabled = (msg.arg1 == ENABLED);
1835 handleSetMobileData(enabled);
1836 break;
1837 }
Robert Greenwaltc3c5f862010-10-11 16:00:27 -07001838 case EVENT_APPLY_GLOBAL_HTTP_PROXY:
1839 {
1840 handleDeprecatedGlobalHttpProxy();
Robert Greenwalt34848c02011-03-25 13:09:25 -07001841 break;
1842 }
1843 case EVENT_SET_DEPENDENCY_MET:
1844 {
1845 boolean met = (msg.arg1 == ENABLED);
1846 handleSetDependencyMet(msg.arg2, met);
1847 break;
Robert Greenwaltc3c5f862010-10-11 16:00:27 -07001848 }
The Android Open Source Project28527d22009-03-03 19:31:44 -08001849 }
1850 }
1851 }
Robert Greenwalt0c4828c2010-01-26 11:40:34 -08001852
1853 // javadoc from interface
Robert Greenwalt4283ded2010-03-02 17:25:02 -08001854 public int tether(String iface) {
Robert Greenwalt0c4828c2010-01-26 11:40:34 -08001855 enforceTetherChangePermission();
Robert Greenwalt4283ded2010-03-02 17:25:02 -08001856
1857 if (isTetheringSupported()) {
1858 return mTethering.tether(iface);
1859 } else {
1860 return ConnectivityManager.TETHER_ERROR_UNSUPPORTED;
1861 }
Robert Greenwalt0c4828c2010-01-26 11:40:34 -08001862 }
1863
1864 // javadoc from interface
Robert Greenwalt4283ded2010-03-02 17:25:02 -08001865 public int untether(String iface) {
Robert Greenwalt0c4828c2010-01-26 11:40:34 -08001866 enforceTetherChangePermission();
Robert Greenwalt4283ded2010-03-02 17:25:02 -08001867
1868 if (isTetheringSupported()) {
1869 return mTethering.untether(iface);
1870 } else {
1871 return ConnectivityManager.TETHER_ERROR_UNSUPPORTED;
1872 }
1873 }
1874
1875 // javadoc from interface
1876 public int getLastTetherError(String iface) {
1877 enforceTetherAccessPermission();
1878
1879 if (isTetheringSupported()) {
1880 return mTethering.getLastTetherError(iface);
1881 } else {
1882 return ConnectivityManager.TETHER_ERROR_UNSUPPORTED;
1883 }
Robert Greenwalt8e87f122010-02-11 18:18:40 -08001884 }
1885
1886 // TODO - proper iface API for selection by property, inspection, etc
1887 public String[] getTetherableUsbRegexs() {
1888 enforceTetherAccessPermission();
1889 if (isTetheringSupported()) {
1890 return mTethering.getTetherableUsbRegexs();
1891 } else {
1892 return new String[0];
1893 }
1894 }
1895
1896 public String[] getTetherableWifiRegexs() {
1897 enforceTetherAccessPermission();
1898 if (isTetheringSupported()) {
1899 return mTethering.getTetherableWifiRegexs();
1900 } else {
1901 return new String[0];
1902 }
Robert Greenwalt0c4828c2010-01-26 11:40:34 -08001903 }
1904
Danica Chang96567052010-08-11 14:54:43 -07001905 public String[] getTetherableBluetoothRegexs() {
1906 enforceTetherAccessPermission();
1907 if (isTetheringSupported()) {
1908 return mTethering.getTetherableBluetoothRegexs();
1909 } else {
1910 return new String[0];
1911 }
1912 }
1913
Robert Greenwalt0c4828c2010-01-26 11:40:34 -08001914 // TODO - move iface listing, queries, etc to new module
1915 // javadoc from interface
1916 public String[] getTetherableIfaces() {
Robert Greenwalt8e87f122010-02-11 18:18:40 -08001917 enforceTetherAccessPermission();
Robert Greenwalt0c4828c2010-01-26 11:40:34 -08001918 return mTethering.getTetherableIfaces();
1919 }
1920
1921 public String[] getTetheredIfaces() {
Robert Greenwalt8e87f122010-02-11 18:18:40 -08001922 enforceTetherAccessPermission();
Robert Greenwalt0c4828c2010-01-26 11:40:34 -08001923 return mTethering.getTetheredIfaces();
1924 }
Robert Greenwalt8e87f122010-02-11 18:18:40 -08001925
Robert Greenwalt4283ded2010-03-02 17:25:02 -08001926 public String[] getTetheringErroredIfaces() {
1927 enforceTetherAccessPermission();
1928 return mTethering.getErroredIfaces();
1929 }
1930
Robert Greenwalt8e87f122010-02-11 18:18:40 -08001931 // if ro.tether.denied = true we default to no tethering
1932 // gservices could set the secure setting to 1 though to enable it on a build where it
1933 // had previously been turned off.
1934 public boolean isTetheringSupported() {
1935 enforceTetherAccessPermission();
1936 int defaultVal = (SystemProperties.get("ro.tether.denied").equals("true") ? 0 : 1);
Robert Greenwaltf1b66e12010-02-25 12:29:30 -08001937 boolean tetherEnabledInSettings = (Settings.Secure.getInt(mContext.getContentResolver(),
1938 Settings.Secure.TETHER_SUPPORTED, defaultVal) != 0);
1939 return tetherEnabledInSettings && mTetheringConfigValid;
Robert Greenwalt8e87f122010-02-11 18:18:40 -08001940 }
Robert Greenwalt93dc1042010-06-15 12:19:37 -07001941
1942 // An API NetworkStateTrackers can call when they lose their network.
1943 // This will automatically be cleared after X seconds or a network becomes CONNECTED,
1944 // whichever happens first. The timer is started by the first caller and not
1945 // restarted by subsequent callers.
1946 public void requestNetworkTransitionWakelock(String forWhom) {
1947 enforceConnectivityInternalPermission();
1948 synchronized (this) {
1949 if (mNetTransitionWakeLock.isHeld()) return;
1950 mNetTransitionWakeLockSerialNumber++;
1951 mNetTransitionWakeLock.acquire();
1952 mNetTransitionWakeLockCausedBy = forWhom;
1953 }
1954 mHandler.sendMessageDelayed(mHandler.obtainMessage(
Robert Greenwaltccb36f92010-09-24 14:32:21 -07001955 EVENT_CLEAR_NET_TRANSITION_WAKELOCK,
Robert Greenwalt93dc1042010-06-15 12:19:37 -07001956 mNetTransitionWakeLockSerialNumber, 0),
1957 mNetTransitionWakeLockTimeout);
1958 return;
1959 }
Robert Greenwalt24118e82010-09-09 13:15:32 -07001960
Robert Greenwalt986c7412010-09-08 15:24:47 -07001961 // 100 percent is full good, 0 is full bad.
1962 public void reportInetCondition(int networkType, int percentage) {
Wink Savillee70c6f52010-12-03 12:01:38 -08001963 if (DBG) log("reportNetworkCondition(" + networkType + ", " + percentage + ")");
Robert Greenwalt986c7412010-09-08 15:24:47 -07001964 mContext.enforceCallingOrSelfPermission(
1965 android.Manifest.permission.STATUS_BAR,
1966 "ConnectivityService");
1967
Robert Greenwalt0e80be12010-09-20 14:35:25 -07001968 if (DBG) {
1969 int pid = getCallingPid();
1970 int uid = getCallingUid();
1971 String s = pid + "(" + uid + ") reports inet is " +
1972 (percentage > 50 ? "connected" : "disconnected") + " (" + percentage + ") on " +
1973 "network Type " + networkType + " at " + GregorianCalendar.getInstance().getTime();
1974 mInetLog.add(s);
1975 while(mInetLog.size() > INET_CONDITION_LOG_MAX_SIZE) {
1976 mInetLog.remove(0);
1977 }
1978 }
Robert Greenwalt986c7412010-09-08 15:24:47 -07001979 mHandler.sendMessage(mHandler.obtainMessage(
Robert Greenwalt6a2db8a2010-09-23 10:05:56 -07001980 EVENT_INET_CONDITION_CHANGE, networkType, percentage));
1981 }
1982
1983 private void handleInetConditionChange(int netType, int condition) {
1984 if (DBG) {
Wink Savillee70c6f52010-12-03 12:01:38 -08001985 log("Inet connectivity change, net=" +
Robert Greenwalt6a2db8a2010-09-23 10:05:56 -07001986 netType + ", condition=" + condition +
1987 ",mActiveDefaultNetwork=" + mActiveDefaultNetwork);
1988 }
1989 if (mActiveDefaultNetwork == -1) {
Wink Savillee70c6f52010-12-03 12:01:38 -08001990 if (DBG) log("no active default network - aborting");
Robert Greenwalt6a2db8a2010-09-23 10:05:56 -07001991 return;
1992 }
1993 if (mActiveDefaultNetwork != netType) {
Wink Savillee70c6f52010-12-03 12:01:38 -08001994 if (DBG) log("given net not default - aborting");
Robert Greenwalt6a2db8a2010-09-23 10:05:56 -07001995 return;
1996 }
1997 mDefaultInetCondition = condition;
1998 int delay;
1999 if (mInetConditionChangeInFlight == false) {
Wink Savillee70c6f52010-12-03 12:01:38 -08002000 if (DBG) log("starting a change hold");
Robert Greenwalt6a2db8a2010-09-23 10:05:56 -07002001 // setup a new hold to debounce this
2002 if (mDefaultInetCondition > 50) {
2003 delay = Settings.Secure.getInt(mContext.getContentResolver(),
2004 Settings.Secure.INET_CONDITION_DEBOUNCE_UP_DELAY, 500);
2005 } else {
2006 delay = Settings.Secure.getInt(mContext.getContentResolver(),
2007 Settings.Secure.INET_CONDITION_DEBOUNCE_DOWN_DELAY, 3000);
2008 }
2009 mInetConditionChangeInFlight = true;
2010 mHandler.sendMessageDelayed(mHandler.obtainMessage(EVENT_INET_CONDITION_HOLD_END,
2011 mActiveDefaultNetwork, mDefaultConnectionSequence), delay);
2012 } else {
2013 // we've set the new condition, when this hold ends that will get
2014 // picked up
Wink Savillee70c6f52010-12-03 12:01:38 -08002015 if (DBG) log("currently in hold - not setting new end evt");
Robert Greenwalt6a2db8a2010-09-23 10:05:56 -07002016 }
2017 }
2018
2019 private void handleInetConditionHoldEnd(int netType, int sequence) {
2020 if (DBG) {
Wink Savillee70c6f52010-12-03 12:01:38 -08002021 log("Inet hold end, net=" + netType +
Robert Greenwalt6a2db8a2010-09-23 10:05:56 -07002022 ", condition =" + mDefaultInetCondition +
2023 ", published condition =" + mDefaultInetConditionPublished);
2024 }
2025 mInetConditionChangeInFlight = false;
2026
2027 if (mActiveDefaultNetwork == -1) {
Wink Savillee70c6f52010-12-03 12:01:38 -08002028 if (DBG) log("no active default network - aborting");
Robert Greenwalt6a2db8a2010-09-23 10:05:56 -07002029 return;
2030 }
2031 if (mDefaultConnectionSequence != sequence) {
Wink Savillee70c6f52010-12-03 12:01:38 -08002032 if (DBG) log("event hold for obsolete network - aborting");
Robert Greenwalt6a2db8a2010-09-23 10:05:56 -07002033 return;
2034 }
2035 if (mDefaultInetConditionPublished == mDefaultInetCondition) {
Wink Savillee70c6f52010-12-03 12:01:38 -08002036 if (DBG) log("no change in condition - aborting");
Robert Greenwalt6a2db8a2010-09-23 10:05:56 -07002037 return;
2038 }
2039 NetworkInfo networkInfo = mNetTrackers[mActiveDefaultNetwork].getNetworkInfo();
2040 if (networkInfo.isConnected() == false) {
Wink Savillee70c6f52010-12-03 12:01:38 -08002041 if (DBG) log("default network not connected - aborting");
Robert Greenwalt6a2db8a2010-09-23 10:05:56 -07002042 return;
2043 }
2044 mDefaultInetConditionPublished = mDefaultInetCondition;
2045 sendInetConditionBroadcast(networkInfo);
2046 return;
Robert Greenwalt986c7412010-09-08 15:24:47 -07002047 }
Robert Greenwaltc3c5f862010-10-11 16:00:27 -07002048
2049 public synchronized ProxyProperties getProxy() {
2050 if (mGlobalProxy != null) return mGlobalProxy;
2051 if (mDefaultProxy != null) return mDefaultProxy;
2052 return null;
2053 }
2054
2055 public void setGlobalProxy(ProxyProperties proxyProperties) {
2056 enforceChangePermission();
2057 synchronized (mGlobalProxyLock) {
2058 if (proxyProperties == mGlobalProxy) return;
2059 if (proxyProperties != null && proxyProperties.equals(mGlobalProxy)) return;
2060 if (mGlobalProxy != null && mGlobalProxy.equals(proxyProperties)) return;
2061
2062 String host = "";
2063 int port = 0;
2064 String exclList = "";
2065 if (proxyProperties != null && !TextUtils.isEmpty(proxyProperties.getHost())) {
2066 mGlobalProxy = new ProxyProperties(proxyProperties);
2067 host = mGlobalProxy.getHost();
2068 port = mGlobalProxy.getPort();
2069 exclList = mGlobalProxy.getExclusionList();
2070 } else {
2071 mGlobalProxy = null;
2072 }
2073 ContentResolver res = mContext.getContentResolver();
2074 Settings.Secure.putString(res, Settings.Secure.GLOBAL_HTTP_PROXY_HOST, host);
2075 Settings.Secure.putInt(res, Settings.Secure.GLOBAL_HTTP_PROXY_PORT, port);
Robert Greenwalt6f7c6092010-12-02 11:31:00 -08002076 Settings.Secure.putString(res, Settings.Secure.GLOBAL_HTTP_PROXY_EXCLUSION_LIST,
Robert Greenwaltc3c5f862010-10-11 16:00:27 -07002077 exclList);
2078 }
2079
2080 if (mGlobalProxy == null) {
2081 proxyProperties = mDefaultProxy;
2082 }
2083 sendProxyBroadcast(proxyProperties);
2084 }
2085
Robert Greenwalt6f7c6092010-12-02 11:31:00 -08002086 private void loadGlobalProxy() {
2087 ContentResolver res = mContext.getContentResolver();
2088 String host = Settings.Secure.getString(res, Settings.Secure.GLOBAL_HTTP_PROXY_HOST);
2089 int port = Settings.Secure.getInt(res, Settings.Secure.GLOBAL_HTTP_PROXY_PORT, 0);
2090 String exclList = Settings.Secure.getString(res,
2091 Settings.Secure.GLOBAL_HTTP_PROXY_EXCLUSION_LIST);
2092 if (!TextUtils.isEmpty(host)) {
2093 ProxyProperties proxyProperties = new ProxyProperties(host, port, exclList);
2094 synchronized (mGlobalProxyLock) {
2095 mGlobalProxy = proxyProperties;
2096 }
2097 }
2098 }
2099
Robert Greenwaltc3c5f862010-10-11 16:00:27 -07002100 public ProxyProperties getGlobalProxy() {
2101 synchronized (mGlobalProxyLock) {
2102 return mGlobalProxy;
2103 }
2104 }
2105
2106 private void handleApplyDefaultProxy(int type) {
2107 // check if new default - push it out to all VM if so
2108 ProxyProperties proxy = mNetTrackers[type].getLinkProperties().getHttpProxy();
2109 synchronized (this) {
2110 if (mDefaultProxy != null && mDefaultProxy.equals(proxy)) return;
2111 if (mDefaultProxy == proxy) return;
2112 if (!TextUtils.isEmpty(proxy.getHost())) {
2113 mDefaultProxy = proxy;
2114 } else {
2115 mDefaultProxy = null;
2116 }
2117 }
Wink Savillee70c6f52010-12-03 12:01:38 -08002118 if (DBG) log("changing default proxy to " + proxy);
Robert Greenwaltc3c5f862010-10-11 16:00:27 -07002119 if ((proxy == null && mGlobalProxy == null) || proxy.equals(mGlobalProxy)) return;
2120 if (mGlobalProxy != null) return;
2121 sendProxyBroadcast(proxy);
2122 }
2123
2124 private void handleDeprecatedGlobalHttpProxy() {
2125 String proxy = Settings.Secure.getString(mContext.getContentResolver(),
2126 Settings.Secure.HTTP_PROXY);
2127 if (!TextUtils.isEmpty(proxy)) {
2128 String data[] = proxy.split(":");
2129 String proxyHost = data[0];
2130 int proxyPort = 8080;
2131 if (data.length > 1) {
2132 try {
2133 proxyPort = Integer.parseInt(data[1]);
2134 } catch (NumberFormatException e) {
2135 return;
2136 }
2137 }
2138 ProxyProperties p = new ProxyProperties(data[0], proxyPort, "");
2139 setGlobalProxy(p);
2140 }
2141 }
2142
2143 private void sendProxyBroadcast(ProxyProperties proxy) {
Robert Greenwalt611291c2010-12-23 15:51:10 -08002144 if (proxy == null) proxy = new ProxyProperties("", 0, "");
Wink Savillee70c6f52010-12-03 12:01:38 -08002145 log("sending Proxy Broadcast for " + proxy);
Robert Greenwaltc3c5f862010-10-11 16:00:27 -07002146 Intent intent = new Intent(Proxy.PROXY_CHANGE_ACTION);
Stan Chesnutt1f2a2ac2011-01-06 11:00:19 -08002147 intent.addFlags(Intent.FLAG_RECEIVER_REPLACE_PENDING |
2148 Intent.FLAG_RECEIVER_REGISTERED_ONLY_BEFORE_BOOT);
Robert Greenwaltc3c5f862010-10-11 16:00:27 -07002149 intent.putExtra(Proxy.EXTRA_PROXY_INFO, proxy);
Robert Greenwaltd93dc8f2010-12-06 11:29:17 -08002150 mContext.sendStickyBroadcast(intent);
Robert Greenwaltc3c5f862010-10-11 16:00:27 -07002151 }
2152
2153 private static class SettingsObserver extends ContentObserver {
2154 private int mWhat;
2155 private Handler mHandler;
2156 SettingsObserver(Handler handler, int what) {
2157 super(handler);
2158 mHandler = handler;
2159 mWhat = what;
2160 }
2161
2162 void observe(Context context) {
2163 ContentResolver resolver = context.getContentResolver();
2164 resolver.registerContentObserver(Settings.Secure.getUriFor(
2165 Settings.Secure.HTTP_PROXY), false, this);
2166 }
2167
2168 @Override
2169 public void onChange(boolean selfChange) {
2170 mHandler.obtainMessage(mWhat).sendToTarget();
2171 }
2172 }
Wink Savillee70c6f52010-12-03 12:01:38 -08002173
2174 private void log(String s) {
2175 Slog.d(TAG, s);
2176 }
2177
2178 private void loge(String s) {
2179 Slog.e(TAG, s);
2180 }
Wink Savilleb7c92c72011-03-12 14:52:01 -08002181 int convertFeatureToNetworkType(String feature){
2182 int networkType = -1;
2183 if (TextUtils.equals(feature, Phone.FEATURE_ENABLE_MMS)) {
2184 networkType = ConnectivityManager.TYPE_MOBILE_MMS;
2185 } else if (TextUtils.equals(feature, Phone.FEATURE_ENABLE_SUPL)) {
2186 networkType = ConnectivityManager.TYPE_MOBILE_SUPL;
2187 } else if (TextUtils.equals(feature, Phone.FEATURE_ENABLE_DUN) ||
2188 TextUtils.equals(feature, Phone.FEATURE_ENABLE_DUN_ALWAYS)) {
2189 networkType = ConnectivityManager.TYPE_MOBILE_DUN;
2190 } else if (TextUtils.equals(feature, Phone.FEATURE_ENABLE_HIPRI)) {
2191 networkType = ConnectivityManager.TYPE_MOBILE_HIPRI;
2192 } else if (TextUtils.equals(feature, Phone.FEATURE_ENABLE_FOTA)) {
2193 networkType = ConnectivityManager.TYPE_MOBILE_FOTA;
2194 } else if (TextUtils.equals(feature, Phone.FEATURE_ENABLE_IMS)) {
2195 networkType = ConnectivityManager.TYPE_MOBILE_IMS;
2196 } else if (TextUtils.equals(feature, Phone.FEATURE_ENABLE_CBS)) {
2197 networkType = ConnectivityManager.TYPE_MOBILE_CBS;
2198 }
2199 return networkType;
2200 }
The Android Open Source Project28527d22009-03-03 19:31:44 -08002201}