blob: 428c94f8bba5cea18c24d9a5458e78959414fba2 [file] [log] [blame]
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001/*
2 * Copyright (C) 2008 The Android Open Source Project
3 *
4 * Licensed under the Apache License, Version 2.0 (the "License");
5 * you may not use this file except in compliance with the License.
6 * You may obtain a copy of the License at
7 *
8 * http://www.apache.org/licenses/LICENSE-2.0
9 *
10 * Unless required by applicable law or agreed to in writing, software
11 * distributed under the License is distributed on an "AS IS" BASIS,
12 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13 * See the License for the specific language governing permissions and
14 * limitations under the License.
15 */
16
17package com.android.server;
18
Jaikumar Ganesh15c74392010-12-21 22:31:44 -080019import android.bluetooth.BluetoothTetheringDataTracker;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080020import android.content.ContentResolver;
21import android.content.Context;
22import android.content.Intent;
23import android.content.pm.PackageManager;
Robert Greenwalt434203a2010-10-11 16:00:27 -070024import android.database.ContentObserver;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080025import android.net.ConnectivityManager;
Robert Greenwaltda3d5e62010-12-06 13:56:24 -080026import android.net.DummyDataStateTracker;
Benoit Goby08c39c62010-12-22 14:29:40 -080027import android.net.EthernetDataTracker;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080028import android.net.IConnectivityManager;
Jaikumar Ganesh15c74392010-12-21 22:31:44 -080029import android.net.LinkProperties;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080030import android.net.MobileDataStateTracker;
Robert Greenwaltd55a6b42011-03-25 13:09:25 -070031import android.net.NetworkConfig;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080032import android.net.NetworkInfo;
33import android.net.NetworkStateTracker;
Robert Greenwalt585ac0f2010-08-27 09:24:29 -070034import android.net.NetworkUtils;
Robert Greenwalt434203a2010-10-11 16:00:27 -070035import android.net.Proxy;
36import android.net.ProxyProperties;
Robert Greenwaltaa70f102011-04-28 14:28:50 -070037import android.net.RouteInfo;
Hung-ying Tyan6b818de2011-01-19 16:48:38 +080038import android.net.vpn.VpnManager;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080039import android.net.wifi.WifiStateTracker;
40import android.os.Binder;
41import android.os.Handler;
Wink Savillebb08caf2010-09-02 19:23:52 -070042import android.os.HandlerThread;
Robert Greenwalt42acef32009-08-12 16:08:25 -070043import android.os.IBinder;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080044import android.os.Looper;
45import android.os.Message;
Robert Greenwalt14f2ef42010-06-15 12:19:37 -070046import android.os.PowerManager;
Robert Greenwalt42acef32009-08-12 16:08:25 -070047import android.os.RemoteException;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080048import android.os.ServiceManager;
49import android.os.SystemProperties;
50import android.provider.Settings;
Robert Greenwalt42acef32009-08-12 16:08:25 -070051import android.text.TextUtils;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080052import android.util.EventLog;
Joe Onorato8a9b2202010-02-26 18:56:32 -080053import android.util.Slog;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080054
Robert Greenwalt42acef32009-08-12 16:08:25 -070055import com.android.internal.telephony.Phone;
Robert Greenwaltd0e18ff2010-01-26 11:40:34 -080056import com.android.server.connectivity.Tethering;
57
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080058import java.io.FileDescriptor;
Irfan Sheriffd649c122010-06-09 15:39:36 -070059import java.io.FileWriter;
60import java.io.IOException;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080061import java.io.PrintWriter;
Robert Greenwalt47f69fe2010-06-15 15:43:39 -070062import java.net.InetAddress;
63import java.net.UnknownHostException;
Robert Greenwalt42acef32009-08-12 16:08:25 -070064import java.util.ArrayList;
Robert Greenwalt47f69fe2010-06-15 15:43:39 -070065import java.util.Collection;
Robert Greenwaltd825ea42010-12-29 16:15:02 -080066import java.util.concurrent.atomic.AtomicBoolean;
Robert Greenwalt4e8dfef2010-09-20 14:35:25 -070067import java.util.GregorianCalendar;
Robert Greenwalt42acef32009-08-12 16:08:25 -070068import java.util.List;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080069
70/**
71 * @hide
72 */
73public class ConnectivityService extends IConnectivityManager.Stub {
74
Robert Greenwaltba175a52010-10-05 19:12:26 -070075 private static final boolean DBG = true;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080076 private static final String TAG = "ConnectivityService";
77
Robert Greenwalt42acef32009-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 Greenwaltd0e18ff2010-01-26 11:40:34 -080084 private Tethering mTethering;
Robert Greenwaltc9d5fb72010-02-25 12:29:30 -080085 private boolean mTetheringConfigValid = false;
Robert Greenwaltd0e18ff2010-01-26 11:40:34 -080086
The Android Open Source Project9066cfe2009-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 Greenwalt42acef32009-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 Sheriffa2a1b912010-06-07 09:03:04 -0700100 private WifiWatchdogService mWifiWatchdogService;
101
Robert Greenwalt42acef32009-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 Project9066cfe2009-03-03 19:31:44 -0800107 private Context mContext;
108 private int mNetworkPreference;
Robert Greenwalt42acef32009-08-12 16:08:25 -0700109 private int mActiveDefaultNetwork = -1;
Robert Greenwaltd7085fc2010-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 Project9066cfe2009-03-03 19:31:44 -0800115
116 private int mNumDnsEntries;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800117
118 private boolean mTestMode;
Joe Onorato00092872010-09-01 21:18:22 -0700119 private static ConnectivityService sServiceInstance;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800120
Robert Greenwaltd825ea42010-12-29 16:15:02 -0800121 private AtomicBoolean mBackgroundDataEnabled = new AtomicBoolean(true);
122
Robert Greenwalt8dcc28b2010-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 Greenwaltf3331232010-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 Greenwalt434203a2010-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 Greenwaltd55a6b42011-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 Greenwalt42acef32009-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 Lockwood0f79b542009-08-14 14:18:49 -0400208 private boolean mSystemReady;
Dianne Hackborn1c633fc2009-12-08 19:45:14 -0800209 private Intent mInitialBroadcast;
Mike Lockwood0f79b542009-08-14 14:18:49 -0400210
Robert Greenwalt14f2ef42010-06-15 12:19:37 -0700211 private PowerManager.WakeLock mNetTransitionWakeLock;
212 private String mNetTransitionWakeLockCausedBy = "";
213 private int mNetTransitionWakeLockSerialNumber;
214 private int mNetTransitionWakeLockTimeout;
215
Robert Greenwalte90aa5e2010-09-01 11:34:05 -0700216 private InetAddress mDefaultDns;
217
Robert Greenwalt4e8dfef2010-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 Greenwalt434203a2010-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 Greenwaltd55a6b42011-03-25 13:09:25 -0700230 NetworkConfig[] mNetConfigs;
Robert Greenwalt5154ae762009-10-30 14:17:42 -0700231 int mNetworksDefined;
Robert Greenwalt42acef32009-08-12 16:08:25 -0700232
Robert Greenwalt511288a2009-12-07 11:33:18 -0800233 private static class RadioAttributes {
Robert Greenwalt42acef32009-08-12 16:08:25 -0700234 public int mSimultaneity;
235 public int mType;
236 public RadioAttributes(String init) {
237 String fragments[] = init.split(",");
Robert Greenwalt5154ae762009-10-30 14:17:42 -0700238 mType = Integer.parseInt(fragments[0]);
239 mSimultaneity = Integer.parseInt(fragments[1]);
Robert Greenwalt42acef32009-08-12 16:08:25 -0700240 }
241 }
242 RadioAttributes[] mRadioAttributes;
243
Wink Savillebb08caf2010-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 Project9066cfe2009-03-03 19:31:44 -0800247 }
Wink Savillebb08caf2010-09-02 19:23:52 -0700248 return sServiceInstance;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800249 }
Robert Greenwalt86e9e552009-07-16 17:21:39 -0700250
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800251 private ConnectivityService(Context context) {
Wink Savilleed9c02b2010-12-03 12:01:38 -0800252 if (DBG) log("ConnectivityService starting up");
Robert Greenwaltde8383c2010-01-14 17:47:58 -0800253
Wink Savillebb08caf2010-09-02 19:23:52 -0700254 HandlerThread handlerThread = new HandlerThread("ConnectivityServiceThread");
255 handlerThread.start();
256 mHandler = new MyHandler(handlerThread.getLooper());
257
Robert Greenwaltd825ea42010-12-29 16:15:02 -0800258 mBackgroundDataEnabled.set(Settings.Secure.getInt(context.getContentResolver(),
259 Settings.Secure.BACKGROUND_DATA, 1) == 1);
260
Robert Greenwaltde8383c2010-01-14 17:47:58 -0800261 // setup our unique device name
Robert Greenwalt733c6292010-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 Greenwaltde8383c2010-01-14 17:47:58 -0800269 }
270
Robert Greenwalte90aa5e2010-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 Greenwalte5903732011-02-22 16:00:42 -0800279 mDefaultDns = NetworkUtils.numericToInetAddress(dns);
280 } catch (IllegalArgumentException e) {
Wink Savilleed9c02b2010-12-03 12:01:38 -0800281 loge("Error setting defaultDns using " + dns);
Robert Greenwalte90aa5e2010-09-01 11:34:05 -0700282 }
283
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800284 mContext = context;
Robert Greenwalt14f2ef42010-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 Greenwalt42acef32009-08-12 16:08:25 -0700291 mNetTrackers = new NetworkStateTracker[
292 ConnectivityManager.MAX_NETWORK_TYPE+1];
Robert Greenwalt86e9e552009-07-16 17:21:39 -0700293
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800294 mNetworkPreference = getPersistedNetworkPreference();
Robert Greenwalt86e9e552009-07-16 17:21:39 -0700295
Robert Greenwalt5154ae762009-10-30 14:17:42 -0700296 mRadioAttributes = new RadioAttributes[ConnectivityManager.MAX_RADIO_TYPE+1];
Robert Greenwaltd55a6b42011-03-25 13:09:25 -0700297 mNetConfigs = new NetworkConfig[ConnectivityManager.MAX_NETWORK_TYPE+1];
Robert Greenwalt5154ae762009-10-30 14:17:42 -0700298
Robert Greenwalt42acef32009-08-12 16:08:25 -0700299 // Load device network attributes from resources
Robert Greenwalt42acef32009-08-12 16:08:25 -0700300 String[] raStrings = context.getResources().getStringArray(
301 com.android.internal.R.array.radioAttributes);
Robert Greenwalt5154ae762009-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 Savilleed9c02b2010-12-03 12:01:38 -0800305 loge("Error in radioAttributes - ignoring attempt to define type " + r.mType);
Robert Greenwalt5154ae762009-10-30 14:17:42 -0700306 continue;
307 }
308 if (mRadioAttributes[r.mType] != null) {
Wink Savilleed9c02b2010-12-03 12:01:38 -0800309 loge("Error in radioAttributes - ignoring attempt to redefine type " +
Robert Greenwalt5154ae762009-10-30 14:17:42 -0700310 r.mType);
311 continue;
312 }
Robert Greenwalt42acef32009-08-12 16:08:25 -0700313 mRadioAttributes[r.mType] = r;
314 }
315
Robert Greenwalt5154ae762009-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 Greenwaltd55a6b42011-03-25 13:09:25 -0700320 NetworkConfig n = new NetworkConfig(naString);
Wink Saville975c8482011-04-07 14:23:45 -0700321 if (n.type > ConnectivityManager.MAX_NETWORK_TYPE) {
Wink Savilleed9c02b2010-12-03 12:01:38 -0800322 loge("Error in networkAttributes - ignoring attempt to define type " +
Wink Saville975c8482011-04-07 14:23:45 -0700323 n.type);
Robert Greenwalt5154ae762009-10-30 14:17:42 -0700324 continue;
Robert Greenwalt42acef32009-08-12 16:08:25 -0700325 }
Wink Saville975c8482011-04-07 14:23:45 -0700326 if (mNetConfigs[n.type] != null) {
Wink Savilleed9c02b2010-12-03 12:01:38 -0800327 loge("Error in networkAttributes - ignoring attempt to redefine type " +
Wink Saville975c8482011-04-07 14:23:45 -0700328 n.type);
Robert Greenwalt5154ae762009-10-30 14:17:42 -0700329 continue;
330 }
Wink Saville975c8482011-04-07 14:23:45 -0700331 if (mRadioAttributes[n.radio] == null) {
Wink Savilleed9c02b2010-12-03 12:01:38 -0800332 loge("Error in networkAttributes - ignoring attempt to use undefined " +
Wink Saville975c8482011-04-07 14:23:45 -0700333 "radio " + n.radio + " in network type " + n.type);
Robert Greenwalt5154ae762009-10-30 14:17:42 -0700334 continue;
335 }
Wink Saville975c8482011-04-07 14:23:45 -0700336 mNetConfigs[n.type] = n;
Robert Greenwalt5154ae762009-10-30 14:17:42 -0700337 mNetworksDefined++;
338 } catch(Exception e) {
339 // ignore it - leave the entry null
Robert Greenwalt42acef32009-08-12 16:08:25 -0700340 }
341 }
342
Robert Greenwalt5154ae762009-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 Greenwaltd55a6b42011-03-25 13:09:25 -0700350 for (NetworkConfig na : mNetConfigs) {
Robert Greenwalt5154ae762009-10-30 14:17:42 -0700351 if (na == null) continue;
Wink Saville975c8482011-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 Greenwalt5154ae762009-10-30 14:17:42 -0700356 }
357 continue;
358 }
Wink Saville975c8482011-04-07 14:23:45 -0700359 mPriorityList[insertionPoint--] = na.type;
Robert Greenwalt5154ae762009-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 Greenwalt42acef32009-08-12 16:08:25 -0700368 mNetRequestersPids[i] = new ArrayList();
369 }
370
371 mFeatureUsers = new ArrayList();
372
Robert Greenwalt5154ae762009-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 Project9066cfe2009-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 Greenwalt5154ae762009-10-30 14:17:42 -0700384 for (int netType : mPriorityList) {
Wink Saville975c8482011-04-07 14:23:45 -0700385 switch (mNetConfigs[netType].radio) {
Robert Greenwalt5154ae762009-10-30 14:17:42 -0700386 case ConnectivityManager.TYPE_WIFI:
Wink Savilleed9c02b2010-12-03 12:01:38 -0800387 if (DBG) log("Starting Wifi Service.");
Wink Savillec7a98342010-08-13 16:11:42 -0700388 WifiStateTracker wst = new WifiStateTracker();
Irfan Sheriff0d255342010-07-28 09:35:20 -0700389 WifiService wifiService = new WifiService(context);
Robert Greenwalt5154ae762009-10-30 14:17:42 -0700390 ServiceManager.addService(Context.WIFI_SERVICE, wifiService);
Irfan Sheriff0d255342010-07-28 09:35:20 -0700391 wifiService.checkAndStartWifi();
Robert Greenwalt5154ae762009-10-30 14:17:42 -0700392 mNetTrackers[ConnectivityManager.TYPE_WIFI] = wst;
Wink Savillec7a98342010-08-13 16:11:42 -0700393 wst.startMonitoring(context, mHandler);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800394
Irfan Sheriffa2a1b912010-06-07 09:03:04 -0700395 //TODO: as part of WWS refactor, create only when needed
Irfan Sheriff0d255342010-07-28 09:35:20 -0700396 mWifiWatchdogService = new WifiWatchdogService(context);
Irfan Sheriffa2a1b912010-06-07 09:03:04 -0700397
Robert Greenwalt5154ae762009-10-30 14:17:42 -0700398 break;
399 case ConnectivityManager.TYPE_MOBILE:
Wink Savillec7a98342010-08-13 16:11:42 -0700400 mNetTrackers[netType] = new MobileDataStateTracker(netType,
Wink Saville975c8482011-04-07 14:23:45 -0700401 mNetConfigs[netType].name);
Wink Savillec7a98342010-08-13 16:11:42 -0700402 mNetTrackers[netType].startMonitoring(context, mHandler);
Robert Greenwalt5154ae762009-10-30 14:17:42 -0700403 break;
Robert Greenwaltda3d5e62010-12-06 13:56:24 -0800404 case ConnectivityManager.TYPE_DUMMY:
405 mNetTrackers[netType] = new DummyDataStateTracker(netType,
Wink Saville975c8482011-04-07 14:23:45 -0700406 mNetConfigs[netType].name);
Robert Greenwaltda3d5e62010-12-06 13:56:24 -0800407 mNetTrackers[netType].startMonitoring(context, mHandler);
408 break;
Jaikumar Ganesh15c74392010-12-21 22:31:44 -0800409 case ConnectivityManager.TYPE_BLUETOOTH:
410 mNetTrackers[netType] = BluetoothTetheringDataTracker.getInstance();
411 mNetTrackers[netType].startMonitoring(context, mHandler);
412 break;
Benoit Goby08c39c62010-12-22 14:29:40 -0800413 case ConnectivityManager.TYPE_ETHERNET:
414 mNetTrackers[netType] = EthernetDataTracker.getInstance();
415 mNetTrackers[netType].startMonitoring(context, mHandler);
416 break;
Robert Greenwalt5154ae762009-10-30 14:17:42 -0700417 default:
Wink Savilleed9c02b2010-12-03 12:01:38 -0800418 loge("Trying to create a DataStateTracker for an unknown radio type " +
Wink Saville975c8482011-04-07 14:23:45 -0700419 mNetConfigs[netType].radio);
Robert Greenwalt5154ae762009-10-30 14:17:42 -0700420 continue;
421 }
422 }
Robert Greenwaltd0e18ff2010-01-26 11:40:34 -0800423
Robert Greenwaltdfadaea2010-03-11 15:03:08 -0800424 mTethering = new Tethering(mContext, mHandler.getLooper());
Robert Greenwaltc9d5fb72010-02-25 12:29:30 -0800425 mTetheringConfigValid = (((mNetTrackers[ConnectivityManager.TYPE_MOBILE_DUN] != null) ||
426 !mTethering.isDunRequired()) &&
427 (mTethering.getTetherableUsbRegexs().length != 0 ||
Danica Chang6fdd0c62010-08-11 14:54:43 -0700428 mTethering.getTetherableWifiRegexs().length != 0 ||
429 mTethering.getTetherableBluetoothRegexs().length != 0) &&
Robert Greenwaltc9d5fb72010-02-25 12:29:30 -0800430 mTethering.getUpstreamIfaceRegexs().length != 0);
431
Robert Greenwalt4e8dfef2010-09-20 14:35:25 -0700432 if (DBG) {
433 mInetLog = new ArrayList();
434 }
Robert Greenwalt434203a2010-10-11 16:00:27 -0700435
436 mSettingsObserver = new SettingsObserver(mHandler, EVENT_APPLY_GLOBAL_HTTP_PROXY);
437 mSettingsObserver.observe(mContext);
Robert Greenwaltb7090d62010-12-02 11:31:00 -0800438
439 loadGlobalProxy();
Hung-ying Tyan6b818de2011-01-19 16:48:38 +0800440
441 VpnManager.startVpnService(context);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800442 }
443
Robert Greenwalt5154ae762009-10-30 14:17:42 -0700444
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800445 /**
Robert Greenwalt86e9e552009-07-16 17:21:39 -0700446 * Sets the preferred network.
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800447 * @param preference the new preference
448 */
Robert Greenwalt8dcc28b2010-09-23 10:05:56 -0700449 public void setNetworkPreference(int preference) {
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800450 enforceChangePermission();
Robert Greenwalt8dcc28b2010-09-23 10:05:56 -0700451
452 mHandler.sendMessage(mHandler.obtainMessage(EVENT_SET_NETWORK_PREFERENCE, preference, 0));
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800453 }
454
455 public int getNetworkPreference() {
456 enforceAccessPermission();
Robert Greenwalt8dcc28b2010-09-23 10:05:56 -0700457 int preference;
458 synchronized(this) {
459 preference = mNetworkPreference;
460 }
461 return preference;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800462 }
463
Robert Greenwalt8dcc28b2010-09-23 10:05:56 -0700464 private void handleSetNetworkPreference(int preference) {
465 if (ConnectivityManager.isNetworkTypeValid(preference) &&
Robert Greenwaltd55a6b42011-03-25 13:09:25 -0700466 mNetConfigs[preference] != null &&
467 mNetConfigs[preference].isDefault()) {
Robert Greenwalt8dcc28b2010-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 Project9066cfe2009-03-03 19:31:44 -0800477 }
Robert Greenwalt86e9e552009-07-16 17:21:39 -0700478
The Android Open Source Project9066cfe2009-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 Greenwalt86e9e552009-07-16 17:21:39 -0700490
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800491 /**
Robert Greenwalt86e9e552009-07-16 17:21:39 -0700492 * Make the state of network connectivity conform to the preference settings
The Android Open Source Project9066cfe2009-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 Greenwalt42acef32009-08-12 16:08:25 -0700499 if (mNetTrackers[mNetworkPreference].getNetworkInfo().isConnected())
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800500 return;
501
Robert Greenwalt42acef32009-08-12 16:08:25 -0700502 if (!mNetTrackers[mNetworkPreference].isAvailable())
503 return;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800504
Robert Greenwalt42acef32009-08-12 16:08:25 -0700505 for (int t=0; t <= ConnectivityManager.MAX_RADIO_TYPE; t++) {
Robert Greenwalt5154ae762009-10-30 14:17:42 -0700506 if (t != mNetworkPreference && mNetTrackers[t] != null &&
Robert Greenwalt42acef32009-08-12 16:08:25 -0700507 mNetTrackers[t].getNetworkInfo().isConnected()) {
Robert Greenwaltec9fe462009-08-20 15:25:14 -0700508 if (DBG) {
Wink Savilleed9c02b2010-12-03 12:01:38 -0800509 log("tearing down " + mNetTrackers[t].getNetworkInfo() +
Robert Greenwaltec9fe462009-08-20 15:25:14 -0700510 " in enforcePreference");
511 }
Robert Greenwalt42acef32009-08-12 16:08:25 -0700512 teardown(mNetTrackers[t]);
The Android Open Source Project9066cfe2009-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 Greenwalt86e9e552009-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 Project9066cfe2009-03-03 19:31:44 -0800532 */
533 public NetworkInfo getActiveNetworkInfo() {
534 enforceAccessPermission();
Robert Greenwalt42acef32009-08-12 16:08:25 -0700535 for (int type=0; type <= ConnectivityManager.MAX_NETWORK_TYPE; type++) {
Robert Greenwaltd55a6b42011-03-25 13:09:25 -0700536 if (mNetConfigs[type] == null || !mNetConfigs[type].isDefault()) {
Robert Greenwalt42acef32009-08-12 16:08:25 -0700537 continue;
538 }
539 NetworkStateTracker t = mNetTrackers[type];
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800540 NetworkInfo info = t.getNetworkInfo();
541 if (info.isConnected()) {
Wink Savilleed9c02b2010-12-03 12:01:38 -0800542 if (DBG && type != mActiveDefaultNetwork) {
543 loge("connected default network is not mActiveDefaultNetwork!");
544 }
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800545 return info;
546 }
547 }
548 return null;
549 }
550
551 public NetworkInfo getNetworkInfo(int networkType) {
552 enforceAccessPermission();
553 if (ConnectivityManager.isNetworkTypeValid(networkType)) {
554 NetworkStateTracker t = mNetTrackers[networkType];
555 if (t != null)
556 return t.getNetworkInfo();
557 }
558 return null;
559 }
560
561 public NetworkInfo[] getAllNetworkInfo() {
562 enforceAccessPermission();
Robert Greenwalt5154ae762009-10-30 14:17:42 -0700563 NetworkInfo[] result = new NetworkInfo[mNetworksDefined];
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800564 int i = 0;
565 for (NetworkStateTracker t : mNetTrackers) {
Robert Greenwalt5154ae762009-10-30 14:17:42 -0700566 if(t != null) result[i++] = t.getNetworkInfo();
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800567 }
568 return result;
569 }
570
Robert Greenwaltd192dad2010-09-14 09:18:02 -0700571 /**
572 * Return LinkProperties for the active (i.e., connected) default
573 * network interface. It is assumed that at most one default network
574 * is active at a time. If more than one is active, it is indeterminate
575 * which will be returned.
576 * @return the ip properties for the active network, or {@code null} if
577 * none is active
578 */
579 public LinkProperties getActiveLinkProperties() {
580 enforceAccessPermission();
581 for (int type=0; type <= ConnectivityManager.MAX_NETWORK_TYPE; type++) {
Robert Greenwaltd55a6b42011-03-25 13:09:25 -0700582 if (mNetConfigs[type] == null || !mNetConfigs[type].isDefault()) {
Robert Greenwaltd192dad2010-09-14 09:18:02 -0700583 continue;
584 }
585 NetworkStateTracker t = mNetTrackers[type];
586 NetworkInfo info = t.getNetworkInfo();
587 if (info.isConnected()) {
588 return t.getLinkProperties();
589 }
590 }
591 return null;
592 }
593
594 public LinkProperties getLinkProperties(int networkType) {
595 enforceAccessPermission();
596 if (ConnectivityManager.isNetworkTypeValid(networkType)) {
597 NetworkStateTracker t = mNetTrackers[networkType];
598 if (t != null) return t.getLinkProperties();
599 }
600 return null;
601 }
602
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800603 public boolean setRadios(boolean turnOn) {
604 boolean result = true;
605 enforceChangePermission();
606 for (NetworkStateTracker t : mNetTrackers) {
Robert Greenwalt5154ae762009-10-30 14:17:42 -0700607 if (t != null) result = t.setRadio(turnOn) && result;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800608 }
609 return result;
610 }
611
612 public boolean setRadio(int netType, boolean turnOn) {
613 enforceChangePermission();
614 if (!ConnectivityManager.isNetworkTypeValid(netType)) {
615 return false;
616 }
617 NetworkStateTracker tracker = mNetTrackers[netType];
618 return tracker != null && tracker.setRadio(turnOn);
619 }
620
Robert Greenwalt9c75d4a2009-09-27 17:27:04 -0700621 /**
622 * Used to notice when the calling process dies so we can self-expire
623 *
624 * Also used to know if the process has cleaned up after itself when
625 * our auto-expire timer goes off. The timer has a link to an object.
626 *
627 */
Robert Greenwalt42acef32009-08-12 16:08:25 -0700628 private class FeatureUser implements IBinder.DeathRecipient {
629 int mNetworkType;
630 String mFeature;
631 IBinder mBinder;
632 int mPid;
633 int mUid;
Robert Greenwaltb9285352009-12-21 18:24:07 -0800634 long mCreateTime;
Robert Greenwalt42acef32009-08-12 16:08:25 -0700635
636 FeatureUser(int type, String feature, IBinder binder) {
637 super();
638 mNetworkType = type;
639 mFeature = feature;
640 mBinder = binder;
641 mPid = getCallingPid();
642 mUid = getCallingUid();
Robert Greenwaltb9285352009-12-21 18:24:07 -0800643 mCreateTime = System.currentTimeMillis();
Robert Greenwalt9c75d4a2009-09-27 17:27:04 -0700644
Robert Greenwalt42acef32009-08-12 16:08:25 -0700645 try {
646 mBinder.linkToDeath(this, 0);
647 } catch (RemoteException e) {
648 binderDied();
649 }
650 }
651
652 void unlinkDeathRecipient() {
653 mBinder.unlinkToDeath(this, 0);
654 }
655
656 public void binderDied() {
Wink Savilleed9c02b2010-12-03 12:01:38 -0800657 log("ConnectivityService FeatureUser binderDied(" +
Robert Greenwaltb9285352009-12-21 18:24:07 -0800658 mNetworkType + ", " + mFeature + ", " + mBinder + "), created " +
659 (System.currentTimeMillis() - mCreateTime) + " mSec ago");
Robert Greenwalt9c75d4a2009-09-27 17:27:04 -0700660 stopUsingNetworkFeature(this, false);
Robert Greenwalt42acef32009-08-12 16:08:25 -0700661 }
662
Robert Greenwalt9c75d4a2009-09-27 17:27:04 -0700663 public void expire() {
Wink Savilleed9c02b2010-12-03 12:01:38 -0800664 log("ConnectivityService FeatureUser expire(" +
Robert Greenwaltb9285352009-12-21 18:24:07 -0800665 mNetworkType + ", " + mFeature + ", " + mBinder +"), created " +
666 (System.currentTimeMillis() - mCreateTime) + " mSec ago");
Robert Greenwalt9c75d4a2009-09-27 17:27:04 -0700667 stopUsingNetworkFeature(this, false);
668 }
Robert Greenwaltb9285352009-12-21 18:24:07 -0800669
670 public String toString() {
671 return "FeatureUser("+mNetworkType+","+mFeature+","+mPid+","+mUid+"), created " +
672 (System.currentTimeMillis() - mCreateTime) + " mSec ago";
673 }
Robert Greenwalt42acef32009-08-12 16:08:25 -0700674 }
675
Robert Greenwalt9c75d4a2009-09-27 17:27:04 -0700676 // javadoc from interface
Robert Greenwalt42acef32009-08-12 16:08:25 -0700677 public int startUsingNetworkFeature(int networkType, String feature,
678 IBinder binder) {
679 if (DBG) {
Wink Savilleed9c02b2010-12-03 12:01:38 -0800680 log("startUsingNetworkFeature for net " + networkType + ": " + feature);
Robert Greenwalt42acef32009-08-12 16:08:25 -0700681 }
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800682 enforceChangePermission();
Robert Greenwalt5154ae762009-10-30 14:17:42 -0700683 if (!ConnectivityManager.isNetworkTypeValid(networkType) ||
Robert Greenwaltd55a6b42011-03-25 13:09:25 -0700684 mNetConfigs[networkType] == null) {
Robert Greenwalt42acef32009-08-12 16:08:25 -0700685 return Phone.APN_REQUEST_FAILED;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800686 }
Robert Greenwalt42acef32009-08-12 16:08:25 -0700687
Robert Greenwalt9c75d4a2009-09-27 17:27:04 -0700688 FeatureUser f = new FeatureUser(networkType, feature, binder);
Robert Greenwalt42acef32009-08-12 16:08:25 -0700689
690 // TODO - move this into the MobileDataStateTracker
691 int usedNetworkType = networkType;
692 if(networkType == ConnectivityManager.TYPE_MOBILE) {
Wink Saville9d7d6282011-03-12 14:52:01 -0800693 usedNetworkType = convertFeatureToNetworkType(feature);
694 if (usedNetworkType < 0) {
695 Slog.e(TAG, "Can't match any netTracker!");
696 usedNetworkType = networkType;
Robert Greenwalt42acef32009-08-12 16:08:25 -0700697 }
698 }
699 NetworkStateTracker network = mNetTrackers[usedNetworkType];
700 if (network != null) {
Robert Greenwalt0be1e982010-12-15 13:26:33 -0800701 Integer currentPid = new Integer(getCallingPid());
Robert Greenwalt42acef32009-08-12 16:08:25 -0700702 if (usedNetworkType != networkType) {
Robert Greenwalt42acef32009-08-12 16:08:25 -0700703 NetworkStateTracker radio = mNetTrackers[networkType];
704 NetworkInfo ni = network.getNetworkInfo();
705
706 if (ni.isAvailable() == false) {
Wink Savilleed9c02b2010-12-03 12:01:38 -0800707 if (DBG) log("special network not available");
Robert Greenwalte32e8122010-12-29 14:35:21 -0800708 if (!TextUtils.equals(feature,Phone.FEATURE_ENABLE_DUN_ALWAYS)) {
709 return Phone.APN_TYPE_NOT_AVAILABLE;
710 } else {
711 // else make the attempt anyway - probably giving REQUEST_STARTED below
712 }
Robert Greenwalt42acef32009-08-12 16:08:25 -0700713 }
714
Robert Greenwalt9c75d4a2009-09-27 17:27:04 -0700715 synchronized(this) {
716 mFeatureUsers.add(f);
717 if (!mNetRequestersPids[usedNetworkType].contains(currentPid)) {
718 // this gets used for per-pid dns when connected
719 mNetRequestersPids[usedNetworkType].add(currentPid);
720 }
Robert Greenwalt42acef32009-08-12 16:08:25 -0700721 }
Robert Greenwalt9c75d4a2009-09-27 17:27:04 -0700722
Robert Greenwaltf2102f72011-05-03 19:02:44 -0700723 int restoreTimer = getRestoreDefaultNetworkDelay(usedNetworkType);
724
725 if (restoreTimer >= 0) {
726 mHandler.sendMessageDelayed(
727 mHandler.obtainMessage(EVENT_RESTORE_DEFAULT_NETWORK, f), restoreTimer);
728 }
Robert Greenwalt42acef32009-08-12 16:08:25 -0700729
Robert Greenwalta64bf832009-08-19 20:19:33 -0700730 if ((ni.isConnectedOrConnecting() == true) &&
731 !network.isTeardownRequested()) {
Robert Greenwalt42acef32009-08-12 16:08:25 -0700732 if (ni.isConnected() == true) {
733 // add the pid-specific dns
Robert Greenwaltc76b8fa2010-07-23 15:46:26 -0700734 handleDnsConfigurationChange(networkType);
Wink Savilleed9c02b2010-12-03 12:01:38 -0800735 if (DBG) log("special network already active");
Robert Greenwalt42acef32009-08-12 16:08:25 -0700736 return Phone.APN_ALREADY_ACTIVE;
737 }
Wink Savilleed9c02b2010-12-03 12:01:38 -0800738 if (DBG) log("special network already connecting");
Robert Greenwalt42acef32009-08-12 16:08:25 -0700739 return Phone.APN_REQUEST_STARTED;
740 }
741
742 // check if the radio in play can make another contact
743 // assume if cannot for now
744
Wink Savilleed9c02b2010-12-03 12:01:38 -0800745 if (DBG) log("reconnecting to special network");
Robert Greenwalt42acef32009-08-12 16:08:25 -0700746 network.reconnect();
747 return Phone.APN_REQUEST_STARTED;
748 } else {
Robert Greenwalt0be1e982010-12-15 13:26:33 -0800749 // need to remember this unsupported request so we respond appropriately on stop
750 synchronized(this) {
751 mFeatureUsers.add(f);
752 if (!mNetRequestersPids[usedNetworkType].contains(currentPid)) {
753 // this gets used for per-pid dns when connected
754 mNetRequestersPids[usedNetworkType].add(currentPid);
755 }
756 }
Robert Greenwalt02648a42010-05-18 10:52:51 -0700757 return -1;
Robert Greenwalt42acef32009-08-12 16:08:25 -0700758 }
759 }
760 return Phone.APN_TYPE_NOT_AVAILABLE;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800761 }
762
Robert Greenwalt9c75d4a2009-09-27 17:27:04 -0700763 // javadoc from interface
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800764 public int stopUsingNetworkFeature(int networkType, String feature) {
Robert Greenwaltb8f16342009-10-06 17:52:40 -0700765 enforceChangePermission();
766
Robert Greenwalt9c75d4a2009-09-27 17:27:04 -0700767 int pid = getCallingPid();
768 int uid = getCallingUid();
769
770 FeatureUser u = null;
771 boolean found = false;
772
773 synchronized(this) {
774 for (int i = 0; i < mFeatureUsers.size() ; i++) {
775 u = (FeatureUser)mFeatureUsers.get(i);
776 if (uid == u.mUid && pid == u.mPid &&
777 networkType == u.mNetworkType &&
778 TextUtils.equals(feature, u.mFeature)) {
779 found = true;
780 break;
781 }
782 }
783 }
784 if (found && u != null) {
785 // stop regardless of how many other time this proc had called start
786 return stopUsingNetworkFeature(u, true);
787 } else {
788 // none found!
Wink Savilleed9c02b2010-12-03 12:01:38 -0800789 if (DBG) log("ignoring stopUsingNetworkFeature - not a live request");
Robert Greenwalt9c75d4a2009-09-27 17:27:04 -0700790 return 1;
791 }
Robert Greenwalt42acef32009-08-12 16:08:25 -0700792 }
793
Robert Greenwalt9c75d4a2009-09-27 17:27:04 -0700794 private int stopUsingNetworkFeature(FeatureUser u, boolean ignoreDups) {
795 int networkType = u.mNetworkType;
796 String feature = u.mFeature;
797 int pid = u.mPid;
798 int uid = u.mUid;
799
800 NetworkStateTracker tracker = null;
801 boolean callTeardown = false; // used to carry our decision outside of sync block
802
Robert Greenwalt42acef32009-08-12 16:08:25 -0700803 if (DBG) {
Wink Savilleed9c02b2010-12-03 12:01:38 -0800804 log("stopUsingNetworkFeature for net " + networkType +
Robert Greenwalt42acef32009-08-12 16:08:25 -0700805 ": " + feature);
806 }
Robert Greenwaltb8f16342009-10-06 17:52:40 -0700807
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800808 if (!ConnectivityManager.isNetworkTypeValid(networkType)) {
809 return -1;
810 }
Robert Greenwalt42acef32009-08-12 16:08:25 -0700811
Robert Greenwalt9c75d4a2009-09-27 17:27:04 -0700812 // need to link the mFeatureUsers list with the mNetRequestersPids state in this
813 // sync block
814 synchronized(this) {
815 // check if this process still has an outstanding start request
816 if (!mFeatureUsers.contains(u)) {
Wink Savilleed9c02b2010-12-03 12:01:38 -0800817 if (DBG) log("ignoring - this process has no outstanding requests");
Robert Greenwalt42acef32009-08-12 16:08:25 -0700818 return 1;
819 }
Robert Greenwalt9c75d4a2009-09-27 17:27:04 -0700820 u.unlinkDeathRecipient();
821 mFeatureUsers.remove(mFeatureUsers.indexOf(u));
822 // If we care about duplicate requests, check for that here.
823 //
824 // This is done to support the extension of a request - the app
825 // can request we start the network feature again and renew the
826 // auto-shutoff delay. Normal "stop" calls from the app though
827 // do not pay attention to duplicate requests - in effect the
828 // API does not refcount and a single stop will counter multiple starts.
829 if (ignoreDups == false) {
830 for (int i = 0; i < mFeatureUsers.size() ; i++) {
831 FeatureUser x = (FeatureUser)mFeatureUsers.get(i);
832 if (x.mUid == u.mUid && x.mPid == u.mPid &&
833 x.mNetworkType == u.mNetworkType &&
834 TextUtils.equals(x.mFeature, u.mFeature)) {
Wink Savilleed9c02b2010-12-03 12:01:38 -0800835 if (DBG) log("ignoring stopUsingNetworkFeature as dup is found");
Robert Greenwalt9c75d4a2009-09-27 17:27:04 -0700836 return 1;
837 }
838 }
Robert Greenwalt42acef32009-08-12 16:08:25 -0700839 }
Robert Greenwalt9c75d4a2009-09-27 17:27:04 -0700840
841 // TODO - move to MobileDataStateTracker
842 int usedNetworkType = networkType;
843 if (networkType == ConnectivityManager.TYPE_MOBILE) {
Wink Saville9d7d6282011-03-12 14:52:01 -0800844 usedNetworkType = convertFeatureToNetworkType(feature);
845 if (usedNetworkType < 0) {
846 usedNetworkType = networkType;
Robert Greenwalt9c75d4a2009-09-27 17:27:04 -0700847 }
848 }
849 tracker = mNetTrackers[usedNetworkType];
Robert Greenwalt5154ae762009-10-30 14:17:42 -0700850 if (tracker == null) {
Wink Savilleed9c02b2010-12-03 12:01:38 -0800851 if (DBG) log("ignoring - no known tracker for net type " + usedNetworkType);
Robert Greenwalt5154ae762009-10-30 14:17:42 -0700852 return -1;
853 }
854 if (usedNetworkType != networkType) {
Robert Greenwalt9c75d4a2009-09-27 17:27:04 -0700855 Integer currentPid = new Integer(pid);
Robert Greenwalt9c75d4a2009-09-27 17:27:04 -0700856 mNetRequestersPids[usedNetworkType].remove(currentPid);
Robert Greenwalt421c72b2009-12-17 14:54:59 -0800857 reassessPidDns(pid, true);
Robert Greenwalt9c75d4a2009-09-27 17:27:04 -0700858 if (mNetRequestersPids[usedNetworkType].size() != 0) {
Wink Savilleed9c02b2010-12-03 12:01:38 -0800859 if (DBG) log("not tearing down special network - " +
Robert Greenwalt9c75d4a2009-09-27 17:27:04 -0700860 "others still using it");
861 return 1;
862 }
863 callTeardown = true;
Robert Greenwalt19b9ab42011-01-10 11:58:31 -0800864 } else {
865 if (DBG) log("not a known feature - dropping");
Robert Greenwalt9c75d4a2009-09-27 17:27:04 -0700866 }
867 }
Wink Savilleed9c02b2010-12-03 12:01:38 -0800868 if (DBG) log("Doing network teardown");
Robert Greenwalt9c75d4a2009-09-27 17:27:04 -0700869 if (callTeardown) {
870 tracker.teardown();
Robert Greenwalt42acef32009-08-12 16:08:25 -0700871 return 1;
872 } else {
Robert Greenwalt02648a42010-05-18 10:52:51 -0700873 return -1;
Robert Greenwalt42acef32009-08-12 16:08:25 -0700874 }
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800875 }
876
877 /**
Robert Greenwalt585ac0f2010-08-27 09:24:29 -0700878 * @deprecated use requestRouteToHostAddress instead
879 *
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800880 * Ensure that a network route exists to deliver traffic to the specified
881 * host via the specified network interface.
Robert Greenwalt86e9e552009-07-16 17:21:39 -0700882 * @param networkType the type of the network over which traffic to the
883 * specified host is to be routed
884 * @param hostAddress the IP address of the host to which the route is
885 * desired
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800886 * @return {@code true} on success, {@code false} on failure
887 */
888 public boolean requestRouteToHost(int networkType, int hostAddress) {
Robert Greenwalt585ac0f2010-08-27 09:24:29 -0700889 InetAddress inetAddress = NetworkUtils.intToInetAddress(hostAddress);
890
891 if (inetAddress == null) {
892 return false;
893 }
894
895 return requestRouteToHostAddress(networkType, inetAddress.getAddress());
896 }
897
898 /**
899 * Ensure that a network route exists to deliver traffic to the specified
900 * host via the specified network interface.
901 * @param networkType the type of the network over which traffic to the
902 * specified host is to be routed
903 * @param hostAddress the IP address of the host to which the route is
904 * desired
905 * @return {@code true} on success, {@code false} on failure
906 */
907 public boolean requestRouteToHostAddress(int networkType, byte[] hostAddress) {
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800908 enforceChangePermission();
909 if (!ConnectivityManager.isNetworkTypeValid(networkType)) {
910 return false;
911 }
912 NetworkStateTracker tracker = mNetTrackers[networkType];
Robert Greenwalt8206ff32009-09-10 15:06:20 -0700913
Robert Greenwalt5154ae762009-10-30 14:17:42 -0700914 if (tracker == null || !tracker.getNetworkInfo().isConnected() ||
915 tracker.isTeardownRequested()) {
Robert Greenwalt8206ff32009-09-10 15:06:20 -0700916 if (DBG) {
Wink Savilleed9c02b2010-12-03 12:01:38 -0800917 log("requestRouteToHostAddress on down network " +
Robert Greenwalt585ac0f2010-08-27 09:24:29 -0700918 "(" + networkType + ") - dropped");
Robert Greenwalt8206ff32009-09-10 15:06:20 -0700919 }
920 return false;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800921 }
Robert Greenwalt47f69fe2010-06-15 15:43:39 -0700922 try {
Robert Greenwalt585ac0f2010-08-27 09:24:29 -0700923 InetAddress addr = InetAddress.getByAddress(hostAddress);
Robert Greenwalt47f69fe2010-06-15 15:43:39 -0700924 return addHostRoute(tracker, addr);
925 } catch (UnknownHostException e) {}
926 return false;
Irfan Sheriffd649c122010-06-09 15:39:36 -0700927 }
928
929 /**
930 * Ensure that a network route exists to deliver traffic to the specified
931 * host via the mobile data network.
932 * @param hostAddress the IP address of the host to which the route is desired,
933 * in network byte order.
Robert Greenwalt47f69fe2010-06-15 15:43:39 -0700934 * TODO - deprecate
Irfan Sheriffd649c122010-06-09 15:39:36 -0700935 * @return {@code true} on success, {@code false} on failure
936 */
Robert Greenwalt47f69fe2010-06-15 15:43:39 -0700937 private boolean addHostRoute(NetworkStateTracker nt, InetAddress hostAddress) {
Irfan Sheriffd649c122010-06-09 15:39:36 -0700938 if (nt.getNetworkInfo().getType() == ConnectivityManager.TYPE_WIFI) {
939 return false;
940 }
941
Robert Greenwalt37e65eb2010-08-30 10:56:47 -0700942 LinkProperties p = nt.getLinkProperties();
Robert Greenwalt47f69fe2010-06-15 15:43:39 -0700943 if (p == null) return false;
944 String interfaceName = p.getInterfaceName();
Irfan Sheriffd649c122010-06-09 15:39:36 -0700945
946 if (DBG) {
Wink Savilleed9c02b2010-12-03 12:01:38 -0800947 log("Requested host route to " + hostAddress + "(" + interfaceName + ")");
Irfan Sheriffd649c122010-06-09 15:39:36 -0700948 }
Robert Greenwalt47f69fe2010-06-15 15:43:39 -0700949 if (interfaceName != null) {
Robert Greenwalt585ac0f2010-08-27 09:24:29 -0700950 return NetworkUtils.addHostRoute(interfaceName, hostAddress, null);
Irfan Sheriffd649c122010-06-09 15:39:36 -0700951 } else {
Wink Savilleed9c02b2010-12-03 12:01:38 -0800952 if (DBG) loge("addHostRoute failed due to null interface name");
Irfan Sheriffd649c122010-06-09 15:39:36 -0700953 return false;
954 }
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800955 }
956
957 /**
958 * @see ConnectivityManager#getBackgroundDataSetting()
959 */
960 public boolean getBackgroundDataSetting() {
Robert Greenwaltd825ea42010-12-29 16:15:02 -0800961 return mBackgroundDataEnabled.get();
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800962 }
Robert Greenwalt86e9e552009-07-16 17:21:39 -0700963
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800964 /**
965 * @see ConnectivityManager#setBackgroundDataSetting(boolean)
966 */
967 public void setBackgroundDataSetting(boolean allowBackgroundDataUsage) {
968 mContext.enforceCallingOrSelfPermission(
969 android.Manifest.permission.CHANGE_BACKGROUND_DATA_SETTING,
970 "ConnectivityService");
Robert Greenwalt86e9e552009-07-16 17:21:39 -0700971
Robert Greenwaltd825ea42010-12-29 16:15:02 -0800972 mBackgroundDataEnabled.set(allowBackgroundDataUsage);
973
Robert Greenwalt8dcc28b2010-09-23 10:05:56 -0700974 mHandler.sendMessage(mHandler.obtainMessage(EVENT_SET_BACKGROUND_DATA,
975 (allowBackgroundDataUsage ? ENABLED : DISABLED), 0));
976 }
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800977
Robert Greenwalt8dcc28b2010-09-23 10:05:56 -0700978 private void handleSetBackgroundData(boolean enabled) {
Robert Greenwaltdb4afae2011-02-25 13:44:09 -0800979 Settings.Secure.putInt(mContext.getContentResolver(),
980 Settings.Secure.BACKGROUND_DATA, enabled ? 1 : 0);
981 Intent broadcast = new Intent(
982 ConnectivityManager.ACTION_BACKGROUND_DATA_SETTING_CHANGED);
983 mContext.sendBroadcast(broadcast);
Robert Greenwalt86e9e552009-07-16 17:21:39 -0700984 }
985
Robert Greenwaltc03fa502010-02-23 18:58:05 -0800986 /**
987 * @see ConnectivityManager#getMobileDataEnabled()
988 */
989 public boolean getMobileDataEnabled() {
Wink Savillee7982682010-12-07 10:31:02 -0800990 // TODO: This detail should probably be in DataConnectionTracker's
991 // which is where we store the value and maybe make this
992 // asynchronous.
Robert Greenwaltc03fa502010-02-23 18:58:05 -0800993 enforceAccessPermission();
994 boolean retVal = Settings.Secure.getInt(mContext.getContentResolver(),
995 Settings.Secure.MOBILE_DATA, 1) == 1;
Wink Savilleed9c02b2010-12-03 12:01:38 -0800996 if (DBG) log("getMobileDataEnabled returning " + retVal);
Robert Greenwaltc03fa502010-02-23 18:58:05 -0800997 return retVal;
998 }
999
Robert Greenwaltd55a6b42011-03-25 13:09:25 -07001000 public void setDataDependency(int networkType, boolean met) {
1001 enforceChangePermission();
1002 if (DBG) {
1003 log("setDataDependency(" + networkType + ", " + met + ")");
1004 }
1005 mHandler.sendMessage(mHandler.obtainMessage(EVENT_SET_DEPENDENCY_MET,
1006 (met ? ENABLED : DISABLED), networkType));
1007 }
1008
1009 private void handleSetDependencyMet(int networkType, boolean met) {
1010 if (mNetTrackers[networkType] != null) {
1011 if (DBG) {
1012 log("handleSetDependencyMet(" + networkType + ", " + met + ")");
1013 }
1014 mNetTrackers[networkType].setDependencyMet(met);
1015 }
1016 }
1017
Robert Greenwaltc03fa502010-02-23 18:58:05 -08001018 /**
1019 * @see ConnectivityManager#setMobileDataEnabled(boolean)
1020 */
Robert Greenwalt8dcc28b2010-09-23 10:05:56 -07001021 public void setMobileDataEnabled(boolean enabled) {
Robert Greenwaltc03fa502010-02-23 18:58:05 -08001022 enforceChangePermission();
Wink Savilleed9c02b2010-12-03 12:01:38 -08001023 if (DBG) log("setMobileDataEnabled(" + enabled + ")");
Robert Greenwaltc03fa502010-02-23 18:58:05 -08001024
Robert Greenwalt8dcc28b2010-09-23 10:05:56 -07001025 mHandler.sendMessage(mHandler.obtainMessage(EVENT_SET_MOBILE_DATA,
Robert Greenwaltd55a6b42011-03-25 13:09:25 -07001026 (enabled ? ENABLED : DISABLED), 0));
Robert Greenwalt8dcc28b2010-09-23 10:05:56 -07001027 }
1028
1029 private void handleSetMobileData(boolean enabled) {
Wink Savillee7982682010-12-07 10:31:02 -08001030 if (mNetTrackers[ConnectivityManager.TYPE_MOBILE] != null) {
1031 if (DBG) {
1032 Slog.d(TAG, mNetTrackers[ConnectivityManager.TYPE_MOBILE].toString() + enabled);
Robert Greenwaltc03fa502010-02-23 18:58:05 -08001033 }
Wink Savillee7982682010-12-07 10:31:02 -08001034 mNetTrackers[ConnectivityManager.TYPE_MOBILE].setDataEnable(enabled);
Robert Greenwaltc03fa502010-02-23 18:58:05 -08001035 }
1036 }
1037
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001038 private void enforceAccessPermission() {
Robert Greenwalt86e9e552009-07-16 17:21:39 -07001039 mContext.enforceCallingOrSelfPermission(
1040 android.Manifest.permission.ACCESS_NETWORK_STATE,
1041 "ConnectivityService");
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001042 }
1043
1044 private void enforceChangePermission() {
Robert Greenwalt86e9e552009-07-16 17:21:39 -07001045 mContext.enforceCallingOrSelfPermission(
1046 android.Manifest.permission.CHANGE_NETWORK_STATE,
1047 "ConnectivityService");
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001048 }
1049
Robert Greenwaltd0e18ff2010-01-26 11:40:34 -08001050 // TODO Make this a special check when it goes public
1051 private void enforceTetherChangePermission() {
1052 mContext.enforceCallingOrSelfPermission(
1053 android.Manifest.permission.CHANGE_NETWORK_STATE,
1054 "ConnectivityService");
1055 }
1056
Robert Greenwalt2a091d72010-02-11 18:18:40 -08001057 private void enforceTetherAccessPermission() {
1058 mContext.enforceCallingOrSelfPermission(
1059 android.Manifest.permission.ACCESS_NETWORK_STATE,
1060 "ConnectivityService");
1061 }
1062
Robert Greenwalt14f2ef42010-06-15 12:19:37 -07001063 private void enforceConnectivityInternalPermission() {
1064 mContext.enforceCallingOrSelfPermission(
1065 android.Manifest.permission.CONNECTIVITY_INTERNAL,
1066 "ConnectivityService");
1067 }
1068
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001069 /**
Robert Greenwalt86e9e552009-07-16 17:21:39 -07001070 * Handle a {@code DISCONNECTED} event. If this pertains to the non-active
1071 * network, we ignore it. If it is for the active network, we send out a
1072 * broadcast. But first, we check whether it might be possible to connect
1073 * to a different network.
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001074 * @param info the {@code NetworkInfo} for the network
1075 */
1076 private void handleDisconnect(NetworkInfo info) {
1077
Robert Greenwalt42acef32009-08-12 16:08:25 -07001078 int prevNetType = info.getType();
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001079
Robert Greenwalt42acef32009-08-12 16:08:25 -07001080 mNetTrackers[prevNetType].setTeardownRequested(false);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001081 /*
1082 * If the disconnected network is not the active one, then don't report
1083 * this as a loss of connectivity. What probably happened is that we're
1084 * getting the disconnect for a network that we explicitly disabled
1085 * in accordance with network preference policies.
1086 */
Robert Greenwaltd55a6b42011-03-25 13:09:25 -07001087 if (!mNetConfigs[prevNetType].isDefault()) {
Robert Greenwalt42acef32009-08-12 16:08:25 -07001088 List pids = mNetRequestersPids[prevNetType];
1089 for (int i = 0; i<pids.size(); i++) {
1090 Integer pid = (Integer)pids.get(i);
1091 // will remove them because the net's no longer connected
1092 // need to do this now as only now do we know the pids and
1093 // can properly null things that are no longer referenced.
1094 reassessPidDns(pid.intValue(), false);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001095 }
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001096 }
1097
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001098 Intent intent = new Intent(ConnectivityManager.CONNECTIVITY_ACTION);
1099 intent.putExtra(ConnectivityManager.EXTRA_NETWORK_INFO, info);
1100 if (info.isFailover()) {
1101 intent.putExtra(ConnectivityManager.EXTRA_IS_FAILOVER, true);
1102 info.setFailover(false);
1103 }
1104 if (info.getReason() != null) {
1105 intent.putExtra(ConnectivityManager.EXTRA_REASON, info.getReason());
1106 }
1107 if (info.getExtraInfo() != null) {
Robert Greenwalt86e9e552009-07-16 17:21:39 -07001108 intent.putExtra(ConnectivityManager.EXTRA_EXTRA_INFO,
1109 info.getExtraInfo());
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001110 }
Robert Greenwalt42acef32009-08-12 16:08:25 -07001111
Robert Greenwaltd55a6b42011-03-25 13:09:25 -07001112 if (mNetConfigs[prevNetType].isDefault()) {
Robert Greenwaltf21ef7d2011-01-11 13:56:33 -08001113 tryFailover(prevNetType);
1114 if (mActiveDefaultNetwork != -1) {
1115 NetworkInfo switchTo = mNetTrackers[mActiveDefaultNetwork].getNetworkInfo();
Robert Greenwaltcc4b4012010-01-25 17:54:29 -08001116 intent.putExtra(ConnectivityManager.EXTRA_OTHER_NETWORK_INFO, switchTo);
1117 } else {
Robert Greenwalt029be812010-09-20 18:01:43 -07001118 mDefaultInetConditionPublished = 0; // we're not connected anymore
Robert Greenwaltcc4b4012010-01-25 17:54:29 -08001119 intent.putExtra(ConnectivityManager.EXTRA_NO_CONNECTIVITY, true);
1120 }
Robert Greenwaltda03c4e2010-01-20 19:29:41 -08001121 }
Robert Greenwalt029be812010-09-20 18:01:43 -07001122 intent.putExtra(ConnectivityManager.EXTRA_INET_CONDITION, mDefaultInetConditionPublished);
Robert Greenwaltda03c4e2010-01-20 19:29:41 -08001123 // do this before we broadcast the change
Robert Greenwaltc76b8fa2010-07-23 15:46:26 -07001124 handleConnectivityChange(prevNetType);
Robert Greenwaltda03c4e2010-01-20 19:29:41 -08001125
1126 sendStickyBroadcast(intent);
1127 /*
1128 * If the failover network is already connected, then immediately send
1129 * out a followup broadcast indicating successful failover
1130 */
Robert Greenwaltf21ef7d2011-01-11 13:56:33 -08001131 if (mActiveDefaultNetwork != -1) {
1132 sendConnectedBroadcast(mNetTrackers[mActiveDefaultNetwork].getNetworkInfo());
Robert Greenwaltda03c4e2010-01-20 19:29:41 -08001133 }
1134 }
1135
Robert Greenwaltf21ef7d2011-01-11 13:56:33 -08001136 private void tryFailover(int prevNetType) {
Robert Greenwalt42acef32009-08-12 16:08:25 -07001137 /*
Robert Greenwaltbff90182011-01-06 15:41:07 -08001138 * If this is a default network, check if other defaults are available.
1139 * Try to reconnect on all available and let them hash it out when
1140 * more than one connects.
Robert Greenwalt42acef32009-08-12 16:08:25 -07001141 */
Robert Greenwaltd55a6b42011-03-25 13:09:25 -07001142 if (mNetConfigs[prevNetType].isDefault()) {
Robert Greenwalt42acef32009-08-12 16:08:25 -07001143 if (mActiveDefaultNetwork == prevNetType) {
1144 mActiveDefaultNetwork = -1;
1145 }
1146
Robert Greenwaltf21ef7d2011-01-11 13:56:33 -08001147 // don't signal a reconnect for anything lower or equal priority than our
1148 // current connected default
1149 // TODO - don't filter by priority now - nice optimization but risky
1150// int currentPriority = -1;
1151// if (mActiveDefaultNetwork != -1) {
Robert Greenwaltd55a6b42011-03-25 13:09:25 -07001152// currentPriority = mNetConfigs[mActiveDefaultNetwork].mPriority;
Robert Greenwaltf21ef7d2011-01-11 13:56:33 -08001153// }
Robert Greenwaltda03c4e2010-01-20 19:29:41 -08001154 for (int checkType=0; checkType <= ConnectivityManager.MAX_NETWORK_TYPE; checkType++) {
Robert Greenwalt5154ae762009-10-30 14:17:42 -07001155 if (checkType == prevNetType) continue;
Robert Greenwaltd55a6b42011-03-25 13:09:25 -07001156 if (mNetConfigs[checkType] == null) continue;
1157 if (!mNetConfigs[checkType].isDefault()) continue;
Wink Saville9f7a0b22011-01-26 15:43:49 -08001158
1159// Enabling the isAvailable() optimization caused mobile to not get
1160// selected if it was in the middle of error handling. Specifically
1161// a moble connection that took 30 seconds to complete the DEACTIVATE_DATA_CALL
1162// would not be available and we wouldn't get connected to anything.
1163// So removing the isAvailable() optimization below for now. TODO: This
1164// optimization should work and we need to investigate why it doesn't work.
1165// This could be related to how DEACTIVATE_DATA_CALL is reporting its
1166// complete before it is really complete.
1167// if (!mNetTrackers[checkType].isAvailable()) continue;
1168
Robert Greenwaltd55a6b42011-03-25 13:09:25 -07001169// if (currentPriority >= mNetConfigs[checkType].mPriority) continue;
Robert Greenwalt42acef32009-08-12 16:08:25 -07001170
Robert Greenwaltf21ef7d2011-01-11 13:56:33 -08001171 NetworkStateTracker checkTracker = mNetTrackers[checkType];
1172 NetworkInfo checkInfo = checkTracker.getNetworkInfo();
1173 if (!checkInfo.isConnectedOrConnecting() || checkTracker.isTeardownRequested()) {
1174 checkInfo.setFailover(true);
1175 checkTracker.reconnect();
Robert Greenwalt42acef32009-08-12 16:08:25 -07001176 }
Robert Greenwaltf21ef7d2011-01-11 13:56:33 -08001177 if (DBG) log("Attempting to switch to " + checkInfo.getTypeName());
Robert Greenwalt42acef32009-08-12 16:08:25 -07001178 }
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001179 }
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001180 }
1181
1182 private void sendConnectedBroadcast(NetworkInfo info) {
Robert Greenwalt1e9aac22010-09-15 17:36:33 -07001183 sendGeneralBroadcast(info, ConnectivityManager.CONNECTIVITY_ACTION);
1184 }
1185
1186 private void sendInetConditionBroadcast(NetworkInfo info) {
1187 sendGeneralBroadcast(info, ConnectivityManager.INET_CONDITION_ACTION);
1188 }
1189
1190 private void sendGeneralBroadcast(NetworkInfo info, String bcastType) {
1191 Intent intent = new Intent(bcastType);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001192 intent.putExtra(ConnectivityManager.EXTRA_NETWORK_INFO, info);
1193 if (info.isFailover()) {
1194 intent.putExtra(ConnectivityManager.EXTRA_IS_FAILOVER, true);
1195 info.setFailover(false);
1196 }
1197 if (info.getReason() != null) {
1198 intent.putExtra(ConnectivityManager.EXTRA_REASON, info.getReason());
1199 }
1200 if (info.getExtraInfo() != null) {
Robert Greenwalt86e9e552009-07-16 17:21:39 -07001201 intent.putExtra(ConnectivityManager.EXTRA_EXTRA_INFO,
1202 info.getExtraInfo());
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001203 }
Robert Greenwaltd7085fc2010-09-08 15:24:47 -07001204 intent.putExtra(ConnectivityManager.EXTRA_INET_CONDITION, mDefaultInetConditionPublished);
Mike Lockwood0f79b542009-08-14 14:18:49 -04001205 sendStickyBroadcast(intent);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001206 }
1207
1208 /**
1209 * Called when an attempt to fail over to another network has failed.
1210 * @param info the {@link NetworkInfo} for the failed network
1211 */
1212 private void handleConnectionFailure(NetworkInfo info) {
1213 mNetTrackers[info.getType()].setTeardownRequested(false);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001214
Robert Greenwalt42acef32009-08-12 16:08:25 -07001215 String reason = info.getReason();
1216 String extraInfo = info.getExtraInfo();
Robert Greenwalt86e9e552009-07-16 17:21:39 -07001217
Robert Greenwalt572172b2010-10-08 16:35:52 -07001218 String reasonText;
1219 if (reason == null) {
1220 reasonText = ".";
1221 } else {
1222 reasonText = " (" + reason + ").";
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001223 }
Wink Savilleed9c02b2010-12-03 12:01:38 -08001224 loge("Attempt to connect to " + info.getTypeName() + " failed" + reasonText);
Robert Greenwalt42acef32009-08-12 16:08:25 -07001225
1226 Intent intent = new Intent(ConnectivityManager.CONNECTIVITY_ACTION);
1227 intent.putExtra(ConnectivityManager.EXTRA_NETWORK_INFO, info);
1228 if (getActiveNetworkInfo() == null) {
1229 intent.putExtra(ConnectivityManager.EXTRA_NO_CONNECTIVITY, true);
1230 }
1231 if (reason != null) {
1232 intent.putExtra(ConnectivityManager.EXTRA_REASON, reason);
1233 }
1234 if (extraInfo != null) {
1235 intent.putExtra(ConnectivityManager.EXTRA_EXTRA_INFO, extraInfo);
1236 }
1237 if (info.isFailover()) {
1238 intent.putExtra(ConnectivityManager.EXTRA_IS_FAILOVER, true);
1239 info.setFailover(false);
1240 }
Robert Greenwaltda03c4e2010-01-20 19:29:41 -08001241
Robert Greenwaltd55a6b42011-03-25 13:09:25 -07001242 if (mNetConfigs[info.getType()].isDefault()) {
Robert Greenwaltf21ef7d2011-01-11 13:56:33 -08001243 tryFailover(info.getType());
1244 if (mActiveDefaultNetwork != -1) {
1245 NetworkInfo switchTo = mNetTrackers[mActiveDefaultNetwork].getNetworkInfo();
Robert Greenwaltcc4b4012010-01-25 17:54:29 -08001246 intent.putExtra(ConnectivityManager.EXTRA_OTHER_NETWORK_INFO, switchTo);
1247 } else {
Robert Greenwalt029be812010-09-20 18:01:43 -07001248 mDefaultInetConditionPublished = 0;
Robert Greenwaltcc4b4012010-01-25 17:54:29 -08001249 intent.putExtra(ConnectivityManager.EXTRA_NO_CONNECTIVITY, true);
1250 }
Robert Greenwaltda03c4e2010-01-20 19:29:41 -08001251 }
Robert Greenwaltcc4b4012010-01-25 17:54:29 -08001252
Robert Greenwalt029be812010-09-20 18:01:43 -07001253 intent.putExtra(ConnectivityManager.EXTRA_INET_CONDITION, mDefaultInetConditionPublished);
Mike Lockwood0f79b542009-08-14 14:18:49 -04001254 sendStickyBroadcast(intent);
Robert Greenwaltda03c4e2010-01-20 19:29:41 -08001255 /*
1256 * If the failover network is already connected, then immediately send
1257 * out a followup broadcast indicating successful failover
1258 */
Robert Greenwaltf21ef7d2011-01-11 13:56:33 -08001259 if (mActiveDefaultNetwork != -1) {
1260 sendConnectedBroadcast(mNetTrackers[mActiveDefaultNetwork].getNetworkInfo());
Robert Greenwaltda03c4e2010-01-20 19:29:41 -08001261 }
Mike Lockwood0f79b542009-08-14 14:18:49 -04001262 }
1263
1264 private void sendStickyBroadcast(Intent intent) {
1265 synchronized(this) {
Dianne Hackborn1c633fc2009-12-08 19:45:14 -08001266 if (!mSystemReady) {
1267 mInitialBroadcast = new Intent(intent);
Mike Lockwood0f79b542009-08-14 14:18:49 -04001268 }
Dianne Hackborn1c633fc2009-12-08 19:45:14 -08001269 intent.addFlags(Intent.FLAG_RECEIVER_REGISTERED_ONLY_BEFORE_BOOT);
1270 mContext.sendStickyBroadcast(intent);
Mike Lockwood0f79b542009-08-14 14:18:49 -04001271 }
1272 }
1273
1274 void systemReady() {
1275 synchronized(this) {
1276 mSystemReady = true;
Dianne Hackborn1c633fc2009-12-08 19:45:14 -08001277 if (mInitialBroadcast != null) {
1278 mContext.sendStickyBroadcast(mInitialBroadcast);
1279 mInitialBroadcast = null;
Mike Lockwood0f79b542009-08-14 14:18:49 -04001280 }
1281 }
Robert Greenwalt434203a2010-10-11 16:00:27 -07001282 // load the global proxy at startup
1283 mHandler.sendMessage(mHandler.obtainMessage(EVENT_APPLY_GLOBAL_HTTP_PROXY));
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001284 }
1285
1286 private void handleConnect(NetworkInfo info) {
Robert Greenwalt42acef32009-08-12 16:08:25 -07001287 int type = info.getType();
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001288
1289 // snapshot isFailover, because sendConnectedBroadcast() resets it
1290 boolean isFailover = info.isFailover();
Robert Greenwalt42acef32009-08-12 16:08:25 -07001291 NetworkStateTracker thisNet = mNetTrackers[type];
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001292
Robert Greenwalt42acef32009-08-12 16:08:25 -07001293 // if this is a default net and other default is running
1294 // kill the one not preferred
Robert Greenwaltd55a6b42011-03-25 13:09:25 -07001295 if (mNetConfigs[type].isDefault()) {
Robert Greenwalt42acef32009-08-12 16:08:25 -07001296 if (mActiveDefaultNetwork != -1 && mActiveDefaultNetwork != type) {
1297 if ((type != mNetworkPreference &&
Wink Saville975c8482011-04-07 14:23:45 -07001298 mNetConfigs[mActiveDefaultNetwork].priority >
1299 mNetConfigs[type].priority) ||
Robert Greenwalt42acef32009-08-12 16:08:25 -07001300 mNetworkPreference == mActiveDefaultNetwork) {
1301 // don't accept this one
Wink Savilleed9c02b2010-12-03 12:01:38 -08001302 if (DBG) {
1303 log("Not broadcasting CONNECT_ACTION " +
Robert Greenwalt42acef32009-08-12 16:08:25 -07001304 "to torn down network " + info.getTypeName());
Wink Savilleed9c02b2010-12-03 12:01:38 -08001305 }
Robert Greenwalt42acef32009-08-12 16:08:25 -07001306 teardown(thisNet);
1307 return;
1308 } else {
1309 // tear down the other
1310 NetworkStateTracker otherNet =
1311 mNetTrackers[mActiveDefaultNetwork];
Wink Savilleed9c02b2010-12-03 12:01:38 -08001312 if (DBG) {
1313 log("Policy requires " + otherNet.getNetworkInfo().getTypeName() +
Robert Greenwalt42acef32009-08-12 16:08:25 -07001314 " teardown");
Wink Savilleed9c02b2010-12-03 12:01:38 -08001315 }
Robert Greenwalt42acef32009-08-12 16:08:25 -07001316 if (!teardown(otherNet)) {
Wink Savilleed9c02b2010-12-03 12:01:38 -08001317 loge("Network declined teardown request");
Robert Greenwalt42acef32009-08-12 16:08:25 -07001318 return;
1319 }
Robert Greenwalt14f2ef42010-06-15 12:19:37 -07001320 }
1321 }
1322 synchronized (ConnectivityService.this) {
1323 // have a new default network, release the transition wakelock in a second
1324 // if it's held. The second pause is to allow apps to reconnect over the
1325 // new network
1326 if (mNetTransitionWakeLock.isHeld()) {
1327 mHandler.sendMessageDelayed(mHandler.obtainMessage(
Robert Greenwaltf3331232010-09-24 14:32:21 -07001328 EVENT_CLEAR_NET_TRANSITION_WAKELOCK,
Robert Greenwalt14f2ef42010-06-15 12:19:37 -07001329 mNetTransitionWakeLockSerialNumber, 0),
1330 1000);
Robert Greenwalt42acef32009-08-12 16:08:25 -07001331 }
1332 }
1333 mActiveDefaultNetwork = type;
Robert Greenwaltd7085fc2010-09-08 15:24:47 -07001334 // this will cause us to come up initially as unconnected and switching
1335 // to connected after our normal pause unless somebody reports us as reall
1336 // disconnected
1337 mDefaultInetConditionPublished = 0;
1338 mDefaultConnectionSequence++;
1339 mInetConditionChangeInFlight = false;
1340 // Don't do this - if we never sign in stay, grey
1341 //reportNetworkCondition(mActiveDefaultNetwork, 100);
Robert Greenwalt42acef32009-08-12 16:08:25 -07001342 }
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001343 thisNet.setTeardownRequested(false);
Irfan Sheriffd649c122010-06-09 15:39:36 -07001344 updateNetworkSettings(thisNet);
Robert Greenwaltc76b8fa2010-07-23 15:46:26 -07001345 handleConnectivityChange(type);
Robert Greenwalt42acef32009-08-12 16:08:25 -07001346 sendConnectedBroadcast(info);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001347 }
1348
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001349 /**
Robert Greenwaltc76b8fa2010-07-23 15:46:26 -07001350 * After a change in the connectivity state of a network. We're mainly
1351 * concerned with making sure that the list of DNS servers is set up
1352 * according to which networks are connected, and ensuring that the
1353 * right routing table entries exist.
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001354 */
Robert Greenwaltc76b8fa2010-07-23 15:46:26 -07001355 private void handleConnectivityChange(int netType) {
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001356 /*
Robert Greenwalt42acef32009-08-12 16:08:25 -07001357 * If a non-default network is enabled, add the host routes that
Robert Greenwaltc76b8fa2010-07-23 15:46:26 -07001358 * will allow it's DNS servers to be accessed.
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001359 */
Robert Greenwaltc76b8fa2010-07-23 15:46:26 -07001360 handleDnsConfigurationChange(netType);
Robert Greenwalt42acef32009-08-12 16:08:25 -07001361
Robert Greenwaltc76b8fa2010-07-23 15:46:26 -07001362 if (mNetTrackers[netType].getNetworkInfo().isConnected()) {
Robert Greenwaltd55a6b42011-03-25 13:09:25 -07001363 if (mNetConfigs[netType].isDefault()) {
Robert Greenwalt434203a2010-10-11 16:00:27 -07001364 handleApplyDefaultProxy(netType);
Robert Greenwaltc76b8fa2010-07-23 15:46:26 -07001365 addDefaultRoute(mNetTrackers[netType]);
Robert Greenwalt42acef32009-08-12 16:08:25 -07001366 } else {
Robert Greenwaltc76b8fa2010-07-23 15:46:26 -07001367 addPrivateDnsRoutes(mNetTrackers[netType]);
1368 }
1369 } else {
Robert Greenwaltd55a6b42011-03-25 13:09:25 -07001370 if (mNetConfigs[netType].isDefault()) {
Robert Greenwaltc76b8fa2010-07-23 15:46:26 -07001371 removeDefaultRoute(mNetTrackers[netType]);
1372 } else {
1373 removePrivateDnsRoutes(mNetTrackers[netType]);
Robert Greenwalt42acef32009-08-12 16:08:25 -07001374 }
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001375 }
1376 }
1377
Irfan Sheriffd649c122010-06-09 15:39:36 -07001378 private void addPrivateDnsRoutes(NetworkStateTracker nt) {
Irfan Sheriffd649c122010-06-09 15:39:36 -07001379 boolean privateDnsRouteSet = nt.isPrivateDnsRouteSet();
Robert Greenwalt37e65eb2010-08-30 10:56:47 -07001380 LinkProperties p = nt.getLinkProperties();
Robert Greenwalt47f69fe2010-06-15 15:43:39 -07001381 if (p == null) return;
1382 String interfaceName = p.getInterfaceName();
Irfan Sheriffd649c122010-06-09 15:39:36 -07001383
1384 if (DBG) {
Wink Savilleed9c02b2010-12-03 12:01:38 -08001385 log("addPrivateDnsRoutes for " + nt +
Irfan Sheriffd649c122010-06-09 15:39:36 -07001386 "(" + interfaceName + ") - mPrivateDnsRouteSet = " + privateDnsRouteSet);
1387 }
Irfan Sheriffd649c122010-06-09 15:39:36 -07001388 if (interfaceName != null && !privateDnsRouteSet) {
Robert Greenwalt47f69fe2010-06-15 15:43:39 -07001389 Collection<InetAddress> dnsList = p.getDnses();
1390 for (InetAddress dns : dnsList) {
Wink Savilleed9c02b2010-12-03 12:01:38 -08001391 if (DBG) log(" adding " + dns);
Robert Greenwalt585ac0f2010-08-27 09:24:29 -07001392 NetworkUtils.addHostRoute(interfaceName, dns, null);
Irfan Sheriffd649c122010-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 Greenwalt37e65eb2010-08-30 10:56:47 -07001401 LinkProperties p = nt.getLinkProperties();
Robert Greenwalt47f69fe2010-06-15 15:43:39 -07001402 if (p == null) return;
1403 String interfaceName = p.getInterfaceName();
Irfan Sheriffd649c122010-06-09 15:39:36 -07001404 boolean privateDnsRouteSet = nt.isPrivateDnsRouteSet();
1405 if (interfaceName != null && privateDnsRouteSet) {
1406 if (DBG) {
Wink Savilleed9c02b2010-12-03 12:01:38 -08001407 log("removePrivateDnsRoutes for " + nt.getNetworkInfo().getTypeName() +
Irfan Sheriffd649c122010-06-09 15:39:36 -07001408 " (" + interfaceName + ")");
1409 }
1410 NetworkUtils.removeHostRoutes(interfaceName);
1411 nt.privateDnsRouteSet(false);
1412 }
1413 }
1414
Irfan Sheriffd649c122010-06-09 15:39:36 -07001415
1416 private void addDefaultRoute(NetworkStateTracker nt) {
Robert Greenwalt37e65eb2010-08-30 10:56:47 -07001417 LinkProperties p = nt.getLinkProperties();
Robert Greenwalt47f69fe2010-06-15 15:43:39 -07001418 if (p == null) return;
1419 String interfaceName = p.getInterfaceName();
Robert Greenwalt992564e2011-02-09 13:56:06 -08001420 if (TextUtils.isEmpty(interfaceName)) return;
Robert Greenwaltaa70f102011-04-28 14:28:50 -07001421 for (RouteInfo route : p.getRoutes()) {
Irfan Sheriffd649c122010-06-09 15:39:36 -07001422
Robert Greenwaltaa70f102011-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 Greenwaltedcb4f92011-03-22 18:47:42 -07001434 }
Irfan Sheriffd649c122010-06-09 15:39:36 -07001435 }
Irfan Sheriffd649c122010-06-09 15:39:36 -07001436 }
1437 }
1438
1439
1440 public void removeDefaultRoute(NetworkStateTracker nt) {
Robert Greenwalt37e65eb2010-08-30 10:56:47 -07001441 LinkProperties p = nt.getLinkProperties();
Robert Greenwalt47f69fe2010-06-15 15:43:39 -07001442 if (p == null) return;
1443 String interfaceName = p.getInterfaceName();
Irfan Sheriffd649c122010-06-09 15:39:36 -07001444
Robert Greenwaltc76b8fa2010-07-23 15:46:26 -07001445 if (interfaceName != null) {
Robert Greenwaltedcb4f92011-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 Sheriffd649c122010-06-09 15:39:36 -07001452 }
Irfan Sheriffd649c122010-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 Savilleed9c02b2010-12-03 12:01:38 -08001466 loge(key + " not found in system properties. Using defaults");
Irfan Sheriffd649c122010-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 Savilleed9c02b2010-12-03 12:01:38 -08001476 log("Setting TCP values: [" + bufferSizes
Irfan Sheriffd649c122010-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 Savilleed9c02b2010-12-03 12:01:38 -08001503 loge("Invalid buffersize string: " + bufferSizes);
Irfan Sheriffd649c122010-06-09 15:39:36 -07001504 }
1505 } catch (IOException e) {
Wink Savilleed9c02b2010-12-03 12:01:38 -08001506 loge("Can't set tcp buffer sizes:" + e);
Irfan Sheriffd649c122010-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 Greenwalt42acef32009-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 Savilleed9c02b2010-12-03 12:01:38 -08001534 if (DBG) log("reassessPidDns for pid " + myPid);
Robert Greenwalt42acef32009-08-12 16:08:25 -07001535 for(int i : mPriorityList) {
Robert Greenwaltd55a6b42011-03-25 13:09:25 -07001536 if (mNetConfigs[i].isDefault()) {
Robert Greenwalt42acef32009-08-12 16:08:25 -07001537 continue;
1538 }
1539 NetworkStateTracker nt = mNetTrackers[i];
Robert Greenwalt86e9e552009-07-16 17:21:39 -07001540 if (nt.getNetworkInfo().isConnected() &&
1541 !nt.isTeardownRequested()) {
Robert Greenwalt37e65eb2010-08-30 10:56:47 -07001542 LinkProperties p = nt.getLinkProperties();
Robert Greenwalt47f69fe2010-06-15 15:43:39 -07001543 if (p == null) continue;
Robert Greenwalt42acef32009-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 Greenwalt47f69fe2010-06-15 15:43:39 -07001548 Collection<InetAddress> dnses = p.getDnses();
1549 writePidDns(dnses, myPid);
Robert Greenwalt42acef32009-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 Greenwalt10398722010-12-17 15:20:36 -08001571 // return true if results in a change
1572 private boolean writePidDns(Collection <InetAddress> dnses, int pid) {
Robert Greenwalt42acef32009-08-12 16:08:25 -07001573 int j = 1;
Robert Greenwalt10398722010-12-17 15:20:36 -08001574 boolean changed = false;
Robert Greenwalt47f69fe2010-06-15 15:43:39 -07001575 for (InetAddress dns : dnses) {
Robert Greenwalt10398722010-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 Greenwalt42acef32009-08-12 16:08:25 -07001581 }
Robert Greenwalt10398722010-12-17 15:20:36 -08001582 return changed;
Robert Greenwalt42acef32009-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 Greenwalt03595d02010-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 Chesnutt3d1db862011-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 Greenwalt03595d02010-11-02 14:08:23 -07001607 mContext.sendBroadcast(intent);
Robert Greenwalt42acef32009-08-12 16:08:25 -07001608 }
1609
Robert Greenwaltc76b8fa2010-07-23 15:46:26 -07001610 private void handleDnsConfigurationChange(int netType) {
Robert Greenwalt42acef32009-08-12 16:08:25 -07001611 // add default net's dns entries
Robert Greenwaltc76b8fa2010-07-23 15:46:26 -07001612 NetworkStateTracker nt = mNetTrackers[netType];
1613 if (nt != null && nt.getNetworkInfo().isConnected() && !nt.isTeardownRequested()) {
Robert Greenwalt37e65eb2010-08-30 10:56:47 -07001614 LinkProperties p = nt.getLinkProperties();
Robert Greenwaltc76b8fa2010-07-23 15:46:26 -07001615 if (p == null) return;
1616 Collection<InetAddress> dnses = p.getDnses();
Robert Greenwalt10398722010-12-17 15:20:36 -08001617 boolean changed = false;
Robert Greenwaltd55a6b42011-03-25 13:09:25 -07001618 if (mNetConfigs[netType].isDefault()) {
Robert Greenwaltc76b8fa2010-07-23 15:46:26 -07001619 int j = 1;
Robert Greenwalte90aa5e2010-09-01 11:34:05 -07001620 if (dnses.size() == 0 && mDefaultDns != null) {
Robert Greenwalt10398722010-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 Greenwalt42acef32009-08-12 16:08:25 -07001628 }
Robert Greenwalte90aa5e2010-09-01 11:34:05 -07001629 j++;
1630 } else {
1631 for (InetAddress dns : dnses) {
Robert Greenwalt10398722010-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 Greenwalte90aa5e2010-09-01 11:34:05 -07001637 if (DBG) {
Wink Savilleed9c02b2010-12-03 12:01:38 -08001638 log("adding dns " + dns + " for " +
Robert Greenwalte90aa5e2010-09-01 11:34:05 -07001639 nt.getNetworkInfo().getTypeName());
1640 }
Robert Greenwalt10398722010-12-17 15:20:36 -08001641 changed = true;
1642 SystemProperties.set("net.dns" + j++, dnsString);
Robert Greenwalte90aa5e2010-09-01 11:34:05 -07001643 }
Robert Greenwaltc76b8fa2010-07-23 15:46:26 -07001644 }
1645 for (int k=j ; k<mNumDnsEntries; k++) {
Robert Greenwalt10398722010-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 Greenwaltc76b8fa2010-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 Greenwalt10398722010-12-17 15:20:36 -08001658 changed = writePidDns(dnses, pid.intValue());
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001659 }
1660 }
Robert Greenwalt10398722010-12-17 15:20:36 -08001661 if (changed) bumpDns();
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001662 }
Robert Greenwalt42acef32009-08-12 16:08:25 -07001663 }
1664
Robert Greenwaltf2102f72011-05-03 19:02:44 -07001665 private int getRestoreDefaultNetworkDelay(int networkType) {
Robert Greenwalt42acef32009-08-12 16:08:25 -07001666 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 Project9066cfe2009-03-03 19:31:44 -08001674 }
Robert Greenwaltf2102f72011-05-03 19:02:44 -07001675 // if the system property isn't set, use the value for the apn type
1676 int ret = RESTORE_DEFAULT_NETWORK_DELAY;
1677
1678 if ((networkType <= ConnectivityManager.MAX_NETWORK_TYPE) &&
1679 (mNetConfigs[networkType] != null)) {
1680 ret = mNetConfigs[networkType].restoreTime;
1681 }
1682 return ret;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001683 }
1684
1685 @Override
1686 protected void dump(FileDescriptor fd, PrintWriter pw, String[] args) {
Robert Greenwalt86e9e552009-07-16 17:21:39 -07001687 if (mContext.checkCallingOrSelfPermission(
1688 android.Manifest.permission.DUMP)
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001689 != PackageManager.PERMISSION_GRANTED) {
Robert Greenwalt86e9e552009-07-16 17:21:39 -07001690 pw.println("Permission Denial: can't dump ConnectivityService " +
1691 "from from pid=" + Binder.getCallingPid() + ", uid=" +
1692 Binder.getCallingUid());
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001693 return;
1694 }
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001695 pw.println();
1696 for (NetworkStateTracker nst : mNetTrackers) {
Robert Greenwaltb9285352009-12-21 18:24:07 -08001697 if (nst != null) {
1698 if (nst.getNetworkInfo().isConnected()) {
1699 pw.println("Active network: " + nst.getNetworkInfo().
1700 getTypeName());
1701 }
1702 pw.println(nst.getNetworkInfo());
1703 pw.println(nst);
1704 pw.println();
Robert Greenwalt42acef32009-08-12 16:08:25 -07001705 }
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001706 }
Robert Greenwaltb9285352009-12-21 18:24:07 -08001707
1708 pw.println("Network Requester Pids:");
1709 for (int net : mPriorityList) {
1710 String pidString = net + ": ";
1711 for (Object pid : mNetRequestersPids[net]) {
1712 pidString = pidString + pid.toString() + ", ";
1713 }
1714 pw.println(pidString);
1715 }
1716 pw.println();
1717
1718 pw.println("FeatureUsers:");
1719 for (Object requester : mFeatureUsers) {
1720 pw.println(requester.toString());
1721 }
1722 pw.println();
Robert Greenwalt2a091d72010-02-11 18:18:40 -08001723
Robert Greenwalt14f2ef42010-06-15 12:19:37 -07001724 synchronized (this) {
1725 pw.println("NetworkTranstionWakeLock is currently " +
1726 (mNetTransitionWakeLock.isHeld() ? "" : "not ") + "held.");
1727 pw.println("It was last requested for "+mNetTransitionWakeLockCausedBy);
1728 }
1729 pw.println();
1730
Robert Greenwalt2a091d72010-02-11 18:18:40 -08001731 mTethering.dump(fd, pw, args);
Robert Greenwalt4e8dfef2010-09-20 14:35:25 -07001732
1733 if (mInetLog != null) {
1734 pw.println();
1735 pw.println("Inet condition reports:");
1736 for(int i = 0; i < mInetLog.size(); i++) {
1737 pw.println(mInetLog.get(i));
1738 }
1739 }
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001740 }
1741
Robert Greenwalt42acef32009-08-12 16:08:25 -07001742 // must be stateless - things change under us.
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001743 private class MyHandler extends Handler {
Wink Savillebb08caf2010-09-02 19:23:52 -07001744 public MyHandler(Looper looper) {
1745 super(looper);
1746 }
1747
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001748 @Override
1749 public void handleMessage(Message msg) {
1750 NetworkInfo info;
1751 switch (msg.what) {
1752 case NetworkStateTracker.EVENT_STATE_CHANGED:
1753 info = (NetworkInfo) msg.obj;
Robert Greenwalt511288a2009-12-07 11:33:18 -08001754 int type = info.getType();
1755 NetworkInfo.State state = info.getState();
Robert Greenwalt511288a2009-12-07 11:33:18 -08001756
Wink Savilleed9c02b2010-12-03 12:01:38 -08001757 if (DBG) log("ConnectivityChange for " +
Robert Greenwalt86e9e552009-07-16 17:21:39 -07001758 info.getTypeName() + ": " +
Robert Greenwalt511288a2009-12-07 11:33:18 -08001759 state + "/" + info.getDetailedState());
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001760
1761 // Connectivity state changed:
1762 // [31-13] Reserved for future use
Robert Greenwalt86e9e552009-07-16 17:21:39 -07001763 // [12-9] Network subtype (for mobile network, as defined
1764 // by TelephonyManager)
1765 // [8-3] Detailed state ordinal (as defined by
1766 // NetworkInfo.DetailedState)
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001767 // [2-0] Network type (as defined by ConnectivityManager)
1768 int eventLogParam = (info.getType() & 0x7) |
1769 ((info.getDetailedState().ordinal() & 0x3f) << 3) |
1770 (info.getSubtype() << 9);
Doug Zongkerab5c49c2009-12-04 10:31:43 -08001771 EventLog.writeEvent(EventLogTags.CONNECTIVITY_STATE_CHANGED,
Robert Greenwalt86e9e552009-07-16 17:21:39 -07001772 eventLogParam);
1773
1774 if (info.getDetailedState() ==
1775 NetworkInfo.DetailedState.FAILED) {
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001776 handleConnectionFailure(info);
Robert Greenwalt511288a2009-12-07 11:33:18 -08001777 } else if (state == NetworkInfo.State.DISCONNECTED) {
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001778 handleDisconnect(info);
Robert Greenwalt511288a2009-12-07 11:33:18 -08001779 } else if (state == NetworkInfo.State.SUSPENDED) {
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001780 // TODO: need to think this over.
Robert Greenwalt86e9e552009-07-16 17:21:39 -07001781 // the logic here is, handle SUSPENDED the same as
1782 // DISCONNECTED. The only difference being we are
1783 // broadcasting an intent with NetworkInfo that's
1784 // suspended. This allows the applications an
1785 // opportunity to handle DISCONNECTED and SUSPENDED
1786 // differently, or not.
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001787 handleDisconnect(info);
Robert Greenwalt511288a2009-12-07 11:33:18 -08001788 } else if (state == NetworkInfo.State.CONNECTED) {
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001789 handleConnect(info);
1790 }
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001791 break;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001792 case NetworkStateTracker.EVENT_CONFIGURATION_CHANGED:
Robert Greenwaltc76b8fa2010-07-23 15:46:26 -07001793 info = (NetworkInfo) msg.obj;
Robert Greenwaltd55a6b42011-03-25 13:09:25 -07001794 handleConnectivityChange(info.getType());
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001795 break;
Robert Greenwaltf3331232010-09-24 14:32:21 -07001796 case EVENT_CLEAR_NET_TRANSITION_WAKELOCK:
Robert Greenwalt14f2ef42010-06-15 12:19:37 -07001797 String causedBy = null;
1798 synchronized (ConnectivityService.this) {
1799 if (msg.arg1 == mNetTransitionWakeLockSerialNumber &&
1800 mNetTransitionWakeLock.isHeld()) {
1801 mNetTransitionWakeLock.release();
1802 causedBy = mNetTransitionWakeLockCausedBy;
1803 }
1804 }
1805 if (causedBy != null) {
Wink Savilleed9c02b2010-12-03 12:01:38 -08001806 log("NetTransition Wakelock for " + causedBy + " released by timeout");
Robert Greenwalt14f2ef42010-06-15 12:19:37 -07001807 }
Robert Greenwalt057d5e92010-09-09 14:05:10 -07001808 break;
Robert Greenwalt8dcc28b2010-09-23 10:05:56 -07001809 case EVENT_RESTORE_DEFAULT_NETWORK:
Robert Greenwalt9c75d4a2009-09-27 17:27:04 -07001810 FeatureUser u = (FeatureUser)msg.obj;
1811 u.expire();
Robert Greenwaltd7085fc2010-09-08 15:24:47 -07001812 break;
Robert Greenwalt8dcc28b2010-09-23 10:05:56 -07001813 case EVENT_INET_CONDITION_CHANGE:
1814 {
1815 int netType = msg.arg1;
1816 int condition = msg.arg2;
1817 handleInetConditionChange(netType, condition);
Robert Greenwalt14f2ef42010-06-15 12:19:37 -07001818 break;
Robert Greenwalt8dcc28b2010-09-23 10:05:56 -07001819 }
1820 case EVENT_INET_CONDITION_HOLD_END:
1821 {
1822 int netType = msg.arg1;
1823 int sequence = msg.arg2;
1824 handleInetConditionHoldEnd(netType, sequence);
Robert Greenwaltd7085fc2010-09-08 15:24:47 -07001825 break;
Robert Greenwalt8dcc28b2010-09-23 10:05:56 -07001826 }
1827 case EVENT_SET_NETWORK_PREFERENCE:
1828 {
1829 int preference = msg.arg1;
1830 handleSetNetworkPreference(preference);
1831 break;
1832 }
1833 case EVENT_SET_BACKGROUND_DATA:
1834 {
1835 boolean enabled = (msg.arg1 == ENABLED);
1836 handleSetBackgroundData(enabled);
1837 break;
1838 }
1839 case EVENT_SET_MOBILE_DATA:
1840 {
1841 boolean enabled = (msg.arg1 == ENABLED);
1842 handleSetMobileData(enabled);
1843 break;
1844 }
Robert Greenwalt434203a2010-10-11 16:00:27 -07001845 case EVENT_APPLY_GLOBAL_HTTP_PROXY:
1846 {
1847 handleDeprecatedGlobalHttpProxy();
Robert Greenwaltd55a6b42011-03-25 13:09:25 -07001848 break;
1849 }
1850 case EVENT_SET_DEPENDENCY_MET:
1851 {
1852 boolean met = (msg.arg1 == ENABLED);
1853 handleSetDependencyMet(msg.arg2, met);
1854 break;
Robert Greenwalt434203a2010-10-11 16:00:27 -07001855 }
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001856 }
1857 }
1858 }
Robert Greenwaltd0e18ff2010-01-26 11:40:34 -08001859
1860 // javadoc from interface
Robert Greenwalt5a735062010-03-02 17:25:02 -08001861 public int tether(String iface) {
Robert Greenwaltd0e18ff2010-01-26 11:40:34 -08001862 enforceTetherChangePermission();
Robert Greenwalt5a735062010-03-02 17:25:02 -08001863
1864 if (isTetheringSupported()) {
1865 return mTethering.tether(iface);
1866 } else {
1867 return ConnectivityManager.TETHER_ERROR_UNSUPPORTED;
1868 }
Robert Greenwaltd0e18ff2010-01-26 11:40:34 -08001869 }
1870
1871 // javadoc from interface
Robert Greenwalt5a735062010-03-02 17:25:02 -08001872 public int untether(String iface) {
Robert Greenwaltd0e18ff2010-01-26 11:40:34 -08001873 enforceTetherChangePermission();
Robert Greenwalt5a735062010-03-02 17:25:02 -08001874
1875 if (isTetheringSupported()) {
1876 return mTethering.untether(iface);
1877 } else {
1878 return ConnectivityManager.TETHER_ERROR_UNSUPPORTED;
1879 }
1880 }
1881
1882 // javadoc from interface
1883 public int getLastTetherError(String iface) {
1884 enforceTetherAccessPermission();
1885
1886 if (isTetheringSupported()) {
1887 return mTethering.getLastTetherError(iface);
1888 } else {
1889 return ConnectivityManager.TETHER_ERROR_UNSUPPORTED;
1890 }
Robert Greenwalt2a091d72010-02-11 18:18:40 -08001891 }
1892
1893 // TODO - proper iface API for selection by property, inspection, etc
1894 public String[] getTetherableUsbRegexs() {
1895 enforceTetherAccessPermission();
1896 if (isTetheringSupported()) {
1897 return mTethering.getTetherableUsbRegexs();
1898 } else {
1899 return new String[0];
1900 }
1901 }
1902
1903 public String[] getTetherableWifiRegexs() {
1904 enforceTetherAccessPermission();
1905 if (isTetheringSupported()) {
1906 return mTethering.getTetherableWifiRegexs();
1907 } else {
1908 return new String[0];
1909 }
Robert Greenwaltd0e18ff2010-01-26 11:40:34 -08001910 }
1911
Danica Chang6fdd0c62010-08-11 14:54:43 -07001912 public String[] getTetherableBluetoothRegexs() {
1913 enforceTetherAccessPermission();
1914 if (isTetheringSupported()) {
1915 return mTethering.getTetherableBluetoothRegexs();
1916 } else {
1917 return new String[0];
1918 }
1919 }
1920
Robert Greenwaltd0e18ff2010-01-26 11:40:34 -08001921 // TODO - move iface listing, queries, etc to new module
1922 // javadoc from interface
1923 public String[] getTetherableIfaces() {
Robert Greenwalt2a091d72010-02-11 18:18:40 -08001924 enforceTetherAccessPermission();
Robert Greenwaltd0e18ff2010-01-26 11:40:34 -08001925 return mTethering.getTetherableIfaces();
1926 }
1927
1928 public String[] getTetheredIfaces() {
Robert Greenwalt2a091d72010-02-11 18:18:40 -08001929 enforceTetherAccessPermission();
Robert Greenwaltd0e18ff2010-01-26 11:40:34 -08001930 return mTethering.getTetheredIfaces();
1931 }
Robert Greenwalt2a091d72010-02-11 18:18:40 -08001932
Robert Greenwalt5a735062010-03-02 17:25:02 -08001933 public String[] getTetheringErroredIfaces() {
1934 enforceTetherAccessPermission();
1935 return mTethering.getErroredIfaces();
1936 }
1937
Robert Greenwalt2a091d72010-02-11 18:18:40 -08001938 // if ro.tether.denied = true we default to no tethering
1939 // gservices could set the secure setting to 1 though to enable it on a build where it
1940 // had previously been turned off.
1941 public boolean isTetheringSupported() {
1942 enforceTetherAccessPermission();
1943 int defaultVal = (SystemProperties.get("ro.tether.denied").equals("true") ? 0 : 1);
Robert Greenwaltc9d5fb72010-02-25 12:29:30 -08001944 boolean tetherEnabledInSettings = (Settings.Secure.getInt(mContext.getContentResolver(),
1945 Settings.Secure.TETHER_SUPPORTED, defaultVal) != 0);
1946 return tetherEnabledInSettings && mTetheringConfigValid;
Robert Greenwalt2a091d72010-02-11 18:18:40 -08001947 }
Robert Greenwalt14f2ef42010-06-15 12:19:37 -07001948
1949 // An API NetworkStateTrackers can call when they lose their network.
1950 // This will automatically be cleared after X seconds or a network becomes CONNECTED,
1951 // whichever happens first. The timer is started by the first caller and not
1952 // restarted by subsequent callers.
1953 public void requestNetworkTransitionWakelock(String forWhom) {
1954 enforceConnectivityInternalPermission();
1955 synchronized (this) {
1956 if (mNetTransitionWakeLock.isHeld()) return;
1957 mNetTransitionWakeLockSerialNumber++;
1958 mNetTransitionWakeLock.acquire();
1959 mNetTransitionWakeLockCausedBy = forWhom;
1960 }
1961 mHandler.sendMessageDelayed(mHandler.obtainMessage(
Robert Greenwaltf3331232010-09-24 14:32:21 -07001962 EVENT_CLEAR_NET_TRANSITION_WAKELOCK,
Robert Greenwalt14f2ef42010-06-15 12:19:37 -07001963 mNetTransitionWakeLockSerialNumber, 0),
1964 mNetTransitionWakeLockTimeout);
1965 return;
1966 }
Robert Greenwaltca4306c2010-09-09 13:15:32 -07001967
Robert Greenwaltd7085fc2010-09-08 15:24:47 -07001968 // 100 percent is full good, 0 is full bad.
1969 public void reportInetCondition(int networkType, int percentage) {
Wink Savilleed9c02b2010-12-03 12:01:38 -08001970 if (DBG) log("reportNetworkCondition(" + networkType + ", " + percentage + ")");
Robert Greenwaltd7085fc2010-09-08 15:24:47 -07001971 mContext.enforceCallingOrSelfPermission(
1972 android.Manifest.permission.STATUS_BAR,
1973 "ConnectivityService");
1974
Robert Greenwalt4e8dfef2010-09-20 14:35:25 -07001975 if (DBG) {
1976 int pid = getCallingPid();
1977 int uid = getCallingUid();
1978 String s = pid + "(" + uid + ") reports inet is " +
1979 (percentage > 50 ? "connected" : "disconnected") + " (" + percentage + ") on " +
1980 "network Type " + networkType + " at " + GregorianCalendar.getInstance().getTime();
1981 mInetLog.add(s);
1982 while(mInetLog.size() > INET_CONDITION_LOG_MAX_SIZE) {
1983 mInetLog.remove(0);
1984 }
1985 }
Robert Greenwaltd7085fc2010-09-08 15:24:47 -07001986 mHandler.sendMessage(mHandler.obtainMessage(
Robert Greenwalt8dcc28b2010-09-23 10:05:56 -07001987 EVENT_INET_CONDITION_CHANGE, networkType, percentage));
1988 }
1989
1990 private void handleInetConditionChange(int netType, int condition) {
1991 if (DBG) {
Wink Savilleed9c02b2010-12-03 12:01:38 -08001992 log("Inet connectivity change, net=" +
Robert Greenwalt8dcc28b2010-09-23 10:05:56 -07001993 netType + ", condition=" + condition +
1994 ",mActiveDefaultNetwork=" + mActiveDefaultNetwork);
1995 }
1996 if (mActiveDefaultNetwork == -1) {
Wink Savilleed9c02b2010-12-03 12:01:38 -08001997 if (DBG) log("no active default network - aborting");
Robert Greenwalt8dcc28b2010-09-23 10:05:56 -07001998 return;
1999 }
2000 if (mActiveDefaultNetwork != netType) {
Wink Savilleed9c02b2010-12-03 12:01:38 -08002001 if (DBG) log("given net not default - aborting");
Robert Greenwalt8dcc28b2010-09-23 10:05:56 -07002002 return;
2003 }
2004 mDefaultInetCondition = condition;
2005 int delay;
2006 if (mInetConditionChangeInFlight == false) {
Wink Savilleed9c02b2010-12-03 12:01:38 -08002007 if (DBG) log("starting a change hold");
Robert Greenwalt8dcc28b2010-09-23 10:05:56 -07002008 // setup a new hold to debounce this
2009 if (mDefaultInetCondition > 50) {
2010 delay = Settings.Secure.getInt(mContext.getContentResolver(),
2011 Settings.Secure.INET_CONDITION_DEBOUNCE_UP_DELAY, 500);
2012 } else {
2013 delay = Settings.Secure.getInt(mContext.getContentResolver(),
2014 Settings.Secure.INET_CONDITION_DEBOUNCE_DOWN_DELAY, 3000);
2015 }
2016 mInetConditionChangeInFlight = true;
2017 mHandler.sendMessageDelayed(mHandler.obtainMessage(EVENT_INET_CONDITION_HOLD_END,
2018 mActiveDefaultNetwork, mDefaultConnectionSequence), delay);
2019 } else {
2020 // we've set the new condition, when this hold ends that will get
2021 // picked up
Wink Savilleed9c02b2010-12-03 12:01:38 -08002022 if (DBG) log("currently in hold - not setting new end evt");
Robert Greenwalt8dcc28b2010-09-23 10:05:56 -07002023 }
2024 }
2025
2026 private void handleInetConditionHoldEnd(int netType, int sequence) {
2027 if (DBG) {
Wink Savilleed9c02b2010-12-03 12:01:38 -08002028 log("Inet hold end, net=" + netType +
Robert Greenwalt8dcc28b2010-09-23 10:05:56 -07002029 ", condition =" + mDefaultInetCondition +
2030 ", published condition =" + mDefaultInetConditionPublished);
2031 }
2032 mInetConditionChangeInFlight = false;
2033
2034 if (mActiveDefaultNetwork == -1) {
Wink Savilleed9c02b2010-12-03 12:01:38 -08002035 if (DBG) log("no active default network - aborting");
Robert Greenwalt8dcc28b2010-09-23 10:05:56 -07002036 return;
2037 }
2038 if (mDefaultConnectionSequence != sequence) {
Wink Savilleed9c02b2010-12-03 12:01:38 -08002039 if (DBG) log("event hold for obsolete network - aborting");
Robert Greenwalt8dcc28b2010-09-23 10:05:56 -07002040 return;
2041 }
2042 if (mDefaultInetConditionPublished == mDefaultInetCondition) {
Wink Savilleed9c02b2010-12-03 12:01:38 -08002043 if (DBG) log("no change in condition - aborting");
Robert Greenwalt8dcc28b2010-09-23 10:05:56 -07002044 return;
2045 }
2046 NetworkInfo networkInfo = mNetTrackers[mActiveDefaultNetwork].getNetworkInfo();
2047 if (networkInfo.isConnected() == false) {
Wink Savilleed9c02b2010-12-03 12:01:38 -08002048 if (DBG) log("default network not connected - aborting");
Robert Greenwalt8dcc28b2010-09-23 10:05:56 -07002049 return;
2050 }
2051 mDefaultInetConditionPublished = mDefaultInetCondition;
2052 sendInetConditionBroadcast(networkInfo);
2053 return;
Robert Greenwaltd7085fc2010-09-08 15:24:47 -07002054 }
Robert Greenwalt434203a2010-10-11 16:00:27 -07002055
2056 public synchronized ProxyProperties getProxy() {
2057 if (mGlobalProxy != null) return mGlobalProxy;
2058 if (mDefaultProxy != null) return mDefaultProxy;
2059 return null;
2060 }
2061
2062 public void setGlobalProxy(ProxyProperties proxyProperties) {
2063 enforceChangePermission();
2064 synchronized (mGlobalProxyLock) {
2065 if (proxyProperties == mGlobalProxy) return;
2066 if (proxyProperties != null && proxyProperties.equals(mGlobalProxy)) return;
2067 if (mGlobalProxy != null && mGlobalProxy.equals(proxyProperties)) return;
2068
2069 String host = "";
2070 int port = 0;
2071 String exclList = "";
2072 if (proxyProperties != null && !TextUtils.isEmpty(proxyProperties.getHost())) {
2073 mGlobalProxy = new ProxyProperties(proxyProperties);
2074 host = mGlobalProxy.getHost();
2075 port = mGlobalProxy.getPort();
2076 exclList = mGlobalProxy.getExclusionList();
2077 } else {
2078 mGlobalProxy = null;
2079 }
2080 ContentResolver res = mContext.getContentResolver();
2081 Settings.Secure.putString(res, Settings.Secure.GLOBAL_HTTP_PROXY_HOST, host);
2082 Settings.Secure.putInt(res, Settings.Secure.GLOBAL_HTTP_PROXY_PORT, port);
Robert Greenwaltb7090d62010-12-02 11:31:00 -08002083 Settings.Secure.putString(res, Settings.Secure.GLOBAL_HTTP_PROXY_EXCLUSION_LIST,
Robert Greenwalt434203a2010-10-11 16:00:27 -07002084 exclList);
2085 }
2086
2087 if (mGlobalProxy == null) {
2088 proxyProperties = mDefaultProxy;
2089 }
2090 sendProxyBroadcast(proxyProperties);
2091 }
2092
Robert Greenwaltb7090d62010-12-02 11:31:00 -08002093 private void loadGlobalProxy() {
2094 ContentResolver res = mContext.getContentResolver();
2095 String host = Settings.Secure.getString(res, Settings.Secure.GLOBAL_HTTP_PROXY_HOST);
2096 int port = Settings.Secure.getInt(res, Settings.Secure.GLOBAL_HTTP_PROXY_PORT, 0);
2097 String exclList = Settings.Secure.getString(res,
2098 Settings.Secure.GLOBAL_HTTP_PROXY_EXCLUSION_LIST);
2099 if (!TextUtils.isEmpty(host)) {
2100 ProxyProperties proxyProperties = new ProxyProperties(host, port, exclList);
2101 synchronized (mGlobalProxyLock) {
2102 mGlobalProxy = proxyProperties;
2103 }
2104 }
2105 }
2106
Robert Greenwalt434203a2010-10-11 16:00:27 -07002107 public ProxyProperties getGlobalProxy() {
2108 synchronized (mGlobalProxyLock) {
2109 return mGlobalProxy;
2110 }
2111 }
2112
2113 private void handleApplyDefaultProxy(int type) {
2114 // check if new default - push it out to all VM if so
2115 ProxyProperties proxy = mNetTrackers[type].getLinkProperties().getHttpProxy();
2116 synchronized (this) {
2117 if (mDefaultProxy != null && mDefaultProxy.equals(proxy)) return;
2118 if (mDefaultProxy == proxy) return;
2119 if (!TextUtils.isEmpty(proxy.getHost())) {
2120 mDefaultProxy = proxy;
2121 } else {
2122 mDefaultProxy = null;
2123 }
2124 }
Wink Savilleed9c02b2010-12-03 12:01:38 -08002125 if (DBG) log("changing default proxy to " + proxy);
Robert Greenwalt434203a2010-10-11 16:00:27 -07002126 if ((proxy == null && mGlobalProxy == null) || proxy.equals(mGlobalProxy)) return;
2127 if (mGlobalProxy != null) return;
2128 sendProxyBroadcast(proxy);
2129 }
2130
2131 private void handleDeprecatedGlobalHttpProxy() {
2132 String proxy = Settings.Secure.getString(mContext.getContentResolver(),
2133 Settings.Secure.HTTP_PROXY);
2134 if (!TextUtils.isEmpty(proxy)) {
2135 String data[] = proxy.split(":");
2136 String proxyHost = data[0];
2137 int proxyPort = 8080;
2138 if (data.length > 1) {
2139 try {
2140 proxyPort = Integer.parseInt(data[1]);
2141 } catch (NumberFormatException e) {
2142 return;
2143 }
2144 }
2145 ProxyProperties p = new ProxyProperties(data[0], proxyPort, "");
2146 setGlobalProxy(p);
2147 }
2148 }
2149
2150 private void sendProxyBroadcast(ProxyProperties proxy) {
Robert Greenwalt55985be2010-12-23 15:51:10 -08002151 if (proxy == null) proxy = new ProxyProperties("", 0, "");
Wink Savilleed9c02b2010-12-03 12:01:38 -08002152 log("sending Proxy Broadcast for " + proxy);
Robert Greenwalt434203a2010-10-11 16:00:27 -07002153 Intent intent = new Intent(Proxy.PROXY_CHANGE_ACTION);
Stan Chesnuttb35d67a2011-01-06 11:00:19 -08002154 intent.addFlags(Intent.FLAG_RECEIVER_REPLACE_PENDING |
2155 Intent.FLAG_RECEIVER_REGISTERED_ONLY_BEFORE_BOOT);
Robert Greenwalt434203a2010-10-11 16:00:27 -07002156 intent.putExtra(Proxy.EXTRA_PROXY_INFO, proxy);
Robert Greenwalta2e13392010-12-06 11:29:17 -08002157 mContext.sendStickyBroadcast(intent);
Robert Greenwalt434203a2010-10-11 16:00:27 -07002158 }
2159
2160 private static class SettingsObserver extends ContentObserver {
2161 private int mWhat;
2162 private Handler mHandler;
2163 SettingsObserver(Handler handler, int what) {
2164 super(handler);
2165 mHandler = handler;
2166 mWhat = what;
2167 }
2168
2169 void observe(Context context) {
2170 ContentResolver resolver = context.getContentResolver();
2171 resolver.registerContentObserver(Settings.Secure.getUriFor(
2172 Settings.Secure.HTTP_PROXY), false, this);
2173 }
2174
2175 @Override
2176 public void onChange(boolean selfChange) {
2177 mHandler.obtainMessage(mWhat).sendToTarget();
2178 }
2179 }
Wink Savilleed9c02b2010-12-03 12:01:38 -08002180
2181 private void log(String s) {
2182 Slog.d(TAG, s);
2183 }
2184
2185 private void loge(String s) {
2186 Slog.e(TAG, s);
2187 }
Wink Saville9d7d6282011-03-12 14:52:01 -08002188 int convertFeatureToNetworkType(String feature){
2189 int networkType = -1;
2190 if (TextUtils.equals(feature, Phone.FEATURE_ENABLE_MMS)) {
2191 networkType = ConnectivityManager.TYPE_MOBILE_MMS;
2192 } else if (TextUtils.equals(feature, Phone.FEATURE_ENABLE_SUPL)) {
2193 networkType = ConnectivityManager.TYPE_MOBILE_SUPL;
2194 } else if (TextUtils.equals(feature, Phone.FEATURE_ENABLE_DUN) ||
2195 TextUtils.equals(feature, Phone.FEATURE_ENABLE_DUN_ALWAYS)) {
2196 networkType = ConnectivityManager.TYPE_MOBILE_DUN;
2197 } else if (TextUtils.equals(feature, Phone.FEATURE_ENABLE_HIPRI)) {
2198 networkType = ConnectivityManager.TYPE_MOBILE_HIPRI;
2199 } else if (TextUtils.equals(feature, Phone.FEATURE_ENABLE_FOTA)) {
2200 networkType = ConnectivityManager.TYPE_MOBILE_FOTA;
2201 } else if (TextUtils.equals(feature, Phone.FEATURE_ENABLE_IMS)) {
2202 networkType = ConnectivityManager.TYPE_MOBILE_IMS;
2203 } else if (TextUtils.equals(feature, Phone.FEATURE_ENABLE_CBS)) {
2204 networkType = ConnectivityManager.TYPE_MOBILE_CBS;
2205 }
2206 return networkType;
2207 }
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002208}