blob: b1552a8ea0e05ceb7b03335e26c7705941ed756d [file] [log] [blame]
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001/*
2 * Copyright (C) 2008 The Android Open Source Project
3 *
4 * Licensed under the Apache License, Version 2.0 (the "License");
5 * you may not use this file except in compliance with the License.
6 * You may obtain a copy of the License at
7 *
8 * http://www.apache.org/licenses/LICENSE-2.0
9 *
10 * Unless required by applicable law or agreed to in writing, software
11 * distributed under the License is distributed on an "AS IS" BASIS,
12 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13 * See the License for the specific language governing permissions and
14 * limitations under the License.
15 */
16
17package com.android.server;
18
Jaikumar Ganesh15c74392010-12-21 22:31:44 -080019import android.bluetooth.BluetoothTetheringDataTracker;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080020import android.content.ContentResolver;
21import android.content.Context;
22import android.content.Intent;
23import android.content.pm.PackageManager;
Robert Greenwalt434203a2010-10-11 16:00:27 -070024import android.database.ContentObserver;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080025import android.net.ConnectivityManager;
Robert Greenwaltda3d5e62010-12-06 13:56:24 -080026import android.net.DummyDataStateTracker;
Benoit Goby08c39c62010-12-22 14:29:40 -080027import android.net.EthernetDataTracker;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080028import android.net.IConnectivityManager;
Jaikumar Ganesh15c74392010-12-21 22:31:44 -080029import android.net.LinkProperties;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080030import android.net.MobileDataStateTracker;
31import android.net.NetworkInfo;
32import android.net.NetworkStateTracker;
Robert Greenwalt585ac0f2010-08-27 09:24:29 -070033import android.net.NetworkUtils;
Robert Greenwalt434203a2010-10-11 16:00:27 -070034import android.net.Proxy;
35import android.net.ProxyProperties;
Hung-ying Tyan6b818de2011-01-19 16:48:38 +080036import android.net.vpn.VpnManager;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080037import android.net.wifi.WifiStateTracker;
38import android.os.Binder;
39import android.os.Handler;
Wink Savillebb08caf2010-09-02 19:23:52 -070040import android.os.HandlerThread;
Robert Greenwalt42acef32009-08-12 16:08:25 -070041import android.os.IBinder;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080042import android.os.Looper;
43import android.os.Message;
Robert Greenwalt14f2ef42010-06-15 12:19:37 -070044import android.os.PowerManager;
Robert Greenwalt42acef32009-08-12 16:08:25 -070045import android.os.RemoteException;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080046import android.os.ServiceManager;
47import android.os.SystemProperties;
48import android.provider.Settings;
Robert Greenwalt42acef32009-08-12 16:08:25 -070049import android.text.TextUtils;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080050import android.util.EventLog;
Joe Onorato8a9b2202010-02-26 18:56:32 -080051import android.util.Slog;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080052
Robert Greenwalt42acef32009-08-12 16:08:25 -070053import com.android.internal.telephony.Phone;
Robert Greenwaltd0e18ff2010-01-26 11:40:34 -080054import com.android.server.connectivity.Tethering;
55
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080056import java.io.FileDescriptor;
Irfan Sheriffd649c122010-06-09 15:39:36 -070057import java.io.FileWriter;
58import java.io.IOException;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080059import java.io.PrintWriter;
Robert Greenwalt47f69fe2010-06-15 15:43:39 -070060import java.net.InetAddress;
61import java.net.UnknownHostException;
Robert Greenwalt42acef32009-08-12 16:08:25 -070062import java.util.ArrayList;
Robert Greenwalt47f69fe2010-06-15 15:43:39 -070063import java.util.Collection;
Robert Greenwaltd825ea42010-12-29 16:15:02 -080064import java.util.concurrent.atomic.AtomicBoolean;
Robert Greenwalt4e8dfef2010-09-20 14:35:25 -070065import java.util.GregorianCalendar;
Robert Greenwalt42acef32009-08-12 16:08:25 -070066import java.util.List;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080067
68/**
69 * @hide
70 */
71public class ConnectivityService extends IConnectivityManager.Stub {
72
Robert Greenwaltba175a52010-10-05 19:12:26 -070073 private static final boolean DBG = true;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080074 private static final String TAG = "ConnectivityService";
75
Robert Greenwalt42acef32009-08-12 16:08:25 -070076 // how long to wait before switching back to a radio's default network
77 private static final int RESTORE_DEFAULT_NETWORK_DELAY = 1 * 60 * 1000;
78 // system property that can override the above value
79 private static final String NETWORK_RESTORE_DELAY_PROP_NAME =
80 "android.telephony.apn-restore";
81
Robert Greenwaltd0e18ff2010-01-26 11:40:34 -080082 private Tethering mTethering;
Robert Greenwaltc9d5fb72010-02-25 12:29:30 -080083 private boolean mTetheringConfigValid = false;
Robert Greenwaltd0e18ff2010-01-26 11:40:34 -080084
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080085 /**
86 * Sometimes we want to refer to the individual network state
87 * trackers separately, and sometimes we just want to treat them
88 * abstractly.
89 */
90 private NetworkStateTracker mNetTrackers[];
Robert Greenwalt42acef32009-08-12 16:08:25 -070091
92 /**
93 * A per Net list of the PID's that requested access to the net
94 * used both as a refcount and for per-PID DNS selection
95 */
96 private List mNetRequestersPids[];
97
Irfan Sheriffa2a1b912010-06-07 09:03:04 -070098 private WifiWatchdogService mWifiWatchdogService;
99
Robert Greenwalt42acef32009-08-12 16:08:25 -0700100 // priority order of the nettrackers
101 // (excluding dynamically set mNetworkPreference)
102 // TODO - move mNetworkTypePreference into this
103 private int[] mPriorityList;
104
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800105 private Context mContext;
106 private int mNetworkPreference;
Robert Greenwalt42acef32009-08-12 16:08:25 -0700107 private int mActiveDefaultNetwork = -1;
Robert Greenwaltd7085fc2010-09-08 15:24:47 -0700108 // 0 is full bad, 100 is full good
109 private int mDefaultInetCondition = 0;
110 private int mDefaultInetConditionPublished = 0;
111 private boolean mInetConditionChangeInFlight = false;
112 private int mDefaultConnectionSequence = 0;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800113
114 private int mNumDnsEntries;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800115
116 private boolean mTestMode;
Joe Onorato00092872010-09-01 21:18:22 -0700117 private static ConnectivityService sServiceInstance;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800118
Robert Greenwaltd825ea42010-12-29 16:15:02 -0800119 private AtomicBoolean mBackgroundDataEnabled = new AtomicBoolean(true);
120
Robert Greenwalt8dcc28b2010-09-23 10:05:56 -0700121 private static final int ENABLED = 1;
122 private static final int DISABLED = 0;
123
124 // Share the event space with NetworkStateTracker (which can't see this
125 // internal class but sends us events). If you change these, change
126 // NetworkStateTracker.java too.
127 private static final int MIN_NETWORK_STATE_TRACKER_EVENT = 1;
128 private static final int MAX_NETWORK_STATE_TRACKER_EVENT = 100;
129
130 /**
131 * used internally as a delayed event to make us switch back to the
132 * default network
133 */
134 private static final int EVENT_RESTORE_DEFAULT_NETWORK =
135 MAX_NETWORK_STATE_TRACKER_EVENT + 1;
136
137 /**
138 * used internally to change our mobile data enabled flag
139 */
140 private static final int EVENT_CHANGE_MOBILE_DATA_ENABLED =
141 MAX_NETWORK_STATE_TRACKER_EVENT + 2;
142
143 /**
144 * used internally to change our network preference setting
145 * arg1 = networkType to prefer
146 */
147 private static final int EVENT_SET_NETWORK_PREFERENCE =
148 MAX_NETWORK_STATE_TRACKER_EVENT + 3;
149
150 /**
151 * used internally to synchronize inet condition reports
152 * arg1 = networkType
153 * arg2 = condition (0 bad, 100 good)
154 */
155 private static final int EVENT_INET_CONDITION_CHANGE =
156 MAX_NETWORK_STATE_TRACKER_EVENT + 4;
157
158 /**
159 * used internally to mark the end of inet condition hold periods
160 * arg1 = networkType
161 */
162 private static final int EVENT_INET_CONDITION_HOLD_END =
163 MAX_NETWORK_STATE_TRACKER_EVENT + 5;
164
165 /**
166 * used internally to set the background data preference
167 * arg1 = TRUE for enabled, FALSE for disabled
168 */
169 private static final int EVENT_SET_BACKGROUND_DATA =
170 MAX_NETWORK_STATE_TRACKER_EVENT + 6;
171
172 /**
173 * used internally to set enable/disable cellular data
174 * arg1 = ENBALED or DISABLED
175 */
176 private static final int EVENT_SET_MOBILE_DATA =
177 MAX_NETWORK_STATE_TRACKER_EVENT + 7;
178
Robert Greenwaltf3331232010-09-24 14:32:21 -0700179 /**
180 * used internally to clear a wakelock when transitioning
181 * from one net to another
182 */
183 private static final int EVENT_CLEAR_NET_TRANSITION_WAKELOCK =
184 MAX_NETWORK_STATE_TRACKER_EVENT + 8;
185
Robert Greenwalt434203a2010-10-11 16:00:27 -0700186 /**
187 * used internally to reload global proxy settings
188 */
189 private static final int EVENT_APPLY_GLOBAL_HTTP_PROXY =
190 MAX_NETWORK_STATE_TRACKER_EVENT + 9;
191
Robert Greenwalt42acef32009-08-12 16:08:25 -0700192 private Handler mHandler;
193
194 // list of DeathRecipients used to make sure features are turned off when
195 // a process dies
196 private List mFeatureUsers;
197
Mike Lockwood0f79b542009-08-14 14:18:49 -0400198 private boolean mSystemReady;
Dianne Hackborn1c633fc2009-12-08 19:45:14 -0800199 private Intent mInitialBroadcast;
Mike Lockwood0f79b542009-08-14 14:18:49 -0400200
Robert Greenwalt14f2ef42010-06-15 12:19:37 -0700201 private PowerManager.WakeLock mNetTransitionWakeLock;
202 private String mNetTransitionWakeLockCausedBy = "";
203 private int mNetTransitionWakeLockSerialNumber;
204 private int mNetTransitionWakeLockTimeout;
205
Robert Greenwalte90aa5e2010-09-01 11:34:05 -0700206 private InetAddress mDefaultDns;
207
Robert Greenwalt4e8dfef2010-09-20 14:35:25 -0700208 // used in DBG mode to track inet condition reports
209 private static final int INET_CONDITION_LOG_MAX_SIZE = 15;
210 private ArrayList mInetLog;
211
Robert Greenwalt434203a2010-10-11 16:00:27 -0700212 // track the current default http proxy - tell the world if we get a new one (real change)
213 private ProxyProperties mDefaultProxy = null;
214 // track the global proxy.
215 private ProxyProperties mGlobalProxy = null;
216 private final Object mGlobalProxyLock = new Object();
217
218 private SettingsObserver mSettingsObserver;
219
Robert Greenwalt511288a2009-12-07 11:33:18 -0800220 private static class NetworkAttributes {
Robert Greenwalt42acef32009-08-12 16:08:25 -0700221 /**
222 * Class for holding settings read from resources.
223 */
224 public String mName;
225 public int mType;
226 public int mRadio;
227 public int mPriority;
Robert Greenwalt511288a2009-12-07 11:33:18 -0800228 public NetworkInfo.State mLastState;
Robert Greenwalt42acef32009-08-12 16:08:25 -0700229 public NetworkAttributes(String init) {
230 String fragments[] = init.split(",");
231 mName = fragments[0].toLowerCase();
Robert Greenwalt5154ae762009-10-30 14:17:42 -0700232 mType = Integer.parseInt(fragments[1]);
233 mRadio = Integer.parseInt(fragments[2]);
234 mPriority = Integer.parseInt(fragments[3]);
Robert Greenwalt511288a2009-12-07 11:33:18 -0800235 mLastState = NetworkInfo.State.UNKNOWN;
Robert Greenwalt42acef32009-08-12 16:08:25 -0700236 }
237 public boolean isDefault() {
238 return (mType == mRadio);
239 }
240 }
241 NetworkAttributes[] mNetAttributes;
Robert Greenwalt5154ae762009-10-30 14:17:42 -0700242 int mNetworksDefined;
Robert Greenwalt42acef32009-08-12 16:08:25 -0700243
Robert Greenwalt511288a2009-12-07 11:33:18 -0800244 private static class RadioAttributes {
Robert Greenwalt42acef32009-08-12 16:08:25 -0700245 public int mSimultaneity;
246 public int mType;
247 public RadioAttributes(String init) {
248 String fragments[] = init.split(",");
Robert Greenwalt5154ae762009-10-30 14:17:42 -0700249 mType = Integer.parseInt(fragments[0]);
250 mSimultaneity = Integer.parseInt(fragments[1]);
Robert Greenwalt42acef32009-08-12 16:08:25 -0700251 }
252 }
253 RadioAttributes[] mRadioAttributes;
254
Wink Savillebb08caf2010-09-02 19:23:52 -0700255 public static synchronized ConnectivityService getInstance(Context context) {
256 if (sServiceInstance == null) {
257 sServiceInstance = new ConnectivityService(context);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800258 }
Wink Savillebb08caf2010-09-02 19:23:52 -0700259 return sServiceInstance;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800260 }
Robert Greenwalt86e9e552009-07-16 17:21:39 -0700261
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800262 private ConnectivityService(Context context) {
Wink Savilleed9c02b2010-12-03 12:01:38 -0800263 if (DBG) log("ConnectivityService starting up");
Robert Greenwaltde8383c2010-01-14 17:47:58 -0800264
Wink Savillebb08caf2010-09-02 19:23:52 -0700265 HandlerThread handlerThread = new HandlerThread("ConnectivityServiceThread");
266 handlerThread.start();
267 mHandler = new MyHandler(handlerThread.getLooper());
268
Robert Greenwaltd825ea42010-12-29 16:15:02 -0800269 mBackgroundDataEnabled.set(Settings.Secure.getInt(context.getContentResolver(),
270 Settings.Secure.BACKGROUND_DATA, 1) == 1);
271
Robert Greenwaltde8383c2010-01-14 17:47:58 -0800272 // setup our unique device name
Robert Greenwalt733c6292010-12-06 09:30:17 -0800273 if (TextUtils.isEmpty(SystemProperties.get("net.hostname"))) {
274 String id = Settings.Secure.getString(context.getContentResolver(),
275 Settings.Secure.ANDROID_ID);
276 if (id != null && id.length() > 0) {
277 String name = new String("android_").concat(id);
278 SystemProperties.set("net.hostname", name);
279 }
Robert Greenwaltde8383c2010-01-14 17:47:58 -0800280 }
281
Robert Greenwalte90aa5e2010-09-01 11:34:05 -0700282 // read our default dns server ip
283 String dns = Settings.Secure.getString(context.getContentResolver(),
284 Settings.Secure.DEFAULT_DNS_SERVER);
285 if (dns == null || dns.length() == 0) {
286 dns = context.getResources().getString(
287 com.android.internal.R.string.config_default_dns_server);
288 }
289 try {
Robert Greenwalte5903732011-02-22 16:00:42 -0800290 mDefaultDns = NetworkUtils.numericToInetAddress(dns);
291 } catch (IllegalArgumentException e) {
Wink Savilleed9c02b2010-12-03 12:01:38 -0800292 loge("Error setting defaultDns using " + dns);
Robert Greenwalte90aa5e2010-09-01 11:34:05 -0700293 }
294
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800295 mContext = context;
Robert Greenwalt14f2ef42010-06-15 12:19:37 -0700296
297 PowerManager powerManager = (PowerManager)mContext.getSystemService(Context.POWER_SERVICE);
298 mNetTransitionWakeLock = powerManager.newWakeLock(PowerManager.PARTIAL_WAKE_LOCK, TAG);
299 mNetTransitionWakeLockTimeout = mContext.getResources().getInteger(
300 com.android.internal.R.integer.config_networkTransitionTimeout);
301
Robert Greenwalt42acef32009-08-12 16:08:25 -0700302 mNetTrackers = new NetworkStateTracker[
303 ConnectivityManager.MAX_NETWORK_TYPE+1];
Robert Greenwalt86e9e552009-07-16 17:21:39 -0700304
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800305 mNetworkPreference = getPersistedNetworkPreference();
Robert Greenwalt86e9e552009-07-16 17:21:39 -0700306
Robert Greenwalt5154ae762009-10-30 14:17:42 -0700307 mRadioAttributes = new RadioAttributes[ConnectivityManager.MAX_RADIO_TYPE+1];
308 mNetAttributes = new NetworkAttributes[ConnectivityManager.MAX_NETWORK_TYPE+1];
309
Robert Greenwalt42acef32009-08-12 16:08:25 -0700310 // Load device network attributes from resources
Robert Greenwalt42acef32009-08-12 16:08:25 -0700311 String[] raStrings = context.getResources().getStringArray(
312 com.android.internal.R.array.radioAttributes);
Robert Greenwalt5154ae762009-10-30 14:17:42 -0700313 for (String raString : raStrings) {
314 RadioAttributes r = new RadioAttributes(raString);
315 if (r.mType > ConnectivityManager.MAX_RADIO_TYPE) {
Wink Savilleed9c02b2010-12-03 12:01:38 -0800316 loge("Error in radioAttributes - ignoring attempt to define type " + r.mType);
Robert Greenwalt5154ae762009-10-30 14:17:42 -0700317 continue;
318 }
319 if (mRadioAttributes[r.mType] != null) {
Wink Savilleed9c02b2010-12-03 12:01:38 -0800320 loge("Error in radioAttributes - ignoring attempt to redefine type " +
Robert Greenwalt5154ae762009-10-30 14:17:42 -0700321 r.mType);
322 continue;
323 }
Robert Greenwalt42acef32009-08-12 16:08:25 -0700324 mRadioAttributes[r.mType] = r;
325 }
326
Robert Greenwalt5154ae762009-10-30 14:17:42 -0700327 String[] naStrings = context.getResources().getStringArray(
328 com.android.internal.R.array.networkAttributes);
329 for (String naString : naStrings) {
330 try {
331 NetworkAttributes n = new NetworkAttributes(naString);
332 if (n.mType > ConnectivityManager.MAX_NETWORK_TYPE) {
Wink Savilleed9c02b2010-12-03 12:01:38 -0800333 loge("Error in networkAttributes - ignoring attempt to define type " +
Robert Greenwalt5154ae762009-10-30 14:17:42 -0700334 n.mType);
335 continue;
Robert Greenwalt42acef32009-08-12 16:08:25 -0700336 }
Robert Greenwalt5154ae762009-10-30 14:17:42 -0700337 if (mNetAttributes[n.mType] != null) {
Wink Savilleed9c02b2010-12-03 12:01:38 -0800338 loge("Error in networkAttributes - ignoring attempt to redefine type " +
Robert Greenwalt5154ae762009-10-30 14:17:42 -0700339 n.mType);
340 continue;
341 }
342 if (mRadioAttributes[n.mRadio] == null) {
Wink Savilleed9c02b2010-12-03 12:01:38 -0800343 loge("Error in networkAttributes - ignoring attempt to use undefined " +
Robert Greenwalt5154ae762009-10-30 14:17:42 -0700344 "radio " + n.mRadio + " in network type " + n.mType);
345 continue;
346 }
347 mNetAttributes[n.mType] = n;
348 mNetworksDefined++;
349 } catch(Exception e) {
350 // ignore it - leave the entry null
Robert Greenwalt42acef32009-08-12 16:08:25 -0700351 }
352 }
353
Robert Greenwalt5154ae762009-10-30 14:17:42 -0700354 // high priority first
355 mPriorityList = new int[mNetworksDefined];
356 {
357 int insertionPoint = mNetworksDefined-1;
358 int currentLowest = 0;
359 int nextLowest = 0;
360 while (insertionPoint > -1) {
361 for (NetworkAttributes na : mNetAttributes) {
362 if (na == null) continue;
363 if (na.mPriority < currentLowest) continue;
364 if (na.mPriority > currentLowest) {
365 if (na.mPriority < nextLowest || nextLowest == 0) {
366 nextLowest = na.mPriority;
367 }
368 continue;
369 }
370 mPriorityList[insertionPoint--] = na.mType;
371 }
372 currentLowest = nextLowest;
373 nextLowest = 0;
374 }
375 }
376
377 mNetRequestersPids = new ArrayList[ConnectivityManager.MAX_NETWORK_TYPE+1];
378 for (int i : mPriorityList) {
Robert Greenwalt42acef32009-08-12 16:08:25 -0700379 mNetRequestersPids[i] = new ArrayList();
380 }
381
382 mFeatureUsers = new ArrayList();
383
Robert Greenwalt5154ae762009-10-30 14:17:42 -0700384 mNumDnsEntries = 0;
385
386 mTestMode = SystemProperties.get("cm.test.mode").equals("true")
387 && SystemProperties.get("ro.build.type").equals("eng");
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800388 /*
389 * Create the network state trackers for Wi-Fi and mobile
390 * data. Maybe this could be done with a factory class,
391 * but it's not clear that it's worth it, given that
392 * the number of different network types is not going
393 * to change very often.
394 */
Robert Greenwalt5154ae762009-10-30 14:17:42 -0700395 for (int netType : mPriorityList) {
396 switch (mNetAttributes[netType].mRadio) {
397 case ConnectivityManager.TYPE_WIFI:
Wink Savilleed9c02b2010-12-03 12:01:38 -0800398 if (DBG) log("Starting Wifi Service.");
Wink Savillec7a98342010-08-13 16:11:42 -0700399 WifiStateTracker wst = new WifiStateTracker();
Irfan Sheriff0d255342010-07-28 09:35:20 -0700400 WifiService wifiService = new WifiService(context);
Robert Greenwalt5154ae762009-10-30 14:17:42 -0700401 ServiceManager.addService(Context.WIFI_SERVICE, wifiService);
Irfan Sheriff0d255342010-07-28 09:35:20 -0700402 wifiService.checkAndStartWifi();
Robert Greenwalt5154ae762009-10-30 14:17:42 -0700403 mNetTrackers[ConnectivityManager.TYPE_WIFI] = wst;
Wink Savillec7a98342010-08-13 16:11:42 -0700404 wst.startMonitoring(context, mHandler);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800405
Irfan Sheriffa2a1b912010-06-07 09:03:04 -0700406 //TODO: as part of WWS refactor, create only when needed
Irfan Sheriff0d255342010-07-28 09:35:20 -0700407 mWifiWatchdogService = new WifiWatchdogService(context);
Irfan Sheriffa2a1b912010-06-07 09:03:04 -0700408
Robert Greenwalt5154ae762009-10-30 14:17:42 -0700409 break;
410 case ConnectivityManager.TYPE_MOBILE:
Wink Savillec7a98342010-08-13 16:11:42 -0700411 mNetTrackers[netType] = new MobileDataStateTracker(netType,
412 mNetAttributes[netType].mName);
413 mNetTrackers[netType].startMonitoring(context, mHandler);
Robert Greenwalt5154ae762009-10-30 14:17:42 -0700414 break;
Robert Greenwaltda3d5e62010-12-06 13:56:24 -0800415 case ConnectivityManager.TYPE_DUMMY:
416 mNetTrackers[netType] = new DummyDataStateTracker(netType,
417 mNetAttributes[netType].mName);
418 mNetTrackers[netType].startMonitoring(context, mHandler);
419 break;
Jaikumar Ganesh15c74392010-12-21 22:31:44 -0800420 case ConnectivityManager.TYPE_BLUETOOTH:
421 mNetTrackers[netType] = BluetoothTetheringDataTracker.getInstance();
422 mNetTrackers[netType].startMonitoring(context, mHandler);
423 break;
Benoit Goby08c39c62010-12-22 14:29:40 -0800424 case ConnectivityManager.TYPE_ETHERNET:
425 mNetTrackers[netType] = EthernetDataTracker.getInstance();
426 mNetTrackers[netType].startMonitoring(context, mHandler);
427 break;
Robert Greenwalt5154ae762009-10-30 14:17:42 -0700428 default:
Wink Savilleed9c02b2010-12-03 12:01:38 -0800429 loge("Trying to create a DataStateTracker for an unknown radio type " +
Robert Greenwalt5154ae762009-10-30 14:17:42 -0700430 mNetAttributes[netType].mRadio);
431 continue;
432 }
433 }
Robert Greenwaltd0e18ff2010-01-26 11:40:34 -0800434
Robert Greenwaltdfadaea2010-03-11 15:03:08 -0800435 mTethering = new Tethering(mContext, mHandler.getLooper());
Robert Greenwaltc9d5fb72010-02-25 12:29:30 -0800436 mTetheringConfigValid = (((mNetTrackers[ConnectivityManager.TYPE_MOBILE_DUN] != null) ||
437 !mTethering.isDunRequired()) &&
438 (mTethering.getTetherableUsbRegexs().length != 0 ||
Danica Chang6fdd0c62010-08-11 14:54:43 -0700439 mTethering.getTetherableWifiRegexs().length != 0 ||
440 mTethering.getTetherableBluetoothRegexs().length != 0) &&
Robert Greenwaltc9d5fb72010-02-25 12:29:30 -0800441 mTethering.getUpstreamIfaceRegexs().length != 0);
442
Robert Greenwalt4e8dfef2010-09-20 14:35:25 -0700443 if (DBG) {
444 mInetLog = new ArrayList();
445 }
Robert Greenwalt434203a2010-10-11 16:00:27 -0700446
447 mSettingsObserver = new SettingsObserver(mHandler, EVENT_APPLY_GLOBAL_HTTP_PROXY);
448 mSettingsObserver.observe(mContext);
Robert Greenwaltb7090d62010-12-02 11:31:00 -0800449
450 loadGlobalProxy();
Hung-ying Tyan6b818de2011-01-19 16:48:38 +0800451
452 VpnManager.startVpnService(context);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800453 }
454
Robert Greenwalt5154ae762009-10-30 14:17:42 -0700455
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800456 /**
Robert Greenwalt86e9e552009-07-16 17:21:39 -0700457 * Sets the preferred network.
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800458 * @param preference the new preference
459 */
Robert Greenwalt8dcc28b2010-09-23 10:05:56 -0700460 public void setNetworkPreference(int preference) {
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800461 enforceChangePermission();
Robert Greenwalt8dcc28b2010-09-23 10:05:56 -0700462
463 mHandler.sendMessage(mHandler.obtainMessage(EVENT_SET_NETWORK_PREFERENCE, preference, 0));
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800464 }
465
466 public int getNetworkPreference() {
467 enforceAccessPermission();
Robert Greenwalt8dcc28b2010-09-23 10:05:56 -0700468 int preference;
469 synchronized(this) {
470 preference = mNetworkPreference;
471 }
472 return preference;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800473 }
474
Robert Greenwalt8dcc28b2010-09-23 10:05:56 -0700475 private void handleSetNetworkPreference(int preference) {
476 if (ConnectivityManager.isNetworkTypeValid(preference) &&
477 mNetAttributes[preference] != null &&
478 mNetAttributes[preference].isDefault()) {
479 if (mNetworkPreference != preference) {
480 final ContentResolver cr = mContext.getContentResolver();
481 Settings.Secure.putInt(cr, Settings.Secure.NETWORK_PREFERENCE, preference);
482 synchronized(this) {
483 mNetworkPreference = preference;
484 }
485 enforcePreference();
486 }
487 }
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800488 }
Robert Greenwalt86e9e552009-07-16 17:21:39 -0700489
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800490 private int getPersistedNetworkPreference() {
491 final ContentResolver cr = mContext.getContentResolver();
492
493 final int networkPrefSetting = Settings.Secure
494 .getInt(cr, Settings.Secure.NETWORK_PREFERENCE, -1);
495 if (networkPrefSetting != -1) {
496 return networkPrefSetting;
497 }
498
499 return ConnectivityManager.DEFAULT_NETWORK_PREFERENCE;
500 }
Robert Greenwalt86e9e552009-07-16 17:21:39 -0700501
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800502 /**
Robert Greenwalt86e9e552009-07-16 17:21:39 -0700503 * Make the state of network connectivity conform to the preference settings
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800504 * In this method, we only tear down a non-preferred network. Establishing
505 * a connection to the preferred network is taken care of when we handle
506 * the disconnect event from the non-preferred network
507 * (see {@link #handleDisconnect(NetworkInfo)}).
508 */
509 private void enforcePreference() {
Robert Greenwalt42acef32009-08-12 16:08:25 -0700510 if (mNetTrackers[mNetworkPreference].getNetworkInfo().isConnected())
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800511 return;
512
Robert Greenwalt42acef32009-08-12 16:08:25 -0700513 if (!mNetTrackers[mNetworkPreference].isAvailable())
514 return;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800515
Robert Greenwalt42acef32009-08-12 16:08:25 -0700516 for (int t=0; t <= ConnectivityManager.MAX_RADIO_TYPE; t++) {
Robert Greenwalt5154ae762009-10-30 14:17:42 -0700517 if (t != mNetworkPreference && mNetTrackers[t] != null &&
Robert Greenwalt42acef32009-08-12 16:08:25 -0700518 mNetTrackers[t].getNetworkInfo().isConnected()) {
Robert Greenwaltec9fe462009-08-20 15:25:14 -0700519 if (DBG) {
Wink Savilleed9c02b2010-12-03 12:01:38 -0800520 log("tearing down " + mNetTrackers[t].getNetworkInfo() +
Robert Greenwaltec9fe462009-08-20 15:25:14 -0700521 " in enforcePreference");
522 }
Robert Greenwalt42acef32009-08-12 16:08:25 -0700523 teardown(mNetTrackers[t]);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800524 }
525 }
526 }
527
528 private boolean teardown(NetworkStateTracker netTracker) {
529 if (netTracker.teardown()) {
530 netTracker.setTeardownRequested(true);
531 return true;
532 } else {
533 return false;
534 }
535 }
536
537 /**
538 * Return NetworkInfo for the active (i.e., connected) network interface.
539 * It is assumed that at most one network is active at a time. If more
540 * than one is active, it is indeterminate which will be returned.
Robert Greenwalt86e9e552009-07-16 17:21:39 -0700541 * @return the info for the active network, or {@code null} if none is
542 * active
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800543 */
544 public NetworkInfo getActiveNetworkInfo() {
545 enforceAccessPermission();
Robert Greenwalt42acef32009-08-12 16:08:25 -0700546 for (int type=0; type <= ConnectivityManager.MAX_NETWORK_TYPE; type++) {
Robert Greenwalt5154ae762009-10-30 14:17:42 -0700547 if (mNetAttributes[type] == null || !mNetAttributes[type].isDefault()) {
Robert Greenwalt42acef32009-08-12 16:08:25 -0700548 continue;
549 }
550 NetworkStateTracker t = mNetTrackers[type];
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800551 NetworkInfo info = t.getNetworkInfo();
552 if (info.isConnected()) {
Wink Savilleed9c02b2010-12-03 12:01:38 -0800553 if (DBG && type != mActiveDefaultNetwork) {
554 loge("connected default network is not mActiveDefaultNetwork!");
555 }
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800556 return info;
557 }
558 }
559 return null;
560 }
561
562 public NetworkInfo getNetworkInfo(int networkType) {
563 enforceAccessPermission();
564 if (ConnectivityManager.isNetworkTypeValid(networkType)) {
565 NetworkStateTracker t = mNetTrackers[networkType];
566 if (t != null)
567 return t.getNetworkInfo();
568 }
569 return null;
570 }
571
572 public NetworkInfo[] getAllNetworkInfo() {
573 enforceAccessPermission();
Robert Greenwalt5154ae762009-10-30 14:17:42 -0700574 NetworkInfo[] result = new NetworkInfo[mNetworksDefined];
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800575 int i = 0;
576 for (NetworkStateTracker t : mNetTrackers) {
Robert Greenwalt5154ae762009-10-30 14:17:42 -0700577 if(t != null) result[i++] = t.getNetworkInfo();
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800578 }
579 return result;
580 }
581
Robert Greenwaltd192dad2010-09-14 09:18:02 -0700582 /**
583 * Return LinkProperties for the active (i.e., connected) default
584 * network interface. It is assumed that at most one default network
585 * is active at a time. If more than one is active, it is indeterminate
586 * which will be returned.
587 * @return the ip properties for the active network, or {@code null} if
588 * none is active
589 */
590 public LinkProperties getActiveLinkProperties() {
591 enforceAccessPermission();
592 for (int type=0; type <= ConnectivityManager.MAX_NETWORK_TYPE; type++) {
593 if (mNetAttributes[type] == null || !mNetAttributes[type].isDefault()) {
594 continue;
595 }
596 NetworkStateTracker t = mNetTrackers[type];
597 NetworkInfo info = t.getNetworkInfo();
598 if (info.isConnected()) {
599 return t.getLinkProperties();
600 }
601 }
602 return null;
603 }
604
605 public LinkProperties getLinkProperties(int networkType) {
606 enforceAccessPermission();
607 if (ConnectivityManager.isNetworkTypeValid(networkType)) {
608 NetworkStateTracker t = mNetTrackers[networkType];
609 if (t != null) return t.getLinkProperties();
610 }
611 return null;
612 }
613
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800614 public boolean setRadios(boolean turnOn) {
615 boolean result = true;
616 enforceChangePermission();
617 for (NetworkStateTracker t : mNetTrackers) {
Robert Greenwalt5154ae762009-10-30 14:17:42 -0700618 if (t != null) result = t.setRadio(turnOn) && result;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800619 }
620 return result;
621 }
622
623 public boolean setRadio(int netType, boolean turnOn) {
624 enforceChangePermission();
625 if (!ConnectivityManager.isNetworkTypeValid(netType)) {
626 return false;
627 }
628 NetworkStateTracker tracker = mNetTrackers[netType];
629 return tracker != null && tracker.setRadio(turnOn);
630 }
631
Robert Greenwalt9c75d4a2009-09-27 17:27:04 -0700632 /**
633 * Used to notice when the calling process dies so we can self-expire
634 *
635 * Also used to know if the process has cleaned up after itself when
636 * our auto-expire timer goes off. The timer has a link to an object.
637 *
638 */
Robert Greenwalt42acef32009-08-12 16:08:25 -0700639 private class FeatureUser implements IBinder.DeathRecipient {
640 int mNetworkType;
641 String mFeature;
642 IBinder mBinder;
643 int mPid;
644 int mUid;
Robert Greenwaltb9285352009-12-21 18:24:07 -0800645 long mCreateTime;
Robert Greenwalt42acef32009-08-12 16:08:25 -0700646
647 FeatureUser(int type, String feature, IBinder binder) {
648 super();
649 mNetworkType = type;
650 mFeature = feature;
651 mBinder = binder;
652 mPid = getCallingPid();
653 mUid = getCallingUid();
Robert Greenwaltb9285352009-12-21 18:24:07 -0800654 mCreateTime = System.currentTimeMillis();
Robert Greenwalt9c75d4a2009-09-27 17:27:04 -0700655
Robert Greenwalt42acef32009-08-12 16:08:25 -0700656 try {
657 mBinder.linkToDeath(this, 0);
658 } catch (RemoteException e) {
659 binderDied();
660 }
661 }
662
663 void unlinkDeathRecipient() {
664 mBinder.unlinkToDeath(this, 0);
665 }
666
667 public void binderDied() {
Wink Savilleed9c02b2010-12-03 12:01:38 -0800668 log("ConnectivityService FeatureUser binderDied(" +
Robert Greenwaltb9285352009-12-21 18:24:07 -0800669 mNetworkType + ", " + mFeature + ", " + mBinder + "), created " +
670 (System.currentTimeMillis() - mCreateTime) + " mSec ago");
Robert Greenwalt9c75d4a2009-09-27 17:27:04 -0700671 stopUsingNetworkFeature(this, false);
Robert Greenwalt42acef32009-08-12 16:08:25 -0700672 }
673
Robert Greenwalt9c75d4a2009-09-27 17:27:04 -0700674 public void expire() {
Wink Savilleed9c02b2010-12-03 12:01:38 -0800675 log("ConnectivityService FeatureUser expire(" +
Robert Greenwaltb9285352009-12-21 18:24:07 -0800676 mNetworkType + ", " + mFeature + ", " + mBinder +"), created " +
677 (System.currentTimeMillis() - mCreateTime) + " mSec ago");
Robert Greenwalt9c75d4a2009-09-27 17:27:04 -0700678 stopUsingNetworkFeature(this, false);
679 }
Robert Greenwaltb9285352009-12-21 18:24:07 -0800680
681 public String toString() {
682 return "FeatureUser("+mNetworkType+","+mFeature+","+mPid+","+mUid+"), created " +
683 (System.currentTimeMillis() - mCreateTime) + " mSec ago";
684 }
Robert Greenwalt42acef32009-08-12 16:08:25 -0700685 }
686
Robert Greenwalt9c75d4a2009-09-27 17:27:04 -0700687 // javadoc from interface
Robert Greenwalt42acef32009-08-12 16:08:25 -0700688 public int startUsingNetworkFeature(int networkType, String feature,
689 IBinder binder) {
690 if (DBG) {
Wink Savilleed9c02b2010-12-03 12:01:38 -0800691 log("startUsingNetworkFeature for net " + networkType + ": " + feature);
Robert Greenwalt42acef32009-08-12 16:08:25 -0700692 }
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800693 enforceChangePermission();
Robert Greenwalt5154ae762009-10-30 14:17:42 -0700694 if (!ConnectivityManager.isNetworkTypeValid(networkType) ||
695 mNetAttributes[networkType] == null) {
Robert Greenwalt42acef32009-08-12 16:08:25 -0700696 return Phone.APN_REQUEST_FAILED;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800697 }
Robert Greenwalt42acef32009-08-12 16:08:25 -0700698
Robert Greenwalt9c75d4a2009-09-27 17:27:04 -0700699 FeatureUser f = new FeatureUser(networkType, feature, binder);
Robert Greenwalt42acef32009-08-12 16:08:25 -0700700
701 // TODO - move this into the MobileDataStateTracker
702 int usedNetworkType = networkType;
703 if(networkType == ConnectivityManager.TYPE_MOBILE) {
Wink Savilleeca208f2011-03-03 09:53:52 -0800704 if (TextUtils.equals(feature, Phone.FEATURE_ENABLE_MMS)) {
705 usedNetworkType = ConnectivityManager.TYPE_MOBILE_MMS;
706 } else if (TextUtils.equals(feature, Phone.FEATURE_ENABLE_SUPL)) {
707 usedNetworkType = ConnectivityManager.TYPE_MOBILE_SUPL;
708 } else if (TextUtils.equals(feature, Phone.FEATURE_ENABLE_DUN) ||
709 TextUtils.equals(feature, Phone.FEATURE_ENABLE_DUN_ALWAYS)) {
710 usedNetworkType = ConnectivityManager.TYPE_MOBILE_DUN;
711 } else if (TextUtils.equals(feature, Phone.FEATURE_ENABLE_HIPRI)) {
712 usedNetworkType = ConnectivityManager.TYPE_MOBILE_HIPRI;
Robert Greenwalt42acef32009-08-12 16:08:25 -0700713 }
714 }
715 NetworkStateTracker network = mNetTrackers[usedNetworkType];
716 if (network != null) {
Robert Greenwalt0be1e982010-12-15 13:26:33 -0800717 Integer currentPid = new Integer(getCallingPid());
Robert Greenwalt42acef32009-08-12 16:08:25 -0700718 if (usedNetworkType != networkType) {
Robert Greenwalt42acef32009-08-12 16:08:25 -0700719 NetworkStateTracker radio = mNetTrackers[networkType];
720 NetworkInfo ni = network.getNetworkInfo();
721
722 if (ni.isAvailable() == false) {
Wink Savilleed9c02b2010-12-03 12:01:38 -0800723 if (DBG) log("special network not available");
Robert Greenwalte32e8122010-12-29 14:35:21 -0800724 if (!TextUtils.equals(feature,Phone.FEATURE_ENABLE_DUN_ALWAYS)) {
725 return Phone.APN_TYPE_NOT_AVAILABLE;
726 } else {
727 // else make the attempt anyway - probably giving REQUEST_STARTED below
728 }
Robert Greenwalt42acef32009-08-12 16:08:25 -0700729 }
730
Robert Greenwalt9c75d4a2009-09-27 17:27:04 -0700731 synchronized(this) {
732 mFeatureUsers.add(f);
733 if (!mNetRequestersPids[usedNetworkType].contains(currentPid)) {
734 // this gets used for per-pid dns when connected
735 mNetRequestersPids[usedNetworkType].add(currentPid);
736 }
Robert Greenwalt42acef32009-08-12 16:08:25 -0700737 }
Robert Greenwalt8dcc28b2010-09-23 10:05:56 -0700738 mHandler.sendMessageDelayed(mHandler.obtainMessage(EVENT_RESTORE_DEFAULT_NETWORK,
Robert Greenwalt9c75d4a2009-09-27 17:27:04 -0700739 f), getRestoreDefaultNetworkDelay());
740
Robert Greenwalt42acef32009-08-12 16:08:25 -0700741
Robert Greenwalta64bf832009-08-19 20:19:33 -0700742 if ((ni.isConnectedOrConnecting() == true) &&
743 !network.isTeardownRequested()) {
Robert Greenwalt42acef32009-08-12 16:08:25 -0700744 if (ni.isConnected() == true) {
745 // add the pid-specific dns
Robert Greenwaltc76b8fa2010-07-23 15:46:26 -0700746 handleDnsConfigurationChange(networkType);
Wink Savilleed9c02b2010-12-03 12:01:38 -0800747 if (DBG) log("special network already active");
Robert Greenwalt42acef32009-08-12 16:08:25 -0700748 return Phone.APN_ALREADY_ACTIVE;
749 }
Wink Savilleed9c02b2010-12-03 12:01:38 -0800750 if (DBG) log("special network already connecting");
Robert Greenwalt42acef32009-08-12 16:08:25 -0700751 return Phone.APN_REQUEST_STARTED;
752 }
753
754 // check if the radio in play can make another contact
755 // assume if cannot for now
756
Wink Savilleed9c02b2010-12-03 12:01:38 -0800757 if (DBG) log("reconnecting to special network");
Robert Greenwalt42acef32009-08-12 16:08:25 -0700758 network.reconnect();
759 return Phone.APN_REQUEST_STARTED;
760 } else {
Robert Greenwalt0be1e982010-12-15 13:26:33 -0800761 // need to remember this unsupported request so we respond appropriately on stop
762 synchronized(this) {
763 mFeatureUsers.add(f);
764 if (!mNetRequestersPids[usedNetworkType].contains(currentPid)) {
765 // this gets used for per-pid dns when connected
766 mNetRequestersPids[usedNetworkType].add(currentPid);
767 }
768 }
Robert Greenwalt02648a42010-05-18 10:52:51 -0700769 return -1;
Robert Greenwalt42acef32009-08-12 16:08:25 -0700770 }
771 }
772 return Phone.APN_TYPE_NOT_AVAILABLE;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800773 }
774
Robert Greenwalt9c75d4a2009-09-27 17:27:04 -0700775 // javadoc from interface
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800776 public int stopUsingNetworkFeature(int networkType, String feature) {
Robert Greenwaltb8f16342009-10-06 17:52:40 -0700777 enforceChangePermission();
778
Robert Greenwalt9c75d4a2009-09-27 17:27:04 -0700779 int pid = getCallingPid();
780 int uid = getCallingUid();
781
782 FeatureUser u = null;
783 boolean found = false;
784
785 synchronized(this) {
786 for (int i = 0; i < mFeatureUsers.size() ; i++) {
787 u = (FeatureUser)mFeatureUsers.get(i);
788 if (uid == u.mUid && pid == u.mPid &&
789 networkType == u.mNetworkType &&
790 TextUtils.equals(feature, u.mFeature)) {
791 found = true;
792 break;
793 }
794 }
795 }
796 if (found && u != null) {
797 // stop regardless of how many other time this proc had called start
798 return stopUsingNetworkFeature(u, true);
799 } else {
800 // none found!
Wink Savilleed9c02b2010-12-03 12:01:38 -0800801 if (DBG) log("ignoring stopUsingNetworkFeature - not a live request");
Robert Greenwalt9c75d4a2009-09-27 17:27:04 -0700802 return 1;
803 }
Robert Greenwalt42acef32009-08-12 16:08:25 -0700804 }
805
Robert Greenwalt9c75d4a2009-09-27 17:27:04 -0700806 private int stopUsingNetworkFeature(FeatureUser u, boolean ignoreDups) {
807 int networkType = u.mNetworkType;
808 String feature = u.mFeature;
809 int pid = u.mPid;
810 int uid = u.mUid;
811
812 NetworkStateTracker tracker = null;
813 boolean callTeardown = false; // used to carry our decision outside of sync block
814
Robert Greenwalt42acef32009-08-12 16:08:25 -0700815 if (DBG) {
Wink Savilleed9c02b2010-12-03 12:01:38 -0800816 log("stopUsingNetworkFeature for net " + networkType +
Robert Greenwalt42acef32009-08-12 16:08:25 -0700817 ": " + feature);
818 }
Robert Greenwaltb8f16342009-10-06 17:52:40 -0700819
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800820 if (!ConnectivityManager.isNetworkTypeValid(networkType)) {
821 return -1;
822 }
Robert Greenwalt42acef32009-08-12 16:08:25 -0700823
Robert Greenwalt9c75d4a2009-09-27 17:27:04 -0700824 // need to link the mFeatureUsers list with the mNetRequestersPids state in this
825 // sync block
826 synchronized(this) {
827 // check if this process still has an outstanding start request
828 if (!mFeatureUsers.contains(u)) {
Wink Savilleed9c02b2010-12-03 12:01:38 -0800829 if (DBG) log("ignoring - this process has no outstanding requests");
Robert Greenwalt42acef32009-08-12 16:08:25 -0700830 return 1;
831 }
Robert Greenwalt9c75d4a2009-09-27 17:27:04 -0700832 u.unlinkDeathRecipient();
833 mFeatureUsers.remove(mFeatureUsers.indexOf(u));
834 // If we care about duplicate requests, check for that here.
835 //
836 // This is done to support the extension of a request - the app
837 // can request we start the network feature again and renew the
838 // auto-shutoff delay. Normal "stop" calls from the app though
839 // do not pay attention to duplicate requests - in effect the
840 // API does not refcount and a single stop will counter multiple starts.
841 if (ignoreDups == false) {
842 for (int i = 0; i < mFeatureUsers.size() ; i++) {
843 FeatureUser x = (FeatureUser)mFeatureUsers.get(i);
844 if (x.mUid == u.mUid && x.mPid == u.mPid &&
845 x.mNetworkType == u.mNetworkType &&
846 TextUtils.equals(x.mFeature, u.mFeature)) {
Wink Savilleed9c02b2010-12-03 12:01:38 -0800847 if (DBG) log("ignoring stopUsingNetworkFeature as dup is found");
Robert Greenwalt9c75d4a2009-09-27 17:27:04 -0700848 return 1;
849 }
850 }
Robert Greenwalt42acef32009-08-12 16:08:25 -0700851 }
Robert Greenwalt9c75d4a2009-09-27 17:27:04 -0700852
853 // TODO - move to MobileDataStateTracker
854 int usedNetworkType = networkType;
855 if (networkType == ConnectivityManager.TYPE_MOBILE) {
Wink Savilleeca208f2011-03-03 09:53:52 -0800856 if (TextUtils.equals(feature, Phone.FEATURE_ENABLE_MMS)) {
857 usedNetworkType = ConnectivityManager.TYPE_MOBILE_MMS;
858 } else if (TextUtils.equals(feature, Phone.FEATURE_ENABLE_SUPL)) {
859 usedNetworkType = ConnectivityManager.TYPE_MOBILE_SUPL;
860 } else if (TextUtils.equals(feature, Phone.FEATURE_ENABLE_DUN) ||
861 TextUtils.equals(feature, Phone.FEATURE_ENABLE_DUN_ALWAYS)) {
862 usedNetworkType = ConnectivityManager.TYPE_MOBILE_DUN;
863 } else if (TextUtils.equals(feature, Phone.FEATURE_ENABLE_HIPRI)) {
864 usedNetworkType = ConnectivityManager.TYPE_MOBILE_HIPRI;
Robert Greenwalt9c75d4a2009-09-27 17:27:04 -0700865 }
866 }
867 tracker = mNetTrackers[usedNetworkType];
Robert Greenwalt5154ae762009-10-30 14:17:42 -0700868 if (tracker == null) {
Wink Savilleed9c02b2010-12-03 12:01:38 -0800869 if (DBG) log("ignoring - no known tracker for net type " + usedNetworkType);
Robert Greenwalt5154ae762009-10-30 14:17:42 -0700870 return -1;
871 }
872 if (usedNetworkType != networkType) {
Robert Greenwalt9c75d4a2009-09-27 17:27:04 -0700873 Integer currentPid = new Integer(pid);
Robert Greenwalt9c75d4a2009-09-27 17:27:04 -0700874 mNetRequestersPids[usedNetworkType].remove(currentPid);
Robert Greenwalt421c72b2009-12-17 14:54:59 -0800875 reassessPidDns(pid, true);
Robert Greenwalt9c75d4a2009-09-27 17:27:04 -0700876 if (mNetRequestersPids[usedNetworkType].size() != 0) {
Wink Savilleed9c02b2010-12-03 12:01:38 -0800877 if (DBG) log("not tearing down special network - " +
Robert Greenwalt9c75d4a2009-09-27 17:27:04 -0700878 "others still using it");
879 return 1;
880 }
881 callTeardown = true;
Robert Greenwalt19b9ab42011-01-10 11:58:31 -0800882 } else {
883 if (DBG) log("not a known feature - dropping");
Robert Greenwalt9c75d4a2009-09-27 17:27:04 -0700884 }
885 }
Wink Savilleed9c02b2010-12-03 12:01:38 -0800886 if (DBG) log("Doing network teardown");
Robert Greenwalt9c75d4a2009-09-27 17:27:04 -0700887 if (callTeardown) {
888 tracker.teardown();
Robert Greenwalt42acef32009-08-12 16:08:25 -0700889 return 1;
890 } else {
Robert Greenwalt02648a42010-05-18 10:52:51 -0700891 return -1;
Robert Greenwalt42acef32009-08-12 16:08:25 -0700892 }
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800893 }
894
895 /**
Robert Greenwalt585ac0f2010-08-27 09:24:29 -0700896 * @deprecated use requestRouteToHostAddress instead
897 *
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800898 * Ensure that a network route exists to deliver traffic to the specified
899 * host via the specified network interface.
Robert Greenwalt86e9e552009-07-16 17:21:39 -0700900 * @param networkType the type of the network over which traffic to the
901 * specified host is to be routed
902 * @param hostAddress the IP address of the host to which the route is
903 * desired
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800904 * @return {@code true} on success, {@code false} on failure
905 */
906 public boolean requestRouteToHost(int networkType, int hostAddress) {
Robert Greenwalt585ac0f2010-08-27 09:24:29 -0700907 InetAddress inetAddress = NetworkUtils.intToInetAddress(hostAddress);
908
909 if (inetAddress == null) {
910 return false;
911 }
912
913 return requestRouteToHostAddress(networkType, inetAddress.getAddress());
914 }
915
916 /**
917 * Ensure that a network route exists to deliver traffic to the specified
918 * host via the specified network interface.
919 * @param networkType the type of the network over which traffic to the
920 * specified host is to be routed
921 * @param hostAddress the IP address of the host to which the route is
922 * desired
923 * @return {@code true} on success, {@code false} on failure
924 */
925 public boolean requestRouteToHostAddress(int networkType, byte[] hostAddress) {
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800926 enforceChangePermission();
927 if (!ConnectivityManager.isNetworkTypeValid(networkType)) {
928 return false;
929 }
930 NetworkStateTracker tracker = mNetTrackers[networkType];
Robert Greenwalt8206ff32009-09-10 15:06:20 -0700931
Robert Greenwalt5154ae762009-10-30 14:17:42 -0700932 if (tracker == null || !tracker.getNetworkInfo().isConnected() ||
933 tracker.isTeardownRequested()) {
Robert Greenwalt8206ff32009-09-10 15:06:20 -0700934 if (DBG) {
Wink Savilleed9c02b2010-12-03 12:01:38 -0800935 log("requestRouteToHostAddress on down network " +
Robert Greenwalt585ac0f2010-08-27 09:24:29 -0700936 "(" + networkType + ") - dropped");
Robert Greenwalt8206ff32009-09-10 15:06:20 -0700937 }
938 return false;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800939 }
Robert Greenwalt47f69fe2010-06-15 15:43:39 -0700940 try {
Robert Greenwalt585ac0f2010-08-27 09:24:29 -0700941 InetAddress addr = InetAddress.getByAddress(hostAddress);
Robert Greenwalt47f69fe2010-06-15 15:43:39 -0700942 return addHostRoute(tracker, addr);
943 } catch (UnknownHostException e) {}
944 return false;
Irfan Sheriffd649c122010-06-09 15:39:36 -0700945 }
946
947 /**
948 * Ensure that a network route exists to deliver traffic to the specified
949 * host via the mobile data network.
950 * @param hostAddress the IP address of the host to which the route is desired,
951 * in network byte order.
Robert Greenwalt47f69fe2010-06-15 15:43:39 -0700952 * TODO - deprecate
Irfan Sheriffd649c122010-06-09 15:39:36 -0700953 * @return {@code true} on success, {@code false} on failure
954 */
Robert Greenwalt47f69fe2010-06-15 15:43:39 -0700955 private boolean addHostRoute(NetworkStateTracker nt, InetAddress hostAddress) {
Irfan Sheriffd649c122010-06-09 15:39:36 -0700956 if (nt.getNetworkInfo().getType() == ConnectivityManager.TYPE_WIFI) {
957 return false;
958 }
959
Robert Greenwalt37e65eb2010-08-30 10:56:47 -0700960 LinkProperties p = nt.getLinkProperties();
Robert Greenwalt47f69fe2010-06-15 15:43:39 -0700961 if (p == null) return false;
962 String interfaceName = p.getInterfaceName();
Irfan Sheriffd649c122010-06-09 15:39:36 -0700963
964 if (DBG) {
Wink Savilleed9c02b2010-12-03 12:01:38 -0800965 log("Requested host route to " + hostAddress + "(" + interfaceName + ")");
Irfan Sheriffd649c122010-06-09 15:39:36 -0700966 }
Robert Greenwalt47f69fe2010-06-15 15:43:39 -0700967 if (interfaceName != null) {
Robert Greenwalt585ac0f2010-08-27 09:24:29 -0700968 return NetworkUtils.addHostRoute(interfaceName, hostAddress, null);
Irfan Sheriffd649c122010-06-09 15:39:36 -0700969 } else {
Wink Savilleed9c02b2010-12-03 12:01:38 -0800970 if (DBG) loge("addHostRoute failed due to null interface name");
Irfan Sheriffd649c122010-06-09 15:39:36 -0700971 return false;
972 }
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800973 }
974
975 /**
976 * @see ConnectivityManager#getBackgroundDataSetting()
977 */
978 public boolean getBackgroundDataSetting() {
Robert Greenwaltd825ea42010-12-29 16:15:02 -0800979 return mBackgroundDataEnabled.get();
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800980 }
Robert Greenwalt86e9e552009-07-16 17:21:39 -0700981
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800982 /**
983 * @see ConnectivityManager#setBackgroundDataSetting(boolean)
984 */
985 public void setBackgroundDataSetting(boolean allowBackgroundDataUsage) {
986 mContext.enforceCallingOrSelfPermission(
987 android.Manifest.permission.CHANGE_BACKGROUND_DATA_SETTING,
988 "ConnectivityService");
Robert Greenwalt86e9e552009-07-16 17:21:39 -0700989
Robert Greenwaltd825ea42010-12-29 16:15:02 -0800990 mBackgroundDataEnabled.set(allowBackgroundDataUsage);
991
Robert Greenwalt8dcc28b2010-09-23 10:05:56 -0700992 mHandler.sendMessage(mHandler.obtainMessage(EVENT_SET_BACKGROUND_DATA,
993 (allowBackgroundDataUsage ? ENABLED : DISABLED), 0));
994 }
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800995
Robert Greenwalt8dcc28b2010-09-23 10:05:56 -0700996 private void handleSetBackgroundData(boolean enabled) {
Robert Greenwaltdb4afae2011-02-25 13:44:09 -0800997 Settings.Secure.putInt(mContext.getContentResolver(),
998 Settings.Secure.BACKGROUND_DATA, enabled ? 1 : 0);
999 Intent broadcast = new Intent(
1000 ConnectivityManager.ACTION_BACKGROUND_DATA_SETTING_CHANGED);
1001 mContext.sendBroadcast(broadcast);
Robert Greenwalt86e9e552009-07-16 17:21:39 -07001002 }
1003
Robert Greenwaltc03fa502010-02-23 18:58:05 -08001004 /**
1005 * @see ConnectivityManager#getMobileDataEnabled()
1006 */
1007 public boolean getMobileDataEnabled() {
Wink Savillee7982682010-12-07 10:31:02 -08001008 // TODO: This detail should probably be in DataConnectionTracker's
1009 // which is where we store the value and maybe make this
1010 // asynchronous.
Robert Greenwaltc03fa502010-02-23 18:58:05 -08001011 enforceAccessPermission();
1012 boolean retVal = Settings.Secure.getInt(mContext.getContentResolver(),
1013 Settings.Secure.MOBILE_DATA, 1) == 1;
Wink Savilleed9c02b2010-12-03 12:01:38 -08001014 if (DBG) log("getMobileDataEnabled returning " + retVal);
Robert Greenwaltc03fa502010-02-23 18:58:05 -08001015 return retVal;
1016 }
1017
1018 /**
1019 * @see ConnectivityManager#setMobileDataEnabled(boolean)
1020 */
Robert Greenwalt8dcc28b2010-09-23 10:05:56 -07001021 public void setMobileDataEnabled(boolean enabled) {
Robert Greenwaltc03fa502010-02-23 18:58:05 -08001022 enforceChangePermission();
Wink Savilleed9c02b2010-12-03 12:01:38 -08001023 if (DBG) log("setMobileDataEnabled(" + enabled + ")");
Robert Greenwaltc03fa502010-02-23 18:58:05 -08001024
Robert Greenwalt8dcc28b2010-09-23 10:05:56 -07001025 mHandler.sendMessage(mHandler.obtainMessage(EVENT_SET_MOBILE_DATA,
1026 (enabled ? ENABLED : DISABLED), 0));
1027 }
1028
1029 private void handleSetMobileData(boolean enabled) {
Wink Savillee7982682010-12-07 10:31:02 -08001030 if (mNetTrackers[ConnectivityManager.TYPE_MOBILE] != null) {
1031 if (DBG) {
1032 Slog.d(TAG, mNetTrackers[ConnectivityManager.TYPE_MOBILE].toString() + enabled);
Robert Greenwaltc03fa502010-02-23 18:58:05 -08001033 }
Wink Savillee7982682010-12-07 10:31:02 -08001034 mNetTrackers[ConnectivityManager.TYPE_MOBILE].setDataEnable(enabled);
Robert Greenwaltc03fa502010-02-23 18:58:05 -08001035 }
1036 }
1037
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001038 private void enforceAccessPermission() {
Robert Greenwalt86e9e552009-07-16 17:21:39 -07001039 mContext.enforceCallingOrSelfPermission(
1040 android.Manifest.permission.ACCESS_NETWORK_STATE,
1041 "ConnectivityService");
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001042 }
1043
1044 private void enforceChangePermission() {
Robert Greenwalt86e9e552009-07-16 17:21:39 -07001045 mContext.enforceCallingOrSelfPermission(
1046 android.Manifest.permission.CHANGE_NETWORK_STATE,
1047 "ConnectivityService");
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001048 }
1049
Robert Greenwaltd0e18ff2010-01-26 11:40:34 -08001050 // TODO Make this a special check when it goes public
1051 private void enforceTetherChangePermission() {
1052 mContext.enforceCallingOrSelfPermission(
1053 android.Manifest.permission.CHANGE_NETWORK_STATE,
1054 "ConnectivityService");
1055 }
1056
Robert Greenwalt2a091d72010-02-11 18:18:40 -08001057 private void enforceTetherAccessPermission() {
1058 mContext.enforceCallingOrSelfPermission(
1059 android.Manifest.permission.ACCESS_NETWORK_STATE,
1060 "ConnectivityService");
1061 }
1062
Robert Greenwalt14f2ef42010-06-15 12:19:37 -07001063 private void enforceConnectivityInternalPermission() {
1064 mContext.enforceCallingOrSelfPermission(
1065 android.Manifest.permission.CONNECTIVITY_INTERNAL,
1066 "ConnectivityService");
1067 }
1068
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001069 /**
Robert Greenwalt86e9e552009-07-16 17:21:39 -07001070 * Handle a {@code DISCONNECTED} event. If this pertains to the non-active
1071 * network, we ignore it. If it is for the active network, we send out a
1072 * broadcast. But first, we check whether it might be possible to connect
1073 * to a different network.
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001074 * @param info the {@code NetworkInfo} for the network
1075 */
1076 private void handleDisconnect(NetworkInfo info) {
1077
Robert Greenwalt42acef32009-08-12 16:08:25 -07001078 int prevNetType = info.getType();
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001079
Robert Greenwalt42acef32009-08-12 16:08:25 -07001080 mNetTrackers[prevNetType].setTeardownRequested(false);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001081 /*
1082 * If the disconnected network is not the active one, then don't report
1083 * this as a loss of connectivity. What probably happened is that we're
1084 * getting the disconnect for a network that we explicitly disabled
1085 * in accordance with network preference policies.
1086 */
Robert Greenwalt42acef32009-08-12 16:08:25 -07001087 if (!mNetAttributes[prevNetType].isDefault()) {
1088 List pids = mNetRequestersPids[prevNetType];
1089 for (int i = 0; i<pids.size(); i++) {
1090 Integer pid = (Integer)pids.get(i);
1091 // will remove them because the net's no longer connected
1092 // need to do this now as only now do we know the pids and
1093 // can properly null things that are no longer referenced.
1094 reassessPidDns(pid.intValue(), false);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001095 }
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001096 }
1097
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001098 Intent intent = new Intent(ConnectivityManager.CONNECTIVITY_ACTION);
1099 intent.putExtra(ConnectivityManager.EXTRA_NETWORK_INFO, info);
1100 if (info.isFailover()) {
1101 intent.putExtra(ConnectivityManager.EXTRA_IS_FAILOVER, true);
1102 info.setFailover(false);
1103 }
1104 if (info.getReason() != null) {
1105 intent.putExtra(ConnectivityManager.EXTRA_REASON, info.getReason());
1106 }
1107 if (info.getExtraInfo() != null) {
Robert Greenwalt86e9e552009-07-16 17:21:39 -07001108 intent.putExtra(ConnectivityManager.EXTRA_EXTRA_INFO,
1109 info.getExtraInfo());
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001110 }
Robert Greenwalt42acef32009-08-12 16:08:25 -07001111
Robert Greenwaltcc4b4012010-01-25 17:54:29 -08001112 if (mNetAttributes[prevNetType].isDefault()) {
Robert Greenwaltf21ef7d2011-01-11 13:56:33 -08001113 tryFailover(prevNetType);
1114 if (mActiveDefaultNetwork != -1) {
1115 NetworkInfo switchTo = mNetTrackers[mActiveDefaultNetwork].getNetworkInfo();
Robert Greenwaltcc4b4012010-01-25 17:54:29 -08001116 intent.putExtra(ConnectivityManager.EXTRA_OTHER_NETWORK_INFO, switchTo);
1117 } else {
Robert Greenwalt029be812010-09-20 18:01:43 -07001118 mDefaultInetConditionPublished = 0; // we're not connected anymore
Robert Greenwaltcc4b4012010-01-25 17:54:29 -08001119 intent.putExtra(ConnectivityManager.EXTRA_NO_CONNECTIVITY, true);
1120 }
Robert Greenwaltda03c4e2010-01-20 19:29:41 -08001121 }
Robert Greenwalt029be812010-09-20 18:01:43 -07001122 intent.putExtra(ConnectivityManager.EXTRA_INET_CONDITION, mDefaultInetConditionPublished);
Robert Greenwaltda03c4e2010-01-20 19:29:41 -08001123 // do this before we broadcast the change
Robert Greenwaltc76b8fa2010-07-23 15:46:26 -07001124 handleConnectivityChange(prevNetType);
Robert Greenwaltda03c4e2010-01-20 19:29:41 -08001125
1126 sendStickyBroadcast(intent);
1127 /*
1128 * If the failover network is already connected, then immediately send
1129 * out a followup broadcast indicating successful failover
1130 */
Robert Greenwaltf21ef7d2011-01-11 13:56:33 -08001131 if (mActiveDefaultNetwork != -1) {
1132 sendConnectedBroadcast(mNetTrackers[mActiveDefaultNetwork].getNetworkInfo());
Robert Greenwaltda03c4e2010-01-20 19:29:41 -08001133 }
1134 }
1135
Robert Greenwaltf21ef7d2011-01-11 13:56:33 -08001136 private void tryFailover(int prevNetType) {
Robert Greenwalt42acef32009-08-12 16:08:25 -07001137 /*
Robert Greenwaltbff90182011-01-06 15:41:07 -08001138 * If this is a default network, check if other defaults are available.
1139 * Try to reconnect on all available and let them hash it out when
1140 * more than one connects.
Robert Greenwalt42acef32009-08-12 16:08:25 -07001141 */
Robert Greenwalt42acef32009-08-12 16:08:25 -07001142 if (mNetAttributes[prevNetType].isDefault()) {
Robert Greenwalt42acef32009-08-12 16:08:25 -07001143 if (mActiveDefaultNetwork == prevNetType) {
1144 mActiveDefaultNetwork = -1;
1145 }
1146
Robert Greenwaltf21ef7d2011-01-11 13:56:33 -08001147 // don't signal a reconnect for anything lower or equal priority than our
1148 // current connected default
1149 // TODO - don't filter by priority now - nice optimization but risky
1150// int currentPriority = -1;
1151// if (mActiveDefaultNetwork != -1) {
1152// currentPriority = mNetAttributes[mActiveDefaultNetwork].mPriority;
1153// }
Robert Greenwaltda03c4e2010-01-20 19:29:41 -08001154 for (int checkType=0; checkType <= ConnectivityManager.MAX_NETWORK_TYPE; checkType++) {
Robert Greenwalt5154ae762009-10-30 14:17:42 -07001155 if (checkType == prevNetType) continue;
1156 if (mNetAttributes[checkType] == null) continue;
Robert Greenwaltbff90182011-01-06 15:41:07 -08001157 if (!mNetAttributes[checkType].isDefault()) continue;
Wink Saville9f7a0b22011-01-26 15:43:49 -08001158
1159// Enabling the isAvailable() optimization caused mobile to not get
1160// selected if it was in the middle of error handling. Specifically
1161// a moble connection that took 30 seconds to complete the DEACTIVATE_DATA_CALL
1162// would not be available and we wouldn't get connected to anything.
1163// So removing the isAvailable() optimization below for now. TODO: This
1164// optimization should work and we need to investigate why it doesn't work.
1165// This could be related to how DEACTIVATE_DATA_CALL is reporting its
1166// complete before it is really complete.
1167// if (!mNetTrackers[checkType].isAvailable()) continue;
1168
Robert Greenwaltf21ef7d2011-01-11 13:56:33 -08001169// if (currentPriority >= mNetAttributes[checkType].mPriority) continue;
Robert Greenwalt42acef32009-08-12 16:08:25 -07001170
Robert Greenwaltf21ef7d2011-01-11 13:56:33 -08001171 NetworkStateTracker checkTracker = mNetTrackers[checkType];
1172 NetworkInfo checkInfo = checkTracker.getNetworkInfo();
1173 if (!checkInfo.isConnectedOrConnecting() || checkTracker.isTeardownRequested()) {
1174 checkInfo.setFailover(true);
1175 checkTracker.reconnect();
Robert Greenwalt42acef32009-08-12 16:08:25 -07001176 }
Robert Greenwaltf21ef7d2011-01-11 13:56:33 -08001177 if (DBG) log("Attempting to switch to " + checkInfo.getTypeName());
Robert Greenwalt42acef32009-08-12 16:08:25 -07001178 }
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001179 }
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001180 }
1181
1182 private void sendConnectedBroadcast(NetworkInfo info) {
Robert Greenwalt1e9aac22010-09-15 17:36:33 -07001183 sendGeneralBroadcast(info, ConnectivityManager.CONNECTIVITY_ACTION);
1184 }
1185
1186 private void sendInetConditionBroadcast(NetworkInfo info) {
1187 sendGeneralBroadcast(info, ConnectivityManager.INET_CONDITION_ACTION);
1188 }
1189
1190 private void sendGeneralBroadcast(NetworkInfo info, String bcastType) {
1191 Intent intent = new Intent(bcastType);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001192 intent.putExtra(ConnectivityManager.EXTRA_NETWORK_INFO, info);
1193 if (info.isFailover()) {
1194 intent.putExtra(ConnectivityManager.EXTRA_IS_FAILOVER, true);
1195 info.setFailover(false);
1196 }
1197 if (info.getReason() != null) {
1198 intent.putExtra(ConnectivityManager.EXTRA_REASON, info.getReason());
1199 }
1200 if (info.getExtraInfo() != null) {
Robert Greenwalt86e9e552009-07-16 17:21:39 -07001201 intent.putExtra(ConnectivityManager.EXTRA_EXTRA_INFO,
1202 info.getExtraInfo());
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001203 }
Robert Greenwaltd7085fc2010-09-08 15:24:47 -07001204 intent.putExtra(ConnectivityManager.EXTRA_INET_CONDITION, mDefaultInetConditionPublished);
Mike Lockwood0f79b542009-08-14 14:18:49 -04001205 sendStickyBroadcast(intent);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001206 }
1207
1208 /**
1209 * Called when an attempt to fail over to another network has failed.
1210 * @param info the {@link NetworkInfo} for the failed network
1211 */
1212 private void handleConnectionFailure(NetworkInfo info) {
1213 mNetTrackers[info.getType()].setTeardownRequested(false);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001214
Robert Greenwalt42acef32009-08-12 16:08:25 -07001215 String reason = info.getReason();
1216 String extraInfo = info.getExtraInfo();
Robert Greenwalt86e9e552009-07-16 17:21:39 -07001217
Robert Greenwalt572172b2010-10-08 16:35:52 -07001218 String reasonText;
1219 if (reason == null) {
1220 reasonText = ".";
1221 } else {
1222 reasonText = " (" + reason + ").";
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001223 }
Wink Savilleed9c02b2010-12-03 12:01:38 -08001224 loge("Attempt to connect to " + info.getTypeName() + " failed" + reasonText);
Robert Greenwalt42acef32009-08-12 16:08:25 -07001225
1226 Intent intent = new Intent(ConnectivityManager.CONNECTIVITY_ACTION);
1227 intent.putExtra(ConnectivityManager.EXTRA_NETWORK_INFO, info);
1228 if (getActiveNetworkInfo() == null) {
1229 intent.putExtra(ConnectivityManager.EXTRA_NO_CONNECTIVITY, true);
1230 }
1231 if (reason != null) {
1232 intent.putExtra(ConnectivityManager.EXTRA_REASON, reason);
1233 }
1234 if (extraInfo != null) {
1235 intent.putExtra(ConnectivityManager.EXTRA_EXTRA_INFO, extraInfo);
1236 }
1237 if (info.isFailover()) {
1238 intent.putExtra(ConnectivityManager.EXTRA_IS_FAILOVER, true);
1239 info.setFailover(false);
1240 }
Robert Greenwaltda03c4e2010-01-20 19:29:41 -08001241
Robert Greenwaltcc4b4012010-01-25 17:54:29 -08001242 if (mNetAttributes[info.getType()].isDefault()) {
Robert Greenwaltf21ef7d2011-01-11 13:56:33 -08001243 tryFailover(info.getType());
1244 if (mActiveDefaultNetwork != -1) {
1245 NetworkInfo switchTo = mNetTrackers[mActiveDefaultNetwork].getNetworkInfo();
Robert Greenwaltcc4b4012010-01-25 17:54:29 -08001246 intent.putExtra(ConnectivityManager.EXTRA_OTHER_NETWORK_INFO, switchTo);
1247 } else {
Robert Greenwalt029be812010-09-20 18:01:43 -07001248 mDefaultInetConditionPublished = 0;
Robert Greenwaltcc4b4012010-01-25 17:54:29 -08001249 intent.putExtra(ConnectivityManager.EXTRA_NO_CONNECTIVITY, true);
1250 }
Robert Greenwaltda03c4e2010-01-20 19:29:41 -08001251 }
Robert Greenwaltcc4b4012010-01-25 17:54:29 -08001252
Robert Greenwalt029be812010-09-20 18:01:43 -07001253 intent.putExtra(ConnectivityManager.EXTRA_INET_CONDITION, mDefaultInetConditionPublished);
Mike Lockwood0f79b542009-08-14 14:18:49 -04001254 sendStickyBroadcast(intent);
Robert Greenwaltda03c4e2010-01-20 19:29:41 -08001255 /*
1256 * If the failover network is already connected, then immediately send
1257 * out a followup broadcast indicating successful failover
1258 */
Robert Greenwaltf21ef7d2011-01-11 13:56:33 -08001259 if (mActiveDefaultNetwork != -1) {
1260 sendConnectedBroadcast(mNetTrackers[mActiveDefaultNetwork].getNetworkInfo());
Robert Greenwaltda03c4e2010-01-20 19:29:41 -08001261 }
Mike Lockwood0f79b542009-08-14 14:18:49 -04001262 }
1263
1264 private void sendStickyBroadcast(Intent intent) {
1265 synchronized(this) {
Dianne Hackborn1c633fc2009-12-08 19:45:14 -08001266 if (!mSystemReady) {
1267 mInitialBroadcast = new Intent(intent);
Mike Lockwood0f79b542009-08-14 14:18:49 -04001268 }
Dianne Hackborn1c633fc2009-12-08 19:45:14 -08001269 intent.addFlags(Intent.FLAG_RECEIVER_REGISTERED_ONLY_BEFORE_BOOT);
1270 mContext.sendStickyBroadcast(intent);
Mike Lockwood0f79b542009-08-14 14:18:49 -04001271 }
1272 }
1273
1274 void systemReady() {
1275 synchronized(this) {
1276 mSystemReady = true;
Dianne Hackborn1c633fc2009-12-08 19:45:14 -08001277 if (mInitialBroadcast != null) {
1278 mContext.sendStickyBroadcast(mInitialBroadcast);
1279 mInitialBroadcast = null;
Mike Lockwood0f79b542009-08-14 14:18:49 -04001280 }
1281 }
Robert Greenwalt434203a2010-10-11 16:00:27 -07001282 // load the global proxy at startup
1283 mHandler.sendMessage(mHandler.obtainMessage(EVENT_APPLY_GLOBAL_HTTP_PROXY));
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001284 }
1285
1286 private void handleConnect(NetworkInfo info) {
Robert Greenwalt42acef32009-08-12 16:08:25 -07001287 int type = info.getType();
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001288
1289 // snapshot isFailover, because sendConnectedBroadcast() resets it
1290 boolean isFailover = info.isFailover();
Robert Greenwalt42acef32009-08-12 16:08:25 -07001291 NetworkStateTracker thisNet = mNetTrackers[type];
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001292
Robert Greenwalt42acef32009-08-12 16:08:25 -07001293 // if this is a default net and other default is running
1294 // kill the one not preferred
1295 if (mNetAttributes[type].isDefault()) {
Robert Greenwalt42acef32009-08-12 16:08:25 -07001296 if (mActiveDefaultNetwork != -1 && mActiveDefaultNetwork != type) {
1297 if ((type != mNetworkPreference &&
1298 mNetAttributes[mActiveDefaultNetwork].mPriority >
1299 mNetAttributes[type].mPriority) ||
1300 mNetworkPreference == mActiveDefaultNetwork) {
1301 // don't accept this one
Wink Savilleed9c02b2010-12-03 12:01:38 -08001302 if (DBG) {
1303 log("Not broadcasting CONNECT_ACTION " +
Robert Greenwalt42acef32009-08-12 16:08:25 -07001304 "to torn down network " + info.getTypeName());
Wink Savilleed9c02b2010-12-03 12:01:38 -08001305 }
Robert Greenwalt42acef32009-08-12 16:08:25 -07001306 teardown(thisNet);
1307 return;
1308 } else {
1309 // tear down the other
1310 NetworkStateTracker otherNet =
1311 mNetTrackers[mActiveDefaultNetwork];
Wink Savilleed9c02b2010-12-03 12:01:38 -08001312 if (DBG) {
1313 log("Policy requires " + otherNet.getNetworkInfo().getTypeName() +
Robert Greenwalt42acef32009-08-12 16:08:25 -07001314 " teardown");
Wink Savilleed9c02b2010-12-03 12:01:38 -08001315 }
Robert Greenwalt42acef32009-08-12 16:08:25 -07001316 if (!teardown(otherNet)) {
Wink Savilleed9c02b2010-12-03 12:01:38 -08001317 loge("Network declined teardown request");
Robert Greenwalt42acef32009-08-12 16:08:25 -07001318 return;
1319 }
Robert Greenwalt14f2ef42010-06-15 12:19:37 -07001320 }
1321 }
1322 synchronized (ConnectivityService.this) {
1323 // have a new default network, release the transition wakelock in a second
1324 // if it's held. The second pause is to allow apps to reconnect over the
1325 // new network
1326 if (mNetTransitionWakeLock.isHeld()) {
1327 mHandler.sendMessageDelayed(mHandler.obtainMessage(
Robert Greenwaltf3331232010-09-24 14:32:21 -07001328 EVENT_CLEAR_NET_TRANSITION_WAKELOCK,
Robert Greenwalt14f2ef42010-06-15 12:19:37 -07001329 mNetTransitionWakeLockSerialNumber, 0),
1330 1000);
Robert Greenwalt42acef32009-08-12 16:08:25 -07001331 }
1332 }
1333 mActiveDefaultNetwork = type;
Robert Greenwaltd7085fc2010-09-08 15:24:47 -07001334 // this will cause us to come up initially as unconnected and switching
1335 // to connected after our normal pause unless somebody reports us as reall
1336 // disconnected
1337 mDefaultInetConditionPublished = 0;
1338 mDefaultConnectionSequence++;
1339 mInetConditionChangeInFlight = false;
1340 // Don't do this - if we never sign in stay, grey
1341 //reportNetworkCondition(mActiveDefaultNetwork, 100);
Robert Greenwalt42acef32009-08-12 16:08:25 -07001342 }
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001343 thisNet.setTeardownRequested(false);
Irfan Sheriffd649c122010-06-09 15:39:36 -07001344 updateNetworkSettings(thisNet);
Robert Greenwaltc76b8fa2010-07-23 15:46:26 -07001345 handleConnectivityChange(type);
Robert Greenwalt42acef32009-08-12 16:08:25 -07001346 sendConnectedBroadcast(info);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001347 }
1348
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001349 /**
Robert Greenwaltc76b8fa2010-07-23 15:46:26 -07001350 * After a change in the connectivity state of a network. We're mainly
1351 * concerned with making sure that the list of DNS servers is set up
1352 * according to which networks are connected, and ensuring that the
1353 * right routing table entries exist.
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001354 */
Robert Greenwaltc76b8fa2010-07-23 15:46:26 -07001355 private void handleConnectivityChange(int netType) {
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001356 /*
Robert Greenwalt42acef32009-08-12 16:08:25 -07001357 * If a non-default network is enabled, add the host routes that
Robert Greenwaltc76b8fa2010-07-23 15:46:26 -07001358 * will allow it's DNS servers to be accessed.
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001359 */
Robert Greenwaltc76b8fa2010-07-23 15:46:26 -07001360 handleDnsConfigurationChange(netType);
Robert Greenwalt42acef32009-08-12 16:08:25 -07001361
Robert Greenwaltc76b8fa2010-07-23 15:46:26 -07001362 if (mNetTrackers[netType].getNetworkInfo().isConnected()) {
1363 if (mNetAttributes[netType].isDefault()) {
Robert Greenwalt434203a2010-10-11 16:00:27 -07001364 handleApplyDefaultProxy(netType);
Robert Greenwaltc76b8fa2010-07-23 15:46:26 -07001365 addDefaultRoute(mNetTrackers[netType]);
Robert Greenwalt42acef32009-08-12 16:08:25 -07001366 } else {
Robert Greenwaltc76b8fa2010-07-23 15:46:26 -07001367 addPrivateDnsRoutes(mNetTrackers[netType]);
1368 }
1369 } else {
1370 if (mNetAttributes[netType].isDefault()) {
1371 removeDefaultRoute(mNetTrackers[netType]);
1372 } else {
1373 removePrivateDnsRoutes(mNetTrackers[netType]);
Robert Greenwalt42acef32009-08-12 16:08:25 -07001374 }
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001375 }
1376 }
1377
Irfan Sheriffd649c122010-06-09 15:39:36 -07001378 private void addPrivateDnsRoutes(NetworkStateTracker nt) {
Irfan Sheriffd649c122010-06-09 15:39:36 -07001379 boolean privateDnsRouteSet = nt.isPrivateDnsRouteSet();
Robert Greenwalt37e65eb2010-08-30 10:56:47 -07001380 LinkProperties p = nt.getLinkProperties();
Robert Greenwalt47f69fe2010-06-15 15:43:39 -07001381 if (p == null) return;
1382 String interfaceName = p.getInterfaceName();
Irfan Sheriffd649c122010-06-09 15:39:36 -07001383
1384 if (DBG) {
Wink Savilleed9c02b2010-12-03 12:01:38 -08001385 log("addPrivateDnsRoutes for " + nt +
Irfan Sheriffd649c122010-06-09 15:39:36 -07001386 "(" + interfaceName + ") - mPrivateDnsRouteSet = " + privateDnsRouteSet);
1387 }
Irfan Sheriffd649c122010-06-09 15:39:36 -07001388 if (interfaceName != null && !privateDnsRouteSet) {
Robert Greenwalt47f69fe2010-06-15 15:43:39 -07001389 Collection<InetAddress> dnsList = p.getDnses();
1390 for (InetAddress dns : dnsList) {
Wink Savilleed9c02b2010-12-03 12:01:38 -08001391 if (DBG) log(" adding " + dns);
Robert Greenwalt585ac0f2010-08-27 09:24:29 -07001392 NetworkUtils.addHostRoute(interfaceName, dns, null);
Irfan Sheriffd649c122010-06-09 15:39:36 -07001393 }
1394 nt.privateDnsRouteSet(true);
1395 }
1396 }
1397
1398 private void removePrivateDnsRoutes(NetworkStateTracker nt) {
1399 // TODO - we should do this explicitly but the NetUtils api doesnt
1400 // support this yet - must remove all. No worse than before
Robert Greenwalt37e65eb2010-08-30 10:56:47 -07001401 LinkProperties p = nt.getLinkProperties();
Robert Greenwalt47f69fe2010-06-15 15:43:39 -07001402 if (p == null) return;
1403 String interfaceName = p.getInterfaceName();
Irfan Sheriffd649c122010-06-09 15:39:36 -07001404 boolean privateDnsRouteSet = nt.isPrivateDnsRouteSet();
1405 if (interfaceName != null && privateDnsRouteSet) {
1406 if (DBG) {
Wink Savilleed9c02b2010-12-03 12:01:38 -08001407 log("removePrivateDnsRoutes for " + nt.getNetworkInfo().getTypeName() +
Irfan Sheriffd649c122010-06-09 15:39:36 -07001408 " (" + interfaceName + ")");
1409 }
1410 NetworkUtils.removeHostRoutes(interfaceName);
1411 nt.privateDnsRouteSet(false);
1412 }
1413 }
1414
Irfan Sheriffd649c122010-06-09 15:39:36 -07001415
1416 private void addDefaultRoute(NetworkStateTracker nt) {
Robert Greenwalt37e65eb2010-08-30 10:56:47 -07001417 LinkProperties p = nt.getLinkProperties();
Robert Greenwalt47f69fe2010-06-15 15:43:39 -07001418 if (p == null) return;
1419 String interfaceName = p.getInterfaceName();
Robert Greenwalt992564e2011-02-09 13:56:06 -08001420 if (TextUtils.isEmpty(interfaceName)) return;
1421 for (InetAddress gateway : p.getGateways()) {
Irfan Sheriffd649c122010-06-09 15:39:36 -07001422
Robert Greenwaltedcb4f92011-03-22 18:47:42 -07001423 if (NetworkUtils.addHostRoute(interfaceName, gateway, null) &&
1424 NetworkUtils.addDefaultRoute(interfaceName, gateway)) {
1425 if (DBG) {
1426 NetworkInfo networkInfo = nt.getNetworkInfo();
1427 log("addDefaultRoute for " + networkInfo.getTypeName() +
1428 " (" + interfaceName + "), GatewayAddr=" + gateway.getHostAddress());
1429 }
Irfan Sheriffd649c122010-06-09 15:39:36 -07001430 }
Irfan Sheriffd649c122010-06-09 15:39:36 -07001431 }
1432 }
1433
1434
1435 public void removeDefaultRoute(NetworkStateTracker nt) {
Robert Greenwalt37e65eb2010-08-30 10:56:47 -07001436 LinkProperties p = nt.getLinkProperties();
Robert Greenwalt47f69fe2010-06-15 15:43:39 -07001437 if (p == null) return;
1438 String interfaceName = p.getInterfaceName();
Irfan Sheriffd649c122010-06-09 15:39:36 -07001439
Robert Greenwaltc76b8fa2010-07-23 15:46:26 -07001440 if (interfaceName != null) {
Robert Greenwaltedcb4f92011-03-22 18:47:42 -07001441 if (NetworkUtils.removeDefaultRoute(interfaceName) >= 0) {
1442 if (DBG) {
1443 NetworkInfo networkInfo = nt.getNetworkInfo();
1444 log("removeDefaultRoute for " + networkInfo.getTypeName() + " (" +
1445 interfaceName + ")");
1446 }
Irfan Sheriffd649c122010-06-09 15:39:36 -07001447 }
Irfan Sheriffd649c122010-06-09 15:39:36 -07001448 }
1449 }
1450
1451 /**
1452 * Reads the network specific TCP buffer sizes from SystemProperties
1453 * net.tcp.buffersize.[default|wifi|umts|edge|gprs] and set them for system
1454 * wide use
1455 */
1456 public void updateNetworkSettings(NetworkStateTracker nt) {
1457 String key = nt.getTcpBufferSizesPropName();
1458 String bufferSizes = SystemProperties.get(key);
1459
1460 if (bufferSizes.length() == 0) {
Wink Savilleed9c02b2010-12-03 12:01:38 -08001461 loge(key + " not found in system properties. Using defaults");
Irfan Sheriffd649c122010-06-09 15:39:36 -07001462
1463 // Setting to default values so we won't be stuck to previous values
1464 key = "net.tcp.buffersize.default";
1465 bufferSizes = SystemProperties.get(key);
1466 }
1467
1468 // Set values in kernel
1469 if (bufferSizes.length() != 0) {
1470 if (DBG) {
Wink Savilleed9c02b2010-12-03 12:01:38 -08001471 log("Setting TCP values: [" + bufferSizes
Irfan Sheriffd649c122010-06-09 15:39:36 -07001472 + "] which comes from [" + key + "]");
1473 }
1474 setBufferSize(bufferSizes);
1475 }
1476 }
1477
1478 /**
1479 * Writes TCP buffer sizes to /sys/kernel/ipv4/tcp_[r/w]mem_[min/def/max]
1480 * which maps to /proc/sys/net/ipv4/tcp_rmem and tcpwmem
1481 *
1482 * @param bufferSizes in the format of "readMin, readInitial, readMax,
1483 * writeMin, writeInitial, writeMax"
1484 */
1485 private void setBufferSize(String bufferSizes) {
1486 try {
1487 String[] values = bufferSizes.split(",");
1488
1489 if (values.length == 6) {
1490 final String prefix = "/sys/kernel/ipv4/tcp_";
1491 stringToFile(prefix + "rmem_min", values[0]);
1492 stringToFile(prefix + "rmem_def", values[1]);
1493 stringToFile(prefix + "rmem_max", values[2]);
1494 stringToFile(prefix + "wmem_min", values[3]);
1495 stringToFile(prefix + "wmem_def", values[4]);
1496 stringToFile(prefix + "wmem_max", values[5]);
1497 } else {
Wink Savilleed9c02b2010-12-03 12:01:38 -08001498 loge("Invalid buffersize string: " + bufferSizes);
Irfan Sheriffd649c122010-06-09 15:39:36 -07001499 }
1500 } catch (IOException e) {
Wink Savilleed9c02b2010-12-03 12:01:38 -08001501 loge("Can't set tcp buffer sizes:" + e);
Irfan Sheriffd649c122010-06-09 15:39:36 -07001502 }
1503 }
1504
1505 /**
1506 * Writes string to file. Basically same as "echo -n $string > $filename"
1507 *
1508 * @param filename
1509 * @param string
1510 * @throws IOException
1511 */
1512 private void stringToFile(String filename, String string) throws IOException {
1513 FileWriter out = new FileWriter(filename);
1514 try {
1515 out.write(string);
1516 } finally {
1517 out.close();
1518 }
1519 }
1520
1521
Robert Greenwalt42acef32009-08-12 16:08:25 -07001522 /**
1523 * Adjust the per-process dns entries (net.dns<x>.<pid>) based
1524 * on the highest priority active net which this process requested.
1525 * If there aren't any, clear it out
1526 */
1527 private void reassessPidDns(int myPid, boolean doBump)
1528 {
Wink Savilleed9c02b2010-12-03 12:01:38 -08001529 if (DBG) log("reassessPidDns for pid " + myPid);
Robert Greenwalt42acef32009-08-12 16:08:25 -07001530 for(int i : mPriorityList) {
1531 if (mNetAttributes[i].isDefault()) {
1532 continue;
1533 }
1534 NetworkStateTracker nt = mNetTrackers[i];
Robert Greenwalt86e9e552009-07-16 17:21:39 -07001535 if (nt.getNetworkInfo().isConnected() &&
1536 !nt.isTeardownRequested()) {
Robert Greenwalt37e65eb2010-08-30 10:56:47 -07001537 LinkProperties p = nt.getLinkProperties();
Robert Greenwalt47f69fe2010-06-15 15:43:39 -07001538 if (p == null) continue;
Robert Greenwalt42acef32009-08-12 16:08:25 -07001539 List pids = mNetRequestersPids[i];
1540 for (int j=0; j<pids.size(); j++) {
1541 Integer pid = (Integer)pids.get(j);
1542 if (pid.intValue() == myPid) {
Robert Greenwalt47f69fe2010-06-15 15:43:39 -07001543 Collection<InetAddress> dnses = p.getDnses();
1544 writePidDns(dnses, myPid);
Robert Greenwalt42acef32009-08-12 16:08:25 -07001545 if (doBump) {
1546 bumpDns();
1547 }
1548 return;
1549 }
1550 }
1551 }
1552 }
1553 // nothing found - delete
1554 for (int i = 1; ; i++) {
1555 String prop = "net.dns" + i + "." + myPid;
1556 if (SystemProperties.get(prop).length() == 0) {
1557 if (doBump) {
1558 bumpDns();
1559 }
1560 return;
1561 }
1562 SystemProperties.set(prop, "");
1563 }
1564 }
1565
Robert Greenwalt10398722010-12-17 15:20:36 -08001566 // return true if results in a change
1567 private boolean writePidDns(Collection <InetAddress> dnses, int pid) {
Robert Greenwalt42acef32009-08-12 16:08:25 -07001568 int j = 1;
Robert Greenwalt10398722010-12-17 15:20:36 -08001569 boolean changed = false;
Robert Greenwalt47f69fe2010-06-15 15:43:39 -07001570 for (InetAddress dns : dnses) {
Robert Greenwalt10398722010-12-17 15:20:36 -08001571 String dnsString = dns.getHostAddress();
1572 if (changed || !dnsString.equals(SystemProperties.get("net.dns" + j + "." + pid))) {
1573 changed = true;
1574 SystemProperties.set("net.dns" + j++ + "." + pid, dns.getHostAddress());
1575 }
Robert Greenwalt42acef32009-08-12 16:08:25 -07001576 }
Robert Greenwalt10398722010-12-17 15:20:36 -08001577 return changed;
Robert Greenwalt42acef32009-08-12 16:08:25 -07001578 }
1579
1580 private void bumpDns() {
1581 /*
1582 * Bump the property that tells the name resolver library to reread
1583 * the DNS server list from the properties.
1584 */
1585 String propVal = SystemProperties.get("net.dnschange");
1586 int n = 0;
1587 if (propVal.length() != 0) {
1588 try {
1589 n = Integer.parseInt(propVal);
1590 } catch (NumberFormatException e) {}
1591 }
1592 SystemProperties.set("net.dnschange", "" + (n+1));
Robert Greenwalt03595d02010-11-02 14:08:23 -07001593 /*
1594 * Tell the VMs to toss their DNS caches
1595 */
1596 Intent intent = new Intent(Intent.ACTION_CLEAR_DNS_CACHE);
1597 intent.addFlags(Intent.FLAG_RECEIVER_REPLACE_PENDING);
Stan Chesnutt3d1db862011-01-05 17:14:03 -08001598 /*
1599 * Connectivity events can happen before boot has completed ...
1600 */
1601 intent.addFlags(Intent.FLAG_RECEIVER_REGISTERED_ONLY_BEFORE_BOOT);
Robert Greenwalt03595d02010-11-02 14:08:23 -07001602 mContext.sendBroadcast(intent);
Robert Greenwalt42acef32009-08-12 16:08:25 -07001603 }
1604
Robert Greenwaltc76b8fa2010-07-23 15:46:26 -07001605 private void handleDnsConfigurationChange(int netType) {
Robert Greenwalt42acef32009-08-12 16:08:25 -07001606 // add default net's dns entries
Robert Greenwaltc76b8fa2010-07-23 15:46:26 -07001607 NetworkStateTracker nt = mNetTrackers[netType];
1608 if (nt != null && nt.getNetworkInfo().isConnected() && !nt.isTeardownRequested()) {
Robert Greenwalt37e65eb2010-08-30 10:56:47 -07001609 LinkProperties p = nt.getLinkProperties();
Robert Greenwaltc76b8fa2010-07-23 15:46:26 -07001610 if (p == null) return;
1611 Collection<InetAddress> dnses = p.getDnses();
Robert Greenwalt10398722010-12-17 15:20:36 -08001612 boolean changed = false;
Robert Greenwaltc76b8fa2010-07-23 15:46:26 -07001613 if (mNetAttributes[netType].isDefault()) {
1614 int j = 1;
Robert Greenwalte90aa5e2010-09-01 11:34:05 -07001615 if (dnses.size() == 0 && mDefaultDns != null) {
Robert Greenwalt10398722010-12-17 15:20:36 -08001616 String dnsString = mDefaultDns.getHostAddress();
1617 if (!dnsString.equals(SystemProperties.get("net.dns1"))) {
1618 if (DBG) {
1619 log("no dns provided - using " + dnsString);
1620 }
1621 changed = true;
1622 SystemProperties.set("net.dns1", dnsString);
Robert Greenwalt42acef32009-08-12 16:08:25 -07001623 }
Robert Greenwalte90aa5e2010-09-01 11:34:05 -07001624 j++;
1625 } else {
1626 for (InetAddress dns : dnses) {
Robert Greenwalt10398722010-12-17 15:20:36 -08001627 String dnsString = dns.getHostAddress();
1628 if (!changed && dnsString.equals(SystemProperties.get("net.dns" + j))) {
1629 j++;
1630 continue;
1631 }
Robert Greenwalte90aa5e2010-09-01 11:34:05 -07001632 if (DBG) {
Wink Savilleed9c02b2010-12-03 12:01:38 -08001633 log("adding dns " + dns + " for " +
Robert Greenwalte90aa5e2010-09-01 11:34:05 -07001634 nt.getNetworkInfo().getTypeName());
1635 }
Robert Greenwalt10398722010-12-17 15:20:36 -08001636 changed = true;
1637 SystemProperties.set("net.dns" + j++, dnsString);
Robert Greenwalte90aa5e2010-09-01 11:34:05 -07001638 }
Robert Greenwaltc76b8fa2010-07-23 15:46:26 -07001639 }
1640 for (int k=j ; k<mNumDnsEntries; k++) {
Robert Greenwalt10398722010-12-17 15:20:36 -08001641 if (changed || !TextUtils.isEmpty(SystemProperties.get("net.dns" + k))) {
1642 if (DBG) log("erasing net.dns" + k);
1643 changed = true;
1644 SystemProperties.set("net.dns" + k, "");
1645 }
Robert Greenwaltc76b8fa2010-07-23 15:46:26 -07001646 }
1647 mNumDnsEntries = j;
1648 } else {
1649 // set per-pid dns for attached secondary nets
1650 List pids = mNetRequestersPids[netType];
1651 for (int y=0; y< pids.size(); y++) {
1652 Integer pid = (Integer)pids.get(y);
Robert Greenwalt10398722010-12-17 15:20:36 -08001653 changed = writePidDns(dnses, pid.intValue());
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001654 }
1655 }
Robert Greenwalt10398722010-12-17 15:20:36 -08001656 if (changed) bumpDns();
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001657 }
Robert Greenwalt42acef32009-08-12 16:08:25 -07001658 }
1659
1660 private int getRestoreDefaultNetworkDelay() {
1661 String restoreDefaultNetworkDelayStr = SystemProperties.get(
1662 NETWORK_RESTORE_DELAY_PROP_NAME);
1663 if(restoreDefaultNetworkDelayStr != null &&
1664 restoreDefaultNetworkDelayStr.length() != 0) {
1665 try {
1666 return Integer.valueOf(restoreDefaultNetworkDelayStr);
1667 } catch (NumberFormatException e) {
1668 }
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001669 }
Robert Greenwalt42acef32009-08-12 16:08:25 -07001670 return RESTORE_DEFAULT_NETWORK_DELAY;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001671 }
1672
1673 @Override
1674 protected void dump(FileDescriptor fd, PrintWriter pw, String[] args) {
Robert Greenwalt86e9e552009-07-16 17:21:39 -07001675 if (mContext.checkCallingOrSelfPermission(
1676 android.Manifest.permission.DUMP)
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001677 != PackageManager.PERMISSION_GRANTED) {
Robert Greenwalt86e9e552009-07-16 17:21:39 -07001678 pw.println("Permission Denial: can't dump ConnectivityService " +
1679 "from from pid=" + Binder.getCallingPid() + ", uid=" +
1680 Binder.getCallingUid());
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001681 return;
1682 }
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001683 pw.println();
1684 for (NetworkStateTracker nst : mNetTrackers) {
Robert Greenwaltb9285352009-12-21 18:24:07 -08001685 if (nst != null) {
1686 if (nst.getNetworkInfo().isConnected()) {
1687 pw.println("Active network: " + nst.getNetworkInfo().
1688 getTypeName());
1689 }
1690 pw.println(nst.getNetworkInfo());
1691 pw.println(nst);
1692 pw.println();
Robert Greenwalt42acef32009-08-12 16:08:25 -07001693 }
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001694 }
Robert Greenwaltb9285352009-12-21 18:24:07 -08001695
1696 pw.println("Network Requester Pids:");
1697 for (int net : mPriorityList) {
1698 String pidString = net + ": ";
1699 for (Object pid : mNetRequestersPids[net]) {
1700 pidString = pidString + pid.toString() + ", ";
1701 }
1702 pw.println(pidString);
1703 }
1704 pw.println();
1705
1706 pw.println("FeatureUsers:");
1707 for (Object requester : mFeatureUsers) {
1708 pw.println(requester.toString());
1709 }
1710 pw.println();
Robert Greenwalt2a091d72010-02-11 18:18:40 -08001711
Robert Greenwalt14f2ef42010-06-15 12:19:37 -07001712 synchronized (this) {
1713 pw.println("NetworkTranstionWakeLock is currently " +
1714 (mNetTransitionWakeLock.isHeld() ? "" : "not ") + "held.");
1715 pw.println("It was last requested for "+mNetTransitionWakeLockCausedBy);
1716 }
1717 pw.println();
1718
Robert Greenwalt2a091d72010-02-11 18:18:40 -08001719 mTethering.dump(fd, pw, args);
Robert Greenwalt4e8dfef2010-09-20 14:35:25 -07001720
1721 if (mInetLog != null) {
1722 pw.println();
1723 pw.println("Inet condition reports:");
1724 for(int i = 0; i < mInetLog.size(); i++) {
1725 pw.println(mInetLog.get(i));
1726 }
1727 }
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001728 }
1729
Robert Greenwalt42acef32009-08-12 16:08:25 -07001730 // must be stateless - things change under us.
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001731 private class MyHandler extends Handler {
Wink Savillebb08caf2010-09-02 19:23:52 -07001732 public MyHandler(Looper looper) {
1733 super(looper);
1734 }
1735
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001736 @Override
1737 public void handleMessage(Message msg) {
1738 NetworkInfo info;
1739 switch (msg.what) {
1740 case NetworkStateTracker.EVENT_STATE_CHANGED:
1741 info = (NetworkInfo) msg.obj;
Robert Greenwalt511288a2009-12-07 11:33:18 -08001742 int type = info.getType();
1743 NetworkInfo.State state = info.getState();
Robert Greenwalt6e6dec22010-01-25 16:14:00 -08001744 // only do this optimization for wifi. It going into scan mode for location
1745 // services generates alot of noise. Meanwhile the mms apn won't send out
1746 // subsequent notifications when on default cellular because it never
1747 // disconnects.. so only do this to wifi notifications. Fixed better when the
1748 // APN notifications are standardized.
1749 if (mNetAttributes[type].mLastState == state &&
1750 mNetAttributes[type].mRadio == ConnectivityManager.TYPE_WIFI) {
Robert Greenwalt511288a2009-12-07 11:33:18 -08001751 if (DBG) {
Robert Greenwalt6e6dec22010-01-25 16:14:00 -08001752 // TODO - remove this after we validate the dropping doesn't break
1753 // anything
Wink Savilleed9c02b2010-12-03 12:01:38 -08001754 log("Dropping ConnectivityChange for " +
Robert Greenwalt1193ae42010-01-13 09:36:31 -08001755 info.getTypeName() + ": " +
Robert Greenwalt511288a2009-12-07 11:33:18 -08001756 state + "/" + info.getDetailedState());
1757 }
1758 return;
1759 }
1760 mNetAttributes[type].mLastState = state;
1761
Wink Savilleed9c02b2010-12-03 12:01:38 -08001762 if (DBG) log("ConnectivityChange for " +
Robert Greenwalt86e9e552009-07-16 17:21:39 -07001763 info.getTypeName() + ": " +
Robert Greenwalt511288a2009-12-07 11:33:18 -08001764 state + "/" + info.getDetailedState());
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001765
1766 // Connectivity state changed:
1767 // [31-13] Reserved for future use
Robert Greenwalt86e9e552009-07-16 17:21:39 -07001768 // [12-9] Network subtype (for mobile network, as defined
1769 // by TelephonyManager)
1770 // [8-3] Detailed state ordinal (as defined by
1771 // NetworkInfo.DetailedState)
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001772 // [2-0] Network type (as defined by ConnectivityManager)
1773 int eventLogParam = (info.getType() & 0x7) |
1774 ((info.getDetailedState().ordinal() & 0x3f) << 3) |
1775 (info.getSubtype() << 9);
Doug Zongkerab5c49c2009-12-04 10:31:43 -08001776 EventLog.writeEvent(EventLogTags.CONNECTIVITY_STATE_CHANGED,
Robert Greenwalt86e9e552009-07-16 17:21:39 -07001777 eventLogParam);
1778
1779 if (info.getDetailedState() ==
1780 NetworkInfo.DetailedState.FAILED) {
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001781 handleConnectionFailure(info);
Robert Greenwalt511288a2009-12-07 11:33:18 -08001782 } else if (state == NetworkInfo.State.DISCONNECTED) {
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001783 handleDisconnect(info);
Robert Greenwalt511288a2009-12-07 11:33:18 -08001784 } else if (state == NetworkInfo.State.SUSPENDED) {
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001785 // TODO: need to think this over.
Robert Greenwalt86e9e552009-07-16 17:21:39 -07001786 // the logic here is, handle SUSPENDED the same as
1787 // DISCONNECTED. The only difference being we are
1788 // broadcasting an intent with NetworkInfo that's
1789 // suspended. This allows the applications an
1790 // opportunity to handle DISCONNECTED and SUSPENDED
1791 // differently, or not.
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001792 handleDisconnect(info);
Robert Greenwalt511288a2009-12-07 11:33:18 -08001793 } else if (state == NetworkInfo.State.CONNECTED) {
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001794 handleConnect(info);
1795 }
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001796 break;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001797 case NetworkStateTracker.EVENT_CONFIGURATION_CHANGED:
Robert Greenwaltc76b8fa2010-07-23 15:46:26 -07001798 info = (NetworkInfo) msg.obj;
1799 type = info.getType();
Robert Greenwalt434203a2010-10-11 16:00:27 -07001800 handleConnectivityChange(type);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001801 break;
Robert Greenwaltf3331232010-09-24 14:32:21 -07001802 case EVENT_CLEAR_NET_TRANSITION_WAKELOCK:
Robert Greenwalt14f2ef42010-06-15 12:19:37 -07001803 String causedBy = null;
1804 synchronized (ConnectivityService.this) {
1805 if (msg.arg1 == mNetTransitionWakeLockSerialNumber &&
1806 mNetTransitionWakeLock.isHeld()) {
1807 mNetTransitionWakeLock.release();
1808 causedBy = mNetTransitionWakeLockCausedBy;
1809 }
1810 }
1811 if (causedBy != null) {
Wink Savilleed9c02b2010-12-03 12:01:38 -08001812 log("NetTransition Wakelock for " + causedBy + " released by timeout");
Robert Greenwalt14f2ef42010-06-15 12:19:37 -07001813 }
Robert Greenwalt057d5e92010-09-09 14:05:10 -07001814 break;
Robert Greenwalt8dcc28b2010-09-23 10:05:56 -07001815 case EVENT_RESTORE_DEFAULT_NETWORK:
Robert Greenwalt9c75d4a2009-09-27 17:27:04 -07001816 FeatureUser u = (FeatureUser)msg.obj;
1817 u.expire();
Robert Greenwaltd7085fc2010-09-08 15:24:47 -07001818 break;
Robert Greenwalt8dcc28b2010-09-23 10:05:56 -07001819 case EVENT_INET_CONDITION_CHANGE:
1820 {
1821 int netType = msg.arg1;
1822 int condition = msg.arg2;
1823 handleInetConditionChange(netType, condition);
Robert Greenwalt14f2ef42010-06-15 12:19:37 -07001824 break;
Robert Greenwalt8dcc28b2010-09-23 10:05:56 -07001825 }
1826 case EVENT_INET_CONDITION_HOLD_END:
1827 {
1828 int netType = msg.arg1;
1829 int sequence = msg.arg2;
1830 handleInetConditionHoldEnd(netType, sequence);
Robert Greenwaltd7085fc2010-09-08 15:24:47 -07001831 break;
Robert Greenwalt8dcc28b2010-09-23 10:05:56 -07001832 }
1833 case EVENT_SET_NETWORK_PREFERENCE:
1834 {
1835 int preference = msg.arg1;
1836 handleSetNetworkPreference(preference);
1837 break;
1838 }
1839 case EVENT_SET_BACKGROUND_DATA:
1840 {
1841 boolean enabled = (msg.arg1 == ENABLED);
1842 handleSetBackgroundData(enabled);
1843 break;
1844 }
1845 case EVENT_SET_MOBILE_DATA:
1846 {
1847 boolean enabled = (msg.arg1 == ENABLED);
1848 handleSetMobileData(enabled);
1849 break;
1850 }
Robert Greenwalt434203a2010-10-11 16:00:27 -07001851 case EVENT_APPLY_GLOBAL_HTTP_PROXY:
1852 {
1853 handleDeprecatedGlobalHttpProxy();
1854 }
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001855 }
1856 }
1857 }
Robert Greenwaltd0e18ff2010-01-26 11:40:34 -08001858
1859 // javadoc from interface
Robert Greenwalt5a735062010-03-02 17:25:02 -08001860 public int tether(String iface) {
Robert Greenwaltd0e18ff2010-01-26 11:40:34 -08001861 enforceTetherChangePermission();
Robert Greenwalt5a735062010-03-02 17:25:02 -08001862
1863 if (isTetheringSupported()) {
1864 return mTethering.tether(iface);
1865 } else {
1866 return ConnectivityManager.TETHER_ERROR_UNSUPPORTED;
1867 }
Robert Greenwaltd0e18ff2010-01-26 11:40:34 -08001868 }
1869
1870 // javadoc from interface
Robert Greenwalt5a735062010-03-02 17:25:02 -08001871 public int untether(String iface) {
Robert Greenwaltd0e18ff2010-01-26 11:40:34 -08001872 enforceTetherChangePermission();
Robert Greenwalt5a735062010-03-02 17:25:02 -08001873
1874 if (isTetheringSupported()) {
1875 return mTethering.untether(iface);
1876 } else {
1877 return ConnectivityManager.TETHER_ERROR_UNSUPPORTED;
1878 }
1879 }
1880
1881 // javadoc from interface
1882 public int getLastTetherError(String iface) {
1883 enforceTetherAccessPermission();
1884
1885 if (isTetheringSupported()) {
1886 return mTethering.getLastTetherError(iface);
1887 } else {
1888 return ConnectivityManager.TETHER_ERROR_UNSUPPORTED;
1889 }
Robert Greenwalt2a091d72010-02-11 18:18:40 -08001890 }
1891
1892 // TODO - proper iface API for selection by property, inspection, etc
1893 public String[] getTetherableUsbRegexs() {
1894 enforceTetherAccessPermission();
1895 if (isTetheringSupported()) {
1896 return mTethering.getTetherableUsbRegexs();
1897 } else {
1898 return new String[0];
1899 }
1900 }
1901
1902 public String[] getTetherableWifiRegexs() {
1903 enforceTetherAccessPermission();
1904 if (isTetheringSupported()) {
1905 return mTethering.getTetherableWifiRegexs();
1906 } else {
1907 return new String[0];
1908 }
Robert Greenwaltd0e18ff2010-01-26 11:40:34 -08001909 }
1910
Danica Chang6fdd0c62010-08-11 14:54:43 -07001911 public String[] getTetherableBluetoothRegexs() {
1912 enforceTetherAccessPermission();
1913 if (isTetheringSupported()) {
1914 return mTethering.getTetherableBluetoothRegexs();
1915 } else {
1916 return new String[0];
1917 }
1918 }
1919
Robert Greenwaltd0e18ff2010-01-26 11:40:34 -08001920 // TODO - move iface listing, queries, etc to new module
1921 // javadoc from interface
1922 public String[] getTetherableIfaces() {
Robert Greenwalt2a091d72010-02-11 18:18:40 -08001923 enforceTetherAccessPermission();
Robert Greenwaltd0e18ff2010-01-26 11:40:34 -08001924 return mTethering.getTetherableIfaces();
1925 }
1926
1927 public String[] getTetheredIfaces() {
Robert Greenwalt2a091d72010-02-11 18:18:40 -08001928 enforceTetherAccessPermission();
Robert Greenwaltd0e18ff2010-01-26 11:40:34 -08001929 return mTethering.getTetheredIfaces();
1930 }
Robert Greenwalt2a091d72010-02-11 18:18:40 -08001931
Robert Greenwalt5a735062010-03-02 17:25:02 -08001932 public String[] getTetheringErroredIfaces() {
1933 enforceTetherAccessPermission();
1934 return mTethering.getErroredIfaces();
1935 }
1936
Robert Greenwalt2a091d72010-02-11 18:18:40 -08001937 // if ro.tether.denied = true we default to no tethering
1938 // gservices could set the secure setting to 1 though to enable it on a build where it
1939 // had previously been turned off.
1940 public boolean isTetheringSupported() {
1941 enforceTetherAccessPermission();
1942 int defaultVal = (SystemProperties.get("ro.tether.denied").equals("true") ? 0 : 1);
Robert Greenwaltc9d5fb72010-02-25 12:29:30 -08001943 boolean tetherEnabledInSettings = (Settings.Secure.getInt(mContext.getContentResolver(),
1944 Settings.Secure.TETHER_SUPPORTED, defaultVal) != 0);
1945 return tetherEnabledInSettings && mTetheringConfigValid;
Robert Greenwalt2a091d72010-02-11 18:18:40 -08001946 }
Robert Greenwalt14f2ef42010-06-15 12:19:37 -07001947
1948 // An API NetworkStateTrackers can call when they lose their network.
1949 // This will automatically be cleared after X seconds or a network becomes CONNECTED,
1950 // whichever happens first. The timer is started by the first caller and not
1951 // restarted by subsequent callers.
1952 public void requestNetworkTransitionWakelock(String forWhom) {
1953 enforceConnectivityInternalPermission();
1954 synchronized (this) {
1955 if (mNetTransitionWakeLock.isHeld()) return;
1956 mNetTransitionWakeLockSerialNumber++;
1957 mNetTransitionWakeLock.acquire();
1958 mNetTransitionWakeLockCausedBy = forWhom;
1959 }
1960 mHandler.sendMessageDelayed(mHandler.obtainMessage(
Robert Greenwaltf3331232010-09-24 14:32:21 -07001961 EVENT_CLEAR_NET_TRANSITION_WAKELOCK,
Robert Greenwalt14f2ef42010-06-15 12:19:37 -07001962 mNetTransitionWakeLockSerialNumber, 0),
1963 mNetTransitionWakeLockTimeout);
1964 return;
1965 }
Robert Greenwaltca4306c2010-09-09 13:15:32 -07001966
Robert Greenwaltd7085fc2010-09-08 15:24:47 -07001967 // 100 percent is full good, 0 is full bad.
1968 public void reportInetCondition(int networkType, int percentage) {
Wink Savilleed9c02b2010-12-03 12:01:38 -08001969 if (DBG) log("reportNetworkCondition(" + networkType + ", " + percentage + ")");
Robert Greenwaltd7085fc2010-09-08 15:24:47 -07001970 mContext.enforceCallingOrSelfPermission(
1971 android.Manifest.permission.STATUS_BAR,
1972 "ConnectivityService");
1973
Robert Greenwalt4e8dfef2010-09-20 14:35:25 -07001974 if (DBG) {
1975 int pid = getCallingPid();
1976 int uid = getCallingUid();
1977 String s = pid + "(" + uid + ") reports inet is " +
1978 (percentage > 50 ? "connected" : "disconnected") + " (" + percentage + ") on " +
1979 "network Type " + networkType + " at " + GregorianCalendar.getInstance().getTime();
1980 mInetLog.add(s);
1981 while(mInetLog.size() > INET_CONDITION_LOG_MAX_SIZE) {
1982 mInetLog.remove(0);
1983 }
1984 }
Robert Greenwaltd7085fc2010-09-08 15:24:47 -07001985 mHandler.sendMessage(mHandler.obtainMessage(
Robert Greenwalt8dcc28b2010-09-23 10:05:56 -07001986 EVENT_INET_CONDITION_CHANGE, networkType, percentage));
1987 }
1988
1989 private void handleInetConditionChange(int netType, int condition) {
1990 if (DBG) {
Wink Savilleed9c02b2010-12-03 12:01:38 -08001991 log("Inet connectivity change, net=" +
Robert Greenwalt8dcc28b2010-09-23 10:05:56 -07001992 netType + ", condition=" + condition +
1993 ",mActiveDefaultNetwork=" + mActiveDefaultNetwork);
1994 }
1995 if (mActiveDefaultNetwork == -1) {
Wink Savilleed9c02b2010-12-03 12:01:38 -08001996 if (DBG) log("no active default network - aborting");
Robert Greenwalt8dcc28b2010-09-23 10:05:56 -07001997 return;
1998 }
1999 if (mActiveDefaultNetwork != netType) {
Wink Savilleed9c02b2010-12-03 12:01:38 -08002000 if (DBG) log("given net not default - aborting");
Robert Greenwalt8dcc28b2010-09-23 10:05:56 -07002001 return;
2002 }
2003 mDefaultInetCondition = condition;
2004 int delay;
2005 if (mInetConditionChangeInFlight == false) {
Wink Savilleed9c02b2010-12-03 12:01:38 -08002006 if (DBG) log("starting a change hold");
Robert Greenwalt8dcc28b2010-09-23 10:05:56 -07002007 // setup a new hold to debounce this
2008 if (mDefaultInetCondition > 50) {
2009 delay = Settings.Secure.getInt(mContext.getContentResolver(),
2010 Settings.Secure.INET_CONDITION_DEBOUNCE_UP_DELAY, 500);
2011 } else {
2012 delay = Settings.Secure.getInt(mContext.getContentResolver(),
2013 Settings.Secure.INET_CONDITION_DEBOUNCE_DOWN_DELAY, 3000);
2014 }
2015 mInetConditionChangeInFlight = true;
2016 mHandler.sendMessageDelayed(mHandler.obtainMessage(EVENT_INET_CONDITION_HOLD_END,
2017 mActiveDefaultNetwork, mDefaultConnectionSequence), delay);
2018 } else {
2019 // we've set the new condition, when this hold ends that will get
2020 // picked up
Wink Savilleed9c02b2010-12-03 12:01:38 -08002021 if (DBG) log("currently in hold - not setting new end evt");
Robert Greenwalt8dcc28b2010-09-23 10:05:56 -07002022 }
2023 }
2024
2025 private void handleInetConditionHoldEnd(int netType, int sequence) {
2026 if (DBG) {
Wink Savilleed9c02b2010-12-03 12:01:38 -08002027 log("Inet hold end, net=" + netType +
Robert Greenwalt8dcc28b2010-09-23 10:05:56 -07002028 ", condition =" + mDefaultInetCondition +
2029 ", published condition =" + mDefaultInetConditionPublished);
2030 }
2031 mInetConditionChangeInFlight = false;
2032
2033 if (mActiveDefaultNetwork == -1) {
Wink Savilleed9c02b2010-12-03 12:01:38 -08002034 if (DBG) log("no active default network - aborting");
Robert Greenwalt8dcc28b2010-09-23 10:05:56 -07002035 return;
2036 }
2037 if (mDefaultConnectionSequence != sequence) {
Wink Savilleed9c02b2010-12-03 12:01:38 -08002038 if (DBG) log("event hold for obsolete network - aborting");
Robert Greenwalt8dcc28b2010-09-23 10:05:56 -07002039 return;
2040 }
2041 if (mDefaultInetConditionPublished == mDefaultInetCondition) {
Wink Savilleed9c02b2010-12-03 12:01:38 -08002042 if (DBG) log("no change in condition - aborting");
Robert Greenwalt8dcc28b2010-09-23 10:05:56 -07002043 return;
2044 }
2045 NetworkInfo networkInfo = mNetTrackers[mActiveDefaultNetwork].getNetworkInfo();
2046 if (networkInfo.isConnected() == false) {
Wink Savilleed9c02b2010-12-03 12:01:38 -08002047 if (DBG) log("default network not connected - aborting");
Robert Greenwalt8dcc28b2010-09-23 10:05:56 -07002048 return;
2049 }
2050 mDefaultInetConditionPublished = mDefaultInetCondition;
2051 sendInetConditionBroadcast(networkInfo);
2052 return;
Robert Greenwaltd7085fc2010-09-08 15:24:47 -07002053 }
Robert Greenwalt434203a2010-10-11 16:00:27 -07002054
2055 public synchronized ProxyProperties getProxy() {
2056 if (mGlobalProxy != null) return mGlobalProxy;
2057 if (mDefaultProxy != null) return mDefaultProxy;
2058 return null;
2059 }
2060
2061 public void setGlobalProxy(ProxyProperties proxyProperties) {
2062 enforceChangePermission();
2063 synchronized (mGlobalProxyLock) {
2064 if (proxyProperties == mGlobalProxy) return;
2065 if (proxyProperties != null && proxyProperties.equals(mGlobalProxy)) return;
2066 if (mGlobalProxy != null && mGlobalProxy.equals(proxyProperties)) return;
2067
2068 String host = "";
2069 int port = 0;
2070 String exclList = "";
2071 if (proxyProperties != null && !TextUtils.isEmpty(proxyProperties.getHost())) {
2072 mGlobalProxy = new ProxyProperties(proxyProperties);
2073 host = mGlobalProxy.getHost();
2074 port = mGlobalProxy.getPort();
2075 exclList = mGlobalProxy.getExclusionList();
2076 } else {
2077 mGlobalProxy = null;
2078 }
2079 ContentResolver res = mContext.getContentResolver();
2080 Settings.Secure.putString(res, Settings.Secure.GLOBAL_HTTP_PROXY_HOST, host);
2081 Settings.Secure.putInt(res, Settings.Secure.GLOBAL_HTTP_PROXY_PORT, port);
Robert Greenwaltb7090d62010-12-02 11:31:00 -08002082 Settings.Secure.putString(res, Settings.Secure.GLOBAL_HTTP_PROXY_EXCLUSION_LIST,
Robert Greenwalt434203a2010-10-11 16:00:27 -07002083 exclList);
2084 }
2085
2086 if (mGlobalProxy == null) {
2087 proxyProperties = mDefaultProxy;
2088 }
2089 sendProxyBroadcast(proxyProperties);
2090 }
2091
Robert Greenwaltb7090d62010-12-02 11:31:00 -08002092 private void loadGlobalProxy() {
2093 ContentResolver res = mContext.getContentResolver();
2094 String host = Settings.Secure.getString(res, Settings.Secure.GLOBAL_HTTP_PROXY_HOST);
2095 int port = Settings.Secure.getInt(res, Settings.Secure.GLOBAL_HTTP_PROXY_PORT, 0);
2096 String exclList = Settings.Secure.getString(res,
2097 Settings.Secure.GLOBAL_HTTP_PROXY_EXCLUSION_LIST);
2098 if (!TextUtils.isEmpty(host)) {
2099 ProxyProperties proxyProperties = new ProxyProperties(host, port, exclList);
2100 synchronized (mGlobalProxyLock) {
2101 mGlobalProxy = proxyProperties;
2102 }
2103 }
2104 }
2105
Robert Greenwalt434203a2010-10-11 16:00:27 -07002106 public ProxyProperties getGlobalProxy() {
2107 synchronized (mGlobalProxyLock) {
2108 return mGlobalProxy;
2109 }
2110 }
2111
2112 private void handleApplyDefaultProxy(int type) {
2113 // check if new default - push it out to all VM if so
2114 ProxyProperties proxy = mNetTrackers[type].getLinkProperties().getHttpProxy();
2115 synchronized (this) {
2116 if (mDefaultProxy != null && mDefaultProxy.equals(proxy)) return;
2117 if (mDefaultProxy == proxy) return;
2118 if (!TextUtils.isEmpty(proxy.getHost())) {
2119 mDefaultProxy = proxy;
2120 } else {
2121 mDefaultProxy = null;
2122 }
2123 }
Wink Savilleed9c02b2010-12-03 12:01:38 -08002124 if (DBG) log("changing default proxy to " + proxy);
Robert Greenwalt434203a2010-10-11 16:00:27 -07002125 if ((proxy == null && mGlobalProxy == null) || proxy.equals(mGlobalProxy)) return;
2126 if (mGlobalProxy != null) return;
2127 sendProxyBroadcast(proxy);
2128 }
2129
2130 private void handleDeprecatedGlobalHttpProxy() {
2131 String proxy = Settings.Secure.getString(mContext.getContentResolver(),
2132 Settings.Secure.HTTP_PROXY);
2133 if (!TextUtils.isEmpty(proxy)) {
2134 String data[] = proxy.split(":");
2135 String proxyHost = data[0];
2136 int proxyPort = 8080;
2137 if (data.length > 1) {
2138 try {
2139 proxyPort = Integer.parseInt(data[1]);
2140 } catch (NumberFormatException e) {
2141 return;
2142 }
2143 }
2144 ProxyProperties p = new ProxyProperties(data[0], proxyPort, "");
2145 setGlobalProxy(p);
2146 }
2147 }
2148
2149 private void sendProxyBroadcast(ProxyProperties proxy) {
Robert Greenwalt55985be2010-12-23 15:51:10 -08002150 if (proxy == null) proxy = new ProxyProperties("", 0, "");
Wink Savilleed9c02b2010-12-03 12:01:38 -08002151 log("sending Proxy Broadcast for " + proxy);
Robert Greenwalt434203a2010-10-11 16:00:27 -07002152 Intent intent = new Intent(Proxy.PROXY_CHANGE_ACTION);
Stan Chesnuttb35d67a2011-01-06 11:00:19 -08002153 intent.addFlags(Intent.FLAG_RECEIVER_REPLACE_PENDING |
2154 Intent.FLAG_RECEIVER_REGISTERED_ONLY_BEFORE_BOOT);
Robert Greenwalt434203a2010-10-11 16:00:27 -07002155 intent.putExtra(Proxy.EXTRA_PROXY_INFO, proxy);
Robert Greenwalta2e13392010-12-06 11:29:17 -08002156 mContext.sendStickyBroadcast(intent);
Robert Greenwalt434203a2010-10-11 16:00:27 -07002157 }
2158
2159 private static class SettingsObserver extends ContentObserver {
2160 private int mWhat;
2161 private Handler mHandler;
2162 SettingsObserver(Handler handler, int what) {
2163 super(handler);
2164 mHandler = handler;
2165 mWhat = what;
2166 }
2167
2168 void observe(Context context) {
2169 ContentResolver resolver = context.getContentResolver();
2170 resolver.registerContentObserver(Settings.Secure.getUriFor(
2171 Settings.Secure.HTTP_PROXY), false, this);
2172 }
2173
2174 @Override
2175 public void onChange(boolean selfChange) {
2176 mHandler.obtainMessage(mWhat).sendToTarget();
2177 }
2178 }
Wink Savilleed9c02b2010-12-03 12:01:38 -08002179
2180 private void log(String s) {
2181 Slog.d(TAG, s);
2182 }
2183
2184 private void loge(String s) {
2185 Slog.e(TAG, s);
2186 }
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002187}