blob: eeb5290b3491cc3f7bb5395bcebd82551f6bb433 [file] [log] [blame]
The Android Open Source Project28527d22009-03-03 19:31:44 -08001/*
2 * Copyright (C) 2008 The Android Open Source Project
3 *
4 * Licensed under the Apache License, Version 2.0 (the "License");
5 * you may not use this file except in compliance with the License.
6 * You may obtain a copy of the License at
7 *
8 * http://www.apache.org/licenses/LICENSE-2.0
9 *
10 * Unless required by applicable law or agreed to in writing, software
11 * distributed under the License is distributed on an "AS IS" BASIS,
12 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13 * See the License for the specific language governing permissions and
14 * limitations under the License.
15 */
16
17package com.android.server;
18
Jaikumar Ganesh0db51a02010-12-21 22:31:44 -080019import android.bluetooth.BluetoothTetheringDataTracker;
The Android Open Source Project28527d22009-03-03 19:31:44 -080020import android.content.ContentResolver;
21import android.content.Context;
22import android.content.Intent;
23import android.content.pm.PackageManager;
Robert Greenwaltc3c5f862010-10-11 16:00:27 -070024import android.database.ContentObserver;
The Android Open Source Project28527d22009-03-03 19:31:44 -080025import android.net.ConnectivityManager;
Robert Greenwalteb123ac2010-12-06 13:56:24 -080026import android.net.DummyDataStateTracker;
Benoit Goby6cec7f32010-12-22 14:29:40 -080027import android.net.EthernetDataTracker;
The Android Open Source Project28527d22009-03-03 19:31:44 -080028import android.net.IConnectivityManager;
Robert Greenwalt355205c2011-05-10 15:05:02 -070029import android.net.LinkAddress;
Jaikumar Ganesh0db51a02010-12-21 22:31:44 -080030import android.net.LinkProperties;
Robert Greenwalte8d2a4a2011-07-14 14:28:05 -070031import android.net.LinkProperties.CompareResult;
The Android Open Source Project28527d22009-03-03 19:31:44 -080032import android.net.MobileDataStateTracker;
Robert Greenwalt34848c02011-03-25 13:09:25 -070033import android.net.NetworkConfig;
The Android Open Source Project28527d22009-03-03 19:31:44 -080034import android.net.NetworkInfo;
35import android.net.NetworkStateTracker;
Robert Greenwalt7fe44cb2010-08-27 09:24:29 -070036import android.net.NetworkUtils;
Robert Greenwaltc3c5f862010-10-11 16:00:27 -070037import android.net.Proxy;
38import android.net.ProxyProperties;
Robert Greenwalt5a901292011-04-28 14:28:50 -070039import android.net.RouteInfo;
Hung-ying Tyan4e723422011-01-19 16:48:38 +080040import android.net.vpn.VpnManager;
The Android Open Source Project28527d22009-03-03 19:31:44 -080041import android.net.wifi.WifiStateTracker;
42import android.os.Binder;
43import android.os.Handler;
Wink Saville775aad62010-09-02 19:23:52 -070044import android.os.HandlerThread;
Robert Greenwalt2034b912009-08-12 16:08:25 -070045import android.os.IBinder;
Robert Greenwalt355205c2011-05-10 15:05:02 -070046import android.os.INetworkManagementService;
The Android Open Source Project28527d22009-03-03 19:31:44 -080047import android.os.Looper;
48import android.os.Message;
Robert Greenwalt93dc1042010-06-15 12:19:37 -070049import android.os.PowerManager;
Robert Greenwalt2034b912009-08-12 16:08:25 -070050import android.os.RemoteException;
The Android Open Source Project28527d22009-03-03 19:31:44 -080051import android.os.ServiceManager;
52import android.os.SystemProperties;
53import android.provider.Settings;
Robert Greenwalt2034b912009-08-12 16:08:25 -070054import android.text.TextUtils;
The Android Open Source Project28527d22009-03-03 19:31:44 -080055import android.util.EventLog;
Joe Onoratoc2386bb2010-02-26 18:56:32 -080056import android.util.Slog;
The Android Open Source Project28527d22009-03-03 19:31:44 -080057
Robert Greenwalt2034b912009-08-12 16:08:25 -070058import com.android.internal.telephony.Phone;
Robert Greenwalt0c4828c2010-01-26 11:40:34 -080059import com.android.server.connectivity.Tethering;
60
The Android Open Source Project28527d22009-03-03 19:31:44 -080061import java.io.FileDescriptor;
Irfan Sheriff7f132d92010-06-09 15:39:36 -070062import java.io.FileWriter;
63import java.io.IOException;
The Android Open Source Project28527d22009-03-03 19:31:44 -080064import java.io.PrintWriter;
Wink Saville051a6642011-07-13 13:44:13 -070065import java.net.Inet6Address;
Robert Greenwalta7dfbd32010-06-15 15:43:39 -070066import java.net.InetAddress;
Robert Greenwalt355205c2011-05-10 15:05:02 -070067import java.net.Inet4Address;
Robert Greenwalta7dfbd32010-06-15 15:43:39 -070068import java.net.UnknownHostException;
Robert Greenwalt2034b912009-08-12 16:08:25 -070069import java.util.ArrayList;
Robert Greenwalta7dfbd32010-06-15 15:43:39 -070070import java.util.Collection;
Robert Greenwaltd62c7002010-12-29 16:15:02 -080071import java.util.concurrent.atomic.AtomicBoolean;
Robert Greenwalt0e80be12010-09-20 14:35:25 -070072import java.util.GregorianCalendar;
Robert Greenwalt2034b912009-08-12 16:08:25 -070073import java.util.List;
The Android Open Source Project28527d22009-03-03 19:31:44 -080074
75/**
76 * @hide
77 */
78public class ConnectivityService extends IConnectivityManager.Stub {
79
Robert Greenwalt063dc7d2010-10-05 19:12:26 -070080 private static final boolean DBG = true;
Wink Saville051a6642011-07-13 13:44:13 -070081 private static final boolean VDBG = true;
The Android Open Source Project28527d22009-03-03 19:31:44 -080082 private static final String TAG = "ConnectivityService";
83
Robert Greenwalt2034b912009-08-12 16:08:25 -070084 // how long to wait before switching back to a radio's default network
85 private static final int RESTORE_DEFAULT_NETWORK_DELAY = 1 * 60 * 1000;
86 // system property that can override the above value
87 private static final String NETWORK_RESTORE_DELAY_PROP_NAME =
88 "android.telephony.apn-restore";
89
Robert Greenwaltbd492212011-05-06 17:10:53 -070090 // used in recursive route setting to add gateways for the host for which
91 // a host route was requested.
92 private static final int MAX_HOSTROUTE_CYCLE_COUNT = 10;
93
Robert Greenwalt0c4828c2010-01-26 11:40:34 -080094 private Tethering mTethering;
Robert Greenwaltf1b66e12010-02-25 12:29:30 -080095 private boolean mTetheringConfigValid = false;
Robert Greenwalt0c4828c2010-01-26 11:40:34 -080096
The Android Open Source Project28527d22009-03-03 19:31:44 -080097 /**
98 * Sometimes we want to refer to the individual network state
99 * trackers separately, and sometimes we just want to treat them
100 * abstractly.
101 */
102 private NetworkStateTracker mNetTrackers[];
Robert Greenwalt2034b912009-08-12 16:08:25 -0700103
104 /**
Wink Saville051a6642011-07-13 13:44:13 -0700105 * The link properties that define the current links
106 */
107 private LinkProperties mCurrentLinkProperties[];
108
109 /**
Robert Greenwalt2034b912009-08-12 16:08:25 -0700110 * A per Net list of the PID's that requested access to the net
111 * used both as a refcount and for per-PID DNS selection
112 */
113 private List mNetRequestersPids[];
114
Irfan Sheriff653e2a22010-06-07 09:03:04 -0700115 private WifiWatchdogService mWifiWatchdogService;
116
Robert Greenwalt2034b912009-08-12 16:08:25 -0700117 // priority order of the nettrackers
118 // (excluding dynamically set mNetworkPreference)
119 // TODO - move mNetworkTypePreference into this
120 private int[] mPriorityList;
121
The Android Open Source Project28527d22009-03-03 19:31:44 -0800122 private Context mContext;
123 private int mNetworkPreference;
Robert Greenwalt2034b912009-08-12 16:08:25 -0700124 private int mActiveDefaultNetwork = -1;
Robert Greenwalt986c7412010-09-08 15:24:47 -0700125 // 0 is full bad, 100 is full good
126 private int mDefaultInetCondition = 0;
127 private int mDefaultInetConditionPublished = 0;
128 private boolean mInetConditionChangeInFlight = false;
129 private int mDefaultConnectionSequence = 0;
The Android Open Source Project28527d22009-03-03 19:31:44 -0800130
131 private int mNumDnsEntries;
The Android Open Source Project28527d22009-03-03 19:31:44 -0800132
133 private boolean mTestMode;
Joe Onorato56023ad2010-09-01 21:18:22 -0700134 private static ConnectivityService sServiceInstance;
The Android Open Source Project28527d22009-03-03 19:31:44 -0800135
Robert Greenwaltd62c7002010-12-29 16:15:02 -0800136 private AtomicBoolean mBackgroundDataEnabled = new AtomicBoolean(true);
137
Robert Greenwalt355205c2011-05-10 15:05:02 -0700138 private INetworkManagementService mNetd;
139
Robert Greenwalt6a2db8a2010-09-23 10:05:56 -0700140 private static final int ENABLED = 1;
141 private static final int DISABLED = 0;
142
143 // Share the event space with NetworkStateTracker (which can't see this
144 // internal class but sends us events). If you change these, change
145 // NetworkStateTracker.java too.
146 private static final int MIN_NETWORK_STATE_TRACKER_EVENT = 1;
147 private static final int MAX_NETWORK_STATE_TRACKER_EVENT = 100;
148
149 /**
150 * used internally as a delayed event to make us switch back to the
151 * default network
152 */
153 private static final int EVENT_RESTORE_DEFAULT_NETWORK =
154 MAX_NETWORK_STATE_TRACKER_EVENT + 1;
155
156 /**
157 * used internally to change our mobile data enabled flag
158 */
159 private static final int EVENT_CHANGE_MOBILE_DATA_ENABLED =
160 MAX_NETWORK_STATE_TRACKER_EVENT + 2;
161
162 /**
163 * used internally to change our network preference setting
164 * arg1 = networkType to prefer
165 */
166 private static final int EVENT_SET_NETWORK_PREFERENCE =
167 MAX_NETWORK_STATE_TRACKER_EVENT + 3;
168
169 /**
170 * used internally to synchronize inet condition reports
171 * arg1 = networkType
172 * arg2 = condition (0 bad, 100 good)
173 */
174 private static final int EVENT_INET_CONDITION_CHANGE =
175 MAX_NETWORK_STATE_TRACKER_EVENT + 4;
176
177 /**
178 * used internally to mark the end of inet condition hold periods
179 * arg1 = networkType
180 */
181 private static final int EVENT_INET_CONDITION_HOLD_END =
182 MAX_NETWORK_STATE_TRACKER_EVENT + 5;
183
184 /**
185 * used internally to set the background data preference
186 * arg1 = TRUE for enabled, FALSE for disabled
187 */
188 private static final int EVENT_SET_BACKGROUND_DATA =
189 MAX_NETWORK_STATE_TRACKER_EVENT + 6;
190
191 /**
192 * used internally to set enable/disable cellular data
193 * arg1 = ENBALED or DISABLED
194 */
195 private static final int EVENT_SET_MOBILE_DATA =
196 MAX_NETWORK_STATE_TRACKER_EVENT + 7;
197
Robert Greenwaltccb36f92010-09-24 14:32:21 -0700198 /**
199 * used internally to clear a wakelock when transitioning
200 * from one net to another
201 */
202 private static final int EVENT_CLEAR_NET_TRANSITION_WAKELOCK =
203 MAX_NETWORK_STATE_TRACKER_EVENT + 8;
204
Robert Greenwaltc3c5f862010-10-11 16:00:27 -0700205 /**
206 * used internally to reload global proxy settings
207 */
208 private static final int EVENT_APPLY_GLOBAL_HTTP_PROXY =
209 MAX_NETWORK_STATE_TRACKER_EVENT + 9;
210
Robert Greenwalt34848c02011-03-25 13:09:25 -0700211 /**
212 * used internally to set external dependency met/unmet
213 * arg1 = ENABLED (met) or DISABLED (unmet)
214 * arg2 = NetworkType
215 */
216 private static final int EVENT_SET_DEPENDENCY_MET =
217 MAX_NETWORK_STATE_TRACKER_EVENT + 10;
218
Wink Saville4f0de1e2011-08-04 15:01:58 -0700219 /**
220 * used internally to send a sticky broadcast delayed.
221 */
222 private static final int EVENT_SEND_STICKY_BROADCAST_INTENT =
223 MAX_NETWORK_STATE_TRACKER_EVENT + 11;
224
Robert Greenwalt2034b912009-08-12 16:08:25 -0700225 private Handler mHandler;
226
227 // list of DeathRecipients used to make sure features are turned off when
228 // a process dies
Kazuhiro Ondocae18f12011-07-19 11:23:37 -0500229 private List<FeatureUser> mFeatureUsers;
Robert Greenwalt2034b912009-08-12 16:08:25 -0700230
Mike Lockwoodfde2b762009-08-14 14:18:49 -0400231 private boolean mSystemReady;
Dianne Hackborna417ff82009-12-08 19:45:14 -0800232 private Intent mInitialBroadcast;
Mike Lockwoodfde2b762009-08-14 14:18:49 -0400233
Robert Greenwalt93dc1042010-06-15 12:19:37 -0700234 private PowerManager.WakeLock mNetTransitionWakeLock;
235 private String mNetTransitionWakeLockCausedBy = "";
236 private int mNetTransitionWakeLockSerialNumber;
237 private int mNetTransitionWakeLockTimeout;
238
Robert Greenwalt94daa182010-09-01 11:34:05 -0700239 private InetAddress mDefaultDns;
240
Robert Greenwalte8d2a4a2011-07-14 14:28:05 -0700241 // this collection is used to refcount the added routes - if there are none left
242 // it's time to remove the route from the route table
243 private Collection<RouteInfo> mAddedRoutes = new ArrayList<RouteInfo>();
244
Robert Greenwalt0e80be12010-09-20 14:35:25 -0700245 // used in DBG mode to track inet condition reports
246 private static final int INET_CONDITION_LOG_MAX_SIZE = 15;
247 private ArrayList mInetLog;
248
Robert Greenwaltc3c5f862010-10-11 16:00:27 -0700249 // track the current default http proxy - tell the world if we get a new one (real change)
250 private ProxyProperties mDefaultProxy = null;
251 // track the global proxy.
252 private ProxyProperties mGlobalProxy = null;
253 private final Object mGlobalProxyLock = new Object();
254
255 private SettingsObserver mSettingsObserver;
256
Robert Greenwalt34848c02011-03-25 13:09:25 -0700257 NetworkConfig[] mNetConfigs;
Robert Greenwaltec05b3c2009-10-30 14:17:42 -0700258 int mNetworksDefined;
Robert Greenwalt2034b912009-08-12 16:08:25 -0700259
Robert Greenwalt12c44552009-12-07 11:33:18 -0800260 private static class RadioAttributes {
Robert Greenwalt2034b912009-08-12 16:08:25 -0700261 public int mSimultaneity;
262 public int mType;
263 public RadioAttributes(String init) {
264 String fragments[] = init.split(",");
Robert Greenwaltec05b3c2009-10-30 14:17:42 -0700265 mType = Integer.parseInt(fragments[0]);
266 mSimultaneity = Integer.parseInt(fragments[1]);
Robert Greenwalt2034b912009-08-12 16:08:25 -0700267 }
268 }
269 RadioAttributes[] mRadioAttributes;
270
Robert Greenwalt6cac0742011-06-21 17:26:14 -0700271 // the set of network types that can only be enabled by system/sig apps
272 List mProtectedNetworks;
273
Wink Saville775aad62010-09-02 19:23:52 -0700274 public static synchronized ConnectivityService getInstance(Context context) {
275 if (sServiceInstance == null) {
276 sServiceInstance = new ConnectivityService(context);
The Android Open Source Project28527d22009-03-03 19:31:44 -0800277 }
Wink Saville775aad62010-09-02 19:23:52 -0700278 return sServiceInstance;
The Android Open Source Project28527d22009-03-03 19:31:44 -0800279 }
Robert Greenwalt0659da32009-07-16 17:21:39 -0700280
The Android Open Source Project28527d22009-03-03 19:31:44 -0800281 private ConnectivityService(Context context) {
Wink Savillee70c6f52010-12-03 12:01:38 -0800282 if (DBG) log("ConnectivityService starting up");
Robert Greenwaltd48f8ee2010-01-14 17:47:58 -0800283
Wink Saville775aad62010-09-02 19:23:52 -0700284 HandlerThread handlerThread = new HandlerThread("ConnectivityServiceThread");
285 handlerThread.start();
286 mHandler = new MyHandler(handlerThread.getLooper());
287
Robert Greenwaltd62c7002010-12-29 16:15:02 -0800288 mBackgroundDataEnabled.set(Settings.Secure.getInt(context.getContentResolver(),
289 Settings.Secure.BACKGROUND_DATA, 1) == 1);
290
Robert Greenwaltd48f8ee2010-01-14 17:47:58 -0800291 // setup our unique device name
Robert Greenwalt82cde132010-12-06 09:30:17 -0800292 if (TextUtils.isEmpty(SystemProperties.get("net.hostname"))) {
293 String id = Settings.Secure.getString(context.getContentResolver(),
294 Settings.Secure.ANDROID_ID);
295 if (id != null && id.length() > 0) {
296 String name = new String("android_").concat(id);
297 SystemProperties.set("net.hostname", name);
298 }
Robert Greenwaltd48f8ee2010-01-14 17:47:58 -0800299 }
300
Robert Greenwalt94daa182010-09-01 11:34:05 -0700301 // read our default dns server ip
302 String dns = Settings.Secure.getString(context.getContentResolver(),
303 Settings.Secure.DEFAULT_DNS_SERVER);
304 if (dns == null || dns.length() == 0) {
305 dns = context.getResources().getString(
306 com.android.internal.R.string.config_default_dns_server);
307 }
308 try {
Robert Greenwalt35e34d12011-02-22 16:00:42 -0800309 mDefaultDns = NetworkUtils.numericToInetAddress(dns);
310 } catch (IllegalArgumentException e) {
Wink Savillee70c6f52010-12-03 12:01:38 -0800311 loge("Error setting defaultDns using " + dns);
Robert Greenwalt94daa182010-09-01 11:34:05 -0700312 }
313
The Android Open Source Project28527d22009-03-03 19:31:44 -0800314 mContext = context;
Robert Greenwalt93dc1042010-06-15 12:19:37 -0700315
316 PowerManager powerManager = (PowerManager)mContext.getSystemService(Context.POWER_SERVICE);
317 mNetTransitionWakeLock = powerManager.newWakeLock(PowerManager.PARTIAL_WAKE_LOCK, TAG);
318 mNetTransitionWakeLockTimeout = mContext.getResources().getInteger(
319 com.android.internal.R.integer.config_networkTransitionTimeout);
320
Robert Greenwalt2034b912009-08-12 16:08:25 -0700321 mNetTrackers = new NetworkStateTracker[
322 ConnectivityManager.MAX_NETWORK_TYPE+1];
Wink Saville051a6642011-07-13 13:44:13 -0700323 mCurrentLinkProperties = new LinkProperties[ConnectivityManager.MAX_NETWORK_TYPE+1];
Robert Greenwalt0659da32009-07-16 17:21:39 -0700324
The Android Open Source Project28527d22009-03-03 19:31:44 -0800325 mNetworkPreference = getPersistedNetworkPreference();
Robert Greenwalt0659da32009-07-16 17:21:39 -0700326
Robert Greenwaltec05b3c2009-10-30 14:17:42 -0700327 mRadioAttributes = new RadioAttributes[ConnectivityManager.MAX_RADIO_TYPE+1];
Robert Greenwalt34848c02011-03-25 13:09:25 -0700328 mNetConfigs = new NetworkConfig[ConnectivityManager.MAX_NETWORK_TYPE+1];
Robert Greenwaltec05b3c2009-10-30 14:17:42 -0700329
Robert Greenwalt2034b912009-08-12 16:08:25 -0700330 // Load device network attributes from resources
Robert Greenwalt2034b912009-08-12 16:08:25 -0700331 String[] raStrings = context.getResources().getStringArray(
332 com.android.internal.R.array.radioAttributes);
Robert Greenwaltec05b3c2009-10-30 14:17:42 -0700333 for (String raString : raStrings) {
334 RadioAttributes r = new RadioAttributes(raString);
335 if (r.mType > ConnectivityManager.MAX_RADIO_TYPE) {
Wink Savillee70c6f52010-12-03 12:01:38 -0800336 loge("Error in radioAttributes - ignoring attempt to define type " + r.mType);
Robert Greenwaltec05b3c2009-10-30 14:17:42 -0700337 continue;
338 }
339 if (mRadioAttributes[r.mType] != null) {
Wink Savillee70c6f52010-12-03 12:01:38 -0800340 loge("Error in radioAttributes - ignoring attempt to redefine type " +
Robert Greenwaltec05b3c2009-10-30 14:17:42 -0700341 r.mType);
342 continue;
343 }
Robert Greenwalt2034b912009-08-12 16:08:25 -0700344 mRadioAttributes[r.mType] = r;
345 }
346
Robert Greenwaltec05b3c2009-10-30 14:17:42 -0700347 String[] naStrings = context.getResources().getStringArray(
348 com.android.internal.R.array.networkAttributes);
349 for (String naString : naStrings) {
350 try {
Robert Greenwalt34848c02011-03-25 13:09:25 -0700351 NetworkConfig n = new NetworkConfig(naString);
Wink Savillef2a62832011-04-07 14:23:45 -0700352 if (n.type > ConnectivityManager.MAX_NETWORK_TYPE) {
Wink Savillee70c6f52010-12-03 12:01:38 -0800353 loge("Error in networkAttributes - ignoring attempt to define type " +
Wink Savillef2a62832011-04-07 14:23:45 -0700354 n.type);
Robert Greenwaltec05b3c2009-10-30 14:17:42 -0700355 continue;
Robert Greenwalt2034b912009-08-12 16:08:25 -0700356 }
Wink Savillef2a62832011-04-07 14:23:45 -0700357 if (mNetConfigs[n.type] != null) {
Wink Savillee70c6f52010-12-03 12:01:38 -0800358 loge("Error in networkAttributes - ignoring attempt to redefine type " +
Wink Savillef2a62832011-04-07 14:23:45 -0700359 n.type);
Robert Greenwaltec05b3c2009-10-30 14:17:42 -0700360 continue;
361 }
Wink Savillef2a62832011-04-07 14:23:45 -0700362 if (mRadioAttributes[n.radio] == null) {
Wink Savillee70c6f52010-12-03 12:01:38 -0800363 loge("Error in networkAttributes - ignoring attempt to use undefined " +
Wink Savillef2a62832011-04-07 14:23:45 -0700364 "radio " + n.radio + " in network type " + n.type);
Robert Greenwaltec05b3c2009-10-30 14:17:42 -0700365 continue;
366 }
Wink Savillef2a62832011-04-07 14:23:45 -0700367 mNetConfigs[n.type] = n;
Robert Greenwaltec05b3c2009-10-30 14:17:42 -0700368 mNetworksDefined++;
369 } catch(Exception e) {
370 // ignore it - leave the entry null
Robert Greenwalt2034b912009-08-12 16:08:25 -0700371 }
372 }
373
Robert Greenwalt6cac0742011-06-21 17:26:14 -0700374 mProtectedNetworks = new ArrayList<Integer>();
375 int[] protectedNetworks = context.getResources().getIntArray(
376 com.android.internal.R.array.config_protectedNetworks);
377 for (int p : protectedNetworks) {
378 if ((mNetConfigs[p] != null) && (mProtectedNetworks.contains(p) == false)) {
379 mProtectedNetworks.add(p);
380 } else {
381 if (DBG) loge("Ignoring protectedNetwork " + p);
382 }
383 }
384
Robert Greenwaltec05b3c2009-10-30 14:17:42 -0700385 // high priority first
386 mPriorityList = new int[mNetworksDefined];
387 {
388 int insertionPoint = mNetworksDefined-1;
389 int currentLowest = 0;
390 int nextLowest = 0;
391 while (insertionPoint > -1) {
Robert Greenwalt34848c02011-03-25 13:09:25 -0700392 for (NetworkConfig na : mNetConfigs) {
Robert Greenwaltec05b3c2009-10-30 14:17:42 -0700393 if (na == null) continue;
Wink Savillef2a62832011-04-07 14:23:45 -0700394 if (na.priority < currentLowest) continue;
395 if (na.priority > currentLowest) {
396 if (na.priority < nextLowest || nextLowest == 0) {
397 nextLowest = na.priority;
Robert Greenwaltec05b3c2009-10-30 14:17:42 -0700398 }
399 continue;
400 }
Wink Savillef2a62832011-04-07 14:23:45 -0700401 mPriorityList[insertionPoint--] = na.type;
Robert Greenwaltec05b3c2009-10-30 14:17:42 -0700402 }
403 currentLowest = nextLowest;
404 nextLowest = 0;
405 }
406 }
407
408 mNetRequestersPids = new ArrayList[ConnectivityManager.MAX_NETWORK_TYPE+1];
409 for (int i : mPriorityList) {
Robert Greenwalt2034b912009-08-12 16:08:25 -0700410 mNetRequestersPids[i] = new ArrayList();
411 }
412
Kazuhiro Ondocae18f12011-07-19 11:23:37 -0500413 mFeatureUsers = new ArrayList<FeatureUser>();
Robert Greenwalt2034b912009-08-12 16:08:25 -0700414
Robert Greenwaltec05b3c2009-10-30 14:17:42 -0700415 mNumDnsEntries = 0;
416
417 mTestMode = SystemProperties.get("cm.test.mode").equals("true")
418 && SystemProperties.get("ro.build.type").equals("eng");
The Android Open Source Project28527d22009-03-03 19:31:44 -0800419 /*
420 * Create the network state trackers for Wi-Fi and mobile
421 * data. Maybe this could be done with a factory class,
422 * but it's not clear that it's worth it, given that
423 * the number of different network types is not going
424 * to change very often.
425 */
Robert Greenwaltec05b3c2009-10-30 14:17:42 -0700426 for (int netType : mPriorityList) {
Wink Savillef2a62832011-04-07 14:23:45 -0700427 switch (mNetConfigs[netType].radio) {
Robert Greenwaltec05b3c2009-10-30 14:17:42 -0700428 case ConnectivityManager.TYPE_WIFI:
Wink Savillee70c6f52010-12-03 12:01:38 -0800429 if (DBG) log("Starting Wifi Service.");
Wink Saville7fabfa22010-08-13 16:11:42 -0700430 WifiStateTracker wst = new WifiStateTracker();
Irfan Sheriff25be0762010-07-28 09:35:20 -0700431 WifiService wifiService = new WifiService(context);
Robert Greenwaltec05b3c2009-10-30 14:17:42 -0700432 ServiceManager.addService(Context.WIFI_SERVICE, wifiService);
Irfan Sheriff25be0762010-07-28 09:35:20 -0700433 wifiService.checkAndStartWifi();
Robert Greenwaltec05b3c2009-10-30 14:17:42 -0700434 mNetTrackers[ConnectivityManager.TYPE_WIFI] = wst;
Wink Saville7fabfa22010-08-13 16:11:42 -0700435 wst.startMonitoring(context, mHandler);
The Android Open Source Project28527d22009-03-03 19:31:44 -0800436
Irfan Sheriff653e2a22010-06-07 09:03:04 -0700437 //TODO: as part of WWS refactor, create only when needed
Irfan Sheriff25be0762010-07-28 09:35:20 -0700438 mWifiWatchdogService = new WifiWatchdogService(context);
Irfan Sheriff653e2a22010-06-07 09:03:04 -0700439
Robert Greenwaltec05b3c2009-10-30 14:17:42 -0700440 break;
441 case ConnectivityManager.TYPE_MOBILE:
Wink Saville7fabfa22010-08-13 16:11:42 -0700442 mNetTrackers[netType] = new MobileDataStateTracker(netType,
Wink Savillef2a62832011-04-07 14:23:45 -0700443 mNetConfigs[netType].name);
Wink Saville7fabfa22010-08-13 16:11:42 -0700444 mNetTrackers[netType].startMonitoring(context, mHandler);
Robert Greenwaltec05b3c2009-10-30 14:17:42 -0700445 break;
Robert Greenwalteb123ac2010-12-06 13:56:24 -0800446 case ConnectivityManager.TYPE_DUMMY:
447 mNetTrackers[netType] = new DummyDataStateTracker(netType,
Wink Savillef2a62832011-04-07 14:23:45 -0700448 mNetConfigs[netType].name);
Robert Greenwalteb123ac2010-12-06 13:56:24 -0800449 mNetTrackers[netType].startMonitoring(context, mHandler);
450 break;
Jaikumar Ganesh0db51a02010-12-21 22:31:44 -0800451 case ConnectivityManager.TYPE_BLUETOOTH:
452 mNetTrackers[netType] = BluetoothTetheringDataTracker.getInstance();
453 mNetTrackers[netType].startMonitoring(context, mHandler);
454 break;
Benoit Goby6cec7f32010-12-22 14:29:40 -0800455 case ConnectivityManager.TYPE_ETHERNET:
456 mNetTrackers[netType] = EthernetDataTracker.getInstance();
457 mNetTrackers[netType].startMonitoring(context, mHandler);
458 break;
Robert Greenwaltec05b3c2009-10-30 14:17:42 -0700459 default:
Wink Savillee70c6f52010-12-03 12:01:38 -0800460 loge("Trying to create a DataStateTracker for an unknown radio type " +
Wink Savillef2a62832011-04-07 14:23:45 -0700461 mNetConfigs[netType].radio);
Robert Greenwaltec05b3c2009-10-30 14:17:42 -0700462 continue;
463 }
Robert Greenwalte8d2a4a2011-07-14 14:28:05 -0700464 mCurrentLinkProperties[netType] = null;
Robert Greenwaltec05b3c2009-10-30 14:17:42 -0700465 }
Robert Greenwalt0c4828c2010-01-26 11:40:34 -0800466
Robert Greenwaltc0b6c602010-03-11 15:03:08 -0800467 mTethering = new Tethering(mContext, mHandler.getLooper());
Robert Greenwalt33cdcdf2011-06-02 17:30:47 -0700468 mTetheringConfigValid = ((mTethering.getTetherableUsbRegexs().length != 0 ||
Danica Chang96567052010-08-11 14:54:43 -0700469 mTethering.getTetherableWifiRegexs().length != 0 ||
470 mTethering.getTetherableBluetoothRegexs().length != 0) &&
Robert Greenwalt33cdcdf2011-06-02 17:30:47 -0700471 mTethering.getUpstreamIfaceTypes().length != 0);
Robert Greenwaltf1b66e12010-02-25 12:29:30 -0800472
Robert Greenwalt0e80be12010-09-20 14:35:25 -0700473 if (DBG) {
474 mInetLog = new ArrayList();
475 }
Robert Greenwaltc3c5f862010-10-11 16:00:27 -0700476
477 mSettingsObserver = new SettingsObserver(mHandler, EVENT_APPLY_GLOBAL_HTTP_PROXY);
478 mSettingsObserver.observe(mContext);
Robert Greenwalt6f7c6092010-12-02 11:31:00 -0800479
480 loadGlobalProxy();
Hung-ying Tyan4e723422011-01-19 16:48:38 +0800481
482 VpnManager.startVpnService(context);
The Android Open Source Project28527d22009-03-03 19:31:44 -0800483 }
484
Robert Greenwaltec05b3c2009-10-30 14:17:42 -0700485
The Android Open Source Project28527d22009-03-03 19:31:44 -0800486 /**
Robert Greenwalt0659da32009-07-16 17:21:39 -0700487 * Sets the preferred network.
The Android Open Source Project28527d22009-03-03 19:31:44 -0800488 * @param preference the new preference
489 */
Robert Greenwalt6a2db8a2010-09-23 10:05:56 -0700490 public void setNetworkPreference(int preference) {
The Android Open Source Project28527d22009-03-03 19:31:44 -0800491 enforceChangePermission();
Robert Greenwalt6a2db8a2010-09-23 10:05:56 -0700492
493 mHandler.sendMessage(mHandler.obtainMessage(EVENT_SET_NETWORK_PREFERENCE, preference, 0));
The Android Open Source Project28527d22009-03-03 19:31:44 -0800494 }
495
496 public int getNetworkPreference() {
497 enforceAccessPermission();
Robert Greenwalt6a2db8a2010-09-23 10:05:56 -0700498 int preference;
499 synchronized(this) {
500 preference = mNetworkPreference;
501 }
502 return preference;
The Android Open Source Project28527d22009-03-03 19:31:44 -0800503 }
504
Robert Greenwalt6a2db8a2010-09-23 10:05:56 -0700505 private void handleSetNetworkPreference(int preference) {
506 if (ConnectivityManager.isNetworkTypeValid(preference) &&
Robert Greenwalt34848c02011-03-25 13:09:25 -0700507 mNetConfigs[preference] != null &&
508 mNetConfigs[preference].isDefault()) {
Robert Greenwalt6a2db8a2010-09-23 10:05:56 -0700509 if (mNetworkPreference != preference) {
510 final ContentResolver cr = mContext.getContentResolver();
511 Settings.Secure.putInt(cr, Settings.Secure.NETWORK_PREFERENCE, preference);
512 synchronized(this) {
513 mNetworkPreference = preference;
514 }
515 enforcePreference();
516 }
517 }
The Android Open Source Project28527d22009-03-03 19:31:44 -0800518 }
Robert Greenwalt0659da32009-07-16 17:21:39 -0700519
Wink Saville4f0de1e2011-08-04 15:01:58 -0700520 private int getConnectivityChangeDelay() {
521 final ContentResolver cr = mContext.getContentResolver();
522
523 /** Check system properties for the default value then use secure settings value, if any. */
524 int defaultDelay = SystemProperties.getInt(
525 "conn." + Settings.Secure.CONNECTIVITY_CHANGE_DELAY,
526 Settings.Secure.CONNECTIVITY_CHANGE_DELAY_DEFAULT);
527 return Settings.Secure.getInt(cr, Settings.Secure.CONNECTIVITY_CHANGE_DELAY,
528 defaultDelay);
529 }
530
The Android Open Source Project28527d22009-03-03 19:31:44 -0800531 private int getPersistedNetworkPreference() {
532 final ContentResolver cr = mContext.getContentResolver();
533
534 final int networkPrefSetting = Settings.Secure
535 .getInt(cr, Settings.Secure.NETWORK_PREFERENCE, -1);
536 if (networkPrefSetting != -1) {
537 return networkPrefSetting;
538 }
539
540 return ConnectivityManager.DEFAULT_NETWORK_PREFERENCE;
541 }
Robert Greenwalt0659da32009-07-16 17:21:39 -0700542
The Android Open Source Project28527d22009-03-03 19:31:44 -0800543 /**
Robert Greenwalt0659da32009-07-16 17:21:39 -0700544 * Make the state of network connectivity conform to the preference settings
The Android Open Source Project28527d22009-03-03 19:31:44 -0800545 * In this method, we only tear down a non-preferred network. Establishing
546 * a connection to the preferred network is taken care of when we handle
547 * the disconnect event from the non-preferred network
548 * (see {@link #handleDisconnect(NetworkInfo)}).
549 */
550 private void enforcePreference() {
Robert Greenwalt2034b912009-08-12 16:08:25 -0700551 if (mNetTrackers[mNetworkPreference].getNetworkInfo().isConnected())
The Android Open Source Project28527d22009-03-03 19:31:44 -0800552 return;
553
Robert Greenwalt2034b912009-08-12 16:08:25 -0700554 if (!mNetTrackers[mNetworkPreference].isAvailable())
555 return;
The Android Open Source Project28527d22009-03-03 19:31:44 -0800556
Robert Greenwalt2034b912009-08-12 16:08:25 -0700557 for (int t=0; t <= ConnectivityManager.MAX_RADIO_TYPE; t++) {
Robert Greenwaltec05b3c2009-10-30 14:17:42 -0700558 if (t != mNetworkPreference && mNetTrackers[t] != null &&
Robert Greenwalt2034b912009-08-12 16:08:25 -0700559 mNetTrackers[t].getNetworkInfo().isConnected()) {
Robert Greenwaltf3f045b2009-08-20 15:25:14 -0700560 if (DBG) {
Wink Savillee70c6f52010-12-03 12:01:38 -0800561 log("tearing down " + mNetTrackers[t].getNetworkInfo() +
Robert Greenwaltf3f045b2009-08-20 15:25:14 -0700562 " in enforcePreference");
563 }
Robert Greenwalt2034b912009-08-12 16:08:25 -0700564 teardown(mNetTrackers[t]);
The Android Open Source Project28527d22009-03-03 19:31:44 -0800565 }
566 }
567 }
568
569 private boolean teardown(NetworkStateTracker netTracker) {
570 if (netTracker.teardown()) {
571 netTracker.setTeardownRequested(true);
572 return true;
573 } else {
574 return false;
575 }
576 }
577
578 /**
579 * Return NetworkInfo for the active (i.e., connected) network interface.
580 * It is assumed that at most one network is active at a time. If more
581 * than one is active, it is indeterminate which will be returned.
Robert Greenwalt0659da32009-07-16 17:21:39 -0700582 * @return the info for the active network, or {@code null} if none is
583 * active
The Android Open Source Project28527d22009-03-03 19:31:44 -0800584 */
585 public NetworkInfo getActiveNetworkInfo() {
Robert Greenwalte1544bb2011-05-20 12:23:41 -0700586 return getNetworkInfo(mActiveDefaultNetwork);
The Android Open Source Project28527d22009-03-03 19:31:44 -0800587 }
588
589 public NetworkInfo getNetworkInfo(int networkType) {
590 enforceAccessPermission();
591 if (ConnectivityManager.isNetworkTypeValid(networkType)) {
592 NetworkStateTracker t = mNetTrackers[networkType];
593 if (t != null)
594 return t.getNetworkInfo();
595 }
596 return null;
597 }
598
599 public NetworkInfo[] getAllNetworkInfo() {
600 enforceAccessPermission();
Robert Greenwaltec05b3c2009-10-30 14:17:42 -0700601 NetworkInfo[] result = new NetworkInfo[mNetworksDefined];
The Android Open Source Project28527d22009-03-03 19:31:44 -0800602 int i = 0;
603 for (NetworkStateTracker t : mNetTrackers) {
Robert Greenwaltec05b3c2009-10-30 14:17:42 -0700604 if(t != null) result[i++] = t.getNetworkInfo();
The Android Open Source Project28527d22009-03-03 19:31:44 -0800605 }
606 return result;
607 }
608
Robert Greenwalt9f0ee4f2010-09-14 09:18:02 -0700609 /**
610 * Return LinkProperties for the active (i.e., connected) default
611 * network interface. It is assumed that at most one default network
612 * is active at a time. If more than one is active, it is indeterminate
613 * which will be returned.
614 * @return the ip properties for the active network, or {@code null} if
615 * none is active
616 */
617 public LinkProperties getActiveLinkProperties() {
Robert Greenwalte1544bb2011-05-20 12:23:41 -0700618 return getLinkProperties(mActiveDefaultNetwork);
Robert Greenwalt9f0ee4f2010-09-14 09:18:02 -0700619 }
620
621 public LinkProperties getLinkProperties(int networkType) {
622 enforceAccessPermission();
623 if (ConnectivityManager.isNetworkTypeValid(networkType)) {
624 NetworkStateTracker t = mNetTrackers[networkType];
625 if (t != null) return t.getLinkProperties();
626 }
627 return null;
628 }
629
The Android Open Source Project28527d22009-03-03 19:31:44 -0800630 public boolean setRadios(boolean turnOn) {
631 boolean result = true;
632 enforceChangePermission();
633 for (NetworkStateTracker t : mNetTrackers) {
Robert Greenwaltec05b3c2009-10-30 14:17:42 -0700634 if (t != null) result = t.setRadio(turnOn) && result;
The Android Open Source Project28527d22009-03-03 19:31:44 -0800635 }
636 return result;
637 }
638
639 public boolean setRadio(int netType, boolean turnOn) {
640 enforceChangePermission();
641 if (!ConnectivityManager.isNetworkTypeValid(netType)) {
642 return false;
643 }
644 NetworkStateTracker tracker = mNetTrackers[netType];
645 return tracker != null && tracker.setRadio(turnOn);
646 }
647
Robert Greenwaltaffc3a12009-09-27 17:27:04 -0700648 /**
649 * Used to notice when the calling process dies so we can self-expire
650 *
651 * Also used to know if the process has cleaned up after itself when
652 * our auto-expire timer goes off. The timer has a link to an object.
653 *
654 */
Robert Greenwalt2034b912009-08-12 16:08:25 -0700655 private class FeatureUser implements IBinder.DeathRecipient {
656 int mNetworkType;
657 String mFeature;
658 IBinder mBinder;
659 int mPid;
660 int mUid;
Robert Greenwalt3eeb6032009-12-21 18:24:07 -0800661 long mCreateTime;
Robert Greenwalt2034b912009-08-12 16:08:25 -0700662
663 FeatureUser(int type, String feature, IBinder binder) {
664 super();
665 mNetworkType = type;
666 mFeature = feature;
667 mBinder = binder;
668 mPid = getCallingPid();
669 mUid = getCallingUid();
Robert Greenwalt3eeb6032009-12-21 18:24:07 -0800670 mCreateTime = System.currentTimeMillis();
Robert Greenwaltaffc3a12009-09-27 17:27:04 -0700671
Robert Greenwalt2034b912009-08-12 16:08:25 -0700672 try {
673 mBinder.linkToDeath(this, 0);
674 } catch (RemoteException e) {
675 binderDied();
676 }
677 }
678
679 void unlinkDeathRecipient() {
680 mBinder.unlinkToDeath(this, 0);
681 }
682
683 public void binderDied() {
Wink Savillee70c6f52010-12-03 12:01:38 -0800684 log("ConnectivityService FeatureUser binderDied(" +
Robert Greenwalt3eeb6032009-12-21 18:24:07 -0800685 mNetworkType + ", " + mFeature + ", " + mBinder + "), created " +
686 (System.currentTimeMillis() - mCreateTime) + " mSec ago");
Robert Greenwaltaffc3a12009-09-27 17:27:04 -0700687 stopUsingNetworkFeature(this, false);
Robert Greenwalt2034b912009-08-12 16:08:25 -0700688 }
689
Robert Greenwaltaffc3a12009-09-27 17:27:04 -0700690 public void expire() {
Wink Savillee70c6f52010-12-03 12:01:38 -0800691 log("ConnectivityService FeatureUser expire(" +
Robert Greenwalt3eeb6032009-12-21 18:24:07 -0800692 mNetworkType + ", " + mFeature + ", " + mBinder +"), created " +
693 (System.currentTimeMillis() - mCreateTime) + " mSec ago");
Robert Greenwaltaffc3a12009-09-27 17:27:04 -0700694 stopUsingNetworkFeature(this, false);
695 }
Robert Greenwalt3eeb6032009-12-21 18:24:07 -0800696
Kazuhiro Ondocae18f12011-07-19 11:23:37 -0500697 public boolean isSameUser(FeatureUser u) {
698 if (u == null) return false;
699
700 return isSameUser(u.mPid, u.mUid, u.mNetworkType, u.mFeature);
701 }
702
703 public boolean isSameUser(int pid, int uid, int networkType, String feature) {
704 if ((mPid == pid) && (mUid == uid) && (mNetworkType == networkType) &&
705 TextUtils.equals(mFeature, feature)) {
706 return true;
707 }
708 return false;
709 }
710
Robert Greenwalt3eeb6032009-12-21 18:24:07 -0800711 public String toString() {
712 return "FeatureUser("+mNetworkType+","+mFeature+","+mPid+","+mUid+"), created " +
713 (System.currentTimeMillis() - mCreateTime) + " mSec ago";
714 }
Robert Greenwalt2034b912009-08-12 16:08:25 -0700715 }
716
Robert Greenwaltaffc3a12009-09-27 17:27:04 -0700717 // javadoc from interface
Robert Greenwalt2034b912009-08-12 16:08:25 -0700718 public int startUsingNetworkFeature(int networkType, String feature,
719 IBinder binder) {
720 if (DBG) {
Wink Savillee70c6f52010-12-03 12:01:38 -0800721 log("startUsingNetworkFeature for net " + networkType + ": " + feature);
Robert Greenwalt2034b912009-08-12 16:08:25 -0700722 }
The Android Open Source Project28527d22009-03-03 19:31:44 -0800723 enforceChangePermission();
Robert Greenwaltec05b3c2009-10-30 14:17:42 -0700724 if (!ConnectivityManager.isNetworkTypeValid(networkType) ||
Robert Greenwalt34848c02011-03-25 13:09:25 -0700725 mNetConfigs[networkType] == null) {
Robert Greenwalt2034b912009-08-12 16:08:25 -0700726 return Phone.APN_REQUEST_FAILED;
The Android Open Source Project28527d22009-03-03 19:31:44 -0800727 }
Robert Greenwalt2034b912009-08-12 16:08:25 -0700728
Robert Greenwaltaffc3a12009-09-27 17:27:04 -0700729 FeatureUser f = new FeatureUser(networkType, feature, binder);
Robert Greenwalt2034b912009-08-12 16:08:25 -0700730
731 // TODO - move this into the MobileDataStateTracker
732 int usedNetworkType = networkType;
733 if(networkType == ConnectivityManager.TYPE_MOBILE) {
Wink Savilleb7c92c72011-03-12 14:52:01 -0800734 usedNetworkType = convertFeatureToNetworkType(feature);
735 if (usedNetworkType < 0) {
736 Slog.e(TAG, "Can't match any netTracker!");
737 usedNetworkType = networkType;
Robert Greenwalt2034b912009-08-12 16:08:25 -0700738 }
739 }
Robert Greenwalt6cac0742011-06-21 17:26:14 -0700740
741 if (mProtectedNetworks.contains(usedNetworkType)) {
742 enforceConnectivityInternalPermission();
743 }
744
Robert Greenwalt2034b912009-08-12 16:08:25 -0700745 NetworkStateTracker network = mNetTrackers[usedNetworkType];
746 if (network != null) {
Robert Greenwalt5364d752010-12-15 13:26:33 -0800747 Integer currentPid = new Integer(getCallingPid());
Robert Greenwalt2034b912009-08-12 16:08:25 -0700748 if (usedNetworkType != networkType) {
Robert Greenwalt2034b912009-08-12 16:08:25 -0700749 NetworkStateTracker radio = mNetTrackers[networkType];
750 NetworkInfo ni = network.getNetworkInfo();
751
752 if (ni.isAvailable() == false) {
Wink Savillee70c6f52010-12-03 12:01:38 -0800753 if (DBG) log("special network not available");
Robert Greenwalt2cc87442010-12-29 14:35:21 -0800754 if (!TextUtils.equals(feature,Phone.FEATURE_ENABLE_DUN_ALWAYS)) {
755 return Phone.APN_TYPE_NOT_AVAILABLE;
756 } else {
757 // else make the attempt anyway - probably giving REQUEST_STARTED below
758 }
Robert Greenwalt2034b912009-08-12 16:08:25 -0700759 }
760
Kazuhiro Ondocae18f12011-07-19 11:23:37 -0500761 int restoreTimer = getRestoreDefaultNetworkDelay(usedNetworkType);
762
Robert Greenwaltaffc3a12009-09-27 17:27:04 -0700763 synchronized(this) {
Kazuhiro Ondocae18f12011-07-19 11:23:37 -0500764 boolean addToList = true;
765 if (restoreTimer < 0) {
766 // In case there is no timer is specified for the feature,
767 // make sure we don't add duplicate entry with the same request.
768 for (FeatureUser u : mFeatureUsers) {
769 if (u.isSameUser(f)) {
770 // Duplicate user is found. Do not add.
771 addToList = false;
772 break;
773 }
774 }
775 }
776
777 if (addToList) mFeatureUsers.add(f);
Robert Greenwaltaffc3a12009-09-27 17:27:04 -0700778 if (!mNetRequestersPids[usedNetworkType].contains(currentPid)) {
779 // this gets used for per-pid dns when connected
780 mNetRequestersPids[usedNetworkType].add(currentPid);
781 }
Robert Greenwalt2034b912009-08-12 16:08:25 -0700782 }
Robert Greenwaltaffc3a12009-09-27 17:27:04 -0700783
Robert Greenwalt20f819c2011-05-03 19:02:44 -0700784 if (restoreTimer >= 0) {
785 mHandler.sendMessageDelayed(
786 mHandler.obtainMessage(EVENT_RESTORE_DEFAULT_NETWORK, f), restoreTimer);
787 }
Robert Greenwalt2034b912009-08-12 16:08:25 -0700788
Robert Greenwalta52c75a2009-08-19 20:19:33 -0700789 if ((ni.isConnectedOrConnecting() == true) &&
790 !network.isTeardownRequested()) {
Robert Greenwalt2034b912009-08-12 16:08:25 -0700791 if (ni.isConnected() == true) {
792 // add the pid-specific dns
Robert Greenwalt3afbead2010-07-23 15:46:26 -0700793 handleDnsConfigurationChange(networkType);
Wink Savillee70c6f52010-12-03 12:01:38 -0800794 if (DBG) log("special network already active");
Robert Greenwalt2034b912009-08-12 16:08:25 -0700795 return Phone.APN_ALREADY_ACTIVE;
796 }
Wink Savillee70c6f52010-12-03 12:01:38 -0800797 if (DBG) log("special network already connecting");
Robert Greenwalt2034b912009-08-12 16:08:25 -0700798 return Phone.APN_REQUEST_STARTED;
799 }
800
801 // check if the radio in play can make another contact
802 // assume if cannot for now
803
Wink Savillee70c6f52010-12-03 12:01:38 -0800804 if (DBG) log("reconnecting to special network");
Robert Greenwalt2034b912009-08-12 16:08:25 -0700805 network.reconnect();
806 return Phone.APN_REQUEST_STARTED;
807 } else {
Robert Greenwalt5364d752010-12-15 13:26:33 -0800808 // need to remember this unsupported request so we respond appropriately on stop
809 synchronized(this) {
810 mFeatureUsers.add(f);
811 if (!mNetRequestersPids[usedNetworkType].contains(currentPid)) {
812 // this gets used for per-pid dns when connected
813 mNetRequestersPids[usedNetworkType].add(currentPid);
814 }
815 }
Robert Greenwaltd391e892010-05-18 10:52:51 -0700816 return -1;
Robert Greenwalt2034b912009-08-12 16:08:25 -0700817 }
818 }
819 return Phone.APN_TYPE_NOT_AVAILABLE;
The Android Open Source Project28527d22009-03-03 19:31:44 -0800820 }
821
Robert Greenwaltaffc3a12009-09-27 17:27:04 -0700822 // javadoc from interface
The Android Open Source Project28527d22009-03-03 19:31:44 -0800823 public int stopUsingNetworkFeature(int networkType, String feature) {
Robert Greenwalt28f43012009-10-06 17:52:40 -0700824 enforceChangePermission();
825
Robert Greenwaltaffc3a12009-09-27 17:27:04 -0700826 int pid = getCallingPid();
827 int uid = getCallingUid();
828
829 FeatureUser u = null;
830 boolean found = false;
831
832 synchronized(this) {
Kazuhiro Ondocae18f12011-07-19 11:23:37 -0500833 for (FeatureUser x : mFeatureUsers) {
834 if (x.isSameUser(pid, uid, networkType, feature)) {
835 u = x;
Robert Greenwaltaffc3a12009-09-27 17:27:04 -0700836 found = true;
837 break;
838 }
839 }
840 }
841 if (found && u != null) {
842 // stop regardless of how many other time this proc had called start
843 return stopUsingNetworkFeature(u, true);
844 } else {
845 // none found!
Wink Savillee70c6f52010-12-03 12:01:38 -0800846 if (DBG) log("ignoring stopUsingNetworkFeature - not a live request");
Robert Greenwaltaffc3a12009-09-27 17:27:04 -0700847 return 1;
848 }
Robert Greenwalt2034b912009-08-12 16:08:25 -0700849 }
850
Robert Greenwaltaffc3a12009-09-27 17:27:04 -0700851 private int stopUsingNetworkFeature(FeatureUser u, boolean ignoreDups) {
852 int networkType = u.mNetworkType;
853 String feature = u.mFeature;
854 int pid = u.mPid;
855 int uid = u.mUid;
856
857 NetworkStateTracker tracker = null;
858 boolean callTeardown = false; // used to carry our decision outside of sync block
859
Robert Greenwalt2034b912009-08-12 16:08:25 -0700860 if (DBG) {
Wink Savillee70c6f52010-12-03 12:01:38 -0800861 log("stopUsingNetworkFeature for net " + networkType +
Robert Greenwalt2034b912009-08-12 16:08:25 -0700862 ": " + feature);
863 }
Robert Greenwalt28f43012009-10-06 17:52:40 -0700864
The Android Open Source Project28527d22009-03-03 19:31:44 -0800865 if (!ConnectivityManager.isNetworkTypeValid(networkType)) {
866 return -1;
867 }
Robert Greenwalt2034b912009-08-12 16:08:25 -0700868
Robert Greenwaltaffc3a12009-09-27 17:27:04 -0700869 // need to link the mFeatureUsers list with the mNetRequestersPids state in this
870 // sync block
871 synchronized(this) {
872 // check if this process still has an outstanding start request
873 if (!mFeatureUsers.contains(u)) {
Wink Savillee70c6f52010-12-03 12:01:38 -0800874 if (DBG) log("ignoring - this process has no outstanding requests");
Robert Greenwalt2034b912009-08-12 16:08:25 -0700875 return 1;
876 }
Robert Greenwaltaffc3a12009-09-27 17:27:04 -0700877 u.unlinkDeathRecipient();
878 mFeatureUsers.remove(mFeatureUsers.indexOf(u));
879 // If we care about duplicate requests, check for that here.
880 //
881 // This is done to support the extension of a request - the app
882 // can request we start the network feature again and renew the
883 // auto-shutoff delay. Normal "stop" calls from the app though
884 // do not pay attention to duplicate requests - in effect the
885 // API does not refcount and a single stop will counter multiple starts.
886 if (ignoreDups == false) {
Kazuhiro Ondocae18f12011-07-19 11:23:37 -0500887 for (FeatureUser x : mFeatureUsers) {
888 if (x.isSameUser(u)) {
Wink Savillee70c6f52010-12-03 12:01:38 -0800889 if (DBG) log("ignoring stopUsingNetworkFeature as dup is found");
Robert Greenwaltaffc3a12009-09-27 17:27:04 -0700890 return 1;
891 }
892 }
Robert Greenwalt2034b912009-08-12 16:08:25 -0700893 }
Robert Greenwaltaffc3a12009-09-27 17:27:04 -0700894
895 // TODO - move to MobileDataStateTracker
896 int usedNetworkType = networkType;
897 if (networkType == ConnectivityManager.TYPE_MOBILE) {
Wink Savilleb7c92c72011-03-12 14:52:01 -0800898 usedNetworkType = convertFeatureToNetworkType(feature);
899 if (usedNetworkType < 0) {
900 usedNetworkType = networkType;
Robert Greenwaltaffc3a12009-09-27 17:27:04 -0700901 }
902 }
903 tracker = mNetTrackers[usedNetworkType];
Robert Greenwaltec05b3c2009-10-30 14:17:42 -0700904 if (tracker == null) {
Wink Savillee70c6f52010-12-03 12:01:38 -0800905 if (DBG) log("ignoring - no known tracker for net type " + usedNetworkType);
Robert Greenwaltec05b3c2009-10-30 14:17:42 -0700906 return -1;
907 }
908 if (usedNetworkType != networkType) {
Robert Greenwaltaffc3a12009-09-27 17:27:04 -0700909 Integer currentPid = new Integer(pid);
Robert Greenwaltaffc3a12009-09-27 17:27:04 -0700910 mNetRequestersPids[usedNetworkType].remove(currentPid);
Robert Greenwalt0ca68a02009-12-17 14:54:59 -0800911 reassessPidDns(pid, true);
Robert Greenwaltaffc3a12009-09-27 17:27:04 -0700912 if (mNetRequestersPids[usedNetworkType].size() != 0) {
Wink Savillee70c6f52010-12-03 12:01:38 -0800913 if (DBG) log("not tearing down special network - " +
Robert Greenwaltaffc3a12009-09-27 17:27:04 -0700914 "others still using it");
915 return 1;
916 }
917 callTeardown = true;
Robert Greenwalt9f3be4c2011-01-10 11:58:31 -0800918 } else {
919 if (DBG) log("not a known feature - dropping");
Robert Greenwaltaffc3a12009-09-27 17:27:04 -0700920 }
921 }
Wink Savillee70c6f52010-12-03 12:01:38 -0800922 if (DBG) log("Doing network teardown");
Robert Greenwaltaffc3a12009-09-27 17:27:04 -0700923 if (callTeardown) {
924 tracker.teardown();
Robert Greenwalt2034b912009-08-12 16:08:25 -0700925 return 1;
926 } else {
Robert Greenwaltd391e892010-05-18 10:52:51 -0700927 return -1;
Robert Greenwalt2034b912009-08-12 16:08:25 -0700928 }
The Android Open Source Project28527d22009-03-03 19:31:44 -0800929 }
930
931 /**
Robert Greenwalt7fe44cb2010-08-27 09:24:29 -0700932 * @deprecated use requestRouteToHostAddress instead
933 *
The Android Open Source Project28527d22009-03-03 19:31:44 -0800934 * Ensure that a network route exists to deliver traffic to the specified
935 * host via the specified network interface.
Robert Greenwalt0659da32009-07-16 17:21:39 -0700936 * @param networkType the type of the network over which traffic to the
937 * specified host is to be routed
938 * @param hostAddress the IP address of the host to which the route is
939 * desired
The Android Open Source Project28527d22009-03-03 19:31:44 -0800940 * @return {@code true} on success, {@code false} on failure
941 */
942 public boolean requestRouteToHost(int networkType, int hostAddress) {
Robert Greenwalt7fe44cb2010-08-27 09:24:29 -0700943 InetAddress inetAddress = NetworkUtils.intToInetAddress(hostAddress);
944
945 if (inetAddress == null) {
946 return false;
947 }
948
949 return requestRouteToHostAddress(networkType, inetAddress.getAddress());
950 }
951
952 /**
953 * Ensure that a network route exists to deliver traffic to the specified
954 * host via the specified network interface.
955 * @param networkType the type of the network over which traffic to the
956 * specified host is to be routed
957 * @param hostAddress the IP address of the host to which the route is
958 * desired
959 * @return {@code true} on success, {@code false} on failure
960 */
961 public boolean requestRouteToHostAddress(int networkType, byte[] hostAddress) {
The Android Open Source Project28527d22009-03-03 19:31:44 -0800962 enforceChangePermission();
Robert Greenwalt6cac0742011-06-21 17:26:14 -0700963 if (mProtectedNetworks.contains(networkType)) {
964 enforceConnectivityInternalPermission();
965 }
966
The Android Open Source Project28527d22009-03-03 19:31:44 -0800967 if (!ConnectivityManager.isNetworkTypeValid(networkType)) {
968 return false;
969 }
970 NetworkStateTracker tracker = mNetTrackers[networkType];
Robert Greenwalt4666ed02009-09-10 15:06:20 -0700971
Robert Greenwaltec05b3c2009-10-30 14:17:42 -0700972 if (tracker == null || !tracker.getNetworkInfo().isConnected() ||
973 tracker.isTeardownRequested()) {
Robert Greenwalt4666ed02009-09-10 15:06:20 -0700974 if (DBG) {
Wink Savillee70c6f52010-12-03 12:01:38 -0800975 log("requestRouteToHostAddress on down network " +
Robert Greenwalt7fe44cb2010-08-27 09:24:29 -0700976 "(" + networkType + ") - dropped");
Robert Greenwalt4666ed02009-09-10 15:06:20 -0700977 }
978 return false;
The Android Open Source Project28527d22009-03-03 19:31:44 -0800979 }
Robert Greenwalta7dfbd32010-06-15 15:43:39 -0700980 try {
Robert Greenwalt7fe44cb2010-08-27 09:24:29 -0700981 InetAddress addr = InetAddress.getByAddress(hostAddress);
Robert Greenwalte8d2a4a2011-07-14 14:28:05 -0700982 LinkProperties lp = tracker.getLinkProperties();
Robert Greenwalt98107422011-07-22 11:55:33 -0700983 return addRouteToAddress(lp, addr);
Robert Greenwalta7dfbd32010-06-15 15:43:39 -0700984 } catch (UnknownHostException e) {}
985 return false;
Irfan Sheriff7f132d92010-06-09 15:39:36 -0700986 }
987
Robert Greenwalte8d2a4a2011-07-14 14:28:05 -0700988 private boolean addRoute(LinkProperties p, RouteInfo r) {
989 return modifyRoute(p.getInterfaceName(), p, r, 0, true);
Robert Greenwaltbd492212011-05-06 17:10:53 -0700990 }
991
Robert Greenwalte8d2a4a2011-07-14 14:28:05 -0700992 private boolean removeRoute(LinkProperties p, RouteInfo r) {
993 return modifyRoute(p.getInterfaceName(), p, r, 0, false);
994 }
995
Robert Greenwalt98107422011-07-22 11:55:33 -0700996 private boolean addRouteToAddress(LinkProperties lp, InetAddress addr) {
997 return modifyRouteToAddress(lp, addr, true);
998 }
999
1000 private boolean removeRouteToAddress(LinkProperties lp, InetAddress addr) {
1001 return modifyRouteToAddress(lp, addr, false);
1002 }
1003
1004 private boolean modifyRouteToAddress(LinkProperties lp, InetAddress addr, boolean doAdd) {
1005 RouteInfo bestRoute = RouteInfo.selectBestRoute(lp.getRoutes(), addr);
1006 if (bestRoute == null) {
1007 bestRoute = RouteInfo.makeHostRoute(addr);
1008 } else {
1009 if (bestRoute.getGateway().equals(addr)) {
1010 // if there is no better route, add the implied hostroute for our gateway
1011 bestRoute = RouteInfo.makeHostRoute(addr);
1012 } else {
1013 // if we will connect to this through another route, add a direct route
1014 // to it's gateway
1015 bestRoute = RouteInfo.makeHostRoute(addr, bestRoute.getGateway());
1016 }
1017 }
1018 return modifyRoute(lp.getInterfaceName(), lp, bestRoute, 0, doAdd);
1019 }
1020
Robert Greenwalte8d2a4a2011-07-14 14:28:05 -07001021 private boolean modifyRoute(String ifaceName, LinkProperties lp, RouteInfo r, int cycleCount,
1022 boolean doAdd) {
1023 if ((ifaceName == null) || (lp == null) || (r == null)) return false;
1024
1025 if (cycleCount > MAX_HOSTROUTE_CYCLE_COUNT) {
1026 loge("Error adding route - too much recursion");
1027 return false;
1028 }
1029
1030 if (r.isHostRoute() == false) {
1031 RouteInfo bestRoute = RouteInfo.selectBestRoute(lp.getRoutes(), r.getGateway());
1032 if (bestRoute != null) {
Robert Greenwalt57c83bb2011-07-15 09:45:08 -07001033 if (bestRoute.getGateway().equals(r.getGateway())) {
1034 // if there is no better route, add the implied hostroute for our gateway
Robert Greenwalte8d2a4a2011-07-14 14:28:05 -07001035 bestRoute = RouteInfo.makeHostRoute(r.getGateway());
Robert Greenwalt57c83bb2011-07-15 09:45:08 -07001036 } else {
1037 // if we will connect to our gateway through another route, add a direct
1038 // route to it's gateway
1039 bestRoute = RouteInfo.makeHostRoute(r.getGateway(), bestRoute.getGateway());
Robert Greenwalte8d2a4a2011-07-14 14:28:05 -07001040 }
Robert Greenwalt57c83bb2011-07-15 09:45:08 -07001041 modifyRoute(ifaceName, lp, bestRoute, cycleCount+1, doAdd);
Robert Greenwalte8d2a4a2011-07-14 14:28:05 -07001042 }
1043 }
1044 if (doAdd) {
1045 if (DBG) log("Adding " + r + " for interface " + ifaceName);
1046 mAddedRoutes.add(r);
1047 try {
1048 mNetd.addRoute(ifaceName, r);
1049 } catch (Exception e) {
1050 // never crash - catch them all
1051 loge("Exception trying to add a route: " + e);
1052 return false;
1053 }
1054 } else {
1055 // if we remove this one and there are no more like it, then refcount==0 and
1056 // we can remove it from the table
1057 mAddedRoutes.remove(r);
1058 if (mAddedRoutes.contains(r) == false) {
1059 if (DBG) log("Removing " + r + " for interface " + ifaceName);
1060 try {
1061 mNetd.removeRoute(ifaceName, r);
1062 } catch (Exception e) {
1063 // never crash - catch them all
1064 loge("Exception trying to remove a route: " + e);
1065 return false;
1066 }
1067 } else {
1068 if (DBG) log("not removing " + r + " as it's still in use");
1069 }
1070 }
1071 return true;
The Android Open Source Project28527d22009-03-03 19:31:44 -08001072 }
1073
1074 /**
1075 * @see ConnectivityManager#getBackgroundDataSetting()
1076 */
1077 public boolean getBackgroundDataSetting() {
Robert Greenwaltd62c7002010-12-29 16:15:02 -08001078 return mBackgroundDataEnabled.get();
The Android Open Source Project28527d22009-03-03 19:31:44 -08001079 }
Robert Greenwalt0659da32009-07-16 17:21:39 -07001080
The Android Open Source Project28527d22009-03-03 19:31:44 -08001081 /**
1082 * @see ConnectivityManager#setBackgroundDataSetting(boolean)
1083 */
1084 public void setBackgroundDataSetting(boolean allowBackgroundDataUsage) {
1085 mContext.enforceCallingOrSelfPermission(
1086 android.Manifest.permission.CHANGE_BACKGROUND_DATA_SETTING,
1087 "ConnectivityService");
Robert Greenwalt0659da32009-07-16 17:21:39 -07001088
Robert Greenwaltd62c7002010-12-29 16:15:02 -08001089 mBackgroundDataEnabled.set(allowBackgroundDataUsage);
1090
Robert Greenwalt6a2db8a2010-09-23 10:05:56 -07001091 mHandler.sendMessage(mHandler.obtainMessage(EVENT_SET_BACKGROUND_DATA,
1092 (allowBackgroundDataUsage ? ENABLED : DISABLED), 0));
1093 }
The Android Open Source Project28527d22009-03-03 19:31:44 -08001094
Robert Greenwalt6a2db8a2010-09-23 10:05:56 -07001095 private void handleSetBackgroundData(boolean enabled) {
Robert Greenwalt0ffdef12011-02-25 13:44:09 -08001096 Settings.Secure.putInt(mContext.getContentResolver(),
1097 Settings.Secure.BACKGROUND_DATA, enabled ? 1 : 0);
1098 Intent broadcast = new Intent(
1099 ConnectivityManager.ACTION_BACKGROUND_DATA_SETTING_CHANGED);
1100 mContext.sendBroadcast(broadcast);
Robert Greenwalt0659da32009-07-16 17:21:39 -07001101 }
1102
Robert Greenwalt1b21f6c2010-02-23 18:58:05 -08001103 /**
1104 * @see ConnectivityManager#getMobileDataEnabled()
1105 */
1106 public boolean getMobileDataEnabled() {
Wink Savilleb9024c62010-12-07 10:31:02 -08001107 // TODO: This detail should probably be in DataConnectionTracker's
1108 // which is where we store the value and maybe make this
1109 // asynchronous.
Robert Greenwalt1b21f6c2010-02-23 18:58:05 -08001110 enforceAccessPermission();
1111 boolean retVal = Settings.Secure.getInt(mContext.getContentResolver(),
1112 Settings.Secure.MOBILE_DATA, 1) == 1;
Wink Savillee70c6f52010-12-03 12:01:38 -08001113 if (DBG) log("getMobileDataEnabled returning " + retVal);
Robert Greenwalt1b21f6c2010-02-23 18:58:05 -08001114 return retVal;
1115 }
1116
Robert Greenwalt34848c02011-03-25 13:09:25 -07001117 public void setDataDependency(int networkType, boolean met) {
Robert Greenwalt6cac0742011-06-21 17:26:14 -07001118 enforceConnectivityInternalPermission();
1119
Robert Greenwalt34848c02011-03-25 13:09:25 -07001120 mHandler.sendMessage(mHandler.obtainMessage(EVENT_SET_DEPENDENCY_MET,
1121 (met ? ENABLED : DISABLED), networkType));
1122 }
1123
1124 private void handleSetDependencyMet(int networkType, boolean met) {
1125 if (mNetTrackers[networkType] != null) {
1126 if (DBG) {
1127 log("handleSetDependencyMet(" + networkType + ", " + met + ")");
1128 }
1129 mNetTrackers[networkType].setDependencyMet(met);
1130 }
1131 }
1132
Robert Greenwalt1b21f6c2010-02-23 18:58:05 -08001133 /**
1134 * @see ConnectivityManager#setMobileDataEnabled(boolean)
1135 */
Robert Greenwalt6a2db8a2010-09-23 10:05:56 -07001136 public void setMobileDataEnabled(boolean enabled) {
Robert Greenwalt1b21f6c2010-02-23 18:58:05 -08001137 enforceChangePermission();
Wink Savillee70c6f52010-12-03 12:01:38 -08001138 if (DBG) log("setMobileDataEnabled(" + enabled + ")");
Robert Greenwalt1b21f6c2010-02-23 18:58:05 -08001139
Robert Greenwalt6a2db8a2010-09-23 10:05:56 -07001140 mHandler.sendMessage(mHandler.obtainMessage(EVENT_SET_MOBILE_DATA,
Robert Greenwalt34848c02011-03-25 13:09:25 -07001141 (enabled ? ENABLED : DISABLED), 0));
Robert Greenwalt6a2db8a2010-09-23 10:05:56 -07001142 }
1143
1144 private void handleSetMobileData(boolean enabled) {
Wink Savilleb9024c62010-12-07 10:31:02 -08001145 if (mNetTrackers[ConnectivityManager.TYPE_MOBILE] != null) {
1146 if (DBG) {
1147 Slog.d(TAG, mNetTrackers[ConnectivityManager.TYPE_MOBILE].toString() + enabled);
Robert Greenwalt1b21f6c2010-02-23 18:58:05 -08001148 }
Wink Savilleb9024c62010-12-07 10:31:02 -08001149 mNetTrackers[ConnectivityManager.TYPE_MOBILE].setDataEnable(enabled);
Robert Greenwalt1b21f6c2010-02-23 18:58:05 -08001150 }
1151 }
1152
The Android Open Source Project28527d22009-03-03 19:31:44 -08001153 private void enforceAccessPermission() {
Robert Greenwalt0659da32009-07-16 17:21:39 -07001154 mContext.enforceCallingOrSelfPermission(
1155 android.Manifest.permission.ACCESS_NETWORK_STATE,
1156 "ConnectivityService");
The Android Open Source Project28527d22009-03-03 19:31:44 -08001157 }
1158
1159 private void enforceChangePermission() {
Robert Greenwalt0659da32009-07-16 17:21:39 -07001160 mContext.enforceCallingOrSelfPermission(
1161 android.Manifest.permission.CHANGE_NETWORK_STATE,
1162 "ConnectivityService");
The Android Open Source Project28527d22009-03-03 19:31:44 -08001163 }
1164
Robert Greenwalt0c4828c2010-01-26 11:40:34 -08001165 // TODO Make this a special check when it goes public
1166 private void enforceTetherChangePermission() {
1167 mContext.enforceCallingOrSelfPermission(
1168 android.Manifest.permission.CHANGE_NETWORK_STATE,
1169 "ConnectivityService");
1170 }
1171
Robert Greenwalt8e87f122010-02-11 18:18:40 -08001172 private void enforceTetherAccessPermission() {
1173 mContext.enforceCallingOrSelfPermission(
1174 android.Manifest.permission.ACCESS_NETWORK_STATE,
1175 "ConnectivityService");
1176 }
1177
Robert Greenwalt93dc1042010-06-15 12:19:37 -07001178 private void enforceConnectivityInternalPermission() {
1179 mContext.enforceCallingOrSelfPermission(
1180 android.Manifest.permission.CONNECTIVITY_INTERNAL,
1181 "ConnectivityService");
1182 }
1183
The Android Open Source Project28527d22009-03-03 19:31:44 -08001184 /**
Robert Greenwalt0659da32009-07-16 17:21:39 -07001185 * Handle a {@code DISCONNECTED} event. If this pertains to the non-active
1186 * network, we ignore it. If it is for the active network, we send out a
1187 * broadcast. But first, we check whether it might be possible to connect
1188 * to a different network.
The Android Open Source Project28527d22009-03-03 19:31:44 -08001189 * @param info the {@code NetworkInfo} for the network
1190 */
1191 private void handleDisconnect(NetworkInfo info) {
1192
Robert Greenwalt2034b912009-08-12 16:08:25 -07001193 int prevNetType = info.getType();
The Android Open Source Project28527d22009-03-03 19:31:44 -08001194
Robert Greenwalt2034b912009-08-12 16:08:25 -07001195 mNetTrackers[prevNetType].setTeardownRequested(false);
The Android Open Source Project28527d22009-03-03 19:31:44 -08001196 /*
1197 * If the disconnected network is not the active one, then don't report
1198 * this as a loss of connectivity. What probably happened is that we're
1199 * getting the disconnect for a network that we explicitly disabled
1200 * in accordance with network preference policies.
1201 */
Robert Greenwalt34848c02011-03-25 13:09:25 -07001202 if (!mNetConfigs[prevNetType].isDefault()) {
Robert Greenwalt2034b912009-08-12 16:08:25 -07001203 List pids = mNetRequestersPids[prevNetType];
1204 for (int i = 0; i<pids.size(); i++) {
1205 Integer pid = (Integer)pids.get(i);
1206 // will remove them because the net's no longer connected
1207 // need to do this now as only now do we know the pids and
1208 // can properly null things that are no longer referenced.
1209 reassessPidDns(pid.intValue(), false);
The Android Open Source Project28527d22009-03-03 19:31:44 -08001210 }
The Android Open Source Project28527d22009-03-03 19:31:44 -08001211 }
1212
The Android Open Source Project28527d22009-03-03 19:31:44 -08001213 Intent intent = new Intent(ConnectivityManager.CONNECTIVITY_ACTION);
1214 intent.putExtra(ConnectivityManager.EXTRA_NETWORK_INFO, info);
1215 if (info.isFailover()) {
1216 intent.putExtra(ConnectivityManager.EXTRA_IS_FAILOVER, true);
1217 info.setFailover(false);
1218 }
1219 if (info.getReason() != null) {
1220 intent.putExtra(ConnectivityManager.EXTRA_REASON, info.getReason());
1221 }
1222 if (info.getExtraInfo() != null) {
Robert Greenwalt0659da32009-07-16 17:21:39 -07001223 intent.putExtra(ConnectivityManager.EXTRA_EXTRA_INFO,
1224 info.getExtraInfo());
The Android Open Source Project28527d22009-03-03 19:31:44 -08001225 }
Robert Greenwalt2034b912009-08-12 16:08:25 -07001226
Robert Greenwalt34848c02011-03-25 13:09:25 -07001227 if (mNetConfigs[prevNetType].isDefault()) {
Robert Greenwalt4b7c55e2011-01-11 13:56:33 -08001228 tryFailover(prevNetType);
1229 if (mActiveDefaultNetwork != -1) {
1230 NetworkInfo switchTo = mNetTrackers[mActiveDefaultNetwork].getNetworkInfo();
Robert Greenwalt3cc68d32010-01-25 17:54:29 -08001231 intent.putExtra(ConnectivityManager.EXTRA_OTHER_NETWORK_INFO, switchTo);
1232 } else {
Robert Greenwaltb2a9e492010-09-20 18:01:43 -07001233 mDefaultInetConditionPublished = 0; // we're not connected anymore
Robert Greenwalt3cc68d32010-01-25 17:54:29 -08001234 intent.putExtra(ConnectivityManager.EXTRA_NO_CONNECTIVITY, true);
1235 }
Robert Greenwaltf55ced92010-01-20 19:29:41 -08001236 }
Robert Greenwaltb2a9e492010-09-20 18:01:43 -07001237 intent.putExtra(ConnectivityManager.EXTRA_INET_CONDITION, mDefaultInetConditionPublished);
Robert Greenwalt36ea8692011-06-15 12:22:07 -07001238
1239 // Reset interface if no other connections are using the same interface
1240 boolean doReset = true;
1241 LinkProperties linkProperties = mNetTrackers[prevNetType].getLinkProperties();
1242 if (linkProperties != null) {
1243 String oldIface = linkProperties.getInterfaceName();
1244 if (TextUtils.isEmpty(oldIface) == false) {
1245 for (NetworkStateTracker networkStateTracker : mNetTrackers) {
1246 if (networkStateTracker == null) continue;
1247 NetworkInfo networkInfo = networkStateTracker.getNetworkInfo();
1248 if (networkInfo.isConnected() && networkInfo.getType() != prevNetType) {
1249 LinkProperties l = networkStateTracker.getLinkProperties();
1250 if (l == null) continue;
1251 if (oldIface.equals(l.getInterfaceName())) {
1252 doReset = false;
1253 break;
1254 }
1255 }
1256 }
1257 }
1258 }
1259
Robert Greenwaltf55ced92010-01-20 19:29:41 -08001260 // do this before we broadcast the change
Robert Greenwalt36ea8692011-06-15 12:22:07 -07001261 handleConnectivityChange(prevNetType, doReset);
Robert Greenwaltf55ced92010-01-20 19:29:41 -08001262
Wink Saville4f0de1e2011-08-04 15:01:58 -07001263 sendStickyBroadcastDelayed(intent, getConnectivityChangeDelay());
Robert Greenwaltf55ced92010-01-20 19:29:41 -08001264 /*
1265 * If the failover network is already connected, then immediately send
1266 * out a followup broadcast indicating successful failover
1267 */
Robert Greenwalt4b7c55e2011-01-11 13:56:33 -08001268 if (mActiveDefaultNetwork != -1) {
Wink Saville4f0de1e2011-08-04 15:01:58 -07001269 sendConnectedBroadcastDelayed(mNetTrackers[mActiveDefaultNetwork].getNetworkInfo(),
1270 getConnectivityChangeDelay());
Robert Greenwaltf55ced92010-01-20 19:29:41 -08001271 }
1272 }
1273
Robert Greenwalt4b7c55e2011-01-11 13:56:33 -08001274 private void tryFailover(int prevNetType) {
Robert Greenwalt2034b912009-08-12 16:08:25 -07001275 /*
Robert Greenwalt92564852011-01-06 15:41:07 -08001276 * If this is a default network, check if other defaults are available.
1277 * Try to reconnect on all available and let them hash it out when
1278 * more than one connects.
Robert Greenwalt2034b912009-08-12 16:08:25 -07001279 */
Robert Greenwalt34848c02011-03-25 13:09:25 -07001280 if (mNetConfigs[prevNetType].isDefault()) {
Robert Greenwalt2034b912009-08-12 16:08:25 -07001281 if (mActiveDefaultNetwork == prevNetType) {
1282 mActiveDefaultNetwork = -1;
1283 }
1284
Robert Greenwalt4b7c55e2011-01-11 13:56:33 -08001285 // don't signal a reconnect for anything lower or equal priority than our
1286 // current connected default
1287 // TODO - don't filter by priority now - nice optimization but risky
1288// int currentPriority = -1;
1289// if (mActiveDefaultNetwork != -1) {
Robert Greenwalt34848c02011-03-25 13:09:25 -07001290// currentPriority = mNetConfigs[mActiveDefaultNetwork].mPriority;
Robert Greenwalt4b7c55e2011-01-11 13:56:33 -08001291// }
Robert Greenwaltf55ced92010-01-20 19:29:41 -08001292 for (int checkType=0; checkType <= ConnectivityManager.MAX_NETWORK_TYPE; checkType++) {
Robert Greenwaltec05b3c2009-10-30 14:17:42 -07001293 if (checkType == prevNetType) continue;
Robert Greenwalt34848c02011-03-25 13:09:25 -07001294 if (mNetConfigs[checkType] == null) continue;
1295 if (!mNetConfigs[checkType].isDefault()) continue;
Wink Saville72a95b92011-01-26 15:43:49 -08001296
1297// Enabling the isAvailable() optimization caused mobile to not get
1298// selected if it was in the middle of error handling. Specifically
1299// a moble connection that took 30 seconds to complete the DEACTIVATE_DATA_CALL
1300// would not be available and we wouldn't get connected to anything.
1301// So removing the isAvailable() optimization below for now. TODO: This
1302// optimization should work and we need to investigate why it doesn't work.
1303// This could be related to how DEACTIVATE_DATA_CALL is reporting its
1304// complete before it is really complete.
1305// if (!mNetTrackers[checkType].isAvailable()) continue;
1306
Robert Greenwalt34848c02011-03-25 13:09:25 -07001307// if (currentPriority >= mNetConfigs[checkType].mPriority) continue;
Robert Greenwalt2034b912009-08-12 16:08:25 -07001308
Robert Greenwalt4b7c55e2011-01-11 13:56:33 -08001309 NetworkStateTracker checkTracker = mNetTrackers[checkType];
1310 NetworkInfo checkInfo = checkTracker.getNetworkInfo();
1311 if (!checkInfo.isConnectedOrConnecting() || checkTracker.isTeardownRequested()) {
1312 checkInfo.setFailover(true);
1313 checkTracker.reconnect();
Robert Greenwalt2034b912009-08-12 16:08:25 -07001314 }
Robert Greenwalt4b7c55e2011-01-11 13:56:33 -08001315 if (DBG) log("Attempting to switch to " + checkInfo.getTypeName());
Robert Greenwalt2034b912009-08-12 16:08:25 -07001316 }
The Android Open Source Project28527d22009-03-03 19:31:44 -08001317 }
The Android Open Source Project28527d22009-03-03 19:31:44 -08001318 }
1319
1320 private void sendConnectedBroadcast(NetworkInfo info) {
Robert Greenwaltd3401f92010-09-15 17:36:33 -07001321 sendGeneralBroadcast(info, ConnectivityManager.CONNECTIVITY_ACTION);
1322 }
1323
Wink Saville4f0de1e2011-08-04 15:01:58 -07001324 private void sendConnectedBroadcastDelayed(NetworkInfo info, int delayMs) {
1325 sendGeneralBroadcastDelayed(info, ConnectivityManager.CONNECTIVITY_ACTION, delayMs);
1326 }
1327
Robert Greenwaltd3401f92010-09-15 17:36:33 -07001328 private void sendInetConditionBroadcast(NetworkInfo info) {
1329 sendGeneralBroadcast(info, ConnectivityManager.INET_CONDITION_ACTION);
1330 }
1331
Wink Saville4f0de1e2011-08-04 15:01:58 -07001332 private Intent makeGeneralIntent(NetworkInfo info, String bcastType) {
Robert Greenwaltd3401f92010-09-15 17:36:33 -07001333 Intent intent = new Intent(bcastType);
The Android Open Source Project28527d22009-03-03 19:31:44 -08001334 intent.putExtra(ConnectivityManager.EXTRA_NETWORK_INFO, info);
1335 if (info.isFailover()) {
1336 intent.putExtra(ConnectivityManager.EXTRA_IS_FAILOVER, true);
1337 info.setFailover(false);
1338 }
1339 if (info.getReason() != null) {
1340 intent.putExtra(ConnectivityManager.EXTRA_REASON, info.getReason());
1341 }
1342 if (info.getExtraInfo() != null) {
Robert Greenwalt0659da32009-07-16 17:21:39 -07001343 intent.putExtra(ConnectivityManager.EXTRA_EXTRA_INFO,
1344 info.getExtraInfo());
The Android Open Source Project28527d22009-03-03 19:31:44 -08001345 }
Robert Greenwalt986c7412010-09-08 15:24:47 -07001346 intent.putExtra(ConnectivityManager.EXTRA_INET_CONDITION, mDefaultInetConditionPublished);
Wink Saville4f0de1e2011-08-04 15:01:58 -07001347 return intent;
1348 }
1349
1350 private void sendGeneralBroadcast(NetworkInfo info, String bcastType) {
1351 sendStickyBroadcast(makeGeneralIntent(info, bcastType));
1352 }
1353
1354 private void sendGeneralBroadcastDelayed(NetworkInfo info, String bcastType, int delayMs) {
1355 sendStickyBroadcastDelayed(makeGeneralIntent(info, bcastType), delayMs);
The Android Open Source Project28527d22009-03-03 19:31:44 -08001356 }
1357
1358 /**
1359 * Called when an attempt to fail over to another network has failed.
1360 * @param info the {@link NetworkInfo} for the failed network
1361 */
1362 private void handleConnectionFailure(NetworkInfo info) {
1363 mNetTrackers[info.getType()].setTeardownRequested(false);
The Android Open Source Project28527d22009-03-03 19:31:44 -08001364
Robert Greenwalt2034b912009-08-12 16:08:25 -07001365 String reason = info.getReason();
1366 String extraInfo = info.getExtraInfo();
Robert Greenwalt0659da32009-07-16 17:21:39 -07001367
Robert Greenwalte981bc52010-10-08 16:35:52 -07001368 String reasonText;
1369 if (reason == null) {
1370 reasonText = ".";
1371 } else {
1372 reasonText = " (" + reason + ").";
The Android Open Source Project28527d22009-03-03 19:31:44 -08001373 }
Wink Savillee70c6f52010-12-03 12:01:38 -08001374 loge("Attempt to connect to " + info.getTypeName() + " failed" + reasonText);
Robert Greenwalt2034b912009-08-12 16:08:25 -07001375
1376 Intent intent = new Intent(ConnectivityManager.CONNECTIVITY_ACTION);
1377 intent.putExtra(ConnectivityManager.EXTRA_NETWORK_INFO, info);
1378 if (getActiveNetworkInfo() == null) {
1379 intent.putExtra(ConnectivityManager.EXTRA_NO_CONNECTIVITY, true);
1380 }
1381 if (reason != null) {
1382 intent.putExtra(ConnectivityManager.EXTRA_REASON, reason);
1383 }
1384 if (extraInfo != null) {
1385 intent.putExtra(ConnectivityManager.EXTRA_EXTRA_INFO, extraInfo);
1386 }
1387 if (info.isFailover()) {
1388 intent.putExtra(ConnectivityManager.EXTRA_IS_FAILOVER, true);
1389 info.setFailover(false);
1390 }
Robert Greenwaltf55ced92010-01-20 19:29:41 -08001391
Robert Greenwalt34848c02011-03-25 13:09:25 -07001392 if (mNetConfigs[info.getType()].isDefault()) {
Robert Greenwalt4b7c55e2011-01-11 13:56:33 -08001393 tryFailover(info.getType());
1394 if (mActiveDefaultNetwork != -1) {
1395 NetworkInfo switchTo = mNetTrackers[mActiveDefaultNetwork].getNetworkInfo();
Robert Greenwalt3cc68d32010-01-25 17:54:29 -08001396 intent.putExtra(ConnectivityManager.EXTRA_OTHER_NETWORK_INFO, switchTo);
1397 } else {
Robert Greenwaltb2a9e492010-09-20 18:01:43 -07001398 mDefaultInetConditionPublished = 0;
Robert Greenwalt3cc68d32010-01-25 17:54:29 -08001399 intent.putExtra(ConnectivityManager.EXTRA_NO_CONNECTIVITY, true);
1400 }
Robert Greenwaltf55ced92010-01-20 19:29:41 -08001401 }
Robert Greenwalt3cc68d32010-01-25 17:54:29 -08001402
Robert Greenwaltb2a9e492010-09-20 18:01:43 -07001403 intent.putExtra(ConnectivityManager.EXTRA_INET_CONDITION, mDefaultInetConditionPublished);
Mike Lockwoodfde2b762009-08-14 14:18:49 -04001404 sendStickyBroadcast(intent);
Robert Greenwaltf55ced92010-01-20 19:29:41 -08001405 /*
1406 * If the failover network is already connected, then immediately send
1407 * out a followup broadcast indicating successful failover
1408 */
Robert Greenwalt4b7c55e2011-01-11 13:56:33 -08001409 if (mActiveDefaultNetwork != -1) {
1410 sendConnectedBroadcast(mNetTrackers[mActiveDefaultNetwork].getNetworkInfo());
Robert Greenwaltf55ced92010-01-20 19:29:41 -08001411 }
Mike Lockwoodfde2b762009-08-14 14:18:49 -04001412 }
1413
1414 private void sendStickyBroadcast(Intent intent) {
1415 synchronized(this) {
Dianne Hackborna417ff82009-12-08 19:45:14 -08001416 if (!mSystemReady) {
1417 mInitialBroadcast = new Intent(intent);
Mike Lockwoodfde2b762009-08-14 14:18:49 -04001418 }
Dianne Hackborna417ff82009-12-08 19:45:14 -08001419 intent.addFlags(Intent.FLAG_RECEIVER_REGISTERED_ONLY_BEFORE_BOOT);
Wink Saville4f0de1e2011-08-04 15:01:58 -07001420 if (DBG) {
1421 log("sendStickyBroadcast: NetworkInfo=" +
1422 intent.getParcelableExtra(ConnectivityManager.EXTRA_NETWORK_INFO));
1423 }
1424
Dianne Hackborna417ff82009-12-08 19:45:14 -08001425 mContext.sendStickyBroadcast(intent);
Mike Lockwoodfde2b762009-08-14 14:18:49 -04001426 }
1427 }
1428
Wink Saville4f0de1e2011-08-04 15:01:58 -07001429 private void sendStickyBroadcastDelayed(Intent intent, int delayMs) {
1430 if (delayMs <= 0) {
1431 sendStickyBroadcast(intent);
1432 } else {
1433 if (DBG) log("sendStickyBroadcastDelayed: delayMs=" + delayMs + " intent=" + intent);
1434 mHandler.sendMessageDelayed(mHandler.obtainMessage(
1435 EVENT_SEND_STICKY_BROADCAST_INTENT, intent), delayMs);
1436 }
1437 }
1438
Mike Lockwoodfde2b762009-08-14 14:18:49 -04001439 void systemReady() {
Robert Greenwalt355205c2011-05-10 15:05:02 -07001440 IBinder b = ServiceManager.getService(Context.NETWORKMANAGEMENT_SERVICE);
1441 mNetd = INetworkManagementService.Stub.asInterface(b);
1442
Mike Lockwoodfde2b762009-08-14 14:18:49 -04001443 synchronized(this) {
1444 mSystemReady = true;
Dianne Hackborna417ff82009-12-08 19:45:14 -08001445 if (mInitialBroadcast != null) {
1446 mContext.sendStickyBroadcast(mInitialBroadcast);
1447 mInitialBroadcast = null;
Mike Lockwoodfde2b762009-08-14 14:18:49 -04001448 }
1449 }
Robert Greenwaltc3c5f862010-10-11 16:00:27 -07001450 // load the global proxy at startup
1451 mHandler.sendMessage(mHandler.obtainMessage(EVENT_APPLY_GLOBAL_HTTP_PROXY));
The Android Open Source Project28527d22009-03-03 19:31:44 -08001452 }
1453
1454 private void handleConnect(NetworkInfo info) {
Robert Greenwalt2034b912009-08-12 16:08:25 -07001455 int type = info.getType();
The Android Open Source Project28527d22009-03-03 19:31:44 -08001456
1457 // snapshot isFailover, because sendConnectedBroadcast() resets it
1458 boolean isFailover = info.isFailover();
Robert Greenwalt2034b912009-08-12 16:08:25 -07001459 NetworkStateTracker thisNet = mNetTrackers[type];
The Android Open Source Project28527d22009-03-03 19:31:44 -08001460
Robert Greenwalt2034b912009-08-12 16:08:25 -07001461 // if this is a default net and other default is running
1462 // kill the one not preferred
Robert Greenwalt34848c02011-03-25 13:09:25 -07001463 if (mNetConfigs[type].isDefault()) {
Robert Greenwalt2034b912009-08-12 16:08:25 -07001464 if (mActiveDefaultNetwork != -1 && mActiveDefaultNetwork != type) {
1465 if ((type != mNetworkPreference &&
Wink Savillef2a62832011-04-07 14:23:45 -07001466 mNetConfigs[mActiveDefaultNetwork].priority >
1467 mNetConfigs[type].priority) ||
Robert Greenwalt2034b912009-08-12 16:08:25 -07001468 mNetworkPreference == mActiveDefaultNetwork) {
1469 // don't accept this one
Wink Savillee70c6f52010-12-03 12:01:38 -08001470 if (DBG) {
1471 log("Not broadcasting CONNECT_ACTION " +
Robert Greenwalt2034b912009-08-12 16:08:25 -07001472 "to torn down network " + info.getTypeName());
Wink Savillee70c6f52010-12-03 12:01:38 -08001473 }
Robert Greenwalt2034b912009-08-12 16:08:25 -07001474 teardown(thisNet);
1475 return;
1476 } else {
1477 // tear down the other
1478 NetworkStateTracker otherNet =
1479 mNetTrackers[mActiveDefaultNetwork];
Wink Savillee70c6f52010-12-03 12:01:38 -08001480 if (DBG) {
1481 log("Policy requires " + otherNet.getNetworkInfo().getTypeName() +
Robert Greenwalt2034b912009-08-12 16:08:25 -07001482 " teardown");
Wink Savillee70c6f52010-12-03 12:01:38 -08001483 }
Robert Greenwalt2034b912009-08-12 16:08:25 -07001484 if (!teardown(otherNet)) {
Wink Savillee70c6f52010-12-03 12:01:38 -08001485 loge("Network declined teardown request");
Robert Greenwalt2034b912009-08-12 16:08:25 -07001486 return;
1487 }
Robert Greenwalt93dc1042010-06-15 12:19:37 -07001488 }
1489 }
1490 synchronized (ConnectivityService.this) {
1491 // have a new default network, release the transition wakelock in a second
1492 // if it's held. The second pause is to allow apps to reconnect over the
1493 // new network
1494 if (mNetTransitionWakeLock.isHeld()) {
1495 mHandler.sendMessageDelayed(mHandler.obtainMessage(
Robert Greenwaltccb36f92010-09-24 14:32:21 -07001496 EVENT_CLEAR_NET_TRANSITION_WAKELOCK,
Robert Greenwalt93dc1042010-06-15 12:19:37 -07001497 mNetTransitionWakeLockSerialNumber, 0),
1498 1000);
Robert Greenwalt2034b912009-08-12 16:08:25 -07001499 }
1500 }
1501 mActiveDefaultNetwork = type;
Robert Greenwalt986c7412010-09-08 15:24:47 -07001502 // this will cause us to come up initially as unconnected and switching
1503 // to connected after our normal pause unless somebody reports us as reall
1504 // disconnected
1505 mDefaultInetConditionPublished = 0;
1506 mDefaultConnectionSequence++;
1507 mInetConditionChangeInFlight = false;
1508 // Don't do this - if we never sign in stay, grey
1509 //reportNetworkCondition(mActiveDefaultNetwork, 100);
Robert Greenwalt2034b912009-08-12 16:08:25 -07001510 }
The Android Open Source Project28527d22009-03-03 19:31:44 -08001511 thisNet.setTeardownRequested(false);
Irfan Sheriff7f132d92010-06-09 15:39:36 -07001512 updateNetworkSettings(thisNet);
Robert Greenwalt36ea8692011-06-15 12:22:07 -07001513 handleConnectivityChange(type, false);
Wink Saville4f0de1e2011-08-04 15:01:58 -07001514 sendConnectedBroadcastDelayed(info, getConnectivityChangeDelay());
The Android Open Source Project28527d22009-03-03 19:31:44 -08001515 }
1516
The Android Open Source Project28527d22009-03-03 19:31:44 -08001517 /**
Robert Greenwalt3afbead2010-07-23 15:46:26 -07001518 * After a change in the connectivity state of a network. We're mainly
1519 * concerned with making sure that the list of DNS servers is set up
1520 * according to which networks are connected, and ensuring that the
1521 * right routing table entries exist.
The Android Open Source Project28527d22009-03-03 19:31:44 -08001522 */
Robert Greenwalt36ea8692011-06-15 12:22:07 -07001523 private void handleConnectivityChange(int netType, boolean doReset) {
Wink Saville051a6642011-07-13 13:44:13 -07001524 int resetMask = doReset ? NetworkUtils.RESET_ALL_ADDRESSES : 0;
1525
The Android Open Source Project28527d22009-03-03 19:31:44 -08001526 /*
Robert Greenwalt2034b912009-08-12 16:08:25 -07001527 * If a non-default network is enabled, add the host routes that
Robert Greenwalt3afbead2010-07-23 15:46:26 -07001528 * will allow it's DNS servers to be accessed.
The Android Open Source Project28527d22009-03-03 19:31:44 -08001529 */
Robert Greenwalt3afbead2010-07-23 15:46:26 -07001530 handleDnsConfigurationChange(netType);
Robert Greenwalt2034b912009-08-12 16:08:25 -07001531
Robert Greenwalte8d2a4a2011-07-14 14:28:05 -07001532 LinkProperties curLp = mCurrentLinkProperties[netType];
1533 LinkProperties newLp = null;
1534
Robert Greenwalt3afbead2010-07-23 15:46:26 -07001535 if (mNetTrackers[netType].getNetworkInfo().isConnected()) {
Robert Greenwalte8d2a4a2011-07-14 14:28:05 -07001536 newLp = mNetTrackers[netType].getLinkProperties();
Wink Saville051a6642011-07-13 13:44:13 -07001537 if (VDBG) {
1538 log("handleConnectivityChange: changed linkProperty[" + netType + "]:" +
1539 " doReset=" + doReset + " resetMask=" + resetMask +
1540 "\n curLp=" + curLp +
1541 "\n newLp=" + newLp);
1542 }
1543
Robert Greenwalte8d2a4a2011-07-14 14:28:05 -07001544 if (curLp != null) {
1545 if (curLp.isIdenticalInterfaceName(newLp)) {
1546 CompareResult<LinkAddress> car = curLp.compareAddresses(newLp);
1547 if ((car.removed.size() != 0) || (car.added.size() != 0)) {
1548 for (LinkAddress linkAddr : car.removed) {
1549 if (linkAddr.getAddress() instanceof Inet4Address) {
1550 resetMask |= NetworkUtils.RESET_IPV4_ADDRESSES;
1551 }
1552 if (linkAddr.getAddress() instanceof Inet6Address) {
1553 resetMask |= NetworkUtils.RESET_IPV6_ADDRESSES;
1554 }
Wink Saville051a6642011-07-13 13:44:13 -07001555 }
Robert Greenwalte8d2a4a2011-07-14 14:28:05 -07001556 if (DBG) {
1557 log("handleConnectivityChange: addresses changed" +
1558 " linkProperty[" + netType + "]:" + " resetMask=" + resetMask +
1559 "\n car=" + car);
Wink Saville051a6642011-07-13 13:44:13 -07001560 }
Robert Greenwalte8d2a4a2011-07-14 14:28:05 -07001561 } else {
1562 if (DBG) {
1563 log("handleConnectivityChange: address are the same reset per doReset" +
1564 " linkProperty[" + netType + "]:" +
1565 " resetMask=" + resetMask);
1566 }
Wink Saville051a6642011-07-13 13:44:13 -07001567 }
1568 } else {
Robert Greenwalte8d2a4a2011-07-14 14:28:05 -07001569 resetMask = NetworkUtils.RESET_ALL_ADDRESSES;
1570 log("handleConnectivityChange: interface not not equivalent reset both" +
1571 " linkProperty[" + netType + "]:" +
1572 " resetMask=" + resetMask);
Wink Saville051a6642011-07-13 13:44:13 -07001573 }
Wink Saville051a6642011-07-13 13:44:13 -07001574 }
Robert Greenwalt34848c02011-03-25 13:09:25 -07001575 if (mNetConfigs[netType].isDefault()) {
Robert Greenwaltc3c5f862010-10-11 16:00:27 -07001576 handleApplyDefaultProxy(netType);
Robert Greenwalt3afbead2010-07-23 15:46:26 -07001577 }
1578 } else {
Robert Greenwalte8d2a4a2011-07-14 14:28:05 -07001579 if (VDBG) {
1580 log("handleConnectivityChange: changed linkProperty[" + netType + "]:" +
1581 " doReset=" + doReset + " resetMask=" + resetMask +
1582 "\n curLp=" + curLp +
1583 "\n newLp= null");
Robert Greenwalt2034b912009-08-12 16:08:25 -07001584 }
The Android Open Source Project28527d22009-03-03 19:31:44 -08001585 }
Robert Greenwalte8d2a4a2011-07-14 14:28:05 -07001586 mCurrentLinkProperties[netType] = newLp;
1587 updateRoutes(newLp, curLp, mNetConfigs[netType].isDefault());
Robert Greenwalt36ea8692011-06-15 12:22:07 -07001588
Wink Saville051a6642011-07-13 13:44:13 -07001589 if (doReset || resetMask != 0) {
Robert Greenwalt36ea8692011-06-15 12:22:07 -07001590 LinkProperties linkProperties = mNetTrackers[netType].getLinkProperties();
1591 if (linkProperties != null) {
1592 String iface = linkProperties.getInterfaceName();
1593 if (TextUtils.isEmpty(iface) == false) {
Wink Saville051a6642011-07-13 13:44:13 -07001594 if (DBG) log("resetConnections(" + iface + ", " + resetMask + ")");
1595 NetworkUtils.resetConnections(iface, resetMask);
Robert Greenwalt36ea8692011-06-15 12:22:07 -07001596 }
1597 }
1598 }
Kazuhiro Ondo07680062011-06-22 21:10:34 -05001599
1600 // TODO: Temporary notifying upstread change to Tethering.
1601 // @see bug/4455071
1602 /** Notify TetheringService if interface name has been changed. */
1603 if (TextUtils.equals(mNetTrackers[netType].getNetworkInfo().getReason(),
1604 Phone.REASON_LINK_PROPERTIES_CHANGED)) {
1605 if (isTetheringSupported()) {
1606 mTethering.handleTetherIfaceChange();
1607 }
1608 }
The Android Open Source Project28527d22009-03-03 19:31:44 -08001609 }
1610
Robert Greenwalte8d2a4a2011-07-14 14:28:05 -07001611 /**
1612 * Add and remove routes using the old properties (null if not previously connected),
1613 * new properties (null if becoming disconnected). May even be double null, which
1614 * is a noop.
1615 * Uses isLinkDefault to determine if default routes should be set or conversely if
1616 * host routes should be set to the dns servers
1617 */
1618 private void updateRoutes(LinkProperties newLp, LinkProperties curLp, boolean isLinkDefault) {
1619 Collection<RouteInfo> routesToAdd = null;
Robert Greenwalt98107422011-07-22 11:55:33 -07001620 CompareResult<InetAddress> dnsDiff = new CompareResult<InetAddress>();
1621 CompareResult<RouteInfo> routeDiff = new CompareResult<RouteInfo>();
Robert Greenwalte8d2a4a2011-07-14 14:28:05 -07001622 if (curLp != null) {
1623 // check for the delta between the current set and the new
Robert Greenwalt98107422011-07-22 11:55:33 -07001624 routeDiff = curLp.compareRoutes(newLp);
Robert Greenwalte8d2a4a2011-07-14 14:28:05 -07001625 dnsDiff = curLp.compareDnses(newLp);
Robert Greenwalt98107422011-07-22 11:55:33 -07001626 } else if (newLp != null) {
1627 routeDiff.added = newLp.getRoutes();
1628 dnsDiff.added = newLp.getDnses();
Irfan Sheriff7f132d92010-06-09 15:39:36 -07001629 }
Irfan Sheriff7f132d92010-06-09 15:39:36 -07001630
Robert Greenwalt98107422011-07-22 11:55:33 -07001631 for (RouteInfo r : routeDiff.removed) {
1632 if (isLinkDefault || ! r.isDefaultRoute()) {
1633 removeRoute(curLp, r);
Robert Greenwalte8d2a4a2011-07-14 14:28:05 -07001634 }
Robert Greenwalt98107422011-07-22 11:55:33 -07001635 }
Irfan Sheriff7f132d92010-06-09 15:39:36 -07001636
Robert Greenwalt98107422011-07-22 11:55:33 -07001637 for (RouteInfo r : routeDiff.added) {
1638 if (isLinkDefault || ! r.isDefaultRoute()) {
1639 addRoute(newLp, r);
Robert Greenwalte8d2a4a2011-07-14 14:28:05 -07001640 }
1641 }
Irfan Sheriff7f132d92010-06-09 15:39:36 -07001642
Robert Greenwalte8d2a4a2011-07-14 14:28:05 -07001643 if (!isLinkDefault) {
1644 // handle DNS routes
Robert Greenwalt98107422011-07-22 11:55:33 -07001645 if (routeDiff.removed.size() == 0 && routeDiff.added.size() == 0) {
1646 // no change in routes, check for change in dns themselves
1647 for (InetAddress oldDns : dnsDiff.removed) {
1648 removeRouteToAddress(curLp, oldDns);
Robert Greenwalte8d2a4a2011-07-14 14:28:05 -07001649 }
Robert Greenwalt98107422011-07-22 11:55:33 -07001650 for (InetAddress newDns : dnsDiff.added) {
1651 addRouteToAddress(newLp, newDns);
Robert Greenwalte8d2a4a2011-07-14 14:28:05 -07001652 }
Robert Greenwalt98107422011-07-22 11:55:33 -07001653 } else {
1654 // routes changed - remove all old dns entries and add new
1655 if (curLp != null) {
1656 for (InetAddress oldDns : curLp.getDnses()) {
1657 removeRouteToAddress(curLp, oldDns);
1658 }
1659 }
1660 if (newLp != null) {
1661 for (InetAddress newDns : newLp.getDnses()) {
1662 addRouteToAddress(newLp, newDns);
1663 }
Robert Greenwalt03d53da2011-03-22 18:47:42 -07001664 }
Irfan Sheriff7f132d92010-06-09 15:39:36 -07001665 }
Irfan Sheriff7f132d92010-06-09 15:39:36 -07001666 }
1667 }
1668
1669
Irfan Sheriff7f132d92010-06-09 15:39:36 -07001670 /**
1671 * Reads the network specific TCP buffer sizes from SystemProperties
1672 * net.tcp.buffersize.[default|wifi|umts|edge|gprs] and set them for system
1673 * wide use
1674 */
1675 public void updateNetworkSettings(NetworkStateTracker nt) {
1676 String key = nt.getTcpBufferSizesPropName();
1677 String bufferSizes = SystemProperties.get(key);
1678
1679 if (bufferSizes.length() == 0) {
Wink Savillee70c6f52010-12-03 12:01:38 -08001680 loge(key + " not found in system properties. Using defaults");
Irfan Sheriff7f132d92010-06-09 15:39:36 -07001681
1682 // Setting to default values so we won't be stuck to previous values
1683 key = "net.tcp.buffersize.default";
1684 bufferSizes = SystemProperties.get(key);
1685 }
1686
1687 // Set values in kernel
1688 if (bufferSizes.length() != 0) {
1689 if (DBG) {
Wink Savillee70c6f52010-12-03 12:01:38 -08001690 log("Setting TCP values: [" + bufferSizes
Irfan Sheriff7f132d92010-06-09 15:39:36 -07001691 + "] which comes from [" + key + "]");
1692 }
1693 setBufferSize(bufferSizes);
1694 }
1695 }
1696
1697 /**
1698 * Writes TCP buffer sizes to /sys/kernel/ipv4/tcp_[r/w]mem_[min/def/max]
1699 * which maps to /proc/sys/net/ipv4/tcp_rmem and tcpwmem
1700 *
1701 * @param bufferSizes in the format of "readMin, readInitial, readMax,
1702 * writeMin, writeInitial, writeMax"
1703 */
1704 private void setBufferSize(String bufferSizes) {
1705 try {
1706 String[] values = bufferSizes.split(",");
1707
1708 if (values.length == 6) {
1709 final String prefix = "/sys/kernel/ipv4/tcp_";
1710 stringToFile(prefix + "rmem_min", values[0]);
1711 stringToFile(prefix + "rmem_def", values[1]);
1712 stringToFile(prefix + "rmem_max", values[2]);
1713 stringToFile(prefix + "wmem_min", values[3]);
1714 stringToFile(prefix + "wmem_def", values[4]);
1715 stringToFile(prefix + "wmem_max", values[5]);
1716 } else {
Wink Savillee70c6f52010-12-03 12:01:38 -08001717 loge("Invalid buffersize string: " + bufferSizes);
Irfan Sheriff7f132d92010-06-09 15:39:36 -07001718 }
1719 } catch (IOException e) {
Wink Savillee70c6f52010-12-03 12:01:38 -08001720 loge("Can't set tcp buffer sizes:" + e);
Irfan Sheriff7f132d92010-06-09 15:39:36 -07001721 }
1722 }
1723
1724 /**
1725 * Writes string to file. Basically same as "echo -n $string > $filename"
1726 *
1727 * @param filename
1728 * @param string
1729 * @throws IOException
1730 */
1731 private void stringToFile(String filename, String string) throws IOException {
1732 FileWriter out = new FileWriter(filename);
1733 try {
1734 out.write(string);
1735 } finally {
1736 out.close();
1737 }
1738 }
1739
1740
Robert Greenwalt2034b912009-08-12 16:08:25 -07001741 /**
1742 * Adjust the per-process dns entries (net.dns<x>.<pid>) based
1743 * on the highest priority active net which this process requested.
1744 * If there aren't any, clear it out
1745 */
1746 private void reassessPidDns(int myPid, boolean doBump)
1747 {
Wink Savillee70c6f52010-12-03 12:01:38 -08001748 if (DBG) log("reassessPidDns for pid " + myPid);
Robert Greenwalt2034b912009-08-12 16:08:25 -07001749 for(int i : mPriorityList) {
Robert Greenwalt34848c02011-03-25 13:09:25 -07001750 if (mNetConfigs[i].isDefault()) {
Robert Greenwalt2034b912009-08-12 16:08:25 -07001751 continue;
1752 }
1753 NetworkStateTracker nt = mNetTrackers[i];
Robert Greenwalt0659da32009-07-16 17:21:39 -07001754 if (nt.getNetworkInfo().isConnected() &&
1755 !nt.isTeardownRequested()) {
Robert Greenwalt1f1bcfe2010-08-30 10:56:47 -07001756 LinkProperties p = nt.getLinkProperties();
Robert Greenwalta7dfbd32010-06-15 15:43:39 -07001757 if (p == null) continue;
Robert Greenwalt2034b912009-08-12 16:08:25 -07001758 List pids = mNetRequestersPids[i];
1759 for (int j=0; j<pids.size(); j++) {
1760 Integer pid = (Integer)pids.get(j);
1761 if (pid.intValue() == myPid) {
Robert Greenwalta7dfbd32010-06-15 15:43:39 -07001762 Collection<InetAddress> dnses = p.getDnses();
1763 writePidDns(dnses, myPid);
Robert Greenwalt2034b912009-08-12 16:08:25 -07001764 if (doBump) {
1765 bumpDns();
1766 }
1767 return;
1768 }
1769 }
1770 }
1771 }
1772 // nothing found - delete
1773 for (int i = 1; ; i++) {
1774 String prop = "net.dns" + i + "." + myPid;
1775 if (SystemProperties.get(prop).length() == 0) {
1776 if (doBump) {
1777 bumpDns();
1778 }
1779 return;
1780 }
1781 SystemProperties.set(prop, "");
1782 }
1783 }
1784
Robert Greenwalt8ca88762010-12-17 15:20:36 -08001785 // return true if results in a change
1786 private boolean writePidDns(Collection <InetAddress> dnses, int pid) {
Robert Greenwalt2034b912009-08-12 16:08:25 -07001787 int j = 1;
Robert Greenwalt8ca88762010-12-17 15:20:36 -08001788 boolean changed = false;
Robert Greenwalta7dfbd32010-06-15 15:43:39 -07001789 for (InetAddress dns : dnses) {
Robert Greenwalt8ca88762010-12-17 15:20:36 -08001790 String dnsString = dns.getHostAddress();
1791 if (changed || !dnsString.equals(SystemProperties.get("net.dns" + j + "." + pid))) {
1792 changed = true;
1793 SystemProperties.set("net.dns" + j++ + "." + pid, dns.getHostAddress());
1794 }
Robert Greenwalt2034b912009-08-12 16:08:25 -07001795 }
Robert Greenwalt8ca88762010-12-17 15:20:36 -08001796 return changed;
Robert Greenwalt2034b912009-08-12 16:08:25 -07001797 }
1798
1799 private void bumpDns() {
1800 /*
1801 * Bump the property that tells the name resolver library to reread
1802 * the DNS server list from the properties.
1803 */
1804 String propVal = SystemProperties.get("net.dnschange");
1805 int n = 0;
1806 if (propVal.length() != 0) {
1807 try {
1808 n = Integer.parseInt(propVal);
1809 } catch (NumberFormatException e) {}
1810 }
1811 SystemProperties.set("net.dnschange", "" + (n+1));
Robert Greenwalt051642b2010-11-02 14:08:23 -07001812 /*
1813 * Tell the VMs to toss their DNS caches
1814 */
1815 Intent intent = new Intent(Intent.ACTION_CLEAR_DNS_CACHE);
1816 intent.addFlags(Intent.FLAG_RECEIVER_REPLACE_PENDING);
Stan Chesnuttf444f502011-01-05 17:14:03 -08001817 /*
1818 * Connectivity events can happen before boot has completed ...
1819 */
1820 intent.addFlags(Intent.FLAG_RECEIVER_REGISTERED_ONLY_BEFORE_BOOT);
Robert Greenwalt051642b2010-11-02 14:08:23 -07001821 mContext.sendBroadcast(intent);
Robert Greenwalt2034b912009-08-12 16:08:25 -07001822 }
1823
Robert Greenwalt3afbead2010-07-23 15:46:26 -07001824 private void handleDnsConfigurationChange(int netType) {
Robert Greenwalt2034b912009-08-12 16:08:25 -07001825 // add default net's dns entries
Robert Greenwalt3afbead2010-07-23 15:46:26 -07001826 NetworkStateTracker nt = mNetTrackers[netType];
1827 if (nt != null && nt.getNetworkInfo().isConnected() && !nt.isTeardownRequested()) {
Robert Greenwalt1f1bcfe2010-08-30 10:56:47 -07001828 LinkProperties p = nt.getLinkProperties();
Robert Greenwalt3afbead2010-07-23 15:46:26 -07001829 if (p == null) return;
1830 Collection<InetAddress> dnses = p.getDnses();
Robert Greenwaltfd0597c2011-07-25 17:00:13 -07001831 try {
1832 mNetd.setDnsServersForInterface(p.getInterfaceName(),
1833 NetworkUtils.makeStrings(dnses));
1834 } catch (Exception e) {
1835 Slog.e(TAG, "exception setting dns servers: " + e);
1836 }
Robert Greenwalt8ca88762010-12-17 15:20:36 -08001837 boolean changed = false;
Robert Greenwalt34848c02011-03-25 13:09:25 -07001838 if (mNetConfigs[netType].isDefault()) {
Robert Greenwaltfd0597c2011-07-25 17:00:13 -07001839 try {
1840 mNetd.setDefaultInterfaceForDns(p.getInterfaceName());
1841 } catch (Exception e) {
1842 Slog.e(TAG, "exception setting default dns interface: " + e);
1843 }
Robert Greenwalt3afbead2010-07-23 15:46:26 -07001844 int j = 1;
Robert Greenwalt94daa182010-09-01 11:34:05 -07001845 if (dnses.size() == 0 && mDefaultDns != null) {
Robert Greenwalt8ca88762010-12-17 15:20:36 -08001846 String dnsString = mDefaultDns.getHostAddress();
1847 if (!dnsString.equals(SystemProperties.get("net.dns1"))) {
1848 if (DBG) {
1849 log("no dns provided - using " + dnsString);
1850 }
1851 changed = true;
1852 SystemProperties.set("net.dns1", dnsString);
Robert Greenwalt2034b912009-08-12 16:08:25 -07001853 }
Robert Greenwalt94daa182010-09-01 11:34:05 -07001854 j++;
1855 } else {
1856 for (InetAddress dns : dnses) {
Robert Greenwalt8ca88762010-12-17 15:20:36 -08001857 String dnsString = dns.getHostAddress();
1858 if (!changed && dnsString.equals(SystemProperties.get("net.dns" + j))) {
1859 j++;
1860 continue;
1861 }
Robert Greenwalt94daa182010-09-01 11:34:05 -07001862 if (DBG) {
Wink Savillee70c6f52010-12-03 12:01:38 -08001863 log("adding dns " + dns + " for " +
Robert Greenwalt94daa182010-09-01 11:34:05 -07001864 nt.getNetworkInfo().getTypeName());
1865 }
Robert Greenwalt8ca88762010-12-17 15:20:36 -08001866 changed = true;
1867 SystemProperties.set("net.dns" + j++, dnsString);
Robert Greenwalt94daa182010-09-01 11:34:05 -07001868 }
Robert Greenwalt3afbead2010-07-23 15:46:26 -07001869 }
1870 for (int k=j ; k<mNumDnsEntries; k++) {
Robert Greenwalt8ca88762010-12-17 15:20:36 -08001871 if (changed || !TextUtils.isEmpty(SystemProperties.get("net.dns" + k))) {
1872 if (DBG) log("erasing net.dns" + k);
1873 changed = true;
1874 SystemProperties.set("net.dns" + k, "");
1875 }
Robert Greenwalt3afbead2010-07-23 15:46:26 -07001876 }
1877 mNumDnsEntries = j;
1878 } else {
1879 // set per-pid dns for attached secondary nets
1880 List pids = mNetRequestersPids[netType];
1881 for (int y=0; y< pids.size(); y++) {
1882 Integer pid = (Integer)pids.get(y);
Robert Greenwalt8ca88762010-12-17 15:20:36 -08001883 changed = writePidDns(dnses, pid.intValue());
The Android Open Source Project28527d22009-03-03 19:31:44 -08001884 }
1885 }
Robert Greenwalt8ca88762010-12-17 15:20:36 -08001886 if (changed) bumpDns();
The Android Open Source Project28527d22009-03-03 19:31:44 -08001887 }
Robert Greenwalt2034b912009-08-12 16:08:25 -07001888 }
1889
Robert Greenwalt20f819c2011-05-03 19:02:44 -07001890 private int getRestoreDefaultNetworkDelay(int networkType) {
Robert Greenwalt2034b912009-08-12 16:08:25 -07001891 String restoreDefaultNetworkDelayStr = SystemProperties.get(
1892 NETWORK_RESTORE_DELAY_PROP_NAME);
1893 if(restoreDefaultNetworkDelayStr != null &&
1894 restoreDefaultNetworkDelayStr.length() != 0) {
1895 try {
1896 return Integer.valueOf(restoreDefaultNetworkDelayStr);
1897 } catch (NumberFormatException e) {
1898 }
The Android Open Source Project28527d22009-03-03 19:31:44 -08001899 }
Robert Greenwalt20f819c2011-05-03 19:02:44 -07001900 // if the system property isn't set, use the value for the apn type
1901 int ret = RESTORE_DEFAULT_NETWORK_DELAY;
1902
1903 if ((networkType <= ConnectivityManager.MAX_NETWORK_TYPE) &&
1904 (mNetConfigs[networkType] != null)) {
1905 ret = mNetConfigs[networkType].restoreTime;
1906 }
1907 return ret;
The Android Open Source Project28527d22009-03-03 19:31:44 -08001908 }
1909
1910 @Override
1911 protected void dump(FileDescriptor fd, PrintWriter pw, String[] args) {
Robert Greenwalt0659da32009-07-16 17:21:39 -07001912 if (mContext.checkCallingOrSelfPermission(
1913 android.Manifest.permission.DUMP)
The Android Open Source Project28527d22009-03-03 19:31:44 -08001914 != PackageManager.PERMISSION_GRANTED) {
Robert Greenwalt0659da32009-07-16 17:21:39 -07001915 pw.println("Permission Denial: can't dump ConnectivityService " +
1916 "from from pid=" + Binder.getCallingPid() + ", uid=" +
1917 Binder.getCallingUid());
The Android Open Source Project28527d22009-03-03 19:31:44 -08001918 return;
1919 }
The Android Open Source Project28527d22009-03-03 19:31:44 -08001920 pw.println();
1921 for (NetworkStateTracker nst : mNetTrackers) {
Robert Greenwalt3eeb6032009-12-21 18:24:07 -08001922 if (nst != null) {
1923 if (nst.getNetworkInfo().isConnected()) {
1924 pw.println("Active network: " + nst.getNetworkInfo().
1925 getTypeName());
1926 }
1927 pw.println(nst.getNetworkInfo());
1928 pw.println(nst);
1929 pw.println();
Robert Greenwalt2034b912009-08-12 16:08:25 -07001930 }
The Android Open Source Project28527d22009-03-03 19:31:44 -08001931 }
Robert Greenwalt3eeb6032009-12-21 18:24:07 -08001932
1933 pw.println("Network Requester Pids:");
1934 for (int net : mPriorityList) {
1935 String pidString = net + ": ";
1936 for (Object pid : mNetRequestersPids[net]) {
1937 pidString = pidString + pid.toString() + ", ";
1938 }
1939 pw.println(pidString);
1940 }
1941 pw.println();
1942
1943 pw.println("FeatureUsers:");
1944 for (Object requester : mFeatureUsers) {
1945 pw.println(requester.toString());
1946 }
1947 pw.println();
Robert Greenwalt8e87f122010-02-11 18:18:40 -08001948
Robert Greenwalt93dc1042010-06-15 12:19:37 -07001949 synchronized (this) {
1950 pw.println("NetworkTranstionWakeLock is currently " +
1951 (mNetTransitionWakeLock.isHeld() ? "" : "not ") + "held.");
1952 pw.println("It was last requested for "+mNetTransitionWakeLockCausedBy);
1953 }
1954 pw.println();
1955
Robert Greenwalt8e87f122010-02-11 18:18:40 -08001956 mTethering.dump(fd, pw, args);
Robert Greenwalt0e80be12010-09-20 14:35:25 -07001957
1958 if (mInetLog != null) {
1959 pw.println();
1960 pw.println("Inet condition reports:");
1961 for(int i = 0; i < mInetLog.size(); i++) {
1962 pw.println(mInetLog.get(i));
1963 }
1964 }
The Android Open Source Project28527d22009-03-03 19:31:44 -08001965 }
1966
Robert Greenwalt2034b912009-08-12 16:08:25 -07001967 // must be stateless - things change under us.
The Android Open Source Project28527d22009-03-03 19:31:44 -08001968 private class MyHandler extends Handler {
Wink Saville775aad62010-09-02 19:23:52 -07001969 public MyHandler(Looper looper) {
1970 super(looper);
1971 }
1972
The Android Open Source Project28527d22009-03-03 19:31:44 -08001973 @Override
1974 public void handleMessage(Message msg) {
1975 NetworkInfo info;
1976 switch (msg.what) {
1977 case NetworkStateTracker.EVENT_STATE_CHANGED:
1978 info = (NetworkInfo) msg.obj;
Robert Greenwalt12c44552009-12-07 11:33:18 -08001979 int type = info.getType();
1980 NetworkInfo.State state = info.getState();
Robert Greenwalt12c44552009-12-07 11:33:18 -08001981
Wink Savillee70c6f52010-12-03 12:01:38 -08001982 if (DBG) log("ConnectivityChange for " +
Robert Greenwalt0659da32009-07-16 17:21:39 -07001983 info.getTypeName() + ": " +
Robert Greenwalt12c44552009-12-07 11:33:18 -08001984 state + "/" + info.getDetailedState());
The Android Open Source Project28527d22009-03-03 19:31:44 -08001985
1986 // Connectivity state changed:
1987 // [31-13] Reserved for future use
Robert Greenwalt0659da32009-07-16 17:21:39 -07001988 // [12-9] Network subtype (for mobile network, as defined
1989 // by TelephonyManager)
1990 // [8-3] Detailed state ordinal (as defined by
1991 // NetworkInfo.DetailedState)
The Android Open Source Project28527d22009-03-03 19:31:44 -08001992 // [2-0] Network type (as defined by ConnectivityManager)
1993 int eventLogParam = (info.getType() & 0x7) |
1994 ((info.getDetailedState().ordinal() & 0x3f) << 3) |
1995 (info.getSubtype() << 9);
Doug Zongker2fc96232009-12-04 10:31:43 -08001996 EventLog.writeEvent(EventLogTags.CONNECTIVITY_STATE_CHANGED,
Robert Greenwalt0659da32009-07-16 17:21:39 -07001997 eventLogParam);
1998
1999 if (info.getDetailedState() ==
2000 NetworkInfo.DetailedState.FAILED) {
The Android Open Source Project28527d22009-03-03 19:31:44 -08002001 handleConnectionFailure(info);
Robert Greenwalt12c44552009-12-07 11:33:18 -08002002 } else if (state == NetworkInfo.State.DISCONNECTED) {
The Android Open Source Project28527d22009-03-03 19:31:44 -08002003 handleDisconnect(info);
Robert Greenwalt12c44552009-12-07 11:33:18 -08002004 } else if (state == NetworkInfo.State.SUSPENDED) {
The Android Open Source Project28527d22009-03-03 19:31:44 -08002005 // TODO: need to think this over.
Robert Greenwalt0659da32009-07-16 17:21:39 -07002006 // the logic here is, handle SUSPENDED the same as
2007 // DISCONNECTED. The only difference being we are
2008 // broadcasting an intent with NetworkInfo that's
2009 // suspended. This allows the applications an
2010 // opportunity to handle DISCONNECTED and SUSPENDED
2011 // differently, or not.
The Android Open Source Project28527d22009-03-03 19:31:44 -08002012 handleDisconnect(info);
Robert Greenwalt12c44552009-12-07 11:33:18 -08002013 } else if (state == NetworkInfo.State.CONNECTED) {
The Android Open Source Project28527d22009-03-03 19:31:44 -08002014 handleConnect(info);
2015 }
The Android Open Source Project28527d22009-03-03 19:31:44 -08002016 break;
The Android Open Source Project28527d22009-03-03 19:31:44 -08002017 case NetworkStateTracker.EVENT_CONFIGURATION_CHANGED:
Robert Greenwalt3afbead2010-07-23 15:46:26 -07002018 info = (NetworkInfo) msg.obj;
Kazuhiro Ondo07680062011-06-22 21:10:34 -05002019 // TODO: Temporary allowing network configuration
2020 // change not resetting sockets.
2021 // @see bug/4455071
2022 handleConnectivityChange(info.getType(), false);
The Android Open Source Project28527d22009-03-03 19:31:44 -08002023 break;
Robert Greenwaltccb36f92010-09-24 14:32:21 -07002024 case EVENT_CLEAR_NET_TRANSITION_WAKELOCK:
Robert Greenwalt93dc1042010-06-15 12:19:37 -07002025 String causedBy = null;
2026 synchronized (ConnectivityService.this) {
2027 if (msg.arg1 == mNetTransitionWakeLockSerialNumber &&
2028 mNetTransitionWakeLock.isHeld()) {
2029 mNetTransitionWakeLock.release();
2030 causedBy = mNetTransitionWakeLockCausedBy;
2031 }
2032 }
2033 if (causedBy != null) {
Wink Savillee70c6f52010-12-03 12:01:38 -08002034 log("NetTransition Wakelock for " + causedBy + " released by timeout");
Robert Greenwalt93dc1042010-06-15 12:19:37 -07002035 }
Robert Greenwaltcf1a56c2010-09-09 14:05:10 -07002036 break;
Robert Greenwalt6a2db8a2010-09-23 10:05:56 -07002037 case EVENT_RESTORE_DEFAULT_NETWORK:
Robert Greenwaltaffc3a12009-09-27 17:27:04 -07002038 FeatureUser u = (FeatureUser)msg.obj;
2039 u.expire();
Robert Greenwalt986c7412010-09-08 15:24:47 -07002040 break;
Robert Greenwalt6a2db8a2010-09-23 10:05:56 -07002041 case EVENT_INET_CONDITION_CHANGE:
2042 {
2043 int netType = msg.arg1;
2044 int condition = msg.arg2;
2045 handleInetConditionChange(netType, condition);
Robert Greenwalt93dc1042010-06-15 12:19:37 -07002046 break;
Robert Greenwalt6a2db8a2010-09-23 10:05:56 -07002047 }
2048 case EVENT_INET_CONDITION_HOLD_END:
2049 {
2050 int netType = msg.arg1;
2051 int sequence = msg.arg2;
2052 handleInetConditionHoldEnd(netType, sequence);
Robert Greenwalt986c7412010-09-08 15:24:47 -07002053 break;
Robert Greenwalt6a2db8a2010-09-23 10:05:56 -07002054 }
2055 case EVENT_SET_NETWORK_PREFERENCE:
2056 {
2057 int preference = msg.arg1;
2058 handleSetNetworkPreference(preference);
2059 break;
2060 }
2061 case EVENT_SET_BACKGROUND_DATA:
2062 {
2063 boolean enabled = (msg.arg1 == ENABLED);
2064 handleSetBackgroundData(enabled);
2065 break;
2066 }
2067 case EVENT_SET_MOBILE_DATA:
2068 {
2069 boolean enabled = (msg.arg1 == ENABLED);
2070 handleSetMobileData(enabled);
2071 break;
2072 }
Robert Greenwaltc3c5f862010-10-11 16:00:27 -07002073 case EVENT_APPLY_GLOBAL_HTTP_PROXY:
2074 {
2075 handleDeprecatedGlobalHttpProxy();
Robert Greenwalt34848c02011-03-25 13:09:25 -07002076 break;
2077 }
2078 case EVENT_SET_DEPENDENCY_MET:
2079 {
2080 boolean met = (msg.arg1 == ENABLED);
2081 handleSetDependencyMet(msg.arg2, met);
2082 break;
Robert Greenwaltc3c5f862010-10-11 16:00:27 -07002083 }
Wink Saville4f0de1e2011-08-04 15:01:58 -07002084 case EVENT_SEND_STICKY_BROADCAST_INTENT:
2085 {
2086 Intent intent = (Intent)msg.obj;
2087 log("EVENT_SEND_STICKY_BROADCAST_INTENT: sendStickyBroadcast intent=" + intent);
2088 sendStickyBroadcast(intent);
2089 break;
2090 }
The Android Open Source Project28527d22009-03-03 19:31:44 -08002091 }
2092 }
2093 }
Robert Greenwalt0c4828c2010-01-26 11:40:34 -08002094
2095 // javadoc from interface
Robert Greenwalt4283ded2010-03-02 17:25:02 -08002096 public int tether(String iface) {
Robert Greenwalt0c4828c2010-01-26 11:40:34 -08002097 enforceTetherChangePermission();
Robert Greenwalt4283ded2010-03-02 17:25:02 -08002098
2099 if (isTetheringSupported()) {
2100 return mTethering.tether(iface);
2101 } else {
2102 return ConnectivityManager.TETHER_ERROR_UNSUPPORTED;
2103 }
Robert Greenwalt0c4828c2010-01-26 11:40:34 -08002104 }
2105
2106 // javadoc from interface
Robert Greenwalt4283ded2010-03-02 17:25:02 -08002107 public int untether(String iface) {
Robert Greenwalt0c4828c2010-01-26 11:40:34 -08002108 enforceTetherChangePermission();
Robert Greenwalt4283ded2010-03-02 17:25:02 -08002109
2110 if (isTetheringSupported()) {
2111 return mTethering.untether(iface);
2112 } else {
2113 return ConnectivityManager.TETHER_ERROR_UNSUPPORTED;
2114 }
2115 }
2116
2117 // javadoc from interface
2118 public int getLastTetherError(String iface) {
2119 enforceTetherAccessPermission();
2120
2121 if (isTetheringSupported()) {
2122 return mTethering.getLastTetherError(iface);
2123 } else {
2124 return ConnectivityManager.TETHER_ERROR_UNSUPPORTED;
2125 }
Robert Greenwalt8e87f122010-02-11 18:18:40 -08002126 }
2127
2128 // TODO - proper iface API for selection by property, inspection, etc
2129 public String[] getTetherableUsbRegexs() {
2130 enforceTetherAccessPermission();
2131 if (isTetheringSupported()) {
2132 return mTethering.getTetherableUsbRegexs();
2133 } else {
2134 return new String[0];
2135 }
2136 }
2137
2138 public String[] getTetherableWifiRegexs() {
2139 enforceTetherAccessPermission();
2140 if (isTetheringSupported()) {
2141 return mTethering.getTetherableWifiRegexs();
2142 } else {
2143 return new String[0];
2144 }
Robert Greenwalt0c4828c2010-01-26 11:40:34 -08002145 }
2146
Danica Chang96567052010-08-11 14:54:43 -07002147 public String[] getTetherableBluetoothRegexs() {
2148 enforceTetherAccessPermission();
2149 if (isTetheringSupported()) {
2150 return mTethering.getTetherableBluetoothRegexs();
2151 } else {
2152 return new String[0];
2153 }
2154 }
2155
Robert Greenwalt0c4828c2010-01-26 11:40:34 -08002156 // TODO - move iface listing, queries, etc to new module
2157 // javadoc from interface
2158 public String[] getTetherableIfaces() {
Robert Greenwalt8e87f122010-02-11 18:18:40 -08002159 enforceTetherAccessPermission();
Robert Greenwalt0c4828c2010-01-26 11:40:34 -08002160 return mTethering.getTetherableIfaces();
2161 }
2162
2163 public String[] getTetheredIfaces() {
Robert Greenwalt8e87f122010-02-11 18:18:40 -08002164 enforceTetherAccessPermission();
Robert Greenwalt0c4828c2010-01-26 11:40:34 -08002165 return mTethering.getTetheredIfaces();
2166 }
Robert Greenwalt8e87f122010-02-11 18:18:40 -08002167
Robert Greenwalt4283ded2010-03-02 17:25:02 -08002168 public String[] getTetheringErroredIfaces() {
2169 enforceTetherAccessPermission();
2170 return mTethering.getErroredIfaces();
2171 }
2172
Robert Greenwalt8e87f122010-02-11 18:18:40 -08002173 // if ro.tether.denied = true we default to no tethering
2174 // gservices could set the secure setting to 1 though to enable it on a build where it
2175 // had previously been turned off.
2176 public boolean isTetheringSupported() {
2177 enforceTetherAccessPermission();
2178 int defaultVal = (SystemProperties.get("ro.tether.denied").equals("true") ? 0 : 1);
Robert Greenwaltf1b66e12010-02-25 12:29:30 -08002179 boolean tetherEnabledInSettings = (Settings.Secure.getInt(mContext.getContentResolver(),
2180 Settings.Secure.TETHER_SUPPORTED, defaultVal) != 0);
2181 return tetherEnabledInSettings && mTetheringConfigValid;
Robert Greenwalt8e87f122010-02-11 18:18:40 -08002182 }
Robert Greenwalt93dc1042010-06-15 12:19:37 -07002183
2184 // An API NetworkStateTrackers can call when they lose their network.
2185 // This will automatically be cleared after X seconds or a network becomes CONNECTED,
2186 // whichever happens first. The timer is started by the first caller and not
2187 // restarted by subsequent callers.
2188 public void requestNetworkTransitionWakelock(String forWhom) {
2189 enforceConnectivityInternalPermission();
2190 synchronized (this) {
2191 if (mNetTransitionWakeLock.isHeld()) return;
2192 mNetTransitionWakeLockSerialNumber++;
2193 mNetTransitionWakeLock.acquire();
2194 mNetTransitionWakeLockCausedBy = forWhom;
2195 }
2196 mHandler.sendMessageDelayed(mHandler.obtainMessage(
Robert Greenwaltccb36f92010-09-24 14:32:21 -07002197 EVENT_CLEAR_NET_TRANSITION_WAKELOCK,
Robert Greenwalt93dc1042010-06-15 12:19:37 -07002198 mNetTransitionWakeLockSerialNumber, 0),
2199 mNetTransitionWakeLockTimeout);
2200 return;
2201 }
Robert Greenwalt24118e82010-09-09 13:15:32 -07002202
Robert Greenwalt986c7412010-09-08 15:24:47 -07002203 // 100 percent is full good, 0 is full bad.
2204 public void reportInetCondition(int networkType, int percentage) {
Wink Savillee70c6f52010-12-03 12:01:38 -08002205 if (DBG) log("reportNetworkCondition(" + networkType + ", " + percentage + ")");
Robert Greenwalt986c7412010-09-08 15:24:47 -07002206 mContext.enforceCallingOrSelfPermission(
2207 android.Manifest.permission.STATUS_BAR,
2208 "ConnectivityService");
2209
Robert Greenwalt0e80be12010-09-20 14:35:25 -07002210 if (DBG) {
2211 int pid = getCallingPid();
2212 int uid = getCallingUid();
2213 String s = pid + "(" + uid + ") reports inet is " +
2214 (percentage > 50 ? "connected" : "disconnected") + " (" + percentage + ") on " +
2215 "network Type " + networkType + " at " + GregorianCalendar.getInstance().getTime();
2216 mInetLog.add(s);
2217 while(mInetLog.size() > INET_CONDITION_LOG_MAX_SIZE) {
2218 mInetLog.remove(0);
2219 }
2220 }
Robert Greenwalt986c7412010-09-08 15:24:47 -07002221 mHandler.sendMessage(mHandler.obtainMessage(
Robert Greenwalt6a2db8a2010-09-23 10:05:56 -07002222 EVENT_INET_CONDITION_CHANGE, networkType, percentage));
2223 }
2224
2225 private void handleInetConditionChange(int netType, int condition) {
2226 if (DBG) {
Wink Savillee70c6f52010-12-03 12:01:38 -08002227 log("Inet connectivity change, net=" +
Robert Greenwalt6a2db8a2010-09-23 10:05:56 -07002228 netType + ", condition=" + condition +
2229 ",mActiveDefaultNetwork=" + mActiveDefaultNetwork);
2230 }
2231 if (mActiveDefaultNetwork == -1) {
Wink Savillee70c6f52010-12-03 12:01:38 -08002232 if (DBG) log("no active default network - aborting");
Robert Greenwalt6a2db8a2010-09-23 10:05:56 -07002233 return;
2234 }
2235 if (mActiveDefaultNetwork != netType) {
Wink Savillee70c6f52010-12-03 12:01:38 -08002236 if (DBG) log("given net not default - aborting");
Robert Greenwalt6a2db8a2010-09-23 10:05:56 -07002237 return;
2238 }
2239 mDefaultInetCondition = condition;
2240 int delay;
2241 if (mInetConditionChangeInFlight == false) {
Wink Savillee70c6f52010-12-03 12:01:38 -08002242 if (DBG) log("starting a change hold");
Robert Greenwalt6a2db8a2010-09-23 10:05:56 -07002243 // setup a new hold to debounce this
2244 if (mDefaultInetCondition > 50) {
2245 delay = Settings.Secure.getInt(mContext.getContentResolver(),
2246 Settings.Secure.INET_CONDITION_DEBOUNCE_UP_DELAY, 500);
2247 } else {
2248 delay = Settings.Secure.getInt(mContext.getContentResolver(),
2249 Settings.Secure.INET_CONDITION_DEBOUNCE_DOWN_DELAY, 3000);
2250 }
2251 mInetConditionChangeInFlight = true;
2252 mHandler.sendMessageDelayed(mHandler.obtainMessage(EVENT_INET_CONDITION_HOLD_END,
2253 mActiveDefaultNetwork, mDefaultConnectionSequence), delay);
2254 } else {
2255 // we've set the new condition, when this hold ends that will get
2256 // picked up
Wink Savillee70c6f52010-12-03 12:01:38 -08002257 if (DBG) log("currently in hold - not setting new end evt");
Robert Greenwalt6a2db8a2010-09-23 10:05:56 -07002258 }
2259 }
2260
2261 private void handleInetConditionHoldEnd(int netType, int sequence) {
2262 if (DBG) {
Wink Savillee70c6f52010-12-03 12:01:38 -08002263 log("Inet hold end, net=" + netType +
Robert Greenwalt6a2db8a2010-09-23 10:05:56 -07002264 ", condition =" + mDefaultInetCondition +
2265 ", published condition =" + mDefaultInetConditionPublished);
2266 }
2267 mInetConditionChangeInFlight = false;
2268
2269 if (mActiveDefaultNetwork == -1) {
Wink Savillee70c6f52010-12-03 12:01:38 -08002270 if (DBG) log("no active default network - aborting");
Robert Greenwalt6a2db8a2010-09-23 10:05:56 -07002271 return;
2272 }
2273 if (mDefaultConnectionSequence != sequence) {
Wink Savillee70c6f52010-12-03 12:01:38 -08002274 if (DBG) log("event hold for obsolete network - aborting");
Robert Greenwalt6a2db8a2010-09-23 10:05:56 -07002275 return;
2276 }
Wink Saville4f0de1e2011-08-04 15:01:58 -07002277 // TODO: Figure out why this optimization sometimes causes a
2278 // change in mDefaultInetCondition to be missed and the
2279 // UI to not be updated.
2280 //if (mDefaultInetConditionPublished == mDefaultInetCondition) {
2281 // if (DBG) log("no change in condition - aborting");
2282 // return;
2283 //}
Robert Greenwalt6a2db8a2010-09-23 10:05:56 -07002284 NetworkInfo networkInfo = mNetTrackers[mActiveDefaultNetwork].getNetworkInfo();
2285 if (networkInfo.isConnected() == false) {
Wink Savillee70c6f52010-12-03 12:01:38 -08002286 if (DBG) log("default network not connected - aborting");
Robert Greenwalt6a2db8a2010-09-23 10:05:56 -07002287 return;
2288 }
2289 mDefaultInetConditionPublished = mDefaultInetCondition;
2290 sendInetConditionBroadcast(networkInfo);
2291 return;
Robert Greenwalt986c7412010-09-08 15:24:47 -07002292 }
Robert Greenwaltc3c5f862010-10-11 16:00:27 -07002293
2294 public synchronized ProxyProperties getProxy() {
2295 if (mGlobalProxy != null) return mGlobalProxy;
2296 if (mDefaultProxy != null) return mDefaultProxy;
2297 return null;
2298 }
2299
2300 public void setGlobalProxy(ProxyProperties proxyProperties) {
2301 enforceChangePermission();
2302 synchronized (mGlobalProxyLock) {
2303 if (proxyProperties == mGlobalProxy) return;
2304 if (proxyProperties != null && proxyProperties.equals(mGlobalProxy)) return;
2305 if (mGlobalProxy != null && mGlobalProxy.equals(proxyProperties)) return;
2306
2307 String host = "";
2308 int port = 0;
2309 String exclList = "";
2310 if (proxyProperties != null && !TextUtils.isEmpty(proxyProperties.getHost())) {
2311 mGlobalProxy = new ProxyProperties(proxyProperties);
2312 host = mGlobalProxy.getHost();
2313 port = mGlobalProxy.getPort();
2314 exclList = mGlobalProxy.getExclusionList();
2315 } else {
2316 mGlobalProxy = null;
2317 }
2318 ContentResolver res = mContext.getContentResolver();
2319 Settings.Secure.putString(res, Settings.Secure.GLOBAL_HTTP_PROXY_HOST, host);
2320 Settings.Secure.putInt(res, Settings.Secure.GLOBAL_HTTP_PROXY_PORT, port);
Robert Greenwalt6f7c6092010-12-02 11:31:00 -08002321 Settings.Secure.putString(res, Settings.Secure.GLOBAL_HTTP_PROXY_EXCLUSION_LIST,
Robert Greenwaltc3c5f862010-10-11 16:00:27 -07002322 exclList);
2323 }
2324
2325 if (mGlobalProxy == null) {
2326 proxyProperties = mDefaultProxy;
2327 }
2328 sendProxyBroadcast(proxyProperties);
2329 }
2330
Robert Greenwalt6f7c6092010-12-02 11:31:00 -08002331 private void loadGlobalProxy() {
2332 ContentResolver res = mContext.getContentResolver();
2333 String host = Settings.Secure.getString(res, Settings.Secure.GLOBAL_HTTP_PROXY_HOST);
2334 int port = Settings.Secure.getInt(res, Settings.Secure.GLOBAL_HTTP_PROXY_PORT, 0);
2335 String exclList = Settings.Secure.getString(res,
2336 Settings.Secure.GLOBAL_HTTP_PROXY_EXCLUSION_LIST);
2337 if (!TextUtils.isEmpty(host)) {
2338 ProxyProperties proxyProperties = new ProxyProperties(host, port, exclList);
2339 synchronized (mGlobalProxyLock) {
2340 mGlobalProxy = proxyProperties;
2341 }
2342 }
2343 }
2344
Robert Greenwaltc3c5f862010-10-11 16:00:27 -07002345 public ProxyProperties getGlobalProxy() {
2346 synchronized (mGlobalProxyLock) {
2347 return mGlobalProxy;
2348 }
2349 }
2350
2351 private void handleApplyDefaultProxy(int type) {
2352 // check if new default - push it out to all VM if so
2353 ProxyProperties proxy = mNetTrackers[type].getLinkProperties().getHttpProxy();
2354 synchronized (this) {
2355 if (mDefaultProxy != null && mDefaultProxy.equals(proxy)) return;
2356 if (mDefaultProxy == proxy) return;
Robert Greenwalt027f5052011-07-29 10:03:37 -07002357 if (proxy != null && !TextUtils.isEmpty(proxy.getHost())) {
Robert Greenwaltc3c5f862010-10-11 16:00:27 -07002358 mDefaultProxy = proxy;
2359 } else {
2360 mDefaultProxy = null;
2361 }
2362 }
Wink Savillee70c6f52010-12-03 12:01:38 -08002363 if (DBG) log("changing default proxy to " + proxy);
Robert Greenwaltc3c5f862010-10-11 16:00:27 -07002364 if ((proxy == null && mGlobalProxy == null) || proxy.equals(mGlobalProxy)) return;
2365 if (mGlobalProxy != null) return;
2366 sendProxyBroadcast(proxy);
2367 }
2368
2369 private void handleDeprecatedGlobalHttpProxy() {
2370 String proxy = Settings.Secure.getString(mContext.getContentResolver(),
2371 Settings.Secure.HTTP_PROXY);
2372 if (!TextUtils.isEmpty(proxy)) {
2373 String data[] = proxy.split(":");
2374 String proxyHost = data[0];
2375 int proxyPort = 8080;
2376 if (data.length > 1) {
2377 try {
2378 proxyPort = Integer.parseInt(data[1]);
2379 } catch (NumberFormatException e) {
2380 return;
2381 }
2382 }
2383 ProxyProperties p = new ProxyProperties(data[0], proxyPort, "");
2384 setGlobalProxy(p);
2385 }
2386 }
2387
2388 private void sendProxyBroadcast(ProxyProperties proxy) {
Robert Greenwalt611291c2010-12-23 15:51:10 -08002389 if (proxy == null) proxy = new ProxyProperties("", 0, "");
Wink Savillee70c6f52010-12-03 12:01:38 -08002390 log("sending Proxy Broadcast for " + proxy);
Robert Greenwaltc3c5f862010-10-11 16:00:27 -07002391 Intent intent = new Intent(Proxy.PROXY_CHANGE_ACTION);
Stan Chesnutt1f2a2ac2011-01-06 11:00:19 -08002392 intent.addFlags(Intent.FLAG_RECEIVER_REPLACE_PENDING |
2393 Intent.FLAG_RECEIVER_REGISTERED_ONLY_BEFORE_BOOT);
Robert Greenwaltc3c5f862010-10-11 16:00:27 -07002394 intent.putExtra(Proxy.EXTRA_PROXY_INFO, proxy);
Robert Greenwaltd93dc8f2010-12-06 11:29:17 -08002395 mContext.sendStickyBroadcast(intent);
Robert Greenwaltc3c5f862010-10-11 16:00:27 -07002396 }
2397
2398 private static class SettingsObserver extends ContentObserver {
2399 private int mWhat;
2400 private Handler mHandler;
2401 SettingsObserver(Handler handler, int what) {
2402 super(handler);
2403 mHandler = handler;
2404 mWhat = what;
2405 }
2406
2407 void observe(Context context) {
2408 ContentResolver resolver = context.getContentResolver();
2409 resolver.registerContentObserver(Settings.Secure.getUriFor(
2410 Settings.Secure.HTTP_PROXY), false, this);
2411 }
2412
2413 @Override
2414 public void onChange(boolean selfChange) {
2415 mHandler.obtainMessage(mWhat).sendToTarget();
2416 }
2417 }
Wink Savillee70c6f52010-12-03 12:01:38 -08002418
2419 private void log(String s) {
2420 Slog.d(TAG, s);
2421 }
2422
2423 private void loge(String s) {
2424 Slog.e(TAG, s);
2425 }
Wink Savilleb7c92c72011-03-12 14:52:01 -08002426 int convertFeatureToNetworkType(String feature){
2427 int networkType = -1;
2428 if (TextUtils.equals(feature, Phone.FEATURE_ENABLE_MMS)) {
2429 networkType = ConnectivityManager.TYPE_MOBILE_MMS;
2430 } else if (TextUtils.equals(feature, Phone.FEATURE_ENABLE_SUPL)) {
2431 networkType = ConnectivityManager.TYPE_MOBILE_SUPL;
2432 } else if (TextUtils.equals(feature, Phone.FEATURE_ENABLE_DUN) ||
2433 TextUtils.equals(feature, Phone.FEATURE_ENABLE_DUN_ALWAYS)) {
2434 networkType = ConnectivityManager.TYPE_MOBILE_DUN;
2435 } else if (TextUtils.equals(feature, Phone.FEATURE_ENABLE_HIPRI)) {
2436 networkType = ConnectivityManager.TYPE_MOBILE_HIPRI;
2437 } else if (TextUtils.equals(feature, Phone.FEATURE_ENABLE_FOTA)) {
2438 networkType = ConnectivityManager.TYPE_MOBILE_FOTA;
2439 } else if (TextUtils.equals(feature, Phone.FEATURE_ENABLE_IMS)) {
2440 networkType = ConnectivityManager.TYPE_MOBILE_IMS;
2441 } else if (TextUtils.equals(feature, Phone.FEATURE_ENABLE_CBS)) {
2442 networkType = ConnectivityManager.TYPE_MOBILE_CBS;
2443 }
2444 return networkType;
2445 }
The Android Open Source Project28527d22009-03-03 19:31:44 -08002446}