blob: 12ac052617bf8ecbbf762e13a08bfb413fa4000d [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();
Michael Jurka8fad7ee2011-03-30 19:54:08 -0700546 for (int type=0; type <= ConnectivityManager.MAX_NETWORK_TYPE; type++) {
547 if (mNetAttributes[type] == null || !mNetAttributes[type].isDefault()) {
548 continue;
549 }
550 NetworkStateTracker t = mNetTrackers[type];
551 NetworkInfo info = t.getNetworkInfo();
552 if (info.isConnected()) {
553 if (DBG && type != mActiveDefaultNetwork) {
554 loge("connected default network is not mActiveDefaultNetwork!");
555 }
556 return info;
557 }
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800558 }
559 return null;
560 }
561
562 public NetworkInfo getNetworkInfo(int networkType) {
563 enforceAccessPermission();
564 if (ConnectivityManager.isNetworkTypeValid(networkType)) {
565 NetworkStateTracker t = mNetTrackers[networkType];
566 if (t != null)
567 return t.getNetworkInfo();
568 }
569 return null;
570 }
571
572 public NetworkInfo[] getAllNetworkInfo() {
573 enforceAccessPermission();
Robert Greenwalt5154ae762009-10-30 14:17:42 -0700574 NetworkInfo[] result = new NetworkInfo[mNetworksDefined];
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800575 int i = 0;
576 for (NetworkStateTracker t : mNetTrackers) {
Robert Greenwalt5154ae762009-10-30 14:17:42 -0700577 if(t != null) result[i++] = t.getNetworkInfo();
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800578 }
579 return result;
580 }
581
Robert Greenwaltd192dad2010-09-14 09:18:02 -0700582 /**
583 * Return LinkProperties for the active (i.e., connected) default
584 * network interface. It is assumed that at most one default network
585 * is active at a time. If more than one is active, it is indeterminate
586 * which will be returned.
587 * @return the ip properties for the active network, or {@code null} if
588 * none is active
589 */
590 public LinkProperties getActiveLinkProperties() {
591 enforceAccessPermission();
592 for (int type=0; type <= ConnectivityManager.MAX_NETWORK_TYPE; type++) {
593 if (mNetAttributes[type] == null || !mNetAttributes[type].isDefault()) {
594 continue;
595 }
596 NetworkStateTracker t = mNetTrackers[type];
597 NetworkInfo info = t.getNetworkInfo();
598 if (info.isConnected()) {
599 return t.getLinkProperties();
600 }
601 }
602 return null;
603 }
604
605 public LinkProperties getLinkProperties(int networkType) {
606 enforceAccessPermission();
607 if (ConnectivityManager.isNetworkTypeValid(networkType)) {
608 NetworkStateTracker t = mNetTrackers[networkType];
609 if (t != null) return t.getLinkProperties();
610 }
611 return null;
612 }
613
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800614 public boolean setRadios(boolean turnOn) {
615 boolean result = true;
616 enforceChangePermission();
617 for (NetworkStateTracker t : mNetTrackers) {
Robert Greenwalt5154ae762009-10-30 14:17:42 -0700618 if (t != null) result = t.setRadio(turnOn) && result;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800619 }
620 return result;
621 }
622
623 public boolean setRadio(int netType, boolean turnOn) {
624 enforceChangePermission();
625 if (!ConnectivityManager.isNetworkTypeValid(netType)) {
626 return false;
627 }
628 NetworkStateTracker tracker = mNetTrackers[netType];
629 return tracker != null && tracker.setRadio(turnOn);
630 }
631
Robert Greenwalt9c75d4a2009-09-27 17:27:04 -0700632 /**
633 * Used to notice when the calling process dies so we can self-expire
634 *
635 * Also used to know if the process has cleaned up after itself when
636 * our auto-expire timer goes off. The timer has a link to an object.
637 *
638 */
Robert Greenwalt42acef32009-08-12 16:08:25 -0700639 private class FeatureUser implements IBinder.DeathRecipient {
640 int mNetworkType;
641 String mFeature;
642 IBinder mBinder;
643 int mPid;
644 int mUid;
Robert Greenwaltb9285352009-12-21 18:24:07 -0800645 long mCreateTime;
Robert Greenwalt42acef32009-08-12 16:08:25 -0700646
647 FeatureUser(int type, String feature, IBinder binder) {
648 super();
649 mNetworkType = type;
650 mFeature = feature;
651 mBinder = binder;
652 mPid = getCallingPid();
653 mUid = getCallingUid();
Robert Greenwaltb9285352009-12-21 18:24:07 -0800654 mCreateTime = System.currentTimeMillis();
Robert Greenwalt9c75d4a2009-09-27 17:27:04 -0700655
Robert Greenwalt42acef32009-08-12 16:08:25 -0700656 try {
657 mBinder.linkToDeath(this, 0);
658 } catch (RemoteException e) {
659 binderDied();
660 }
661 }
662
663 void unlinkDeathRecipient() {
664 mBinder.unlinkToDeath(this, 0);
665 }
666
667 public void binderDied() {
Wink Savilleed9c02b2010-12-03 12:01:38 -0800668 log("ConnectivityService FeatureUser binderDied(" +
Robert Greenwaltb9285352009-12-21 18:24:07 -0800669 mNetworkType + ", " + mFeature + ", " + mBinder + "), created " +
670 (System.currentTimeMillis() - mCreateTime) + " mSec ago");
Robert Greenwalt9c75d4a2009-09-27 17:27:04 -0700671 stopUsingNetworkFeature(this, false);
Robert Greenwalt42acef32009-08-12 16:08:25 -0700672 }
673
Robert Greenwalt9c75d4a2009-09-27 17:27:04 -0700674 public void expire() {
Wink Savilleed9c02b2010-12-03 12:01:38 -0800675 log("ConnectivityService FeatureUser expire(" +
Robert Greenwaltb9285352009-12-21 18:24:07 -0800676 mNetworkType + ", " + mFeature + ", " + mBinder +"), created " +
677 (System.currentTimeMillis() - mCreateTime) + " mSec ago");
Robert Greenwalt9c75d4a2009-09-27 17:27:04 -0700678 stopUsingNetworkFeature(this, false);
679 }
Robert Greenwaltb9285352009-12-21 18:24:07 -0800680
681 public String toString() {
682 return "FeatureUser("+mNetworkType+","+mFeature+","+mPid+","+mUid+"), created " +
683 (System.currentTimeMillis() - mCreateTime) + " mSec ago";
684 }
Robert Greenwalt42acef32009-08-12 16:08:25 -0700685 }
686
Robert Greenwalt9c75d4a2009-09-27 17:27:04 -0700687 // javadoc from interface
Robert Greenwalt42acef32009-08-12 16:08:25 -0700688 public int startUsingNetworkFeature(int networkType, String feature,
689 IBinder binder) {
690 if (DBG) {
Wink Savilleed9c02b2010-12-03 12:01:38 -0800691 log("startUsingNetworkFeature for net " + networkType + ": " + feature);
Robert Greenwalt42acef32009-08-12 16:08:25 -0700692 }
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800693 enforceChangePermission();
Robert Greenwalt5154ae762009-10-30 14:17:42 -0700694 if (!ConnectivityManager.isNetworkTypeValid(networkType) ||
695 mNetAttributes[networkType] == null) {
Robert Greenwalt42acef32009-08-12 16:08:25 -0700696 return Phone.APN_REQUEST_FAILED;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800697 }
Robert Greenwalt42acef32009-08-12 16:08:25 -0700698
Robert Greenwalt9c75d4a2009-09-27 17:27:04 -0700699 FeatureUser f = new FeatureUser(networkType, feature, binder);
Robert Greenwalt42acef32009-08-12 16:08:25 -0700700
701 // TODO - move this into the MobileDataStateTracker
702 int usedNetworkType = networkType;
703 if(networkType == ConnectivityManager.TYPE_MOBILE) {
Wink Saville2b8bcfe2011-02-24 17:58:51 -0800704 usedNetworkType = convertFeatureToNetworkType(feature);
705 if (usedNetworkType < 0) {
706 Slog.e(TAG, "Can't match any netTracker!");
707 usedNetworkType = networkType;
Robert Greenwalt42acef32009-08-12 16:08:25 -0700708 }
709 }
710 NetworkStateTracker network = mNetTrackers[usedNetworkType];
711 if (network != null) {
Robert Greenwalt0be1e982010-12-15 13:26:33 -0800712 Integer currentPid = new Integer(getCallingPid());
Robert Greenwalt42acef32009-08-12 16:08:25 -0700713 if (usedNetworkType != networkType) {
Robert Greenwalt42acef32009-08-12 16:08:25 -0700714 NetworkStateTracker radio = mNetTrackers[networkType];
715 NetworkInfo ni = network.getNetworkInfo();
716
717 if (ni.isAvailable() == false) {
Wink Savilleed9c02b2010-12-03 12:01:38 -0800718 if (DBG) log("special network not available");
Robert Greenwalte32e8122010-12-29 14:35:21 -0800719 if (!TextUtils.equals(feature,Phone.FEATURE_ENABLE_DUN_ALWAYS)) {
720 return Phone.APN_TYPE_NOT_AVAILABLE;
721 } else {
722 // else make the attempt anyway - probably giving REQUEST_STARTED below
723 }
Robert Greenwalt42acef32009-08-12 16:08:25 -0700724 }
725
Robert Greenwalt9c75d4a2009-09-27 17:27:04 -0700726 synchronized(this) {
727 mFeatureUsers.add(f);
728 if (!mNetRequestersPids[usedNetworkType].contains(currentPid)) {
729 // this gets used for per-pid dns when connected
730 mNetRequestersPids[usedNetworkType].add(currentPid);
731 }
Robert Greenwalt42acef32009-08-12 16:08:25 -0700732 }
Robert Greenwalt8dcc28b2010-09-23 10:05:56 -0700733 mHandler.sendMessageDelayed(mHandler.obtainMessage(EVENT_RESTORE_DEFAULT_NETWORK,
Robert Greenwalt9c75d4a2009-09-27 17:27:04 -0700734 f), getRestoreDefaultNetworkDelay());
735
Robert Greenwalt42acef32009-08-12 16:08:25 -0700736
Robert Greenwalta64bf832009-08-19 20:19:33 -0700737 if ((ni.isConnectedOrConnecting() == true) &&
738 !network.isTeardownRequested()) {
Robert Greenwalt42acef32009-08-12 16:08:25 -0700739 if (ni.isConnected() == true) {
740 // add the pid-specific dns
Robert Greenwaltc76b8fa2010-07-23 15:46:26 -0700741 handleDnsConfigurationChange(networkType);
Wink Savilleed9c02b2010-12-03 12:01:38 -0800742 if (DBG) log("special network already active");
Robert Greenwalt42acef32009-08-12 16:08:25 -0700743 return Phone.APN_ALREADY_ACTIVE;
744 }
Wink Savilleed9c02b2010-12-03 12:01:38 -0800745 if (DBG) log("special network already connecting");
Robert Greenwalt42acef32009-08-12 16:08:25 -0700746 return Phone.APN_REQUEST_STARTED;
747 }
748
749 // check if the radio in play can make another contact
750 // assume if cannot for now
751
Wink Savilleed9c02b2010-12-03 12:01:38 -0800752 if (DBG) log("reconnecting to special network");
Robert Greenwalt42acef32009-08-12 16:08:25 -0700753 network.reconnect();
754 return Phone.APN_REQUEST_STARTED;
755 } else {
Robert Greenwalt0be1e982010-12-15 13:26:33 -0800756 // need to remember this unsupported request so we respond appropriately on stop
757 synchronized(this) {
758 mFeatureUsers.add(f);
759 if (!mNetRequestersPids[usedNetworkType].contains(currentPid)) {
760 // this gets used for per-pid dns when connected
761 mNetRequestersPids[usedNetworkType].add(currentPid);
762 }
763 }
Robert Greenwalt02648a42010-05-18 10:52:51 -0700764 return -1;
Robert Greenwalt42acef32009-08-12 16:08:25 -0700765 }
766 }
767 return Phone.APN_TYPE_NOT_AVAILABLE;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800768 }
769
Robert Greenwalt9c75d4a2009-09-27 17:27:04 -0700770 // javadoc from interface
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800771 public int stopUsingNetworkFeature(int networkType, String feature) {
Robert Greenwaltb8f16342009-10-06 17:52:40 -0700772 enforceChangePermission();
773
Robert Greenwalt9c75d4a2009-09-27 17:27:04 -0700774 int pid = getCallingPid();
775 int uid = getCallingUid();
776
777 FeatureUser u = null;
778 boolean found = false;
779
780 synchronized(this) {
781 for (int i = 0; i < mFeatureUsers.size() ; i++) {
782 u = (FeatureUser)mFeatureUsers.get(i);
783 if (uid == u.mUid && pid == u.mPid &&
784 networkType == u.mNetworkType &&
785 TextUtils.equals(feature, u.mFeature)) {
786 found = true;
787 break;
788 }
789 }
790 }
791 if (found && u != null) {
792 // stop regardless of how many other time this proc had called start
793 return stopUsingNetworkFeature(u, true);
794 } else {
795 // none found!
Wink Savilleed9c02b2010-12-03 12:01:38 -0800796 if (DBG) log("ignoring stopUsingNetworkFeature - not a live request");
Robert Greenwalt9c75d4a2009-09-27 17:27:04 -0700797 return 1;
798 }
Robert Greenwalt42acef32009-08-12 16:08:25 -0700799 }
800
Robert Greenwalt9c75d4a2009-09-27 17:27:04 -0700801 private int stopUsingNetworkFeature(FeatureUser u, boolean ignoreDups) {
802 int networkType = u.mNetworkType;
803 String feature = u.mFeature;
804 int pid = u.mPid;
805 int uid = u.mUid;
806
807 NetworkStateTracker tracker = null;
808 boolean callTeardown = false; // used to carry our decision outside of sync block
809
Robert Greenwalt42acef32009-08-12 16:08:25 -0700810 if (DBG) {
Wink Savilleed9c02b2010-12-03 12:01:38 -0800811 log("stopUsingNetworkFeature for net " + networkType +
Robert Greenwalt42acef32009-08-12 16:08:25 -0700812 ": " + feature);
813 }
Robert Greenwaltb8f16342009-10-06 17:52:40 -0700814
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800815 if (!ConnectivityManager.isNetworkTypeValid(networkType)) {
816 return -1;
817 }
Robert Greenwalt42acef32009-08-12 16:08:25 -0700818
Robert Greenwalt9c75d4a2009-09-27 17:27:04 -0700819 // need to link the mFeatureUsers list with the mNetRequestersPids state in this
820 // sync block
821 synchronized(this) {
822 // check if this process still has an outstanding start request
823 if (!mFeatureUsers.contains(u)) {
Wink Savilleed9c02b2010-12-03 12:01:38 -0800824 if (DBG) log("ignoring - this process has no outstanding requests");
Robert Greenwalt42acef32009-08-12 16:08:25 -0700825 return 1;
826 }
Robert Greenwalt9c75d4a2009-09-27 17:27:04 -0700827 u.unlinkDeathRecipient();
828 mFeatureUsers.remove(mFeatureUsers.indexOf(u));
829 // If we care about duplicate requests, check for that here.
830 //
831 // This is done to support the extension of a request - the app
832 // can request we start the network feature again and renew the
833 // auto-shutoff delay. Normal "stop" calls from the app though
834 // do not pay attention to duplicate requests - in effect the
835 // API does not refcount and a single stop will counter multiple starts.
836 if (ignoreDups == false) {
837 for (int i = 0; i < mFeatureUsers.size() ; i++) {
838 FeatureUser x = (FeatureUser)mFeatureUsers.get(i);
839 if (x.mUid == u.mUid && x.mPid == u.mPid &&
840 x.mNetworkType == u.mNetworkType &&
841 TextUtils.equals(x.mFeature, u.mFeature)) {
Wink Savilleed9c02b2010-12-03 12:01:38 -0800842 if (DBG) log("ignoring stopUsingNetworkFeature as dup is found");
Robert Greenwalt9c75d4a2009-09-27 17:27:04 -0700843 return 1;
844 }
845 }
Robert Greenwalt42acef32009-08-12 16:08:25 -0700846 }
Robert Greenwalt9c75d4a2009-09-27 17:27:04 -0700847
848 // TODO - move to MobileDataStateTracker
849 int usedNetworkType = networkType;
850 if (networkType == ConnectivityManager.TYPE_MOBILE) {
Wink Saville2b8bcfe2011-02-24 17:58:51 -0800851 usedNetworkType = convertFeatureToNetworkType(feature);
852 if (usedNetworkType < 0) {
853 usedNetworkType = networkType;
Robert Greenwalt9c75d4a2009-09-27 17:27:04 -0700854 }
855 }
856 tracker = mNetTrackers[usedNetworkType];
Robert Greenwalt5154ae762009-10-30 14:17:42 -0700857 if (tracker == null) {
Wink Savilleed9c02b2010-12-03 12:01:38 -0800858 if (DBG) log("ignoring - no known tracker for net type " + usedNetworkType);
Robert Greenwalt5154ae762009-10-30 14:17:42 -0700859 return -1;
860 }
861 if (usedNetworkType != networkType) {
Robert Greenwalt9c75d4a2009-09-27 17:27:04 -0700862 Integer currentPid = new Integer(pid);
Robert Greenwalt9c75d4a2009-09-27 17:27:04 -0700863 mNetRequestersPids[usedNetworkType].remove(currentPid);
Robert Greenwalt421c72b2009-12-17 14:54:59 -0800864 reassessPidDns(pid, true);
Robert Greenwalt9c75d4a2009-09-27 17:27:04 -0700865 if (mNetRequestersPids[usedNetworkType].size() != 0) {
Wink Savilleed9c02b2010-12-03 12:01:38 -0800866 if (DBG) log("not tearing down special network - " +
Robert Greenwalt9c75d4a2009-09-27 17:27:04 -0700867 "others still using it");
868 return 1;
869 }
870 callTeardown = true;
Robert Greenwalt19b9ab42011-01-10 11:58:31 -0800871 } else {
872 if (DBG) log("not a known feature - dropping");
Robert Greenwalt9c75d4a2009-09-27 17:27:04 -0700873 }
874 }
Wink Savilleed9c02b2010-12-03 12:01:38 -0800875 if (DBG) log("Doing network teardown");
Robert Greenwalt9c75d4a2009-09-27 17:27:04 -0700876 if (callTeardown) {
877 tracker.teardown();
Robert Greenwalt42acef32009-08-12 16:08:25 -0700878 return 1;
879 } else {
Robert Greenwalt02648a42010-05-18 10:52:51 -0700880 return -1;
Robert Greenwalt42acef32009-08-12 16:08:25 -0700881 }
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800882 }
883
884 /**
Robert Greenwalt585ac0f2010-08-27 09:24:29 -0700885 * @deprecated use requestRouteToHostAddress instead
886 *
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800887 * Ensure that a network route exists to deliver traffic to the specified
888 * host via the specified network interface.
Robert Greenwalt86e9e552009-07-16 17:21:39 -0700889 * @param networkType the type of the network over which traffic to the
890 * specified host is to be routed
891 * @param hostAddress the IP address of the host to which the route is
892 * desired
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800893 * @return {@code true} on success, {@code false} on failure
894 */
895 public boolean requestRouteToHost(int networkType, int hostAddress) {
Robert Greenwalt585ac0f2010-08-27 09:24:29 -0700896 InetAddress inetAddress = NetworkUtils.intToInetAddress(hostAddress);
897
898 if (inetAddress == null) {
899 return false;
900 }
901
902 return requestRouteToHostAddress(networkType, inetAddress.getAddress());
903 }
904
905 /**
906 * Ensure that a network route exists to deliver traffic to the specified
907 * host via the specified network interface.
908 * @param networkType the type of the network over which traffic to the
909 * specified host is to be routed
910 * @param hostAddress the IP address of the host to which the route is
911 * desired
912 * @return {@code true} on success, {@code false} on failure
913 */
914 public boolean requestRouteToHostAddress(int networkType, byte[] hostAddress) {
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800915 enforceChangePermission();
916 if (!ConnectivityManager.isNetworkTypeValid(networkType)) {
917 return false;
918 }
919 NetworkStateTracker tracker = mNetTrackers[networkType];
Robert Greenwalt8206ff32009-09-10 15:06:20 -0700920
Robert Greenwalt5154ae762009-10-30 14:17:42 -0700921 if (tracker == null || !tracker.getNetworkInfo().isConnected() ||
922 tracker.isTeardownRequested()) {
Robert Greenwalt8206ff32009-09-10 15:06:20 -0700923 if (DBG) {
Wink Savilleed9c02b2010-12-03 12:01:38 -0800924 log("requestRouteToHostAddress on down network " +
Robert Greenwalt585ac0f2010-08-27 09:24:29 -0700925 "(" + networkType + ") - dropped");
Robert Greenwalt8206ff32009-09-10 15:06:20 -0700926 }
927 return false;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800928 }
Robert Greenwalt47f69fe2010-06-15 15:43:39 -0700929 try {
Robert Greenwalt585ac0f2010-08-27 09:24:29 -0700930 InetAddress addr = InetAddress.getByAddress(hostAddress);
Robert Greenwalt47f69fe2010-06-15 15:43:39 -0700931 return addHostRoute(tracker, addr);
932 } catch (UnknownHostException e) {}
933 return false;
Irfan Sheriffd649c122010-06-09 15:39:36 -0700934 }
935
936 /**
937 * Ensure that a network route exists to deliver traffic to the specified
938 * host via the mobile data network.
939 * @param hostAddress the IP address of the host to which the route is desired,
940 * in network byte order.
Robert Greenwalt47f69fe2010-06-15 15:43:39 -0700941 * TODO - deprecate
Irfan Sheriffd649c122010-06-09 15:39:36 -0700942 * @return {@code true} on success, {@code false} on failure
943 */
Robert Greenwalt47f69fe2010-06-15 15:43:39 -0700944 private boolean addHostRoute(NetworkStateTracker nt, InetAddress hostAddress) {
Irfan Sheriffd649c122010-06-09 15:39:36 -0700945 if (nt.getNetworkInfo().getType() == ConnectivityManager.TYPE_WIFI) {
946 return false;
947 }
948
Robert Greenwalt37e65eb2010-08-30 10:56:47 -0700949 LinkProperties p = nt.getLinkProperties();
Robert Greenwalt47f69fe2010-06-15 15:43:39 -0700950 if (p == null) return false;
951 String interfaceName = p.getInterfaceName();
Irfan Sheriffd649c122010-06-09 15:39:36 -0700952
953 if (DBG) {
Wink Savilleed9c02b2010-12-03 12:01:38 -0800954 log("Requested host route to " + hostAddress + "(" + interfaceName + ")");
Irfan Sheriffd649c122010-06-09 15:39:36 -0700955 }
Robert Greenwalt47f69fe2010-06-15 15:43:39 -0700956 if (interfaceName != null) {
Robert Greenwalt585ac0f2010-08-27 09:24:29 -0700957 return NetworkUtils.addHostRoute(interfaceName, hostAddress, null);
Irfan Sheriffd649c122010-06-09 15:39:36 -0700958 } else {
Wink Savilleed9c02b2010-12-03 12:01:38 -0800959 if (DBG) loge("addHostRoute failed due to null interface name");
Irfan Sheriffd649c122010-06-09 15:39:36 -0700960 return false;
961 }
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800962 }
963
964 /**
965 * @see ConnectivityManager#getBackgroundDataSetting()
966 */
967 public boolean getBackgroundDataSetting() {
Robert Greenwaltd825ea42010-12-29 16:15:02 -0800968 return mBackgroundDataEnabled.get();
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800969 }
Robert Greenwalt86e9e552009-07-16 17:21:39 -0700970
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800971 /**
972 * @see ConnectivityManager#setBackgroundDataSetting(boolean)
973 */
974 public void setBackgroundDataSetting(boolean allowBackgroundDataUsage) {
975 mContext.enforceCallingOrSelfPermission(
976 android.Manifest.permission.CHANGE_BACKGROUND_DATA_SETTING,
977 "ConnectivityService");
Robert Greenwalt86e9e552009-07-16 17:21:39 -0700978
Robert Greenwaltd825ea42010-12-29 16:15:02 -0800979 mBackgroundDataEnabled.set(allowBackgroundDataUsage);
980
Robert Greenwalt8dcc28b2010-09-23 10:05:56 -0700981 mHandler.sendMessage(mHandler.obtainMessage(EVENT_SET_BACKGROUND_DATA,
982 (allowBackgroundDataUsage ? ENABLED : DISABLED), 0));
983 }
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800984
Robert Greenwalt8dcc28b2010-09-23 10:05:56 -0700985 private void handleSetBackgroundData(boolean enabled) {
Robert Greenwaltdb4afae2011-02-25 13:44:09 -0800986 Settings.Secure.putInt(mContext.getContentResolver(),
987 Settings.Secure.BACKGROUND_DATA, enabled ? 1 : 0);
988 Intent broadcast = new Intent(
989 ConnectivityManager.ACTION_BACKGROUND_DATA_SETTING_CHANGED);
990 mContext.sendBroadcast(broadcast);
Robert Greenwalt86e9e552009-07-16 17:21:39 -0700991 }
992
Robert Greenwaltc03fa502010-02-23 18:58:05 -0800993 /**
994 * @see ConnectivityManager#getMobileDataEnabled()
995 */
996 public boolean getMobileDataEnabled() {
Wink Savillee7982682010-12-07 10:31:02 -0800997 // TODO: This detail should probably be in DataConnectionTracker's
998 // which is where we store the value and maybe make this
999 // asynchronous.
Robert Greenwaltc03fa502010-02-23 18:58:05 -08001000 enforceAccessPermission();
1001 boolean retVal = Settings.Secure.getInt(mContext.getContentResolver(),
1002 Settings.Secure.MOBILE_DATA, 1) == 1;
Wink Savilleed9c02b2010-12-03 12:01:38 -08001003 if (DBG) log("getMobileDataEnabled returning " + retVal);
Robert Greenwaltc03fa502010-02-23 18:58:05 -08001004 return retVal;
1005 }
1006
1007 /**
1008 * @see ConnectivityManager#setMobileDataEnabled(boolean)
1009 */
Robert Greenwalt8dcc28b2010-09-23 10:05:56 -07001010 public void setMobileDataEnabled(boolean enabled) {
Robert Greenwaltc03fa502010-02-23 18:58:05 -08001011 enforceChangePermission();
Wink Savilleed9c02b2010-12-03 12:01:38 -08001012 if (DBG) log("setMobileDataEnabled(" + enabled + ")");
Robert Greenwaltc03fa502010-02-23 18:58:05 -08001013
Robert Greenwalt8dcc28b2010-09-23 10:05:56 -07001014 mHandler.sendMessage(mHandler.obtainMessage(EVENT_SET_MOBILE_DATA,
1015 (enabled ? ENABLED : DISABLED), 0));
1016 }
1017
1018 private void handleSetMobileData(boolean enabled) {
Wink Savillee7982682010-12-07 10:31:02 -08001019 if (mNetTrackers[ConnectivityManager.TYPE_MOBILE] != null) {
1020 if (DBG) {
1021 Slog.d(TAG, mNetTrackers[ConnectivityManager.TYPE_MOBILE].toString() + enabled);
Robert Greenwaltc03fa502010-02-23 18:58:05 -08001022 }
Wink Savillee7982682010-12-07 10:31:02 -08001023 mNetTrackers[ConnectivityManager.TYPE_MOBILE].setDataEnable(enabled);
Robert Greenwaltc03fa502010-02-23 18:58:05 -08001024 }
1025 }
1026
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001027 private void enforceAccessPermission() {
Robert Greenwalt86e9e552009-07-16 17:21:39 -07001028 mContext.enforceCallingOrSelfPermission(
1029 android.Manifest.permission.ACCESS_NETWORK_STATE,
1030 "ConnectivityService");
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001031 }
1032
1033 private void enforceChangePermission() {
Robert Greenwalt86e9e552009-07-16 17:21:39 -07001034 mContext.enforceCallingOrSelfPermission(
1035 android.Manifest.permission.CHANGE_NETWORK_STATE,
1036 "ConnectivityService");
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001037 }
1038
Robert Greenwaltd0e18ff2010-01-26 11:40:34 -08001039 // TODO Make this a special check when it goes public
1040 private void enforceTetherChangePermission() {
1041 mContext.enforceCallingOrSelfPermission(
1042 android.Manifest.permission.CHANGE_NETWORK_STATE,
1043 "ConnectivityService");
1044 }
1045
Robert Greenwalt2a091d72010-02-11 18:18:40 -08001046 private void enforceTetherAccessPermission() {
1047 mContext.enforceCallingOrSelfPermission(
1048 android.Manifest.permission.ACCESS_NETWORK_STATE,
1049 "ConnectivityService");
1050 }
1051
Robert Greenwalt14f2ef42010-06-15 12:19:37 -07001052 private void enforceConnectivityInternalPermission() {
1053 mContext.enforceCallingOrSelfPermission(
1054 android.Manifest.permission.CONNECTIVITY_INTERNAL,
1055 "ConnectivityService");
1056 }
1057
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001058 /**
Robert Greenwalt86e9e552009-07-16 17:21:39 -07001059 * Handle a {@code DISCONNECTED} event. If this pertains to the non-active
1060 * network, we ignore it. If it is for the active network, we send out a
1061 * broadcast. But first, we check whether it might be possible to connect
1062 * to a different network.
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001063 * @param info the {@code NetworkInfo} for the network
1064 */
1065 private void handleDisconnect(NetworkInfo info) {
1066
Robert Greenwalt42acef32009-08-12 16:08:25 -07001067 int prevNetType = info.getType();
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001068
Robert Greenwalt42acef32009-08-12 16:08:25 -07001069 mNetTrackers[prevNetType].setTeardownRequested(false);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001070 /*
1071 * If the disconnected network is not the active one, then don't report
1072 * this as a loss of connectivity. What probably happened is that we're
1073 * getting the disconnect for a network that we explicitly disabled
1074 * in accordance with network preference policies.
1075 */
Robert Greenwalt42acef32009-08-12 16:08:25 -07001076 if (!mNetAttributes[prevNetType].isDefault()) {
1077 List pids = mNetRequestersPids[prevNetType];
1078 for (int i = 0; i<pids.size(); i++) {
1079 Integer pid = (Integer)pids.get(i);
1080 // will remove them because the net's no longer connected
1081 // need to do this now as only now do we know the pids and
1082 // can properly null things that are no longer referenced.
1083 reassessPidDns(pid.intValue(), false);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001084 }
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001085 }
1086
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001087 Intent intent = new Intent(ConnectivityManager.CONNECTIVITY_ACTION);
1088 intent.putExtra(ConnectivityManager.EXTRA_NETWORK_INFO, info);
1089 if (info.isFailover()) {
1090 intent.putExtra(ConnectivityManager.EXTRA_IS_FAILOVER, true);
1091 info.setFailover(false);
1092 }
1093 if (info.getReason() != null) {
1094 intent.putExtra(ConnectivityManager.EXTRA_REASON, info.getReason());
1095 }
1096 if (info.getExtraInfo() != null) {
Robert Greenwalt86e9e552009-07-16 17:21:39 -07001097 intent.putExtra(ConnectivityManager.EXTRA_EXTRA_INFO,
1098 info.getExtraInfo());
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001099 }
Robert Greenwalt42acef32009-08-12 16:08:25 -07001100
Robert Greenwaltcc4b4012010-01-25 17:54:29 -08001101 if (mNetAttributes[prevNetType].isDefault()) {
Robert Greenwaltf21ef7d2011-01-11 13:56:33 -08001102 tryFailover(prevNetType);
1103 if (mActiveDefaultNetwork != -1) {
1104 NetworkInfo switchTo = mNetTrackers[mActiveDefaultNetwork].getNetworkInfo();
Robert Greenwaltcc4b4012010-01-25 17:54:29 -08001105 intent.putExtra(ConnectivityManager.EXTRA_OTHER_NETWORK_INFO, switchTo);
1106 } else {
Robert Greenwalt029be812010-09-20 18:01:43 -07001107 mDefaultInetConditionPublished = 0; // we're not connected anymore
Robert Greenwaltcc4b4012010-01-25 17:54:29 -08001108 intent.putExtra(ConnectivityManager.EXTRA_NO_CONNECTIVITY, true);
1109 }
Robert Greenwaltda03c4e2010-01-20 19:29:41 -08001110 }
Robert Greenwalt029be812010-09-20 18:01:43 -07001111 intent.putExtra(ConnectivityManager.EXTRA_INET_CONDITION, mDefaultInetConditionPublished);
Robert Greenwaltda03c4e2010-01-20 19:29:41 -08001112 // do this before we broadcast the change
Robert Greenwaltc76b8fa2010-07-23 15:46:26 -07001113 handleConnectivityChange(prevNetType);
Robert Greenwaltda03c4e2010-01-20 19:29:41 -08001114
1115 sendStickyBroadcast(intent);
1116 /*
1117 * If the failover network is already connected, then immediately send
1118 * out a followup broadcast indicating successful failover
1119 */
Robert Greenwaltf21ef7d2011-01-11 13:56:33 -08001120 if (mActiveDefaultNetwork != -1) {
1121 sendConnectedBroadcast(mNetTrackers[mActiveDefaultNetwork].getNetworkInfo());
Robert Greenwaltda03c4e2010-01-20 19:29:41 -08001122 }
1123 }
1124
Robert Greenwaltf21ef7d2011-01-11 13:56:33 -08001125 private void tryFailover(int prevNetType) {
Robert Greenwalt42acef32009-08-12 16:08:25 -07001126 /*
Robert Greenwaltbff90182011-01-06 15:41:07 -08001127 * If this is a default network, check if other defaults are available.
1128 * Try to reconnect on all available and let them hash it out when
1129 * more than one connects.
Robert Greenwalt42acef32009-08-12 16:08:25 -07001130 */
Robert Greenwalt42acef32009-08-12 16:08:25 -07001131 if (mNetAttributes[prevNetType].isDefault()) {
Robert Greenwalt42acef32009-08-12 16:08:25 -07001132 if (mActiveDefaultNetwork == prevNetType) {
1133 mActiveDefaultNetwork = -1;
1134 }
1135
Robert Greenwaltf21ef7d2011-01-11 13:56:33 -08001136 // don't signal a reconnect for anything lower or equal priority than our
1137 // current connected default
1138 // TODO - don't filter by priority now - nice optimization but risky
1139// int currentPriority = -1;
1140// if (mActiveDefaultNetwork != -1) {
1141// currentPriority = mNetAttributes[mActiveDefaultNetwork].mPriority;
1142// }
Robert Greenwaltda03c4e2010-01-20 19:29:41 -08001143 for (int checkType=0; checkType <= ConnectivityManager.MAX_NETWORK_TYPE; checkType++) {
Robert Greenwalt5154ae762009-10-30 14:17:42 -07001144 if (checkType == prevNetType) continue;
1145 if (mNetAttributes[checkType] == null) continue;
Robert Greenwaltbff90182011-01-06 15:41:07 -08001146 if (!mNetAttributes[checkType].isDefault()) continue;
Wink Saville9f7a0b22011-01-26 15:43:49 -08001147
1148// Enabling the isAvailable() optimization caused mobile to not get
1149// selected if it was in the middle of error handling. Specifically
1150// a moble connection that took 30 seconds to complete the DEACTIVATE_DATA_CALL
1151// would not be available and we wouldn't get connected to anything.
1152// So removing the isAvailable() optimization below for now. TODO: This
1153// optimization should work and we need to investigate why it doesn't work.
1154// This could be related to how DEACTIVATE_DATA_CALL is reporting its
1155// complete before it is really complete.
1156// if (!mNetTrackers[checkType].isAvailable()) continue;
1157
Robert Greenwaltf21ef7d2011-01-11 13:56:33 -08001158// if (currentPriority >= mNetAttributes[checkType].mPriority) continue;
Robert Greenwalt42acef32009-08-12 16:08:25 -07001159
Robert Greenwaltf21ef7d2011-01-11 13:56:33 -08001160 NetworkStateTracker checkTracker = mNetTrackers[checkType];
1161 NetworkInfo checkInfo = checkTracker.getNetworkInfo();
1162 if (!checkInfo.isConnectedOrConnecting() || checkTracker.isTeardownRequested()) {
1163 checkInfo.setFailover(true);
1164 checkTracker.reconnect();
Robert Greenwalt42acef32009-08-12 16:08:25 -07001165 }
Robert Greenwaltf21ef7d2011-01-11 13:56:33 -08001166 if (DBG) log("Attempting to switch to " + checkInfo.getTypeName());
Robert Greenwalt42acef32009-08-12 16:08:25 -07001167 }
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001168 }
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001169 }
1170
1171 private void sendConnectedBroadcast(NetworkInfo info) {
Robert Greenwalt1e9aac22010-09-15 17:36:33 -07001172 sendGeneralBroadcast(info, ConnectivityManager.CONNECTIVITY_ACTION);
1173 }
1174
1175 private void sendInetConditionBroadcast(NetworkInfo info) {
1176 sendGeneralBroadcast(info, ConnectivityManager.INET_CONDITION_ACTION);
1177 }
1178
1179 private void sendGeneralBroadcast(NetworkInfo info, String bcastType) {
1180 Intent intent = new Intent(bcastType);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001181 intent.putExtra(ConnectivityManager.EXTRA_NETWORK_INFO, info);
1182 if (info.isFailover()) {
1183 intent.putExtra(ConnectivityManager.EXTRA_IS_FAILOVER, true);
1184 info.setFailover(false);
1185 }
1186 if (info.getReason() != null) {
1187 intent.putExtra(ConnectivityManager.EXTRA_REASON, info.getReason());
1188 }
1189 if (info.getExtraInfo() != null) {
Robert Greenwalt86e9e552009-07-16 17:21:39 -07001190 intent.putExtra(ConnectivityManager.EXTRA_EXTRA_INFO,
1191 info.getExtraInfo());
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001192 }
Robert Greenwaltd7085fc2010-09-08 15:24:47 -07001193 intent.putExtra(ConnectivityManager.EXTRA_INET_CONDITION, mDefaultInetConditionPublished);
Mike Lockwood0f79b542009-08-14 14:18:49 -04001194 sendStickyBroadcast(intent);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001195 }
1196
1197 /**
1198 * Called when an attempt to fail over to another network has failed.
1199 * @param info the {@link NetworkInfo} for the failed network
1200 */
1201 private void handleConnectionFailure(NetworkInfo info) {
1202 mNetTrackers[info.getType()].setTeardownRequested(false);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001203
Robert Greenwalt42acef32009-08-12 16:08:25 -07001204 String reason = info.getReason();
1205 String extraInfo = info.getExtraInfo();
Robert Greenwalt86e9e552009-07-16 17:21:39 -07001206
Robert Greenwalt572172b2010-10-08 16:35:52 -07001207 String reasonText;
1208 if (reason == null) {
1209 reasonText = ".";
1210 } else {
1211 reasonText = " (" + reason + ").";
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001212 }
Wink Savilleed9c02b2010-12-03 12:01:38 -08001213 loge("Attempt to connect to " + info.getTypeName() + " failed" + reasonText);
Robert Greenwalt42acef32009-08-12 16:08:25 -07001214
1215 Intent intent = new Intent(ConnectivityManager.CONNECTIVITY_ACTION);
1216 intent.putExtra(ConnectivityManager.EXTRA_NETWORK_INFO, info);
1217 if (getActiveNetworkInfo() == null) {
1218 intent.putExtra(ConnectivityManager.EXTRA_NO_CONNECTIVITY, true);
1219 }
1220 if (reason != null) {
1221 intent.putExtra(ConnectivityManager.EXTRA_REASON, reason);
1222 }
1223 if (extraInfo != null) {
1224 intent.putExtra(ConnectivityManager.EXTRA_EXTRA_INFO, extraInfo);
1225 }
1226 if (info.isFailover()) {
1227 intent.putExtra(ConnectivityManager.EXTRA_IS_FAILOVER, true);
1228 info.setFailover(false);
1229 }
Robert Greenwaltda03c4e2010-01-20 19:29:41 -08001230
Robert Greenwaltcc4b4012010-01-25 17:54:29 -08001231 if (mNetAttributes[info.getType()].isDefault()) {
Robert Greenwaltf21ef7d2011-01-11 13:56:33 -08001232 tryFailover(info.getType());
1233 if (mActiveDefaultNetwork != -1) {
1234 NetworkInfo switchTo = mNetTrackers[mActiveDefaultNetwork].getNetworkInfo();
Robert Greenwaltcc4b4012010-01-25 17:54:29 -08001235 intent.putExtra(ConnectivityManager.EXTRA_OTHER_NETWORK_INFO, switchTo);
1236 } else {
Robert Greenwalt029be812010-09-20 18:01:43 -07001237 mDefaultInetConditionPublished = 0;
Robert Greenwaltcc4b4012010-01-25 17:54:29 -08001238 intent.putExtra(ConnectivityManager.EXTRA_NO_CONNECTIVITY, true);
1239 }
Robert Greenwaltda03c4e2010-01-20 19:29:41 -08001240 }
Robert Greenwaltcc4b4012010-01-25 17:54:29 -08001241
Robert Greenwalt029be812010-09-20 18:01:43 -07001242 intent.putExtra(ConnectivityManager.EXTRA_INET_CONDITION, mDefaultInetConditionPublished);
Mike Lockwood0f79b542009-08-14 14:18:49 -04001243 sendStickyBroadcast(intent);
Robert Greenwaltda03c4e2010-01-20 19:29:41 -08001244 /*
1245 * If the failover network is already connected, then immediately send
1246 * out a followup broadcast indicating successful failover
1247 */
Robert Greenwaltf21ef7d2011-01-11 13:56:33 -08001248 if (mActiveDefaultNetwork != -1) {
1249 sendConnectedBroadcast(mNetTrackers[mActiveDefaultNetwork].getNetworkInfo());
Robert Greenwaltda03c4e2010-01-20 19:29:41 -08001250 }
Mike Lockwood0f79b542009-08-14 14:18:49 -04001251 }
1252
1253 private void sendStickyBroadcast(Intent intent) {
1254 synchronized(this) {
Dianne Hackborn1c633fc2009-12-08 19:45:14 -08001255 if (!mSystemReady) {
1256 mInitialBroadcast = new Intent(intent);
Mike Lockwood0f79b542009-08-14 14:18:49 -04001257 }
Dianne Hackborn1c633fc2009-12-08 19:45:14 -08001258 intent.addFlags(Intent.FLAG_RECEIVER_REGISTERED_ONLY_BEFORE_BOOT);
1259 mContext.sendStickyBroadcast(intent);
Mike Lockwood0f79b542009-08-14 14:18:49 -04001260 }
1261 }
1262
1263 void systemReady() {
1264 synchronized(this) {
1265 mSystemReady = true;
Dianne Hackborn1c633fc2009-12-08 19:45:14 -08001266 if (mInitialBroadcast != null) {
1267 mContext.sendStickyBroadcast(mInitialBroadcast);
1268 mInitialBroadcast = null;
Mike Lockwood0f79b542009-08-14 14:18:49 -04001269 }
1270 }
Robert Greenwalt434203a2010-10-11 16:00:27 -07001271 // load the global proxy at startup
1272 mHandler.sendMessage(mHandler.obtainMessage(EVENT_APPLY_GLOBAL_HTTP_PROXY));
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001273 }
1274
1275 private void handleConnect(NetworkInfo info) {
Robert Greenwalt42acef32009-08-12 16:08:25 -07001276 int type = info.getType();
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001277
1278 // snapshot isFailover, because sendConnectedBroadcast() resets it
1279 boolean isFailover = info.isFailover();
Robert Greenwalt42acef32009-08-12 16:08:25 -07001280 NetworkStateTracker thisNet = mNetTrackers[type];
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001281
Robert Greenwalt42acef32009-08-12 16:08:25 -07001282 // if this is a default net and other default is running
1283 // kill the one not preferred
1284 if (mNetAttributes[type].isDefault()) {
Robert Greenwalt42acef32009-08-12 16:08:25 -07001285 if (mActiveDefaultNetwork != -1 && mActiveDefaultNetwork != type) {
1286 if ((type != mNetworkPreference &&
1287 mNetAttributes[mActiveDefaultNetwork].mPriority >
1288 mNetAttributes[type].mPriority) ||
1289 mNetworkPreference == mActiveDefaultNetwork) {
1290 // don't accept this one
Wink Savilleed9c02b2010-12-03 12:01:38 -08001291 if (DBG) {
1292 log("Not broadcasting CONNECT_ACTION " +
Robert Greenwalt42acef32009-08-12 16:08:25 -07001293 "to torn down network " + info.getTypeName());
Wink Savilleed9c02b2010-12-03 12:01:38 -08001294 }
Robert Greenwalt42acef32009-08-12 16:08:25 -07001295 teardown(thisNet);
1296 return;
1297 } else {
1298 // tear down the other
1299 NetworkStateTracker otherNet =
1300 mNetTrackers[mActiveDefaultNetwork];
Wink Savilleed9c02b2010-12-03 12:01:38 -08001301 if (DBG) {
1302 log("Policy requires " + otherNet.getNetworkInfo().getTypeName() +
Robert Greenwalt42acef32009-08-12 16:08:25 -07001303 " teardown");
Wink Savilleed9c02b2010-12-03 12:01:38 -08001304 }
Robert Greenwalt42acef32009-08-12 16:08:25 -07001305 if (!teardown(otherNet)) {
Wink Savilleed9c02b2010-12-03 12:01:38 -08001306 loge("Network declined teardown request");
Robert Greenwalt42acef32009-08-12 16:08:25 -07001307 return;
1308 }
Robert Greenwalt14f2ef42010-06-15 12:19:37 -07001309 }
1310 }
1311 synchronized (ConnectivityService.this) {
1312 // have a new default network, release the transition wakelock in a second
1313 // if it's held. The second pause is to allow apps to reconnect over the
1314 // new network
1315 if (mNetTransitionWakeLock.isHeld()) {
1316 mHandler.sendMessageDelayed(mHandler.obtainMessage(
Robert Greenwaltf3331232010-09-24 14:32:21 -07001317 EVENT_CLEAR_NET_TRANSITION_WAKELOCK,
Robert Greenwalt14f2ef42010-06-15 12:19:37 -07001318 mNetTransitionWakeLockSerialNumber, 0),
1319 1000);
Robert Greenwalt42acef32009-08-12 16:08:25 -07001320 }
1321 }
1322 mActiveDefaultNetwork = type;
Robert Greenwaltd7085fc2010-09-08 15:24:47 -07001323 // this will cause us to come up initially as unconnected and switching
1324 // to connected after our normal pause unless somebody reports us as reall
1325 // disconnected
1326 mDefaultInetConditionPublished = 0;
1327 mDefaultConnectionSequence++;
1328 mInetConditionChangeInFlight = false;
1329 // Don't do this - if we never sign in stay, grey
1330 //reportNetworkCondition(mActiveDefaultNetwork, 100);
Robert Greenwalt42acef32009-08-12 16:08:25 -07001331 }
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001332 thisNet.setTeardownRequested(false);
Irfan Sheriffd649c122010-06-09 15:39:36 -07001333 updateNetworkSettings(thisNet);
Robert Greenwaltc76b8fa2010-07-23 15:46:26 -07001334 handleConnectivityChange(type);
Robert Greenwalt42acef32009-08-12 16:08:25 -07001335 sendConnectedBroadcast(info);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001336 }
1337
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001338 /**
Robert Greenwaltc76b8fa2010-07-23 15:46:26 -07001339 * After a change in the connectivity state of a network. We're mainly
1340 * concerned with making sure that the list of DNS servers is set up
1341 * according to which networks are connected, and ensuring that the
1342 * right routing table entries exist.
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001343 */
Robert Greenwaltc76b8fa2010-07-23 15:46:26 -07001344 private void handleConnectivityChange(int netType) {
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001345 /*
Robert Greenwalt42acef32009-08-12 16:08:25 -07001346 * If a non-default network is enabled, add the host routes that
Robert Greenwaltc76b8fa2010-07-23 15:46:26 -07001347 * will allow it's DNS servers to be accessed.
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001348 */
Robert Greenwaltc76b8fa2010-07-23 15:46:26 -07001349 handleDnsConfigurationChange(netType);
Robert Greenwalt42acef32009-08-12 16:08:25 -07001350
Robert Greenwaltc76b8fa2010-07-23 15:46:26 -07001351 if (mNetTrackers[netType].getNetworkInfo().isConnected()) {
1352 if (mNetAttributes[netType].isDefault()) {
Robert Greenwalt434203a2010-10-11 16:00:27 -07001353 handleApplyDefaultProxy(netType);
Robert Greenwaltc76b8fa2010-07-23 15:46:26 -07001354 addDefaultRoute(mNetTrackers[netType]);
Robert Greenwalt42acef32009-08-12 16:08:25 -07001355 } else {
Michael Jurka8fad7ee2011-03-30 19:54:08 -07001356 addPrivateDnsRoutes(mNetTrackers[netType]);
Robert Greenwaltc76b8fa2010-07-23 15:46:26 -07001357 }
1358 } else {
1359 if (mNetAttributes[netType].isDefault()) {
1360 removeDefaultRoute(mNetTrackers[netType]);
1361 } else {
1362 removePrivateDnsRoutes(mNetTrackers[netType]);
Robert Greenwalt42acef32009-08-12 16:08:25 -07001363 }
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001364 }
1365 }
1366
Irfan Sheriffd649c122010-06-09 15:39:36 -07001367 private void addPrivateDnsRoutes(NetworkStateTracker nt) {
Irfan Sheriffd649c122010-06-09 15:39:36 -07001368 boolean privateDnsRouteSet = nt.isPrivateDnsRouteSet();
Robert Greenwalt37e65eb2010-08-30 10:56:47 -07001369 LinkProperties p = nt.getLinkProperties();
Robert Greenwalt47f69fe2010-06-15 15:43:39 -07001370 if (p == null) return;
1371 String interfaceName = p.getInterfaceName();
Irfan Sheriffd649c122010-06-09 15:39:36 -07001372
1373 if (DBG) {
Wink Savilleed9c02b2010-12-03 12:01:38 -08001374 log("addPrivateDnsRoutes for " + nt +
Irfan Sheriffd649c122010-06-09 15:39:36 -07001375 "(" + interfaceName + ") - mPrivateDnsRouteSet = " + privateDnsRouteSet);
1376 }
Irfan Sheriffd649c122010-06-09 15:39:36 -07001377 if (interfaceName != null && !privateDnsRouteSet) {
Robert Greenwalt47f69fe2010-06-15 15:43:39 -07001378 Collection<InetAddress> dnsList = p.getDnses();
1379 for (InetAddress dns : dnsList) {
Wink Savilleed9c02b2010-12-03 12:01:38 -08001380 if (DBG) log(" adding " + dns);
Robert Greenwalt585ac0f2010-08-27 09:24:29 -07001381 NetworkUtils.addHostRoute(interfaceName, dns, null);
Irfan Sheriffd649c122010-06-09 15:39:36 -07001382 }
1383 nt.privateDnsRouteSet(true);
1384 }
1385 }
1386
1387 private void removePrivateDnsRoutes(NetworkStateTracker nt) {
1388 // TODO - we should do this explicitly but the NetUtils api doesnt
1389 // support this yet - must remove all. No worse than before
Robert Greenwalt37e65eb2010-08-30 10:56:47 -07001390 LinkProperties p = nt.getLinkProperties();
Robert Greenwalt47f69fe2010-06-15 15:43:39 -07001391 if (p == null) return;
1392 String interfaceName = p.getInterfaceName();
Irfan Sheriffd649c122010-06-09 15:39:36 -07001393 boolean privateDnsRouteSet = nt.isPrivateDnsRouteSet();
1394 if (interfaceName != null && privateDnsRouteSet) {
1395 if (DBG) {
Wink Savilleed9c02b2010-12-03 12:01:38 -08001396 log("removePrivateDnsRoutes for " + nt.getNetworkInfo().getTypeName() +
Irfan Sheriffd649c122010-06-09 15:39:36 -07001397 " (" + interfaceName + ")");
1398 }
1399 NetworkUtils.removeHostRoutes(interfaceName);
1400 nt.privateDnsRouteSet(false);
1401 }
1402 }
1403
Irfan Sheriffd649c122010-06-09 15:39:36 -07001404
1405 private void addDefaultRoute(NetworkStateTracker nt) {
Robert Greenwalt37e65eb2010-08-30 10:56:47 -07001406 LinkProperties p = nt.getLinkProperties();
Robert Greenwalt47f69fe2010-06-15 15:43:39 -07001407 if (p == null) return;
1408 String interfaceName = p.getInterfaceName();
Robert Greenwalt992564e2011-02-09 13:56:06 -08001409 if (TextUtils.isEmpty(interfaceName)) return;
1410 for (InetAddress gateway : p.getGateways()) {
Irfan Sheriffd649c122010-06-09 15:39:36 -07001411
Robert Greenwaltedcb4f92011-03-22 18:47:42 -07001412 if (NetworkUtils.addHostRoute(interfaceName, gateway, null) &&
1413 NetworkUtils.addDefaultRoute(interfaceName, gateway)) {
1414 if (DBG) {
1415 NetworkInfo networkInfo = nt.getNetworkInfo();
1416 log("addDefaultRoute for " + networkInfo.getTypeName() +
1417 " (" + interfaceName + "), GatewayAddr=" + gateway.getHostAddress());
1418 }
Irfan Sheriffd649c122010-06-09 15:39:36 -07001419 }
Irfan Sheriffd649c122010-06-09 15:39:36 -07001420 }
1421 }
1422
1423
1424 public void removeDefaultRoute(NetworkStateTracker nt) {
Robert Greenwalt37e65eb2010-08-30 10:56:47 -07001425 LinkProperties p = nt.getLinkProperties();
Robert Greenwalt47f69fe2010-06-15 15:43:39 -07001426 if (p == null) return;
1427 String interfaceName = p.getInterfaceName();
Irfan Sheriffd649c122010-06-09 15:39:36 -07001428
Robert Greenwaltc76b8fa2010-07-23 15:46:26 -07001429 if (interfaceName != null) {
Robert Greenwaltedcb4f92011-03-22 18:47:42 -07001430 if (NetworkUtils.removeDefaultRoute(interfaceName) >= 0) {
1431 if (DBG) {
1432 NetworkInfo networkInfo = nt.getNetworkInfo();
1433 log("removeDefaultRoute for " + networkInfo.getTypeName() + " (" +
1434 interfaceName + ")");
1435 }
Irfan Sheriffd649c122010-06-09 15:39:36 -07001436 }
Irfan Sheriffd649c122010-06-09 15:39:36 -07001437 }
1438 }
1439
1440 /**
1441 * Reads the network specific TCP buffer sizes from SystemProperties
1442 * net.tcp.buffersize.[default|wifi|umts|edge|gprs] and set them for system
1443 * wide use
1444 */
1445 public void updateNetworkSettings(NetworkStateTracker nt) {
1446 String key = nt.getTcpBufferSizesPropName();
1447 String bufferSizes = SystemProperties.get(key);
1448
1449 if (bufferSizes.length() == 0) {
Wink Savilleed9c02b2010-12-03 12:01:38 -08001450 loge(key + " not found in system properties. Using defaults");
Irfan Sheriffd649c122010-06-09 15:39:36 -07001451
1452 // Setting to default values so we won't be stuck to previous values
1453 key = "net.tcp.buffersize.default";
1454 bufferSizes = SystemProperties.get(key);
1455 }
1456
1457 // Set values in kernel
1458 if (bufferSizes.length() != 0) {
1459 if (DBG) {
Wink Savilleed9c02b2010-12-03 12:01:38 -08001460 log("Setting TCP values: [" + bufferSizes
Irfan Sheriffd649c122010-06-09 15:39:36 -07001461 + "] which comes from [" + key + "]");
1462 }
1463 setBufferSize(bufferSizes);
1464 }
1465 }
1466
1467 /**
1468 * Writes TCP buffer sizes to /sys/kernel/ipv4/tcp_[r/w]mem_[min/def/max]
1469 * which maps to /proc/sys/net/ipv4/tcp_rmem and tcpwmem
1470 *
1471 * @param bufferSizes in the format of "readMin, readInitial, readMax,
1472 * writeMin, writeInitial, writeMax"
1473 */
1474 private void setBufferSize(String bufferSizes) {
1475 try {
1476 String[] values = bufferSizes.split(",");
1477
1478 if (values.length == 6) {
1479 final String prefix = "/sys/kernel/ipv4/tcp_";
1480 stringToFile(prefix + "rmem_min", values[0]);
1481 stringToFile(prefix + "rmem_def", values[1]);
1482 stringToFile(prefix + "rmem_max", values[2]);
1483 stringToFile(prefix + "wmem_min", values[3]);
1484 stringToFile(prefix + "wmem_def", values[4]);
1485 stringToFile(prefix + "wmem_max", values[5]);
1486 } else {
Wink Savilleed9c02b2010-12-03 12:01:38 -08001487 loge("Invalid buffersize string: " + bufferSizes);
Irfan Sheriffd649c122010-06-09 15:39:36 -07001488 }
1489 } catch (IOException e) {
Wink Savilleed9c02b2010-12-03 12:01:38 -08001490 loge("Can't set tcp buffer sizes:" + e);
Irfan Sheriffd649c122010-06-09 15:39:36 -07001491 }
1492 }
1493
1494 /**
1495 * Writes string to file. Basically same as "echo -n $string > $filename"
1496 *
1497 * @param filename
1498 * @param string
1499 * @throws IOException
1500 */
1501 private void stringToFile(String filename, String string) throws IOException {
1502 FileWriter out = new FileWriter(filename);
1503 try {
1504 out.write(string);
1505 } finally {
1506 out.close();
1507 }
1508 }
1509
1510
Robert Greenwalt42acef32009-08-12 16:08:25 -07001511 /**
1512 * Adjust the per-process dns entries (net.dns<x>.<pid>) based
1513 * on the highest priority active net which this process requested.
1514 * If there aren't any, clear it out
1515 */
1516 private void reassessPidDns(int myPid, boolean doBump)
1517 {
Wink Savilleed9c02b2010-12-03 12:01:38 -08001518 if (DBG) log("reassessPidDns for pid " + myPid);
Robert Greenwalt42acef32009-08-12 16:08:25 -07001519 for(int i : mPriorityList) {
1520 if (mNetAttributes[i].isDefault()) {
1521 continue;
1522 }
1523 NetworkStateTracker nt = mNetTrackers[i];
Robert Greenwalt86e9e552009-07-16 17:21:39 -07001524 if (nt.getNetworkInfo().isConnected() &&
1525 !nt.isTeardownRequested()) {
Robert Greenwalt37e65eb2010-08-30 10:56:47 -07001526 LinkProperties p = nt.getLinkProperties();
Robert Greenwalt47f69fe2010-06-15 15:43:39 -07001527 if (p == null) continue;
Robert Greenwalt42acef32009-08-12 16:08:25 -07001528 List pids = mNetRequestersPids[i];
1529 for (int j=0; j<pids.size(); j++) {
1530 Integer pid = (Integer)pids.get(j);
1531 if (pid.intValue() == myPid) {
Robert Greenwalt47f69fe2010-06-15 15:43:39 -07001532 Collection<InetAddress> dnses = p.getDnses();
1533 writePidDns(dnses, myPid);
Robert Greenwalt42acef32009-08-12 16:08:25 -07001534 if (doBump) {
1535 bumpDns();
1536 }
1537 return;
1538 }
1539 }
1540 }
1541 }
1542 // nothing found - delete
1543 for (int i = 1; ; i++) {
1544 String prop = "net.dns" + i + "." + myPid;
1545 if (SystemProperties.get(prop).length() == 0) {
1546 if (doBump) {
1547 bumpDns();
1548 }
1549 return;
1550 }
1551 SystemProperties.set(prop, "");
1552 }
1553 }
1554
Robert Greenwalt10398722010-12-17 15:20:36 -08001555 // return true if results in a change
1556 private boolean writePidDns(Collection <InetAddress> dnses, int pid) {
Robert Greenwalt42acef32009-08-12 16:08:25 -07001557 int j = 1;
Robert Greenwalt10398722010-12-17 15:20:36 -08001558 boolean changed = false;
Robert Greenwalt47f69fe2010-06-15 15:43:39 -07001559 for (InetAddress dns : dnses) {
Robert Greenwalt10398722010-12-17 15:20:36 -08001560 String dnsString = dns.getHostAddress();
1561 if (changed || !dnsString.equals(SystemProperties.get("net.dns" + j + "." + pid))) {
1562 changed = true;
1563 SystemProperties.set("net.dns" + j++ + "." + pid, dns.getHostAddress());
1564 }
Robert Greenwalt42acef32009-08-12 16:08:25 -07001565 }
Robert Greenwalt10398722010-12-17 15:20:36 -08001566 return changed;
Robert Greenwalt42acef32009-08-12 16:08:25 -07001567 }
1568
1569 private void bumpDns() {
1570 /*
1571 * Bump the property that tells the name resolver library to reread
1572 * the DNS server list from the properties.
1573 */
1574 String propVal = SystemProperties.get("net.dnschange");
1575 int n = 0;
1576 if (propVal.length() != 0) {
1577 try {
1578 n = Integer.parseInt(propVal);
1579 } catch (NumberFormatException e) {}
1580 }
1581 SystemProperties.set("net.dnschange", "" + (n+1));
Robert Greenwalt03595d02010-11-02 14:08:23 -07001582 /*
1583 * Tell the VMs to toss their DNS caches
1584 */
1585 Intent intent = new Intent(Intent.ACTION_CLEAR_DNS_CACHE);
1586 intent.addFlags(Intent.FLAG_RECEIVER_REPLACE_PENDING);
Stan Chesnutt3d1db862011-01-05 17:14:03 -08001587 /*
1588 * Connectivity events can happen before boot has completed ...
1589 */
1590 intent.addFlags(Intent.FLAG_RECEIVER_REGISTERED_ONLY_BEFORE_BOOT);
Robert Greenwalt03595d02010-11-02 14:08:23 -07001591 mContext.sendBroadcast(intent);
Robert Greenwalt42acef32009-08-12 16:08:25 -07001592 }
1593
Robert Greenwaltc76b8fa2010-07-23 15:46:26 -07001594 private void handleDnsConfigurationChange(int netType) {
Robert Greenwalt42acef32009-08-12 16:08:25 -07001595 // add default net's dns entries
Robert Greenwaltc76b8fa2010-07-23 15:46:26 -07001596 NetworkStateTracker nt = mNetTrackers[netType];
1597 if (nt != null && nt.getNetworkInfo().isConnected() && !nt.isTeardownRequested()) {
Robert Greenwalt37e65eb2010-08-30 10:56:47 -07001598 LinkProperties p = nt.getLinkProperties();
Robert Greenwaltc76b8fa2010-07-23 15:46:26 -07001599 if (p == null) return;
1600 Collection<InetAddress> dnses = p.getDnses();
Robert Greenwalt10398722010-12-17 15:20:36 -08001601 boolean changed = false;
Robert Greenwaltc76b8fa2010-07-23 15:46:26 -07001602 if (mNetAttributes[netType].isDefault()) {
1603 int j = 1;
Robert Greenwalte90aa5e2010-09-01 11:34:05 -07001604 if (dnses.size() == 0 && mDefaultDns != null) {
Robert Greenwalt10398722010-12-17 15:20:36 -08001605 String dnsString = mDefaultDns.getHostAddress();
1606 if (!dnsString.equals(SystemProperties.get("net.dns1"))) {
1607 if (DBG) {
1608 log("no dns provided - using " + dnsString);
1609 }
1610 changed = true;
1611 SystemProperties.set("net.dns1", dnsString);
Robert Greenwalt42acef32009-08-12 16:08:25 -07001612 }
Robert Greenwalte90aa5e2010-09-01 11:34:05 -07001613 j++;
1614 } else {
1615 for (InetAddress dns : dnses) {
Robert Greenwalt10398722010-12-17 15:20:36 -08001616 String dnsString = dns.getHostAddress();
1617 if (!changed && dnsString.equals(SystemProperties.get("net.dns" + j))) {
1618 j++;
1619 continue;
1620 }
Robert Greenwalte90aa5e2010-09-01 11:34:05 -07001621 if (DBG) {
Wink Savilleed9c02b2010-12-03 12:01:38 -08001622 log("adding dns " + dns + " for " +
Robert Greenwalte90aa5e2010-09-01 11:34:05 -07001623 nt.getNetworkInfo().getTypeName());
1624 }
Robert Greenwalt10398722010-12-17 15:20:36 -08001625 changed = true;
1626 SystemProperties.set("net.dns" + j++, dnsString);
Robert Greenwalte90aa5e2010-09-01 11:34:05 -07001627 }
Robert Greenwaltc76b8fa2010-07-23 15:46:26 -07001628 }
1629 for (int k=j ; k<mNumDnsEntries; k++) {
Robert Greenwalt10398722010-12-17 15:20:36 -08001630 if (changed || !TextUtils.isEmpty(SystemProperties.get("net.dns" + k))) {
1631 if (DBG) log("erasing net.dns" + k);
1632 changed = true;
1633 SystemProperties.set("net.dns" + k, "");
1634 }
Robert Greenwaltc76b8fa2010-07-23 15:46:26 -07001635 }
1636 mNumDnsEntries = j;
1637 } else {
1638 // set per-pid dns for attached secondary nets
1639 List pids = mNetRequestersPids[netType];
1640 for (int y=0; y< pids.size(); y++) {
1641 Integer pid = (Integer)pids.get(y);
Robert Greenwalt10398722010-12-17 15:20:36 -08001642 changed = writePidDns(dnses, pid.intValue());
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001643 }
1644 }
Robert Greenwalt10398722010-12-17 15:20:36 -08001645 if (changed) bumpDns();
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001646 }
Robert Greenwalt42acef32009-08-12 16:08:25 -07001647 }
1648
1649 private int getRestoreDefaultNetworkDelay() {
1650 String restoreDefaultNetworkDelayStr = SystemProperties.get(
1651 NETWORK_RESTORE_DELAY_PROP_NAME);
1652 if(restoreDefaultNetworkDelayStr != null &&
1653 restoreDefaultNetworkDelayStr.length() != 0) {
1654 try {
1655 return Integer.valueOf(restoreDefaultNetworkDelayStr);
1656 } catch (NumberFormatException e) {
1657 }
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001658 }
Robert Greenwalt42acef32009-08-12 16:08:25 -07001659 return RESTORE_DEFAULT_NETWORK_DELAY;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001660 }
1661
1662 @Override
1663 protected void dump(FileDescriptor fd, PrintWriter pw, String[] args) {
Robert Greenwalt86e9e552009-07-16 17:21:39 -07001664 if (mContext.checkCallingOrSelfPermission(
1665 android.Manifest.permission.DUMP)
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001666 != PackageManager.PERMISSION_GRANTED) {
Robert Greenwalt86e9e552009-07-16 17:21:39 -07001667 pw.println("Permission Denial: can't dump ConnectivityService " +
1668 "from from pid=" + Binder.getCallingPid() + ", uid=" +
1669 Binder.getCallingUid());
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001670 return;
1671 }
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001672 pw.println();
1673 for (NetworkStateTracker nst : mNetTrackers) {
Robert Greenwaltb9285352009-12-21 18:24:07 -08001674 if (nst != null) {
1675 if (nst.getNetworkInfo().isConnected()) {
1676 pw.println("Active network: " + nst.getNetworkInfo().
1677 getTypeName());
1678 }
1679 pw.println(nst.getNetworkInfo());
1680 pw.println(nst);
1681 pw.println();
Robert Greenwalt42acef32009-08-12 16:08:25 -07001682 }
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001683 }
Robert Greenwaltb9285352009-12-21 18:24:07 -08001684
1685 pw.println("Network Requester Pids:");
1686 for (int net : mPriorityList) {
1687 String pidString = net + ": ";
1688 for (Object pid : mNetRequestersPids[net]) {
1689 pidString = pidString + pid.toString() + ", ";
1690 }
1691 pw.println(pidString);
1692 }
1693 pw.println();
1694
1695 pw.println("FeatureUsers:");
1696 for (Object requester : mFeatureUsers) {
1697 pw.println(requester.toString());
1698 }
1699 pw.println();
Robert Greenwalt2a091d72010-02-11 18:18:40 -08001700
Robert Greenwalt14f2ef42010-06-15 12:19:37 -07001701 synchronized (this) {
1702 pw.println("NetworkTranstionWakeLock is currently " +
1703 (mNetTransitionWakeLock.isHeld() ? "" : "not ") + "held.");
1704 pw.println("It was last requested for "+mNetTransitionWakeLockCausedBy);
1705 }
1706 pw.println();
1707
Robert Greenwalt2a091d72010-02-11 18:18:40 -08001708 mTethering.dump(fd, pw, args);
Robert Greenwalt4e8dfef2010-09-20 14:35:25 -07001709
1710 if (mInetLog != null) {
1711 pw.println();
1712 pw.println("Inet condition reports:");
1713 for(int i = 0; i < mInetLog.size(); i++) {
1714 pw.println(mInetLog.get(i));
1715 }
1716 }
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001717 }
1718
Robert Greenwalt42acef32009-08-12 16:08:25 -07001719 // must be stateless - things change under us.
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001720 private class MyHandler extends Handler {
Wink Savillebb08caf2010-09-02 19:23:52 -07001721 public MyHandler(Looper looper) {
1722 super(looper);
1723 }
1724
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001725 @Override
1726 public void handleMessage(Message msg) {
1727 NetworkInfo info;
1728 switch (msg.what) {
1729 case NetworkStateTracker.EVENT_STATE_CHANGED:
1730 info = (NetworkInfo) msg.obj;
Robert Greenwalt511288a2009-12-07 11:33:18 -08001731 int type = info.getType();
1732 NetworkInfo.State state = info.getState();
Robert Greenwalt6e6dec22010-01-25 16:14:00 -08001733 // only do this optimization for wifi. It going into scan mode for location
1734 // services generates alot of noise. Meanwhile the mms apn won't send out
1735 // subsequent notifications when on default cellular because it never
1736 // disconnects.. so only do this to wifi notifications. Fixed better when the
1737 // APN notifications are standardized.
1738 if (mNetAttributes[type].mLastState == state &&
1739 mNetAttributes[type].mRadio == ConnectivityManager.TYPE_WIFI) {
Robert Greenwalt511288a2009-12-07 11:33:18 -08001740 if (DBG) {
Robert Greenwalt6e6dec22010-01-25 16:14:00 -08001741 // TODO - remove this after we validate the dropping doesn't break
1742 // anything
Wink Savilleed9c02b2010-12-03 12:01:38 -08001743 log("Dropping ConnectivityChange for " +
Robert Greenwalt1193ae42010-01-13 09:36:31 -08001744 info.getTypeName() + ": " +
Robert Greenwalt511288a2009-12-07 11:33:18 -08001745 state + "/" + info.getDetailedState());
1746 }
1747 return;
1748 }
1749 mNetAttributes[type].mLastState = state;
1750
Wink Savilleed9c02b2010-12-03 12:01:38 -08001751 if (DBG) log("ConnectivityChange for " +
Robert Greenwalt86e9e552009-07-16 17:21:39 -07001752 info.getTypeName() + ": " +
Robert Greenwalt511288a2009-12-07 11:33:18 -08001753 state + "/" + info.getDetailedState());
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001754
1755 // Connectivity state changed:
1756 // [31-13] Reserved for future use
Robert Greenwalt86e9e552009-07-16 17:21:39 -07001757 // [12-9] Network subtype (for mobile network, as defined
1758 // by TelephonyManager)
1759 // [8-3] Detailed state ordinal (as defined by
1760 // NetworkInfo.DetailedState)
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001761 // [2-0] Network type (as defined by ConnectivityManager)
1762 int eventLogParam = (info.getType() & 0x7) |
1763 ((info.getDetailedState().ordinal() & 0x3f) << 3) |
1764 (info.getSubtype() << 9);
Doug Zongkerab5c49c2009-12-04 10:31:43 -08001765 EventLog.writeEvent(EventLogTags.CONNECTIVITY_STATE_CHANGED,
Robert Greenwalt86e9e552009-07-16 17:21:39 -07001766 eventLogParam);
1767
1768 if (info.getDetailedState() ==
1769 NetworkInfo.DetailedState.FAILED) {
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001770 handleConnectionFailure(info);
Robert Greenwalt511288a2009-12-07 11:33:18 -08001771 } else if (state == NetworkInfo.State.DISCONNECTED) {
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001772 handleDisconnect(info);
Robert Greenwalt511288a2009-12-07 11:33:18 -08001773 } else if (state == NetworkInfo.State.SUSPENDED) {
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001774 // TODO: need to think this over.
Robert Greenwalt86e9e552009-07-16 17:21:39 -07001775 // the logic here is, handle SUSPENDED the same as
1776 // DISCONNECTED. The only difference being we are
1777 // broadcasting an intent with NetworkInfo that's
1778 // suspended. This allows the applications an
1779 // opportunity to handle DISCONNECTED and SUSPENDED
1780 // differently, or not.
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001781 handleDisconnect(info);
Robert Greenwalt511288a2009-12-07 11:33:18 -08001782 } else if (state == NetworkInfo.State.CONNECTED) {
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001783 handleConnect(info);
1784 }
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001785 break;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001786 case NetworkStateTracker.EVENT_CONFIGURATION_CHANGED:
Robert Greenwaltc76b8fa2010-07-23 15:46:26 -07001787 info = (NetworkInfo) msg.obj;
1788 type = info.getType();
Robert Greenwalt434203a2010-10-11 16:00:27 -07001789 handleConnectivityChange(type);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001790 break;
Robert Greenwaltf3331232010-09-24 14:32:21 -07001791 case EVENT_CLEAR_NET_TRANSITION_WAKELOCK:
Robert Greenwalt14f2ef42010-06-15 12:19:37 -07001792 String causedBy = null;
1793 synchronized (ConnectivityService.this) {
1794 if (msg.arg1 == mNetTransitionWakeLockSerialNumber &&
1795 mNetTransitionWakeLock.isHeld()) {
1796 mNetTransitionWakeLock.release();
1797 causedBy = mNetTransitionWakeLockCausedBy;
1798 }
1799 }
1800 if (causedBy != null) {
Wink Savilleed9c02b2010-12-03 12:01:38 -08001801 log("NetTransition Wakelock for " + causedBy + " released by timeout");
Robert Greenwalt14f2ef42010-06-15 12:19:37 -07001802 }
Robert Greenwalt057d5e92010-09-09 14:05:10 -07001803 break;
Robert Greenwalt8dcc28b2010-09-23 10:05:56 -07001804 case EVENT_RESTORE_DEFAULT_NETWORK:
Robert Greenwalt9c75d4a2009-09-27 17:27:04 -07001805 FeatureUser u = (FeatureUser)msg.obj;
1806 u.expire();
Robert Greenwaltd7085fc2010-09-08 15:24:47 -07001807 break;
Robert Greenwalt8dcc28b2010-09-23 10:05:56 -07001808 case EVENT_INET_CONDITION_CHANGE:
1809 {
1810 int netType = msg.arg1;
1811 int condition = msg.arg2;
1812 handleInetConditionChange(netType, condition);
Robert Greenwalt14f2ef42010-06-15 12:19:37 -07001813 break;
Robert Greenwalt8dcc28b2010-09-23 10:05:56 -07001814 }
1815 case EVENT_INET_CONDITION_HOLD_END:
1816 {
1817 int netType = msg.arg1;
1818 int sequence = msg.arg2;
1819 handleInetConditionHoldEnd(netType, sequence);
Robert Greenwaltd7085fc2010-09-08 15:24:47 -07001820 break;
Robert Greenwalt8dcc28b2010-09-23 10:05:56 -07001821 }
1822 case EVENT_SET_NETWORK_PREFERENCE:
1823 {
1824 int preference = msg.arg1;
1825 handleSetNetworkPreference(preference);
1826 break;
1827 }
1828 case EVENT_SET_BACKGROUND_DATA:
1829 {
1830 boolean enabled = (msg.arg1 == ENABLED);
1831 handleSetBackgroundData(enabled);
1832 break;
1833 }
1834 case EVENT_SET_MOBILE_DATA:
1835 {
1836 boolean enabled = (msg.arg1 == ENABLED);
1837 handleSetMobileData(enabled);
1838 break;
1839 }
Robert Greenwalt434203a2010-10-11 16:00:27 -07001840 case EVENT_APPLY_GLOBAL_HTTP_PROXY:
1841 {
1842 handleDeprecatedGlobalHttpProxy();
1843 }
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001844 }
1845 }
1846 }
Robert Greenwaltd0e18ff2010-01-26 11:40:34 -08001847
1848 // javadoc from interface
Robert Greenwalt5a735062010-03-02 17:25:02 -08001849 public int tether(String iface) {
Robert Greenwaltd0e18ff2010-01-26 11:40:34 -08001850 enforceTetherChangePermission();
Robert Greenwalt5a735062010-03-02 17:25:02 -08001851
1852 if (isTetheringSupported()) {
1853 return mTethering.tether(iface);
1854 } else {
1855 return ConnectivityManager.TETHER_ERROR_UNSUPPORTED;
1856 }
Robert Greenwaltd0e18ff2010-01-26 11:40:34 -08001857 }
1858
1859 // javadoc from interface
Robert Greenwalt5a735062010-03-02 17:25:02 -08001860 public int untether(String iface) {
Robert Greenwaltd0e18ff2010-01-26 11:40:34 -08001861 enforceTetherChangePermission();
Robert Greenwalt5a735062010-03-02 17:25:02 -08001862
1863 if (isTetheringSupported()) {
1864 return mTethering.untether(iface);
1865 } else {
1866 return ConnectivityManager.TETHER_ERROR_UNSUPPORTED;
1867 }
1868 }
1869
1870 // javadoc from interface
1871 public int getLastTetherError(String iface) {
1872 enforceTetherAccessPermission();
1873
1874 if (isTetheringSupported()) {
1875 return mTethering.getLastTetherError(iface);
1876 } else {
1877 return ConnectivityManager.TETHER_ERROR_UNSUPPORTED;
1878 }
Robert Greenwalt2a091d72010-02-11 18:18:40 -08001879 }
1880
1881 // TODO - proper iface API for selection by property, inspection, etc
1882 public String[] getTetherableUsbRegexs() {
1883 enforceTetherAccessPermission();
1884 if (isTetheringSupported()) {
1885 return mTethering.getTetherableUsbRegexs();
1886 } else {
1887 return new String[0];
1888 }
1889 }
1890
1891 public String[] getTetherableWifiRegexs() {
1892 enforceTetherAccessPermission();
1893 if (isTetheringSupported()) {
1894 return mTethering.getTetherableWifiRegexs();
1895 } else {
1896 return new String[0];
1897 }
Robert Greenwaltd0e18ff2010-01-26 11:40:34 -08001898 }
1899
Danica Chang6fdd0c62010-08-11 14:54:43 -07001900 public String[] getTetherableBluetoothRegexs() {
1901 enforceTetherAccessPermission();
1902 if (isTetheringSupported()) {
1903 return mTethering.getTetherableBluetoothRegexs();
1904 } else {
1905 return new String[0];
1906 }
1907 }
1908
Robert Greenwaltd0e18ff2010-01-26 11:40:34 -08001909 // TODO - move iface listing, queries, etc to new module
1910 // javadoc from interface
1911 public String[] getTetherableIfaces() {
Robert Greenwalt2a091d72010-02-11 18:18:40 -08001912 enforceTetherAccessPermission();
Robert Greenwaltd0e18ff2010-01-26 11:40:34 -08001913 return mTethering.getTetherableIfaces();
1914 }
1915
1916 public String[] getTetheredIfaces() {
Robert Greenwalt2a091d72010-02-11 18:18:40 -08001917 enforceTetherAccessPermission();
Robert Greenwaltd0e18ff2010-01-26 11:40:34 -08001918 return mTethering.getTetheredIfaces();
1919 }
Robert Greenwalt2a091d72010-02-11 18:18:40 -08001920
Robert Greenwalt5a735062010-03-02 17:25:02 -08001921 public String[] getTetheringErroredIfaces() {
1922 enforceTetherAccessPermission();
1923 return mTethering.getErroredIfaces();
1924 }
1925
Robert Greenwalt2a091d72010-02-11 18:18:40 -08001926 // if ro.tether.denied = true we default to no tethering
1927 // gservices could set the secure setting to 1 though to enable it on a build where it
1928 // had previously been turned off.
1929 public boolean isTetheringSupported() {
1930 enforceTetherAccessPermission();
1931 int defaultVal = (SystemProperties.get("ro.tether.denied").equals("true") ? 0 : 1);
Robert Greenwaltc9d5fb72010-02-25 12:29:30 -08001932 boolean tetherEnabledInSettings = (Settings.Secure.getInt(mContext.getContentResolver(),
1933 Settings.Secure.TETHER_SUPPORTED, defaultVal) != 0);
1934 return tetherEnabledInSettings && mTetheringConfigValid;
Robert Greenwalt2a091d72010-02-11 18:18:40 -08001935 }
Robert Greenwalt14f2ef42010-06-15 12:19:37 -07001936
1937 // An API NetworkStateTrackers can call when they lose their network.
1938 // This will automatically be cleared after X seconds or a network becomes CONNECTED,
1939 // whichever happens first. The timer is started by the first caller and not
1940 // restarted by subsequent callers.
1941 public void requestNetworkTransitionWakelock(String forWhom) {
1942 enforceConnectivityInternalPermission();
1943 synchronized (this) {
1944 if (mNetTransitionWakeLock.isHeld()) return;
1945 mNetTransitionWakeLockSerialNumber++;
1946 mNetTransitionWakeLock.acquire();
1947 mNetTransitionWakeLockCausedBy = forWhom;
1948 }
1949 mHandler.sendMessageDelayed(mHandler.obtainMessage(
Robert Greenwaltf3331232010-09-24 14:32:21 -07001950 EVENT_CLEAR_NET_TRANSITION_WAKELOCK,
Robert Greenwalt14f2ef42010-06-15 12:19:37 -07001951 mNetTransitionWakeLockSerialNumber, 0),
1952 mNetTransitionWakeLockTimeout);
1953 return;
1954 }
Robert Greenwaltca4306c2010-09-09 13:15:32 -07001955
Robert Greenwaltd7085fc2010-09-08 15:24:47 -07001956 // 100 percent is full good, 0 is full bad.
1957 public void reportInetCondition(int networkType, int percentage) {
Wink Savilleed9c02b2010-12-03 12:01:38 -08001958 if (DBG) log("reportNetworkCondition(" + networkType + ", " + percentage + ")");
Robert Greenwaltd7085fc2010-09-08 15:24:47 -07001959 mContext.enforceCallingOrSelfPermission(
1960 android.Manifest.permission.STATUS_BAR,
1961 "ConnectivityService");
1962
Robert Greenwalt4e8dfef2010-09-20 14:35:25 -07001963 if (DBG) {
1964 int pid = getCallingPid();
1965 int uid = getCallingUid();
1966 String s = pid + "(" + uid + ") reports inet is " +
1967 (percentage > 50 ? "connected" : "disconnected") + " (" + percentage + ") on " +
1968 "network Type " + networkType + " at " + GregorianCalendar.getInstance().getTime();
1969 mInetLog.add(s);
1970 while(mInetLog.size() > INET_CONDITION_LOG_MAX_SIZE) {
1971 mInetLog.remove(0);
1972 }
1973 }
Robert Greenwaltd7085fc2010-09-08 15:24:47 -07001974 mHandler.sendMessage(mHandler.obtainMessage(
Robert Greenwalt8dcc28b2010-09-23 10:05:56 -07001975 EVENT_INET_CONDITION_CHANGE, networkType, percentage));
1976 }
1977
1978 private void handleInetConditionChange(int netType, int condition) {
1979 if (DBG) {
Wink Savilleed9c02b2010-12-03 12:01:38 -08001980 log("Inet connectivity change, net=" +
Robert Greenwalt8dcc28b2010-09-23 10:05:56 -07001981 netType + ", condition=" + condition +
1982 ",mActiveDefaultNetwork=" + mActiveDefaultNetwork);
1983 }
1984 if (mActiveDefaultNetwork == -1) {
Wink Savilleed9c02b2010-12-03 12:01:38 -08001985 if (DBG) log("no active default network - aborting");
Robert Greenwalt8dcc28b2010-09-23 10:05:56 -07001986 return;
1987 }
1988 if (mActiveDefaultNetwork != netType) {
Wink Savilleed9c02b2010-12-03 12:01:38 -08001989 if (DBG) log("given net not default - aborting");
Robert Greenwalt8dcc28b2010-09-23 10:05:56 -07001990 return;
1991 }
1992 mDefaultInetCondition = condition;
1993 int delay;
1994 if (mInetConditionChangeInFlight == false) {
Wink Savilleed9c02b2010-12-03 12:01:38 -08001995 if (DBG) log("starting a change hold");
Robert Greenwalt8dcc28b2010-09-23 10:05:56 -07001996 // setup a new hold to debounce this
1997 if (mDefaultInetCondition > 50) {
1998 delay = Settings.Secure.getInt(mContext.getContentResolver(),
1999 Settings.Secure.INET_CONDITION_DEBOUNCE_UP_DELAY, 500);
2000 } else {
2001 delay = Settings.Secure.getInt(mContext.getContentResolver(),
2002 Settings.Secure.INET_CONDITION_DEBOUNCE_DOWN_DELAY, 3000);
2003 }
2004 mInetConditionChangeInFlight = true;
2005 mHandler.sendMessageDelayed(mHandler.obtainMessage(EVENT_INET_CONDITION_HOLD_END,
2006 mActiveDefaultNetwork, mDefaultConnectionSequence), delay);
2007 } else {
2008 // we've set the new condition, when this hold ends that will get
2009 // picked up
Wink Savilleed9c02b2010-12-03 12:01:38 -08002010 if (DBG) log("currently in hold - not setting new end evt");
Robert Greenwalt8dcc28b2010-09-23 10:05:56 -07002011 }
2012 }
2013
2014 private void handleInetConditionHoldEnd(int netType, int sequence) {
2015 if (DBG) {
Wink Savilleed9c02b2010-12-03 12:01:38 -08002016 log("Inet hold end, net=" + netType +
Robert Greenwalt8dcc28b2010-09-23 10:05:56 -07002017 ", condition =" + mDefaultInetCondition +
2018 ", published condition =" + mDefaultInetConditionPublished);
2019 }
2020 mInetConditionChangeInFlight = false;
2021
2022 if (mActiveDefaultNetwork == -1) {
Wink Savilleed9c02b2010-12-03 12:01:38 -08002023 if (DBG) log("no active default network - aborting");
Robert Greenwalt8dcc28b2010-09-23 10:05:56 -07002024 return;
2025 }
2026 if (mDefaultConnectionSequence != sequence) {
Wink Savilleed9c02b2010-12-03 12:01:38 -08002027 if (DBG) log("event hold for obsolete network - aborting");
Robert Greenwalt8dcc28b2010-09-23 10:05:56 -07002028 return;
2029 }
2030 if (mDefaultInetConditionPublished == mDefaultInetCondition) {
Wink Savilleed9c02b2010-12-03 12:01:38 -08002031 if (DBG) log("no change in condition - aborting");
Robert Greenwalt8dcc28b2010-09-23 10:05:56 -07002032 return;
2033 }
2034 NetworkInfo networkInfo = mNetTrackers[mActiveDefaultNetwork].getNetworkInfo();
2035 if (networkInfo.isConnected() == false) {
Wink Savilleed9c02b2010-12-03 12:01:38 -08002036 if (DBG) log("default network not connected - aborting");
Robert Greenwalt8dcc28b2010-09-23 10:05:56 -07002037 return;
2038 }
2039 mDefaultInetConditionPublished = mDefaultInetCondition;
2040 sendInetConditionBroadcast(networkInfo);
2041 return;
Robert Greenwaltd7085fc2010-09-08 15:24:47 -07002042 }
Robert Greenwalt434203a2010-10-11 16:00:27 -07002043
2044 public synchronized ProxyProperties getProxy() {
2045 if (mGlobalProxy != null) return mGlobalProxy;
2046 if (mDefaultProxy != null) return mDefaultProxy;
2047 return null;
2048 }
2049
2050 public void setGlobalProxy(ProxyProperties proxyProperties) {
2051 enforceChangePermission();
2052 synchronized (mGlobalProxyLock) {
2053 if (proxyProperties == mGlobalProxy) return;
2054 if (proxyProperties != null && proxyProperties.equals(mGlobalProxy)) return;
2055 if (mGlobalProxy != null && mGlobalProxy.equals(proxyProperties)) return;
2056
2057 String host = "";
2058 int port = 0;
2059 String exclList = "";
2060 if (proxyProperties != null && !TextUtils.isEmpty(proxyProperties.getHost())) {
2061 mGlobalProxy = new ProxyProperties(proxyProperties);
2062 host = mGlobalProxy.getHost();
2063 port = mGlobalProxy.getPort();
2064 exclList = mGlobalProxy.getExclusionList();
2065 } else {
2066 mGlobalProxy = null;
2067 }
2068 ContentResolver res = mContext.getContentResolver();
2069 Settings.Secure.putString(res, Settings.Secure.GLOBAL_HTTP_PROXY_HOST, host);
2070 Settings.Secure.putInt(res, Settings.Secure.GLOBAL_HTTP_PROXY_PORT, port);
Robert Greenwaltb7090d62010-12-02 11:31:00 -08002071 Settings.Secure.putString(res, Settings.Secure.GLOBAL_HTTP_PROXY_EXCLUSION_LIST,
Robert Greenwalt434203a2010-10-11 16:00:27 -07002072 exclList);
2073 }
2074
2075 if (mGlobalProxy == null) {
2076 proxyProperties = mDefaultProxy;
2077 }
2078 sendProxyBroadcast(proxyProperties);
2079 }
2080
Robert Greenwaltb7090d62010-12-02 11:31:00 -08002081 private void loadGlobalProxy() {
2082 ContentResolver res = mContext.getContentResolver();
2083 String host = Settings.Secure.getString(res, Settings.Secure.GLOBAL_HTTP_PROXY_HOST);
2084 int port = Settings.Secure.getInt(res, Settings.Secure.GLOBAL_HTTP_PROXY_PORT, 0);
2085 String exclList = Settings.Secure.getString(res,
2086 Settings.Secure.GLOBAL_HTTP_PROXY_EXCLUSION_LIST);
2087 if (!TextUtils.isEmpty(host)) {
2088 ProxyProperties proxyProperties = new ProxyProperties(host, port, exclList);
2089 synchronized (mGlobalProxyLock) {
2090 mGlobalProxy = proxyProperties;
2091 }
2092 }
2093 }
2094
Robert Greenwalt434203a2010-10-11 16:00:27 -07002095 public ProxyProperties getGlobalProxy() {
2096 synchronized (mGlobalProxyLock) {
2097 return mGlobalProxy;
2098 }
2099 }
2100
2101 private void handleApplyDefaultProxy(int type) {
2102 // check if new default - push it out to all VM if so
2103 ProxyProperties proxy = mNetTrackers[type].getLinkProperties().getHttpProxy();
2104 synchronized (this) {
2105 if (mDefaultProxy != null && mDefaultProxy.equals(proxy)) return;
2106 if (mDefaultProxy == proxy) return;
2107 if (!TextUtils.isEmpty(proxy.getHost())) {
2108 mDefaultProxy = proxy;
2109 } else {
2110 mDefaultProxy = null;
2111 }
2112 }
Wink Savilleed9c02b2010-12-03 12:01:38 -08002113 if (DBG) log("changing default proxy to " + proxy);
Robert Greenwalt434203a2010-10-11 16:00:27 -07002114 if ((proxy == null && mGlobalProxy == null) || proxy.equals(mGlobalProxy)) return;
2115 if (mGlobalProxy != null) return;
2116 sendProxyBroadcast(proxy);
2117 }
2118
2119 private void handleDeprecatedGlobalHttpProxy() {
2120 String proxy = Settings.Secure.getString(mContext.getContentResolver(),
2121 Settings.Secure.HTTP_PROXY);
2122 if (!TextUtils.isEmpty(proxy)) {
2123 String data[] = proxy.split(":");
2124 String proxyHost = data[0];
2125 int proxyPort = 8080;
2126 if (data.length > 1) {
2127 try {
2128 proxyPort = Integer.parseInt(data[1]);
2129 } catch (NumberFormatException e) {
2130 return;
2131 }
2132 }
2133 ProxyProperties p = new ProxyProperties(data[0], proxyPort, "");
2134 setGlobalProxy(p);
2135 }
2136 }
2137
2138 private void sendProxyBroadcast(ProxyProperties proxy) {
Robert Greenwalt55985be2010-12-23 15:51:10 -08002139 if (proxy == null) proxy = new ProxyProperties("", 0, "");
Wink Savilleed9c02b2010-12-03 12:01:38 -08002140 log("sending Proxy Broadcast for " + proxy);
Robert Greenwalt434203a2010-10-11 16:00:27 -07002141 Intent intent = new Intent(Proxy.PROXY_CHANGE_ACTION);
Stan Chesnuttb35d67a2011-01-06 11:00:19 -08002142 intent.addFlags(Intent.FLAG_RECEIVER_REPLACE_PENDING |
2143 Intent.FLAG_RECEIVER_REGISTERED_ONLY_BEFORE_BOOT);
Robert Greenwalt434203a2010-10-11 16:00:27 -07002144 intent.putExtra(Proxy.EXTRA_PROXY_INFO, proxy);
Robert Greenwalta2e13392010-12-06 11:29:17 -08002145 mContext.sendStickyBroadcast(intent);
Robert Greenwalt434203a2010-10-11 16:00:27 -07002146 }
2147
2148 private static class SettingsObserver extends ContentObserver {
2149 private int mWhat;
2150 private Handler mHandler;
2151 SettingsObserver(Handler handler, int what) {
2152 super(handler);
2153 mHandler = handler;
2154 mWhat = what;
2155 }
2156
2157 void observe(Context context) {
2158 ContentResolver resolver = context.getContentResolver();
2159 resolver.registerContentObserver(Settings.Secure.getUriFor(
2160 Settings.Secure.HTTP_PROXY), false, this);
2161 }
2162
2163 @Override
2164 public void onChange(boolean selfChange) {
2165 mHandler.obtainMessage(mWhat).sendToTarget();
2166 }
2167 }
Wink Savilleed9c02b2010-12-03 12:01:38 -08002168
2169 private void log(String s) {
2170 Slog.d(TAG, s);
2171 }
2172
2173 private void loge(String s) {
2174 Slog.e(TAG, s);
2175 }
Wink Saville2b8bcfe2011-02-24 17:58:51 -08002176 int convertFeatureToNetworkType(String feature){
2177 int networkType = -1;
2178 if (TextUtils.equals(feature, Phone.FEATURE_ENABLE_MMS)) {
2179 networkType = ConnectivityManager.TYPE_MOBILE_MMS;
2180 } else if (TextUtils.equals(feature, Phone.FEATURE_ENABLE_SUPL)) {
2181 networkType = ConnectivityManager.TYPE_MOBILE_SUPL;
2182 } else if (TextUtils.equals(feature, Phone.FEATURE_ENABLE_DUN) ||
2183 TextUtils.equals(feature, Phone.FEATURE_ENABLE_DUN_ALWAYS)) {
2184 networkType = ConnectivityManager.TYPE_MOBILE_DUN;
2185 } else if (TextUtils.equals(feature, Phone.FEATURE_ENABLE_HIPRI)) {
2186 networkType = ConnectivityManager.TYPE_MOBILE_HIPRI;
2187 } else if (TextUtils.equals(feature, Phone.FEATURE_ENABLE_FOTA)) {
2188 networkType = ConnectivityManager.TYPE_MOBILE_FOTA;
2189 } else if (TextUtils.equals(feature, Phone.FEATURE_ENABLE_IMS)) {
2190 networkType = ConnectivityManager.TYPE_MOBILE_IMS;
2191 } else if (TextUtils.equals(feature, Phone.FEATURE_ENABLE_CBS)) {
2192 networkType = ConnectivityManager.TYPE_MOBILE_CBS;
2193 }
2194 return networkType;
2195 }
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002196}