blob: f82a243a6bcc1d713a6e909d5e72e3fd87abb8d0 [file] [log] [blame]
The Android Open Source Project28527d22009-03-03 19:31:44 -08001/*
2 * Copyright (C) 2008 The Android Open Source Project
3 *
4 * Licensed under the Apache License, Version 2.0 (the "License");
5 * you may not use this file except in compliance with the License.
6 * You may obtain a copy of the License at
7 *
8 * http://www.apache.org/licenses/LICENSE-2.0
9 *
10 * Unless required by applicable law or agreed to in writing, software
11 * distributed under the License is distributed on an "AS IS" BASIS,
12 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13 * See the License for the specific language governing permissions and
14 * limitations under the License.
15 */
16
17package com.android.server;
18
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 Greenwaltc3c5f862010-10-11 16:00:27 -070025import android.database.ContentObserver;
The Android Open Source Project28527d22009-03-03 19:31:44 -080026import android.net.ConnectivityManager;
Robert Greenwalteb123ac2010-12-06 13:56:24 -080027import android.net.DummyDataStateTracker;
The Android Open Source Project28527d22009-03-03 19:31:44 -080028import android.net.IConnectivityManager;
29import android.net.MobileDataStateTracker;
30import android.net.NetworkInfo;
Robert Greenwalt1f1bcfe2010-08-30 10:56:47 -070031import android.net.LinkProperties;
The Android Open Source Project28527d22009-03-03 19:31:44 -080032import android.net.NetworkStateTracker;
Robert Greenwalt7fe44cb2010-08-27 09:24:29 -070033import android.net.NetworkUtils;
Robert Greenwaltc3c5f862010-10-11 16:00:27 -070034import android.net.Proxy;
35import android.net.ProxyProperties;
The Android Open Source Project28527d22009-03-03 19:31:44 -080036import android.net.wifi.WifiStateTracker;
37import android.os.Binder;
38import android.os.Handler;
Wink Saville775aad62010-09-02 19:23:52 -070039import android.os.HandlerThread;
Robert Greenwalt2034b912009-08-12 16:08:25 -070040import android.os.IBinder;
The Android Open Source Project28527d22009-03-03 19:31:44 -080041import android.os.Looper;
42import android.os.Message;
Robert Greenwalt93dc1042010-06-15 12:19:37 -070043import android.os.PowerManager;
Robert Greenwalt2034b912009-08-12 16:08:25 -070044import android.os.RemoteException;
The Android Open Source Project28527d22009-03-03 19:31:44 -080045import android.os.ServiceManager;
46import android.os.SystemProperties;
47import android.provider.Settings;
Robert Greenwalt2034b912009-08-12 16:08:25 -070048import android.text.TextUtils;
The Android Open Source Project28527d22009-03-03 19:31:44 -080049import android.util.EventLog;
Joe Onoratoc2386bb2010-02-26 18:56:32 -080050import android.util.Slog;
The Android Open Source Project28527d22009-03-03 19:31:44 -080051
Robert Greenwalt2034b912009-08-12 16:08:25 -070052import com.android.internal.telephony.Phone;
53
Robert Greenwalt0c4828c2010-01-26 11:40:34 -080054import com.android.server.connectivity.Tethering;
55
The Android Open Source Project28527d22009-03-03 19:31:44 -080056import java.io.FileDescriptor;
Irfan Sheriff7f132d92010-06-09 15:39:36 -070057import java.io.FileWriter;
58import java.io.IOException;
The Android Open Source Project28527d22009-03-03 19:31:44 -080059import java.io.PrintWriter;
Robert Greenwalta7dfbd32010-06-15 15:43:39 -070060import java.net.InetAddress;
Robert Greenwaltc3c5f862010-10-11 16:00:27 -070061import java.net.InetSocketAddress;
Robert Greenwalta7dfbd32010-06-15 15:43:39 -070062import java.net.UnknownHostException;
Robert Greenwalt2034b912009-08-12 16:08:25 -070063import java.util.ArrayList;
Robert Greenwalta7dfbd32010-06-15 15:43:39 -070064import java.util.Collection;
Robert Greenwalt0e80be12010-09-20 14:35:25 -070065import java.util.GregorianCalendar;
Robert Greenwalt2034b912009-08-12 16:08:25 -070066import java.util.List;
The Android Open Source Project28527d22009-03-03 19:31:44 -080067
68/**
69 * @hide
70 */
71public class ConnectivityService extends IConnectivityManager.Stub {
72
Robert Greenwalt063dc7d2010-10-05 19:12:26 -070073 private static final boolean DBG = true;
The Android Open Source Project28527d22009-03-03 19:31:44 -080074 private static final String TAG = "ConnectivityService";
75
Robert Greenwalt2034b912009-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 Greenwalt0c4828c2010-01-26 11:40:34 -080082 private Tethering mTethering;
Robert Greenwaltf1b66e12010-02-25 12:29:30 -080083 private boolean mTetheringConfigValid = false;
Robert Greenwalt0c4828c2010-01-26 11:40:34 -080084
The Android Open Source Project28527d22009-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 Greenwalt2034b912009-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 Sheriff653e2a22010-06-07 09:03:04 -070098 private WifiWatchdogService mWifiWatchdogService;
99
Robert Greenwalt2034b912009-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 Project28527d22009-03-03 19:31:44 -0800105 private Context mContext;
106 private int mNetworkPreference;
Robert Greenwalt2034b912009-08-12 16:08:25 -0700107 private int mActiveDefaultNetwork = -1;
Robert Greenwalt986c7412010-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 Project28527d22009-03-03 19:31:44 -0800113
114 private int mNumDnsEntries;
The Android Open Source Project28527d22009-03-03 19:31:44 -0800115
116 private boolean mTestMode;
Joe Onorato56023ad2010-09-01 21:18:22 -0700117 private static ConnectivityService sServiceInstance;
The Android Open Source Project28527d22009-03-03 19:31:44 -0800118
Robert Greenwalt6a2db8a2010-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 Greenwaltccb36f92010-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 Greenwaltc3c5f862010-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 Greenwalt2034b912009-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 Lockwoodfde2b762009-08-14 14:18:49 -0400196 private boolean mSystemReady;
Dianne Hackborna417ff82009-12-08 19:45:14 -0800197 private Intent mInitialBroadcast;
Mike Lockwoodfde2b762009-08-14 14:18:49 -0400198
Robert Greenwalt93dc1042010-06-15 12:19:37 -0700199 private PowerManager.WakeLock mNetTransitionWakeLock;
200 private String mNetTransitionWakeLockCausedBy = "";
201 private int mNetTransitionWakeLockSerialNumber;
202 private int mNetTransitionWakeLockTimeout;
203
Robert Greenwalt94daa182010-09-01 11:34:05 -0700204 private InetAddress mDefaultDns;
205
Robert Greenwalt0e80be12010-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 Greenwaltc3c5f862010-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 Greenwalt12c44552009-12-07 11:33:18 -0800218 private static class NetworkAttributes {
Robert Greenwalt2034b912009-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 Greenwalt12c44552009-12-07 11:33:18 -0800226 public NetworkInfo.State mLastState;
Robert Greenwalt2034b912009-08-12 16:08:25 -0700227 public NetworkAttributes(String init) {
228 String fragments[] = init.split(",");
229 mName = fragments[0].toLowerCase();
Robert Greenwaltec05b3c2009-10-30 14:17:42 -0700230 mType = Integer.parseInt(fragments[1]);
231 mRadio = Integer.parseInt(fragments[2]);
232 mPriority = Integer.parseInt(fragments[3]);
Robert Greenwalt12c44552009-12-07 11:33:18 -0800233 mLastState = NetworkInfo.State.UNKNOWN;
Robert Greenwalt2034b912009-08-12 16:08:25 -0700234 }
235 public boolean isDefault() {
236 return (mType == mRadio);
237 }
238 }
239 NetworkAttributes[] mNetAttributes;
Robert Greenwaltec05b3c2009-10-30 14:17:42 -0700240 int mNetworksDefined;
Robert Greenwalt2034b912009-08-12 16:08:25 -0700241
Robert Greenwalt12c44552009-12-07 11:33:18 -0800242 private static class RadioAttributes {
Robert Greenwalt2034b912009-08-12 16:08:25 -0700243 public int mSimultaneity;
244 public int mType;
245 public RadioAttributes(String init) {
246 String fragments[] = init.split(",");
Robert Greenwaltec05b3c2009-10-30 14:17:42 -0700247 mType = Integer.parseInt(fragments[0]);
248 mSimultaneity = Integer.parseInt(fragments[1]);
Robert Greenwalt2034b912009-08-12 16:08:25 -0700249 }
250 }
251 RadioAttributes[] mRadioAttributes;
252
Wink Saville775aad62010-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 Project28527d22009-03-03 19:31:44 -0800256 }
Wink Saville775aad62010-09-02 19:23:52 -0700257 return sServiceInstance;
The Android Open Source Project28527d22009-03-03 19:31:44 -0800258 }
Robert Greenwalt0659da32009-07-16 17:21:39 -0700259
The Android Open Source Project28527d22009-03-03 19:31:44 -0800260 private ConnectivityService(Context context) {
Wink Savillee70c6f52010-12-03 12:01:38 -0800261 if (DBG) log("ConnectivityService starting up");
Robert Greenwaltd48f8ee2010-01-14 17:47:58 -0800262
Wink Saville775aad62010-09-02 19:23:52 -0700263 HandlerThread handlerThread = new HandlerThread("ConnectivityServiceThread");
264 handlerThread.start();
265 mHandler = new MyHandler(handlerThread.getLooper());
266
Robert Greenwaltd48f8ee2010-01-14 17:47:58 -0800267 // setup our unique device name
Robert Greenwalt82cde132010-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 Greenwaltd48f8ee2010-01-14 17:47:58 -0800275 }
276
Robert Greenwalt94daa182010-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 Savillee70c6f52010-12-03 12:01:38 -0800287 loge("Error setting defaultDns using " + dns);
Robert Greenwalt94daa182010-09-01 11:34:05 -0700288 }
289
The Android Open Source Project28527d22009-03-03 19:31:44 -0800290 mContext = context;
Robert Greenwalt93dc1042010-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 Greenwalt2034b912009-08-12 16:08:25 -0700297 mNetTrackers = new NetworkStateTracker[
298 ConnectivityManager.MAX_NETWORK_TYPE+1];
Robert Greenwalt0659da32009-07-16 17:21:39 -0700299
The Android Open Source Project28527d22009-03-03 19:31:44 -0800300 mNetworkPreference = getPersistedNetworkPreference();
Robert Greenwalt0659da32009-07-16 17:21:39 -0700301
Robert Greenwaltec05b3c2009-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 Greenwalt2034b912009-08-12 16:08:25 -0700305 // Load device network attributes from resources
Robert Greenwalt2034b912009-08-12 16:08:25 -0700306 String[] raStrings = context.getResources().getStringArray(
307 com.android.internal.R.array.radioAttributes);
Robert Greenwaltec05b3c2009-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 Savillee70c6f52010-12-03 12:01:38 -0800311 loge("Error in radioAttributes - ignoring attempt to define type " + r.mType);
Robert Greenwaltec05b3c2009-10-30 14:17:42 -0700312 continue;
313 }
314 if (mRadioAttributes[r.mType] != null) {
Wink Savillee70c6f52010-12-03 12:01:38 -0800315 loge("Error in radioAttributes - ignoring attempt to redefine type " +
Robert Greenwaltec05b3c2009-10-30 14:17:42 -0700316 r.mType);
317 continue;
318 }
Robert Greenwalt2034b912009-08-12 16:08:25 -0700319 mRadioAttributes[r.mType] = r;
320 }
321
Robert Greenwaltec05b3c2009-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 Savillee70c6f52010-12-03 12:01:38 -0800328 loge("Error in networkAttributes - ignoring attempt to define type " +
Robert Greenwaltec05b3c2009-10-30 14:17:42 -0700329 n.mType);
330 continue;
Robert Greenwalt2034b912009-08-12 16:08:25 -0700331 }
Robert Greenwaltec05b3c2009-10-30 14:17:42 -0700332 if (mNetAttributes[n.mType] != null) {
Wink Savillee70c6f52010-12-03 12:01:38 -0800333 loge("Error in networkAttributes - ignoring attempt to redefine type " +
Robert Greenwaltec05b3c2009-10-30 14:17:42 -0700334 n.mType);
335 continue;
336 }
337 if (mRadioAttributes[n.mRadio] == null) {
Wink Savillee70c6f52010-12-03 12:01:38 -0800338 loge("Error in networkAttributes - ignoring attempt to use undefined " +
Robert Greenwaltec05b3c2009-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 Greenwalt2034b912009-08-12 16:08:25 -0700346 }
347 }
348
Robert Greenwaltec05b3c2009-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 Greenwalt2034b912009-08-12 16:08:25 -0700374 mNetRequestersPids[i] = new ArrayList();
375 }
376
377 mFeatureUsers = new ArrayList();
378
Robert Greenwaltec05b3c2009-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 Project28527d22009-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 Greenwaltec05b3c2009-10-30 14:17:42 -0700390 for (int netType : mPriorityList) {
391 switch (mNetAttributes[netType].mRadio) {
392 case ConnectivityManager.TYPE_WIFI:
Wink Savillee70c6f52010-12-03 12:01:38 -0800393 if (DBG) log("Starting Wifi Service.");
Wink Saville7fabfa22010-08-13 16:11:42 -0700394 WifiStateTracker wst = new WifiStateTracker();
Irfan Sheriff25be0762010-07-28 09:35:20 -0700395 WifiService wifiService = new WifiService(context);
Robert Greenwaltec05b3c2009-10-30 14:17:42 -0700396 ServiceManager.addService(Context.WIFI_SERVICE, wifiService);
Irfan Sheriff25be0762010-07-28 09:35:20 -0700397 wifiService.checkAndStartWifi();
Robert Greenwaltec05b3c2009-10-30 14:17:42 -0700398 mNetTrackers[ConnectivityManager.TYPE_WIFI] = wst;
Wink Saville7fabfa22010-08-13 16:11:42 -0700399 wst.startMonitoring(context, mHandler);
The Android Open Source Project28527d22009-03-03 19:31:44 -0800400
Irfan Sheriff653e2a22010-06-07 09:03:04 -0700401 //TODO: as part of WWS refactor, create only when needed
Irfan Sheriff25be0762010-07-28 09:35:20 -0700402 mWifiWatchdogService = new WifiWatchdogService(context);
Irfan Sheriff653e2a22010-06-07 09:03:04 -0700403
Robert Greenwaltec05b3c2009-10-30 14:17:42 -0700404 break;
405 case ConnectivityManager.TYPE_MOBILE:
Wink Saville7fabfa22010-08-13 16:11:42 -0700406 mNetTrackers[netType] = new MobileDataStateTracker(netType,
407 mNetAttributes[netType].mName);
408 mNetTrackers[netType].startMonitoring(context, mHandler);
Robert Greenwaltec05b3c2009-10-30 14:17:42 -0700409 break;
Robert Greenwalteb123ac2010-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 Greenwaltec05b3c2009-10-30 14:17:42 -0700415 default:
Wink Savillee70c6f52010-12-03 12:01:38 -0800416 loge("Trying to create a DataStateTracker for an unknown radio type " +
Robert Greenwaltec05b3c2009-10-30 14:17:42 -0700417 mNetAttributes[netType].mRadio);
418 continue;
419 }
420 }
Robert Greenwalt0c4828c2010-01-26 11:40:34 -0800421
Robert Greenwaltc0b6c602010-03-11 15:03:08 -0800422 mTethering = new Tethering(mContext, mHandler.getLooper());
Robert Greenwaltf1b66e12010-02-25 12:29:30 -0800423 mTetheringConfigValid = (((mNetTrackers[ConnectivityManager.TYPE_MOBILE_DUN] != null) ||
424 !mTethering.isDunRequired()) &&
425 (mTethering.getTetherableUsbRegexs().length != 0 ||
Danica Chang96567052010-08-11 14:54:43 -0700426 mTethering.getTetherableWifiRegexs().length != 0 ||
427 mTethering.getTetherableBluetoothRegexs().length != 0) &&
Robert Greenwaltf1b66e12010-02-25 12:29:30 -0800428 mTethering.getUpstreamIfaceRegexs().length != 0);
429
Robert Greenwalt0e80be12010-09-20 14:35:25 -0700430 if (DBG) {
431 mInetLog = new ArrayList();
432 }
Robert Greenwaltc3c5f862010-10-11 16:00:27 -0700433
434 mSettingsObserver = new SettingsObserver(mHandler, EVENT_APPLY_GLOBAL_HTTP_PROXY);
435 mSettingsObserver.observe(mContext);
Robert Greenwalt6f7c6092010-12-02 11:31:00 -0800436
437 loadGlobalProxy();
The Android Open Source Project28527d22009-03-03 19:31:44 -0800438 }
439
Robert Greenwaltec05b3c2009-10-30 14:17:42 -0700440
The Android Open Source Project28527d22009-03-03 19:31:44 -0800441 /**
Robert Greenwalt0659da32009-07-16 17:21:39 -0700442 * Sets the preferred network.
The Android Open Source Project28527d22009-03-03 19:31:44 -0800443 * @param preference the new preference
444 */
Robert Greenwalt6a2db8a2010-09-23 10:05:56 -0700445 public void setNetworkPreference(int preference) {
The Android Open Source Project28527d22009-03-03 19:31:44 -0800446 enforceChangePermission();
Robert Greenwalt6a2db8a2010-09-23 10:05:56 -0700447
448 mHandler.sendMessage(mHandler.obtainMessage(EVENT_SET_NETWORK_PREFERENCE, preference, 0));
The Android Open Source Project28527d22009-03-03 19:31:44 -0800449 }
450
451 public int getNetworkPreference() {
452 enforceAccessPermission();
Robert Greenwalt6a2db8a2010-09-23 10:05:56 -0700453 int preference;
454 synchronized(this) {
455 preference = mNetworkPreference;
456 }
457 return preference;
The Android Open Source Project28527d22009-03-03 19:31:44 -0800458 }
459
Robert Greenwalt6a2db8a2010-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 Project28527d22009-03-03 19:31:44 -0800473 }
Robert Greenwalt0659da32009-07-16 17:21:39 -0700474
The Android Open Source Project28527d22009-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 Greenwalt0659da32009-07-16 17:21:39 -0700486
The Android Open Source Project28527d22009-03-03 19:31:44 -0800487 /**
Robert Greenwalt0659da32009-07-16 17:21:39 -0700488 * Make the state of network connectivity conform to the preference settings
The Android Open Source Project28527d22009-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 Greenwalt2034b912009-08-12 16:08:25 -0700495 if (mNetTrackers[mNetworkPreference].getNetworkInfo().isConnected())
The Android Open Source Project28527d22009-03-03 19:31:44 -0800496 return;
497
Robert Greenwalt2034b912009-08-12 16:08:25 -0700498 if (!mNetTrackers[mNetworkPreference].isAvailable())
499 return;
The Android Open Source Project28527d22009-03-03 19:31:44 -0800500
Robert Greenwalt2034b912009-08-12 16:08:25 -0700501 for (int t=0; t <= ConnectivityManager.MAX_RADIO_TYPE; t++) {
Robert Greenwaltec05b3c2009-10-30 14:17:42 -0700502 if (t != mNetworkPreference && mNetTrackers[t] != null &&
Robert Greenwalt2034b912009-08-12 16:08:25 -0700503 mNetTrackers[t].getNetworkInfo().isConnected()) {
Robert Greenwaltf3f045b2009-08-20 15:25:14 -0700504 if (DBG) {
Wink Savillee70c6f52010-12-03 12:01:38 -0800505 log("tearing down " + mNetTrackers[t].getNetworkInfo() +
Robert Greenwaltf3f045b2009-08-20 15:25:14 -0700506 " in enforcePreference");
507 }
Robert Greenwalt2034b912009-08-12 16:08:25 -0700508 teardown(mNetTrackers[t]);
The Android Open Source Project28527d22009-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 Greenwalt0659da32009-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 Project28527d22009-03-03 19:31:44 -0800528 */
529 public NetworkInfo getActiveNetworkInfo() {
530 enforceAccessPermission();
Robert Greenwalt2034b912009-08-12 16:08:25 -0700531 for (int type=0; type <= ConnectivityManager.MAX_NETWORK_TYPE; type++) {
Robert Greenwaltec05b3c2009-10-30 14:17:42 -0700532 if (mNetAttributes[type] == null || !mNetAttributes[type].isDefault()) {
Robert Greenwalt2034b912009-08-12 16:08:25 -0700533 continue;
534 }
535 NetworkStateTracker t = mNetTrackers[type];
The Android Open Source Project28527d22009-03-03 19:31:44 -0800536 NetworkInfo info = t.getNetworkInfo();
537 if (info.isConnected()) {
Wink Savillee70c6f52010-12-03 12:01:38 -0800538 if (DBG && type != mActiveDefaultNetwork) {
539 loge("connected default network is not mActiveDefaultNetwork!");
540 }
The Android Open Source Project28527d22009-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 Greenwaltec05b3c2009-10-30 14:17:42 -0700559 NetworkInfo[] result = new NetworkInfo[mNetworksDefined];
The Android Open Source Project28527d22009-03-03 19:31:44 -0800560 int i = 0;
561 for (NetworkStateTracker t : mNetTrackers) {
Robert Greenwaltec05b3c2009-10-30 14:17:42 -0700562 if(t != null) result[i++] = t.getNetworkInfo();
The Android Open Source Project28527d22009-03-03 19:31:44 -0800563 }
564 return result;
565 }
566
Robert Greenwalt9f0ee4f2010-09-14 09:18:02 -0700567 /**
568 * Return LinkProperties for the active (i.e., connected) default
569 * network interface. It is assumed that at most one default network
570 * is active at a time. If more than one is active, it is indeterminate
571 * which will be returned.
572 * @return the ip properties for the active network, or {@code null} if
573 * none is active
574 */
575 public LinkProperties getActiveLinkProperties() {
576 enforceAccessPermission();
577 for (int type=0; type <= ConnectivityManager.MAX_NETWORK_TYPE; type++) {
578 if (mNetAttributes[type] == null || !mNetAttributes[type].isDefault()) {
579 continue;
580 }
581 NetworkStateTracker t = mNetTrackers[type];
582 NetworkInfo info = t.getNetworkInfo();
583 if (info.isConnected()) {
584 return t.getLinkProperties();
585 }
586 }
587 return null;
588 }
589
590 public LinkProperties getLinkProperties(int networkType) {
591 enforceAccessPermission();
592 if (ConnectivityManager.isNetworkTypeValid(networkType)) {
593 NetworkStateTracker t = mNetTrackers[networkType];
594 if (t != null) return t.getLinkProperties();
595 }
596 return null;
597 }
598
The Android Open Source Project28527d22009-03-03 19:31:44 -0800599 public boolean setRadios(boolean turnOn) {
600 boolean result = true;
601 enforceChangePermission();
602 for (NetworkStateTracker t : mNetTrackers) {
Robert Greenwaltec05b3c2009-10-30 14:17:42 -0700603 if (t != null) result = t.setRadio(turnOn) && result;
The Android Open Source Project28527d22009-03-03 19:31:44 -0800604 }
605 return result;
606 }
607
608 public boolean setRadio(int netType, boolean turnOn) {
609 enforceChangePermission();
610 if (!ConnectivityManager.isNetworkTypeValid(netType)) {
611 return false;
612 }
613 NetworkStateTracker tracker = mNetTrackers[netType];
614 return tracker != null && tracker.setRadio(turnOn);
615 }
616
Robert Greenwaltaffc3a12009-09-27 17:27:04 -0700617 /**
618 * Used to notice when the calling process dies so we can self-expire
619 *
620 * Also used to know if the process has cleaned up after itself when
621 * our auto-expire timer goes off. The timer has a link to an object.
622 *
623 */
Robert Greenwalt2034b912009-08-12 16:08:25 -0700624 private class FeatureUser implements IBinder.DeathRecipient {
625 int mNetworkType;
626 String mFeature;
627 IBinder mBinder;
628 int mPid;
629 int mUid;
Robert Greenwalt3eeb6032009-12-21 18:24:07 -0800630 long mCreateTime;
Robert Greenwalt2034b912009-08-12 16:08:25 -0700631
632 FeatureUser(int type, String feature, IBinder binder) {
633 super();
634 mNetworkType = type;
635 mFeature = feature;
636 mBinder = binder;
637 mPid = getCallingPid();
638 mUid = getCallingUid();
Robert Greenwalt3eeb6032009-12-21 18:24:07 -0800639 mCreateTime = System.currentTimeMillis();
Robert Greenwaltaffc3a12009-09-27 17:27:04 -0700640
Robert Greenwalt2034b912009-08-12 16:08:25 -0700641 try {
642 mBinder.linkToDeath(this, 0);
643 } catch (RemoteException e) {
644 binderDied();
645 }
646 }
647
648 void unlinkDeathRecipient() {
649 mBinder.unlinkToDeath(this, 0);
650 }
651
652 public void binderDied() {
Wink Savillee70c6f52010-12-03 12:01:38 -0800653 log("ConnectivityService FeatureUser binderDied(" +
Robert Greenwalt3eeb6032009-12-21 18:24:07 -0800654 mNetworkType + ", " + mFeature + ", " + mBinder + "), created " +
655 (System.currentTimeMillis() - mCreateTime) + " mSec ago");
Robert Greenwaltaffc3a12009-09-27 17:27:04 -0700656 stopUsingNetworkFeature(this, false);
Robert Greenwalt2034b912009-08-12 16:08:25 -0700657 }
658
Robert Greenwaltaffc3a12009-09-27 17:27:04 -0700659 public void expire() {
Wink Savillee70c6f52010-12-03 12:01:38 -0800660 log("ConnectivityService FeatureUser expire(" +
Robert Greenwalt3eeb6032009-12-21 18:24:07 -0800661 mNetworkType + ", " + mFeature + ", " + mBinder +"), created " +
662 (System.currentTimeMillis() - mCreateTime) + " mSec ago");
Robert Greenwaltaffc3a12009-09-27 17:27:04 -0700663 stopUsingNetworkFeature(this, false);
664 }
Robert Greenwalt3eeb6032009-12-21 18:24:07 -0800665
666 public String toString() {
667 return "FeatureUser("+mNetworkType+","+mFeature+","+mPid+","+mUid+"), created " +
668 (System.currentTimeMillis() - mCreateTime) + " mSec ago";
669 }
Robert Greenwalt2034b912009-08-12 16:08:25 -0700670 }
671
Robert Greenwaltaffc3a12009-09-27 17:27:04 -0700672 // javadoc from interface
Robert Greenwalt2034b912009-08-12 16:08:25 -0700673 public int startUsingNetworkFeature(int networkType, String feature,
674 IBinder binder) {
675 if (DBG) {
Wink Savillee70c6f52010-12-03 12:01:38 -0800676 log("startUsingNetworkFeature for net " + networkType + ": " + feature);
Robert Greenwalt2034b912009-08-12 16:08:25 -0700677 }
The Android Open Source Project28527d22009-03-03 19:31:44 -0800678 enforceChangePermission();
Robert Greenwaltec05b3c2009-10-30 14:17:42 -0700679 if (!ConnectivityManager.isNetworkTypeValid(networkType) ||
680 mNetAttributes[networkType] == null) {
Robert Greenwalt2034b912009-08-12 16:08:25 -0700681 return Phone.APN_REQUEST_FAILED;
The Android Open Source Project28527d22009-03-03 19:31:44 -0800682 }
Robert Greenwalt2034b912009-08-12 16:08:25 -0700683
Robert Greenwaltaffc3a12009-09-27 17:27:04 -0700684 FeatureUser f = new FeatureUser(networkType, feature, binder);
Robert Greenwalt2034b912009-08-12 16:08:25 -0700685
686 // TODO - move this into the MobileDataStateTracker
687 int usedNetworkType = networkType;
688 if(networkType == ConnectivityManager.TYPE_MOBILE) {
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) {
Robert Greenwalt5364d752010-12-15 13:26:33 -0800701 Integer currentPid = new Integer(getCallingPid());
Robert Greenwalt2034b912009-08-12 16:08:25 -0700702 if (usedNetworkType != networkType) {
Robert Greenwalt2034b912009-08-12 16:08:25 -0700703 NetworkStateTracker radio = mNetTrackers[networkType];
704 NetworkInfo ni = network.getNetworkInfo();
705
706 if (ni.isAvailable() == false) {
Wink Savillee70c6f52010-12-03 12:01:38 -0800707 if (DBG) log("special network not available");
Robert Greenwalt2034b912009-08-12 16:08:25 -0700708 return Phone.APN_TYPE_NOT_AVAILABLE;
709 }
710
Robert Greenwaltaffc3a12009-09-27 17:27:04 -0700711 synchronized(this) {
712 mFeatureUsers.add(f);
713 if (!mNetRequestersPids[usedNetworkType].contains(currentPid)) {
714 // this gets used for per-pid dns when connected
715 mNetRequestersPids[usedNetworkType].add(currentPid);
716 }
Robert Greenwalt2034b912009-08-12 16:08:25 -0700717 }
Robert Greenwalt6a2db8a2010-09-23 10:05:56 -0700718 mHandler.sendMessageDelayed(mHandler.obtainMessage(EVENT_RESTORE_DEFAULT_NETWORK,
Robert Greenwaltaffc3a12009-09-27 17:27:04 -0700719 f), getRestoreDefaultNetworkDelay());
720
Robert Greenwalt2034b912009-08-12 16:08:25 -0700721
Robert Greenwalta52c75a2009-08-19 20:19:33 -0700722 if ((ni.isConnectedOrConnecting() == true) &&
723 !network.isTeardownRequested()) {
Robert Greenwalt2034b912009-08-12 16:08:25 -0700724 if (ni.isConnected() == true) {
725 // add the pid-specific dns
Robert Greenwalt3afbead2010-07-23 15:46:26 -0700726 handleDnsConfigurationChange(networkType);
Wink Savillee70c6f52010-12-03 12:01:38 -0800727 if (DBG) log("special network already active");
Robert Greenwalt2034b912009-08-12 16:08:25 -0700728 return Phone.APN_ALREADY_ACTIVE;
729 }
Wink Savillee70c6f52010-12-03 12:01:38 -0800730 if (DBG) log("special network already connecting");
Robert Greenwalt2034b912009-08-12 16:08:25 -0700731 return Phone.APN_REQUEST_STARTED;
732 }
733
734 // check if the radio in play can make another contact
735 // assume if cannot for now
736
Wink Savillee70c6f52010-12-03 12:01:38 -0800737 if (DBG) log("reconnecting to special network");
Robert Greenwalt2034b912009-08-12 16:08:25 -0700738 network.reconnect();
739 return Phone.APN_REQUEST_STARTED;
740 } else {
Robert Greenwalt5364d752010-12-15 13:26:33 -0800741 // need to remember this unsupported request so we respond appropriately on stop
742 synchronized(this) {
743 mFeatureUsers.add(f);
744 if (!mNetRequestersPids[usedNetworkType].contains(currentPid)) {
745 // this gets used for per-pid dns when connected
746 mNetRequestersPids[usedNetworkType].add(currentPid);
747 }
748 }
Robert Greenwaltd391e892010-05-18 10:52:51 -0700749 return -1;
Robert Greenwalt2034b912009-08-12 16:08:25 -0700750 }
751 }
752 return Phone.APN_TYPE_NOT_AVAILABLE;
The Android Open Source Project28527d22009-03-03 19:31:44 -0800753 }
754
Robert Greenwaltaffc3a12009-09-27 17:27:04 -0700755 // javadoc from interface
The Android Open Source Project28527d22009-03-03 19:31:44 -0800756 public int stopUsingNetworkFeature(int networkType, String feature) {
Robert Greenwalt28f43012009-10-06 17:52:40 -0700757 enforceChangePermission();
758
Robert Greenwaltaffc3a12009-09-27 17:27:04 -0700759 int pid = getCallingPid();
760 int uid = getCallingUid();
761
762 FeatureUser u = null;
763 boolean found = false;
764
765 synchronized(this) {
766 for (int i = 0; i < mFeatureUsers.size() ; i++) {
767 u = (FeatureUser)mFeatureUsers.get(i);
768 if (uid == u.mUid && pid == u.mPid &&
769 networkType == u.mNetworkType &&
770 TextUtils.equals(feature, u.mFeature)) {
771 found = true;
772 break;
773 }
774 }
775 }
776 if (found && u != null) {
777 // stop regardless of how many other time this proc had called start
778 return stopUsingNetworkFeature(u, true);
779 } else {
780 // none found!
Wink Savillee70c6f52010-12-03 12:01:38 -0800781 if (DBG) log("ignoring stopUsingNetworkFeature - not a live request");
Robert Greenwaltaffc3a12009-09-27 17:27:04 -0700782 return 1;
783 }
Robert Greenwalt2034b912009-08-12 16:08:25 -0700784 }
785
Robert Greenwaltaffc3a12009-09-27 17:27:04 -0700786 private int stopUsingNetworkFeature(FeatureUser u, boolean ignoreDups) {
787 int networkType = u.mNetworkType;
788 String feature = u.mFeature;
789 int pid = u.mPid;
790 int uid = u.mUid;
791
792 NetworkStateTracker tracker = null;
793 boolean callTeardown = false; // used to carry our decision outside of sync block
794
Robert Greenwalt2034b912009-08-12 16:08:25 -0700795 if (DBG) {
Wink Savillee70c6f52010-12-03 12:01:38 -0800796 log("stopUsingNetworkFeature for net " + networkType +
Robert Greenwalt2034b912009-08-12 16:08:25 -0700797 ": " + feature);
798 }
Robert Greenwalt28f43012009-10-06 17:52:40 -0700799
The Android Open Source Project28527d22009-03-03 19:31:44 -0800800 if (!ConnectivityManager.isNetworkTypeValid(networkType)) {
801 return -1;
802 }
Robert Greenwalt2034b912009-08-12 16:08:25 -0700803
Robert Greenwaltaffc3a12009-09-27 17:27:04 -0700804 // need to link the mFeatureUsers list with the mNetRequestersPids state in this
805 // sync block
806 synchronized(this) {
807 // check if this process still has an outstanding start request
808 if (!mFeatureUsers.contains(u)) {
Wink Savillee70c6f52010-12-03 12:01:38 -0800809 if (DBG) log("ignoring - this process has no outstanding requests");
Robert Greenwalt2034b912009-08-12 16:08:25 -0700810 return 1;
811 }
Robert Greenwaltaffc3a12009-09-27 17:27:04 -0700812 u.unlinkDeathRecipient();
813 mFeatureUsers.remove(mFeatureUsers.indexOf(u));
814 // If we care about duplicate requests, check for that here.
815 //
816 // This is done to support the extension of a request - the app
817 // can request we start the network feature again and renew the
818 // auto-shutoff delay. Normal "stop" calls from the app though
819 // do not pay attention to duplicate requests - in effect the
820 // API does not refcount and a single stop will counter multiple starts.
821 if (ignoreDups == false) {
822 for (int i = 0; i < mFeatureUsers.size() ; i++) {
823 FeatureUser x = (FeatureUser)mFeatureUsers.get(i);
824 if (x.mUid == u.mUid && x.mPid == u.mPid &&
825 x.mNetworkType == u.mNetworkType &&
826 TextUtils.equals(x.mFeature, u.mFeature)) {
Wink Savillee70c6f52010-12-03 12:01:38 -0800827 if (DBG) log("ignoring stopUsingNetworkFeature as dup is found");
Robert Greenwaltaffc3a12009-09-27 17:27:04 -0700828 return 1;
829 }
830 }
Robert Greenwalt2034b912009-08-12 16:08:25 -0700831 }
Robert Greenwaltaffc3a12009-09-27 17:27:04 -0700832
833 // TODO - move to MobileDataStateTracker
834 int usedNetworkType = networkType;
835 if (networkType == ConnectivityManager.TYPE_MOBILE) {
836 if (TextUtils.equals(feature, Phone.FEATURE_ENABLE_MMS)) {
837 usedNetworkType = ConnectivityManager.TYPE_MOBILE_MMS;
838 } else if (TextUtils.equals(feature, Phone.FEATURE_ENABLE_SUPL)) {
839 usedNetworkType = ConnectivityManager.TYPE_MOBILE_SUPL;
840 } else if (TextUtils.equals(feature, Phone.FEATURE_ENABLE_DUN)) {
841 usedNetworkType = ConnectivityManager.TYPE_MOBILE_DUN;
842 } else if (TextUtils.equals(feature, Phone.FEATURE_ENABLE_HIPRI)) {
843 usedNetworkType = ConnectivityManager.TYPE_MOBILE_HIPRI;
844 }
845 }
846 tracker = mNetTrackers[usedNetworkType];
Robert Greenwaltec05b3c2009-10-30 14:17:42 -0700847 if (tracker == null) {
Wink Savillee70c6f52010-12-03 12:01:38 -0800848 if (DBG) log("ignoring - no known tracker for net type " + usedNetworkType);
Robert Greenwaltec05b3c2009-10-30 14:17:42 -0700849 return -1;
850 }
851 if (usedNetworkType != networkType) {
Robert Greenwaltaffc3a12009-09-27 17:27:04 -0700852 Integer currentPid = new Integer(pid);
Robert Greenwaltaffc3a12009-09-27 17:27:04 -0700853 mNetRequestersPids[usedNetworkType].remove(currentPid);
Robert Greenwalt0ca68a02009-12-17 14:54:59 -0800854 reassessPidDns(pid, true);
Robert Greenwaltaffc3a12009-09-27 17:27:04 -0700855 if (mNetRequestersPids[usedNetworkType].size() != 0) {
Wink Savillee70c6f52010-12-03 12:01:38 -0800856 if (DBG) log("not tearing down special network - " +
Robert Greenwaltaffc3a12009-09-27 17:27:04 -0700857 "others still using it");
858 return 1;
859 }
860 callTeardown = true;
861 }
862 }
Wink Savillee70c6f52010-12-03 12:01:38 -0800863 if (DBG) log("Doing network teardown");
Robert Greenwaltaffc3a12009-09-27 17:27:04 -0700864 if (callTeardown) {
865 tracker.teardown();
Robert Greenwalt2034b912009-08-12 16:08:25 -0700866 return 1;
867 } else {
Robert Greenwaltd391e892010-05-18 10:52:51 -0700868 return -1;
Robert Greenwalt2034b912009-08-12 16:08:25 -0700869 }
The Android Open Source Project28527d22009-03-03 19:31:44 -0800870 }
871
872 /**
Robert Greenwalt7fe44cb2010-08-27 09:24:29 -0700873 * @deprecated use requestRouteToHostAddress instead
874 *
The Android Open Source Project28527d22009-03-03 19:31:44 -0800875 * Ensure that a network route exists to deliver traffic to the specified
876 * host via the specified network interface.
Robert Greenwalt0659da32009-07-16 17:21:39 -0700877 * @param networkType the type of the network over which traffic to the
878 * specified host is to be routed
879 * @param hostAddress the IP address of the host to which the route is
880 * desired
The Android Open Source Project28527d22009-03-03 19:31:44 -0800881 * @return {@code true} on success, {@code false} on failure
882 */
883 public boolean requestRouteToHost(int networkType, int hostAddress) {
Robert Greenwalt7fe44cb2010-08-27 09:24:29 -0700884 InetAddress inetAddress = NetworkUtils.intToInetAddress(hostAddress);
885
886 if (inetAddress == null) {
887 return false;
888 }
889
890 return requestRouteToHostAddress(networkType, inetAddress.getAddress());
891 }
892
893 /**
894 * Ensure that a network route exists to deliver traffic to the specified
895 * host via the specified network interface.
896 * @param networkType the type of the network over which traffic to the
897 * specified host is to be routed
898 * @param hostAddress the IP address of the host to which the route is
899 * desired
900 * @return {@code true} on success, {@code false} on failure
901 */
902 public boolean requestRouteToHostAddress(int networkType, byte[] hostAddress) {
The Android Open Source Project28527d22009-03-03 19:31:44 -0800903 enforceChangePermission();
904 if (!ConnectivityManager.isNetworkTypeValid(networkType)) {
905 return false;
906 }
907 NetworkStateTracker tracker = mNetTrackers[networkType];
Robert Greenwalt4666ed02009-09-10 15:06:20 -0700908
Robert Greenwaltec05b3c2009-10-30 14:17:42 -0700909 if (tracker == null || !tracker.getNetworkInfo().isConnected() ||
910 tracker.isTeardownRequested()) {
Robert Greenwalt4666ed02009-09-10 15:06:20 -0700911 if (DBG) {
Wink Savillee70c6f52010-12-03 12:01:38 -0800912 log("requestRouteToHostAddress on down network " +
Robert Greenwalt7fe44cb2010-08-27 09:24:29 -0700913 "(" + networkType + ") - dropped");
Robert Greenwalt4666ed02009-09-10 15:06:20 -0700914 }
915 return false;
The Android Open Source Project28527d22009-03-03 19:31:44 -0800916 }
Robert Greenwalta7dfbd32010-06-15 15:43:39 -0700917 try {
Robert Greenwalt7fe44cb2010-08-27 09:24:29 -0700918 InetAddress addr = InetAddress.getByAddress(hostAddress);
Robert Greenwalta7dfbd32010-06-15 15:43:39 -0700919 return addHostRoute(tracker, addr);
920 } catch (UnknownHostException e) {}
921 return false;
Irfan Sheriff7f132d92010-06-09 15:39:36 -0700922 }
923
924 /**
925 * Ensure that a network route exists to deliver traffic to the specified
926 * host via the mobile data network.
927 * @param hostAddress the IP address of the host to which the route is desired,
928 * in network byte order.
Robert Greenwalta7dfbd32010-06-15 15:43:39 -0700929 * TODO - deprecate
Irfan Sheriff7f132d92010-06-09 15:39:36 -0700930 * @return {@code true} on success, {@code false} on failure
931 */
Robert Greenwalta7dfbd32010-06-15 15:43:39 -0700932 private boolean addHostRoute(NetworkStateTracker nt, InetAddress hostAddress) {
Irfan Sheriff7f132d92010-06-09 15:39:36 -0700933 if (nt.getNetworkInfo().getType() == ConnectivityManager.TYPE_WIFI) {
934 return false;
935 }
936
Robert Greenwalt1f1bcfe2010-08-30 10:56:47 -0700937 LinkProperties p = nt.getLinkProperties();
Robert Greenwalta7dfbd32010-06-15 15:43:39 -0700938 if (p == null) return false;
939 String interfaceName = p.getInterfaceName();
Irfan Sheriff7f132d92010-06-09 15:39:36 -0700940
941 if (DBG) {
Wink Savillee70c6f52010-12-03 12:01:38 -0800942 log("Requested host route to " + hostAddress + "(" + interfaceName + ")");
Irfan Sheriff7f132d92010-06-09 15:39:36 -0700943 }
Robert Greenwalta7dfbd32010-06-15 15:43:39 -0700944 if (interfaceName != null) {
Robert Greenwalt7fe44cb2010-08-27 09:24:29 -0700945 return NetworkUtils.addHostRoute(interfaceName, hostAddress, null);
Irfan Sheriff7f132d92010-06-09 15:39:36 -0700946 } else {
Wink Savillee70c6f52010-12-03 12:01:38 -0800947 if (DBG) loge("addHostRoute failed due to null interface name");
Irfan Sheriff7f132d92010-06-09 15:39:36 -0700948 return false;
949 }
The Android Open Source Project28527d22009-03-03 19:31:44 -0800950 }
951
952 /**
953 * @see ConnectivityManager#getBackgroundDataSetting()
954 */
955 public boolean getBackgroundDataSetting() {
956 return Settings.Secure.getInt(mContext.getContentResolver(),
957 Settings.Secure.BACKGROUND_DATA, 1) == 1;
958 }
Robert Greenwalt0659da32009-07-16 17:21:39 -0700959
The Android Open Source Project28527d22009-03-03 19:31:44 -0800960 /**
961 * @see ConnectivityManager#setBackgroundDataSetting(boolean)
962 */
963 public void setBackgroundDataSetting(boolean allowBackgroundDataUsage) {
964 mContext.enforceCallingOrSelfPermission(
965 android.Manifest.permission.CHANGE_BACKGROUND_DATA_SETTING,
966 "ConnectivityService");
Robert Greenwalt0659da32009-07-16 17:21:39 -0700967
Robert Greenwalt6a2db8a2010-09-23 10:05:56 -0700968 mHandler.sendMessage(mHandler.obtainMessage(EVENT_SET_BACKGROUND_DATA,
969 (allowBackgroundDataUsage ? ENABLED : DISABLED), 0));
970 }
The Android Open Source Project28527d22009-03-03 19:31:44 -0800971
Robert Greenwalt6a2db8a2010-09-23 10:05:56 -0700972 private void handleSetBackgroundData(boolean enabled) {
973 if (enabled != getBackgroundDataSetting()) {
974 Settings.Secure.putInt(mContext.getContentResolver(),
975 Settings.Secure.BACKGROUND_DATA, enabled ? 1 : 0);
976 Intent broadcast = new Intent(
977 ConnectivityManager.ACTION_BACKGROUND_DATA_SETTING_CHANGED);
978 mContext.sendBroadcast(broadcast);
979 }
Robert Greenwalt0659da32009-07-16 17:21:39 -0700980 }
981
Robert Greenwalt1b21f6c2010-02-23 18:58:05 -0800982 /**
983 * @see ConnectivityManager#getMobileDataEnabled()
984 */
985 public boolean getMobileDataEnabled() {
Wink Savilleb9024c62010-12-07 10:31:02 -0800986 // TODO: This detail should probably be in DataConnectionTracker's
987 // which is where we store the value and maybe make this
988 // asynchronous.
Robert Greenwalt1b21f6c2010-02-23 18:58:05 -0800989 enforceAccessPermission();
990 boolean retVal = Settings.Secure.getInt(mContext.getContentResolver(),
991 Settings.Secure.MOBILE_DATA, 1) == 1;
Wink Savillee70c6f52010-12-03 12:01:38 -0800992 if (DBG) log("getMobileDataEnabled returning " + retVal);
Robert Greenwalt1b21f6c2010-02-23 18:58:05 -0800993 return retVal;
994 }
995
996 /**
997 * @see ConnectivityManager#setMobileDataEnabled(boolean)
998 */
Robert Greenwalt6a2db8a2010-09-23 10:05:56 -0700999 public void setMobileDataEnabled(boolean enabled) {
Robert Greenwalt1b21f6c2010-02-23 18:58:05 -08001000 enforceChangePermission();
Wink Savillee70c6f52010-12-03 12:01:38 -08001001 if (DBG) log("setMobileDataEnabled(" + enabled + ")");
Robert Greenwalt1b21f6c2010-02-23 18:58:05 -08001002
Robert Greenwalt6a2db8a2010-09-23 10:05:56 -07001003 mHandler.sendMessage(mHandler.obtainMessage(EVENT_SET_MOBILE_DATA,
1004 (enabled ? ENABLED : DISABLED), 0));
1005 }
1006
1007 private void handleSetMobileData(boolean enabled) {
Wink Savilleb9024c62010-12-07 10:31:02 -08001008 if (mNetTrackers[ConnectivityManager.TYPE_MOBILE] != null) {
1009 if (DBG) {
1010 Slog.d(TAG, mNetTrackers[ConnectivityManager.TYPE_MOBILE].toString() + enabled);
Robert Greenwalt1b21f6c2010-02-23 18:58:05 -08001011 }
Wink Savilleb9024c62010-12-07 10:31:02 -08001012 mNetTrackers[ConnectivityManager.TYPE_MOBILE].setDataEnable(enabled);
Robert Greenwalt1b21f6c2010-02-23 18:58:05 -08001013 }
1014 }
1015
The Android Open Source Project28527d22009-03-03 19:31:44 -08001016 private void enforceAccessPermission() {
Robert Greenwalt0659da32009-07-16 17:21:39 -07001017 mContext.enforceCallingOrSelfPermission(
1018 android.Manifest.permission.ACCESS_NETWORK_STATE,
1019 "ConnectivityService");
The Android Open Source Project28527d22009-03-03 19:31:44 -08001020 }
1021
1022 private void enforceChangePermission() {
Robert Greenwalt0659da32009-07-16 17:21:39 -07001023 mContext.enforceCallingOrSelfPermission(
1024 android.Manifest.permission.CHANGE_NETWORK_STATE,
1025 "ConnectivityService");
The Android Open Source Project28527d22009-03-03 19:31:44 -08001026 }
1027
Robert Greenwalt0c4828c2010-01-26 11:40:34 -08001028 // TODO Make this a special check when it goes public
1029 private void enforceTetherChangePermission() {
1030 mContext.enforceCallingOrSelfPermission(
1031 android.Manifest.permission.CHANGE_NETWORK_STATE,
1032 "ConnectivityService");
1033 }
1034
Robert Greenwalt8e87f122010-02-11 18:18:40 -08001035 private void enforceTetherAccessPermission() {
1036 mContext.enforceCallingOrSelfPermission(
1037 android.Manifest.permission.ACCESS_NETWORK_STATE,
1038 "ConnectivityService");
1039 }
1040
Robert Greenwalt93dc1042010-06-15 12:19:37 -07001041 private void enforceConnectivityInternalPermission() {
1042 mContext.enforceCallingOrSelfPermission(
1043 android.Manifest.permission.CONNECTIVITY_INTERNAL,
1044 "ConnectivityService");
1045 }
1046
The Android Open Source Project28527d22009-03-03 19:31:44 -08001047 /**
Robert Greenwalt0659da32009-07-16 17:21:39 -07001048 * Handle a {@code DISCONNECTED} event. If this pertains to the non-active
1049 * network, we ignore it. If it is for the active network, we send out a
1050 * broadcast. But first, we check whether it might be possible to connect
1051 * to a different network.
The Android Open Source Project28527d22009-03-03 19:31:44 -08001052 * @param info the {@code NetworkInfo} for the network
1053 */
1054 private void handleDisconnect(NetworkInfo info) {
1055
Robert Greenwalt2034b912009-08-12 16:08:25 -07001056 int prevNetType = info.getType();
The Android Open Source Project28527d22009-03-03 19:31:44 -08001057
Robert Greenwalt2034b912009-08-12 16:08:25 -07001058 mNetTrackers[prevNetType].setTeardownRequested(false);
The Android Open Source Project28527d22009-03-03 19:31:44 -08001059 /*
1060 * If the disconnected network is not the active one, then don't report
1061 * this as a loss of connectivity. What probably happened is that we're
1062 * getting the disconnect for a network that we explicitly disabled
1063 * in accordance with network preference policies.
1064 */
Robert Greenwalt2034b912009-08-12 16:08:25 -07001065 if (!mNetAttributes[prevNetType].isDefault()) {
1066 List pids = mNetRequestersPids[prevNetType];
1067 for (int i = 0; i<pids.size(); i++) {
1068 Integer pid = (Integer)pids.get(i);
1069 // will remove them because the net's no longer connected
1070 // need to do this now as only now do we know the pids and
1071 // can properly null things that are no longer referenced.
1072 reassessPidDns(pid.intValue(), false);
The Android Open Source Project28527d22009-03-03 19:31:44 -08001073 }
The Android Open Source Project28527d22009-03-03 19:31:44 -08001074 }
1075
The Android Open Source Project28527d22009-03-03 19:31:44 -08001076 Intent intent = new Intent(ConnectivityManager.CONNECTIVITY_ACTION);
1077 intent.putExtra(ConnectivityManager.EXTRA_NETWORK_INFO, info);
1078 if (info.isFailover()) {
1079 intent.putExtra(ConnectivityManager.EXTRA_IS_FAILOVER, true);
1080 info.setFailover(false);
1081 }
1082 if (info.getReason() != null) {
1083 intent.putExtra(ConnectivityManager.EXTRA_REASON, info.getReason());
1084 }
1085 if (info.getExtraInfo() != null) {
Robert Greenwalt0659da32009-07-16 17:21:39 -07001086 intent.putExtra(ConnectivityManager.EXTRA_EXTRA_INFO,
1087 info.getExtraInfo());
The Android Open Source Project28527d22009-03-03 19:31:44 -08001088 }
Robert Greenwalt2034b912009-08-12 16:08:25 -07001089
Robert Greenwalt3cc68d32010-01-25 17:54:29 -08001090 NetworkStateTracker newNet = null;
1091 if (mNetAttributes[prevNetType].isDefault()) {
1092 newNet = tryFailover(prevNetType);
1093 if (newNet != null) {
1094 NetworkInfo switchTo = newNet.getNetworkInfo();
Robert Greenwaltb2a9e492010-09-20 18:01:43 -07001095 if (!switchTo.isConnected()) {
Wink Savillee70c6f52010-12-03 12:01:38 -08001096 // if the other net is connected they've already reset this and perhaps even
1097 // gotten a positive report we don't want to overwrite, but if not we need to
1098 // clear this now to turn our cellular sig strength white
Robert Greenwaltb2a9e492010-09-20 18:01:43 -07001099 mDefaultInetConditionPublished = 0;
1100 }
Robert Greenwalt3cc68d32010-01-25 17:54:29 -08001101 intent.putExtra(ConnectivityManager.EXTRA_OTHER_NETWORK_INFO, switchTo);
1102 } else {
Robert Greenwaltb2a9e492010-09-20 18:01:43 -07001103 mDefaultInetConditionPublished = 0; // we're not connected anymore
Robert Greenwalt3cc68d32010-01-25 17:54:29 -08001104 intent.putExtra(ConnectivityManager.EXTRA_NO_CONNECTIVITY, true);
1105 }
Robert Greenwaltf55ced92010-01-20 19:29:41 -08001106 }
Robert Greenwaltb2a9e492010-09-20 18:01:43 -07001107 intent.putExtra(ConnectivityManager.EXTRA_INET_CONDITION, mDefaultInetConditionPublished);
Robert Greenwaltf55ced92010-01-20 19:29:41 -08001108 // do this before we broadcast the change
Robert Greenwalt3afbead2010-07-23 15:46:26 -07001109 handleConnectivityChange(prevNetType);
Robert Greenwaltf55ced92010-01-20 19:29:41 -08001110
1111 sendStickyBroadcast(intent);
1112 /*
1113 * If the failover network is already connected, then immediately send
1114 * out a followup broadcast indicating successful failover
1115 */
1116 if (newNet != null && newNet.getNetworkInfo().isConnected()) {
1117 sendConnectedBroadcast(newNet.getNetworkInfo());
1118 }
1119 }
1120
Robert Greenwalt3cc68d32010-01-25 17:54:29 -08001121 // returns null if no failover available
Robert Greenwaltf55ced92010-01-20 19:29:41 -08001122 private NetworkStateTracker tryFailover(int prevNetType) {
Robert Greenwalt2034b912009-08-12 16:08:25 -07001123 /*
1124 * If this is a default network, check if other defaults are available
1125 * or active
1126 */
1127 NetworkStateTracker newNet = null;
1128 if (mNetAttributes[prevNetType].isDefault()) {
Robert Greenwalt2034b912009-08-12 16:08:25 -07001129 if (mActiveDefaultNetwork == prevNetType) {
1130 mActiveDefaultNetwork = -1;
1131 }
1132
1133 int newType = -1;
1134 int newPriority = -1;
Robert Greenwaltf55ced92010-01-20 19:29:41 -08001135 for (int checkType=0; checkType <= ConnectivityManager.MAX_NETWORK_TYPE; checkType++) {
Robert Greenwaltec05b3c2009-10-30 14:17:42 -07001136 if (checkType == prevNetType) continue;
1137 if (mNetAttributes[checkType] == null) continue;
Robert Greenwalt2034b912009-08-12 16:08:25 -07001138 if (mNetAttributes[checkType].isDefault()) {
1139 /* TODO - if we have multiple nets we could use
1140 * we may want to put more thought into which we choose
1141 */
1142 if (checkType == mNetworkPreference) {
1143 newType = checkType;
1144 break;
1145 }
Robert Greenwaltec05b3c2009-10-30 14:17:42 -07001146 if (mNetAttributes[checkType].mPriority > newPriority) {
Robert Greenwalt2034b912009-08-12 16:08:25 -07001147 newType = checkType;
Robert Greenwaltec05b3c2009-10-30 14:17:42 -07001148 newPriority = mNetAttributes[newType].mPriority;
Robert Greenwalt2034b912009-08-12 16:08:25 -07001149 }
The Android Open Source Project28527d22009-03-03 19:31:44 -08001150 }
1151 }
Robert Greenwalt2034b912009-08-12 16:08:25 -07001152
1153 if (newType != -1) {
1154 newNet = mNetTrackers[newType];
1155 /**
1156 * See if the other network is available to fail over to.
1157 * If is not available, we enable it anyway, so that it
1158 * will be able to connect when it does become available,
1159 * but we report a total loss of connectivity rather than
1160 * report that we are attempting to fail over.
1161 */
1162 if (newNet.isAvailable()) {
1163 NetworkInfo switchTo = newNet.getNetworkInfo();
1164 switchTo.setFailover(true);
Robert Greenwalta52c75a2009-08-19 20:19:33 -07001165 if (!switchTo.isConnectedOrConnecting() ||
1166 newNet.isTeardownRequested()) {
Robert Greenwalt2034b912009-08-12 16:08:25 -07001167 newNet.reconnect();
1168 }
1169 if (DBG) {
1170 if (switchTo.isConnected()) {
Wink Savillee70c6f52010-12-03 12:01:38 -08001171 log("Switching to already connected " + switchTo.getTypeName());
Robert Greenwalt2034b912009-08-12 16:08:25 -07001172 } else {
Wink Savillee70c6f52010-12-03 12:01:38 -08001173 log("Attempting to switch to " + switchTo.getTypeName());
Robert Greenwalt2034b912009-08-12 16:08:25 -07001174 }
1175 }
Robert Greenwalt2034b912009-08-12 16:08:25 -07001176 } else {
1177 newNet.reconnect();
Robert Greenwalt12984322010-03-09 14:55:08 -08001178 newNet = null; // not officially avail.. try anyway, but
1179 // report no failover
Robert Greenwalt2034b912009-08-12 16:08:25 -07001180 }
Robert Greenwalte981bc52010-10-08 16:35:52 -07001181 } else {
Wink Savillee70c6f52010-12-03 12:01:38 -08001182 loge("Network failover failing.");
Robert Greenwalt2034b912009-08-12 16:08:25 -07001183 }
The Android Open Source Project28527d22009-03-03 19:31:44 -08001184 }
Robert Greenwalt2034b912009-08-12 16:08:25 -07001185
Robert Greenwaltf55ced92010-01-20 19:29:41 -08001186 return newNet;
The Android Open Source Project28527d22009-03-03 19:31:44 -08001187 }
1188
1189 private void sendConnectedBroadcast(NetworkInfo info) {
Robert Greenwaltd3401f92010-09-15 17:36:33 -07001190 sendGeneralBroadcast(info, ConnectivityManager.CONNECTIVITY_ACTION);
1191 }
1192
1193 private void sendInetConditionBroadcast(NetworkInfo info) {
1194 sendGeneralBroadcast(info, ConnectivityManager.INET_CONDITION_ACTION);
1195 }
1196
1197 private void sendGeneralBroadcast(NetworkInfo info, String bcastType) {
1198 Intent intent = new Intent(bcastType);
The Android Open Source Project28527d22009-03-03 19:31:44 -08001199 intent.putExtra(ConnectivityManager.EXTRA_NETWORK_INFO, info);
1200 if (info.isFailover()) {
1201 intent.putExtra(ConnectivityManager.EXTRA_IS_FAILOVER, true);
1202 info.setFailover(false);
1203 }
1204 if (info.getReason() != null) {
1205 intent.putExtra(ConnectivityManager.EXTRA_REASON, info.getReason());
1206 }
1207 if (info.getExtraInfo() != null) {
Robert Greenwalt0659da32009-07-16 17:21:39 -07001208 intent.putExtra(ConnectivityManager.EXTRA_EXTRA_INFO,
1209 info.getExtraInfo());
The Android Open Source Project28527d22009-03-03 19:31:44 -08001210 }
Robert Greenwalt986c7412010-09-08 15:24:47 -07001211 intent.putExtra(ConnectivityManager.EXTRA_INET_CONDITION, mDefaultInetConditionPublished);
Mike Lockwoodfde2b762009-08-14 14:18:49 -04001212 sendStickyBroadcast(intent);
The Android Open Source Project28527d22009-03-03 19:31:44 -08001213 }
1214
1215 /**
1216 * Called when an attempt to fail over to another network has failed.
1217 * @param info the {@link NetworkInfo} for the failed network
1218 */
1219 private void handleConnectionFailure(NetworkInfo info) {
1220 mNetTrackers[info.getType()].setTeardownRequested(false);
The Android Open Source Project28527d22009-03-03 19:31:44 -08001221
Robert Greenwalt2034b912009-08-12 16:08:25 -07001222 String reason = info.getReason();
1223 String extraInfo = info.getExtraInfo();
Robert Greenwalt0659da32009-07-16 17:21:39 -07001224
Robert Greenwalte981bc52010-10-08 16:35:52 -07001225 String reasonText;
1226 if (reason == null) {
1227 reasonText = ".";
1228 } else {
1229 reasonText = " (" + reason + ").";
The Android Open Source Project28527d22009-03-03 19:31:44 -08001230 }
Wink Savillee70c6f52010-12-03 12:01:38 -08001231 loge("Attempt to connect to " + info.getTypeName() + " failed" + reasonText);
Robert Greenwalt2034b912009-08-12 16:08:25 -07001232
1233 Intent intent = new Intent(ConnectivityManager.CONNECTIVITY_ACTION);
1234 intent.putExtra(ConnectivityManager.EXTRA_NETWORK_INFO, info);
1235 if (getActiveNetworkInfo() == null) {
1236 intent.putExtra(ConnectivityManager.EXTRA_NO_CONNECTIVITY, true);
1237 }
1238 if (reason != null) {
1239 intent.putExtra(ConnectivityManager.EXTRA_REASON, reason);
1240 }
1241 if (extraInfo != null) {
1242 intent.putExtra(ConnectivityManager.EXTRA_EXTRA_INFO, extraInfo);
1243 }
1244 if (info.isFailover()) {
1245 intent.putExtra(ConnectivityManager.EXTRA_IS_FAILOVER, true);
1246 info.setFailover(false);
1247 }
Robert Greenwaltf55ced92010-01-20 19:29:41 -08001248
Robert Greenwalt3cc68d32010-01-25 17:54:29 -08001249 NetworkStateTracker newNet = null;
1250 if (mNetAttributes[info.getType()].isDefault()) {
1251 newNet = tryFailover(info.getType());
1252 if (newNet != null) {
1253 NetworkInfo switchTo = newNet.getNetworkInfo();
Robert Greenwaltb2a9e492010-09-20 18:01:43 -07001254 if (!switchTo.isConnected()) {
Robert Greenwalte981bc52010-10-08 16:35:52 -07001255 // if the other net is connected they've already reset this and perhaps
1256 // even gotten a positive report we don't want to overwrite, but if not
1257 // we need to clear this now to turn our cellular sig strength white
Robert Greenwaltb2a9e492010-09-20 18:01:43 -07001258 mDefaultInetConditionPublished = 0;
1259 }
Robert Greenwalt3cc68d32010-01-25 17:54:29 -08001260 intent.putExtra(ConnectivityManager.EXTRA_OTHER_NETWORK_INFO, switchTo);
1261 } else {
Robert Greenwaltb2a9e492010-09-20 18:01:43 -07001262 mDefaultInetConditionPublished = 0;
Robert Greenwalt3cc68d32010-01-25 17:54:29 -08001263 intent.putExtra(ConnectivityManager.EXTRA_NO_CONNECTIVITY, true);
1264 }
Robert Greenwaltf55ced92010-01-20 19:29:41 -08001265 }
Robert Greenwalt3cc68d32010-01-25 17:54:29 -08001266
Robert Greenwaltb2a9e492010-09-20 18:01:43 -07001267 intent.putExtra(ConnectivityManager.EXTRA_INET_CONDITION, mDefaultInetConditionPublished);
Mike Lockwoodfde2b762009-08-14 14:18:49 -04001268 sendStickyBroadcast(intent);
Robert Greenwaltf55ced92010-01-20 19:29:41 -08001269 /*
1270 * If the failover network is already connected, then immediately send
1271 * out a followup broadcast indicating successful failover
1272 */
1273 if (newNet != null && newNet.getNetworkInfo().isConnected()) {
1274 sendConnectedBroadcast(newNet.getNetworkInfo());
1275 }
Mike Lockwoodfde2b762009-08-14 14:18:49 -04001276 }
1277
1278 private void sendStickyBroadcast(Intent intent) {
1279 synchronized(this) {
Dianne Hackborna417ff82009-12-08 19:45:14 -08001280 if (!mSystemReady) {
1281 mInitialBroadcast = new Intent(intent);
Mike Lockwoodfde2b762009-08-14 14:18:49 -04001282 }
Dianne Hackborna417ff82009-12-08 19:45:14 -08001283 intent.addFlags(Intent.FLAG_RECEIVER_REGISTERED_ONLY_BEFORE_BOOT);
1284 mContext.sendStickyBroadcast(intent);
Mike Lockwoodfde2b762009-08-14 14:18:49 -04001285 }
1286 }
1287
1288 void systemReady() {
1289 synchronized(this) {
1290 mSystemReady = true;
Dianne Hackborna417ff82009-12-08 19:45:14 -08001291 if (mInitialBroadcast != null) {
1292 mContext.sendStickyBroadcast(mInitialBroadcast);
1293 mInitialBroadcast = null;
Mike Lockwoodfde2b762009-08-14 14:18:49 -04001294 }
1295 }
Robert Greenwaltc3c5f862010-10-11 16:00:27 -07001296 // load the global proxy at startup
1297 mHandler.sendMessage(mHandler.obtainMessage(EVENT_APPLY_GLOBAL_HTTP_PROXY));
The Android Open Source Project28527d22009-03-03 19:31:44 -08001298 }
1299
1300 private void handleConnect(NetworkInfo info) {
Robert Greenwalt2034b912009-08-12 16:08:25 -07001301 int type = info.getType();
The Android Open Source Project28527d22009-03-03 19:31:44 -08001302
1303 // snapshot isFailover, because sendConnectedBroadcast() resets it
1304 boolean isFailover = info.isFailover();
Robert Greenwalt2034b912009-08-12 16:08:25 -07001305 NetworkStateTracker thisNet = mNetTrackers[type];
The Android Open Source Project28527d22009-03-03 19:31:44 -08001306
Robert Greenwalt2034b912009-08-12 16:08:25 -07001307 // if this is a default net and other default is running
1308 // kill the one not preferred
1309 if (mNetAttributes[type].isDefault()) {
Robert Greenwalt2034b912009-08-12 16:08:25 -07001310 if (mActiveDefaultNetwork != -1 && mActiveDefaultNetwork != type) {
1311 if ((type != mNetworkPreference &&
1312 mNetAttributes[mActiveDefaultNetwork].mPriority >
1313 mNetAttributes[type].mPriority) ||
1314 mNetworkPreference == mActiveDefaultNetwork) {
1315 // don't accept this one
Wink Savillee70c6f52010-12-03 12:01:38 -08001316 if (DBG) {
1317 log("Not broadcasting CONNECT_ACTION " +
Robert Greenwalt2034b912009-08-12 16:08:25 -07001318 "to torn down network " + info.getTypeName());
Wink Savillee70c6f52010-12-03 12:01:38 -08001319 }
Robert Greenwalt2034b912009-08-12 16:08:25 -07001320 teardown(thisNet);
1321 return;
1322 } else {
1323 // tear down the other
1324 NetworkStateTracker otherNet =
1325 mNetTrackers[mActiveDefaultNetwork];
Wink Savillee70c6f52010-12-03 12:01:38 -08001326 if (DBG) {
1327 log("Policy requires " + otherNet.getNetworkInfo().getTypeName() +
Robert Greenwalt2034b912009-08-12 16:08:25 -07001328 " teardown");
Wink Savillee70c6f52010-12-03 12:01:38 -08001329 }
Robert Greenwalt2034b912009-08-12 16:08:25 -07001330 if (!teardown(otherNet)) {
Wink Savillee70c6f52010-12-03 12:01:38 -08001331 loge("Network declined teardown request");
Robert Greenwalt2034b912009-08-12 16:08:25 -07001332 return;
1333 }
Robert Greenwalt93dc1042010-06-15 12:19:37 -07001334 }
1335 }
1336 synchronized (ConnectivityService.this) {
1337 // have a new default network, release the transition wakelock in a second
1338 // if it's held. The second pause is to allow apps to reconnect over the
1339 // new network
1340 if (mNetTransitionWakeLock.isHeld()) {
1341 mHandler.sendMessageDelayed(mHandler.obtainMessage(
Robert Greenwaltccb36f92010-09-24 14:32:21 -07001342 EVENT_CLEAR_NET_TRANSITION_WAKELOCK,
Robert Greenwalt93dc1042010-06-15 12:19:37 -07001343 mNetTransitionWakeLockSerialNumber, 0),
1344 1000);
Robert Greenwalt2034b912009-08-12 16:08:25 -07001345 }
1346 }
1347 mActiveDefaultNetwork = type;
Robert Greenwalt986c7412010-09-08 15:24:47 -07001348 // this will cause us to come up initially as unconnected and switching
1349 // to connected after our normal pause unless somebody reports us as reall
1350 // disconnected
1351 mDefaultInetConditionPublished = 0;
1352 mDefaultConnectionSequence++;
1353 mInetConditionChangeInFlight = false;
1354 // Don't do this - if we never sign in stay, grey
1355 //reportNetworkCondition(mActiveDefaultNetwork, 100);
Robert Greenwalt2034b912009-08-12 16:08:25 -07001356 }
The Android Open Source Project28527d22009-03-03 19:31:44 -08001357 thisNet.setTeardownRequested(false);
Irfan Sheriff7f132d92010-06-09 15:39:36 -07001358 updateNetworkSettings(thisNet);
Robert Greenwalt3afbead2010-07-23 15:46:26 -07001359 handleConnectivityChange(type);
Robert Greenwalt2034b912009-08-12 16:08:25 -07001360 sendConnectedBroadcast(info);
The Android Open Source Project28527d22009-03-03 19:31:44 -08001361 }
1362
The Android Open Source Project28527d22009-03-03 19:31:44 -08001363 /**
Robert Greenwalt3afbead2010-07-23 15:46:26 -07001364 * After a change in the connectivity state of a network. We're mainly
1365 * concerned with making sure that the list of DNS servers is set up
1366 * according to which networks are connected, and ensuring that the
1367 * right routing table entries exist.
The Android Open Source Project28527d22009-03-03 19:31:44 -08001368 */
Robert Greenwalt3afbead2010-07-23 15:46:26 -07001369 private void handleConnectivityChange(int netType) {
The Android Open Source Project28527d22009-03-03 19:31:44 -08001370 /*
Robert Greenwalt2034b912009-08-12 16:08:25 -07001371 * If a non-default network is enabled, add the host routes that
Robert Greenwalt3afbead2010-07-23 15:46:26 -07001372 * will allow it's DNS servers to be accessed.
The Android Open Source Project28527d22009-03-03 19:31:44 -08001373 */
Robert Greenwalt3afbead2010-07-23 15:46:26 -07001374 handleDnsConfigurationChange(netType);
Robert Greenwalt2034b912009-08-12 16:08:25 -07001375
Robert Greenwalt3afbead2010-07-23 15:46:26 -07001376 if (mNetTrackers[netType].getNetworkInfo().isConnected()) {
1377 if (mNetAttributes[netType].isDefault()) {
Robert Greenwaltc3c5f862010-10-11 16:00:27 -07001378 handleApplyDefaultProxy(netType);
Robert Greenwalt3afbead2010-07-23 15:46:26 -07001379 addDefaultRoute(mNetTrackers[netType]);
Robert Greenwalt2034b912009-08-12 16:08:25 -07001380 } else {
Robert Greenwalt3afbead2010-07-23 15:46:26 -07001381 addPrivateDnsRoutes(mNetTrackers[netType]);
1382 }
1383 } else {
1384 if (mNetAttributes[netType].isDefault()) {
1385 removeDefaultRoute(mNetTrackers[netType]);
1386 } else {
1387 removePrivateDnsRoutes(mNetTrackers[netType]);
Robert Greenwalt2034b912009-08-12 16:08:25 -07001388 }
The Android Open Source Project28527d22009-03-03 19:31:44 -08001389 }
1390 }
1391
Irfan Sheriff7f132d92010-06-09 15:39:36 -07001392 private void addPrivateDnsRoutes(NetworkStateTracker nt) {
Irfan Sheriff7f132d92010-06-09 15:39:36 -07001393 boolean privateDnsRouteSet = nt.isPrivateDnsRouteSet();
Robert Greenwalt1f1bcfe2010-08-30 10:56:47 -07001394 LinkProperties p = nt.getLinkProperties();
Robert Greenwalta7dfbd32010-06-15 15:43:39 -07001395 if (p == null) return;
1396 String interfaceName = p.getInterfaceName();
Irfan Sheriff7f132d92010-06-09 15:39:36 -07001397
1398 if (DBG) {
Wink Savillee70c6f52010-12-03 12:01:38 -08001399 log("addPrivateDnsRoutes for " + nt +
Irfan Sheriff7f132d92010-06-09 15:39:36 -07001400 "(" + interfaceName + ") - mPrivateDnsRouteSet = " + privateDnsRouteSet);
1401 }
Irfan Sheriff7f132d92010-06-09 15:39:36 -07001402 if (interfaceName != null && !privateDnsRouteSet) {
Robert Greenwalta7dfbd32010-06-15 15:43:39 -07001403 Collection<InetAddress> dnsList = p.getDnses();
1404 for (InetAddress dns : dnsList) {
Wink Savillee70c6f52010-12-03 12:01:38 -08001405 if (DBG) log(" adding " + dns);
Robert Greenwalt7fe44cb2010-08-27 09:24:29 -07001406 NetworkUtils.addHostRoute(interfaceName, dns, null);
Irfan Sheriff7f132d92010-06-09 15:39:36 -07001407 }
1408 nt.privateDnsRouteSet(true);
1409 }
1410 }
1411
1412 private void removePrivateDnsRoutes(NetworkStateTracker nt) {
1413 // TODO - we should do this explicitly but the NetUtils api doesnt
1414 // support this yet - must remove all. No worse than before
Robert Greenwalt1f1bcfe2010-08-30 10:56:47 -07001415 LinkProperties p = nt.getLinkProperties();
Robert Greenwalta7dfbd32010-06-15 15:43:39 -07001416 if (p == null) return;
1417 String interfaceName = p.getInterfaceName();
Irfan Sheriff7f132d92010-06-09 15:39:36 -07001418 boolean privateDnsRouteSet = nt.isPrivateDnsRouteSet();
1419 if (interfaceName != null && privateDnsRouteSet) {
1420 if (DBG) {
Wink Savillee70c6f52010-12-03 12:01:38 -08001421 log("removePrivateDnsRoutes for " + nt.getNetworkInfo().getTypeName() +
Irfan Sheriff7f132d92010-06-09 15:39:36 -07001422 " (" + interfaceName + ")");
1423 }
1424 NetworkUtils.removeHostRoutes(interfaceName);
1425 nt.privateDnsRouteSet(false);
1426 }
1427 }
1428
Irfan Sheriff7f132d92010-06-09 15:39:36 -07001429
1430 private void addDefaultRoute(NetworkStateTracker nt) {
Robert Greenwalt1f1bcfe2010-08-30 10:56:47 -07001431 LinkProperties p = nt.getLinkProperties();
Robert Greenwalta7dfbd32010-06-15 15:43:39 -07001432 if (p == null) return;
1433 String interfaceName = p.getInterfaceName();
1434 InetAddress defaultGatewayAddr = p.getGateway();
Irfan Sheriff7f132d92010-06-09 15:39:36 -07001435
Robert Greenwalt3afbead2010-07-23 15:46:26 -07001436 if ((interfaceName != null) && (defaultGatewayAddr != null )) {
Robert Greenwalt7fe44cb2010-08-27 09:24:29 -07001437 if (!NetworkUtils.addDefaultRoute(interfaceName, defaultGatewayAddr) && DBG) {
Robert Greenwalta7dfbd32010-06-15 15:43:39 -07001438 NetworkInfo networkInfo = nt.getNetworkInfo();
Wink Savillee70c6f52010-12-03 12:01:38 -08001439 log("addDefaultRoute for " + networkInfo.getTypeName() +
Irfan Sheriff7f132d92010-06-09 15:39:36 -07001440 " (" + interfaceName + "), GatewayAddr=" + defaultGatewayAddr);
1441 }
Irfan Sheriff7f132d92010-06-09 15:39:36 -07001442 }
1443 }
1444
1445
1446 public void removeDefaultRoute(NetworkStateTracker nt) {
Robert Greenwalt1f1bcfe2010-08-30 10:56:47 -07001447 LinkProperties p = nt.getLinkProperties();
Robert Greenwalta7dfbd32010-06-15 15:43:39 -07001448 if (p == null) return;
1449 String interfaceName = p.getInterfaceName();
Irfan Sheriff7f132d92010-06-09 15:39:36 -07001450
Robert Greenwalt3afbead2010-07-23 15:46:26 -07001451 if (interfaceName != null) {
1452 if ((NetworkUtils.removeDefaultRoute(interfaceName) >= 0) && DBG) {
Robert Greenwalta7dfbd32010-06-15 15:43:39 -07001453 NetworkInfo networkInfo = nt.getNetworkInfo();
Wink Savillee70c6f52010-12-03 12:01:38 -08001454 log("removeDefaultRoute for " + networkInfo.getTypeName() + " (" +
Irfan Sheriff7f132d92010-06-09 15:39:36 -07001455 interfaceName + ")");
1456 }
Irfan Sheriff7f132d92010-06-09 15:39:36 -07001457 }
1458 }
1459
1460 /**
1461 * Reads the network specific TCP buffer sizes from SystemProperties
1462 * net.tcp.buffersize.[default|wifi|umts|edge|gprs] and set them for system
1463 * wide use
1464 */
1465 public void updateNetworkSettings(NetworkStateTracker nt) {
1466 String key = nt.getTcpBufferSizesPropName();
1467 String bufferSizes = SystemProperties.get(key);
1468
1469 if (bufferSizes.length() == 0) {
Wink Savillee70c6f52010-12-03 12:01:38 -08001470 loge(key + " not found in system properties. Using defaults");
Irfan Sheriff7f132d92010-06-09 15:39:36 -07001471
1472 // Setting to default values so we won't be stuck to previous values
1473 key = "net.tcp.buffersize.default";
1474 bufferSizes = SystemProperties.get(key);
1475 }
1476
1477 // Set values in kernel
1478 if (bufferSizes.length() != 0) {
1479 if (DBG) {
Wink Savillee70c6f52010-12-03 12:01:38 -08001480 log("Setting TCP values: [" + bufferSizes
Irfan Sheriff7f132d92010-06-09 15:39:36 -07001481 + "] which comes from [" + key + "]");
1482 }
1483 setBufferSize(bufferSizes);
1484 }
1485 }
1486
1487 /**
1488 * Writes TCP buffer sizes to /sys/kernel/ipv4/tcp_[r/w]mem_[min/def/max]
1489 * which maps to /proc/sys/net/ipv4/tcp_rmem and tcpwmem
1490 *
1491 * @param bufferSizes in the format of "readMin, readInitial, readMax,
1492 * writeMin, writeInitial, writeMax"
1493 */
1494 private void setBufferSize(String bufferSizes) {
1495 try {
1496 String[] values = bufferSizes.split(",");
1497
1498 if (values.length == 6) {
1499 final String prefix = "/sys/kernel/ipv4/tcp_";
1500 stringToFile(prefix + "rmem_min", values[0]);
1501 stringToFile(prefix + "rmem_def", values[1]);
1502 stringToFile(prefix + "rmem_max", values[2]);
1503 stringToFile(prefix + "wmem_min", values[3]);
1504 stringToFile(prefix + "wmem_def", values[4]);
1505 stringToFile(prefix + "wmem_max", values[5]);
1506 } else {
Wink Savillee70c6f52010-12-03 12:01:38 -08001507 loge("Invalid buffersize string: " + bufferSizes);
Irfan Sheriff7f132d92010-06-09 15:39:36 -07001508 }
1509 } catch (IOException e) {
Wink Savillee70c6f52010-12-03 12:01:38 -08001510 loge("Can't set tcp buffer sizes:" + e);
Irfan Sheriff7f132d92010-06-09 15:39:36 -07001511 }
1512 }
1513
1514 /**
1515 * Writes string to file. Basically same as "echo -n $string > $filename"
1516 *
1517 * @param filename
1518 * @param string
1519 * @throws IOException
1520 */
1521 private void stringToFile(String filename, String string) throws IOException {
1522 FileWriter out = new FileWriter(filename);
1523 try {
1524 out.write(string);
1525 } finally {
1526 out.close();
1527 }
1528 }
1529
1530
Robert Greenwalt2034b912009-08-12 16:08:25 -07001531 /**
1532 * Adjust the per-process dns entries (net.dns<x>.<pid>) based
1533 * on the highest priority active net which this process requested.
1534 * If there aren't any, clear it out
1535 */
1536 private void reassessPidDns(int myPid, boolean doBump)
1537 {
Wink Savillee70c6f52010-12-03 12:01:38 -08001538 if (DBG) log("reassessPidDns for pid " + myPid);
Robert Greenwalt2034b912009-08-12 16:08:25 -07001539 for(int i : mPriorityList) {
1540 if (mNetAttributes[i].isDefault()) {
1541 continue;
1542 }
1543 NetworkStateTracker nt = mNetTrackers[i];
Robert Greenwalt0659da32009-07-16 17:21:39 -07001544 if (nt.getNetworkInfo().isConnected() &&
1545 !nt.isTeardownRequested()) {
Robert Greenwalt1f1bcfe2010-08-30 10:56:47 -07001546 LinkProperties p = nt.getLinkProperties();
Robert Greenwalta7dfbd32010-06-15 15:43:39 -07001547 if (p == null) continue;
Robert Greenwalt2034b912009-08-12 16:08:25 -07001548 List pids = mNetRequestersPids[i];
1549 for (int j=0; j<pids.size(); j++) {
1550 Integer pid = (Integer)pids.get(j);
1551 if (pid.intValue() == myPid) {
Robert Greenwalta7dfbd32010-06-15 15:43:39 -07001552 Collection<InetAddress> dnses = p.getDnses();
1553 writePidDns(dnses, myPid);
Robert Greenwalt2034b912009-08-12 16:08:25 -07001554 if (doBump) {
1555 bumpDns();
1556 }
1557 return;
1558 }
1559 }
1560 }
1561 }
1562 // nothing found - delete
1563 for (int i = 1; ; i++) {
1564 String prop = "net.dns" + i + "." + myPid;
1565 if (SystemProperties.get(prop).length() == 0) {
1566 if (doBump) {
1567 bumpDns();
1568 }
1569 return;
1570 }
1571 SystemProperties.set(prop, "");
1572 }
1573 }
1574
Robert Greenwalta7dfbd32010-06-15 15:43:39 -07001575 private void writePidDns(Collection <InetAddress> dnses, int pid) {
Robert Greenwalt2034b912009-08-12 16:08:25 -07001576 int j = 1;
Robert Greenwalta7dfbd32010-06-15 15:43:39 -07001577 for (InetAddress dns : dnses) {
1578 SystemProperties.set("net.dns" + j++ + "." + pid, dns.getHostAddress());
Robert Greenwalt2034b912009-08-12 16:08:25 -07001579 }
1580 }
1581
1582 private void bumpDns() {
1583 /*
1584 * Bump the property that tells the name resolver library to reread
1585 * the DNS server list from the properties.
1586 */
1587 String propVal = SystemProperties.get("net.dnschange");
1588 int n = 0;
1589 if (propVal.length() != 0) {
1590 try {
1591 n = Integer.parseInt(propVal);
1592 } catch (NumberFormatException e) {}
1593 }
1594 SystemProperties.set("net.dnschange", "" + (n+1));
Robert Greenwalt051642b2010-11-02 14:08:23 -07001595 /*
1596 * Tell the VMs to toss their DNS caches
1597 */
1598 Intent intent = new Intent(Intent.ACTION_CLEAR_DNS_CACHE);
1599 intent.addFlags(Intent.FLAG_RECEIVER_REPLACE_PENDING);
1600 mContext.sendBroadcast(intent);
Robert Greenwalt2034b912009-08-12 16:08:25 -07001601 }
1602
Robert Greenwalt3afbead2010-07-23 15:46:26 -07001603 private void handleDnsConfigurationChange(int netType) {
Robert Greenwalt2034b912009-08-12 16:08:25 -07001604 // add default net's dns entries
Robert Greenwalt3afbead2010-07-23 15:46:26 -07001605 NetworkStateTracker nt = mNetTrackers[netType];
1606 if (nt != null && nt.getNetworkInfo().isConnected() && !nt.isTeardownRequested()) {
Robert Greenwalt1f1bcfe2010-08-30 10:56:47 -07001607 LinkProperties p = nt.getLinkProperties();
Robert Greenwalt3afbead2010-07-23 15:46:26 -07001608 if (p == null) return;
1609 Collection<InetAddress> dnses = p.getDnses();
1610 if (mNetAttributes[netType].isDefault()) {
1611 int j = 1;
Robert Greenwalt94daa182010-09-01 11:34:05 -07001612 if (dnses.size() == 0 && mDefaultDns != null) {
Robert Greenwalt3afbead2010-07-23 15:46:26 -07001613 if (DBG) {
Wink Savillee70c6f52010-12-03 12:01:38 -08001614 log("no dns provided - using " + mDefaultDns.getHostAddress());
Robert Greenwalt2034b912009-08-12 16:08:25 -07001615 }
Robert Greenwalt94daa182010-09-01 11:34:05 -07001616 SystemProperties.set("net.dns1", mDefaultDns.getHostAddress());
1617 j++;
1618 } else {
1619 for (InetAddress dns : dnses) {
1620 if (DBG) {
Wink Savillee70c6f52010-12-03 12:01:38 -08001621 log("adding dns " + dns + " for " +
Robert Greenwalt94daa182010-09-01 11:34:05 -07001622 nt.getNetworkInfo().getTypeName());
1623 }
1624 SystemProperties.set("net.dns" + j++, dns.getHostAddress());
1625 }
Robert Greenwalt3afbead2010-07-23 15:46:26 -07001626 }
1627 for (int k=j ; k<mNumDnsEntries; k++) {
Wink Savillee70c6f52010-12-03 12:01:38 -08001628 if (DBG) log("erasing net.dns" + k);
Robert Greenwalt3afbead2010-07-23 15:46:26 -07001629 SystemProperties.set("net.dns" + k, "");
1630 }
1631 mNumDnsEntries = j;
1632 } else {
1633 // set per-pid dns for attached secondary nets
1634 List pids = mNetRequestersPids[netType];
1635 for (int y=0; y< pids.size(); y++) {
1636 Integer pid = (Integer)pids.get(y);
1637 writePidDns(dnses, pid.intValue());
The Android Open Source Project28527d22009-03-03 19:31:44 -08001638 }
1639 }
Robert Greenwalt3afbead2010-07-23 15:46:26 -07001640 bumpDns();
The Android Open Source Project28527d22009-03-03 19:31:44 -08001641 }
Robert Greenwalt2034b912009-08-12 16:08:25 -07001642 }
1643
1644 private int getRestoreDefaultNetworkDelay() {
1645 String restoreDefaultNetworkDelayStr = SystemProperties.get(
1646 NETWORK_RESTORE_DELAY_PROP_NAME);
1647 if(restoreDefaultNetworkDelayStr != null &&
1648 restoreDefaultNetworkDelayStr.length() != 0) {
1649 try {
1650 return Integer.valueOf(restoreDefaultNetworkDelayStr);
1651 } catch (NumberFormatException e) {
1652 }
The Android Open Source Project28527d22009-03-03 19:31:44 -08001653 }
Robert Greenwalt2034b912009-08-12 16:08:25 -07001654 return RESTORE_DEFAULT_NETWORK_DELAY;
The Android Open Source Project28527d22009-03-03 19:31:44 -08001655 }
1656
1657 @Override
1658 protected void dump(FileDescriptor fd, PrintWriter pw, String[] args) {
Robert Greenwalt0659da32009-07-16 17:21:39 -07001659 if (mContext.checkCallingOrSelfPermission(
1660 android.Manifest.permission.DUMP)
The Android Open Source Project28527d22009-03-03 19:31:44 -08001661 != PackageManager.PERMISSION_GRANTED) {
Robert Greenwalt0659da32009-07-16 17:21:39 -07001662 pw.println("Permission Denial: can't dump ConnectivityService " +
1663 "from from pid=" + Binder.getCallingPid() + ", uid=" +
1664 Binder.getCallingUid());
The Android Open Source Project28527d22009-03-03 19:31:44 -08001665 return;
1666 }
The Android Open Source Project28527d22009-03-03 19:31:44 -08001667 pw.println();
1668 for (NetworkStateTracker nst : mNetTrackers) {
Robert Greenwalt3eeb6032009-12-21 18:24:07 -08001669 if (nst != null) {
1670 if (nst.getNetworkInfo().isConnected()) {
1671 pw.println("Active network: " + nst.getNetworkInfo().
1672 getTypeName());
1673 }
1674 pw.println(nst.getNetworkInfo());
1675 pw.println(nst);
1676 pw.println();
Robert Greenwalt2034b912009-08-12 16:08:25 -07001677 }
The Android Open Source Project28527d22009-03-03 19:31:44 -08001678 }
Robert Greenwalt3eeb6032009-12-21 18:24:07 -08001679
1680 pw.println("Network Requester Pids:");
1681 for (int net : mPriorityList) {
1682 String pidString = net + ": ";
1683 for (Object pid : mNetRequestersPids[net]) {
1684 pidString = pidString + pid.toString() + ", ";
1685 }
1686 pw.println(pidString);
1687 }
1688 pw.println();
1689
1690 pw.println("FeatureUsers:");
1691 for (Object requester : mFeatureUsers) {
1692 pw.println(requester.toString());
1693 }
1694 pw.println();
Robert Greenwalt8e87f122010-02-11 18:18:40 -08001695
Robert Greenwalt93dc1042010-06-15 12:19:37 -07001696 synchronized (this) {
1697 pw.println("NetworkTranstionWakeLock is currently " +
1698 (mNetTransitionWakeLock.isHeld() ? "" : "not ") + "held.");
1699 pw.println("It was last requested for "+mNetTransitionWakeLockCausedBy);
1700 }
1701 pw.println();
1702
Robert Greenwalt8e87f122010-02-11 18:18:40 -08001703 mTethering.dump(fd, pw, args);
Robert Greenwalt0e80be12010-09-20 14:35:25 -07001704
1705 if (mInetLog != null) {
1706 pw.println();
1707 pw.println("Inet condition reports:");
1708 for(int i = 0; i < mInetLog.size(); i++) {
1709 pw.println(mInetLog.get(i));
1710 }
1711 }
The Android Open Source Project28527d22009-03-03 19:31:44 -08001712 }
1713
Robert Greenwalt2034b912009-08-12 16:08:25 -07001714 // must be stateless - things change under us.
The Android Open Source Project28527d22009-03-03 19:31:44 -08001715 private class MyHandler extends Handler {
Wink Saville775aad62010-09-02 19:23:52 -07001716 public MyHandler(Looper looper) {
1717 super(looper);
1718 }
1719
The Android Open Source Project28527d22009-03-03 19:31:44 -08001720 @Override
1721 public void handleMessage(Message msg) {
1722 NetworkInfo info;
1723 switch (msg.what) {
1724 case NetworkStateTracker.EVENT_STATE_CHANGED:
1725 info = (NetworkInfo) msg.obj;
Robert Greenwalt12c44552009-12-07 11:33:18 -08001726 int type = info.getType();
1727 NetworkInfo.State state = info.getState();
Robert Greenwalt24e2d2b2010-01-25 16:14:00 -08001728 // only do this optimization for wifi. It going into scan mode for location
1729 // services generates alot of noise. Meanwhile the mms apn won't send out
1730 // subsequent notifications when on default cellular because it never
1731 // disconnects.. so only do this to wifi notifications. Fixed better when the
1732 // APN notifications are standardized.
1733 if (mNetAttributes[type].mLastState == state &&
1734 mNetAttributes[type].mRadio == ConnectivityManager.TYPE_WIFI) {
Robert Greenwalt12c44552009-12-07 11:33:18 -08001735 if (DBG) {
Robert Greenwalt24e2d2b2010-01-25 16:14:00 -08001736 // TODO - remove this after we validate the dropping doesn't break
1737 // anything
Wink Savillee70c6f52010-12-03 12:01:38 -08001738 log("Dropping ConnectivityChange for " +
Robert Greenwalt2adbc7f2010-01-13 09:36:31 -08001739 info.getTypeName() + ": " +
Robert Greenwalt12c44552009-12-07 11:33:18 -08001740 state + "/" + info.getDetailedState());
1741 }
1742 return;
1743 }
1744 mNetAttributes[type].mLastState = state;
1745
Wink Savillee70c6f52010-12-03 12:01:38 -08001746 if (DBG) log("ConnectivityChange for " +
Robert Greenwalt0659da32009-07-16 17:21:39 -07001747 info.getTypeName() + ": " +
Robert Greenwalt12c44552009-12-07 11:33:18 -08001748 state + "/" + info.getDetailedState());
The Android Open Source Project28527d22009-03-03 19:31:44 -08001749
1750 // Connectivity state changed:
1751 // [31-13] Reserved for future use
Robert Greenwalt0659da32009-07-16 17:21:39 -07001752 // [12-9] Network subtype (for mobile network, as defined
1753 // by TelephonyManager)
1754 // [8-3] Detailed state ordinal (as defined by
1755 // NetworkInfo.DetailedState)
The Android Open Source Project28527d22009-03-03 19:31:44 -08001756 // [2-0] Network type (as defined by ConnectivityManager)
1757 int eventLogParam = (info.getType() & 0x7) |
1758 ((info.getDetailedState().ordinal() & 0x3f) << 3) |
1759 (info.getSubtype() << 9);
Doug Zongker2fc96232009-12-04 10:31:43 -08001760 EventLog.writeEvent(EventLogTags.CONNECTIVITY_STATE_CHANGED,
Robert Greenwalt0659da32009-07-16 17:21:39 -07001761 eventLogParam);
1762
1763 if (info.getDetailedState() ==
1764 NetworkInfo.DetailedState.FAILED) {
The Android Open Source Project28527d22009-03-03 19:31:44 -08001765 handleConnectionFailure(info);
Robert Greenwalt12c44552009-12-07 11:33:18 -08001766 } else if (state == NetworkInfo.State.DISCONNECTED) {
The Android Open Source Project28527d22009-03-03 19:31:44 -08001767 handleDisconnect(info);
Robert Greenwalt12c44552009-12-07 11:33:18 -08001768 } else if (state == NetworkInfo.State.SUSPENDED) {
The Android Open Source Project28527d22009-03-03 19:31:44 -08001769 // TODO: need to think this over.
Robert Greenwalt0659da32009-07-16 17:21:39 -07001770 // the logic here is, handle SUSPENDED the same as
1771 // DISCONNECTED. The only difference being we are
1772 // broadcasting an intent with NetworkInfo that's
1773 // suspended. This allows the applications an
1774 // opportunity to handle DISCONNECTED and SUSPENDED
1775 // differently, or not.
The Android Open Source Project28527d22009-03-03 19:31:44 -08001776 handleDisconnect(info);
Robert Greenwalt12c44552009-12-07 11:33:18 -08001777 } else if (state == NetworkInfo.State.CONNECTED) {
The Android Open Source Project28527d22009-03-03 19:31:44 -08001778 handleConnect(info);
1779 }
The Android Open Source Project28527d22009-03-03 19:31:44 -08001780 break;
The Android Open Source Project28527d22009-03-03 19:31:44 -08001781 case NetworkStateTracker.EVENT_CONFIGURATION_CHANGED:
Robert Greenwalt3afbead2010-07-23 15:46:26 -07001782 info = (NetworkInfo) msg.obj;
1783 type = info.getType();
Robert Greenwaltc3c5f862010-10-11 16:00:27 -07001784 handleConnectivityChange(type);
The Android Open Source Project28527d22009-03-03 19:31:44 -08001785 break;
Robert Greenwaltccb36f92010-09-24 14:32:21 -07001786 case EVENT_CLEAR_NET_TRANSITION_WAKELOCK:
Robert Greenwalt93dc1042010-06-15 12:19:37 -07001787 String causedBy = null;
1788 synchronized (ConnectivityService.this) {
1789 if (msg.arg1 == mNetTransitionWakeLockSerialNumber &&
1790 mNetTransitionWakeLock.isHeld()) {
1791 mNetTransitionWakeLock.release();
1792 causedBy = mNetTransitionWakeLockCausedBy;
1793 }
1794 }
1795 if (causedBy != null) {
Wink Savillee70c6f52010-12-03 12:01:38 -08001796 log("NetTransition Wakelock for " + causedBy + " released by timeout");
Robert Greenwalt93dc1042010-06-15 12:19:37 -07001797 }
Robert Greenwaltcf1a56c2010-09-09 14:05:10 -07001798 break;
Robert Greenwalt6a2db8a2010-09-23 10:05:56 -07001799 case EVENT_RESTORE_DEFAULT_NETWORK:
Robert Greenwaltaffc3a12009-09-27 17:27:04 -07001800 FeatureUser u = (FeatureUser)msg.obj;
1801 u.expire();
Robert Greenwalt986c7412010-09-08 15:24:47 -07001802 break;
Robert Greenwalt6a2db8a2010-09-23 10:05:56 -07001803 case EVENT_INET_CONDITION_CHANGE:
1804 {
1805 int netType = msg.arg1;
1806 int condition = msg.arg2;
1807 handleInetConditionChange(netType, condition);
Robert Greenwalt93dc1042010-06-15 12:19:37 -07001808 break;
Robert Greenwalt6a2db8a2010-09-23 10:05:56 -07001809 }
1810 case EVENT_INET_CONDITION_HOLD_END:
1811 {
1812 int netType = msg.arg1;
1813 int sequence = msg.arg2;
1814 handleInetConditionHoldEnd(netType, sequence);
Robert Greenwalt986c7412010-09-08 15:24:47 -07001815 break;
Robert Greenwalt6a2db8a2010-09-23 10:05:56 -07001816 }
1817 case EVENT_SET_NETWORK_PREFERENCE:
1818 {
1819 int preference = msg.arg1;
1820 handleSetNetworkPreference(preference);
1821 break;
1822 }
1823 case EVENT_SET_BACKGROUND_DATA:
1824 {
1825 boolean enabled = (msg.arg1 == ENABLED);
1826 handleSetBackgroundData(enabled);
1827 break;
1828 }
1829 case EVENT_SET_MOBILE_DATA:
1830 {
1831 boolean enabled = (msg.arg1 == ENABLED);
1832 handleSetMobileData(enabled);
1833 break;
1834 }
Robert Greenwaltc3c5f862010-10-11 16:00:27 -07001835 case EVENT_APPLY_GLOBAL_HTTP_PROXY:
1836 {
1837 handleDeprecatedGlobalHttpProxy();
1838 }
The Android Open Source Project28527d22009-03-03 19:31:44 -08001839 }
1840 }
1841 }
Robert Greenwalt0c4828c2010-01-26 11:40:34 -08001842
1843 // javadoc from interface
Robert Greenwalt4283ded2010-03-02 17:25:02 -08001844 public int tether(String iface) {
Robert Greenwalt0c4828c2010-01-26 11:40:34 -08001845 enforceTetherChangePermission();
Robert Greenwalt4283ded2010-03-02 17:25:02 -08001846
1847 if (isTetheringSupported()) {
1848 return mTethering.tether(iface);
1849 } else {
1850 return ConnectivityManager.TETHER_ERROR_UNSUPPORTED;
1851 }
Robert Greenwalt0c4828c2010-01-26 11:40:34 -08001852 }
1853
1854 // javadoc from interface
Robert Greenwalt4283ded2010-03-02 17:25:02 -08001855 public int untether(String iface) {
Robert Greenwalt0c4828c2010-01-26 11:40:34 -08001856 enforceTetherChangePermission();
Robert Greenwalt4283ded2010-03-02 17:25:02 -08001857
1858 if (isTetheringSupported()) {
1859 return mTethering.untether(iface);
1860 } else {
1861 return ConnectivityManager.TETHER_ERROR_UNSUPPORTED;
1862 }
1863 }
1864
1865 // javadoc from interface
1866 public int getLastTetherError(String iface) {
1867 enforceTetherAccessPermission();
1868
1869 if (isTetheringSupported()) {
1870 return mTethering.getLastTetherError(iface);
1871 } else {
1872 return ConnectivityManager.TETHER_ERROR_UNSUPPORTED;
1873 }
Robert Greenwalt8e87f122010-02-11 18:18:40 -08001874 }
1875
1876 // TODO - proper iface API for selection by property, inspection, etc
1877 public String[] getTetherableUsbRegexs() {
1878 enforceTetherAccessPermission();
1879 if (isTetheringSupported()) {
1880 return mTethering.getTetherableUsbRegexs();
1881 } else {
1882 return new String[0];
1883 }
1884 }
1885
1886 public String[] getTetherableWifiRegexs() {
1887 enforceTetherAccessPermission();
1888 if (isTetheringSupported()) {
1889 return mTethering.getTetherableWifiRegexs();
1890 } else {
1891 return new String[0];
1892 }
Robert Greenwalt0c4828c2010-01-26 11:40:34 -08001893 }
1894
Danica Chang96567052010-08-11 14:54:43 -07001895 public String[] getTetherableBluetoothRegexs() {
1896 enforceTetherAccessPermission();
1897 if (isTetheringSupported()) {
1898 return mTethering.getTetherableBluetoothRegexs();
1899 } else {
1900 return new String[0];
1901 }
1902 }
1903
Robert Greenwalt0c4828c2010-01-26 11:40:34 -08001904 // TODO - move iface listing, queries, etc to new module
1905 // javadoc from interface
1906 public String[] getTetherableIfaces() {
Robert Greenwalt8e87f122010-02-11 18:18:40 -08001907 enforceTetherAccessPermission();
Robert Greenwalt0c4828c2010-01-26 11:40:34 -08001908 return mTethering.getTetherableIfaces();
1909 }
1910
1911 public String[] getTetheredIfaces() {
Robert Greenwalt8e87f122010-02-11 18:18:40 -08001912 enforceTetherAccessPermission();
Robert Greenwalt0c4828c2010-01-26 11:40:34 -08001913 return mTethering.getTetheredIfaces();
1914 }
Robert Greenwalt8e87f122010-02-11 18:18:40 -08001915
Robert Greenwalt4283ded2010-03-02 17:25:02 -08001916 public String[] getTetheringErroredIfaces() {
1917 enforceTetherAccessPermission();
1918 return mTethering.getErroredIfaces();
1919 }
1920
Robert Greenwalt8e87f122010-02-11 18:18:40 -08001921 // if ro.tether.denied = true we default to no tethering
1922 // gservices could set the secure setting to 1 though to enable it on a build where it
1923 // had previously been turned off.
1924 public boolean isTetheringSupported() {
1925 enforceTetherAccessPermission();
1926 int defaultVal = (SystemProperties.get("ro.tether.denied").equals("true") ? 0 : 1);
Robert Greenwaltf1b66e12010-02-25 12:29:30 -08001927 boolean tetherEnabledInSettings = (Settings.Secure.getInt(mContext.getContentResolver(),
1928 Settings.Secure.TETHER_SUPPORTED, defaultVal) != 0);
1929 return tetherEnabledInSettings && mTetheringConfigValid;
Robert Greenwalt8e87f122010-02-11 18:18:40 -08001930 }
Robert Greenwalt93dc1042010-06-15 12:19:37 -07001931
1932 // An API NetworkStateTrackers can call when they lose their network.
1933 // This will automatically be cleared after X seconds or a network becomes CONNECTED,
1934 // whichever happens first. The timer is started by the first caller and not
1935 // restarted by subsequent callers.
1936 public void requestNetworkTransitionWakelock(String forWhom) {
1937 enforceConnectivityInternalPermission();
1938 synchronized (this) {
1939 if (mNetTransitionWakeLock.isHeld()) return;
1940 mNetTransitionWakeLockSerialNumber++;
1941 mNetTransitionWakeLock.acquire();
1942 mNetTransitionWakeLockCausedBy = forWhom;
1943 }
1944 mHandler.sendMessageDelayed(mHandler.obtainMessage(
Robert Greenwaltccb36f92010-09-24 14:32:21 -07001945 EVENT_CLEAR_NET_TRANSITION_WAKELOCK,
Robert Greenwalt93dc1042010-06-15 12:19:37 -07001946 mNetTransitionWakeLockSerialNumber, 0),
1947 mNetTransitionWakeLockTimeout);
1948 return;
1949 }
Robert Greenwalt24118e82010-09-09 13:15:32 -07001950
Robert Greenwalt986c7412010-09-08 15:24:47 -07001951 // 100 percent is full good, 0 is full bad.
1952 public void reportInetCondition(int networkType, int percentage) {
Wink Savillee70c6f52010-12-03 12:01:38 -08001953 if (DBG) log("reportNetworkCondition(" + networkType + ", " + percentage + ")");
Robert Greenwalt986c7412010-09-08 15:24:47 -07001954 mContext.enforceCallingOrSelfPermission(
1955 android.Manifest.permission.STATUS_BAR,
1956 "ConnectivityService");
1957
Robert Greenwalt0e80be12010-09-20 14:35:25 -07001958 if (DBG) {
1959 int pid = getCallingPid();
1960 int uid = getCallingUid();
1961 String s = pid + "(" + uid + ") reports inet is " +
1962 (percentage > 50 ? "connected" : "disconnected") + " (" + percentage + ") on " +
1963 "network Type " + networkType + " at " + GregorianCalendar.getInstance().getTime();
1964 mInetLog.add(s);
1965 while(mInetLog.size() > INET_CONDITION_LOG_MAX_SIZE) {
1966 mInetLog.remove(0);
1967 }
1968 }
Robert Greenwalt986c7412010-09-08 15:24:47 -07001969 mHandler.sendMessage(mHandler.obtainMessage(
Robert Greenwalt6a2db8a2010-09-23 10:05:56 -07001970 EVENT_INET_CONDITION_CHANGE, networkType, percentage));
1971 }
1972
1973 private void handleInetConditionChange(int netType, int condition) {
1974 if (DBG) {
Wink Savillee70c6f52010-12-03 12:01:38 -08001975 log("Inet connectivity change, net=" +
Robert Greenwalt6a2db8a2010-09-23 10:05:56 -07001976 netType + ", condition=" + condition +
1977 ",mActiveDefaultNetwork=" + mActiveDefaultNetwork);
1978 }
1979 if (mActiveDefaultNetwork == -1) {
Wink Savillee70c6f52010-12-03 12:01:38 -08001980 if (DBG) log("no active default network - aborting");
Robert Greenwalt6a2db8a2010-09-23 10:05:56 -07001981 return;
1982 }
1983 if (mActiveDefaultNetwork != netType) {
Wink Savillee70c6f52010-12-03 12:01:38 -08001984 if (DBG) log("given net not default - aborting");
Robert Greenwalt6a2db8a2010-09-23 10:05:56 -07001985 return;
1986 }
1987 mDefaultInetCondition = condition;
1988 int delay;
1989 if (mInetConditionChangeInFlight == false) {
Wink Savillee70c6f52010-12-03 12:01:38 -08001990 if (DBG) log("starting a change hold");
Robert Greenwalt6a2db8a2010-09-23 10:05:56 -07001991 // setup a new hold to debounce this
1992 if (mDefaultInetCondition > 50) {
1993 delay = Settings.Secure.getInt(mContext.getContentResolver(),
1994 Settings.Secure.INET_CONDITION_DEBOUNCE_UP_DELAY, 500);
1995 } else {
1996 delay = Settings.Secure.getInt(mContext.getContentResolver(),
1997 Settings.Secure.INET_CONDITION_DEBOUNCE_DOWN_DELAY, 3000);
1998 }
1999 mInetConditionChangeInFlight = true;
2000 mHandler.sendMessageDelayed(mHandler.obtainMessage(EVENT_INET_CONDITION_HOLD_END,
2001 mActiveDefaultNetwork, mDefaultConnectionSequence), delay);
2002 } else {
2003 // we've set the new condition, when this hold ends that will get
2004 // picked up
Wink Savillee70c6f52010-12-03 12:01:38 -08002005 if (DBG) log("currently in hold - not setting new end evt");
Robert Greenwalt6a2db8a2010-09-23 10:05:56 -07002006 }
2007 }
2008
2009 private void handleInetConditionHoldEnd(int netType, int sequence) {
2010 if (DBG) {
Wink Savillee70c6f52010-12-03 12:01:38 -08002011 log("Inet hold end, net=" + netType +
Robert Greenwalt6a2db8a2010-09-23 10:05:56 -07002012 ", condition =" + mDefaultInetCondition +
2013 ", published condition =" + mDefaultInetConditionPublished);
2014 }
2015 mInetConditionChangeInFlight = false;
2016
2017 if (mActiveDefaultNetwork == -1) {
Wink Savillee70c6f52010-12-03 12:01:38 -08002018 if (DBG) log("no active default network - aborting");
Robert Greenwalt6a2db8a2010-09-23 10:05:56 -07002019 return;
2020 }
2021 if (mDefaultConnectionSequence != sequence) {
Wink Savillee70c6f52010-12-03 12:01:38 -08002022 if (DBG) log("event hold for obsolete network - aborting");
Robert Greenwalt6a2db8a2010-09-23 10:05:56 -07002023 return;
2024 }
2025 if (mDefaultInetConditionPublished == mDefaultInetCondition) {
Wink Savillee70c6f52010-12-03 12:01:38 -08002026 if (DBG) log("no change in condition - aborting");
Robert Greenwalt6a2db8a2010-09-23 10:05:56 -07002027 return;
2028 }
2029 NetworkInfo networkInfo = mNetTrackers[mActiveDefaultNetwork].getNetworkInfo();
2030 if (networkInfo.isConnected() == false) {
Wink Savillee70c6f52010-12-03 12:01:38 -08002031 if (DBG) log("default network not connected - aborting");
Robert Greenwalt6a2db8a2010-09-23 10:05:56 -07002032 return;
2033 }
2034 mDefaultInetConditionPublished = mDefaultInetCondition;
2035 sendInetConditionBroadcast(networkInfo);
2036 return;
Robert Greenwalt986c7412010-09-08 15:24:47 -07002037 }
Robert Greenwaltc3c5f862010-10-11 16:00:27 -07002038
2039 public synchronized ProxyProperties getProxy() {
2040 if (mGlobalProxy != null) return mGlobalProxy;
2041 if (mDefaultProxy != null) return mDefaultProxy;
2042 return null;
2043 }
2044
2045 public void setGlobalProxy(ProxyProperties proxyProperties) {
2046 enforceChangePermission();
2047 synchronized (mGlobalProxyLock) {
2048 if (proxyProperties == mGlobalProxy) return;
2049 if (proxyProperties != null && proxyProperties.equals(mGlobalProxy)) return;
2050 if (mGlobalProxy != null && mGlobalProxy.equals(proxyProperties)) return;
2051
2052 String host = "";
2053 int port = 0;
2054 String exclList = "";
2055 if (proxyProperties != null && !TextUtils.isEmpty(proxyProperties.getHost())) {
2056 mGlobalProxy = new ProxyProperties(proxyProperties);
2057 host = mGlobalProxy.getHost();
2058 port = mGlobalProxy.getPort();
2059 exclList = mGlobalProxy.getExclusionList();
2060 } else {
2061 mGlobalProxy = null;
2062 }
2063 ContentResolver res = mContext.getContentResolver();
2064 Settings.Secure.putString(res, Settings.Secure.GLOBAL_HTTP_PROXY_HOST, host);
2065 Settings.Secure.putInt(res, Settings.Secure.GLOBAL_HTTP_PROXY_PORT, port);
Robert Greenwalt6f7c6092010-12-02 11:31:00 -08002066 Settings.Secure.putString(res, Settings.Secure.GLOBAL_HTTP_PROXY_EXCLUSION_LIST,
Robert Greenwaltc3c5f862010-10-11 16:00:27 -07002067 exclList);
2068 }
2069
2070 if (mGlobalProxy == null) {
2071 proxyProperties = mDefaultProxy;
2072 }
2073 sendProxyBroadcast(proxyProperties);
2074 }
2075
Robert Greenwalt6f7c6092010-12-02 11:31:00 -08002076 private void loadGlobalProxy() {
2077 ContentResolver res = mContext.getContentResolver();
2078 String host = Settings.Secure.getString(res, Settings.Secure.GLOBAL_HTTP_PROXY_HOST);
2079 int port = Settings.Secure.getInt(res, Settings.Secure.GLOBAL_HTTP_PROXY_PORT, 0);
2080 String exclList = Settings.Secure.getString(res,
2081 Settings.Secure.GLOBAL_HTTP_PROXY_EXCLUSION_LIST);
2082 if (!TextUtils.isEmpty(host)) {
2083 ProxyProperties proxyProperties = new ProxyProperties(host, port, exclList);
2084 synchronized (mGlobalProxyLock) {
2085 mGlobalProxy = proxyProperties;
2086 }
2087 }
2088 }
2089
Robert Greenwaltc3c5f862010-10-11 16:00:27 -07002090 public ProxyProperties getGlobalProxy() {
2091 synchronized (mGlobalProxyLock) {
2092 return mGlobalProxy;
2093 }
2094 }
2095
2096 private void handleApplyDefaultProxy(int type) {
2097 // check if new default - push it out to all VM if so
2098 ProxyProperties proxy = mNetTrackers[type].getLinkProperties().getHttpProxy();
2099 synchronized (this) {
2100 if (mDefaultProxy != null && mDefaultProxy.equals(proxy)) return;
2101 if (mDefaultProxy == proxy) return;
2102 if (!TextUtils.isEmpty(proxy.getHost())) {
2103 mDefaultProxy = proxy;
2104 } else {
2105 mDefaultProxy = null;
2106 }
2107 }
Wink Savillee70c6f52010-12-03 12:01:38 -08002108 if (DBG) log("changing default proxy to " + proxy);
Robert Greenwaltc3c5f862010-10-11 16:00:27 -07002109 if ((proxy == null && mGlobalProxy == null) || proxy.equals(mGlobalProxy)) return;
2110 if (mGlobalProxy != null) return;
2111 sendProxyBroadcast(proxy);
2112 }
2113
2114 private void handleDeprecatedGlobalHttpProxy() {
2115 String proxy = Settings.Secure.getString(mContext.getContentResolver(),
2116 Settings.Secure.HTTP_PROXY);
2117 if (!TextUtils.isEmpty(proxy)) {
2118 String data[] = proxy.split(":");
2119 String proxyHost = data[0];
2120 int proxyPort = 8080;
2121 if (data.length > 1) {
2122 try {
2123 proxyPort = Integer.parseInt(data[1]);
2124 } catch (NumberFormatException e) {
2125 return;
2126 }
2127 }
2128 ProxyProperties p = new ProxyProperties(data[0], proxyPort, "");
2129 setGlobalProxy(p);
2130 }
2131 }
2132
2133 private void sendProxyBroadcast(ProxyProperties proxy) {
Wink Savillee70c6f52010-12-03 12:01:38 -08002134 log("sending Proxy Broadcast for " + proxy);
Robert Greenwaltc3c5f862010-10-11 16:00:27 -07002135 Intent intent = new Intent(Proxy.PROXY_CHANGE_ACTION);
2136 intent.addFlags(Intent.FLAG_RECEIVER_REPLACE_PENDING);
2137 intent.putExtra(Proxy.EXTRA_PROXY_INFO, proxy);
Robert Greenwaltd93dc8f2010-12-06 11:29:17 -08002138 mContext.sendStickyBroadcast(intent);
Robert Greenwaltc3c5f862010-10-11 16:00:27 -07002139 }
2140
2141 private static class SettingsObserver extends ContentObserver {
2142 private int mWhat;
2143 private Handler mHandler;
2144 SettingsObserver(Handler handler, int what) {
2145 super(handler);
2146 mHandler = handler;
2147 mWhat = what;
2148 }
2149
2150 void observe(Context context) {
2151 ContentResolver resolver = context.getContentResolver();
2152 resolver.registerContentObserver(Settings.Secure.getUriFor(
2153 Settings.Secure.HTTP_PROXY), false, this);
2154 }
2155
2156 @Override
2157 public void onChange(boolean selfChange) {
2158 mHandler.obtainMessage(mWhat).sendToTarget();
2159 }
2160 }
Wink Savillee70c6f52010-12-03 12:01:38 -08002161
2162 private void log(String s) {
2163 Slog.d(TAG, s);
2164 }
2165
2166 private void loge(String s) {
2167 Slog.e(TAG, s);
2168 }
The Android Open Source Project28527d22009-03-03 19:31:44 -08002169}