blob: a5e9bb01c912b8ae67b332bbbe462ad64c878ba0 [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 {
Robert Greenwalt35e34d12011-02-22 16:00:42 -0800289 mDefaultDns = NetworkUtils.numericToInetAddress(dns);
290 } catch (IllegalArgumentException 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 Greenwalta71471b2011-03-24 21:41:41 -0700541 if (mActiveDefaultNetwork != -1) {
542 return mNetTrackers[mActiveDefaultNetwork].getNetworkInfo();
The Android Open Source Project28527d22009-03-03 19:31:44 -0800543 }
544 return null;
545 }
546
547 public NetworkInfo getNetworkInfo(int networkType) {
548 enforceAccessPermission();
549 if (ConnectivityManager.isNetworkTypeValid(networkType)) {
550 NetworkStateTracker t = mNetTrackers[networkType];
551 if (t != null)
552 return t.getNetworkInfo();
553 }
554 return null;
555 }
556
557 public NetworkInfo[] getAllNetworkInfo() {
558 enforceAccessPermission();
Robert Greenwaltec05b3c2009-10-30 14:17:42 -0700559 NetworkInfo[] result = new NetworkInfo[mNetworksDefined];
The Android Open Source Project28527d22009-03-03 19:31:44 -0800560 int i = 0;
561 for (NetworkStateTracker t : mNetTrackers) {
Robert Greenwaltec05b3c2009-10-30 14:17:42 -0700562 if(t != null) result[i++] = t.getNetworkInfo();
The Android Open Source Project28527d22009-03-03 19:31:44 -0800563 }
564 return result;
565 }
566
Robert Greenwalt9f0ee4f2010-09-14 09:18:02 -0700567 /**
568 * Return LinkProperties for the active (i.e., connected) default
569 * network interface. It is assumed that at most one default network
570 * is active at a time. If more than one is active, it is indeterminate
571 * which will be returned.
572 * @return the ip properties for the active network, or {@code null} if
573 * none is active
574 */
575 public LinkProperties getActiveLinkProperties() {
576 enforceAccessPermission();
577 for (int type=0; type <= ConnectivityManager.MAX_NETWORK_TYPE; type++) {
578 if (mNetAttributes[type] == null || !mNetAttributes[type].isDefault()) {
579 continue;
580 }
581 NetworkStateTracker t = mNetTrackers[type];
582 NetworkInfo info = t.getNetworkInfo();
583 if (info.isConnected()) {
584 return t.getLinkProperties();
585 }
586 }
587 return null;
588 }
589
590 public LinkProperties getLinkProperties(int networkType) {
591 enforceAccessPermission();
592 if (ConnectivityManager.isNetworkTypeValid(networkType)) {
593 NetworkStateTracker t = mNetTrackers[networkType];
594 if (t != null) return t.getLinkProperties();
595 }
596 return null;
597 }
598
The Android Open Source Project28527d22009-03-03 19:31:44 -0800599 public boolean setRadios(boolean turnOn) {
600 boolean result = true;
601 enforceChangePermission();
602 for (NetworkStateTracker t : mNetTrackers) {
Robert Greenwaltec05b3c2009-10-30 14:17:42 -0700603 if (t != null) result = t.setRadio(turnOn) && result;
The Android Open Source Project28527d22009-03-03 19:31:44 -0800604 }
605 return result;
606 }
607
608 public boolean setRadio(int netType, boolean turnOn) {
609 enforceChangePermission();
610 if (!ConnectivityManager.isNetworkTypeValid(netType)) {
611 return false;
612 }
613 NetworkStateTracker tracker = mNetTrackers[netType];
614 return tracker != null && tracker.setRadio(turnOn);
615 }
616
Robert Greenwaltaffc3a12009-09-27 17:27:04 -0700617 /**
618 * Used to notice when the calling process dies so we can self-expire
619 *
620 * Also used to know if the process has cleaned up after itself when
621 * our auto-expire timer goes off. The timer has a link to an object.
622 *
623 */
Robert Greenwalt2034b912009-08-12 16:08:25 -0700624 private class FeatureUser implements IBinder.DeathRecipient {
625 int mNetworkType;
626 String mFeature;
627 IBinder mBinder;
628 int mPid;
629 int mUid;
Robert Greenwalt3eeb6032009-12-21 18:24:07 -0800630 long mCreateTime;
Robert Greenwalt2034b912009-08-12 16:08:25 -0700631
632 FeatureUser(int type, String feature, IBinder binder) {
633 super();
634 mNetworkType = type;
635 mFeature = feature;
636 mBinder = binder;
637 mPid = getCallingPid();
638 mUid = getCallingUid();
Robert Greenwalt3eeb6032009-12-21 18:24:07 -0800639 mCreateTime = System.currentTimeMillis();
Robert Greenwaltaffc3a12009-09-27 17:27:04 -0700640
Robert Greenwalt2034b912009-08-12 16:08:25 -0700641 try {
642 mBinder.linkToDeath(this, 0);
643 } catch (RemoteException e) {
644 binderDied();
645 }
646 }
647
648 void unlinkDeathRecipient() {
649 mBinder.unlinkToDeath(this, 0);
650 }
651
652 public void binderDied() {
Wink Savillee70c6f52010-12-03 12:01:38 -0800653 log("ConnectivityService FeatureUser binderDied(" +
Robert Greenwalt3eeb6032009-12-21 18:24:07 -0800654 mNetworkType + ", " + mFeature + ", " + mBinder + "), created " +
655 (System.currentTimeMillis() - mCreateTime) + " mSec ago");
Robert Greenwaltaffc3a12009-09-27 17:27:04 -0700656 stopUsingNetworkFeature(this, false);
Robert Greenwalt2034b912009-08-12 16:08:25 -0700657 }
658
Robert Greenwaltaffc3a12009-09-27 17:27:04 -0700659 public void expire() {
Wink Savillee70c6f52010-12-03 12:01:38 -0800660 log("ConnectivityService FeatureUser expire(" +
Robert Greenwalt3eeb6032009-12-21 18:24:07 -0800661 mNetworkType + ", " + mFeature + ", " + mBinder +"), created " +
662 (System.currentTimeMillis() - mCreateTime) + " mSec ago");
Robert Greenwaltaffc3a12009-09-27 17:27:04 -0700663 stopUsingNetworkFeature(this, false);
664 }
Robert Greenwalt3eeb6032009-12-21 18:24:07 -0800665
666 public String toString() {
667 return "FeatureUser("+mNetworkType+","+mFeature+","+mPid+","+mUid+"), created " +
668 (System.currentTimeMillis() - mCreateTime) + " mSec ago";
669 }
Robert Greenwalt2034b912009-08-12 16:08:25 -0700670 }
671
Robert Greenwaltaffc3a12009-09-27 17:27:04 -0700672 // javadoc from interface
Robert Greenwalt2034b912009-08-12 16:08:25 -0700673 public int startUsingNetworkFeature(int networkType, String feature,
674 IBinder binder) {
675 if (DBG) {
Wink Savillee70c6f52010-12-03 12:01:38 -0800676 log("startUsingNetworkFeature for net " + networkType + ": " + feature);
Robert Greenwalt2034b912009-08-12 16:08:25 -0700677 }
The Android Open Source Project28527d22009-03-03 19:31:44 -0800678 enforceChangePermission();
Robert Greenwaltec05b3c2009-10-30 14:17:42 -0700679 if (!ConnectivityManager.isNetworkTypeValid(networkType) ||
680 mNetAttributes[networkType] == null) {
Robert Greenwalt2034b912009-08-12 16:08:25 -0700681 return Phone.APN_REQUEST_FAILED;
The Android Open Source Project28527d22009-03-03 19:31:44 -0800682 }
Robert Greenwalt2034b912009-08-12 16:08:25 -0700683
Robert Greenwaltaffc3a12009-09-27 17:27:04 -0700684 FeatureUser f = new FeatureUser(networkType, feature, binder);
Robert Greenwalt2034b912009-08-12 16:08:25 -0700685
686 // TODO - move this into the MobileDataStateTracker
687 int usedNetworkType = networkType;
688 if(networkType == ConnectivityManager.TYPE_MOBILE) {
Wink Savilleb7c92c72011-03-12 14:52:01 -0800689 usedNetworkType = convertFeatureToNetworkType(feature);
690 if (usedNetworkType < 0) {
691 Slog.e(TAG, "Can't match any netTracker!");
692 usedNetworkType = networkType;
Robert Greenwalt2034b912009-08-12 16:08:25 -0700693 }
694 }
695 NetworkStateTracker network = mNetTrackers[usedNetworkType];
696 if (network != null) {
Robert Greenwalt5364d752010-12-15 13:26:33 -0800697 Integer currentPid = new Integer(getCallingPid());
Robert Greenwalt2034b912009-08-12 16:08:25 -0700698 if (usedNetworkType != networkType) {
Robert Greenwalt2034b912009-08-12 16:08:25 -0700699 NetworkStateTracker radio = mNetTrackers[networkType];
700 NetworkInfo ni = network.getNetworkInfo();
701
702 if (ni.isAvailable() == false) {
Wink Savillee70c6f52010-12-03 12:01:38 -0800703 if (DBG) log("special network not available");
Robert Greenwalt2cc87442010-12-29 14:35:21 -0800704 if (!TextUtils.equals(feature,Phone.FEATURE_ENABLE_DUN_ALWAYS)) {
705 return Phone.APN_TYPE_NOT_AVAILABLE;
706 } else {
707 // else make the attempt anyway - probably giving REQUEST_STARTED below
708 }
Robert Greenwalt2034b912009-08-12 16:08:25 -0700709 }
710
Robert Greenwaltaffc3a12009-09-27 17:27:04 -0700711 synchronized(this) {
712 mFeatureUsers.add(f);
713 if (!mNetRequestersPids[usedNetworkType].contains(currentPid)) {
714 // this gets used for per-pid dns when connected
715 mNetRequestersPids[usedNetworkType].add(currentPid);
716 }
Robert Greenwalt2034b912009-08-12 16:08:25 -0700717 }
Robert Greenwalt6a2db8a2010-09-23 10:05:56 -0700718 mHandler.sendMessageDelayed(mHandler.obtainMessage(EVENT_RESTORE_DEFAULT_NETWORK,
Robert Greenwaltaffc3a12009-09-27 17:27:04 -0700719 f), getRestoreDefaultNetworkDelay());
720
Robert Greenwalt2034b912009-08-12 16:08:25 -0700721
Robert Greenwalta52c75a2009-08-19 20:19:33 -0700722 if ((ni.isConnectedOrConnecting() == true) &&
723 !network.isTeardownRequested()) {
Robert Greenwalt2034b912009-08-12 16:08:25 -0700724 if (ni.isConnected() == true) {
725 // add the pid-specific dns
Robert Greenwalt3afbead2010-07-23 15:46:26 -0700726 handleDnsConfigurationChange(networkType);
Wink Savillee70c6f52010-12-03 12:01:38 -0800727 if (DBG) log("special network already active");
Robert Greenwalt2034b912009-08-12 16:08:25 -0700728 return Phone.APN_ALREADY_ACTIVE;
729 }
Wink Savillee70c6f52010-12-03 12:01:38 -0800730 if (DBG) log("special network already connecting");
Robert Greenwalt2034b912009-08-12 16:08:25 -0700731 return Phone.APN_REQUEST_STARTED;
732 }
733
734 // check if the radio in play can make another contact
735 // assume if cannot for now
736
Wink Savillee70c6f52010-12-03 12:01:38 -0800737 if (DBG) log("reconnecting to special network");
Robert Greenwalt2034b912009-08-12 16:08:25 -0700738 network.reconnect();
739 return Phone.APN_REQUEST_STARTED;
740 } else {
Robert Greenwalt5364d752010-12-15 13:26:33 -0800741 // need to remember this unsupported request so we respond appropriately on stop
742 synchronized(this) {
743 mFeatureUsers.add(f);
744 if (!mNetRequestersPids[usedNetworkType].contains(currentPid)) {
745 // this gets used for per-pid dns when connected
746 mNetRequestersPids[usedNetworkType].add(currentPid);
747 }
748 }
Robert Greenwaltd391e892010-05-18 10:52:51 -0700749 return -1;
Robert Greenwalt2034b912009-08-12 16:08:25 -0700750 }
751 }
752 return Phone.APN_TYPE_NOT_AVAILABLE;
The Android Open Source Project28527d22009-03-03 19:31:44 -0800753 }
754
Robert Greenwaltaffc3a12009-09-27 17:27:04 -0700755 // javadoc from interface
The Android Open Source Project28527d22009-03-03 19:31:44 -0800756 public int stopUsingNetworkFeature(int networkType, String feature) {
Robert Greenwalt28f43012009-10-06 17:52:40 -0700757 enforceChangePermission();
758
Robert Greenwaltaffc3a12009-09-27 17:27:04 -0700759 int pid = getCallingPid();
760 int uid = getCallingUid();
761
762 FeatureUser u = null;
763 boolean found = false;
764
765 synchronized(this) {
766 for (int i = 0; i < mFeatureUsers.size() ; i++) {
767 u = (FeatureUser)mFeatureUsers.get(i);
768 if (uid == u.mUid && pid == u.mPid &&
769 networkType == u.mNetworkType &&
770 TextUtils.equals(feature, u.mFeature)) {
771 found = true;
772 break;
773 }
774 }
775 }
776 if (found && u != null) {
777 // stop regardless of how many other time this proc had called start
778 return stopUsingNetworkFeature(u, true);
779 } else {
780 // none found!
Wink Savillee70c6f52010-12-03 12:01:38 -0800781 if (DBG) log("ignoring stopUsingNetworkFeature - not a live request");
Robert Greenwaltaffc3a12009-09-27 17:27:04 -0700782 return 1;
783 }
Robert Greenwalt2034b912009-08-12 16:08:25 -0700784 }
785
Robert Greenwaltaffc3a12009-09-27 17:27:04 -0700786 private int stopUsingNetworkFeature(FeatureUser u, boolean ignoreDups) {
787 int networkType = u.mNetworkType;
788 String feature = u.mFeature;
789 int pid = u.mPid;
790 int uid = u.mUid;
791
792 NetworkStateTracker tracker = null;
793 boolean callTeardown = false; // used to carry our decision outside of sync block
794
Robert Greenwalt2034b912009-08-12 16:08:25 -0700795 if (DBG) {
Wink Savillee70c6f52010-12-03 12:01:38 -0800796 log("stopUsingNetworkFeature for net " + networkType +
Robert Greenwalt2034b912009-08-12 16:08:25 -0700797 ": " + feature);
798 }
Robert Greenwalt28f43012009-10-06 17:52:40 -0700799
The Android Open Source Project28527d22009-03-03 19:31:44 -0800800 if (!ConnectivityManager.isNetworkTypeValid(networkType)) {
801 return -1;
802 }
Robert Greenwalt2034b912009-08-12 16:08:25 -0700803
Robert Greenwaltaffc3a12009-09-27 17:27:04 -0700804 // need to link the mFeatureUsers list with the mNetRequestersPids state in this
805 // sync block
806 synchronized(this) {
807 // check if this process still has an outstanding start request
808 if (!mFeatureUsers.contains(u)) {
Wink Savillee70c6f52010-12-03 12:01:38 -0800809 if (DBG) log("ignoring - this process has no outstanding requests");
Robert Greenwalt2034b912009-08-12 16:08:25 -0700810 return 1;
811 }
Robert Greenwaltaffc3a12009-09-27 17:27:04 -0700812 u.unlinkDeathRecipient();
813 mFeatureUsers.remove(mFeatureUsers.indexOf(u));
814 // If we care about duplicate requests, check for that here.
815 //
816 // This is done to support the extension of a request - the app
817 // can request we start the network feature again and renew the
818 // auto-shutoff delay. Normal "stop" calls from the app though
819 // do not pay attention to duplicate requests - in effect the
820 // API does not refcount and a single stop will counter multiple starts.
821 if (ignoreDups == false) {
822 for (int i = 0; i < mFeatureUsers.size() ; i++) {
823 FeatureUser x = (FeatureUser)mFeatureUsers.get(i);
824 if (x.mUid == u.mUid && x.mPid == u.mPid &&
825 x.mNetworkType == u.mNetworkType &&
826 TextUtils.equals(x.mFeature, u.mFeature)) {
Wink Savillee70c6f52010-12-03 12:01:38 -0800827 if (DBG) log("ignoring stopUsingNetworkFeature as dup is found");
Robert Greenwaltaffc3a12009-09-27 17:27:04 -0700828 return 1;
829 }
830 }
Robert Greenwalt2034b912009-08-12 16:08:25 -0700831 }
Robert Greenwaltaffc3a12009-09-27 17:27:04 -0700832
833 // TODO - move to MobileDataStateTracker
834 int usedNetworkType = networkType;
835 if (networkType == ConnectivityManager.TYPE_MOBILE) {
Wink Savilleb7c92c72011-03-12 14:52:01 -0800836 usedNetworkType = convertFeatureToNetworkType(feature);
837 if (usedNetworkType < 0) {
838 usedNetworkType = networkType;
Robert Greenwaltaffc3a12009-09-27 17:27:04 -0700839 }
840 }
841 tracker = mNetTrackers[usedNetworkType];
Robert Greenwaltec05b3c2009-10-30 14:17:42 -0700842 if (tracker == null) {
Wink Savillee70c6f52010-12-03 12:01:38 -0800843 if (DBG) log("ignoring - no known tracker for net type " + usedNetworkType);
Robert Greenwaltec05b3c2009-10-30 14:17:42 -0700844 return -1;
845 }
846 if (usedNetworkType != networkType) {
Robert Greenwaltaffc3a12009-09-27 17:27:04 -0700847 Integer currentPid = new Integer(pid);
Robert Greenwaltaffc3a12009-09-27 17:27:04 -0700848 mNetRequestersPids[usedNetworkType].remove(currentPid);
Robert Greenwalt0ca68a02009-12-17 14:54:59 -0800849 reassessPidDns(pid, true);
Robert Greenwaltaffc3a12009-09-27 17:27:04 -0700850 if (mNetRequestersPids[usedNetworkType].size() != 0) {
Wink Savillee70c6f52010-12-03 12:01:38 -0800851 if (DBG) log("not tearing down special network - " +
Robert Greenwaltaffc3a12009-09-27 17:27:04 -0700852 "others still using it");
853 return 1;
854 }
855 callTeardown = true;
Robert Greenwalt9f3be4c2011-01-10 11:58:31 -0800856 } else {
857 if (DBG) log("not a known feature - dropping");
Robert Greenwaltaffc3a12009-09-27 17:27:04 -0700858 }
859 }
Wink Savillee70c6f52010-12-03 12:01:38 -0800860 if (DBG) log("Doing network teardown");
Robert Greenwaltaffc3a12009-09-27 17:27:04 -0700861 if (callTeardown) {
862 tracker.teardown();
Robert Greenwalt2034b912009-08-12 16:08:25 -0700863 return 1;
864 } else {
Robert Greenwaltd391e892010-05-18 10:52:51 -0700865 return -1;
Robert Greenwalt2034b912009-08-12 16:08:25 -0700866 }
The Android Open Source Project28527d22009-03-03 19:31:44 -0800867 }
868
869 /**
Robert Greenwalt7fe44cb2010-08-27 09:24:29 -0700870 * @deprecated use requestRouteToHostAddress instead
871 *
The Android Open Source Project28527d22009-03-03 19:31:44 -0800872 * Ensure that a network route exists to deliver traffic to the specified
873 * host via the specified network interface.
Robert Greenwalt0659da32009-07-16 17:21:39 -0700874 * @param networkType the type of the network over which traffic to the
875 * specified host is to be routed
876 * @param hostAddress the IP address of the host to which the route is
877 * desired
The Android Open Source Project28527d22009-03-03 19:31:44 -0800878 * @return {@code true} on success, {@code false} on failure
879 */
880 public boolean requestRouteToHost(int networkType, int hostAddress) {
Robert Greenwalt7fe44cb2010-08-27 09:24:29 -0700881 InetAddress inetAddress = NetworkUtils.intToInetAddress(hostAddress);
882
883 if (inetAddress == null) {
884 return false;
885 }
886
887 return requestRouteToHostAddress(networkType, inetAddress.getAddress());
888 }
889
890 /**
891 * Ensure that a network route exists to deliver traffic to the specified
892 * host via the specified network interface.
893 * @param networkType the type of the network over which traffic to the
894 * specified host is to be routed
895 * @param hostAddress the IP address of the host to which the route is
896 * desired
897 * @return {@code true} on success, {@code false} on failure
898 */
899 public boolean requestRouteToHostAddress(int networkType, byte[] hostAddress) {
The Android Open Source Project28527d22009-03-03 19:31:44 -0800900 enforceChangePermission();
901 if (!ConnectivityManager.isNetworkTypeValid(networkType)) {
902 return false;
903 }
904 NetworkStateTracker tracker = mNetTrackers[networkType];
Robert Greenwalt4666ed02009-09-10 15:06:20 -0700905
Robert Greenwaltec05b3c2009-10-30 14:17:42 -0700906 if (tracker == null || !tracker.getNetworkInfo().isConnected() ||
907 tracker.isTeardownRequested()) {
Robert Greenwalt4666ed02009-09-10 15:06:20 -0700908 if (DBG) {
Wink Savillee70c6f52010-12-03 12:01:38 -0800909 log("requestRouteToHostAddress on down network " +
Robert Greenwalt7fe44cb2010-08-27 09:24:29 -0700910 "(" + networkType + ") - dropped");
Robert Greenwalt4666ed02009-09-10 15:06:20 -0700911 }
912 return false;
The Android Open Source Project28527d22009-03-03 19:31:44 -0800913 }
Robert Greenwalta7dfbd32010-06-15 15:43:39 -0700914 try {
Robert Greenwalt7fe44cb2010-08-27 09:24:29 -0700915 InetAddress addr = InetAddress.getByAddress(hostAddress);
Robert Greenwalta7dfbd32010-06-15 15:43:39 -0700916 return addHostRoute(tracker, addr);
917 } catch (UnknownHostException e) {}
918 return false;
Irfan Sheriff7f132d92010-06-09 15:39:36 -0700919 }
920
921 /**
922 * Ensure that a network route exists to deliver traffic to the specified
923 * host via the mobile data network.
924 * @param hostAddress the IP address of the host to which the route is desired,
925 * in network byte order.
Robert Greenwalta7dfbd32010-06-15 15:43:39 -0700926 * TODO - deprecate
Irfan Sheriff7f132d92010-06-09 15:39:36 -0700927 * @return {@code true} on success, {@code false} on failure
928 */
Robert Greenwalta7dfbd32010-06-15 15:43:39 -0700929 private boolean addHostRoute(NetworkStateTracker nt, InetAddress hostAddress) {
Irfan Sheriff7f132d92010-06-09 15:39:36 -0700930 if (nt.getNetworkInfo().getType() == ConnectivityManager.TYPE_WIFI) {
931 return false;
932 }
933
Robert Greenwalt1f1bcfe2010-08-30 10:56:47 -0700934 LinkProperties p = nt.getLinkProperties();
Robert Greenwalta7dfbd32010-06-15 15:43:39 -0700935 if (p == null) return false;
936 String interfaceName = p.getInterfaceName();
Irfan Sheriff7f132d92010-06-09 15:39:36 -0700937
938 if (DBG) {
Wink Savillee70c6f52010-12-03 12:01:38 -0800939 log("Requested host route to " + hostAddress + "(" + interfaceName + ")");
Irfan Sheriff7f132d92010-06-09 15:39:36 -0700940 }
Robert Greenwalta7dfbd32010-06-15 15:43:39 -0700941 if (interfaceName != null) {
Robert Greenwalt7fe44cb2010-08-27 09:24:29 -0700942 return NetworkUtils.addHostRoute(interfaceName, hostAddress, null);
Irfan Sheriff7f132d92010-06-09 15:39:36 -0700943 } else {
Wink Savillee70c6f52010-12-03 12:01:38 -0800944 if (DBG) loge("addHostRoute failed due to null interface name");
Irfan Sheriff7f132d92010-06-09 15:39:36 -0700945 return false;
946 }
The Android Open Source Project28527d22009-03-03 19:31:44 -0800947 }
948
949 /**
950 * @see ConnectivityManager#getBackgroundDataSetting()
951 */
952 public boolean getBackgroundDataSetting() {
Robert Greenwaltd62c7002010-12-29 16:15:02 -0800953 return mBackgroundDataEnabled.get();
The Android Open Source Project28527d22009-03-03 19:31:44 -0800954 }
Robert Greenwalt0659da32009-07-16 17:21:39 -0700955
The Android Open Source Project28527d22009-03-03 19:31:44 -0800956 /**
957 * @see ConnectivityManager#setBackgroundDataSetting(boolean)
958 */
959 public void setBackgroundDataSetting(boolean allowBackgroundDataUsage) {
960 mContext.enforceCallingOrSelfPermission(
961 android.Manifest.permission.CHANGE_BACKGROUND_DATA_SETTING,
962 "ConnectivityService");
Robert Greenwalt0659da32009-07-16 17:21:39 -0700963
Robert Greenwaltd62c7002010-12-29 16:15:02 -0800964 mBackgroundDataEnabled.set(allowBackgroundDataUsage);
965
Robert Greenwalt6a2db8a2010-09-23 10:05:56 -0700966 mHandler.sendMessage(mHandler.obtainMessage(EVENT_SET_BACKGROUND_DATA,
967 (allowBackgroundDataUsage ? ENABLED : DISABLED), 0));
968 }
The Android Open Source Project28527d22009-03-03 19:31:44 -0800969
Robert Greenwalt6a2db8a2010-09-23 10:05:56 -0700970 private void handleSetBackgroundData(boolean enabled) {
Robert Greenwalt0ffdef12011-02-25 13:44:09 -0800971 Settings.Secure.putInt(mContext.getContentResolver(),
972 Settings.Secure.BACKGROUND_DATA, enabled ? 1 : 0);
973 Intent broadcast = new Intent(
974 ConnectivityManager.ACTION_BACKGROUND_DATA_SETTING_CHANGED);
975 mContext.sendBroadcast(broadcast);
Robert Greenwalt0659da32009-07-16 17:21:39 -0700976 }
977
Robert Greenwalt1b21f6c2010-02-23 18:58:05 -0800978 /**
979 * @see ConnectivityManager#getMobileDataEnabled()
980 */
981 public boolean getMobileDataEnabled() {
Wink Savilleb9024c62010-12-07 10:31:02 -0800982 // TODO: This detail should probably be in DataConnectionTracker's
983 // which is where we store the value and maybe make this
984 // asynchronous.
Robert Greenwalt1b21f6c2010-02-23 18:58:05 -0800985 enforceAccessPermission();
986 boolean retVal = Settings.Secure.getInt(mContext.getContentResolver(),
987 Settings.Secure.MOBILE_DATA, 1) == 1;
Wink Savillee70c6f52010-12-03 12:01:38 -0800988 if (DBG) log("getMobileDataEnabled returning " + retVal);
Robert Greenwalt1b21f6c2010-02-23 18:58:05 -0800989 return retVal;
990 }
991
992 /**
993 * @see ConnectivityManager#setMobileDataEnabled(boolean)
994 */
Robert Greenwalt6a2db8a2010-09-23 10:05:56 -0700995 public void setMobileDataEnabled(boolean enabled) {
Robert Greenwalt1b21f6c2010-02-23 18:58:05 -0800996 enforceChangePermission();
Wink Savillee70c6f52010-12-03 12:01:38 -0800997 if (DBG) log("setMobileDataEnabled(" + enabled + ")");
Robert Greenwalt1b21f6c2010-02-23 18:58:05 -0800998
Robert Greenwalt6a2db8a2010-09-23 10:05:56 -0700999 mHandler.sendMessage(mHandler.obtainMessage(EVENT_SET_MOBILE_DATA,
1000 (enabled ? ENABLED : DISABLED), 0));
1001 }
1002
1003 private void handleSetMobileData(boolean enabled) {
Wink Savilleb9024c62010-12-07 10:31:02 -08001004 if (mNetTrackers[ConnectivityManager.TYPE_MOBILE] != null) {
1005 if (DBG) {
1006 Slog.d(TAG, mNetTrackers[ConnectivityManager.TYPE_MOBILE].toString() + enabled);
Robert Greenwalt1b21f6c2010-02-23 18:58:05 -08001007 }
Wink Savilleb9024c62010-12-07 10:31:02 -08001008 mNetTrackers[ConnectivityManager.TYPE_MOBILE].setDataEnable(enabled);
Robert Greenwalt1b21f6c2010-02-23 18:58:05 -08001009 }
1010 }
1011
The Android Open Source Project28527d22009-03-03 19:31:44 -08001012 private void enforceAccessPermission() {
Robert Greenwalt0659da32009-07-16 17:21:39 -07001013 mContext.enforceCallingOrSelfPermission(
1014 android.Manifest.permission.ACCESS_NETWORK_STATE,
1015 "ConnectivityService");
The Android Open Source Project28527d22009-03-03 19:31:44 -08001016 }
1017
1018 private void enforceChangePermission() {
Robert Greenwalt0659da32009-07-16 17:21:39 -07001019 mContext.enforceCallingOrSelfPermission(
1020 android.Manifest.permission.CHANGE_NETWORK_STATE,
1021 "ConnectivityService");
The Android Open Source Project28527d22009-03-03 19:31:44 -08001022 }
1023
Robert Greenwalt0c4828c2010-01-26 11:40:34 -08001024 // TODO Make this a special check when it goes public
1025 private void enforceTetherChangePermission() {
1026 mContext.enforceCallingOrSelfPermission(
1027 android.Manifest.permission.CHANGE_NETWORK_STATE,
1028 "ConnectivityService");
1029 }
1030
Robert Greenwalt8e87f122010-02-11 18:18:40 -08001031 private void enforceTetherAccessPermission() {
1032 mContext.enforceCallingOrSelfPermission(
1033 android.Manifest.permission.ACCESS_NETWORK_STATE,
1034 "ConnectivityService");
1035 }
1036
Robert Greenwalt93dc1042010-06-15 12:19:37 -07001037 private void enforceConnectivityInternalPermission() {
1038 mContext.enforceCallingOrSelfPermission(
1039 android.Manifest.permission.CONNECTIVITY_INTERNAL,
1040 "ConnectivityService");
1041 }
1042
The Android Open Source Project28527d22009-03-03 19:31:44 -08001043 /**
Robert Greenwalt0659da32009-07-16 17:21:39 -07001044 * Handle a {@code DISCONNECTED} event. If this pertains to the non-active
1045 * network, we ignore it. If it is for the active network, we send out a
1046 * broadcast. But first, we check whether it might be possible to connect
1047 * to a different network.
The Android Open Source Project28527d22009-03-03 19:31:44 -08001048 * @param info the {@code NetworkInfo} for the network
1049 */
1050 private void handleDisconnect(NetworkInfo info) {
1051
Robert Greenwalt2034b912009-08-12 16:08:25 -07001052 int prevNetType = info.getType();
The Android Open Source Project28527d22009-03-03 19:31:44 -08001053
Robert Greenwalt2034b912009-08-12 16:08:25 -07001054 mNetTrackers[prevNetType].setTeardownRequested(false);
The Android Open Source Project28527d22009-03-03 19:31:44 -08001055 /*
1056 * If the disconnected network is not the active one, then don't report
1057 * this as a loss of connectivity. What probably happened is that we're
1058 * getting the disconnect for a network that we explicitly disabled
1059 * in accordance with network preference policies.
1060 */
Robert Greenwalt2034b912009-08-12 16:08:25 -07001061 if (!mNetAttributes[prevNetType].isDefault()) {
1062 List pids = mNetRequestersPids[prevNetType];
1063 for (int i = 0; i<pids.size(); i++) {
1064 Integer pid = (Integer)pids.get(i);
1065 // will remove them because the net's no longer connected
1066 // need to do this now as only now do we know the pids and
1067 // can properly null things that are no longer referenced.
1068 reassessPidDns(pid.intValue(), false);
The Android Open Source Project28527d22009-03-03 19:31:44 -08001069 }
The Android Open Source Project28527d22009-03-03 19:31:44 -08001070 }
1071
The Android Open Source Project28527d22009-03-03 19:31:44 -08001072 Intent intent = new Intent(ConnectivityManager.CONNECTIVITY_ACTION);
1073 intent.putExtra(ConnectivityManager.EXTRA_NETWORK_INFO, info);
1074 if (info.isFailover()) {
1075 intent.putExtra(ConnectivityManager.EXTRA_IS_FAILOVER, true);
1076 info.setFailover(false);
1077 }
1078 if (info.getReason() != null) {
1079 intent.putExtra(ConnectivityManager.EXTRA_REASON, info.getReason());
1080 }
1081 if (info.getExtraInfo() != null) {
Robert Greenwalt0659da32009-07-16 17:21:39 -07001082 intent.putExtra(ConnectivityManager.EXTRA_EXTRA_INFO,
1083 info.getExtraInfo());
The Android Open Source Project28527d22009-03-03 19:31:44 -08001084 }
Robert Greenwalt2034b912009-08-12 16:08:25 -07001085
Robert Greenwalt3cc68d32010-01-25 17:54:29 -08001086 if (mNetAttributes[prevNetType].isDefault()) {
Robert Greenwalt4b7c55e2011-01-11 13:56:33 -08001087 tryFailover(prevNetType);
1088 if (mActiveDefaultNetwork != -1) {
1089 NetworkInfo switchTo = mNetTrackers[mActiveDefaultNetwork].getNetworkInfo();
Robert Greenwalt3cc68d32010-01-25 17:54:29 -08001090 intent.putExtra(ConnectivityManager.EXTRA_OTHER_NETWORK_INFO, switchTo);
1091 } else {
Robert Greenwaltb2a9e492010-09-20 18:01:43 -07001092 mDefaultInetConditionPublished = 0; // we're not connected anymore
Robert Greenwalt3cc68d32010-01-25 17:54:29 -08001093 intent.putExtra(ConnectivityManager.EXTRA_NO_CONNECTIVITY, true);
1094 }
Robert Greenwaltf55ced92010-01-20 19:29:41 -08001095 }
Robert Greenwaltb2a9e492010-09-20 18:01:43 -07001096 intent.putExtra(ConnectivityManager.EXTRA_INET_CONDITION, mDefaultInetConditionPublished);
Robert Greenwaltf55ced92010-01-20 19:29:41 -08001097 // do this before we broadcast the change
Robert Greenwalt3afbead2010-07-23 15:46:26 -07001098 handleConnectivityChange(prevNetType);
Robert Greenwaltf55ced92010-01-20 19:29:41 -08001099
1100 sendStickyBroadcast(intent);
1101 /*
1102 * If the failover network is already connected, then immediately send
1103 * out a followup broadcast indicating successful failover
1104 */
Robert Greenwalt4b7c55e2011-01-11 13:56:33 -08001105 if (mActiveDefaultNetwork != -1) {
1106 sendConnectedBroadcast(mNetTrackers[mActiveDefaultNetwork].getNetworkInfo());
Robert Greenwaltf55ced92010-01-20 19:29:41 -08001107 }
1108 }
1109
Robert Greenwalt4b7c55e2011-01-11 13:56:33 -08001110 private void tryFailover(int prevNetType) {
Robert Greenwalt2034b912009-08-12 16:08:25 -07001111 /*
Robert Greenwalt92564852011-01-06 15:41:07 -08001112 * If this is a default network, check if other defaults are available.
1113 * Try to reconnect on all available and let them hash it out when
1114 * more than one connects.
Robert Greenwalt2034b912009-08-12 16:08:25 -07001115 */
Robert Greenwalt2034b912009-08-12 16:08:25 -07001116 if (mNetAttributes[prevNetType].isDefault()) {
Robert Greenwalt2034b912009-08-12 16:08:25 -07001117 if (mActiveDefaultNetwork == prevNetType) {
1118 mActiveDefaultNetwork = -1;
1119 }
1120
Robert Greenwalt4b7c55e2011-01-11 13:56:33 -08001121 // don't signal a reconnect for anything lower or equal priority than our
1122 // current connected default
1123 // TODO - don't filter by priority now - nice optimization but risky
1124// int currentPriority = -1;
1125// if (mActiveDefaultNetwork != -1) {
1126// currentPriority = mNetAttributes[mActiveDefaultNetwork].mPriority;
1127// }
Robert Greenwaltf55ced92010-01-20 19:29:41 -08001128 for (int checkType=0; checkType <= ConnectivityManager.MAX_NETWORK_TYPE; checkType++) {
Robert Greenwaltec05b3c2009-10-30 14:17:42 -07001129 if (checkType == prevNetType) continue;
1130 if (mNetAttributes[checkType] == null) continue;
Robert Greenwalt92564852011-01-06 15:41:07 -08001131 if (!mNetAttributes[checkType].isDefault()) continue;
Wink Saville72a95b92011-01-26 15:43:49 -08001132
1133// Enabling the isAvailable() optimization caused mobile to not get
1134// selected if it was in the middle of error handling. Specifically
1135// a moble connection that took 30 seconds to complete the DEACTIVATE_DATA_CALL
1136// would not be available and we wouldn't get connected to anything.
1137// So removing the isAvailable() optimization below for now. TODO: This
1138// optimization should work and we need to investigate why it doesn't work.
1139// This could be related to how DEACTIVATE_DATA_CALL is reporting its
1140// complete before it is really complete.
1141// if (!mNetTrackers[checkType].isAvailable()) continue;
1142
Robert Greenwalt4b7c55e2011-01-11 13:56:33 -08001143// if (currentPriority >= mNetAttributes[checkType].mPriority) continue;
Robert Greenwalt2034b912009-08-12 16:08:25 -07001144
Robert Greenwalt4b7c55e2011-01-11 13:56:33 -08001145 NetworkStateTracker checkTracker = mNetTrackers[checkType];
1146 NetworkInfo checkInfo = checkTracker.getNetworkInfo();
1147 if (!checkInfo.isConnectedOrConnecting() || checkTracker.isTeardownRequested()) {
1148 checkInfo.setFailover(true);
1149 checkTracker.reconnect();
Robert Greenwalt2034b912009-08-12 16:08:25 -07001150 }
Robert Greenwalt4b7c55e2011-01-11 13:56:33 -08001151 if (DBG) log("Attempting to switch to " + checkInfo.getTypeName());
Robert Greenwalt2034b912009-08-12 16:08:25 -07001152 }
The Android Open Source Project28527d22009-03-03 19:31:44 -08001153 }
The Android Open Source Project28527d22009-03-03 19:31:44 -08001154 }
1155
1156 private void sendConnectedBroadcast(NetworkInfo info) {
Robert Greenwaltd3401f92010-09-15 17:36:33 -07001157 sendGeneralBroadcast(info, ConnectivityManager.CONNECTIVITY_ACTION);
1158 }
1159
1160 private void sendInetConditionBroadcast(NetworkInfo info) {
1161 sendGeneralBroadcast(info, ConnectivityManager.INET_CONDITION_ACTION);
1162 }
1163
1164 private void sendGeneralBroadcast(NetworkInfo info, String bcastType) {
1165 Intent intent = new Intent(bcastType);
The Android Open Source Project28527d22009-03-03 19:31:44 -08001166 intent.putExtra(ConnectivityManager.EXTRA_NETWORK_INFO, info);
1167 if (info.isFailover()) {
1168 intent.putExtra(ConnectivityManager.EXTRA_IS_FAILOVER, true);
1169 info.setFailover(false);
1170 }
1171 if (info.getReason() != null) {
1172 intent.putExtra(ConnectivityManager.EXTRA_REASON, info.getReason());
1173 }
1174 if (info.getExtraInfo() != null) {
Robert Greenwalt0659da32009-07-16 17:21:39 -07001175 intent.putExtra(ConnectivityManager.EXTRA_EXTRA_INFO,
1176 info.getExtraInfo());
The Android Open Source Project28527d22009-03-03 19:31:44 -08001177 }
Robert Greenwalt986c7412010-09-08 15:24:47 -07001178 intent.putExtra(ConnectivityManager.EXTRA_INET_CONDITION, mDefaultInetConditionPublished);
Mike Lockwoodfde2b762009-08-14 14:18:49 -04001179 sendStickyBroadcast(intent);
The Android Open Source Project28527d22009-03-03 19:31:44 -08001180 }
1181
1182 /**
1183 * Called when an attempt to fail over to another network has failed.
1184 * @param info the {@link NetworkInfo} for the failed network
1185 */
1186 private void handleConnectionFailure(NetworkInfo info) {
1187 mNetTrackers[info.getType()].setTeardownRequested(false);
The Android Open Source Project28527d22009-03-03 19:31:44 -08001188
Robert Greenwalt2034b912009-08-12 16:08:25 -07001189 String reason = info.getReason();
1190 String extraInfo = info.getExtraInfo();
Robert Greenwalt0659da32009-07-16 17:21:39 -07001191
Robert Greenwalte981bc52010-10-08 16:35:52 -07001192 String reasonText;
1193 if (reason == null) {
1194 reasonText = ".";
1195 } else {
1196 reasonText = " (" + reason + ").";
The Android Open Source Project28527d22009-03-03 19:31:44 -08001197 }
Wink Savillee70c6f52010-12-03 12:01:38 -08001198 loge("Attempt to connect to " + info.getTypeName() + " failed" + reasonText);
Robert Greenwalt2034b912009-08-12 16:08:25 -07001199
1200 Intent intent = new Intent(ConnectivityManager.CONNECTIVITY_ACTION);
1201 intent.putExtra(ConnectivityManager.EXTRA_NETWORK_INFO, info);
1202 if (getActiveNetworkInfo() == null) {
1203 intent.putExtra(ConnectivityManager.EXTRA_NO_CONNECTIVITY, true);
1204 }
1205 if (reason != null) {
1206 intent.putExtra(ConnectivityManager.EXTRA_REASON, reason);
1207 }
1208 if (extraInfo != null) {
1209 intent.putExtra(ConnectivityManager.EXTRA_EXTRA_INFO, extraInfo);
1210 }
1211 if (info.isFailover()) {
1212 intent.putExtra(ConnectivityManager.EXTRA_IS_FAILOVER, true);
1213 info.setFailover(false);
1214 }
Robert Greenwaltf55ced92010-01-20 19:29:41 -08001215
Robert Greenwalt3cc68d32010-01-25 17:54:29 -08001216 if (mNetAttributes[info.getType()].isDefault()) {
Robert Greenwalt4b7c55e2011-01-11 13:56:33 -08001217 tryFailover(info.getType());
1218 if (mActiveDefaultNetwork != -1) {
1219 NetworkInfo switchTo = mNetTrackers[mActiveDefaultNetwork].getNetworkInfo();
Robert Greenwalt3cc68d32010-01-25 17:54:29 -08001220 intent.putExtra(ConnectivityManager.EXTRA_OTHER_NETWORK_INFO, switchTo);
1221 } else {
Robert Greenwaltb2a9e492010-09-20 18:01:43 -07001222 mDefaultInetConditionPublished = 0;
Robert Greenwalt3cc68d32010-01-25 17:54:29 -08001223 intent.putExtra(ConnectivityManager.EXTRA_NO_CONNECTIVITY, true);
1224 }
Robert Greenwaltf55ced92010-01-20 19:29:41 -08001225 }
Robert Greenwalt3cc68d32010-01-25 17:54:29 -08001226
Robert Greenwaltb2a9e492010-09-20 18:01:43 -07001227 intent.putExtra(ConnectivityManager.EXTRA_INET_CONDITION, mDefaultInetConditionPublished);
Mike Lockwoodfde2b762009-08-14 14:18:49 -04001228 sendStickyBroadcast(intent);
Robert Greenwaltf55ced92010-01-20 19:29:41 -08001229 /*
1230 * If the failover network is already connected, then immediately send
1231 * out a followup broadcast indicating successful failover
1232 */
Robert Greenwalt4b7c55e2011-01-11 13:56:33 -08001233 if (mActiveDefaultNetwork != -1) {
1234 sendConnectedBroadcast(mNetTrackers[mActiveDefaultNetwork].getNetworkInfo());
Robert Greenwaltf55ced92010-01-20 19:29:41 -08001235 }
Mike Lockwoodfde2b762009-08-14 14:18:49 -04001236 }
1237
1238 private void sendStickyBroadcast(Intent intent) {
1239 synchronized(this) {
Dianne Hackborna417ff82009-12-08 19:45:14 -08001240 if (!mSystemReady) {
1241 mInitialBroadcast = new Intent(intent);
Mike Lockwoodfde2b762009-08-14 14:18:49 -04001242 }
Dianne Hackborna417ff82009-12-08 19:45:14 -08001243 intent.addFlags(Intent.FLAG_RECEIVER_REGISTERED_ONLY_BEFORE_BOOT);
1244 mContext.sendStickyBroadcast(intent);
Mike Lockwoodfde2b762009-08-14 14:18:49 -04001245 }
1246 }
1247
1248 void systemReady() {
1249 synchronized(this) {
1250 mSystemReady = true;
Dianne Hackborna417ff82009-12-08 19:45:14 -08001251 if (mInitialBroadcast != null) {
1252 mContext.sendStickyBroadcast(mInitialBroadcast);
1253 mInitialBroadcast = null;
Mike Lockwoodfde2b762009-08-14 14:18:49 -04001254 }
1255 }
Robert Greenwaltc3c5f862010-10-11 16:00:27 -07001256 // load the global proxy at startup
1257 mHandler.sendMessage(mHandler.obtainMessage(EVENT_APPLY_GLOBAL_HTTP_PROXY));
The Android Open Source Project28527d22009-03-03 19:31:44 -08001258 }
1259
1260 private void handleConnect(NetworkInfo info) {
Robert Greenwalt2034b912009-08-12 16:08:25 -07001261 int type = info.getType();
The Android Open Source Project28527d22009-03-03 19:31:44 -08001262
1263 // snapshot isFailover, because sendConnectedBroadcast() resets it
1264 boolean isFailover = info.isFailover();
Robert Greenwalt2034b912009-08-12 16:08:25 -07001265 NetworkStateTracker thisNet = mNetTrackers[type];
The Android Open Source Project28527d22009-03-03 19:31:44 -08001266
Robert Greenwalt2034b912009-08-12 16:08:25 -07001267 // if this is a default net and other default is running
1268 // kill the one not preferred
1269 if (mNetAttributes[type].isDefault()) {
Robert Greenwalt2034b912009-08-12 16:08:25 -07001270 if (mActiveDefaultNetwork != -1 && mActiveDefaultNetwork != type) {
1271 if ((type != mNetworkPreference &&
1272 mNetAttributes[mActiveDefaultNetwork].mPriority >
1273 mNetAttributes[type].mPriority) ||
1274 mNetworkPreference == mActiveDefaultNetwork) {
1275 // don't accept this one
Wink Savillee70c6f52010-12-03 12:01:38 -08001276 if (DBG) {
1277 log("Not broadcasting CONNECT_ACTION " +
Robert Greenwalt2034b912009-08-12 16:08:25 -07001278 "to torn down network " + info.getTypeName());
Wink Savillee70c6f52010-12-03 12:01:38 -08001279 }
Robert Greenwalt2034b912009-08-12 16:08:25 -07001280 teardown(thisNet);
1281 return;
1282 } else {
1283 // tear down the other
1284 NetworkStateTracker otherNet =
1285 mNetTrackers[mActiveDefaultNetwork];
Wink Savillee70c6f52010-12-03 12:01:38 -08001286 if (DBG) {
1287 log("Policy requires " + otherNet.getNetworkInfo().getTypeName() +
Robert Greenwalt2034b912009-08-12 16:08:25 -07001288 " teardown");
Wink Savillee70c6f52010-12-03 12:01:38 -08001289 }
Robert Greenwalt2034b912009-08-12 16:08:25 -07001290 if (!teardown(otherNet)) {
Wink Savillee70c6f52010-12-03 12:01:38 -08001291 loge("Network declined teardown request");
Robert Greenwalt99910172011-03-29 11:36:28 -07001292 teardown(thisNet);
Robert Greenwalt2034b912009-08-12 16:08:25 -07001293 return;
1294 }
Robert Greenwalt93dc1042010-06-15 12:19:37 -07001295 }
1296 }
1297 synchronized (ConnectivityService.this) {
1298 // have a new default network, release the transition wakelock in a second
1299 // if it's held. The second pause is to allow apps to reconnect over the
1300 // new network
1301 if (mNetTransitionWakeLock.isHeld()) {
1302 mHandler.sendMessageDelayed(mHandler.obtainMessage(
Robert Greenwaltccb36f92010-09-24 14:32:21 -07001303 EVENT_CLEAR_NET_TRANSITION_WAKELOCK,
Robert Greenwalt93dc1042010-06-15 12:19:37 -07001304 mNetTransitionWakeLockSerialNumber, 0),
1305 1000);
Robert Greenwalt2034b912009-08-12 16:08:25 -07001306 }
1307 }
1308 mActiveDefaultNetwork = type;
Robert Greenwalt986c7412010-09-08 15:24:47 -07001309 // this will cause us to come up initially as unconnected and switching
1310 // to connected after our normal pause unless somebody reports us as reall
1311 // disconnected
1312 mDefaultInetConditionPublished = 0;
1313 mDefaultConnectionSequence++;
1314 mInetConditionChangeInFlight = false;
1315 // Don't do this - if we never sign in stay, grey
1316 //reportNetworkCondition(mActiveDefaultNetwork, 100);
Robert Greenwalt2034b912009-08-12 16:08:25 -07001317 }
The Android Open Source Project28527d22009-03-03 19:31:44 -08001318 thisNet.setTeardownRequested(false);
Irfan Sheriff7f132d92010-06-09 15:39:36 -07001319 updateNetworkSettings(thisNet);
Robert Greenwalt3afbead2010-07-23 15:46:26 -07001320 handleConnectivityChange(type);
Robert Greenwalt2034b912009-08-12 16:08:25 -07001321 sendConnectedBroadcast(info);
The Android Open Source Project28527d22009-03-03 19:31:44 -08001322 }
1323
The Android Open Source Project28527d22009-03-03 19:31:44 -08001324 /**
Robert Greenwalt3afbead2010-07-23 15:46:26 -07001325 * After a change in the connectivity state of a network. We're mainly
1326 * concerned with making sure that the list of DNS servers is set up
1327 * according to which networks are connected, and ensuring that the
1328 * right routing table entries exist.
The Android Open Source Project28527d22009-03-03 19:31:44 -08001329 */
Robert Greenwalt3afbead2010-07-23 15:46:26 -07001330 private void handleConnectivityChange(int netType) {
The Android Open Source Project28527d22009-03-03 19:31:44 -08001331 /*
Robert Greenwalt2034b912009-08-12 16:08:25 -07001332 * If a non-default network is enabled, add the host routes that
Robert Greenwalt3afbead2010-07-23 15:46:26 -07001333 * will allow it's DNS servers to be accessed.
The Android Open Source Project28527d22009-03-03 19:31:44 -08001334 */
Robert Greenwalt3afbead2010-07-23 15:46:26 -07001335 handleDnsConfigurationChange(netType);
Robert Greenwalt2034b912009-08-12 16:08:25 -07001336
Robert Greenwalt3afbead2010-07-23 15:46:26 -07001337 if (mNetTrackers[netType].getNetworkInfo().isConnected()) {
1338 if (mNetAttributes[netType].isDefault()) {
Robert Greenwaltc3c5f862010-10-11 16:00:27 -07001339 handleApplyDefaultProxy(netType);
Robert Greenwalt3afbead2010-07-23 15:46:26 -07001340 addDefaultRoute(mNetTrackers[netType]);
Robert Greenwalt2034b912009-08-12 16:08:25 -07001341 } else {
Robert Greenwalta71471b2011-03-24 21:41:41 -07001342 // many radios add a default route even when we don't want one.
1343 // remove the default interface unless we need it for our active network
1344 if (mActiveDefaultNetwork != -1) {
Robert Greenwalt3a378b92011-03-30 15:03:13 -07001345 LinkProperties linkProperties =
1346 mNetTrackers[mActiveDefaultNetwork].getLinkProperties();
1347 LinkProperties newLinkProperties =
1348 mNetTrackers[netType].getLinkProperties();
1349 String defaultIface = linkProperties.getInterfaceName();
Robert Greenwalta71471b2011-03-24 21:41:41 -07001350 if (defaultIface != null &&
Robert Greenwalt3a378b92011-03-30 15:03:13 -07001351 !defaultIface.equals(newLinkProperties.getInterfaceName())) {
Robert Greenwaltf7dae302011-03-31 10:44:47 -07001352 removeDefaultRoute(mNetTrackers[netType]);
Robert Greenwalta71471b2011-03-24 21:41:41 -07001353 }
1354 }
Robert Greenwaltf7dae302011-03-31 10:44:47 -07001355 addPrivateDnsRoutes(mNetTrackers[netType]);
Robert Greenwalt3afbead2010-07-23 15:46:26 -07001356 }
1357 } else {
1358 if (mNetAttributes[netType].isDefault()) {
1359 removeDefaultRoute(mNetTrackers[netType]);
1360 } else {
1361 removePrivateDnsRoutes(mNetTrackers[netType]);
Robert Greenwalt2034b912009-08-12 16:08:25 -07001362 }
The Android Open Source Project28527d22009-03-03 19:31:44 -08001363 }
1364 }
1365
Irfan Sheriff7f132d92010-06-09 15:39:36 -07001366 private void addPrivateDnsRoutes(NetworkStateTracker nt) {
Irfan Sheriff7f132d92010-06-09 15:39:36 -07001367 boolean privateDnsRouteSet = nt.isPrivateDnsRouteSet();
Robert Greenwalt1f1bcfe2010-08-30 10:56:47 -07001368 LinkProperties p = nt.getLinkProperties();
Robert Greenwalta7dfbd32010-06-15 15:43:39 -07001369 if (p == null) return;
1370 String interfaceName = p.getInterfaceName();
Irfan Sheriff7f132d92010-06-09 15:39:36 -07001371
1372 if (DBG) {
Wink Savillee70c6f52010-12-03 12:01:38 -08001373 log("addPrivateDnsRoutes for " + nt +
Irfan Sheriff7f132d92010-06-09 15:39:36 -07001374 "(" + interfaceName + ") - mPrivateDnsRouteSet = " + privateDnsRouteSet);
1375 }
Irfan Sheriff7f132d92010-06-09 15:39:36 -07001376 if (interfaceName != null && !privateDnsRouteSet) {
Robert Greenwalta7dfbd32010-06-15 15:43:39 -07001377 Collection<InetAddress> dnsList = p.getDnses();
1378 for (InetAddress dns : dnsList) {
Wink Savillee70c6f52010-12-03 12:01:38 -08001379 if (DBG) log(" adding " + dns);
Robert Greenwalt7fe44cb2010-08-27 09:24:29 -07001380 NetworkUtils.addHostRoute(interfaceName, dns, null);
Irfan Sheriff7f132d92010-06-09 15:39:36 -07001381 }
1382 nt.privateDnsRouteSet(true);
1383 }
1384 }
1385
1386 private void removePrivateDnsRoutes(NetworkStateTracker nt) {
1387 // TODO - we should do this explicitly but the NetUtils api doesnt
1388 // support this yet - must remove all. No worse than before
Robert Greenwalt1f1bcfe2010-08-30 10:56:47 -07001389 LinkProperties p = nt.getLinkProperties();
Robert Greenwalta7dfbd32010-06-15 15:43:39 -07001390 if (p == null) return;
1391 String interfaceName = p.getInterfaceName();
Irfan Sheriff7f132d92010-06-09 15:39:36 -07001392 boolean privateDnsRouteSet = nt.isPrivateDnsRouteSet();
1393 if (interfaceName != null && privateDnsRouteSet) {
1394 if (DBG) {
Wink Savillee70c6f52010-12-03 12:01:38 -08001395 log("removePrivateDnsRoutes for " + nt.getNetworkInfo().getTypeName() +
Irfan Sheriff7f132d92010-06-09 15:39:36 -07001396 " (" + interfaceName + ")");
1397 }
1398 NetworkUtils.removeHostRoutes(interfaceName);
1399 nt.privateDnsRouteSet(false);
1400 }
1401 }
1402
Irfan Sheriff7f132d92010-06-09 15:39:36 -07001403
1404 private void addDefaultRoute(NetworkStateTracker nt) {
Robert Greenwalt1f1bcfe2010-08-30 10:56:47 -07001405 LinkProperties p = nt.getLinkProperties();
Robert Greenwalta7dfbd32010-06-15 15:43:39 -07001406 if (p == null) return;
1407 String interfaceName = p.getInterfaceName();
Robert Greenwalt5c733972011-02-09 13:56:06 -08001408 if (TextUtils.isEmpty(interfaceName)) return;
1409 for (InetAddress gateway : p.getGateways()) {
Irfan Sheriff7f132d92010-06-09 15:39:36 -07001410
Robert Greenwalt03d53da2011-03-22 18:47:42 -07001411 if (NetworkUtils.addHostRoute(interfaceName, gateway, null) &&
1412 NetworkUtils.addDefaultRoute(interfaceName, gateway)) {
1413 if (DBG) {
1414 NetworkInfo networkInfo = nt.getNetworkInfo();
1415 log("addDefaultRoute for " + networkInfo.getTypeName() +
1416 " (" + interfaceName + "), GatewayAddr=" + gateway.getHostAddress());
1417 }
Irfan Sheriff7f132d92010-06-09 15:39:36 -07001418 }
Irfan Sheriff7f132d92010-06-09 15:39:36 -07001419 }
1420 }
1421
1422
1423 public void removeDefaultRoute(NetworkStateTracker nt) {
Robert Greenwalt1f1bcfe2010-08-30 10:56:47 -07001424 LinkProperties p = nt.getLinkProperties();
Robert Greenwalta7dfbd32010-06-15 15:43:39 -07001425 if (p == null) return;
1426 String interfaceName = p.getInterfaceName();
Irfan Sheriff7f132d92010-06-09 15:39:36 -07001427
Robert Greenwalt3afbead2010-07-23 15:46:26 -07001428 if (interfaceName != null) {
Robert Greenwalt03d53da2011-03-22 18:47:42 -07001429 if (NetworkUtils.removeDefaultRoute(interfaceName) >= 0) {
1430 if (DBG) {
1431 NetworkInfo networkInfo = nt.getNetworkInfo();
1432 log("removeDefaultRoute for " + networkInfo.getTypeName() + " (" +
1433 interfaceName + ")");
1434 }
Irfan Sheriff7f132d92010-06-09 15:39:36 -07001435 }
Irfan Sheriff7f132d92010-06-09 15:39:36 -07001436 }
1437 }
1438
1439 /**
1440 * Reads the network specific TCP buffer sizes from SystemProperties
1441 * net.tcp.buffersize.[default|wifi|umts|edge|gprs] and set them for system
1442 * wide use
1443 */
1444 public void updateNetworkSettings(NetworkStateTracker nt) {
1445 String key = nt.getTcpBufferSizesPropName();
1446 String bufferSizes = SystemProperties.get(key);
1447
1448 if (bufferSizes.length() == 0) {
Wink Savillee70c6f52010-12-03 12:01:38 -08001449 loge(key + " not found in system properties. Using defaults");
Irfan Sheriff7f132d92010-06-09 15:39:36 -07001450
1451 // Setting to default values so we won't be stuck to previous values
1452 key = "net.tcp.buffersize.default";
1453 bufferSizes = SystemProperties.get(key);
1454 }
1455
1456 // Set values in kernel
1457 if (bufferSizes.length() != 0) {
1458 if (DBG) {
Wink Savillee70c6f52010-12-03 12:01:38 -08001459 log("Setting TCP values: [" + bufferSizes
Irfan Sheriff7f132d92010-06-09 15:39:36 -07001460 + "] which comes from [" + key + "]");
1461 }
1462 setBufferSize(bufferSizes);
1463 }
1464 }
1465
1466 /**
1467 * Writes TCP buffer sizes to /sys/kernel/ipv4/tcp_[r/w]mem_[min/def/max]
1468 * which maps to /proc/sys/net/ipv4/tcp_rmem and tcpwmem
1469 *
1470 * @param bufferSizes in the format of "readMin, readInitial, readMax,
1471 * writeMin, writeInitial, writeMax"
1472 */
1473 private void setBufferSize(String bufferSizes) {
1474 try {
1475 String[] values = bufferSizes.split(",");
1476
1477 if (values.length == 6) {
1478 final String prefix = "/sys/kernel/ipv4/tcp_";
1479 stringToFile(prefix + "rmem_min", values[0]);
1480 stringToFile(prefix + "rmem_def", values[1]);
1481 stringToFile(prefix + "rmem_max", values[2]);
1482 stringToFile(prefix + "wmem_min", values[3]);
1483 stringToFile(prefix + "wmem_def", values[4]);
1484 stringToFile(prefix + "wmem_max", values[5]);
1485 } else {
Wink Savillee70c6f52010-12-03 12:01:38 -08001486 loge("Invalid buffersize string: " + bufferSizes);
Irfan Sheriff7f132d92010-06-09 15:39:36 -07001487 }
1488 } catch (IOException e) {
Wink Savillee70c6f52010-12-03 12:01:38 -08001489 loge("Can't set tcp buffer sizes:" + e);
Irfan Sheriff7f132d92010-06-09 15:39:36 -07001490 }
1491 }
1492
1493 /**
1494 * Writes string to file. Basically same as "echo -n $string > $filename"
1495 *
1496 * @param filename
1497 * @param string
1498 * @throws IOException
1499 */
1500 private void stringToFile(String filename, String string) throws IOException {
1501 FileWriter out = new FileWriter(filename);
1502 try {
1503 out.write(string);
1504 } finally {
1505 out.close();
1506 }
1507 }
1508
1509
Robert Greenwalt2034b912009-08-12 16:08:25 -07001510 /**
1511 * Adjust the per-process dns entries (net.dns<x>.<pid>) based
1512 * on the highest priority active net which this process requested.
1513 * If there aren't any, clear it out
1514 */
1515 private void reassessPidDns(int myPid, boolean doBump)
1516 {
Wink Savillee70c6f52010-12-03 12:01:38 -08001517 if (DBG) log("reassessPidDns for pid " + myPid);
Robert Greenwalt2034b912009-08-12 16:08:25 -07001518 for(int i : mPriorityList) {
1519 if (mNetAttributes[i].isDefault()) {
1520 continue;
1521 }
1522 NetworkStateTracker nt = mNetTrackers[i];
Robert Greenwalt0659da32009-07-16 17:21:39 -07001523 if (nt.getNetworkInfo().isConnected() &&
1524 !nt.isTeardownRequested()) {
Robert Greenwalt1f1bcfe2010-08-30 10:56:47 -07001525 LinkProperties p = nt.getLinkProperties();
Robert Greenwalta7dfbd32010-06-15 15:43:39 -07001526 if (p == null) continue;
Robert Greenwalt2034b912009-08-12 16:08:25 -07001527 List pids = mNetRequestersPids[i];
1528 for (int j=0; j<pids.size(); j++) {
1529 Integer pid = (Integer)pids.get(j);
1530 if (pid.intValue() == myPid) {
Robert Greenwalta7dfbd32010-06-15 15:43:39 -07001531 Collection<InetAddress> dnses = p.getDnses();
1532 writePidDns(dnses, myPid);
Robert Greenwalt2034b912009-08-12 16:08:25 -07001533 if (doBump) {
1534 bumpDns();
1535 }
1536 return;
1537 }
1538 }
1539 }
1540 }
1541 // nothing found - delete
1542 for (int i = 1; ; i++) {
1543 String prop = "net.dns" + i + "." + myPid;
1544 if (SystemProperties.get(prop).length() == 0) {
1545 if (doBump) {
1546 bumpDns();
1547 }
1548 return;
1549 }
1550 SystemProperties.set(prop, "");
1551 }
1552 }
1553
Robert Greenwalt8ca88762010-12-17 15:20:36 -08001554 // return true if results in a change
1555 private boolean writePidDns(Collection <InetAddress> dnses, int pid) {
Robert Greenwalt2034b912009-08-12 16:08:25 -07001556 int j = 1;
Robert Greenwalt8ca88762010-12-17 15:20:36 -08001557 boolean changed = false;
Robert Greenwalta7dfbd32010-06-15 15:43:39 -07001558 for (InetAddress dns : dnses) {
Robert Greenwalt8ca88762010-12-17 15:20:36 -08001559 String dnsString = dns.getHostAddress();
1560 if (changed || !dnsString.equals(SystemProperties.get("net.dns" + j + "." + pid))) {
1561 changed = true;
1562 SystemProperties.set("net.dns" + j++ + "." + pid, dns.getHostAddress());
1563 }
Robert Greenwalt2034b912009-08-12 16:08:25 -07001564 }
Robert Greenwalt8ca88762010-12-17 15:20:36 -08001565 return changed;
Robert Greenwalt2034b912009-08-12 16:08:25 -07001566 }
1567
1568 private void bumpDns() {
1569 /*
1570 * Bump the property that tells the name resolver library to reread
1571 * the DNS server list from the properties.
1572 */
1573 String propVal = SystemProperties.get("net.dnschange");
1574 int n = 0;
1575 if (propVal.length() != 0) {
1576 try {
1577 n = Integer.parseInt(propVal);
1578 } catch (NumberFormatException e) {}
1579 }
1580 SystemProperties.set("net.dnschange", "" + (n+1));
Robert Greenwalt051642b2010-11-02 14:08:23 -07001581 /*
1582 * Tell the VMs to toss their DNS caches
1583 */
1584 Intent intent = new Intent(Intent.ACTION_CLEAR_DNS_CACHE);
1585 intent.addFlags(Intent.FLAG_RECEIVER_REPLACE_PENDING);
Stan Chesnuttf444f502011-01-05 17:14:03 -08001586 /*
1587 * Connectivity events can happen before boot has completed ...
1588 */
1589 intent.addFlags(Intent.FLAG_RECEIVER_REGISTERED_ONLY_BEFORE_BOOT);
Robert Greenwalt051642b2010-11-02 14:08:23 -07001590 mContext.sendBroadcast(intent);
Robert Greenwalt2034b912009-08-12 16:08:25 -07001591 }
1592
Robert Greenwalt3afbead2010-07-23 15:46:26 -07001593 private void handleDnsConfigurationChange(int netType) {
Robert Greenwalt2034b912009-08-12 16:08:25 -07001594 // add default net's dns entries
Robert Greenwalt3afbead2010-07-23 15:46:26 -07001595 NetworkStateTracker nt = mNetTrackers[netType];
1596 if (nt != null && nt.getNetworkInfo().isConnected() && !nt.isTeardownRequested()) {
Robert Greenwalt1f1bcfe2010-08-30 10:56:47 -07001597 LinkProperties p = nt.getLinkProperties();
Robert Greenwalt3afbead2010-07-23 15:46:26 -07001598 if (p == null) return;
1599 Collection<InetAddress> dnses = p.getDnses();
Robert Greenwalt8ca88762010-12-17 15:20:36 -08001600 boolean changed = false;
Robert Greenwalt3afbead2010-07-23 15:46:26 -07001601 if (mNetAttributes[netType].isDefault()) {
1602 int j = 1;
Robert Greenwalt94daa182010-09-01 11:34:05 -07001603 if (dnses.size() == 0 && mDefaultDns != null) {
Robert Greenwalt8ca88762010-12-17 15:20:36 -08001604 String dnsString = mDefaultDns.getHostAddress();
1605 if (!dnsString.equals(SystemProperties.get("net.dns1"))) {
1606 if (DBG) {
1607 log("no dns provided - using " + dnsString);
1608 }
1609 changed = true;
1610 SystemProperties.set("net.dns1", dnsString);
Robert Greenwalt2034b912009-08-12 16:08:25 -07001611 }
Robert Greenwalt94daa182010-09-01 11:34:05 -07001612 j++;
1613 } else {
1614 for (InetAddress dns : dnses) {
Robert Greenwalt8ca88762010-12-17 15:20:36 -08001615 String dnsString = dns.getHostAddress();
1616 if (!changed && dnsString.equals(SystemProperties.get("net.dns" + j))) {
1617 j++;
1618 continue;
1619 }
Robert Greenwalt94daa182010-09-01 11:34:05 -07001620 if (DBG) {
Wink Savillee70c6f52010-12-03 12:01:38 -08001621 log("adding dns " + dns + " for " +
Robert Greenwalt94daa182010-09-01 11:34:05 -07001622 nt.getNetworkInfo().getTypeName());
1623 }
Robert Greenwalt8ca88762010-12-17 15:20:36 -08001624 changed = true;
1625 SystemProperties.set("net.dns" + j++, dnsString);
Robert Greenwalt94daa182010-09-01 11:34:05 -07001626 }
Robert Greenwalt3afbead2010-07-23 15:46:26 -07001627 }
1628 for (int k=j ; k<mNumDnsEntries; k++) {
Robert Greenwalt8ca88762010-12-17 15:20:36 -08001629 if (changed || !TextUtils.isEmpty(SystemProperties.get("net.dns" + k))) {
1630 if (DBG) log("erasing net.dns" + k);
1631 changed = true;
1632 SystemProperties.set("net.dns" + k, "");
1633 }
Robert Greenwalt3afbead2010-07-23 15:46:26 -07001634 }
1635 mNumDnsEntries = j;
1636 } else {
1637 // set per-pid dns for attached secondary nets
1638 List pids = mNetRequestersPids[netType];
1639 for (int y=0; y< pids.size(); y++) {
1640 Integer pid = (Integer)pids.get(y);
Robert Greenwalt8ca88762010-12-17 15:20:36 -08001641 changed = writePidDns(dnses, pid.intValue());
The Android Open Source Project28527d22009-03-03 19:31:44 -08001642 }
1643 }
Robert Greenwalt8ca88762010-12-17 15:20:36 -08001644 if (changed) bumpDns();
The Android Open Source Project28527d22009-03-03 19:31:44 -08001645 }
Robert Greenwalt2034b912009-08-12 16:08:25 -07001646 }
1647
1648 private int getRestoreDefaultNetworkDelay() {
1649 String restoreDefaultNetworkDelayStr = SystemProperties.get(
1650 NETWORK_RESTORE_DELAY_PROP_NAME);
1651 if(restoreDefaultNetworkDelayStr != null &&
1652 restoreDefaultNetworkDelayStr.length() != 0) {
1653 try {
1654 return Integer.valueOf(restoreDefaultNetworkDelayStr);
1655 } catch (NumberFormatException e) {
1656 }
The Android Open Source Project28527d22009-03-03 19:31:44 -08001657 }
Robert Greenwalt2034b912009-08-12 16:08:25 -07001658 return RESTORE_DEFAULT_NETWORK_DELAY;
The Android Open Source Project28527d22009-03-03 19:31:44 -08001659 }
1660
1661 @Override
1662 protected void dump(FileDescriptor fd, PrintWriter pw, String[] args) {
Robert Greenwalt0659da32009-07-16 17:21:39 -07001663 if (mContext.checkCallingOrSelfPermission(
1664 android.Manifest.permission.DUMP)
The Android Open Source Project28527d22009-03-03 19:31:44 -08001665 != PackageManager.PERMISSION_GRANTED) {
Robert Greenwalt0659da32009-07-16 17:21:39 -07001666 pw.println("Permission Denial: can't dump ConnectivityService " +
1667 "from from pid=" + Binder.getCallingPid() + ", uid=" +
1668 Binder.getCallingUid());
The Android Open Source Project28527d22009-03-03 19:31:44 -08001669 return;
1670 }
The Android Open Source Project28527d22009-03-03 19:31:44 -08001671 pw.println();
1672 for (NetworkStateTracker nst : mNetTrackers) {
Robert Greenwalt3eeb6032009-12-21 18:24:07 -08001673 if (nst != null) {
1674 if (nst.getNetworkInfo().isConnected()) {
1675 pw.println("Active network: " + nst.getNetworkInfo().
1676 getTypeName());
1677 }
1678 pw.println(nst.getNetworkInfo());
1679 pw.println(nst);
1680 pw.println();
Robert Greenwalt2034b912009-08-12 16:08:25 -07001681 }
The Android Open Source Project28527d22009-03-03 19:31:44 -08001682 }
Robert Greenwalt3eeb6032009-12-21 18:24:07 -08001683
1684 pw.println("Network Requester Pids:");
1685 for (int net : mPriorityList) {
1686 String pidString = net + ": ";
1687 for (Object pid : mNetRequestersPids[net]) {
1688 pidString = pidString + pid.toString() + ", ";
1689 }
1690 pw.println(pidString);
1691 }
1692 pw.println();
1693
1694 pw.println("FeatureUsers:");
1695 for (Object requester : mFeatureUsers) {
1696 pw.println(requester.toString());
1697 }
1698 pw.println();
Robert Greenwalt8e87f122010-02-11 18:18:40 -08001699
Robert Greenwalt93dc1042010-06-15 12:19:37 -07001700 synchronized (this) {
1701 pw.println("NetworkTranstionWakeLock is currently " +
1702 (mNetTransitionWakeLock.isHeld() ? "" : "not ") + "held.");
1703 pw.println("It was last requested for "+mNetTransitionWakeLockCausedBy);
1704 }
1705 pw.println();
1706
Robert Greenwalt8e87f122010-02-11 18:18:40 -08001707 mTethering.dump(fd, pw, args);
Robert Greenwalt0e80be12010-09-20 14:35:25 -07001708
1709 if (mInetLog != null) {
1710 pw.println();
1711 pw.println("Inet condition reports:");
1712 for(int i = 0; i < mInetLog.size(); i++) {
1713 pw.println(mInetLog.get(i));
1714 }
1715 }
The Android Open Source Project28527d22009-03-03 19:31:44 -08001716 }
1717
Robert Greenwalt2034b912009-08-12 16:08:25 -07001718 // must be stateless - things change under us.
The Android Open Source Project28527d22009-03-03 19:31:44 -08001719 private class MyHandler extends Handler {
Wink Saville775aad62010-09-02 19:23:52 -07001720 public MyHandler(Looper looper) {
1721 super(looper);
1722 }
1723
The Android Open Source Project28527d22009-03-03 19:31:44 -08001724 @Override
1725 public void handleMessage(Message msg) {
1726 NetworkInfo info;
1727 switch (msg.what) {
1728 case NetworkStateTracker.EVENT_STATE_CHANGED:
1729 info = (NetworkInfo) msg.obj;
Robert Greenwalt12c44552009-12-07 11:33:18 -08001730 int type = info.getType();
1731 NetworkInfo.State state = info.getState();
Robert Greenwalt24e2d2b2010-01-25 16:14:00 -08001732 // only do this optimization for wifi. It going into scan mode for location
1733 // services generates alot of noise. Meanwhile the mms apn won't send out
1734 // subsequent notifications when on default cellular because it never
1735 // disconnects.. so only do this to wifi notifications. Fixed better when the
1736 // APN notifications are standardized.
1737 if (mNetAttributes[type].mLastState == state &&
1738 mNetAttributes[type].mRadio == ConnectivityManager.TYPE_WIFI) {
Robert Greenwalt12c44552009-12-07 11:33:18 -08001739 if (DBG) {
Robert Greenwalt24e2d2b2010-01-25 16:14:00 -08001740 // TODO - remove this after we validate the dropping doesn't break
1741 // anything
Wink Savillee70c6f52010-12-03 12:01:38 -08001742 log("Dropping ConnectivityChange for " +
Robert Greenwalt2adbc7f2010-01-13 09:36:31 -08001743 info.getTypeName() + ": " +
Robert Greenwalt12c44552009-12-07 11:33:18 -08001744 state + "/" + info.getDetailedState());
1745 }
1746 return;
1747 }
1748 mNetAttributes[type].mLastState = state;
1749
Wink Savillee70c6f52010-12-03 12:01:38 -08001750 if (DBG) log("ConnectivityChange for " +
Robert Greenwalt0659da32009-07-16 17:21:39 -07001751 info.getTypeName() + ": " +
Robert Greenwalt12c44552009-12-07 11:33:18 -08001752 state + "/" + info.getDetailedState());
The Android Open Source Project28527d22009-03-03 19:31:44 -08001753
1754 // Connectivity state changed:
1755 // [31-13] Reserved for future use
Robert Greenwalt0659da32009-07-16 17:21:39 -07001756 // [12-9] Network subtype (for mobile network, as defined
1757 // by TelephonyManager)
1758 // [8-3] Detailed state ordinal (as defined by
1759 // NetworkInfo.DetailedState)
The Android Open Source Project28527d22009-03-03 19:31:44 -08001760 // [2-0] Network type (as defined by ConnectivityManager)
1761 int eventLogParam = (info.getType() & 0x7) |
1762 ((info.getDetailedState().ordinal() & 0x3f) << 3) |
1763 (info.getSubtype() << 9);
Doug Zongker2fc96232009-12-04 10:31:43 -08001764 EventLog.writeEvent(EventLogTags.CONNECTIVITY_STATE_CHANGED,
Robert Greenwalt0659da32009-07-16 17:21:39 -07001765 eventLogParam);
1766
1767 if (info.getDetailedState() ==
1768 NetworkInfo.DetailedState.FAILED) {
The Android Open Source Project28527d22009-03-03 19:31:44 -08001769 handleConnectionFailure(info);
Robert Greenwalt12c44552009-12-07 11:33:18 -08001770 } else if (state == NetworkInfo.State.DISCONNECTED) {
The Android Open Source Project28527d22009-03-03 19:31:44 -08001771 handleDisconnect(info);
Robert Greenwalt12c44552009-12-07 11:33:18 -08001772 } else if (state == NetworkInfo.State.SUSPENDED) {
The Android Open Source Project28527d22009-03-03 19:31:44 -08001773 // TODO: need to think this over.
Robert Greenwalt0659da32009-07-16 17:21:39 -07001774 // the logic here is, handle SUSPENDED the same as
1775 // DISCONNECTED. The only difference being we are
1776 // broadcasting an intent with NetworkInfo that's
1777 // suspended. This allows the applications an
1778 // opportunity to handle DISCONNECTED and SUSPENDED
1779 // differently, or not.
The Android Open Source Project28527d22009-03-03 19:31:44 -08001780 handleDisconnect(info);
Robert Greenwalt12c44552009-12-07 11:33:18 -08001781 } else if (state == NetworkInfo.State.CONNECTED) {
The Android Open Source Project28527d22009-03-03 19:31:44 -08001782 handleConnect(info);
1783 }
The Android Open Source Project28527d22009-03-03 19:31:44 -08001784 break;
The Android Open Source Project28527d22009-03-03 19:31:44 -08001785 case NetworkStateTracker.EVENT_CONFIGURATION_CHANGED:
Robert Greenwalt3afbead2010-07-23 15:46:26 -07001786 info = (NetworkInfo) msg.obj;
1787 type = info.getType();
Robert Greenwaltc3c5f862010-10-11 16:00:27 -07001788 handleConnectivityChange(type);
The Android Open Source Project28527d22009-03-03 19:31:44 -08001789 break;
Robert Greenwaltccb36f92010-09-24 14:32:21 -07001790 case EVENT_CLEAR_NET_TRANSITION_WAKELOCK:
Robert Greenwalt93dc1042010-06-15 12:19:37 -07001791 String causedBy = null;
1792 synchronized (ConnectivityService.this) {
1793 if (msg.arg1 == mNetTransitionWakeLockSerialNumber &&
1794 mNetTransitionWakeLock.isHeld()) {
1795 mNetTransitionWakeLock.release();
1796 causedBy = mNetTransitionWakeLockCausedBy;
1797 }
1798 }
1799 if (causedBy != null) {
Wink Savillee70c6f52010-12-03 12:01:38 -08001800 log("NetTransition Wakelock for " + causedBy + " released by timeout");
Robert Greenwalt93dc1042010-06-15 12:19:37 -07001801 }
Robert Greenwaltcf1a56c2010-09-09 14:05:10 -07001802 break;
Robert Greenwalt6a2db8a2010-09-23 10:05:56 -07001803 case EVENT_RESTORE_DEFAULT_NETWORK:
Robert Greenwaltaffc3a12009-09-27 17:27:04 -07001804 FeatureUser u = (FeatureUser)msg.obj;
1805 u.expire();
Robert Greenwalt986c7412010-09-08 15:24:47 -07001806 break;
Robert Greenwalt6a2db8a2010-09-23 10:05:56 -07001807 case EVENT_INET_CONDITION_CHANGE:
1808 {
1809 int netType = msg.arg1;
1810 int condition = msg.arg2;
1811 handleInetConditionChange(netType, condition);
Robert Greenwalt93dc1042010-06-15 12:19:37 -07001812 break;
Robert Greenwalt6a2db8a2010-09-23 10:05:56 -07001813 }
1814 case EVENT_INET_CONDITION_HOLD_END:
1815 {
1816 int netType = msg.arg1;
1817 int sequence = msg.arg2;
1818 handleInetConditionHoldEnd(netType, sequence);
Robert Greenwalt986c7412010-09-08 15:24:47 -07001819 break;
Robert Greenwalt6a2db8a2010-09-23 10:05:56 -07001820 }
1821 case EVENT_SET_NETWORK_PREFERENCE:
1822 {
1823 int preference = msg.arg1;
1824 handleSetNetworkPreference(preference);
1825 break;
1826 }
1827 case EVENT_SET_BACKGROUND_DATA:
1828 {
1829 boolean enabled = (msg.arg1 == ENABLED);
1830 handleSetBackgroundData(enabled);
1831 break;
1832 }
1833 case EVENT_SET_MOBILE_DATA:
1834 {
1835 boolean enabled = (msg.arg1 == ENABLED);
1836 handleSetMobileData(enabled);
1837 break;
1838 }
Robert Greenwaltc3c5f862010-10-11 16:00:27 -07001839 case EVENT_APPLY_GLOBAL_HTTP_PROXY:
1840 {
1841 handleDeprecatedGlobalHttpProxy();
1842 }
The Android Open Source Project28527d22009-03-03 19:31:44 -08001843 }
1844 }
1845 }
Robert Greenwalt0c4828c2010-01-26 11:40:34 -08001846
1847 // javadoc from interface
Robert Greenwalt4283ded2010-03-02 17:25:02 -08001848 public int tether(String iface) {
Robert Greenwalt0c4828c2010-01-26 11:40:34 -08001849 enforceTetherChangePermission();
Robert Greenwalt4283ded2010-03-02 17:25:02 -08001850
1851 if (isTetheringSupported()) {
1852 return mTethering.tether(iface);
1853 } else {
1854 return ConnectivityManager.TETHER_ERROR_UNSUPPORTED;
1855 }
Robert Greenwalt0c4828c2010-01-26 11:40:34 -08001856 }
1857
1858 // javadoc from interface
Robert Greenwalt4283ded2010-03-02 17:25:02 -08001859 public int untether(String iface) {
Robert Greenwalt0c4828c2010-01-26 11:40:34 -08001860 enforceTetherChangePermission();
Robert Greenwalt4283ded2010-03-02 17:25:02 -08001861
1862 if (isTetheringSupported()) {
1863 return mTethering.untether(iface);
1864 } else {
1865 return ConnectivityManager.TETHER_ERROR_UNSUPPORTED;
1866 }
1867 }
1868
1869 // javadoc from interface
1870 public int getLastTetherError(String iface) {
1871 enforceTetherAccessPermission();
1872
1873 if (isTetheringSupported()) {
1874 return mTethering.getLastTetherError(iface);
1875 } else {
1876 return ConnectivityManager.TETHER_ERROR_UNSUPPORTED;
1877 }
Robert Greenwalt8e87f122010-02-11 18:18:40 -08001878 }
1879
1880 // TODO - proper iface API for selection by property, inspection, etc
1881 public String[] getTetherableUsbRegexs() {
1882 enforceTetherAccessPermission();
1883 if (isTetheringSupported()) {
1884 return mTethering.getTetherableUsbRegexs();
1885 } else {
1886 return new String[0];
1887 }
1888 }
1889
1890 public String[] getTetherableWifiRegexs() {
1891 enforceTetherAccessPermission();
1892 if (isTetheringSupported()) {
1893 return mTethering.getTetherableWifiRegexs();
1894 } else {
1895 return new String[0];
1896 }
Robert Greenwalt0c4828c2010-01-26 11:40:34 -08001897 }
1898
Danica Chang96567052010-08-11 14:54:43 -07001899 public String[] getTetherableBluetoothRegexs() {
1900 enforceTetherAccessPermission();
1901 if (isTetheringSupported()) {
1902 return mTethering.getTetherableBluetoothRegexs();
1903 } else {
1904 return new String[0];
1905 }
1906 }
1907
Robert Greenwalt0c4828c2010-01-26 11:40:34 -08001908 // TODO - move iface listing, queries, etc to new module
1909 // javadoc from interface
1910 public String[] getTetherableIfaces() {
Robert Greenwalt8e87f122010-02-11 18:18:40 -08001911 enforceTetherAccessPermission();
Robert Greenwalt0c4828c2010-01-26 11:40:34 -08001912 return mTethering.getTetherableIfaces();
1913 }
1914
1915 public String[] getTetheredIfaces() {
Robert Greenwalt8e87f122010-02-11 18:18:40 -08001916 enforceTetherAccessPermission();
Robert Greenwalt0c4828c2010-01-26 11:40:34 -08001917 return mTethering.getTetheredIfaces();
1918 }
Robert Greenwalt8e87f122010-02-11 18:18:40 -08001919
Robert Greenwalt4283ded2010-03-02 17:25:02 -08001920 public String[] getTetheringErroredIfaces() {
1921 enforceTetherAccessPermission();
1922 return mTethering.getErroredIfaces();
1923 }
1924
Robert Greenwalt8e87f122010-02-11 18:18:40 -08001925 // if ro.tether.denied = true we default to no tethering
1926 // gservices could set the secure setting to 1 though to enable it on a build where it
1927 // had previously been turned off.
1928 public boolean isTetheringSupported() {
1929 enforceTetherAccessPermission();
1930 int defaultVal = (SystemProperties.get("ro.tether.denied").equals("true") ? 0 : 1);
Robert Greenwaltf1b66e12010-02-25 12:29:30 -08001931 boolean tetherEnabledInSettings = (Settings.Secure.getInt(mContext.getContentResolver(),
1932 Settings.Secure.TETHER_SUPPORTED, defaultVal) != 0);
1933 return tetherEnabledInSettings && mTetheringConfigValid;
Robert Greenwalt8e87f122010-02-11 18:18:40 -08001934 }
Robert Greenwalt93dc1042010-06-15 12:19:37 -07001935
1936 // An API NetworkStateTrackers can call when they lose their network.
1937 // This will automatically be cleared after X seconds or a network becomes CONNECTED,
1938 // whichever happens first. The timer is started by the first caller and not
1939 // restarted by subsequent callers.
1940 public void requestNetworkTransitionWakelock(String forWhom) {
1941 enforceConnectivityInternalPermission();
1942 synchronized (this) {
1943 if (mNetTransitionWakeLock.isHeld()) return;
1944 mNetTransitionWakeLockSerialNumber++;
1945 mNetTransitionWakeLock.acquire();
1946 mNetTransitionWakeLockCausedBy = forWhom;
1947 }
1948 mHandler.sendMessageDelayed(mHandler.obtainMessage(
Robert Greenwaltccb36f92010-09-24 14:32:21 -07001949 EVENT_CLEAR_NET_TRANSITION_WAKELOCK,
Robert Greenwalt93dc1042010-06-15 12:19:37 -07001950 mNetTransitionWakeLockSerialNumber, 0),
1951 mNetTransitionWakeLockTimeout);
1952 return;
1953 }
Robert Greenwalt24118e82010-09-09 13:15:32 -07001954
Robert Greenwalt986c7412010-09-08 15:24:47 -07001955 // 100 percent is full good, 0 is full bad.
1956 public void reportInetCondition(int networkType, int percentage) {
Wink Savillee70c6f52010-12-03 12:01:38 -08001957 if (DBG) log("reportNetworkCondition(" + networkType + ", " + percentage + ")");
Robert Greenwalt986c7412010-09-08 15:24:47 -07001958 mContext.enforceCallingOrSelfPermission(
1959 android.Manifest.permission.STATUS_BAR,
1960 "ConnectivityService");
1961
Robert Greenwalt0e80be12010-09-20 14:35:25 -07001962 if (DBG) {
1963 int pid = getCallingPid();
1964 int uid = getCallingUid();
1965 String s = pid + "(" + uid + ") reports inet is " +
1966 (percentage > 50 ? "connected" : "disconnected") + " (" + percentage + ") on " +
1967 "network Type " + networkType + " at " + GregorianCalendar.getInstance().getTime();
1968 mInetLog.add(s);
1969 while(mInetLog.size() > INET_CONDITION_LOG_MAX_SIZE) {
1970 mInetLog.remove(0);
1971 }
1972 }
Robert Greenwalt986c7412010-09-08 15:24:47 -07001973 mHandler.sendMessage(mHandler.obtainMessage(
Robert Greenwalt6a2db8a2010-09-23 10:05:56 -07001974 EVENT_INET_CONDITION_CHANGE, networkType, percentage));
1975 }
1976
1977 private void handleInetConditionChange(int netType, int condition) {
1978 if (DBG) {
Wink Savillee70c6f52010-12-03 12:01:38 -08001979 log("Inet connectivity change, net=" +
Robert Greenwalt6a2db8a2010-09-23 10:05:56 -07001980 netType + ", condition=" + condition +
1981 ",mActiveDefaultNetwork=" + mActiveDefaultNetwork);
1982 }
1983 if (mActiveDefaultNetwork == -1) {
Wink Savillee70c6f52010-12-03 12:01:38 -08001984 if (DBG) log("no active default network - aborting");
Robert Greenwalt6a2db8a2010-09-23 10:05:56 -07001985 return;
1986 }
1987 if (mActiveDefaultNetwork != netType) {
Wink Savillee70c6f52010-12-03 12:01:38 -08001988 if (DBG) log("given net not default - aborting");
Robert Greenwalt6a2db8a2010-09-23 10:05:56 -07001989 return;
1990 }
1991 mDefaultInetCondition = condition;
1992 int delay;
1993 if (mInetConditionChangeInFlight == false) {
Wink Savillee70c6f52010-12-03 12:01:38 -08001994 if (DBG) log("starting a change hold");
Robert Greenwalt6a2db8a2010-09-23 10:05:56 -07001995 // setup a new hold to debounce this
1996 if (mDefaultInetCondition > 50) {
1997 delay = Settings.Secure.getInt(mContext.getContentResolver(),
1998 Settings.Secure.INET_CONDITION_DEBOUNCE_UP_DELAY, 500);
1999 } else {
2000 delay = Settings.Secure.getInt(mContext.getContentResolver(),
2001 Settings.Secure.INET_CONDITION_DEBOUNCE_DOWN_DELAY, 3000);
2002 }
2003 mInetConditionChangeInFlight = true;
2004 mHandler.sendMessageDelayed(mHandler.obtainMessage(EVENT_INET_CONDITION_HOLD_END,
2005 mActiveDefaultNetwork, mDefaultConnectionSequence), delay);
2006 } else {
2007 // we've set the new condition, when this hold ends that will get
2008 // picked up
Wink Savillee70c6f52010-12-03 12:01:38 -08002009 if (DBG) log("currently in hold - not setting new end evt");
Robert Greenwalt6a2db8a2010-09-23 10:05:56 -07002010 }
2011 }
2012
2013 private void handleInetConditionHoldEnd(int netType, int sequence) {
2014 if (DBG) {
Wink Savillee70c6f52010-12-03 12:01:38 -08002015 log("Inet hold end, net=" + netType +
Robert Greenwalt6a2db8a2010-09-23 10:05:56 -07002016 ", condition =" + mDefaultInetCondition +
2017 ", published condition =" + mDefaultInetConditionPublished);
2018 }
2019 mInetConditionChangeInFlight = false;
2020
2021 if (mActiveDefaultNetwork == -1) {
Wink Savillee70c6f52010-12-03 12:01:38 -08002022 if (DBG) log("no active default network - aborting");
Robert Greenwalt6a2db8a2010-09-23 10:05:56 -07002023 return;
2024 }
2025 if (mDefaultConnectionSequence != sequence) {
Wink Savillee70c6f52010-12-03 12:01:38 -08002026 if (DBG) log("event hold for obsolete network - aborting");
Robert Greenwalt6a2db8a2010-09-23 10:05:56 -07002027 return;
2028 }
2029 if (mDefaultInetConditionPublished == mDefaultInetCondition) {
Wink Savillee70c6f52010-12-03 12:01:38 -08002030 if (DBG) log("no change in condition - aborting");
Robert Greenwalt6a2db8a2010-09-23 10:05:56 -07002031 return;
2032 }
2033 NetworkInfo networkInfo = mNetTrackers[mActiveDefaultNetwork].getNetworkInfo();
2034 if (networkInfo.isConnected() == false) {
Wink Savillee70c6f52010-12-03 12:01:38 -08002035 if (DBG) log("default network not connected - aborting");
Robert Greenwalt6a2db8a2010-09-23 10:05:56 -07002036 return;
2037 }
2038 mDefaultInetConditionPublished = mDefaultInetCondition;
2039 sendInetConditionBroadcast(networkInfo);
2040 return;
Robert Greenwalt986c7412010-09-08 15:24:47 -07002041 }
Robert Greenwaltc3c5f862010-10-11 16:00:27 -07002042
2043 public synchronized ProxyProperties getProxy() {
2044 if (mGlobalProxy != null) return mGlobalProxy;
2045 if (mDefaultProxy != null) return mDefaultProxy;
2046 return null;
2047 }
2048
2049 public void setGlobalProxy(ProxyProperties proxyProperties) {
2050 enforceChangePermission();
2051 synchronized (mGlobalProxyLock) {
2052 if (proxyProperties == mGlobalProxy) return;
2053 if (proxyProperties != null && proxyProperties.equals(mGlobalProxy)) return;
2054 if (mGlobalProxy != null && mGlobalProxy.equals(proxyProperties)) return;
2055
2056 String host = "";
2057 int port = 0;
2058 String exclList = "";
2059 if (proxyProperties != null && !TextUtils.isEmpty(proxyProperties.getHost())) {
2060 mGlobalProxy = new ProxyProperties(proxyProperties);
2061 host = mGlobalProxy.getHost();
2062 port = mGlobalProxy.getPort();
2063 exclList = mGlobalProxy.getExclusionList();
2064 } else {
2065 mGlobalProxy = null;
2066 }
2067 ContentResolver res = mContext.getContentResolver();
2068 Settings.Secure.putString(res, Settings.Secure.GLOBAL_HTTP_PROXY_HOST, host);
2069 Settings.Secure.putInt(res, Settings.Secure.GLOBAL_HTTP_PROXY_PORT, port);
Robert Greenwalt6f7c6092010-12-02 11:31:00 -08002070 Settings.Secure.putString(res, Settings.Secure.GLOBAL_HTTP_PROXY_EXCLUSION_LIST,
Robert Greenwaltc3c5f862010-10-11 16:00:27 -07002071 exclList);
2072 }
2073
2074 if (mGlobalProxy == null) {
2075 proxyProperties = mDefaultProxy;
2076 }
2077 sendProxyBroadcast(proxyProperties);
2078 }
2079
Robert Greenwalt6f7c6092010-12-02 11:31:00 -08002080 private void loadGlobalProxy() {
2081 ContentResolver res = mContext.getContentResolver();
2082 String host = Settings.Secure.getString(res, Settings.Secure.GLOBAL_HTTP_PROXY_HOST);
2083 int port = Settings.Secure.getInt(res, Settings.Secure.GLOBAL_HTTP_PROXY_PORT, 0);
2084 String exclList = Settings.Secure.getString(res,
2085 Settings.Secure.GLOBAL_HTTP_PROXY_EXCLUSION_LIST);
2086 if (!TextUtils.isEmpty(host)) {
2087 ProxyProperties proxyProperties = new ProxyProperties(host, port, exclList);
2088 synchronized (mGlobalProxyLock) {
2089 mGlobalProxy = proxyProperties;
2090 }
2091 }
2092 }
2093
Robert Greenwaltc3c5f862010-10-11 16:00:27 -07002094 public ProxyProperties getGlobalProxy() {
2095 synchronized (mGlobalProxyLock) {
2096 return mGlobalProxy;
2097 }
2098 }
2099
2100 private void handleApplyDefaultProxy(int type) {
2101 // check if new default - push it out to all VM if so
2102 ProxyProperties proxy = mNetTrackers[type].getLinkProperties().getHttpProxy();
2103 synchronized (this) {
2104 if (mDefaultProxy != null && mDefaultProxy.equals(proxy)) return;
2105 if (mDefaultProxy == proxy) return;
2106 if (!TextUtils.isEmpty(proxy.getHost())) {
2107 mDefaultProxy = proxy;
2108 } else {
2109 mDefaultProxy = null;
2110 }
2111 }
Wink Savillee70c6f52010-12-03 12:01:38 -08002112 if (DBG) log("changing default proxy to " + proxy);
Robert Greenwaltc3c5f862010-10-11 16:00:27 -07002113 if ((proxy == null && mGlobalProxy == null) || proxy.equals(mGlobalProxy)) return;
2114 if (mGlobalProxy != null) return;
2115 sendProxyBroadcast(proxy);
2116 }
2117
2118 private void handleDeprecatedGlobalHttpProxy() {
2119 String proxy = Settings.Secure.getString(mContext.getContentResolver(),
2120 Settings.Secure.HTTP_PROXY);
2121 if (!TextUtils.isEmpty(proxy)) {
2122 String data[] = proxy.split(":");
2123 String proxyHost = data[0];
2124 int proxyPort = 8080;
2125 if (data.length > 1) {
2126 try {
2127 proxyPort = Integer.parseInt(data[1]);
2128 } catch (NumberFormatException e) {
2129 return;
2130 }
2131 }
2132 ProxyProperties p = new ProxyProperties(data[0], proxyPort, "");
2133 setGlobalProxy(p);
2134 }
2135 }
2136
2137 private void sendProxyBroadcast(ProxyProperties proxy) {
Robert Greenwalt611291c2010-12-23 15:51:10 -08002138 if (proxy == null) proxy = new ProxyProperties("", 0, "");
Wink Savillee70c6f52010-12-03 12:01:38 -08002139 log("sending Proxy Broadcast for " + proxy);
Robert Greenwaltc3c5f862010-10-11 16:00:27 -07002140 Intent intent = new Intent(Proxy.PROXY_CHANGE_ACTION);
Stan Chesnutt1f2a2ac2011-01-06 11:00:19 -08002141 intent.addFlags(Intent.FLAG_RECEIVER_REPLACE_PENDING |
2142 Intent.FLAG_RECEIVER_REGISTERED_ONLY_BEFORE_BOOT);
Robert Greenwaltc3c5f862010-10-11 16:00:27 -07002143 intent.putExtra(Proxy.EXTRA_PROXY_INFO, proxy);
Robert Greenwaltd93dc8f2010-12-06 11:29:17 -08002144 mContext.sendStickyBroadcast(intent);
Robert Greenwaltc3c5f862010-10-11 16:00:27 -07002145 }
2146
2147 private static class SettingsObserver extends ContentObserver {
2148 private int mWhat;
2149 private Handler mHandler;
2150 SettingsObserver(Handler handler, int what) {
2151 super(handler);
2152 mHandler = handler;
2153 mWhat = what;
2154 }
2155
2156 void observe(Context context) {
2157 ContentResolver resolver = context.getContentResolver();
2158 resolver.registerContentObserver(Settings.Secure.getUriFor(
2159 Settings.Secure.HTTP_PROXY), false, this);
2160 }
2161
2162 @Override
2163 public void onChange(boolean selfChange) {
2164 mHandler.obtainMessage(mWhat).sendToTarget();
2165 }
2166 }
Wink Savillee70c6f52010-12-03 12:01:38 -08002167
2168 private void log(String s) {
2169 Slog.d(TAG, s);
2170 }
2171
2172 private void loge(String s) {
2173 Slog.e(TAG, s);
2174 }
Wink Savilleb7c92c72011-03-12 14:52:01 -08002175 int convertFeatureToNetworkType(String feature){
2176 int networkType = -1;
2177 if (TextUtils.equals(feature, Phone.FEATURE_ENABLE_MMS)) {
2178 networkType = ConnectivityManager.TYPE_MOBILE_MMS;
2179 } else if (TextUtils.equals(feature, Phone.FEATURE_ENABLE_SUPL)) {
2180 networkType = ConnectivityManager.TYPE_MOBILE_SUPL;
2181 } else if (TextUtils.equals(feature, Phone.FEATURE_ENABLE_DUN) ||
2182 TextUtils.equals(feature, Phone.FEATURE_ENABLE_DUN_ALWAYS)) {
2183 networkType = ConnectivityManager.TYPE_MOBILE_DUN;
2184 } else if (TextUtils.equals(feature, Phone.FEATURE_ENABLE_HIPRI)) {
2185 networkType = ConnectivityManager.TYPE_MOBILE_HIPRI;
2186 } else if (TextUtils.equals(feature, Phone.FEATURE_ENABLE_FOTA)) {
2187 networkType = ConnectivityManager.TYPE_MOBILE_FOTA;
2188 } else if (TextUtils.equals(feature, Phone.FEATURE_ENABLE_IMS)) {
2189 networkType = ConnectivityManager.TYPE_MOBILE_IMS;
2190 } else if (TextUtils.equals(feature, Phone.FEATURE_ENABLE_CBS)) {
2191 networkType = ConnectivityManager.TYPE_MOBILE_CBS;
2192 }
2193 return networkType;
2194 }
The Android Open Source Project28527d22009-03-03 19:31:44 -08002195}