blob: d77ab60ca9098ba2ea730d65c655ca9f70a5cade [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 Greenwalt8dcc28b2010-09-23 10:05:56 -0700722 mHandler.sendMessageDelayed(mHandler.obtainMessage(EVENT_RESTORE_DEFAULT_NETWORK,
Robert Greenwalt9c75d4a2009-09-27 17:27:04 -0700723 f), getRestoreDefaultNetworkDelay());
724
Robert Greenwalt42acef32009-08-12 16:08:25 -0700725
Robert Greenwalta64bf832009-08-19 20:19:33 -0700726 if ((ni.isConnectedOrConnecting() == true) &&
727 !network.isTeardownRequested()) {
Robert Greenwalt42acef32009-08-12 16:08:25 -0700728 if (ni.isConnected() == true) {
729 // add the pid-specific dns
Robert Greenwaltc76b8fa2010-07-23 15:46:26 -0700730 handleDnsConfigurationChange(networkType);
Wink Savilleed9c02b2010-12-03 12:01:38 -0800731 if (DBG) log("special network already active");
Robert Greenwalt42acef32009-08-12 16:08:25 -0700732 return Phone.APN_ALREADY_ACTIVE;
733 }
Wink Savilleed9c02b2010-12-03 12:01:38 -0800734 if (DBG) log("special network already connecting");
Robert Greenwalt42acef32009-08-12 16:08:25 -0700735 return Phone.APN_REQUEST_STARTED;
736 }
737
738 // check if the radio in play can make another contact
739 // assume if cannot for now
740
Wink Savilleed9c02b2010-12-03 12:01:38 -0800741 if (DBG) log("reconnecting to special network");
Robert Greenwalt42acef32009-08-12 16:08:25 -0700742 network.reconnect();
743 return Phone.APN_REQUEST_STARTED;
744 } else {
Robert Greenwalt0be1e982010-12-15 13:26:33 -0800745 // need to remember this unsupported request so we respond appropriately on stop
746 synchronized(this) {
747 mFeatureUsers.add(f);
748 if (!mNetRequestersPids[usedNetworkType].contains(currentPid)) {
749 // this gets used for per-pid dns when connected
750 mNetRequestersPids[usedNetworkType].add(currentPid);
751 }
752 }
Robert Greenwalt02648a42010-05-18 10:52:51 -0700753 return -1;
Robert Greenwalt42acef32009-08-12 16:08:25 -0700754 }
755 }
756 return Phone.APN_TYPE_NOT_AVAILABLE;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800757 }
758
Robert Greenwalt9c75d4a2009-09-27 17:27:04 -0700759 // javadoc from interface
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800760 public int stopUsingNetworkFeature(int networkType, String feature) {
Robert Greenwaltb8f16342009-10-06 17:52:40 -0700761 enforceChangePermission();
762
Robert Greenwalt9c75d4a2009-09-27 17:27:04 -0700763 int pid = getCallingPid();
764 int uid = getCallingUid();
765
766 FeatureUser u = null;
767 boolean found = false;
768
769 synchronized(this) {
770 for (int i = 0; i < mFeatureUsers.size() ; i++) {
771 u = (FeatureUser)mFeatureUsers.get(i);
772 if (uid == u.mUid && pid == u.mPid &&
773 networkType == u.mNetworkType &&
774 TextUtils.equals(feature, u.mFeature)) {
775 found = true;
776 break;
777 }
778 }
779 }
780 if (found && u != null) {
781 // stop regardless of how many other time this proc had called start
782 return stopUsingNetworkFeature(u, true);
783 } else {
784 // none found!
Wink Savilleed9c02b2010-12-03 12:01:38 -0800785 if (DBG) log("ignoring stopUsingNetworkFeature - not a live request");
Robert Greenwalt9c75d4a2009-09-27 17:27:04 -0700786 return 1;
787 }
Robert Greenwalt42acef32009-08-12 16:08:25 -0700788 }
789
Robert Greenwalt9c75d4a2009-09-27 17:27:04 -0700790 private int stopUsingNetworkFeature(FeatureUser u, boolean ignoreDups) {
791 int networkType = u.mNetworkType;
792 String feature = u.mFeature;
793 int pid = u.mPid;
794 int uid = u.mUid;
795
796 NetworkStateTracker tracker = null;
797 boolean callTeardown = false; // used to carry our decision outside of sync block
798
Robert Greenwalt42acef32009-08-12 16:08:25 -0700799 if (DBG) {
Wink Savilleed9c02b2010-12-03 12:01:38 -0800800 log("stopUsingNetworkFeature for net " + networkType +
Robert Greenwalt42acef32009-08-12 16:08:25 -0700801 ": " + feature);
802 }
Robert Greenwaltb8f16342009-10-06 17:52:40 -0700803
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800804 if (!ConnectivityManager.isNetworkTypeValid(networkType)) {
805 return -1;
806 }
Robert Greenwalt42acef32009-08-12 16:08:25 -0700807
Robert Greenwalt9c75d4a2009-09-27 17:27:04 -0700808 // need to link the mFeatureUsers list with the mNetRequestersPids state in this
809 // sync block
810 synchronized(this) {
811 // check if this process still has an outstanding start request
812 if (!mFeatureUsers.contains(u)) {
Wink Savilleed9c02b2010-12-03 12:01:38 -0800813 if (DBG) log("ignoring - this process has no outstanding requests");
Robert Greenwalt42acef32009-08-12 16:08:25 -0700814 return 1;
815 }
Robert Greenwalt9c75d4a2009-09-27 17:27:04 -0700816 u.unlinkDeathRecipient();
817 mFeatureUsers.remove(mFeatureUsers.indexOf(u));
818 // If we care about duplicate requests, check for that here.
819 //
820 // This is done to support the extension of a request - the app
821 // can request we start the network feature again and renew the
822 // auto-shutoff delay. Normal "stop" calls from the app though
823 // do not pay attention to duplicate requests - in effect the
824 // API does not refcount and a single stop will counter multiple starts.
825 if (ignoreDups == false) {
826 for (int i = 0; i < mFeatureUsers.size() ; i++) {
827 FeatureUser x = (FeatureUser)mFeatureUsers.get(i);
828 if (x.mUid == u.mUid && x.mPid == u.mPid &&
829 x.mNetworkType == u.mNetworkType &&
830 TextUtils.equals(x.mFeature, u.mFeature)) {
Wink Savilleed9c02b2010-12-03 12:01:38 -0800831 if (DBG) log("ignoring stopUsingNetworkFeature as dup is found");
Robert Greenwalt9c75d4a2009-09-27 17:27:04 -0700832 return 1;
833 }
834 }
Robert Greenwalt42acef32009-08-12 16:08:25 -0700835 }
Robert Greenwalt9c75d4a2009-09-27 17:27:04 -0700836
837 // TODO - move to MobileDataStateTracker
838 int usedNetworkType = networkType;
839 if (networkType == ConnectivityManager.TYPE_MOBILE) {
Wink Saville9d7d6282011-03-12 14:52:01 -0800840 usedNetworkType = convertFeatureToNetworkType(feature);
841 if (usedNetworkType < 0) {
842 usedNetworkType = networkType;
Robert Greenwalt9c75d4a2009-09-27 17:27:04 -0700843 }
844 }
845 tracker = mNetTrackers[usedNetworkType];
Robert Greenwalt5154ae762009-10-30 14:17:42 -0700846 if (tracker == null) {
Wink Savilleed9c02b2010-12-03 12:01:38 -0800847 if (DBG) log("ignoring - no known tracker for net type " + usedNetworkType);
Robert Greenwalt5154ae762009-10-30 14:17:42 -0700848 return -1;
849 }
850 if (usedNetworkType != networkType) {
Robert Greenwalt9c75d4a2009-09-27 17:27:04 -0700851 Integer currentPid = new Integer(pid);
Robert Greenwalt9c75d4a2009-09-27 17:27:04 -0700852 mNetRequestersPids[usedNetworkType].remove(currentPid);
Robert Greenwalt421c72b2009-12-17 14:54:59 -0800853 reassessPidDns(pid, true);
Robert Greenwalt9c75d4a2009-09-27 17:27:04 -0700854 if (mNetRequestersPids[usedNetworkType].size() != 0) {
Wink Savilleed9c02b2010-12-03 12:01:38 -0800855 if (DBG) log("not tearing down special network - " +
Robert Greenwalt9c75d4a2009-09-27 17:27:04 -0700856 "others still using it");
857 return 1;
858 }
859 callTeardown = true;
Robert Greenwalt19b9ab42011-01-10 11:58:31 -0800860 } else {
861 if (DBG) log("not a known feature - dropping");
Robert Greenwalt9c75d4a2009-09-27 17:27:04 -0700862 }
863 }
Wink Savilleed9c02b2010-12-03 12:01:38 -0800864 if (DBG) log("Doing network teardown");
Robert Greenwalt9c75d4a2009-09-27 17:27:04 -0700865 if (callTeardown) {
866 tracker.teardown();
Robert Greenwalt42acef32009-08-12 16:08:25 -0700867 return 1;
868 } else {
Robert Greenwalt02648a42010-05-18 10:52:51 -0700869 return -1;
Robert Greenwalt42acef32009-08-12 16:08:25 -0700870 }
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800871 }
872
873 /**
Robert Greenwalt585ac0f2010-08-27 09:24:29 -0700874 * @deprecated use requestRouteToHostAddress instead
875 *
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800876 * Ensure that a network route exists to deliver traffic to the specified
877 * host via the specified network interface.
Robert Greenwalt86e9e552009-07-16 17:21:39 -0700878 * @param networkType the type of the network over which traffic to the
879 * specified host is to be routed
880 * @param hostAddress the IP address of the host to which the route is
881 * desired
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800882 * @return {@code true} on success, {@code false} on failure
883 */
884 public boolean requestRouteToHost(int networkType, int hostAddress) {
Robert Greenwalt585ac0f2010-08-27 09:24:29 -0700885 InetAddress inetAddress = NetworkUtils.intToInetAddress(hostAddress);
886
887 if (inetAddress == null) {
888 return false;
889 }
890
891 return requestRouteToHostAddress(networkType, inetAddress.getAddress());
892 }
893
894 /**
895 * Ensure that a network route exists to deliver traffic to the specified
896 * host via the specified network interface.
897 * @param networkType the type of the network over which traffic to the
898 * specified host is to be routed
899 * @param hostAddress the IP address of the host to which the route is
900 * desired
901 * @return {@code true} on success, {@code false} on failure
902 */
903 public boolean requestRouteToHostAddress(int networkType, byte[] hostAddress) {
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800904 enforceChangePermission();
905 if (!ConnectivityManager.isNetworkTypeValid(networkType)) {
906 return false;
907 }
908 NetworkStateTracker tracker = mNetTrackers[networkType];
Robert Greenwalt8206ff32009-09-10 15:06:20 -0700909
Robert Greenwalt5154ae762009-10-30 14:17:42 -0700910 if (tracker == null || !tracker.getNetworkInfo().isConnected() ||
911 tracker.isTeardownRequested()) {
Robert Greenwalt8206ff32009-09-10 15:06:20 -0700912 if (DBG) {
Wink Savilleed9c02b2010-12-03 12:01:38 -0800913 log("requestRouteToHostAddress on down network " +
Robert Greenwalt585ac0f2010-08-27 09:24:29 -0700914 "(" + networkType + ") - dropped");
Robert Greenwalt8206ff32009-09-10 15:06:20 -0700915 }
916 return false;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800917 }
Robert Greenwalt47f69fe2010-06-15 15:43:39 -0700918 try {
Robert Greenwalt585ac0f2010-08-27 09:24:29 -0700919 InetAddress addr = InetAddress.getByAddress(hostAddress);
Robert Greenwalt47f69fe2010-06-15 15:43:39 -0700920 return addHostRoute(tracker, addr);
921 } catch (UnknownHostException e) {}
922 return false;
Irfan Sheriffd649c122010-06-09 15:39:36 -0700923 }
924
925 /**
926 * Ensure that a network route exists to deliver traffic to the specified
927 * host via the mobile data network.
928 * @param hostAddress the IP address of the host to which the route is desired,
929 * in network byte order.
Robert Greenwalt47f69fe2010-06-15 15:43:39 -0700930 * TODO - deprecate
Irfan Sheriffd649c122010-06-09 15:39:36 -0700931 * @return {@code true} on success, {@code false} on failure
932 */
Robert Greenwalt47f69fe2010-06-15 15:43:39 -0700933 private boolean addHostRoute(NetworkStateTracker nt, InetAddress hostAddress) {
Irfan Sheriffd649c122010-06-09 15:39:36 -0700934 if (nt.getNetworkInfo().getType() == ConnectivityManager.TYPE_WIFI) {
935 return false;
936 }
937
Robert Greenwalt37e65eb2010-08-30 10:56:47 -0700938 LinkProperties p = nt.getLinkProperties();
Robert Greenwalt47f69fe2010-06-15 15:43:39 -0700939 if (p == null) return false;
940 String interfaceName = p.getInterfaceName();
Irfan Sheriffd649c122010-06-09 15:39:36 -0700941
942 if (DBG) {
Wink Savilleed9c02b2010-12-03 12:01:38 -0800943 log("Requested host route to " + hostAddress + "(" + interfaceName + ")");
Irfan Sheriffd649c122010-06-09 15:39:36 -0700944 }
Robert Greenwalt47f69fe2010-06-15 15:43:39 -0700945 if (interfaceName != null) {
Robert Greenwalt585ac0f2010-08-27 09:24:29 -0700946 return NetworkUtils.addHostRoute(interfaceName, hostAddress, null);
Irfan Sheriffd649c122010-06-09 15:39:36 -0700947 } else {
Wink Savilleed9c02b2010-12-03 12:01:38 -0800948 if (DBG) loge("addHostRoute failed due to null interface name");
Irfan Sheriffd649c122010-06-09 15:39:36 -0700949 return false;
950 }
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800951 }
952
953 /**
954 * @see ConnectivityManager#getBackgroundDataSetting()
955 */
956 public boolean getBackgroundDataSetting() {
Robert Greenwaltd825ea42010-12-29 16:15:02 -0800957 return mBackgroundDataEnabled.get();
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800958 }
Robert Greenwalt86e9e552009-07-16 17:21:39 -0700959
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800960 /**
961 * @see ConnectivityManager#setBackgroundDataSetting(boolean)
962 */
963 public void setBackgroundDataSetting(boolean allowBackgroundDataUsage) {
964 mContext.enforceCallingOrSelfPermission(
965 android.Manifest.permission.CHANGE_BACKGROUND_DATA_SETTING,
966 "ConnectivityService");
Robert Greenwalt86e9e552009-07-16 17:21:39 -0700967
Robert Greenwaltd825ea42010-12-29 16:15:02 -0800968 mBackgroundDataEnabled.set(allowBackgroundDataUsage);
969
Robert Greenwalt8dcc28b2010-09-23 10:05:56 -0700970 mHandler.sendMessage(mHandler.obtainMessage(EVENT_SET_BACKGROUND_DATA,
971 (allowBackgroundDataUsage ? ENABLED : DISABLED), 0));
972 }
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800973
Robert Greenwalt8dcc28b2010-09-23 10:05:56 -0700974 private void handleSetBackgroundData(boolean enabled) {
Robert Greenwaltdb4afae2011-02-25 13:44:09 -0800975 Settings.Secure.putInt(mContext.getContentResolver(),
976 Settings.Secure.BACKGROUND_DATA, enabled ? 1 : 0);
977 Intent broadcast = new Intent(
978 ConnectivityManager.ACTION_BACKGROUND_DATA_SETTING_CHANGED);
979 mContext.sendBroadcast(broadcast);
Robert Greenwalt86e9e552009-07-16 17:21:39 -0700980 }
981
Robert Greenwaltc03fa502010-02-23 18:58:05 -0800982 /**
983 * @see ConnectivityManager#getMobileDataEnabled()
984 */
985 public boolean getMobileDataEnabled() {
Wink Savillee7982682010-12-07 10:31:02 -0800986 // TODO: This detail should probably be in DataConnectionTracker's
987 // which is where we store the value and maybe make this
988 // asynchronous.
Robert Greenwaltc03fa502010-02-23 18:58:05 -0800989 enforceAccessPermission();
990 boolean retVal = Settings.Secure.getInt(mContext.getContentResolver(),
991 Settings.Secure.MOBILE_DATA, 1) == 1;
Wink Savilleed9c02b2010-12-03 12:01:38 -0800992 if (DBG) log("getMobileDataEnabled returning " + retVal);
Robert Greenwaltc03fa502010-02-23 18:58:05 -0800993 return retVal;
994 }
995
Robert Greenwaltd55a6b42011-03-25 13:09:25 -0700996 public void setDataDependency(int networkType, boolean met) {
997 enforceChangePermission();
998 if (DBG) {
999 log("setDataDependency(" + networkType + ", " + met + ")");
1000 }
1001 mHandler.sendMessage(mHandler.obtainMessage(EVENT_SET_DEPENDENCY_MET,
1002 (met ? ENABLED : DISABLED), networkType));
1003 }
1004
1005 private void handleSetDependencyMet(int networkType, boolean met) {
1006 if (mNetTrackers[networkType] != null) {
1007 if (DBG) {
1008 log("handleSetDependencyMet(" + networkType + ", " + met + ")");
1009 }
1010 mNetTrackers[networkType].setDependencyMet(met);
1011 }
1012 }
1013
Robert Greenwaltc03fa502010-02-23 18:58:05 -08001014 /**
1015 * @see ConnectivityManager#setMobileDataEnabled(boolean)
1016 */
Robert Greenwalt8dcc28b2010-09-23 10:05:56 -07001017 public void setMobileDataEnabled(boolean enabled) {
Robert Greenwaltc03fa502010-02-23 18:58:05 -08001018 enforceChangePermission();
Wink Savilleed9c02b2010-12-03 12:01:38 -08001019 if (DBG) log("setMobileDataEnabled(" + enabled + ")");
Robert Greenwaltc03fa502010-02-23 18:58:05 -08001020
Robert Greenwalt8dcc28b2010-09-23 10:05:56 -07001021 mHandler.sendMessage(mHandler.obtainMessage(EVENT_SET_MOBILE_DATA,
Robert Greenwaltd55a6b42011-03-25 13:09:25 -07001022 (enabled ? ENABLED : DISABLED), 0));
Robert Greenwalt8dcc28b2010-09-23 10:05:56 -07001023 }
1024
1025 private void handleSetMobileData(boolean enabled) {
Wink Savillee7982682010-12-07 10:31:02 -08001026 if (mNetTrackers[ConnectivityManager.TYPE_MOBILE] != null) {
1027 if (DBG) {
1028 Slog.d(TAG, mNetTrackers[ConnectivityManager.TYPE_MOBILE].toString() + enabled);
Robert Greenwaltc03fa502010-02-23 18:58:05 -08001029 }
Wink Savillee7982682010-12-07 10:31:02 -08001030 mNetTrackers[ConnectivityManager.TYPE_MOBILE].setDataEnable(enabled);
Robert Greenwaltc03fa502010-02-23 18:58:05 -08001031 }
1032 }
1033
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001034 private void enforceAccessPermission() {
Robert Greenwalt86e9e552009-07-16 17:21:39 -07001035 mContext.enforceCallingOrSelfPermission(
1036 android.Manifest.permission.ACCESS_NETWORK_STATE,
1037 "ConnectivityService");
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001038 }
1039
1040 private void enforceChangePermission() {
Robert Greenwalt86e9e552009-07-16 17:21:39 -07001041 mContext.enforceCallingOrSelfPermission(
1042 android.Manifest.permission.CHANGE_NETWORK_STATE,
1043 "ConnectivityService");
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001044 }
1045
Robert Greenwaltd0e18ff2010-01-26 11:40:34 -08001046 // TODO Make this a special check when it goes public
1047 private void enforceTetherChangePermission() {
1048 mContext.enforceCallingOrSelfPermission(
1049 android.Manifest.permission.CHANGE_NETWORK_STATE,
1050 "ConnectivityService");
1051 }
1052
Robert Greenwalt2a091d72010-02-11 18:18:40 -08001053 private void enforceTetherAccessPermission() {
1054 mContext.enforceCallingOrSelfPermission(
1055 android.Manifest.permission.ACCESS_NETWORK_STATE,
1056 "ConnectivityService");
1057 }
1058
Robert Greenwalt14f2ef42010-06-15 12:19:37 -07001059 private void enforceConnectivityInternalPermission() {
1060 mContext.enforceCallingOrSelfPermission(
1061 android.Manifest.permission.CONNECTIVITY_INTERNAL,
1062 "ConnectivityService");
1063 }
1064
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001065 /**
Robert Greenwalt86e9e552009-07-16 17:21:39 -07001066 * Handle a {@code DISCONNECTED} event. If this pertains to the non-active
1067 * network, we ignore it. If it is for the active network, we send out a
1068 * broadcast. But first, we check whether it might be possible to connect
1069 * to a different network.
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001070 * @param info the {@code NetworkInfo} for the network
1071 */
1072 private void handleDisconnect(NetworkInfo info) {
1073
Robert Greenwalt42acef32009-08-12 16:08:25 -07001074 int prevNetType = info.getType();
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001075
Robert Greenwalt42acef32009-08-12 16:08:25 -07001076 mNetTrackers[prevNetType].setTeardownRequested(false);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001077 /*
1078 * If the disconnected network is not the active one, then don't report
1079 * this as a loss of connectivity. What probably happened is that we're
1080 * getting the disconnect for a network that we explicitly disabled
1081 * in accordance with network preference policies.
1082 */
Robert Greenwaltd55a6b42011-03-25 13:09:25 -07001083 if (!mNetConfigs[prevNetType].isDefault()) {
Robert Greenwalt42acef32009-08-12 16:08:25 -07001084 List pids = mNetRequestersPids[prevNetType];
1085 for (int i = 0; i<pids.size(); i++) {
1086 Integer pid = (Integer)pids.get(i);
1087 // will remove them because the net's no longer connected
1088 // need to do this now as only now do we know the pids and
1089 // can properly null things that are no longer referenced.
1090 reassessPidDns(pid.intValue(), false);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001091 }
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001092 }
1093
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001094 Intent intent = new Intent(ConnectivityManager.CONNECTIVITY_ACTION);
1095 intent.putExtra(ConnectivityManager.EXTRA_NETWORK_INFO, info);
1096 if (info.isFailover()) {
1097 intent.putExtra(ConnectivityManager.EXTRA_IS_FAILOVER, true);
1098 info.setFailover(false);
1099 }
1100 if (info.getReason() != null) {
1101 intent.putExtra(ConnectivityManager.EXTRA_REASON, info.getReason());
1102 }
1103 if (info.getExtraInfo() != null) {
Robert Greenwalt86e9e552009-07-16 17:21:39 -07001104 intent.putExtra(ConnectivityManager.EXTRA_EXTRA_INFO,
1105 info.getExtraInfo());
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001106 }
Robert Greenwalt42acef32009-08-12 16:08:25 -07001107
Robert Greenwaltd55a6b42011-03-25 13:09:25 -07001108 if (mNetConfigs[prevNetType].isDefault()) {
Robert Greenwaltf21ef7d2011-01-11 13:56:33 -08001109 tryFailover(prevNetType);
1110 if (mActiveDefaultNetwork != -1) {
1111 NetworkInfo switchTo = mNetTrackers[mActiveDefaultNetwork].getNetworkInfo();
Robert Greenwaltcc4b4012010-01-25 17:54:29 -08001112 intent.putExtra(ConnectivityManager.EXTRA_OTHER_NETWORK_INFO, switchTo);
1113 } else {
Robert Greenwalt029be812010-09-20 18:01:43 -07001114 mDefaultInetConditionPublished = 0; // we're not connected anymore
Robert Greenwaltcc4b4012010-01-25 17:54:29 -08001115 intent.putExtra(ConnectivityManager.EXTRA_NO_CONNECTIVITY, true);
1116 }
Robert Greenwaltda03c4e2010-01-20 19:29:41 -08001117 }
Robert Greenwalt029be812010-09-20 18:01:43 -07001118 intent.putExtra(ConnectivityManager.EXTRA_INET_CONDITION, mDefaultInetConditionPublished);
Robert Greenwaltda03c4e2010-01-20 19:29:41 -08001119 // do this before we broadcast the change
Robert Greenwaltc76b8fa2010-07-23 15:46:26 -07001120 handleConnectivityChange(prevNetType);
Robert Greenwaltda03c4e2010-01-20 19:29:41 -08001121
1122 sendStickyBroadcast(intent);
1123 /*
1124 * If the failover network is already connected, then immediately send
1125 * out a followup broadcast indicating successful failover
1126 */
Robert Greenwaltf21ef7d2011-01-11 13:56:33 -08001127 if (mActiveDefaultNetwork != -1) {
1128 sendConnectedBroadcast(mNetTrackers[mActiveDefaultNetwork].getNetworkInfo());
Robert Greenwaltda03c4e2010-01-20 19:29:41 -08001129 }
1130 }
1131
Robert Greenwaltf21ef7d2011-01-11 13:56:33 -08001132 private void tryFailover(int prevNetType) {
Robert Greenwalt42acef32009-08-12 16:08:25 -07001133 /*
Robert Greenwaltbff90182011-01-06 15:41:07 -08001134 * If this is a default network, check if other defaults are available.
1135 * Try to reconnect on all available and let them hash it out when
1136 * more than one connects.
Robert Greenwalt42acef32009-08-12 16:08:25 -07001137 */
Robert Greenwaltd55a6b42011-03-25 13:09:25 -07001138 if (mNetConfigs[prevNetType].isDefault()) {
Robert Greenwalt42acef32009-08-12 16:08:25 -07001139 if (mActiveDefaultNetwork == prevNetType) {
1140 mActiveDefaultNetwork = -1;
1141 }
1142
Robert Greenwaltf21ef7d2011-01-11 13:56:33 -08001143 // don't signal a reconnect for anything lower or equal priority than our
1144 // current connected default
1145 // TODO - don't filter by priority now - nice optimization but risky
1146// int currentPriority = -1;
1147// if (mActiveDefaultNetwork != -1) {
Robert Greenwaltd55a6b42011-03-25 13:09:25 -07001148// currentPriority = mNetConfigs[mActiveDefaultNetwork].mPriority;
Robert Greenwaltf21ef7d2011-01-11 13:56:33 -08001149// }
Robert Greenwaltda03c4e2010-01-20 19:29:41 -08001150 for (int checkType=0; checkType <= ConnectivityManager.MAX_NETWORK_TYPE; checkType++) {
Robert Greenwalt5154ae762009-10-30 14:17:42 -07001151 if (checkType == prevNetType) continue;
Robert Greenwaltd55a6b42011-03-25 13:09:25 -07001152 if (mNetConfigs[checkType] == null) continue;
1153 if (!mNetConfigs[checkType].isDefault()) continue;
Wink Saville9f7a0b22011-01-26 15:43:49 -08001154
1155// Enabling the isAvailable() optimization caused mobile to not get
1156// selected if it was in the middle of error handling. Specifically
1157// a moble connection that took 30 seconds to complete the DEACTIVATE_DATA_CALL
1158// would not be available and we wouldn't get connected to anything.
1159// So removing the isAvailable() optimization below for now. TODO: This
1160// optimization should work and we need to investigate why it doesn't work.
1161// This could be related to how DEACTIVATE_DATA_CALL is reporting its
1162// complete before it is really complete.
1163// if (!mNetTrackers[checkType].isAvailable()) continue;
1164
Robert Greenwaltd55a6b42011-03-25 13:09:25 -07001165// if (currentPriority >= mNetConfigs[checkType].mPriority) continue;
Robert Greenwalt42acef32009-08-12 16:08:25 -07001166
Robert Greenwaltf21ef7d2011-01-11 13:56:33 -08001167 NetworkStateTracker checkTracker = mNetTrackers[checkType];
1168 NetworkInfo checkInfo = checkTracker.getNetworkInfo();
1169 if (!checkInfo.isConnectedOrConnecting() || checkTracker.isTeardownRequested()) {
1170 checkInfo.setFailover(true);
1171 checkTracker.reconnect();
Robert Greenwalt42acef32009-08-12 16:08:25 -07001172 }
Robert Greenwaltf21ef7d2011-01-11 13:56:33 -08001173 if (DBG) log("Attempting to switch to " + checkInfo.getTypeName());
Robert Greenwalt42acef32009-08-12 16:08:25 -07001174 }
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001175 }
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001176 }
1177
1178 private void sendConnectedBroadcast(NetworkInfo info) {
Robert Greenwalt1e9aac22010-09-15 17:36:33 -07001179 sendGeneralBroadcast(info, ConnectivityManager.CONNECTIVITY_ACTION);
1180 }
1181
1182 private void sendInetConditionBroadcast(NetworkInfo info) {
1183 sendGeneralBroadcast(info, ConnectivityManager.INET_CONDITION_ACTION);
1184 }
1185
1186 private void sendGeneralBroadcast(NetworkInfo info, String bcastType) {
1187 Intent intent = new Intent(bcastType);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001188 intent.putExtra(ConnectivityManager.EXTRA_NETWORK_INFO, info);
1189 if (info.isFailover()) {
1190 intent.putExtra(ConnectivityManager.EXTRA_IS_FAILOVER, true);
1191 info.setFailover(false);
1192 }
1193 if (info.getReason() != null) {
1194 intent.putExtra(ConnectivityManager.EXTRA_REASON, info.getReason());
1195 }
1196 if (info.getExtraInfo() != null) {
Robert Greenwalt86e9e552009-07-16 17:21:39 -07001197 intent.putExtra(ConnectivityManager.EXTRA_EXTRA_INFO,
1198 info.getExtraInfo());
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001199 }
Robert Greenwaltd7085fc2010-09-08 15:24:47 -07001200 intent.putExtra(ConnectivityManager.EXTRA_INET_CONDITION, mDefaultInetConditionPublished);
Mike Lockwood0f79b542009-08-14 14:18:49 -04001201 sendStickyBroadcast(intent);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001202 }
1203
1204 /**
1205 * Called when an attempt to fail over to another network has failed.
1206 * @param info the {@link NetworkInfo} for the failed network
1207 */
1208 private void handleConnectionFailure(NetworkInfo info) {
1209 mNetTrackers[info.getType()].setTeardownRequested(false);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001210
Robert Greenwalt42acef32009-08-12 16:08:25 -07001211 String reason = info.getReason();
1212 String extraInfo = info.getExtraInfo();
Robert Greenwalt86e9e552009-07-16 17:21:39 -07001213
Robert Greenwalt572172b2010-10-08 16:35:52 -07001214 String reasonText;
1215 if (reason == null) {
1216 reasonText = ".";
1217 } else {
1218 reasonText = " (" + reason + ").";
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001219 }
Wink Savilleed9c02b2010-12-03 12:01:38 -08001220 loge("Attempt to connect to " + info.getTypeName() + " failed" + reasonText);
Robert Greenwalt42acef32009-08-12 16:08:25 -07001221
1222 Intent intent = new Intent(ConnectivityManager.CONNECTIVITY_ACTION);
1223 intent.putExtra(ConnectivityManager.EXTRA_NETWORK_INFO, info);
1224 if (getActiveNetworkInfo() == null) {
1225 intent.putExtra(ConnectivityManager.EXTRA_NO_CONNECTIVITY, true);
1226 }
1227 if (reason != null) {
1228 intent.putExtra(ConnectivityManager.EXTRA_REASON, reason);
1229 }
1230 if (extraInfo != null) {
1231 intent.putExtra(ConnectivityManager.EXTRA_EXTRA_INFO, extraInfo);
1232 }
1233 if (info.isFailover()) {
1234 intent.putExtra(ConnectivityManager.EXTRA_IS_FAILOVER, true);
1235 info.setFailover(false);
1236 }
Robert Greenwaltda03c4e2010-01-20 19:29:41 -08001237
Robert Greenwaltd55a6b42011-03-25 13:09:25 -07001238 if (mNetConfigs[info.getType()].isDefault()) {
Robert Greenwaltf21ef7d2011-01-11 13:56:33 -08001239 tryFailover(info.getType());
1240 if (mActiveDefaultNetwork != -1) {
1241 NetworkInfo switchTo = mNetTrackers[mActiveDefaultNetwork].getNetworkInfo();
Robert Greenwaltcc4b4012010-01-25 17:54:29 -08001242 intent.putExtra(ConnectivityManager.EXTRA_OTHER_NETWORK_INFO, switchTo);
1243 } else {
Robert Greenwalt029be812010-09-20 18:01:43 -07001244 mDefaultInetConditionPublished = 0;
Robert Greenwaltcc4b4012010-01-25 17:54:29 -08001245 intent.putExtra(ConnectivityManager.EXTRA_NO_CONNECTIVITY, true);
1246 }
Robert Greenwaltda03c4e2010-01-20 19:29:41 -08001247 }
Robert Greenwaltcc4b4012010-01-25 17:54:29 -08001248
Robert Greenwalt029be812010-09-20 18:01:43 -07001249 intent.putExtra(ConnectivityManager.EXTRA_INET_CONDITION, mDefaultInetConditionPublished);
Mike Lockwood0f79b542009-08-14 14:18:49 -04001250 sendStickyBroadcast(intent);
Robert Greenwaltda03c4e2010-01-20 19:29:41 -08001251 /*
1252 * If the failover network is already connected, then immediately send
1253 * out a followup broadcast indicating successful failover
1254 */
Robert Greenwaltf21ef7d2011-01-11 13:56:33 -08001255 if (mActiveDefaultNetwork != -1) {
1256 sendConnectedBroadcast(mNetTrackers[mActiveDefaultNetwork].getNetworkInfo());
Robert Greenwaltda03c4e2010-01-20 19:29:41 -08001257 }
Mike Lockwood0f79b542009-08-14 14:18:49 -04001258 }
1259
1260 private void sendStickyBroadcast(Intent intent) {
1261 synchronized(this) {
Dianne Hackborn1c633fc2009-12-08 19:45:14 -08001262 if (!mSystemReady) {
1263 mInitialBroadcast = new Intent(intent);
Mike Lockwood0f79b542009-08-14 14:18:49 -04001264 }
Dianne Hackborn1c633fc2009-12-08 19:45:14 -08001265 intent.addFlags(Intent.FLAG_RECEIVER_REGISTERED_ONLY_BEFORE_BOOT);
1266 mContext.sendStickyBroadcast(intent);
Mike Lockwood0f79b542009-08-14 14:18:49 -04001267 }
1268 }
1269
1270 void systemReady() {
1271 synchronized(this) {
1272 mSystemReady = true;
Dianne Hackborn1c633fc2009-12-08 19:45:14 -08001273 if (mInitialBroadcast != null) {
1274 mContext.sendStickyBroadcast(mInitialBroadcast);
1275 mInitialBroadcast = null;
Mike Lockwood0f79b542009-08-14 14:18:49 -04001276 }
1277 }
Robert Greenwalt434203a2010-10-11 16:00:27 -07001278 // load the global proxy at startup
1279 mHandler.sendMessage(mHandler.obtainMessage(EVENT_APPLY_GLOBAL_HTTP_PROXY));
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001280 }
1281
1282 private void handleConnect(NetworkInfo info) {
Robert Greenwalt42acef32009-08-12 16:08:25 -07001283 int type = info.getType();
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001284
1285 // snapshot isFailover, because sendConnectedBroadcast() resets it
1286 boolean isFailover = info.isFailover();
Robert Greenwalt42acef32009-08-12 16:08:25 -07001287 NetworkStateTracker thisNet = mNetTrackers[type];
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001288
Robert Greenwalt42acef32009-08-12 16:08:25 -07001289 // if this is a default net and other default is running
1290 // kill the one not preferred
Robert Greenwaltd55a6b42011-03-25 13:09:25 -07001291 if (mNetConfigs[type].isDefault()) {
Robert Greenwalt42acef32009-08-12 16:08:25 -07001292 if (mActiveDefaultNetwork != -1 && mActiveDefaultNetwork != type) {
1293 if ((type != mNetworkPreference &&
Wink Saville975c8482011-04-07 14:23:45 -07001294 mNetConfigs[mActiveDefaultNetwork].priority >
1295 mNetConfigs[type].priority) ||
Robert Greenwalt42acef32009-08-12 16:08:25 -07001296 mNetworkPreference == mActiveDefaultNetwork) {
1297 // don't accept this one
Wink Savilleed9c02b2010-12-03 12:01:38 -08001298 if (DBG) {
1299 log("Not broadcasting CONNECT_ACTION " +
Robert Greenwalt42acef32009-08-12 16:08:25 -07001300 "to torn down network " + info.getTypeName());
Wink Savilleed9c02b2010-12-03 12:01:38 -08001301 }
Robert Greenwalt42acef32009-08-12 16:08:25 -07001302 teardown(thisNet);
1303 return;
1304 } else {
1305 // tear down the other
1306 NetworkStateTracker otherNet =
1307 mNetTrackers[mActiveDefaultNetwork];
Wink Savilleed9c02b2010-12-03 12:01:38 -08001308 if (DBG) {
1309 log("Policy requires " + otherNet.getNetworkInfo().getTypeName() +
Robert Greenwalt42acef32009-08-12 16:08:25 -07001310 " teardown");
Wink Savilleed9c02b2010-12-03 12:01:38 -08001311 }
Robert Greenwalt42acef32009-08-12 16:08:25 -07001312 if (!teardown(otherNet)) {
Wink Savilleed9c02b2010-12-03 12:01:38 -08001313 loge("Network declined teardown request");
Robert Greenwalt42acef32009-08-12 16:08:25 -07001314 return;
1315 }
Robert Greenwalt14f2ef42010-06-15 12:19:37 -07001316 }
1317 }
1318 synchronized (ConnectivityService.this) {
1319 // have a new default network, release the transition wakelock in a second
1320 // if it's held. The second pause is to allow apps to reconnect over the
1321 // new network
1322 if (mNetTransitionWakeLock.isHeld()) {
1323 mHandler.sendMessageDelayed(mHandler.obtainMessage(
Robert Greenwaltf3331232010-09-24 14:32:21 -07001324 EVENT_CLEAR_NET_TRANSITION_WAKELOCK,
Robert Greenwalt14f2ef42010-06-15 12:19:37 -07001325 mNetTransitionWakeLockSerialNumber, 0),
1326 1000);
Robert Greenwalt42acef32009-08-12 16:08:25 -07001327 }
1328 }
1329 mActiveDefaultNetwork = type;
Robert Greenwaltd7085fc2010-09-08 15:24:47 -07001330 // this will cause us to come up initially as unconnected and switching
1331 // to connected after our normal pause unless somebody reports us as reall
1332 // disconnected
1333 mDefaultInetConditionPublished = 0;
1334 mDefaultConnectionSequence++;
1335 mInetConditionChangeInFlight = false;
1336 // Don't do this - if we never sign in stay, grey
1337 //reportNetworkCondition(mActiveDefaultNetwork, 100);
Robert Greenwalt42acef32009-08-12 16:08:25 -07001338 }
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001339 thisNet.setTeardownRequested(false);
Irfan Sheriffd649c122010-06-09 15:39:36 -07001340 updateNetworkSettings(thisNet);
Robert Greenwaltc76b8fa2010-07-23 15:46:26 -07001341 handleConnectivityChange(type);
Robert Greenwalt42acef32009-08-12 16:08:25 -07001342 sendConnectedBroadcast(info);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001343 }
1344
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001345 /**
Robert Greenwaltc76b8fa2010-07-23 15:46:26 -07001346 * After a change in the connectivity state of a network. We're mainly
1347 * concerned with making sure that the list of DNS servers is set up
1348 * according to which networks are connected, and ensuring that the
1349 * right routing table entries exist.
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001350 */
Robert Greenwaltc76b8fa2010-07-23 15:46:26 -07001351 private void handleConnectivityChange(int netType) {
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001352 /*
Robert Greenwalt42acef32009-08-12 16:08:25 -07001353 * If a non-default network is enabled, add the host routes that
Robert Greenwaltc76b8fa2010-07-23 15:46:26 -07001354 * will allow it's DNS servers to be accessed.
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001355 */
Robert Greenwaltc76b8fa2010-07-23 15:46:26 -07001356 handleDnsConfigurationChange(netType);
Robert Greenwalt42acef32009-08-12 16:08:25 -07001357
Robert Greenwaltc76b8fa2010-07-23 15:46:26 -07001358 if (mNetTrackers[netType].getNetworkInfo().isConnected()) {
Robert Greenwaltd55a6b42011-03-25 13:09:25 -07001359 if (mNetConfigs[netType].isDefault()) {
Robert Greenwalt434203a2010-10-11 16:00:27 -07001360 handleApplyDefaultProxy(netType);
Robert Greenwaltc76b8fa2010-07-23 15:46:26 -07001361 addDefaultRoute(mNetTrackers[netType]);
Robert Greenwalt42acef32009-08-12 16:08:25 -07001362 } else {
Robert Greenwaltc76b8fa2010-07-23 15:46:26 -07001363 addPrivateDnsRoutes(mNetTrackers[netType]);
1364 }
1365 } else {
Robert Greenwaltd55a6b42011-03-25 13:09:25 -07001366 if (mNetConfigs[netType].isDefault()) {
Robert Greenwaltc76b8fa2010-07-23 15:46:26 -07001367 removeDefaultRoute(mNetTrackers[netType]);
1368 } else {
1369 removePrivateDnsRoutes(mNetTrackers[netType]);
Robert Greenwalt42acef32009-08-12 16:08:25 -07001370 }
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001371 }
1372 }
1373
Irfan Sheriffd649c122010-06-09 15:39:36 -07001374 private void addPrivateDnsRoutes(NetworkStateTracker nt) {
Irfan Sheriffd649c122010-06-09 15:39:36 -07001375 boolean privateDnsRouteSet = nt.isPrivateDnsRouteSet();
Robert Greenwalt37e65eb2010-08-30 10:56:47 -07001376 LinkProperties p = nt.getLinkProperties();
Robert Greenwalt47f69fe2010-06-15 15:43:39 -07001377 if (p == null) return;
1378 String interfaceName = p.getInterfaceName();
Irfan Sheriffd649c122010-06-09 15:39:36 -07001379
1380 if (DBG) {
Wink Savilleed9c02b2010-12-03 12:01:38 -08001381 log("addPrivateDnsRoutes for " + nt +
Irfan Sheriffd649c122010-06-09 15:39:36 -07001382 "(" + interfaceName + ") - mPrivateDnsRouteSet = " + privateDnsRouteSet);
1383 }
Irfan Sheriffd649c122010-06-09 15:39:36 -07001384 if (interfaceName != null && !privateDnsRouteSet) {
Robert Greenwalt47f69fe2010-06-15 15:43:39 -07001385 Collection<InetAddress> dnsList = p.getDnses();
1386 for (InetAddress dns : dnsList) {
Wink Savilleed9c02b2010-12-03 12:01:38 -08001387 if (DBG) log(" adding " + dns);
Robert Greenwalt585ac0f2010-08-27 09:24:29 -07001388 NetworkUtils.addHostRoute(interfaceName, dns, null);
Irfan Sheriffd649c122010-06-09 15:39:36 -07001389 }
1390 nt.privateDnsRouteSet(true);
1391 }
1392 }
1393
1394 private void removePrivateDnsRoutes(NetworkStateTracker nt) {
1395 // TODO - we should do this explicitly but the NetUtils api doesnt
1396 // support this yet - must remove all. No worse than before
Robert Greenwalt37e65eb2010-08-30 10:56:47 -07001397 LinkProperties p = nt.getLinkProperties();
Robert Greenwalt47f69fe2010-06-15 15:43:39 -07001398 if (p == null) return;
1399 String interfaceName = p.getInterfaceName();
Irfan Sheriffd649c122010-06-09 15:39:36 -07001400 boolean privateDnsRouteSet = nt.isPrivateDnsRouteSet();
1401 if (interfaceName != null && privateDnsRouteSet) {
1402 if (DBG) {
Wink Savilleed9c02b2010-12-03 12:01:38 -08001403 log("removePrivateDnsRoutes for " + nt.getNetworkInfo().getTypeName() +
Irfan Sheriffd649c122010-06-09 15:39:36 -07001404 " (" + interfaceName + ")");
1405 }
1406 NetworkUtils.removeHostRoutes(interfaceName);
1407 nt.privateDnsRouteSet(false);
1408 }
1409 }
1410
Irfan Sheriffd649c122010-06-09 15:39:36 -07001411
1412 private void addDefaultRoute(NetworkStateTracker nt) {
Robert Greenwalt37e65eb2010-08-30 10:56:47 -07001413 LinkProperties p = nt.getLinkProperties();
Robert Greenwalt47f69fe2010-06-15 15:43:39 -07001414 if (p == null) return;
1415 String interfaceName = p.getInterfaceName();
Robert Greenwalt992564e2011-02-09 13:56:06 -08001416 if (TextUtils.isEmpty(interfaceName)) return;
Robert Greenwaltaa70f102011-04-28 14:28:50 -07001417 for (RouteInfo route : p.getRoutes()) {
Irfan Sheriffd649c122010-06-09 15:39:36 -07001418
Robert Greenwaltaa70f102011-04-28 14:28:50 -07001419 //TODO - handle non-default routes
1420 if (route.isDefaultRoute()) {
1421 InetAddress gateway = route.getGateway();
1422 if (NetworkUtils.addHostRoute(interfaceName, gateway, null) &&
1423 NetworkUtils.addDefaultRoute(interfaceName, gateway)) {
1424 if (DBG) {
1425 NetworkInfo networkInfo = nt.getNetworkInfo();
1426 log("addDefaultRoute for " + networkInfo.getTypeName() +
1427 " (" + interfaceName + "), GatewayAddr=" +
1428 gateway.getHostAddress());
1429 }
Robert Greenwaltedcb4f92011-03-22 18:47:42 -07001430 }
Irfan Sheriffd649c122010-06-09 15:39:36 -07001431 }
Irfan Sheriffd649c122010-06-09 15:39:36 -07001432 }
1433 }
1434
1435
1436 public void removeDefaultRoute(NetworkStateTracker nt) {
Robert Greenwalt37e65eb2010-08-30 10:56:47 -07001437 LinkProperties p = nt.getLinkProperties();
Robert Greenwalt47f69fe2010-06-15 15:43:39 -07001438 if (p == null) return;
1439 String interfaceName = p.getInterfaceName();
Irfan Sheriffd649c122010-06-09 15:39:36 -07001440
Robert Greenwaltc76b8fa2010-07-23 15:46:26 -07001441 if (interfaceName != null) {
Robert Greenwaltedcb4f92011-03-22 18:47:42 -07001442 if (NetworkUtils.removeDefaultRoute(interfaceName) >= 0) {
1443 if (DBG) {
1444 NetworkInfo networkInfo = nt.getNetworkInfo();
1445 log("removeDefaultRoute for " + networkInfo.getTypeName() + " (" +
1446 interfaceName + ")");
1447 }
Irfan Sheriffd649c122010-06-09 15:39:36 -07001448 }
Irfan Sheriffd649c122010-06-09 15:39:36 -07001449 }
1450 }
1451
1452 /**
1453 * Reads the network specific TCP buffer sizes from SystemProperties
1454 * net.tcp.buffersize.[default|wifi|umts|edge|gprs] and set them for system
1455 * wide use
1456 */
1457 public void updateNetworkSettings(NetworkStateTracker nt) {
1458 String key = nt.getTcpBufferSizesPropName();
1459 String bufferSizes = SystemProperties.get(key);
1460
1461 if (bufferSizes.length() == 0) {
Wink Savilleed9c02b2010-12-03 12:01:38 -08001462 loge(key + " not found in system properties. Using defaults");
Irfan Sheriffd649c122010-06-09 15:39:36 -07001463
1464 // Setting to default values so we won't be stuck to previous values
1465 key = "net.tcp.buffersize.default";
1466 bufferSizes = SystemProperties.get(key);
1467 }
1468
1469 // Set values in kernel
1470 if (bufferSizes.length() != 0) {
1471 if (DBG) {
Wink Savilleed9c02b2010-12-03 12:01:38 -08001472 log("Setting TCP values: [" + bufferSizes
Irfan Sheriffd649c122010-06-09 15:39:36 -07001473 + "] which comes from [" + key + "]");
1474 }
1475 setBufferSize(bufferSizes);
1476 }
1477 }
1478
1479 /**
1480 * Writes TCP buffer sizes to /sys/kernel/ipv4/tcp_[r/w]mem_[min/def/max]
1481 * which maps to /proc/sys/net/ipv4/tcp_rmem and tcpwmem
1482 *
1483 * @param bufferSizes in the format of "readMin, readInitial, readMax,
1484 * writeMin, writeInitial, writeMax"
1485 */
1486 private void setBufferSize(String bufferSizes) {
1487 try {
1488 String[] values = bufferSizes.split(",");
1489
1490 if (values.length == 6) {
1491 final String prefix = "/sys/kernel/ipv4/tcp_";
1492 stringToFile(prefix + "rmem_min", values[0]);
1493 stringToFile(prefix + "rmem_def", values[1]);
1494 stringToFile(prefix + "rmem_max", values[2]);
1495 stringToFile(prefix + "wmem_min", values[3]);
1496 stringToFile(prefix + "wmem_def", values[4]);
1497 stringToFile(prefix + "wmem_max", values[5]);
1498 } else {
Wink Savilleed9c02b2010-12-03 12:01:38 -08001499 loge("Invalid buffersize string: " + bufferSizes);
Irfan Sheriffd649c122010-06-09 15:39:36 -07001500 }
1501 } catch (IOException e) {
Wink Savilleed9c02b2010-12-03 12:01:38 -08001502 loge("Can't set tcp buffer sizes:" + e);
Irfan Sheriffd649c122010-06-09 15:39:36 -07001503 }
1504 }
1505
1506 /**
1507 * Writes string to file. Basically same as "echo -n $string > $filename"
1508 *
1509 * @param filename
1510 * @param string
1511 * @throws IOException
1512 */
1513 private void stringToFile(String filename, String string) throws IOException {
1514 FileWriter out = new FileWriter(filename);
1515 try {
1516 out.write(string);
1517 } finally {
1518 out.close();
1519 }
1520 }
1521
1522
Robert Greenwalt42acef32009-08-12 16:08:25 -07001523 /**
1524 * Adjust the per-process dns entries (net.dns<x>.<pid>) based
1525 * on the highest priority active net which this process requested.
1526 * If there aren't any, clear it out
1527 */
1528 private void reassessPidDns(int myPid, boolean doBump)
1529 {
Wink Savilleed9c02b2010-12-03 12:01:38 -08001530 if (DBG) log("reassessPidDns for pid " + myPid);
Robert Greenwalt42acef32009-08-12 16:08:25 -07001531 for(int i : mPriorityList) {
Robert Greenwaltd55a6b42011-03-25 13:09:25 -07001532 if (mNetConfigs[i].isDefault()) {
Robert Greenwalt42acef32009-08-12 16:08:25 -07001533 continue;
1534 }
1535 NetworkStateTracker nt = mNetTrackers[i];
Robert Greenwalt86e9e552009-07-16 17:21:39 -07001536 if (nt.getNetworkInfo().isConnected() &&
1537 !nt.isTeardownRequested()) {
Robert Greenwalt37e65eb2010-08-30 10:56:47 -07001538 LinkProperties p = nt.getLinkProperties();
Robert Greenwalt47f69fe2010-06-15 15:43:39 -07001539 if (p == null) continue;
Robert Greenwalt42acef32009-08-12 16:08:25 -07001540 List pids = mNetRequestersPids[i];
1541 for (int j=0; j<pids.size(); j++) {
1542 Integer pid = (Integer)pids.get(j);
1543 if (pid.intValue() == myPid) {
Robert Greenwalt47f69fe2010-06-15 15:43:39 -07001544 Collection<InetAddress> dnses = p.getDnses();
1545 writePidDns(dnses, myPid);
Robert Greenwalt42acef32009-08-12 16:08:25 -07001546 if (doBump) {
1547 bumpDns();
1548 }
1549 return;
1550 }
1551 }
1552 }
1553 }
1554 // nothing found - delete
1555 for (int i = 1; ; i++) {
1556 String prop = "net.dns" + i + "." + myPid;
1557 if (SystemProperties.get(prop).length() == 0) {
1558 if (doBump) {
1559 bumpDns();
1560 }
1561 return;
1562 }
1563 SystemProperties.set(prop, "");
1564 }
1565 }
1566
Robert Greenwalt10398722010-12-17 15:20:36 -08001567 // return true if results in a change
1568 private boolean writePidDns(Collection <InetAddress> dnses, int pid) {
Robert Greenwalt42acef32009-08-12 16:08:25 -07001569 int j = 1;
Robert Greenwalt10398722010-12-17 15:20:36 -08001570 boolean changed = false;
Robert Greenwalt47f69fe2010-06-15 15:43:39 -07001571 for (InetAddress dns : dnses) {
Robert Greenwalt10398722010-12-17 15:20:36 -08001572 String dnsString = dns.getHostAddress();
1573 if (changed || !dnsString.equals(SystemProperties.get("net.dns" + j + "." + pid))) {
1574 changed = true;
1575 SystemProperties.set("net.dns" + j++ + "." + pid, dns.getHostAddress());
1576 }
Robert Greenwalt42acef32009-08-12 16:08:25 -07001577 }
Robert Greenwalt10398722010-12-17 15:20:36 -08001578 return changed;
Robert Greenwalt42acef32009-08-12 16:08:25 -07001579 }
1580
1581 private void bumpDns() {
1582 /*
1583 * Bump the property that tells the name resolver library to reread
1584 * the DNS server list from the properties.
1585 */
1586 String propVal = SystemProperties.get("net.dnschange");
1587 int n = 0;
1588 if (propVal.length() != 0) {
1589 try {
1590 n = Integer.parseInt(propVal);
1591 } catch (NumberFormatException e) {}
1592 }
1593 SystemProperties.set("net.dnschange", "" + (n+1));
Robert Greenwalt03595d02010-11-02 14:08:23 -07001594 /*
1595 * Tell the VMs to toss their DNS caches
1596 */
1597 Intent intent = new Intent(Intent.ACTION_CLEAR_DNS_CACHE);
1598 intent.addFlags(Intent.FLAG_RECEIVER_REPLACE_PENDING);
Stan Chesnutt3d1db862011-01-05 17:14:03 -08001599 /*
1600 * Connectivity events can happen before boot has completed ...
1601 */
1602 intent.addFlags(Intent.FLAG_RECEIVER_REGISTERED_ONLY_BEFORE_BOOT);
Robert Greenwalt03595d02010-11-02 14:08:23 -07001603 mContext.sendBroadcast(intent);
Robert Greenwalt42acef32009-08-12 16:08:25 -07001604 }
1605
Robert Greenwaltc76b8fa2010-07-23 15:46:26 -07001606 private void handleDnsConfigurationChange(int netType) {
Robert Greenwalt42acef32009-08-12 16:08:25 -07001607 // add default net's dns entries
Robert Greenwaltc76b8fa2010-07-23 15:46:26 -07001608 NetworkStateTracker nt = mNetTrackers[netType];
1609 if (nt != null && nt.getNetworkInfo().isConnected() && !nt.isTeardownRequested()) {
Robert Greenwalt37e65eb2010-08-30 10:56:47 -07001610 LinkProperties p = nt.getLinkProperties();
Robert Greenwaltc76b8fa2010-07-23 15:46:26 -07001611 if (p == null) return;
1612 Collection<InetAddress> dnses = p.getDnses();
Robert Greenwalt10398722010-12-17 15:20:36 -08001613 boolean changed = false;
Robert Greenwaltd55a6b42011-03-25 13:09:25 -07001614 if (mNetConfigs[netType].isDefault()) {
Robert Greenwaltc76b8fa2010-07-23 15:46:26 -07001615 int j = 1;
Robert Greenwalte90aa5e2010-09-01 11:34:05 -07001616 if (dnses.size() == 0 && mDefaultDns != null) {
Robert Greenwalt10398722010-12-17 15:20:36 -08001617 String dnsString = mDefaultDns.getHostAddress();
1618 if (!dnsString.equals(SystemProperties.get("net.dns1"))) {
1619 if (DBG) {
1620 log("no dns provided - using " + dnsString);
1621 }
1622 changed = true;
1623 SystemProperties.set("net.dns1", dnsString);
Robert Greenwalt42acef32009-08-12 16:08:25 -07001624 }
Robert Greenwalte90aa5e2010-09-01 11:34:05 -07001625 j++;
1626 } else {
1627 for (InetAddress dns : dnses) {
Robert Greenwalt10398722010-12-17 15:20:36 -08001628 String dnsString = dns.getHostAddress();
1629 if (!changed && dnsString.equals(SystemProperties.get("net.dns" + j))) {
1630 j++;
1631 continue;
1632 }
Robert Greenwalte90aa5e2010-09-01 11:34:05 -07001633 if (DBG) {
Wink Savilleed9c02b2010-12-03 12:01:38 -08001634 log("adding dns " + dns + " for " +
Robert Greenwalte90aa5e2010-09-01 11:34:05 -07001635 nt.getNetworkInfo().getTypeName());
1636 }
Robert Greenwalt10398722010-12-17 15:20:36 -08001637 changed = true;
1638 SystemProperties.set("net.dns" + j++, dnsString);
Robert Greenwalte90aa5e2010-09-01 11:34:05 -07001639 }
Robert Greenwaltc76b8fa2010-07-23 15:46:26 -07001640 }
1641 for (int k=j ; k<mNumDnsEntries; k++) {
Robert Greenwalt10398722010-12-17 15:20:36 -08001642 if (changed || !TextUtils.isEmpty(SystemProperties.get("net.dns" + k))) {
1643 if (DBG) log("erasing net.dns" + k);
1644 changed = true;
1645 SystemProperties.set("net.dns" + k, "");
1646 }
Robert Greenwaltc76b8fa2010-07-23 15:46:26 -07001647 }
1648 mNumDnsEntries = j;
1649 } else {
1650 // set per-pid dns for attached secondary nets
1651 List pids = mNetRequestersPids[netType];
1652 for (int y=0; y< pids.size(); y++) {
1653 Integer pid = (Integer)pids.get(y);
Robert Greenwalt10398722010-12-17 15:20:36 -08001654 changed = writePidDns(dnses, pid.intValue());
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001655 }
1656 }
Robert Greenwalt10398722010-12-17 15:20:36 -08001657 if (changed) bumpDns();
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001658 }
Robert Greenwalt42acef32009-08-12 16:08:25 -07001659 }
1660
1661 private int getRestoreDefaultNetworkDelay() {
1662 String restoreDefaultNetworkDelayStr = SystemProperties.get(
1663 NETWORK_RESTORE_DELAY_PROP_NAME);
1664 if(restoreDefaultNetworkDelayStr != null &&
1665 restoreDefaultNetworkDelayStr.length() != 0) {
1666 try {
1667 return Integer.valueOf(restoreDefaultNetworkDelayStr);
1668 } catch (NumberFormatException e) {
1669 }
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001670 }
Robert Greenwalt42acef32009-08-12 16:08:25 -07001671 return RESTORE_DEFAULT_NETWORK_DELAY;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001672 }
1673
1674 @Override
1675 protected void dump(FileDescriptor fd, PrintWriter pw, String[] args) {
Robert Greenwalt86e9e552009-07-16 17:21:39 -07001676 if (mContext.checkCallingOrSelfPermission(
1677 android.Manifest.permission.DUMP)
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001678 != PackageManager.PERMISSION_GRANTED) {
Robert Greenwalt86e9e552009-07-16 17:21:39 -07001679 pw.println("Permission Denial: can't dump ConnectivityService " +
1680 "from from pid=" + Binder.getCallingPid() + ", uid=" +
1681 Binder.getCallingUid());
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001682 return;
1683 }
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001684 pw.println();
1685 for (NetworkStateTracker nst : mNetTrackers) {
Robert Greenwaltb9285352009-12-21 18:24:07 -08001686 if (nst != null) {
1687 if (nst.getNetworkInfo().isConnected()) {
1688 pw.println("Active network: " + nst.getNetworkInfo().
1689 getTypeName());
1690 }
1691 pw.println(nst.getNetworkInfo());
1692 pw.println(nst);
1693 pw.println();
Robert Greenwalt42acef32009-08-12 16:08:25 -07001694 }
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001695 }
Robert Greenwaltb9285352009-12-21 18:24:07 -08001696
1697 pw.println("Network Requester Pids:");
1698 for (int net : mPriorityList) {
1699 String pidString = net + ": ";
1700 for (Object pid : mNetRequestersPids[net]) {
1701 pidString = pidString + pid.toString() + ", ";
1702 }
1703 pw.println(pidString);
1704 }
1705 pw.println();
1706
1707 pw.println("FeatureUsers:");
1708 for (Object requester : mFeatureUsers) {
1709 pw.println(requester.toString());
1710 }
1711 pw.println();
Robert Greenwalt2a091d72010-02-11 18:18:40 -08001712
Robert Greenwalt14f2ef42010-06-15 12:19:37 -07001713 synchronized (this) {
1714 pw.println("NetworkTranstionWakeLock is currently " +
1715 (mNetTransitionWakeLock.isHeld() ? "" : "not ") + "held.");
1716 pw.println("It was last requested for "+mNetTransitionWakeLockCausedBy);
1717 }
1718 pw.println();
1719
Robert Greenwalt2a091d72010-02-11 18:18:40 -08001720 mTethering.dump(fd, pw, args);
Robert Greenwalt4e8dfef2010-09-20 14:35:25 -07001721
1722 if (mInetLog != null) {
1723 pw.println();
1724 pw.println("Inet condition reports:");
1725 for(int i = 0; i < mInetLog.size(); i++) {
1726 pw.println(mInetLog.get(i));
1727 }
1728 }
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001729 }
1730
Robert Greenwalt42acef32009-08-12 16:08:25 -07001731 // must be stateless - things change under us.
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001732 private class MyHandler extends Handler {
Wink Savillebb08caf2010-09-02 19:23:52 -07001733 public MyHandler(Looper looper) {
1734 super(looper);
1735 }
1736
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001737 @Override
1738 public void handleMessage(Message msg) {
1739 NetworkInfo info;
1740 switch (msg.what) {
1741 case NetworkStateTracker.EVENT_STATE_CHANGED:
1742 info = (NetworkInfo) msg.obj;
Robert Greenwalt511288a2009-12-07 11:33:18 -08001743 int type = info.getType();
1744 NetworkInfo.State state = info.getState();
Robert Greenwalt511288a2009-12-07 11:33:18 -08001745
Wink Savilleed9c02b2010-12-03 12:01:38 -08001746 if (DBG) log("ConnectivityChange for " +
Robert Greenwalt86e9e552009-07-16 17:21:39 -07001747 info.getTypeName() + ": " +
Robert Greenwalt511288a2009-12-07 11:33:18 -08001748 state + "/" + info.getDetailedState());
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001749
1750 // Connectivity state changed:
1751 // [31-13] Reserved for future use
Robert Greenwalt86e9e552009-07-16 17:21:39 -07001752 // [12-9] Network subtype (for mobile network, as defined
1753 // by TelephonyManager)
1754 // [8-3] Detailed state ordinal (as defined by
1755 // NetworkInfo.DetailedState)
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001756 // [2-0] Network type (as defined by ConnectivityManager)
1757 int eventLogParam = (info.getType() & 0x7) |
1758 ((info.getDetailedState().ordinal() & 0x3f) << 3) |
1759 (info.getSubtype() << 9);
Doug Zongkerab5c49c2009-12-04 10:31:43 -08001760 EventLog.writeEvent(EventLogTags.CONNECTIVITY_STATE_CHANGED,
Robert Greenwalt86e9e552009-07-16 17:21:39 -07001761 eventLogParam);
1762
1763 if (info.getDetailedState() ==
1764 NetworkInfo.DetailedState.FAILED) {
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001765 handleConnectionFailure(info);
Robert Greenwalt511288a2009-12-07 11:33:18 -08001766 } else if (state == NetworkInfo.State.DISCONNECTED) {
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001767 handleDisconnect(info);
Robert Greenwalt511288a2009-12-07 11:33:18 -08001768 } else if (state == NetworkInfo.State.SUSPENDED) {
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001769 // TODO: need to think this over.
Robert Greenwalt86e9e552009-07-16 17:21:39 -07001770 // the logic here is, handle SUSPENDED the same as
1771 // DISCONNECTED. The only difference being we are
1772 // broadcasting an intent with NetworkInfo that's
1773 // suspended. This allows the applications an
1774 // opportunity to handle DISCONNECTED and SUSPENDED
1775 // differently, or not.
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001776 handleDisconnect(info);
Robert Greenwalt511288a2009-12-07 11:33:18 -08001777 } else if (state == NetworkInfo.State.CONNECTED) {
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001778 handleConnect(info);
1779 }
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001780 break;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001781 case NetworkStateTracker.EVENT_CONFIGURATION_CHANGED:
Robert Greenwaltc76b8fa2010-07-23 15:46:26 -07001782 info = (NetworkInfo) msg.obj;
Robert Greenwaltd55a6b42011-03-25 13:09:25 -07001783 handleConnectivityChange(info.getType());
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001784 break;
Robert Greenwaltf3331232010-09-24 14:32:21 -07001785 case EVENT_CLEAR_NET_TRANSITION_WAKELOCK:
Robert Greenwalt14f2ef42010-06-15 12:19:37 -07001786 String causedBy = null;
1787 synchronized (ConnectivityService.this) {
1788 if (msg.arg1 == mNetTransitionWakeLockSerialNumber &&
1789 mNetTransitionWakeLock.isHeld()) {
1790 mNetTransitionWakeLock.release();
1791 causedBy = mNetTransitionWakeLockCausedBy;
1792 }
1793 }
1794 if (causedBy != null) {
Wink Savilleed9c02b2010-12-03 12:01:38 -08001795 log("NetTransition Wakelock for " + causedBy + " released by timeout");
Robert Greenwalt14f2ef42010-06-15 12:19:37 -07001796 }
Robert Greenwalt057d5e92010-09-09 14:05:10 -07001797 break;
Robert Greenwalt8dcc28b2010-09-23 10:05:56 -07001798 case EVENT_RESTORE_DEFAULT_NETWORK:
Robert Greenwalt9c75d4a2009-09-27 17:27:04 -07001799 FeatureUser u = (FeatureUser)msg.obj;
1800 u.expire();
Robert Greenwaltd7085fc2010-09-08 15:24:47 -07001801 break;
Robert Greenwalt8dcc28b2010-09-23 10:05:56 -07001802 case EVENT_INET_CONDITION_CHANGE:
1803 {
1804 int netType = msg.arg1;
1805 int condition = msg.arg2;
1806 handleInetConditionChange(netType, condition);
Robert Greenwalt14f2ef42010-06-15 12:19:37 -07001807 break;
Robert Greenwalt8dcc28b2010-09-23 10:05:56 -07001808 }
1809 case EVENT_INET_CONDITION_HOLD_END:
1810 {
1811 int netType = msg.arg1;
1812 int sequence = msg.arg2;
1813 handleInetConditionHoldEnd(netType, sequence);
Robert Greenwaltd7085fc2010-09-08 15:24:47 -07001814 break;
Robert Greenwalt8dcc28b2010-09-23 10:05:56 -07001815 }
1816 case EVENT_SET_NETWORK_PREFERENCE:
1817 {
1818 int preference = msg.arg1;
1819 handleSetNetworkPreference(preference);
1820 break;
1821 }
1822 case EVENT_SET_BACKGROUND_DATA:
1823 {
1824 boolean enabled = (msg.arg1 == ENABLED);
1825 handleSetBackgroundData(enabled);
1826 break;
1827 }
1828 case EVENT_SET_MOBILE_DATA:
1829 {
1830 boolean enabled = (msg.arg1 == ENABLED);
1831 handleSetMobileData(enabled);
1832 break;
1833 }
Robert Greenwalt434203a2010-10-11 16:00:27 -07001834 case EVENT_APPLY_GLOBAL_HTTP_PROXY:
1835 {
1836 handleDeprecatedGlobalHttpProxy();
Robert Greenwaltd55a6b42011-03-25 13:09:25 -07001837 break;
1838 }
1839 case EVENT_SET_DEPENDENCY_MET:
1840 {
1841 boolean met = (msg.arg1 == ENABLED);
1842 handleSetDependencyMet(msg.arg2, met);
1843 break;
Robert Greenwalt434203a2010-10-11 16:00:27 -07001844 }
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001845 }
1846 }
1847 }
Robert Greenwaltd0e18ff2010-01-26 11:40:34 -08001848
1849 // javadoc from interface
Robert Greenwalt5a735062010-03-02 17:25:02 -08001850 public int tether(String iface) {
Robert Greenwaltd0e18ff2010-01-26 11:40:34 -08001851 enforceTetherChangePermission();
Robert Greenwalt5a735062010-03-02 17:25:02 -08001852
1853 if (isTetheringSupported()) {
1854 return mTethering.tether(iface);
1855 } else {
1856 return ConnectivityManager.TETHER_ERROR_UNSUPPORTED;
1857 }
Robert Greenwaltd0e18ff2010-01-26 11:40:34 -08001858 }
1859
1860 // javadoc from interface
Robert Greenwalt5a735062010-03-02 17:25:02 -08001861 public int untether(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.untether(iface);
1866 } else {
1867 return ConnectivityManager.TETHER_ERROR_UNSUPPORTED;
1868 }
1869 }
1870
1871 // javadoc from interface
1872 public int getLastTetherError(String iface) {
1873 enforceTetherAccessPermission();
1874
1875 if (isTetheringSupported()) {
1876 return mTethering.getLastTetherError(iface);
1877 } else {
1878 return ConnectivityManager.TETHER_ERROR_UNSUPPORTED;
1879 }
Robert Greenwalt2a091d72010-02-11 18:18:40 -08001880 }
1881
1882 // TODO - proper iface API for selection by property, inspection, etc
1883 public String[] getTetherableUsbRegexs() {
1884 enforceTetherAccessPermission();
1885 if (isTetheringSupported()) {
1886 return mTethering.getTetherableUsbRegexs();
1887 } else {
1888 return new String[0];
1889 }
1890 }
1891
1892 public String[] getTetherableWifiRegexs() {
1893 enforceTetherAccessPermission();
1894 if (isTetheringSupported()) {
1895 return mTethering.getTetherableWifiRegexs();
1896 } else {
1897 return new String[0];
1898 }
Robert Greenwaltd0e18ff2010-01-26 11:40:34 -08001899 }
1900
Danica Chang6fdd0c62010-08-11 14:54:43 -07001901 public String[] getTetherableBluetoothRegexs() {
1902 enforceTetherAccessPermission();
1903 if (isTetheringSupported()) {
1904 return mTethering.getTetherableBluetoothRegexs();
1905 } else {
1906 return new String[0];
1907 }
1908 }
1909
Robert Greenwaltd0e18ff2010-01-26 11:40:34 -08001910 // TODO - move iface listing, queries, etc to new module
1911 // javadoc from interface
1912 public String[] getTetherableIfaces() {
Robert Greenwalt2a091d72010-02-11 18:18:40 -08001913 enforceTetherAccessPermission();
Robert Greenwaltd0e18ff2010-01-26 11:40:34 -08001914 return mTethering.getTetherableIfaces();
1915 }
1916
1917 public String[] getTetheredIfaces() {
Robert Greenwalt2a091d72010-02-11 18:18:40 -08001918 enforceTetherAccessPermission();
Robert Greenwaltd0e18ff2010-01-26 11:40:34 -08001919 return mTethering.getTetheredIfaces();
1920 }
Robert Greenwalt2a091d72010-02-11 18:18:40 -08001921
Robert Greenwalt5a735062010-03-02 17:25:02 -08001922 public String[] getTetheringErroredIfaces() {
1923 enforceTetherAccessPermission();
1924 return mTethering.getErroredIfaces();
1925 }
1926
Robert Greenwalt2a091d72010-02-11 18:18:40 -08001927 // if ro.tether.denied = true we default to no tethering
1928 // gservices could set the secure setting to 1 though to enable it on a build where it
1929 // had previously been turned off.
1930 public boolean isTetheringSupported() {
1931 enforceTetherAccessPermission();
1932 int defaultVal = (SystemProperties.get("ro.tether.denied").equals("true") ? 0 : 1);
Robert Greenwaltc9d5fb72010-02-25 12:29:30 -08001933 boolean tetherEnabledInSettings = (Settings.Secure.getInt(mContext.getContentResolver(),
1934 Settings.Secure.TETHER_SUPPORTED, defaultVal) != 0);
1935 return tetherEnabledInSettings && mTetheringConfigValid;
Robert Greenwalt2a091d72010-02-11 18:18:40 -08001936 }
Robert Greenwalt14f2ef42010-06-15 12:19:37 -07001937
1938 // An API NetworkStateTrackers can call when they lose their network.
1939 // This will automatically be cleared after X seconds or a network becomes CONNECTED,
1940 // whichever happens first. The timer is started by the first caller and not
1941 // restarted by subsequent callers.
1942 public void requestNetworkTransitionWakelock(String forWhom) {
1943 enforceConnectivityInternalPermission();
1944 synchronized (this) {
1945 if (mNetTransitionWakeLock.isHeld()) return;
1946 mNetTransitionWakeLockSerialNumber++;
1947 mNetTransitionWakeLock.acquire();
1948 mNetTransitionWakeLockCausedBy = forWhom;
1949 }
1950 mHandler.sendMessageDelayed(mHandler.obtainMessage(
Robert Greenwaltf3331232010-09-24 14:32:21 -07001951 EVENT_CLEAR_NET_TRANSITION_WAKELOCK,
Robert Greenwalt14f2ef42010-06-15 12:19:37 -07001952 mNetTransitionWakeLockSerialNumber, 0),
1953 mNetTransitionWakeLockTimeout);
1954 return;
1955 }
Robert Greenwaltca4306c2010-09-09 13:15:32 -07001956
Robert Greenwaltd7085fc2010-09-08 15:24:47 -07001957 // 100 percent is full good, 0 is full bad.
1958 public void reportInetCondition(int networkType, int percentage) {
Wink Savilleed9c02b2010-12-03 12:01:38 -08001959 if (DBG) log("reportNetworkCondition(" + networkType + ", " + percentage + ")");
Robert Greenwaltd7085fc2010-09-08 15:24:47 -07001960 mContext.enforceCallingOrSelfPermission(
1961 android.Manifest.permission.STATUS_BAR,
1962 "ConnectivityService");
1963
Robert Greenwalt4e8dfef2010-09-20 14:35:25 -07001964 if (DBG) {
1965 int pid = getCallingPid();
1966 int uid = getCallingUid();
1967 String s = pid + "(" + uid + ") reports inet is " +
1968 (percentage > 50 ? "connected" : "disconnected") + " (" + percentage + ") on " +
1969 "network Type " + networkType + " at " + GregorianCalendar.getInstance().getTime();
1970 mInetLog.add(s);
1971 while(mInetLog.size() > INET_CONDITION_LOG_MAX_SIZE) {
1972 mInetLog.remove(0);
1973 }
1974 }
Robert Greenwaltd7085fc2010-09-08 15:24:47 -07001975 mHandler.sendMessage(mHandler.obtainMessage(
Robert Greenwalt8dcc28b2010-09-23 10:05:56 -07001976 EVENT_INET_CONDITION_CHANGE, networkType, percentage));
1977 }
1978
1979 private void handleInetConditionChange(int netType, int condition) {
1980 if (DBG) {
Wink Savilleed9c02b2010-12-03 12:01:38 -08001981 log("Inet connectivity change, net=" +
Robert Greenwalt8dcc28b2010-09-23 10:05:56 -07001982 netType + ", condition=" + condition +
1983 ",mActiveDefaultNetwork=" + mActiveDefaultNetwork);
1984 }
1985 if (mActiveDefaultNetwork == -1) {
Wink Savilleed9c02b2010-12-03 12:01:38 -08001986 if (DBG) log("no active default network - aborting");
Robert Greenwalt8dcc28b2010-09-23 10:05:56 -07001987 return;
1988 }
1989 if (mActiveDefaultNetwork != netType) {
Wink Savilleed9c02b2010-12-03 12:01:38 -08001990 if (DBG) log("given net not default - aborting");
Robert Greenwalt8dcc28b2010-09-23 10:05:56 -07001991 return;
1992 }
1993 mDefaultInetCondition = condition;
1994 int delay;
1995 if (mInetConditionChangeInFlight == false) {
Wink Savilleed9c02b2010-12-03 12:01:38 -08001996 if (DBG) log("starting a change hold");
Robert Greenwalt8dcc28b2010-09-23 10:05:56 -07001997 // setup a new hold to debounce this
1998 if (mDefaultInetCondition > 50) {
1999 delay = Settings.Secure.getInt(mContext.getContentResolver(),
2000 Settings.Secure.INET_CONDITION_DEBOUNCE_UP_DELAY, 500);
2001 } else {
2002 delay = Settings.Secure.getInt(mContext.getContentResolver(),
2003 Settings.Secure.INET_CONDITION_DEBOUNCE_DOWN_DELAY, 3000);
2004 }
2005 mInetConditionChangeInFlight = true;
2006 mHandler.sendMessageDelayed(mHandler.obtainMessage(EVENT_INET_CONDITION_HOLD_END,
2007 mActiveDefaultNetwork, mDefaultConnectionSequence), delay);
2008 } else {
2009 // we've set the new condition, when this hold ends that will get
2010 // picked up
Wink Savilleed9c02b2010-12-03 12:01:38 -08002011 if (DBG) log("currently in hold - not setting new end evt");
Robert Greenwalt8dcc28b2010-09-23 10:05:56 -07002012 }
2013 }
2014
2015 private void handleInetConditionHoldEnd(int netType, int sequence) {
2016 if (DBG) {
Wink Savilleed9c02b2010-12-03 12:01:38 -08002017 log("Inet hold end, net=" + netType +
Robert Greenwalt8dcc28b2010-09-23 10:05:56 -07002018 ", condition =" + mDefaultInetCondition +
2019 ", published condition =" + mDefaultInetConditionPublished);
2020 }
2021 mInetConditionChangeInFlight = false;
2022
2023 if (mActiveDefaultNetwork == -1) {
Wink Savilleed9c02b2010-12-03 12:01:38 -08002024 if (DBG) log("no active default network - aborting");
Robert Greenwalt8dcc28b2010-09-23 10:05:56 -07002025 return;
2026 }
2027 if (mDefaultConnectionSequence != sequence) {
Wink Savilleed9c02b2010-12-03 12:01:38 -08002028 if (DBG) log("event hold for obsolete network - aborting");
Robert Greenwalt8dcc28b2010-09-23 10:05:56 -07002029 return;
2030 }
2031 if (mDefaultInetConditionPublished == mDefaultInetCondition) {
Wink Savilleed9c02b2010-12-03 12:01:38 -08002032 if (DBG) log("no change in condition - aborting");
Robert Greenwalt8dcc28b2010-09-23 10:05:56 -07002033 return;
2034 }
2035 NetworkInfo networkInfo = mNetTrackers[mActiveDefaultNetwork].getNetworkInfo();
2036 if (networkInfo.isConnected() == false) {
Wink Savilleed9c02b2010-12-03 12:01:38 -08002037 if (DBG) log("default network not connected - aborting");
Robert Greenwalt8dcc28b2010-09-23 10:05:56 -07002038 return;
2039 }
2040 mDefaultInetConditionPublished = mDefaultInetCondition;
2041 sendInetConditionBroadcast(networkInfo);
2042 return;
Robert Greenwaltd7085fc2010-09-08 15:24:47 -07002043 }
Robert Greenwalt434203a2010-10-11 16:00:27 -07002044
2045 public synchronized ProxyProperties getProxy() {
2046 if (mGlobalProxy != null) return mGlobalProxy;
2047 if (mDefaultProxy != null) return mDefaultProxy;
2048 return null;
2049 }
2050
2051 public void setGlobalProxy(ProxyProperties proxyProperties) {
2052 enforceChangePermission();
2053 synchronized (mGlobalProxyLock) {
2054 if (proxyProperties == mGlobalProxy) return;
2055 if (proxyProperties != null && proxyProperties.equals(mGlobalProxy)) return;
2056 if (mGlobalProxy != null && mGlobalProxy.equals(proxyProperties)) return;
2057
2058 String host = "";
2059 int port = 0;
2060 String exclList = "";
2061 if (proxyProperties != null && !TextUtils.isEmpty(proxyProperties.getHost())) {
2062 mGlobalProxy = new ProxyProperties(proxyProperties);
2063 host = mGlobalProxy.getHost();
2064 port = mGlobalProxy.getPort();
2065 exclList = mGlobalProxy.getExclusionList();
2066 } else {
2067 mGlobalProxy = null;
2068 }
2069 ContentResolver res = mContext.getContentResolver();
2070 Settings.Secure.putString(res, Settings.Secure.GLOBAL_HTTP_PROXY_HOST, host);
2071 Settings.Secure.putInt(res, Settings.Secure.GLOBAL_HTTP_PROXY_PORT, port);
Robert Greenwaltb7090d62010-12-02 11:31:00 -08002072 Settings.Secure.putString(res, Settings.Secure.GLOBAL_HTTP_PROXY_EXCLUSION_LIST,
Robert Greenwalt434203a2010-10-11 16:00:27 -07002073 exclList);
2074 }
2075
2076 if (mGlobalProxy == null) {
2077 proxyProperties = mDefaultProxy;
2078 }
2079 sendProxyBroadcast(proxyProperties);
2080 }
2081
Robert Greenwaltb7090d62010-12-02 11:31:00 -08002082 private void loadGlobalProxy() {
2083 ContentResolver res = mContext.getContentResolver();
2084 String host = Settings.Secure.getString(res, Settings.Secure.GLOBAL_HTTP_PROXY_HOST);
2085 int port = Settings.Secure.getInt(res, Settings.Secure.GLOBAL_HTTP_PROXY_PORT, 0);
2086 String exclList = Settings.Secure.getString(res,
2087 Settings.Secure.GLOBAL_HTTP_PROXY_EXCLUSION_LIST);
2088 if (!TextUtils.isEmpty(host)) {
2089 ProxyProperties proxyProperties = new ProxyProperties(host, port, exclList);
2090 synchronized (mGlobalProxyLock) {
2091 mGlobalProxy = proxyProperties;
2092 }
2093 }
2094 }
2095
Robert Greenwalt434203a2010-10-11 16:00:27 -07002096 public ProxyProperties getGlobalProxy() {
2097 synchronized (mGlobalProxyLock) {
2098 return mGlobalProxy;
2099 }
2100 }
2101
2102 private void handleApplyDefaultProxy(int type) {
2103 // check if new default - push it out to all VM if so
2104 ProxyProperties proxy = mNetTrackers[type].getLinkProperties().getHttpProxy();
2105 synchronized (this) {
2106 if (mDefaultProxy != null && mDefaultProxy.equals(proxy)) return;
2107 if (mDefaultProxy == proxy) return;
2108 if (!TextUtils.isEmpty(proxy.getHost())) {
2109 mDefaultProxy = proxy;
2110 } else {
2111 mDefaultProxy = null;
2112 }
2113 }
Wink Savilleed9c02b2010-12-03 12:01:38 -08002114 if (DBG) log("changing default proxy to " + proxy);
Robert Greenwalt434203a2010-10-11 16:00:27 -07002115 if ((proxy == null && mGlobalProxy == null) || proxy.equals(mGlobalProxy)) return;
2116 if (mGlobalProxy != null) return;
2117 sendProxyBroadcast(proxy);
2118 }
2119
2120 private void handleDeprecatedGlobalHttpProxy() {
2121 String proxy = Settings.Secure.getString(mContext.getContentResolver(),
2122 Settings.Secure.HTTP_PROXY);
2123 if (!TextUtils.isEmpty(proxy)) {
2124 String data[] = proxy.split(":");
2125 String proxyHost = data[0];
2126 int proxyPort = 8080;
2127 if (data.length > 1) {
2128 try {
2129 proxyPort = Integer.parseInt(data[1]);
2130 } catch (NumberFormatException e) {
2131 return;
2132 }
2133 }
2134 ProxyProperties p = new ProxyProperties(data[0], proxyPort, "");
2135 setGlobalProxy(p);
2136 }
2137 }
2138
2139 private void sendProxyBroadcast(ProxyProperties proxy) {
Robert Greenwalt55985be2010-12-23 15:51:10 -08002140 if (proxy == null) proxy = new ProxyProperties("", 0, "");
Wink Savilleed9c02b2010-12-03 12:01:38 -08002141 log("sending Proxy Broadcast for " + proxy);
Robert Greenwalt434203a2010-10-11 16:00:27 -07002142 Intent intent = new Intent(Proxy.PROXY_CHANGE_ACTION);
Stan Chesnuttb35d67a2011-01-06 11:00:19 -08002143 intent.addFlags(Intent.FLAG_RECEIVER_REPLACE_PENDING |
2144 Intent.FLAG_RECEIVER_REGISTERED_ONLY_BEFORE_BOOT);
Robert Greenwalt434203a2010-10-11 16:00:27 -07002145 intent.putExtra(Proxy.EXTRA_PROXY_INFO, proxy);
Robert Greenwalta2e13392010-12-06 11:29:17 -08002146 mContext.sendStickyBroadcast(intent);
Robert Greenwalt434203a2010-10-11 16:00:27 -07002147 }
2148
2149 private static class SettingsObserver extends ContentObserver {
2150 private int mWhat;
2151 private Handler mHandler;
2152 SettingsObserver(Handler handler, int what) {
2153 super(handler);
2154 mHandler = handler;
2155 mWhat = what;
2156 }
2157
2158 void observe(Context context) {
2159 ContentResolver resolver = context.getContentResolver();
2160 resolver.registerContentObserver(Settings.Secure.getUriFor(
2161 Settings.Secure.HTTP_PROXY), false, this);
2162 }
2163
2164 @Override
2165 public void onChange(boolean selfChange) {
2166 mHandler.obtainMessage(mWhat).sendToTarget();
2167 }
2168 }
Wink Savilleed9c02b2010-12-03 12:01:38 -08002169
2170 private void log(String s) {
2171 Slog.d(TAG, s);
2172 }
2173
2174 private void loge(String s) {
2175 Slog.e(TAG, s);
2176 }
Wink Saville9d7d6282011-03-12 14:52:01 -08002177 int convertFeatureToNetworkType(String feature){
2178 int networkType = -1;
2179 if (TextUtils.equals(feature, Phone.FEATURE_ENABLE_MMS)) {
2180 networkType = ConnectivityManager.TYPE_MOBILE_MMS;
2181 } else if (TextUtils.equals(feature, Phone.FEATURE_ENABLE_SUPL)) {
2182 networkType = ConnectivityManager.TYPE_MOBILE_SUPL;
2183 } else if (TextUtils.equals(feature, Phone.FEATURE_ENABLE_DUN) ||
2184 TextUtils.equals(feature, Phone.FEATURE_ENABLE_DUN_ALWAYS)) {
2185 networkType = ConnectivityManager.TYPE_MOBILE_DUN;
2186 } else if (TextUtils.equals(feature, Phone.FEATURE_ENABLE_HIPRI)) {
2187 networkType = ConnectivityManager.TYPE_MOBILE_HIPRI;
2188 } else if (TextUtils.equals(feature, Phone.FEATURE_ENABLE_FOTA)) {
2189 networkType = ConnectivityManager.TYPE_MOBILE_FOTA;
2190 } else if (TextUtils.equals(feature, Phone.FEATURE_ENABLE_IMS)) {
2191 networkType = ConnectivityManager.TYPE_MOBILE_IMS;
2192 } else if (TextUtils.equals(feature, Phone.FEATURE_ENABLE_CBS)) {
2193 networkType = ConnectivityManager.TYPE_MOBILE_CBS;
2194 }
2195 return networkType;
2196 }
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002197}