blob: 3e2eac284ebc9ee16c6e664ff101acb2775f04d6 [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;
Robert Greenwaltea8bca02010-12-21 11:43:28 -08001100 intent.putExtra(ConnectivityManager.EXTRA_NO_CONNECTIVITY, true);
Robert Greenwaltb2a9e492010-09-20 18:01:43 -07001101 }
Robert Greenwalt3cc68d32010-01-25 17:54:29 -08001102 intent.putExtra(ConnectivityManager.EXTRA_OTHER_NETWORK_INFO, switchTo);
1103 } else {
Robert Greenwaltb2a9e492010-09-20 18:01:43 -07001104 mDefaultInetConditionPublished = 0; // we're not connected anymore
Robert Greenwalt3cc68d32010-01-25 17:54:29 -08001105 intent.putExtra(ConnectivityManager.EXTRA_NO_CONNECTIVITY, true);
1106 }
Robert Greenwaltf55ced92010-01-20 19:29:41 -08001107 }
Robert Greenwaltb2a9e492010-09-20 18:01:43 -07001108 intent.putExtra(ConnectivityManager.EXTRA_INET_CONDITION, mDefaultInetConditionPublished);
Robert Greenwaltf55ced92010-01-20 19:29:41 -08001109 // do this before we broadcast the change
Robert Greenwalt3afbead2010-07-23 15:46:26 -07001110 handleConnectivityChange(prevNetType);
Robert Greenwaltf55ced92010-01-20 19:29:41 -08001111
1112 sendStickyBroadcast(intent);
1113 /*
1114 * If the failover network is already connected, then immediately send
1115 * out a followup broadcast indicating successful failover
1116 */
1117 if (newNet != null && newNet.getNetworkInfo().isConnected()) {
1118 sendConnectedBroadcast(newNet.getNetworkInfo());
1119 }
1120 }
1121
Robert Greenwalt3cc68d32010-01-25 17:54:29 -08001122 // returns null if no failover available
Robert Greenwaltf55ced92010-01-20 19:29:41 -08001123 private NetworkStateTracker tryFailover(int prevNetType) {
Robert Greenwalt2034b912009-08-12 16:08:25 -07001124 /*
1125 * If this is a default network, check if other defaults are available
1126 * or active
1127 */
1128 NetworkStateTracker newNet = null;
1129 if (mNetAttributes[prevNetType].isDefault()) {
Robert Greenwalt2034b912009-08-12 16:08:25 -07001130 if (mActiveDefaultNetwork == prevNetType) {
1131 mActiveDefaultNetwork = -1;
1132 }
1133
1134 int newType = -1;
1135 int newPriority = -1;
Robert Greenwaltf55ced92010-01-20 19:29:41 -08001136 for (int checkType=0; checkType <= ConnectivityManager.MAX_NETWORK_TYPE; checkType++) {
Robert Greenwaltec05b3c2009-10-30 14:17:42 -07001137 if (checkType == prevNetType) continue;
1138 if (mNetAttributes[checkType] == null) continue;
Robert Greenwalt2034b912009-08-12 16:08:25 -07001139 if (mNetAttributes[checkType].isDefault()) {
1140 /* TODO - if we have multiple nets we could use
1141 * we may want to put more thought into which we choose
1142 */
1143 if (checkType == mNetworkPreference) {
1144 newType = checkType;
1145 break;
1146 }
Robert Greenwaltec05b3c2009-10-30 14:17:42 -07001147 if (mNetAttributes[checkType].mPriority > newPriority) {
Robert Greenwalt2034b912009-08-12 16:08:25 -07001148 newType = checkType;
Robert Greenwaltec05b3c2009-10-30 14:17:42 -07001149 newPriority = mNetAttributes[newType].mPriority;
Robert Greenwalt2034b912009-08-12 16:08:25 -07001150 }
The Android Open Source Project28527d22009-03-03 19:31:44 -08001151 }
1152 }
Robert Greenwalt2034b912009-08-12 16:08:25 -07001153
1154 if (newType != -1) {
1155 newNet = mNetTrackers[newType];
1156 /**
1157 * See if the other network is available to fail over to.
1158 * If is not available, we enable it anyway, so that it
1159 * will be able to connect when it does become available,
1160 * but we report a total loss of connectivity rather than
1161 * report that we are attempting to fail over.
1162 */
1163 if (newNet.isAvailable()) {
1164 NetworkInfo switchTo = newNet.getNetworkInfo();
1165 switchTo.setFailover(true);
Robert Greenwalta52c75a2009-08-19 20:19:33 -07001166 if (!switchTo.isConnectedOrConnecting() ||
1167 newNet.isTeardownRequested()) {
Robert Greenwalt2034b912009-08-12 16:08:25 -07001168 newNet.reconnect();
1169 }
1170 if (DBG) {
1171 if (switchTo.isConnected()) {
Wink Savillee70c6f52010-12-03 12:01:38 -08001172 log("Switching to already connected " + switchTo.getTypeName());
Robert Greenwalt2034b912009-08-12 16:08:25 -07001173 } else {
Wink Savillee70c6f52010-12-03 12:01:38 -08001174 log("Attempting to switch to " + switchTo.getTypeName());
Robert Greenwalt2034b912009-08-12 16:08:25 -07001175 }
1176 }
Robert Greenwalt2034b912009-08-12 16:08:25 -07001177 } else {
1178 newNet.reconnect();
Robert Greenwalt12984322010-03-09 14:55:08 -08001179 newNet = null; // not officially avail.. try anyway, but
1180 // report no failover
Robert Greenwalt2034b912009-08-12 16:08:25 -07001181 }
Robert Greenwalte981bc52010-10-08 16:35:52 -07001182 } else {
Wink Savillee70c6f52010-12-03 12:01:38 -08001183 loge("Network failover failing.");
Robert Greenwalt2034b912009-08-12 16:08:25 -07001184 }
The Android Open Source Project28527d22009-03-03 19:31:44 -08001185 }
Robert Greenwalt2034b912009-08-12 16:08:25 -07001186
Robert Greenwaltf55ced92010-01-20 19:29:41 -08001187 return newNet;
The Android Open Source Project28527d22009-03-03 19:31:44 -08001188 }
1189
1190 private void sendConnectedBroadcast(NetworkInfo info) {
Robert Greenwaltd3401f92010-09-15 17:36:33 -07001191 sendGeneralBroadcast(info, ConnectivityManager.CONNECTIVITY_ACTION);
1192 }
1193
1194 private void sendInetConditionBroadcast(NetworkInfo info) {
1195 sendGeneralBroadcast(info, ConnectivityManager.INET_CONDITION_ACTION);
1196 }
1197
1198 private void sendGeneralBroadcast(NetworkInfo info, String bcastType) {
1199 Intent intent = new Intent(bcastType);
The Android Open Source Project28527d22009-03-03 19:31:44 -08001200 intent.putExtra(ConnectivityManager.EXTRA_NETWORK_INFO, info);
1201 if (info.isFailover()) {
1202 intent.putExtra(ConnectivityManager.EXTRA_IS_FAILOVER, true);
1203 info.setFailover(false);
1204 }
1205 if (info.getReason() != null) {
1206 intent.putExtra(ConnectivityManager.EXTRA_REASON, info.getReason());
1207 }
1208 if (info.getExtraInfo() != null) {
Robert Greenwalt0659da32009-07-16 17:21:39 -07001209 intent.putExtra(ConnectivityManager.EXTRA_EXTRA_INFO,
1210 info.getExtraInfo());
The Android Open Source Project28527d22009-03-03 19:31:44 -08001211 }
Robert Greenwalt986c7412010-09-08 15:24:47 -07001212 intent.putExtra(ConnectivityManager.EXTRA_INET_CONDITION, mDefaultInetConditionPublished);
Mike Lockwoodfde2b762009-08-14 14:18:49 -04001213 sendStickyBroadcast(intent);
The Android Open Source Project28527d22009-03-03 19:31:44 -08001214 }
1215
1216 /**
1217 * Called when an attempt to fail over to another network has failed.
1218 * @param info the {@link NetworkInfo} for the failed network
1219 */
1220 private void handleConnectionFailure(NetworkInfo info) {
1221 mNetTrackers[info.getType()].setTeardownRequested(false);
The Android Open Source Project28527d22009-03-03 19:31:44 -08001222
Robert Greenwalt2034b912009-08-12 16:08:25 -07001223 String reason = info.getReason();
1224 String extraInfo = info.getExtraInfo();
Robert Greenwalt0659da32009-07-16 17:21:39 -07001225
Robert Greenwalte981bc52010-10-08 16:35:52 -07001226 String reasonText;
1227 if (reason == null) {
1228 reasonText = ".";
1229 } else {
1230 reasonText = " (" + reason + ").";
The Android Open Source Project28527d22009-03-03 19:31:44 -08001231 }
Wink Savillee70c6f52010-12-03 12:01:38 -08001232 loge("Attempt to connect to " + info.getTypeName() + " failed" + reasonText);
Robert Greenwalt2034b912009-08-12 16:08:25 -07001233
1234 Intent intent = new Intent(ConnectivityManager.CONNECTIVITY_ACTION);
1235 intent.putExtra(ConnectivityManager.EXTRA_NETWORK_INFO, info);
1236 if (getActiveNetworkInfo() == null) {
1237 intent.putExtra(ConnectivityManager.EXTRA_NO_CONNECTIVITY, true);
1238 }
1239 if (reason != null) {
1240 intent.putExtra(ConnectivityManager.EXTRA_REASON, reason);
1241 }
1242 if (extraInfo != null) {
1243 intent.putExtra(ConnectivityManager.EXTRA_EXTRA_INFO, extraInfo);
1244 }
1245 if (info.isFailover()) {
1246 intent.putExtra(ConnectivityManager.EXTRA_IS_FAILOVER, true);
1247 info.setFailover(false);
1248 }
Robert Greenwaltf55ced92010-01-20 19:29:41 -08001249
Robert Greenwalt3cc68d32010-01-25 17:54:29 -08001250 NetworkStateTracker newNet = null;
1251 if (mNetAttributes[info.getType()].isDefault()) {
1252 newNet = tryFailover(info.getType());
1253 if (newNet != null) {
1254 NetworkInfo switchTo = newNet.getNetworkInfo();
Robert Greenwaltb2a9e492010-09-20 18:01:43 -07001255 if (!switchTo.isConnected()) {
Robert Greenwalte981bc52010-10-08 16:35:52 -07001256 // if the other net is connected they've already reset this and perhaps
1257 // even gotten a positive report we don't want to overwrite, but if not
1258 // we need to clear this now to turn our cellular sig strength white
Robert Greenwaltb2a9e492010-09-20 18:01:43 -07001259 mDefaultInetConditionPublished = 0;
1260 }
Robert Greenwalt3cc68d32010-01-25 17:54:29 -08001261 intent.putExtra(ConnectivityManager.EXTRA_OTHER_NETWORK_INFO, switchTo);
1262 } else {
Robert Greenwaltb2a9e492010-09-20 18:01:43 -07001263 mDefaultInetConditionPublished = 0;
Robert Greenwalt3cc68d32010-01-25 17:54:29 -08001264 intent.putExtra(ConnectivityManager.EXTRA_NO_CONNECTIVITY, true);
1265 }
Robert Greenwaltf55ced92010-01-20 19:29:41 -08001266 }
Robert Greenwalt3cc68d32010-01-25 17:54:29 -08001267
Robert Greenwaltb2a9e492010-09-20 18:01:43 -07001268 intent.putExtra(ConnectivityManager.EXTRA_INET_CONDITION, mDefaultInetConditionPublished);
Mike Lockwoodfde2b762009-08-14 14:18:49 -04001269 sendStickyBroadcast(intent);
Robert Greenwaltf55ced92010-01-20 19:29:41 -08001270 /*
1271 * If the failover network is already connected, then immediately send
1272 * out a followup broadcast indicating successful failover
1273 */
1274 if (newNet != null && newNet.getNetworkInfo().isConnected()) {
1275 sendConnectedBroadcast(newNet.getNetworkInfo());
1276 }
Mike Lockwoodfde2b762009-08-14 14:18:49 -04001277 }
1278
1279 private void sendStickyBroadcast(Intent intent) {
1280 synchronized(this) {
Dianne Hackborna417ff82009-12-08 19:45:14 -08001281 if (!mSystemReady) {
1282 mInitialBroadcast = new Intent(intent);
Mike Lockwoodfde2b762009-08-14 14:18:49 -04001283 }
Dianne Hackborna417ff82009-12-08 19:45:14 -08001284 intent.addFlags(Intent.FLAG_RECEIVER_REGISTERED_ONLY_BEFORE_BOOT);
1285 mContext.sendStickyBroadcast(intent);
Mike Lockwoodfde2b762009-08-14 14:18:49 -04001286 }
1287 }
1288
1289 void systemReady() {
1290 synchronized(this) {
1291 mSystemReady = true;
Dianne Hackborna417ff82009-12-08 19:45:14 -08001292 if (mInitialBroadcast != null) {
1293 mContext.sendStickyBroadcast(mInitialBroadcast);
1294 mInitialBroadcast = null;
Mike Lockwoodfde2b762009-08-14 14:18:49 -04001295 }
1296 }
Robert Greenwaltc3c5f862010-10-11 16:00:27 -07001297 // load the global proxy at startup
1298 mHandler.sendMessage(mHandler.obtainMessage(EVENT_APPLY_GLOBAL_HTTP_PROXY));
The Android Open Source Project28527d22009-03-03 19:31:44 -08001299 }
1300
1301 private void handleConnect(NetworkInfo info) {
Robert Greenwalt2034b912009-08-12 16:08:25 -07001302 int type = info.getType();
The Android Open Source Project28527d22009-03-03 19:31:44 -08001303
1304 // snapshot isFailover, because sendConnectedBroadcast() resets it
1305 boolean isFailover = info.isFailover();
Robert Greenwalt2034b912009-08-12 16:08:25 -07001306 NetworkStateTracker thisNet = mNetTrackers[type];
The Android Open Source Project28527d22009-03-03 19:31:44 -08001307
Robert Greenwalt2034b912009-08-12 16:08:25 -07001308 // if this is a default net and other default is running
1309 // kill the one not preferred
1310 if (mNetAttributes[type].isDefault()) {
Robert Greenwalt2034b912009-08-12 16:08:25 -07001311 if (mActiveDefaultNetwork != -1 && mActiveDefaultNetwork != type) {
1312 if ((type != mNetworkPreference &&
1313 mNetAttributes[mActiveDefaultNetwork].mPriority >
1314 mNetAttributes[type].mPriority) ||
1315 mNetworkPreference == mActiveDefaultNetwork) {
1316 // don't accept this one
Wink Savillee70c6f52010-12-03 12:01:38 -08001317 if (DBG) {
1318 log("Not broadcasting CONNECT_ACTION " +
Robert Greenwalt2034b912009-08-12 16:08:25 -07001319 "to torn down network " + info.getTypeName());
Wink Savillee70c6f52010-12-03 12:01:38 -08001320 }
Robert Greenwalt2034b912009-08-12 16:08:25 -07001321 teardown(thisNet);
1322 return;
1323 } else {
1324 // tear down the other
1325 NetworkStateTracker otherNet =
1326 mNetTrackers[mActiveDefaultNetwork];
Wink Savillee70c6f52010-12-03 12:01:38 -08001327 if (DBG) {
1328 log("Policy requires " + otherNet.getNetworkInfo().getTypeName() +
Robert Greenwalt2034b912009-08-12 16:08:25 -07001329 " teardown");
Wink Savillee70c6f52010-12-03 12:01:38 -08001330 }
Robert Greenwalt2034b912009-08-12 16:08:25 -07001331 if (!teardown(otherNet)) {
Wink Savillee70c6f52010-12-03 12:01:38 -08001332 loge("Network declined teardown request");
Robert Greenwalt2034b912009-08-12 16:08:25 -07001333 return;
1334 }
Robert Greenwalt93dc1042010-06-15 12:19:37 -07001335 }
1336 }
1337 synchronized (ConnectivityService.this) {
1338 // have a new default network, release the transition wakelock in a second
1339 // if it's held. The second pause is to allow apps to reconnect over the
1340 // new network
1341 if (mNetTransitionWakeLock.isHeld()) {
1342 mHandler.sendMessageDelayed(mHandler.obtainMessage(
Robert Greenwaltccb36f92010-09-24 14:32:21 -07001343 EVENT_CLEAR_NET_TRANSITION_WAKELOCK,
Robert Greenwalt93dc1042010-06-15 12:19:37 -07001344 mNetTransitionWakeLockSerialNumber, 0),
1345 1000);
Robert Greenwalt2034b912009-08-12 16:08:25 -07001346 }
1347 }
1348 mActiveDefaultNetwork = type;
Robert Greenwalt986c7412010-09-08 15:24:47 -07001349 // this will cause us to come up initially as unconnected and switching
1350 // to connected after our normal pause unless somebody reports us as reall
1351 // disconnected
1352 mDefaultInetConditionPublished = 0;
1353 mDefaultConnectionSequence++;
1354 mInetConditionChangeInFlight = false;
1355 // Don't do this - if we never sign in stay, grey
1356 //reportNetworkCondition(mActiveDefaultNetwork, 100);
Robert Greenwalt2034b912009-08-12 16:08:25 -07001357 }
The Android Open Source Project28527d22009-03-03 19:31:44 -08001358 thisNet.setTeardownRequested(false);
Irfan Sheriff7f132d92010-06-09 15:39:36 -07001359 updateNetworkSettings(thisNet);
Robert Greenwalt3afbead2010-07-23 15:46:26 -07001360 handleConnectivityChange(type);
Robert Greenwalt2034b912009-08-12 16:08:25 -07001361 sendConnectedBroadcast(info);
The Android Open Source Project28527d22009-03-03 19:31:44 -08001362 }
1363
The Android Open Source Project28527d22009-03-03 19:31:44 -08001364 /**
Robert Greenwalt3afbead2010-07-23 15:46:26 -07001365 * After a change in the connectivity state of a network. We're mainly
1366 * concerned with making sure that the list of DNS servers is set up
1367 * according to which networks are connected, and ensuring that the
1368 * right routing table entries exist.
The Android Open Source Project28527d22009-03-03 19:31:44 -08001369 */
Robert Greenwalt3afbead2010-07-23 15:46:26 -07001370 private void handleConnectivityChange(int netType) {
The Android Open Source Project28527d22009-03-03 19:31:44 -08001371 /*
Robert Greenwalt2034b912009-08-12 16:08:25 -07001372 * If a non-default network is enabled, add the host routes that
Robert Greenwalt3afbead2010-07-23 15:46:26 -07001373 * will allow it's DNS servers to be accessed.
The Android Open Source Project28527d22009-03-03 19:31:44 -08001374 */
Robert Greenwalt3afbead2010-07-23 15:46:26 -07001375 handleDnsConfigurationChange(netType);
Robert Greenwalt2034b912009-08-12 16:08:25 -07001376
Robert Greenwalt3afbead2010-07-23 15:46:26 -07001377 if (mNetTrackers[netType].getNetworkInfo().isConnected()) {
1378 if (mNetAttributes[netType].isDefault()) {
Robert Greenwaltc3c5f862010-10-11 16:00:27 -07001379 handleApplyDefaultProxy(netType);
Robert Greenwalt3afbead2010-07-23 15:46:26 -07001380 addDefaultRoute(mNetTrackers[netType]);
Robert Greenwalt2034b912009-08-12 16:08:25 -07001381 } else {
Robert Greenwalt3afbead2010-07-23 15:46:26 -07001382 addPrivateDnsRoutes(mNetTrackers[netType]);
1383 }
1384 } else {
1385 if (mNetAttributes[netType].isDefault()) {
1386 removeDefaultRoute(mNetTrackers[netType]);
1387 } else {
1388 removePrivateDnsRoutes(mNetTrackers[netType]);
Robert Greenwalt2034b912009-08-12 16:08:25 -07001389 }
The Android Open Source Project28527d22009-03-03 19:31:44 -08001390 }
1391 }
1392
Irfan Sheriff7f132d92010-06-09 15:39:36 -07001393 private void addPrivateDnsRoutes(NetworkStateTracker nt) {
Irfan Sheriff7f132d92010-06-09 15:39:36 -07001394 boolean privateDnsRouteSet = nt.isPrivateDnsRouteSet();
Robert Greenwalt1f1bcfe2010-08-30 10:56:47 -07001395 LinkProperties p = nt.getLinkProperties();
Robert Greenwalta7dfbd32010-06-15 15:43:39 -07001396 if (p == null) return;
1397 String interfaceName = p.getInterfaceName();
Irfan Sheriff7f132d92010-06-09 15:39:36 -07001398
1399 if (DBG) {
Wink Savillee70c6f52010-12-03 12:01:38 -08001400 log("addPrivateDnsRoutes for " + nt +
Irfan Sheriff7f132d92010-06-09 15:39:36 -07001401 "(" + interfaceName + ") - mPrivateDnsRouteSet = " + privateDnsRouteSet);
1402 }
Irfan Sheriff7f132d92010-06-09 15:39:36 -07001403 if (interfaceName != null && !privateDnsRouteSet) {
Robert Greenwalta7dfbd32010-06-15 15:43:39 -07001404 Collection<InetAddress> dnsList = p.getDnses();
1405 for (InetAddress dns : dnsList) {
Wink Savillee70c6f52010-12-03 12:01:38 -08001406 if (DBG) log(" adding " + dns);
Robert Greenwalt7fe44cb2010-08-27 09:24:29 -07001407 NetworkUtils.addHostRoute(interfaceName, dns, null);
Irfan Sheriff7f132d92010-06-09 15:39:36 -07001408 }
1409 nt.privateDnsRouteSet(true);
1410 }
1411 }
1412
1413 private void removePrivateDnsRoutes(NetworkStateTracker nt) {
1414 // TODO - we should do this explicitly but the NetUtils api doesnt
1415 // support this yet - must remove all. No worse than before
Robert Greenwalt1f1bcfe2010-08-30 10:56:47 -07001416 LinkProperties p = nt.getLinkProperties();
Robert Greenwalta7dfbd32010-06-15 15:43:39 -07001417 if (p == null) return;
1418 String interfaceName = p.getInterfaceName();
Irfan Sheriff7f132d92010-06-09 15:39:36 -07001419 boolean privateDnsRouteSet = nt.isPrivateDnsRouteSet();
1420 if (interfaceName != null && privateDnsRouteSet) {
1421 if (DBG) {
Wink Savillee70c6f52010-12-03 12:01:38 -08001422 log("removePrivateDnsRoutes for " + nt.getNetworkInfo().getTypeName() +
Irfan Sheriff7f132d92010-06-09 15:39:36 -07001423 " (" + interfaceName + ")");
1424 }
1425 NetworkUtils.removeHostRoutes(interfaceName);
1426 nt.privateDnsRouteSet(false);
1427 }
1428 }
1429
Irfan Sheriff7f132d92010-06-09 15:39:36 -07001430
1431 private void addDefaultRoute(NetworkStateTracker nt) {
Robert Greenwalt1f1bcfe2010-08-30 10:56:47 -07001432 LinkProperties p = nt.getLinkProperties();
Robert Greenwalta7dfbd32010-06-15 15:43:39 -07001433 if (p == null) return;
1434 String interfaceName = p.getInterfaceName();
1435 InetAddress defaultGatewayAddr = p.getGateway();
Irfan Sheriff7f132d92010-06-09 15:39:36 -07001436
Robert Greenwalt3afbead2010-07-23 15:46:26 -07001437 if ((interfaceName != null) && (defaultGatewayAddr != null )) {
Robert Greenwalt7fe44cb2010-08-27 09:24:29 -07001438 if (!NetworkUtils.addDefaultRoute(interfaceName, defaultGatewayAddr) && DBG) {
Robert Greenwalta7dfbd32010-06-15 15:43:39 -07001439 NetworkInfo networkInfo = nt.getNetworkInfo();
Wink Savillee70c6f52010-12-03 12:01:38 -08001440 log("addDefaultRoute for " + networkInfo.getTypeName() +
Irfan Sheriff7f132d92010-06-09 15:39:36 -07001441 " (" + interfaceName + "), GatewayAddr=" + defaultGatewayAddr);
1442 }
Irfan Sheriff7f132d92010-06-09 15:39:36 -07001443 }
1444 }
1445
1446
1447 public void removeDefaultRoute(NetworkStateTracker nt) {
Robert Greenwalt1f1bcfe2010-08-30 10:56:47 -07001448 LinkProperties p = nt.getLinkProperties();
Robert Greenwalta7dfbd32010-06-15 15:43:39 -07001449 if (p == null) return;
1450 String interfaceName = p.getInterfaceName();
Irfan Sheriff7f132d92010-06-09 15:39:36 -07001451
Robert Greenwalt3afbead2010-07-23 15:46:26 -07001452 if (interfaceName != null) {
1453 if ((NetworkUtils.removeDefaultRoute(interfaceName) >= 0) && DBG) {
Robert Greenwalta7dfbd32010-06-15 15:43:39 -07001454 NetworkInfo networkInfo = nt.getNetworkInfo();
Wink Savillee70c6f52010-12-03 12:01:38 -08001455 log("removeDefaultRoute for " + networkInfo.getTypeName() + " (" +
Irfan Sheriff7f132d92010-06-09 15:39:36 -07001456 interfaceName + ")");
1457 }
Irfan Sheriff7f132d92010-06-09 15:39:36 -07001458 }
1459 }
1460
1461 /**
1462 * Reads the network specific TCP buffer sizes from SystemProperties
1463 * net.tcp.buffersize.[default|wifi|umts|edge|gprs] and set them for system
1464 * wide use
1465 */
1466 public void updateNetworkSettings(NetworkStateTracker nt) {
1467 String key = nt.getTcpBufferSizesPropName();
1468 String bufferSizes = SystemProperties.get(key);
1469
1470 if (bufferSizes.length() == 0) {
Wink Savillee70c6f52010-12-03 12:01:38 -08001471 loge(key + " not found in system properties. Using defaults");
Irfan Sheriff7f132d92010-06-09 15:39:36 -07001472
1473 // Setting to default values so we won't be stuck to previous values
1474 key = "net.tcp.buffersize.default";
1475 bufferSizes = SystemProperties.get(key);
1476 }
1477
1478 // Set values in kernel
1479 if (bufferSizes.length() != 0) {
1480 if (DBG) {
Wink Savillee70c6f52010-12-03 12:01:38 -08001481 log("Setting TCP values: [" + bufferSizes
Irfan Sheriff7f132d92010-06-09 15:39:36 -07001482 + "] which comes from [" + key + "]");
1483 }
1484 setBufferSize(bufferSizes);
1485 }
1486 }
1487
1488 /**
1489 * Writes TCP buffer sizes to /sys/kernel/ipv4/tcp_[r/w]mem_[min/def/max]
1490 * which maps to /proc/sys/net/ipv4/tcp_rmem and tcpwmem
1491 *
1492 * @param bufferSizes in the format of "readMin, readInitial, readMax,
1493 * writeMin, writeInitial, writeMax"
1494 */
1495 private void setBufferSize(String bufferSizes) {
1496 try {
1497 String[] values = bufferSizes.split(",");
1498
1499 if (values.length == 6) {
1500 final String prefix = "/sys/kernel/ipv4/tcp_";
1501 stringToFile(prefix + "rmem_min", values[0]);
1502 stringToFile(prefix + "rmem_def", values[1]);
1503 stringToFile(prefix + "rmem_max", values[2]);
1504 stringToFile(prefix + "wmem_min", values[3]);
1505 stringToFile(prefix + "wmem_def", values[4]);
1506 stringToFile(prefix + "wmem_max", values[5]);
1507 } else {
Wink Savillee70c6f52010-12-03 12:01:38 -08001508 loge("Invalid buffersize string: " + bufferSizes);
Irfan Sheriff7f132d92010-06-09 15:39:36 -07001509 }
1510 } catch (IOException e) {
Wink Savillee70c6f52010-12-03 12:01:38 -08001511 loge("Can't set tcp buffer sizes:" + e);
Irfan Sheriff7f132d92010-06-09 15:39:36 -07001512 }
1513 }
1514
1515 /**
1516 * Writes string to file. Basically same as "echo -n $string > $filename"
1517 *
1518 * @param filename
1519 * @param string
1520 * @throws IOException
1521 */
1522 private void stringToFile(String filename, String string) throws IOException {
1523 FileWriter out = new FileWriter(filename);
1524 try {
1525 out.write(string);
1526 } finally {
1527 out.close();
1528 }
1529 }
1530
1531
Robert Greenwalt2034b912009-08-12 16:08:25 -07001532 /**
1533 * Adjust the per-process dns entries (net.dns<x>.<pid>) based
1534 * on the highest priority active net which this process requested.
1535 * If there aren't any, clear it out
1536 */
1537 private void reassessPidDns(int myPid, boolean doBump)
1538 {
Wink Savillee70c6f52010-12-03 12:01:38 -08001539 if (DBG) log("reassessPidDns for pid " + myPid);
Robert Greenwalt2034b912009-08-12 16:08:25 -07001540 for(int i : mPriorityList) {
1541 if (mNetAttributes[i].isDefault()) {
1542 continue;
1543 }
1544 NetworkStateTracker nt = mNetTrackers[i];
Robert Greenwalt0659da32009-07-16 17:21:39 -07001545 if (nt.getNetworkInfo().isConnected() &&
1546 !nt.isTeardownRequested()) {
Robert Greenwalt1f1bcfe2010-08-30 10:56:47 -07001547 LinkProperties p = nt.getLinkProperties();
Robert Greenwalta7dfbd32010-06-15 15:43:39 -07001548 if (p == null) continue;
Robert Greenwalt2034b912009-08-12 16:08:25 -07001549 List pids = mNetRequestersPids[i];
1550 for (int j=0; j<pids.size(); j++) {
1551 Integer pid = (Integer)pids.get(j);
1552 if (pid.intValue() == myPid) {
Robert Greenwalta7dfbd32010-06-15 15:43:39 -07001553 Collection<InetAddress> dnses = p.getDnses();
1554 writePidDns(dnses, myPid);
Robert Greenwalt2034b912009-08-12 16:08:25 -07001555 if (doBump) {
1556 bumpDns();
1557 }
1558 return;
1559 }
1560 }
1561 }
1562 }
1563 // nothing found - delete
1564 for (int i = 1; ; i++) {
1565 String prop = "net.dns" + i + "." + myPid;
1566 if (SystemProperties.get(prop).length() == 0) {
1567 if (doBump) {
1568 bumpDns();
1569 }
1570 return;
1571 }
1572 SystemProperties.set(prop, "");
1573 }
1574 }
1575
Robert Greenwalta7dfbd32010-06-15 15:43:39 -07001576 private void writePidDns(Collection <InetAddress> dnses, int pid) {
Robert Greenwalt2034b912009-08-12 16:08:25 -07001577 int j = 1;
Robert Greenwalta7dfbd32010-06-15 15:43:39 -07001578 for (InetAddress dns : dnses) {
1579 SystemProperties.set("net.dns" + j++ + "." + pid, dns.getHostAddress());
Robert Greenwalt2034b912009-08-12 16:08:25 -07001580 }
1581 }
1582
1583 private void bumpDns() {
1584 /*
1585 * Bump the property that tells the name resolver library to reread
1586 * the DNS server list from the properties.
1587 */
1588 String propVal = SystemProperties.get("net.dnschange");
1589 int n = 0;
1590 if (propVal.length() != 0) {
1591 try {
1592 n = Integer.parseInt(propVal);
1593 } catch (NumberFormatException e) {}
1594 }
1595 SystemProperties.set("net.dnschange", "" + (n+1));
Robert Greenwalt051642b2010-11-02 14:08:23 -07001596 /*
1597 * Tell the VMs to toss their DNS caches
1598 */
1599 Intent intent = new Intent(Intent.ACTION_CLEAR_DNS_CACHE);
1600 intent.addFlags(Intent.FLAG_RECEIVER_REPLACE_PENDING);
1601 mContext.sendBroadcast(intent);
Robert Greenwalt2034b912009-08-12 16:08:25 -07001602 }
1603
Robert Greenwalt3afbead2010-07-23 15:46:26 -07001604 private void handleDnsConfigurationChange(int netType) {
Robert Greenwalt2034b912009-08-12 16:08:25 -07001605 // add default net's dns entries
Robert Greenwalt3afbead2010-07-23 15:46:26 -07001606 NetworkStateTracker nt = mNetTrackers[netType];
1607 if (nt != null && nt.getNetworkInfo().isConnected() && !nt.isTeardownRequested()) {
Robert Greenwalt1f1bcfe2010-08-30 10:56:47 -07001608 LinkProperties p = nt.getLinkProperties();
Robert Greenwalt3afbead2010-07-23 15:46:26 -07001609 if (p == null) return;
1610 Collection<InetAddress> dnses = p.getDnses();
1611 if (mNetAttributes[netType].isDefault()) {
1612 int j = 1;
Robert Greenwalt94daa182010-09-01 11:34:05 -07001613 if (dnses.size() == 0 && mDefaultDns != null) {
Robert Greenwalt3afbead2010-07-23 15:46:26 -07001614 if (DBG) {
Wink Savillee70c6f52010-12-03 12:01:38 -08001615 log("no dns provided - using " + mDefaultDns.getHostAddress());
Robert Greenwalt2034b912009-08-12 16:08:25 -07001616 }
Robert Greenwalt94daa182010-09-01 11:34:05 -07001617 SystemProperties.set("net.dns1", mDefaultDns.getHostAddress());
1618 j++;
1619 } else {
1620 for (InetAddress dns : dnses) {
1621 if (DBG) {
Wink Savillee70c6f52010-12-03 12:01:38 -08001622 log("adding dns " + dns + " for " +
Robert Greenwalt94daa182010-09-01 11:34:05 -07001623 nt.getNetworkInfo().getTypeName());
1624 }
1625 SystemProperties.set("net.dns" + j++, dns.getHostAddress());
1626 }
Robert Greenwalt3afbead2010-07-23 15:46:26 -07001627 }
1628 for (int k=j ; k<mNumDnsEntries; k++) {
Wink Savillee70c6f52010-12-03 12:01:38 -08001629 if (DBG) log("erasing net.dns" + k);
Robert Greenwalt3afbead2010-07-23 15:46:26 -07001630 SystemProperties.set("net.dns" + k, "");
1631 }
1632 mNumDnsEntries = j;
1633 } else {
1634 // set per-pid dns for attached secondary nets
1635 List pids = mNetRequestersPids[netType];
1636 for (int y=0; y< pids.size(); y++) {
1637 Integer pid = (Integer)pids.get(y);
1638 writePidDns(dnses, pid.intValue());
The Android Open Source Project28527d22009-03-03 19:31:44 -08001639 }
1640 }
Robert Greenwalt3afbead2010-07-23 15:46:26 -07001641 bumpDns();
The Android Open Source Project28527d22009-03-03 19:31:44 -08001642 }
Robert Greenwalt2034b912009-08-12 16:08:25 -07001643 }
1644
1645 private int getRestoreDefaultNetworkDelay() {
1646 String restoreDefaultNetworkDelayStr = SystemProperties.get(
1647 NETWORK_RESTORE_DELAY_PROP_NAME);
1648 if(restoreDefaultNetworkDelayStr != null &&
1649 restoreDefaultNetworkDelayStr.length() != 0) {
1650 try {
1651 return Integer.valueOf(restoreDefaultNetworkDelayStr);
1652 } catch (NumberFormatException e) {
1653 }
The Android Open Source Project28527d22009-03-03 19:31:44 -08001654 }
Robert Greenwalt2034b912009-08-12 16:08:25 -07001655 return RESTORE_DEFAULT_NETWORK_DELAY;
The Android Open Source Project28527d22009-03-03 19:31:44 -08001656 }
1657
1658 @Override
1659 protected void dump(FileDescriptor fd, PrintWriter pw, String[] args) {
Robert Greenwalt0659da32009-07-16 17:21:39 -07001660 if (mContext.checkCallingOrSelfPermission(
1661 android.Manifest.permission.DUMP)
The Android Open Source Project28527d22009-03-03 19:31:44 -08001662 != PackageManager.PERMISSION_GRANTED) {
Robert Greenwalt0659da32009-07-16 17:21:39 -07001663 pw.println("Permission Denial: can't dump ConnectivityService " +
1664 "from from pid=" + Binder.getCallingPid() + ", uid=" +
1665 Binder.getCallingUid());
The Android Open Source Project28527d22009-03-03 19:31:44 -08001666 return;
1667 }
The Android Open Source Project28527d22009-03-03 19:31:44 -08001668 pw.println();
1669 for (NetworkStateTracker nst : mNetTrackers) {
Robert Greenwalt3eeb6032009-12-21 18:24:07 -08001670 if (nst != null) {
1671 if (nst.getNetworkInfo().isConnected()) {
1672 pw.println("Active network: " + nst.getNetworkInfo().
1673 getTypeName());
1674 }
1675 pw.println(nst.getNetworkInfo());
1676 pw.println(nst);
1677 pw.println();
Robert Greenwalt2034b912009-08-12 16:08:25 -07001678 }
The Android Open Source Project28527d22009-03-03 19:31:44 -08001679 }
Robert Greenwalt3eeb6032009-12-21 18:24:07 -08001680
1681 pw.println("Network Requester Pids:");
1682 for (int net : mPriorityList) {
1683 String pidString = net + ": ";
1684 for (Object pid : mNetRequestersPids[net]) {
1685 pidString = pidString + pid.toString() + ", ";
1686 }
1687 pw.println(pidString);
1688 }
1689 pw.println();
1690
1691 pw.println("FeatureUsers:");
1692 for (Object requester : mFeatureUsers) {
1693 pw.println(requester.toString());
1694 }
1695 pw.println();
Robert Greenwalt8e87f122010-02-11 18:18:40 -08001696
Robert Greenwalt93dc1042010-06-15 12:19:37 -07001697 synchronized (this) {
1698 pw.println("NetworkTranstionWakeLock is currently " +
1699 (mNetTransitionWakeLock.isHeld() ? "" : "not ") + "held.");
1700 pw.println("It was last requested for "+mNetTransitionWakeLockCausedBy);
1701 }
1702 pw.println();
1703
Robert Greenwalt8e87f122010-02-11 18:18:40 -08001704 mTethering.dump(fd, pw, args);
Robert Greenwalt0e80be12010-09-20 14:35:25 -07001705
1706 if (mInetLog != null) {
1707 pw.println();
1708 pw.println("Inet condition reports:");
1709 for(int i = 0; i < mInetLog.size(); i++) {
1710 pw.println(mInetLog.get(i));
1711 }
1712 }
The Android Open Source Project28527d22009-03-03 19:31:44 -08001713 }
1714
Robert Greenwalt2034b912009-08-12 16:08:25 -07001715 // must be stateless - things change under us.
The Android Open Source Project28527d22009-03-03 19:31:44 -08001716 private class MyHandler extends Handler {
Wink Saville775aad62010-09-02 19:23:52 -07001717 public MyHandler(Looper looper) {
1718 super(looper);
1719 }
1720
The Android Open Source Project28527d22009-03-03 19:31:44 -08001721 @Override
1722 public void handleMessage(Message msg) {
1723 NetworkInfo info;
1724 switch (msg.what) {
1725 case NetworkStateTracker.EVENT_STATE_CHANGED:
1726 info = (NetworkInfo) msg.obj;
Robert Greenwalt12c44552009-12-07 11:33:18 -08001727 int type = info.getType();
1728 NetworkInfo.State state = info.getState();
Robert Greenwalt24e2d2b2010-01-25 16:14:00 -08001729 // only do this optimization for wifi. It going into scan mode for location
1730 // services generates alot of noise. Meanwhile the mms apn won't send out
1731 // subsequent notifications when on default cellular because it never
1732 // disconnects.. so only do this to wifi notifications. Fixed better when the
1733 // APN notifications are standardized.
1734 if (mNetAttributes[type].mLastState == state &&
1735 mNetAttributes[type].mRadio == ConnectivityManager.TYPE_WIFI) {
Robert Greenwalt12c44552009-12-07 11:33:18 -08001736 if (DBG) {
Robert Greenwalt24e2d2b2010-01-25 16:14:00 -08001737 // TODO - remove this after we validate the dropping doesn't break
1738 // anything
Wink Savillee70c6f52010-12-03 12:01:38 -08001739 log("Dropping ConnectivityChange for " +
Robert Greenwalt2adbc7f2010-01-13 09:36:31 -08001740 info.getTypeName() + ": " +
Robert Greenwalt12c44552009-12-07 11:33:18 -08001741 state + "/" + info.getDetailedState());
1742 }
1743 return;
1744 }
1745 mNetAttributes[type].mLastState = state;
1746
Wink Savillee70c6f52010-12-03 12:01:38 -08001747 if (DBG) log("ConnectivityChange for " +
Robert Greenwalt0659da32009-07-16 17:21:39 -07001748 info.getTypeName() + ": " +
Robert Greenwalt12c44552009-12-07 11:33:18 -08001749 state + "/" + info.getDetailedState());
The Android Open Source Project28527d22009-03-03 19:31:44 -08001750
1751 // Connectivity state changed:
1752 // [31-13] Reserved for future use
Robert Greenwalt0659da32009-07-16 17:21:39 -07001753 // [12-9] Network subtype (for mobile network, as defined
1754 // by TelephonyManager)
1755 // [8-3] Detailed state ordinal (as defined by
1756 // NetworkInfo.DetailedState)
The Android Open Source Project28527d22009-03-03 19:31:44 -08001757 // [2-0] Network type (as defined by ConnectivityManager)
1758 int eventLogParam = (info.getType() & 0x7) |
1759 ((info.getDetailedState().ordinal() & 0x3f) << 3) |
1760 (info.getSubtype() << 9);
Doug Zongker2fc96232009-12-04 10:31:43 -08001761 EventLog.writeEvent(EventLogTags.CONNECTIVITY_STATE_CHANGED,
Robert Greenwalt0659da32009-07-16 17:21:39 -07001762 eventLogParam);
1763
1764 if (info.getDetailedState() ==
1765 NetworkInfo.DetailedState.FAILED) {
The Android Open Source Project28527d22009-03-03 19:31:44 -08001766 handleConnectionFailure(info);
Robert Greenwalt12c44552009-12-07 11:33:18 -08001767 } else if (state == NetworkInfo.State.DISCONNECTED) {
The Android Open Source Project28527d22009-03-03 19:31:44 -08001768 handleDisconnect(info);
Robert Greenwalt12c44552009-12-07 11:33:18 -08001769 } else if (state == NetworkInfo.State.SUSPENDED) {
The Android Open Source Project28527d22009-03-03 19:31:44 -08001770 // TODO: need to think this over.
Robert Greenwalt0659da32009-07-16 17:21:39 -07001771 // the logic here is, handle SUSPENDED the same as
1772 // DISCONNECTED. The only difference being we are
1773 // broadcasting an intent with NetworkInfo that's
1774 // suspended. This allows the applications an
1775 // opportunity to handle DISCONNECTED and SUSPENDED
1776 // differently, or not.
The Android Open Source Project28527d22009-03-03 19:31:44 -08001777 handleDisconnect(info);
Robert Greenwalt12c44552009-12-07 11:33:18 -08001778 } else if (state == NetworkInfo.State.CONNECTED) {
The Android Open Source Project28527d22009-03-03 19:31:44 -08001779 handleConnect(info);
1780 }
The Android Open Source Project28527d22009-03-03 19:31:44 -08001781 break;
The Android Open Source Project28527d22009-03-03 19:31:44 -08001782 case NetworkStateTracker.EVENT_CONFIGURATION_CHANGED:
Robert Greenwalt3afbead2010-07-23 15:46:26 -07001783 info = (NetworkInfo) msg.obj;
1784 type = info.getType();
Robert Greenwaltc3c5f862010-10-11 16:00:27 -07001785 handleConnectivityChange(type);
The Android Open Source Project28527d22009-03-03 19:31:44 -08001786 break;
Robert Greenwaltccb36f92010-09-24 14:32:21 -07001787 case EVENT_CLEAR_NET_TRANSITION_WAKELOCK:
Robert Greenwalt93dc1042010-06-15 12:19:37 -07001788 String causedBy = null;
1789 synchronized (ConnectivityService.this) {
1790 if (msg.arg1 == mNetTransitionWakeLockSerialNumber &&
1791 mNetTransitionWakeLock.isHeld()) {
1792 mNetTransitionWakeLock.release();
1793 causedBy = mNetTransitionWakeLockCausedBy;
1794 }
1795 }
1796 if (causedBy != null) {
Wink Savillee70c6f52010-12-03 12:01:38 -08001797 log("NetTransition Wakelock for " + causedBy + " released by timeout");
Robert Greenwalt93dc1042010-06-15 12:19:37 -07001798 }
Robert Greenwaltcf1a56c2010-09-09 14:05:10 -07001799 break;
Robert Greenwalt6a2db8a2010-09-23 10:05:56 -07001800 case EVENT_RESTORE_DEFAULT_NETWORK:
Robert Greenwaltaffc3a12009-09-27 17:27:04 -07001801 FeatureUser u = (FeatureUser)msg.obj;
1802 u.expire();
Robert Greenwalt986c7412010-09-08 15:24:47 -07001803 break;
Robert Greenwalt6a2db8a2010-09-23 10:05:56 -07001804 case EVENT_INET_CONDITION_CHANGE:
1805 {
1806 int netType = msg.arg1;
1807 int condition = msg.arg2;
1808 handleInetConditionChange(netType, condition);
Robert Greenwalt93dc1042010-06-15 12:19:37 -07001809 break;
Robert Greenwalt6a2db8a2010-09-23 10:05:56 -07001810 }
1811 case EVENT_INET_CONDITION_HOLD_END:
1812 {
1813 int netType = msg.arg1;
1814 int sequence = msg.arg2;
1815 handleInetConditionHoldEnd(netType, sequence);
Robert Greenwalt986c7412010-09-08 15:24:47 -07001816 break;
Robert Greenwalt6a2db8a2010-09-23 10:05:56 -07001817 }
1818 case EVENT_SET_NETWORK_PREFERENCE:
1819 {
1820 int preference = msg.arg1;
1821 handleSetNetworkPreference(preference);
1822 break;
1823 }
1824 case EVENT_SET_BACKGROUND_DATA:
1825 {
1826 boolean enabled = (msg.arg1 == ENABLED);
1827 handleSetBackgroundData(enabled);
1828 break;
1829 }
1830 case EVENT_SET_MOBILE_DATA:
1831 {
1832 boolean enabled = (msg.arg1 == ENABLED);
1833 handleSetMobileData(enabled);
1834 break;
1835 }
Robert Greenwaltc3c5f862010-10-11 16:00:27 -07001836 case EVENT_APPLY_GLOBAL_HTTP_PROXY:
1837 {
1838 handleDeprecatedGlobalHttpProxy();
1839 }
The Android Open Source Project28527d22009-03-03 19:31:44 -08001840 }
1841 }
1842 }
Robert Greenwalt0c4828c2010-01-26 11:40:34 -08001843
1844 // javadoc from interface
Robert Greenwalt4283ded2010-03-02 17:25:02 -08001845 public int tether(String iface) {
Robert Greenwalt0c4828c2010-01-26 11:40:34 -08001846 enforceTetherChangePermission();
Robert Greenwalt4283ded2010-03-02 17:25:02 -08001847
1848 if (isTetheringSupported()) {
1849 return mTethering.tether(iface);
1850 } else {
1851 return ConnectivityManager.TETHER_ERROR_UNSUPPORTED;
1852 }
Robert Greenwalt0c4828c2010-01-26 11:40:34 -08001853 }
1854
1855 // javadoc from interface
Robert Greenwalt4283ded2010-03-02 17:25:02 -08001856 public int untether(String iface) {
Robert Greenwalt0c4828c2010-01-26 11:40:34 -08001857 enforceTetherChangePermission();
Robert Greenwalt4283ded2010-03-02 17:25:02 -08001858
1859 if (isTetheringSupported()) {
1860 return mTethering.untether(iface);
1861 } else {
1862 return ConnectivityManager.TETHER_ERROR_UNSUPPORTED;
1863 }
1864 }
1865
1866 // javadoc from interface
1867 public int getLastTetherError(String iface) {
1868 enforceTetherAccessPermission();
1869
1870 if (isTetheringSupported()) {
1871 return mTethering.getLastTetherError(iface);
1872 } else {
1873 return ConnectivityManager.TETHER_ERROR_UNSUPPORTED;
1874 }
Robert Greenwalt8e87f122010-02-11 18:18:40 -08001875 }
1876
1877 // TODO - proper iface API for selection by property, inspection, etc
1878 public String[] getTetherableUsbRegexs() {
1879 enforceTetherAccessPermission();
1880 if (isTetheringSupported()) {
1881 return mTethering.getTetherableUsbRegexs();
1882 } else {
1883 return new String[0];
1884 }
1885 }
1886
1887 public String[] getTetherableWifiRegexs() {
1888 enforceTetherAccessPermission();
1889 if (isTetheringSupported()) {
1890 return mTethering.getTetherableWifiRegexs();
1891 } else {
1892 return new String[0];
1893 }
Robert Greenwalt0c4828c2010-01-26 11:40:34 -08001894 }
1895
Danica Chang96567052010-08-11 14:54:43 -07001896 public String[] getTetherableBluetoothRegexs() {
1897 enforceTetherAccessPermission();
1898 if (isTetheringSupported()) {
1899 return mTethering.getTetherableBluetoothRegexs();
1900 } else {
1901 return new String[0];
1902 }
1903 }
1904
Robert Greenwalt0c4828c2010-01-26 11:40:34 -08001905 // TODO - move iface listing, queries, etc to new module
1906 // javadoc from interface
1907 public String[] getTetherableIfaces() {
Robert Greenwalt8e87f122010-02-11 18:18:40 -08001908 enforceTetherAccessPermission();
Robert Greenwalt0c4828c2010-01-26 11:40:34 -08001909 return mTethering.getTetherableIfaces();
1910 }
1911
1912 public String[] getTetheredIfaces() {
Robert Greenwalt8e87f122010-02-11 18:18:40 -08001913 enforceTetherAccessPermission();
Robert Greenwalt0c4828c2010-01-26 11:40:34 -08001914 return mTethering.getTetheredIfaces();
1915 }
Robert Greenwalt8e87f122010-02-11 18:18:40 -08001916
Robert Greenwalt4283ded2010-03-02 17:25:02 -08001917 public String[] getTetheringErroredIfaces() {
1918 enforceTetherAccessPermission();
1919 return mTethering.getErroredIfaces();
1920 }
1921
Robert Greenwalt8e87f122010-02-11 18:18:40 -08001922 // if ro.tether.denied = true we default to no tethering
1923 // gservices could set the secure setting to 1 though to enable it on a build where it
1924 // had previously been turned off.
1925 public boolean isTetheringSupported() {
1926 enforceTetherAccessPermission();
1927 int defaultVal = (SystemProperties.get("ro.tether.denied").equals("true") ? 0 : 1);
Robert Greenwaltf1b66e12010-02-25 12:29:30 -08001928 boolean tetherEnabledInSettings = (Settings.Secure.getInt(mContext.getContentResolver(),
1929 Settings.Secure.TETHER_SUPPORTED, defaultVal) != 0);
1930 return tetherEnabledInSettings && mTetheringConfigValid;
Robert Greenwalt8e87f122010-02-11 18:18:40 -08001931 }
Robert Greenwalt93dc1042010-06-15 12:19:37 -07001932
1933 // An API NetworkStateTrackers can call when they lose their network.
1934 // This will automatically be cleared after X seconds or a network becomes CONNECTED,
1935 // whichever happens first. The timer is started by the first caller and not
1936 // restarted by subsequent callers.
1937 public void requestNetworkTransitionWakelock(String forWhom) {
1938 enforceConnectivityInternalPermission();
1939 synchronized (this) {
1940 if (mNetTransitionWakeLock.isHeld()) return;
1941 mNetTransitionWakeLockSerialNumber++;
1942 mNetTransitionWakeLock.acquire();
1943 mNetTransitionWakeLockCausedBy = forWhom;
1944 }
1945 mHandler.sendMessageDelayed(mHandler.obtainMessage(
Robert Greenwaltccb36f92010-09-24 14:32:21 -07001946 EVENT_CLEAR_NET_TRANSITION_WAKELOCK,
Robert Greenwalt93dc1042010-06-15 12:19:37 -07001947 mNetTransitionWakeLockSerialNumber, 0),
1948 mNetTransitionWakeLockTimeout);
1949 return;
1950 }
Robert Greenwalt24118e82010-09-09 13:15:32 -07001951
Robert Greenwalt986c7412010-09-08 15:24:47 -07001952 // 100 percent is full good, 0 is full bad.
1953 public void reportInetCondition(int networkType, int percentage) {
Wink Savillee70c6f52010-12-03 12:01:38 -08001954 if (DBG) log("reportNetworkCondition(" + networkType + ", " + percentage + ")");
Robert Greenwalt986c7412010-09-08 15:24:47 -07001955 mContext.enforceCallingOrSelfPermission(
1956 android.Manifest.permission.STATUS_BAR,
1957 "ConnectivityService");
1958
Robert Greenwalt0e80be12010-09-20 14:35:25 -07001959 if (DBG) {
1960 int pid = getCallingPid();
1961 int uid = getCallingUid();
1962 String s = pid + "(" + uid + ") reports inet is " +
1963 (percentage > 50 ? "connected" : "disconnected") + " (" + percentage + ") on " +
1964 "network Type " + networkType + " at " + GregorianCalendar.getInstance().getTime();
1965 mInetLog.add(s);
1966 while(mInetLog.size() > INET_CONDITION_LOG_MAX_SIZE) {
1967 mInetLog.remove(0);
1968 }
1969 }
Robert Greenwalt986c7412010-09-08 15:24:47 -07001970 mHandler.sendMessage(mHandler.obtainMessage(
Robert Greenwalt6a2db8a2010-09-23 10:05:56 -07001971 EVENT_INET_CONDITION_CHANGE, networkType, percentage));
1972 }
1973
1974 private void handleInetConditionChange(int netType, int condition) {
1975 if (DBG) {
Wink Savillee70c6f52010-12-03 12:01:38 -08001976 log("Inet connectivity change, net=" +
Robert Greenwalt6a2db8a2010-09-23 10:05:56 -07001977 netType + ", condition=" + condition +
1978 ",mActiveDefaultNetwork=" + mActiveDefaultNetwork);
1979 }
1980 if (mActiveDefaultNetwork == -1) {
Wink Savillee70c6f52010-12-03 12:01:38 -08001981 if (DBG) log("no active default network - aborting");
Robert Greenwalt6a2db8a2010-09-23 10:05:56 -07001982 return;
1983 }
1984 if (mActiveDefaultNetwork != netType) {
Wink Savillee70c6f52010-12-03 12:01:38 -08001985 if (DBG) log("given net not default - aborting");
Robert Greenwalt6a2db8a2010-09-23 10:05:56 -07001986 return;
1987 }
1988 mDefaultInetCondition = condition;
1989 int delay;
1990 if (mInetConditionChangeInFlight == false) {
Wink Savillee70c6f52010-12-03 12:01:38 -08001991 if (DBG) log("starting a change hold");
Robert Greenwalt6a2db8a2010-09-23 10:05:56 -07001992 // setup a new hold to debounce this
1993 if (mDefaultInetCondition > 50) {
1994 delay = Settings.Secure.getInt(mContext.getContentResolver(),
1995 Settings.Secure.INET_CONDITION_DEBOUNCE_UP_DELAY, 500);
1996 } else {
1997 delay = Settings.Secure.getInt(mContext.getContentResolver(),
1998 Settings.Secure.INET_CONDITION_DEBOUNCE_DOWN_DELAY, 3000);
1999 }
2000 mInetConditionChangeInFlight = true;
2001 mHandler.sendMessageDelayed(mHandler.obtainMessage(EVENT_INET_CONDITION_HOLD_END,
2002 mActiveDefaultNetwork, mDefaultConnectionSequence), delay);
2003 } else {
2004 // we've set the new condition, when this hold ends that will get
2005 // picked up
Wink Savillee70c6f52010-12-03 12:01:38 -08002006 if (DBG) log("currently in hold - not setting new end evt");
Robert Greenwalt6a2db8a2010-09-23 10:05:56 -07002007 }
2008 }
2009
2010 private void handleInetConditionHoldEnd(int netType, int sequence) {
2011 if (DBG) {
Wink Savillee70c6f52010-12-03 12:01:38 -08002012 log("Inet hold end, net=" + netType +
Robert Greenwalt6a2db8a2010-09-23 10:05:56 -07002013 ", condition =" + mDefaultInetCondition +
2014 ", published condition =" + mDefaultInetConditionPublished);
2015 }
2016 mInetConditionChangeInFlight = false;
2017
2018 if (mActiveDefaultNetwork == -1) {
Wink Savillee70c6f52010-12-03 12:01:38 -08002019 if (DBG) log("no active default network - aborting");
Robert Greenwalt6a2db8a2010-09-23 10:05:56 -07002020 return;
2021 }
2022 if (mDefaultConnectionSequence != sequence) {
Wink Savillee70c6f52010-12-03 12:01:38 -08002023 if (DBG) log("event hold for obsolete network - aborting");
Robert Greenwalt6a2db8a2010-09-23 10:05:56 -07002024 return;
2025 }
2026 if (mDefaultInetConditionPublished == mDefaultInetCondition) {
Wink Savillee70c6f52010-12-03 12:01:38 -08002027 if (DBG) log("no change in condition - aborting");
Robert Greenwalt6a2db8a2010-09-23 10:05:56 -07002028 return;
2029 }
2030 NetworkInfo networkInfo = mNetTrackers[mActiveDefaultNetwork].getNetworkInfo();
2031 if (networkInfo.isConnected() == false) {
Wink Savillee70c6f52010-12-03 12:01:38 -08002032 if (DBG) log("default network not connected - aborting");
Robert Greenwalt6a2db8a2010-09-23 10:05:56 -07002033 return;
2034 }
2035 mDefaultInetConditionPublished = mDefaultInetCondition;
2036 sendInetConditionBroadcast(networkInfo);
2037 return;
Robert Greenwalt986c7412010-09-08 15:24:47 -07002038 }
Robert Greenwaltc3c5f862010-10-11 16:00:27 -07002039
2040 public synchronized ProxyProperties getProxy() {
2041 if (mGlobalProxy != null) return mGlobalProxy;
2042 if (mDefaultProxy != null) return mDefaultProxy;
2043 return null;
2044 }
2045
2046 public void setGlobalProxy(ProxyProperties proxyProperties) {
2047 enforceChangePermission();
2048 synchronized (mGlobalProxyLock) {
2049 if (proxyProperties == mGlobalProxy) return;
2050 if (proxyProperties != null && proxyProperties.equals(mGlobalProxy)) return;
2051 if (mGlobalProxy != null && mGlobalProxy.equals(proxyProperties)) return;
2052
2053 String host = "";
2054 int port = 0;
2055 String exclList = "";
2056 if (proxyProperties != null && !TextUtils.isEmpty(proxyProperties.getHost())) {
2057 mGlobalProxy = new ProxyProperties(proxyProperties);
2058 host = mGlobalProxy.getHost();
2059 port = mGlobalProxy.getPort();
2060 exclList = mGlobalProxy.getExclusionList();
2061 } else {
2062 mGlobalProxy = null;
2063 }
2064 ContentResolver res = mContext.getContentResolver();
2065 Settings.Secure.putString(res, Settings.Secure.GLOBAL_HTTP_PROXY_HOST, host);
2066 Settings.Secure.putInt(res, Settings.Secure.GLOBAL_HTTP_PROXY_PORT, port);
Robert Greenwalt6f7c6092010-12-02 11:31:00 -08002067 Settings.Secure.putString(res, Settings.Secure.GLOBAL_HTTP_PROXY_EXCLUSION_LIST,
Robert Greenwaltc3c5f862010-10-11 16:00:27 -07002068 exclList);
2069 }
2070
2071 if (mGlobalProxy == null) {
2072 proxyProperties = mDefaultProxy;
2073 }
2074 sendProxyBroadcast(proxyProperties);
2075 }
2076
Robert Greenwalt6f7c6092010-12-02 11:31:00 -08002077 private void loadGlobalProxy() {
2078 ContentResolver res = mContext.getContentResolver();
2079 String host = Settings.Secure.getString(res, Settings.Secure.GLOBAL_HTTP_PROXY_HOST);
2080 int port = Settings.Secure.getInt(res, Settings.Secure.GLOBAL_HTTP_PROXY_PORT, 0);
2081 String exclList = Settings.Secure.getString(res,
2082 Settings.Secure.GLOBAL_HTTP_PROXY_EXCLUSION_LIST);
2083 if (!TextUtils.isEmpty(host)) {
2084 ProxyProperties proxyProperties = new ProxyProperties(host, port, exclList);
2085 synchronized (mGlobalProxyLock) {
2086 mGlobalProxy = proxyProperties;
2087 }
2088 }
2089 }
2090
Robert Greenwaltc3c5f862010-10-11 16:00:27 -07002091 public ProxyProperties getGlobalProxy() {
2092 synchronized (mGlobalProxyLock) {
2093 return mGlobalProxy;
2094 }
2095 }
2096
2097 private void handleApplyDefaultProxy(int type) {
2098 // check if new default - push it out to all VM if so
2099 ProxyProperties proxy = mNetTrackers[type].getLinkProperties().getHttpProxy();
2100 synchronized (this) {
2101 if (mDefaultProxy != null && mDefaultProxy.equals(proxy)) return;
2102 if (mDefaultProxy == proxy) return;
2103 if (!TextUtils.isEmpty(proxy.getHost())) {
2104 mDefaultProxy = proxy;
2105 } else {
2106 mDefaultProxy = null;
2107 }
2108 }
Wink Savillee70c6f52010-12-03 12:01:38 -08002109 if (DBG) log("changing default proxy to " + proxy);
Robert Greenwaltc3c5f862010-10-11 16:00:27 -07002110 if ((proxy == null && mGlobalProxy == null) || proxy.equals(mGlobalProxy)) return;
2111 if (mGlobalProxy != null) return;
2112 sendProxyBroadcast(proxy);
2113 }
2114
2115 private void handleDeprecatedGlobalHttpProxy() {
2116 String proxy = Settings.Secure.getString(mContext.getContentResolver(),
2117 Settings.Secure.HTTP_PROXY);
2118 if (!TextUtils.isEmpty(proxy)) {
2119 String data[] = proxy.split(":");
2120 String proxyHost = data[0];
2121 int proxyPort = 8080;
2122 if (data.length > 1) {
2123 try {
2124 proxyPort = Integer.parseInt(data[1]);
2125 } catch (NumberFormatException e) {
2126 return;
2127 }
2128 }
2129 ProxyProperties p = new ProxyProperties(data[0], proxyPort, "");
2130 setGlobalProxy(p);
2131 }
2132 }
2133
2134 private void sendProxyBroadcast(ProxyProperties proxy) {
Wink Savillee70c6f52010-12-03 12:01:38 -08002135 log("sending Proxy Broadcast for " + proxy);
Robert Greenwaltc3c5f862010-10-11 16:00:27 -07002136 Intent intent = new Intent(Proxy.PROXY_CHANGE_ACTION);
2137 intent.addFlags(Intent.FLAG_RECEIVER_REPLACE_PENDING);
2138 intent.putExtra(Proxy.EXTRA_PROXY_INFO, proxy);
Robert Greenwaltd93dc8f2010-12-06 11:29:17 -08002139 mContext.sendStickyBroadcast(intent);
Robert Greenwaltc3c5f862010-10-11 16:00:27 -07002140 }
2141
2142 private static class SettingsObserver extends ContentObserver {
2143 private int mWhat;
2144 private Handler mHandler;
2145 SettingsObserver(Handler handler, int what) {
2146 super(handler);
2147 mHandler = handler;
2148 mWhat = what;
2149 }
2150
2151 void observe(Context context) {
2152 ContentResolver resolver = context.getContentResolver();
2153 resolver.registerContentObserver(Settings.Secure.getUriFor(
2154 Settings.Secure.HTTP_PROXY), false, this);
2155 }
2156
2157 @Override
2158 public void onChange(boolean selfChange) {
2159 mHandler.obtainMessage(mWhat).sendToTarget();
2160 }
2161 }
Wink Savillee70c6f52010-12-03 12:01:38 -08002162
2163 private void log(String s) {
2164 Slog.d(TAG, s);
2165 }
2166
2167 private void loge(String s) {
2168 Slog.e(TAG, s);
2169 }
The Android Open Source Project28527d22009-03-03 19:31:44 -08002170}