blob: d3349cc9c3d33e5c757adb1ac3d965dfe4f6a51a [file] [log] [blame]
The Android Open Source Project28527d22009-03-03 19:31:44 -08001/*
2 * Copyright (C) 2008 The Android Open Source Project
3 *
4 * Licensed under the Apache License, Version 2.0 (the "License");
5 * you may not use this file except in compliance with the License.
6 * You may obtain a copy of the License at
7 *
8 * http://www.apache.org/licenses/LICENSE-2.0
9 *
10 * Unless required by applicable law or agreed to in writing, software
11 * distributed under the License is distributed on an "AS IS" BASIS,
12 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13 * See the License for the specific language governing permissions and
14 * limitations under the License.
15 */
16
17package com.android.server;
18
Jaikumar Ganesh0db51a02010-12-21 22:31:44 -080019import android.bluetooth.BluetoothTetheringDataTracker;
The Android Open Source Project28527d22009-03-03 19:31:44 -080020import android.content.ContentResolver;
21import android.content.Context;
22import android.content.Intent;
23import android.content.pm.PackageManager;
Robert Greenwaltc3c5f862010-10-11 16:00:27 -070024import android.database.ContentObserver;
The Android Open Source Project28527d22009-03-03 19:31:44 -080025import android.net.ConnectivityManager;
Robert Greenwalteb123ac2010-12-06 13:56:24 -080026import android.net.DummyDataStateTracker;
The Android Open Source Project28527d22009-03-03 19:31:44 -080027import android.net.IConnectivityManager;
Jaikumar Ganesh0db51a02010-12-21 22:31:44 -080028import android.net.LinkProperties;
The Android Open Source Project28527d22009-03-03 19:31:44 -080029import android.net.MobileDataStateTracker;
Robert Greenwalt34848c02011-03-25 13:09:25 -070030import android.net.NetworkConfig;
The Android Open Source Project28527d22009-03-03 19:31:44 -080031import android.net.NetworkInfo;
32import android.net.NetworkStateTracker;
Robert Greenwalt7fe44cb2010-08-27 09:24:29 -070033import android.net.NetworkUtils;
Robert Greenwaltc3c5f862010-10-11 16:00:27 -070034import android.net.Proxy;
35import android.net.ProxyProperties;
Hung-ying Tyan4e723422011-01-19 16:48:38 +080036import android.net.vpn.VpnManager;
The Android Open Source Project28527d22009-03-03 19:31:44 -080037import android.net.wifi.WifiStateTracker;
38import android.os.Binder;
39import android.os.Handler;
Wink Saville775aad62010-09-02 19:23:52 -070040import android.os.HandlerThread;
Robert Greenwalt2034b912009-08-12 16:08:25 -070041import android.os.IBinder;
The Android Open Source Project28527d22009-03-03 19:31:44 -080042import android.os.Looper;
43import android.os.Message;
Robert Greenwalt93dc1042010-06-15 12:19:37 -070044import android.os.PowerManager;
Robert Greenwalt2034b912009-08-12 16:08:25 -070045import android.os.RemoteException;
The Android Open Source Project28527d22009-03-03 19:31:44 -080046import android.os.ServiceManager;
47import android.os.SystemProperties;
48import android.provider.Settings;
Robert Greenwalt2034b912009-08-12 16:08:25 -070049import android.text.TextUtils;
The Android Open Source Project28527d22009-03-03 19:31:44 -080050import android.util.EventLog;
Joe Onoratoc2386bb2010-02-26 18:56:32 -080051import android.util.Slog;
The Android Open Source Project28527d22009-03-03 19:31:44 -080052
Robert Greenwalt2034b912009-08-12 16:08:25 -070053import com.android.internal.telephony.Phone;
Robert Greenwalt0c4828c2010-01-26 11:40:34 -080054import com.android.server.connectivity.Tethering;
55
The Android Open Source Project28527d22009-03-03 19:31:44 -080056import java.io.FileDescriptor;
Irfan Sheriff7f132d92010-06-09 15:39:36 -070057import java.io.FileWriter;
58import java.io.IOException;
The Android Open Source Project28527d22009-03-03 19:31:44 -080059import java.io.PrintWriter;
Robert Greenwalta7dfbd32010-06-15 15:43:39 -070060import java.net.InetAddress;
61import java.net.UnknownHostException;
Robert Greenwalt2034b912009-08-12 16:08:25 -070062import java.util.ArrayList;
Robert Greenwalta7dfbd32010-06-15 15:43:39 -070063import java.util.Collection;
Robert Greenwaltd62c7002010-12-29 16:15:02 -080064import java.util.concurrent.atomic.AtomicBoolean;
Robert Greenwalt0e80be12010-09-20 14:35:25 -070065import java.util.GregorianCalendar;
Robert Greenwalt2034b912009-08-12 16:08:25 -070066import java.util.List;
The Android Open Source Project28527d22009-03-03 19:31:44 -080067
68/**
69 * @hide
70 */
71public class ConnectivityService extends IConnectivityManager.Stub {
72
Robert Greenwalt063dc7d2010-10-05 19:12:26 -070073 private static final boolean DBG = true;
The Android Open Source Project28527d22009-03-03 19:31:44 -080074 private static final String TAG = "ConnectivityService";
75
Robert Greenwalt2034b912009-08-12 16:08:25 -070076 // how long to wait before switching back to a radio's default network
77 private static final int RESTORE_DEFAULT_NETWORK_DELAY = 1 * 60 * 1000;
78 // system property that can override the above value
79 private static final String NETWORK_RESTORE_DELAY_PROP_NAME =
80 "android.telephony.apn-restore";
81
Robert Greenwalt0c4828c2010-01-26 11:40:34 -080082 private Tethering mTethering;
Robert Greenwaltf1b66e12010-02-25 12:29:30 -080083 private boolean mTetheringConfigValid = false;
Robert Greenwalt0c4828c2010-01-26 11:40:34 -080084
The Android Open Source Project28527d22009-03-03 19:31:44 -080085 /**
86 * Sometimes we want to refer to the individual network state
87 * trackers separately, and sometimes we just want to treat them
88 * abstractly.
89 */
90 private NetworkStateTracker mNetTrackers[];
Robert Greenwalt2034b912009-08-12 16:08:25 -070091
92 /**
93 * A per Net list of the PID's that requested access to the net
94 * used both as a refcount and for per-PID DNS selection
95 */
96 private List mNetRequestersPids[];
97
Irfan Sheriff653e2a22010-06-07 09:03:04 -070098 private WifiWatchdogService mWifiWatchdogService;
99
Robert Greenwalt2034b912009-08-12 16:08:25 -0700100 // priority order of the nettrackers
101 // (excluding dynamically set mNetworkPreference)
102 // TODO - move mNetworkTypePreference into this
103 private int[] mPriorityList;
104
The Android Open Source Project28527d22009-03-03 19:31:44 -0800105 private Context mContext;
106 private int mNetworkPreference;
Robert Greenwalt2034b912009-08-12 16:08:25 -0700107 private int mActiveDefaultNetwork = -1;
Robert Greenwalt986c7412010-09-08 15:24:47 -0700108 // 0 is full bad, 100 is full good
109 private int mDefaultInetCondition = 0;
110 private int mDefaultInetConditionPublished = 0;
111 private boolean mInetConditionChangeInFlight = false;
112 private int mDefaultConnectionSequence = 0;
The Android Open Source Project28527d22009-03-03 19:31:44 -0800113
114 private int mNumDnsEntries;
The Android Open Source Project28527d22009-03-03 19:31:44 -0800115
116 private boolean mTestMode;
Joe Onorato56023ad2010-09-01 21:18:22 -0700117 private static ConnectivityService sServiceInstance;
The Android Open Source Project28527d22009-03-03 19:31:44 -0800118
Robert Greenwaltd62c7002010-12-29 16:15:02 -0800119 private AtomicBoolean mBackgroundDataEnabled = new AtomicBoolean(true);
120
Robert Greenwalt6a2db8a2010-09-23 10:05:56 -0700121 private static final int ENABLED = 1;
122 private static final int DISABLED = 0;
123
124 // Share the event space with NetworkStateTracker (which can't see this
125 // internal class but sends us events). If you change these, change
126 // NetworkStateTracker.java too.
127 private static final int MIN_NETWORK_STATE_TRACKER_EVENT = 1;
128 private static final int MAX_NETWORK_STATE_TRACKER_EVENT = 100;
129
130 /**
131 * used internally as a delayed event to make us switch back to the
132 * default network
133 */
134 private static final int EVENT_RESTORE_DEFAULT_NETWORK =
135 MAX_NETWORK_STATE_TRACKER_EVENT + 1;
136
137 /**
138 * used internally to change our mobile data enabled flag
139 */
140 private static final int EVENT_CHANGE_MOBILE_DATA_ENABLED =
141 MAX_NETWORK_STATE_TRACKER_EVENT + 2;
142
143 /**
144 * used internally to change our network preference setting
145 * arg1 = networkType to prefer
146 */
147 private static final int EVENT_SET_NETWORK_PREFERENCE =
148 MAX_NETWORK_STATE_TRACKER_EVENT + 3;
149
150 /**
151 * used internally to synchronize inet condition reports
152 * arg1 = networkType
153 * arg2 = condition (0 bad, 100 good)
154 */
155 private static final int EVENT_INET_CONDITION_CHANGE =
156 MAX_NETWORK_STATE_TRACKER_EVENT + 4;
157
158 /**
159 * used internally to mark the end of inet condition hold periods
160 * arg1 = networkType
161 */
162 private static final int EVENT_INET_CONDITION_HOLD_END =
163 MAX_NETWORK_STATE_TRACKER_EVENT + 5;
164
165 /**
166 * used internally to set the background data preference
167 * arg1 = TRUE for enabled, FALSE for disabled
168 */
169 private static final int EVENT_SET_BACKGROUND_DATA =
170 MAX_NETWORK_STATE_TRACKER_EVENT + 6;
171
172 /**
173 * used internally to set enable/disable cellular data
174 * arg1 = ENBALED or DISABLED
175 */
176 private static final int EVENT_SET_MOBILE_DATA =
177 MAX_NETWORK_STATE_TRACKER_EVENT + 7;
178
Robert Greenwaltccb36f92010-09-24 14:32:21 -0700179 /**
180 * used internally to clear a wakelock when transitioning
181 * from one net to another
182 */
183 private static final int EVENT_CLEAR_NET_TRANSITION_WAKELOCK =
184 MAX_NETWORK_STATE_TRACKER_EVENT + 8;
185
Robert Greenwaltc3c5f862010-10-11 16:00:27 -0700186 /**
187 * used internally to reload global proxy settings
188 */
189 private static final int EVENT_APPLY_GLOBAL_HTTP_PROXY =
190 MAX_NETWORK_STATE_TRACKER_EVENT + 9;
191
Robert Greenwalt34848c02011-03-25 13:09:25 -0700192 /**
193 * used internally to set external dependency met/unmet
194 * arg1 = ENABLED (met) or DISABLED (unmet)
195 * arg2 = NetworkType
196 */
197 private static final int EVENT_SET_DEPENDENCY_MET =
198 MAX_NETWORK_STATE_TRACKER_EVENT + 10;
199
Robert Greenwalt2034b912009-08-12 16:08:25 -0700200 private Handler mHandler;
201
202 // list of DeathRecipients used to make sure features are turned off when
203 // a process dies
204 private List mFeatureUsers;
205
Mike Lockwoodfde2b762009-08-14 14:18:49 -0400206 private boolean mSystemReady;
Dianne Hackborna417ff82009-12-08 19:45:14 -0800207 private Intent mInitialBroadcast;
Mike Lockwoodfde2b762009-08-14 14:18:49 -0400208
Robert Greenwalt93dc1042010-06-15 12:19:37 -0700209 private PowerManager.WakeLock mNetTransitionWakeLock;
210 private String mNetTransitionWakeLockCausedBy = "";
211 private int mNetTransitionWakeLockSerialNumber;
212 private int mNetTransitionWakeLockTimeout;
213
Robert Greenwalt94daa182010-09-01 11:34:05 -0700214 private InetAddress mDefaultDns;
215
Robert Greenwalt0e80be12010-09-20 14:35:25 -0700216 // used in DBG mode to track inet condition reports
217 private static final int INET_CONDITION_LOG_MAX_SIZE = 15;
218 private ArrayList mInetLog;
219
Robert Greenwaltc3c5f862010-10-11 16:00:27 -0700220 // track the current default http proxy - tell the world if we get a new one (real change)
221 private ProxyProperties mDefaultProxy = null;
222 // track the global proxy.
223 private ProxyProperties mGlobalProxy = null;
224 private final Object mGlobalProxyLock = new Object();
225
226 private SettingsObserver mSettingsObserver;
227
Robert Greenwalt34848c02011-03-25 13:09:25 -0700228 NetworkConfig[] mNetConfigs;
Robert Greenwaltec05b3c2009-10-30 14:17:42 -0700229 int mNetworksDefined;
Robert Greenwalt2034b912009-08-12 16:08:25 -0700230
Robert Greenwalt12c44552009-12-07 11:33:18 -0800231 private static class RadioAttributes {
Robert Greenwalt2034b912009-08-12 16:08:25 -0700232 public int mSimultaneity;
233 public int mType;
234 public RadioAttributes(String init) {
235 String fragments[] = init.split(",");
Robert Greenwaltec05b3c2009-10-30 14:17:42 -0700236 mType = Integer.parseInt(fragments[0]);
237 mSimultaneity = Integer.parseInt(fragments[1]);
Robert Greenwalt2034b912009-08-12 16:08:25 -0700238 }
239 }
240 RadioAttributes[] mRadioAttributes;
241
Wink Saville775aad62010-09-02 19:23:52 -0700242 public static synchronized ConnectivityService getInstance(Context context) {
243 if (sServiceInstance == null) {
244 sServiceInstance = new ConnectivityService(context);
The Android Open Source Project28527d22009-03-03 19:31:44 -0800245 }
Wink Saville775aad62010-09-02 19:23:52 -0700246 return sServiceInstance;
The Android Open Source Project28527d22009-03-03 19:31:44 -0800247 }
Robert Greenwalt0659da32009-07-16 17:21:39 -0700248
The Android Open Source Project28527d22009-03-03 19:31:44 -0800249 private ConnectivityService(Context context) {
Wink Savillee70c6f52010-12-03 12:01:38 -0800250 if (DBG) log("ConnectivityService starting up");
Robert Greenwaltd48f8ee2010-01-14 17:47:58 -0800251
Wink Saville775aad62010-09-02 19:23:52 -0700252 HandlerThread handlerThread = new HandlerThread("ConnectivityServiceThread");
253 handlerThread.start();
254 mHandler = new MyHandler(handlerThread.getLooper());
255
Robert Greenwaltd62c7002010-12-29 16:15:02 -0800256 mBackgroundDataEnabled.set(Settings.Secure.getInt(context.getContentResolver(),
257 Settings.Secure.BACKGROUND_DATA, 1) == 1);
258
Robert Greenwaltd48f8ee2010-01-14 17:47:58 -0800259 // setup our unique device name
Robert Greenwalt82cde132010-12-06 09:30:17 -0800260 if (TextUtils.isEmpty(SystemProperties.get("net.hostname"))) {
261 String id = Settings.Secure.getString(context.getContentResolver(),
262 Settings.Secure.ANDROID_ID);
263 if (id != null && id.length() > 0) {
264 String name = new String("android_").concat(id);
265 SystemProperties.set("net.hostname", name);
266 }
Robert Greenwaltd48f8ee2010-01-14 17:47:58 -0800267 }
268
Robert Greenwalt94daa182010-09-01 11:34:05 -0700269 // read our default dns server ip
270 String dns = Settings.Secure.getString(context.getContentResolver(),
271 Settings.Secure.DEFAULT_DNS_SERVER);
272 if (dns == null || dns.length() == 0) {
273 dns = context.getResources().getString(
274 com.android.internal.R.string.config_default_dns_server);
275 }
276 try {
Robert Greenwalt35e34d12011-02-22 16:00:42 -0800277 mDefaultDns = NetworkUtils.numericToInetAddress(dns);
278 } catch (IllegalArgumentException e) {
Wink Savillee70c6f52010-12-03 12:01:38 -0800279 loge("Error setting defaultDns using " + dns);
Robert Greenwalt94daa182010-09-01 11:34:05 -0700280 }
281
The Android Open Source Project28527d22009-03-03 19:31:44 -0800282 mContext = context;
Robert Greenwalt93dc1042010-06-15 12:19:37 -0700283
284 PowerManager powerManager = (PowerManager)mContext.getSystemService(Context.POWER_SERVICE);
285 mNetTransitionWakeLock = powerManager.newWakeLock(PowerManager.PARTIAL_WAKE_LOCK, TAG);
286 mNetTransitionWakeLockTimeout = mContext.getResources().getInteger(
287 com.android.internal.R.integer.config_networkTransitionTimeout);
288
Robert Greenwalt2034b912009-08-12 16:08:25 -0700289 mNetTrackers = new NetworkStateTracker[
290 ConnectivityManager.MAX_NETWORK_TYPE+1];
Robert Greenwalt0659da32009-07-16 17:21:39 -0700291
The Android Open Source Project28527d22009-03-03 19:31:44 -0800292 mNetworkPreference = getPersistedNetworkPreference();
Robert Greenwalt0659da32009-07-16 17:21:39 -0700293
Robert Greenwaltec05b3c2009-10-30 14:17:42 -0700294 mRadioAttributes = new RadioAttributes[ConnectivityManager.MAX_RADIO_TYPE+1];
Robert Greenwalt34848c02011-03-25 13:09:25 -0700295 mNetConfigs = new NetworkConfig[ConnectivityManager.MAX_NETWORK_TYPE+1];
Robert Greenwaltec05b3c2009-10-30 14:17:42 -0700296
Robert Greenwalt2034b912009-08-12 16:08:25 -0700297 // Load device network attributes from resources
Robert Greenwalt2034b912009-08-12 16:08:25 -0700298 String[] raStrings = context.getResources().getStringArray(
299 com.android.internal.R.array.radioAttributes);
Robert Greenwaltec05b3c2009-10-30 14:17:42 -0700300 for (String raString : raStrings) {
301 RadioAttributes r = new RadioAttributes(raString);
302 if (r.mType > ConnectivityManager.MAX_RADIO_TYPE) {
Wink Savillee70c6f52010-12-03 12:01:38 -0800303 loge("Error in radioAttributes - ignoring attempt to define type " + r.mType);
Robert Greenwaltec05b3c2009-10-30 14:17:42 -0700304 continue;
305 }
306 if (mRadioAttributes[r.mType] != null) {
Wink Savillee70c6f52010-12-03 12:01:38 -0800307 loge("Error in radioAttributes - ignoring attempt to redefine type " +
Robert Greenwaltec05b3c2009-10-30 14:17:42 -0700308 r.mType);
309 continue;
310 }
Robert Greenwalt2034b912009-08-12 16:08:25 -0700311 mRadioAttributes[r.mType] = r;
312 }
313
Robert Greenwaltec05b3c2009-10-30 14:17:42 -0700314 String[] naStrings = context.getResources().getStringArray(
315 com.android.internal.R.array.networkAttributes);
316 for (String naString : naStrings) {
317 try {
Robert Greenwalt34848c02011-03-25 13:09:25 -0700318 NetworkConfig n = new NetworkConfig(naString);
Robert Greenwaltec05b3c2009-10-30 14:17:42 -0700319 if (n.mType > ConnectivityManager.MAX_NETWORK_TYPE) {
Wink Savillee70c6f52010-12-03 12:01:38 -0800320 loge("Error in networkAttributes - ignoring attempt to define type " +
Robert Greenwaltec05b3c2009-10-30 14:17:42 -0700321 n.mType);
322 continue;
Robert Greenwalt2034b912009-08-12 16:08:25 -0700323 }
Robert Greenwalt34848c02011-03-25 13:09:25 -0700324 if (mNetConfigs[n.mType] != null) {
Wink Savillee70c6f52010-12-03 12:01:38 -0800325 loge("Error in networkAttributes - ignoring attempt to redefine type " +
Robert Greenwaltec05b3c2009-10-30 14:17:42 -0700326 n.mType);
327 continue;
328 }
329 if (mRadioAttributes[n.mRadio] == null) {
Wink Savillee70c6f52010-12-03 12:01:38 -0800330 loge("Error in networkAttributes - ignoring attempt to use undefined " +
Robert Greenwaltec05b3c2009-10-30 14:17:42 -0700331 "radio " + n.mRadio + " in network type " + n.mType);
332 continue;
333 }
Robert Greenwalt34848c02011-03-25 13:09:25 -0700334 mNetConfigs[n.mType] = n;
Robert Greenwaltec05b3c2009-10-30 14:17:42 -0700335 mNetworksDefined++;
336 } catch(Exception e) {
337 // ignore it - leave the entry null
Robert Greenwalt2034b912009-08-12 16:08:25 -0700338 }
339 }
340
Robert Greenwaltec05b3c2009-10-30 14:17:42 -0700341 // high priority first
342 mPriorityList = new int[mNetworksDefined];
343 {
344 int insertionPoint = mNetworksDefined-1;
345 int currentLowest = 0;
346 int nextLowest = 0;
347 while (insertionPoint > -1) {
Robert Greenwalt34848c02011-03-25 13:09:25 -0700348 for (NetworkConfig na : mNetConfigs) {
Robert Greenwaltec05b3c2009-10-30 14:17:42 -0700349 if (na == null) continue;
350 if (na.mPriority < currentLowest) continue;
351 if (na.mPriority > currentLowest) {
352 if (na.mPriority < nextLowest || nextLowest == 0) {
353 nextLowest = na.mPriority;
354 }
355 continue;
356 }
357 mPriorityList[insertionPoint--] = na.mType;
358 }
359 currentLowest = nextLowest;
360 nextLowest = 0;
361 }
362 }
363
364 mNetRequestersPids = new ArrayList[ConnectivityManager.MAX_NETWORK_TYPE+1];
365 for (int i : mPriorityList) {
Robert Greenwalt2034b912009-08-12 16:08:25 -0700366 mNetRequestersPids[i] = new ArrayList();
367 }
368
369 mFeatureUsers = new ArrayList();
370
Robert Greenwaltec05b3c2009-10-30 14:17:42 -0700371 mNumDnsEntries = 0;
372
373 mTestMode = SystemProperties.get("cm.test.mode").equals("true")
374 && SystemProperties.get("ro.build.type").equals("eng");
The Android Open Source Project28527d22009-03-03 19:31:44 -0800375 /*
376 * Create the network state trackers for Wi-Fi and mobile
377 * data. Maybe this could be done with a factory class,
378 * but it's not clear that it's worth it, given that
379 * the number of different network types is not going
380 * to change very often.
381 */
Robert Greenwaltec05b3c2009-10-30 14:17:42 -0700382 for (int netType : mPriorityList) {
Robert Greenwalt34848c02011-03-25 13:09:25 -0700383 switch (mNetConfigs[netType].mRadio) {
Robert Greenwaltec05b3c2009-10-30 14:17:42 -0700384 case ConnectivityManager.TYPE_WIFI:
Wink Savillee70c6f52010-12-03 12:01:38 -0800385 if (DBG) log("Starting Wifi Service.");
Wink Saville7fabfa22010-08-13 16:11:42 -0700386 WifiStateTracker wst = new WifiStateTracker();
Irfan Sheriff25be0762010-07-28 09:35:20 -0700387 WifiService wifiService = new WifiService(context);
Robert Greenwaltec05b3c2009-10-30 14:17:42 -0700388 ServiceManager.addService(Context.WIFI_SERVICE, wifiService);
Irfan Sheriff25be0762010-07-28 09:35:20 -0700389 wifiService.checkAndStartWifi();
Robert Greenwaltec05b3c2009-10-30 14:17:42 -0700390 mNetTrackers[ConnectivityManager.TYPE_WIFI] = wst;
Wink Saville7fabfa22010-08-13 16:11:42 -0700391 wst.startMonitoring(context, mHandler);
The Android Open Source Project28527d22009-03-03 19:31:44 -0800392
Irfan Sheriff653e2a22010-06-07 09:03:04 -0700393 //TODO: as part of WWS refactor, create only when needed
Irfan Sheriff25be0762010-07-28 09:35:20 -0700394 mWifiWatchdogService = new WifiWatchdogService(context);
Irfan Sheriff653e2a22010-06-07 09:03:04 -0700395
Robert Greenwaltec05b3c2009-10-30 14:17:42 -0700396 break;
397 case ConnectivityManager.TYPE_MOBILE:
Wink Saville7fabfa22010-08-13 16:11:42 -0700398 mNetTrackers[netType] = new MobileDataStateTracker(netType,
Robert Greenwalt34848c02011-03-25 13:09:25 -0700399 mNetConfigs[netType].mName);
Wink Saville7fabfa22010-08-13 16:11:42 -0700400 mNetTrackers[netType].startMonitoring(context, mHandler);
Robert Greenwaltec05b3c2009-10-30 14:17:42 -0700401 break;
Robert Greenwalteb123ac2010-12-06 13:56:24 -0800402 case ConnectivityManager.TYPE_DUMMY:
403 mNetTrackers[netType] = new DummyDataStateTracker(netType,
Robert Greenwalt34848c02011-03-25 13:09:25 -0700404 mNetConfigs[netType].mName);
Robert Greenwalteb123ac2010-12-06 13:56:24 -0800405 mNetTrackers[netType].startMonitoring(context, mHandler);
406 break;
Jaikumar Ganesh0db51a02010-12-21 22:31:44 -0800407 case ConnectivityManager.TYPE_BLUETOOTH:
408 mNetTrackers[netType] = BluetoothTetheringDataTracker.getInstance();
409 mNetTrackers[netType].startMonitoring(context, mHandler);
410 break;
Robert Greenwaltec05b3c2009-10-30 14:17:42 -0700411 default:
Wink Savillee70c6f52010-12-03 12:01:38 -0800412 loge("Trying to create a DataStateTracker for an unknown radio type " +
Robert Greenwalt34848c02011-03-25 13:09:25 -0700413 mNetConfigs[netType].mRadio);
Robert Greenwaltec05b3c2009-10-30 14:17:42 -0700414 continue;
415 }
416 }
Robert Greenwalt0c4828c2010-01-26 11:40:34 -0800417
Robert Greenwaltc0b6c602010-03-11 15:03:08 -0800418 mTethering = new Tethering(mContext, mHandler.getLooper());
Robert Greenwaltf1b66e12010-02-25 12:29:30 -0800419 mTetheringConfigValid = (((mNetTrackers[ConnectivityManager.TYPE_MOBILE_DUN] != null) ||
420 !mTethering.isDunRequired()) &&
421 (mTethering.getTetherableUsbRegexs().length != 0 ||
Danica Chang96567052010-08-11 14:54:43 -0700422 mTethering.getTetherableWifiRegexs().length != 0 ||
423 mTethering.getTetherableBluetoothRegexs().length != 0) &&
Robert Greenwaltf1b66e12010-02-25 12:29:30 -0800424 mTethering.getUpstreamIfaceRegexs().length != 0);
425
Robert Greenwalt0e80be12010-09-20 14:35:25 -0700426 if (DBG) {
427 mInetLog = new ArrayList();
428 }
Robert Greenwaltc3c5f862010-10-11 16:00:27 -0700429
430 mSettingsObserver = new SettingsObserver(mHandler, EVENT_APPLY_GLOBAL_HTTP_PROXY);
431 mSettingsObserver.observe(mContext);
Robert Greenwalt6f7c6092010-12-02 11:31:00 -0800432
433 loadGlobalProxy();
Hung-ying Tyan4e723422011-01-19 16:48:38 +0800434
435 VpnManager.startVpnService(context);
The Android Open Source Project28527d22009-03-03 19:31:44 -0800436 }
437
Robert Greenwaltec05b3c2009-10-30 14:17:42 -0700438
The Android Open Source Project28527d22009-03-03 19:31:44 -0800439 /**
Robert Greenwalt0659da32009-07-16 17:21:39 -0700440 * Sets the preferred network.
The Android Open Source Project28527d22009-03-03 19:31:44 -0800441 * @param preference the new preference
442 */
Robert Greenwalt6a2db8a2010-09-23 10:05:56 -0700443 public void setNetworkPreference(int preference) {
The Android Open Source Project28527d22009-03-03 19:31:44 -0800444 enforceChangePermission();
Robert Greenwalt6a2db8a2010-09-23 10:05:56 -0700445
446 mHandler.sendMessage(mHandler.obtainMessage(EVENT_SET_NETWORK_PREFERENCE, preference, 0));
The Android Open Source Project28527d22009-03-03 19:31:44 -0800447 }
448
449 public int getNetworkPreference() {
450 enforceAccessPermission();
Robert Greenwalt6a2db8a2010-09-23 10:05:56 -0700451 int preference;
452 synchronized(this) {
453 preference = mNetworkPreference;
454 }
455 return preference;
The Android Open Source Project28527d22009-03-03 19:31:44 -0800456 }
457
Robert Greenwalt6a2db8a2010-09-23 10:05:56 -0700458 private void handleSetNetworkPreference(int preference) {
459 if (ConnectivityManager.isNetworkTypeValid(preference) &&
Robert Greenwalt34848c02011-03-25 13:09:25 -0700460 mNetConfigs[preference] != null &&
461 mNetConfigs[preference].isDefault()) {
Robert Greenwalt6a2db8a2010-09-23 10:05:56 -0700462 if (mNetworkPreference != preference) {
463 final ContentResolver cr = mContext.getContentResolver();
464 Settings.Secure.putInt(cr, Settings.Secure.NETWORK_PREFERENCE, preference);
465 synchronized(this) {
466 mNetworkPreference = preference;
467 }
468 enforcePreference();
469 }
470 }
The Android Open Source Project28527d22009-03-03 19:31:44 -0800471 }
Robert Greenwalt0659da32009-07-16 17:21:39 -0700472
The Android Open Source Project28527d22009-03-03 19:31:44 -0800473 private int getPersistedNetworkPreference() {
474 final ContentResolver cr = mContext.getContentResolver();
475
476 final int networkPrefSetting = Settings.Secure
477 .getInt(cr, Settings.Secure.NETWORK_PREFERENCE, -1);
478 if (networkPrefSetting != -1) {
479 return networkPrefSetting;
480 }
481
482 return ConnectivityManager.DEFAULT_NETWORK_PREFERENCE;
483 }
Robert Greenwalt0659da32009-07-16 17:21:39 -0700484
The Android Open Source Project28527d22009-03-03 19:31:44 -0800485 /**
Robert Greenwalt0659da32009-07-16 17:21:39 -0700486 * Make the state of network connectivity conform to the preference settings
The Android Open Source Project28527d22009-03-03 19:31:44 -0800487 * In this method, we only tear down a non-preferred network. Establishing
488 * a connection to the preferred network is taken care of when we handle
489 * the disconnect event from the non-preferred network
490 * (see {@link #handleDisconnect(NetworkInfo)}).
491 */
492 private void enforcePreference() {
Robert Greenwalt2034b912009-08-12 16:08:25 -0700493 if (mNetTrackers[mNetworkPreference].getNetworkInfo().isConnected())
The Android Open Source Project28527d22009-03-03 19:31:44 -0800494 return;
495
Robert Greenwalt2034b912009-08-12 16:08:25 -0700496 if (!mNetTrackers[mNetworkPreference].isAvailable())
497 return;
The Android Open Source Project28527d22009-03-03 19:31:44 -0800498
Robert Greenwalt2034b912009-08-12 16:08:25 -0700499 for (int t=0; t <= ConnectivityManager.MAX_RADIO_TYPE; t++) {
Robert Greenwaltec05b3c2009-10-30 14:17:42 -0700500 if (t != mNetworkPreference && mNetTrackers[t] != null &&
Robert Greenwalt2034b912009-08-12 16:08:25 -0700501 mNetTrackers[t].getNetworkInfo().isConnected()) {
Robert Greenwaltf3f045b2009-08-20 15:25:14 -0700502 if (DBG) {
Wink Savillee70c6f52010-12-03 12:01:38 -0800503 log("tearing down " + mNetTrackers[t].getNetworkInfo() +
Robert Greenwaltf3f045b2009-08-20 15:25:14 -0700504 " in enforcePreference");
505 }
Robert Greenwalt2034b912009-08-12 16:08:25 -0700506 teardown(mNetTrackers[t]);
The Android Open Source Project28527d22009-03-03 19:31:44 -0800507 }
508 }
509 }
510
511 private boolean teardown(NetworkStateTracker netTracker) {
512 if (netTracker.teardown()) {
513 netTracker.setTeardownRequested(true);
514 return true;
515 } else {
516 return false;
517 }
518 }
519
520 /**
521 * Return NetworkInfo for the active (i.e., connected) network interface.
522 * It is assumed that at most one network is active at a time. If more
523 * than one is active, it is indeterminate which will be returned.
Robert Greenwalt0659da32009-07-16 17:21:39 -0700524 * @return the info for the active network, or {@code null} if none is
525 * active
The Android Open Source Project28527d22009-03-03 19:31:44 -0800526 */
527 public NetworkInfo getActiveNetworkInfo() {
528 enforceAccessPermission();
Robert Greenwalt2034b912009-08-12 16:08:25 -0700529 for (int type=0; type <= ConnectivityManager.MAX_NETWORK_TYPE; type++) {
Robert Greenwalt34848c02011-03-25 13:09:25 -0700530 if (mNetConfigs[type] == null || !mNetConfigs[type].isDefault()) {
Robert Greenwalt2034b912009-08-12 16:08:25 -0700531 continue;
532 }
533 NetworkStateTracker t = mNetTrackers[type];
The Android Open Source Project28527d22009-03-03 19:31:44 -0800534 NetworkInfo info = t.getNetworkInfo();
535 if (info.isConnected()) {
Wink Savillee70c6f52010-12-03 12:01:38 -0800536 if (DBG && type != mActiveDefaultNetwork) {
537 loge("connected default network is not mActiveDefaultNetwork!");
538 }
The Android Open Source Project28527d22009-03-03 19:31:44 -0800539 return info;
540 }
541 }
542 return null;
543 }
544
545 public NetworkInfo getNetworkInfo(int networkType) {
546 enforceAccessPermission();
547 if (ConnectivityManager.isNetworkTypeValid(networkType)) {
548 NetworkStateTracker t = mNetTrackers[networkType];
549 if (t != null)
550 return t.getNetworkInfo();
551 }
552 return null;
553 }
554
555 public NetworkInfo[] getAllNetworkInfo() {
556 enforceAccessPermission();
Robert Greenwaltec05b3c2009-10-30 14:17:42 -0700557 NetworkInfo[] result = new NetworkInfo[mNetworksDefined];
The Android Open Source Project28527d22009-03-03 19:31:44 -0800558 int i = 0;
559 for (NetworkStateTracker t : mNetTrackers) {
Robert Greenwaltec05b3c2009-10-30 14:17:42 -0700560 if(t != null) result[i++] = t.getNetworkInfo();
The Android Open Source Project28527d22009-03-03 19:31:44 -0800561 }
562 return result;
563 }
564
Robert Greenwalt9f0ee4f2010-09-14 09:18:02 -0700565 /**
566 * Return LinkProperties for the active (i.e., connected) default
567 * network interface. It is assumed that at most one default network
568 * is active at a time. If more than one is active, it is indeterminate
569 * which will be returned.
570 * @return the ip properties for the active network, or {@code null} if
571 * none is active
572 */
573 public LinkProperties getActiveLinkProperties() {
574 enforceAccessPermission();
575 for (int type=0; type <= ConnectivityManager.MAX_NETWORK_TYPE; type++) {
Robert Greenwalt34848c02011-03-25 13:09:25 -0700576 if (mNetConfigs[type] == null || !mNetConfigs[type].isDefault()) {
Robert Greenwalt9f0ee4f2010-09-14 09:18:02 -0700577 continue;
578 }
579 NetworkStateTracker t = mNetTrackers[type];
580 NetworkInfo info = t.getNetworkInfo();
581 if (info.isConnected()) {
582 return t.getLinkProperties();
583 }
584 }
585 return null;
586 }
587
588 public LinkProperties getLinkProperties(int networkType) {
589 enforceAccessPermission();
590 if (ConnectivityManager.isNetworkTypeValid(networkType)) {
591 NetworkStateTracker t = mNetTrackers[networkType];
592 if (t != null) return t.getLinkProperties();
593 }
594 return null;
595 }
596
The Android Open Source Project28527d22009-03-03 19:31:44 -0800597 public boolean setRadios(boolean turnOn) {
598 boolean result = true;
599 enforceChangePermission();
600 for (NetworkStateTracker t : mNetTrackers) {
Robert Greenwaltec05b3c2009-10-30 14:17:42 -0700601 if (t != null) result = t.setRadio(turnOn) && result;
The Android Open Source Project28527d22009-03-03 19:31:44 -0800602 }
603 return result;
604 }
605
606 public boolean setRadio(int netType, boolean turnOn) {
607 enforceChangePermission();
608 if (!ConnectivityManager.isNetworkTypeValid(netType)) {
609 return false;
610 }
611 NetworkStateTracker tracker = mNetTrackers[netType];
612 return tracker != null && tracker.setRadio(turnOn);
613 }
614
Robert Greenwaltaffc3a12009-09-27 17:27:04 -0700615 /**
616 * Used to notice when the calling process dies so we can self-expire
617 *
618 * Also used to know if the process has cleaned up after itself when
619 * our auto-expire timer goes off. The timer has a link to an object.
620 *
621 */
Robert Greenwalt2034b912009-08-12 16:08:25 -0700622 private class FeatureUser implements IBinder.DeathRecipient {
623 int mNetworkType;
624 String mFeature;
625 IBinder mBinder;
626 int mPid;
627 int mUid;
Robert Greenwalt3eeb6032009-12-21 18:24:07 -0800628 long mCreateTime;
Robert Greenwalt2034b912009-08-12 16:08:25 -0700629
630 FeatureUser(int type, String feature, IBinder binder) {
631 super();
632 mNetworkType = type;
633 mFeature = feature;
634 mBinder = binder;
635 mPid = getCallingPid();
636 mUid = getCallingUid();
Robert Greenwalt3eeb6032009-12-21 18:24:07 -0800637 mCreateTime = System.currentTimeMillis();
Robert Greenwaltaffc3a12009-09-27 17:27:04 -0700638
Robert Greenwalt2034b912009-08-12 16:08:25 -0700639 try {
640 mBinder.linkToDeath(this, 0);
641 } catch (RemoteException e) {
642 binderDied();
643 }
644 }
645
646 void unlinkDeathRecipient() {
647 mBinder.unlinkToDeath(this, 0);
648 }
649
650 public void binderDied() {
Wink Savillee70c6f52010-12-03 12:01:38 -0800651 log("ConnectivityService FeatureUser binderDied(" +
Robert Greenwalt3eeb6032009-12-21 18:24:07 -0800652 mNetworkType + ", " + mFeature + ", " + mBinder + "), created " +
653 (System.currentTimeMillis() - mCreateTime) + " mSec ago");
Robert Greenwaltaffc3a12009-09-27 17:27:04 -0700654 stopUsingNetworkFeature(this, false);
Robert Greenwalt2034b912009-08-12 16:08:25 -0700655 }
656
Robert Greenwaltaffc3a12009-09-27 17:27:04 -0700657 public void expire() {
Wink Savillee70c6f52010-12-03 12:01:38 -0800658 log("ConnectivityService FeatureUser expire(" +
Robert Greenwalt3eeb6032009-12-21 18:24:07 -0800659 mNetworkType + ", " + mFeature + ", " + mBinder +"), created " +
660 (System.currentTimeMillis() - mCreateTime) + " mSec ago");
Robert Greenwaltaffc3a12009-09-27 17:27:04 -0700661 stopUsingNetworkFeature(this, false);
662 }
Robert Greenwalt3eeb6032009-12-21 18:24:07 -0800663
664 public String toString() {
665 return "FeatureUser("+mNetworkType+","+mFeature+","+mPid+","+mUid+"), created " +
666 (System.currentTimeMillis() - mCreateTime) + " mSec ago";
667 }
Robert Greenwalt2034b912009-08-12 16:08:25 -0700668 }
669
Robert Greenwaltaffc3a12009-09-27 17:27:04 -0700670 // javadoc from interface
Robert Greenwalt2034b912009-08-12 16:08:25 -0700671 public int startUsingNetworkFeature(int networkType, String feature,
672 IBinder binder) {
673 if (DBG) {
Wink Savillee70c6f52010-12-03 12:01:38 -0800674 log("startUsingNetworkFeature for net " + networkType + ": " + feature);
Robert Greenwalt2034b912009-08-12 16:08:25 -0700675 }
The Android Open Source Project28527d22009-03-03 19:31:44 -0800676 enforceChangePermission();
Robert Greenwaltec05b3c2009-10-30 14:17:42 -0700677 if (!ConnectivityManager.isNetworkTypeValid(networkType) ||
Robert Greenwalt34848c02011-03-25 13:09:25 -0700678 mNetConfigs[networkType] == null) {
Robert Greenwalt2034b912009-08-12 16:08:25 -0700679 return Phone.APN_REQUEST_FAILED;
The Android Open Source Project28527d22009-03-03 19:31:44 -0800680 }
Robert Greenwalt2034b912009-08-12 16:08:25 -0700681
Robert Greenwaltaffc3a12009-09-27 17:27:04 -0700682 FeatureUser f = new FeatureUser(networkType, feature, binder);
Robert Greenwalt2034b912009-08-12 16:08:25 -0700683
684 // TODO - move this into the MobileDataStateTracker
685 int usedNetworkType = networkType;
686 if(networkType == ConnectivityManager.TYPE_MOBILE) {
Wink Savilleb7c92c72011-03-12 14:52:01 -0800687 usedNetworkType = convertFeatureToNetworkType(feature);
688 if (usedNetworkType < 0) {
689 Slog.e(TAG, "Can't match any netTracker!");
690 usedNetworkType = networkType;
Robert Greenwalt2034b912009-08-12 16:08:25 -0700691 }
692 }
693 NetworkStateTracker network = mNetTrackers[usedNetworkType];
694 if (network != null) {
Robert Greenwalt5364d752010-12-15 13:26:33 -0800695 Integer currentPid = new Integer(getCallingPid());
Robert Greenwalt2034b912009-08-12 16:08:25 -0700696 if (usedNetworkType != networkType) {
Robert Greenwalt2034b912009-08-12 16:08:25 -0700697 NetworkStateTracker radio = mNetTrackers[networkType];
698 NetworkInfo ni = network.getNetworkInfo();
699
700 if (ni.isAvailable() == false) {
Wink Savillee70c6f52010-12-03 12:01:38 -0800701 if (DBG) log("special network not available");
Robert Greenwalt2cc87442010-12-29 14:35:21 -0800702 if (!TextUtils.equals(feature,Phone.FEATURE_ENABLE_DUN_ALWAYS)) {
703 return Phone.APN_TYPE_NOT_AVAILABLE;
704 } else {
705 // else make the attempt anyway - probably giving REQUEST_STARTED below
706 }
Robert Greenwalt2034b912009-08-12 16:08:25 -0700707 }
708
Robert Greenwaltaffc3a12009-09-27 17:27:04 -0700709 synchronized(this) {
710 mFeatureUsers.add(f);
711 if (!mNetRequestersPids[usedNetworkType].contains(currentPid)) {
712 // this gets used for per-pid dns when connected
713 mNetRequestersPids[usedNetworkType].add(currentPid);
714 }
Robert Greenwalt2034b912009-08-12 16:08:25 -0700715 }
Robert Greenwalt6a2db8a2010-09-23 10:05:56 -0700716 mHandler.sendMessageDelayed(mHandler.obtainMessage(EVENT_RESTORE_DEFAULT_NETWORK,
Robert Greenwaltaffc3a12009-09-27 17:27:04 -0700717 f), getRestoreDefaultNetworkDelay());
718
Robert Greenwalt2034b912009-08-12 16:08:25 -0700719
Robert Greenwalta52c75a2009-08-19 20:19:33 -0700720 if ((ni.isConnectedOrConnecting() == true) &&
721 !network.isTeardownRequested()) {
Robert Greenwalt2034b912009-08-12 16:08:25 -0700722 if (ni.isConnected() == true) {
723 // add the pid-specific dns
Robert Greenwalt3afbead2010-07-23 15:46:26 -0700724 handleDnsConfigurationChange(networkType);
Wink Savillee70c6f52010-12-03 12:01:38 -0800725 if (DBG) log("special network already active");
Robert Greenwalt2034b912009-08-12 16:08:25 -0700726 return Phone.APN_ALREADY_ACTIVE;
727 }
Wink Savillee70c6f52010-12-03 12:01:38 -0800728 if (DBG) log("special network already connecting");
Robert Greenwalt2034b912009-08-12 16:08:25 -0700729 return Phone.APN_REQUEST_STARTED;
730 }
731
732 // check if the radio in play can make another contact
733 // assume if cannot for now
734
Wink Savillee70c6f52010-12-03 12:01:38 -0800735 if (DBG) log("reconnecting to special network");
Robert Greenwalt2034b912009-08-12 16:08:25 -0700736 network.reconnect();
737 return Phone.APN_REQUEST_STARTED;
738 } else {
Robert Greenwalt5364d752010-12-15 13:26:33 -0800739 // need to remember this unsupported request so we respond appropriately on stop
740 synchronized(this) {
741 mFeatureUsers.add(f);
742 if (!mNetRequestersPids[usedNetworkType].contains(currentPid)) {
743 // this gets used for per-pid dns when connected
744 mNetRequestersPids[usedNetworkType].add(currentPid);
745 }
746 }
Robert Greenwaltd391e892010-05-18 10:52:51 -0700747 return -1;
Robert Greenwalt2034b912009-08-12 16:08:25 -0700748 }
749 }
750 return Phone.APN_TYPE_NOT_AVAILABLE;
The Android Open Source Project28527d22009-03-03 19:31:44 -0800751 }
752
Robert Greenwaltaffc3a12009-09-27 17:27:04 -0700753 // javadoc from interface
The Android Open Source Project28527d22009-03-03 19:31:44 -0800754 public int stopUsingNetworkFeature(int networkType, String feature) {
Robert Greenwalt28f43012009-10-06 17:52:40 -0700755 enforceChangePermission();
756
Robert Greenwaltaffc3a12009-09-27 17:27:04 -0700757 int pid = getCallingPid();
758 int uid = getCallingUid();
759
760 FeatureUser u = null;
761 boolean found = false;
762
763 synchronized(this) {
764 for (int i = 0; i < mFeatureUsers.size() ; i++) {
765 u = (FeatureUser)mFeatureUsers.get(i);
766 if (uid == u.mUid && pid == u.mPid &&
767 networkType == u.mNetworkType &&
768 TextUtils.equals(feature, u.mFeature)) {
769 found = true;
770 break;
771 }
772 }
773 }
774 if (found && u != null) {
775 // stop regardless of how many other time this proc had called start
776 return stopUsingNetworkFeature(u, true);
777 } else {
778 // none found!
Wink Savillee70c6f52010-12-03 12:01:38 -0800779 if (DBG) log("ignoring stopUsingNetworkFeature - not a live request");
Robert Greenwaltaffc3a12009-09-27 17:27:04 -0700780 return 1;
781 }
Robert Greenwalt2034b912009-08-12 16:08:25 -0700782 }
783
Robert Greenwaltaffc3a12009-09-27 17:27:04 -0700784 private int stopUsingNetworkFeature(FeatureUser u, boolean ignoreDups) {
785 int networkType = u.mNetworkType;
786 String feature = u.mFeature;
787 int pid = u.mPid;
788 int uid = u.mUid;
789
790 NetworkStateTracker tracker = null;
791 boolean callTeardown = false; // used to carry our decision outside of sync block
792
Robert Greenwalt2034b912009-08-12 16:08:25 -0700793 if (DBG) {
Wink Savillee70c6f52010-12-03 12:01:38 -0800794 log("stopUsingNetworkFeature for net " + networkType +
Robert Greenwalt2034b912009-08-12 16:08:25 -0700795 ": " + feature);
796 }
Robert Greenwalt28f43012009-10-06 17:52:40 -0700797
The Android Open Source Project28527d22009-03-03 19:31:44 -0800798 if (!ConnectivityManager.isNetworkTypeValid(networkType)) {
799 return -1;
800 }
Robert Greenwalt2034b912009-08-12 16:08:25 -0700801
Robert Greenwaltaffc3a12009-09-27 17:27:04 -0700802 // need to link the mFeatureUsers list with the mNetRequestersPids state in this
803 // sync block
804 synchronized(this) {
805 // check if this process still has an outstanding start request
806 if (!mFeatureUsers.contains(u)) {
Wink Savillee70c6f52010-12-03 12:01:38 -0800807 if (DBG) log("ignoring - this process has no outstanding requests");
Robert Greenwalt2034b912009-08-12 16:08:25 -0700808 return 1;
809 }
Robert Greenwaltaffc3a12009-09-27 17:27:04 -0700810 u.unlinkDeathRecipient();
811 mFeatureUsers.remove(mFeatureUsers.indexOf(u));
812 // If we care about duplicate requests, check for that here.
813 //
814 // This is done to support the extension of a request - the app
815 // can request we start the network feature again and renew the
816 // auto-shutoff delay. Normal "stop" calls from the app though
817 // do not pay attention to duplicate requests - in effect the
818 // API does not refcount and a single stop will counter multiple starts.
819 if (ignoreDups == false) {
820 for (int i = 0; i < mFeatureUsers.size() ; i++) {
821 FeatureUser x = (FeatureUser)mFeatureUsers.get(i);
822 if (x.mUid == u.mUid && x.mPid == u.mPid &&
823 x.mNetworkType == u.mNetworkType &&
824 TextUtils.equals(x.mFeature, u.mFeature)) {
Wink Savillee70c6f52010-12-03 12:01:38 -0800825 if (DBG) log("ignoring stopUsingNetworkFeature as dup is found");
Robert Greenwaltaffc3a12009-09-27 17:27:04 -0700826 return 1;
827 }
828 }
Robert Greenwalt2034b912009-08-12 16:08:25 -0700829 }
Robert Greenwaltaffc3a12009-09-27 17:27:04 -0700830
831 // TODO - move to MobileDataStateTracker
832 int usedNetworkType = networkType;
833 if (networkType == ConnectivityManager.TYPE_MOBILE) {
Wink Savilleb7c92c72011-03-12 14:52:01 -0800834 usedNetworkType = convertFeatureToNetworkType(feature);
835 if (usedNetworkType < 0) {
836 usedNetworkType = networkType;
Robert Greenwaltaffc3a12009-09-27 17:27:04 -0700837 }
838 }
839 tracker = mNetTrackers[usedNetworkType];
Robert Greenwaltec05b3c2009-10-30 14:17:42 -0700840 if (tracker == null) {
Wink Savillee70c6f52010-12-03 12:01:38 -0800841 if (DBG) log("ignoring - no known tracker for net type " + usedNetworkType);
Robert Greenwaltec05b3c2009-10-30 14:17:42 -0700842 return -1;
843 }
844 if (usedNetworkType != networkType) {
Robert Greenwaltaffc3a12009-09-27 17:27:04 -0700845 Integer currentPid = new Integer(pid);
Robert Greenwaltaffc3a12009-09-27 17:27:04 -0700846 mNetRequestersPids[usedNetworkType].remove(currentPid);
Robert Greenwalt0ca68a02009-12-17 14:54:59 -0800847 reassessPidDns(pid, true);
Robert Greenwaltaffc3a12009-09-27 17:27:04 -0700848 if (mNetRequestersPids[usedNetworkType].size() != 0) {
Wink Savillee70c6f52010-12-03 12:01:38 -0800849 if (DBG) log("not tearing down special network - " +
Robert Greenwaltaffc3a12009-09-27 17:27:04 -0700850 "others still using it");
851 return 1;
852 }
853 callTeardown = true;
Robert Greenwalt9f3be4c2011-01-10 11:58:31 -0800854 } else {
855 if (DBG) log("not a known feature - dropping");
Robert Greenwaltaffc3a12009-09-27 17:27:04 -0700856 }
857 }
Wink Savillee70c6f52010-12-03 12:01:38 -0800858 if (DBG) log("Doing network teardown");
Robert Greenwaltaffc3a12009-09-27 17:27:04 -0700859 if (callTeardown) {
860 tracker.teardown();
Robert Greenwalt2034b912009-08-12 16:08:25 -0700861 return 1;
862 } else {
Robert Greenwaltd391e892010-05-18 10:52:51 -0700863 return -1;
Robert Greenwalt2034b912009-08-12 16:08:25 -0700864 }
The Android Open Source Project28527d22009-03-03 19:31:44 -0800865 }
866
867 /**
Robert Greenwalt7fe44cb2010-08-27 09:24:29 -0700868 * @deprecated use requestRouteToHostAddress instead
869 *
The Android Open Source Project28527d22009-03-03 19:31:44 -0800870 * Ensure that a network route exists to deliver traffic to the specified
871 * host via the specified network interface.
Robert Greenwalt0659da32009-07-16 17:21:39 -0700872 * @param networkType the type of the network over which traffic to the
873 * specified host is to be routed
874 * @param hostAddress the IP address of the host to which the route is
875 * desired
The Android Open Source Project28527d22009-03-03 19:31:44 -0800876 * @return {@code true} on success, {@code false} on failure
877 */
878 public boolean requestRouteToHost(int networkType, int hostAddress) {
Robert Greenwalt7fe44cb2010-08-27 09:24:29 -0700879 InetAddress inetAddress = NetworkUtils.intToInetAddress(hostAddress);
880
881 if (inetAddress == null) {
882 return false;
883 }
884
885 return requestRouteToHostAddress(networkType, inetAddress.getAddress());
886 }
887
888 /**
889 * Ensure that a network route exists to deliver traffic to the specified
890 * host via the specified network interface.
891 * @param networkType the type of the network over which traffic to the
892 * specified host is to be routed
893 * @param hostAddress the IP address of the host to which the route is
894 * desired
895 * @return {@code true} on success, {@code false} on failure
896 */
897 public boolean requestRouteToHostAddress(int networkType, byte[] hostAddress) {
The Android Open Source Project28527d22009-03-03 19:31:44 -0800898 enforceChangePermission();
899 if (!ConnectivityManager.isNetworkTypeValid(networkType)) {
900 return false;
901 }
902 NetworkStateTracker tracker = mNetTrackers[networkType];
Robert Greenwalt4666ed02009-09-10 15:06:20 -0700903
Robert Greenwaltec05b3c2009-10-30 14:17:42 -0700904 if (tracker == null || !tracker.getNetworkInfo().isConnected() ||
905 tracker.isTeardownRequested()) {
Robert Greenwalt4666ed02009-09-10 15:06:20 -0700906 if (DBG) {
Wink Savillee70c6f52010-12-03 12:01:38 -0800907 log("requestRouteToHostAddress on down network " +
Robert Greenwalt7fe44cb2010-08-27 09:24:29 -0700908 "(" + networkType + ") - dropped");
Robert Greenwalt4666ed02009-09-10 15:06:20 -0700909 }
910 return false;
The Android Open Source Project28527d22009-03-03 19:31:44 -0800911 }
Robert Greenwalta7dfbd32010-06-15 15:43:39 -0700912 try {
Robert Greenwalt7fe44cb2010-08-27 09:24:29 -0700913 InetAddress addr = InetAddress.getByAddress(hostAddress);
Robert Greenwalta7dfbd32010-06-15 15:43:39 -0700914 return addHostRoute(tracker, addr);
915 } catch (UnknownHostException e) {}
916 return false;
Irfan Sheriff7f132d92010-06-09 15:39:36 -0700917 }
918
919 /**
920 * Ensure that a network route exists to deliver traffic to the specified
921 * host via the mobile data network.
922 * @param hostAddress the IP address of the host to which the route is desired,
923 * in network byte order.
Robert Greenwalta7dfbd32010-06-15 15:43:39 -0700924 * TODO - deprecate
Irfan Sheriff7f132d92010-06-09 15:39:36 -0700925 * @return {@code true} on success, {@code false} on failure
926 */
Robert Greenwalta7dfbd32010-06-15 15:43:39 -0700927 private boolean addHostRoute(NetworkStateTracker nt, InetAddress hostAddress) {
Irfan Sheriff7f132d92010-06-09 15:39:36 -0700928 if (nt.getNetworkInfo().getType() == ConnectivityManager.TYPE_WIFI) {
929 return false;
930 }
931
Robert Greenwalt1f1bcfe2010-08-30 10:56:47 -0700932 LinkProperties p = nt.getLinkProperties();
Robert Greenwalta7dfbd32010-06-15 15:43:39 -0700933 if (p == null) return false;
934 String interfaceName = p.getInterfaceName();
Irfan Sheriff7f132d92010-06-09 15:39:36 -0700935
936 if (DBG) {
Wink Savillee70c6f52010-12-03 12:01:38 -0800937 log("Requested host route to " + hostAddress + "(" + interfaceName + ")");
Irfan Sheriff7f132d92010-06-09 15:39:36 -0700938 }
Robert Greenwalta7dfbd32010-06-15 15:43:39 -0700939 if (interfaceName != null) {
Robert Greenwalt7fe44cb2010-08-27 09:24:29 -0700940 return NetworkUtils.addHostRoute(interfaceName, hostAddress, null);
Irfan Sheriff7f132d92010-06-09 15:39:36 -0700941 } else {
Wink Savillee70c6f52010-12-03 12:01:38 -0800942 if (DBG) loge("addHostRoute failed due to null interface name");
Irfan Sheriff7f132d92010-06-09 15:39:36 -0700943 return false;
944 }
The Android Open Source Project28527d22009-03-03 19:31:44 -0800945 }
946
947 /**
948 * @see ConnectivityManager#getBackgroundDataSetting()
949 */
950 public boolean getBackgroundDataSetting() {
Robert Greenwaltd62c7002010-12-29 16:15:02 -0800951 return mBackgroundDataEnabled.get();
The Android Open Source Project28527d22009-03-03 19:31:44 -0800952 }
Robert Greenwalt0659da32009-07-16 17:21:39 -0700953
The Android Open Source Project28527d22009-03-03 19:31:44 -0800954 /**
955 * @see ConnectivityManager#setBackgroundDataSetting(boolean)
956 */
957 public void setBackgroundDataSetting(boolean allowBackgroundDataUsage) {
958 mContext.enforceCallingOrSelfPermission(
959 android.Manifest.permission.CHANGE_BACKGROUND_DATA_SETTING,
960 "ConnectivityService");
Robert Greenwalt0659da32009-07-16 17:21:39 -0700961
Robert Greenwaltd62c7002010-12-29 16:15:02 -0800962 mBackgroundDataEnabled.set(allowBackgroundDataUsage);
963
Robert Greenwalt6a2db8a2010-09-23 10:05:56 -0700964 mHandler.sendMessage(mHandler.obtainMessage(EVENT_SET_BACKGROUND_DATA,
965 (allowBackgroundDataUsage ? ENABLED : DISABLED), 0));
966 }
The Android Open Source Project28527d22009-03-03 19:31:44 -0800967
Robert Greenwalt6a2db8a2010-09-23 10:05:56 -0700968 private void handleSetBackgroundData(boolean enabled) {
Robert Greenwalt0ffdef12011-02-25 13:44:09 -0800969 Settings.Secure.putInt(mContext.getContentResolver(),
970 Settings.Secure.BACKGROUND_DATA, enabled ? 1 : 0);
971 Intent broadcast = new Intent(
972 ConnectivityManager.ACTION_BACKGROUND_DATA_SETTING_CHANGED);
973 mContext.sendBroadcast(broadcast);
Robert Greenwalt0659da32009-07-16 17:21:39 -0700974 }
975
Robert Greenwalt1b21f6c2010-02-23 18:58:05 -0800976 /**
977 * @see ConnectivityManager#getMobileDataEnabled()
978 */
979 public boolean getMobileDataEnabled() {
Wink Savilleb9024c62010-12-07 10:31:02 -0800980 // TODO: This detail should probably be in DataConnectionTracker's
981 // which is where we store the value and maybe make this
982 // asynchronous.
Robert Greenwalt1b21f6c2010-02-23 18:58:05 -0800983 enforceAccessPermission();
984 boolean retVal = Settings.Secure.getInt(mContext.getContentResolver(),
985 Settings.Secure.MOBILE_DATA, 1) == 1;
Wink Savillee70c6f52010-12-03 12:01:38 -0800986 if (DBG) log("getMobileDataEnabled returning " + retVal);
Robert Greenwalt1b21f6c2010-02-23 18:58:05 -0800987 return retVal;
988 }
989
Robert Greenwalt34848c02011-03-25 13:09:25 -0700990 public void setDataDependency(int networkType, boolean met) {
991 enforceChangePermission();
992 if (DBG) {
993 log("setDataDependency(" + networkType + ", " + met + ")");
994 }
995 mHandler.sendMessage(mHandler.obtainMessage(EVENT_SET_DEPENDENCY_MET,
996 (met ? ENABLED : DISABLED), networkType));
997 }
998
999 private void handleSetDependencyMet(int networkType, boolean met) {
1000 if (mNetTrackers[networkType] != null) {
1001 if (DBG) {
1002 log("handleSetDependencyMet(" + networkType + ", " + met + ")");
1003 }
1004 mNetTrackers[networkType].setDependencyMet(met);
1005 }
1006 }
1007
Robert Greenwalt1b21f6c2010-02-23 18:58:05 -08001008 /**
1009 * @see ConnectivityManager#setMobileDataEnabled(boolean)
1010 */
Robert Greenwalt6a2db8a2010-09-23 10:05:56 -07001011 public void setMobileDataEnabled(boolean enabled) {
Robert Greenwalt1b21f6c2010-02-23 18:58:05 -08001012 enforceChangePermission();
Wink Savillee70c6f52010-12-03 12:01:38 -08001013 if (DBG) log("setMobileDataEnabled(" + enabled + ")");
Robert Greenwalt1b21f6c2010-02-23 18:58:05 -08001014
Robert Greenwalt6a2db8a2010-09-23 10:05:56 -07001015 mHandler.sendMessage(mHandler.obtainMessage(EVENT_SET_MOBILE_DATA,
Robert Greenwalt34848c02011-03-25 13:09:25 -07001016 (enabled ? ENABLED : DISABLED), 0));
Robert Greenwalt6a2db8a2010-09-23 10:05:56 -07001017 }
1018
1019 private void handleSetMobileData(boolean enabled) {
Wink Savilleb9024c62010-12-07 10:31:02 -08001020 if (mNetTrackers[ConnectivityManager.TYPE_MOBILE] != null) {
1021 if (DBG) {
1022 Slog.d(TAG, mNetTrackers[ConnectivityManager.TYPE_MOBILE].toString() + enabled);
Robert Greenwalt1b21f6c2010-02-23 18:58:05 -08001023 }
Wink Savilleb9024c62010-12-07 10:31:02 -08001024 mNetTrackers[ConnectivityManager.TYPE_MOBILE].setDataEnable(enabled);
Robert Greenwalt1b21f6c2010-02-23 18:58:05 -08001025 }
1026 }
1027
The Android Open Source Project28527d22009-03-03 19:31:44 -08001028 private void enforceAccessPermission() {
Robert Greenwalt0659da32009-07-16 17:21:39 -07001029 mContext.enforceCallingOrSelfPermission(
1030 android.Manifest.permission.ACCESS_NETWORK_STATE,
1031 "ConnectivityService");
The Android Open Source Project28527d22009-03-03 19:31:44 -08001032 }
1033
1034 private void enforceChangePermission() {
Robert Greenwalt0659da32009-07-16 17:21:39 -07001035 mContext.enforceCallingOrSelfPermission(
1036 android.Manifest.permission.CHANGE_NETWORK_STATE,
1037 "ConnectivityService");
The Android Open Source Project28527d22009-03-03 19:31:44 -08001038 }
1039
Robert Greenwalt0c4828c2010-01-26 11:40:34 -08001040 // TODO Make this a special check when it goes public
1041 private void enforceTetherChangePermission() {
1042 mContext.enforceCallingOrSelfPermission(
1043 android.Manifest.permission.CHANGE_NETWORK_STATE,
1044 "ConnectivityService");
1045 }
1046
Robert Greenwalt8e87f122010-02-11 18:18:40 -08001047 private void enforceTetherAccessPermission() {
1048 mContext.enforceCallingOrSelfPermission(
1049 android.Manifest.permission.ACCESS_NETWORK_STATE,
1050 "ConnectivityService");
1051 }
1052
Robert Greenwalt93dc1042010-06-15 12:19:37 -07001053 private void enforceConnectivityInternalPermission() {
1054 mContext.enforceCallingOrSelfPermission(
1055 android.Manifest.permission.CONNECTIVITY_INTERNAL,
1056 "ConnectivityService");
1057 }
1058
The Android Open Source Project28527d22009-03-03 19:31:44 -08001059 /**
Robert Greenwalt0659da32009-07-16 17:21:39 -07001060 * Handle a {@code DISCONNECTED} event. If this pertains to the non-active
1061 * network, we ignore it. If it is for the active network, we send out a
1062 * broadcast. But first, we check whether it might be possible to connect
1063 * to a different network.
The Android Open Source Project28527d22009-03-03 19:31:44 -08001064 * @param info the {@code NetworkInfo} for the network
1065 */
1066 private void handleDisconnect(NetworkInfo info) {
1067
Robert Greenwalt2034b912009-08-12 16:08:25 -07001068 int prevNetType = info.getType();
The Android Open Source Project28527d22009-03-03 19:31:44 -08001069
Robert Greenwalt2034b912009-08-12 16:08:25 -07001070 mNetTrackers[prevNetType].setTeardownRequested(false);
The Android Open Source Project28527d22009-03-03 19:31:44 -08001071 /*
1072 * If the disconnected network is not the active one, then don't report
1073 * this as a loss of connectivity. What probably happened is that we're
1074 * getting the disconnect for a network that we explicitly disabled
1075 * in accordance with network preference policies.
1076 */
Robert Greenwalt34848c02011-03-25 13:09:25 -07001077 if (!mNetConfigs[prevNetType].isDefault()) {
Robert Greenwalt2034b912009-08-12 16:08:25 -07001078 List pids = mNetRequestersPids[prevNetType];
1079 for (int i = 0; i<pids.size(); i++) {
1080 Integer pid = (Integer)pids.get(i);
1081 // will remove them because the net's no longer connected
1082 // need to do this now as only now do we know the pids and
1083 // can properly null things that are no longer referenced.
1084 reassessPidDns(pid.intValue(), false);
The Android Open Source Project28527d22009-03-03 19:31:44 -08001085 }
The Android Open Source Project28527d22009-03-03 19:31:44 -08001086 }
1087
The Android Open Source Project28527d22009-03-03 19:31:44 -08001088 Intent intent = new Intent(ConnectivityManager.CONNECTIVITY_ACTION);
1089 intent.putExtra(ConnectivityManager.EXTRA_NETWORK_INFO, info);
1090 if (info.isFailover()) {
1091 intent.putExtra(ConnectivityManager.EXTRA_IS_FAILOVER, true);
1092 info.setFailover(false);
1093 }
1094 if (info.getReason() != null) {
1095 intent.putExtra(ConnectivityManager.EXTRA_REASON, info.getReason());
1096 }
1097 if (info.getExtraInfo() != null) {
Robert Greenwalt0659da32009-07-16 17:21:39 -07001098 intent.putExtra(ConnectivityManager.EXTRA_EXTRA_INFO,
1099 info.getExtraInfo());
The Android Open Source Project28527d22009-03-03 19:31:44 -08001100 }
Robert Greenwalt2034b912009-08-12 16:08:25 -07001101
Robert Greenwalt34848c02011-03-25 13:09:25 -07001102 if (mNetConfigs[prevNetType].isDefault()) {
Robert Greenwalt4b7c55e2011-01-11 13:56:33 -08001103 tryFailover(prevNetType);
1104 if (mActiveDefaultNetwork != -1) {
1105 NetworkInfo switchTo = mNetTrackers[mActiveDefaultNetwork].getNetworkInfo();
Robert Greenwalt3cc68d32010-01-25 17:54:29 -08001106 intent.putExtra(ConnectivityManager.EXTRA_OTHER_NETWORK_INFO, switchTo);
1107 } else {
Robert Greenwaltb2a9e492010-09-20 18:01:43 -07001108 mDefaultInetConditionPublished = 0; // we're not connected anymore
Robert Greenwalt3cc68d32010-01-25 17:54:29 -08001109 intent.putExtra(ConnectivityManager.EXTRA_NO_CONNECTIVITY, true);
1110 }
Robert Greenwaltf55ced92010-01-20 19:29:41 -08001111 }
Robert Greenwaltb2a9e492010-09-20 18:01:43 -07001112 intent.putExtra(ConnectivityManager.EXTRA_INET_CONDITION, mDefaultInetConditionPublished);
Robert Greenwaltf55ced92010-01-20 19:29:41 -08001113 // do this before we broadcast the change
Robert Greenwalt3afbead2010-07-23 15:46:26 -07001114 handleConnectivityChange(prevNetType);
Robert Greenwaltf55ced92010-01-20 19:29:41 -08001115
1116 sendStickyBroadcast(intent);
1117 /*
1118 * If the failover network is already connected, then immediately send
1119 * out a followup broadcast indicating successful failover
1120 */
Robert Greenwalt4b7c55e2011-01-11 13:56:33 -08001121 if (mActiveDefaultNetwork != -1) {
1122 sendConnectedBroadcast(mNetTrackers[mActiveDefaultNetwork].getNetworkInfo());
Robert Greenwaltf55ced92010-01-20 19:29:41 -08001123 }
1124 }
1125
Robert Greenwalt4b7c55e2011-01-11 13:56:33 -08001126 private void tryFailover(int prevNetType) {
Robert Greenwalt2034b912009-08-12 16:08:25 -07001127 /*
Robert Greenwalt92564852011-01-06 15:41:07 -08001128 * If this is a default network, check if other defaults are available.
1129 * Try to reconnect on all available and let them hash it out when
1130 * more than one connects.
Robert Greenwalt2034b912009-08-12 16:08:25 -07001131 */
Robert Greenwalt34848c02011-03-25 13:09:25 -07001132 if (mNetConfigs[prevNetType].isDefault()) {
Robert Greenwalt2034b912009-08-12 16:08:25 -07001133 if (mActiveDefaultNetwork == prevNetType) {
1134 mActiveDefaultNetwork = -1;
1135 }
1136
Robert Greenwalt4b7c55e2011-01-11 13:56:33 -08001137 // don't signal a reconnect for anything lower or equal priority than our
1138 // current connected default
1139 // TODO - don't filter by priority now - nice optimization but risky
1140// int currentPriority = -1;
1141// if (mActiveDefaultNetwork != -1) {
Robert Greenwalt34848c02011-03-25 13:09:25 -07001142// currentPriority = mNetConfigs[mActiveDefaultNetwork].mPriority;
Robert Greenwalt4b7c55e2011-01-11 13:56:33 -08001143// }
Robert Greenwaltf55ced92010-01-20 19:29:41 -08001144 for (int checkType=0; checkType <= ConnectivityManager.MAX_NETWORK_TYPE; checkType++) {
Robert Greenwaltec05b3c2009-10-30 14:17:42 -07001145 if (checkType == prevNetType) continue;
Robert Greenwalt34848c02011-03-25 13:09:25 -07001146 if (mNetConfigs[checkType] == null) continue;
1147 if (!mNetConfigs[checkType].isDefault()) continue;
Wink Saville72a95b92011-01-26 15:43:49 -08001148
1149// Enabling the isAvailable() optimization caused mobile to not get
1150// selected if it was in the middle of error handling. Specifically
1151// a moble connection that took 30 seconds to complete the DEACTIVATE_DATA_CALL
1152// would not be available and we wouldn't get connected to anything.
1153// So removing the isAvailable() optimization below for now. TODO: This
1154// optimization should work and we need to investigate why it doesn't work.
1155// This could be related to how DEACTIVATE_DATA_CALL is reporting its
1156// complete before it is really complete.
1157// if (!mNetTrackers[checkType].isAvailable()) continue;
1158
Robert Greenwalt34848c02011-03-25 13:09:25 -07001159// if (currentPriority >= mNetConfigs[checkType].mPriority) continue;
Robert Greenwalt2034b912009-08-12 16:08:25 -07001160
Robert Greenwalt4b7c55e2011-01-11 13:56:33 -08001161 NetworkStateTracker checkTracker = mNetTrackers[checkType];
1162 NetworkInfo checkInfo = checkTracker.getNetworkInfo();
1163 if (!checkInfo.isConnectedOrConnecting() || checkTracker.isTeardownRequested()) {
1164 checkInfo.setFailover(true);
1165 checkTracker.reconnect();
Robert Greenwalt2034b912009-08-12 16:08:25 -07001166 }
Robert Greenwalt4b7c55e2011-01-11 13:56:33 -08001167 if (DBG) log("Attempting to switch to " + checkInfo.getTypeName());
Robert Greenwalt2034b912009-08-12 16:08:25 -07001168 }
The Android Open Source Project28527d22009-03-03 19:31:44 -08001169 }
The Android Open Source Project28527d22009-03-03 19:31:44 -08001170 }
1171
1172 private void sendConnectedBroadcast(NetworkInfo info) {
Robert Greenwaltd3401f92010-09-15 17:36:33 -07001173 sendGeneralBroadcast(info, ConnectivityManager.CONNECTIVITY_ACTION);
1174 }
1175
1176 private void sendInetConditionBroadcast(NetworkInfo info) {
1177 sendGeneralBroadcast(info, ConnectivityManager.INET_CONDITION_ACTION);
1178 }
1179
1180 private void sendGeneralBroadcast(NetworkInfo info, String bcastType) {
1181 Intent intent = new Intent(bcastType);
The Android Open Source Project28527d22009-03-03 19:31:44 -08001182 intent.putExtra(ConnectivityManager.EXTRA_NETWORK_INFO, info);
1183 if (info.isFailover()) {
1184 intent.putExtra(ConnectivityManager.EXTRA_IS_FAILOVER, true);
1185 info.setFailover(false);
1186 }
1187 if (info.getReason() != null) {
1188 intent.putExtra(ConnectivityManager.EXTRA_REASON, info.getReason());
1189 }
1190 if (info.getExtraInfo() != null) {
Robert Greenwalt0659da32009-07-16 17:21:39 -07001191 intent.putExtra(ConnectivityManager.EXTRA_EXTRA_INFO,
1192 info.getExtraInfo());
The Android Open Source Project28527d22009-03-03 19:31:44 -08001193 }
Robert Greenwalt986c7412010-09-08 15:24:47 -07001194 intent.putExtra(ConnectivityManager.EXTRA_INET_CONDITION, mDefaultInetConditionPublished);
Mike Lockwoodfde2b762009-08-14 14:18:49 -04001195 sendStickyBroadcast(intent);
The Android Open Source Project28527d22009-03-03 19:31:44 -08001196 }
1197
1198 /**
1199 * Called when an attempt to fail over to another network has failed.
1200 * @param info the {@link NetworkInfo} for the failed network
1201 */
1202 private void handleConnectionFailure(NetworkInfo info) {
1203 mNetTrackers[info.getType()].setTeardownRequested(false);
The Android Open Source Project28527d22009-03-03 19:31:44 -08001204
Robert Greenwalt2034b912009-08-12 16:08:25 -07001205 String reason = info.getReason();
1206 String extraInfo = info.getExtraInfo();
Robert Greenwalt0659da32009-07-16 17:21:39 -07001207
Robert Greenwalte981bc52010-10-08 16:35:52 -07001208 String reasonText;
1209 if (reason == null) {
1210 reasonText = ".";
1211 } else {
1212 reasonText = " (" + reason + ").";
The Android Open Source Project28527d22009-03-03 19:31:44 -08001213 }
Wink Savillee70c6f52010-12-03 12:01:38 -08001214 loge("Attempt to connect to " + info.getTypeName() + " failed" + reasonText);
Robert Greenwalt2034b912009-08-12 16:08:25 -07001215
1216 Intent intent = new Intent(ConnectivityManager.CONNECTIVITY_ACTION);
1217 intent.putExtra(ConnectivityManager.EXTRA_NETWORK_INFO, info);
1218 if (getActiveNetworkInfo() == null) {
1219 intent.putExtra(ConnectivityManager.EXTRA_NO_CONNECTIVITY, true);
1220 }
1221 if (reason != null) {
1222 intent.putExtra(ConnectivityManager.EXTRA_REASON, reason);
1223 }
1224 if (extraInfo != null) {
1225 intent.putExtra(ConnectivityManager.EXTRA_EXTRA_INFO, extraInfo);
1226 }
1227 if (info.isFailover()) {
1228 intent.putExtra(ConnectivityManager.EXTRA_IS_FAILOVER, true);
1229 info.setFailover(false);
1230 }
Robert Greenwaltf55ced92010-01-20 19:29:41 -08001231
Robert Greenwalt34848c02011-03-25 13:09:25 -07001232 if (mNetConfigs[info.getType()].isDefault()) {
Robert Greenwalt4b7c55e2011-01-11 13:56:33 -08001233 tryFailover(info.getType());
1234 if (mActiveDefaultNetwork != -1) {
1235 NetworkInfo switchTo = mNetTrackers[mActiveDefaultNetwork].getNetworkInfo();
Robert Greenwalt3cc68d32010-01-25 17:54:29 -08001236 intent.putExtra(ConnectivityManager.EXTRA_OTHER_NETWORK_INFO, switchTo);
1237 } else {
Robert Greenwaltb2a9e492010-09-20 18:01:43 -07001238 mDefaultInetConditionPublished = 0;
Robert Greenwalt3cc68d32010-01-25 17:54:29 -08001239 intent.putExtra(ConnectivityManager.EXTRA_NO_CONNECTIVITY, true);
1240 }
Robert Greenwaltf55ced92010-01-20 19:29:41 -08001241 }
Robert Greenwalt3cc68d32010-01-25 17:54:29 -08001242
Robert Greenwaltb2a9e492010-09-20 18:01:43 -07001243 intent.putExtra(ConnectivityManager.EXTRA_INET_CONDITION, mDefaultInetConditionPublished);
Mike Lockwoodfde2b762009-08-14 14:18:49 -04001244 sendStickyBroadcast(intent);
Robert Greenwaltf55ced92010-01-20 19:29:41 -08001245 /*
1246 * If the failover network is already connected, then immediately send
1247 * out a followup broadcast indicating successful failover
1248 */
Robert Greenwalt4b7c55e2011-01-11 13:56:33 -08001249 if (mActiveDefaultNetwork != -1) {
1250 sendConnectedBroadcast(mNetTrackers[mActiveDefaultNetwork].getNetworkInfo());
Robert Greenwaltf55ced92010-01-20 19:29:41 -08001251 }
Mike Lockwoodfde2b762009-08-14 14:18:49 -04001252 }
1253
1254 private void sendStickyBroadcast(Intent intent) {
1255 synchronized(this) {
Dianne Hackborna417ff82009-12-08 19:45:14 -08001256 if (!mSystemReady) {
1257 mInitialBroadcast = new Intent(intent);
Mike Lockwoodfde2b762009-08-14 14:18:49 -04001258 }
Dianne Hackborna417ff82009-12-08 19:45:14 -08001259 intent.addFlags(Intent.FLAG_RECEIVER_REGISTERED_ONLY_BEFORE_BOOT);
1260 mContext.sendStickyBroadcast(intent);
Mike Lockwoodfde2b762009-08-14 14:18:49 -04001261 }
1262 }
1263
1264 void systemReady() {
1265 synchronized(this) {
1266 mSystemReady = true;
Dianne Hackborna417ff82009-12-08 19:45:14 -08001267 if (mInitialBroadcast != null) {
1268 mContext.sendStickyBroadcast(mInitialBroadcast);
1269 mInitialBroadcast = null;
Mike Lockwoodfde2b762009-08-14 14:18:49 -04001270 }
1271 }
Robert Greenwaltc3c5f862010-10-11 16:00:27 -07001272 // load the global proxy at startup
1273 mHandler.sendMessage(mHandler.obtainMessage(EVENT_APPLY_GLOBAL_HTTP_PROXY));
The Android Open Source Project28527d22009-03-03 19:31:44 -08001274 }
1275
1276 private void handleConnect(NetworkInfo info) {
Robert Greenwalt2034b912009-08-12 16:08:25 -07001277 int type = info.getType();
The Android Open Source Project28527d22009-03-03 19:31:44 -08001278
1279 // snapshot isFailover, because sendConnectedBroadcast() resets it
1280 boolean isFailover = info.isFailover();
Robert Greenwalt2034b912009-08-12 16:08:25 -07001281 NetworkStateTracker thisNet = mNetTrackers[type];
The Android Open Source Project28527d22009-03-03 19:31:44 -08001282
Robert Greenwalt2034b912009-08-12 16:08:25 -07001283 // if this is a default net and other default is running
1284 // kill the one not preferred
Robert Greenwalt34848c02011-03-25 13:09:25 -07001285 if (mNetConfigs[type].isDefault()) {
Robert Greenwalt2034b912009-08-12 16:08:25 -07001286 if (mActiveDefaultNetwork != -1 && mActiveDefaultNetwork != type) {
1287 if ((type != mNetworkPreference &&
Robert Greenwalt34848c02011-03-25 13:09:25 -07001288 mNetConfigs[mActiveDefaultNetwork].mPriority >
1289 mNetConfigs[type].mPriority) ||
Robert Greenwalt2034b912009-08-12 16:08:25 -07001290 mNetworkPreference == mActiveDefaultNetwork) {
1291 // don't accept this one
Wink Savillee70c6f52010-12-03 12:01:38 -08001292 if (DBG) {
1293 log("Not broadcasting CONNECT_ACTION " +
Robert Greenwalt2034b912009-08-12 16:08:25 -07001294 "to torn down network " + info.getTypeName());
Wink Savillee70c6f52010-12-03 12:01:38 -08001295 }
Robert Greenwalt2034b912009-08-12 16:08:25 -07001296 teardown(thisNet);
1297 return;
1298 } else {
1299 // tear down the other
1300 NetworkStateTracker otherNet =
1301 mNetTrackers[mActiveDefaultNetwork];
Wink Savillee70c6f52010-12-03 12:01:38 -08001302 if (DBG) {
1303 log("Policy requires " + otherNet.getNetworkInfo().getTypeName() +
Robert Greenwalt2034b912009-08-12 16:08:25 -07001304 " teardown");
Wink Savillee70c6f52010-12-03 12:01:38 -08001305 }
Robert Greenwalt2034b912009-08-12 16:08:25 -07001306 if (!teardown(otherNet)) {
Wink Savillee70c6f52010-12-03 12:01:38 -08001307 loge("Network declined teardown request");
Robert Greenwalt2034b912009-08-12 16:08:25 -07001308 return;
1309 }
Robert Greenwalt93dc1042010-06-15 12:19:37 -07001310 }
1311 }
1312 synchronized (ConnectivityService.this) {
1313 // have a new default network, release the transition wakelock in a second
1314 // if it's held. The second pause is to allow apps to reconnect over the
1315 // new network
1316 if (mNetTransitionWakeLock.isHeld()) {
1317 mHandler.sendMessageDelayed(mHandler.obtainMessage(
Robert Greenwaltccb36f92010-09-24 14:32:21 -07001318 EVENT_CLEAR_NET_TRANSITION_WAKELOCK,
Robert Greenwalt93dc1042010-06-15 12:19:37 -07001319 mNetTransitionWakeLockSerialNumber, 0),
1320 1000);
Robert Greenwalt2034b912009-08-12 16:08:25 -07001321 }
1322 }
1323 mActiveDefaultNetwork = type;
Robert Greenwalt986c7412010-09-08 15:24:47 -07001324 // this will cause us to come up initially as unconnected and switching
1325 // to connected after our normal pause unless somebody reports us as reall
1326 // disconnected
1327 mDefaultInetConditionPublished = 0;
1328 mDefaultConnectionSequence++;
1329 mInetConditionChangeInFlight = false;
1330 // Don't do this - if we never sign in stay, grey
1331 //reportNetworkCondition(mActiveDefaultNetwork, 100);
Robert Greenwalt2034b912009-08-12 16:08:25 -07001332 }
The Android Open Source Project28527d22009-03-03 19:31:44 -08001333 thisNet.setTeardownRequested(false);
Irfan Sheriff7f132d92010-06-09 15:39:36 -07001334 updateNetworkSettings(thisNet);
Robert Greenwalt3afbead2010-07-23 15:46:26 -07001335 handleConnectivityChange(type);
Robert Greenwalt2034b912009-08-12 16:08:25 -07001336 sendConnectedBroadcast(info);
The Android Open Source Project28527d22009-03-03 19:31:44 -08001337 }
1338
The Android Open Source Project28527d22009-03-03 19:31:44 -08001339 /**
Robert Greenwalt3afbead2010-07-23 15:46:26 -07001340 * After a change in the connectivity state of a network. We're mainly
1341 * concerned with making sure that the list of DNS servers is set up
1342 * according to which networks are connected, and ensuring that the
1343 * right routing table entries exist.
The Android Open Source Project28527d22009-03-03 19:31:44 -08001344 */
Robert Greenwalt3afbead2010-07-23 15:46:26 -07001345 private void handleConnectivityChange(int netType) {
The Android Open Source Project28527d22009-03-03 19:31:44 -08001346 /*
Robert Greenwalt2034b912009-08-12 16:08:25 -07001347 * If a non-default network is enabled, add the host routes that
Robert Greenwalt3afbead2010-07-23 15:46:26 -07001348 * will allow it's DNS servers to be accessed.
The Android Open Source Project28527d22009-03-03 19:31:44 -08001349 */
Robert Greenwalt3afbead2010-07-23 15:46:26 -07001350 handleDnsConfigurationChange(netType);
Robert Greenwalt2034b912009-08-12 16:08:25 -07001351
Robert Greenwalt3afbead2010-07-23 15:46:26 -07001352 if (mNetTrackers[netType].getNetworkInfo().isConnected()) {
Robert Greenwalt34848c02011-03-25 13:09:25 -07001353 if (mNetConfigs[netType].isDefault()) {
Robert Greenwaltc3c5f862010-10-11 16:00:27 -07001354 handleApplyDefaultProxy(netType);
Robert Greenwalt3afbead2010-07-23 15:46:26 -07001355 addDefaultRoute(mNetTrackers[netType]);
Robert Greenwalt2034b912009-08-12 16:08:25 -07001356 } else {
Robert Greenwalt3afbead2010-07-23 15:46:26 -07001357 addPrivateDnsRoutes(mNetTrackers[netType]);
1358 }
1359 } else {
Robert Greenwalt34848c02011-03-25 13:09:25 -07001360 if (mNetConfigs[netType].isDefault()) {
Robert Greenwalt3afbead2010-07-23 15:46:26 -07001361 removeDefaultRoute(mNetTrackers[netType]);
1362 } else {
1363 removePrivateDnsRoutes(mNetTrackers[netType]);
Robert Greenwalt2034b912009-08-12 16:08:25 -07001364 }
The Android Open Source Project28527d22009-03-03 19:31:44 -08001365 }
1366 }
1367
Irfan Sheriff7f132d92010-06-09 15:39:36 -07001368 private void addPrivateDnsRoutes(NetworkStateTracker nt) {
Irfan Sheriff7f132d92010-06-09 15:39:36 -07001369 boolean privateDnsRouteSet = nt.isPrivateDnsRouteSet();
Robert Greenwalt1f1bcfe2010-08-30 10:56:47 -07001370 LinkProperties p = nt.getLinkProperties();
Robert Greenwalta7dfbd32010-06-15 15:43:39 -07001371 if (p == null) return;
1372 String interfaceName = p.getInterfaceName();
Irfan Sheriff7f132d92010-06-09 15:39:36 -07001373
1374 if (DBG) {
Wink Savillee70c6f52010-12-03 12:01:38 -08001375 log("addPrivateDnsRoutes for " + nt +
Irfan Sheriff7f132d92010-06-09 15:39:36 -07001376 "(" + interfaceName + ") - mPrivateDnsRouteSet = " + privateDnsRouteSet);
1377 }
Irfan Sheriff7f132d92010-06-09 15:39:36 -07001378 if (interfaceName != null && !privateDnsRouteSet) {
Robert Greenwalta7dfbd32010-06-15 15:43:39 -07001379 Collection<InetAddress> dnsList = p.getDnses();
1380 for (InetAddress dns : dnsList) {
Wink Savillee70c6f52010-12-03 12:01:38 -08001381 if (DBG) log(" adding " + dns);
Robert Greenwalt7fe44cb2010-08-27 09:24:29 -07001382 NetworkUtils.addHostRoute(interfaceName, dns, null);
Irfan Sheriff7f132d92010-06-09 15:39:36 -07001383 }
1384 nt.privateDnsRouteSet(true);
1385 }
1386 }
1387
1388 private void removePrivateDnsRoutes(NetworkStateTracker nt) {
1389 // TODO - we should do this explicitly but the NetUtils api doesnt
1390 // support this yet - must remove all. No worse than before
Robert Greenwalt1f1bcfe2010-08-30 10:56:47 -07001391 LinkProperties p = nt.getLinkProperties();
Robert Greenwalta7dfbd32010-06-15 15:43:39 -07001392 if (p == null) return;
1393 String interfaceName = p.getInterfaceName();
Irfan Sheriff7f132d92010-06-09 15:39:36 -07001394 boolean privateDnsRouteSet = nt.isPrivateDnsRouteSet();
1395 if (interfaceName != null && privateDnsRouteSet) {
1396 if (DBG) {
Wink Savillee70c6f52010-12-03 12:01:38 -08001397 log("removePrivateDnsRoutes for " + nt.getNetworkInfo().getTypeName() +
Irfan Sheriff7f132d92010-06-09 15:39:36 -07001398 " (" + interfaceName + ")");
1399 }
1400 NetworkUtils.removeHostRoutes(interfaceName);
1401 nt.privateDnsRouteSet(false);
1402 }
1403 }
1404
Irfan Sheriff7f132d92010-06-09 15:39:36 -07001405
1406 private void addDefaultRoute(NetworkStateTracker nt) {
Robert Greenwalt1f1bcfe2010-08-30 10:56:47 -07001407 LinkProperties p = nt.getLinkProperties();
Robert Greenwalta7dfbd32010-06-15 15:43:39 -07001408 if (p == null) return;
1409 String interfaceName = p.getInterfaceName();
Robert Greenwalt5c733972011-02-09 13:56:06 -08001410 if (TextUtils.isEmpty(interfaceName)) return;
1411 for (InetAddress gateway : p.getGateways()) {
Irfan Sheriff7f132d92010-06-09 15:39:36 -07001412
Robert Greenwalt03d53da2011-03-22 18:47:42 -07001413 if (NetworkUtils.addHostRoute(interfaceName, gateway, null) &&
1414 NetworkUtils.addDefaultRoute(interfaceName, gateway)) {
1415 if (DBG) {
1416 NetworkInfo networkInfo = nt.getNetworkInfo();
1417 log("addDefaultRoute for " + networkInfo.getTypeName() +
1418 " (" + interfaceName + "), GatewayAddr=" + gateway.getHostAddress());
1419 }
Irfan Sheriff7f132d92010-06-09 15:39:36 -07001420 }
Irfan Sheriff7f132d92010-06-09 15:39:36 -07001421 }
1422 }
1423
1424
1425 public void removeDefaultRoute(NetworkStateTracker nt) {
Robert Greenwalt1f1bcfe2010-08-30 10:56:47 -07001426 LinkProperties p = nt.getLinkProperties();
Robert Greenwalta7dfbd32010-06-15 15:43:39 -07001427 if (p == null) return;
1428 String interfaceName = p.getInterfaceName();
Irfan Sheriff7f132d92010-06-09 15:39:36 -07001429
Robert Greenwalt3afbead2010-07-23 15:46:26 -07001430 if (interfaceName != null) {
Robert Greenwalt03d53da2011-03-22 18:47:42 -07001431 if (NetworkUtils.removeDefaultRoute(interfaceName) >= 0) {
1432 if (DBG) {
1433 NetworkInfo networkInfo = nt.getNetworkInfo();
1434 log("removeDefaultRoute for " + networkInfo.getTypeName() + " (" +
1435 interfaceName + ")");
1436 }
Irfan Sheriff7f132d92010-06-09 15:39:36 -07001437 }
Irfan Sheriff7f132d92010-06-09 15:39:36 -07001438 }
1439 }
1440
1441 /**
1442 * Reads the network specific TCP buffer sizes from SystemProperties
1443 * net.tcp.buffersize.[default|wifi|umts|edge|gprs] and set them for system
1444 * wide use
1445 */
1446 public void updateNetworkSettings(NetworkStateTracker nt) {
1447 String key = nt.getTcpBufferSizesPropName();
1448 String bufferSizes = SystemProperties.get(key);
1449
1450 if (bufferSizes.length() == 0) {
Wink Savillee70c6f52010-12-03 12:01:38 -08001451 loge(key + " not found in system properties. Using defaults");
Irfan Sheriff7f132d92010-06-09 15:39:36 -07001452
1453 // Setting to default values so we won't be stuck to previous values
1454 key = "net.tcp.buffersize.default";
1455 bufferSizes = SystemProperties.get(key);
1456 }
1457
1458 // Set values in kernel
1459 if (bufferSizes.length() != 0) {
1460 if (DBG) {
Wink Savillee70c6f52010-12-03 12:01:38 -08001461 log("Setting TCP values: [" + bufferSizes
Irfan Sheriff7f132d92010-06-09 15:39:36 -07001462 + "] which comes from [" + key + "]");
1463 }
1464 setBufferSize(bufferSizes);
1465 }
1466 }
1467
1468 /**
1469 * Writes TCP buffer sizes to /sys/kernel/ipv4/tcp_[r/w]mem_[min/def/max]
1470 * which maps to /proc/sys/net/ipv4/tcp_rmem and tcpwmem
1471 *
1472 * @param bufferSizes in the format of "readMin, readInitial, readMax,
1473 * writeMin, writeInitial, writeMax"
1474 */
1475 private void setBufferSize(String bufferSizes) {
1476 try {
1477 String[] values = bufferSizes.split(",");
1478
1479 if (values.length == 6) {
1480 final String prefix = "/sys/kernel/ipv4/tcp_";
1481 stringToFile(prefix + "rmem_min", values[0]);
1482 stringToFile(prefix + "rmem_def", values[1]);
1483 stringToFile(prefix + "rmem_max", values[2]);
1484 stringToFile(prefix + "wmem_min", values[3]);
1485 stringToFile(prefix + "wmem_def", values[4]);
1486 stringToFile(prefix + "wmem_max", values[5]);
1487 } else {
Wink Savillee70c6f52010-12-03 12:01:38 -08001488 loge("Invalid buffersize string: " + bufferSizes);
Irfan Sheriff7f132d92010-06-09 15:39:36 -07001489 }
1490 } catch (IOException e) {
Wink Savillee70c6f52010-12-03 12:01:38 -08001491 loge("Can't set tcp buffer sizes:" + e);
Irfan Sheriff7f132d92010-06-09 15:39:36 -07001492 }
1493 }
1494
1495 /**
1496 * Writes string to file. Basically same as "echo -n $string > $filename"
1497 *
1498 * @param filename
1499 * @param string
1500 * @throws IOException
1501 */
1502 private void stringToFile(String filename, String string) throws IOException {
1503 FileWriter out = new FileWriter(filename);
1504 try {
1505 out.write(string);
1506 } finally {
1507 out.close();
1508 }
1509 }
1510
1511
Robert Greenwalt2034b912009-08-12 16:08:25 -07001512 /**
1513 * Adjust the per-process dns entries (net.dns<x>.<pid>) based
1514 * on the highest priority active net which this process requested.
1515 * If there aren't any, clear it out
1516 */
1517 private void reassessPidDns(int myPid, boolean doBump)
1518 {
Wink Savillee70c6f52010-12-03 12:01:38 -08001519 if (DBG) log("reassessPidDns for pid " + myPid);
Robert Greenwalt2034b912009-08-12 16:08:25 -07001520 for(int i : mPriorityList) {
Robert Greenwalt34848c02011-03-25 13:09:25 -07001521 if (mNetConfigs[i].isDefault()) {
Robert Greenwalt2034b912009-08-12 16:08:25 -07001522 continue;
1523 }
1524 NetworkStateTracker nt = mNetTrackers[i];
Robert Greenwalt0659da32009-07-16 17:21:39 -07001525 if (nt.getNetworkInfo().isConnected() &&
1526 !nt.isTeardownRequested()) {
Robert Greenwalt1f1bcfe2010-08-30 10:56:47 -07001527 LinkProperties p = nt.getLinkProperties();
Robert Greenwalta7dfbd32010-06-15 15:43:39 -07001528 if (p == null) continue;
Robert Greenwalt2034b912009-08-12 16:08:25 -07001529 List pids = mNetRequestersPids[i];
1530 for (int j=0; j<pids.size(); j++) {
1531 Integer pid = (Integer)pids.get(j);
1532 if (pid.intValue() == myPid) {
Robert Greenwalta7dfbd32010-06-15 15:43:39 -07001533 Collection<InetAddress> dnses = p.getDnses();
1534 writePidDns(dnses, myPid);
Robert Greenwalt2034b912009-08-12 16:08:25 -07001535 if (doBump) {
1536 bumpDns();
1537 }
1538 return;
1539 }
1540 }
1541 }
1542 }
1543 // nothing found - delete
1544 for (int i = 1; ; i++) {
1545 String prop = "net.dns" + i + "." + myPid;
1546 if (SystemProperties.get(prop).length() == 0) {
1547 if (doBump) {
1548 bumpDns();
1549 }
1550 return;
1551 }
1552 SystemProperties.set(prop, "");
1553 }
1554 }
1555
Robert Greenwalt8ca88762010-12-17 15:20:36 -08001556 // return true if results in a change
1557 private boolean writePidDns(Collection <InetAddress> dnses, int pid) {
Robert Greenwalt2034b912009-08-12 16:08:25 -07001558 int j = 1;
Robert Greenwalt8ca88762010-12-17 15:20:36 -08001559 boolean changed = false;
Robert Greenwalta7dfbd32010-06-15 15:43:39 -07001560 for (InetAddress dns : dnses) {
Robert Greenwalt8ca88762010-12-17 15:20:36 -08001561 String dnsString = dns.getHostAddress();
1562 if (changed || !dnsString.equals(SystemProperties.get("net.dns" + j + "." + pid))) {
1563 changed = true;
1564 SystemProperties.set("net.dns" + j++ + "." + pid, dns.getHostAddress());
1565 }
Robert Greenwalt2034b912009-08-12 16:08:25 -07001566 }
Robert Greenwalt8ca88762010-12-17 15:20:36 -08001567 return changed;
Robert Greenwalt2034b912009-08-12 16:08:25 -07001568 }
1569
1570 private void bumpDns() {
1571 /*
1572 * Bump the property that tells the name resolver library to reread
1573 * the DNS server list from the properties.
1574 */
1575 String propVal = SystemProperties.get("net.dnschange");
1576 int n = 0;
1577 if (propVal.length() != 0) {
1578 try {
1579 n = Integer.parseInt(propVal);
1580 } catch (NumberFormatException e) {}
1581 }
1582 SystemProperties.set("net.dnschange", "" + (n+1));
Robert Greenwalt051642b2010-11-02 14:08:23 -07001583 /*
1584 * Tell the VMs to toss their DNS caches
1585 */
1586 Intent intent = new Intent(Intent.ACTION_CLEAR_DNS_CACHE);
1587 intent.addFlags(Intent.FLAG_RECEIVER_REPLACE_PENDING);
Stan Chesnuttf444f502011-01-05 17:14:03 -08001588 /*
1589 * Connectivity events can happen before boot has completed ...
1590 */
1591 intent.addFlags(Intent.FLAG_RECEIVER_REGISTERED_ONLY_BEFORE_BOOT);
Robert Greenwalt051642b2010-11-02 14:08:23 -07001592 mContext.sendBroadcast(intent);
Robert Greenwalt2034b912009-08-12 16:08:25 -07001593 }
1594
Robert Greenwalt3afbead2010-07-23 15:46:26 -07001595 private void handleDnsConfigurationChange(int netType) {
Robert Greenwalt2034b912009-08-12 16:08:25 -07001596 // add default net's dns entries
Robert Greenwalt3afbead2010-07-23 15:46:26 -07001597 NetworkStateTracker nt = mNetTrackers[netType];
1598 if (nt != null && nt.getNetworkInfo().isConnected() && !nt.isTeardownRequested()) {
Robert Greenwalt1f1bcfe2010-08-30 10:56:47 -07001599 LinkProperties p = nt.getLinkProperties();
Robert Greenwalt3afbead2010-07-23 15:46:26 -07001600 if (p == null) return;
1601 Collection<InetAddress> dnses = p.getDnses();
Robert Greenwalt8ca88762010-12-17 15:20:36 -08001602 boolean changed = false;
Robert Greenwalt34848c02011-03-25 13:09:25 -07001603 if (mNetConfigs[netType].isDefault()) {
Robert Greenwalt3afbead2010-07-23 15:46:26 -07001604 int j = 1;
Robert Greenwalt94daa182010-09-01 11:34:05 -07001605 if (dnses.size() == 0 && mDefaultDns != null) {
Robert Greenwalt8ca88762010-12-17 15:20:36 -08001606 String dnsString = mDefaultDns.getHostAddress();
1607 if (!dnsString.equals(SystemProperties.get("net.dns1"))) {
1608 if (DBG) {
1609 log("no dns provided - using " + dnsString);
1610 }
1611 changed = true;
1612 SystemProperties.set("net.dns1", dnsString);
Robert Greenwalt2034b912009-08-12 16:08:25 -07001613 }
Robert Greenwalt94daa182010-09-01 11:34:05 -07001614 j++;
1615 } else {
1616 for (InetAddress dns : dnses) {
Robert Greenwalt8ca88762010-12-17 15:20:36 -08001617 String dnsString = dns.getHostAddress();
1618 if (!changed && dnsString.equals(SystemProperties.get("net.dns" + j))) {
1619 j++;
1620 continue;
1621 }
Robert Greenwalt94daa182010-09-01 11:34:05 -07001622 if (DBG) {
Wink Savillee70c6f52010-12-03 12:01:38 -08001623 log("adding dns " + dns + " for " +
Robert Greenwalt94daa182010-09-01 11:34:05 -07001624 nt.getNetworkInfo().getTypeName());
1625 }
Robert Greenwalt8ca88762010-12-17 15:20:36 -08001626 changed = true;
1627 SystemProperties.set("net.dns" + j++, dnsString);
Robert Greenwalt94daa182010-09-01 11:34:05 -07001628 }
Robert Greenwalt3afbead2010-07-23 15:46:26 -07001629 }
1630 for (int k=j ; k<mNumDnsEntries; k++) {
Robert Greenwalt8ca88762010-12-17 15:20:36 -08001631 if (changed || !TextUtils.isEmpty(SystemProperties.get("net.dns" + k))) {
1632 if (DBG) log("erasing net.dns" + k);
1633 changed = true;
1634 SystemProperties.set("net.dns" + k, "");
1635 }
Robert Greenwalt3afbead2010-07-23 15:46:26 -07001636 }
1637 mNumDnsEntries = j;
1638 } else {
1639 // set per-pid dns for attached secondary nets
1640 List pids = mNetRequestersPids[netType];
1641 for (int y=0; y< pids.size(); y++) {
1642 Integer pid = (Integer)pids.get(y);
Robert Greenwalt8ca88762010-12-17 15:20:36 -08001643 changed = writePidDns(dnses, pid.intValue());
The Android Open Source Project28527d22009-03-03 19:31:44 -08001644 }
1645 }
Robert Greenwalt8ca88762010-12-17 15:20:36 -08001646 if (changed) bumpDns();
The Android Open Source Project28527d22009-03-03 19:31:44 -08001647 }
Robert Greenwalt2034b912009-08-12 16:08:25 -07001648 }
1649
1650 private int getRestoreDefaultNetworkDelay() {
1651 String restoreDefaultNetworkDelayStr = SystemProperties.get(
1652 NETWORK_RESTORE_DELAY_PROP_NAME);
1653 if(restoreDefaultNetworkDelayStr != null &&
1654 restoreDefaultNetworkDelayStr.length() != 0) {
1655 try {
1656 return Integer.valueOf(restoreDefaultNetworkDelayStr);
1657 } catch (NumberFormatException e) {
1658 }
The Android Open Source Project28527d22009-03-03 19:31:44 -08001659 }
Robert Greenwalt2034b912009-08-12 16:08:25 -07001660 return RESTORE_DEFAULT_NETWORK_DELAY;
The Android Open Source Project28527d22009-03-03 19:31:44 -08001661 }
1662
1663 @Override
1664 protected void dump(FileDescriptor fd, PrintWriter pw, String[] args) {
Robert Greenwalt0659da32009-07-16 17:21:39 -07001665 if (mContext.checkCallingOrSelfPermission(
1666 android.Manifest.permission.DUMP)
The Android Open Source Project28527d22009-03-03 19:31:44 -08001667 != PackageManager.PERMISSION_GRANTED) {
Robert Greenwalt0659da32009-07-16 17:21:39 -07001668 pw.println("Permission Denial: can't dump ConnectivityService " +
1669 "from from pid=" + Binder.getCallingPid() + ", uid=" +
1670 Binder.getCallingUid());
The Android Open Source Project28527d22009-03-03 19:31:44 -08001671 return;
1672 }
The Android Open Source Project28527d22009-03-03 19:31:44 -08001673 pw.println();
1674 for (NetworkStateTracker nst : mNetTrackers) {
Robert Greenwalt3eeb6032009-12-21 18:24:07 -08001675 if (nst != null) {
1676 if (nst.getNetworkInfo().isConnected()) {
1677 pw.println("Active network: " + nst.getNetworkInfo().
1678 getTypeName());
1679 }
1680 pw.println(nst.getNetworkInfo());
1681 pw.println(nst);
1682 pw.println();
Robert Greenwalt2034b912009-08-12 16:08:25 -07001683 }
The Android Open Source Project28527d22009-03-03 19:31:44 -08001684 }
Robert Greenwalt3eeb6032009-12-21 18:24:07 -08001685
1686 pw.println("Network Requester Pids:");
1687 for (int net : mPriorityList) {
1688 String pidString = net + ": ";
1689 for (Object pid : mNetRequestersPids[net]) {
1690 pidString = pidString + pid.toString() + ", ";
1691 }
1692 pw.println(pidString);
1693 }
1694 pw.println();
1695
1696 pw.println("FeatureUsers:");
1697 for (Object requester : mFeatureUsers) {
1698 pw.println(requester.toString());
1699 }
1700 pw.println();
Robert Greenwalt8e87f122010-02-11 18:18:40 -08001701
Robert Greenwalt93dc1042010-06-15 12:19:37 -07001702 synchronized (this) {
1703 pw.println("NetworkTranstionWakeLock is currently " +
1704 (mNetTransitionWakeLock.isHeld() ? "" : "not ") + "held.");
1705 pw.println("It was last requested for "+mNetTransitionWakeLockCausedBy);
1706 }
1707 pw.println();
1708
Robert Greenwalt8e87f122010-02-11 18:18:40 -08001709 mTethering.dump(fd, pw, args);
Robert Greenwalt0e80be12010-09-20 14:35:25 -07001710
1711 if (mInetLog != null) {
1712 pw.println();
1713 pw.println("Inet condition reports:");
1714 for(int i = 0; i < mInetLog.size(); i++) {
1715 pw.println(mInetLog.get(i));
1716 }
1717 }
The Android Open Source Project28527d22009-03-03 19:31:44 -08001718 }
1719
Robert Greenwalt2034b912009-08-12 16:08:25 -07001720 // must be stateless - things change under us.
The Android Open Source Project28527d22009-03-03 19:31:44 -08001721 private class MyHandler extends Handler {
Wink Saville775aad62010-09-02 19:23:52 -07001722 public MyHandler(Looper looper) {
1723 super(looper);
1724 }
1725
The Android Open Source Project28527d22009-03-03 19:31:44 -08001726 @Override
1727 public void handleMessage(Message msg) {
1728 NetworkInfo info;
1729 switch (msg.what) {
1730 case NetworkStateTracker.EVENT_STATE_CHANGED:
1731 info = (NetworkInfo) msg.obj;
Robert Greenwalt12c44552009-12-07 11:33:18 -08001732 int type = info.getType();
1733 NetworkInfo.State state = info.getState();
Robert Greenwalt12c44552009-12-07 11:33:18 -08001734
Wink Savillee70c6f52010-12-03 12:01:38 -08001735 if (DBG) log("ConnectivityChange for " +
Robert Greenwalt0659da32009-07-16 17:21:39 -07001736 info.getTypeName() + ": " +
Robert Greenwalt12c44552009-12-07 11:33:18 -08001737 state + "/" + info.getDetailedState());
The Android Open Source Project28527d22009-03-03 19:31:44 -08001738
1739 // Connectivity state changed:
1740 // [31-13] Reserved for future use
Robert Greenwalt0659da32009-07-16 17:21:39 -07001741 // [12-9] Network subtype (for mobile network, as defined
1742 // by TelephonyManager)
1743 // [8-3] Detailed state ordinal (as defined by
1744 // NetworkInfo.DetailedState)
The Android Open Source Project28527d22009-03-03 19:31:44 -08001745 // [2-0] Network type (as defined by ConnectivityManager)
1746 int eventLogParam = (info.getType() & 0x7) |
1747 ((info.getDetailedState().ordinal() & 0x3f) << 3) |
1748 (info.getSubtype() << 9);
Doug Zongker2fc96232009-12-04 10:31:43 -08001749 EventLog.writeEvent(EventLogTags.CONNECTIVITY_STATE_CHANGED,
Robert Greenwalt0659da32009-07-16 17:21:39 -07001750 eventLogParam);
1751
1752 if (info.getDetailedState() ==
1753 NetworkInfo.DetailedState.FAILED) {
The Android Open Source Project28527d22009-03-03 19:31:44 -08001754 handleConnectionFailure(info);
Robert Greenwalt12c44552009-12-07 11:33:18 -08001755 } else if (state == NetworkInfo.State.DISCONNECTED) {
The Android Open Source Project28527d22009-03-03 19:31:44 -08001756 handleDisconnect(info);
Robert Greenwalt12c44552009-12-07 11:33:18 -08001757 } else if (state == NetworkInfo.State.SUSPENDED) {
The Android Open Source Project28527d22009-03-03 19:31:44 -08001758 // TODO: need to think this over.
Robert Greenwalt0659da32009-07-16 17:21:39 -07001759 // the logic here is, handle SUSPENDED the same as
1760 // DISCONNECTED. The only difference being we are
1761 // broadcasting an intent with NetworkInfo that's
1762 // suspended. This allows the applications an
1763 // opportunity to handle DISCONNECTED and SUSPENDED
1764 // differently, or not.
The Android Open Source Project28527d22009-03-03 19:31:44 -08001765 handleDisconnect(info);
Robert Greenwalt12c44552009-12-07 11:33:18 -08001766 } else if (state == NetworkInfo.State.CONNECTED) {
The Android Open Source Project28527d22009-03-03 19:31:44 -08001767 handleConnect(info);
1768 }
The Android Open Source Project28527d22009-03-03 19:31:44 -08001769 break;
The Android Open Source Project28527d22009-03-03 19:31:44 -08001770 case NetworkStateTracker.EVENT_CONFIGURATION_CHANGED:
Robert Greenwalt3afbead2010-07-23 15:46:26 -07001771 info = (NetworkInfo) msg.obj;
Robert Greenwalt34848c02011-03-25 13:09:25 -07001772 handleConnectivityChange(info.getType());
The Android Open Source Project28527d22009-03-03 19:31:44 -08001773 break;
Robert Greenwaltccb36f92010-09-24 14:32:21 -07001774 case EVENT_CLEAR_NET_TRANSITION_WAKELOCK:
Robert Greenwalt93dc1042010-06-15 12:19:37 -07001775 String causedBy = null;
1776 synchronized (ConnectivityService.this) {
1777 if (msg.arg1 == mNetTransitionWakeLockSerialNumber &&
1778 mNetTransitionWakeLock.isHeld()) {
1779 mNetTransitionWakeLock.release();
1780 causedBy = mNetTransitionWakeLockCausedBy;
1781 }
1782 }
1783 if (causedBy != null) {
Wink Savillee70c6f52010-12-03 12:01:38 -08001784 log("NetTransition Wakelock for " + causedBy + " released by timeout");
Robert Greenwalt93dc1042010-06-15 12:19:37 -07001785 }
Robert Greenwaltcf1a56c2010-09-09 14:05:10 -07001786 break;
Robert Greenwalt6a2db8a2010-09-23 10:05:56 -07001787 case EVENT_RESTORE_DEFAULT_NETWORK:
Robert Greenwaltaffc3a12009-09-27 17:27:04 -07001788 FeatureUser u = (FeatureUser)msg.obj;
1789 u.expire();
Robert Greenwalt986c7412010-09-08 15:24:47 -07001790 break;
Robert Greenwalt6a2db8a2010-09-23 10:05:56 -07001791 case EVENT_INET_CONDITION_CHANGE:
1792 {
1793 int netType = msg.arg1;
1794 int condition = msg.arg2;
1795 handleInetConditionChange(netType, condition);
Robert Greenwalt93dc1042010-06-15 12:19:37 -07001796 break;
Robert Greenwalt6a2db8a2010-09-23 10:05:56 -07001797 }
1798 case EVENT_INET_CONDITION_HOLD_END:
1799 {
1800 int netType = msg.arg1;
1801 int sequence = msg.arg2;
1802 handleInetConditionHoldEnd(netType, sequence);
Robert Greenwalt986c7412010-09-08 15:24:47 -07001803 break;
Robert Greenwalt6a2db8a2010-09-23 10:05:56 -07001804 }
1805 case EVENT_SET_NETWORK_PREFERENCE:
1806 {
1807 int preference = msg.arg1;
1808 handleSetNetworkPreference(preference);
1809 break;
1810 }
1811 case EVENT_SET_BACKGROUND_DATA:
1812 {
1813 boolean enabled = (msg.arg1 == ENABLED);
1814 handleSetBackgroundData(enabled);
1815 break;
1816 }
1817 case EVENT_SET_MOBILE_DATA:
1818 {
1819 boolean enabled = (msg.arg1 == ENABLED);
1820 handleSetMobileData(enabled);
1821 break;
1822 }
Robert Greenwaltc3c5f862010-10-11 16:00:27 -07001823 case EVENT_APPLY_GLOBAL_HTTP_PROXY:
1824 {
1825 handleDeprecatedGlobalHttpProxy();
Robert Greenwalt34848c02011-03-25 13:09:25 -07001826 break;
1827 }
1828 case EVENT_SET_DEPENDENCY_MET:
1829 {
1830 boolean met = (msg.arg1 == ENABLED);
1831 handleSetDependencyMet(msg.arg2, met);
1832 break;
Robert Greenwaltc3c5f862010-10-11 16:00:27 -07001833 }
The Android Open Source Project28527d22009-03-03 19:31:44 -08001834 }
1835 }
1836 }
Robert Greenwalt0c4828c2010-01-26 11:40:34 -08001837
1838 // javadoc from interface
Robert Greenwalt4283ded2010-03-02 17:25:02 -08001839 public int tether(String iface) {
Robert Greenwalt0c4828c2010-01-26 11:40:34 -08001840 enforceTetherChangePermission();
Robert Greenwalt4283ded2010-03-02 17:25:02 -08001841
1842 if (isTetheringSupported()) {
1843 return mTethering.tether(iface);
1844 } else {
1845 return ConnectivityManager.TETHER_ERROR_UNSUPPORTED;
1846 }
Robert Greenwalt0c4828c2010-01-26 11:40:34 -08001847 }
1848
1849 // javadoc from interface
Robert Greenwalt4283ded2010-03-02 17:25:02 -08001850 public int untether(String iface) {
Robert Greenwalt0c4828c2010-01-26 11:40:34 -08001851 enforceTetherChangePermission();
Robert Greenwalt4283ded2010-03-02 17:25:02 -08001852
1853 if (isTetheringSupported()) {
1854 return mTethering.untether(iface);
1855 } else {
1856 return ConnectivityManager.TETHER_ERROR_UNSUPPORTED;
1857 }
1858 }
1859
1860 // javadoc from interface
1861 public int getLastTetherError(String iface) {
1862 enforceTetherAccessPermission();
1863
1864 if (isTetheringSupported()) {
1865 return mTethering.getLastTetherError(iface);
1866 } else {
1867 return ConnectivityManager.TETHER_ERROR_UNSUPPORTED;
1868 }
Robert Greenwalt8e87f122010-02-11 18:18:40 -08001869 }
1870
1871 // TODO - proper iface API for selection by property, inspection, etc
1872 public String[] getTetherableUsbRegexs() {
1873 enforceTetherAccessPermission();
1874 if (isTetheringSupported()) {
1875 return mTethering.getTetherableUsbRegexs();
1876 } else {
1877 return new String[0];
1878 }
1879 }
1880
1881 public String[] getTetherableWifiRegexs() {
1882 enforceTetherAccessPermission();
1883 if (isTetheringSupported()) {
1884 return mTethering.getTetherableWifiRegexs();
1885 } else {
1886 return new String[0];
1887 }
Robert Greenwalt0c4828c2010-01-26 11:40:34 -08001888 }
1889
Danica Chang96567052010-08-11 14:54:43 -07001890 public String[] getTetherableBluetoothRegexs() {
1891 enforceTetherAccessPermission();
1892 if (isTetheringSupported()) {
1893 return mTethering.getTetherableBluetoothRegexs();
1894 } else {
1895 return new String[0];
1896 }
1897 }
1898
Robert Greenwalt0c4828c2010-01-26 11:40:34 -08001899 // TODO - move iface listing, queries, etc to new module
1900 // javadoc from interface
1901 public String[] getTetherableIfaces() {
Robert Greenwalt8e87f122010-02-11 18:18:40 -08001902 enforceTetherAccessPermission();
Robert Greenwalt0c4828c2010-01-26 11:40:34 -08001903 return mTethering.getTetherableIfaces();
1904 }
1905
1906 public String[] getTetheredIfaces() {
Robert Greenwalt8e87f122010-02-11 18:18:40 -08001907 enforceTetherAccessPermission();
Robert Greenwalt0c4828c2010-01-26 11:40:34 -08001908 return mTethering.getTetheredIfaces();
1909 }
Robert Greenwalt8e87f122010-02-11 18:18:40 -08001910
Robert Greenwalt4283ded2010-03-02 17:25:02 -08001911 public String[] getTetheringErroredIfaces() {
1912 enforceTetherAccessPermission();
1913 return mTethering.getErroredIfaces();
1914 }
1915
Robert Greenwalt8e87f122010-02-11 18:18:40 -08001916 // if ro.tether.denied = true we default to no tethering
1917 // gservices could set the secure setting to 1 though to enable it on a build where it
1918 // had previously been turned off.
1919 public boolean isTetheringSupported() {
1920 enforceTetherAccessPermission();
1921 int defaultVal = (SystemProperties.get("ro.tether.denied").equals("true") ? 0 : 1);
Robert Greenwaltf1b66e12010-02-25 12:29:30 -08001922 boolean tetherEnabledInSettings = (Settings.Secure.getInt(mContext.getContentResolver(),
1923 Settings.Secure.TETHER_SUPPORTED, defaultVal) != 0);
1924 return tetherEnabledInSettings && mTetheringConfigValid;
Robert Greenwalt8e87f122010-02-11 18:18:40 -08001925 }
Robert Greenwalt93dc1042010-06-15 12:19:37 -07001926
1927 // An API NetworkStateTrackers can call when they lose their network.
1928 // This will automatically be cleared after X seconds or a network becomes CONNECTED,
1929 // whichever happens first. The timer is started by the first caller and not
1930 // restarted by subsequent callers.
1931 public void requestNetworkTransitionWakelock(String forWhom) {
1932 enforceConnectivityInternalPermission();
1933 synchronized (this) {
1934 if (mNetTransitionWakeLock.isHeld()) return;
1935 mNetTransitionWakeLockSerialNumber++;
1936 mNetTransitionWakeLock.acquire();
1937 mNetTransitionWakeLockCausedBy = forWhom;
1938 }
1939 mHandler.sendMessageDelayed(mHandler.obtainMessage(
Robert Greenwaltccb36f92010-09-24 14:32:21 -07001940 EVENT_CLEAR_NET_TRANSITION_WAKELOCK,
Robert Greenwalt93dc1042010-06-15 12:19:37 -07001941 mNetTransitionWakeLockSerialNumber, 0),
1942 mNetTransitionWakeLockTimeout);
1943 return;
1944 }
Robert Greenwalt24118e82010-09-09 13:15:32 -07001945
Robert Greenwalt986c7412010-09-08 15:24:47 -07001946 // 100 percent is full good, 0 is full bad.
1947 public void reportInetCondition(int networkType, int percentage) {
Wink Savillee70c6f52010-12-03 12:01:38 -08001948 if (DBG) log("reportNetworkCondition(" + networkType + ", " + percentage + ")");
Robert Greenwalt986c7412010-09-08 15:24:47 -07001949 mContext.enforceCallingOrSelfPermission(
1950 android.Manifest.permission.STATUS_BAR,
1951 "ConnectivityService");
1952
Robert Greenwalt0e80be12010-09-20 14:35:25 -07001953 if (DBG) {
1954 int pid = getCallingPid();
1955 int uid = getCallingUid();
1956 String s = pid + "(" + uid + ") reports inet is " +
1957 (percentage > 50 ? "connected" : "disconnected") + " (" + percentage + ") on " +
1958 "network Type " + networkType + " at " + GregorianCalendar.getInstance().getTime();
1959 mInetLog.add(s);
1960 while(mInetLog.size() > INET_CONDITION_LOG_MAX_SIZE) {
1961 mInetLog.remove(0);
1962 }
1963 }
Robert Greenwalt986c7412010-09-08 15:24:47 -07001964 mHandler.sendMessage(mHandler.obtainMessage(
Robert Greenwalt6a2db8a2010-09-23 10:05:56 -07001965 EVENT_INET_CONDITION_CHANGE, networkType, percentage));
1966 }
1967
1968 private void handleInetConditionChange(int netType, int condition) {
1969 if (DBG) {
Wink Savillee70c6f52010-12-03 12:01:38 -08001970 log("Inet connectivity change, net=" +
Robert Greenwalt6a2db8a2010-09-23 10:05:56 -07001971 netType + ", condition=" + condition +
1972 ",mActiveDefaultNetwork=" + mActiveDefaultNetwork);
1973 }
1974 if (mActiveDefaultNetwork == -1) {
Wink Savillee70c6f52010-12-03 12:01:38 -08001975 if (DBG) log("no active default network - aborting");
Robert Greenwalt6a2db8a2010-09-23 10:05:56 -07001976 return;
1977 }
1978 if (mActiveDefaultNetwork != netType) {
Wink Savillee70c6f52010-12-03 12:01:38 -08001979 if (DBG) log("given net not default - aborting");
Robert Greenwalt6a2db8a2010-09-23 10:05:56 -07001980 return;
1981 }
1982 mDefaultInetCondition = condition;
1983 int delay;
1984 if (mInetConditionChangeInFlight == false) {
Wink Savillee70c6f52010-12-03 12:01:38 -08001985 if (DBG) log("starting a change hold");
Robert Greenwalt6a2db8a2010-09-23 10:05:56 -07001986 // setup a new hold to debounce this
1987 if (mDefaultInetCondition > 50) {
1988 delay = Settings.Secure.getInt(mContext.getContentResolver(),
1989 Settings.Secure.INET_CONDITION_DEBOUNCE_UP_DELAY, 500);
1990 } else {
1991 delay = Settings.Secure.getInt(mContext.getContentResolver(),
1992 Settings.Secure.INET_CONDITION_DEBOUNCE_DOWN_DELAY, 3000);
1993 }
1994 mInetConditionChangeInFlight = true;
1995 mHandler.sendMessageDelayed(mHandler.obtainMessage(EVENT_INET_CONDITION_HOLD_END,
1996 mActiveDefaultNetwork, mDefaultConnectionSequence), delay);
1997 } else {
1998 // we've set the new condition, when this hold ends that will get
1999 // picked up
Wink Savillee70c6f52010-12-03 12:01:38 -08002000 if (DBG) log("currently in hold - not setting new end evt");
Robert Greenwalt6a2db8a2010-09-23 10:05:56 -07002001 }
2002 }
2003
2004 private void handleInetConditionHoldEnd(int netType, int sequence) {
2005 if (DBG) {
Wink Savillee70c6f52010-12-03 12:01:38 -08002006 log("Inet hold end, net=" + netType +
Robert Greenwalt6a2db8a2010-09-23 10:05:56 -07002007 ", condition =" + mDefaultInetCondition +
2008 ", published condition =" + mDefaultInetConditionPublished);
2009 }
2010 mInetConditionChangeInFlight = false;
2011
2012 if (mActiveDefaultNetwork == -1) {
Wink Savillee70c6f52010-12-03 12:01:38 -08002013 if (DBG) log("no active default network - aborting");
Robert Greenwalt6a2db8a2010-09-23 10:05:56 -07002014 return;
2015 }
2016 if (mDefaultConnectionSequence != sequence) {
Wink Savillee70c6f52010-12-03 12:01:38 -08002017 if (DBG) log("event hold for obsolete network - aborting");
Robert Greenwalt6a2db8a2010-09-23 10:05:56 -07002018 return;
2019 }
2020 if (mDefaultInetConditionPublished == mDefaultInetCondition) {
Wink Savillee70c6f52010-12-03 12:01:38 -08002021 if (DBG) log("no change in condition - aborting");
Robert Greenwalt6a2db8a2010-09-23 10:05:56 -07002022 return;
2023 }
2024 NetworkInfo networkInfo = mNetTrackers[mActiveDefaultNetwork].getNetworkInfo();
2025 if (networkInfo.isConnected() == false) {
Wink Savillee70c6f52010-12-03 12:01:38 -08002026 if (DBG) log("default network not connected - aborting");
Robert Greenwalt6a2db8a2010-09-23 10:05:56 -07002027 return;
2028 }
2029 mDefaultInetConditionPublished = mDefaultInetCondition;
2030 sendInetConditionBroadcast(networkInfo);
2031 return;
Robert Greenwalt986c7412010-09-08 15:24:47 -07002032 }
Robert Greenwaltc3c5f862010-10-11 16:00:27 -07002033
2034 public synchronized ProxyProperties getProxy() {
2035 if (mGlobalProxy != null) return mGlobalProxy;
2036 if (mDefaultProxy != null) return mDefaultProxy;
2037 return null;
2038 }
2039
2040 public void setGlobalProxy(ProxyProperties proxyProperties) {
2041 enforceChangePermission();
2042 synchronized (mGlobalProxyLock) {
2043 if (proxyProperties == mGlobalProxy) return;
2044 if (proxyProperties != null && proxyProperties.equals(mGlobalProxy)) return;
2045 if (mGlobalProxy != null && mGlobalProxy.equals(proxyProperties)) return;
2046
2047 String host = "";
2048 int port = 0;
2049 String exclList = "";
2050 if (proxyProperties != null && !TextUtils.isEmpty(proxyProperties.getHost())) {
2051 mGlobalProxy = new ProxyProperties(proxyProperties);
2052 host = mGlobalProxy.getHost();
2053 port = mGlobalProxy.getPort();
2054 exclList = mGlobalProxy.getExclusionList();
2055 } else {
2056 mGlobalProxy = null;
2057 }
2058 ContentResolver res = mContext.getContentResolver();
2059 Settings.Secure.putString(res, Settings.Secure.GLOBAL_HTTP_PROXY_HOST, host);
2060 Settings.Secure.putInt(res, Settings.Secure.GLOBAL_HTTP_PROXY_PORT, port);
Robert Greenwalt6f7c6092010-12-02 11:31:00 -08002061 Settings.Secure.putString(res, Settings.Secure.GLOBAL_HTTP_PROXY_EXCLUSION_LIST,
Robert Greenwaltc3c5f862010-10-11 16:00:27 -07002062 exclList);
2063 }
2064
2065 if (mGlobalProxy == null) {
2066 proxyProperties = mDefaultProxy;
2067 }
2068 sendProxyBroadcast(proxyProperties);
2069 }
2070
Robert Greenwalt6f7c6092010-12-02 11:31:00 -08002071 private void loadGlobalProxy() {
2072 ContentResolver res = mContext.getContentResolver();
2073 String host = Settings.Secure.getString(res, Settings.Secure.GLOBAL_HTTP_PROXY_HOST);
2074 int port = Settings.Secure.getInt(res, Settings.Secure.GLOBAL_HTTP_PROXY_PORT, 0);
2075 String exclList = Settings.Secure.getString(res,
2076 Settings.Secure.GLOBAL_HTTP_PROXY_EXCLUSION_LIST);
2077 if (!TextUtils.isEmpty(host)) {
2078 ProxyProperties proxyProperties = new ProxyProperties(host, port, exclList);
2079 synchronized (mGlobalProxyLock) {
2080 mGlobalProxy = proxyProperties;
2081 }
2082 }
2083 }
2084
Robert Greenwaltc3c5f862010-10-11 16:00:27 -07002085 public ProxyProperties getGlobalProxy() {
2086 synchronized (mGlobalProxyLock) {
2087 return mGlobalProxy;
2088 }
2089 }
2090
2091 private void handleApplyDefaultProxy(int type) {
2092 // check if new default - push it out to all VM if so
2093 ProxyProperties proxy = mNetTrackers[type].getLinkProperties().getHttpProxy();
2094 synchronized (this) {
2095 if (mDefaultProxy != null && mDefaultProxy.equals(proxy)) return;
2096 if (mDefaultProxy == proxy) return;
2097 if (!TextUtils.isEmpty(proxy.getHost())) {
2098 mDefaultProxy = proxy;
2099 } else {
2100 mDefaultProxy = null;
2101 }
2102 }
Wink Savillee70c6f52010-12-03 12:01:38 -08002103 if (DBG) log("changing default proxy to " + proxy);
Robert Greenwaltc3c5f862010-10-11 16:00:27 -07002104 if ((proxy == null && mGlobalProxy == null) || proxy.equals(mGlobalProxy)) return;
2105 if (mGlobalProxy != null) return;
2106 sendProxyBroadcast(proxy);
2107 }
2108
2109 private void handleDeprecatedGlobalHttpProxy() {
2110 String proxy = Settings.Secure.getString(mContext.getContentResolver(),
2111 Settings.Secure.HTTP_PROXY);
2112 if (!TextUtils.isEmpty(proxy)) {
2113 String data[] = proxy.split(":");
2114 String proxyHost = data[0];
2115 int proxyPort = 8080;
2116 if (data.length > 1) {
2117 try {
2118 proxyPort = Integer.parseInt(data[1]);
2119 } catch (NumberFormatException e) {
2120 return;
2121 }
2122 }
2123 ProxyProperties p = new ProxyProperties(data[0], proxyPort, "");
2124 setGlobalProxy(p);
2125 }
2126 }
2127
2128 private void sendProxyBroadcast(ProxyProperties proxy) {
Robert Greenwalt611291c2010-12-23 15:51:10 -08002129 if (proxy == null) proxy = new ProxyProperties("", 0, "");
Wink Savillee70c6f52010-12-03 12:01:38 -08002130 log("sending Proxy Broadcast for " + proxy);
Robert Greenwaltc3c5f862010-10-11 16:00:27 -07002131 Intent intent = new Intent(Proxy.PROXY_CHANGE_ACTION);
Stan Chesnutt1f2a2ac2011-01-06 11:00:19 -08002132 intent.addFlags(Intent.FLAG_RECEIVER_REPLACE_PENDING |
2133 Intent.FLAG_RECEIVER_REGISTERED_ONLY_BEFORE_BOOT);
Robert Greenwaltc3c5f862010-10-11 16:00:27 -07002134 intent.putExtra(Proxy.EXTRA_PROXY_INFO, proxy);
Robert Greenwaltd93dc8f2010-12-06 11:29:17 -08002135 mContext.sendStickyBroadcast(intent);
Robert Greenwaltc3c5f862010-10-11 16:00:27 -07002136 }
2137
2138 private static class SettingsObserver extends ContentObserver {
2139 private int mWhat;
2140 private Handler mHandler;
2141 SettingsObserver(Handler handler, int what) {
2142 super(handler);
2143 mHandler = handler;
2144 mWhat = what;
2145 }
2146
2147 void observe(Context context) {
2148 ContentResolver resolver = context.getContentResolver();
2149 resolver.registerContentObserver(Settings.Secure.getUriFor(
2150 Settings.Secure.HTTP_PROXY), false, this);
2151 }
2152
2153 @Override
2154 public void onChange(boolean selfChange) {
2155 mHandler.obtainMessage(mWhat).sendToTarget();
2156 }
2157 }
Wink Savillee70c6f52010-12-03 12:01:38 -08002158
2159 private void log(String s) {
2160 Slog.d(TAG, s);
2161 }
2162
2163 private void loge(String s) {
2164 Slog.e(TAG, s);
2165 }
Wink Savilleb7c92c72011-03-12 14:52:01 -08002166 int convertFeatureToNetworkType(String feature){
2167 int networkType = -1;
2168 if (TextUtils.equals(feature, Phone.FEATURE_ENABLE_MMS)) {
2169 networkType = ConnectivityManager.TYPE_MOBILE_MMS;
2170 } else if (TextUtils.equals(feature, Phone.FEATURE_ENABLE_SUPL)) {
2171 networkType = ConnectivityManager.TYPE_MOBILE_SUPL;
2172 } else if (TextUtils.equals(feature, Phone.FEATURE_ENABLE_DUN) ||
2173 TextUtils.equals(feature, Phone.FEATURE_ENABLE_DUN_ALWAYS)) {
2174 networkType = ConnectivityManager.TYPE_MOBILE_DUN;
2175 } else if (TextUtils.equals(feature, Phone.FEATURE_ENABLE_HIPRI)) {
2176 networkType = ConnectivityManager.TYPE_MOBILE_HIPRI;
2177 } else if (TextUtils.equals(feature, Phone.FEATURE_ENABLE_FOTA)) {
2178 networkType = ConnectivityManager.TYPE_MOBILE_FOTA;
2179 } else if (TextUtils.equals(feature, Phone.FEATURE_ENABLE_IMS)) {
2180 networkType = ConnectivityManager.TYPE_MOBILE_IMS;
2181 } else if (TextUtils.equals(feature, Phone.FEATURE_ENABLE_CBS)) {
2182 networkType = ConnectivityManager.TYPE_MOBILE_CBS;
2183 }
2184 return networkType;
2185 }
The Android Open Source Project28527d22009-03-03 19:31:44 -08002186}