blob: 758a46a82973c4b1b33c4a7164a6c2019a368dfc [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;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080027import android.net.IConnectivityManager;
Jaikumar Ganesh15c74392010-12-21 22:31:44 -080028import android.net.LinkProperties;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080029import android.net.MobileDataStateTracker;
30import android.net.NetworkInfo;
31import android.net.NetworkStateTracker;
Robert Greenwalt585ac0f2010-08-27 09:24:29 -070032import android.net.NetworkUtils;
Robert Greenwalt434203a2010-10-11 16:00:27 -070033import android.net.Proxy;
34import android.net.ProxyProperties;
Hung-ying Tyan6b818de2011-01-19 16:48:38 +080035import android.net.vpn.VpnManager;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080036import android.net.wifi.WifiStateTracker;
37import android.os.Binder;
38import android.os.Handler;
Wink Savillebb08caf2010-09-02 19:23:52 -070039import android.os.HandlerThread;
Robert Greenwalt42acef32009-08-12 16:08:25 -070040import android.os.IBinder;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080041import android.os.Looper;
42import android.os.Message;
Robert Greenwalt14f2ef42010-06-15 12:19:37 -070043import android.os.PowerManager;
Robert Greenwalt42acef32009-08-12 16:08:25 -070044import android.os.RemoteException;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080045import android.os.ServiceManager;
46import android.os.SystemProperties;
47import android.provider.Settings;
Robert Greenwalt42acef32009-08-12 16:08:25 -070048import android.text.TextUtils;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080049import android.util.EventLog;
Joe Onorato8a9b2202010-02-26 18:56:32 -080050import android.util.Slog;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080051
Robert Greenwalt42acef32009-08-12 16:08:25 -070052import com.android.internal.telephony.Phone;
Robert Greenwaltd0e18ff2010-01-26 11:40:34 -080053import com.android.server.connectivity.Tethering;
54
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080055import java.io.FileDescriptor;
Irfan Sheriffd649c122010-06-09 15:39:36 -070056import java.io.FileWriter;
57import java.io.IOException;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080058import java.io.PrintWriter;
Robert Greenwalt47f69fe2010-06-15 15:43:39 -070059import java.net.InetAddress;
60import java.net.UnknownHostException;
Robert Greenwalt42acef32009-08-12 16:08:25 -070061import java.util.ArrayList;
Robert Greenwalt47f69fe2010-06-15 15:43:39 -070062import java.util.Collection;
Robert Greenwaltd825ea42010-12-29 16:15:02 -080063import java.util.concurrent.atomic.AtomicBoolean;
Robert Greenwalt4e8dfef2010-09-20 14:35:25 -070064import java.util.GregorianCalendar;
Robert Greenwalt42acef32009-08-12 16:08:25 -070065import java.util.List;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080066
67/**
68 * @hide
69 */
70public class ConnectivityService extends IConnectivityManager.Stub {
71
Robert Greenwaltba175a52010-10-05 19:12:26 -070072 private static final boolean DBG = true;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080073 private static final String TAG = "ConnectivityService";
74
Robert Greenwalt42acef32009-08-12 16:08:25 -070075 // how long to wait before switching back to a radio's default network
76 private static final int RESTORE_DEFAULT_NETWORK_DELAY = 1 * 60 * 1000;
77 // system property that can override the above value
78 private static final String NETWORK_RESTORE_DELAY_PROP_NAME =
79 "android.telephony.apn-restore";
80
Robert Greenwaltd0e18ff2010-01-26 11:40:34 -080081 private Tethering mTethering;
Robert Greenwaltc9d5fb72010-02-25 12:29:30 -080082 private boolean mTetheringConfigValid = false;
Robert Greenwaltd0e18ff2010-01-26 11:40:34 -080083
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080084 /**
85 * Sometimes we want to refer to the individual network state
86 * trackers separately, and sometimes we just want to treat them
87 * abstractly.
88 */
89 private NetworkStateTracker mNetTrackers[];
Robert Greenwalt42acef32009-08-12 16:08:25 -070090
91 /**
92 * A per Net list of the PID's that requested access to the net
93 * used both as a refcount and for per-PID DNS selection
94 */
95 private List mNetRequestersPids[];
96
Irfan Sheriffa2a1b912010-06-07 09:03:04 -070097 private WifiWatchdogService mWifiWatchdogService;
98
Robert Greenwalt42acef32009-08-12 16:08:25 -070099 // priority order of the nettrackers
100 // (excluding dynamically set mNetworkPreference)
101 // TODO - move mNetworkTypePreference into this
102 private int[] mPriorityList;
103
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800104 private Context mContext;
105 private int mNetworkPreference;
Robert Greenwalt42acef32009-08-12 16:08:25 -0700106 private int mActiveDefaultNetwork = -1;
Robert Greenwaltd7085fc2010-09-08 15:24:47 -0700107 // 0 is full bad, 100 is full good
108 private int mDefaultInetCondition = 0;
109 private int mDefaultInetConditionPublished = 0;
110 private boolean mInetConditionChangeInFlight = false;
111 private int mDefaultConnectionSequence = 0;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800112
113 private int mNumDnsEntries;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800114
115 private boolean mTestMode;
Joe Onorato00092872010-09-01 21:18:22 -0700116 private static ConnectivityService sServiceInstance;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800117
Robert Greenwaltd825ea42010-12-29 16:15:02 -0800118 private AtomicBoolean mBackgroundDataEnabled = new AtomicBoolean(true);
119
Robert Greenwalt8dcc28b2010-09-23 10:05:56 -0700120 private static final int ENABLED = 1;
121 private static final int DISABLED = 0;
122
123 // Share the event space with NetworkStateTracker (which can't see this
124 // internal class but sends us events). If you change these, change
125 // NetworkStateTracker.java too.
126 private static final int MIN_NETWORK_STATE_TRACKER_EVENT = 1;
127 private static final int MAX_NETWORK_STATE_TRACKER_EVENT = 100;
128
129 /**
130 * used internally as a delayed event to make us switch back to the
131 * default network
132 */
133 private static final int EVENT_RESTORE_DEFAULT_NETWORK =
134 MAX_NETWORK_STATE_TRACKER_EVENT + 1;
135
136 /**
137 * used internally to change our mobile data enabled flag
138 */
139 private static final int EVENT_CHANGE_MOBILE_DATA_ENABLED =
140 MAX_NETWORK_STATE_TRACKER_EVENT + 2;
141
142 /**
143 * used internally to change our network preference setting
144 * arg1 = networkType to prefer
145 */
146 private static final int EVENT_SET_NETWORK_PREFERENCE =
147 MAX_NETWORK_STATE_TRACKER_EVENT + 3;
148
149 /**
150 * used internally to synchronize inet condition reports
151 * arg1 = networkType
152 * arg2 = condition (0 bad, 100 good)
153 */
154 private static final int EVENT_INET_CONDITION_CHANGE =
155 MAX_NETWORK_STATE_TRACKER_EVENT + 4;
156
157 /**
158 * used internally to mark the end of inet condition hold periods
159 * arg1 = networkType
160 */
161 private static final int EVENT_INET_CONDITION_HOLD_END =
162 MAX_NETWORK_STATE_TRACKER_EVENT + 5;
163
164 /**
165 * used internally to set the background data preference
166 * arg1 = TRUE for enabled, FALSE for disabled
167 */
168 private static final int EVENT_SET_BACKGROUND_DATA =
169 MAX_NETWORK_STATE_TRACKER_EVENT + 6;
170
171 /**
172 * used internally to set enable/disable cellular data
173 * arg1 = ENBALED or DISABLED
174 */
175 private static final int EVENT_SET_MOBILE_DATA =
176 MAX_NETWORK_STATE_TRACKER_EVENT + 7;
177
Robert Greenwaltf3331232010-09-24 14:32:21 -0700178 /**
179 * used internally to clear a wakelock when transitioning
180 * from one net to another
181 */
182 private static final int EVENT_CLEAR_NET_TRANSITION_WAKELOCK =
183 MAX_NETWORK_STATE_TRACKER_EVENT + 8;
184
Robert Greenwalt434203a2010-10-11 16:00:27 -0700185 /**
186 * used internally to reload global proxy settings
187 */
188 private static final int EVENT_APPLY_GLOBAL_HTTP_PROXY =
189 MAX_NETWORK_STATE_TRACKER_EVENT + 9;
190
Robert Greenwalt42acef32009-08-12 16:08:25 -0700191 private Handler mHandler;
192
193 // list of DeathRecipients used to make sure features are turned off when
194 // a process dies
195 private List mFeatureUsers;
196
Mike Lockwood0f79b542009-08-14 14:18:49 -0400197 private boolean mSystemReady;
Dianne Hackborn1c633fc2009-12-08 19:45:14 -0800198 private Intent mInitialBroadcast;
Mike Lockwood0f79b542009-08-14 14:18:49 -0400199
Robert Greenwalt14f2ef42010-06-15 12:19:37 -0700200 private PowerManager.WakeLock mNetTransitionWakeLock;
201 private String mNetTransitionWakeLockCausedBy = "";
202 private int mNetTransitionWakeLockSerialNumber;
203 private int mNetTransitionWakeLockTimeout;
204
Robert Greenwalte90aa5e2010-09-01 11:34:05 -0700205 private InetAddress mDefaultDns;
206
Robert Greenwalt4e8dfef2010-09-20 14:35:25 -0700207 // used in DBG mode to track inet condition reports
208 private static final int INET_CONDITION_LOG_MAX_SIZE = 15;
209 private ArrayList mInetLog;
210
Robert Greenwalt434203a2010-10-11 16:00:27 -0700211 // track the current default http proxy - tell the world if we get a new one (real change)
212 private ProxyProperties mDefaultProxy = null;
213 // track the global proxy.
214 private ProxyProperties mGlobalProxy = null;
215 private final Object mGlobalProxyLock = new Object();
216
217 private SettingsObserver mSettingsObserver;
218
Robert Greenwalt511288a2009-12-07 11:33:18 -0800219 private static class NetworkAttributes {
Robert Greenwalt42acef32009-08-12 16:08:25 -0700220 /**
221 * Class for holding settings read from resources.
222 */
223 public String mName;
224 public int mType;
225 public int mRadio;
226 public int mPriority;
Robert Greenwalt511288a2009-12-07 11:33:18 -0800227 public NetworkInfo.State mLastState;
Robert Greenwalt42acef32009-08-12 16:08:25 -0700228 public NetworkAttributes(String init) {
229 String fragments[] = init.split(",");
230 mName = fragments[0].toLowerCase();
Robert Greenwalt5154ae762009-10-30 14:17:42 -0700231 mType = Integer.parseInt(fragments[1]);
232 mRadio = Integer.parseInt(fragments[2]);
233 mPriority = Integer.parseInt(fragments[3]);
Robert Greenwalt511288a2009-12-07 11:33:18 -0800234 mLastState = NetworkInfo.State.UNKNOWN;
Robert Greenwalt42acef32009-08-12 16:08:25 -0700235 }
236 public boolean isDefault() {
237 return (mType == mRadio);
238 }
239 }
240 NetworkAttributes[] mNetAttributes;
Robert Greenwalt5154ae762009-10-30 14:17:42 -0700241 int mNetworksDefined;
Robert Greenwalt42acef32009-08-12 16:08:25 -0700242
Robert Greenwalt511288a2009-12-07 11:33:18 -0800243 private static class RadioAttributes {
Robert Greenwalt42acef32009-08-12 16:08:25 -0700244 public int mSimultaneity;
245 public int mType;
246 public RadioAttributes(String init) {
247 String fragments[] = init.split(",");
Robert Greenwalt5154ae762009-10-30 14:17:42 -0700248 mType = Integer.parseInt(fragments[0]);
249 mSimultaneity = Integer.parseInt(fragments[1]);
Robert Greenwalt42acef32009-08-12 16:08:25 -0700250 }
251 }
252 RadioAttributes[] mRadioAttributes;
253
Wink Savillebb08caf2010-09-02 19:23:52 -0700254 public static synchronized ConnectivityService getInstance(Context context) {
255 if (sServiceInstance == null) {
256 sServiceInstance = new ConnectivityService(context);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800257 }
Wink Savillebb08caf2010-09-02 19:23:52 -0700258 return sServiceInstance;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800259 }
Robert Greenwalt86e9e552009-07-16 17:21:39 -0700260
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800261 private ConnectivityService(Context context) {
Wink Savilleed9c02b2010-12-03 12:01:38 -0800262 if (DBG) log("ConnectivityService starting up");
Robert Greenwaltde8383c2010-01-14 17:47:58 -0800263
Wink Savillebb08caf2010-09-02 19:23:52 -0700264 HandlerThread handlerThread = new HandlerThread("ConnectivityServiceThread");
265 handlerThread.start();
266 mHandler = new MyHandler(handlerThread.getLooper());
267
Robert Greenwaltd825ea42010-12-29 16:15:02 -0800268 mBackgroundDataEnabled.set(Settings.Secure.getInt(context.getContentResolver(),
269 Settings.Secure.BACKGROUND_DATA, 1) == 1);
270
Robert Greenwaltde8383c2010-01-14 17:47:58 -0800271 // setup our unique device name
Robert Greenwalt733c6292010-12-06 09:30:17 -0800272 if (TextUtils.isEmpty(SystemProperties.get("net.hostname"))) {
273 String id = Settings.Secure.getString(context.getContentResolver(),
274 Settings.Secure.ANDROID_ID);
275 if (id != null && id.length() > 0) {
276 String name = new String("android_").concat(id);
277 SystemProperties.set("net.hostname", name);
278 }
Robert Greenwaltde8383c2010-01-14 17:47:58 -0800279 }
280
Robert Greenwalte90aa5e2010-09-01 11:34:05 -0700281 // read our default dns server ip
282 String dns = Settings.Secure.getString(context.getContentResolver(),
283 Settings.Secure.DEFAULT_DNS_SERVER);
284 if (dns == null || dns.length() == 0) {
285 dns = context.getResources().getString(
286 com.android.internal.R.string.config_default_dns_server);
287 }
288 try {
289 mDefaultDns = InetAddress.getByName(dns);
290 } catch (UnknownHostException e) {
Wink Savilleed9c02b2010-12-03 12:01:38 -0800291 loge("Error setting defaultDns using " + dns);
Robert Greenwalte90aa5e2010-09-01 11:34:05 -0700292 }
293
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800294 mContext = context;
Robert Greenwalt14f2ef42010-06-15 12:19:37 -0700295
296 PowerManager powerManager = (PowerManager)mContext.getSystemService(Context.POWER_SERVICE);
297 mNetTransitionWakeLock = powerManager.newWakeLock(PowerManager.PARTIAL_WAKE_LOCK, TAG);
298 mNetTransitionWakeLockTimeout = mContext.getResources().getInteger(
299 com.android.internal.R.integer.config_networkTransitionTimeout);
300
Robert Greenwalt42acef32009-08-12 16:08:25 -0700301 mNetTrackers = new NetworkStateTracker[
302 ConnectivityManager.MAX_NETWORK_TYPE+1];
Robert Greenwalt86e9e552009-07-16 17:21:39 -0700303
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800304 mNetworkPreference = getPersistedNetworkPreference();
Robert Greenwalt86e9e552009-07-16 17:21:39 -0700305
Robert Greenwalt5154ae762009-10-30 14:17:42 -0700306 mRadioAttributes = new RadioAttributes[ConnectivityManager.MAX_RADIO_TYPE+1];
307 mNetAttributes = new NetworkAttributes[ConnectivityManager.MAX_NETWORK_TYPE+1];
308
Robert Greenwalt42acef32009-08-12 16:08:25 -0700309 // Load device network attributes from resources
Robert Greenwalt42acef32009-08-12 16:08:25 -0700310 String[] raStrings = context.getResources().getStringArray(
311 com.android.internal.R.array.radioAttributes);
Robert Greenwalt5154ae762009-10-30 14:17:42 -0700312 for (String raString : raStrings) {
313 RadioAttributes r = new RadioAttributes(raString);
314 if (r.mType > ConnectivityManager.MAX_RADIO_TYPE) {
Wink Savilleed9c02b2010-12-03 12:01:38 -0800315 loge("Error in radioAttributes - ignoring attempt to define type " + r.mType);
Robert Greenwalt5154ae762009-10-30 14:17:42 -0700316 continue;
317 }
318 if (mRadioAttributes[r.mType] != null) {
Wink Savilleed9c02b2010-12-03 12:01:38 -0800319 loge("Error in radioAttributes - ignoring attempt to redefine type " +
Robert Greenwalt5154ae762009-10-30 14:17:42 -0700320 r.mType);
321 continue;
322 }
Robert Greenwalt42acef32009-08-12 16:08:25 -0700323 mRadioAttributes[r.mType] = r;
324 }
325
Robert Greenwalt5154ae762009-10-30 14:17:42 -0700326 String[] naStrings = context.getResources().getStringArray(
327 com.android.internal.R.array.networkAttributes);
328 for (String naString : naStrings) {
329 try {
330 NetworkAttributes n = new NetworkAttributes(naString);
331 if (n.mType > ConnectivityManager.MAX_NETWORK_TYPE) {
Wink Savilleed9c02b2010-12-03 12:01:38 -0800332 loge("Error in networkAttributes - ignoring attempt to define type " +
Robert Greenwalt5154ae762009-10-30 14:17:42 -0700333 n.mType);
334 continue;
Robert Greenwalt42acef32009-08-12 16:08:25 -0700335 }
Robert Greenwalt5154ae762009-10-30 14:17:42 -0700336 if (mNetAttributes[n.mType] != null) {
Wink Savilleed9c02b2010-12-03 12:01:38 -0800337 loge("Error in networkAttributes - ignoring attempt to redefine type " +
Robert Greenwalt5154ae762009-10-30 14:17:42 -0700338 n.mType);
339 continue;
340 }
341 if (mRadioAttributes[n.mRadio] == null) {
Wink Savilleed9c02b2010-12-03 12:01:38 -0800342 loge("Error in networkAttributes - ignoring attempt to use undefined " +
Robert Greenwalt5154ae762009-10-30 14:17:42 -0700343 "radio " + n.mRadio + " in network type " + n.mType);
344 continue;
345 }
346 mNetAttributes[n.mType] = n;
347 mNetworksDefined++;
348 } catch(Exception e) {
349 // ignore it - leave the entry null
Robert Greenwalt42acef32009-08-12 16:08:25 -0700350 }
351 }
352
Robert Greenwalt5154ae762009-10-30 14:17:42 -0700353 // high priority first
354 mPriorityList = new int[mNetworksDefined];
355 {
356 int insertionPoint = mNetworksDefined-1;
357 int currentLowest = 0;
358 int nextLowest = 0;
359 while (insertionPoint > -1) {
360 for (NetworkAttributes na : mNetAttributes) {
361 if (na == null) continue;
362 if (na.mPriority < currentLowest) continue;
363 if (na.mPriority > currentLowest) {
364 if (na.mPriority < nextLowest || nextLowest == 0) {
365 nextLowest = na.mPriority;
366 }
367 continue;
368 }
369 mPriorityList[insertionPoint--] = na.mType;
370 }
371 currentLowest = nextLowest;
372 nextLowest = 0;
373 }
374 }
375
376 mNetRequestersPids = new ArrayList[ConnectivityManager.MAX_NETWORK_TYPE+1];
377 for (int i : mPriorityList) {
Robert Greenwalt42acef32009-08-12 16:08:25 -0700378 mNetRequestersPids[i] = new ArrayList();
379 }
380
381 mFeatureUsers = new ArrayList();
382
Robert Greenwalt5154ae762009-10-30 14:17:42 -0700383 mNumDnsEntries = 0;
384
385 mTestMode = SystemProperties.get("cm.test.mode").equals("true")
386 && SystemProperties.get("ro.build.type").equals("eng");
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800387 /*
388 * Create the network state trackers for Wi-Fi and mobile
389 * data. Maybe this could be done with a factory class,
390 * but it's not clear that it's worth it, given that
391 * the number of different network types is not going
392 * to change very often.
393 */
Robert Greenwalt5154ae762009-10-30 14:17:42 -0700394 for (int netType : mPriorityList) {
395 switch (mNetAttributes[netType].mRadio) {
396 case ConnectivityManager.TYPE_WIFI:
Wink Savilleed9c02b2010-12-03 12:01:38 -0800397 if (DBG) log("Starting Wifi Service.");
Wink Savillec7a98342010-08-13 16:11:42 -0700398 WifiStateTracker wst = new WifiStateTracker();
Irfan Sheriff0d255342010-07-28 09:35:20 -0700399 WifiService wifiService = new WifiService(context);
Robert Greenwalt5154ae762009-10-30 14:17:42 -0700400 ServiceManager.addService(Context.WIFI_SERVICE, wifiService);
Irfan Sheriff0d255342010-07-28 09:35:20 -0700401 wifiService.checkAndStartWifi();
Robert Greenwalt5154ae762009-10-30 14:17:42 -0700402 mNetTrackers[ConnectivityManager.TYPE_WIFI] = wst;
Wink Savillec7a98342010-08-13 16:11:42 -0700403 wst.startMonitoring(context, mHandler);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800404
Irfan Sheriffa2a1b912010-06-07 09:03:04 -0700405 //TODO: as part of WWS refactor, create only when needed
Irfan Sheriff0d255342010-07-28 09:35:20 -0700406 mWifiWatchdogService = new WifiWatchdogService(context);
Irfan Sheriffa2a1b912010-06-07 09:03:04 -0700407
Robert Greenwalt5154ae762009-10-30 14:17:42 -0700408 break;
409 case ConnectivityManager.TYPE_MOBILE:
Wink Savillec7a98342010-08-13 16:11:42 -0700410 mNetTrackers[netType] = new MobileDataStateTracker(netType,
411 mNetAttributes[netType].mName);
412 mNetTrackers[netType].startMonitoring(context, mHandler);
Robert Greenwalt5154ae762009-10-30 14:17:42 -0700413 break;
Robert Greenwaltda3d5e62010-12-06 13:56:24 -0800414 case ConnectivityManager.TYPE_DUMMY:
415 mNetTrackers[netType] = new DummyDataStateTracker(netType,
416 mNetAttributes[netType].mName);
417 mNetTrackers[netType].startMonitoring(context, mHandler);
418 break;
Jaikumar Ganesh15c74392010-12-21 22:31:44 -0800419 case ConnectivityManager.TYPE_BLUETOOTH:
420 mNetTrackers[netType] = BluetoothTetheringDataTracker.getInstance();
421 mNetTrackers[netType].startMonitoring(context, mHandler);
422 break;
Robert Greenwalt5154ae762009-10-30 14:17:42 -0700423 default:
Wink Savilleed9c02b2010-12-03 12:01:38 -0800424 loge("Trying to create a DataStateTracker for an unknown radio type " +
Robert Greenwalt5154ae762009-10-30 14:17:42 -0700425 mNetAttributes[netType].mRadio);
426 continue;
427 }
428 }
Robert Greenwaltd0e18ff2010-01-26 11:40:34 -0800429
Robert Greenwaltdfadaea2010-03-11 15:03:08 -0800430 mTethering = new Tethering(mContext, mHandler.getLooper());
Robert Greenwaltc9d5fb72010-02-25 12:29:30 -0800431 mTetheringConfigValid = (((mNetTrackers[ConnectivityManager.TYPE_MOBILE_DUN] != null) ||
432 !mTethering.isDunRequired()) &&
433 (mTethering.getTetherableUsbRegexs().length != 0 ||
Danica Chang6fdd0c62010-08-11 14:54:43 -0700434 mTethering.getTetherableWifiRegexs().length != 0 ||
435 mTethering.getTetherableBluetoothRegexs().length != 0) &&
Robert Greenwaltc9d5fb72010-02-25 12:29:30 -0800436 mTethering.getUpstreamIfaceRegexs().length != 0);
437
Robert Greenwalt4e8dfef2010-09-20 14:35:25 -0700438 if (DBG) {
439 mInetLog = new ArrayList();
440 }
Robert Greenwalt434203a2010-10-11 16:00:27 -0700441
442 mSettingsObserver = new SettingsObserver(mHandler, EVENT_APPLY_GLOBAL_HTTP_PROXY);
443 mSettingsObserver.observe(mContext);
Robert Greenwaltb7090d62010-12-02 11:31:00 -0800444
445 loadGlobalProxy();
Hung-ying Tyan6b818de2011-01-19 16:48:38 +0800446
447 VpnManager.startVpnService(context);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800448 }
449
Robert Greenwalt5154ae762009-10-30 14:17:42 -0700450
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800451 /**
Robert Greenwalt86e9e552009-07-16 17:21:39 -0700452 * Sets the preferred network.
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800453 * @param preference the new preference
454 */
Robert Greenwalt8dcc28b2010-09-23 10:05:56 -0700455 public void setNetworkPreference(int preference) {
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800456 enforceChangePermission();
Robert Greenwalt8dcc28b2010-09-23 10:05:56 -0700457
458 mHandler.sendMessage(mHandler.obtainMessage(EVENT_SET_NETWORK_PREFERENCE, preference, 0));
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800459 }
460
461 public int getNetworkPreference() {
462 enforceAccessPermission();
Robert Greenwalt8dcc28b2010-09-23 10:05:56 -0700463 int preference;
464 synchronized(this) {
465 preference = mNetworkPreference;
466 }
467 return preference;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800468 }
469
Robert Greenwalt8dcc28b2010-09-23 10:05:56 -0700470 private void handleSetNetworkPreference(int preference) {
471 if (ConnectivityManager.isNetworkTypeValid(preference) &&
472 mNetAttributes[preference] != null &&
473 mNetAttributes[preference].isDefault()) {
474 if (mNetworkPreference != preference) {
475 final ContentResolver cr = mContext.getContentResolver();
476 Settings.Secure.putInt(cr, Settings.Secure.NETWORK_PREFERENCE, preference);
477 synchronized(this) {
478 mNetworkPreference = preference;
479 }
480 enforcePreference();
481 }
482 }
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800483 }
Robert Greenwalt86e9e552009-07-16 17:21:39 -0700484
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800485 private int getPersistedNetworkPreference() {
486 final ContentResolver cr = mContext.getContentResolver();
487
488 final int networkPrefSetting = Settings.Secure
489 .getInt(cr, Settings.Secure.NETWORK_PREFERENCE, -1);
490 if (networkPrefSetting != -1) {
491 return networkPrefSetting;
492 }
493
494 return ConnectivityManager.DEFAULT_NETWORK_PREFERENCE;
495 }
Robert Greenwalt86e9e552009-07-16 17:21:39 -0700496
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800497 /**
Robert Greenwalt86e9e552009-07-16 17:21:39 -0700498 * Make the state of network connectivity conform to the preference settings
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800499 * In this method, we only tear down a non-preferred network. Establishing
500 * a connection to the preferred network is taken care of when we handle
501 * the disconnect event from the non-preferred network
502 * (see {@link #handleDisconnect(NetworkInfo)}).
503 */
504 private void enforcePreference() {
Robert Greenwalt42acef32009-08-12 16:08:25 -0700505 if (mNetTrackers[mNetworkPreference].getNetworkInfo().isConnected())
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800506 return;
507
Robert Greenwalt42acef32009-08-12 16:08:25 -0700508 if (!mNetTrackers[mNetworkPreference].isAvailable())
509 return;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800510
Robert Greenwalt42acef32009-08-12 16:08:25 -0700511 for (int t=0; t <= ConnectivityManager.MAX_RADIO_TYPE; t++) {
Robert Greenwalt5154ae762009-10-30 14:17:42 -0700512 if (t != mNetworkPreference && mNetTrackers[t] != null &&
Robert Greenwalt42acef32009-08-12 16:08:25 -0700513 mNetTrackers[t].getNetworkInfo().isConnected()) {
Robert Greenwaltec9fe462009-08-20 15:25:14 -0700514 if (DBG) {
Wink Savilleed9c02b2010-12-03 12:01:38 -0800515 log("tearing down " + mNetTrackers[t].getNetworkInfo() +
Robert Greenwaltec9fe462009-08-20 15:25:14 -0700516 " in enforcePreference");
517 }
Robert Greenwalt42acef32009-08-12 16:08:25 -0700518 teardown(mNetTrackers[t]);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800519 }
520 }
521 }
522
523 private boolean teardown(NetworkStateTracker netTracker) {
524 if (netTracker.teardown()) {
525 netTracker.setTeardownRequested(true);
526 return true;
527 } else {
528 return false;
529 }
530 }
531
532 /**
533 * Return NetworkInfo for the active (i.e., connected) network interface.
534 * It is assumed that at most one network is active at a time. If more
535 * than one is active, it is indeterminate which will be returned.
Robert Greenwalt86e9e552009-07-16 17:21:39 -0700536 * @return the info for the active network, or {@code null} if none is
537 * active
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800538 */
539 public NetworkInfo getActiveNetworkInfo() {
540 enforceAccessPermission();
Robert Greenwalt42acef32009-08-12 16:08:25 -0700541 for (int type=0; type <= ConnectivityManager.MAX_NETWORK_TYPE; type++) {
Robert Greenwalt5154ae762009-10-30 14:17:42 -0700542 if (mNetAttributes[type] == null || !mNetAttributes[type].isDefault()) {
Robert Greenwalt42acef32009-08-12 16:08:25 -0700543 continue;
544 }
545 NetworkStateTracker t = mNetTrackers[type];
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800546 NetworkInfo info = t.getNetworkInfo();
547 if (info.isConnected()) {
Wink Savilleed9c02b2010-12-03 12:01:38 -0800548 if (DBG && type != mActiveDefaultNetwork) {
549 loge("connected default network is not mActiveDefaultNetwork!");
550 }
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800551 return info;
552 }
553 }
554 return null;
555 }
556
557 public NetworkInfo getNetworkInfo(int networkType) {
558 enforceAccessPermission();
559 if (ConnectivityManager.isNetworkTypeValid(networkType)) {
560 NetworkStateTracker t = mNetTrackers[networkType];
561 if (t != null)
562 return t.getNetworkInfo();
563 }
564 return null;
565 }
566
567 public NetworkInfo[] getAllNetworkInfo() {
568 enforceAccessPermission();
Robert Greenwalt5154ae762009-10-30 14:17:42 -0700569 NetworkInfo[] result = new NetworkInfo[mNetworksDefined];
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800570 int i = 0;
571 for (NetworkStateTracker t : mNetTrackers) {
Robert Greenwalt5154ae762009-10-30 14:17:42 -0700572 if(t != null) result[i++] = t.getNetworkInfo();
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800573 }
574 return result;
575 }
576
Robert Greenwaltd192dad2010-09-14 09:18:02 -0700577 /**
578 * Return LinkProperties for the active (i.e., connected) default
579 * network interface. It is assumed that at most one default network
580 * is active at a time. If more than one is active, it is indeterminate
581 * which will be returned.
582 * @return the ip properties for the active network, or {@code null} if
583 * none is active
584 */
585 public LinkProperties getActiveLinkProperties() {
586 enforceAccessPermission();
587 for (int type=0; type <= ConnectivityManager.MAX_NETWORK_TYPE; type++) {
588 if (mNetAttributes[type] == null || !mNetAttributes[type].isDefault()) {
589 continue;
590 }
591 NetworkStateTracker t = mNetTrackers[type];
592 NetworkInfo info = t.getNetworkInfo();
593 if (info.isConnected()) {
594 return t.getLinkProperties();
595 }
596 }
597 return null;
598 }
599
600 public LinkProperties getLinkProperties(int networkType) {
601 enforceAccessPermission();
602 if (ConnectivityManager.isNetworkTypeValid(networkType)) {
603 NetworkStateTracker t = mNetTrackers[networkType];
604 if (t != null) return t.getLinkProperties();
605 }
606 return null;
607 }
608
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800609 public boolean setRadios(boolean turnOn) {
610 boolean result = true;
611 enforceChangePermission();
612 for (NetworkStateTracker t : mNetTrackers) {
Robert Greenwalt5154ae762009-10-30 14:17:42 -0700613 if (t != null) result = t.setRadio(turnOn) && result;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800614 }
615 return result;
616 }
617
618 public boolean setRadio(int netType, boolean turnOn) {
619 enforceChangePermission();
620 if (!ConnectivityManager.isNetworkTypeValid(netType)) {
621 return false;
622 }
623 NetworkStateTracker tracker = mNetTrackers[netType];
624 return tracker != null && tracker.setRadio(turnOn);
625 }
626
Robert Greenwalt9c75d4a2009-09-27 17:27:04 -0700627 /**
628 * Used to notice when the calling process dies so we can self-expire
629 *
630 * Also used to know if the process has cleaned up after itself when
631 * our auto-expire timer goes off. The timer has a link to an object.
632 *
633 */
Robert Greenwalt42acef32009-08-12 16:08:25 -0700634 private class FeatureUser implements IBinder.DeathRecipient {
635 int mNetworkType;
636 String mFeature;
637 IBinder mBinder;
638 int mPid;
639 int mUid;
Robert Greenwaltb9285352009-12-21 18:24:07 -0800640 long mCreateTime;
Robert Greenwalt42acef32009-08-12 16:08:25 -0700641
642 FeatureUser(int type, String feature, IBinder binder) {
643 super();
644 mNetworkType = type;
645 mFeature = feature;
646 mBinder = binder;
647 mPid = getCallingPid();
648 mUid = getCallingUid();
Robert Greenwaltb9285352009-12-21 18:24:07 -0800649 mCreateTime = System.currentTimeMillis();
Robert Greenwalt9c75d4a2009-09-27 17:27:04 -0700650
Robert Greenwalt42acef32009-08-12 16:08:25 -0700651 try {
652 mBinder.linkToDeath(this, 0);
653 } catch (RemoteException e) {
654 binderDied();
655 }
656 }
657
658 void unlinkDeathRecipient() {
659 mBinder.unlinkToDeath(this, 0);
660 }
661
662 public void binderDied() {
Wink Savilleed9c02b2010-12-03 12:01:38 -0800663 log("ConnectivityService FeatureUser binderDied(" +
Robert Greenwaltb9285352009-12-21 18:24:07 -0800664 mNetworkType + ", " + mFeature + ", " + mBinder + "), created " +
665 (System.currentTimeMillis() - mCreateTime) + " mSec ago");
Robert Greenwalt9c75d4a2009-09-27 17:27:04 -0700666 stopUsingNetworkFeature(this, false);
Robert Greenwalt42acef32009-08-12 16:08:25 -0700667 }
668
Robert Greenwalt9c75d4a2009-09-27 17:27:04 -0700669 public void expire() {
Wink Savilleed9c02b2010-12-03 12:01:38 -0800670 log("ConnectivityService FeatureUser expire(" +
Robert Greenwaltb9285352009-12-21 18:24:07 -0800671 mNetworkType + ", " + mFeature + ", " + mBinder +"), created " +
672 (System.currentTimeMillis() - mCreateTime) + " mSec ago");
Robert Greenwalt9c75d4a2009-09-27 17:27:04 -0700673 stopUsingNetworkFeature(this, false);
674 }
Robert Greenwaltb9285352009-12-21 18:24:07 -0800675
676 public String toString() {
677 return "FeatureUser("+mNetworkType+","+mFeature+","+mPid+","+mUid+"), created " +
678 (System.currentTimeMillis() - mCreateTime) + " mSec ago";
679 }
Robert Greenwalt42acef32009-08-12 16:08:25 -0700680 }
681
Robert Greenwalt9c75d4a2009-09-27 17:27:04 -0700682 // javadoc from interface
Robert Greenwalt42acef32009-08-12 16:08:25 -0700683 public int startUsingNetworkFeature(int networkType, String feature,
684 IBinder binder) {
685 if (DBG) {
Wink Savilleed9c02b2010-12-03 12:01:38 -0800686 log("startUsingNetworkFeature for net " + networkType + ": " + feature);
Robert Greenwalt42acef32009-08-12 16:08:25 -0700687 }
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800688 enforceChangePermission();
Robert Greenwalt5154ae762009-10-30 14:17:42 -0700689 if (!ConnectivityManager.isNetworkTypeValid(networkType) ||
690 mNetAttributes[networkType] == null) {
Robert Greenwalt42acef32009-08-12 16:08:25 -0700691 return Phone.APN_REQUEST_FAILED;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800692 }
Robert Greenwalt42acef32009-08-12 16:08:25 -0700693
Robert Greenwalt9c75d4a2009-09-27 17:27:04 -0700694 FeatureUser f = new FeatureUser(networkType, feature, binder);
Robert Greenwalt42acef32009-08-12 16:08:25 -0700695
696 // TODO - move this into the MobileDataStateTracker
697 int usedNetworkType = networkType;
698 if(networkType == ConnectivityManager.TYPE_MOBILE) {
699 if (TextUtils.equals(feature, Phone.FEATURE_ENABLE_MMS)) {
700 usedNetworkType = ConnectivityManager.TYPE_MOBILE_MMS;
701 } else if (TextUtils.equals(feature, Phone.FEATURE_ENABLE_SUPL)) {
702 usedNetworkType = ConnectivityManager.TYPE_MOBILE_SUPL;
Robert Greenwalte32e8122010-12-29 14:35:21 -0800703 } else if (TextUtils.equals(feature, Phone.FEATURE_ENABLE_DUN) ||
704 TextUtils.equals(feature, Phone.FEATURE_ENABLE_DUN_ALWAYS)) {
Robert Greenwalt42acef32009-08-12 16:08:25 -0700705 usedNetworkType = ConnectivityManager.TYPE_MOBILE_DUN;
706 } else if (TextUtils.equals(feature, Phone.FEATURE_ENABLE_HIPRI)) {
707 usedNetworkType = ConnectivityManager.TYPE_MOBILE_HIPRI;
708 }
709 }
710 NetworkStateTracker network = mNetTrackers[usedNetworkType];
711 if (network != null) {
Robert Greenwalt0be1e982010-12-15 13:26:33 -0800712 Integer currentPid = new Integer(getCallingPid());
Robert Greenwalt42acef32009-08-12 16:08:25 -0700713 if (usedNetworkType != networkType) {
Robert Greenwalt42acef32009-08-12 16:08:25 -0700714 NetworkStateTracker radio = mNetTrackers[networkType];
715 NetworkInfo ni = network.getNetworkInfo();
716
717 if (ni.isAvailable() == false) {
Wink Savilleed9c02b2010-12-03 12:01:38 -0800718 if (DBG) log("special network not available");
Robert Greenwalte32e8122010-12-29 14:35:21 -0800719 if (!TextUtils.equals(feature,Phone.FEATURE_ENABLE_DUN_ALWAYS)) {
720 return Phone.APN_TYPE_NOT_AVAILABLE;
721 } else {
722 // else make the attempt anyway - probably giving REQUEST_STARTED below
723 }
Robert Greenwalt42acef32009-08-12 16:08:25 -0700724 }
725
Robert Greenwalt9c75d4a2009-09-27 17:27:04 -0700726 synchronized(this) {
727 mFeatureUsers.add(f);
728 if (!mNetRequestersPids[usedNetworkType].contains(currentPid)) {
729 // this gets used for per-pid dns when connected
730 mNetRequestersPids[usedNetworkType].add(currentPid);
731 }
Robert Greenwalt42acef32009-08-12 16:08:25 -0700732 }
Robert Greenwalt8dcc28b2010-09-23 10:05:56 -0700733 mHandler.sendMessageDelayed(mHandler.obtainMessage(EVENT_RESTORE_DEFAULT_NETWORK,
Robert Greenwalt9c75d4a2009-09-27 17:27:04 -0700734 f), getRestoreDefaultNetworkDelay());
735
Robert Greenwalt42acef32009-08-12 16:08:25 -0700736
Robert Greenwalta64bf832009-08-19 20:19:33 -0700737 if ((ni.isConnectedOrConnecting() == true) &&
738 !network.isTeardownRequested()) {
Robert Greenwalt42acef32009-08-12 16:08:25 -0700739 if (ni.isConnected() == true) {
740 // add the pid-specific dns
Robert Greenwaltc76b8fa2010-07-23 15:46:26 -0700741 handleDnsConfigurationChange(networkType);
Wink Savilleed9c02b2010-12-03 12:01:38 -0800742 if (DBG) log("special network already active");
Robert Greenwalt42acef32009-08-12 16:08:25 -0700743 return Phone.APN_ALREADY_ACTIVE;
744 }
Wink Savilleed9c02b2010-12-03 12:01:38 -0800745 if (DBG) log("special network already connecting");
Robert Greenwalt42acef32009-08-12 16:08:25 -0700746 return Phone.APN_REQUEST_STARTED;
747 }
748
749 // check if the radio in play can make another contact
750 // assume if cannot for now
751
Wink Savilleed9c02b2010-12-03 12:01:38 -0800752 if (DBG) log("reconnecting to special network");
Robert Greenwalt42acef32009-08-12 16:08:25 -0700753 network.reconnect();
754 return Phone.APN_REQUEST_STARTED;
755 } else {
Robert Greenwalt0be1e982010-12-15 13:26:33 -0800756 // need to remember this unsupported request so we respond appropriately on stop
757 synchronized(this) {
758 mFeatureUsers.add(f);
759 if (!mNetRequestersPids[usedNetworkType].contains(currentPid)) {
760 // this gets used for per-pid dns when connected
761 mNetRequestersPids[usedNetworkType].add(currentPid);
762 }
763 }
Robert Greenwalt02648a42010-05-18 10:52:51 -0700764 return -1;
Robert Greenwalt42acef32009-08-12 16:08:25 -0700765 }
766 }
767 return Phone.APN_TYPE_NOT_AVAILABLE;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800768 }
769
Robert Greenwalt9c75d4a2009-09-27 17:27:04 -0700770 // javadoc from interface
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800771 public int stopUsingNetworkFeature(int networkType, String feature) {
Robert Greenwaltb8f16342009-10-06 17:52:40 -0700772 enforceChangePermission();
773
Robert Greenwalt9c75d4a2009-09-27 17:27:04 -0700774 int pid = getCallingPid();
775 int uid = getCallingUid();
776
777 FeatureUser u = null;
778 boolean found = false;
779
780 synchronized(this) {
781 for (int i = 0; i < mFeatureUsers.size() ; i++) {
782 u = (FeatureUser)mFeatureUsers.get(i);
783 if (uid == u.mUid && pid == u.mPid &&
784 networkType == u.mNetworkType &&
785 TextUtils.equals(feature, u.mFeature)) {
786 found = true;
787 break;
788 }
789 }
790 }
791 if (found && u != null) {
792 // stop regardless of how many other time this proc had called start
793 return stopUsingNetworkFeature(u, true);
794 } else {
795 // none found!
Wink Savilleed9c02b2010-12-03 12:01:38 -0800796 if (DBG) log("ignoring stopUsingNetworkFeature - not a live request");
Robert Greenwalt9c75d4a2009-09-27 17:27:04 -0700797 return 1;
798 }
Robert Greenwalt42acef32009-08-12 16:08:25 -0700799 }
800
Robert Greenwalt9c75d4a2009-09-27 17:27:04 -0700801 private int stopUsingNetworkFeature(FeatureUser u, boolean ignoreDups) {
802 int networkType = u.mNetworkType;
803 String feature = u.mFeature;
804 int pid = u.mPid;
805 int uid = u.mUid;
806
807 NetworkStateTracker tracker = null;
808 boolean callTeardown = false; // used to carry our decision outside of sync block
809
Robert Greenwalt42acef32009-08-12 16:08:25 -0700810 if (DBG) {
Wink Savilleed9c02b2010-12-03 12:01:38 -0800811 log("stopUsingNetworkFeature for net " + networkType +
Robert Greenwalt42acef32009-08-12 16:08:25 -0700812 ": " + feature);
813 }
Robert Greenwaltb8f16342009-10-06 17:52:40 -0700814
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800815 if (!ConnectivityManager.isNetworkTypeValid(networkType)) {
816 return -1;
817 }
Robert Greenwalt42acef32009-08-12 16:08:25 -0700818
Robert Greenwalt9c75d4a2009-09-27 17:27:04 -0700819 // need to link the mFeatureUsers list with the mNetRequestersPids state in this
820 // sync block
821 synchronized(this) {
822 // check if this process still has an outstanding start request
823 if (!mFeatureUsers.contains(u)) {
Wink Savilleed9c02b2010-12-03 12:01:38 -0800824 if (DBG) log("ignoring - this process has no outstanding requests");
Robert Greenwalt42acef32009-08-12 16:08:25 -0700825 return 1;
826 }
Robert Greenwalt9c75d4a2009-09-27 17:27:04 -0700827 u.unlinkDeathRecipient();
828 mFeatureUsers.remove(mFeatureUsers.indexOf(u));
829 // If we care about duplicate requests, check for that here.
830 //
831 // This is done to support the extension of a request - the app
832 // can request we start the network feature again and renew the
833 // auto-shutoff delay. Normal "stop" calls from the app though
834 // do not pay attention to duplicate requests - in effect the
835 // API does not refcount and a single stop will counter multiple starts.
836 if (ignoreDups == false) {
837 for (int i = 0; i < mFeatureUsers.size() ; i++) {
838 FeatureUser x = (FeatureUser)mFeatureUsers.get(i);
839 if (x.mUid == u.mUid && x.mPid == u.mPid &&
840 x.mNetworkType == u.mNetworkType &&
841 TextUtils.equals(x.mFeature, u.mFeature)) {
Wink Savilleed9c02b2010-12-03 12:01:38 -0800842 if (DBG) log("ignoring stopUsingNetworkFeature as dup is found");
Robert Greenwalt9c75d4a2009-09-27 17:27:04 -0700843 return 1;
844 }
845 }
Robert Greenwalt42acef32009-08-12 16:08:25 -0700846 }
Robert Greenwalt9c75d4a2009-09-27 17:27:04 -0700847
848 // TODO - move to MobileDataStateTracker
849 int usedNetworkType = networkType;
850 if (networkType == ConnectivityManager.TYPE_MOBILE) {
851 if (TextUtils.equals(feature, Phone.FEATURE_ENABLE_MMS)) {
852 usedNetworkType = ConnectivityManager.TYPE_MOBILE_MMS;
853 } else if (TextUtils.equals(feature, Phone.FEATURE_ENABLE_SUPL)) {
854 usedNetworkType = ConnectivityManager.TYPE_MOBILE_SUPL;
Robert Greenwalt19b9ab42011-01-10 11:58:31 -0800855 } else if (TextUtils.equals(feature, Phone.FEATURE_ENABLE_DUN) ||
856 TextUtils.equals(feature, Phone.FEATURE_ENABLE_DUN_ALWAYS)) {
Robert Greenwalt9c75d4a2009-09-27 17:27:04 -0700857 usedNetworkType = ConnectivityManager.TYPE_MOBILE_DUN;
858 } else if (TextUtils.equals(feature, Phone.FEATURE_ENABLE_HIPRI)) {
859 usedNetworkType = ConnectivityManager.TYPE_MOBILE_HIPRI;
860 }
861 }
862 tracker = mNetTrackers[usedNetworkType];
Robert Greenwalt5154ae762009-10-30 14:17:42 -0700863 if (tracker == null) {
Wink Savilleed9c02b2010-12-03 12:01:38 -0800864 if (DBG) log("ignoring - no known tracker for net type " + usedNetworkType);
Robert Greenwalt5154ae762009-10-30 14:17:42 -0700865 return -1;
866 }
867 if (usedNetworkType != networkType) {
Robert Greenwalt9c75d4a2009-09-27 17:27:04 -0700868 Integer currentPid = new Integer(pid);
Robert Greenwalt9c75d4a2009-09-27 17:27:04 -0700869 mNetRequestersPids[usedNetworkType].remove(currentPid);
Robert Greenwalt421c72b2009-12-17 14:54:59 -0800870 reassessPidDns(pid, true);
Robert Greenwalt9c75d4a2009-09-27 17:27:04 -0700871 if (mNetRequestersPids[usedNetworkType].size() != 0) {
Wink Savilleed9c02b2010-12-03 12:01:38 -0800872 if (DBG) log("not tearing down special network - " +
Robert Greenwalt9c75d4a2009-09-27 17:27:04 -0700873 "others still using it");
874 return 1;
875 }
876 callTeardown = true;
Robert Greenwalt19b9ab42011-01-10 11:58:31 -0800877 } else {
878 if (DBG) log("not a known feature - dropping");
Robert Greenwalt9c75d4a2009-09-27 17:27:04 -0700879 }
880 }
Wink Savilleed9c02b2010-12-03 12:01:38 -0800881 if (DBG) log("Doing network teardown");
Robert Greenwalt9c75d4a2009-09-27 17:27:04 -0700882 if (callTeardown) {
883 tracker.teardown();
Robert Greenwalt42acef32009-08-12 16:08:25 -0700884 return 1;
885 } else {
Robert Greenwalt02648a42010-05-18 10:52:51 -0700886 return -1;
Robert Greenwalt42acef32009-08-12 16:08:25 -0700887 }
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800888 }
889
890 /**
Robert Greenwalt585ac0f2010-08-27 09:24:29 -0700891 * @deprecated use requestRouteToHostAddress instead
892 *
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800893 * Ensure that a network route exists to deliver traffic to the specified
894 * host via the specified network interface.
Robert Greenwalt86e9e552009-07-16 17:21:39 -0700895 * @param networkType the type of the network over which traffic to the
896 * specified host is to be routed
897 * @param hostAddress the IP address of the host to which the route is
898 * desired
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800899 * @return {@code true} on success, {@code false} on failure
900 */
901 public boolean requestRouteToHost(int networkType, int hostAddress) {
Robert Greenwalt585ac0f2010-08-27 09:24:29 -0700902 InetAddress inetAddress = NetworkUtils.intToInetAddress(hostAddress);
903
904 if (inetAddress == null) {
905 return false;
906 }
907
908 return requestRouteToHostAddress(networkType, inetAddress.getAddress());
909 }
910
911 /**
912 * Ensure that a network route exists to deliver traffic to the specified
913 * host via the specified network interface.
914 * @param networkType the type of the network over which traffic to the
915 * specified host is to be routed
916 * @param hostAddress the IP address of the host to which the route is
917 * desired
918 * @return {@code true} on success, {@code false} on failure
919 */
920 public boolean requestRouteToHostAddress(int networkType, byte[] hostAddress) {
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800921 enforceChangePermission();
922 if (!ConnectivityManager.isNetworkTypeValid(networkType)) {
923 return false;
924 }
925 NetworkStateTracker tracker = mNetTrackers[networkType];
Robert Greenwalt8206ff32009-09-10 15:06:20 -0700926
Robert Greenwalt5154ae762009-10-30 14:17:42 -0700927 if (tracker == null || !tracker.getNetworkInfo().isConnected() ||
928 tracker.isTeardownRequested()) {
Robert Greenwalt8206ff32009-09-10 15:06:20 -0700929 if (DBG) {
Wink Savilleed9c02b2010-12-03 12:01:38 -0800930 log("requestRouteToHostAddress on down network " +
Robert Greenwalt585ac0f2010-08-27 09:24:29 -0700931 "(" + networkType + ") - dropped");
Robert Greenwalt8206ff32009-09-10 15:06:20 -0700932 }
933 return false;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800934 }
Robert Greenwalt47f69fe2010-06-15 15:43:39 -0700935 try {
Robert Greenwalt585ac0f2010-08-27 09:24:29 -0700936 InetAddress addr = InetAddress.getByAddress(hostAddress);
Robert Greenwalt47f69fe2010-06-15 15:43:39 -0700937 return addHostRoute(tracker, addr);
938 } catch (UnknownHostException e) {}
939 return false;
Irfan Sheriffd649c122010-06-09 15:39:36 -0700940 }
941
942 /**
943 * Ensure that a network route exists to deliver traffic to the specified
944 * host via the mobile data network.
945 * @param hostAddress the IP address of the host to which the route is desired,
946 * in network byte order.
Robert Greenwalt47f69fe2010-06-15 15:43:39 -0700947 * TODO - deprecate
Irfan Sheriffd649c122010-06-09 15:39:36 -0700948 * @return {@code true} on success, {@code false} on failure
949 */
Robert Greenwalt47f69fe2010-06-15 15:43:39 -0700950 private boolean addHostRoute(NetworkStateTracker nt, InetAddress hostAddress) {
Irfan Sheriffd649c122010-06-09 15:39:36 -0700951 if (nt.getNetworkInfo().getType() == ConnectivityManager.TYPE_WIFI) {
952 return false;
953 }
954
Robert Greenwalt37e65eb2010-08-30 10:56:47 -0700955 LinkProperties p = nt.getLinkProperties();
Robert Greenwalt47f69fe2010-06-15 15:43:39 -0700956 if (p == null) return false;
957 String interfaceName = p.getInterfaceName();
Irfan Sheriffd649c122010-06-09 15:39:36 -0700958
959 if (DBG) {
Wink Savilleed9c02b2010-12-03 12:01:38 -0800960 log("Requested host route to " + hostAddress + "(" + interfaceName + ")");
Irfan Sheriffd649c122010-06-09 15:39:36 -0700961 }
Robert Greenwalt47f69fe2010-06-15 15:43:39 -0700962 if (interfaceName != null) {
Robert Greenwalt585ac0f2010-08-27 09:24:29 -0700963 return NetworkUtils.addHostRoute(interfaceName, hostAddress, null);
Irfan Sheriffd649c122010-06-09 15:39:36 -0700964 } else {
Wink Savilleed9c02b2010-12-03 12:01:38 -0800965 if (DBG) loge("addHostRoute failed due to null interface name");
Irfan Sheriffd649c122010-06-09 15:39:36 -0700966 return false;
967 }
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800968 }
969
970 /**
971 * @see ConnectivityManager#getBackgroundDataSetting()
972 */
973 public boolean getBackgroundDataSetting() {
Robert Greenwaltd825ea42010-12-29 16:15:02 -0800974 return mBackgroundDataEnabled.get();
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800975 }
Robert Greenwalt86e9e552009-07-16 17:21:39 -0700976
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800977 /**
978 * @see ConnectivityManager#setBackgroundDataSetting(boolean)
979 */
980 public void setBackgroundDataSetting(boolean allowBackgroundDataUsage) {
981 mContext.enforceCallingOrSelfPermission(
982 android.Manifest.permission.CHANGE_BACKGROUND_DATA_SETTING,
983 "ConnectivityService");
Robert Greenwalt86e9e552009-07-16 17:21:39 -0700984
Robert Greenwaltd825ea42010-12-29 16:15:02 -0800985 mBackgroundDataEnabled.set(allowBackgroundDataUsage);
986
Robert Greenwalt8dcc28b2010-09-23 10:05:56 -0700987 mHandler.sendMessage(mHandler.obtainMessage(EVENT_SET_BACKGROUND_DATA,
988 (allowBackgroundDataUsage ? ENABLED : DISABLED), 0));
989 }
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800990
Robert Greenwalt8dcc28b2010-09-23 10:05:56 -0700991 private void handleSetBackgroundData(boolean enabled) {
Robert Greenwaltdb4afae2011-02-25 13:44:09 -0800992 Settings.Secure.putInt(mContext.getContentResolver(),
993 Settings.Secure.BACKGROUND_DATA, enabled ? 1 : 0);
994 Intent broadcast = new Intent(
995 ConnectivityManager.ACTION_BACKGROUND_DATA_SETTING_CHANGED);
996 mContext.sendBroadcast(broadcast);
Robert Greenwalt86e9e552009-07-16 17:21:39 -0700997 }
998
Robert Greenwaltc03fa502010-02-23 18:58:05 -0800999 /**
1000 * @see ConnectivityManager#getMobileDataEnabled()
1001 */
1002 public boolean getMobileDataEnabled() {
Wink Savillee7982682010-12-07 10:31:02 -08001003 // TODO: This detail should probably be in DataConnectionTracker's
1004 // which is where we store the value and maybe make this
1005 // asynchronous.
Robert Greenwaltc03fa502010-02-23 18:58:05 -08001006 enforceAccessPermission();
1007 boolean retVal = Settings.Secure.getInt(mContext.getContentResolver(),
1008 Settings.Secure.MOBILE_DATA, 1) == 1;
Wink Savilleed9c02b2010-12-03 12:01:38 -08001009 if (DBG) log("getMobileDataEnabled returning " + retVal);
Robert Greenwaltc03fa502010-02-23 18:58:05 -08001010 return retVal;
1011 }
1012
1013 /**
1014 * @see ConnectivityManager#setMobileDataEnabled(boolean)
1015 */
Robert Greenwalt8dcc28b2010-09-23 10:05:56 -07001016 public void setMobileDataEnabled(boolean enabled) {
Robert Greenwaltc03fa502010-02-23 18:58:05 -08001017 enforceChangePermission();
Wink Savilleed9c02b2010-12-03 12:01:38 -08001018 if (DBG) log("setMobileDataEnabled(" + enabled + ")");
Robert Greenwaltc03fa502010-02-23 18:58:05 -08001019
Robert Greenwalt8dcc28b2010-09-23 10:05:56 -07001020 mHandler.sendMessage(mHandler.obtainMessage(EVENT_SET_MOBILE_DATA,
1021 (enabled ? ENABLED : DISABLED), 0));
1022 }
1023
1024 private void handleSetMobileData(boolean enabled) {
Wink Savillee7982682010-12-07 10:31:02 -08001025 if (mNetTrackers[ConnectivityManager.TYPE_MOBILE] != null) {
1026 if (DBG) {
1027 Slog.d(TAG, mNetTrackers[ConnectivityManager.TYPE_MOBILE].toString() + enabled);
Robert Greenwaltc03fa502010-02-23 18:58:05 -08001028 }
Wink Savillee7982682010-12-07 10:31:02 -08001029 mNetTrackers[ConnectivityManager.TYPE_MOBILE].setDataEnable(enabled);
Robert Greenwaltc03fa502010-02-23 18:58:05 -08001030 }
1031 }
1032
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001033 private void enforceAccessPermission() {
Robert Greenwalt86e9e552009-07-16 17:21:39 -07001034 mContext.enforceCallingOrSelfPermission(
1035 android.Manifest.permission.ACCESS_NETWORK_STATE,
1036 "ConnectivityService");
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001037 }
1038
1039 private void enforceChangePermission() {
Robert Greenwalt86e9e552009-07-16 17:21:39 -07001040 mContext.enforceCallingOrSelfPermission(
1041 android.Manifest.permission.CHANGE_NETWORK_STATE,
1042 "ConnectivityService");
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001043 }
1044
Robert Greenwaltd0e18ff2010-01-26 11:40:34 -08001045 // TODO Make this a special check when it goes public
1046 private void enforceTetherChangePermission() {
1047 mContext.enforceCallingOrSelfPermission(
1048 android.Manifest.permission.CHANGE_NETWORK_STATE,
1049 "ConnectivityService");
1050 }
1051
Robert Greenwalt2a091d72010-02-11 18:18:40 -08001052 private void enforceTetherAccessPermission() {
1053 mContext.enforceCallingOrSelfPermission(
1054 android.Manifest.permission.ACCESS_NETWORK_STATE,
1055 "ConnectivityService");
1056 }
1057
Robert Greenwalt14f2ef42010-06-15 12:19:37 -07001058 private void enforceConnectivityInternalPermission() {
1059 mContext.enforceCallingOrSelfPermission(
1060 android.Manifest.permission.CONNECTIVITY_INTERNAL,
1061 "ConnectivityService");
1062 }
1063
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001064 /**
Robert Greenwalt86e9e552009-07-16 17:21:39 -07001065 * Handle a {@code DISCONNECTED} event. If this pertains to the non-active
1066 * network, we ignore it. If it is for the active network, we send out a
1067 * broadcast. But first, we check whether it might be possible to connect
1068 * to a different network.
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001069 * @param info the {@code NetworkInfo} for the network
1070 */
1071 private void handleDisconnect(NetworkInfo info) {
1072
Robert Greenwalt42acef32009-08-12 16:08:25 -07001073 int prevNetType = info.getType();
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001074
Robert Greenwalt42acef32009-08-12 16:08:25 -07001075 mNetTrackers[prevNetType].setTeardownRequested(false);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001076 /*
1077 * If the disconnected network is not the active one, then don't report
1078 * this as a loss of connectivity. What probably happened is that we're
1079 * getting the disconnect for a network that we explicitly disabled
1080 * in accordance with network preference policies.
1081 */
Robert Greenwalt42acef32009-08-12 16:08:25 -07001082 if (!mNetAttributes[prevNetType].isDefault()) {
1083 List pids = mNetRequestersPids[prevNetType];
1084 for (int i = 0; i<pids.size(); i++) {
1085 Integer pid = (Integer)pids.get(i);
1086 // will remove them because the net's no longer connected
1087 // need to do this now as only now do we know the pids and
1088 // can properly null things that are no longer referenced.
1089 reassessPidDns(pid.intValue(), false);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001090 }
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001091 }
1092
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001093 Intent intent = new Intent(ConnectivityManager.CONNECTIVITY_ACTION);
1094 intent.putExtra(ConnectivityManager.EXTRA_NETWORK_INFO, info);
1095 if (info.isFailover()) {
1096 intent.putExtra(ConnectivityManager.EXTRA_IS_FAILOVER, true);
1097 info.setFailover(false);
1098 }
1099 if (info.getReason() != null) {
1100 intent.putExtra(ConnectivityManager.EXTRA_REASON, info.getReason());
1101 }
1102 if (info.getExtraInfo() != null) {
Robert Greenwalt86e9e552009-07-16 17:21:39 -07001103 intent.putExtra(ConnectivityManager.EXTRA_EXTRA_INFO,
1104 info.getExtraInfo());
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001105 }
Robert Greenwalt42acef32009-08-12 16:08:25 -07001106
Robert Greenwaltcc4b4012010-01-25 17:54:29 -08001107 if (mNetAttributes[prevNetType].isDefault()) {
Robert Greenwaltf21ef7d2011-01-11 13:56:33 -08001108 tryFailover(prevNetType);
1109 if (mActiveDefaultNetwork != -1) {
1110 NetworkInfo switchTo = mNetTrackers[mActiveDefaultNetwork].getNetworkInfo();
Robert Greenwaltcc4b4012010-01-25 17:54:29 -08001111 intent.putExtra(ConnectivityManager.EXTRA_OTHER_NETWORK_INFO, switchTo);
1112 } else {
Robert Greenwalt029be812010-09-20 18:01:43 -07001113 mDefaultInetConditionPublished = 0; // we're not connected anymore
Robert Greenwaltcc4b4012010-01-25 17:54:29 -08001114 intent.putExtra(ConnectivityManager.EXTRA_NO_CONNECTIVITY, true);
1115 }
Robert Greenwaltda03c4e2010-01-20 19:29:41 -08001116 }
Robert Greenwalt029be812010-09-20 18:01:43 -07001117 intent.putExtra(ConnectivityManager.EXTRA_INET_CONDITION, mDefaultInetConditionPublished);
Robert Greenwaltda03c4e2010-01-20 19:29:41 -08001118 // do this before we broadcast the change
Robert Greenwaltc76b8fa2010-07-23 15:46:26 -07001119 handleConnectivityChange(prevNetType);
Robert Greenwaltda03c4e2010-01-20 19:29:41 -08001120
1121 sendStickyBroadcast(intent);
1122 /*
1123 * If the failover network is already connected, then immediately send
1124 * out a followup broadcast indicating successful failover
1125 */
Robert Greenwaltf21ef7d2011-01-11 13:56:33 -08001126 if (mActiveDefaultNetwork != -1) {
1127 sendConnectedBroadcast(mNetTrackers[mActiveDefaultNetwork].getNetworkInfo());
Robert Greenwaltda03c4e2010-01-20 19:29:41 -08001128 }
1129 }
1130
Robert Greenwaltf21ef7d2011-01-11 13:56:33 -08001131 private void tryFailover(int prevNetType) {
Robert Greenwalt42acef32009-08-12 16:08:25 -07001132 /*
Robert Greenwaltbff90182011-01-06 15:41:07 -08001133 * If this is a default network, check if other defaults are available.
1134 * Try to reconnect on all available and let them hash it out when
1135 * more than one connects.
Robert Greenwalt42acef32009-08-12 16:08:25 -07001136 */
Robert Greenwalt42acef32009-08-12 16:08:25 -07001137 if (mNetAttributes[prevNetType].isDefault()) {
Robert Greenwalt42acef32009-08-12 16:08:25 -07001138 if (mActiveDefaultNetwork == prevNetType) {
1139 mActiveDefaultNetwork = -1;
1140 }
1141
Robert Greenwaltf21ef7d2011-01-11 13:56:33 -08001142 // don't signal a reconnect for anything lower or equal priority than our
1143 // current connected default
1144 // TODO - don't filter by priority now - nice optimization but risky
1145// int currentPriority = -1;
1146// if (mActiveDefaultNetwork != -1) {
1147// currentPriority = mNetAttributes[mActiveDefaultNetwork].mPriority;
1148// }
Robert Greenwaltda03c4e2010-01-20 19:29:41 -08001149 for (int checkType=0; checkType <= ConnectivityManager.MAX_NETWORK_TYPE; checkType++) {
Robert Greenwalt5154ae762009-10-30 14:17:42 -07001150 if (checkType == prevNetType) continue;
1151 if (mNetAttributes[checkType] == null) continue;
Robert Greenwaltbff90182011-01-06 15:41:07 -08001152 if (!mNetAttributes[checkType].isDefault()) continue;
Wink Saville9f7a0b22011-01-26 15:43:49 -08001153
1154// Enabling the isAvailable() optimization caused mobile to not get
1155// selected if it was in the middle of error handling. Specifically
1156// a moble connection that took 30 seconds to complete the DEACTIVATE_DATA_CALL
1157// would not be available and we wouldn't get connected to anything.
1158// So removing the isAvailable() optimization below for now. TODO: This
1159// optimization should work and we need to investigate why it doesn't work.
1160// This could be related to how DEACTIVATE_DATA_CALL is reporting its
1161// complete before it is really complete.
1162// if (!mNetTrackers[checkType].isAvailable()) continue;
1163
Robert Greenwaltf21ef7d2011-01-11 13:56:33 -08001164// if (currentPriority >= mNetAttributes[checkType].mPriority) continue;
Robert Greenwalt42acef32009-08-12 16:08:25 -07001165
Robert Greenwaltf21ef7d2011-01-11 13:56:33 -08001166 NetworkStateTracker checkTracker = mNetTrackers[checkType];
1167 NetworkInfo checkInfo = checkTracker.getNetworkInfo();
1168 if (!checkInfo.isConnectedOrConnecting() || checkTracker.isTeardownRequested()) {
1169 checkInfo.setFailover(true);
1170 checkTracker.reconnect();
Robert Greenwalt42acef32009-08-12 16:08:25 -07001171 }
Robert Greenwaltf21ef7d2011-01-11 13:56:33 -08001172 if (DBG) log("Attempting to switch to " + checkInfo.getTypeName());
Robert Greenwalt42acef32009-08-12 16:08:25 -07001173 }
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001174 }
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001175 }
1176
1177 private void sendConnectedBroadcast(NetworkInfo info) {
Robert Greenwalt1e9aac22010-09-15 17:36:33 -07001178 sendGeneralBroadcast(info, ConnectivityManager.CONNECTIVITY_ACTION);
1179 }
1180
1181 private void sendInetConditionBroadcast(NetworkInfo info) {
1182 sendGeneralBroadcast(info, ConnectivityManager.INET_CONDITION_ACTION);
1183 }
1184
1185 private void sendGeneralBroadcast(NetworkInfo info, String bcastType) {
1186 Intent intent = new Intent(bcastType);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001187 intent.putExtra(ConnectivityManager.EXTRA_NETWORK_INFO, info);
1188 if (info.isFailover()) {
1189 intent.putExtra(ConnectivityManager.EXTRA_IS_FAILOVER, true);
1190 info.setFailover(false);
1191 }
1192 if (info.getReason() != null) {
1193 intent.putExtra(ConnectivityManager.EXTRA_REASON, info.getReason());
1194 }
1195 if (info.getExtraInfo() != null) {
Robert Greenwalt86e9e552009-07-16 17:21:39 -07001196 intent.putExtra(ConnectivityManager.EXTRA_EXTRA_INFO,
1197 info.getExtraInfo());
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001198 }
Robert Greenwaltd7085fc2010-09-08 15:24:47 -07001199 intent.putExtra(ConnectivityManager.EXTRA_INET_CONDITION, mDefaultInetConditionPublished);
Mike Lockwood0f79b542009-08-14 14:18:49 -04001200 sendStickyBroadcast(intent);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001201 }
1202
1203 /**
1204 * Called when an attempt to fail over to another network has failed.
1205 * @param info the {@link NetworkInfo} for the failed network
1206 */
1207 private void handleConnectionFailure(NetworkInfo info) {
1208 mNetTrackers[info.getType()].setTeardownRequested(false);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001209
Robert Greenwalt42acef32009-08-12 16:08:25 -07001210 String reason = info.getReason();
1211 String extraInfo = info.getExtraInfo();
Robert Greenwalt86e9e552009-07-16 17:21:39 -07001212
Robert Greenwalt572172b2010-10-08 16:35:52 -07001213 String reasonText;
1214 if (reason == null) {
1215 reasonText = ".";
1216 } else {
1217 reasonText = " (" + reason + ").";
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001218 }
Wink Savilleed9c02b2010-12-03 12:01:38 -08001219 loge("Attempt to connect to " + info.getTypeName() + " failed" + reasonText);
Robert Greenwalt42acef32009-08-12 16:08:25 -07001220
1221 Intent intent = new Intent(ConnectivityManager.CONNECTIVITY_ACTION);
1222 intent.putExtra(ConnectivityManager.EXTRA_NETWORK_INFO, info);
1223 if (getActiveNetworkInfo() == null) {
1224 intent.putExtra(ConnectivityManager.EXTRA_NO_CONNECTIVITY, true);
1225 }
1226 if (reason != null) {
1227 intent.putExtra(ConnectivityManager.EXTRA_REASON, reason);
1228 }
1229 if (extraInfo != null) {
1230 intent.putExtra(ConnectivityManager.EXTRA_EXTRA_INFO, extraInfo);
1231 }
1232 if (info.isFailover()) {
1233 intent.putExtra(ConnectivityManager.EXTRA_IS_FAILOVER, true);
1234 info.setFailover(false);
1235 }
Robert Greenwaltda03c4e2010-01-20 19:29:41 -08001236
Robert Greenwaltcc4b4012010-01-25 17:54:29 -08001237 if (mNetAttributes[info.getType()].isDefault()) {
Robert Greenwaltf21ef7d2011-01-11 13:56:33 -08001238 tryFailover(info.getType());
1239 if (mActiveDefaultNetwork != -1) {
1240 NetworkInfo switchTo = mNetTrackers[mActiveDefaultNetwork].getNetworkInfo();
Robert Greenwaltcc4b4012010-01-25 17:54:29 -08001241 intent.putExtra(ConnectivityManager.EXTRA_OTHER_NETWORK_INFO, switchTo);
1242 } else {
Robert Greenwalt029be812010-09-20 18:01:43 -07001243 mDefaultInetConditionPublished = 0;
Robert Greenwaltcc4b4012010-01-25 17:54:29 -08001244 intent.putExtra(ConnectivityManager.EXTRA_NO_CONNECTIVITY, true);
1245 }
Robert Greenwaltda03c4e2010-01-20 19:29:41 -08001246 }
Robert Greenwaltcc4b4012010-01-25 17:54:29 -08001247
Robert Greenwalt029be812010-09-20 18:01:43 -07001248 intent.putExtra(ConnectivityManager.EXTRA_INET_CONDITION, mDefaultInetConditionPublished);
Mike Lockwood0f79b542009-08-14 14:18:49 -04001249 sendStickyBroadcast(intent);
Robert Greenwaltda03c4e2010-01-20 19:29:41 -08001250 /*
1251 * If the failover network is already connected, then immediately send
1252 * out a followup broadcast indicating successful failover
1253 */
Robert Greenwaltf21ef7d2011-01-11 13:56:33 -08001254 if (mActiveDefaultNetwork != -1) {
1255 sendConnectedBroadcast(mNetTrackers[mActiveDefaultNetwork].getNetworkInfo());
Robert Greenwaltda03c4e2010-01-20 19:29:41 -08001256 }
Mike Lockwood0f79b542009-08-14 14:18:49 -04001257 }
1258
1259 private void sendStickyBroadcast(Intent intent) {
1260 synchronized(this) {
Dianne Hackborn1c633fc2009-12-08 19:45:14 -08001261 if (!mSystemReady) {
1262 mInitialBroadcast = new Intent(intent);
Mike Lockwood0f79b542009-08-14 14:18:49 -04001263 }
Dianne Hackborn1c633fc2009-12-08 19:45:14 -08001264 intent.addFlags(Intent.FLAG_RECEIVER_REGISTERED_ONLY_BEFORE_BOOT);
1265 mContext.sendStickyBroadcast(intent);
Mike Lockwood0f79b542009-08-14 14:18:49 -04001266 }
1267 }
1268
1269 void systemReady() {
1270 synchronized(this) {
1271 mSystemReady = true;
Dianne Hackborn1c633fc2009-12-08 19:45:14 -08001272 if (mInitialBroadcast != null) {
1273 mContext.sendStickyBroadcast(mInitialBroadcast);
1274 mInitialBroadcast = null;
Mike Lockwood0f79b542009-08-14 14:18:49 -04001275 }
1276 }
Robert Greenwalt434203a2010-10-11 16:00:27 -07001277 // load the global proxy at startup
1278 mHandler.sendMessage(mHandler.obtainMessage(EVENT_APPLY_GLOBAL_HTTP_PROXY));
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001279 }
1280
1281 private void handleConnect(NetworkInfo info) {
Robert Greenwalt42acef32009-08-12 16:08:25 -07001282 int type = info.getType();
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001283
1284 // snapshot isFailover, because sendConnectedBroadcast() resets it
1285 boolean isFailover = info.isFailover();
Robert Greenwalt42acef32009-08-12 16:08:25 -07001286 NetworkStateTracker thisNet = mNetTrackers[type];
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001287
Robert Greenwalt42acef32009-08-12 16:08:25 -07001288 // if this is a default net and other default is running
1289 // kill the one not preferred
1290 if (mNetAttributes[type].isDefault()) {
Robert Greenwalt42acef32009-08-12 16:08:25 -07001291 if (mActiveDefaultNetwork != -1 && mActiveDefaultNetwork != type) {
1292 if ((type != mNetworkPreference &&
1293 mNetAttributes[mActiveDefaultNetwork].mPriority >
1294 mNetAttributes[type].mPriority) ||
1295 mNetworkPreference == mActiveDefaultNetwork) {
1296 // don't accept this one
Wink Savilleed9c02b2010-12-03 12:01:38 -08001297 if (DBG) {
1298 log("Not broadcasting CONNECT_ACTION " +
Robert Greenwalt42acef32009-08-12 16:08:25 -07001299 "to torn down network " + info.getTypeName());
Wink Savilleed9c02b2010-12-03 12:01:38 -08001300 }
Robert Greenwalt42acef32009-08-12 16:08:25 -07001301 teardown(thisNet);
1302 return;
1303 } else {
1304 // tear down the other
1305 NetworkStateTracker otherNet =
1306 mNetTrackers[mActiveDefaultNetwork];
Wink Savilleed9c02b2010-12-03 12:01:38 -08001307 if (DBG) {
1308 log("Policy requires " + otherNet.getNetworkInfo().getTypeName() +
Robert Greenwalt42acef32009-08-12 16:08:25 -07001309 " teardown");
Wink Savilleed9c02b2010-12-03 12:01:38 -08001310 }
Robert Greenwalt42acef32009-08-12 16:08:25 -07001311 if (!teardown(otherNet)) {
Wink Savilleed9c02b2010-12-03 12:01:38 -08001312 loge("Network declined teardown request");
Robert Greenwalt42acef32009-08-12 16:08:25 -07001313 return;
1314 }
Robert Greenwalt14f2ef42010-06-15 12:19:37 -07001315 }
1316 }
1317 synchronized (ConnectivityService.this) {
1318 // have a new default network, release the transition wakelock in a second
1319 // if it's held. The second pause is to allow apps to reconnect over the
1320 // new network
1321 if (mNetTransitionWakeLock.isHeld()) {
1322 mHandler.sendMessageDelayed(mHandler.obtainMessage(
Robert Greenwaltf3331232010-09-24 14:32:21 -07001323 EVENT_CLEAR_NET_TRANSITION_WAKELOCK,
Robert Greenwalt14f2ef42010-06-15 12:19:37 -07001324 mNetTransitionWakeLockSerialNumber, 0),
1325 1000);
Robert Greenwalt42acef32009-08-12 16:08:25 -07001326 }
1327 }
1328 mActiveDefaultNetwork = type;
Robert Greenwaltd7085fc2010-09-08 15:24:47 -07001329 // this will cause us to come up initially as unconnected and switching
1330 // to connected after our normal pause unless somebody reports us as reall
1331 // disconnected
1332 mDefaultInetConditionPublished = 0;
1333 mDefaultConnectionSequence++;
1334 mInetConditionChangeInFlight = false;
1335 // Don't do this - if we never sign in stay, grey
1336 //reportNetworkCondition(mActiveDefaultNetwork, 100);
Robert Greenwalt42acef32009-08-12 16:08:25 -07001337 }
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001338 thisNet.setTeardownRequested(false);
Irfan Sheriffd649c122010-06-09 15:39:36 -07001339 updateNetworkSettings(thisNet);
Robert Greenwaltc76b8fa2010-07-23 15:46:26 -07001340 handleConnectivityChange(type);
Robert Greenwalt42acef32009-08-12 16:08:25 -07001341 sendConnectedBroadcast(info);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001342 }
1343
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001344 /**
Robert Greenwaltc76b8fa2010-07-23 15:46:26 -07001345 * After a change in the connectivity state of a network. We're mainly
1346 * concerned with making sure that the list of DNS servers is set up
1347 * according to which networks are connected, and ensuring that the
1348 * right routing table entries exist.
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001349 */
Robert Greenwaltc76b8fa2010-07-23 15:46:26 -07001350 private void handleConnectivityChange(int netType) {
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001351 /*
Robert Greenwalt42acef32009-08-12 16:08:25 -07001352 * If a non-default network is enabled, add the host routes that
Robert Greenwaltc76b8fa2010-07-23 15:46:26 -07001353 * will allow it's DNS servers to be accessed.
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001354 */
Robert Greenwaltc76b8fa2010-07-23 15:46:26 -07001355 handleDnsConfigurationChange(netType);
Robert Greenwalt42acef32009-08-12 16:08:25 -07001356
Robert Greenwaltc76b8fa2010-07-23 15:46:26 -07001357 if (mNetTrackers[netType].getNetworkInfo().isConnected()) {
1358 if (mNetAttributes[netType].isDefault()) {
Robert Greenwalt434203a2010-10-11 16:00:27 -07001359 handleApplyDefaultProxy(netType);
Robert Greenwaltc76b8fa2010-07-23 15:46:26 -07001360 addDefaultRoute(mNetTrackers[netType]);
Robert Greenwalt42acef32009-08-12 16:08:25 -07001361 } else {
Robert Greenwaltc76b8fa2010-07-23 15:46:26 -07001362 addPrivateDnsRoutes(mNetTrackers[netType]);
1363 }
1364 } else {
1365 if (mNetAttributes[netType].isDefault()) {
1366 removeDefaultRoute(mNetTrackers[netType]);
1367 } else {
1368 removePrivateDnsRoutes(mNetTrackers[netType]);
Robert Greenwalt42acef32009-08-12 16:08:25 -07001369 }
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001370 }
1371 }
1372
Irfan Sheriffd649c122010-06-09 15:39:36 -07001373 private void addPrivateDnsRoutes(NetworkStateTracker nt) {
Irfan Sheriffd649c122010-06-09 15:39:36 -07001374 boolean privateDnsRouteSet = nt.isPrivateDnsRouteSet();
Robert Greenwalt37e65eb2010-08-30 10:56:47 -07001375 LinkProperties p = nt.getLinkProperties();
Robert Greenwalt47f69fe2010-06-15 15:43:39 -07001376 if (p == null) return;
1377 String interfaceName = p.getInterfaceName();
Irfan Sheriffd649c122010-06-09 15:39:36 -07001378
1379 if (DBG) {
Wink Savilleed9c02b2010-12-03 12:01:38 -08001380 log("addPrivateDnsRoutes for " + nt +
Irfan Sheriffd649c122010-06-09 15:39:36 -07001381 "(" + interfaceName + ") - mPrivateDnsRouteSet = " + privateDnsRouteSet);
1382 }
Irfan Sheriffd649c122010-06-09 15:39:36 -07001383 if (interfaceName != null && !privateDnsRouteSet) {
Robert Greenwalt47f69fe2010-06-15 15:43:39 -07001384 Collection<InetAddress> dnsList = p.getDnses();
1385 for (InetAddress dns : dnsList) {
Wink Savilleed9c02b2010-12-03 12:01:38 -08001386 if (DBG) log(" adding " + dns);
Robert Greenwalt585ac0f2010-08-27 09:24:29 -07001387 NetworkUtils.addHostRoute(interfaceName, dns, null);
Irfan Sheriffd649c122010-06-09 15:39:36 -07001388 }
1389 nt.privateDnsRouteSet(true);
1390 }
1391 }
1392
1393 private void removePrivateDnsRoutes(NetworkStateTracker nt) {
1394 // TODO - we should do this explicitly but the NetUtils api doesnt
1395 // support this yet - must remove all. No worse than before
Robert Greenwalt37e65eb2010-08-30 10:56:47 -07001396 LinkProperties p = nt.getLinkProperties();
Robert Greenwalt47f69fe2010-06-15 15:43:39 -07001397 if (p == null) return;
1398 String interfaceName = p.getInterfaceName();
Irfan Sheriffd649c122010-06-09 15:39:36 -07001399 boolean privateDnsRouteSet = nt.isPrivateDnsRouteSet();
1400 if (interfaceName != null && privateDnsRouteSet) {
1401 if (DBG) {
Wink Savilleed9c02b2010-12-03 12:01:38 -08001402 log("removePrivateDnsRoutes for " + nt.getNetworkInfo().getTypeName() +
Irfan Sheriffd649c122010-06-09 15:39:36 -07001403 " (" + interfaceName + ")");
1404 }
1405 NetworkUtils.removeHostRoutes(interfaceName);
1406 nt.privateDnsRouteSet(false);
1407 }
1408 }
1409
Irfan Sheriffd649c122010-06-09 15:39:36 -07001410
1411 private void addDefaultRoute(NetworkStateTracker nt) {
Robert Greenwalt37e65eb2010-08-30 10:56:47 -07001412 LinkProperties p = nt.getLinkProperties();
Robert Greenwalt47f69fe2010-06-15 15:43:39 -07001413 if (p == null) return;
1414 String interfaceName = p.getInterfaceName();
Robert Greenwalt992564e2011-02-09 13:56:06 -08001415 if (TextUtils.isEmpty(interfaceName)) return;
1416 for (InetAddress gateway : p.getGateways()) {
Irfan Sheriffd649c122010-06-09 15:39:36 -07001417
Robert Greenwalt992564e2011-02-09 13:56:06 -08001418 if (!NetworkUtils.addDefaultRoute(interfaceName, gateway) && DBG) {
Robert Greenwalt47f69fe2010-06-15 15:43:39 -07001419 NetworkInfo networkInfo = nt.getNetworkInfo();
Wink Savilleed9c02b2010-12-03 12:01:38 -08001420 log("addDefaultRoute for " + networkInfo.getTypeName() +
Robert Greenwalt992564e2011-02-09 13:56:06 -08001421 " (" + interfaceName + "), GatewayAddr=" + gateway.getHostAddress());
Irfan Sheriffd649c122010-06-09 15:39:36 -07001422 }
Irfan Sheriffd649c122010-06-09 15:39:36 -07001423 }
1424 }
1425
1426
1427 public void removeDefaultRoute(NetworkStateTracker nt) {
Robert Greenwalt37e65eb2010-08-30 10:56:47 -07001428 LinkProperties p = nt.getLinkProperties();
Robert Greenwalt47f69fe2010-06-15 15:43:39 -07001429 if (p == null) return;
1430 String interfaceName = p.getInterfaceName();
Irfan Sheriffd649c122010-06-09 15:39:36 -07001431
Robert Greenwaltc76b8fa2010-07-23 15:46:26 -07001432 if (interfaceName != null) {
1433 if ((NetworkUtils.removeDefaultRoute(interfaceName) >= 0) && DBG) {
Robert Greenwalt47f69fe2010-06-15 15:43:39 -07001434 NetworkInfo networkInfo = nt.getNetworkInfo();
Wink Savilleed9c02b2010-12-03 12:01:38 -08001435 log("removeDefaultRoute for " + networkInfo.getTypeName() + " (" +
Irfan Sheriffd649c122010-06-09 15:39:36 -07001436 interfaceName + ")");
1437 }
Irfan Sheriffd649c122010-06-09 15:39:36 -07001438 }
1439 }
1440
1441 /**
1442 * Reads the network specific TCP buffer sizes from SystemProperties
1443 * net.tcp.buffersize.[default|wifi|umts|edge|gprs] and set them for system
1444 * wide use
1445 */
1446 public void updateNetworkSettings(NetworkStateTracker nt) {
1447 String key = nt.getTcpBufferSizesPropName();
1448 String bufferSizes = SystemProperties.get(key);
1449
1450 if (bufferSizes.length() == 0) {
Wink Savilleed9c02b2010-12-03 12:01:38 -08001451 loge(key + " not found in system properties. Using defaults");
Irfan Sheriffd649c122010-06-09 15:39:36 -07001452
1453 // Setting to default values so we won't be stuck to previous values
1454 key = "net.tcp.buffersize.default";
1455 bufferSizes = SystemProperties.get(key);
1456 }
1457
1458 // Set values in kernel
1459 if (bufferSizes.length() != 0) {
1460 if (DBG) {
Wink Savilleed9c02b2010-12-03 12:01:38 -08001461 log("Setting TCP values: [" + bufferSizes
Irfan Sheriffd649c122010-06-09 15:39:36 -07001462 + "] which comes from [" + key + "]");
1463 }
1464 setBufferSize(bufferSizes);
1465 }
1466 }
1467
1468 /**
1469 * Writes TCP buffer sizes to /sys/kernel/ipv4/tcp_[r/w]mem_[min/def/max]
1470 * which maps to /proc/sys/net/ipv4/tcp_rmem and tcpwmem
1471 *
1472 * @param bufferSizes in the format of "readMin, readInitial, readMax,
1473 * writeMin, writeInitial, writeMax"
1474 */
1475 private void setBufferSize(String bufferSizes) {
1476 try {
1477 String[] values = bufferSizes.split(",");
1478
1479 if (values.length == 6) {
1480 final String prefix = "/sys/kernel/ipv4/tcp_";
1481 stringToFile(prefix + "rmem_min", values[0]);
1482 stringToFile(prefix + "rmem_def", values[1]);
1483 stringToFile(prefix + "rmem_max", values[2]);
1484 stringToFile(prefix + "wmem_min", values[3]);
1485 stringToFile(prefix + "wmem_def", values[4]);
1486 stringToFile(prefix + "wmem_max", values[5]);
1487 } else {
Wink Savilleed9c02b2010-12-03 12:01:38 -08001488 loge("Invalid buffersize string: " + bufferSizes);
Irfan Sheriffd649c122010-06-09 15:39:36 -07001489 }
1490 } catch (IOException e) {
Wink Savilleed9c02b2010-12-03 12:01:38 -08001491 loge("Can't set tcp buffer sizes:" + e);
Irfan Sheriffd649c122010-06-09 15:39:36 -07001492 }
1493 }
1494
1495 /**
1496 * Writes string to file. Basically same as "echo -n $string > $filename"
1497 *
1498 * @param filename
1499 * @param string
1500 * @throws IOException
1501 */
1502 private void stringToFile(String filename, String string) throws IOException {
1503 FileWriter out = new FileWriter(filename);
1504 try {
1505 out.write(string);
1506 } finally {
1507 out.close();
1508 }
1509 }
1510
1511
Robert Greenwalt42acef32009-08-12 16:08:25 -07001512 /**
1513 * Adjust the per-process dns entries (net.dns<x>.<pid>) based
1514 * on the highest priority active net which this process requested.
1515 * If there aren't any, clear it out
1516 */
1517 private void reassessPidDns(int myPid, boolean doBump)
1518 {
Wink Savilleed9c02b2010-12-03 12:01:38 -08001519 if (DBG) log("reassessPidDns for pid " + myPid);
Robert Greenwalt42acef32009-08-12 16:08:25 -07001520 for(int i : mPriorityList) {
1521 if (mNetAttributes[i].isDefault()) {
1522 continue;
1523 }
1524 NetworkStateTracker nt = mNetTrackers[i];
Robert Greenwalt86e9e552009-07-16 17:21:39 -07001525 if (nt.getNetworkInfo().isConnected() &&
1526 !nt.isTeardownRequested()) {
Robert Greenwalt37e65eb2010-08-30 10:56:47 -07001527 LinkProperties p = nt.getLinkProperties();
Robert Greenwalt47f69fe2010-06-15 15:43:39 -07001528 if (p == null) continue;
Robert Greenwalt42acef32009-08-12 16:08:25 -07001529 List pids = mNetRequestersPids[i];
1530 for (int j=0; j<pids.size(); j++) {
1531 Integer pid = (Integer)pids.get(j);
1532 if (pid.intValue() == myPid) {
Robert Greenwalt47f69fe2010-06-15 15:43:39 -07001533 Collection<InetAddress> dnses = p.getDnses();
1534 writePidDns(dnses, myPid);
Robert Greenwalt42acef32009-08-12 16:08:25 -07001535 if (doBump) {
1536 bumpDns();
1537 }
1538 return;
1539 }
1540 }
1541 }
1542 }
1543 // nothing found - delete
1544 for (int i = 1; ; i++) {
1545 String prop = "net.dns" + i + "." + myPid;
1546 if (SystemProperties.get(prop).length() == 0) {
1547 if (doBump) {
1548 bumpDns();
1549 }
1550 return;
1551 }
1552 SystemProperties.set(prop, "");
1553 }
1554 }
1555
Robert Greenwalt10398722010-12-17 15:20:36 -08001556 // return true if results in a change
1557 private boolean writePidDns(Collection <InetAddress> dnses, int pid) {
Robert Greenwalt42acef32009-08-12 16:08:25 -07001558 int j = 1;
Robert Greenwalt10398722010-12-17 15:20:36 -08001559 boolean changed = false;
Robert Greenwalt47f69fe2010-06-15 15:43:39 -07001560 for (InetAddress dns : dnses) {
Robert Greenwalt10398722010-12-17 15:20:36 -08001561 String dnsString = dns.getHostAddress();
1562 if (changed || !dnsString.equals(SystemProperties.get("net.dns" + j + "." + pid))) {
1563 changed = true;
1564 SystemProperties.set("net.dns" + j++ + "." + pid, dns.getHostAddress());
1565 }
Robert Greenwalt42acef32009-08-12 16:08:25 -07001566 }
Robert Greenwalt10398722010-12-17 15:20:36 -08001567 return changed;
Robert Greenwalt42acef32009-08-12 16:08:25 -07001568 }
1569
1570 private void bumpDns() {
1571 /*
1572 * Bump the property that tells the name resolver library to reread
1573 * the DNS server list from the properties.
1574 */
1575 String propVal = SystemProperties.get("net.dnschange");
1576 int n = 0;
1577 if (propVal.length() != 0) {
1578 try {
1579 n = Integer.parseInt(propVal);
1580 } catch (NumberFormatException e) {}
1581 }
1582 SystemProperties.set("net.dnschange", "" + (n+1));
Robert Greenwalt03595d02010-11-02 14:08:23 -07001583 /*
1584 * Tell the VMs to toss their DNS caches
1585 */
1586 Intent intent = new Intent(Intent.ACTION_CLEAR_DNS_CACHE);
1587 intent.addFlags(Intent.FLAG_RECEIVER_REPLACE_PENDING);
Stan Chesnutt3d1db862011-01-05 17:14:03 -08001588 /*
1589 * Connectivity events can happen before boot has completed ...
1590 */
1591 intent.addFlags(Intent.FLAG_RECEIVER_REGISTERED_ONLY_BEFORE_BOOT);
Robert Greenwalt03595d02010-11-02 14:08:23 -07001592 mContext.sendBroadcast(intent);
Robert Greenwalt42acef32009-08-12 16:08:25 -07001593 }
1594
Robert Greenwaltc76b8fa2010-07-23 15:46:26 -07001595 private void handleDnsConfigurationChange(int netType) {
Robert Greenwalt42acef32009-08-12 16:08:25 -07001596 // add default net's dns entries
Robert Greenwaltc76b8fa2010-07-23 15:46:26 -07001597 NetworkStateTracker nt = mNetTrackers[netType];
1598 if (nt != null && nt.getNetworkInfo().isConnected() && !nt.isTeardownRequested()) {
Robert Greenwalt37e65eb2010-08-30 10:56:47 -07001599 LinkProperties p = nt.getLinkProperties();
Robert Greenwaltc76b8fa2010-07-23 15:46:26 -07001600 if (p == null) return;
1601 Collection<InetAddress> dnses = p.getDnses();
Robert Greenwalt10398722010-12-17 15:20:36 -08001602 boolean changed = false;
Robert Greenwaltc76b8fa2010-07-23 15:46:26 -07001603 if (mNetAttributes[netType].isDefault()) {
1604 int j = 1;
Robert Greenwalte90aa5e2010-09-01 11:34:05 -07001605 if (dnses.size() == 0 && mDefaultDns != null) {
Robert Greenwalt10398722010-12-17 15:20:36 -08001606 String dnsString = mDefaultDns.getHostAddress();
1607 if (!dnsString.equals(SystemProperties.get("net.dns1"))) {
1608 if (DBG) {
1609 log("no dns provided - using " + dnsString);
1610 }
1611 changed = true;
1612 SystemProperties.set("net.dns1", dnsString);
Robert Greenwalt42acef32009-08-12 16:08:25 -07001613 }
Robert Greenwalte90aa5e2010-09-01 11:34:05 -07001614 j++;
1615 } else {
1616 for (InetAddress dns : dnses) {
Robert Greenwalt10398722010-12-17 15:20:36 -08001617 String dnsString = dns.getHostAddress();
1618 if (!changed && dnsString.equals(SystemProperties.get("net.dns" + j))) {
1619 j++;
1620 continue;
1621 }
Robert Greenwalte90aa5e2010-09-01 11:34:05 -07001622 if (DBG) {
Wink Savilleed9c02b2010-12-03 12:01:38 -08001623 log("adding dns " + dns + " for " +
Robert Greenwalte90aa5e2010-09-01 11:34:05 -07001624 nt.getNetworkInfo().getTypeName());
1625 }
Robert Greenwalt10398722010-12-17 15:20:36 -08001626 changed = true;
1627 SystemProperties.set("net.dns" + j++, dnsString);
Robert Greenwalte90aa5e2010-09-01 11:34:05 -07001628 }
Robert Greenwaltc76b8fa2010-07-23 15:46:26 -07001629 }
1630 for (int k=j ; k<mNumDnsEntries; k++) {
Robert Greenwalt10398722010-12-17 15:20:36 -08001631 if (changed || !TextUtils.isEmpty(SystemProperties.get("net.dns" + k))) {
1632 if (DBG) log("erasing net.dns" + k);
1633 changed = true;
1634 SystemProperties.set("net.dns" + k, "");
1635 }
Robert Greenwaltc76b8fa2010-07-23 15:46:26 -07001636 }
1637 mNumDnsEntries = j;
1638 } else {
1639 // set per-pid dns for attached secondary nets
1640 List pids = mNetRequestersPids[netType];
1641 for (int y=0; y< pids.size(); y++) {
1642 Integer pid = (Integer)pids.get(y);
Robert Greenwalt10398722010-12-17 15:20:36 -08001643 changed = writePidDns(dnses, pid.intValue());
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001644 }
1645 }
Robert Greenwalt10398722010-12-17 15:20:36 -08001646 if (changed) bumpDns();
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001647 }
Robert Greenwalt42acef32009-08-12 16:08:25 -07001648 }
1649
1650 private int getRestoreDefaultNetworkDelay() {
1651 String restoreDefaultNetworkDelayStr = SystemProperties.get(
1652 NETWORK_RESTORE_DELAY_PROP_NAME);
1653 if(restoreDefaultNetworkDelayStr != null &&
1654 restoreDefaultNetworkDelayStr.length() != 0) {
1655 try {
1656 return Integer.valueOf(restoreDefaultNetworkDelayStr);
1657 } catch (NumberFormatException e) {
1658 }
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001659 }
Robert Greenwalt42acef32009-08-12 16:08:25 -07001660 return RESTORE_DEFAULT_NETWORK_DELAY;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001661 }
1662
1663 @Override
1664 protected void dump(FileDescriptor fd, PrintWriter pw, String[] args) {
Robert Greenwalt86e9e552009-07-16 17:21:39 -07001665 if (mContext.checkCallingOrSelfPermission(
1666 android.Manifest.permission.DUMP)
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001667 != PackageManager.PERMISSION_GRANTED) {
Robert Greenwalt86e9e552009-07-16 17:21:39 -07001668 pw.println("Permission Denial: can't dump ConnectivityService " +
1669 "from from pid=" + Binder.getCallingPid() + ", uid=" +
1670 Binder.getCallingUid());
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001671 return;
1672 }
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001673 pw.println();
1674 for (NetworkStateTracker nst : mNetTrackers) {
Robert Greenwaltb9285352009-12-21 18:24:07 -08001675 if (nst != null) {
1676 if (nst.getNetworkInfo().isConnected()) {
1677 pw.println("Active network: " + nst.getNetworkInfo().
1678 getTypeName());
1679 }
1680 pw.println(nst.getNetworkInfo());
1681 pw.println(nst);
1682 pw.println();
Robert Greenwalt42acef32009-08-12 16:08:25 -07001683 }
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001684 }
Robert Greenwaltb9285352009-12-21 18:24:07 -08001685
1686 pw.println("Network Requester Pids:");
1687 for (int net : mPriorityList) {
1688 String pidString = net + ": ";
1689 for (Object pid : mNetRequestersPids[net]) {
1690 pidString = pidString + pid.toString() + ", ";
1691 }
1692 pw.println(pidString);
1693 }
1694 pw.println();
1695
1696 pw.println("FeatureUsers:");
1697 for (Object requester : mFeatureUsers) {
1698 pw.println(requester.toString());
1699 }
1700 pw.println();
Robert Greenwalt2a091d72010-02-11 18:18:40 -08001701
Robert Greenwalt14f2ef42010-06-15 12:19:37 -07001702 synchronized (this) {
1703 pw.println("NetworkTranstionWakeLock is currently " +
1704 (mNetTransitionWakeLock.isHeld() ? "" : "not ") + "held.");
1705 pw.println("It was last requested for "+mNetTransitionWakeLockCausedBy);
1706 }
1707 pw.println();
1708
Robert Greenwalt2a091d72010-02-11 18:18:40 -08001709 mTethering.dump(fd, pw, args);
Robert Greenwalt4e8dfef2010-09-20 14:35:25 -07001710
1711 if (mInetLog != null) {
1712 pw.println();
1713 pw.println("Inet condition reports:");
1714 for(int i = 0; i < mInetLog.size(); i++) {
1715 pw.println(mInetLog.get(i));
1716 }
1717 }
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001718 }
1719
Robert Greenwalt42acef32009-08-12 16:08:25 -07001720 // must be stateless - things change under us.
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001721 private class MyHandler extends Handler {
Wink Savillebb08caf2010-09-02 19:23:52 -07001722 public MyHandler(Looper looper) {
1723 super(looper);
1724 }
1725
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001726 @Override
1727 public void handleMessage(Message msg) {
1728 NetworkInfo info;
1729 switch (msg.what) {
1730 case NetworkStateTracker.EVENT_STATE_CHANGED:
1731 info = (NetworkInfo) msg.obj;
Robert Greenwalt511288a2009-12-07 11:33:18 -08001732 int type = info.getType();
1733 NetworkInfo.State state = info.getState();
Robert Greenwalt6e6dec22010-01-25 16:14:00 -08001734 // only do this optimization for wifi. It going into scan mode for location
1735 // services generates alot of noise. Meanwhile the mms apn won't send out
1736 // subsequent notifications when on default cellular because it never
1737 // disconnects.. so only do this to wifi notifications. Fixed better when the
1738 // APN notifications are standardized.
1739 if (mNetAttributes[type].mLastState == state &&
1740 mNetAttributes[type].mRadio == ConnectivityManager.TYPE_WIFI) {
Robert Greenwalt511288a2009-12-07 11:33:18 -08001741 if (DBG) {
Robert Greenwalt6e6dec22010-01-25 16:14:00 -08001742 // TODO - remove this after we validate the dropping doesn't break
1743 // anything
Wink Savilleed9c02b2010-12-03 12:01:38 -08001744 log("Dropping ConnectivityChange for " +
Robert Greenwalt1193ae42010-01-13 09:36:31 -08001745 info.getTypeName() + ": " +
Robert Greenwalt511288a2009-12-07 11:33:18 -08001746 state + "/" + info.getDetailedState());
1747 }
1748 return;
1749 }
1750 mNetAttributes[type].mLastState = state;
1751
Wink Savilleed9c02b2010-12-03 12:01:38 -08001752 if (DBG) log("ConnectivityChange for " +
Robert Greenwalt86e9e552009-07-16 17:21:39 -07001753 info.getTypeName() + ": " +
Robert Greenwalt511288a2009-12-07 11:33:18 -08001754 state + "/" + info.getDetailedState());
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001755
1756 // Connectivity state changed:
1757 // [31-13] Reserved for future use
Robert Greenwalt86e9e552009-07-16 17:21:39 -07001758 // [12-9] Network subtype (for mobile network, as defined
1759 // by TelephonyManager)
1760 // [8-3] Detailed state ordinal (as defined by
1761 // NetworkInfo.DetailedState)
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001762 // [2-0] Network type (as defined by ConnectivityManager)
1763 int eventLogParam = (info.getType() & 0x7) |
1764 ((info.getDetailedState().ordinal() & 0x3f) << 3) |
1765 (info.getSubtype() << 9);
Doug Zongkerab5c49c2009-12-04 10:31:43 -08001766 EventLog.writeEvent(EventLogTags.CONNECTIVITY_STATE_CHANGED,
Robert Greenwalt86e9e552009-07-16 17:21:39 -07001767 eventLogParam);
1768
1769 if (info.getDetailedState() ==
1770 NetworkInfo.DetailedState.FAILED) {
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001771 handleConnectionFailure(info);
Robert Greenwalt511288a2009-12-07 11:33:18 -08001772 } else if (state == NetworkInfo.State.DISCONNECTED) {
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001773 handleDisconnect(info);
Robert Greenwalt511288a2009-12-07 11:33:18 -08001774 } else if (state == NetworkInfo.State.SUSPENDED) {
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001775 // TODO: need to think this over.
Robert Greenwalt86e9e552009-07-16 17:21:39 -07001776 // the logic here is, handle SUSPENDED the same as
1777 // DISCONNECTED. The only difference being we are
1778 // broadcasting an intent with NetworkInfo that's
1779 // suspended. This allows the applications an
1780 // opportunity to handle DISCONNECTED and SUSPENDED
1781 // differently, or not.
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001782 handleDisconnect(info);
Robert Greenwalt511288a2009-12-07 11:33:18 -08001783 } else if (state == NetworkInfo.State.CONNECTED) {
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001784 handleConnect(info);
1785 }
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001786 break;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001787 case NetworkStateTracker.EVENT_CONFIGURATION_CHANGED:
Robert Greenwaltc76b8fa2010-07-23 15:46:26 -07001788 info = (NetworkInfo) msg.obj;
1789 type = info.getType();
Robert Greenwalt434203a2010-10-11 16:00:27 -07001790 handleConnectivityChange(type);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001791 break;
Robert Greenwaltf3331232010-09-24 14:32:21 -07001792 case EVENT_CLEAR_NET_TRANSITION_WAKELOCK:
Robert Greenwalt14f2ef42010-06-15 12:19:37 -07001793 String causedBy = null;
1794 synchronized (ConnectivityService.this) {
1795 if (msg.arg1 == mNetTransitionWakeLockSerialNumber &&
1796 mNetTransitionWakeLock.isHeld()) {
1797 mNetTransitionWakeLock.release();
1798 causedBy = mNetTransitionWakeLockCausedBy;
1799 }
1800 }
1801 if (causedBy != null) {
Wink Savilleed9c02b2010-12-03 12:01:38 -08001802 log("NetTransition Wakelock for " + causedBy + " released by timeout");
Robert Greenwalt14f2ef42010-06-15 12:19:37 -07001803 }
Robert Greenwalt057d5e92010-09-09 14:05:10 -07001804 break;
Robert Greenwalt8dcc28b2010-09-23 10:05:56 -07001805 case EVENT_RESTORE_DEFAULT_NETWORK:
Robert Greenwalt9c75d4a2009-09-27 17:27:04 -07001806 FeatureUser u = (FeatureUser)msg.obj;
1807 u.expire();
Robert Greenwaltd7085fc2010-09-08 15:24:47 -07001808 break;
Robert Greenwalt8dcc28b2010-09-23 10:05:56 -07001809 case EVENT_INET_CONDITION_CHANGE:
1810 {
1811 int netType = msg.arg1;
1812 int condition = msg.arg2;
1813 handleInetConditionChange(netType, condition);
Robert Greenwalt14f2ef42010-06-15 12:19:37 -07001814 break;
Robert Greenwalt8dcc28b2010-09-23 10:05:56 -07001815 }
1816 case EVENT_INET_CONDITION_HOLD_END:
1817 {
1818 int netType = msg.arg1;
1819 int sequence = msg.arg2;
1820 handleInetConditionHoldEnd(netType, sequence);
Robert Greenwaltd7085fc2010-09-08 15:24:47 -07001821 break;
Robert Greenwalt8dcc28b2010-09-23 10:05:56 -07001822 }
1823 case EVENT_SET_NETWORK_PREFERENCE:
1824 {
1825 int preference = msg.arg1;
1826 handleSetNetworkPreference(preference);
1827 break;
1828 }
1829 case EVENT_SET_BACKGROUND_DATA:
1830 {
1831 boolean enabled = (msg.arg1 == ENABLED);
1832 handleSetBackgroundData(enabled);
1833 break;
1834 }
1835 case EVENT_SET_MOBILE_DATA:
1836 {
1837 boolean enabled = (msg.arg1 == ENABLED);
1838 handleSetMobileData(enabled);
1839 break;
1840 }
Robert Greenwalt434203a2010-10-11 16:00:27 -07001841 case EVENT_APPLY_GLOBAL_HTTP_PROXY:
1842 {
1843 handleDeprecatedGlobalHttpProxy();
1844 }
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 }
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002177}