blob: 0fa97a3812f9001e3725b8409ff8bf7138b19df0 [file] [log] [blame]
The Android Open Source Project28527d22009-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 Ganesh0db51a02010-12-21 22:31:44 -080019import android.bluetooth.BluetoothTetheringDataTracker;
The Android Open Source Project28527d22009-03-03 19:31:44 -080020import android.content.ContentResolver;
21import android.content.Context;
22import android.content.Intent;
23import android.content.pm.PackageManager;
Robert Greenwaltc3c5f862010-10-11 16:00:27 -070024import android.database.ContentObserver;
The Android Open Source Project28527d22009-03-03 19:31:44 -080025import android.net.ConnectivityManager;
Robert Greenwalteb123ac2010-12-06 13:56:24 -080026import android.net.DummyDataStateTracker;
The Android Open Source Project28527d22009-03-03 19:31:44 -080027import android.net.IConnectivityManager;
Jaikumar Ganesh0db51a02010-12-21 22:31:44 -080028import android.net.LinkProperties;
The Android Open Source Project28527d22009-03-03 19:31:44 -080029import android.net.MobileDataStateTracker;
30import android.net.NetworkInfo;
31import android.net.NetworkStateTracker;
Robert Greenwalt7fe44cb2010-08-27 09:24:29 -070032import android.net.NetworkUtils;
Robert Greenwaltc3c5f862010-10-11 16:00:27 -070033import android.net.Proxy;
34import android.net.ProxyProperties;
Hung-ying Tyan4e723422011-01-19 16:48:38 +080035import android.net.vpn.VpnManager;
The Android Open Source Project28527d22009-03-03 19:31:44 -080036import android.net.wifi.WifiStateTracker;
37import android.os.Binder;
38import android.os.Handler;
Wink Saville775aad62010-09-02 19:23:52 -070039import android.os.HandlerThread;
Robert Greenwalt2034b912009-08-12 16:08:25 -070040import android.os.IBinder;
The Android Open Source Project28527d22009-03-03 19:31:44 -080041import android.os.Looper;
42import android.os.Message;
Robert Greenwalt93dc1042010-06-15 12:19:37 -070043import android.os.PowerManager;
Robert Greenwalt2034b912009-08-12 16:08:25 -070044import android.os.RemoteException;
The Android Open Source Project28527d22009-03-03 19:31:44 -080045import android.os.ServiceManager;
46import android.os.SystemProperties;
47import android.provider.Settings;
Robert Greenwalt2034b912009-08-12 16:08:25 -070048import android.text.TextUtils;
The Android Open Source Project28527d22009-03-03 19:31:44 -080049import android.util.EventLog;
Joe Onoratoc2386bb2010-02-26 18:56:32 -080050import android.util.Slog;
The Android Open Source Project28527d22009-03-03 19:31:44 -080051
Robert Greenwalt2034b912009-08-12 16:08:25 -070052import com.android.internal.telephony.Phone;
Robert Greenwalt0c4828c2010-01-26 11:40:34 -080053import com.android.server.connectivity.Tethering;
54
The Android Open Source Project28527d22009-03-03 19:31:44 -080055import java.io.FileDescriptor;
Irfan Sheriff7f132d92010-06-09 15:39:36 -070056import java.io.FileWriter;
57import java.io.IOException;
The Android Open Source Project28527d22009-03-03 19:31:44 -080058import java.io.PrintWriter;
Robert Greenwalta7dfbd32010-06-15 15:43:39 -070059import java.net.InetAddress;
60import java.net.UnknownHostException;
Robert Greenwalt2034b912009-08-12 16:08:25 -070061import java.util.ArrayList;
Robert Greenwalta7dfbd32010-06-15 15:43:39 -070062import java.util.Collection;
Robert Greenwaltd62c7002010-12-29 16:15:02 -080063import java.util.concurrent.atomic.AtomicBoolean;
Robert Greenwalt0e80be12010-09-20 14:35:25 -070064import java.util.GregorianCalendar;
Robert Greenwalt2034b912009-08-12 16:08:25 -070065import java.util.List;
The Android Open Source Project28527d22009-03-03 19:31:44 -080066
67/**
68 * @hide
69 */
70public class ConnectivityService extends IConnectivityManager.Stub {
71
Robert Greenwalt063dc7d2010-10-05 19:12:26 -070072 private static final boolean DBG = true;
The Android Open Source Project28527d22009-03-03 19:31:44 -080073 private static final String TAG = "ConnectivityService";
74
Robert Greenwalt2034b912009-08-12 16:08:25 -070075 // how long to wait before switching back to a radio's default network
76 private static final int RESTORE_DEFAULT_NETWORK_DELAY = 1 * 60 * 1000;
77 // system property that can override the above value
78 private static final String NETWORK_RESTORE_DELAY_PROP_NAME =
79 "android.telephony.apn-restore";
80
Robert Greenwalt0c4828c2010-01-26 11:40:34 -080081 private Tethering mTethering;
Robert Greenwaltf1b66e12010-02-25 12:29:30 -080082 private boolean mTetheringConfigValid = false;
Robert Greenwalt0c4828c2010-01-26 11:40:34 -080083
The Android Open Source Project28527d22009-03-03 19:31:44 -080084 /**
85 * Sometimes we want to refer to the individual network state
86 * trackers separately, and sometimes we just want to treat them
87 * abstractly.
88 */
89 private NetworkStateTracker mNetTrackers[];
Robert Greenwalt2034b912009-08-12 16:08:25 -070090
91 /**
92 * A per Net list of the PID's that requested access to the net
93 * used both as a refcount and for per-PID DNS selection
94 */
95 private List mNetRequestersPids[];
96
Irfan Sheriff653e2a22010-06-07 09:03:04 -070097 private WifiWatchdogService mWifiWatchdogService;
98
Robert Greenwalt2034b912009-08-12 16:08:25 -070099 // priority order of the nettrackers
100 // (excluding dynamically set mNetworkPreference)
101 // TODO - move mNetworkTypePreference into this
102 private int[] mPriorityList;
103
The Android Open Source Project28527d22009-03-03 19:31:44 -0800104 private Context mContext;
105 private int mNetworkPreference;
Robert Greenwalt2034b912009-08-12 16:08:25 -0700106 private int mActiveDefaultNetwork = -1;
Robert Greenwalt986c7412010-09-08 15:24:47 -0700107 // 0 is full bad, 100 is full good
108 private int mDefaultInetCondition = 0;
109 private int mDefaultInetConditionPublished = 0;
110 private boolean mInetConditionChangeInFlight = false;
111 private int mDefaultConnectionSequence = 0;
The Android Open Source Project28527d22009-03-03 19:31:44 -0800112
113 private int mNumDnsEntries;
The Android Open Source Project28527d22009-03-03 19:31:44 -0800114
115 private boolean mTestMode;
Joe Onorato56023ad2010-09-01 21:18:22 -0700116 private static ConnectivityService sServiceInstance;
The Android Open Source Project28527d22009-03-03 19:31:44 -0800117
Robert Greenwaltd62c7002010-12-29 16:15:02 -0800118 private AtomicBoolean mBackgroundDataEnabled = new AtomicBoolean(true);
119
Robert Greenwalt6a2db8a2010-09-23 10:05:56 -0700120 private static final int ENABLED = 1;
121 private static final int DISABLED = 0;
122
123 // Share the event space with NetworkStateTracker (which can't see this
124 // internal class but sends us events). If you change these, change
125 // NetworkStateTracker.java too.
126 private static final int MIN_NETWORK_STATE_TRACKER_EVENT = 1;
127 private static final int MAX_NETWORK_STATE_TRACKER_EVENT = 100;
128
129 /**
130 * used internally as a delayed event to make us switch back to the
131 * default network
132 */
133 private static final int EVENT_RESTORE_DEFAULT_NETWORK =
134 MAX_NETWORK_STATE_TRACKER_EVENT + 1;
135
136 /**
137 * used internally to change our mobile data enabled flag
138 */
139 private static final int EVENT_CHANGE_MOBILE_DATA_ENABLED =
140 MAX_NETWORK_STATE_TRACKER_EVENT + 2;
141
142 /**
143 * used internally to change our network preference setting
144 * arg1 = networkType to prefer
145 */
146 private static final int EVENT_SET_NETWORK_PREFERENCE =
147 MAX_NETWORK_STATE_TRACKER_EVENT + 3;
148
149 /**
150 * used internally to synchronize inet condition reports
151 * arg1 = networkType
152 * arg2 = condition (0 bad, 100 good)
153 */
154 private static final int EVENT_INET_CONDITION_CHANGE =
155 MAX_NETWORK_STATE_TRACKER_EVENT + 4;
156
157 /**
158 * used internally to mark the end of inet condition hold periods
159 * arg1 = networkType
160 */
161 private static final int EVENT_INET_CONDITION_HOLD_END =
162 MAX_NETWORK_STATE_TRACKER_EVENT + 5;
163
164 /**
165 * used internally to set the background data preference
166 * arg1 = TRUE for enabled, FALSE for disabled
167 */
168 private static final int EVENT_SET_BACKGROUND_DATA =
169 MAX_NETWORK_STATE_TRACKER_EVENT + 6;
170
171 /**
172 * used internally to set enable/disable cellular data
173 * arg1 = ENBALED or DISABLED
174 */
175 private static final int EVENT_SET_MOBILE_DATA =
176 MAX_NETWORK_STATE_TRACKER_EVENT + 7;
177
Robert Greenwaltccb36f92010-09-24 14:32:21 -0700178 /**
179 * used internally to clear a wakelock when transitioning
180 * from one net to another
181 */
182 private static final int EVENT_CLEAR_NET_TRANSITION_WAKELOCK =
183 MAX_NETWORK_STATE_TRACKER_EVENT + 8;
184
Robert Greenwaltc3c5f862010-10-11 16:00:27 -0700185 /**
186 * used internally to reload global proxy settings
187 */
188 private static final int EVENT_APPLY_GLOBAL_HTTP_PROXY =
189 MAX_NETWORK_STATE_TRACKER_EVENT + 9;
190
Robert Greenwalt2034b912009-08-12 16:08:25 -0700191 private Handler mHandler;
192
193 // list of DeathRecipients used to make sure features are turned off when
194 // a process dies
195 private List mFeatureUsers;
196
Mike Lockwoodfde2b762009-08-14 14:18:49 -0400197 private boolean mSystemReady;
Dianne Hackborna417ff82009-12-08 19:45:14 -0800198 private Intent mInitialBroadcast;
Mike Lockwoodfde2b762009-08-14 14:18:49 -0400199
Robert Greenwalt93dc1042010-06-15 12:19:37 -0700200 private PowerManager.WakeLock mNetTransitionWakeLock;
201 private String mNetTransitionWakeLockCausedBy = "";
202 private int mNetTransitionWakeLockSerialNumber;
203 private int mNetTransitionWakeLockTimeout;
204
Robert Greenwalt94daa182010-09-01 11:34:05 -0700205 private InetAddress mDefaultDns;
206
Robert Greenwalt0e80be12010-09-20 14:35:25 -0700207 // used in DBG mode to track inet condition reports
208 private static final int INET_CONDITION_LOG_MAX_SIZE = 15;
209 private ArrayList mInetLog;
210
Robert Greenwaltc3c5f862010-10-11 16:00:27 -0700211 // track the current default http proxy - tell the world if we get a new one (real change)
212 private ProxyProperties mDefaultProxy = null;
213 // track the global proxy.
214 private ProxyProperties mGlobalProxy = null;
215 private final Object mGlobalProxyLock = new Object();
216
217 private SettingsObserver mSettingsObserver;
218
Robert Greenwalt12c44552009-12-07 11:33:18 -0800219 private static class NetworkAttributes {
Robert Greenwalt2034b912009-08-12 16:08:25 -0700220 /**
221 * Class for holding settings read from resources.
222 */
223 public String mName;
224 public int mType;
225 public int mRadio;
226 public int mPriority;
Robert Greenwalt12c44552009-12-07 11:33:18 -0800227 public NetworkInfo.State mLastState;
Robert Greenwalt2034b912009-08-12 16:08:25 -0700228 public NetworkAttributes(String init) {
229 String fragments[] = init.split(",");
230 mName = fragments[0].toLowerCase();
Robert Greenwaltec05b3c2009-10-30 14:17:42 -0700231 mType = Integer.parseInt(fragments[1]);
232 mRadio = Integer.parseInt(fragments[2]);
233 mPriority = Integer.parseInt(fragments[3]);
Robert Greenwalt12c44552009-12-07 11:33:18 -0800234 mLastState = NetworkInfo.State.UNKNOWN;
Robert Greenwalt2034b912009-08-12 16:08:25 -0700235 }
236 public boolean isDefault() {
237 return (mType == mRadio);
238 }
239 }
240 NetworkAttributes[] mNetAttributes;
Robert Greenwaltec05b3c2009-10-30 14:17:42 -0700241 int mNetworksDefined;
Robert Greenwalt2034b912009-08-12 16:08:25 -0700242
Robert Greenwalt12c44552009-12-07 11:33:18 -0800243 private static class RadioAttributes {
Robert Greenwalt2034b912009-08-12 16:08:25 -0700244 public int mSimultaneity;
245 public int mType;
246 public RadioAttributes(String init) {
247 String fragments[] = init.split(",");
Robert Greenwaltec05b3c2009-10-30 14:17:42 -0700248 mType = Integer.parseInt(fragments[0]);
249 mSimultaneity = Integer.parseInt(fragments[1]);
Robert Greenwalt2034b912009-08-12 16:08:25 -0700250 }
251 }
252 RadioAttributes[] mRadioAttributes;
253
Wink Saville775aad62010-09-02 19:23:52 -0700254 public static synchronized ConnectivityService getInstance(Context context) {
255 if (sServiceInstance == null) {
256 sServiceInstance = new ConnectivityService(context);
The Android Open Source Project28527d22009-03-03 19:31:44 -0800257 }
Wink Saville775aad62010-09-02 19:23:52 -0700258 return sServiceInstance;
The Android Open Source Project28527d22009-03-03 19:31:44 -0800259 }
Robert Greenwalt0659da32009-07-16 17:21:39 -0700260
The Android Open Source Project28527d22009-03-03 19:31:44 -0800261 private ConnectivityService(Context context) {
Wink Savillee70c6f52010-12-03 12:01:38 -0800262 if (DBG) log("ConnectivityService starting up");
Robert Greenwaltd48f8ee2010-01-14 17:47:58 -0800263
Wink Saville775aad62010-09-02 19:23:52 -0700264 HandlerThread handlerThread = new HandlerThread("ConnectivityServiceThread");
265 handlerThread.start();
266 mHandler = new MyHandler(handlerThread.getLooper());
267
Robert Greenwaltd62c7002010-12-29 16:15:02 -0800268 mBackgroundDataEnabled.set(Settings.Secure.getInt(context.getContentResolver(),
269 Settings.Secure.BACKGROUND_DATA, 1) == 1);
270
Robert Greenwaltd48f8ee2010-01-14 17:47:58 -0800271 // setup our unique device name
Robert Greenwalt82cde132010-12-06 09:30:17 -0800272 if (TextUtils.isEmpty(SystemProperties.get("net.hostname"))) {
273 String id = Settings.Secure.getString(context.getContentResolver(),
274 Settings.Secure.ANDROID_ID);
275 if (id != null && id.length() > 0) {
276 String name = new String("android_").concat(id);
277 SystemProperties.set("net.hostname", name);
278 }
Robert Greenwaltd48f8ee2010-01-14 17:47:58 -0800279 }
280
Robert Greenwalt94daa182010-09-01 11:34:05 -0700281 // read our default dns server ip
282 String dns = Settings.Secure.getString(context.getContentResolver(),
283 Settings.Secure.DEFAULT_DNS_SERVER);
284 if (dns == null || dns.length() == 0) {
285 dns = context.getResources().getString(
286 com.android.internal.R.string.config_default_dns_server);
287 }
288 try {
289 mDefaultDns = InetAddress.getByName(dns);
290 } catch (UnknownHostException e) {
Wink Savillee70c6f52010-12-03 12:01:38 -0800291 loge("Error setting defaultDns using " + dns);
Robert Greenwalt94daa182010-09-01 11:34:05 -0700292 }
293
The Android Open Source Project28527d22009-03-03 19:31:44 -0800294 mContext = context;
Robert Greenwalt93dc1042010-06-15 12:19:37 -0700295
296 PowerManager powerManager = (PowerManager)mContext.getSystemService(Context.POWER_SERVICE);
297 mNetTransitionWakeLock = powerManager.newWakeLock(PowerManager.PARTIAL_WAKE_LOCK, TAG);
298 mNetTransitionWakeLockTimeout = mContext.getResources().getInteger(
299 com.android.internal.R.integer.config_networkTransitionTimeout);
300
Robert Greenwalt2034b912009-08-12 16:08:25 -0700301 mNetTrackers = new NetworkStateTracker[
302 ConnectivityManager.MAX_NETWORK_TYPE+1];
Robert Greenwalt0659da32009-07-16 17:21:39 -0700303
The Android Open Source Project28527d22009-03-03 19:31:44 -0800304 mNetworkPreference = getPersistedNetworkPreference();
Robert Greenwalt0659da32009-07-16 17:21:39 -0700305
Robert Greenwaltec05b3c2009-10-30 14:17:42 -0700306 mRadioAttributes = new RadioAttributes[ConnectivityManager.MAX_RADIO_TYPE+1];
307 mNetAttributes = new NetworkAttributes[ConnectivityManager.MAX_NETWORK_TYPE+1];
308
Robert Greenwalt2034b912009-08-12 16:08:25 -0700309 // Load device network attributes from resources
Robert Greenwalt2034b912009-08-12 16:08:25 -0700310 String[] raStrings = context.getResources().getStringArray(
311 com.android.internal.R.array.radioAttributes);
Robert Greenwaltec05b3c2009-10-30 14:17:42 -0700312 for (String raString : raStrings) {
313 RadioAttributes r = new RadioAttributes(raString);
314 if (r.mType > ConnectivityManager.MAX_RADIO_TYPE) {
Wink Savillee70c6f52010-12-03 12:01:38 -0800315 loge("Error in radioAttributes - ignoring attempt to define type " + r.mType);
Robert Greenwaltec05b3c2009-10-30 14:17:42 -0700316 continue;
317 }
318 if (mRadioAttributes[r.mType] != null) {
Wink Savillee70c6f52010-12-03 12:01:38 -0800319 loge("Error in radioAttributes - ignoring attempt to redefine type " +
Robert Greenwaltec05b3c2009-10-30 14:17:42 -0700320 r.mType);
321 continue;
322 }
Robert Greenwalt2034b912009-08-12 16:08:25 -0700323 mRadioAttributes[r.mType] = r;
324 }
325
Robert Greenwaltec05b3c2009-10-30 14:17:42 -0700326 String[] naStrings = context.getResources().getStringArray(
327 com.android.internal.R.array.networkAttributes);
328 for (String naString : naStrings) {
329 try {
330 NetworkAttributes n = new NetworkAttributes(naString);
331 if (n.mType > ConnectivityManager.MAX_NETWORK_TYPE) {
Wink Savillee70c6f52010-12-03 12:01:38 -0800332 loge("Error in networkAttributes - ignoring attempt to define type " +
Robert Greenwaltec05b3c2009-10-30 14:17:42 -0700333 n.mType);
334 continue;
Robert Greenwalt2034b912009-08-12 16:08:25 -0700335 }
Robert Greenwaltec05b3c2009-10-30 14:17:42 -0700336 if (mNetAttributes[n.mType] != null) {
Wink Savillee70c6f52010-12-03 12:01:38 -0800337 loge("Error in networkAttributes - ignoring attempt to redefine type " +
Robert Greenwaltec05b3c2009-10-30 14:17:42 -0700338 n.mType);
339 continue;
340 }
341 if (mRadioAttributes[n.mRadio] == null) {
Wink Savillee70c6f52010-12-03 12:01:38 -0800342 loge("Error in networkAttributes - ignoring attempt to use undefined " +
Robert Greenwaltec05b3c2009-10-30 14:17:42 -0700343 "radio " + n.mRadio + " in network type " + n.mType);
344 continue;
345 }
346 mNetAttributes[n.mType] = n;
347 mNetworksDefined++;
348 } catch(Exception e) {
349 // ignore it - leave the entry null
Robert Greenwalt2034b912009-08-12 16:08:25 -0700350 }
351 }
352
Robert Greenwaltec05b3c2009-10-30 14:17:42 -0700353 // high priority first
354 mPriorityList = new int[mNetworksDefined];
355 {
356 int insertionPoint = mNetworksDefined-1;
357 int currentLowest = 0;
358 int nextLowest = 0;
359 while (insertionPoint > -1) {
360 for (NetworkAttributes na : mNetAttributes) {
361 if (na == null) continue;
362 if (na.mPriority < currentLowest) continue;
363 if (na.mPriority > currentLowest) {
364 if (na.mPriority < nextLowest || nextLowest == 0) {
365 nextLowest = na.mPriority;
366 }
367 continue;
368 }
369 mPriorityList[insertionPoint--] = na.mType;
370 }
371 currentLowest = nextLowest;
372 nextLowest = 0;
373 }
374 }
375
376 mNetRequestersPids = new ArrayList[ConnectivityManager.MAX_NETWORK_TYPE+1];
377 for (int i : mPriorityList) {
Robert Greenwalt2034b912009-08-12 16:08:25 -0700378 mNetRequestersPids[i] = new ArrayList();
379 }
380
381 mFeatureUsers = new ArrayList();
382
Robert Greenwaltec05b3c2009-10-30 14:17:42 -0700383 mNumDnsEntries = 0;
384
385 mTestMode = SystemProperties.get("cm.test.mode").equals("true")
386 && SystemProperties.get("ro.build.type").equals("eng");
The Android Open Source Project28527d22009-03-03 19:31:44 -0800387 /*
388 * Create the network state trackers for Wi-Fi and mobile
389 * data. Maybe this could be done with a factory class,
390 * but it's not clear that it's worth it, given that
391 * the number of different network types is not going
392 * to change very often.
393 */
Robert Greenwaltec05b3c2009-10-30 14:17:42 -0700394 for (int netType : mPriorityList) {
395 switch (mNetAttributes[netType].mRadio) {
396 case ConnectivityManager.TYPE_WIFI:
Wink Savillee70c6f52010-12-03 12:01:38 -0800397 if (DBG) log("Starting Wifi Service.");
Wink Saville7fabfa22010-08-13 16:11:42 -0700398 WifiStateTracker wst = new WifiStateTracker();
Irfan Sheriff25be0762010-07-28 09:35:20 -0700399 WifiService wifiService = new WifiService(context);
Robert Greenwaltec05b3c2009-10-30 14:17:42 -0700400 ServiceManager.addService(Context.WIFI_SERVICE, wifiService);
Irfan Sheriff25be0762010-07-28 09:35:20 -0700401 wifiService.checkAndStartWifi();
Robert Greenwaltec05b3c2009-10-30 14:17:42 -0700402 mNetTrackers[ConnectivityManager.TYPE_WIFI] = wst;
Wink Saville7fabfa22010-08-13 16:11:42 -0700403 wst.startMonitoring(context, mHandler);
The Android Open Source Project28527d22009-03-03 19:31:44 -0800404
Irfan Sheriff653e2a22010-06-07 09:03:04 -0700405 //TODO: as part of WWS refactor, create only when needed
Irfan Sheriff25be0762010-07-28 09:35:20 -0700406 mWifiWatchdogService = new WifiWatchdogService(context);
Irfan Sheriff653e2a22010-06-07 09:03:04 -0700407
Robert Greenwaltec05b3c2009-10-30 14:17:42 -0700408 break;
409 case ConnectivityManager.TYPE_MOBILE:
Wink Saville7fabfa22010-08-13 16:11:42 -0700410 mNetTrackers[netType] = new MobileDataStateTracker(netType,
411 mNetAttributes[netType].mName);
412 mNetTrackers[netType].startMonitoring(context, mHandler);
Robert Greenwaltec05b3c2009-10-30 14:17:42 -0700413 break;
Robert Greenwalteb123ac2010-12-06 13:56:24 -0800414 case ConnectivityManager.TYPE_DUMMY:
415 mNetTrackers[netType] = new DummyDataStateTracker(netType,
416 mNetAttributes[netType].mName);
417 mNetTrackers[netType].startMonitoring(context, mHandler);
418 break;
Jaikumar Ganesh0db51a02010-12-21 22:31:44 -0800419 case ConnectivityManager.TYPE_BLUETOOTH:
420 mNetTrackers[netType] = BluetoothTetheringDataTracker.getInstance();
421 mNetTrackers[netType].startMonitoring(context, mHandler);
422 break;
Robert Greenwaltec05b3c2009-10-30 14:17:42 -0700423 default:
Wink Savillee70c6f52010-12-03 12:01:38 -0800424 loge("Trying to create a DataStateTracker for an unknown radio type " +
Robert Greenwaltec05b3c2009-10-30 14:17:42 -0700425 mNetAttributes[netType].mRadio);
426 continue;
427 }
428 }
Robert Greenwalt0c4828c2010-01-26 11:40:34 -0800429
Robert Greenwaltc0b6c602010-03-11 15:03:08 -0800430 mTethering = new Tethering(mContext, mHandler.getLooper());
Robert Greenwaltf1b66e12010-02-25 12:29:30 -0800431 mTetheringConfigValid = (((mNetTrackers[ConnectivityManager.TYPE_MOBILE_DUN] != null) ||
432 !mTethering.isDunRequired()) &&
433 (mTethering.getTetherableUsbRegexs().length != 0 ||
Danica Chang96567052010-08-11 14:54:43 -0700434 mTethering.getTetherableWifiRegexs().length != 0 ||
435 mTethering.getTetherableBluetoothRegexs().length != 0) &&
Robert Greenwaltf1b66e12010-02-25 12:29:30 -0800436 mTethering.getUpstreamIfaceRegexs().length != 0);
437
Robert Greenwalt0e80be12010-09-20 14:35:25 -0700438 if (DBG) {
439 mInetLog = new ArrayList();
440 }
Robert Greenwaltc3c5f862010-10-11 16:00:27 -0700441
442 mSettingsObserver = new SettingsObserver(mHandler, EVENT_APPLY_GLOBAL_HTTP_PROXY);
443 mSettingsObserver.observe(mContext);
Robert Greenwalt6f7c6092010-12-02 11:31:00 -0800444
445 loadGlobalProxy();
Hung-ying Tyan4e723422011-01-19 16:48:38 +0800446
447 VpnManager.startVpnService(context);
The Android Open Source Project28527d22009-03-03 19:31:44 -0800448 }
449
Robert Greenwaltec05b3c2009-10-30 14:17:42 -0700450
The Android Open Source Project28527d22009-03-03 19:31:44 -0800451 /**
Robert Greenwalt0659da32009-07-16 17:21:39 -0700452 * Sets the preferred network.
The Android Open Source Project28527d22009-03-03 19:31:44 -0800453 * @param preference the new preference
454 */
Robert Greenwalt6a2db8a2010-09-23 10:05:56 -0700455 public void setNetworkPreference(int preference) {
The Android Open Source Project28527d22009-03-03 19:31:44 -0800456 enforceChangePermission();
Robert Greenwalt6a2db8a2010-09-23 10:05:56 -0700457
458 mHandler.sendMessage(mHandler.obtainMessage(EVENT_SET_NETWORK_PREFERENCE, preference, 0));
The Android Open Source Project28527d22009-03-03 19:31:44 -0800459 }
460
461 public int getNetworkPreference() {
462 enforceAccessPermission();
Robert Greenwalt6a2db8a2010-09-23 10:05:56 -0700463 int preference;
464 synchronized(this) {
465 preference = mNetworkPreference;
466 }
467 return preference;
The Android Open Source Project28527d22009-03-03 19:31:44 -0800468 }
469
Robert Greenwalt6a2db8a2010-09-23 10:05:56 -0700470 private void handleSetNetworkPreference(int preference) {
471 if (ConnectivityManager.isNetworkTypeValid(preference) &&
472 mNetAttributes[preference] != null &&
473 mNetAttributes[preference].isDefault()) {
474 if (mNetworkPreference != preference) {
475 final ContentResolver cr = mContext.getContentResolver();
476 Settings.Secure.putInt(cr, Settings.Secure.NETWORK_PREFERENCE, preference);
477 synchronized(this) {
478 mNetworkPreference = preference;
479 }
480 enforcePreference();
481 }
482 }
The Android Open Source Project28527d22009-03-03 19:31:44 -0800483 }
Robert Greenwalt0659da32009-07-16 17:21:39 -0700484
The Android Open Source Project28527d22009-03-03 19:31:44 -0800485 private int getPersistedNetworkPreference() {
486 final ContentResolver cr = mContext.getContentResolver();
487
488 final int networkPrefSetting = Settings.Secure
489 .getInt(cr, Settings.Secure.NETWORK_PREFERENCE, -1);
490 if (networkPrefSetting != -1) {
491 return networkPrefSetting;
492 }
493
494 return ConnectivityManager.DEFAULT_NETWORK_PREFERENCE;
495 }
Robert Greenwalt0659da32009-07-16 17:21:39 -0700496
The Android Open Source Project28527d22009-03-03 19:31:44 -0800497 /**
Robert Greenwalt0659da32009-07-16 17:21:39 -0700498 * Make the state of network connectivity conform to the preference settings
The Android Open Source Project28527d22009-03-03 19:31:44 -0800499 * In this method, we only tear down a non-preferred network. Establishing
500 * a connection to the preferred network is taken care of when we handle
501 * the disconnect event from the non-preferred network
502 * (see {@link #handleDisconnect(NetworkInfo)}).
503 */
504 private void enforcePreference() {
Robert Greenwalt2034b912009-08-12 16:08:25 -0700505 if (mNetTrackers[mNetworkPreference].getNetworkInfo().isConnected())
The Android Open Source Project28527d22009-03-03 19:31:44 -0800506 return;
507
Robert Greenwalt2034b912009-08-12 16:08:25 -0700508 if (!mNetTrackers[mNetworkPreference].isAvailable())
509 return;
The Android Open Source Project28527d22009-03-03 19:31:44 -0800510
Robert Greenwalt2034b912009-08-12 16:08:25 -0700511 for (int t=0; t <= ConnectivityManager.MAX_RADIO_TYPE; t++) {
Robert Greenwaltec05b3c2009-10-30 14:17:42 -0700512 if (t != mNetworkPreference && mNetTrackers[t] != null &&
Robert Greenwalt2034b912009-08-12 16:08:25 -0700513 mNetTrackers[t].getNetworkInfo().isConnected()) {
Robert Greenwaltf3f045b2009-08-20 15:25:14 -0700514 if (DBG) {
Wink Savillee70c6f52010-12-03 12:01:38 -0800515 log("tearing down " + mNetTrackers[t].getNetworkInfo() +
Robert Greenwaltf3f045b2009-08-20 15:25:14 -0700516 " in enforcePreference");
517 }
Robert Greenwalt2034b912009-08-12 16:08:25 -0700518 teardown(mNetTrackers[t]);
The Android Open Source Project28527d22009-03-03 19:31:44 -0800519 }
520 }
521 }
522
523 private boolean teardown(NetworkStateTracker netTracker) {
524 if (netTracker.teardown()) {
525 netTracker.setTeardownRequested(true);
526 return true;
527 } else {
528 return false;
529 }
530 }
531
532 /**
533 * Return NetworkInfo for the active (i.e., connected) network interface.
534 * It is assumed that at most one network is active at a time. If more
535 * than one is active, it is indeterminate which will be returned.
Robert Greenwalt0659da32009-07-16 17:21:39 -0700536 * @return the info for the active network, or {@code null} if none is
537 * active
The Android Open Source Project28527d22009-03-03 19:31:44 -0800538 */
539 public NetworkInfo getActiveNetworkInfo() {
540 enforceAccessPermission();
Robert Greenwalt2034b912009-08-12 16:08:25 -0700541 for (int type=0; type <= ConnectivityManager.MAX_NETWORK_TYPE; type++) {
Robert Greenwaltec05b3c2009-10-30 14:17:42 -0700542 if (mNetAttributes[type] == null || !mNetAttributes[type].isDefault()) {
Robert Greenwalt2034b912009-08-12 16:08:25 -0700543 continue;
544 }
545 NetworkStateTracker t = mNetTrackers[type];
The Android Open Source Project28527d22009-03-03 19:31:44 -0800546 NetworkInfo info = t.getNetworkInfo();
547 if (info.isConnected()) {
Wink Savillee70c6f52010-12-03 12:01:38 -0800548 if (DBG && type != mActiveDefaultNetwork) {
549 loge("connected default network is not mActiveDefaultNetwork!");
550 }
The Android Open Source Project28527d22009-03-03 19:31:44 -0800551 return info;
552 }
553 }
554 return null;
555 }
556
557 public NetworkInfo getNetworkInfo(int networkType) {
558 enforceAccessPermission();
559 if (ConnectivityManager.isNetworkTypeValid(networkType)) {
560 NetworkStateTracker t = mNetTrackers[networkType];
561 if (t != null)
562 return t.getNetworkInfo();
563 }
564 return null;
565 }
566
567 public NetworkInfo[] getAllNetworkInfo() {
568 enforceAccessPermission();
Robert Greenwaltec05b3c2009-10-30 14:17:42 -0700569 NetworkInfo[] result = new NetworkInfo[mNetworksDefined];
The Android Open Source Project28527d22009-03-03 19:31:44 -0800570 int i = 0;
571 for (NetworkStateTracker t : mNetTrackers) {
Robert Greenwaltec05b3c2009-10-30 14:17:42 -0700572 if(t != null) result[i++] = t.getNetworkInfo();
The Android Open Source Project28527d22009-03-03 19:31:44 -0800573 }
574 return result;
575 }
576
Robert Greenwalt9f0ee4f2010-09-14 09:18:02 -0700577 /**
578 * Return LinkProperties for the active (i.e., connected) default
579 * network interface. It is assumed that at most one default network
580 * is active at a time. If more than one is active, it is indeterminate
581 * which will be returned.
582 * @return the ip properties for the active network, or {@code null} if
583 * none is active
584 */
585 public LinkProperties getActiveLinkProperties() {
586 enforceAccessPermission();
587 for (int type=0; type <= ConnectivityManager.MAX_NETWORK_TYPE; type++) {
588 if (mNetAttributes[type] == null || !mNetAttributes[type].isDefault()) {
589 continue;
590 }
591 NetworkStateTracker t = mNetTrackers[type];
592 NetworkInfo info = t.getNetworkInfo();
593 if (info.isConnected()) {
594 return t.getLinkProperties();
595 }
596 }
597 return null;
598 }
599
600 public LinkProperties getLinkProperties(int networkType) {
601 enforceAccessPermission();
602 if (ConnectivityManager.isNetworkTypeValid(networkType)) {
603 NetworkStateTracker t = mNetTrackers[networkType];
604 if (t != null) return t.getLinkProperties();
605 }
606 return null;
607 }
608
The Android Open Source Project28527d22009-03-03 19:31:44 -0800609 public boolean setRadios(boolean turnOn) {
610 boolean result = true;
611 enforceChangePermission();
612 for (NetworkStateTracker t : mNetTrackers) {
Robert Greenwaltec05b3c2009-10-30 14:17:42 -0700613 if (t != null) result = t.setRadio(turnOn) && result;
The Android Open Source Project28527d22009-03-03 19:31:44 -0800614 }
615 return result;
616 }
617
618 public boolean setRadio(int netType, boolean turnOn) {
619 enforceChangePermission();
620 if (!ConnectivityManager.isNetworkTypeValid(netType)) {
621 return false;
622 }
623 NetworkStateTracker tracker = mNetTrackers[netType];
624 return tracker != null && tracker.setRadio(turnOn);
625 }
626
Robert Greenwaltaffc3a12009-09-27 17:27:04 -0700627 /**
628 * Used to notice when the calling process dies so we can self-expire
629 *
630 * Also used to know if the process has cleaned up after itself when
631 * our auto-expire timer goes off. The timer has a link to an object.
632 *
633 */
Robert Greenwalt2034b912009-08-12 16:08:25 -0700634 private class FeatureUser implements IBinder.DeathRecipient {
635 int mNetworkType;
636 String mFeature;
637 IBinder mBinder;
638 int mPid;
639 int mUid;
Robert Greenwalt3eeb6032009-12-21 18:24:07 -0800640 long mCreateTime;
Robert Greenwalt2034b912009-08-12 16:08:25 -0700641
642 FeatureUser(int type, String feature, IBinder binder) {
643 super();
644 mNetworkType = type;
645 mFeature = feature;
646 mBinder = binder;
647 mPid = getCallingPid();
648 mUid = getCallingUid();
Robert Greenwalt3eeb6032009-12-21 18:24:07 -0800649 mCreateTime = System.currentTimeMillis();
Robert Greenwaltaffc3a12009-09-27 17:27:04 -0700650
Robert Greenwalt2034b912009-08-12 16:08:25 -0700651 try {
652 mBinder.linkToDeath(this, 0);
653 } catch (RemoteException e) {
654 binderDied();
655 }
656 }
657
658 void unlinkDeathRecipient() {
659 mBinder.unlinkToDeath(this, 0);
660 }
661
662 public void binderDied() {
Wink Savillee70c6f52010-12-03 12:01:38 -0800663 log("ConnectivityService FeatureUser binderDied(" +
Robert Greenwalt3eeb6032009-12-21 18:24:07 -0800664 mNetworkType + ", " + mFeature + ", " + mBinder + "), created " +
665 (System.currentTimeMillis() - mCreateTime) + " mSec ago");
Robert Greenwaltaffc3a12009-09-27 17:27:04 -0700666 stopUsingNetworkFeature(this, false);
Robert Greenwalt2034b912009-08-12 16:08:25 -0700667 }
668
Robert Greenwaltaffc3a12009-09-27 17:27:04 -0700669 public void expire() {
Wink Savillee70c6f52010-12-03 12:01:38 -0800670 log("ConnectivityService FeatureUser expire(" +
Robert Greenwalt3eeb6032009-12-21 18:24:07 -0800671 mNetworkType + ", " + mFeature + ", " + mBinder +"), created " +
672 (System.currentTimeMillis() - mCreateTime) + " mSec ago");
Robert Greenwaltaffc3a12009-09-27 17:27:04 -0700673 stopUsingNetworkFeature(this, false);
674 }
Robert Greenwalt3eeb6032009-12-21 18:24:07 -0800675
676 public String toString() {
677 return "FeatureUser("+mNetworkType+","+mFeature+","+mPid+","+mUid+"), created " +
678 (System.currentTimeMillis() - mCreateTime) + " mSec ago";
679 }
Robert Greenwalt2034b912009-08-12 16:08:25 -0700680 }
681
Robert Greenwaltaffc3a12009-09-27 17:27:04 -0700682 // javadoc from interface
Robert Greenwalt2034b912009-08-12 16:08:25 -0700683 public int startUsingNetworkFeature(int networkType, String feature,
684 IBinder binder) {
685 if (DBG) {
Wink Savillee70c6f52010-12-03 12:01:38 -0800686 log("startUsingNetworkFeature for net " + networkType + ": " + feature);
Robert Greenwalt2034b912009-08-12 16:08:25 -0700687 }
The Android Open Source Project28527d22009-03-03 19:31:44 -0800688 enforceChangePermission();
Robert Greenwaltec05b3c2009-10-30 14:17:42 -0700689 if (!ConnectivityManager.isNetworkTypeValid(networkType) ||
690 mNetAttributes[networkType] == null) {
Robert Greenwalt2034b912009-08-12 16:08:25 -0700691 return Phone.APN_REQUEST_FAILED;
The Android Open Source Project28527d22009-03-03 19:31:44 -0800692 }
Robert Greenwalt2034b912009-08-12 16:08:25 -0700693
Robert Greenwaltaffc3a12009-09-27 17:27:04 -0700694 FeatureUser f = new FeatureUser(networkType, feature, binder);
Robert Greenwalt2034b912009-08-12 16:08:25 -0700695
696 // TODO - move this into the MobileDataStateTracker
697 int usedNetworkType = networkType;
698 if(networkType == ConnectivityManager.TYPE_MOBILE) {
Wink Savillef6b76692011-02-24 17:58:51 -0800699 usedNetworkType = convertFeatureToNetworkType(feature);
700 if (usedNetworkType < 0) {
701 Slog.e(TAG, "Can't match any netTracker!");
702 usedNetworkType = networkType;
Robert Greenwalt2034b912009-08-12 16:08:25 -0700703 }
704 }
705 NetworkStateTracker network = mNetTrackers[usedNetworkType];
706 if (network != null) {
Robert Greenwalt5364d752010-12-15 13:26:33 -0800707 Integer currentPid = new Integer(getCallingPid());
Robert Greenwalt2034b912009-08-12 16:08:25 -0700708 if (usedNetworkType != networkType) {
Robert Greenwalt2034b912009-08-12 16:08:25 -0700709 NetworkStateTracker radio = mNetTrackers[networkType];
710 NetworkInfo ni = network.getNetworkInfo();
711
712 if (ni.isAvailable() == false) {
Wink Savillee70c6f52010-12-03 12:01:38 -0800713 if (DBG) log("special network not available");
Robert Greenwalt2cc87442010-12-29 14:35:21 -0800714 if (!TextUtils.equals(feature,Phone.FEATURE_ENABLE_DUN_ALWAYS)) {
715 return Phone.APN_TYPE_NOT_AVAILABLE;
716 } else {
717 // else make the attempt anyway - probably giving REQUEST_STARTED below
718 }
Robert Greenwalt2034b912009-08-12 16:08:25 -0700719 }
720
Robert Greenwaltaffc3a12009-09-27 17:27:04 -0700721 synchronized(this) {
722 mFeatureUsers.add(f);
723 if (!mNetRequestersPids[usedNetworkType].contains(currentPid)) {
724 // this gets used for per-pid dns when connected
725 mNetRequestersPids[usedNetworkType].add(currentPid);
726 }
Robert Greenwalt2034b912009-08-12 16:08:25 -0700727 }
Robert Greenwalt6a2db8a2010-09-23 10:05:56 -0700728 mHandler.sendMessageDelayed(mHandler.obtainMessage(EVENT_RESTORE_DEFAULT_NETWORK,
Robert Greenwaltaffc3a12009-09-27 17:27:04 -0700729 f), getRestoreDefaultNetworkDelay());
730
Robert Greenwalt2034b912009-08-12 16:08:25 -0700731
Robert Greenwalta52c75a2009-08-19 20:19:33 -0700732 if ((ni.isConnectedOrConnecting() == true) &&
733 !network.isTeardownRequested()) {
Robert Greenwalt2034b912009-08-12 16:08:25 -0700734 if (ni.isConnected() == true) {
735 // add the pid-specific dns
Robert Greenwalt3afbead2010-07-23 15:46:26 -0700736 handleDnsConfigurationChange(networkType);
Wink Savillee70c6f52010-12-03 12:01:38 -0800737 if (DBG) log("special network already active");
Robert Greenwalt2034b912009-08-12 16:08:25 -0700738 return Phone.APN_ALREADY_ACTIVE;
739 }
Wink Savillee70c6f52010-12-03 12:01:38 -0800740 if (DBG) log("special network already connecting");
Robert Greenwalt2034b912009-08-12 16:08:25 -0700741 return Phone.APN_REQUEST_STARTED;
742 }
743
744 // check if the radio in play can make another contact
745 // assume if cannot for now
746
Wink Savillee70c6f52010-12-03 12:01:38 -0800747 if (DBG) log("reconnecting to special network");
Robert Greenwalt2034b912009-08-12 16:08:25 -0700748 network.reconnect();
749 return Phone.APN_REQUEST_STARTED;
750 } else {
Robert Greenwalt5364d752010-12-15 13:26:33 -0800751 // need to remember this unsupported request so we respond appropriately on stop
752 synchronized(this) {
753 mFeatureUsers.add(f);
754 if (!mNetRequestersPids[usedNetworkType].contains(currentPid)) {
755 // this gets used for per-pid dns when connected
756 mNetRequestersPids[usedNetworkType].add(currentPid);
757 }
758 }
Robert Greenwaltd391e892010-05-18 10:52:51 -0700759 return -1;
Robert Greenwalt2034b912009-08-12 16:08:25 -0700760 }
761 }
762 return Phone.APN_TYPE_NOT_AVAILABLE;
The Android Open Source Project28527d22009-03-03 19:31:44 -0800763 }
764
Robert Greenwaltaffc3a12009-09-27 17:27:04 -0700765 // javadoc from interface
The Android Open Source Project28527d22009-03-03 19:31:44 -0800766 public int stopUsingNetworkFeature(int networkType, String feature) {
Robert Greenwalt28f43012009-10-06 17:52:40 -0700767 enforceChangePermission();
768
Robert Greenwaltaffc3a12009-09-27 17:27:04 -0700769 int pid = getCallingPid();
770 int uid = getCallingUid();
771
772 FeatureUser u = null;
773 boolean found = false;
774
775 synchronized(this) {
776 for (int i = 0; i < mFeatureUsers.size() ; i++) {
777 u = (FeatureUser)mFeatureUsers.get(i);
778 if (uid == u.mUid && pid == u.mPid &&
779 networkType == u.mNetworkType &&
780 TextUtils.equals(feature, u.mFeature)) {
781 found = true;
782 break;
783 }
784 }
785 }
786 if (found && u != null) {
787 // stop regardless of how many other time this proc had called start
788 return stopUsingNetworkFeature(u, true);
789 } else {
790 // none found!
Wink Savillee70c6f52010-12-03 12:01:38 -0800791 if (DBG) log("ignoring stopUsingNetworkFeature - not a live request");
Robert Greenwaltaffc3a12009-09-27 17:27:04 -0700792 return 1;
793 }
Robert Greenwalt2034b912009-08-12 16:08:25 -0700794 }
795
Robert Greenwaltaffc3a12009-09-27 17:27:04 -0700796 private int stopUsingNetworkFeature(FeatureUser u, boolean ignoreDups) {
797 int networkType = u.mNetworkType;
798 String feature = u.mFeature;
799 int pid = u.mPid;
800 int uid = u.mUid;
801
802 NetworkStateTracker tracker = null;
803 boolean callTeardown = false; // used to carry our decision outside of sync block
804
Robert Greenwalt2034b912009-08-12 16:08:25 -0700805 if (DBG) {
Wink Savillee70c6f52010-12-03 12:01:38 -0800806 log("stopUsingNetworkFeature for net " + networkType +
Robert Greenwalt2034b912009-08-12 16:08:25 -0700807 ": " + feature);
808 }
Robert Greenwalt28f43012009-10-06 17:52:40 -0700809
The Android Open Source Project28527d22009-03-03 19:31:44 -0800810 if (!ConnectivityManager.isNetworkTypeValid(networkType)) {
811 return -1;
812 }
Robert Greenwalt2034b912009-08-12 16:08:25 -0700813
Robert Greenwaltaffc3a12009-09-27 17:27:04 -0700814 // need to link the mFeatureUsers list with the mNetRequestersPids state in this
815 // sync block
816 synchronized(this) {
817 // check if this process still has an outstanding start request
818 if (!mFeatureUsers.contains(u)) {
Wink Savillee70c6f52010-12-03 12:01:38 -0800819 if (DBG) log("ignoring - this process has no outstanding requests");
Robert Greenwalt2034b912009-08-12 16:08:25 -0700820 return 1;
821 }
Robert Greenwaltaffc3a12009-09-27 17:27:04 -0700822 u.unlinkDeathRecipient();
823 mFeatureUsers.remove(mFeatureUsers.indexOf(u));
824 // If we care about duplicate requests, check for that here.
825 //
826 // This is done to support the extension of a request - the app
827 // can request we start the network feature again and renew the
828 // auto-shutoff delay. Normal "stop" calls from the app though
829 // do not pay attention to duplicate requests - in effect the
830 // API does not refcount and a single stop will counter multiple starts.
831 if (ignoreDups == false) {
832 for (int i = 0; i < mFeatureUsers.size() ; i++) {
833 FeatureUser x = (FeatureUser)mFeatureUsers.get(i);
834 if (x.mUid == u.mUid && x.mPid == u.mPid &&
835 x.mNetworkType == u.mNetworkType &&
836 TextUtils.equals(x.mFeature, u.mFeature)) {
Wink Savillee70c6f52010-12-03 12:01:38 -0800837 if (DBG) log("ignoring stopUsingNetworkFeature as dup is found");
Robert Greenwaltaffc3a12009-09-27 17:27:04 -0700838 return 1;
839 }
840 }
Robert Greenwalt2034b912009-08-12 16:08:25 -0700841 }
Robert Greenwaltaffc3a12009-09-27 17:27:04 -0700842
843 // TODO - move to MobileDataStateTracker
844 int usedNetworkType = networkType;
845 if (networkType == ConnectivityManager.TYPE_MOBILE) {
Wink Savillef6b76692011-02-24 17:58:51 -0800846 usedNetworkType = convertFeatureToNetworkType(feature);
847 if (usedNetworkType < 0) {
848 usedNetworkType = networkType;
Robert Greenwaltaffc3a12009-09-27 17:27:04 -0700849 }
850 }
851 tracker = mNetTrackers[usedNetworkType];
Robert Greenwaltec05b3c2009-10-30 14:17:42 -0700852 if (tracker == null) {
Wink Savillee70c6f52010-12-03 12:01:38 -0800853 if (DBG) log("ignoring - no known tracker for net type " + usedNetworkType);
Robert Greenwaltec05b3c2009-10-30 14:17:42 -0700854 return -1;
855 }
856 if (usedNetworkType != networkType) {
Robert Greenwaltaffc3a12009-09-27 17:27:04 -0700857 Integer currentPid = new Integer(pid);
Robert Greenwaltaffc3a12009-09-27 17:27:04 -0700858 mNetRequestersPids[usedNetworkType].remove(currentPid);
Robert Greenwalt0ca68a02009-12-17 14:54:59 -0800859 reassessPidDns(pid, true);
Robert Greenwaltaffc3a12009-09-27 17:27:04 -0700860 if (mNetRequestersPids[usedNetworkType].size() != 0) {
Wink Savillee70c6f52010-12-03 12:01:38 -0800861 if (DBG) log("not tearing down special network - " +
Robert Greenwaltaffc3a12009-09-27 17:27:04 -0700862 "others still using it");
863 return 1;
864 }
865 callTeardown = true;
Robert Greenwalt9f3be4c2011-01-10 11:58:31 -0800866 } else {
867 if (DBG) log("not a known feature - dropping");
Robert Greenwaltaffc3a12009-09-27 17:27:04 -0700868 }
869 }
Wink Savillee70c6f52010-12-03 12:01:38 -0800870 if (DBG) log("Doing network teardown");
Robert Greenwaltaffc3a12009-09-27 17:27:04 -0700871 if (callTeardown) {
872 tracker.teardown();
Robert Greenwalt2034b912009-08-12 16:08:25 -0700873 return 1;
874 } else {
Robert Greenwaltd391e892010-05-18 10:52:51 -0700875 return -1;
Robert Greenwalt2034b912009-08-12 16:08:25 -0700876 }
The Android Open Source Project28527d22009-03-03 19:31:44 -0800877 }
878
879 /**
Robert Greenwalt7fe44cb2010-08-27 09:24:29 -0700880 * @deprecated use requestRouteToHostAddress instead
881 *
The Android Open Source Project28527d22009-03-03 19:31:44 -0800882 * Ensure that a network route exists to deliver traffic to the specified
883 * host via the specified network interface.
Robert Greenwalt0659da32009-07-16 17:21:39 -0700884 * @param networkType the type of the network over which traffic to the
885 * specified host is to be routed
886 * @param hostAddress the IP address of the host to which the route is
887 * desired
The Android Open Source Project28527d22009-03-03 19:31:44 -0800888 * @return {@code true} on success, {@code false} on failure
889 */
890 public boolean requestRouteToHost(int networkType, int hostAddress) {
Robert Greenwalt7fe44cb2010-08-27 09:24:29 -0700891 InetAddress inetAddress = NetworkUtils.intToInetAddress(hostAddress);
892
893 if (inetAddress == null) {
894 return false;
895 }
896
897 return requestRouteToHostAddress(networkType, inetAddress.getAddress());
898 }
899
900 /**
901 * Ensure that a network route exists to deliver traffic to the specified
902 * host via the specified network interface.
903 * @param networkType the type of the network over which traffic to the
904 * specified host is to be routed
905 * @param hostAddress the IP address of the host to which the route is
906 * desired
907 * @return {@code true} on success, {@code false} on failure
908 */
909 public boolean requestRouteToHostAddress(int networkType, byte[] hostAddress) {
The Android Open Source Project28527d22009-03-03 19:31:44 -0800910 enforceChangePermission();
911 if (!ConnectivityManager.isNetworkTypeValid(networkType)) {
912 return false;
913 }
914 NetworkStateTracker tracker = mNetTrackers[networkType];
Robert Greenwalt4666ed02009-09-10 15:06:20 -0700915
Robert Greenwaltec05b3c2009-10-30 14:17:42 -0700916 if (tracker == null || !tracker.getNetworkInfo().isConnected() ||
917 tracker.isTeardownRequested()) {
Robert Greenwalt4666ed02009-09-10 15:06:20 -0700918 if (DBG) {
Wink Savillee70c6f52010-12-03 12:01:38 -0800919 log("requestRouteToHostAddress on down network " +
Robert Greenwalt7fe44cb2010-08-27 09:24:29 -0700920 "(" + networkType + ") - dropped");
Robert Greenwalt4666ed02009-09-10 15:06:20 -0700921 }
922 return false;
The Android Open Source Project28527d22009-03-03 19:31:44 -0800923 }
Robert Greenwalta7dfbd32010-06-15 15:43:39 -0700924 try {
Robert Greenwalt7fe44cb2010-08-27 09:24:29 -0700925 InetAddress addr = InetAddress.getByAddress(hostAddress);
Robert Greenwalta7dfbd32010-06-15 15:43:39 -0700926 return addHostRoute(tracker, addr);
927 } catch (UnknownHostException e) {}
928 return false;
Irfan Sheriff7f132d92010-06-09 15:39:36 -0700929 }
930
931 /**
932 * Ensure that a network route exists to deliver traffic to the specified
933 * host via the mobile data network.
934 * @param hostAddress the IP address of the host to which the route is desired,
935 * in network byte order.
Robert Greenwalta7dfbd32010-06-15 15:43:39 -0700936 * TODO - deprecate
Irfan Sheriff7f132d92010-06-09 15:39:36 -0700937 * @return {@code true} on success, {@code false} on failure
938 */
Robert Greenwalta7dfbd32010-06-15 15:43:39 -0700939 private boolean addHostRoute(NetworkStateTracker nt, InetAddress hostAddress) {
Irfan Sheriff7f132d92010-06-09 15:39:36 -0700940 if (nt.getNetworkInfo().getType() == ConnectivityManager.TYPE_WIFI) {
941 return false;
942 }
943
Robert Greenwalt1f1bcfe2010-08-30 10:56:47 -0700944 LinkProperties p = nt.getLinkProperties();
Robert Greenwalta7dfbd32010-06-15 15:43:39 -0700945 if (p == null) return false;
946 String interfaceName = p.getInterfaceName();
Irfan Sheriff7f132d92010-06-09 15:39:36 -0700947
948 if (DBG) {
Wink Savillee70c6f52010-12-03 12:01:38 -0800949 log("Requested host route to " + hostAddress + "(" + interfaceName + ")");
Irfan Sheriff7f132d92010-06-09 15:39:36 -0700950 }
Robert Greenwalta7dfbd32010-06-15 15:43:39 -0700951 if (interfaceName != null) {
Robert Greenwalt7fe44cb2010-08-27 09:24:29 -0700952 return NetworkUtils.addHostRoute(interfaceName, hostAddress, null);
Irfan Sheriff7f132d92010-06-09 15:39:36 -0700953 } else {
Wink Savillee70c6f52010-12-03 12:01:38 -0800954 if (DBG) loge("addHostRoute failed due to null interface name");
Irfan Sheriff7f132d92010-06-09 15:39:36 -0700955 return false;
956 }
The Android Open Source Project28527d22009-03-03 19:31:44 -0800957 }
958
959 /**
960 * @see ConnectivityManager#getBackgroundDataSetting()
961 */
962 public boolean getBackgroundDataSetting() {
Robert Greenwaltd62c7002010-12-29 16:15:02 -0800963 return mBackgroundDataEnabled.get();
The Android Open Source Project28527d22009-03-03 19:31:44 -0800964 }
Robert Greenwalt0659da32009-07-16 17:21:39 -0700965
The Android Open Source Project28527d22009-03-03 19:31:44 -0800966 /**
967 * @see ConnectivityManager#setBackgroundDataSetting(boolean)
968 */
969 public void setBackgroundDataSetting(boolean allowBackgroundDataUsage) {
970 mContext.enforceCallingOrSelfPermission(
971 android.Manifest.permission.CHANGE_BACKGROUND_DATA_SETTING,
972 "ConnectivityService");
Robert Greenwalt0659da32009-07-16 17:21:39 -0700973
Robert Greenwaltd62c7002010-12-29 16:15:02 -0800974 mBackgroundDataEnabled.set(allowBackgroundDataUsage);
975
Robert Greenwalt6a2db8a2010-09-23 10:05:56 -0700976 mHandler.sendMessage(mHandler.obtainMessage(EVENT_SET_BACKGROUND_DATA,
977 (allowBackgroundDataUsage ? ENABLED : DISABLED), 0));
978 }
The Android Open Source Project28527d22009-03-03 19:31:44 -0800979
Robert Greenwalt6a2db8a2010-09-23 10:05:56 -0700980 private void handleSetBackgroundData(boolean enabled) {
981 if (enabled != getBackgroundDataSetting()) {
982 Settings.Secure.putInt(mContext.getContentResolver(),
983 Settings.Secure.BACKGROUND_DATA, enabled ? 1 : 0);
984 Intent broadcast = new Intent(
985 ConnectivityManager.ACTION_BACKGROUND_DATA_SETTING_CHANGED);
986 mContext.sendBroadcast(broadcast);
987 }
Robert Greenwalt0659da32009-07-16 17:21:39 -0700988 }
989
Robert Greenwalt1b21f6c2010-02-23 18:58:05 -0800990 /**
991 * @see ConnectivityManager#getMobileDataEnabled()
992 */
993 public boolean getMobileDataEnabled() {
Wink Savilleb9024c62010-12-07 10:31:02 -0800994 // TODO: This detail should probably be in DataConnectionTracker's
995 // which is where we store the value and maybe make this
996 // asynchronous.
Robert Greenwalt1b21f6c2010-02-23 18:58:05 -0800997 enforceAccessPermission();
998 boolean retVal = Settings.Secure.getInt(mContext.getContentResolver(),
999 Settings.Secure.MOBILE_DATA, 1) == 1;
Wink Savillee70c6f52010-12-03 12:01:38 -08001000 if (DBG) log("getMobileDataEnabled returning " + retVal);
Robert Greenwalt1b21f6c2010-02-23 18:58:05 -08001001 return retVal;
1002 }
1003
1004 /**
1005 * @see ConnectivityManager#setMobileDataEnabled(boolean)
1006 */
Robert Greenwalt6a2db8a2010-09-23 10:05:56 -07001007 public void setMobileDataEnabled(boolean enabled) {
Robert Greenwalt1b21f6c2010-02-23 18:58:05 -08001008 enforceChangePermission();
Wink Savillee70c6f52010-12-03 12:01:38 -08001009 if (DBG) log("setMobileDataEnabled(" + enabled + ")");
Robert Greenwalt1b21f6c2010-02-23 18:58:05 -08001010
Robert Greenwalt6a2db8a2010-09-23 10:05:56 -07001011 mHandler.sendMessage(mHandler.obtainMessage(EVENT_SET_MOBILE_DATA,
1012 (enabled ? ENABLED : DISABLED), 0));
1013 }
1014
1015 private void handleSetMobileData(boolean enabled) {
Wink Savilleb9024c62010-12-07 10:31:02 -08001016 if (mNetTrackers[ConnectivityManager.TYPE_MOBILE] != null) {
1017 if (DBG) {
1018 Slog.d(TAG, mNetTrackers[ConnectivityManager.TYPE_MOBILE].toString() + enabled);
Robert Greenwalt1b21f6c2010-02-23 18:58:05 -08001019 }
Wink Savilleb9024c62010-12-07 10:31:02 -08001020 mNetTrackers[ConnectivityManager.TYPE_MOBILE].setDataEnable(enabled);
Robert Greenwalt1b21f6c2010-02-23 18:58:05 -08001021 }
1022 }
1023
The Android Open Source Project28527d22009-03-03 19:31:44 -08001024 private void enforceAccessPermission() {
Robert Greenwalt0659da32009-07-16 17:21:39 -07001025 mContext.enforceCallingOrSelfPermission(
1026 android.Manifest.permission.ACCESS_NETWORK_STATE,
1027 "ConnectivityService");
The Android Open Source Project28527d22009-03-03 19:31:44 -08001028 }
1029
1030 private void enforceChangePermission() {
Robert Greenwalt0659da32009-07-16 17:21:39 -07001031 mContext.enforceCallingOrSelfPermission(
1032 android.Manifest.permission.CHANGE_NETWORK_STATE,
1033 "ConnectivityService");
The Android Open Source Project28527d22009-03-03 19:31:44 -08001034 }
1035
Robert Greenwalt0c4828c2010-01-26 11:40:34 -08001036 // TODO Make this a special check when it goes public
1037 private void enforceTetherChangePermission() {
1038 mContext.enforceCallingOrSelfPermission(
1039 android.Manifest.permission.CHANGE_NETWORK_STATE,
1040 "ConnectivityService");
1041 }
1042
Robert Greenwalt8e87f122010-02-11 18:18:40 -08001043 private void enforceTetherAccessPermission() {
1044 mContext.enforceCallingOrSelfPermission(
1045 android.Manifest.permission.ACCESS_NETWORK_STATE,
1046 "ConnectivityService");
1047 }
1048
Robert Greenwalt93dc1042010-06-15 12:19:37 -07001049 private void enforceConnectivityInternalPermission() {
1050 mContext.enforceCallingOrSelfPermission(
1051 android.Manifest.permission.CONNECTIVITY_INTERNAL,
1052 "ConnectivityService");
1053 }
1054
The Android Open Source Project28527d22009-03-03 19:31:44 -08001055 /**
Robert Greenwalt0659da32009-07-16 17:21:39 -07001056 * Handle a {@code DISCONNECTED} event. If this pertains to the non-active
1057 * network, we ignore it. If it is for the active network, we send out a
1058 * broadcast. But first, we check whether it might be possible to connect
1059 * to a different network.
The Android Open Source Project28527d22009-03-03 19:31:44 -08001060 * @param info the {@code NetworkInfo} for the network
1061 */
1062 private void handleDisconnect(NetworkInfo info) {
1063
Robert Greenwalt2034b912009-08-12 16:08:25 -07001064 int prevNetType = info.getType();
The Android Open Source Project28527d22009-03-03 19:31:44 -08001065
Robert Greenwalt2034b912009-08-12 16:08:25 -07001066 mNetTrackers[prevNetType].setTeardownRequested(false);
The Android Open Source Project28527d22009-03-03 19:31:44 -08001067 /*
1068 * If the disconnected network is not the active one, then don't report
1069 * this as a loss of connectivity. What probably happened is that we're
1070 * getting the disconnect for a network that we explicitly disabled
1071 * in accordance with network preference policies.
1072 */
Robert Greenwalt2034b912009-08-12 16:08:25 -07001073 if (!mNetAttributes[prevNetType].isDefault()) {
1074 List pids = mNetRequestersPids[prevNetType];
1075 for (int i = 0; i<pids.size(); i++) {
1076 Integer pid = (Integer)pids.get(i);
1077 // will remove them because the net's no longer connected
1078 // need to do this now as only now do we know the pids and
1079 // can properly null things that are no longer referenced.
1080 reassessPidDns(pid.intValue(), false);
The Android Open Source Project28527d22009-03-03 19:31:44 -08001081 }
The Android Open Source Project28527d22009-03-03 19:31:44 -08001082 }
1083
The Android Open Source Project28527d22009-03-03 19:31:44 -08001084 Intent intent = new Intent(ConnectivityManager.CONNECTIVITY_ACTION);
1085 intent.putExtra(ConnectivityManager.EXTRA_NETWORK_INFO, info);
1086 if (info.isFailover()) {
1087 intent.putExtra(ConnectivityManager.EXTRA_IS_FAILOVER, true);
1088 info.setFailover(false);
1089 }
1090 if (info.getReason() != null) {
1091 intent.putExtra(ConnectivityManager.EXTRA_REASON, info.getReason());
1092 }
1093 if (info.getExtraInfo() != null) {
Robert Greenwalt0659da32009-07-16 17:21:39 -07001094 intent.putExtra(ConnectivityManager.EXTRA_EXTRA_INFO,
1095 info.getExtraInfo());
The Android Open Source Project28527d22009-03-03 19:31:44 -08001096 }
Robert Greenwalt2034b912009-08-12 16:08:25 -07001097
Robert Greenwalt3cc68d32010-01-25 17:54:29 -08001098 if (mNetAttributes[prevNetType].isDefault()) {
Robert Greenwalt4b7c55e2011-01-11 13:56:33 -08001099 tryFailover(prevNetType);
1100 if (mActiveDefaultNetwork != -1) {
1101 NetworkInfo switchTo = mNetTrackers[mActiveDefaultNetwork].getNetworkInfo();
Robert Greenwalt3cc68d32010-01-25 17:54:29 -08001102 intent.putExtra(ConnectivityManager.EXTRA_OTHER_NETWORK_INFO, switchTo);
1103 } else {
Robert Greenwaltb2a9e492010-09-20 18:01:43 -07001104 mDefaultInetConditionPublished = 0; // we're not connected anymore
Robert Greenwalt3cc68d32010-01-25 17:54:29 -08001105 intent.putExtra(ConnectivityManager.EXTRA_NO_CONNECTIVITY, true);
1106 }
Robert Greenwaltf55ced92010-01-20 19:29:41 -08001107 }
Robert Greenwaltb2a9e492010-09-20 18:01:43 -07001108 intent.putExtra(ConnectivityManager.EXTRA_INET_CONDITION, mDefaultInetConditionPublished);
Robert Greenwaltf55ced92010-01-20 19:29:41 -08001109 // do this before we broadcast the change
Robert Greenwalt3afbead2010-07-23 15:46:26 -07001110 handleConnectivityChange(prevNetType);
Robert Greenwaltf55ced92010-01-20 19:29:41 -08001111
1112 sendStickyBroadcast(intent);
1113 /*
1114 * If the failover network is already connected, then immediately send
1115 * out a followup broadcast indicating successful failover
1116 */
Robert Greenwalt4b7c55e2011-01-11 13:56:33 -08001117 if (mActiveDefaultNetwork != -1) {
1118 sendConnectedBroadcast(mNetTrackers[mActiveDefaultNetwork].getNetworkInfo());
Robert Greenwaltf55ced92010-01-20 19:29:41 -08001119 }
1120 }
1121
Robert Greenwalt4b7c55e2011-01-11 13:56:33 -08001122 private void tryFailover(int prevNetType) {
Robert Greenwalt2034b912009-08-12 16:08:25 -07001123 /*
Robert Greenwalt92564852011-01-06 15:41:07 -08001124 * If this is a default network, check if other defaults are available.
1125 * Try to reconnect on all available and let them hash it out when
1126 * more than one connects.
Robert Greenwalt2034b912009-08-12 16:08:25 -07001127 */
Robert Greenwalt2034b912009-08-12 16:08:25 -07001128 if (mNetAttributes[prevNetType].isDefault()) {
Robert Greenwalt2034b912009-08-12 16:08:25 -07001129 if (mActiveDefaultNetwork == prevNetType) {
1130 mActiveDefaultNetwork = -1;
1131 }
1132
Robert Greenwalt4b7c55e2011-01-11 13:56:33 -08001133 // don't signal a reconnect for anything lower or equal priority than our
1134 // current connected default
1135 // TODO - don't filter by priority now - nice optimization but risky
1136// int currentPriority = -1;
1137// if (mActiveDefaultNetwork != -1) {
1138// currentPriority = mNetAttributes[mActiveDefaultNetwork].mPriority;
1139// }
Robert Greenwaltf55ced92010-01-20 19:29:41 -08001140 for (int checkType=0; checkType <= ConnectivityManager.MAX_NETWORK_TYPE; checkType++) {
Robert Greenwaltec05b3c2009-10-30 14:17:42 -07001141 if (checkType == prevNetType) continue;
1142 if (mNetAttributes[checkType] == null) continue;
Robert Greenwalt92564852011-01-06 15:41:07 -08001143 if (!mNetAttributes[checkType].isDefault()) continue;
Wink Saville72a95b92011-01-26 15:43:49 -08001144
1145// Enabling the isAvailable() optimization caused mobile to not get
1146// selected if it was in the middle of error handling. Specifically
1147// a moble connection that took 30 seconds to complete the DEACTIVATE_DATA_CALL
1148// would not be available and we wouldn't get connected to anything.
1149// So removing the isAvailable() optimization below for now. TODO: This
1150// optimization should work and we need to investigate why it doesn't work.
1151// This could be related to how DEACTIVATE_DATA_CALL is reporting its
1152// complete before it is really complete.
1153// if (!mNetTrackers[checkType].isAvailable()) continue;
1154
Robert Greenwalt4b7c55e2011-01-11 13:56:33 -08001155// if (currentPriority >= mNetAttributes[checkType].mPriority) continue;
Robert Greenwalt2034b912009-08-12 16:08:25 -07001156
Robert Greenwalt4b7c55e2011-01-11 13:56:33 -08001157 NetworkStateTracker checkTracker = mNetTrackers[checkType];
1158 NetworkInfo checkInfo = checkTracker.getNetworkInfo();
1159 if (!checkInfo.isConnectedOrConnecting() || checkTracker.isTeardownRequested()) {
1160 checkInfo.setFailover(true);
1161 checkTracker.reconnect();
Robert Greenwalt2034b912009-08-12 16:08:25 -07001162 }
Robert Greenwalt4b7c55e2011-01-11 13:56:33 -08001163 if (DBG) log("Attempting to switch to " + checkInfo.getTypeName());
Robert Greenwalt2034b912009-08-12 16:08:25 -07001164 }
The Android Open Source Project28527d22009-03-03 19:31:44 -08001165 }
The Android Open Source Project28527d22009-03-03 19:31:44 -08001166 }
1167
1168 private void sendConnectedBroadcast(NetworkInfo info) {
Robert Greenwaltd3401f92010-09-15 17:36:33 -07001169 sendGeneralBroadcast(info, ConnectivityManager.CONNECTIVITY_ACTION);
1170 }
1171
1172 private void sendInetConditionBroadcast(NetworkInfo info) {
1173 sendGeneralBroadcast(info, ConnectivityManager.INET_CONDITION_ACTION);
1174 }
1175
1176 private void sendGeneralBroadcast(NetworkInfo info, String bcastType) {
1177 Intent intent = new Intent(bcastType);
The Android Open Source Project28527d22009-03-03 19:31:44 -08001178 intent.putExtra(ConnectivityManager.EXTRA_NETWORK_INFO, info);
1179 if (info.isFailover()) {
1180 intent.putExtra(ConnectivityManager.EXTRA_IS_FAILOVER, true);
1181 info.setFailover(false);
1182 }
1183 if (info.getReason() != null) {
1184 intent.putExtra(ConnectivityManager.EXTRA_REASON, info.getReason());
1185 }
1186 if (info.getExtraInfo() != null) {
Robert Greenwalt0659da32009-07-16 17:21:39 -07001187 intent.putExtra(ConnectivityManager.EXTRA_EXTRA_INFO,
1188 info.getExtraInfo());
The Android Open Source Project28527d22009-03-03 19:31:44 -08001189 }
Robert Greenwalt986c7412010-09-08 15:24:47 -07001190 intent.putExtra(ConnectivityManager.EXTRA_INET_CONDITION, mDefaultInetConditionPublished);
Mike Lockwoodfde2b762009-08-14 14:18:49 -04001191 sendStickyBroadcast(intent);
The Android Open Source Project28527d22009-03-03 19:31:44 -08001192 }
1193
1194 /**
1195 * Called when an attempt to fail over to another network has failed.
1196 * @param info the {@link NetworkInfo} for the failed network
1197 */
1198 private void handleConnectionFailure(NetworkInfo info) {
1199 mNetTrackers[info.getType()].setTeardownRequested(false);
The Android Open Source Project28527d22009-03-03 19:31:44 -08001200
Robert Greenwalt2034b912009-08-12 16:08:25 -07001201 String reason = info.getReason();
1202 String extraInfo = info.getExtraInfo();
Robert Greenwalt0659da32009-07-16 17:21:39 -07001203
Robert Greenwalte981bc52010-10-08 16:35:52 -07001204 String reasonText;
1205 if (reason == null) {
1206 reasonText = ".";
1207 } else {
1208 reasonText = " (" + reason + ").";
The Android Open Source Project28527d22009-03-03 19:31:44 -08001209 }
Wink Savillee70c6f52010-12-03 12:01:38 -08001210 loge("Attempt to connect to " + info.getTypeName() + " failed" + reasonText);
Robert Greenwalt2034b912009-08-12 16:08:25 -07001211
1212 Intent intent = new Intent(ConnectivityManager.CONNECTIVITY_ACTION);
1213 intent.putExtra(ConnectivityManager.EXTRA_NETWORK_INFO, info);
1214 if (getActiveNetworkInfo() == null) {
1215 intent.putExtra(ConnectivityManager.EXTRA_NO_CONNECTIVITY, true);
1216 }
1217 if (reason != null) {
1218 intent.putExtra(ConnectivityManager.EXTRA_REASON, reason);
1219 }
1220 if (extraInfo != null) {
1221 intent.putExtra(ConnectivityManager.EXTRA_EXTRA_INFO, extraInfo);
1222 }
1223 if (info.isFailover()) {
1224 intent.putExtra(ConnectivityManager.EXTRA_IS_FAILOVER, true);
1225 info.setFailover(false);
1226 }
Robert Greenwaltf55ced92010-01-20 19:29:41 -08001227
Robert Greenwalt3cc68d32010-01-25 17:54:29 -08001228 if (mNetAttributes[info.getType()].isDefault()) {
Robert Greenwalt4b7c55e2011-01-11 13:56:33 -08001229 tryFailover(info.getType());
1230 if (mActiveDefaultNetwork != -1) {
1231 NetworkInfo switchTo = mNetTrackers[mActiveDefaultNetwork].getNetworkInfo();
Robert Greenwalt3cc68d32010-01-25 17:54:29 -08001232 intent.putExtra(ConnectivityManager.EXTRA_OTHER_NETWORK_INFO, switchTo);
1233 } else {
Robert Greenwaltb2a9e492010-09-20 18:01:43 -07001234 mDefaultInetConditionPublished = 0;
Robert Greenwalt3cc68d32010-01-25 17:54:29 -08001235 intent.putExtra(ConnectivityManager.EXTRA_NO_CONNECTIVITY, true);
1236 }
Robert Greenwaltf55ced92010-01-20 19:29:41 -08001237 }
Robert Greenwalt3cc68d32010-01-25 17:54:29 -08001238
Robert Greenwaltb2a9e492010-09-20 18:01:43 -07001239 intent.putExtra(ConnectivityManager.EXTRA_INET_CONDITION, mDefaultInetConditionPublished);
Mike Lockwoodfde2b762009-08-14 14:18:49 -04001240 sendStickyBroadcast(intent);
Robert Greenwaltf55ced92010-01-20 19:29:41 -08001241 /*
1242 * If the failover network is already connected, then immediately send
1243 * out a followup broadcast indicating successful failover
1244 */
Robert Greenwalt4b7c55e2011-01-11 13:56:33 -08001245 if (mActiveDefaultNetwork != -1) {
1246 sendConnectedBroadcast(mNetTrackers[mActiveDefaultNetwork].getNetworkInfo());
Robert Greenwaltf55ced92010-01-20 19:29:41 -08001247 }
Mike Lockwoodfde2b762009-08-14 14:18:49 -04001248 }
1249
1250 private void sendStickyBroadcast(Intent intent) {
1251 synchronized(this) {
Dianne Hackborna417ff82009-12-08 19:45:14 -08001252 if (!mSystemReady) {
1253 mInitialBroadcast = new Intent(intent);
Mike Lockwoodfde2b762009-08-14 14:18:49 -04001254 }
Dianne Hackborna417ff82009-12-08 19:45:14 -08001255 intent.addFlags(Intent.FLAG_RECEIVER_REGISTERED_ONLY_BEFORE_BOOT);
1256 mContext.sendStickyBroadcast(intent);
Mike Lockwoodfde2b762009-08-14 14:18:49 -04001257 }
1258 }
1259
1260 void systemReady() {
1261 synchronized(this) {
1262 mSystemReady = true;
Dianne Hackborna417ff82009-12-08 19:45:14 -08001263 if (mInitialBroadcast != null) {
1264 mContext.sendStickyBroadcast(mInitialBroadcast);
1265 mInitialBroadcast = null;
Mike Lockwoodfde2b762009-08-14 14:18:49 -04001266 }
1267 }
Robert Greenwaltc3c5f862010-10-11 16:00:27 -07001268 // load the global proxy at startup
1269 mHandler.sendMessage(mHandler.obtainMessage(EVENT_APPLY_GLOBAL_HTTP_PROXY));
The Android Open Source Project28527d22009-03-03 19:31:44 -08001270 }
1271
1272 private void handleConnect(NetworkInfo info) {
Robert Greenwalt2034b912009-08-12 16:08:25 -07001273 int type = info.getType();
The Android Open Source Project28527d22009-03-03 19:31:44 -08001274
1275 // snapshot isFailover, because sendConnectedBroadcast() resets it
1276 boolean isFailover = info.isFailover();
Robert Greenwalt2034b912009-08-12 16:08:25 -07001277 NetworkStateTracker thisNet = mNetTrackers[type];
The Android Open Source Project28527d22009-03-03 19:31:44 -08001278
Robert Greenwalt2034b912009-08-12 16:08:25 -07001279 // if this is a default net and other default is running
1280 // kill the one not preferred
1281 if (mNetAttributes[type].isDefault()) {
Robert Greenwalt2034b912009-08-12 16:08:25 -07001282 if (mActiveDefaultNetwork != -1 && mActiveDefaultNetwork != type) {
1283 if ((type != mNetworkPreference &&
1284 mNetAttributes[mActiveDefaultNetwork].mPriority >
1285 mNetAttributes[type].mPriority) ||
1286 mNetworkPreference == mActiveDefaultNetwork) {
1287 // don't accept this one
Wink Savillee70c6f52010-12-03 12:01:38 -08001288 if (DBG) {
1289 log("Not broadcasting CONNECT_ACTION " +
Robert Greenwalt2034b912009-08-12 16:08:25 -07001290 "to torn down network " + info.getTypeName());
Wink Savillee70c6f52010-12-03 12:01:38 -08001291 }
Robert Greenwalt2034b912009-08-12 16:08:25 -07001292 teardown(thisNet);
1293 return;
1294 } else {
1295 // tear down the other
1296 NetworkStateTracker otherNet =
1297 mNetTrackers[mActiveDefaultNetwork];
Wink Savillee70c6f52010-12-03 12:01:38 -08001298 if (DBG) {
1299 log("Policy requires " + otherNet.getNetworkInfo().getTypeName() +
Robert Greenwalt2034b912009-08-12 16:08:25 -07001300 " teardown");
Wink Savillee70c6f52010-12-03 12:01:38 -08001301 }
Robert Greenwalt2034b912009-08-12 16:08:25 -07001302 if (!teardown(otherNet)) {
Wink Savillee70c6f52010-12-03 12:01:38 -08001303 loge("Network declined teardown request");
Robert Greenwalt2034b912009-08-12 16:08:25 -07001304 return;
1305 }
Robert Greenwalt93dc1042010-06-15 12:19:37 -07001306 }
1307 }
1308 synchronized (ConnectivityService.this) {
1309 // have a new default network, release the transition wakelock in a second
1310 // if it's held. The second pause is to allow apps to reconnect over the
1311 // new network
1312 if (mNetTransitionWakeLock.isHeld()) {
1313 mHandler.sendMessageDelayed(mHandler.obtainMessage(
Robert Greenwaltccb36f92010-09-24 14:32:21 -07001314 EVENT_CLEAR_NET_TRANSITION_WAKELOCK,
Robert Greenwalt93dc1042010-06-15 12:19:37 -07001315 mNetTransitionWakeLockSerialNumber, 0),
1316 1000);
Robert Greenwalt2034b912009-08-12 16:08:25 -07001317 }
1318 }
1319 mActiveDefaultNetwork = type;
Robert Greenwalt986c7412010-09-08 15:24:47 -07001320 // this will cause us to come up initially as unconnected and switching
1321 // to connected after our normal pause unless somebody reports us as reall
1322 // disconnected
1323 mDefaultInetConditionPublished = 0;
1324 mDefaultConnectionSequence++;
1325 mInetConditionChangeInFlight = false;
1326 // Don't do this - if we never sign in stay, grey
1327 //reportNetworkCondition(mActiveDefaultNetwork, 100);
Robert Greenwalt2034b912009-08-12 16:08:25 -07001328 }
The Android Open Source Project28527d22009-03-03 19:31:44 -08001329 thisNet.setTeardownRequested(false);
Irfan Sheriff7f132d92010-06-09 15:39:36 -07001330 updateNetworkSettings(thisNet);
Robert Greenwalt3afbead2010-07-23 15:46:26 -07001331 handleConnectivityChange(type);
Robert Greenwalt2034b912009-08-12 16:08:25 -07001332 sendConnectedBroadcast(info);
The Android Open Source Project28527d22009-03-03 19:31:44 -08001333 }
1334
The Android Open Source Project28527d22009-03-03 19:31:44 -08001335 /**
Robert Greenwalt3afbead2010-07-23 15:46:26 -07001336 * After a change in the connectivity state of a network. We're mainly
1337 * concerned with making sure that the list of DNS servers is set up
1338 * according to which networks are connected, and ensuring that the
1339 * right routing table entries exist.
The Android Open Source Project28527d22009-03-03 19:31:44 -08001340 */
Robert Greenwalt3afbead2010-07-23 15:46:26 -07001341 private void handleConnectivityChange(int netType) {
The Android Open Source Project28527d22009-03-03 19:31:44 -08001342 /*
Robert Greenwalt2034b912009-08-12 16:08:25 -07001343 * If a non-default network is enabled, add the host routes that
Robert Greenwalt3afbead2010-07-23 15:46:26 -07001344 * will allow it's DNS servers to be accessed.
The Android Open Source Project28527d22009-03-03 19:31:44 -08001345 */
Robert Greenwalt3afbead2010-07-23 15:46:26 -07001346 handleDnsConfigurationChange(netType);
Robert Greenwalt2034b912009-08-12 16:08:25 -07001347
Robert Greenwalt3afbead2010-07-23 15:46:26 -07001348 if (mNetTrackers[netType].getNetworkInfo().isConnected()) {
1349 if (mNetAttributes[netType].isDefault()) {
Robert Greenwaltc3c5f862010-10-11 16:00:27 -07001350 handleApplyDefaultProxy(netType);
Robert Greenwalt3afbead2010-07-23 15:46:26 -07001351 addDefaultRoute(mNetTrackers[netType]);
Robert Greenwalt2034b912009-08-12 16:08:25 -07001352 } else {
Robert Greenwalt3afbead2010-07-23 15:46:26 -07001353 addPrivateDnsRoutes(mNetTrackers[netType]);
1354 }
1355 } else {
1356 if (mNetAttributes[netType].isDefault()) {
1357 removeDefaultRoute(mNetTrackers[netType]);
1358 } else {
1359 removePrivateDnsRoutes(mNetTrackers[netType]);
Robert Greenwalt2034b912009-08-12 16:08:25 -07001360 }
The Android Open Source Project28527d22009-03-03 19:31:44 -08001361 }
1362 }
1363
Irfan Sheriff7f132d92010-06-09 15:39:36 -07001364 private void addPrivateDnsRoutes(NetworkStateTracker nt) {
Irfan Sheriff7f132d92010-06-09 15:39:36 -07001365 boolean privateDnsRouteSet = nt.isPrivateDnsRouteSet();
Robert Greenwalt1f1bcfe2010-08-30 10:56:47 -07001366 LinkProperties p = nt.getLinkProperties();
Robert Greenwalta7dfbd32010-06-15 15:43:39 -07001367 if (p == null) return;
1368 String interfaceName = p.getInterfaceName();
Irfan Sheriff7f132d92010-06-09 15:39:36 -07001369
1370 if (DBG) {
Wink Savillee70c6f52010-12-03 12:01:38 -08001371 log("addPrivateDnsRoutes for " + nt +
Irfan Sheriff7f132d92010-06-09 15:39:36 -07001372 "(" + interfaceName + ") - mPrivateDnsRouteSet = " + privateDnsRouteSet);
1373 }
Irfan Sheriff7f132d92010-06-09 15:39:36 -07001374 if (interfaceName != null && !privateDnsRouteSet) {
Robert Greenwalta7dfbd32010-06-15 15:43:39 -07001375 Collection<InetAddress> dnsList = p.getDnses();
1376 for (InetAddress dns : dnsList) {
Wink Savillee70c6f52010-12-03 12:01:38 -08001377 if (DBG) log(" adding " + dns);
Robert Greenwalt7fe44cb2010-08-27 09:24:29 -07001378 NetworkUtils.addHostRoute(interfaceName, dns, null);
Irfan Sheriff7f132d92010-06-09 15:39:36 -07001379 }
1380 nt.privateDnsRouteSet(true);
1381 }
1382 }
1383
1384 private void removePrivateDnsRoutes(NetworkStateTracker nt) {
1385 // TODO - we should do this explicitly but the NetUtils api doesnt
1386 // support this yet - must remove all. No worse than before
Robert Greenwalt1f1bcfe2010-08-30 10:56:47 -07001387 LinkProperties p = nt.getLinkProperties();
Robert Greenwalta7dfbd32010-06-15 15:43:39 -07001388 if (p == null) return;
1389 String interfaceName = p.getInterfaceName();
Irfan Sheriff7f132d92010-06-09 15:39:36 -07001390 boolean privateDnsRouteSet = nt.isPrivateDnsRouteSet();
1391 if (interfaceName != null && privateDnsRouteSet) {
1392 if (DBG) {
Wink Savillee70c6f52010-12-03 12:01:38 -08001393 log("removePrivateDnsRoutes for " + nt.getNetworkInfo().getTypeName() +
Irfan Sheriff7f132d92010-06-09 15:39:36 -07001394 " (" + interfaceName + ")");
1395 }
1396 NetworkUtils.removeHostRoutes(interfaceName);
1397 nt.privateDnsRouteSet(false);
1398 }
1399 }
1400
Irfan Sheriff7f132d92010-06-09 15:39:36 -07001401
1402 private void addDefaultRoute(NetworkStateTracker nt) {
Robert Greenwalt1f1bcfe2010-08-30 10:56:47 -07001403 LinkProperties p = nt.getLinkProperties();
Robert Greenwalta7dfbd32010-06-15 15:43:39 -07001404 if (p == null) return;
1405 String interfaceName = p.getInterfaceName();
Robert Greenwalt5c733972011-02-09 13:56:06 -08001406 if (TextUtils.isEmpty(interfaceName)) return;
1407 for (InetAddress gateway : p.getGateways()) {
Irfan Sheriff7f132d92010-06-09 15:39:36 -07001408
Robert Greenwalt5c733972011-02-09 13:56:06 -08001409 if (!NetworkUtils.addDefaultRoute(interfaceName, gateway) && DBG) {
Robert Greenwalta7dfbd32010-06-15 15:43:39 -07001410 NetworkInfo networkInfo = nt.getNetworkInfo();
Wink Savillee70c6f52010-12-03 12:01:38 -08001411 log("addDefaultRoute for " + networkInfo.getTypeName() +
Robert Greenwalt5c733972011-02-09 13:56:06 -08001412 " (" + interfaceName + "), GatewayAddr=" + gateway.getHostAddress());
Irfan Sheriff7f132d92010-06-09 15:39:36 -07001413 }
Irfan Sheriff7f132d92010-06-09 15:39:36 -07001414 }
1415 }
1416
1417
1418 public void removeDefaultRoute(NetworkStateTracker nt) {
Robert Greenwalt1f1bcfe2010-08-30 10:56:47 -07001419 LinkProperties p = nt.getLinkProperties();
Robert Greenwalta7dfbd32010-06-15 15:43:39 -07001420 if (p == null) return;
1421 String interfaceName = p.getInterfaceName();
Irfan Sheriff7f132d92010-06-09 15:39:36 -07001422
Robert Greenwalt3afbead2010-07-23 15:46:26 -07001423 if (interfaceName != null) {
1424 if ((NetworkUtils.removeDefaultRoute(interfaceName) >= 0) && DBG) {
Robert Greenwalta7dfbd32010-06-15 15:43:39 -07001425 NetworkInfo networkInfo = nt.getNetworkInfo();
Wink Savillee70c6f52010-12-03 12:01:38 -08001426 log("removeDefaultRoute for " + networkInfo.getTypeName() + " (" +
Irfan Sheriff7f132d92010-06-09 15:39:36 -07001427 interfaceName + ")");
1428 }
Irfan Sheriff7f132d92010-06-09 15:39:36 -07001429 }
1430 }
1431
1432 /**
1433 * Reads the network specific TCP buffer sizes from SystemProperties
1434 * net.tcp.buffersize.[default|wifi|umts|edge|gprs] and set them for system
1435 * wide use
1436 */
1437 public void updateNetworkSettings(NetworkStateTracker nt) {
1438 String key = nt.getTcpBufferSizesPropName();
1439 String bufferSizes = SystemProperties.get(key);
1440
1441 if (bufferSizes.length() == 0) {
Wink Savillee70c6f52010-12-03 12:01:38 -08001442 loge(key + " not found in system properties. Using defaults");
Irfan Sheriff7f132d92010-06-09 15:39:36 -07001443
1444 // Setting to default values so we won't be stuck to previous values
1445 key = "net.tcp.buffersize.default";
1446 bufferSizes = SystemProperties.get(key);
1447 }
1448
1449 // Set values in kernel
1450 if (bufferSizes.length() != 0) {
1451 if (DBG) {
Wink Savillee70c6f52010-12-03 12:01:38 -08001452 log("Setting TCP values: [" + bufferSizes
Irfan Sheriff7f132d92010-06-09 15:39:36 -07001453 + "] which comes from [" + key + "]");
1454 }
1455 setBufferSize(bufferSizes);
1456 }
1457 }
1458
1459 /**
1460 * Writes TCP buffer sizes to /sys/kernel/ipv4/tcp_[r/w]mem_[min/def/max]
1461 * which maps to /proc/sys/net/ipv4/tcp_rmem and tcpwmem
1462 *
1463 * @param bufferSizes in the format of "readMin, readInitial, readMax,
1464 * writeMin, writeInitial, writeMax"
1465 */
1466 private void setBufferSize(String bufferSizes) {
1467 try {
1468 String[] values = bufferSizes.split(",");
1469
1470 if (values.length == 6) {
1471 final String prefix = "/sys/kernel/ipv4/tcp_";
1472 stringToFile(prefix + "rmem_min", values[0]);
1473 stringToFile(prefix + "rmem_def", values[1]);
1474 stringToFile(prefix + "rmem_max", values[2]);
1475 stringToFile(prefix + "wmem_min", values[3]);
1476 stringToFile(prefix + "wmem_def", values[4]);
1477 stringToFile(prefix + "wmem_max", values[5]);
1478 } else {
Wink Savillee70c6f52010-12-03 12:01:38 -08001479 loge("Invalid buffersize string: " + bufferSizes);
Irfan Sheriff7f132d92010-06-09 15:39:36 -07001480 }
1481 } catch (IOException e) {
Wink Savillee70c6f52010-12-03 12:01:38 -08001482 loge("Can't set tcp buffer sizes:" + e);
Irfan Sheriff7f132d92010-06-09 15:39:36 -07001483 }
1484 }
1485
1486 /**
1487 * Writes string to file. Basically same as "echo -n $string > $filename"
1488 *
1489 * @param filename
1490 * @param string
1491 * @throws IOException
1492 */
1493 private void stringToFile(String filename, String string) throws IOException {
1494 FileWriter out = new FileWriter(filename);
1495 try {
1496 out.write(string);
1497 } finally {
1498 out.close();
1499 }
1500 }
1501
1502
Robert Greenwalt2034b912009-08-12 16:08:25 -07001503 /**
1504 * Adjust the per-process dns entries (net.dns<x>.<pid>) based
1505 * on the highest priority active net which this process requested.
1506 * If there aren't any, clear it out
1507 */
1508 private void reassessPidDns(int myPid, boolean doBump)
1509 {
Wink Savillee70c6f52010-12-03 12:01:38 -08001510 if (DBG) log("reassessPidDns for pid " + myPid);
Robert Greenwalt2034b912009-08-12 16:08:25 -07001511 for(int i : mPriorityList) {
1512 if (mNetAttributes[i].isDefault()) {
1513 continue;
1514 }
1515 NetworkStateTracker nt = mNetTrackers[i];
Robert Greenwalt0659da32009-07-16 17:21:39 -07001516 if (nt.getNetworkInfo().isConnected() &&
1517 !nt.isTeardownRequested()) {
Robert Greenwalt1f1bcfe2010-08-30 10:56:47 -07001518 LinkProperties p = nt.getLinkProperties();
Robert Greenwalta7dfbd32010-06-15 15:43:39 -07001519 if (p == null) continue;
Robert Greenwalt2034b912009-08-12 16:08:25 -07001520 List pids = mNetRequestersPids[i];
1521 for (int j=0; j<pids.size(); j++) {
1522 Integer pid = (Integer)pids.get(j);
1523 if (pid.intValue() == myPid) {
Robert Greenwalta7dfbd32010-06-15 15:43:39 -07001524 Collection<InetAddress> dnses = p.getDnses();
1525 writePidDns(dnses, myPid);
Robert Greenwalt2034b912009-08-12 16:08:25 -07001526 if (doBump) {
1527 bumpDns();
1528 }
1529 return;
1530 }
1531 }
1532 }
1533 }
1534 // nothing found - delete
1535 for (int i = 1; ; i++) {
1536 String prop = "net.dns" + i + "." + myPid;
1537 if (SystemProperties.get(prop).length() == 0) {
1538 if (doBump) {
1539 bumpDns();
1540 }
1541 return;
1542 }
1543 SystemProperties.set(prop, "");
1544 }
1545 }
1546
Robert Greenwalt8ca88762010-12-17 15:20:36 -08001547 // return true if results in a change
1548 private boolean writePidDns(Collection <InetAddress> dnses, int pid) {
Robert Greenwalt2034b912009-08-12 16:08:25 -07001549 int j = 1;
Robert Greenwalt8ca88762010-12-17 15:20:36 -08001550 boolean changed = false;
Robert Greenwalta7dfbd32010-06-15 15:43:39 -07001551 for (InetAddress dns : dnses) {
Robert Greenwalt8ca88762010-12-17 15:20:36 -08001552 String dnsString = dns.getHostAddress();
1553 if (changed || !dnsString.equals(SystemProperties.get("net.dns" + j + "." + pid))) {
1554 changed = true;
1555 SystemProperties.set("net.dns" + j++ + "." + pid, dns.getHostAddress());
1556 }
Robert Greenwalt2034b912009-08-12 16:08:25 -07001557 }
Robert Greenwalt8ca88762010-12-17 15:20:36 -08001558 return changed;
Robert Greenwalt2034b912009-08-12 16:08:25 -07001559 }
1560
1561 private void bumpDns() {
1562 /*
1563 * Bump the property that tells the name resolver library to reread
1564 * the DNS server list from the properties.
1565 */
1566 String propVal = SystemProperties.get("net.dnschange");
1567 int n = 0;
1568 if (propVal.length() != 0) {
1569 try {
1570 n = Integer.parseInt(propVal);
1571 } catch (NumberFormatException e) {}
1572 }
1573 SystemProperties.set("net.dnschange", "" + (n+1));
Robert Greenwalt051642b2010-11-02 14:08:23 -07001574 /*
1575 * Tell the VMs to toss their DNS caches
1576 */
1577 Intent intent = new Intent(Intent.ACTION_CLEAR_DNS_CACHE);
1578 intent.addFlags(Intent.FLAG_RECEIVER_REPLACE_PENDING);
Stan Chesnuttf444f502011-01-05 17:14:03 -08001579 /*
1580 * Connectivity events can happen before boot has completed ...
1581 */
1582 intent.addFlags(Intent.FLAG_RECEIVER_REGISTERED_ONLY_BEFORE_BOOT);
Robert Greenwalt051642b2010-11-02 14:08:23 -07001583 mContext.sendBroadcast(intent);
Robert Greenwalt2034b912009-08-12 16:08:25 -07001584 }
1585
Robert Greenwalt3afbead2010-07-23 15:46:26 -07001586 private void handleDnsConfigurationChange(int netType) {
Robert Greenwalt2034b912009-08-12 16:08:25 -07001587 // add default net's dns entries
Robert Greenwalt3afbead2010-07-23 15:46:26 -07001588 NetworkStateTracker nt = mNetTrackers[netType];
1589 if (nt != null && nt.getNetworkInfo().isConnected() && !nt.isTeardownRequested()) {
Robert Greenwalt1f1bcfe2010-08-30 10:56:47 -07001590 LinkProperties p = nt.getLinkProperties();
Robert Greenwalt3afbead2010-07-23 15:46:26 -07001591 if (p == null) return;
1592 Collection<InetAddress> dnses = p.getDnses();
Robert Greenwalt8ca88762010-12-17 15:20:36 -08001593 boolean changed = false;
Robert Greenwalt3afbead2010-07-23 15:46:26 -07001594 if (mNetAttributes[netType].isDefault()) {
1595 int j = 1;
Robert Greenwalt94daa182010-09-01 11:34:05 -07001596 if (dnses.size() == 0 && mDefaultDns != null) {
Robert Greenwalt8ca88762010-12-17 15:20:36 -08001597 String dnsString = mDefaultDns.getHostAddress();
1598 if (!dnsString.equals(SystemProperties.get("net.dns1"))) {
1599 if (DBG) {
1600 log("no dns provided - using " + dnsString);
1601 }
1602 changed = true;
1603 SystemProperties.set("net.dns1", dnsString);
Robert Greenwalt2034b912009-08-12 16:08:25 -07001604 }
Robert Greenwalt94daa182010-09-01 11:34:05 -07001605 j++;
1606 } else {
1607 for (InetAddress dns : dnses) {
Robert Greenwalt8ca88762010-12-17 15:20:36 -08001608 String dnsString = dns.getHostAddress();
1609 if (!changed && dnsString.equals(SystemProperties.get("net.dns" + j))) {
1610 j++;
1611 continue;
1612 }
Robert Greenwalt94daa182010-09-01 11:34:05 -07001613 if (DBG) {
Wink Savillee70c6f52010-12-03 12:01:38 -08001614 log("adding dns " + dns + " for " +
Robert Greenwalt94daa182010-09-01 11:34:05 -07001615 nt.getNetworkInfo().getTypeName());
1616 }
Robert Greenwalt8ca88762010-12-17 15:20:36 -08001617 changed = true;
1618 SystemProperties.set("net.dns" + j++, dnsString);
Robert Greenwalt94daa182010-09-01 11:34:05 -07001619 }
Robert Greenwalt3afbead2010-07-23 15:46:26 -07001620 }
1621 for (int k=j ; k<mNumDnsEntries; k++) {
Robert Greenwalt8ca88762010-12-17 15:20:36 -08001622 if (changed || !TextUtils.isEmpty(SystemProperties.get("net.dns" + k))) {
1623 if (DBG) log("erasing net.dns" + k);
1624 changed = true;
1625 SystemProperties.set("net.dns" + k, "");
1626 }
Robert Greenwalt3afbead2010-07-23 15:46:26 -07001627 }
1628 mNumDnsEntries = j;
1629 } else {
1630 // set per-pid dns for attached secondary nets
1631 List pids = mNetRequestersPids[netType];
1632 for (int y=0; y< pids.size(); y++) {
1633 Integer pid = (Integer)pids.get(y);
Robert Greenwalt8ca88762010-12-17 15:20:36 -08001634 changed = writePidDns(dnses, pid.intValue());
The Android Open Source Project28527d22009-03-03 19:31:44 -08001635 }
1636 }
Robert Greenwalt8ca88762010-12-17 15:20:36 -08001637 if (changed) bumpDns();
The Android Open Source Project28527d22009-03-03 19:31:44 -08001638 }
Robert Greenwalt2034b912009-08-12 16:08:25 -07001639 }
1640
1641 private int getRestoreDefaultNetworkDelay() {
1642 String restoreDefaultNetworkDelayStr = SystemProperties.get(
1643 NETWORK_RESTORE_DELAY_PROP_NAME);
1644 if(restoreDefaultNetworkDelayStr != null &&
1645 restoreDefaultNetworkDelayStr.length() != 0) {
1646 try {
1647 return Integer.valueOf(restoreDefaultNetworkDelayStr);
1648 } catch (NumberFormatException e) {
1649 }
The Android Open Source Project28527d22009-03-03 19:31:44 -08001650 }
Robert Greenwalt2034b912009-08-12 16:08:25 -07001651 return RESTORE_DEFAULT_NETWORK_DELAY;
The Android Open Source Project28527d22009-03-03 19:31:44 -08001652 }
1653
1654 @Override
1655 protected void dump(FileDescriptor fd, PrintWriter pw, String[] args) {
Robert Greenwalt0659da32009-07-16 17:21:39 -07001656 if (mContext.checkCallingOrSelfPermission(
1657 android.Manifest.permission.DUMP)
The Android Open Source Project28527d22009-03-03 19:31:44 -08001658 != PackageManager.PERMISSION_GRANTED) {
Robert Greenwalt0659da32009-07-16 17:21:39 -07001659 pw.println("Permission Denial: can't dump ConnectivityService " +
1660 "from from pid=" + Binder.getCallingPid() + ", uid=" +
1661 Binder.getCallingUid());
The Android Open Source Project28527d22009-03-03 19:31:44 -08001662 return;
1663 }
The Android Open Source Project28527d22009-03-03 19:31:44 -08001664 pw.println();
1665 for (NetworkStateTracker nst : mNetTrackers) {
Robert Greenwalt3eeb6032009-12-21 18:24:07 -08001666 if (nst != null) {
1667 if (nst.getNetworkInfo().isConnected()) {
1668 pw.println("Active network: " + nst.getNetworkInfo().
1669 getTypeName());
1670 }
1671 pw.println(nst.getNetworkInfo());
1672 pw.println(nst);
1673 pw.println();
Robert Greenwalt2034b912009-08-12 16:08:25 -07001674 }
The Android Open Source Project28527d22009-03-03 19:31:44 -08001675 }
Robert Greenwalt3eeb6032009-12-21 18:24:07 -08001676
1677 pw.println("Network Requester Pids:");
1678 for (int net : mPriorityList) {
1679 String pidString = net + ": ";
1680 for (Object pid : mNetRequestersPids[net]) {
1681 pidString = pidString + pid.toString() + ", ";
1682 }
1683 pw.println(pidString);
1684 }
1685 pw.println();
1686
1687 pw.println("FeatureUsers:");
1688 for (Object requester : mFeatureUsers) {
1689 pw.println(requester.toString());
1690 }
1691 pw.println();
Robert Greenwalt8e87f122010-02-11 18:18:40 -08001692
Robert Greenwalt93dc1042010-06-15 12:19:37 -07001693 synchronized (this) {
1694 pw.println("NetworkTranstionWakeLock is currently " +
1695 (mNetTransitionWakeLock.isHeld() ? "" : "not ") + "held.");
1696 pw.println("It was last requested for "+mNetTransitionWakeLockCausedBy);
1697 }
1698 pw.println();
1699
Robert Greenwalt8e87f122010-02-11 18:18:40 -08001700 mTethering.dump(fd, pw, args);
Robert Greenwalt0e80be12010-09-20 14:35:25 -07001701
1702 if (mInetLog != null) {
1703 pw.println();
1704 pw.println("Inet condition reports:");
1705 for(int i = 0; i < mInetLog.size(); i++) {
1706 pw.println(mInetLog.get(i));
1707 }
1708 }
The Android Open Source Project28527d22009-03-03 19:31:44 -08001709 }
1710
Robert Greenwalt2034b912009-08-12 16:08:25 -07001711 // must be stateless - things change under us.
The Android Open Source Project28527d22009-03-03 19:31:44 -08001712 private class MyHandler extends Handler {
Wink Saville775aad62010-09-02 19:23:52 -07001713 public MyHandler(Looper looper) {
1714 super(looper);
1715 }
1716
The Android Open Source Project28527d22009-03-03 19:31:44 -08001717 @Override
1718 public void handleMessage(Message msg) {
1719 NetworkInfo info;
1720 switch (msg.what) {
1721 case NetworkStateTracker.EVENT_STATE_CHANGED:
1722 info = (NetworkInfo) msg.obj;
Robert Greenwalt12c44552009-12-07 11:33:18 -08001723 int type = info.getType();
1724 NetworkInfo.State state = info.getState();
Robert Greenwalt24e2d2b2010-01-25 16:14:00 -08001725 // only do this optimization for wifi. It going into scan mode for location
1726 // services generates alot of noise. Meanwhile the mms apn won't send out
1727 // subsequent notifications when on default cellular because it never
1728 // disconnects.. so only do this to wifi notifications. Fixed better when the
1729 // APN notifications are standardized.
1730 if (mNetAttributes[type].mLastState == state &&
1731 mNetAttributes[type].mRadio == ConnectivityManager.TYPE_WIFI) {
Robert Greenwalt12c44552009-12-07 11:33:18 -08001732 if (DBG) {
Robert Greenwalt24e2d2b2010-01-25 16:14:00 -08001733 // TODO - remove this after we validate the dropping doesn't break
1734 // anything
Wink Savillee70c6f52010-12-03 12:01:38 -08001735 log("Dropping ConnectivityChange for " +
Robert Greenwalt2adbc7f2010-01-13 09:36:31 -08001736 info.getTypeName() + ": " +
Robert Greenwalt12c44552009-12-07 11:33:18 -08001737 state + "/" + info.getDetailedState());
1738 }
1739 return;
1740 }
1741 mNetAttributes[type].mLastState = state;
1742
Wink Savillee70c6f52010-12-03 12:01:38 -08001743 if (DBG) log("ConnectivityChange for " +
Robert Greenwalt0659da32009-07-16 17:21:39 -07001744 info.getTypeName() + ": " +
Robert Greenwalt12c44552009-12-07 11:33:18 -08001745 state + "/" + info.getDetailedState());
The Android Open Source Project28527d22009-03-03 19:31:44 -08001746
1747 // Connectivity state changed:
1748 // [31-13] Reserved for future use
Robert Greenwalt0659da32009-07-16 17:21:39 -07001749 // [12-9] Network subtype (for mobile network, as defined
1750 // by TelephonyManager)
1751 // [8-3] Detailed state ordinal (as defined by
1752 // NetworkInfo.DetailedState)
The Android Open Source Project28527d22009-03-03 19:31:44 -08001753 // [2-0] Network type (as defined by ConnectivityManager)
1754 int eventLogParam = (info.getType() & 0x7) |
1755 ((info.getDetailedState().ordinal() & 0x3f) << 3) |
1756 (info.getSubtype() << 9);
Doug Zongker2fc96232009-12-04 10:31:43 -08001757 EventLog.writeEvent(EventLogTags.CONNECTIVITY_STATE_CHANGED,
Robert Greenwalt0659da32009-07-16 17:21:39 -07001758 eventLogParam);
1759
1760 if (info.getDetailedState() ==
1761 NetworkInfo.DetailedState.FAILED) {
The Android Open Source Project28527d22009-03-03 19:31:44 -08001762 handleConnectionFailure(info);
Robert Greenwalt12c44552009-12-07 11:33:18 -08001763 } else if (state == NetworkInfo.State.DISCONNECTED) {
The Android Open Source Project28527d22009-03-03 19:31:44 -08001764 handleDisconnect(info);
Robert Greenwalt12c44552009-12-07 11:33:18 -08001765 } else if (state == NetworkInfo.State.SUSPENDED) {
The Android Open Source Project28527d22009-03-03 19:31:44 -08001766 // TODO: need to think this over.
Robert Greenwalt0659da32009-07-16 17:21:39 -07001767 // the logic here is, handle SUSPENDED the same as
1768 // DISCONNECTED. The only difference being we are
1769 // broadcasting an intent with NetworkInfo that's
1770 // suspended. This allows the applications an
1771 // opportunity to handle DISCONNECTED and SUSPENDED
1772 // differently, or not.
The Android Open Source Project28527d22009-03-03 19:31:44 -08001773 handleDisconnect(info);
Robert Greenwalt12c44552009-12-07 11:33:18 -08001774 } else if (state == NetworkInfo.State.CONNECTED) {
The Android Open Source Project28527d22009-03-03 19:31:44 -08001775 handleConnect(info);
1776 }
The Android Open Source Project28527d22009-03-03 19:31:44 -08001777 break;
The Android Open Source Project28527d22009-03-03 19:31:44 -08001778 case NetworkStateTracker.EVENT_CONFIGURATION_CHANGED:
Robert Greenwalt3afbead2010-07-23 15:46:26 -07001779 info = (NetworkInfo) msg.obj;
1780 type = info.getType();
Robert Greenwaltc3c5f862010-10-11 16:00:27 -07001781 handleConnectivityChange(type);
The Android Open Source Project28527d22009-03-03 19:31:44 -08001782 break;
Robert Greenwaltccb36f92010-09-24 14:32:21 -07001783 case EVENT_CLEAR_NET_TRANSITION_WAKELOCK:
Robert Greenwalt93dc1042010-06-15 12:19:37 -07001784 String causedBy = null;
1785 synchronized (ConnectivityService.this) {
1786 if (msg.arg1 == mNetTransitionWakeLockSerialNumber &&
1787 mNetTransitionWakeLock.isHeld()) {
1788 mNetTransitionWakeLock.release();
1789 causedBy = mNetTransitionWakeLockCausedBy;
1790 }
1791 }
1792 if (causedBy != null) {
Wink Savillee70c6f52010-12-03 12:01:38 -08001793 log("NetTransition Wakelock for " + causedBy + " released by timeout");
Robert Greenwalt93dc1042010-06-15 12:19:37 -07001794 }
Robert Greenwaltcf1a56c2010-09-09 14:05:10 -07001795 break;
Robert Greenwalt6a2db8a2010-09-23 10:05:56 -07001796 case EVENT_RESTORE_DEFAULT_NETWORK:
Robert Greenwaltaffc3a12009-09-27 17:27:04 -07001797 FeatureUser u = (FeatureUser)msg.obj;
1798 u.expire();
Robert Greenwalt986c7412010-09-08 15:24:47 -07001799 break;
Robert Greenwalt6a2db8a2010-09-23 10:05:56 -07001800 case EVENT_INET_CONDITION_CHANGE:
1801 {
1802 int netType = msg.arg1;
1803 int condition = msg.arg2;
1804 handleInetConditionChange(netType, condition);
Robert Greenwalt93dc1042010-06-15 12:19:37 -07001805 break;
Robert Greenwalt6a2db8a2010-09-23 10:05:56 -07001806 }
1807 case EVENT_INET_CONDITION_HOLD_END:
1808 {
1809 int netType = msg.arg1;
1810 int sequence = msg.arg2;
1811 handleInetConditionHoldEnd(netType, sequence);
Robert Greenwalt986c7412010-09-08 15:24:47 -07001812 break;
Robert Greenwalt6a2db8a2010-09-23 10:05:56 -07001813 }
1814 case EVENT_SET_NETWORK_PREFERENCE:
1815 {
1816 int preference = msg.arg1;
1817 handleSetNetworkPreference(preference);
1818 break;
1819 }
1820 case EVENT_SET_BACKGROUND_DATA:
1821 {
1822 boolean enabled = (msg.arg1 == ENABLED);
1823 handleSetBackgroundData(enabled);
1824 break;
1825 }
1826 case EVENT_SET_MOBILE_DATA:
1827 {
1828 boolean enabled = (msg.arg1 == ENABLED);
1829 handleSetMobileData(enabled);
1830 break;
1831 }
Robert Greenwaltc3c5f862010-10-11 16:00:27 -07001832 case EVENT_APPLY_GLOBAL_HTTP_PROXY:
1833 {
1834 handleDeprecatedGlobalHttpProxy();
1835 }
The Android Open Source Project28527d22009-03-03 19:31:44 -08001836 }
1837 }
1838 }
Robert Greenwalt0c4828c2010-01-26 11:40:34 -08001839
1840 // javadoc from interface
Robert Greenwalt4283ded2010-03-02 17:25:02 -08001841 public int tether(String iface) {
Robert Greenwalt0c4828c2010-01-26 11:40:34 -08001842 enforceTetherChangePermission();
Robert Greenwalt4283ded2010-03-02 17:25:02 -08001843
1844 if (isTetheringSupported()) {
1845 return mTethering.tether(iface);
1846 } else {
1847 return ConnectivityManager.TETHER_ERROR_UNSUPPORTED;
1848 }
Robert Greenwalt0c4828c2010-01-26 11:40:34 -08001849 }
1850
1851 // javadoc from interface
Robert Greenwalt4283ded2010-03-02 17:25:02 -08001852 public int untether(String iface) {
Robert Greenwalt0c4828c2010-01-26 11:40:34 -08001853 enforceTetherChangePermission();
Robert Greenwalt4283ded2010-03-02 17:25:02 -08001854
1855 if (isTetheringSupported()) {
1856 return mTethering.untether(iface);
1857 } else {
1858 return ConnectivityManager.TETHER_ERROR_UNSUPPORTED;
1859 }
1860 }
1861
1862 // javadoc from interface
1863 public int getLastTetherError(String iface) {
1864 enforceTetherAccessPermission();
1865
1866 if (isTetheringSupported()) {
1867 return mTethering.getLastTetherError(iface);
1868 } else {
1869 return ConnectivityManager.TETHER_ERROR_UNSUPPORTED;
1870 }
Robert Greenwalt8e87f122010-02-11 18:18:40 -08001871 }
1872
1873 // TODO - proper iface API for selection by property, inspection, etc
1874 public String[] getTetherableUsbRegexs() {
1875 enforceTetherAccessPermission();
1876 if (isTetheringSupported()) {
1877 return mTethering.getTetherableUsbRegexs();
1878 } else {
1879 return new String[0];
1880 }
1881 }
1882
1883 public String[] getTetherableWifiRegexs() {
1884 enforceTetherAccessPermission();
1885 if (isTetheringSupported()) {
1886 return mTethering.getTetherableWifiRegexs();
1887 } else {
1888 return new String[0];
1889 }
Robert Greenwalt0c4828c2010-01-26 11:40:34 -08001890 }
1891
Danica Chang96567052010-08-11 14:54:43 -07001892 public String[] getTetherableBluetoothRegexs() {
1893 enforceTetherAccessPermission();
1894 if (isTetheringSupported()) {
1895 return mTethering.getTetherableBluetoothRegexs();
1896 } else {
1897 return new String[0];
1898 }
1899 }
1900
Robert Greenwalt0c4828c2010-01-26 11:40:34 -08001901 // TODO - move iface listing, queries, etc to new module
1902 // javadoc from interface
1903 public String[] getTetherableIfaces() {
Robert Greenwalt8e87f122010-02-11 18:18:40 -08001904 enforceTetherAccessPermission();
Robert Greenwalt0c4828c2010-01-26 11:40:34 -08001905 return mTethering.getTetherableIfaces();
1906 }
1907
1908 public String[] getTetheredIfaces() {
Robert Greenwalt8e87f122010-02-11 18:18:40 -08001909 enforceTetherAccessPermission();
Robert Greenwalt0c4828c2010-01-26 11:40:34 -08001910 return mTethering.getTetheredIfaces();
1911 }
Robert Greenwalt8e87f122010-02-11 18:18:40 -08001912
Robert Greenwalt4283ded2010-03-02 17:25:02 -08001913 public String[] getTetheringErroredIfaces() {
1914 enforceTetherAccessPermission();
1915 return mTethering.getErroredIfaces();
1916 }
1917
Robert Greenwalt8e87f122010-02-11 18:18:40 -08001918 // if ro.tether.denied = true we default to no tethering
1919 // gservices could set the secure setting to 1 though to enable it on a build where it
1920 // had previously been turned off.
1921 public boolean isTetheringSupported() {
1922 enforceTetherAccessPermission();
1923 int defaultVal = (SystemProperties.get("ro.tether.denied").equals("true") ? 0 : 1);
Robert Greenwaltf1b66e12010-02-25 12:29:30 -08001924 boolean tetherEnabledInSettings = (Settings.Secure.getInt(mContext.getContentResolver(),
1925 Settings.Secure.TETHER_SUPPORTED, defaultVal) != 0);
1926 return tetherEnabledInSettings && mTetheringConfigValid;
Robert Greenwalt8e87f122010-02-11 18:18:40 -08001927 }
Robert Greenwalt93dc1042010-06-15 12:19:37 -07001928
1929 // An API NetworkStateTrackers can call when they lose their network.
1930 // This will automatically be cleared after X seconds or a network becomes CONNECTED,
1931 // whichever happens first. The timer is started by the first caller and not
1932 // restarted by subsequent callers.
1933 public void requestNetworkTransitionWakelock(String forWhom) {
1934 enforceConnectivityInternalPermission();
1935 synchronized (this) {
1936 if (mNetTransitionWakeLock.isHeld()) return;
1937 mNetTransitionWakeLockSerialNumber++;
1938 mNetTransitionWakeLock.acquire();
1939 mNetTransitionWakeLockCausedBy = forWhom;
1940 }
1941 mHandler.sendMessageDelayed(mHandler.obtainMessage(
Robert Greenwaltccb36f92010-09-24 14:32:21 -07001942 EVENT_CLEAR_NET_TRANSITION_WAKELOCK,
Robert Greenwalt93dc1042010-06-15 12:19:37 -07001943 mNetTransitionWakeLockSerialNumber, 0),
1944 mNetTransitionWakeLockTimeout);
1945 return;
1946 }
Robert Greenwalt24118e82010-09-09 13:15:32 -07001947
Robert Greenwalt986c7412010-09-08 15:24:47 -07001948 // 100 percent is full good, 0 is full bad.
1949 public void reportInetCondition(int networkType, int percentage) {
Wink Savillee70c6f52010-12-03 12:01:38 -08001950 if (DBG) log("reportNetworkCondition(" + networkType + ", " + percentage + ")");
Robert Greenwalt986c7412010-09-08 15:24:47 -07001951 mContext.enforceCallingOrSelfPermission(
1952 android.Manifest.permission.STATUS_BAR,
1953 "ConnectivityService");
1954
Robert Greenwalt0e80be12010-09-20 14:35:25 -07001955 if (DBG) {
1956 int pid = getCallingPid();
1957 int uid = getCallingUid();
1958 String s = pid + "(" + uid + ") reports inet is " +
1959 (percentage > 50 ? "connected" : "disconnected") + " (" + percentage + ") on " +
1960 "network Type " + networkType + " at " + GregorianCalendar.getInstance().getTime();
1961 mInetLog.add(s);
1962 while(mInetLog.size() > INET_CONDITION_LOG_MAX_SIZE) {
1963 mInetLog.remove(0);
1964 }
1965 }
Robert Greenwalt986c7412010-09-08 15:24:47 -07001966 mHandler.sendMessage(mHandler.obtainMessage(
Robert Greenwalt6a2db8a2010-09-23 10:05:56 -07001967 EVENT_INET_CONDITION_CHANGE, networkType, percentage));
1968 }
1969
1970 private void handleInetConditionChange(int netType, int condition) {
1971 if (DBG) {
Wink Savillee70c6f52010-12-03 12:01:38 -08001972 log("Inet connectivity change, net=" +
Robert Greenwalt6a2db8a2010-09-23 10:05:56 -07001973 netType + ", condition=" + condition +
1974 ",mActiveDefaultNetwork=" + mActiveDefaultNetwork);
1975 }
1976 if (mActiveDefaultNetwork == -1) {
Wink Savillee70c6f52010-12-03 12:01:38 -08001977 if (DBG) log("no active default network - aborting");
Robert Greenwalt6a2db8a2010-09-23 10:05:56 -07001978 return;
1979 }
1980 if (mActiveDefaultNetwork != netType) {
Wink Savillee70c6f52010-12-03 12:01:38 -08001981 if (DBG) log("given net not default - aborting");
Robert Greenwalt6a2db8a2010-09-23 10:05:56 -07001982 return;
1983 }
1984 mDefaultInetCondition = condition;
1985 int delay;
1986 if (mInetConditionChangeInFlight == false) {
Wink Savillee70c6f52010-12-03 12:01:38 -08001987 if (DBG) log("starting a change hold");
Robert Greenwalt6a2db8a2010-09-23 10:05:56 -07001988 // setup a new hold to debounce this
1989 if (mDefaultInetCondition > 50) {
1990 delay = Settings.Secure.getInt(mContext.getContentResolver(),
1991 Settings.Secure.INET_CONDITION_DEBOUNCE_UP_DELAY, 500);
1992 } else {
1993 delay = Settings.Secure.getInt(mContext.getContentResolver(),
1994 Settings.Secure.INET_CONDITION_DEBOUNCE_DOWN_DELAY, 3000);
1995 }
1996 mInetConditionChangeInFlight = true;
1997 mHandler.sendMessageDelayed(mHandler.obtainMessage(EVENT_INET_CONDITION_HOLD_END,
1998 mActiveDefaultNetwork, mDefaultConnectionSequence), delay);
1999 } else {
2000 // we've set the new condition, when this hold ends that will get
2001 // picked up
Wink Savillee70c6f52010-12-03 12:01:38 -08002002 if (DBG) log("currently in hold - not setting new end evt");
Robert Greenwalt6a2db8a2010-09-23 10:05:56 -07002003 }
2004 }
2005
2006 private void handleInetConditionHoldEnd(int netType, int sequence) {
2007 if (DBG) {
Wink Savillee70c6f52010-12-03 12:01:38 -08002008 log("Inet hold end, net=" + netType +
Robert Greenwalt6a2db8a2010-09-23 10:05:56 -07002009 ", condition =" + mDefaultInetCondition +
2010 ", published condition =" + mDefaultInetConditionPublished);
2011 }
2012 mInetConditionChangeInFlight = false;
2013
2014 if (mActiveDefaultNetwork == -1) {
Wink Savillee70c6f52010-12-03 12:01:38 -08002015 if (DBG) log("no active default network - aborting");
Robert Greenwalt6a2db8a2010-09-23 10:05:56 -07002016 return;
2017 }
2018 if (mDefaultConnectionSequence != sequence) {
Wink Savillee70c6f52010-12-03 12:01:38 -08002019 if (DBG) log("event hold for obsolete network - aborting");
Robert Greenwalt6a2db8a2010-09-23 10:05:56 -07002020 return;
2021 }
2022 if (mDefaultInetConditionPublished == mDefaultInetCondition) {
Wink Savillee70c6f52010-12-03 12:01:38 -08002023 if (DBG) log("no change in condition - aborting");
Robert Greenwalt6a2db8a2010-09-23 10:05:56 -07002024 return;
2025 }
2026 NetworkInfo networkInfo = mNetTrackers[mActiveDefaultNetwork].getNetworkInfo();
2027 if (networkInfo.isConnected() == false) {
Wink Savillee70c6f52010-12-03 12:01:38 -08002028 if (DBG) log("default network not connected - aborting");
Robert Greenwalt6a2db8a2010-09-23 10:05:56 -07002029 return;
2030 }
2031 mDefaultInetConditionPublished = mDefaultInetCondition;
2032 sendInetConditionBroadcast(networkInfo);
2033 return;
Robert Greenwalt986c7412010-09-08 15:24:47 -07002034 }
Robert Greenwaltc3c5f862010-10-11 16:00:27 -07002035
2036 public synchronized ProxyProperties getProxy() {
2037 if (mGlobalProxy != null) return mGlobalProxy;
2038 if (mDefaultProxy != null) return mDefaultProxy;
2039 return null;
2040 }
2041
2042 public void setGlobalProxy(ProxyProperties proxyProperties) {
2043 enforceChangePermission();
2044 synchronized (mGlobalProxyLock) {
2045 if (proxyProperties == mGlobalProxy) return;
2046 if (proxyProperties != null && proxyProperties.equals(mGlobalProxy)) return;
2047 if (mGlobalProxy != null && mGlobalProxy.equals(proxyProperties)) return;
2048
2049 String host = "";
2050 int port = 0;
2051 String exclList = "";
2052 if (proxyProperties != null && !TextUtils.isEmpty(proxyProperties.getHost())) {
2053 mGlobalProxy = new ProxyProperties(proxyProperties);
2054 host = mGlobalProxy.getHost();
2055 port = mGlobalProxy.getPort();
2056 exclList = mGlobalProxy.getExclusionList();
2057 } else {
2058 mGlobalProxy = null;
2059 }
2060 ContentResolver res = mContext.getContentResolver();
2061 Settings.Secure.putString(res, Settings.Secure.GLOBAL_HTTP_PROXY_HOST, host);
2062 Settings.Secure.putInt(res, Settings.Secure.GLOBAL_HTTP_PROXY_PORT, port);
Robert Greenwalt6f7c6092010-12-02 11:31:00 -08002063 Settings.Secure.putString(res, Settings.Secure.GLOBAL_HTTP_PROXY_EXCLUSION_LIST,
Robert Greenwaltc3c5f862010-10-11 16:00:27 -07002064 exclList);
2065 }
2066
2067 if (mGlobalProxy == null) {
2068 proxyProperties = mDefaultProxy;
2069 }
2070 sendProxyBroadcast(proxyProperties);
2071 }
2072
Robert Greenwalt6f7c6092010-12-02 11:31:00 -08002073 private void loadGlobalProxy() {
2074 ContentResolver res = mContext.getContentResolver();
2075 String host = Settings.Secure.getString(res, Settings.Secure.GLOBAL_HTTP_PROXY_HOST);
2076 int port = Settings.Secure.getInt(res, Settings.Secure.GLOBAL_HTTP_PROXY_PORT, 0);
2077 String exclList = Settings.Secure.getString(res,
2078 Settings.Secure.GLOBAL_HTTP_PROXY_EXCLUSION_LIST);
2079 if (!TextUtils.isEmpty(host)) {
2080 ProxyProperties proxyProperties = new ProxyProperties(host, port, exclList);
2081 synchronized (mGlobalProxyLock) {
2082 mGlobalProxy = proxyProperties;
2083 }
2084 }
2085 }
2086
Robert Greenwaltc3c5f862010-10-11 16:00:27 -07002087 public ProxyProperties getGlobalProxy() {
2088 synchronized (mGlobalProxyLock) {
2089 return mGlobalProxy;
2090 }
2091 }
2092
2093 private void handleApplyDefaultProxy(int type) {
2094 // check if new default - push it out to all VM if so
2095 ProxyProperties proxy = mNetTrackers[type].getLinkProperties().getHttpProxy();
2096 synchronized (this) {
2097 if (mDefaultProxy != null && mDefaultProxy.equals(proxy)) return;
2098 if (mDefaultProxy == proxy) return;
2099 if (!TextUtils.isEmpty(proxy.getHost())) {
2100 mDefaultProxy = proxy;
2101 } else {
2102 mDefaultProxy = null;
2103 }
2104 }
Wink Savillee70c6f52010-12-03 12:01:38 -08002105 if (DBG) log("changing default proxy to " + proxy);
Robert Greenwaltc3c5f862010-10-11 16:00:27 -07002106 if ((proxy == null && mGlobalProxy == null) || proxy.equals(mGlobalProxy)) return;
2107 if (mGlobalProxy != null) return;
2108 sendProxyBroadcast(proxy);
2109 }
2110
2111 private void handleDeprecatedGlobalHttpProxy() {
2112 String proxy = Settings.Secure.getString(mContext.getContentResolver(),
2113 Settings.Secure.HTTP_PROXY);
2114 if (!TextUtils.isEmpty(proxy)) {
2115 String data[] = proxy.split(":");
2116 String proxyHost = data[0];
2117 int proxyPort = 8080;
2118 if (data.length > 1) {
2119 try {
2120 proxyPort = Integer.parseInt(data[1]);
2121 } catch (NumberFormatException e) {
2122 return;
2123 }
2124 }
2125 ProxyProperties p = new ProxyProperties(data[0], proxyPort, "");
2126 setGlobalProxy(p);
2127 }
2128 }
2129
2130 private void sendProxyBroadcast(ProxyProperties proxy) {
Robert Greenwalt611291c2010-12-23 15:51:10 -08002131 if (proxy == null) proxy = new ProxyProperties("", 0, "");
Wink Savillee70c6f52010-12-03 12:01:38 -08002132 log("sending Proxy Broadcast for " + proxy);
Robert Greenwaltc3c5f862010-10-11 16:00:27 -07002133 Intent intent = new Intent(Proxy.PROXY_CHANGE_ACTION);
Stan Chesnutt1f2a2ac2011-01-06 11:00:19 -08002134 intent.addFlags(Intent.FLAG_RECEIVER_REPLACE_PENDING |
2135 Intent.FLAG_RECEIVER_REGISTERED_ONLY_BEFORE_BOOT);
Robert Greenwaltc3c5f862010-10-11 16:00:27 -07002136 intent.putExtra(Proxy.EXTRA_PROXY_INFO, proxy);
Robert Greenwaltd93dc8f2010-12-06 11:29:17 -08002137 mContext.sendStickyBroadcast(intent);
Robert Greenwaltc3c5f862010-10-11 16:00:27 -07002138 }
2139
2140 private static class SettingsObserver extends ContentObserver {
2141 private int mWhat;
2142 private Handler mHandler;
2143 SettingsObserver(Handler handler, int what) {
2144 super(handler);
2145 mHandler = handler;
2146 mWhat = what;
2147 }
2148
2149 void observe(Context context) {
2150 ContentResolver resolver = context.getContentResolver();
2151 resolver.registerContentObserver(Settings.Secure.getUriFor(
2152 Settings.Secure.HTTP_PROXY), false, this);
2153 }
2154
2155 @Override
2156 public void onChange(boolean selfChange) {
2157 mHandler.obtainMessage(mWhat).sendToTarget();
2158 }
2159 }
Wink Savillee70c6f52010-12-03 12:01:38 -08002160
2161 private void log(String s) {
2162 Slog.d(TAG, s);
2163 }
2164
2165 private void loge(String s) {
2166 Slog.e(TAG, s);
2167 }
Wink Savillef6b76692011-02-24 17:58:51 -08002168 int convertFeatureToNetworkType(String feature){
2169 int networkType = -1;
2170 if (TextUtils.equals(feature, Phone.FEATURE_ENABLE_MMS)) {
2171 networkType = ConnectivityManager.TYPE_MOBILE_MMS;
2172 } else if (TextUtils.equals(feature, Phone.FEATURE_ENABLE_SUPL)) {
2173 networkType = ConnectivityManager.TYPE_MOBILE_SUPL;
2174 } else if (TextUtils.equals(feature, Phone.FEATURE_ENABLE_DUN) ||
2175 TextUtils.equals(feature, Phone.FEATURE_ENABLE_DUN_ALWAYS)) {
2176 networkType = ConnectivityManager.TYPE_MOBILE_DUN;
2177 } else if (TextUtils.equals(feature, Phone.FEATURE_ENABLE_HIPRI)) {
2178 networkType = ConnectivityManager.TYPE_MOBILE_HIPRI;
2179 } else if (TextUtils.equals(feature, Phone.FEATURE_ENABLE_FOTA)) {
2180 networkType = ConnectivityManager.TYPE_MOBILE_FOTA;
2181 } else if (TextUtils.equals(feature, Phone.FEATURE_ENABLE_IMS)) {
2182 networkType = ConnectivityManager.TYPE_MOBILE_IMS;
2183 } else if (TextUtils.equals(feature, Phone.FEATURE_ENABLE_CBS)) {
2184 networkType = ConnectivityManager.TYPE_MOBILE_CBS;
2185 }
2186 return networkType;
2187 }
The Android Open Source Project28527d22009-03-03 19:31:44 -08002188}