blob: 9d11d87e4dc274ac5cf645ff09c6cd0ec90377ce [file] [log] [blame]
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001/*
2 * Copyright (C) 2008 The Android Open Source Project
3 *
4 * Licensed under the Apache License, Version 2.0 (the "License");
5 * you may not use this file except in compliance with the License.
6 * You may obtain a copy of the License at
7 *
8 * http://www.apache.org/licenses/LICENSE-2.0
9 *
10 * Unless required by applicable law or agreed to in writing, software
11 * distributed under the License is distributed on an "AS IS" BASIS,
12 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13 * See the License for the specific language governing permissions and
14 * limitations under the License.
15 */
16
17package com.android.server;
18
19import android.app.Notification;
20import android.app.NotificationManager;
21import android.content.ContentResolver;
22import android.content.Context;
23import android.content.Intent;
24import android.content.pm.PackageManager;
Robert Greenwalt434203a2010-10-11 16:00:27 -070025import android.database.ContentObserver;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080026import android.net.ConnectivityManager;
Robert Greenwaltda3d5e62010-12-06 13:56:24 -080027import android.net.DummyDataStateTracker;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080028import android.net.IConnectivityManager;
29import android.net.MobileDataStateTracker;
30import android.net.NetworkInfo;
Robert Greenwalt37e65eb2010-08-30 10:56:47 -070031import android.net.LinkProperties;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080032import android.net.NetworkStateTracker;
Robert Greenwalt585ac0f2010-08-27 09:24:29 -070033import android.net.NetworkUtils;
Robert Greenwalt434203a2010-10-11 16:00:27 -070034import android.net.Proxy;
35import android.net.ProxyProperties;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080036import android.net.wifi.WifiStateTracker;
37import android.os.Binder;
38import android.os.Handler;
Wink Savillebb08caf2010-09-02 19:23:52 -070039import android.os.HandlerThread;
Robert Greenwalt42acef32009-08-12 16:08:25 -070040import android.os.IBinder;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080041import android.os.Looper;
42import android.os.Message;
Robert Greenwalt14f2ef42010-06-15 12:19:37 -070043import android.os.PowerManager;
Robert Greenwalt42acef32009-08-12 16:08:25 -070044import android.os.RemoteException;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080045import android.os.ServiceManager;
46import android.os.SystemProperties;
47import android.provider.Settings;
Robert Greenwalt42acef32009-08-12 16:08:25 -070048import android.text.TextUtils;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080049import android.util.EventLog;
Joe Onorato8a9b2202010-02-26 18:56:32 -080050import android.util.Slog;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080051
Robert Greenwalt42acef32009-08-12 16:08:25 -070052import com.android.internal.telephony.Phone;
53
Robert Greenwaltd0e18ff2010-01-26 11:40:34 -080054import com.android.server.connectivity.Tethering;
55
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080056import java.io.FileDescriptor;
Irfan Sheriffd649c122010-06-09 15:39:36 -070057import java.io.FileWriter;
58import java.io.IOException;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080059import java.io.PrintWriter;
Robert Greenwalt47f69fe2010-06-15 15:43:39 -070060import java.net.InetAddress;
Robert Greenwalt434203a2010-10-11 16:00:27 -070061import java.net.InetSocketAddress;
Robert Greenwalt47f69fe2010-06-15 15:43:39 -070062import java.net.UnknownHostException;
Robert Greenwalt42acef32009-08-12 16:08:25 -070063import java.util.ArrayList;
Robert Greenwalt47f69fe2010-06-15 15:43:39 -070064import java.util.Collection;
Robert Greenwalt4e8dfef2010-09-20 14:35:25 -070065import java.util.GregorianCalendar;
Robert Greenwalt42acef32009-08-12 16:08:25 -070066import java.util.List;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080067
68/**
69 * @hide
70 */
71public class ConnectivityService extends IConnectivityManager.Stub {
72
Robert Greenwaltba175a52010-10-05 19:12:26 -070073 private static final boolean DBG = true;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080074 private static final String TAG = "ConnectivityService";
75
Robert Greenwalt42acef32009-08-12 16:08:25 -070076 // how long to wait before switching back to a radio's default network
77 private static final int RESTORE_DEFAULT_NETWORK_DELAY = 1 * 60 * 1000;
78 // system property that can override the above value
79 private static final String NETWORK_RESTORE_DELAY_PROP_NAME =
80 "android.telephony.apn-restore";
81
Robert Greenwaltd0e18ff2010-01-26 11:40:34 -080082 private Tethering mTethering;
Robert Greenwaltc9d5fb72010-02-25 12:29:30 -080083 private boolean mTetheringConfigValid = false;
Robert Greenwaltd0e18ff2010-01-26 11:40:34 -080084
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080085 /**
86 * Sometimes we want to refer to the individual network state
87 * trackers separately, and sometimes we just want to treat them
88 * abstractly.
89 */
90 private NetworkStateTracker mNetTrackers[];
Robert Greenwalt42acef32009-08-12 16:08:25 -070091
92 /**
93 * A per Net list of the PID's that requested access to the net
94 * used both as a refcount and for per-PID DNS selection
95 */
96 private List mNetRequestersPids[];
97
Irfan Sheriffa2a1b912010-06-07 09:03:04 -070098 private WifiWatchdogService mWifiWatchdogService;
99
Robert Greenwalt42acef32009-08-12 16:08:25 -0700100 // priority order of the nettrackers
101 // (excluding dynamically set mNetworkPreference)
102 // TODO - move mNetworkTypePreference into this
103 private int[] mPriorityList;
104
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800105 private Context mContext;
106 private int mNetworkPreference;
Robert Greenwalt42acef32009-08-12 16:08:25 -0700107 private int mActiveDefaultNetwork = -1;
Robert Greenwaltd7085fc2010-09-08 15:24:47 -0700108 // 0 is full bad, 100 is full good
109 private int mDefaultInetCondition = 0;
110 private int mDefaultInetConditionPublished = 0;
111 private boolean mInetConditionChangeInFlight = false;
112 private int mDefaultConnectionSequence = 0;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800113
114 private int mNumDnsEntries;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800115
116 private boolean mTestMode;
Joe Onorato00092872010-09-01 21:18:22 -0700117 private static ConnectivityService sServiceInstance;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800118
Robert Greenwalt8dcc28b2010-09-23 10:05:56 -0700119 private static final int ENABLED = 1;
120 private static final int DISABLED = 0;
121
122 // Share the event space with NetworkStateTracker (which can't see this
123 // internal class but sends us events). If you change these, change
124 // NetworkStateTracker.java too.
125 private static final int MIN_NETWORK_STATE_TRACKER_EVENT = 1;
126 private static final int MAX_NETWORK_STATE_TRACKER_EVENT = 100;
127
128 /**
129 * used internally as a delayed event to make us switch back to the
130 * default network
131 */
132 private static final int EVENT_RESTORE_DEFAULT_NETWORK =
133 MAX_NETWORK_STATE_TRACKER_EVENT + 1;
134
135 /**
136 * used internally to change our mobile data enabled flag
137 */
138 private static final int EVENT_CHANGE_MOBILE_DATA_ENABLED =
139 MAX_NETWORK_STATE_TRACKER_EVENT + 2;
140
141 /**
142 * used internally to change our network preference setting
143 * arg1 = networkType to prefer
144 */
145 private static final int EVENT_SET_NETWORK_PREFERENCE =
146 MAX_NETWORK_STATE_TRACKER_EVENT + 3;
147
148 /**
149 * used internally to synchronize inet condition reports
150 * arg1 = networkType
151 * arg2 = condition (0 bad, 100 good)
152 */
153 private static final int EVENT_INET_CONDITION_CHANGE =
154 MAX_NETWORK_STATE_TRACKER_EVENT + 4;
155
156 /**
157 * used internally to mark the end of inet condition hold periods
158 * arg1 = networkType
159 */
160 private static final int EVENT_INET_CONDITION_HOLD_END =
161 MAX_NETWORK_STATE_TRACKER_EVENT + 5;
162
163 /**
164 * used internally to set the background data preference
165 * arg1 = TRUE for enabled, FALSE for disabled
166 */
167 private static final int EVENT_SET_BACKGROUND_DATA =
168 MAX_NETWORK_STATE_TRACKER_EVENT + 6;
169
170 /**
171 * used internally to set enable/disable cellular data
172 * arg1 = ENBALED or DISABLED
173 */
174 private static final int EVENT_SET_MOBILE_DATA =
175 MAX_NETWORK_STATE_TRACKER_EVENT + 7;
176
Robert Greenwaltf3331232010-09-24 14:32:21 -0700177 /**
178 * used internally to clear a wakelock when transitioning
179 * from one net to another
180 */
181 private static final int EVENT_CLEAR_NET_TRANSITION_WAKELOCK =
182 MAX_NETWORK_STATE_TRACKER_EVENT + 8;
183
Robert Greenwalt434203a2010-10-11 16:00:27 -0700184 /**
185 * used internally to reload global proxy settings
186 */
187 private static final int EVENT_APPLY_GLOBAL_HTTP_PROXY =
188 MAX_NETWORK_STATE_TRACKER_EVENT + 9;
189
Robert Greenwalt42acef32009-08-12 16:08:25 -0700190 private Handler mHandler;
191
192 // list of DeathRecipients used to make sure features are turned off when
193 // a process dies
194 private List mFeatureUsers;
195
Mike Lockwood0f79b542009-08-14 14:18:49 -0400196 private boolean mSystemReady;
Dianne Hackborn1c633fc2009-12-08 19:45:14 -0800197 private Intent mInitialBroadcast;
Mike Lockwood0f79b542009-08-14 14:18:49 -0400198
Robert Greenwalt14f2ef42010-06-15 12:19:37 -0700199 private PowerManager.WakeLock mNetTransitionWakeLock;
200 private String mNetTransitionWakeLockCausedBy = "";
201 private int mNetTransitionWakeLockSerialNumber;
202 private int mNetTransitionWakeLockTimeout;
203
Robert Greenwalte90aa5e2010-09-01 11:34:05 -0700204 private InetAddress mDefaultDns;
205
Robert Greenwalt4e8dfef2010-09-20 14:35:25 -0700206 // used in DBG mode to track inet condition reports
207 private static final int INET_CONDITION_LOG_MAX_SIZE = 15;
208 private ArrayList mInetLog;
209
Robert Greenwalt434203a2010-10-11 16:00:27 -0700210 // track the current default http proxy - tell the world if we get a new one (real change)
211 private ProxyProperties mDefaultProxy = null;
212 // track the global proxy.
213 private ProxyProperties mGlobalProxy = null;
214 private final Object mGlobalProxyLock = new Object();
215
216 private SettingsObserver mSettingsObserver;
217
Robert Greenwalt511288a2009-12-07 11:33:18 -0800218 private static class NetworkAttributes {
Robert Greenwalt42acef32009-08-12 16:08:25 -0700219 /**
220 * Class for holding settings read from resources.
221 */
222 public String mName;
223 public int mType;
224 public int mRadio;
225 public int mPriority;
Robert Greenwalt511288a2009-12-07 11:33:18 -0800226 public NetworkInfo.State mLastState;
Robert Greenwalt42acef32009-08-12 16:08:25 -0700227 public NetworkAttributes(String init) {
228 String fragments[] = init.split(",");
229 mName = fragments[0].toLowerCase();
Robert Greenwalt5154ae762009-10-30 14:17:42 -0700230 mType = Integer.parseInt(fragments[1]);
231 mRadio = Integer.parseInt(fragments[2]);
232 mPriority = Integer.parseInt(fragments[3]);
Robert Greenwalt511288a2009-12-07 11:33:18 -0800233 mLastState = NetworkInfo.State.UNKNOWN;
Robert Greenwalt42acef32009-08-12 16:08:25 -0700234 }
235 public boolean isDefault() {
236 return (mType == mRadio);
237 }
238 }
239 NetworkAttributes[] mNetAttributes;
Robert Greenwalt5154ae762009-10-30 14:17:42 -0700240 int mNetworksDefined;
Robert Greenwalt42acef32009-08-12 16:08:25 -0700241
Robert Greenwalt511288a2009-12-07 11:33:18 -0800242 private static class RadioAttributes {
Robert Greenwalt42acef32009-08-12 16:08:25 -0700243 public int mSimultaneity;
244 public int mType;
245 public RadioAttributes(String init) {
246 String fragments[] = init.split(",");
Robert Greenwalt5154ae762009-10-30 14:17:42 -0700247 mType = Integer.parseInt(fragments[0]);
248 mSimultaneity = Integer.parseInt(fragments[1]);
Robert Greenwalt42acef32009-08-12 16:08:25 -0700249 }
250 }
251 RadioAttributes[] mRadioAttributes;
252
Wink Savillebb08caf2010-09-02 19:23:52 -0700253 public static synchronized ConnectivityService getInstance(Context context) {
254 if (sServiceInstance == null) {
255 sServiceInstance = new ConnectivityService(context);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800256 }
Wink Savillebb08caf2010-09-02 19:23:52 -0700257 return sServiceInstance;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800258 }
Robert Greenwalt86e9e552009-07-16 17:21:39 -0700259
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800260 private ConnectivityService(Context context) {
Wink Savilleed9c02b2010-12-03 12:01:38 -0800261 if (DBG) log("ConnectivityService starting up");
Robert Greenwaltde8383c2010-01-14 17:47:58 -0800262
Wink Savillebb08caf2010-09-02 19:23:52 -0700263 HandlerThread handlerThread = new HandlerThread("ConnectivityServiceThread");
264 handlerThread.start();
265 mHandler = new MyHandler(handlerThread.getLooper());
266
Robert Greenwaltde8383c2010-01-14 17:47:58 -0800267 // setup our unique device name
Robert Greenwalt733c6292010-12-06 09:30:17 -0800268 if (TextUtils.isEmpty(SystemProperties.get("net.hostname"))) {
269 String id = Settings.Secure.getString(context.getContentResolver(),
270 Settings.Secure.ANDROID_ID);
271 if (id != null && id.length() > 0) {
272 String name = new String("android_").concat(id);
273 SystemProperties.set("net.hostname", name);
274 }
Robert Greenwaltde8383c2010-01-14 17:47:58 -0800275 }
276
Robert Greenwalte90aa5e2010-09-01 11:34:05 -0700277 // read our default dns server ip
278 String dns = Settings.Secure.getString(context.getContentResolver(),
279 Settings.Secure.DEFAULT_DNS_SERVER);
280 if (dns == null || dns.length() == 0) {
281 dns = context.getResources().getString(
282 com.android.internal.R.string.config_default_dns_server);
283 }
284 try {
285 mDefaultDns = InetAddress.getByName(dns);
286 } catch (UnknownHostException e) {
Wink Savilleed9c02b2010-12-03 12:01:38 -0800287 loge("Error setting defaultDns using " + dns);
Robert Greenwalte90aa5e2010-09-01 11:34:05 -0700288 }
289
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800290 mContext = context;
Robert Greenwalt14f2ef42010-06-15 12:19:37 -0700291
292 PowerManager powerManager = (PowerManager)mContext.getSystemService(Context.POWER_SERVICE);
293 mNetTransitionWakeLock = powerManager.newWakeLock(PowerManager.PARTIAL_WAKE_LOCK, TAG);
294 mNetTransitionWakeLockTimeout = mContext.getResources().getInteger(
295 com.android.internal.R.integer.config_networkTransitionTimeout);
296
Robert Greenwalt42acef32009-08-12 16:08:25 -0700297 mNetTrackers = new NetworkStateTracker[
298 ConnectivityManager.MAX_NETWORK_TYPE+1];
Robert Greenwalt86e9e552009-07-16 17:21:39 -0700299
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800300 mNetworkPreference = getPersistedNetworkPreference();
Robert Greenwalt86e9e552009-07-16 17:21:39 -0700301
Robert Greenwalt5154ae762009-10-30 14:17:42 -0700302 mRadioAttributes = new RadioAttributes[ConnectivityManager.MAX_RADIO_TYPE+1];
303 mNetAttributes = new NetworkAttributes[ConnectivityManager.MAX_NETWORK_TYPE+1];
304
Robert Greenwalt42acef32009-08-12 16:08:25 -0700305 // Load device network attributes from resources
Robert Greenwalt42acef32009-08-12 16:08:25 -0700306 String[] raStrings = context.getResources().getStringArray(
307 com.android.internal.R.array.radioAttributes);
Robert Greenwalt5154ae762009-10-30 14:17:42 -0700308 for (String raString : raStrings) {
309 RadioAttributes r = new RadioAttributes(raString);
310 if (r.mType > ConnectivityManager.MAX_RADIO_TYPE) {
Wink Savilleed9c02b2010-12-03 12:01:38 -0800311 loge("Error in radioAttributes - ignoring attempt to define type " + r.mType);
Robert Greenwalt5154ae762009-10-30 14:17:42 -0700312 continue;
313 }
314 if (mRadioAttributes[r.mType] != null) {
Wink Savilleed9c02b2010-12-03 12:01:38 -0800315 loge("Error in radioAttributes - ignoring attempt to redefine type " +
Robert Greenwalt5154ae762009-10-30 14:17:42 -0700316 r.mType);
317 continue;
318 }
Robert Greenwalt42acef32009-08-12 16:08:25 -0700319 mRadioAttributes[r.mType] = r;
320 }
321
Robert Greenwalt5154ae762009-10-30 14:17:42 -0700322 String[] naStrings = context.getResources().getStringArray(
323 com.android.internal.R.array.networkAttributes);
324 for (String naString : naStrings) {
325 try {
326 NetworkAttributes n = new NetworkAttributes(naString);
327 if (n.mType > ConnectivityManager.MAX_NETWORK_TYPE) {
Wink Savilleed9c02b2010-12-03 12:01:38 -0800328 loge("Error in networkAttributes - ignoring attempt to define type " +
Robert Greenwalt5154ae762009-10-30 14:17:42 -0700329 n.mType);
330 continue;
Robert Greenwalt42acef32009-08-12 16:08:25 -0700331 }
Robert Greenwalt5154ae762009-10-30 14:17:42 -0700332 if (mNetAttributes[n.mType] != null) {
Wink Savilleed9c02b2010-12-03 12:01:38 -0800333 loge("Error in networkAttributes - ignoring attempt to redefine type " +
Robert Greenwalt5154ae762009-10-30 14:17:42 -0700334 n.mType);
335 continue;
336 }
337 if (mRadioAttributes[n.mRadio] == null) {
Wink Savilleed9c02b2010-12-03 12:01:38 -0800338 loge("Error in networkAttributes - ignoring attempt to use undefined " +
Robert Greenwalt5154ae762009-10-30 14:17:42 -0700339 "radio " + n.mRadio + " in network type " + n.mType);
340 continue;
341 }
342 mNetAttributes[n.mType] = n;
343 mNetworksDefined++;
344 } catch(Exception e) {
345 // ignore it - leave the entry null
Robert Greenwalt42acef32009-08-12 16:08:25 -0700346 }
347 }
348
Robert Greenwalt5154ae762009-10-30 14:17:42 -0700349 // high priority first
350 mPriorityList = new int[mNetworksDefined];
351 {
352 int insertionPoint = mNetworksDefined-1;
353 int currentLowest = 0;
354 int nextLowest = 0;
355 while (insertionPoint > -1) {
356 for (NetworkAttributes na : mNetAttributes) {
357 if (na == null) continue;
358 if (na.mPriority < currentLowest) continue;
359 if (na.mPriority > currentLowest) {
360 if (na.mPriority < nextLowest || nextLowest == 0) {
361 nextLowest = na.mPriority;
362 }
363 continue;
364 }
365 mPriorityList[insertionPoint--] = na.mType;
366 }
367 currentLowest = nextLowest;
368 nextLowest = 0;
369 }
370 }
371
372 mNetRequestersPids = new ArrayList[ConnectivityManager.MAX_NETWORK_TYPE+1];
373 for (int i : mPriorityList) {
Robert Greenwalt42acef32009-08-12 16:08:25 -0700374 mNetRequestersPids[i] = new ArrayList();
375 }
376
377 mFeatureUsers = new ArrayList();
378
Robert Greenwalt5154ae762009-10-30 14:17:42 -0700379 mNumDnsEntries = 0;
380
381 mTestMode = SystemProperties.get("cm.test.mode").equals("true")
382 && SystemProperties.get("ro.build.type").equals("eng");
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800383 /*
384 * Create the network state trackers for Wi-Fi and mobile
385 * data. Maybe this could be done with a factory class,
386 * but it's not clear that it's worth it, given that
387 * the number of different network types is not going
388 * to change very often.
389 */
Robert Greenwalt5154ae762009-10-30 14:17:42 -0700390 for (int netType : mPriorityList) {
391 switch (mNetAttributes[netType].mRadio) {
392 case ConnectivityManager.TYPE_WIFI:
Wink Savilleed9c02b2010-12-03 12:01:38 -0800393 if (DBG) log("Starting Wifi Service.");
Wink Savillec7a98342010-08-13 16:11:42 -0700394 WifiStateTracker wst = new WifiStateTracker();
Irfan Sheriff0d255342010-07-28 09:35:20 -0700395 WifiService wifiService = new WifiService(context);
Robert Greenwalt5154ae762009-10-30 14:17:42 -0700396 ServiceManager.addService(Context.WIFI_SERVICE, wifiService);
Irfan Sheriff0d255342010-07-28 09:35:20 -0700397 wifiService.checkAndStartWifi();
Robert Greenwalt5154ae762009-10-30 14:17:42 -0700398 mNetTrackers[ConnectivityManager.TYPE_WIFI] = wst;
Wink Savillec7a98342010-08-13 16:11:42 -0700399 wst.startMonitoring(context, mHandler);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800400
Irfan Sheriffa2a1b912010-06-07 09:03:04 -0700401 //TODO: as part of WWS refactor, create only when needed
Irfan Sheriff0d255342010-07-28 09:35:20 -0700402 mWifiWatchdogService = new WifiWatchdogService(context);
Irfan Sheriffa2a1b912010-06-07 09:03:04 -0700403
Robert Greenwalt5154ae762009-10-30 14:17:42 -0700404 break;
405 case ConnectivityManager.TYPE_MOBILE:
Wink Savillec7a98342010-08-13 16:11:42 -0700406 mNetTrackers[netType] = new MobileDataStateTracker(netType,
407 mNetAttributes[netType].mName);
408 mNetTrackers[netType].startMonitoring(context, mHandler);
Robert Greenwalt5154ae762009-10-30 14:17:42 -0700409 break;
Robert Greenwaltda3d5e62010-12-06 13:56:24 -0800410 case ConnectivityManager.TYPE_DUMMY:
411 mNetTrackers[netType] = new DummyDataStateTracker(netType,
412 mNetAttributes[netType].mName);
413 mNetTrackers[netType].startMonitoring(context, mHandler);
414 break;
Robert Greenwalt5154ae762009-10-30 14:17:42 -0700415 default:
Wink Savilleed9c02b2010-12-03 12:01:38 -0800416 loge("Trying to create a DataStateTracker for an unknown radio type " +
Robert Greenwalt5154ae762009-10-30 14:17:42 -0700417 mNetAttributes[netType].mRadio);
418 continue;
419 }
420 }
Robert Greenwaltd0e18ff2010-01-26 11:40:34 -0800421
Robert Greenwaltdfadaea2010-03-11 15:03:08 -0800422 mTethering = new Tethering(mContext, mHandler.getLooper());
Robert Greenwaltc9d5fb72010-02-25 12:29:30 -0800423 mTetheringConfigValid = (((mNetTrackers[ConnectivityManager.TYPE_MOBILE_DUN] != null) ||
424 !mTethering.isDunRequired()) &&
425 (mTethering.getTetherableUsbRegexs().length != 0 ||
Danica Chang6fdd0c62010-08-11 14:54:43 -0700426 mTethering.getTetherableWifiRegexs().length != 0 ||
427 mTethering.getTetherableBluetoothRegexs().length != 0) &&
Robert Greenwaltc9d5fb72010-02-25 12:29:30 -0800428 mTethering.getUpstreamIfaceRegexs().length != 0);
429
Robert Greenwalt4e8dfef2010-09-20 14:35:25 -0700430 if (DBG) {
431 mInetLog = new ArrayList();
432 }
Robert Greenwalt434203a2010-10-11 16:00:27 -0700433
434 mSettingsObserver = new SettingsObserver(mHandler, EVENT_APPLY_GLOBAL_HTTP_PROXY);
435 mSettingsObserver.observe(mContext);
Robert Greenwaltb7090d62010-12-02 11:31:00 -0800436
437 loadGlobalProxy();
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800438 }
439
Robert Greenwalt5154ae762009-10-30 14:17:42 -0700440
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800441 /**
Robert Greenwalt86e9e552009-07-16 17:21:39 -0700442 * Sets the preferred network.
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800443 * @param preference the new preference
444 */
Robert Greenwalt8dcc28b2010-09-23 10:05:56 -0700445 public void setNetworkPreference(int preference) {
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800446 enforceChangePermission();
Robert Greenwalt8dcc28b2010-09-23 10:05:56 -0700447
448 mHandler.sendMessage(mHandler.obtainMessage(EVENT_SET_NETWORK_PREFERENCE, preference, 0));
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800449 }
450
451 public int getNetworkPreference() {
452 enforceAccessPermission();
Robert Greenwalt8dcc28b2010-09-23 10:05:56 -0700453 int preference;
454 synchronized(this) {
455 preference = mNetworkPreference;
456 }
457 return preference;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800458 }
459
Robert Greenwalt8dcc28b2010-09-23 10:05:56 -0700460 private void handleSetNetworkPreference(int preference) {
461 if (ConnectivityManager.isNetworkTypeValid(preference) &&
462 mNetAttributes[preference] != null &&
463 mNetAttributes[preference].isDefault()) {
464 if (mNetworkPreference != preference) {
465 final ContentResolver cr = mContext.getContentResolver();
466 Settings.Secure.putInt(cr, Settings.Secure.NETWORK_PREFERENCE, preference);
467 synchronized(this) {
468 mNetworkPreference = preference;
469 }
470 enforcePreference();
471 }
472 }
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800473 }
Robert Greenwalt86e9e552009-07-16 17:21:39 -0700474
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800475 private int getPersistedNetworkPreference() {
476 final ContentResolver cr = mContext.getContentResolver();
477
478 final int networkPrefSetting = Settings.Secure
479 .getInt(cr, Settings.Secure.NETWORK_PREFERENCE, -1);
480 if (networkPrefSetting != -1) {
481 return networkPrefSetting;
482 }
483
484 return ConnectivityManager.DEFAULT_NETWORK_PREFERENCE;
485 }
Robert Greenwalt86e9e552009-07-16 17:21:39 -0700486
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800487 /**
Robert Greenwalt86e9e552009-07-16 17:21:39 -0700488 * Make the state of network connectivity conform to the preference settings
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800489 * In this method, we only tear down a non-preferred network. Establishing
490 * a connection to the preferred network is taken care of when we handle
491 * the disconnect event from the non-preferred network
492 * (see {@link #handleDisconnect(NetworkInfo)}).
493 */
494 private void enforcePreference() {
Robert Greenwalt42acef32009-08-12 16:08:25 -0700495 if (mNetTrackers[mNetworkPreference].getNetworkInfo().isConnected())
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800496 return;
497
Robert Greenwalt42acef32009-08-12 16:08:25 -0700498 if (!mNetTrackers[mNetworkPreference].isAvailable())
499 return;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800500
Robert Greenwalt42acef32009-08-12 16:08:25 -0700501 for (int t=0; t <= ConnectivityManager.MAX_RADIO_TYPE; t++) {
Robert Greenwalt5154ae762009-10-30 14:17:42 -0700502 if (t != mNetworkPreference && mNetTrackers[t] != null &&
Robert Greenwalt42acef32009-08-12 16:08:25 -0700503 mNetTrackers[t].getNetworkInfo().isConnected()) {
Robert Greenwaltec9fe462009-08-20 15:25:14 -0700504 if (DBG) {
Wink Savilleed9c02b2010-12-03 12:01:38 -0800505 log("tearing down " + mNetTrackers[t].getNetworkInfo() +
Robert Greenwaltec9fe462009-08-20 15:25:14 -0700506 " in enforcePreference");
507 }
Robert Greenwalt42acef32009-08-12 16:08:25 -0700508 teardown(mNetTrackers[t]);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800509 }
510 }
511 }
512
513 private boolean teardown(NetworkStateTracker netTracker) {
514 if (netTracker.teardown()) {
515 netTracker.setTeardownRequested(true);
516 return true;
517 } else {
518 return false;
519 }
520 }
521
522 /**
523 * Return NetworkInfo for the active (i.e., connected) network interface.
524 * It is assumed that at most one network is active at a time. If more
525 * than one is active, it is indeterminate which will be returned.
Robert Greenwalt86e9e552009-07-16 17:21:39 -0700526 * @return the info for the active network, or {@code null} if none is
527 * active
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800528 */
529 public NetworkInfo getActiveNetworkInfo() {
530 enforceAccessPermission();
Robert Greenwalt42acef32009-08-12 16:08:25 -0700531 for (int type=0; type <= ConnectivityManager.MAX_NETWORK_TYPE; type++) {
Robert Greenwalt5154ae762009-10-30 14:17:42 -0700532 if (mNetAttributes[type] == null || !mNetAttributes[type].isDefault()) {
Robert Greenwalt42acef32009-08-12 16:08:25 -0700533 continue;
534 }
535 NetworkStateTracker t = mNetTrackers[type];
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800536 NetworkInfo info = t.getNetworkInfo();
537 if (info.isConnected()) {
Wink Savilleed9c02b2010-12-03 12:01:38 -0800538 if (DBG && type != mActiveDefaultNetwork) {
539 loge("connected default network is not mActiveDefaultNetwork!");
540 }
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800541 return info;
542 }
543 }
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 Greenwalt5154ae762009-10-30 14:17:42 -0700559 NetworkInfo[] result = new NetworkInfo[mNetworksDefined];
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800560 int i = 0;
561 for (NetworkStateTracker t : mNetTrackers) {
Robert Greenwalt5154ae762009-10-30 14:17:42 -0700562 if(t != null) result[i++] = t.getNetworkInfo();
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800563 }
564 return result;
565 }
566
Robert Greenwaltd192dad2010-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 Project9066cfe2009-03-03 19:31:44 -0800599 public boolean setRadios(boolean turnOn) {
600 boolean result = true;
601 enforceChangePermission();
602 for (NetworkStateTracker t : mNetTrackers) {
Robert Greenwalt5154ae762009-10-30 14:17:42 -0700603 if (t != null) result = t.setRadio(turnOn) && result;
The Android Open Source Project9066cfe2009-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 Greenwalt9c75d4a2009-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 Greenwalt42acef32009-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 Greenwaltb9285352009-12-21 18:24:07 -0800630 long mCreateTime;
Robert Greenwalt42acef32009-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 Greenwaltb9285352009-12-21 18:24:07 -0800639 mCreateTime = System.currentTimeMillis();
Robert Greenwalt9c75d4a2009-09-27 17:27:04 -0700640
Robert Greenwalt42acef32009-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 Savilleed9c02b2010-12-03 12:01:38 -0800653 log("ConnectivityService FeatureUser binderDied(" +
Robert Greenwaltb9285352009-12-21 18:24:07 -0800654 mNetworkType + ", " + mFeature + ", " + mBinder + "), created " +
655 (System.currentTimeMillis() - mCreateTime) + " mSec ago");
Robert Greenwalt9c75d4a2009-09-27 17:27:04 -0700656 stopUsingNetworkFeature(this, false);
Robert Greenwalt42acef32009-08-12 16:08:25 -0700657 }
658
Robert Greenwalt9c75d4a2009-09-27 17:27:04 -0700659 public void expire() {
Wink Savilleed9c02b2010-12-03 12:01:38 -0800660 log("ConnectivityService FeatureUser expire(" +
Robert Greenwaltb9285352009-12-21 18:24:07 -0800661 mNetworkType + ", " + mFeature + ", " + mBinder +"), created " +
662 (System.currentTimeMillis() - mCreateTime) + " mSec ago");
Robert Greenwalt9c75d4a2009-09-27 17:27:04 -0700663 stopUsingNetworkFeature(this, false);
664 }
Robert Greenwaltb9285352009-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 Greenwalt42acef32009-08-12 16:08:25 -0700670 }
671
Robert Greenwalt9c75d4a2009-09-27 17:27:04 -0700672 // javadoc from interface
Robert Greenwalt42acef32009-08-12 16:08:25 -0700673 public int startUsingNetworkFeature(int networkType, String feature,
674 IBinder binder) {
675 if (DBG) {
Wink Savilleed9c02b2010-12-03 12:01:38 -0800676 log("startUsingNetworkFeature for net " + networkType + ": " + feature);
Robert Greenwalt42acef32009-08-12 16:08:25 -0700677 }
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800678 enforceChangePermission();
Robert Greenwalt5154ae762009-10-30 14:17:42 -0700679 if (!ConnectivityManager.isNetworkTypeValid(networkType) ||
680 mNetAttributes[networkType] == null) {
Robert Greenwalt42acef32009-08-12 16:08:25 -0700681 return Phone.APN_REQUEST_FAILED;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800682 }
Robert Greenwalt42acef32009-08-12 16:08:25 -0700683
Robert Greenwalt9c75d4a2009-09-27 17:27:04 -0700684 FeatureUser f = new FeatureUser(networkType, feature, binder);
Robert Greenwalt42acef32009-08-12 16:08:25 -0700685
686 // TODO - move this into the MobileDataStateTracker
687 int usedNetworkType = networkType;
688 if(networkType == ConnectivityManager.TYPE_MOBILE) {
689 if (TextUtils.equals(feature, Phone.FEATURE_ENABLE_MMS)) {
690 usedNetworkType = ConnectivityManager.TYPE_MOBILE_MMS;
691 } else if (TextUtils.equals(feature, Phone.FEATURE_ENABLE_SUPL)) {
692 usedNetworkType = ConnectivityManager.TYPE_MOBILE_SUPL;
693 } else if (TextUtils.equals(feature, Phone.FEATURE_ENABLE_DUN)) {
694 usedNetworkType = ConnectivityManager.TYPE_MOBILE_DUN;
695 } else if (TextUtils.equals(feature, Phone.FEATURE_ENABLE_HIPRI)) {
696 usedNetworkType = ConnectivityManager.TYPE_MOBILE_HIPRI;
697 }
698 }
699 NetworkStateTracker network = mNetTrackers[usedNetworkType];
700 if (network != null) {
701 if (usedNetworkType != networkType) {
702 Integer currentPid = new Integer(getCallingPid());
703
704 NetworkStateTracker radio = mNetTrackers[networkType];
705 NetworkInfo ni = network.getNetworkInfo();
706
707 if (ni.isAvailable() == false) {
Wink Savilleed9c02b2010-12-03 12:01:38 -0800708 if (DBG) log("special network not available");
Robert Greenwalt42acef32009-08-12 16:08:25 -0700709 return Phone.APN_TYPE_NOT_AVAILABLE;
710 }
711
Robert Greenwalt9c75d4a2009-09-27 17:27:04 -0700712 synchronized(this) {
713 mFeatureUsers.add(f);
714 if (!mNetRequestersPids[usedNetworkType].contains(currentPid)) {
715 // this gets used for per-pid dns when connected
716 mNetRequestersPids[usedNetworkType].add(currentPid);
717 }
Robert Greenwalt42acef32009-08-12 16:08:25 -0700718 }
Robert Greenwalt8dcc28b2010-09-23 10:05:56 -0700719 mHandler.sendMessageDelayed(mHandler.obtainMessage(EVENT_RESTORE_DEFAULT_NETWORK,
Robert Greenwalt9c75d4a2009-09-27 17:27:04 -0700720 f), getRestoreDefaultNetworkDelay());
721
Robert Greenwalt42acef32009-08-12 16:08:25 -0700722
Robert Greenwalta64bf832009-08-19 20:19:33 -0700723 if ((ni.isConnectedOrConnecting() == true) &&
724 !network.isTeardownRequested()) {
Robert Greenwalt42acef32009-08-12 16:08:25 -0700725 if (ni.isConnected() == true) {
726 // add the pid-specific dns
Robert Greenwaltc76b8fa2010-07-23 15:46:26 -0700727 handleDnsConfigurationChange(networkType);
Wink Savilleed9c02b2010-12-03 12:01:38 -0800728 if (DBG) log("special network already active");
Robert Greenwalt42acef32009-08-12 16:08:25 -0700729 return Phone.APN_ALREADY_ACTIVE;
730 }
Wink Savilleed9c02b2010-12-03 12:01:38 -0800731 if (DBG) log("special network already connecting");
Robert Greenwalt42acef32009-08-12 16:08:25 -0700732 return Phone.APN_REQUEST_STARTED;
733 }
734
735 // check if the radio in play can make another contact
736 // assume if cannot for now
737
Wink Savilleed9c02b2010-12-03 12:01:38 -0800738 if (DBG) log("reconnecting to special network");
Robert Greenwalt42acef32009-08-12 16:08:25 -0700739 network.reconnect();
740 return Phone.APN_REQUEST_STARTED;
741 } else {
Robert Greenwalt02648a42010-05-18 10:52:51 -0700742 return -1;
Robert Greenwalt42acef32009-08-12 16:08:25 -0700743 }
744 }
745 return Phone.APN_TYPE_NOT_AVAILABLE;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800746 }
747
Robert Greenwalt9c75d4a2009-09-27 17:27:04 -0700748 // javadoc from interface
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800749 public int stopUsingNetworkFeature(int networkType, String feature) {
Robert Greenwaltb8f16342009-10-06 17:52:40 -0700750 enforceChangePermission();
751
Robert Greenwalt9c75d4a2009-09-27 17:27:04 -0700752 int pid = getCallingPid();
753 int uid = getCallingUid();
754
755 FeatureUser u = null;
756 boolean found = false;
757
758 synchronized(this) {
759 for (int i = 0; i < mFeatureUsers.size() ; i++) {
760 u = (FeatureUser)mFeatureUsers.get(i);
761 if (uid == u.mUid && pid == u.mPid &&
762 networkType == u.mNetworkType &&
763 TextUtils.equals(feature, u.mFeature)) {
764 found = true;
765 break;
766 }
767 }
768 }
769 if (found && u != null) {
770 // stop regardless of how many other time this proc had called start
771 return stopUsingNetworkFeature(u, true);
772 } else {
773 // none found!
Wink Savilleed9c02b2010-12-03 12:01:38 -0800774 if (DBG) log("ignoring stopUsingNetworkFeature - not a live request");
Robert Greenwalt9c75d4a2009-09-27 17:27:04 -0700775 return 1;
776 }
Robert Greenwalt42acef32009-08-12 16:08:25 -0700777 }
778
Robert Greenwalt9c75d4a2009-09-27 17:27:04 -0700779 private int stopUsingNetworkFeature(FeatureUser u, boolean ignoreDups) {
780 int networkType = u.mNetworkType;
781 String feature = u.mFeature;
782 int pid = u.mPid;
783 int uid = u.mUid;
784
785 NetworkStateTracker tracker = null;
786 boolean callTeardown = false; // used to carry our decision outside of sync block
787
Robert Greenwalt42acef32009-08-12 16:08:25 -0700788 if (DBG) {
Wink Savilleed9c02b2010-12-03 12:01:38 -0800789 log("stopUsingNetworkFeature for net " + networkType +
Robert Greenwalt42acef32009-08-12 16:08:25 -0700790 ": " + feature);
791 }
Robert Greenwaltb8f16342009-10-06 17:52:40 -0700792
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800793 if (!ConnectivityManager.isNetworkTypeValid(networkType)) {
794 return -1;
795 }
Robert Greenwalt42acef32009-08-12 16:08:25 -0700796
Robert Greenwalt9c75d4a2009-09-27 17:27:04 -0700797 // need to link the mFeatureUsers list with the mNetRequestersPids state in this
798 // sync block
799 synchronized(this) {
800 // check if this process still has an outstanding start request
801 if (!mFeatureUsers.contains(u)) {
Wink Savilleed9c02b2010-12-03 12:01:38 -0800802 if (DBG) log("ignoring - this process has no outstanding requests");
Robert Greenwalt42acef32009-08-12 16:08:25 -0700803 return 1;
804 }
Robert Greenwalt9c75d4a2009-09-27 17:27:04 -0700805 u.unlinkDeathRecipient();
806 mFeatureUsers.remove(mFeatureUsers.indexOf(u));
807 // If we care about duplicate requests, check for that here.
808 //
809 // This is done to support the extension of a request - the app
810 // can request we start the network feature again and renew the
811 // auto-shutoff delay. Normal "stop" calls from the app though
812 // do not pay attention to duplicate requests - in effect the
813 // API does not refcount and a single stop will counter multiple starts.
814 if (ignoreDups == false) {
815 for (int i = 0; i < mFeatureUsers.size() ; i++) {
816 FeatureUser x = (FeatureUser)mFeatureUsers.get(i);
817 if (x.mUid == u.mUid && x.mPid == u.mPid &&
818 x.mNetworkType == u.mNetworkType &&
819 TextUtils.equals(x.mFeature, u.mFeature)) {
Wink Savilleed9c02b2010-12-03 12:01:38 -0800820 if (DBG) log("ignoring stopUsingNetworkFeature as dup is found");
Robert Greenwalt9c75d4a2009-09-27 17:27:04 -0700821 return 1;
822 }
823 }
Robert Greenwalt42acef32009-08-12 16:08:25 -0700824 }
Robert Greenwalt9c75d4a2009-09-27 17:27:04 -0700825
826 // TODO - move to MobileDataStateTracker
827 int usedNetworkType = networkType;
828 if (networkType == ConnectivityManager.TYPE_MOBILE) {
829 if (TextUtils.equals(feature, Phone.FEATURE_ENABLE_MMS)) {
830 usedNetworkType = ConnectivityManager.TYPE_MOBILE_MMS;
831 } else if (TextUtils.equals(feature, Phone.FEATURE_ENABLE_SUPL)) {
832 usedNetworkType = ConnectivityManager.TYPE_MOBILE_SUPL;
833 } else if (TextUtils.equals(feature, Phone.FEATURE_ENABLE_DUN)) {
834 usedNetworkType = ConnectivityManager.TYPE_MOBILE_DUN;
835 } else if (TextUtils.equals(feature, Phone.FEATURE_ENABLE_HIPRI)) {
836 usedNetworkType = ConnectivityManager.TYPE_MOBILE_HIPRI;
837 }
838 }
839 tracker = mNetTrackers[usedNetworkType];
Robert Greenwalt5154ae762009-10-30 14:17:42 -0700840 if (tracker == null) {
Wink Savilleed9c02b2010-12-03 12:01:38 -0800841 if (DBG) log("ignoring - no known tracker for net type " + usedNetworkType);
Robert Greenwalt5154ae762009-10-30 14:17:42 -0700842 return -1;
843 }
844 if (usedNetworkType != networkType) {
Robert Greenwalt9c75d4a2009-09-27 17:27:04 -0700845 Integer currentPid = new Integer(pid);
Robert Greenwalt9c75d4a2009-09-27 17:27:04 -0700846 mNetRequestersPids[usedNetworkType].remove(currentPid);
Robert Greenwalt421c72b2009-12-17 14:54:59 -0800847 reassessPidDns(pid, true);
Robert Greenwalt9c75d4a2009-09-27 17:27:04 -0700848 if (mNetRequestersPids[usedNetworkType].size() != 0) {
Wink Savilleed9c02b2010-12-03 12:01:38 -0800849 if (DBG) log("not tearing down special network - " +
Robert Greenwalt9c75d4a2009-09-27 17:27:04 -0700850 "others still using it");
851 return 1;
852 }
853 callTeardown = true;
854 }
855 }
Wink Savilleed9c02b2010-12-03 12:01:38 -0800856 if (DBG) log("Doing network teardown");
Robert Greenwalt9c75d4a2009-09-27 17:27:04 -0700857 if (callTeardown) {
858 tracker.teardown();
Robert Greenwalt42acef32009-08-12 16:08:25 -0700859 return 1;
860 } else {
Robert Greenwalt02648a42010-05-18 10:52:51 -0700861 return -1;
Robert Greenwalt42acef32009-08-12 16:08:25 -0700862 }
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800863 }
864
865 /**
Robert Greenwalt585ac0f2010-08-27 09:24:29 -0700866 * @deprecated use requestRouteToHostAddress instead
867 *
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800868 * Ensure that a network route exists to deliver traffic to the specified
869 * host via the specified network interface.
Robert Greenwalt86e9e552009-07-16 17:21:39 -0700870 * @param networkType the type of the network over which traffic to the
871 * specified host is to be routed
872 * @param hostAddress the IP address of the host to which the route is
873 * desired
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800874 * @return {@code true} on success, {@code false} on failure
875 */
876 public boolean requestRouteToHost(int networkType, int hostAddress) {
Robert Greenwalt585ac0f2010-08-27 09:24:29 -0700877 InetAddress inetAddress = NetworkUtils.intToInetAddress(hostAddress);
878
879 if (inetAddress == null) {
880 return false;
881 }
882
883 return requestRouteToHostAddress(networkType, inetAddress.getAddress());
884 }
885
886 /**
887 * Ensure that a network route exists to deliver traffic to the specified
888 * host via the specified network interface.
889 * @param networkType the type of the network over which traffic to the
890 * specified host is to be routed
891 * @param hostAddress the IP address of the host to which the route is
892 * desired
893 * @return {@code true} on success, {@code false} on failure
894 */
895 public boolean requestRouteToHostAddress(int networkType, byte[] hostAddress) {
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800896 enforceChangePermission();
897 if (!ConnectivityManager.isNetworkTypeValid(networkType)) {
898 return false;
899 }
900 NetworkStateTracker tracker = mNetTrackers[networkType];
Robert Greenwalt8206ff32009-09-10 15:06:20 -0700901
Robert Greenwalt5154ae762009-10-30 14:17:42 -0700902 if (tracker == null || !tracker.getNetworkInfo().isConnected() ||
903 tracker.isTeardownRequested()) {
Robert Greenwalt8206ff32009-09-10 15:06:20 -0700904 if (DBG) {
Wink Savilleed9c02b2010-12-03 12:01:38 -0800905 log("requestRouteToHostAddress on down network " +
Robert Greenwalt585ac0f2010-08-27 09:24:29 -0700906 "(" + networkType + ") - dropped");
Robert Greenwalt8206ff32009-09-10 15:06:20 -0700907 }
908 return false;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800909 }
Robert Greenwalt47f69fe2010-06-15 15:43:39 -0700910 try {
Robert Greenwalt585ac0f2010-08-27 09:24:29 -0700911 InetAddress addr = InetAddress.getByAddress(hostAddress);
Robert Greenwalt47f69fe2010-06-15 15:43:39 -0700912 return addHostRoute(tracker, addr);
913 } catch (UnknownHostException e) {}
914 return false;
Irfan Sheriffd649c122010-06-09 15:39:36 -0700915 }
916
917 /**
918 * Ensure that a network route exists to deliver traffic to the specified
919 * host via the mobile data network.
920 * @param hostAddress the IP address of the host to which the route is desired,
921 * in network byte order.
Robert Greenwalt47f69fe2010-06-15 15:43:39 -0700922 * TODO - deprecate
Irfan Sheriffd649c122010-06-09 15:39:36 -0700923 * @return {@code true} on success, {@code false} on failure
924 */
Robert Greenwalt47f69fe2010-06-15 15:43:39 -0700925 private boolean addHostRoute(NetworkStateTracker nt, InetAddress hostAddress) {
Irfan Sheriffd649c122010-06-09 15:39:36 -0700926 if (nt.getNetworkInfo().getType() == ConnectivityManager.TYPE_WIFI) {
927 return false;
928 }
929
Robert Greenwalt37e65eb2010-08-30 10:56:47 -0700930 LinkProperties p = nt.getLinkProperties();
Robert Greenwalt47f69fe2010-06-15 15:43:39 -0700931 if (p == null) return false;
932 String interfaceName = p.getInterfaceName();
Irfan Sheriffd649c122010-06-09 15:39:36 -0700933
934 if (DBG) {
Wink Savilleed9c02b2010-12-03 12:01:38 -0800935 log("Requested host route to " + hostAddress + "(" + interfaceName + ")");
Irfan Sheriffd649c122010-06-09 15:39:36 -0700936 }
Robert Greenwalt47f69fe2010-06-15 15:43:39 -0700937 if (interfaceName != null) {
Robert Greenwalt585ac0f2010-08-27 09:24:29 -0700938 return NetworkUtils.addHostRoute(interfaceName, hostAddress, null);
Irfan Sheriffd649c122010-06-09 15:39:36 -0700939 } else {
Wink Savilleed9c02b2010-12-03 12:01:38 -0800940 if (DBG) loge("addHostRoute failed due to null interface name");
Irfan Sheriffd649c122010-06-09 15:39:36 -0700941 return false;
942 }
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800943 }
944
945 /**
946 * @see ConnectivityManager#getBackgroundDataSetting()
947 */
948 public boolean getBackgroundDataSetting() {
949 return Settings.Secure.getInt(mContext.getContentResolver(),
950 Settings.Secure.BACKGROUND_DATA, 1) == 1;
951 }
Robert Greenwalt86e9e552009-07-16 17:21:39 -0700952
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800953 /**
954 * @see ConnectivityManager#setBackgroundDataSetting(boolean)
955 */
956 public void setBackgroundDataSetting(boolean allowBackgroundDataUsage) {
957 mContext.enforceCallingOrSelfPermission(
958 android.Manifest.permission.CHANGE_BACKGROUND_DATA_SETTING,
959 "ConnectivityService");
Robert Greenwalt86e9e552009-07-16 17:21:39 -0700960
Robert Greenwalt8dcc28b2010-09-23 10:05:56 -0700961 mHandler.sendMessage(mHandler.obtainMessage(EVENT_SET_BACKGROUND_DATA,
962 (allowBackgroundDataUsage ? ENABLED : DISABLED), 0));
963 }
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800964
Robert Greenwalt8dcc28b2010-09-23 10:05:56 -0700965 private void handleSetBackgroundData(boolean enabled) {
966 if (enabled != getBackgroundDataSetting()) {
967 Settings.Secure.putInt(mContext.getContentResolver(),
968 Settings.Secure.BACKGROUND_DATA, enabled ? 1 : 0);
969 Intent broadcast = new Intent(
970 ConnectivityManager.ACTION_BACKGROUND_DATA_SETTING_CHANGED);
971 mContext.sendBroadcast(broadcast);
972 }
Robert Greenwalt86e9e552009-07-16 17:21:39 -0700973 }
974
Robert Greenwaltc03fa502010-02-23 18:58:05 -0800975 /**
976 * @see ConnectivityManager#getMobileDataEnabled()
977 */
978 public boolean getMobileDataEnabled() {
Wink Savillee7982682010-12-07 10:31:02 -0800979 // TODO: This detail should probably be in DataConnectionTracker's
980 // which is where we store the value and maybe make this
981 // asynchronous.
Robert Greenwaltc03fa502010-02-23 18:58:05 -0800982 enforceAccessPermission();
983 boolean retVal = Settings.Secure.getInt(mContext.getContentResolver(),
984 Settings.Secure.MOBILE_DATA, 1) == 1;
Wink Savilleed9c02b2010-12-03 12:01:38 -0800985 if (DBG) log("getMobileDataEnabled returning " + retVal);
Robert Greenwaltc03fa502010-02-23 18:58:05 -0800986 return retVal;
987 }
988
989 /**
990 * @see ConnectivityManager#setMobileDataEnabled(boolean)
991 */
Robert Greenwalt8dcc28b2010-09-23 10:05:56 -0700992 public void setMobileDataEnabled(boolean enabled) {
Robert Greenwaltc03fa502010-02-23 18:58:05 -0800993 enforceChangePermission();
Wink Savilleed9c02b2010-12-03 12:01:38 -0800994 if (DBG) log("setMobileDataEnabled(" + enabled + ")");
Robert Greenwaltc03fa502010-02-23 18:58:05 -0800995
Robert Greenwalt8dcc28b2010-09-23 10:05:56 -0700996 mHandler.sendMessage(mHandler.obtainMessage(EVENT_SET_MOBILE_DATA,
997 (enabled ? ENABLED : DISABLED), 0));
998 }
999
1000 private void handleSetMobileData(boolean enabled) {
Wink Savillee7982682010-12-07 10:31:02 -08001001 if (mNetTrackers[ConnectivityManager.TYPE_MOBILE] != null) {
1002 if (DBG) {
1003 Slog.d(TAG, mNetTrackers[ConnectivityManager.TYPE_MOBILE].toString() + enabled);
Robert Greenwaltc03fa502010-02-23 18:58:05 -08001004 }
Wink Savillee7982682010-12-07 10:31:02 -08001005 mNetTrackers[ConnectivityManager.TYPE_MOBILE].setDataEnable(enabled);
Robert Greenwaltc03fa502010-02-23 18:58:05 -08001006 }
1007 }
1008
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001009 private void enforceAccessPermission() {
Robert Greenwalt86e9e552009-07-16 17:21:39 -07001010 mContext.enforceCallingOrSelfPermission(
1011 android.Manifest.permission.ACCESS_NETWORK_STATE,
1012 "ConnectivityService");
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001013 }
1014
1015 private void enforceChangePermission() {
Robert Greenwalt86e9e552009-07-16 17:21:39 -07001016 mContext.enforceCallingOrSelfPermission(
1017 android.Manifest.permission.CHANGE_NETWORK_STATE,
1018 "ConnectivityService");
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001019 }
1020
Robert Greenwaltd0e18ff2010-01-26 11:40:34 -08001021 // TODO Make this a special check when it goes public
1022 private void enforceTetherChangePermission() {
1023 mContext.enforceCallingOrSelfPermission(
1024 android.Manifest.permission.CHANGE_NETWORK_STATE,
1025 "ConnectivityService");
1026 }
1027
Robert Greenwalt2a091d72010-02-11 18:18:40 -08001028 private void enforceTetherAccessPermission() {
1029 mContext.enforceCallingOrSelfPermission(
1030 android.Manifest.permission.ACCESS_NETWORK_STATE,
1031 "ConnectivityService");
1032 }
1033
Robert Greenwalt14f2ef42010-06-15 12:19:37 -07001034 private void enforceConnectivityInternalPermission() {
1035 mContext.enforceCallingOrSelfPermission(
1036 android.Manifest.permission.CONNECTIVITY_INTERNAL,
1037 "ConnectivityService");
1038 }
1039
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001040 /**
Robert Greenwalt86e9e552009-07-16 17:21:39 -07001041 * Handle a {@code DISCONNECTED} event. If this pertains to the non-active
1042 * network, we ignore it. If it is for the active network, we send out a
1043 * broadcast. But first, we check whether it might be possible to connect
1044 * to a different network.
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001045 * @param info the {@code NetworkInfo} for the network
1046 */
1047 private void handleDisconnect(NetworkInfo info) {
1048
Robert Greenwalt42acef32009-08-12 16:08:25 -07001049 int prevNetType = info.getType();
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001050
Robert Greenwalt42acef32009-08-12 16:08:25 -07001051 mNetTrackers[prevNetType].setTeardownRequested(false);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001052 /*
1053 * If the disconnected network is not the active one, then don't report
1054 * this as a loss of connectivity. What probably happened is that we're
1055 * getting the disconnect for a network that we explicitly disabled
1056 * in accordance with network preference policies.
1057 */
Robert Greenwalt42acef32009-08-12 16:08:25 -07001058 if (!mNetAttributes[prevNetType].isDefault()) {
1059 List pids = mNetRequestersPids[prevNetType];
1060 for (int i = 0; i<pids.size(); i++) {
1061 Integer pid = (Integer)pids.get(i);
1062 // will remove them because the net's no longer connected
1063 // need to do this now as only now do we know the pids and
1064 // can properly null things that are no longer referenced.
1065 reassessPidDns(pid.intValue(), false);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001066 }
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001067 }
1068
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001069 Intent intent = new Intent(ConnectivityManager.CONNECTIVITY_ACTION);
1070 intent.putExtra(ConnectivityManager.EXTRA_NETWORK_INFO, info);
1071 if (info.isFailover()) {
1072 intent.putExtra(ConnectivityManager.EXTRA_IS_FAILOVER, true);
1073 info.setFailover(false);
1074 }
1075 if (info.getReason() != null) {
1076 intent.putExtra(ConnectivityManager.EXTRA_REASON, info.getReason());
1077 }
1078 if (info.getExtraInfo() != null) {
Robert Greenwalt86e9e552009-07-16 17:21:39 -07001079 intent.putExtra(ConnectivityManager.EXTRA_EXTRA_INFO,
1080 info.getExtraInfo());
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001081 }
Robert Greenwalt42acef32009-08-12 16:08:25 -07001082
Robert Greenwaltcc4b4012010-01-25 17:54:29 -08001083 NetworkStateTracker newNet = null;
1084 if (mNetAttributes[prevNetType].isDefault()) {
1085 newNet = tryFailover(prevNetType);
1086 if (newNet != null) {
1087 NetworkInfo switchTo = newNet.getNetworkInfo();
Robert Greenwalt029be812010-09-20 18:01:43 -07001088 if (!switchTo.isConnected()) {
Wink Savilleed9c02b2010-12-03 12:01:38 -08001089 // if the other net is connected they've already reset this and perhaps even
1090 // gotten a positive report we don't want to overwrite, but if not we need to
1091 // clear this now to turn our cellular sig strength white
Robert Greenwalt029be812010-09-20 18:01:43 -07001092 mDefaultInetConditionPublished = 0;
1093 }
Robert Greenwaltcc4b4012010-01-25 17:54:29 -08001094 intent.putExtra(ConnectivityManager.EXTRA_OTHER_NETWORK_INFO, switchTo);
1095 } else {
Robert Greenwalt029be812010-09-20 18:01:43 -07001096 mDefaultInetConditionPublished = 0; // we're not connected anymore
Robert Greenwaltcc4b4012010-01-25 17:54:29 -08001097 intent.putExtra(ConnectivityManager.EXTRA_NO_CONNECTIVITY, true);
1098 }
Robert Greenwaltda03c4e2010-01-20 19:29:41 -08001099 }
Robert Greenwalt029be812010-09-20 18:01:43 -07001100 intent.putExtra(ConnectivityManager.EXTRA_INET_CONDITION, mDefaultInetConditionPublished);
Robert Greenwaltda03c4e2010-01-20 19:29:41 -08001101 // do this before we broadcast the change
Robert Greenwaltc76b8fa2010-07-23 15:46:26 -07001102 handleConnectivityChange(prevNetType);
Robert Greenwaltda03c4e2010-01-20 19:29:41 -08001103
1104 sendStickyBroadcast(intent);
1105 /*
1106 * If the failover network is already connected, then immediately send
1107 * out a followup broadcast indicating successful failover
1108 */
1109 if (newNet != null && newNet.getNetworkInfo().isConnected()) {
1110 sendConnectedBroadcast(newNet.getNetworkInfo());
1111 }
1112 }
1113
Robert Greenwaltcc4b4012010-01-25 17:54:29 -08001114 // returns null if no failover available
Robert Greenwaltda03c4e2010-01-20 19:29:41 -08001115 private NetworkStateTracker tryFailover(int prevNetType) {
Robert Greenwalt42acef32009-08-12 16:08:25 -07001116 /*
1117 * If this is a default network, check if other defaults are available
1118 * or active
1119 */
1120 NetworkStateTracker newNet = null;
1121 if (mNetAttributes[prevNetType].isDefault()) {
Robert Greenwalt42acef32009-08-12 16:08:25 -07001122 if (mActiveDefaultNetwork == prevNetType) {
1123 mActiveDefaultNetwork = -1;
1124 }
1125
1126 int newType = -1;
1127 int newPriority = -1;
Robert Greenwaltda03c4e2010-01-20 19:29:41 -08001128 for (int checkType=0; checkType <= ConnectivityManager.MAX_NETWORK_TYPE; checkType++) {
Robert Greenwalt5154ae762009-10-30 14:17:42 -07001129 if (checkType == prevNetType) continue;
1130 if (mNetAttributes[checkType] == null) continue;
Robert Greenwalt42acef32009-08-12 16:08:25 -07001131 if (mNetAttributes[checkType].isDefault()) {
1132 /* TODO - if we have multiple nets we could use
1133 * we may want to put more thought into which we choose
1134 */
1135 if (checkType == mNetworkPreference) {
1136 newType = checkType;
1137 break;
1138 }
Robert Greenwalt5154ae762009-10-30 14:17:42 -07001139 if (mNetAttributes[checkType].mPriority > newPriority) {
Robert Greenwalt42acef32009-08-12 16:08:25 -07001140 newType = checkType;
Robert Greenwalt5154ae762009-10-30 14:17:42 -07001141 newPriority = mNetAttributes[newType].mPriority;
Robert Greenwalt42acef32009-08-12 16:08:25 -07001142 }
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001143 }
1144 }
Robert Greenwalt42acef32009-08-12 16:08:25 -07001145
1146 if (newType != -1) {
1147 newNet = mNetTrackers[newType];
1148 /**
1149 * See if the other network is available to fail over to.
1150 * If is not available, we enable it anyway, so that it
1151 * will be able to connect when it does become available,
1152 * but we report a total loss of connectivity rather than
1153 * report that we are attempting to fail over.
1154 */
1155 if (newNet.isAvailable()) {
1156 NetworkInfo switchTo = newNet.getNetworkInfo();
1157 switchTo.setFailover(true);
Robert Greenwalta64bf832009-08-19 20:19:33 -07001158 if (!switchTo.isConnectedOrConnecting() ||
1159 newNet.isTeardownRequested()) {
Robert Greenwalt42acef32009-08-12 16:08:25 -07001160 newNet.reconnect();
1161 }
1162 if (DBG) {
1163 if (switchTo.isConnected()) {
Wink Savilleed9c02b2010-12-03 12:01:38 -08001164 log("Switching to already connected " + switchTo.getTypeName());
Robert Greenwalt42acef32009-08-12 16:08:25 -07001165 } else {
Wink Savilleed9c02b2010-12-03 12:01:38 -08001166 log("Attempting to switch to " + switchTo.getTypeName());
Robert Greenwalt42acef32009-08-12 16:08:25 -07001167 }
1168 }
Robert Greenwalt42acef32009-08-12 16:08:25 -07001169 } else {
1170 newNet.reconnect();
Robert Greenwaltf0fa39e2010-03-09 14:55:08 -08001171 newNet = null; // not officially avail.. try anyway, but
1172 // report no failover
Robert Greenwalt42acef32009-08-12 16:08:25 -07001173 }
Robert Greenwalt572172b2010-10-08 16:35:52 -07001174 } else {
Wink Savilleed9c02b2010-12-03 12:01:38 -08001175 loge("Network failover failing.");
Robert Greenwalt42acef32009-08-12 16:08:25 -07001176 }
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001177 }
Robert Greenwalt42acef32009-08-12 16:08:25 -07001178
Robert Greenwaltda03c4e2010-01-20 19:29:41 -08001179 return newNet;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001180 }
1181
1182 private void sendConnectedBroadcast(NetworkInfo info) {
Robert Greenwalt1e9aac22010-09-15 17:36:33 -07001183 sendGeneralBroadcast(info, ConnectivityManager.CONNECTIVITY_ACTION);
1184 }
1185
1186 private void sendInetConditionBroadcast(NetworkInfo info) {
1187 sendGeneralBroadcast(info, ConnectivityManager.INET_CONDITION_ACTION);
1188 }
1189
1190 private void sendGeneralBroadcast(NetworkInfo info, String bcastType) {
1191 Intent intent = new Intent(bcastType);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001192 intent.putExtra(ConnectivityManager.EXTRA_NETWORK_INFO, info);
1193 if (info.isFailover()) {
1194 intent.putExtra(ConnectivityManager.EXTRA_IS_FAILOVER, true);
1195 info.setFailover(false);
1196 }
1197 if (info.getReason() != null) {
1198 intent.putExtra(ConnectivityManager.EXTRA_REASON, info.getReason());
1199 }
1200 if (info.getExtraInfo() != null) {
Robert Greenwalt86e9e552009-07-16 17:21:39 -07001201 intent.putExtra(ConnectivityManager.EXTRA_EXTRA_INFO,
1202 info.getExtraInfo());
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001203 }
Robert Greenwaltd7085fc2010-09-08 15:24:47 -07001204 intent.putExtra(ConnectivityManager.EXTRA_INET_CONDITION, mDefaultInetConditionPublished);
Mike Lockwood0f79b542009-08-14 14:18:49 -04001205 sendStickyBroadcast(intent);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001206 }
1207
1208 /**
1209 * Called when an attempt to fail over to another network has failed.
1210 * @param info the {@link NetworkInfo} for the failed network
1211 */
1212 private void handleConnectionFailure(NetworkInfo info) {
1213 mNetTrackers[info.getType()].setTeardownRequested(false);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001214
Robert Greenwalt42acef32009-08-12 16:08:25 -07001215 String reason = info.getReason();
1216 String extraInfo = info.getExtraInfo();
Robert Greenwalt86e9e552009-07-16 17:21:39 -07001217
Robert Greenwalt572172b2010-10-08 16:35:52 -07001218 String reasonText;
1219 if (reason == null) {
1220 reasonText = ".";
1221 } else {
1222 reasonText = " (" + reason + ").";
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001223 }
Wink Savilleed9c02b2010-12-03 12:01:38 -08001224 loge("Attempt to connect to " + info.getTypeName() + " failed" + reasonText);
Robert Greenwalt42acef32009-08-12 16:08:25 -07001225
1226 Intent intent = new Intent(ConnectivityManager.CONNECTIVITY_ACTION);
1227 intent.putExtra(ConnectivityManager.EXTRA_NETWORK_INFO, info);
1228 if (getActiveNetworkInfo() == null) {
1229 intent.putExtra(ConnectivityManager.EXTRA_NO_CONNECTIVITY, true);
1230 }
1231 if (reason != null) {
1232 intent.putExtra(ConnectivityManager.EXTRA_REASON, reason);
1233 }
1234 if (extraInfo != null) {
1235 intent.putExtra(ConnectivityManager.EXTRA_EXTRA_INFO, extraInfo);
1236 }
1237 if (info.isFailover()) {
1238 intent.putExtra(ConnectivityManager.EXTRA_IS_FAILOVER, true);
1239 info.setFailover(false);
1240 }
Robert Greenwaltda03c4e2010-01-20 19:29:41 -08001241
Robert Greenwaltcc4b4012010-01-25 17:54:29 -08001242 NetworkStateTracker newNet = null;
1243 if (mNetAttributes[info.getType()].isDefault()) {
1244 newNet = tryFailover(info.getType());
1245 if (newNet != null) {
1246 NetworkInfo switchTo = newNet.getNetworkInfo();
Robert Greenwalt029be812010-09-20 18:01:43 -07001247 if (!switchTo.isConnected()) {
Robert Greenwalt572172b2010-10-08 16:35:52 -07001248 // if the other net is connected they've already reset this and perhaps
1249 // even gotten a positive report we don't want to overwrite, but if not
1250 // we need to clear this now to turn our cellular sig strength white
Robert Greenwalt029be812010-09-20 18:01:43 -07001251 mDefaultInetConditionPublished = 0;
1252 }
Robert Greenwaltcc4b4012010-01-25 17:54:29 -08001253 intent.putExtra(ConnectivityManager.EXTRA_OTHER_NETWORK_INFO, switchTo);
1254 } else {
Robert Greenwalt029be812010-09-20 18:01:43 -07001255 mDefaultInetConditionPublished = 0;
Robert Greenwaltcc4b4012010-01-25 17:54:29 -08001256 intent.putExtra(ConnectivityManager.EXTRA_NO_CONNECTIVITY, true);
1257 }
Robert Greenwaltda03c4e2010-01-20 19:29:41 -08001258 }
Robert Greenwaltcc4b4012010-01-25 17:54:29 -08001259
Robert Greenwalt029be812010-09-20 18:01:43 -07001260 intent.putExtra(ConnectivityManager.EXTRA_INET_CONDITION, mDefaultInetConditionPublished);
Mike Lockwood0f79b542009-08-14 14:18:49 -04001261 sendStickyBroadcast(intent);
Robert Greenwaltda03c4e2010-01-20 19:29:41 -08001262 /*
1263 * If the failover network is already connected, then immediately send
1264 * out a followup broadcast indicating successful failover
1265 */
1266 if (newNet != null && newNet.getNetworkInfo().isConnected()) {
1267 sendConnectedBroadcast(newNet.getNetworkInfo());
1268 }
Mike Lockwood0f79b542009-08-14 14:18:49 -04001269 }
1270
1271 private void sendStickyBroadcast(Intent intent) {
1272 synchronized(this) {
Dianne Hackborn1c633fc2009-12-08 19:45:14 -08001273 if (!mSystemReady) {
1274 mInitialBroadcast = new Intent(intent);
Mike Lockwood0f79b542009-08-14 14:18:49 -04001275 }
Dianne Hackborn1c633fc2009-12-08 19:45:14 -08001276 intent.addFlags(Intent.FLAG_RECEIVER_REGISTERED_ONLY_BEFORE_BOOT);
1277 mContext.sendStickyBroadcast(intent);
Mike Lockwood0f79b542009-08-14 14:18:49 -04001278 }
1279 }
1280
1281 void systemReady() {
1282 synchronized(this) {
1283 mSystemReady = true;
Dianne Hackborn1c633fc2009-12-08 19:45:14 -08001284 if (mInitialBroadcast != null) {
1285 mContext.sendStickyBroadcast(mInitialBroadcast);
1286 mInitialBroadcast = null;
Mike Lockwood0f79b542009-08-14 14:18:49 -04001287 }
1288 }
Robert Greenwalt434203a2010-10-11 16:00:27 -07001289 // load the global proxy at startup
1290 mHandler.sendMessage(mHandler.obtainMessage(EVENT_APPLY_GLOBAL_HTTP_PROXY));
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001291 }
1292
1293 private void handleConnect(NetworkInfo info) {
Robert Greenwalt42acef32009-08-12 16:08:25 -07001294 int type = info.getType();
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001295
1296 // snapshot isFailover, because sendConnectedBroadcast() resets it
1297 boolean isFailover = info.isFailover();
Robert Greenwalt42acef32009-08-12 16:08:25 -07001298 NetworkStateTracker thisNet = mNetTrackers[type];
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001299
Robert Greenwalt42acef32009-08-12 16:08:25 -07001300 // if this is a default net and other default is running
1301 // kill the one not preferred
1302 if (mNetAttributes[type].isDefault()) {
Robert Greenwalt42acef32009-08-12 16:08:25 -07001303 if (mActiveDefaultNetwork != -1 && mActiveDefaultNetwork != type) {
1304 if ((type != mNetworkPreference &&
1305 mNetAttributes[mActiveDefaultNetwork].mPriority >
1306 mNetAttributes[type].mPriority) ||
1307 mNetworkPreference == mActiveDefaultNetwork) {
1308 // don't accept this one
Wink Savilleed9c02b2010-12-03 12:01:38 -08001309 if (DBG) {
1310 log("Not broadcasting CONNECT_ACTION " +
Robert Greenwalt42acef32009-08-12 16:08:25 -07001311 "to torn down network " + info.getTypeName());
Wink Savilleed9c02b2010-12-03 12:01:38 -08001312 }
Robert Greenwalt42acef32009-08-12 16:08:25 -07001313 teardown(thisNet);
1314 return;
1315 } else {
1316 // tear down the other
1317 NetworkStateTracker otherNet =
1318 mNetTrackers[mActiveDefaultNetwork];
Wink Savilleed9c02b2010-12-03 12:01:38 -08001319 if (DBG) {
1320 log("Policy requires " + otherNet.getNetworkInfo().getTypeName() +
Robert Greenwalt42acef32009-08-12 16:08:25 -07001321 " teardown");
Wink Savilleed9c02b2010-12-03 12:01:38 -08001322 }
Robert Greenwalt42acef32009-08-12 16:08:25 -07001323 if (!teardown(otherNet)) {
Wink Savilleed9c02b2010-12-03 12:01:38 -08001324 loge("Network declined teardown request");
Robert Greenwalt42acef32009-08-12 16:08:25 -07001325 return;
1326 }
Robert Greenwalt14f2ef42010-06-15 12:19:37 -07001327 }
1328 }
1329 synchronized (ConnectivityService.this) {
1330 // have a new default network, release the transition wakelock in a second
1331 // if it's held. The second pause is to allow apps to reconnect over the
1332 // new network
1333 if (mNetTransitionWakeLock.isHeld()) {
1334 mHandler.sendMessageDelayed(mHandler.obtainMessage(
Robert Greenwaltf3331232010-09-24 14:32:21 -07001335 EVENT_CLEAR_NET_TRANSITION_WAKELOCK,
Robert Greenwalt14f2ef42010-06-15 12:19:37 -07001336 mNetTransitionWakeLockSerialNumber, 0),
1337 1000);
Robert Greenwalt42acef32009-08-12 16:08:25 -07001338 }
1339 }
1340 mActiveDefaultNetwork = type;
Robert Greenwaltd7085fc2010-09-08 15:24:47 -07001341 // this will cause us to come up initially as unconnected and switching
1342 // to connected after our normal pause unless somebody reports us as reall
1343 // disconnected
1344 mDefaultInetConditionPublished = 0;
1345 mDefaultConnectionSequence++;
1346 mInetConditionChangeInFlight = false;
1347 // Don't do this - if we never sign in stay, grey
1348 //reportNetworkCondition(mActiveDefaultNetwork, 100);
Robert Greenwalt42acef32009-08-12 16:08:25 -07001349 }
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001350 thisNet.setTeardownRequested(false);
Irfan Sheriffd649c122010-06-09 15:39:36 -07001351 updateNetworkSettings(thisNet);
Robert Greenwaltc76b8fa2010-07-23 15:46:26 -07001352 handleConnectivityChange(type);
Robert Greenwalt42acef32009-08-12 16:08:25 -07001353 sendConnectedBroadcast(info);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001354 }
1355
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001356 /**
Robert Greenwaltc76b8fa2010-07-23 15:46:26 -07001357 * After a change in the connectivity state of a network. We're mainly
1358 * concerned with making sure that the list of DNS servers is set up
1359 * according to which networks are connected, and ensuring that the
1360 * right routing table entries exist.
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001361 */
Robert Greenwaltc76b8fa2010-07-23 15:46:26 -07001362 private void handleConnectivityChange(int netType) {
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001363 /*
Robert Greenwalt42acef32009-08-12 16:08:25 -07001364 * If a non-default network is enabled, add the host routes that
Robert Greenwaltc76b8fa2010-07-23 15:46:26 -07001365 * will allow it's DNS servers to be accessed.
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001366 */
Robert Greenwaltc76b8fa2010-07-23 15:46:26 -07001367 handleDnsConfigurationChange(netType);
Robert Greenwalt42acef32009-08-12 16:08:25 -07001368
Robert Greenwaltc76b8fa2010-07-23 15:46:26 -07001369 if (mNetTrackers[netType].getNetworkInfo().isConnected()) {
1370 if (mNetAttributes[netType].isDefault()) {
Robert Greenwalt434203a2010-10-11 16:00:27 -07001371 handleApplyDefaultProxy(netType);
Robert Greenwaltc76b8fa2010-07-23 15:46:26 -07001372 addDefaultRoute(mNetTrackers[netType]);
Robert Greenwalt42acef32009-08-12 16:08:25 -07001373 } else {
Robert Greenwaltc76b8fa2010-07-23 15:46:26 -07001374 addPrivateDnsRoutes(mNetTrackers[netType]);
1375 }
1376 } else {
1377 if (mNetAttributes[netType].isDefault()) {
1378 removeDefaultRoute(mNetTrackers[netType]);
1379 } else {
1380 removePrivateDnsRoutes(mNetTrackers[netType]);
Robert Greenwalt42acef32009-08-12 16:08:25 -07001381 }
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001382 }
1383 }
1384
Irfan Sheriffd649c122010-06-09 15:39:36 -07001385 private void addPrivateDnsRoutes(NetworkStateTracker nt) {
Irfan Sheriffd649c122010-06-09 15:39:36 -07001386 boolean privateDnsRouteSet = nt.isPrivateDnsRouteSet();
Robert Greenwalt37e65eb2010-08-30 10:56:47 -07001387 LinkProperties p = nt.getLinkProperties();
Robert Greenwalt47f69fe2010-06-15 15:43:39 -07001388 if (p == null) return;
1389 String interfaceName = p.getInterfaceName();
Irfan Sheriffd649c122010-06-09 15:39:36 -07001390
1391 if (DBG) {
Wink Savilleed9c02b2010-12-03 12:01:38 -08001392 log("addPrivateDnsRoutes for " + nt +
Irfan Sheriffd649c122010-06-09 15:39:36 -07001393 "(" + interfaceName + ") - mPrivateDnsRouteSet = " + privateDnsRouteSet);
1394 }
Irfan Sheriffd649c122010-06-09 15:39:36 -07001395 if (interfaceName != null && !privateDnsRouteSet) {
Robert Greenwalt47f69fe2010-06-15 15:43:39 -07001396 Collection<InetAddress> dnsList = p.getDnses();
1397 for (InetAddress dns : dnsList) {
Wink Savilleed9c02b2010-12-03 12:01:38 -08001398 if (DBG) log(" adding " + dns);
Robert Greenwalt585ac0f2010-08-27 09:24:29 -07001399 NetworkUtils.addHostRoute(interfaceName, dns, null);
Irfan Sheriffd649c122010-06-09 15:39:36 -07001400 }
1401 nt.privateDnsRouteSet(true);
1402 }
1403 }
1404
1405 private void removePrivateDnsRoutes(NetworkStateTracker nt) {
1406 // TODO - we should do this explicitly but the NetUtils api doesnt
1407 // support this yet - must remove all. No worse than before
Robert Greenwalt37e65eb2010-08-30 10:56:47 -07001408 LinkProperties p = nt.getLinkProperties();
Robert Greenwalt47f69fe2010-06-15 15:43:39 -07001409 if (p == null) return;
1410 String interfaceName = p.getInterfaceName();
Irfan Sheriffd649c122010-06-09 15:39:36 -07001411 boolean privateDnsRouteSet = nt.isPrivateDnsRouteSet();
1412 if (interfaceName != null && privateDnsRouteSet) {
1413 if (DBG) {
Wink Savilleed9c02b2010-12-03 12:01:38 -08001414 log("removePrivateDnsRoutes for " + nt.getNetworkInfo().getTypeName() +
Irfan Sheriffd649c122010-06-09 15:39:36 -07001415 " (" + interfaceName + ")");
1416 }
1417 NetworkUtils.removeHostRoutes(interfaceName);
1418 nt.privateDnsRouteSet(false);
1419 }
1420 }
1421
Irfan Sheriffd649c122010-06-09 15:39:36 -07001422
1423 private void addDefaultRoute(NetworkStateTracker nt) {
Robert Greenwalt37e65eb2010-08-30 10:56:47 -07001424 LinkProperties p = nt.getLinkProperties();
Robert Greenwalt47f69fe2010-06-15 15:43:39 -07001425 if (p == null) return;
1426 String interfaceName = p.getInterfaceName();
1427 InetAddress defaultGatewayAddr = p.getGateway();
Irfan Sheriffd649c122010-06-09 15:39:36 -07001428
Robert Greenwaltc76b8fa2010-07-23 15:46:26 -07001429 if ((interfaceName != null) && (defaultGatewayAddr != null )) {
Robert Greenwalt585ac0f2010-08-27 09:24:29 -07001430 if (!NetworkUtils.addDefaultRoute(interfaceName, defaultGatewayAddr) && DBG) {
Robert Greenwalt47f69fe2010-06-15 15:43:39 -07001431 NetworkInfo networkInfo = nt.getNetworkInfo();
Wink Savilleed9c02b2010-12-03 12:01:38 -08001432 log("addDefaultRoute for " + networkInfo.getTypeName() +
Irfan Sheriffd649c122010-06-09 15:39:36 -07001433 " (" + interfaceName + "), GatewayAddr=" + defaultGatewayAddr);
1434 }
Irfan Sheriffd649c122010-06-09 15:39:36 -07001435 }
1436 }
1437
1438
1439 public void removeDefaultRoute(NetworkStateTracker nt) {
Robert Greenwalt37e65eb2010-08-30 10:56:47 -07001440 LinkProperties p = nt.getLinkProperties();
Robert Greenwalt47f69fe2010-06-15 15:43:39 -07001441 if (p == null) return;
1442 String interfaceName = p.getInterfaceName();
Irfan Sheriffd649c122010-06-09 15:39:36 -07001443
Robert Greenwaltc76b8fa2010-07-23 15:46:26 -07001444 if (interfaceName != null) {
1445 if ((NetworkUtils.removeDefaultRoute(interfaceName) >= 0) && DBG) {
Robert Greenwalt47f69fe2010-06-15 15:43:39 -07001446 NetworkInfo networkInfo = nt.getNetworkInfo();
Wink Savilleed9c02b2010-12-03 12:01:38 -08001447 log("removeDefaultRoute for " + networkInfo.getTypeName() + " (" +
Irfan Sheriffd649c122010-06-09 15:39:36 -07001448 interfaceName + ")");
1449 }
Irfan Sheriffd649c122010-06-09 15:39:36 -07001450 }
1451 }
1452
1453 /**
1454 * Reads the network specific TCP buffer sizes from SystemProperties
1455 * net.tcp.buffersize.[default|wifi|umts|edge|gprs] and set them for system
1456 * wide use
1457 */
1458 public void updateNetworkSettings(NetworkStateTracker nt) {
1459 String key = nt.getTcpBufferSizesPropName();
1460 String bufferSizes = SystemProperties.get(key);
1461
1462 if (bufferSizes.length() == 0) {
Wink Savilleed9c02b2010-12-03 12:01:38 -08001463 loge(key + " not found in system properties. Using defaults");
Irfan Sheriffd649c122010-06-09 15:39:36 -07001464
1465 // Setting to default values so we won't be stuck to previous values
1466 key = "net.tcp.buffersize.default";
1467 bufferSizes = SystemProperties.get(key);
1468 }
1469
1470 // Set values in kernel
1471 if (bufferSizes.length() != 0) {
1472 if (DBG) {
Wink Savilleed9c02b2010-12-03 12:01:38 -08001473 log("Setting TCP values: [" + bufferSizes
Irfan Sheriffd649c122010-06-09 15:39:36 -07001474 + "] which comes from [" + key + "]");
1475 }
1476 setBufferSize(bufferSizes);
1477 }
1478 }
1479
1480 /**
1481 * Writes TCP buffer sizes to /sys/kernel/ipv4/tcp_[r/w]mem_[min/def/max]
1482 * which maps to /proc/sys/net/ipv4/tcp_rmem and tcpwmem
1483 *
1484 * @param bufferSizes in the format of "readMin, readInitial, readMax,
1485 * writeMin, writeInitial, writeMax"
1486 */
1487 private void setBufferSize(String bufferSizes) {
1488 try {
1489 String[] values = bufferSizes.split(",");
1490
1491 if (values.length == 6) {
1492 final String prefix = "/sys/kernel/ipv4/tcp_";
1493 stringToFile(prefix + "rmem_min", values[0]);
1494 stringToFile(prefix + "rmem_def", values[1]);
1495 stringToFile(prefix + "rmem_max", values[2]);
1496 stringToFile(prefix + "wmem_min", values[3]);
1497 stringToFile(prefix + "wmem_def", values[4]);
1498 stringToFile(prefix + "wmem_max", values[5]);
1499 } else {
Wink Savilleed9c02b2010-12-03 12:01:38 -08001500 loge("Invalid buffersize string: " + bufferSizes);
Irfan Sheriffd649c122010-06-09 15:39:36 -07001501 }
1502 } catch (IOException e) {
Wink Savilleed9c02b2010-12-03 12:01:38 -08001503 loge("Can't set tcp buffer sizes:" + e);
Irfan Sheriffd649c122010-06-09 15:39:36 -07001504 }
1505 }
1506
1507 /**
1508 * Writes string to file. Basically same as "echo -n $string > $filename"
1509 *
1510 * @param filename
1511 * @param string
1512 * @throws IOException
1513 */
1514 private void stringToFile(String filename, String string) throws IOException {
1515 FileWriter out = new FileWriter(filename);
1516 try {
1517 out.write(string);
1518 } finally {
1519 out.close();
1520 }
1521 }
1522
1523
Robert Greenwalt42acef32009-08-12 16:08:25 -07001524 /**
1525 * Adjust the per-process dns entries (net.dns<x>.<pid>) based
1526 * on the highest priority active net which this process requested.
1527 * If there aren't any, clear it out
1528 */
1529 private void reassessPidDns(int myPid, boolean doBump)
1530 {
Wink Savilleed9c02b2010-12-03 12:01:38 -08001531 if (DBG) log("reassessPidDns for pid " + myPid);
Robert Greenwalt42acef32009-08-12 16:08:25 -07001532 for(int i : mPriorityList) {
1533 if (mNetAttributes[i].isDefault()) {
1534 continue;
1535 }
1536 NetworkStateTracker nt = mNetTrackers[i];
Robert Greenwalt86e9e552009-07-16 17:21:39 -07001537 if (nt.getNetworkInfo().isConnected() &&
1538 !nt.isTeardownRequested()) {
Robert Greenwalt37e65eb2010-08-30 10:56:47 -07001539 LinkProperties p = nt.getLinkProperties();
Robert Greenwalt47f69fe2010-06-15 15:43:39 -07001540 if (p == null) continue;
Robert Greenwalt42acef32009-08-12 16:08:25 -07001541 List pids = mNetRequestersPids[i];
1542 for (int j=0; j<pids.size(); j++) {
1543 Integer pid = (Integer)pids.get(j);
1544 if (pid.intValue() == myPid) {
Robert Greenwalt47f69fe2010-06-15 15:43:39 -07001545 Collection<InetAddress> dnses = p.getDnses();
1546 writePidDns(dnses, myPid);
Robert Greenwalt42acef32009-08-12 16:08:25 -07001547 if (doBump) {
1548 bumpDns();
1549 }
1550 return;
1551 }
1552 }
1553 }
1554 }
1555 // nothing found - delete
1556 for (int i = 1; ; i++) {
1557 String prop = "net.dns" + i + "." + myPid;
1558 if (SystemProperties.get(prop).length() == 0) {
1559 if (doBump) {
1560 bumpDns();
1561 }
1562 return;
1563 }
1564 SystemProperties.set(prop, "");
1565 }
1566 }
1567
Robert Greenwalt47f69fe2010-06-15 15:43:39 -07001568 private void writePidDns(Collection <InetAddress> dnses, int pid) {
Robert Greenwalt42acef32009-08-12 16:08:25 -07001569 int j = 1;
Robert Greenwalt47f69fe2010-06-15 15:43:39 -07001570 for (InetAddress dns : dnses) {
1571 SystemProperties.set("net.dns" + j++ + "." + pid, dns.getHostAddress());
Robert Greenwalt42acef32009-08-12 16:08:25 -07001572 }
1573 }
1574
1575 private void bumpDns() {
1576 /*
1577 * Bump the property that tells the name resolver library to reread
1578 * the DNS server list from the properties.
1579 */
1580 String propVal = SystemProperties.get("net.dnschange");
1581 int n = 0;
1582 if (propVal.length() != 0) {
1583 try {
1584 n = Integer.parseInt(propVal);
1585 } catch (NumberFormatException e) {}
1586 }
1587 SystemProperties.set("net.dnschange", "" + (n+1));
Robert Greenwalt03595d02010-11-02 14:08:23 -07001588 /*
1589 * Tell the VMs to toss their DNS caches
1590 */
1591 Intent intent = new Intent(Intent.ACTION_CLEAR_DNS_CACHE);
1592 intent.addFlags(Intent.FLAG_RECEIVER_REPLACE_PENDING);
1593 mContext.sendBroadcast(intent);
Robert Greenwalt42acef32009-08-12 16:08:25 -07001594 }
1595
Robert Greenwaltc76b8fa2010-07-23 15:46:26 -07001596 private void handleDnsConfigurationChange(int netType) {
Robert Greenwalt42acef32009-08-12 16:08:25 -07001597 // add default net's dns entries
Robert Greenwaltc76b8fa2010-07-23 15:46:26 -07001598 NetworkStateTracker nt = mNetTrackers[netType];
1599 if (nt != null && nt.getNetworkInfo().isConnected() && !nt.isTeardownRequested()) {
Robert Greenwalt37e65eb2010-08-30 10:56:47 -07001600 LinkProperties p = nt.getLinkProperties();
Robert Greenwaltc76b8fa2010-07-23 15:46:26 -07001601 if (p == null) return;
1602 Collection<InetAddress> dnses = p.getDnses();
1603 if (mNetAttributes[netType].isDefault()) {
1604 int j = 1;
Robert Greenwalte90aa5e2010-09-01 11:34:05 -07001605 if (dnses.size() == 0 && mDefaultDns != null) {
Robert Greenwaltc76b8fa2010-07-23 15:46:26 -07001606 if (DBG) {
Wink Savilleed9c02b2010-12-03 12:01:38 -08001607 log("no dns provided - using " + mDefaultDns.getHostAddress());
Robert Greenwalt42acef32009-08-12 16:08:25 -07001608 }
Robert Greenwalte90aa5e2010-09-01 11:34:05 -07001609 SystemProperties.set("net.dns1", mDefaultDns.getHostAddress());
1610 j++;
1611 } else {
1612 for (InetAddress dns : dnses) {
1613 if (DBG) {
Wink Savilleed9c02b2010-12-03 12:01:38 -08001614 log("adding dns " + dns + " for " +
Robert Greenwalte90aa5e2010-09-01 11:34:05 -07001615 nt.getNetworkInfo().getTypeName());
1616 }
1617 SystemProperties.set("net.dns" + j++, dns.getHostAddress());
1618 }
Robert Greenwaltc76b8fa2010-07-23 15:46:26 -07001619 }
1620 for (int k=j ; k<mNumDnsEntries; k++) {
Wink Savilleed9c02b2010-12-03 12:01:38 -08001621 if (DBG) log("erasing net.dns" + k);
Robert Greenwaltc76b8fa2010-07-23 15:46:26 -07001622 SystemProperties.set("net.dns" + k, "");
1623 }
1624 mNumDnsEntries = j;
1625 } else {
1626 // set per-pid dns for attached secondary nets
1627 List pids = mNetRequestersPids[netType];
1628 for (int y=0; y< pids.size(); y++) {
1629 Integer pid = (Integer)pids.get(y);
1630 writePidDns(dnses, pid.intValue());
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001631 }
1632 }
Robert Greenwaltc76b8fa2010-07-23 15:46:26 -07001633 bumpDns();
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001634 }
Robert Greenwalt42acef32009-08-12 16:08:25 -07001635 }
1636
1637 private int getRestoreDefaultNetworkDelay() {
1638 String restoreDefaultNetworkDelayStr = SystemProperties.get(
1639 NETWORK_RESTORE_DELAY_PROP_NAME);
1640 if(restoreDefaultNetworkDelayStr != null &&
1641 restoreDefaultNetworkDelayStr.length() != 0) {
1642 try {
1643 return Integer.valueOf(restoreDefaultNetworkDelayStr);
1644 } catch (NumberFormatException e) {
1645 }
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001646 }
Robert Greenwalt42acef32009-08-12 16:08:25 -07001647 return RESTORE_DEFAULT_NETWORK_DELAY;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001648 }
1649
1650 @Override
1651 protected void dump(FileDescriptor fd, PrintWriter pw, String[] args) {
Robert Greenwalt86e9e552009-07-16 17:21:39 -07001652 if (mContext.checkCallingOrSelfPermission(
1653 android.Manifest.permission.DUMP)
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001654 != PackageManager.PERMISSION_GRANTED) {
Robert Greenwalt86e9e552009-07-16 17:21:39 -07001655 pw.println("Permission Denial: can't dump ConnectivityService " +
1656 "from from pid=" + Binder.getCallingPid() + ", uid=" +
1657 Binder.getCallingUid());
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001658 return;
1659 }
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001660 pw.println();
1661 for (NetworkStateTracker nst : mNetTrackers) {
Robert Greenwaltb9285352009-12-21 18:24:07 -08001662 if (nst != null) {
1663 if (nst.getNetworkInfo().isConnected()) {
1664 pw.println("Active network: " + nst.getNetworkInfo().
1665 getTypeName());
1666 }
1667 pw.println(nst.getNetworkInfo());
1668 pw.println(nst);
1669 pw.println();
Robert Greenwalt42acef32009-08-12 16:08:25 -07001670 }
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001671 }
Robert Greenwaltb9285352009-12-21 18:24:07 -08001672
1673 pw.println("Network Requester Pids:");
1674 for (int net : mPriorityList) {
1675 String pidString = net + ": ";
1676 for (Object pid : mNetRequestersPids[net]) {
1677 pidString = pidString + pid.toString() + ", ";
1678 }
1679 pw.println(pidString);
1680 }
1681 pw.println();
1682
1683 pw.println("FeatureUsers:");
1684 for (Object requester : mFeatureUsers) {
1685 pw.println(requester.toString());
1686 }
1687 pw.println();
Robert Greenwalt2a091d72010-02-11 18:18:40 -08001688
Robert Greenwalt14f2ef42010-06-15 12:19:37 -07001689 synchronized (this) {
1690 pw.println("NetworkTranstionWakeLock is currently " +
1691 (mNetTransitionWakeLock.isHeld() ? "" : "not ") + "held.");
1692 pw.println("It was last requested for "+mNetTransitionWakeLockCausedBy);
1693 }
1694 pw.println();
1695
Robert Greenwalt2a091d72010-02-11 18:18:40 -08001696 mTethering.dump(fd, pw, args);
Robert Greenwalt4e8dfef2010-09-20 14:35:25 -07001697
1698 if (mInetLog != null) {
1699 pw.println();
1700 pw.println("Inet condition reports:");
1701 for(int i = 0; i < mInetLog.size(); i++) {
1702 pw.println(mInetLog.get(i));
1703 }
1704 }
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001705 }
1706
Robert Greenwalt42acef32009-08-12 16:08:25 -07001707 // must be stateless - things change under us.
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001708 private class MyHandler extends Handler {
Wink Savillebb08caf2010-09-02 19:23:52 -07001709 public MyHandler(Looper looper) {
1710 super(looper);
1711 }
1712
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001713 @Override
1714 public void handleMessage(Message msg) {
1715 NetworkInfo info;
1716 switch (msg.what) {
1717 case NetworkStateTracker.EVENT_STATE_CHANGED:
1718 info = (NetworkInfo) msg.obj;
Robert Greenwalt511288a2009-12-07 11:33:18 -08001719 int type = info.getType();
1720 NetworkInfo.State state = info.getState();
Robert Greenwalt6e6dec22010-01-25 16:14:00 -08001721 // only do this optimization for wifi. It going into scan mode for location
1722 // services generates alot of noise. Meanwhile the mms apn won't send out
1723 // subsequent notifications when on default cellular because it never
1724 // disconnects.. so only do this to wifi notifications. Fixed better when the
1725 // APN notifications are standardized.
1726 if (mNetAttributes[type].mLastState == state &&
1727 mNetAttributes[type].mRadio == ConnectivityManager.TYPE_WIFI) {
Robert Greenwalt511288a2009-12-07 11:33:18 -08001728 if (DBG) {
Robert Greenwalt6e6dec22010-01-25 16:14:00 -08001729 // TODO - remove this after we validate the dropping doesn't break
1730 // anything
Wink Savilleed9c02b2010-12-03 12:01:38 -08001731 log("Dropping ConnectivityChange for " +
Robert Greenwalt1193ae42010-01-13 09:36:31 -08001732 info.getTypeName() + ": " +
Robert Greenwalt511288a2009-12-07 11:33:18 -08001733 state + "/" + info.getDetailedState());
1734 }
1735 return;
1736 }
1737 mNetAttributes[type].mLastState = state;
1738
Wink Savilleed9c02b2010-12-03 12:01:38 -08001739 if (DBG) log("ConnectivityChange for " +
Robert Greenwalt86e9e552009-07-16 17:21:39 -07001740 info.getTypeName() + ": " +
Robert Greenwalt511288a2009-12-07 11:33:18 -08001741 state + "/" + info.getDetailedState());
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001742
1743 // Connectivity state changed:
1744 // [31-13] Reserved for future use
Robert Greenwalt86e9e552009-07-16 17:21:39 -07001745 // [12-9] Network subtype (for mobile network, as defined
1746 // by TelephonyManager)
1747 // [8-3] Detailed state ordinal (as defined by
1748 // NetworkInfo.DetailedState)
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001749 // [2-0] Network type (as defined by ConnectivityManager)
1750 int eventLogParam = (info.getType() & 0x7) |
1751 ((info.getDetailedState().ordinal() & 0x3f) << 3) |
1752 (info.getSubtype() << 9);
Doug Zongkerab5c49c2009-12-04 10:31:43 -08001753 EventLog.writeEvent(EventLogTags.CONNECTIVITY_STATE_CHANGED,
Robert Greenwalt86e9e552009-07-16 17:21:39 -07001754 eventLogParam);
1755
1756 if (info.getDetailedState() ==
1757 NetworkInfo.DetailedState.FAILED) {
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001758 handleConnectionFailure(info);
Robert Greenwalt511288a2009-12-07 11:33:18 -08001759 } else if (state == NetworkInfo.State.DISCONNECTED) {
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001760 handleDisconnect(info);
Robert Greenwalt511288a2009-12-07 11:33:18 -08001761 } else if (state == NetworkInfo.State.SUSPENDED) {
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001762 // TODO: need to think this over.
Robert Greenwalt86e9e552009-07-16 17:21:39 -07001763 // the logic here is, handle SUSPENDED the same as
1764 // DISCONNECTED. The only difference being we are
1765 // broadcasting an intent with NetworkInfo that's
1766 // suspended. This allows the applications an
1767 // opportunity to handle DISCONNECTED and SUSPENDED
1768 // differently, or not.
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001769 handleDisconnect(info);
Robert Greenwalt511288a2009-12-07 11:33:18 -08001770 } else if (state == NetworkInfo.State.CONNECTED) {
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001771 handleConnect(info);
1772 }
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001773 break;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001774 case NetworkStateTracker.EVENT_CONFIGURATION_CHANGED:
Robert Greenwaltc76b8fa2010-07-23 15:46:26 -07001775 info = (NetworkInfo) msg.obj;
1776 type = info.getType();
Robert Greenwalt434203a2010-10-11 16:00:27 -07001777 handleConnectivityChange(type);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001778 break;
Robert Greenwaltf3331232010-09-24 14:32:21 -07001779 case EVENT_CLEAR_NET_TRANSITION_WAKELOCK:
Robert Greenwalt14f2ef42010-06-15 12:19:37 -07001780 String causedBy = null;
1781 synchronized (ConnectivityService.this) {
1782 if (msg.arg1 == mNetTransitionWakeLockSerialNumber &&
1783 mNetTransitionWakeLock.isHeld()) {
1784 mNetTransitionWakeLock.release();
1785 causedBy = mNetTransitionWakeLockCausedBy;
1786 }
1787 }
1788 if (causedBy != null) {
Wink Savilleed9c02b2010-12-03 12:01:38 -08001789 log("NetTransition Wakelock for " + causedBy + " released by timeout");
Robert Greenwalt14f2ef42010-06-15 12:19:37 -07001790 }
Robert Greenwalt057d5e92010-09-09 14:05:10 -07001791 break;
Robert Greenwalt8dcc28b2010-09-23 10:05:56 -07001792 case EVENT_RESTORE_DEFAULT_NETWORK:
Robert Greenwalt9c75d4a2009-09-27 17:27:04 -07001793 FeatureUser u = (FeatureUser)msg.obj;
1794 u.expire();
Robert Greenwaltd7085fc2010-09-08 15:24:47 -07001795 break;
Robert Greenwalt8dcc28b2010-09-23 10:05:56 -07001796 case EVENT_INET_CONDITION_CHANGE:
1797 {
1798 int netType = msg.arg1;
1799 int condition = msg.arg2;
1800 handleInetConditionChange(netType, condition);
Robert Greenwalt14f2ef42010-06-15 12:19:37 -07001801 break;
Robert Greenwalt8dcc28b2010-09-23 10:05:56 -07001802 }
1803 case EVENT_INET_CONDITION_HOLD_END:
1804 {
1805 int netType = msg.arg1;
1806 int sequence = msg.arg2;
1807 handleInetConditionHoldEnd(netType, sequence);
Robert Greenwaltd7085fc2010-09-08 15:24:47 -07001808 break;
Robert Greenwalt8dcc28b2010-09-23 10:05:56 -07001809 }
1810 case EVENT_SET_NETWORK_PREFERENCE:
1811 {
1812 int preference = msg.arg1;
1813 handleSetNetworkPreference(preference);
1814 break;
1815 }
1816 case EVENT_SET_BACKGROUND_DATA:
1817 {
1818 boolean enabled = (msg.arg1 == ENABLED);
1819 handleSetBackgroundData(enabled);
1820 break;
1821 }
1822 case EVENT_SET_MOBILE_DATA:
1823 {
1824 boolean enabled = (msg.arg1 == ENABLED);
1825 handleSetMobileData(enabled);
1826 break;
1827 }
Robert Greenwalt434203a2010-10-11 16:00:27 -07001828 case EVENT_APPLY_GLOBAL_HTTP_PROXY:
1829 {
1830 handleDeprecatedGlobalHttpProxy();
1831 }
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001832 }
1833 }
1834 }
Robert Greenwaltd0e18ff2010-01-26 11:40:34 -08001835
1836 // javadoc from interface
Robert Greenwalt5a735062010-03-02 17:25:02 -08001837 public int tether(String iface) {
Robert Greenwaltd0e18ff2010-01-26 11:40:34 -08001838 enforceTetherChangePermission();
Robert Greenwalt5a735062010-03-02 17:25:02 -08001839
1840 if (isTetheringSupported()) {
1841 return mTethering.tether(iface);
1842 } else {
1843 return ConnectivityManager.TETHER_ERROR_UNSUPPORTED;
1844 }
Robert Greenwaltd0e18ff2010-01-26 11:40:34 -08001845 }
1846
1847 // javadoc from interface
Robert Greenwalt5a735062010-03-02 17:25:02 -08001848 public int untether(String iface) {
Robert Greenwaltd0e18ff2010-01-26 11:40:34 -08001849 enforceTetherChangePermission();
Robert Greenwalt5a735062010-03-02 17:25:02 -08001850
1851 if (isTetheringSupported()) {
1852 return mTethering.untether(iface);
1853 } else {
1854 return ConnectivityManager.TETHER_ERROR_UNSUPPORTED;
1855 }
1856 }
1857
1858 // javadoc from interface
1859 public int getLastTetherError(String iface) {
1860 enforceTetherAccessPermission();
1861
1862 if (isTetheringSupported()) {
1863 return mTethering.getLastTetherError(iface);
1864 } else {
1865 return ConnectivityManager.TETHER_ERROR_UNSUPPORTED;
1866 }
Robert Greenwalt2a091d72010-02-11 18:18:40 -08001867 }
1868
1869 // TODO - proper iface API for selection by property, inspection, etc
1870 public String[] getTetherableUsbRegexs() {
1871 enforceTetherAccessPermission();
1872 if (isTetheringSupported()) {
1873 return mTethering.getTetherableUsbRegexs();
1874 } else {
1875 return new String[0];
1876 }
1877 }
1878
1879 public String[] getTetherableWifiRegexs() {
1880 enforceTetherAccessPermission();
1881 if (isTetheringSupported()) {
1882 return mTethering.getTetherableWifiRegexs();
1883 } else {
1884 return new String[0];
1885 }
Robert Greenwaltd0e18ff2010-01-26 11:40:34 -08001886 }
1887
Danica Chang6fdd0c62010-08-11 14:54:43 -07001888 public String[] getTetherableBluetoothRegexs() {
1889 enforceTetherAccessPermission();
1890 if (isTetheringSupported()) {
1891 return mTethering.getTetherableBluetoothRegexs();
1892 } else {
1893 return new String[0];
1894 }
1895 }
1896
Robert Greenwaltd0e18ff2010-01-26 11:40:34 -08001897 // TODO - move iface listing, queries, etc to new module
1898 // javadoc from interface
1899 public String[] getTetherableIfaces() {
Robert Greenwalt2a091d72010-02-11 18:18:40 -08001900 enforceTetherAccessPermission();
Robert Greenwaltd0e18ff2010-01-26 11:40:34 -08001901 return mTethering.getTetherableIfaces();
1902 }
1903
1904 public String[] getTetheredIfaces() {
Robert Greenwalt2a091d72010-02-11 18:18:40 -08001905 enforceTetherAccessPermission();
Robert Greenwaltd0e18ff2010-01-26 11:40:34 -08001906 return mTethering.getTetheredIfaces();
1907 }
Robert Greenwalt2a091d72010-02-11 18:18:40 -08001908
Robert Greenwalt5a735062010-03-02 17:25:02 -08001909 public String[] getTetheringErroredIfaces() {
1910 enforceTetherAccessPermission();
1911 return mTethering.getErroredIfaces();
1912 }
1913
Robert Greenwalt2a091d72010-02-11 18:18:40 -08001914 // if ro.tether.denied = true we default to no tethering
1915 // gservices could set the secure setting to 1 though to enable it on a build where it
1916 // had previously been turned off.
1917 public boolean isTetheringSupported() {
1918 enforceTetherAccessPermission();
1919 int defaultVal = (SystemProperties.get("ro.tether.denied").equals("true") ? 0 : 1);
Robert Greenwaltc9d5fb72010-02-25 12:29:30 -08001920 boolean tetherEnabledInSettings = (Settings.Secure.getInt(mContext.getContentResolver(),
1921 Settings.Secure.TETHER_SUPPORTED, defaultVal) != 0);
1922 return tetherEnabledInSettings && mTetheringConfigValid;
Robert Greenwalt2a091d72010-02-11 18:18:40 -08001923 }
Robert Greenwalt14f2ef42010-06-15 12:19:37 -07001924
1925 // An API NetworkStateTrackers can call when they lose their network.
1926 // This will automatically be cleared after X seconds or a network becomes CONNECTED,
1927 // whichever happens first. The timer is started by the first caller and not
1928 // restarted by subsequent callers.
1929 public void requestNetworkTransitionWakelock(String forWhom) {
1930 enforceConnectivityInternalPermission();
1931 synchronized (this) {
1932 if (mNetTransitionWakeLock.isHeld()) return;
1933 mNetTransitionWakeLockSerialNumber++;
1934 mNetTransitionWakeLock.acquire();
1935 mNetTransitionWakeLockCausedBy = forWhom;
1936 }
1937 mHandler.sendMessageDelayed(mHandler.obtainMessage(
Robert Greenwaltf3331232010-09-24 14:32:21 -07001938 EVENT_CLEAR_NET_TRANSITION_WAKELOCK,
Robert Greenwalt14f2ef42010-06-15 12:19:37 -07001939 mNetTransitionWakeLockSerialNumber, 0),
1940 mNetTransitionWakeLockTimeout);
1941 return;
1942 }
Robert Greenwaltca4306c2010-09-09 13:15:32 -07001943
Robert Greenwaltd7085fc2010-09-08 15:24:47 -07001944 // 100 percent is full good, 0 is full bad.
1945 public void reportInetCondition(int networkType, int percentage) {
Wink Savilleed9c02b2010-12-03 12:01:38 -08001946 if (DBG) log("reportNetworkCondition(" + networkType + ", " + percentage + ")");
Robert Greenwaltd7085fc2010-09-08 15:24:47 -07001947 mContext.enforceCallingOrSelfPermission(
1948 android.Manifest.permission.STATUS_BAR,
1949 "ConnectivityService");
1950
Robert Greenwalt4e8dfef2010-09-20 14:35:25 -07001951 if (DBG) {
1952 int pid = getCallingPid();
1953 int uid = getCallingUid();
1954 String s = pid + "(" + uid + ") reports inet is " +
1955 (percentage > 50 ? "connected" : "disconnected") + " (" + percentage + ") on " +
1956 "network Type " + networkType + " at " + GregorianCalendar.getInstance().getTime();
1957 mInetLog.add(s);
1958 while(mInetLog.size() > INET_CONDITION_LOG_MAX_SIZE) {
1959 mInetLog.remove(0);
1960 }
1961 }
Robert Greenwaltd7085fc2010-09-08 15:24:47 -07001962 mHandler.sendMessage(mHandler.obtainMessage(
Robert Greenwalt8dcc28b2010-09-23 10:05:56 -07001963 EVENT_INET_CONDITION_CHANGE, networkType, percentage));
1964 }
1965
1966 private void handleInetConditionChange(int netType, int condition) {
1967 if (DBG) {
Wink Savilleed9c02b2010-12-03 12:01:38 -08001968 log("Inet connectivity change, net=" +
Robert Greenwalt8dcc28b2010-09-23 10:05:56 -07001969 netType + ", condition=" + condition +
1970 ",mActiveDefaultNetwork=" + mActiveDefaultNetwork);
1971 }
1972 if (mActiveDefaultNetwork == -1) {
Wink Savilleed9c02b2010-12-03 12:01:38 -08001973 if (DBG) log("no active default network - aborting");
Robert Greenwalt8dcc28b2010-09-23 10:05:56 -07001974 return;
1975 }
1976 if (mActiveDefaultNetwork != netType) {
Wink Savilleed9c02b2010-12-03 12:01:38 -08001977 if (DBG) log("given net not default - aborting");
Robert Greenwalt8dcc28b2010-09-23 10:05:56 -07001978 return;
1979 }
1980 mDefaultInetCondition = condition;
1981 int delay;
1982 if (mInetConditionChangeInFlight == false) {
Wink Savilleed9c02b2010-12-03 12:01:38 -08001983 if (DBG) log("starting a change hold");
Robert Greenwalt8dcc28b2010-09-23 10:05:56 -07001984 // setup a new hold to debounce this
1985 if (mDefaultInetCondition > 50) {
1986 delay = Settings.Secure.getInt(mContext.getContentResolver(),
1987 Settings.Secure.INET_CONDITION_DEBOUNCE_UP_DELAY, 500);
1988 } else {
1989 delay = Settings.Secure.getInt(mContext.getContentResolver(),
1990 Settings.Secure.INET_CONDITION_DEBOUNCE_DOWN_DELAY, 3000);
1991 }
1992 mInetConditionChangeInFlight = true;
1993 mHandler.sendMessageDelayed(mHandler.obtainMessage(EVENT_INET_CONDITION_HOLD_END,
1994 mActiveDefaultNetwork, mDefaultConnectionSequence), delay);
1995 } else {
1996 // we've set the new condition, when this hold ends that will get
1997 // picked up
Wink Savilleed9c02b2010-12-03 12:01:38 -08001998 if (DBG) log("currently in hold - not setting new end evt");
Robert Greenwalt8dcc28b2010-09-23 10:05:56 -07001999 }
2000 }
2001
2002 private void handleInetConditionHoldEnd(int netType, int sequence) {
2003 if (DBG) {
Wink Savilleed9c02b2010-12-03 12:01:38 -08002004 log("Inet hold end, net=" + netType +
Robert Greenwalt8dcc28b2010-09-23 10:05:56 -07002005 ", condition =" + mDefaultInetCondition +
2006 ", published condition =" + mDefaultInetConditionPublished);
2007 }
2008 mInetConditionChangeInFlight = false;
2009
2010 if (mActiveDefaultNetwork == -1) {
Wink Savilleed9c02b2010-12-03 12:01:38 -08002011 if (DBG) log("no active default network - aborting");
Robert Greenwalt8dcc28b2010-09-23 10:05:56 -07002012 return;
2013 }
2014 if (mDefaultConnectionSequence != sequence) {
Wink Savilleed9c02b2010-12-03 12:01:38 -08002015 if (DBG) log("event hold for obsolete network - aborting");
Robert Greenwalt8dcc28b2010-09-23 10:05:56 -07002016 return;
2017 }
2018 if (mDefaultInetConditionPublished == mDefaultInetCondition) {
Wink Savilleed9c02b2010-12-03 12:01:38 -08002019 if (DBG) log("no change in condition - aborting");
Robert Greenwalt8dcc28b2010-09-23 10:05:56 -07002020 return;
2021 }
2022 NetworkInfo networkInfo = mNetTrackers[mActiveDefaultNetwork].getNetworkInfo();
2023 if (networkInfo.isConnected() == false) {
Wink Savilleed9c02b2010-12-03 12:01:38 -08002024 if (DBG) log("default network not connected - aborting");
Robert Greenwalt8dcc28b2010-09-23 10:05:56 -07002025 return;
2026 }
2027 mDefaultInetConditionPublished = mDefaultInetCondition;
2028 sendInetConditionBroadcast(networkInfo);
2029 return;
Robert Greenwaltd7085fc2010-09-08 15:24:47 -07002030 }
Robert Greenwalt434203a2010-10-11 16:00:27 -07002031
2032 public synchronized ProxyProperties getProxy() {
2033 if (mGlobalProxy != null) return mGlobalProxy;
2034 if (mDefaultProxy != null) return mDefaultProxy;
2035 return null;
2036 }
2037
2038 public void setGlobalProxy(ProxyProperties proxyProperties) {
2039 enforceChangePermission();
2040 synchronized (mGlobalProxyLock) {
2041 if (proxyProperties == mGlobalProxy) return;
2042 if (proxyProperties != null && proxyProperties.equals(mGlobalProxy)) return;
2043 if (mGlobalProxy != null && mGlobalProxy.equals(proxyProperties)) return;
2044
2045 String host = "";
2046 int port = 0;
2047 String exclList = "";
2048 if (proxyProperties != null && !TextUtils.isEmpty(proxyProperties.getHost())) {
2049 mGlobalProxy = new ProxyProperties(proxyProperties);
2050 host = mGlobalProxy.getHost();
2051 port = mGlobalProxy.getPort();
2052 exclList = mGlobalProxy.getExclusionList();
2053 } else {
2054 mGlobalProxy = null;
2055 }
2056 ContentResolver res = mContext.getContentResolver();
2057 Settings.Secure.putString(res, Settings.Secure.GLOBAL_HTTP_PROXY_HOST, host);
2058 Settings.Secure.putInt(res, Settings.Secure.GLOBAL_HTTP_PROXY_PORT, port);
Robert Greenwaltb7090d62010-12-02 11:31:00 -08002059 Settings.Secure.putString(res, Settings.Secure.GLOBAL_HTTP_PROXY_EXCLUSION_LIST,
Robert Greenwalt434203a2010-10-11 16:00:27 -07002060 exclList);
2061 }
2062
2063 if (mGlobalProxy == null) {
2064 proxyProperties = mDefaultProxy;
2065 }
2066 sendProxyBroadcast(proxyProperties);
2067 }
2068
Robert Greenwaltb7090d62010-12-02 11:31:00 -08002069 private void loadGlobalProxy() {
2070 ContentResolver res = mContext.getContentResolver();
2071 String host = Settings.Secure.getString(res, Settings.Secure.GLOBAL_HTTP_PROXY_HOST);
2072 int port = Settings.Secure.getInt(res, Settings.Secure.GLOBAL_HTTP_PROXY_PORT, 0);
2073 String exclList = Settings.Secure.getString(res,
2074 Settings.Secure.GLOBAL_HTTP_PROXY_EXCLUSION_LIST);
2075 if (!TextUtils.isEmpty(host)) {
2076 ProxyProperties proxyProperties = new ProxyProperties(host, port, exclList);
2077 synchronized (mGlobalProxyLock) {
2078 mGlobalProxy = proxyProperties;
2079 }
2080 }
2081 }
2082
Robert Greenwalt434203a2010-10-11 16:00:27 -07002083 public ProxyProperties getGlobalProxy() {
2084 synchronized (mGlobalProxyLock) {
2085 return mGlobalProxy;
2086 }
2087 }
2088
2089 private void handleApplyDefaultProxy(int type) {
2090 // check if new default - push it out to all VM if so
2091 ProxyProperties proxy = mNetTrackers[type].getLinkProperties().getHttpProxy();
2092 synchronized (this) {
2093 if (mDefaultProxy != null && mDefaultProxy.equals(proxy)) return;
2094 if (mDefaultProxy == proxy) return;
2095 if (!TextUtils.isEmpty(proxy.getHost())) {
2096 mDefaultProxy = proxy;
2097 } else {
2098 mDefaultProxy = null;
2099 }
2100 }
Wink Savilleed9c02b2010-12-03 12:01:38 -08002101 if (DBG) log("changing default proxy to " + proxy);
Robert Greenwalt434203a2010-10-11 16:00:27 -07002102 if ((proxy == null && mGlobalProxy == null) || proxy.equals(mGlobalProxy)) return;
2103 if (mGlobalProxy != null) return;
2104 sendProxyBroadcast(proxy);
2105 }
2106
2107 private void handleDeprecatedGlobalHttpProxy() {
2108 String proxy = Settings.Secure.getString(mContext.getContentResolver(),
2109 Settings.Secure.HTTP_PROXY);
2110 if (!TextUtils.isEmpty(proxy)) {
2111 String data[] = proxy.split(":");
2112 String proxyHost = data[0];
2113 int proxyPort = 8080;
2114 if (data.length > 1) {
2115 try {
2116 proxyPort = Integer.parseInt(data[1]);
2117 } catch (NumberFormatException e) {
2118 return;
2119 }
2120 }
2121 ProxyProperties p = new ProxyProperties(data[0], proxyPort, "");
2122 setGlobalProxy(p);
2123 }
2124 }
2125
2126 private void sendProxyBroadcast(ProxyProperties proxy) {
Wink Savilleed9c02b2010-12-03 12:01:38 -08002127 log("sending Proxy Broadcast for " + proxy);
Robert Greenwalt434203a2010-10-11 16:00:27 -07002128 Intent intent = new Intent(Proxy.PROXY_CHANGE_ACTION);
2129 intent.addFlags(Intent.FLAG_RECEIVER_REPLACE_PENDING);
2130 intent.putExtra(Proxy.EXTRA_PROXY_INFO, proxy);
Robert Greenwalta2e13392010-12-06 11:29:17 -08002131 mContext.sendStickyBroadcast(intent);
Robert Greenwalt434203a2010-10-11 16:00:27 -07002132 }
2133
2134 private static class SettingsObserver extends ContentObserver {
2135 private int mWhat;
2136 private Handler mHandler;
2137 SettingsObserver(Handler handler, int what) {
2138 super(handler);
2139 mHandler = handler;
2140 mWhat = what;
2141 }
2142
2143 void observe(Context context) {
2144 ContentResolver resolver = context.getContentResolver();
2145 resolver.registerContentObserver(Settings.Secure.getUriFor(
2146 Settings.Secure.HTTP_PROXY), false, this);
2147 }
2148
2149 @Override
2150 public void onChange(boolean selfChange) {
2151 mHandler.obtainMessage(mWhat).sendToTarget();
2152 }
2153 }
Wink Savilleed9c02b2010-12-03 12:01:38 -08002154
2155 private void log(String s) {
2156 Slog.d(TAG, s);
2157 }
2158
2159 private void loge(String s) {
2160 Slog.e(TAG, s);
2161 }
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002162}