blob: 39977028f0743a915996a9dba1709130d138b675 [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 Goby19970692010-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 Goby19970692010-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 Greenwalt13ec4062011-04-01 10:51:22 -0700546 if (mActiveDefaultNetwork != -1) {
547 return mNetTrackers[mActiveDefaultNetwork].getNetworkInfo();
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800548 }
549 return null;
550 }
551
552 public NetworkInfo getNetworkInfo(int networkType) {
553 enforceAccessPermission();
554 if (ConnectivityManager.isNetworkTypeValid(networkType)) {
555 NetworkStateTracker t = mNetTrackers[networkType];
556 if (t != null)
557 return t.getNetworkInfo();
558 }
559 return null;
560 }
561
562 public NetworkInfo[] getAllNetworkInfo() {
563 enforceAccessPermission();
Robert Greenwalt5154ae762009-10-30 14:17:42 -0700564 NetworkInfo[] result = new NetworkInfo[mNetworksDefined];
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800565 int i = 0;
566 for (NetworkStateTracker t : mNetTrackers) {
Robert Greenwalt5154ae762009-10-30 14:17:42 -0700567 if(t != null) result[i++] = t.getNetworkInfo();
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800568 }
569 return result;
570 }
571
Robert Greenwaltd192dad2010-09-14 09:18:02 -0700572 /**
573 * Return LinkProperties for the active (i.e., connected) default
574 * network interface. It is assumed that at most one default network
575 * is active at a time. If more than one is active, it is indeterminate
576 * which will be returned.
577 * @return the ip properties for the active network, or {@code null} if
578 * none is active
579 */
580 public LinkProperties getActiveLinkProperties() {
581 enforceAccessPermission();
582 for (int type=0; type <= ConnectivityManager.MAX_NETWORK_TYPE; type++) {
583 if (mNetAttributes[type] == null || !mNetAttributes[type].isDefault()) {
584 continue;
585 }
586 NetworkStateTracker t = mNetTrackers[type];
587 NetworkInfo info = t.getNetworkInfo();
588 if (info.isConnected()) {
589 return t.getLinkProperties();
590 }
591 }
592 return null;
593 }
594
595 public LinkProperties getLinkProperties(int networkType) {
596 enforceAccessPermission();
597 if (ConnectivityManager.isNetworkTypeValid(networkType)) {
598 NetworkStateTracker t = mNetTrackers[networkType];
599 if (t != null) return t.getLinkProperties();
600 }
601 return null;
602 }
603
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800604 public boolean setRadios(boolean turnOn) {
605 boolean result = true;
606 enforceChangePermission();
607 for (NetworkStateTracker t : mNetTrackers) {
Robert Greenwalt5154ae762009-10-30 14:17:42 -0700608 if (t != null) result = t.setRadio(turnOn) && result;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800609 }
610 return result;
611 }
612
613 public boolean setRadio(int netType, boolean turnOn) {
614 enforceChangePermission();
615 if (!ConnectivityManager.isNetworkTypeValid(netType)) {
616 return false;
617 }
618 NetworkStateTracker tracker = mNetTrackers[netType];
619 return tracker != null && tracker.setRadio(turnOn);
620 }
621
Robert Greenwalt9c75d4a2009-09-27 17:27:04 -0700622 /**
623 * Used to notice when the calling process dies so we can self-expire
624 *
625 * Also used to know if the process has cleaned up after itself when
626 * our auto-expire timer goes off. The timer has a link to an object.
627 *
628 */
Robert Greenwalt42acef32009-08-12 16:08:25 -0700629 private class FeatureUser implements IBinder.DeathRecipient {
630 int mNetworkType;
631 String mFeature;
632 IBinder mBinder;
633 int mPid;
634 int mUid;
Robert Greenwaltb9285352009-12-21 18:24:07 -0800635 long mCreateTime;
Robert Greenwalt42acef32009-08-12 16:08:25 -0700636
637 FeatureUser(int type, String feature, IBinder binder) {
638 super();
639 mNetworkType = type;
640 mFeature = feature;
641 mBinder = binder;
642 mPid = getCallingPid();
643 mUid = getCallingUid();
Robert Greenwaltb9285352009-12-21 18:24:07 -0800644 mCreateTime = System.currentTimeMillis();
Robert Greenwalt9c75d4a2009-09-27 17:27:04 -0700645
Robert Greenwalt42acef32009-08-12 16:08:25 -0700646 try {
647 mBinder.linkToDeath(this, 0);
648 } catch (RemoteException e) {
649 binderDied();
650 }
651 }
652
653 void unlinkDeathRecipient() {
654 mBinder.unlinkToDeath(this, 0);
655 }
656
657 public void binderDied() {
Wink Savilleed9c02b2010-12-03 12:01:38 -0800658 log("ConnectivityService FeatureUser binderDied(" +
Robert Greenwaltb9285352009-12-21 18:24:07 -0800659 mNetworkType + ", " + mFeature + ", " + mBinder + "), created " +
660 (System.currentTimeMillis() - mCreateTime) + " mSec ago");
Robert Greenwalt9c75d4a2009-09-27 17:27:04 -0700661 stopUsingNetworkFeature(this, false);
Robert Greenwalt42acef32009-08-12 16:08:25 -0700662 }
663
Robert Greenwalt9c75d4a2009-09-27 17:27:04 -0700664 public void expire() {
Wink Savilleed9c02b2010-12-03 12:01:38 -0800665 log("ConnectivityService FeatureUser expire(" +
Robert Greenwaltb9285352009-12-21 18:24:07 -0800666 mNetworkType + ", " + mFeature + ", " + mBinder +"), created " +
667 (System.currentTimeMillis() - mCreateTime) + " mSec ago");
Robert Greenwalt9c75d4a2009-09-27 17:27:04 -0700668 stopUsingNetworkFeature(this, false);
669 }
Robert Greenwaltb9285352009-12-21 18:24:07 -0800670
671 public String toString() {
672 return "FeatureUser("+mNetworkType+","+mFeature+","+mPid+","+mUid+"), created " +
673 (System.currentTimeMillis() - mCreateTime) + " mSec ago";
674 }
Robert Greenwalt42acef32009-08-12 16:08:25 -0700675 }
676
Robert Greenwalt9c75d4a2009-09-27 17:27:04 -0700677 // javadoc from interface
Robert Greenwalt42acef32009-08-12 16:08:25 -0700678 public int startUsingNetworkFeature(int networkType, String feature,
679 IBinder binder) {
680 if (DBG) {
Wink Savilleed9c02b2010-12-03 12:01:38 -0800681 log("startUsingNetworkFeature for net " + networkType + ": " + feature);
Robert Greenwalt42acef32009-08-12 16:08:25 -0700682 }
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800683 enforceChangePermission();
Robert Greenwalt5154ae762009-10-30 14:17:42 -0700684 if (!ConnectivityManager.isNetworkTypeValid(networkType) ||
685 mNetAttributes[networkType] == null) {
Robert Greenwalt42acef32009-08-12 16:08:25 -0700686 return Phone.APN_REQUEST_FAILED;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800687 }
Robert Greenwalt42acef32009-08-12 16:08:25 -0700688
Robert Greenwalt9c75d4a2009-09-27 17:27:04 -0700689 FeatureUser f = new FeatureUser(networkType, feature, binder);
Robert Greenwalt42acef32009-08-12 16:08:25 -0700690
691 // TODO - move this into the MobileDataStateTracker
692 int usedNetworkType = networkType;
693 if(networkType == ConnectivityManager.TYPE_MOBILE) {
Wink Saville2b8bcfe2011-02-24 17:58:51 -0800694 usedNetworkType = convertFeatureToNetworkType(feature);
695 if (usedNetworkType < 0) {
696 Slog.e(TAG, "Can't match any netTracker!");
697 usedNetworkType = networkType;
Robert Greenwalt42acef32009-08-12 16:08:25 -0700698 }
699 }
700 NetworkStateTracker network = mNetTrackers[usedNetworkType];
701 if (network != null) {
Robert Greenwalt0be1e982010-12-15 13:26:33 -0800702 Integer currentPid = new Integer(getCallingPid());
Robert Greenwalt42acef32009-08-12 16:08:25 -0700703 if (usedNetworkType != networkType) {
Robert Greenwalt42acef32009-08-12 16:08:25 -0700704 NetworkStateTracker radio = mNetTrackers[networkType];
705 NetworkInfo ni = network.getNetworkInfo();
706
707 if (ni.isAvailable() == false) {
Wink Savilleed9c02b2010-12-03 12:01:38 -0800708 if (DBG) log("special network not available");
Robert Greenwalte32e8122010-12-29 14:35:21 -0800709 if (!TextUtils.equals(feature,Phone.FEATURE_ENABLE_DUN_ALWAYS)) {
710 return Phone.APN_TYPE_NOT_AVAILABLE;
711 } else {
712 // else make the attempt anyway - probably giving REQUEST_STARTED below
713 }
Robert Greenwalt42acef32009-08-12 16:08:25 -0700714 }
715
Robert Greenwalt9c75d4a2009-09-27 17:27:04 -0700716 synchronized(this) {
717 mFeatureUsers.add(f);
718 if (!mNetRequestersPids[usedNetworkType].contains(currentPid)) {
719 // this gets used for per-pid dns when connected
720 mNetRequestersPids[usedNetworkType].add(currentPid);
721 }
Robert Greenwalt42acef32009-08-12 16:08:25 -0700722 }
Robert Greenwalt8dcc28b2010-09-23 10:05:56 -0700723 mHandler.sendMessageDelayed(mHandler.obtainMessage(EVENT_RESTORE_DEFAULT_NETWORK,
Robert Greenwalt9c75d4a2009-09-27 17:27:04 -0700724 f), getRestoreDefaultNetworkDelay());
725
Robert Greenwalt42acef32009-08-12 16:08:25 -0700726
Robert Greenwalta64bf832009-08-19 20:19:33 -0700727 if ((ni.isConnectedOrConnecting() == true) &&
728 !network.isTeardownRequested()) {
Robert Greenwalt42acef32009-08-12 16:08:25 -0700729 if (ni.isConnected() == true) {
730 // add the pid-specific dns
Robert Greenwaltc76b8fa2010-07-23 15:46:26 -0700731 handleDnsConfigurationChange(networkType);
Wink Savilleed9c02b2010-12-03 12:01:38 -0800732 if (DBG) log("special network already active");
Robert Greenwalt42acef32009-08-12 16:08:25 -0700733 return Phone.APN_ALREADY_ACTIVE;
734 }
Wink Savilleed9c02b2010-12-03 12:01:38 -0800735 if (DBG) log("special network already connecting");
Robert Greenwalt42acef32009-08-12 16:08:25 -0700736 return Phone.APN_REQUEST_STARTED;
737 }
738
739 // check if the radio in play can make another contact
740 // assume if cannot for now
741
Wink Savilleed9c02b2010-12-03 12:01:38 -0800742 if (DBG) log("reconnecting to special network");
Robert Greenwalt42acef32009-08-12 16:08:25 -0700743 network.reconnect();
744 return Phone.APN_REQUEST_STARTED;
745 } else {
Robert Greenwalt0be1e982010-12-15 13:26:33 -0800746 // need to remember this unsupported request so we respond appropriately on stop
747 synchronized(this) {
748 mFeatureUsers.add(f);
749 if (!mNetRequestersPids[usedNetworkType].contains(currentPid)) {
750 // this gets used for per-pid dns when connected
751 mNetRequestersPids[usedNetworkType].add(currentPid);
752 }
753 }
Robert Greenwalt02648a42010-05-18 10:52:51 -0700754 return -1;
Robert Greenwalt42acef32009-08-12 16:08:25 -0700755 }
756 }
757 return Phone.APN_TYPE_NOT_AVAILABLE;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800758 }
759
Robert Greenwalt9c75d4a2009-09-27 17:27:04 -0700760 // javadoc from interface
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800761 public int stopUsingNetworkFeature(int networkType, String feature) {
Robert Greenwaltb8f16342009-10-06 17:52:40 -0700762 enforceChangePermission();
763
Robert Greenwalt9c75d4a2009-09-27 17:27:04 -0700764 int pid = getCallingPid();
765 int uid = getCallingUid();
766
767 FeatureUser u = null;
768 boolean found = false;
769
770 synchronized(this) {
771 for (int i = 0; i < mFeatureUsers.size() ; i++) {
772 u = (FeatureUser)mFeatureUsers.get(i);
773 if (uid == u.mUid && pid == u.mPid &&
774 networkType == u.mNetworkType &&
775 TextUtils.equals(feature, u.mFeature)) {
776 found = true;
777 break;
778 }
779 }
780 }
781 if (found && u != null) {
782 // stop regardless of how many other time this proc had called start
783 return stopUsingNetworkFeature(u, true);
784 } else {
785 // none found!
Wink Savilleed9c02b2010-12-03 12:01:38 -0800786 if (DBG) log("ignoring stopUsingNetworkFeature - not a live request");
Robert Greenwalt9c75d4a2009-09-27 17:27:04 -0700787 return 1;
788 }
Robert Greenwalt42acef32009-08-12 16:08:25 -0700789 }
790
Robert Greenwalt9c75d4a2009-09-27 17:27:04 -0700791 private int stopUsingNetworkFeature(FeatureUser u, boolean ignoreDups) {
792 int networkType = u.mNetworkType;
793 String feature = u.mFeature;
794 int pid = u.mPid;
795 int uid = u.mUid;
796
797 NetworkStateTracker tracker = null;
798 boolean callTeardown = false; // used to carry our decision outside of sync block
799
Robert Greenwalt42acef32009-08-12 16:08:25 -0700800 if (DBG) {
Wink Savilleed9c02b2010-12-03 12:01:38 -0800801 log("stopUsingNetworkFeature for net " + networkType +
Robert Greenwalt42acef32009-08-12 16:08:25 -0700802 ": " + feature);
803 }
Robert Greenwaltb8f16342009-10-06 17:52:40 -0700804
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800805 if (!ConnectivityManager.isNetworkTypeValid(networkType)) {
806 return -1;
807 }
Robert Greenwalt42acef32009-08-12 16:08:25 -0700808
Robert Greenwalt9c75d4a2009-09-27 17:27:04 -0700809 // need to link the mFeatureUsers list with the mNetRequestersPids state in this
810 // sync block
811 synchronized(this) {
812 // check if this process still has an outstanding start request
813 if (!mFeatureUsers.contains(u)) {
Wink Savilleed9c02b2010-12-03 12:01:38 -0800814 if (DBG) log("ignoring - this process has no outstanding requests");
Robert Greenwalt42acef32009-08-12 16:08:25 -0700815 return 1;
816 }
Robert Greenwalt9c75d4a2009-09-27 17:27:04 -0700817 u.unlinkDeathRecipient();
818 mFeatureUsers.remove(mFeatureUsers.indexOf(u));
819 // If we care about duplicate requests, check for that here.
820 //
821 // This is done to support the extension of a request - the app
822 // can request we start the network feature again and renew the
823 // auto-shutoff delay. Normal "stop" calls from the app though
824 // do not pay attention to duplicate requests - in effect the
825 // API does not refcount and a single stop will counter multiple starts.
826 if (ignoreDups == false) {
827 for (int i = 0; i < mFeatureUsers.size() ; i++) {
828 FeatureUser x = (FeatureUser)mFeatureUsers.get(i);
829 if (x.mUid == u.mUid && x.mPid == u.mPid &&
830 x.mNetworkType == u.mNetworkType &&
831 TextUtils.equals(x.mFeature, u.mFeature)) {
Wink Savilleed9c02b2010-12-03 12:01:38 -0800832 if (DBG) log("ignoring stopUsingNetworkFeature as dup is found");
Robert Greenwalt9c75d4a2009-09-27 17:27:04 -0700833 return 1;
834 }
835 }
Robert Greenwalt42acef32009-08-12 16:08:25 -0700836 }
Robert Greenwalt9c75d4a2009-09-27 17:27:04 -0700837
838 // TODO - move to MobileDataStateTracker
839 int usedNetworkType = networkType;
840 if (networkType == ConnectivityManager.TYPE_MOBILE) {
Wink Saville2b8bcfe2011-02-24 17:58:51 -0800841 usedNetworkType = convertFeatureToNetworkType(feature);
842 if (usedNetworkType < 0) {
843 usedNetworkType = networkType;
Robert Greenwalt9c75d4a2009-09-27 17:27:04 -0700844 }
845 }
846 tracker = mNetTrackers[usedNetworkType];
Robert Greenwalt5154ae762009-10-30 14:17:42 -0700847 if (tracker == null) {
Wink Savilleed9c02b2010-12-03 12:01:38 -0800848 if (DBG) log("ignoring - no known tracker for net type " + usedNetworkType);
Robert Greenwalt5154ae762009-10-30 14:17:42 -0700849 return -1;
850 }
851 if (usedNetworkType != networkType) {
Robert Greenwalt9c75d4a2009-09-27 17:27:04 -0700852 Integer currentPid = new Integer(pid);
Robert Greenwalt9c75d4a2009-09-27 17:27:04 -0700853 mNetRequestersPids[usedNetworkType].remove(currentPid);
Robert Greenwalt421c72b2009-12-17 14:54:59 -0800854 reassessPidDns(pid, true);
Robert Greenwalt9c75d4a2009-09-27 17:27:04 -0700855 if (mNetRequestersPids[usedNetworkType].size() != 0) {
Wink Savilleed9c02b2010-12-03 12:01:38 -0800856 if (DBG) log("not tearing down special network - " +
Robert Greenwalt9c75d4a2009-09-27 17:27:04 -0700857 "others still using it");
858 return 1;
859 }
860 callTeardown = true;
Robert Greenwalt19b9ab42011-01-10 11:58:31 -0800861 } else {
862 if (DBG) log("not a known feature - dropping");
Robert Greenwalt9c75d4a2009-09-27 17:27:04 -0700863 }
864 }
Wink Savilleed9c02b2010-12-03 12:01:38 -0800865 if (DBG) log("Doing network teardown");
Robert Greenwalt9c75d4a2009-09-27 17:27:04 -0700866 if (callTeardown) {
867 tracker.teardown();
Robert Greenwalt42acef32009-08-12 16:08:25 -0700868 return 1;
869 } else {
Robert Greenwalt02648a42010-05-18 10:52:51 -0700870 return -1;
Robert Greenwalt42acef32009-08-12 16:08:25 -0700871 }
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800872 }
873
874 /**
Robert Greenwalt585ac0f2010-08-27 09:24:29 -0700875 * @deprecated use requestRouteToHostAddress instead
876 *
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800877 * Ensure that a network route exists to deliver traffic to the specified
878 * host via the specified network interface.
Robert Greenwalt86e9e552009-07-16 17:21:39 -0700879 * @param networkType the type of the network over which traffic to the
880 * specified host is to be routed
881 * @param hostAddress the IP address of the host to which the route is
882 * desired
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800883 * @return {@code true} on success, {@code false} on failure
884 */
885 public boolean requestRouteToHost(int networkType, int hostAddress) {
Robert Greenwalt585ac0f2010-08-27 09:24:29 -0700886 InetAddress inetAddress = NetworkUtils.intToInetAddress(hostAddress);
887
888 if (inetAddress == null) {
889 return false;
890 }
891
892 return requestRouteToHostAddress(networkType, inetAddress.getAddress());
893 }
894
895 /**
896 * Ensure that a network route exists to deliver traffic to the specified
897 * host via the specified network interface.
898 * @param networkType the type of the network over which traffic to the
899 * specified host is to be routed
900 * @param hostAddress the IP address of the host to which the route is
901 * desired
902 * @return {@code true} on success, {@code false} on failure
903 */
904 public boolean requestRouteToHostAddress(int networkType, byte[] hostAddress) {
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800905 enforceChangePermission();
906 if (!ConnectivityManager.isNetworkTypeValid(networkType)) {
907 return false;
908 }
909 NetworkStateTracker tracker = mNetTrackers[networkType];
Robert Greenwalt8206ff32009-09-10 15:06:20 -0700910
Robert Greenwalt5154ae762009-10-30 14:17:42 -0700911 if (tracker == null || !tracker.getNetworkInfo().isConnected() ||
912 tracker.isTeardownRequested()) {
Robert Greenwalt8206ff32009-09-10 15:06:20 -0700913 if (DBG) {
Wink Savilleed9c02b2010-12-03 12:01:38 -0800914 log("requestRouteToHostAddress on down network " +
Robert Greenwalt585ac0f2010-08-27 09:24:29 -0700915 "(" + networkType + ") - dropped");
Robert Greenwalt8206ff32009-09-10 15:06:20 -0700916 }
917 return false;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800918 }
Robert Greenwalt47f69fe2010-06-15 15:43:39 -0700919 try {
Robert Greenwalt585ac0f2010-08-27 09:24:29 -0700920 InetAddress addr = InetAddress.getByAddress(hostAddress);
Robert Greenwalt47f69fe2010-06-15 15:43:39 -0700921 return addHostRoute(tracker, addr);
922 } catch (UnknownHostException e) {}
923 return false;
Irfan Sheriffd649c122010-06-09 15:39:36 -0700924 }
925
926 /**
927 * Ensure that a network route exists to deliver traffic to the specified
928 * host via the mobile data network.
929 * @param hostAddress the IP address of the host to which the route is desired,
930 * in network byte order.
Robert Greenwalt47f69fe2010-06-15 15:43:39 -0700931 * TODO - deprecate
Irfan Sheriffd649c122010-06-09 15:39:36 -0700932 * @return {@code true} on success, {@code false} on failure
933 */
Robert Greenwalt47f69fe2010-06-15 15:43:39 -0700934 private boolean addHostRoute(NetworkStateTracker nt, InetAddress hostAddress) {
Irfan Sheriffd649c122010-06-09 15:39:36 -0700935 if (nt.getNetworkInfo().getType() == ConnectivityManager.TYPE_WIFI) {
936 return false;
937 }
938
Robert Greenwalt37e65eb2010-08-30 10:56:47 -0700939 LinkProperties p = nt.getLinkProperties();
Robert Greenwalt47f69fe2010-06-15 15:43:39 -0700940 if (p == null) return false;
941 String interfaceName = p.getInterfaceName();
Irfan Sheriffd649c122010-06-09 15:39:36 -0700942
943 if (DBG) {
Wink Savilleed9c02b2010-12-03 12:01:38 -0800944 log("Requested host route to " + hostAddress + "(" + interfaceName + ")");
Irfan Sheriffd649c122010-06-09 15:39:36 -0700945 }
Robert Greenwalt47f69fe2010-06-15 15:43:39 -0700946 if (interfaceName != null) {
Robert Greenwalt585ac0f2010-08-27 09:24:29 -0700947 return NetworkUtils.addHostRoute(interfaceName, hostAddress, null);
Irfan Sheriffd649c122010-06-09 15:39:36 -0700948 } else {
Wink Savilleed9c02b2010-12-03 12:01:38 -0800949 if (DBG) loge("addHostRoute failed due to null interface name");
Irfan Sheriffd649c122010-06-09 15:39:36 -0700950 return false;
951 }
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800952 }
953
954 /**
955 * @see ConnectivityManager#getBackgroundDataSetting()
956 */
957 public boolean getBackgroundDataSetting() {
Robert Greenwaltd825ea42010-12-29 16:15:02 -0800958 return mBackgroundDataEnabled.get();
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800959 }
Robert Greenwalt86e9e552009-07-16 17:21:39 -0700960
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800961 /**
962 * @see ConnectivityManager#setBackgroundDataSetting(boolean)
963 */
964 public void setBackgroundDataSetting(boolean allowBackgroundDataUsage) {
965 mContext.enforceCallingOrSelfPermission(
966 android.Manifest.permission.CHANGE_BACKGROUND_DATA_SETTING,
967 "ConnectivityService");
Robert Greenwalt86e9e552009-07-16 17:21:39 -0700968
Robert Greenwaltd825ea42010-12-29 16:15:02 -0800969 mBackgroundDataEnabled.set(allowBackgroundDataUsage);
970
Robert Greenwalt8dcc28b2010-09-23 10:05:56 -0700971 mHandler.sendMessage(mHandler.obtainMessage(EVENT_SET_BACKGROUND_DATA,
972 (allowBackgroundDataUsage ? ENABLED : DISABLED), 0));
973 }
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800974
Robert Greenwalt8dcc28b2010-09-23 10:05:56 -0700975 private void handleSetBackgroundData(boolean enabled) {
Robert Greenwaltdb4afae2011-02-25 13:44:09 -0800976 Settings.Secure.putInt(mContext.getContentResolver(),
977 Settings.Secure.BACKGROUND_DATA, enabled ? 1 : 0);
978 Intent broadcast = new Intent(
979 ConnectivityManager.ACTION_BACKGROUND_DATA_SETTING_CHANGED);
980 mContext.sendBroadcast(broadcast);
Robert Greenwalt86e9e552009-07-16 17:21:39 -0700981 }
982
Robert Greenwaltc03fa502010-02-23 18:58:05 -0800983 /**
984 * @see ConnectivityManager#getMobileDataEnabled()
985 */
986 public boolean getMobileDataEnabled() {
Wink Savillee7982682010-12-07 10:31:02 -0800987 // TODO: This detail should probably be in DataConnectionTracker's
988 // which is where we store the value and maybe make this
989 // asynchronous.
Robert Greenwaltc03fa502010-02-23 18:58:05 -0800990 enforceAccessPermission();
991 boolean retVal = Settings.Secure.getInt(mContext.getContentResolver(),
992 Settings.Secure.MOBILE_DATA, 1) == 1;
Wink Savilleed9c02b2010-12-03 12:01:38 -0800993 if (DBG) log("getMobileDataEnabled returning " + retVal);
Robert Greenwaltc03fa502010-02-23 18:58:05 -0800994 return retVal;
995 }
996
997 /**
998 * @see ConnectivityManager#setMobileDataEnabled(boolean)
999 */
Robert Greenwalt8dcc28b2010-09-23 10:05:56 -07001000 public void setMobileDataEnabled(boolean enabled) {
Robert Greenwaltc03fa502010-02-23 18:58:05 -08001001 enforceChangePermission();
Wink Savilleed9c02b2010-12-03 12:01:38 -08001002 if (DBG) log("setMobileDataEnabled(" + enabled + ")");
Robert Greenwaltc03fa502010-02-23 18:58:05 -08001003
Robert Greenwalt8dcc28b2010-09-23 10:05:56 -07001004 mHandler.sendMessage(mHandler.obtainMessage(EVENT_SET_MOBILE_DATA,
1005 (enabled ? ENABLED : DISABLED), 0));
1006 }
1007
1008 private void handleSetMobileData(boolean enabled) {
Wink Savillee7982682010-12-07 10:31:02 -08001009 if (mNetTrackers[ConnectivityManager.TYPE_MOBILE] != null) {
1010 if (DBG) {
1011 Slog.d(TAG, mNetTrackers[ConnectivityManager.TYPE_MOBILE].toString() + enabled);
Robert Greenwaltc03fa502010-02-23 18:58:05 -08001012 }
Wink Savillee7982682010-12-07 10:31:02 -08001013 mNetTrackers[ConnectivityManager.TYPE_MOBILE].setDataEnable(enabled);
Robert Greenwaltc03fa502010-02-23 18:58:05 -08001014 }
1015 }
1016
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001017 private void enforceAccessPermission() {
Robert Greenwalt86e9e552009-07-16 17:21:39 -07001018 mContext.enforceCallingOrSelfPermission(
1019 android.Manifest.permission.ACCESS_NETWORK_STATE,
1020 "ConnectivityService");
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001021 }
1022
1023 private void enforceChangePermission() {
Robert Greenwalt86e9e552009-07-16 17:21:39 -07001024 mContext.enforceCallingOrSelfPermission(
1025 android.Manifest.permission.CHANGE_NETWORK_STATE,
1026 "ConnectivityService");
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001027 }
1028
Robert Greenwaltd0e18ff2010-01-26 11:40:34 -08001029 // TODO Make this a special check when it goes public
1030 private void enforceTetherChangePermission() {
1031 mContext.enforceCallingOrSelfPermission(
1032 android.Manifest.permission.CHANGE_NETWORK_STATE,
1033 "ConnectivityService");
1034 }
1035
Robert Greenwalt2a091d72010-02-11 18:18:40 -08001036 private void enforceTetherAccessPermission() {
1037 mContext.enforceCallingOrSelfPermission(
1038 android.Manifest.permission.ACCESS_NETWORK_STATE,
1039 "ConnectivityService");
1040 }
1041
Robert Greenwalt14f2ef42010-06-15 12:19:37 -07001042 private void enforceConnectivityInternalPermission() {
1043 mContext.enforceCallingOrSelfPermission(
1044 android.Manifest.permission.CONNECTIVITY_INTERNAL,
1045 "ConnectivityService");
1046 }
1047
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001048 /**
Robert Greenwalt86e9e552009-07-16 17:21:39 -07001049 * Handle a {@code DISCONNECTED} event. If this pertains to the non-active
1050 * network, we ignore it. If it is for the active network, we send out a
1051 * broadcast. But first, we check whether it might be possible to connect
1052 * to a different network.
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001053 * @param info the {@code NetworkInfo} for the network
1054 */
1055 private void handleDisconnect(NetworkInfo info) {
1056
Robert Greenwalt42acef32009-08-12 16:08:25 -07001057 int prevNetType = info.getType();
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001058
Robert Greenwalt42acef32009-08-12 16:08:25 -07001059 mNetTrackers[prevNetType].setTeardownRequested(false);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001060 /*
1061 * If the disconnected network is not the active one, then don't report
1062 * this as a loss of connectivity. What probably happened is that we're
1063 * getting the disconnect for a network that we explicitly disabled
1064 * in accordance with network preference policies.
1065 */
Robert Greenwalt42acef32009-08-12 16:08:25 -07001066 if (!mNetAttributes[prevNetType].isDefault()) {
1067 List pids = mNetRequestersPids[prevNetType];
1068 for (int i = 0; i<pids.size(); i++) {
1069 Integer pid = (Integer)pids.get(i);
1070 // will remove them because the net's no longer connected
1071 // need to do this now as only now do we know the pids and
1072 // can properly null things that are no longer referenced.
1073 reassessPidDns(pid.intValue(), false);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001074 }
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001075 }
1076
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001077 Intent intent = new Intent(ConnectivityManager.CONNECTIVITY_ACTION);
1078 intent.putExtra(ConnectivityManager.EXTRA_NETWORK_INFO, info);
1079 if (info.isFailover()) {
1080 intent.putExtra(ConnectivityManager.EXTRA_IS_FAILOVER, true);
1081 info.setFailover(false);
1082 }
1083 if (info.getReason() != null) {
1084 intent.putExtra(ConnectivityManager.EXTRA_REASON, info.getReason());
1085 }
1086 if (info.getExtraInfo() != null) {
Robert Greenwalt86e9e552009-07-16 17:21:39 -07001087 intent.putExtra(ConnectivityManager.EXTRA_EXTRA_INFO,
1088 info.getExtraInfo());
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001089 }
Robert Greenwalt42acef32009-08-12 16:08:25 -07001090
Robert Greenwaltcc4b4012010-01-25 17:54:29 -08001091 if (mNetAttributes[prevNetType].isDefault()) {
Robert Greenwaltf21ef7d2011-01-11 13:56:33 -08001092 tryFailover(prevNetType);
1093 if (mActiveDefaultNetwork != -1) {
1094 NetworkInfo switchTo = mNetTrackers[mActiveDefaultNetwork].getNetworkInfo();
Robert Greenwaltcc4b4012010-01-25 17:54:29 -08001095 intent.putExtra(ConnectivityManager.EXTRA_OTHER_NETWORK_INFO, switchTo);
1096 } else {
Robert Greenwalt029be812010-09-20 18:01:43 -07001097 mDefaultInetConditionPublished = 0; // we're not connected anymore
Robert Greenwaltcc4b4012010-01-25 17:54:29 -08001098 intent.putExtra(ConnectivityManager.EXTRA_NO_CONNECTIVITY, true);
1099 }
Robert Greenwaltda03c4e2010-01-20 19:29:41 -08001100 }
Robert Greenwalt029be812010-09-20 18:01:43 -07001101 intent.putExtra(ConnectivityManager.EXTRA_INET_CONDITION, mDefaultInetConditionPublished);
Robert Greenwaltda03c4e2010-01-20 19:29:41 -08001102 // do this before we broadcast the change
Robert Greenwaltc76b8fa2010-07-23 15:46:26 -07001103 handleConnectivityChange(prevNetType);
Robert Greenwaltda03c4e2010-01-20 19:29:41 -08001104
1105 sendStickyBroadcast(intent);
1106 /*
1107 * If the failover network is already connected, then immediately send
1108 * out a followup broadcast indicating successful failover
1109 */
Robert Greenwaltf21ef7d2011-01-11 13:56:33 -08001110 if (mActiveDefaultNetwork != -1) {
1111 sendConnectedBroadcast(mNetTrackers[mActiveDefaultNetwork].getNetworkInfo());
Robert Greenwaltda03c4e2010-01-20 19:29:41 -08001112 }
1113 }
1114
Robert Greenwaltf21ef7d2011-01-11 13:56:33 -08001115 private void tryFailover(int prevNetType) {
Robert Greenwalt42acef32009-08-12 16:08:25 -07001116 /*
Robert Greenwaltbff90182011-01-06 15:41:07 -08001117 * If this is a default network, check if other defaults are available.
1118 * Try to reconnect on all available and let them hash it out when
1119 * more than one connects.
Robert Greenwalt42acef32009-08-12 16:08:25 -07001120 */
Robert Greenwalt42acef32009-08-12 16:08:25 -07001121 if (mNetAttributes[prevNetType].isDefault()) {
Robert Greenwalt42acef32009-08-12 16:08:25 -07001122 if (mActiveDefaultNetwork == prevNetType) {
1123 mActiveDefaultNetwork = -1;
1124 }
1125
Robert Greenwaltf21ef7d2011-01-11 13:56:33 -08001126 // don't signal a reconnect for anything lower or equal priority than our
1127 // current connected default
1128 // TODO - don't filter by priority now - nice optimization but risky
1129// int currentPriority = -1;
1130// if (mActiveDefaultNetwork != -1) {
1131// currentPriority = mNetAttributes[mActiveDefaultNetwork].mPriority;
1132// }
Robert Greenwaltda03c4e2010-01-20 19:29:41 -08001133 for (int checkType=0; checkType <= ConnectivityManager.MAX_NETWORK_TYPE; checkType++) {
Robert Greenwalt5154ae762009-10-30 14:17:42 -07001134 if (checkType == prevNetType) continue;
1135 if (mNetAttributes[checkType] == null) continue;
Robert Greenwaltbff90182011-01-06 15:41:07 -08001136 if (!mNetAttributes[checkType].isDefault()) continue;
Wink Saville9f7a0b22011-01-26 15:43:49 -08001137
1138// Enabling the isAvailable() optimization caused mobile to not get
1139// selected if it was in the middle of error handling. Specifically
1140// a moble connection that took 30 seconds to complete the DEACTIVATE_DATA_CALL
1141// would not be available and we wouldn't get connected to anything.
1142// So removing the isAvailable() optimization below for now. TODO: This
1143// optimization should work and we need to investigate why it doesn't work.
1144// This could be related to how DEACTIVATE_DATA_CALL is reporting its
1145// complete before it is really complete.
1146// if (!mNetTrackers[checkType].isAvailable()) continue;
1147
Robert Greenwaltf21ef7d2011-01-11 13:56:33 -08001148// if (currentPriority >= mNetAttributes[checkType].mPriority) continue;
Robert Greenwalt42acef32009-08-12 16:08:25 -07001149
Robert Greenwaltf21ef7d2011-01-11 13:56:33 -08001150 NetworkStateTracker checkTracker = mNetTrackers[checkType];
1151 NetworkInfo checkInfo = checkTracker.getNetworkInfo();
1152 if (!checkInfo.isConnectedOrConnecting() || checkTracker.isTeardownRequested()) {
1153 checkInfo.setFailover(true);
1154 checkTracker.reconnect();
Robert Greenwalt42acef32009-08-12 16:08:25 -07001155 }
Robert Greenwaltf21ef7d2011-01-11 13:56:33 -08001156 if (DBG) log("Attempting to switch to " + checkInfo.getTypeName());
Robert Greenwalt42acef32009-08-12 16:08:25 -07001157 }
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001158 }
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001159 }
1160
1161 private void sendConnectedBroadcast(NetworkInfo info) {
Robert Greenwalt1e9aac22010-09-15 17:36:33 -07001162 sendGeneralBroadcast(info, ConnectivityManager.CONNECTIVITY_ACTION);
1163 }
1164
1165 private void sendInetConditionBroadcast(NetworkInfo info) {
1166 sendGeneralBroadcast(info, ConnectivityManager.INET_CONDITION_ACTION);
1167 }
1168
1169 private void sendGeneralBroadcast(NetworkInfo info, String bcastType) {
1170 Intent intent = new Intent(bcastType);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001171 intent.putExtra(ConnectivityManager.EXTRA_NETWORK_INFO, info);
1172 if (info.isFailover()) {
1173 intent.putExtra(ConnectivityManager.EXTRA_IS_FAILOVER, true);
1174 info.setFailover(false);
1175 }
1176 if (info.getReason() != null) {
1177 intent.putExtra(ConnectivityManager.EXTRA_REASON, info.getReason());
1178 }
1179 if (info.getExtraInfo() != null) {
Robert Greenwalt86e9e552009-07-16 17:21:39 -07001180 intent.putExtra(ConnectivityManager.EXTRA_EXTRA_INFO,
1181 info.getExtraInfo());
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001182 }
Robert Greenwaltd7085fc2010-09-08 15:24:47 -07001183 intent.putExtra(ConnectivityManager.EXTRA_INET_CONDITION, mDefaultInetConditionPublished);
Mike Lockwood0f79b542009-08-14 14:18:49 -04001184 sendStickyBroadcast(intent);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001185 }
1186
1187 /**
1188 * Called when an attempt to fail over to another network has failed.
1189 * @param info the {@link NetworkInfo} for the failed network
1190 */
1191 private void handleConnectionFailure(NetworkInfo info) {
1192 mNetTrackers[info.getType()].setTeardownRequested(false);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001193
Robert Greenwalt42acef32009-08-12 16:08:25 -07001194 String reason = info.getReason();
1195 String extraInfo = info.getExtraInfo();
Robert Greenwalt86e9e552009-07-16 17:21:39 -07001196
Robert Greenwalt572172b2010-10-08 16:35:52 -07001197 String reasonText;
1198 if (reason == null) {
1199 reasonText = ".";
1200 } else {
1201 reasonText = " (" + reason + ").";
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001202 }
Wink Savilleed9c02b2010-12-03 12:01:38 -08001203 loge("Attempt to connect to " + info.getTypeName() + " failed" + reasonText);
Robert Greenwalt42acef32009-08-12 16:08:25 -07001204
1205 Intent intent = new Intent(ConnectivityManager.CONNECTIVITY_ACTION);
1206 intent.putExtra(ConnectivityManager.EXTRA_NETWORK_INFO, info);
1207 if (getActiveNetworkInfo() == null) {
1208 intent.putExtra(ConnectivityManager.EXTRA_NO_CONNECTIVITY, true);
1209 }
1210 if (reason != null) {
1211 intent.putExtra(ConnectivityManager.EXTRA_REASON, reason);
1212 }
1213 if (extraInfo != null) {
1214 intent.putExtra(ConnectivityManager.EXTRA_EXTRA_INFO, extraInfo);
1215 }
1216 if (info.isFailover()) {
1217 intent.putExtra(ConnectivityManager.EXTRA_IS_FAILOVER, true);
1218 info.setFailover(false);
1219 }
Robert Greenwaltda03c4e2010-01-20 19:29:41 -08001220
Robert Greenwaltcc4b4012010-01-25 17:54:29 -08001221 if (mNetAttributes[info.getType()].isDefault()) {
Robert Greenwaltf21ef7d2011-01-11 13:56:33 -08001222 tryFailover(info.getType());
1223 if (mActiveDefaultNetwork != -1) {
1224 NetworkInfo switchTo = mNetTrackers[mActiveDefaultNetwork].getNetworkInfo();
Robert Greenwaltcc4b4012010-01-25 17:54:29 -08001225 intent.putExtra(ConnectivityManager.EXTRA_OTHER_NETWORK_INFO, switchTo);
1226 } else {
Robert Greenwalt029be812010-09-20 18:01:43 -07001227 mDefaultInetConditionPublished = 0;
Robert Greenwaltcc4b4012010-01-25 17:54:29 -08001228 intent.putExtra(ConnectivityManager.EXTRA_NO_CONNECTIVITY, true);
1229 }
Robert Greenwaltda03c4e2010-01-20 19:29:41 -08001230 }
Robert Greenwaltcc4b4012010-01-25 17:54:29 -08001231
Robert Greenwalt029be812010-09-20 18:01:43 -07001232 intent.putExtra(ConnectivityManager.EXTRA_INET_CONDITION, mDefaultInetConditionPublished);
Mike Lockwood0f79b542009-08-14 14:18:49 -04001233 sendStickyBroadcast(intent);
Robert Greenwaltda03c4e2010-01-20 19:29:41 -08001234 /*
1235 * If the failover network is already connected, then immediately send
1236 * out a followup broadcast indicating successful failover
1237 */
Robert Greenwaltf21ef7d2011-01-11 13:56:33 -08001238 if (mActiveDefaultNetwork != -1) {
1239 sendConnectedBroadcast(mNetTrackers[mActiveDefaultNetwork].getNetworkInfo());
Robert Greenwaltda03c4e2010-01-20 19:29:41 -08001240 }
Mike Lockwood0f79b542009-08-14 14:18:49 -04001241 }
1242
1243 private void sendStickyBroadcast(Intent intent) {
1244 synchronized(this) {
Dianne Hackborn1c633fc2009-12-08 19:45:14 -08001245 if (!mSystemReady) {
1246 mInitialBroadcast = new Intent(intent);
Mike Lockwood0f79b542009-08-14 14:18:49 -04001247 }
Dianne Hackborn1c633fc2009-12-08 19:45:14 -08001248 intent.addFlags(Intent.FLAG_RECEIVER_REGISTERED_ONLY_BEFORE_BOOT);
1249 mContext.sendStickyBroadcast(intent);
Mike Lockwood0f79b542009-08-14 14:18:49 -04001250 }
1251 }
1252
1253 void systemReady() {
1254 synchronized(this) {
1255 mSystemReady = true;
Dianne Hackborn1c633fc2009-12-08 19:45:14 -08001256 if (mInitialBroadcast != null) {
1257 mContext.sendStickyBroadcast(mInitialBroadcast);
1258 mInitialBroadcast = null;
Mike Lockwood0f79b542009-08-14 14:18:49 -04001259 }
1260 }
Robert Greenwalt434203a2010-10-11 16:00:27 -07001261 // load the global proxy at startup
1262 mHandler.sendMessage(mHandler.obtainMessage(EVENT_APPLY_GLOBAL_HTTP_PROXY));
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001263 }
1264
1265 private void handleConnect(NetworkInfo info) {
Robert Greenwalt42acef32009-08-12 16:08:25 -07001266 int type = info.getType();
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001267
1268 // snapshot isFailover, because sendConnectedBroadcast() resets it
1269 boolean isFailover = info.isFailover();
Robert Greenwalt42acef32009-08-12 16:08:25 -07001270 NetworkStateTracker thisNet = mNetTrackers[type];
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001271
Robert Greenwalt42acef32009-08-12 16:08:25 -07001272 // if this is a default net and other default is running
1273 // kill the one not preferred
1274 if (mNetAttributes[type].isDefault()) {
Robert Greenwalt42acef32009-08-12 16:08:25 -07001275 if (mActiveDefaultNetwork != -1 && mActiveDefaultNetwork != type) {
1276 if ((type != mNetworkPreference &&
1277 mNetAttributes[mActiveDefaultNetwork].mPriority >
1278 mNetAttributes[type].mPriority) ||
1279 mNetworkPreference == mActiveDefaultNetwork) {
1280 // don't accept this one
Wink Savilleed9c02b2010-12-03 12:01:38 -08001281 if (DBG) {
1282 log("Not broadcasting CONNECT_ACTION " +
Robert Greenwalt42acef32009-08-12 16:08:25 -07001283 "to torn down network " + info.getTypeName());
Wink Savilleed9c02b2010-12-03 12:01:38 -08001284 }
Robert Greenwalt42acef32009-08-12 16:08:25 -07001285 teardown(thisNet);
1286 return;
1287 } else {
1288 // tear down the other
1289 NetworkStateTracker otherNet =
1290 mNetTrackers[mActiveDefaultNetwork];
Wink Savilleed9c02b2010-12-03 12:01:38 -08001291 if (DBG) {
1292 log("Policy requires " + otherNet.getNetworkInfo().getTypeName() +
Robert Greenwalt42acef32009-08-12 16:08:25 -07001293 " teardown");
Wink Savilleed9c02b2010-12-03 12:01:38 -08001294 }
Robert Greenwalt42acef32009-08-12 16:08:25 -07001295 if (!teardown(otherNet)) {
Wink Savilleed9c02b2010-12-03 12:01:38 -08001296 loge("Network declined teardown request");
Robert Greenwalt42acef32009-08-12 16:08:25 -07001297 return;
1298 }
Robert Greenwalt14f2ef42010-06-15 12:19:37 -07001299 }
1300 }
1301 synchronized (ConnectivityService.this) {
1302 // have a new default network, release the transition wakelock in a second
1303 // if it's held. The second pause is to allow apps to reconnect over the
1304 // new network
1305 if (mNetTransitionWakeLock.isHeld()) {
1306 mHandler.sendMessageDelayed(mHandler.obtainMessage(
Robert Greenwaltf3331232010-09-24 14:32:21 -07001307 EVENT_CLEAR_NET_TRANSITION_WAKELOCK,
Robert Greenwalt14f2ef42010-06-15 12:19:37 -07001308 mNetTransitionWakeLockSerialNumber, 0),
1309 1000);
Robert Greenwalt42acef32009-08-12 16:08:25 -07001310 }
1311 }
1312 mActiveDefaultNetwork = type;
Robert Greenwaltd7085fc2010-09-08 15:24:47 -07001313 // this will cause us to come up initially as unconnected and switching
1314 // to connected after our normal pause unless somebody reports us as reall
1315 // disconnected
1316 mDefaultInetConditionPublished = 0;
1317 mDefaultConnectionSequence++;
1318 mInetConditionChangeInFlight = false;
1319 // Don't do this - if we never sign in stay, grey
1320 //reportNetworkCondition(mActiveDefaultNetwork, 100);
Robert Greenwalt42acef32009-08-12 16:08:25 -07001321 }
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001322 thisNet.setTeardownRequested(false);
Irfan Sheriffd649c122010-06-09 15:39:36 -07001323 updateNetworkSettings(thisNet);
Robert Greenwaltc76b8fa2010-07-23 15:46:26 -07001324 handleConnectivityChange(type);
Robert Greenwalt42acef32009-08-12 16:08:25 -07001325 sendConnectedBroadcast(info);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001326 }
1327
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001328 /**
Robert Greenwaltc76b8fa2010-07-23 15:46:26 -07001329 * After a change in the connectivity state of a network. We're mainly
1330 * concerned with making sure that the list of DNS servers is set up
1331 * according to which networks are connected, and ensuring that the
1332 * right routing table entries exist.
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001333 */
Robert Greenwaltc76b8fa2010-07-23 15:46:26 -07001334 private void handleConnectivityChange(int netType) {
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001335 /*
Robert Greenwalt42acef32009-08-12 16:08:25 -07001336 * If a non-default network is enabled, add the host routes that
Robert Greenwaltc76b8fa2010-07-23 15:46:26 -07001337 * will allow it's DNS servers to be accessed.
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001338 */
Robert Greenwaltc76b8fa2010-07-23 15:46:26 -07001339 handleDnsConfigurationChange(netType);
Robert Greenwalt42acef32009-08-12 16:08:25 -07001340
Robert Greenwaltc76b8fa2010-07-23 15:46:26 -07001341 if (mNetTrackers[netType].getNetworkInfo().isConnected()) {
1342 if (mNetAttributes[netType].isDefault()) {
Robert Greenwalt434203a2010-10-11 16:00:27 -07001343 handleApplyDefaultProxy(netType);
Robert Greenwaltc76b8fa2010-07-23 15:46:26 -07001344 addDefaultRoute(mNetTrackers[netType]);
Robert Greenwalt42acef32009-08-12 16:08:25 -07001345 } else {
Robert Greenwalt13ec4062011-04-01 10:51:22 -07001346 // many radios add a default route even when we don't want one.
1347 // remove the default route unless we need it for our active network
1348 if (mActiveDefaultNetwork != -1) {
1349 LinkProperties defaultLinkProperties =
1350 mNetTrackers[mActiveDefaultNetwork].getLinkProperties();
1351 LinkProperties newLinkProperties =
1352 mNetTrackers[netType].getLinkProperties();
1353 String defaultIface = defaultLinkProperties.getInterfaceName();
1354 if (defaultIface != null &&
1355 !defaultIface.equals(newLinkProperties.getInterfaceName())) {
1356 removeDefaultRoute(mNetTrackers[netType]);
1357 }
1358 }
Michael Jurka8fad7ee2011-03-30 19:54:08 -07001359 addPrivateDnsRoutes(mNetTrackers[netType]);
Robert Greenwaltc76b8fa2010-07-23 15:46:26 -07001360 }
1361 } else {
1362 if (mNetAttributes[netType].isDefault()) {
1363 removeDefaultRoute(mNetTrackers[netType]);
1364 } else {
1365 removePrivateDnsRoutes(mNetTrackers[netType]);
Robert Greenwalt42acef32009-08-12 16:08:25 -07001366 }
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001367 }
1368 }
1369
Irfan Sheriffd649c122010-06-09 15:39:36 -07001370 private void addPrivateDnsRoutes(NetworkStateTracker nt) {
Irfan Sheriffd649c122010-06-09 15:39:36 -07001371 boolean privateDnsRouteSet = nt.isPrivateDnsRouteSet();
Robert Greenwalt37e65eb2010-08-30 10:56:47 -07001372 LinkProperties p = nt.getLinkProperties();
Robert Greenwalt47f69fe2010-06-15 15:43:39 -07001373 if (p == null) return;
1374 String interfaceName = p.getInterfaceName();
Irfan Sheriffd649c122010-06-09 15:39:36 -07001375
1376 if (DBG) {
Wink Savilleed9c02b2010-12-03 12:01:38 -08001377 log("addPrivateDnsRoutes for " + nt +
Irfan Sheriffd649c122010-06-09 15:39:36 -07001378 "(" + interfaceName + ") - mPrivateDnsRouteSet = " + privateDnsRouteSet);
1379 }
Irfan Sheriffd649c122010-06-09 15:39:36 -07001380 if (interfaceName != null && !privateDnsRouteSet) {
Robert Greenwalt47f69fe2010-06-15 15:43:39 -07001381 Collection<InetAddress> dnsList = p.getDnses();
1382 for (InetAddress dns : dnsList) {
Wink Savilleed9c02b2010-12-03 12:01:38 -08001383 if (DBG) log(" adding " + dns);
Robert Greenwalt585ac0f2010-08-27 09:24:29 -07001384 NetworkUtils.addHostRoute(interfaceName, dns, null);
Irfan Sheriffd649c122010-06-09 15:39:36 -07001385 }
1386 nt.privateDnsRouteSet(true);
1387 }
1388 }
1389
1390 private void removePrivateDnsRoutes(NetworkStateTracker nt) {
1391 // TODO - we should do this explicitly but the NetUtils api doesnt
1392 // support this yet - must remove all. No worse than before
Robert Greenwalt37e65eb2010-08-30 10:56:47 -07001393 LinkProperties p = nt.getLinkProperties();
Robert Greenwalt47f69fe2010-06-15 15:43:39 -07001394 if (p == null) return;
1395 String interfaceName = p.getInterfaceName();
Irfan Sheriffd649c122010-06-09 15:39:36 -07001396 boolean privateDnsRouteSet = nt.isPrivateDnsRouteSet();
1397 if (interfaceName != null && privateDnsRouteSet) {
1398 if (DBG) {
Wink Savilleed9c02b2010-12-03 12:01:38 -08001399 log("removePrivateDnsRoutes for " + nt.getNetworkInfo().getTypeName() +
Irfan Sheriffd649c122010-06-09 15:39:36 -07001400 " (" + interfaceName + ")");
1401 }
1402 NetworkUtils.removeHostRoutes(interfaceName);
1403 nt.privateDnsRouteSet(false);
1404 }
1405 }
1406
Irfan Sheriffd649c122010-06-09 15:39:36 -07001407
1408 private void addDefaultRoute(NetworkStateTracker nt) {
Robert Greenwalt37e65eb2010-08-30 10:56:47 -07001409 LinkProperties p = nt.getLinkProperties();
Robert Greenwalt47f69fe2010-06-15 15:43:39 -07001410 if (p == null) return;
1411 String interfaceName = p.getInterfaceName();
Robert Greenwalt992564e2011-02-09 13:56:06 -08001412 if (TextUtils.isEmpty(interfaceName)) return;
1413 for (InetAddress gateway : p.getGateways()) {
Irfan Sheriffd649c122010-06-09 15:39:36 -07001414
Robert Greenwaltedcb4f92011-03-22 18:47:42 -07001415 if (NetworkUtils.addHostRoute(interfaceName, gateway, null) &&
1416 NetworkUtils.addDefaultRoute(interfaceName, gateway)) {
1417 if (DBG) {
1418 NetworkInfo networkInfo = nt.getNetworkInfo();
1419 log("addDefaultRoute for " + networkInfo.getTypeName() +
1420 " (" + interfaceName + "), GatewayAddr=" + gateway.getHostAddress());
1421 }
Irfan Sheriffd649c122010-06-09 15:39:36 -07001422 }
Irfan Sheriffd649c122010-06-09 15:39:36 -07001423 }
1424 }
1425
1426
1427 public void removeDefaultRoute(NetworkStateTracker nt) {
Robert Greenwalt37e65eb2010-08-30 10:56:47 -07001428 LinkProperties p = nt.getLinkProperties();
Robert Greenwalt47f69fe2010-06-15 15:43:39 -07001429 if (p == null) return;
1430 String interfaceName = p.getInterfaceName();
Irfan Sheriffd649c122010-06-09 15:39:36 -07001431
Robert Greenwaltc76b8fa2010-07-23 15:46:26 -07001432 if (interfaceName != null) {
Robert Greenwaltedcb4f92011-03-22 18:47:42 -07001433 if (NetworkUtils.removeDefaultRoute(interfaceName) >= 0) {
1434 if (DBG) {
1435 NetworkInfo networkInfo = nt.getNetworkInfo();
1436 log("removeDefaultRoute for " + networkInfo.getTypeName() + " (" +
1437 interfaceName + ")");
1438 }
Irfan Sheriffd649c122010-06-09 15:39:36 -07001439 }
Irfan Sheriffd649c122010-06-09 15:39:36 -07001440 }
1441 }
1442
1443 /**
1444 * Reads the network specific TCP buffer sizes from SystemProperties
1445 * net.tcp.buffersize.[default|wifi|umts|edge|gprs] and set them for system
1446 * wide use
1447 */
1448 public void updateNetworkSettings(NetworkStateTracker nt) {
1449 String key = nt.getTcpBufferSizesPropName();
1450 String bufferSizes = SystemProperties.get(key);
1451
1452 if (bufferSizes.length() == 0) {
Wink Savilleed9c02b2010-12-03 12:01:38 -08001453 loge(key + " not found in system properties. Using defaults");
Irfan Sheriffd649c122010-06-09 15:39:36 -07001454
1455 // Setting to default values so we won't be stuck to previous values
1456 key = "net.tcp.buffersize.default";
1457 bufferSizes = SystemProperties.get(key);
1458 }
1459
1460 // Set values in kernel
1461 if (bufferSizes.length() != 0) {
1462 if (DBG) {
Wink Savilleed9c02b2010-12-03 12:01:38 -08001463 log("Setting TCP values: [" + bufferSizes
Irfan Sheriffd649c122010-06-09 15:39:36 -07001464 + "] which comes from [" + key + "]");
1465 }
1466 setBufferSize(bufferSizes);
1467 }
1468 }
1469
1470 /**
1471 * Writes TCP buffer sizes to /sys/kernel/ipv4/tcp_[r/w]mem_[min/def/max]
1472 * which maps to /proc/sys/net/ipv4/tcp_rmem and tcpwmem
1473 *
1474 * @param bufferSizes in the format of "readMin, readInitial, readMax,
1475 * writeMin, writeInitial, writeMax"
1476 */
1477 private void setBufferSize(String bufferSizes) {
1478 try {
1479 String[] values = bufferSizes.split(",");
1480
1481 if (values.length == 6) {
1482 final String prefix = "/sys/kernel/ipv4/tcp_";
1483 stringToFile(prefix + "rmem_min", values[0]);
1484 stringToFile(prefix + "rmem_def", values[1]);
1485 stringToFile(prefix + "rmem_max", values[2]);
1486 stringToFile(prefix + "wmem_min", values[3]);
1487 stringToFile(prefix + "wmem_def", values[4]);
1488 stringToFile(prefix + "wmem_max", values[5]);
1489 } else {
Wink Savilleed9c02b2010-12-03 12:01:38 -08001490 loge("Invalid buffersize string: " + bufferSizes);
Irfan Sheriffd649c122010-06-09 15:39:36 -07001491 }
1492 } catch (IOException e) {
Wink Savilleed9c02b2010-12-03 12:01:38 -08001493 loge("Can't set tcp buffer sizes:" + e);
Irfan Sheriffd649c122010-06-09 15:39:36 -07001494 }
1495 }
1496
1497 /**
1498 * Writes string to file. Basically same as "echo -n $string > $filename"
1499 *
1500 * @param filename
1501 * @param string
1502 * @throws IOException
1503 */
1504 private void stringToFile(String filename, String string) throws IOException {
1505 FileWriter out = new FileWriter(filename);
1506 try {
1507 out.write(string);
1508 } finally {
1509 out.close();
1510 }
1511 }
1512
1513
Robert Greenwalt42acef32009-08-12 16:08:25 -07001514 /**
1515 * Adjust the per-process dns entries (net.dns<x>.<pid>) based
1516 * on the highest priority active net which this process requested.
1517 * If there aren't any, clear it out
1518 */
1519 private void reassessPidDns(int myPid, boolean doBump)
1520 {
Wink Savilleed9c02b2010-12-03 12:01:38 -08001521 if (DBG) log("reassessPidDns for pid " + myPid);
Robert Greenwalt42acef32009-08-12 16:08:25 -07001522 for(int i : mPriorityList) {
1523 if (mNetAttributes[i].isDefault()) {
1524 continue;
1525 }
1526 NetworkStateTracker nt = mNetTrackers[i];
Robert Greenwalt86e9e552009-07-16 17:21:39 -07001527 if (nt.getNetworkInfo().isConnected() &&
1528 !nt.isTeardownRequested()) {
Robert Greenwalt37e65eb2010-08-30 10:56:47 -07001529 LinkProperties p = nt.getLinkProperties();
Robert Greenwalt47f69fe2010-06-15 15:43:39 -07001530 if (p == null) continue;
Robert Greenwalt42acef32009-08-12 16:08:25 -07001531 List pids = mNetRequestersPids[i];
1532 for (int j=0; j<pids.size(); j++) {
1533 Integer pid = (Integer)pids.get(j);
1534 if (pid.intValue() == myPid) {
Robert Greenwalt47f69fe2010-06-15 15:43:39 -07001535 Collection<InetAddress> dnses = p.getDnses();
1536 writePidDns(dnses, myPid);
Robert Greenwalt42acef32009-08-12 16:08:25 -07001537 if (doBump) {
1538 bumpDns();
1539 }
1540 return;
1541 }
1542 }
1543 }
1544 }
1545 // nothing found - delete
1546 for (int i = 1; ; i++) {
1547 String prop = "net.dns" + i + "." + myPid;
1548 if (SystemProperties.get(prop).length() == 0) {
1549 if (doBump) {
1550 bumpDns();
1551 }
1552 return;
1553 }
1554 SystemProperties.set(prop, "");
1555 }
1556 }
1557
Robert Greenwalt10398722010-12-17 15:20:36 -08001558 // return true if results in a change
1559 private boolean writePidDns(Collection <InetAddress> dnses, int pid) {
Robert Greenwalt42acef32009-08-12 16:08:25 -07001560 int j = 1;
Robert Greenwalt10398722010-12-17 15:20:36 -08001561 boolean changed = false;
Robert Greenwalt47f69fe2010-06-15 15:43:39 -07001562 for (InetAddress dns : dnses) {
Robert Greenwalt10398722010-12-17 15:20:36 -08001563 String dnsString = dns.getHostAddress();
1564 if (changed || !dnsString.equals(SystemProperties.get("net.dns" + j + "." + pid))) {
1565 changed = true;
1566 SystemProperties.set("net.dns" + j++ + "." + pid, dns.getHostAddress());
1567 }
Robert Greenwalt42acef32009-08-12 16:08:25 -07001568 }
Robert Greenwalt10398722010-12-17 15:20:36 -08001569 return changed;
Robert Greenwalt42acef32009-08-12 16:08:25 -07001570 }
1571
1572 private void bumpDns() {
1573 /*
1574 * Bump the property that tells the name resolver library to reread
1575 * the DNS server list from the properties.
1576 */
1577 String propVal = SystemProperties.get("net.dnschange");
1578 int n = 0;
1579 if (propVal.length() != 0) {
1580 try {
1581 n = Integer.parseInt(propVal);
1582 } catch (NumberFormatException e) {}
1583 }
1584 SystemProperties.set("net.dnschange", "" + (n+1));
Robert Greenwalt03595d02010-11-02 14:08:23 -07001585 /*
1586 * Tell the VMs to toss their DNS caches
1587 */
1588 Intent intent = new Intent(Intent.ACTION_CLEAR_DNS_CACHE);
1589 intent.addFlags(Intent.FLAG_RECEIVER_REPLACE_PENDING);
Stan Chesnutt3d1db862011-01-05 17:14:03 -08001590 /*
1591 * Connectivity events can happen before boot has completed ...
1592 */
1593 intent.addFlags(Intent.FLAG_RECEIVER_REGISTERED_ONLY_BEFORE_BOOT);
Robert Greenwalt03595d02010-11-02 14:08:23 -07001594 mContext.sendBroadcast(intent);
Robert Greenwalt42acef32009-08-12 16:08:25 -07001595 }
1596
Robert Greenwaltc76b8fa2010-07-23 15:46:26 -07001597 private void handleDnsConfigurationChange(int netType) {
Robert Greenwalt42acef32009-08-12 16:08:25 -07001598 // add default net's dns entries
Robert Greenwaltc76b8fa2010-07-23 15:46:26 -07001599 NetworkStateTracker nt = mNetTrackers[netType];
1600 if (nt != null && nt.getNetworkInfo().isConnected() && !nt.isTeardownRequested()) {
Robert Greenwalt37e65eb2010-08-30 10:56:47 -07001601 LinkProperties p = nt.getLinkProperties();
Robert Greenwaltc76b8fa2010-07-23 15:46:26 -07001602 if (p == null) return;
1603 Collection<InetAddress> dnses = p.getDnses();
Robert Greenwalt10398722010-12-17 15:20:36 -08001604 boolean changed = false;
Robert Greenwaltc76b8fa2010-07-23 15:46:26 -07001605 if (mNetAttributes[netType].isDefault()) {
1606 int j = 1;
Robert Greenwalte90aa5e2010-09-01 11:34:05 -07001607 if (dnses.size() == 0 && mDefaultDns != null) {
Robert Greenwalt10398722010-12-17 15:20:36 -08001608 String dnsString = mDefaultDns.getHostAddress();
1609 if (!dnsString.equals(SystemProperties.get("net.dns1"))) {
1610 if (DBG) {
1611 log("no dns provided - using " + dnsString);
1612 }
1613 changed = true;
1614 SystemProperties.set("net.dns1", dnsString);
Robert Greenwalt42acef32009-08-12 16:08:25 -07001615 }
Robert Greenwalte90aa5e2010-09-01 11:34:05 -07001616 j++;
1617 } else {
1618 for (InetAddress dns : dnses) {
Robert Greenwalt10398722010-12-17 15:20:36 -08001619 String dnsString = dns.getHostAddress();
1620 if (!changed && dnsString.equals(SystemProperties.get("net.dns" + j))) {
1621 j++;
1622 continue;
1623 }
Robert Greenwalte90aa5e2010-09-01 11:34:05 -07001624 if (DBG) {
Wink Savilleed9c02b2010-12-03 12:01:38 -08001625 log("adding dns " + dns + " for " +
Robert Greenwalte90aa5e2010-09-01 11:34:05 -07001626 nt.getNetworkInfo().getTypeName());
1627 }
Robert Greenwalt10398722010-12-17 15:20:36 -08001628 changed = true;
1629 SystemProperties.set("net.dns" + j++, dnsString);
Robert Greenwalte90aa5e2010-09-01 11:34:05 -07001630 }
Robert Greenwaltc76b8fa2010-07-23 15:46:26 -07001631 }
1632 for (int k=j ; k<mNumDnsEntries; k++) {
Robert Greenwalt10398722010-12-17 15:20:36 -08001633 if (changed || !TextUtils.isEmpty(SystemProperties.get("net.dns" + k))) {
1634 if (DBG) log("erasing net.dns" + k);
1635 changed = true;
1636 SystemProperties.set("net.dns" + k, "");
1637 }
Robert Greenwaltc76b8fa2010-07-23 15:46:26 -07001638 }
1639 mNumDnsEntries = j;
1640 } else {
1641 // set per-pid dns for attached secondary nets
1642 List pids = mNetRequestersPids[netType];
1643 for (int y=0; y< pids.size(); y++) {
1644 Integer pid = (Integer)pids.get(y);
Robert Greenwalt10398722010-12-17 15:20:36 -08001645 changed = writePidDns(dnses, pid.intValue());
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001646 }
1647 }
Robert Greenwalt10398722010-12-17 15:20:36 -08001648 if (changed) bumpDns();
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001649 }
Robert Greenwalt42acef32009-08-12 16:08:25 -07001650 }
1651
1652 private int getRestoreDefaultNetworkDelay() {
1653 String restoreDefaultNetworkDelayStr = SystemProperties.get(
1654 NETWORK_RESTORE_DELAY_PROP_NAME);
1655 if(restoreDefaultNetworkDelayStr != null &&
1656 restoreDefaultNetworkDelayStr.length() != 0) {
1657 try {
1658 return Integer.valueOf(restoreDefaultNetworkDelayStr);
1659 } catch (NumberFormatException e) {
1660 }
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001661 }
Robert Greenwalt42acef32009-08-12 16:08:25 -07001662 return RESTORE_DEFAULT_NETWORK_DELAY;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001663 }
1664
1665 @Override
1666 protected void dump(FileDescriptor fd, PrintWriter pw, String[] args) {
Robert Greenwalt86e9e552009-07-16 17:21:39 -07001667 if (mContext.checkCallingOrSelfPermission(
1668 android.Manifest.permission.DUMP)
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001669 != PackageManager.PERMISSION_GRANTED) {
Robert Greenwalt86e9e552009-07-16 17:21:39 -07001670 pw.println("Permission Denial: can't dump ConnectivityService " +
1671 "from from pid=" + Binder.getCallingPid() + ", uid=" +
1672 Binder.getCallingUid());
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001673 return;
1674 }
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001675 pw.println();
1676 for (NetworkStateTracker nst : mNetTrackers) {
Robert Greenwaltb9285352009-12-21 18:24:07 -08001677 if (nst != null) {
1678 if (nst.getNetworkInfo().isConnected()) {
1679 pw.println("Active network: " + nst.getNetworkInfo().
1680 getTypeName());
1681 }
1682 pw.println(nst.getNetworkInfo());
1683 pw.println(nst);
1684 pw.println();
Robert Greenwalt42acef32009-08-12 16:08:25 -07001685 }
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001686 }
Robert Greenwaltb9285352009-12-21 18:24:07 -08001687
1688 pw.println("Network Requester Pids:");
1689 for (int net : mPriorityList) {
1690 String pidString = net + ": ";
1691 for (Object pid : mNetRequestersPids[net]) {
1692 pidString = pidString + pid.toString() + ", ";
1693 }
1694 pw.println(pidString);
1695 }
1696 pw.println();
1697
1698 pw.println("FeatureUsers:");
1699 for (Object requester : mFeatureUsers) {
1700 pw.println(requester.toString());
1701 }
1702 pw.println();
Robert Greenwalt2a091d72010-02-11 18:18:40 -08001703
Robert Greenwalt14f2ef42010-06-15 12:19:37 -07001704 synchronized (this) {
1705 pw.println("NetworkTranstionWakeLock is currently " +
1706 (mNetTransitionWakeLock.isHeld() ? "" : "not ") + "held.");
1707 pw.println("It was last requested for "+mNetTransitionWakeLockCausedBy);
1708 }
1709 pw.println();
1710
Robert Greenwalt2a091d72010-02-11 18:18:40 -08001711 mTethering.dump(fd, pw, args);
Robert Greenwalt4e8dfef2010-09-20 14:35:25 -07001712
1713 if (mInetLog != null) {
1714 pw.println();
1715 pw.println("Inet condition reports:");
1716 for(int i = 0; i < mInetLog.size(); i++) {
1717 pw.println(mInetLog.get(i));
1718 }
1719 }
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001720 }
1721
Robert Greenwalt42acef32009-08-12 16:08:25 -07001722 // must be stateless - things change under us.
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001723 private class MyHandler extends Handler {
Wink Savillebb08caf2010-09-02 19:23:52 -07001724 public MyHandler(Looper looper) {
1725 super(looper);
1726 }
1727
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001728 @Override
1729 public void handleMessage(Message msg) {
1730 NetworkInfo info;
1731 switch (msg.what) {
1732 case NetworkStateTracker.EVENT_STATE_CHANGED:
1733 info = (NetworkInfo) msg.obj;
Robert Greenwalt511288a2009-12-07 11:33:18 -08001734 int type = info.getType();
1735 NetworkInfo.State state = info.getState();
Robert Greenwalt6e6dec22010-01-25 16:14:00 -08001736 // only do this optimization for wifi. It going into scan mode for location
1737 // services generates alot of noise. Meanwhile the mms apn won't send out
1738 // subsequent notifications when on default cellular because it never
1739 // disconnects.. so only do this to wifi notifications. Fixed better when the
1740 // APN notifications are standardized.
1741 if (mNetAttributes[type].mLastState == state &&
1742 mNetAttributes[type].mRadio == ConnectivityManager.TYPE_WIFI) {
Robert Greenwalt511288a2009-12-07 11:33:18 -08001743 if (DBG) {
Robert Greenwalt6e6dec22010-01-25 16:14:00 -08001744 // TODO - remove this after we validate the dropping doesn't break
1745 // anything
Wink Savilleed9c02b2010-12-03 12:01:38 -08001746 log("Dropping ConnectivityChange for " +
Robert Greenwalt1193ae42010-01-13 09:36:31 -08001747 info.getTypeName() + ": " +
Robert Greenwalt511288a2009-12-07 11:33:18 -08001748 state + "/" + info.getDetailedState());
1749 }
1750 return;
1751 }
1752 mNetAttributes[type].mLastState = state;
1753
Wink Savilleed9c02b2010-12-03 12:01:38 -08001754 if (DBG) log("ConnectivityChange for " +
Robert Greenwalt86e9e552009-07-16 17:21:39 -07001755 info.getTypeName() + ": " +
Robert Greenwalt511288a2009-12-07 11:33:18 -08001756 state + "/" + info.getDetailedState());
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001757
1758 // Connectivity state changed:
1759 // [31-13] Reserved for future use
Robert Greenwalt86e9e552009-07-16 17:21:39 -07001760 // [12-9] Network subtype (for mobile network, as defined
1761 // by TelephonyManager)
1762 // [8-3] Detailed state ordinal (as defined by
1763 // NetworkInfo.DetailedState)
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001764 // [2-0] Network type (as defined by ConnectivityManager)
1765 int eventLogParam = (info.getType() & 0x7) |
1766 ((info.getDetailedState().ordinal() & 0x3f) << 3) |
1767 (info.getSubtype() << 9);
Doug Zongkerab5c49c2009-12-04 10:31:43 -08001768 EventLog.writeEvent(EventLogTags.CONNECTIVITY_STATE_CHANGED,
Robert Greenwalt86e9e552009-07-16 17:21:39 -07001769 eventLogParam);
1770
1771 if (info.getDetailedState() ==
1772 NetworkInfo.DetailedState.FAILED) {
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001773 handleConnectionFailure(info);
Robert Greenwalt511288a2009-12-07 11:33:18 -08001774 } else if (state == NetworkInfo.State.DISCONNECTED) {
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001775 handleDisconnect(info);
Robert Greenwalt511288a2009-12-07 11:33:18 -08001776 } else if (state == NetworkInfo.State.SUSPENDED) {
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001777 // TODO: need to think this over.
Robert Greenwalt86e9e552009-07-16 17:21:39 -07001778 // the logic here is, handle SUSPENDED the same as
1779 // DISCONNECTED. The only difference being we are
1780 // broadcasting an intent with NetworkInfo that's
1781 // suspended. This allows the applications an
1782 // opportunity to handle DISCONNECTED and SUSPENDED
1783 // differently, or not.
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001784 handleDisconnect(info);
Robert Greenwalt511288a2009-12-07 11:33:18 -08001785 } else if (state == NetworkInfo.State.CONNECTED) {
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001786 handleConnect(info);
1787 }
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001788 break;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001789 case NetworkStateTracker.EVENT_CONFIGURATION_CHANGED:
Robert Greenwaltc76b8fa2010-07-23 15:46:26 -07001790 info = (NetworkInfo) msg.obj;
1791 type = info.getType();
Robert Greenwalt434203a2010-10-11 16:00:27 -07001792 handleConnectivityChange(type);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001793 break;
Robert Greenwaltf3331232010-09-24 14:32:21 -07001794 case EVENT_CLEAR_NET_TRANSITION_WAKELOCK:
Robert Greenwalt14f2ef42010-06-15 12:19:37 -07001795 String causedBy = null;
1796 synchronized (ConnectivityService.this) {
1797 if (msg.arg1 == mNetTransitionWakeLockSerialNumber &&
1798 mNetTransitionWakeLock.isHeld()) {
1799 mNetTransitionWakeLock.release();
1800 causedBy = mNetTransitionWakeLockCausedBy;
1801 }
1802 }
1803 if (causedBy != null) {
Wink Savilleed9c02b2010-12-03 12:01:38 -08001804 log("NetTransition Wakelock for " + causedBy + " released by timeout");
Robert Greenwalt14f2ef42010-06-15 12:19:37 -07001805 }
Robert Greenwalt057d5e92010-09-09 14:05:10 -07001806 break;
Robert Greenwalt8dcc28b2010-09-23 10:05:56 -07001807 case EVENT_RESTORE_DEFAULT_NETWORK:
Robert Greenwalt9c75d4a2009-09-27 17:27:04 -07001808 FeatureUser u = (FeatureUser)msg.obj;
1809 u.expire();
Robert Greenwaltd7085fc2010-09-08 15:24:47 -07001810 break;
Robert Greenwalt8dcc28b2010-09-23 10:05:56 -07001811 case EVENT_INET_CONDITION_CHANGE:
1812 {
1813 int netType = msg.arg1;
1814 int condition = msg.arg2;
1815 handleInetConditionChange(netType, condition);
Robert Greenwalt14f2ef42010-06-15 12:19:37 -07001816 break;
Robert Greenwalt8dcc28b2010-09-23 10:05:56 -07001817 }
1818 case EVENT_INET_CONDITION_HOLD_END:
1819 {
1820 int netType = msg.arg1;
1821 int sequence = msg.arg2;
1822 handleInetConditionHoldEnd(netType, sequence);
Robert Greenwaltd7085fc2010-09-08 15:24:47 -07001823 break;
Robert Greenwalt8dcc28b2010-09-23 10:05:56 -07001824 }
1825 case EVENT_SET_NETWORK_PREFERENCE:
1826 {
1827 int preference = msg.arg1;
1828 handleSetNetworkPreference(preference);
1829 break;
1830 }
1831 case EVENT_SET_BACKGROUND_DATA:
1832 {
1833 boolean enabled = (msg.arg1 == ENABLED);
1834 handleSetBackgroundData(enabled);
1835 break;
1836 }
1837 case EVENT_SET_MOBILE_DATA:
1838 {
1839 boolean enabled = (msg.arg1 == ENABLED);
1840 handleSetMobileData(enabled);
1841 break;
1842 }
Robert Greenwalt434203a2010-10-11 16:00:27 -07001843 case EVENT_APPLY_GLOBAL_HTTP_PROXY:
1844 {
1845 handleDeprecatedGlobalHttpProxy();
1846 }
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001847 }
1848 }
1849 }
Robert Greenwaltd0e18ff2010-01-26 11:40:34 -08001850
1851 // javadoc from interface
Robert Greenwalt5a735062010-03-02 17:25:02 -08001852 public int tether(String iface) {
Robert Greenwaltd0e18ff2010-01-26 11:40:34 -08001853 enforceTetherChangePermission();
Robert Greenwalt5a735062010-03-02 17:25:02 -08001854
1855 if (isTetheringSupported()) {
1856 return mTethering.tether(iface);
1857 } else {
1858 return ConnectivityManager.TETHER_ERROR_UNSUPPORTED;
1859 }
Robert Greenwaltd0e18ff2010-01-26 11:40:34 -08001860 }
1861
1862 // javadoc from interface
Robert Greenwalt5a735062010-03-02 17:25:02 -08001863 public int untether(String iface) {
Robert Greenwaltd0e18ff2010-01-26 11:40:34 -08001864 enforceTetherChangePermission();
Robert Greenwalt5a735062010-03-02 17:25:02 -08001865
1866 if (isTetheringSupported()) {
1867 return mTethering.untether(iface);
1868 } else {
1869 return ConnectivityManager.TETHER_ERROR_UNSUPPORTED;
1870 }
1871 }
1872
1873 // javadoc from interface
1874 public int getLastTetherError(String iface) {
1875 enforceTetherAccessPermission();
1876
1877 if (isTetheringSupported()) {
1878 return mTethering.getLastTetherError(iface);
1879 } else {
1880 return ConnectivityManager.TETHER_ERROR_UNSUPPORTED;
1881 }
Robert Greenwalt2a091d72010-02-11 18:18:40 -08001882 }
1883
1884 // TODO - proper iface API for selection by property, inspection, etc
1885 public String[] getTetherableUsbRegexs() {
1886 enforceTetherAccessPermission();
1887 if (isTetheringSupported()) {
1888 return mTethering.getTetherableUsbRegexs();
1889 } else {
1890 return new String[0];
1891 }
1892 }
1893
1894 public String[] getTetherableWifiRegexs() {
1895 enforceTetherAccessPermission();
1896 if (isTetheringSupported()) {
1897 return mTethering.getTetherableWifiRegexs();
1898 } else {
1899 return new String[0];
1900 }
Robert Greenwaltd0e18ff2010-01-26 11:40:34 -08001901 }
1902
Danica Chang6fdd0c62010-08-11 14:54:43 -07001903 public String[] getTetherableBluetoothRegexs() {
1904 enforceTetherAccessPermission();
1905 if (isTetheringSupported()) {
1906 return mTethering.getTetherableBluetoothRegexs();
1907 } else {
1908 return new String[0];
1909 }
1910 }
1911
Robert Greenwaltd0e18ff2010-01-26 11:40:34 -08001912 // TODO - move iface listing, queries, etc to new module
1913 // javadoc from interface
1914 public String[] getTetherableIfaces() {
Robert Greenwalt2a091d72010-02-11 18:18:40 -08001915 enforceTetherAccessPermission();
Robert Greenwaltd0e18ff2010-01-26 11:40:34 -08001916 return mTethering.getTetherableIfaces();
1917 }
1918
1919 public String[] getTetheredIfaces() {
Robert Greenwalt2a091d72010-02-11 18:18:40 -08001920 enforceTetherAccessPermission();
Robert Greenwaltd0e18ff2010-01-26 11:40:34 -08001921 return mTethering.getTetheredIfaces();
1922 }
Robert Greenwalt2a091d72010-02-11 18:18:40 -08001923
Robert Greenwalt5a735062010-03-02 17:25:02 -08001924 public String[] getTetheringErroredIfaces() {
1925 enforceTetherAccessPermission();
1926 return mTethering.getErroredIfaces();
1927 }
1928
Robert Greenwalt2a091d72010-02-11 18:18:40 -08001929 // if ro.tether.denied = true we default to no tethering
1930 // gservices could set the secure setting to 1 though to enable it on a build where it
1931 // had previously been turned off.
1932 public boolean isTetheringSupported() {
1933 enforceTetherAccessPermission();
1934 int defaultVal = (SystemProperties.get("ro.tether.denied").equals("true") ? 0 : 1);
Robert Greenwaltc9d5fb72010-02-25 12:29:30 -08001935 boolean tetherEnabledInSettings = (Settings.Secure.getInt(mContext.getContentResolver(),
1936 Settings.Secure.TETHER_SUPPORTED, defaultVal) != 0);
1937 return tetherEnabledInSettings && mTetheringConfigValid;
Robert Greenwalt2a091d72010-02-11 18:18:40 -08001938 }
Robert Greenwalt14f2ef42010-06-15 12:19:37 -07001939
1940 // An API NetworkStateTrackers can call when they lose their network.
1941 // This will automatically be cleared after X seconds or a network becomes CONNECTED,
1942 // whichever happens first. The timer is started by the first caller and not
1943 // restarted by subsequent callers.
1944 public void requestNetworkTransitionWakelock(String forWhom) {
1945 enforceConnectivityInternalPermission();
1946 synchronized (this) {
1947 if (mNetTransitionWakeLock.isHeld()) return;
1948 mNetTransitionWakeLockSerialNumber++;
1949 mNetTransitionWakeLock.acquire();
1950 mNetTransitionWakeLockCausedBy = forWhom;
1951 }
1952 mHandler.sendMessageDelayed(mHandler.obtainMessage(
Robert Greenwaltf3331232010-09-24 14:32:21 -07001953 EVENT_CLEAR_NET_TRANSITION_WAKELOCK,
Robert Greenwalt14f2ef42010-06-15 12:19:37 -07001954 mNetTransitionWakeLockSerialNumber, 0),
1955 mNetTransitionWakeLockTimeout);
1956 return;
1957 }
Robert Greenwaltca4306c2010-09-09 13:15:32 -07001958
Robert Greenwaltd7085fc2010-09-08 15:24:47 -07001959 // 100 percent is full good, 0 is full bad.
1960 public void reportInetCondition(int networkType, int percentage) {
Wink Savilleed9c02b2010-12-03 12:01:38 -08001961 if (DBG) log("reportNetworkCondition(" + networkType + ", " + percentage + ")");
Robert Greenwaltd7085fc2010-09-08 15:24:47 -07001962 mContext.enforceCallingOrSelfPermission(
1963 android.Manifest.permission.STATUS_BAR,
1964 "ConnectivityService");
1965
Robert Greenwalt4e8dfef2010-09-20 14:35:25 -07001966 if (DBG) {
1967 int pid = getCallingPid();
1968 int uid = getCallingUid();
1969 String s = pid + "(" + uid + ") reports inet is " +
1970 (percentage > 50 ? "connected" : "disconnected") + " (" + percentage + ") on " +
1971 "network Type " + networkType + " at " + GregorianCalendar.getInstance().getTime();
1972 mInetLog.add(s);
1973 while(mInetLog.size() > INET_CONDITION_LOG_MAX_SIZE) {
1974 mInetLog.remove(0);
1975 }
1976 }
Robert Greenwaltd7085fc2010-09-08 15:24:47 -07001977 mHandler.sendMessage(mHandler.obtainMessage(
Robert Greenwalt8dcc28b2010-09-23 10:05:56 -07001978 EVENT_INET_CONDITION_CHANGE, networkType, percentage));
1979 }
1980
1981 private void handleInetConditionChange(int netType, int condition) {
1982 if (DBG) {
Wink Savilleed9c02b2010-12-03 12:01:38 -08001983 log("Inet connectivity change, net=" +
Robert Greenwalt8dcc28b2010-09-23 10:05:56 -07001984 netType + ", condition=" + condition +
1985 ",mActiveDefaultNetwork=" + mActiveDefaultNetwork);
1986 }
1987 if (mActiveDefaultNetwork == -1) {
Wink Savilleed9c02b2010-12-03 12:01:38 -08001988 if (DBG) log("no active default network - aborting");
Robert Greenwalt8dcc28b2010-09-23 10:05:56 -07001989 return;
1990 }
1991 if (mActiveDefaultNetwork != netType) {
Wink Savilleed9c02b2010-12-03 12:01:38 -08001992 if (DBG) log("given net not default - aborting");
Robert Greenwalt8dcc28b2010-09-23 10:05:56 -07001993 return;
1994 }
1995 mDefaultInetCondition = condition;
1996 int delay;
1997 if (mInetConditionChangeInFlight == false) {
Wink Savilleed9c02b2010-12-03 12:01:38 -08001998 if (DBG) log("starting a change hold");
Robert Greenwalt8dcc28b2010-09-23 10:05:56 -07001999 // setup a new hold to debounce this
2000 if (mDefaultInetCondition > 50) {
2001 delay = Settings.Secure.getInt(mContext.getContentResolver(),
2002 Settings.Secure.INET_CONDITION_DEBOUNCE_UP_DELAY, 500);
2003 } else {
2004 delay = Settings.Secure.getInt(mContext.getContentResolver(),
2005 Settings.Secure.INET_CONDITION_DEBOUNCE_DOWN_DELAY, 3000);
2006 }
2007 mInetConditionChangeInFlight = true;
2008 mHandler.sendMessageDelayed(mHandler.obtainMessage(EVENT_INET_CONDITION_HOLD_END,
2009 mActiveDefaultNetwork, mDefaultConnectionSequence), delay);
2010 } else {
2011 // we've set the new condition, when this hold ends that will get
2012 // picked up
Wink Savilleed9c02b2010-12-03 12:01:38 -08002013 if (DBG) log("currently in hold - not setting new end evt");
Robert Greenwalt8dcc28b2010-09-23 10:05:56 -07002014 }
2015 }
2016
2017 private void handleInetConditionHoldEnd(int netType, int sequence) {
2018 if (DBG) {
Wink Savilleed9c02b2010-12-03 12:01:38 -08002019 log("Inet hold end, net=" + netType +
Robert Greenwalt8dcc28b2010-09-23 10:05:56 -07002020 ", condition =" + mDefaultInetCondition +
2021 ", published condition =" + mDefaultInetConditionPublished);
2022 }
2023 mInetConditionChangeInFlight = false;
2024
2025 if (mActiveDefaultNetwork == -1) {
Wink Savilleed9c02b2010-12-03 12:01:38 -08002026 if (DBG) log("no active default network - aborting");
Robert Greenwalt8dcc28b2010-09-23 10:05:56 -07002027 return;
2028 }
2029 if (mDefaultConnectionSequence != sequence) {
Wink Savilleed9c02b2010-12-03 12:01:38 -08002030 if (DBG) log("event hold for obsolete network - aborting");
Robert Greenwalt8dcc28b2010-09-23 10:05:56 -07002031 return;
2032 }
2033 if (mDefaultInetConditionPublished == mDefaultInetCondition) {
Wink Savilleed9c02b2010-12-03 12:01:38 -08002034 if (DBG) log("no change in condition - aborting");
Robert Greenwalt8dcc28b2010-09-23 10:05:56 -07002035 return;
2036 }
2037 NetworkInfo networkInfo = mNetTrackers[mActiveDefaultNetwork].getNetworkInfo();
2038 if (networkInfo.isConnected() == false) {
Wink Savilleed9c02b2010-12-03 12:01:38 -08002039 if (DBG) log("default network not connected - aborting");
Robert Greenwalt8dcc28b2010-09-23 10:05:56 -07002040 return;
2041 }
2042 mDefaultInetConditionPublished = mDefaultInetCondition;
2043 sendInetConditionBroadcast(networkInfo);
2044 return;
Robert Greenwaltd7085fc2010-09-08 15:24:47 -07002045 }
Robert Greenwalt434203a2010-10-11 16:00:27 -07002046
2047 public synchronized ProxyProperties getProxy() {
2048 if (mGlobalProxy != null) return mGlobalProxy;
2049 if (mDefaultProxy != null) return mDefaultProxy;
2050 return null;
2051 }
2052
2053 public void setGlobalProxy(ProxyProperties proxyProperties) {
2054 enforceChangePermission();
2055 synchronized (mGlobalProxyLock) {
2056 if (proxyProperties == mGlobalProxy) return;
2057 if (proxyProperties != null && proxyProperties.equals(mGlobalProxy)) return;
2058 if (mGlobalProxy != null && mGlobalProxy.equals(proxyProperties)) return;
2059
2060 String host = "";
2061 int port = 0;
2062 String exclList = "";
2063 if (proxyProperties != null && !TextUtils.isEmpty(proxyProperties.getHost())) {
2064 mGlobalProxy = new ProxyProperties(proxyProperties);
2065 host = mGlobalProxy.getHost();
2066 port = mGlobalProxy.getPort();
2067 exclList = mGlobalProxy.getExclusionList();
2068 } else {
2069 mGlobalProxy = null;
2070 }
2071 ContentResolver res = mContext.getContentResolver();
2072 Settings.Secure.putString(res, Settings.Secure.GLOBAL_HTTP_PROXY_HOST, host);
2073 Settings.Secure.putInt(res, Settings.Secure.GLOBAL_HTTP_PROXY_PORT, port);
Robert Greenwaltb7090d62010-12-02 11:31:00 -08002074 Settings.Secure.putString(res, Settings.Secure.GLOBAL_HTTP_PROXY_EXCLUSION_LIST,
Robert Greenwalt434203a2010-10-11 16:00:27 -07002075 exclList);
2076 }
2077
2078 if (mGlobalProxy == null) {
2079 proxyProperties = mDefaultProxy;
2080 }
2081 sendProxyBroadcast(proxyProperties);
2082 }
2083
Robert Greenwaltb7090d62010-12-02 11:31:00 -08002084 private void loadGlobalProxy() {
2085 ContentResolver res = mContext.getContentResolver();
2086 String host = Settings.Secure.getString(res, Settings.Secure.GLOBAL_HTTP_PROXY_HOST);
2087 int port = Settings.Secure.getInt(res, Settings.Secure.GLOBAL_HTTP_PROXY_PORT, 0);
2088 String exclList = Settings.Secure.getString(res,
2089 Settings.Secure.GLOBAL_HTTP_PROXY_EXCLUSION_LIST);
2090 if (!TextUtils.isEmpty(host)) {
2091 ProxyProperties proxyProperties = new ProxyProperties(host, port, exclList);
2092 synchronized (mGlobalProxyLock) {
2093 mGlobalProxy = proxyProperties;
2094 }
2095 }
2096 }
2097
Robert Greenwalt434203a2010-10-11 16:00:27 -07002098 public ProxyProperties getGlobalProxy() {
2099 synchronized (mGlobalProxyLock) {
2100 return mGlobalProxy;
2101 }
2102 }
2103
2104 private void handleApplyDefaultProxy(int type) {
2105 // check if new default - push it out to all VM if so
2106 ProxyProperties proxy = mNetTrackers[type].getLinkProperties().getHttpProxy();
2107 synchronized (this) {
2108 if (mDefaultProxy != null && mDefaultProxy.equals(proxy)) return;
2109 if (mDefaultProxy == proxy) return;
2110 if (!TextUtils.isEmpty(proxy.getHost())) {
2111 mDefaultProxy = proxy;
2112 } else {
2113 mDefaultProxy = null;
2114 }
2115 }
Wink Savilleed9c02b2010-12-03 12:01:38 -08002116 if (DBG) log("changing default proxy to " + proxy);
Robert Greenwalt434203a2010-10-11 16:00:27 -07002117 if ((proxy == null && mGlobalProxy == null) || proxy.equals(mGlobalProxy)) return;
2118 if (mGlobalProxy != null) return;
2119 sendProxyBroadcast(proxy);
2120 }
2121
2122 private void handleDeprecatedGlobalHttpProxy() {
2123 String proxy = Settings.Secure.getString(mContext.getContentResolver(),
2124 Settings.Secure.HTTP_PROXY);
2125 if (!TextUtils.isEmpty(proxy)) {
2126 String data[] = proxy.split(":");
2127 String proxyHost = data[0];
2128 int proxyPort = 8080;
2129 if (data.length > 1) {
2130 try {
2131 proxyPort = Integer.parseInt(data[1]);
2132 } catch (NumberFormatException e) {
2133 return;
2134 }
2135 }
2136 ProxyProperties p = new ProxyProperties(data[0], proxyPort, "");
2137 setGlobalProxy(p);
2138 }
2139 }
2140
2141 private void sendProxyBroadcast(ProxyProperties proxy) {
Robert Greenwalt55985be2010-12-23 15:51:10 -08002142 if (proxy == null) proxy = new ProxyProperties("", 0, "");
Wink Savilleed9c02b2010-12-03 12:01:38 -08002143 log("sending Proxy Broadcast for " + proxy);
Robert Greenwalt434203a2010-10-11 16:00:27 -07002144 Intent intent = new Intent(Proxy.PROXY_CHANGE_ACTION);
Stan Chesnuttb35d67a2011-01-06 11:00:19 -08002145 intent.addFlags(Intent.FLAG_RECEIVER_REPLACE_PENDING |
2146 Intent.FLAG_RECEIVER_REGISTERED_ONLY_BEFORE_BOOT);
Robert Greenwalt434203a2010-10-11 16:00:27 -07002147 intent.putExtra(Proxy.EXTRA_PROXY_INFO, proxy);
Robert Greenwalta2e13392010-12-06 11:29:17 -08002148 mContext.sendStickyBroadcast(intent);
Robert Greenwalt434203a2010-10-11 16:00:27 -07002149 }
2150
2151 private static class SettingsObserver extends ContentObserver {
2152 private int mWhat;
2153 private Handler mHandler;
2154 SettingsObserver(Handler handler, int what) {
2155 super(handler);
2156 mHandler = handler;
2157 mWhat = what;
2158 }
2159
2160 void observe(Context context) {
2161 ContentResolver resolver = context.getContentResolver();
2162 resolver.registerContentObserver(Settings.Secure.getUriFor(
2163 Settings.Secure.HTTP_PROXY), false, this);
2164 }
2165
2166 @Override
2167 public void onChange(boolean selfChange) {
2168 mHandler.obtainMessage(mWhat).sendToTarget();
2169 }
2170 }
Wink Savilleed9c02b2010-12-03 12:01:38 -08002171
2172 private void log(String s) {
2173 Slog.d(TAG, s);
2174 }
2175
2176 private void loge(String s) {
2177 Slog.e(TAG, s);
2178 }
Wink Saville2b8bcfe2011-02-24 17:58:51 -08002179 int convertFeatureToNetworkType(String feature){
2180 int networkType = -1;
2181 if (TextUtils.equals(feature, Phone.FEATURE_ENABLE_MMS)) {
2182 networkType = ConnectivityManager.TYPE_MOBILE_MMS;
2183 } else if (TextUtils.equals(feature, Phone.FEATURE_ENABLE_SUPL)) {
2184 networkType = ConnectivityManager.TYPE_MOBILE_SUPL;
2185 } else if (TextUtils.equals(feature, Phone.FEATURE_ENABLE_DUN) ||
2186 TextUtils.equals(feature, Phone.FEATURE_ENABLE_DUN_ALWAYS)) {
2187 networkType = ConnectivityManager.TYPE_MOBILE_DUN;
2188 } else if (TextUtils.equals(feature, Phone.FEATURE_ENABLE_HIPRI)) {
2189 networkType = ConnectivityManager.TYPE_MOBILE_HIPRI;
2190 } else if (TextUtils.equals(feature, Phone.FEATURE_ENABLE_FOTA)) {
2191 networkType = ConnectivityManager.TYPE_MOBILE_FOTA;
2192 } else if (TextUtils.equals(feature, Phone.FEATURE_ENABLE_IMS)) {
2193 networkType = ConnectivityManager.TYPE_MOBILE_IMS;
2194 } else if (TextUtils.equals(feature, Phone.FEATURE_ENABLE_CBS)) {
2195 networkType = ConnectivityManager.TYPE_MOBILE_CBS;
2196 }
2197 return networkType;
2198 }
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002199}