blob: 78fdf7f6a965dbce3bc1bdacd317bde3c3dfd4cf [file] [log] [blame]
The Android Open Source Project28527d22009-03-03 19:31:44 -08001/*
2 * Copyright (C) 2008 The Android Open Source Project
3 *
4 * Licensed under the Apache License, Version 2.0 (the "License");
5 * you may not use this file except in compliance with the License.
6 * You may obtain a copy of the License at
7 *
8 * http://www.apache.org/licenses/LICENSE-2.0
9 *
10 * Unless required by applicable law or agreed to in writing, software
11 * distributed under the License is distributed on an "AS IS" BASIS,
12 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13 * See the License for the specific language governing permissions and
14 * limitations under the License.
15 */
16
17package com.android.server;
18
Jaikumar Ganesh0db51a02010-12-21 22:31:44 -080019import android.bluetooth.BluetoothTetheringDataTracker;
The Android Open Source Project28527d22009-03-03 19:31:44 -080020import android.content.ContentResolver;
21import android.content.Context;
22import android.content.Intent;
23import android.content.pm.PackageManager;
Robert Greenwaltc3c5f862010-10-11 16:00:27 -070024import android.database.ContentObserver;
The Android Open Source Project28527d22009-03-03 19:31:44 -080025import android.net.ConnectivityManager;
Robert Greenwalteb123ac2010-12-06 13:56:24 -080026import android.net.DummyDataStateTracker;
Benoit Goby6cec7f32010-12-22 14:29:40 -080027import android.net.EthernetDataTracker;
The Android Open Source Project28527d22009-03-03 19:31:44 -080028import android.net.IConnectivityManager;
Jaikumar Ganesh0db51a02010-12-21 22:31:44 -080029import android.net.LinkProperties;
The Android Open Source Project28527d22009-03-03 19:31:44 -080030import android.net.MobileDataStateTracker;
Robert Greenwalt34848c02011-03-25 13:09:25 -070031import android.net.NetworkConfig;
The Android Open Source Project28527d22009-03-03 19:31:44 -080032import android.net.NetworkInfo;
33import android.net.NetworkStateTracker;
Robert Greenwalt7fe44cb2010-08-27 09:24:29 -070034import android.net.NetworkUtils;
Robert Greenwaltc3c5f862010-10-11 16:00:27 -070035import android.net.Proxy;
36import android.net.ProxyProperties;
Hung-ying Tyan4e723422011-01-19 16:48:38 +080037import android.net.vpn.VpnManager;
The Android Open Source Project28527d22009-03-03 19:31:44 -080038import android.net.wifi.WifiStateTracker;
39import android.os.Binder;
40import android.os.Handler;
Wink Saville775aad62010-09-02 19:23:52 -070041import android.os.HandlerThread;
Robert Greenwalt2034b912009-08-12 16:08:25 -070042import android.os.IBinder;
The Android Open Source Project28527d22009-03-03 19:31:44 -080043import android.os.Looper;
44import android.os.Message;
Robert Greenwalt93dc1042010-06-15 12:19:37 -070045import android.os.PowerManager;
Robert Greenwalt2034b912009-08-12 16:08:25 -070046import android.os.RemoteException;
The Android Open Source Project28527d22009-03-03 19:31:44 -080047import android.os.ServiceManager;
48import android.os.SystemProperties;
49import android.provider.Settings;
Robert Greenwalt2034b912009-08-12 16:08:25 -070050import android.text.TextUtils;
The Android Open Source Project28527d22009-03-03 19:31:44 -080051import android.util.EventLog;
Joe Onoratoc2386bb2010-02-26 18:56:32 -080052import android.util.Slog;
The Android Open Source Project28527d22009-03-03 19:31:44 -080053
Robert Greenwalt2034b912009-08-12 16:08:25 -070054import com.android.internal.telephony.Phone;
Robert Greenwalt0c4828c2010-01-26 11:40:34 -080055import com.android.server.connectivity.Tethering;
56
The Android Open Source Project28527d22009-03-03 19:31:44 -080057import java.io.FileDescriptor;
Irfan Sheriff7f132d92010-06-09 15:39:36 -070058import java.io.FileWriter;
59import java.io.IOException;
The Android Open Source Project28527d22009-03-03 19:31:44 -080060import java.io.PrintWriter;
Robert Greenwalta7dfbd32010-06-15 15:43:39 -070061import java.net.InetAddress;
62import java.net.UnknownHostException;
Robert Greenwalt2034b912009-08-12 16:08:25 -070063import java.util.ArrayList;
Robert Greenwalta7dfbd32010-06-15 15:43:39 -070064import java.util.Collection;
Robert Greenwaltd62c7002010-12-29 16:15:02 -080065import java.util.concurrent.atomic.AtomicBoolean;
Robert Greenwalt0e80be12010-09-20 14:35:25 -070066import java.util.GregorianCalendar;
Robert Greenwalt2034b912009-08-12 16:08:25 -070067import java.util.List;
The Android Open Source Project28527d22009-03-03 19:31:44 -080068
69/**
70 * @hide
71 */
72public class ConnectivityService extends IConnectivityManager.Stub {
73
Robert Greenwalt063dc7d2010-10-05 19:12:26 -070074 private static final boolean DBG = true;
The Android Open Source Project28527d22009-03-03 19:31:44 -080075 private static final String TAG = "ConnectivityService";
76
Robert Greenwalt2034b912009-08-12 16:08:25 -070077 // how long to wait before switching back to a radio's default network
78 private static final int RESTORE_DEFAULT_NETWORK_DELAY = 1 * 60 * 1000;
79 // system property that can override the above value
80 private static final String NETWORK_RESTORE_DELAY_PROP_NAME =
81 "android.telephony.apn-restore";
82
Robert Greenwalt0c4828c2010-01-26 11:40:34 -080083 private Tethering mTethering;
Robert Greenwaltf1b66e12010-02-25 12:29:30 -080084 private boolean mTetheringConfigValid = false;
Robert Greenwalt0c4828c2010-01-26 11:40:34 -080085
The Android Open Source Project28527d22009-03-03 19:31:44 -080086 /**
87 * Sometimes we want to refer to the individual network state
88 * trackers separately, and sometimes we just want to treat them
89 * abstractly.
90 */
91 private NetworkStateTracker mNetTrackers[];
Robert Greenwalt2034b912009-08-12 16:08:25 -070092
93 /**
94 * A per Net list of the PID's that requested access to the net
95 * used both as a refcount and for per-PID DNS selection
96 */
97 private List mNetRequestersPids[];
98
Irfan Sheriff653e2a22010-06-07 09:03:04 -070099 private WifiWatchdogService mWifiWatchdogService;
100
Robert Greenwalt2034b912009-08-12 16:08:25 -0700101 // priority order of the nettrackers
102 // (excluding dynamically set mNetworkPreference)
103 // TODO - move mNetworkTypePreference into this
104 private int[] mPriorityList;
105
The Android Open Source Project28527d22009-03-03 19:31:44 -0800106 private Context mContext;
107 private int mNetworkPreference;
Robert Greenwalt2034b912009-08-12 16:08:25 -0700108 private int mActiveDefaultNetwork = -1;
Robert Greenwalt986c7412010-09-08 15:24:47 -0700109 // 0 is full bad, 100 is full good
110 private int mDefaultInetCondition = 0;
111 private int mDefaultInetConditionPublished = 0;
112 private boolean mInetConditionChangeInFlight = false;
113 private int mDefaultConnectionSequence = 0;
The Android Open Source Project28527d22009-03-03 19:31:44 -0800114
115 private int mNumDnsEntries;
The Android Open Source Project28527d22009-03-03 19:31:44 -0800116
117 private boolean mTestMode;
Joe Onorato56023ad2010-09-01 21:18:22 -0700118 private static ConnectivityService sServiceInstance;
The Android Open Source Project28527d22009-03-03 19:31:44 -0800119
Robert Greenwaltd62c7002010-12-29 16:15:02 -0800120 private AtomicBoolean mBackgroundDataEnabled = new AtomicBoolean(true);
121
Robert Greenwalt6a2db8a2010-09-23 10:05:56 -0700122 private static final int ENABLED = 1;
123 private static final int DISABLED = 0;
124
125 // Share the event space with NetworkStateTracker (which can't see this
126 // internal class but sends us events). If you change these, change
127 // NetworkStateTracker.java too.
128 private static final int MIN_NETWORK_STATE_TRACKER_EVENT = 1;
129 private static final int MAX_NETWORK_STATE_TRACKER_EVENT = 100;
130
131 /**
132 * used internally as a delayed event to make us switch back to the
133 * default network
134 */
135 private static final int EVENT_RESTORE_DEFAULT_NETWORK =
136 MAX_NETWORK_STATE_TRACKER_EVENT + 1;
137
138 /**
139 * used internally to change our mobile data enabled flag
140 */
141 private static final int EVENT_CHANGE_MOBILE_DATA_ENABLED =
142 MAX_NETWORK_STATE_TRACKER_EVENT + 2;
143
144 /**
145 * used internally to change our network preference setting
146 * arg1 = networkType to prefer
147 */
148 private static final int EVENT_SET_NETWORK_PREFERENCE =
149 MAX_NETWORK_STATE_TRACKER_EVENT + 3;
150
151 /**
152 * used internally to synchronize inet condition reports
153 * arg1 = networkType
154 * arg2 = condition (0 bad, 100 good)
155 */
156 private static final int EVENT_INET_CONDITION_CHANGE =
157 MAX_NETWORK_STATE_TRACKER_EVENT + 4;
158
159 /**
160 * used internally to mark the end of inet condition hold periods
161 * arg1 = networkType
162 */
163 private static final int EVENT_INET_CONDITION_HOLD_END =
164 MAX_NETWORK_STATE_TRACKER_EVENT + 5;
165
166 /**
167 * used internally to set the background data preference
168 * arg1 = TRUE for enabled, FALSE for disabled
169 */
170 private static final int EVENT_SET_BACKGROUND_DATA =
171 MAX_NETWORK_STATE_TRACKER_EVENT + 6;
172
173 /**
174 * used internally to set enable/disable cellular data
175 * arg1 = ENBALED or DISABLED
176 */
177 private static final int EVENT_SET_MOBILE_DATA =
178 MAX_NETWORK_STATE_TRACKER_EVENT + 7;
179
Robert Greenwaltccb36f92010-09-24 14:32:21 -0700180 /**
181 * used internally to clear a wakelock when transitioning
182 * from one net to another
183 */
184 private static final int EVENT_CLEAR_NET_TRANSITION_WAKELOCK =
185 MAX_NETWORK_STATE_TRACKER_EVENT + 8;
186
Robert Greenwaltc3c5f862010-10-11 16:00:27 -0700187 /**
188 * used internally to reload global proxy settings
189 */
190 private static final int EVENT_APPLY_GLOBAL_HTTP_PROXY =
191 MAX_NETWORK_STATE_TRACKER_EVENT + 9;
192
Robert Greenwalt34848c02011-03-25 13:09:25 -0700193 /**
194 * used internally to set external dependency met/unmet
195 * arg1 = ENABLED (met) or DISABLED (unmet)
196 * arg2 = NetworkType
197 */
198 private static final int EVENT_SET_DEPENDENCY_MET =
199 MAX_NETWORK_STATE_TRACKER_EVENT + 10;
200
Robert Greenwalt2034b912009-08-12 16:08:25 -0700201 private Handler mHandler;
202
203 // list of DeathRecipients used to make sure features are turned off when
204 // a process dies
205 private List mFeatureUsers;
206
Mike Lockwoodfde2b762009-08-14 14:18:49 -0400207 private boolean mSystemReady;
Dianne Hackborna417ff82009-12-08 19:45:14 -0800208 private Intent mInitialBroadcast;
Mike Lockwoodfde2b762009-08-14 14:18:49 -0400209
Robert Greenwalt93dc1042010-06-15 12:19:37 -0700210 private PowerManager.WakeLock mNetTransitionWakeLock;
211 private String mNetTransitionWakeLockCausedBy = "";
212 private int mNetTransitionWakeLockSerialNumber;
213 private int mNetTransitionWakeLockTimeout;
214
Robert Greenwalt94daa182010-09-01 11:34:05 -0700215 private InetAddress mDefaultDns;
216
Robert Greenwalt0e80be12010-09-20 14:35:25 -0700217 // used in DBG mode to track inet condition reports
218 private static final int INET_CONDITION_LOG_MAX_SIZE = 15;
219 private ArrayList mInetLog;
220
Robert Greenwaltc3c5f862010-10-11 16:00:27 -0700221 // track the current default http proxy - tell the world if we get a new one (real change)
222 private ProxyProperties mDefaultProxy = null;
223 // track the global proxy.
224 private ProxyProperties mGlobalProxy = null;
225 private final Object mGlobalProxyLock = new Object();
226
227 private SettingsObserver mSettingsObserver;
228
Robert Greenwalt34848c02011-03-25 13:09:25 -0700229 NetworkConfig[] mNetConfigs;
Robert Greenwaltec05b3c2009-10-30 14:17:42 -0700230 int mNetworksDefined;
Robert Greenwalt2034b912009-08-12 16:08:25 -0700231
Robert Greenwalt12c44552009-12-07 11:33:18 -0800232 private static class RadioAttributes {
Robert Greenwalt2034b912009-08-12 16:08:25 -0700233 public int mSimultaneity;
234 public int mType;
235 public RadioAttributes(String init) {
236 String fragments[] = init.split(",");
Robert Greenwaltec05b3c2009-10-30 14:17:42 -0700237 mType = Integer.parseInt(fragments[0]);
238 mSimultaneity = Integer.parseInt(fragments[1]);
Robert Greenwalt2034b912009-08-12 16:08:25 -0700239 }
240 }
241 RadioAttributes[] mRadioAttributes;
242
Wink Saville775aad62010-09-02 19:23:52 -0700243 public static synchronized ConnectivityService getInstance(Context context) {
244 if (sServiceInstance == null) {
245 sServiceInstance = new ConnectivityService(context);
The Android Open Source Project28527d22009-03-03 19:31:44 -0800246 }
Wink Saville775aad62010-09-02 19:23:52 -0700247 return sServiceInstance;
The Android Open Source Project28527d22009-03-03 19:31:44 -0800248 }
Robert Greenwalt0659da32009-07-16 17:21:39 -0700249
The Android Open Source Project28527d22009-03-03 19:31:44 -0800250 private ConnectivityService(Context context) {
Wink Savillee70c6f52010-12-03 12:01:38 -0800251 if (DBG) log("ConnectivityService starting up");
Robert Greenwaltd48f8ee2010-01-14 17:47:58 -0800252
Wink Saville775aad62010-09-02 19:23:52 -0700253 HandlerThread handlerThread = new HandlerThread("ConnectivityServiceThread");
254 handlerThread.start();
255 mHandler = new MyHandler(handlerThread.getLooper());
256
Robert Greenwaltd62c7002010-12-29 16:15:02 -0800257 mBackgroundDataEnabled.set(Settings.Secure.getInt(context.getContentResolver(),
258 Settings.Secure.BACKGROUND_DATA, 1) == 1);
259
Robert Greenwaltd48f8ee2010-01-14 17:47:58 -0800260 // setup our unique device name
Robert Greenwalt82cde132010-12-06 09:30:17 -0800261 if (TextUtils.isEmpty(SystemProperties.get("net.hostname"))) {
262 String id = Settings.Secure.getString(context.getContentResolver(),
263 Settings.Secure.ANDROID_ID);
264 if (id != null && id.length() > 0) {
265 String name = new String("android_").concat(id);
266 SystemProperties.set("net.hostname", name);
267 }
Robert Greenwaltd48f8ee2010-01-14 17:47:58 -0800268 }
269
Robert Greenwalt94daa182010-09-01 11:34:05 -0700270 // read our default dns server ip
271 String dns = Settings.Secure.getString(context.getContentResolver(),
272 Settings.Secure.DEFAULT_DNS_SERVER);
273 if (dns == null || dns.length() == 0) {
274 dns = context.getResources().getString(
275 com.android.internal.R.string.config_default_dns_server);
276 }
277 try {
Robert Greenwalt35e34d12011-02-22 16:00:42 -0800278 mDefaultDns = NetworkUtils.numericToInetAddress(dns);
279 } catch (IllegalArgumentException e) {
Wink Savillee70c6f52010-12-03 12:01:38 -0800280 loge("Error setting defaultDns using " + dns);
Robert Greenwalt94daa182010-09-01 11:34:05 -0700281 }
282
The Android Open Source Project28527d22009-03-03 19:31:44 -0800283 mContext = context;
Robert Greenwalt93dc1042010-06-15 12:19:37 -0700284
285 PowerManager powerManager = (PowerManager)mContext.getSystemService(Context.POWER_SERVICE);
286 mNetTransitionWakeLock = powerManager.newWakeLock(PowerManager.PARTIAL_WAKE_LOCK, TAG);
287 mNetTransitionWakeLockTimeout = mContext.getResources().getInteger(
288 com.android.internal.R.integer.config_networkTransitionTimeout);
289
Robert Greenwalt2034b912009-08-12 16:08:25 -0700290 mNetTrackers = new NetworkStateTracker[
291 ConnectivityManager.MAX_NETWORK_TYPE+1];
Robert Greenwalt0659da32009-07-16 17:21:39 -0700292
The Android Open Source Project28527d22009-03-03 19:31:44 -0800293 mNetworkPreference = getPersistedNetworkPreference();
Robert Greenwalt0659da32009-07-16 17:21:39 -0700294
Robert Greenwaltec05b3c2009-10-30 14:17:42 -0700295 mRadioAttributes = new RadioAttributes[ConnectivityManager.MAX_RADIO_TYPE+1];
Robert Greenwalt34848c02011-03-25 13:09:25 -0700296 mNetConfigs = new NetworkConfig[ConnectivityManager.MAX_NETWORK_TYPE+1];
Robert Greenwaltec05b3c2009-10-30 14:17:42 -0700297
Robert Greenwalt2034b912009-08-12 16:08:25 -0700298 // Load device network attributes from resources
Robert Greenwalt2034b912009-08-12 16:08:25 -0700299 String[] raStrings = context.getResources().getStringArray(
300 com.android.internal.R.array.radioAttributes);
Robert Greenwaltec05b3c2009-10-30 14:17:42 -0700301 for (String raString : raStrings) {
302 RadioAttributes r = new RadioAttributes(raString);
303 if (r.mType > ConnectivityManager.MAX_RADIO_TYPE) {
Wink Savillee70c6f52010-12-03 12:01:38 -0800304 loge("Error in radioAttributes - ignoring attempt to define type " + r.mType);
Robert Greenwaltec05b3c2009-10-30 14:17:42 -0700305 continue;
306 }
307 if (mRadioAttributes[r.mType] != null) {
Wink Savillee70c6f52010-12-03 12:01:38 -0800308 loge("Error in radioAttributes - ignoring attempt to redefine type " +
Robert Greenwaltec05b3c2009-10-30 14:17:42 -0700309 r.mType);
310 continue;
311 }
Robert Greenwalt2034b912009-08-12 16:08:25 -0700312 mRadioAttributes[r.mType] = r;
313 }
314
Robert Greenwaltec05b3c2009-10-30 14:17:42 -0700315 String[] naStrings = context.getResources().getStringArray(
316 com.android.internal.R.array.networkAttributes);
317 for (String naString : naStrings) {
318 try {
Robert Greenwalt34848c02011-03-25 13:09:25 -0700319 NetworkConfig n = new NetworkConfig(naString);
Wink Savillef2a62832011-04-07 14:23:45 -0700320 if (n.type > ConnectivityManager.MAX_NETWORK_TYPE) {
Wink Savillee70c6f52010-12-03 12:01:38 -0800321 loge("Error in networkAttributes - ignoring attempt to define type " +
Wink Savillef2a62832011-04-07 14:23:45 -0700322 n.type);
Robert Greenwaltec05b3c2009-10-30 14:17:42 -0700323 continue;
Robert Greenwalt2034b912009-08-12 16:08:25 -0700324 }
Wink Savillef2a62832011-04-07 14:23:45 -0700325 if (mNetConfigs[n.type] != null) {
Wink Savillee70c6f52010-12-03 12:01:38 -0800326 loge("Error in networkAttributes - ignoring attempt to redefine type " +
Wink Savillef2a62832011-04-07 14:23:45 -0700327 n.type);
Robert Greenwaltec05b3c2009-10-30 14:17:42 -0700328 continue;
329 }
Wink Savillef2a62832011-04-07 14:23:45 -0700330 if (mRadioAttributes[n.radio] == null) {
Wink Savillee70c6f52010-12-03 12:01:38 -0800331 loge("Error in networkAttributes - ignoring attempt to use undefined " +
Wink Savillef2a62832011-04-07 14:23:45 -0700332 "radio " + n.radio + " in network type " + n.type);
Robert Greenwaltec05b3c2009-10-30 14:17:42 -0700333 continue;
334 }
Wink Savillef2a62832011-04-07 14:23:45 -0700335 mNetConfigs[n.type] = n;
Robert Greenwaltec05b3c2009-10-30 14:17:42 -0700336 mNetworksDefined++;
337 } catch(Exception e) {
338 // ignore it - leave the entry null
Robert Greenwalt2034b912009-08-12 16:08:25 -0700339 }
340 }
341
Robert Greenwaltec05b3c2009-10-30 14:17:42 -0700342 // high priority first
343 mPriorityList = new int[mNetworksDefined];
344 {
345 int insertionPoint = mNetworksDefined-1;
346 int currentLowest = 0;
347 int nextLowest = 0;
348 while (insertionPoint > -1) {
Robert Greenwalt34848c02011-03-25 13:09:25 -0700349 for (NetworkConfig na : mNetConfigs) {
Robert Greenwaltec05b3c2009-10-30 14:17:42 -0700350 if (na == null) continue;
Wink Savillef2a62832011-04-07 14:23:45 -0700351 if (na.priority < currentLowest) continue;
352 if (na.priority > currentLowest) {
353 if (na.priority < nextLowest || nextLowest == 0) {
354 nextLowest = na.priority;
Robert Greenwaltec05b3c2009-10-30 14:17:42 -0700355 }
356 continue;
357 }
Wink Savillef2a62832011-04-07 14:23:45 -0700358 mPriorityList[insertionPoint--] = na.type;
Robert Greenwaltec05b3c2009-10-30 14:17:42 -0700359 }
360 currentLowest = nextLowest;
361 nextLowest = 0;
362 }
363 }
364
365 mNetRequestersPids = new ArrayList[ConnectivityManager.MAX_NETWORK_TYPE+1];
366 for (int i : mPriorityList) {
Robert Greenwalt2034b912009-08-12 16:08:25 -0700367 mNetRequestersPids[i] = new ArrayList();
368 }
369
370 mFeatureUsers = new ArrayList();
371
Robert Greenwaltec05b3c2009-10-30 14:17:42 -0700372 mNumDnsEntries = 0;
373
374 mTestMode = SystemProperties.get("cm.test.mode").equals("true")
375 && SystemProperties.get("ro.build.type").equals("eng");
The Android Open Source Project28527d22009-03-03 19:31:44 -0800376 /*
377 * Create the network state trackers for Wi-Fi and mobile
378 * data. Maybe this could be done with a factory class,
379 * but it's not clear that it's worth it, given that
380 * the number of different network types is not going
381 * to change very often.
382 */
Robert Greenwaltec05b3c2009-10-30 14:17:42 -0700383 for (int netType : mPriorityList) {
Wink Savillef2a62832011-04-07 14:23:45 -0700384 switch (mNetConfigs[netType].radio) {
Robert Greenwaltec05b3c2009-10-30 14:17:42 -0700385 case ConnectivityManager.TYPE_WIFI:
Wink Savillee70c6f52010-12-03 12:01:38 -0800386 if (DBG) log("Starting Wifi Service.");
Wink Saville7fabfa22010-08-13 16:11:42 -0700387 WifiStateTracker wst = new WifiStateTracker();
Irfan Sheriff25be0762010-07-28 09:35:20 -0700388 WifiService wifiService = new WifiService(context);
Robert Greenwaltec05b3c2009-10-30 14:17:42 -0700389 ServiceManager.addService(Context.WIFI_SERVICE, wifiService);
Irfan Sheriff25be0762010-07-28 09:35:20 -0700390 wifiService.checkAndStartWifi();
Robert Greenwaltec05b3c2009-10-30 14:17:42 -0700391 mNetTrackers[ConnectivityManager.TYPE_WIFI] = wst;
Wink Saville7fabfa22010-08-13 16:11:42 -0700392 wst.startMonitoring(context, mHandler);
The Android Open Source Project28527d22009-03-03 19:31:44 -0800393
Irfan Sheriff653e2a22010-06-07 09:03:04 -0700394 //TODO: as part of WWS refactor, create only when needed
Irfan Sheriff25be0762010-07-28 09:35:20 -0700395 mWifiWatchdogService = new WifiWatchdogService(context);
Irfan Sheriff653e2a22010-06-07 09:03:04 -0700396
Robert Greenwaltec05b3c2009-10-30 14:17:42 -0700397 break;
398 case ConnectivityManager.TYPE_MOBILE:
Wink Saville7fabfa22010-08-13 16:11:42 -0700399 mNetTrackers[netType] = new MobileDataStateTracker(netType,
Wink Savillef2a62832011-04-07 14:23:45 -0700400 mNetConfigs[netType].name);
Wink Saville7fabfa22010-08-13 16:11:42 -0700401 mNetTrackers[netType].startMonitoring(context, mHandler);
Robert Greenwaltec05b3c2009-10-30 14:17:42 -0700402 break;
Robert Greenwalteb123ac2010-12-06 13:56:24 -0800403 case ConnectivityManager.TYPE_DUMMY:
404 mNetTrackers[netType] = new DummyDataStateTracker(netType,
Wink Savillef2a62832011-04-07 14:23:45 -0700405 mNetConfigs[netType].name);
Robert Greenwalteb123ac2010-12-06 13:56:24 -0800406 mNetTrackers[netType].startMonitoring(context, mHandler);
407 break;
Jaikumar Ganesh0db51a02010-12-21 22:31:44 -0800408 case ConnectivityManager.TYPE_BLUETOOTH:
409 mNetTrackers[netType] = BluetoothTetheringDataTracker.getInstance();
410 mNetTrackers[netType].startMonitoring(context, mHandler);
411 break;
Benoit Goby6cec7f32010-12-22 14:29:40 -0800412 case ConnectivityManager.TYPE_ETHERNET:
413 mNetTrackers[netType] = EthernetDataTracker.getInstance();
414 mNetTrackers[netType].startMonitoring(context, mHandler);
415 break;
Robert Greenwaltec05b3c2009-10-30 14:17:42 -0700416 default:
Wink Savillee70c6f52010-12-03 12:01:38 -0800417 loge("Trying to create a DataStateTracker for an unknown radio type " +
Wink Savillef2a62832011-04-07 14:23:45 -0700418 mNetConfigs[netType].radio);
Robert Greenwaltec05b3c2009-10-30 14:17:42 -0700419 continue;
420 }
421 }
Robert Greenwalt0c4828c2010-01-26 11:40:34 -0800422
Robert Greenwaltc0b6c602010-03-11 15:03:08 -0800423 mTethering = new Tethering(mContext, mHandler.getLooper());
Robert Greenwaltf1b66e12010-02-25 12:29:30 -0800424 mTetheringConfigValid = (((mNetTrackers[ConnectivityManager.TYPE_MOBILE_DUN] != null) ||
425 !mTethering.isDunRequired()) &&
426 (mTethering.getTetherableUsbRegexs().length != 0 ||
Danica Chang96567052010-08-11 14:54:43 -0700427 mTethering.getTetherableWifiRegexs().length != 0 ||
428 mTethering.getTetherableBluetoothRegexs().length != 0) &&
Robert Greenwaltf1b66e12010-02-25 12:29:30 -0800429 mTethering.getUpstreamIfaceRegexs().length != 0);
430
Robert Greenwalt0e80be12010-09-20 14:35:25 -0700431 if (DBG) {
432 mInetLog = new ArrayList();
433 }
Robert Greenwaltc3c5f862010-10-11 16:00:27 -0700434
435 mSettingsObserver = new SettingsObserver(mHandler, EVENT_APPLY_GLOBAL_HTTP_PROXY);
436 mSettingsObserver.observe(mContext);
Robert Greenwalt6f7c6092010-12-02 11:31:00 -0800437
438 loadGlobalProxy();
Hung-ying Tyan4e723422011-01-19 16:48:38 +0800439
440 VpnManager.startVpnService(context);
The Android Open Source Project28527d22009-03-03 19:31:44 -0800441 }
442
Robert Greenwaltec05b3c2009-10-30 14:17:42 -0700443
The Android Open Source Project28527d22009-03-03 19:31:44 -0800444 /**
Robert Greenwalt0659da32009-07-16 17:21:39 -0700445 * Sets the preferred network.
The Android Open Source Project28527d22009-03-03 19:31:44 -0800446 * @param preference the new preference
447 */
Robert Greenwalt6a2db8a2010-09-23 10:05:56 -0700448 public void setNetworkPreference(int preference) {
The Android Open Source Project28527d22009-03-03 19:31:44 -0800449 enforceChangePermission();
Robert Greenwalt6a2db8a2010-09-23 10:05:56 -0700450
451 mHandler.sendMessage(mHandler.obtainMessage(EVENT_SET_NETWORK_PREFERENCE, preference, 0));
The Android Open Source Project28527d22009-03-03 19:31:44 -0800452 }
453
454 public int getNetworkPreference() {
455 enforceAccessPermission();
Robert Greenwalt6a2db8a2010-09-23 10:05:56 -0700456 int preference;
457 synchronized(this) {
458 preference = mNetworkPreference;
459 }
460 return preference;
The Android Open Source Project28527d22009-03-03 19:31:44 -0800461 }
462
Robert Greenwalt6a2db8a2010-09-23 10:05:56 -0700463 private void handleSetNetworkPreference(int preference) {
464 if (ConnectivityManager.isNetworkTypeValid(preference) &&
Robert Greenwalt34848c02011-03-25 13:09:25 -0700465 mNetConfigs[preference] != null &&
466 mNetConfigs[preference].isDefault()) {
Robert Greenwalt6a2db8a2010-09-23 10:05:56 -0700467 if (mNetworkPreference != preference) {
468 final ContentResolver cr = mContext.getContentResolver();
469 Settings.Secure.putInt(cr, Settings.Secure.NETWORK_PREFERENCE, preference);
470 synchronized(this) {
471 mNetworkPreference = preference;
472 }
473 enforcePreference();
474 }
475 }
The Android Open Source Project28527d22009-03-03 19:31:44 -0800476 }
Robert Greenwalt0659da32009-07-16 17:21:39 -0700477
The Android Open Source Project28527d22009-03-03 19:31:44 -0800478 private int getPersistedNetworkPreference() {
479 final ContentResolver cr = mContext.getContentResolver();
480
481 final int networkPrefSetting = Settings.Secure
482 .getInt(cr, Settings.Secure.NETWORK_PREFERENCE, -1);
483 if (networkPrefSetting != -1) {
484 return networkPrefSetting;
485 }
486
487 return ConnectivityManager.DEFAULT_NETWORK_PREFERENCE;
488 }
Robert Greenwalt0659da32009-07-16 17:21:39 -0700489
The Android Open Source Project28527d22009-03-03 19:31:44 -0800490 /**
Robert Greenwalt0659da32009-07-16 17:21:39 -0700491 * Make the state of network connectivity conform to the preference settings
The Android Open Source Project28527d22009-03-03 19:31:44 -0800492 * In this method, we only tear down a non-preferred network. Establishing
493 * a connection to the preferred network is taken care of when we handle
494 * the disconnect event from the non-preferred network
495 * (see {@link #handleDisconnect(NetworkInfo)}).
496 */
497 private void enforcePreference() {
Robert Greenwalt2034b912009-08-12 16:08:25 -0700498 if (mNetTrackers[mNetworkPreference].getNetworkInfo().isConnected())
The Android Open Source Project28527d22009-03-03 19:31:44 -0800499 return;
500
Robert Greenwalt2034b912009-08-12 16:08:25 -0700501 if (!mNetTrackers[mNetworkPreference].isAvailable())
502 return;
The Android Open Source Project28527d22009-03-03 19:31:44 -0800503
Robert Greenwalt2034b912009-08-12 16:08:25 -0700504 for (int t=0; t <= ConnectivityManager.MAX_RADIO_TYPE; t++) {
Robert Greenwaltec05b3c2009-10-30 14:17:42 -0700505 if (t != mNetworkPreference && mNetTrackers[t] != null &&
Robert Greenwalt2034b912009-08-12 16:08:25 -0700506 mNetTrackers[t].getNetworkInfo().isConnected()) {
Robert Greenwaltf3f045b2009-08-20 15:25:14 -0700507 if (DBG) {
Wink Savillee70c6f52010-12-03 12:01:38 -0800508 log("tearing down " + mNetTrackers[t].getNetworkInfo() +
Robert Greenwaltf3f045b2009-08-20 15:25:14 -0700509 " in enforcePreference");
510 }
Robert Greenwalt2034b912009-08-12 16:08:25 -0700511 teardown(mNetTrackers[t]);
The Android Open Source Project28527d22009-03-03 19:31:44 -0800512 }
513 }
514 }
515
516 private boolean teardown(NetworkStateTracker netTracker) {
517 if (netTracker.teardown()) {
518 netTracker.setTeardownRequested(true);
519 return true;
520 } else {
521 return false;
522 }
523 }
524
525 /**
526 * Return NetworkInfo for the active (i.e., connected) network interface.
527 * It is assumed that at most one network is active at a time. If more
528 * than one is active, it is indeterminate which will be returned.
Robert Greenwalt0659da32009-07-16 17:21:39 -0700529 * @return the info for the active network, or {@code null} if none is
530 * active
The Android Open Source Project28527d22009-03-03 19:31:44 -0800531 */
532 public NetworkInfo getActiveNetworkInfo() {
533 enforceAccessPermission();
Robert Greenwalt2034b912009-08-12 16:08:25 -0700534 for (int type=0; type <= ConnectivityManager.MAX_NETWORK_TYPE; type++) {
Robert Greenwalt34848c02011-03-25 13:09:25 -0700535 if (mNetConfigs[type] == null || !mNetConfigs[type].isDefault()) {
Robert Greenwalt2034b912009-08-12 16:08:25 -0700536 continue;
537 }
538 NetworkStateTracker t = mNetTrackers[type];
The Android Open Source Project28527d22009-03-03 19:31:44 -0800539 NetworkInfo info = t.getNetworkInfo();
540 if (info.isConnected()) {
Wink Savillee70c6f52010-12-03 12:01:38 -0800541 if (DBG && type != mActiveDefaultNetwork) {
542 loge("connected default network is not mActiveDefaultNetwork!");
543 }
The Android Open Source Project28527d22009-03-03 19:31:44 -0800544 return info;
545 }
546 }
547 return null;
548 }
549
550 public NetworkInfo getNetworkInfo(int networkType) {
551 enforceAccessPermission();
552 if (ConnectivityManager.isNetworkTypeValid(networkType)) {
553 NetworkStateTracker t = mNetTrackers[networkType];
554 if (t != null)
555 return t.getNetworkInfo();
556 }
557 return null;
558 }
559
560 public NetworkInfo[] getAllNetworkInfo() {
561 enforceAccessPermission();
Robert Greenwaltec05b3c2009-10-30 14:17:42 -0700562 NetworkInfo[] result = new NetworkInfo[mNetworksDefined];
The Android Open Source Project28527d22009-03-03 19:31:44 -0800563 int i = 0;
564 for (NetworkStateTracker t : mNetTrackers) {
Robert Greenwaltec05b3c2009-10-30 14:17:42 -0700565 if(t != null) result[i++] = t.getNetworkInfo();
The Android Open Source Project28527d22009-03-03 19:31:44 -0800566 }
567 return result;
568 }
569
Robert Greenwalt9f0ee4f2010-09-14 09:18:02 -0700570 /**
571 * Return LinkProperties for the active (i.e., connected) default
572 * network interface. It is assumed that at most one default network
573 * is active at a time. If more than one is active, it is indeterminate
574 * which will be returned.
575 * @return the ip properties for the active network, or {@code null} if
576 * none is active
577 */
578 public LinkProperties getActiveLinkProperties() {
579 enforceAccessPermission();
580 for (int type=0; type <= ConnectivityManager.MAX_NETWORK_TYPE; type++) {
Robert Greenwalt34848c02011-03-25 13:09:25 -0700581 if (mNetConfigs[type] == null || !mNetConfigs[type].isDefault()) {
Robert Greenwalt9f0ee4f2010-09-14 09:18:02 -0700582 continue;
583 }
584 NetworkStateTracker t = mNetTrackers[type];
585 NetworkInfo info = t.getNetworkInfo();
586 if (info.isConnected()) {
587 return t.getLinkProperties();
588 }
589 }
590 return null;
591 }
592
593 public LinkProperties getLinkProperties(int networkType) {
594 enforceAccessPermission();
595 if (ConnectivityManager.isNetworkTypeValid(networkType)) {
596 NetworkStateTracker t = mNetTrackers[networkType];
597 if (t != null) return t.getLinkProperties();
598 }
599 return null;
600 }
601
The Android Open Source Project28527d22009-03-03 19:31:44 -0800602 public boolean setRadios(boolean turnOn) {
603 boolean result = true;
604 enforceChangePermission();
605 for (NetworkStateTracker t : mNetTrackers) {
Robert Greenwaltec05b3c2009-10-30 14:17:42 -0700606 if (t != null) result = t.setRadio(turnOn) && result;
The Android Open Source Project28527d22009-03-03 19:31:44 -0800607 }
608 return result;
609 }
610
611 public boolean setRadio(int netType, boolean turnOn) {
612 enforceChangePermission();
613 if (!ConnectivityManager.isNetworkTypeValid(netType)) {
614 return false;
615 }
616 NetworkStateTracker tracker = mNetTrackers[netType];
617 return tracker != null && tracker.setRadio(turnOn);
618 }
619
Robert Greenwaltaffc3a12009-09-27 17:27:04 -0700620 /**
621 * Used to notice when the calling process dies so we can self-expire
622 *
623 * Also used to know if the process has cleaned up after itself when
624 * our auto-expire timer goes off. The timer has a link to an object.
625 *
626 */
Robert Greenwalt2034b912009-08-12 16:08:25 -0700627 private class FeatureUser implements IBinder.DeathRecipient {
628 int mNetworkType;
629 String mFeature;
630 IBinder mBinder;
631 int mPid;
632 int mUid;
Robert Greenwalt3eeb6032009-12-21 18:24:07 -0800633 long mCreateTime;
Robert Greenwalt2034b912009-08-12 16:08:25 -0700634
635 FeatureUser(int type, String feature, IBinder binder) {
636 super();
637 mNetworkType = type;
638 mFeature = feature;
639 mBinder = binder;
640 mPid = getCallingPid();
641 mUid = getCallingUid();
Robert Greenwalt3eeb6032009-12-21 18:24:07 -0800642 mCreateTime = System.currentTimeMillis();
Robert Greenwaltaffc3a12009-09-27 17:27:04 -0700643
Robert Greenwalt2034b912009-08-12 16:08:25 -0700644 try {
645 mBinder.linkToDeath(this, 0);
646 } catch (RemoteException e) {
647 binderDied();
648 }
649 }
650
651 void unlinkDeathRecipient() {
652 mBinder.unlinkToDeath(this, 0);
653 }
654
655 public void binderDied() {
Wink Savillee70c6f52010-12-03 12:01:38 -0800656 log("ConnectivityService FeatureUser binderDied(" +
Robert Greenwalt3eeb6032009-12-21 18:24:07 -0800657 mNetworkType + ", " + mFeature + ", " + mBinder + "), created " +
658 (System.currentTimeMillis() - mCreateTime) + " mSec ago");
Robert Greenwaltaffc3a12009-09-27 17:27:04 -0700659 stopUsingNetworkFeature(this, false);
Robert Greenwalt2034b912009-08-12 16:08:25 -0700660 }
661
Robert Greenwaltaffc3a12009-09-27 17:27:04 -0700662 public void expire() {
Wink Savillee70c6f52010-12-03 12:01:38 -0800663 log("ConnectivityService FeatureUser expire(" +
Robert Greenwalt3eeb6032009-12-21 18:24:07 -0800664 mNetworkType + ", " + mFeature + ", " + mBinder +"), created " +
665 (System.currentTimeMillis() - mCreateTime) + " mSec ago");
Robert Greenwaltaffc3a12009-09-27 17:27:04 -0700666 stopUsingNetworkFeature(this, false);
667 }
Robert Greenwalt3eeb6032009-12-21 18:24:07 -0800668
669 public String toString() {
670 return "FeatureUser("+mNetworkType+","+mFeature+","+mPid+","+mUid+"), created " +
671 (System.currentTimeMillis() - mCreateTime) + " mSec ago";
672 }
Robert Greenwalt2034b912009-08-12 16:08:25 -0700673 }
674
Robert Greenwaltaffc3a12009-09-27 17:27:04 -0700675 // javadoc from interface
Robert Greenwalt2034b912009-08-12 16:08:25 -0700676 public int startUsingNetworkFeature(int networkType, String feature,
677 IBinder binder) {
678 if (DBG) {
Wink Savillee70c6f52010-12-03 12:01:38 -0800679 log("startUsingNetworkFeature for net " + networkType + ": " + feature);
Robert Greenwalt2034b912009-08-12 16:08:25 -0700680 }
The Android Open Source Project28527d22009-03-03 19:31:44 -0800681 enforceChangePermission();
Robert Greenwaltec05b3c2009-10-30 14:17:42 -0700682 if (!ConnectivityManager.isNetworkTypeValid(networkType) ||
Robert Greenwalt34848c02011-03-25 13:09:25 -0700683 mNetConfigs[networkType] == null) {
Robert Greenwalt2034b912009-08-12 16:08:25 -0700684 return Phone.APN_REQUEST_FAILED;
The Android Open Source Project28527d22009-03-03 19:31:44 -0800685 }
Robert Greenwalt2034b912009-08-12 16:08:25 -0700686
Robert Greenwaltaffc3a12009-09-27 17:27:04 -0700687 FeatureUser f = new FeatureUser(networkType, feature, binder);
Robert Greenwalt2034b912009-08-12 16:08:25 -0700688
689 // TODO - move this into the MobileDataStateTracker
690 int usedNetworkType = networkType;
691 if(networkType == ConnectivityManager.TYPE_MOBILE) {
Wink Savilleb7c92c72011-03-12 14:52:01 -0800692 usedNetworkType = convertFeatureToNetworkType(feature);
693 if (usedNetworkType < 0) {
694 Slog.e(TAG, "Can't match any netTracker!");
695 usedNetworkType = networkType;
Robert Greenwalt2034b912009-08-12 16:08:25 -0700696 }
697 }
698 NetworkStateTracker network = mNetTrackers[usedNetworkType];
699 if (network != null) {
Robert Greenwalt5364d752010-12-15 13:26:33 -0800700 Integer currentPid = new Integer(getCallingPid());
Robert Greenwalt2034b912009-08-12 16:08:25 -0700701 if (usedNetworkType != networkType) {
Robert Greenwalt2034b912009-08-12 16:08:25 -0700702 NetworkStateTracker radio = mNetTrackers[networkType];
703 NetworkInfo ni = network.getNetworkInfo();
704
705 if (ni.isAvailable() == false) {
Wink Savillee70c6f52010-12-03 12:01:38 -0800706 if (DBG) log("special network not available");
Robert Greenwalt2cc87442010-12-29 14:35:21 -0800707 if (!TextUtils.equals(feature,Phone.FEATURE_ENABLE_DUN_ALWAYS)) {
708 return Phone.APN_TYPE_NOT_AVAILABLE;
709 } else {
710 // else make the attempt anyway - probably giving REQUEST_STARTED below
711 }
Robert Greenwalt2034b912009-08-12 16:08:25 -0700712 }
713
Robert Greenwaltaffc3a12009-09-27 17:27:04 -0700714 synchronized(this) {
715 mFeatureUsers.add(f);
716 if (!mNetRequestersPids[usedNetworkType].contains(currentPid)) {
717 // this gets used for per-pid dns when connected
718 mNetRequestersPids[usedNetworkType].add(currentPid);
719 }
Robert Greenwalt2034b912009-08-12 16:08:25 -0700720 }
Robert Greenwaltaffc3a12009-09-27 17:27:04 -0700721
Robert Greenwalt20f819c2011-05-03 19:02:44 -0700722 int restoreTimer = getRestoreDefaultNetworkDelay(usedNetworkType);
723
724 if (restoreTimer >= 0) {
725 mHandler.sendMessageDelayed(
726 mHandler.obtainMessage(EVENT_RESTORE_DEFAULT_NETWORK, f), restoreTimer);
727 }
Robert Greenwalt2034b912009-08-12 16:08:25 -0700728
Robert Greenwalta52c75a2009-08-19 20:19:33 -0700729 if ((ni.isConnectedOrConnecting() == true) &&
730 !network.isTeardownRequested()) {
Robert Greenwalt2034b912009-08-12 16:08:25 -0700731 if (ni.isConnected() == true) {
732 // add the pid-specific dns
Robert Greenwalt3afbead2010-07-23 15:46:26 -0700733 handleDnsConfigurationChange(networkType);
Wink Savillee70c6f52010-12-03 12:01:38 -0800734 if (DBG) log("special network already active");
Robert Greenwalt2034b912009-08-12 16:08:25 -0700735 return Phone.APN_ALREADY_ACTIVE;
736 }
Wink Savillee70c6f52010-12-03 12:01:38 -0800737 if (DBG) log("special network already connecting");
Robert Greenwalt2034b912009-08-12 16:08:25 -0700738 return Phone.APN_REQUEST_STARTED;
739 }
740
741 // check if the radio in play can make another contact
742 // assume if cannot for now
743
Wink Savillee70c6f52010-12-03 12:01:38 -0800744 if (DBG) log("reconnecting to special network");
Robert Greenwalt2034b912009-08-12 16:08:25 -0700745 network.reconnect();
746 return Phone.APN_REQUEST_STARTED;
747 } else {
Robert Greenwalt5364d752010-12-15 13:26:33 -0800748 // need to remember this unsupported request so we respond appropriately on stop
749 synchronized(this) {
750 mFeatureUsers.add(f);
751 if (!mNetRequestersPids[usedNetworkType].contains(currentPid)) {
752 // this gets used for per-pid dns when connected
753 mNetRequestersPids[usedNetworkType].add(currentPid);
754 }
755 }
Robert Greenwaltd391e892010-05-18 10:52:51 -0700756 return -1;
Robert Greenwalt2034b912009-08-12 16:08:25 -0700757 }
758 }
759 return Phone.APN_TYPE_NOT_AVAILABLE;
The Android Open Source Project28527d22009-03-03 19:31:44 -0800760 }
761
Robert Greenwaltaffc3a12009-09-27 17:27:04 -0700762 // javadoc from interface
The Android Open Source Project28527d22009-03-03 19:31:44 -0800763 public int stopUsingNetworkFeature(int networkType, String feature) {
Robert Greenwalt28f43012009-10-06 17:52:40 -0700764 enforceChangePermission();
765
Robert Greenwaltaffc3a12009-09-27 17:27:04 -0700766 int pid = getCallingPid();
767 int uid = getCallingUid();
768
769 FeatureUser u = null;
770 boolean found = false;
771
772 synchronized(this) {
773 for (int i = 0; i < mFeatureUsers.size() ; i++) {
774 u = (FeatureUser)mFeatureUsers.get(i);
775 if (uid == u.mUid && pid == u.mPid &&
776 networkType == u.mNetworkType &&
777 TextUtils.equals(feature, u.mFeature)) {
778 found = true;
779 break;
780 }
781 }
782 }
783 if (found && u != null) {
784 // stop regardless of how many other time this proc had called start
785 return stopUsingNetworkFeature(u, true);
786 } else {
787 // none found!
Wink Savillee70c6f52010-12-03 12:01:38 -0800788 if (DBG) log("ignoring stopUsingNetworkFeature - not a live request");
Robert Greenwaltaffc3a12009-09-27 17:27:04 -0700789 return 1;
790 }
Robert Greenwalt2034b912009-08-12 16:08:25 -0700791 }
792
Robert Greenwaltaffc3a12009-09-27 17:27:04 -0700793 private int stopUsingNetworkFeature(FeatureUser u, boolean ignoreDups) {
794 int networkType = u.mNetworkType;
795 String feature = u.mFeature;
796 int pid = u.mPid;
797 int uid = u.mUid;
798
799 NetworkStateTracker tracker = null;
800 boolean callTeardown = false; // used to carry our decision outside of sync block
801
Robert Greenwalt2034b912009-08-12 16:08:25 -0700802 if (DBG) {
Wink Savillee70c6f52010-12-03 12:01:38 -0800803 log("stopUsingNetworkFeature for net " + networkType +
Robert Greenwalt2034b912009-08-12 16:08:25 -0700804 ": " + feature);
805 }
Robert Greenwalt28f43012009-10-06 17:52:40 -0700806
The Android Open Source Project28527d22009-03-03 19:31:44 -0800807 if (!ConnectivityManager.isNetworkTypeValid(networkType)) {
808 return -1;
809 }
Robert Greenwalt2034b912009-08-12 16:08:25 -0700810
Robert Greenwaltaffc3a12009-09-27 17:27:04 -0700811 // need to link the mFeatureUsers list with the mNetRequestersPids state in this
812 // sync block
813 synchronized(this) {
814 // check if this process still has an outstanding start request
815 if (!mFeatureUsers.contains(u)) {
Wink Savillee70c6f52010-12-03 12:01:38 -0800816 if (DBG) log("ignoring - this process has no outstanding requests");
Robert Greenwalt2034b912009-08-12 16:08:25 -0700817 return 1;
818 }
Robert Greenwaltaffc3a12009-09-27 17:27:04 -0700819 u.unlinkDeathRecipient();
820 mFeatureUsers.remove(mFeatureUsers.indexOf(u));
821 // If we care about duplicate requests, check for that here.
822 //
823 // This is done to support the extension of a request - the app
824 // can request we start the network feature again and renew the
825 // auto-shutoff delay. Normal "stop" calls from the app though
826 // do not pay attention to duplicate requests - in effect the
827 // API does not refcount and a single stop will counter multiple starts.
828 if (ignoreDups == false) {
829 for (int i = 0; i < mFeatureUsers.size() ; i++) {
830 FeatureUser x = (FeatureUser)mFeatureUsers.get(i);
831 if (x.mUid == u.mUid && x.mPid == u.mPid &&
832 x.mNetworkType == u.mNetworkType &&
833 TextUtils.equals(x.mFeature, u.mFeature)) {
Wink Savillee70c6f52010-12-03 12:01:38 -0800834 if (DBG) log("ignoring stopUsingNetworkFeature as dup is found");
Robert Greenwaltaffc3a12009-09-27 17:27:04 -0700835 return 1;
836 }
837 }
Robert Greenwalt2034b912009-08-12 16:08:25 -0700838 }
Robert Greenwaltaffc3a12009-09-27 17:27:04 -0700839
840 // TODO - move to MobileDataStateTracker
841 int usedNetworkType = networkType;
842 if (networkType == ConnectivityManager.TYPE_MOBILE) {
Wink Savilleb7c92c72011-03-12 14:52:01 -0800843 usedNetworkType = convertFeatureToNetworkType(feature);
844 if (usedNetworkType < 0) {
845 usedNetworkType = networkType;
Robert Greenwaltaffc3a12009-09-27 17:27:04 -0700846 }
847 }
848 tracker = mNetTrackers[usedNetworkType];
Robert Greenwaltec05b3c2009-10-30 14:17:42 -0700849 if (tracker == null) {
Wink Savillee70c6f52010-12-03 12:01:38 -0800850 if (DBG) log("ignoring - no known tracker for net type " + usedNetworkType);
Robert Greenwaltec05b3c2009-10-30 14:17:42 -0700851 return -1;
852 }
853 if (usedNetworkType != networkType) {
Robert Greenwaltaffc3a12009-09-27 17:27:04 -0700854 Integer currentPid = new Integer(pid);
Robert Greenwaltaffc3a12009-09-27 17:27:04 -0700855 mNetRequestersPids[usedNetworkType].remove(currentPid);
Robert Greenwalt0ca68a02009-12-17 14:54:59 -0800856 reassessPidDns(pid, true);
Robert Greenwaltaffc3a12009-09-27 17:27:04 -0700857 if (mNetRequestersPids[usedNetworkType].size() != 0) {
Wink Savillee70c6f52010-12-03 12:01:38 -0800858 if (DBG) log("not tearing down special network - " +
Robert Greenwaltaffc3a12009-09-27 17:27:04 -0700859 "others still using it");
860 return 1;
861 }
862 callTeardown = true;
Robert Greenwalt9f3be4c2011-01-10 11:58:31 -0800863 } else {
864 if (DBG) log("not a known feature - dropping");
Robert Greenwaltaffc3a12009-09-27 17:27:04 -0700865 }
866 }
Wink Savillee70c6f52010-12-03 12:01:38 -0800867 if (DBG) log("Doing network teardown");
Robert Greenwaltaffc3a12009-09-27 17:27:04 -0700868 if (callTeardown) {
869 tracker.teardown();
Robert Greenwalt2034b912009-08-12 16:08:25 -0700870 return 1;
871 } else {
Robert Greenwaltd391e892010-05-18 10:52:51 -0700872 return -1;
Robert Greenwalt2034b912009-08-12 16:08:25 -0700873 }
The Android Open Source Project28527d22009-03-03 19:31:44 -0800874 }
875
876 /**
Robert Greenwalt7fe44cb2010-08-27 09:24:29 -0700877 * @deprecated use requestRouteToHostAddress instead
878 *
The Android Open Source Project28527d22009-03-03 19:31:44 -0800879 * Ensure that a network route exists to deliver traffic to the specified
880 * host via the specified network interface.
Robert Greenwalt0659da32009-07-16 17:21:39 -0700881 * @param networkType the type of the network over which traffic to the
882 * specified host is to be routed
883 * @param hostAddress the IP address of the host to which the route is
884 * desired
The Android Open Source Project28527d22009-03-03 19:31:44 -0800885 * @return {@code true} on success, {@code false} on failure
886 */
887 public boolean requestRouteToHost(int networkType, int hostAddress) {
Robert Greenwalt7fe44cb2010-08-27 09:24:29 -0700888 InetAddress inetAddress = NetworkUtils.intToInetAddress(hostAddress);
889
890 if (inetAddress == null) {
891 return false;
892 }
893
894 return requestRouteToHostAddress(networkType, inetAddress.getAddress());
895 }
896
897 /**
898 * Ensure that a network route exists to deliver traffic to the specified
899 * host via the specified network interface.
900 * @param networkType the type of the network over which traffic to the
901 * specified host is to be routed
902 * @param hostAddress the IP address of the host to which the route is
903 * desired
904 * @return {@code true} on success, {@code false} on failure
905 */
906 public boolean requestRouteToHostAddress(int networkType, byte[] hostAddress) {
The Android Open Source Project28527d22009-03-03 19:31:44 -0800907 enforceChangePermission();
908 if (!ConnectivityManager.isNetworkTypeValid(networkType)) {
909 return false;
910 }
911 NetworkStateTracker tracker = mNetTrackers[networkType];
Robert Greenwalt4666ed02009-09-10 15:06:20 -0700912
Robert Greenwaltec05b3c2009-10-30 14:17:42 -0700913 if (tracker == null || !tracker.getNetworkInfo().isConnected() ||
914 tracker.isTeardownRequested()) {
Robert Greenwalt4666ed02009-09-10 15:06:20 -0700915 if (DBG) {
Wink Savillee70c6f52010-12-03 12:01:38 -0800916 log("requestRouteToHostAddress on down network " +
Robert Greenwalt7fe44cb2010-08-27 09:24:29 -0700917 "(" + networkType + ") - dropped");
Robert Greenwalt4666ed02009-09-10 15:06:20 -0700918 }
919 return false;
The Android Open Source Project28527d22009-03-03 19:31:44 -0800920 }
Robert Greenwalta7dfbd32010-06-15 15:43:39 -0700921 try {
Robert Greenwalt7fe44cb2010-08-27 09:24:29 -0700922 InetAddress addr = InetAddress.getByAddress(hostAddress);
Robert Greenwalta7dfbd32010-06-15 15:43:39 -0700923 return addHostRoute(tracker, addr);
924 } catch (UnknownHostException e) {}
925 return false;
Irfan Sheriff7f132d92010-06-09 15:39:36 -0700926 }
927
928 /**
929 * Ensure that a network route exists to deliver traffic to the specified
930 * host via the mobile data network.
931 * @param hostAddress the IP address of the host to which the route is desired,
932 * in network byte order.
Robert Greenwalta7dfbd32010-06-15 15:43:39 -0700933 * TODO - deprecate
Irfan Sheriff7f132d92010-06-09 15:39:36 -0700934 * @return {@code true} on success, {@code false} on failure
935 */
Robert Greenwalta7dfbd32010-06-15 15:43:39 -0700936 private boolean addHostRoute(NetworkStateTracker nt, InetAddress hostAddress) {
Irfan Sheriff7f132d92010-06-09 15:39:36 -0700937 if (nt.getNetworkInfo().getType() == ConnectivityManager.TYPE_WIFI) {
938 return false;
939 }
940
Robert Greenwalt1f1bcfe2010-08-30 10:56:47 -0700941 LinkProperties p = nt.getLinkProperties();
Robert Greenwalta7dfbd32010-06-15 15:43:39 -0700942 if (p == null) return false;
943 String interfaceName = p.getInterfaceName();
Irfan Sheriff7f132d92010-06-09 15:39:36 -0700944
945 if (DBG) {
Wink Savillee70c6f52010-12-03 12:01:38 -0800946 log("Requested host route to " + hostAddress + "(" + interfaceName + ")");
Irfan Sheriff7f132d92010-06-09 15:39:36 -0700947 }
Robert Greenwalta7dfbd32010-06-15 15:43:39 -0700948 if (interfaceName != null) {
Robert Greenwalt7fe44cb2010-08-27 09:24:29 -0700949 return NetworkUtils.addHostRoute(interfaceName, hostAddress, null);
Irfan Sheriff7f132d92010-06-09 15:39:36 -0700950 } else {
Wink Savillee70c6f52010-12-03 12:01:38 -0800951 if (DBG) loge("addHostRoute failed due to null interface name");
Irfan Sheriff7f132d92010-06-09 15:39:36 -0700952 return false;
953 }
The Android Open Source Project28527d22009-03-03 19:31:44 -0800954 }
955
956 /**
957 * @see ConnectivityManager#getBackgroundDataSetting()
958 */
959 public boolean getBackgroundDataSetting() {
Robert Greenwaltd62c7002010-12-29 16:15:02 -0800960 return mBackgroundDataEnabled.get();
The Android Open Source Project28527d22009-03-03 19:31:44 -0800961 }
Robert Greenwalt0659da32009-07-16 17:21:39 -0700962
The Android Open Source Project28527d22009-03-03 19:31:44 -0800963 /**
964 * @see ConnectivityManager#setBackgroundDataSetting(boolean)
965 */
966 public void setBackgroundDataSetting(boolean allowBackgroundDataUsage) {
967 mContext.enforceCallingOrSelfPermission(
968 android.Manifest.permission.CHANGE_BACKGROUND_DATA_SETTING,
969 "ConnectivityService");
Robert Greenwalt0659da32009-07-16 17:21:39 -0700970
Robert Greenwaltd62c7002010-12-29 16:15:02 -0800971 mBackgroundDataEnabled.set(allowBackgroundDataUsage);
972
Robert Greenwalt6a2db8a2010-09-23 10:05:56 -0700973 mHandler.sendMessage(mHandler.obtainMessage(EVENT_SET_BACKGROUND_DATA,
974 (allowBackgroundDataUsage ? ENABLED : DISABLED), 0));
975 }
The Android Open Source Project28527d22009-03-03 19:31:44 -0800976
Robert Greenwalt6a2db8a2010-09-23 10:05:56 -0700977 private void handleSetBackgroundData(boolean enabled) {
Robert Greenwalt0ffdef12011-02-25 13:44:09 -0800978 Settings.Secure.putInt(mContext.getContentResolver(),
979 Settings.Secure.BACKGROUND_DATA, enabled ? 1 : 0);
980 Intent broadcast = new Intent(
981 ConnectivityManager.ACTION_BACKGROUND_DATA_SETTING_CHANGED);
982 mContext.sendBroadcast(broadcast);
Robert Greenwalt0659da32009-07-16 17:21:39 -0700983 }
984
Robert Greenwalt1b21f6c2010-02-23 18:58:05 -0800985 /**
986 * @see ConnectivityManager#getMobileDataEnabled()
987 */
988 public boolean getMobileDataEnabled() {
Wink Savilleb9024c62010-12-07 10:31:02 -0800989 // TODO: This detail should probably be in DataConnectionTracker's
990 // which is where we store the value and maybe make this
991 // asynchronous.
Robert Greenwalt1b21f6c2010-02-23 18:58:05 -0800992 enforceAccessPermission();
993 boolean retVal = Settings.Secure.getInt(mContext.getContentResolver(),
994 Settings.Secure.MOBILE_DATA, 1) == 1;
Wink Savillee70c6f52010-12-03 12:01:38 -0800995 if (DBG) log("getMobileDataEnabled returning " + retVal);
Robert Greenwalt1b21f6c2010-02-23 18:58:05 -0800996 return retVal;
997 }
998
Robert Greenwalt34848c02011-03-25 13:09:25 -0700999 public void setDataDependency(int networkType, boolean met) {
1000 enforceChangePermission();
1001 if (DBG) {
1002 log("setDataDependency(" + networkType + ", " + met + ")");
1003 }
1004 mHandler.sendMessage(mHandler.obtainMessage(EVENT_SET_DEPENDENCY_MET,
1005 (met ? ENABLED : DISABLED), networkType));
1006 }
1007
1008 private void handleSetDependencyMet(int networkType, boolean met) {
1009 if (mNetTrackers[networkType] != null) {
1010 if (DBG) {
1011 log("handleSetDependencyMet(" + networkType + ", " + met + ")");
1012 }
1013 mNetTrackers[networkType].setDependencyMet(met);
1014 }
1015 }
1016
Robert Greenwalt1b21f6c2010-02-23 18:58:05 -08001017 /**
1018 * @see ConnectivityManager#setMobileDataEnabled(boolean)
1019 */
Robert Greenwalt6a2db8a2010-09-23 10:05:56 -07001020 public void setMobileDataEnabled(boolean enabled) {
Robert Greenwalt1b21f6c2010-02-23 18:58:05 -08001021 enforceChangePermission();
Wink Savillee70c6f52010-12-03 12:01:38 -08001022 if (DBG) log("setMobileDataEnabled(" + enabled + ")");
Robert Greenwalt1b21f6c2010-02-23 18:58:05 -08001023
Robert Greenwalt6a2db8a2010-09-23 10:05:56 -07001024 mHandler.sendMessage(mHandler.obtainMessage(EVENT_SET_MOBILE_DATA,
Robert Greenwalt34848c02011-03-25 13:09:25 -07001025 (enabled ? ENABLED : DISABLED), 0));
Robert Greenwalt6a2db8a2010-09-23 10:05:56 -07001026 }
1027
1028 private void handleSetMobileData(boolean enabled) {
Wink Savilleb9024c62010-12-07 10:31:02 -08001029 if (mNetTrackers[ConnectivityManager.TYPE_MOBILE] != null) {
1030 if (DBG) {
1031 Slog.d(TAG, mNetTrackers[ConnectivityManager.TYPE_MOBILE].toString() + enabled);
Robert Greenwalt1b21f6c2010-02-23 18:58:05 -08001032 }
Wink Savilleb9024c62010-12-07 10:31:02 -08001033 mNetTrackers[ConnectivityManager.TYPE_MOBILE].setDataEnable(enabled);
Robert Greenwalt1b21f6c2010-02-23 18:58:05 -08001034 }
1035 }
1036
The Android Open Source Project28527d22009-03-03 19:31:44 -08001037 private void enforceAccessPermission() {
Robert Greenwalt0659da32009-07-16 17:21:39 -07001038 mContext.enforceCallingOrSelfPermission(
1039 android.Manifest.permission.ACCESS_NETWORK_STATE,
1040 "ConnectivityService");
The Android Open Source Project28527d22009-03-03 19:31:44 -08001041 }
1042
1043 private void enforceChangePermission() {
Robert Greenwalt0659da32009-07-16 17:21:39 -07001044 mContext.enforceCallingOrSelfPermission(
1045 android.Manifest.permission.CHANGE_NETWORK_STATE,
1046 "ConnectivityService");
The Android Open Source Project28527d22009-03-03 19:31:44 -08001047 }
1048
Robert Greenwalt0c4828c2010-01-26 11:40:34 -08001049 // TODO Make this a special check when it goes public
1050 private void enforceTetherChangePermission() {
1051 mContext.enforceCallingOrSelfPermission(
1052 android.Manifest.permission.CHANGE_NETWORK_STATE,
1053 "ConnectivityService");
1054 }
1055
Robert Greenwalt8e87f122010-02-11 18:18:40 -08001056 private void enforceTetherAccessPermission() {
1057 mContext.enforceCallingOrSelfPermission(
1058 android.Manifest.permission.ACCESS_NETWORK_STATE,
1059 "ConnectivityService");
1060 }
1061
Robert Greenwalt93dc1042010-06-15 12:19:37 -07001062 private void enforceConnectivityInternalPermission() {
1063 mContext.enforceCallingOrSelfPermission(
1064 android.Manifest.permission.CONNECTIVITY_INTERNAL,
1065 "ConnectivityService");
1066 }
1067
The Android Open Source Project28527d22009-03-03 19:31:44 -08001068 /**
Robert Greenwalt0659da32009-07-16 17:21:39 -07001069 * Handle a {@code DISCONNECTED} event. If this pertains to the non-active
1070 * network, we ignore it. If it is for the active network, we send out a
1071 * broadcast. But first, we check whether it might be possible to connect
1072 * to a different network.
The Android Open Source Project28527d22009-03-03 19:31:44 -08001073 * @param info the {@code NetworkInfo} for the network
1074 */
1075 private void handleDisconnect(NetworkInfo info) {
1076
Robert Greenwalt2034b912009-08-12 16:08:25 -07001077 int prevNetType = info.getType();
The Android Open Source Project28527d22009-03-03 19:31:44 -08001078
Robert Greenwalt2034b912009-08-12 16:08:25 -07001079 mNetTrackers[prevNetType].setTeardownRequested(false);
The Android Open Source Project28527d22009-03-03 19:31:44 -08001080 /*
1081 * If the disconnected network is not the active one, then don't report
1082 * this as a loss of connectivity. What probably happened is that we're
1083 * getting the disconnect for a network that we explicitly disabled
1084 * in accordance with network preference policies.
1085 */
Robert Greenwalt34848c02011-03-25 13:09:25 -07001086 if (!mNetConfigs[prevNetType].isDefault()) {
Robert Greenwalt2034b912009-08-12 16:08:25 -07001087 List pids = mNetRequestersPids[prevNetType];
1088 for (int i = 0; i<pids.size(); i++) {
1089 Integer pid = (Integer)pids.get(i);
1090 // will remove them because the net's no longer connected
1091 // need to do this now as only now do we know the pids and
1092 // can properly null things that are no longer referenced.
1093 reassessPidDns(pid.intValue(), false);
The Android Open Source Project28527d22009-03-03 19:31:44 -08001094 }
The Android Open Source Project28527d22009-03-03 19:31:44 -08001095 }
1096
The Android Open Source Project28527d22009-03-03 19:31:44 -08001097 Intent intent = new Intent(ConnectivityManager.CONNECTIVITY_ACTION);
1098 intent.putExtra(ConnectivityManager.EXTRA_NETWORK_INFO, info);
1099 if (info.isFailover()) {
1100 intent.putExtra(ConnectivityManager.EXTRA_IS_FAILOVER, true);
1101 info.setFailover(false);
1102 }
1103 if (info.getReason() != null) {
1104 intent.putExtra(ConnectivityManager.EXTRA_REASON, info.getReason());
1105 }
1106 if (info.getExtraInfo() != null) {
Robert Greenwalt0659da32009-07-16 17:21:39 -07001107 intent.putExtra(ConnectivityManager.EXTRA_EXTRA_INFO,
1108 info.getExtraInfo());
The Android Open Source Project28527d22009-03-03 19:31:44 -08001109 }
Robert Greenwalt2034b912009-08-12 16:08:25 -07001110
Robert Greenwalt34848c02011-03-25 13:09:25 -07001111 if (mNetConfigs[prevNetType].isDefault()) {
Robert Greenwalt4b7c55e2011-01-11 13:56:33 -08001112 tryFailover(prevNetType);
1113 if (mActiveDefaultNetwork != -1) {
1114 NetworkInfo switchTo = mNetTrackers[mActiveDefaultNetwork].getNetworkInfo();
Robert Greenwalt3cc68d32010-01-25 17:54:29 -08001115 intent.putExtra(ConnectivityManager.EXTRA_OTHER_NETWORK_INFO, switchTo);
1116 } else {
Robert Greenwaltb2a9e492010-09-20 18:01:43 -07001117 mDefaultInetConditionPublished = 0; // we're not connected anymore
Robert Greenwalt3cc68d32010-01-25 17:54:29 -08001118 intent.putExtra(ConnectivityManager.EXTRA_NO_CONNECTIVITY, true);
1119 }
Robert Greenwaltf55ced92010-01-20 19:29:41 -08001120 }
Robert Greenwaltb2a9e492010-09-20 18:01:43 -07001121 intent.putExtra(ConnectivityManager.EXTRA_INET_CONDITION, mDefaultInetConditionPublished);
Robert Greenwaltf55ced92010-01-20 19:29:41 -08001122 // do this before we broadcast the change
Robert Greenwalt3afbead2010-07-23 15:46:26 -07001123 handleConnectivityChange(prevNetType);
Robert Greenwaltf55ced92010-01-20 19:29:41 -08001124
1125 sendStickyBroadcast(intent);
1126 /*
1127 * If the failover network is already connected, then immediately send
1128 * out a followup broadcast indicating successful failover
1129 */
Robert Greenwalt4b7c55e2011-01-11 13:56:33 -08001130 if (mActiveDefaultNetwork != -1) {
1131 sendConnectedBroadcast(mNetTrackers[mActiveDefaultNetwork].getNetworkInfo());
Robert Greenwaltf55ced92010-01-20 19:29:41 -08001132 }
1133 }
1134
Robert Greenwalt4b7c55e2011-01-11 13:56:33 -08001135 private void tryFailover(int prevNetType) {
Robert Greenwalt2034b912009-08-12 16:08:25 -07001136 /*
Robert Greenwalt92564852011-01-06 15:41:07 -08001137 * If this is a default network, check if other defaults are available.
1138 * Try to reconnect on all available and let them hash it out when
1139 * more than one connects.
Robert Greenwalt2034b912009-08-12 16:08:25 -07001140 */
Robert Greenwalt34848c02011-03-25 13:09:25 -07001141 if (mNetConfigs[prevNetType].isDefault()) {
Robert Greenwalt2034b912009-08-12 16:08:25 -07001142 if (mActiveDefaultNetwork == prevNetType) {
1143 mActiveDefaultNetwork = -1;
1144 }
1145
Robert Greenwalt4b7c55e2011-01-11 13:56:33 -08001146 // don't signal a reconnect for anything lower or equal priority than our
1147 // current connected default
1148 // TODO - don't filter by priority now - nice optimization but risky
1149// int currentPriority = -1;
1150// if (mActiveDefaultNetwork != -1) {
Robert Greenwalt34848c02011-03-25 13:09:25 -07001151// currentPriority = mNetConfigs[mActiveDefaultNetwork].mPriority;
Robert Greenwalt4b7c55e2011-01-11 13:56:33 -08001152// }
Robert Greenwaltf55ced92010-01-20 19:29:41 -08001153 for (int checkType=0; checkType <= ConnectivityManager.MAX_NETWORK_TYPE; checkType++) {
Robert Greenwaltec05b3c2009-10-30 14:17:42 -07001154 if (checkType == prevNetType) continue;
Robert Greenwalt34848c02011-03-25 13:09:25 -07001155 if (mNetConfigs[checkType] == null) continue;
1156 if (!mNetConfigs[checkType].isDefault()) continue;
Wink Saville72a95b92011-01-26 15:43:49 -08001157
1158// Enabling the isAvailable() optimization caused mobile to not get
1159// selected if it was in the middle of error handling. Specifically
1160// a moble connection that took 30 seconds to complete the DEACTIVATE_DATA_CALL
1161// would not be available and we wouldn't get connected to anything.
1162// So removing the isAvailable() optimization below for now. TODO: This
1163// optimization should work and we need to investigate why it doesn't work.
1164// This could be related to how DEACTIVATE_DATA_CALL is reporting its
1165// complete before it is really complete.
1166// if (!mNetTrackers[checkType].isAvailable()) continue;
1167
Robert Greenwalt34848c02011-03-25 13:09:25 -07001168// if (currentPriority >= mNetConfigs[checkType].mPriority) continue;
Robert Greenwalt2034b912009-08-12 16:08:25 -07001169
Robert Greenwalt4b7c55e2011-01-11 13:56:33 -08001170 NetworkStateTracker checkTracker = mNetTrackers[checkType];
1171 NetworkInfo checkInfo = checkTracker.getNetworkInfo();
1172 if (!checkInfo.isConnectedOrConnecting() || checkTracker.isTeardownRequested()) {
1173 checkInfo.setFailover(true);
1174 checkTracker.reconnect();
Robert Greenwalt2034b912009-08-12 16:08:25 -07001175 }
Robert Greenwalt4b7c55e2011-01-11 13:56:33 -08001176 if (DBG) log("Attempting to switch to " + checkInfo.getTypeName());
Robert Greenwalt2034b912009-08-12 16:08:25 -07001177 }
The Android Open Source Project28527d22009-03-03 19:31:44 -08001178 }
The Android Open Source Project28527d22009-03-03 19:31:44 -08001179 }
1180
1181 private void sendConnectedBroadcast(NetworkInfo info) {
Robert Greenwaltd3401f92010-09-15 17:36:33 -07001182 sendGeneralBroadcast(info, ConnectivityManager.CONNECTIVITY_ACTION);
1183 }
1184
1185 private void sendInetConditionBroadcast(NetworkInfo info) {
1186 sendGeneralBroadcast(info, ConnectivityManager.INET_CONDITION_ACTION);
1187 }
1188
1189 private void sendGeneralBroadcast(NetworkInfo info, String bcastType) {
1190 Intent intent = new Intent(bcastType);
The Android Open Source Project28527d22009-03-03 19:31:44 -08001191 intent.putExtra(ConnectivityManager.EXTRA_NETWORK_INFO, info);
1192 if (info.isFailover()) {
1193 intent.putExtra(ConnectivityManager.EXTRA_IS_FAILOVER, true);
1194 info.setFailover(false);
1195 }
1196 if (info.getReason() != null) {
1197 intent.putExtra(ConnectivityManager.EXTRA_REASON, info.getReason());
1198 }
1199 if (info.getExtraInfo() != null) {
Robert Greenwalt0659da32009-07-16 17:21:39 -07001200 intent.putExtra(ConnectivityManager.EXTRA_EXTRA_INFO,
1201 info.getExtraInfo());
The Android Open Source Project28527d22009-03-03 19:31:44 -08001202 }
Robert Greenwalt986c7412010-09-08 15:24:47 -07001203 intent.putExtra(ConnectivityManager.EXTRA_INET_CONDITION, mDefaultInetConditionPublished);
Mike Lockwoodfde2b762009-08-14 14:18:49 -04001204 sendStickyBroadcast(intent);
The Android Open Source Project28527d22009-03-03 19:31:44 -08001205 }
1206
1207 /**
1208 * Called when an attempt to fail over to another network has failed.
1209 * @param info the {@link NetworkInfo} for the failed network
1210 */
1211 private void handleConnectionFailure(NetworkInfo info) {
1212 mNetTrackers[info.getType()].setTeardownRequested(false);
The Android Open Source Project28527d22009-03-03 19:31:44 -08001213
Robert Greenwalt2034b912009-08-12 16:08:25 -07001214 String reason = info.getReason();
1215 String extraInfo = info.getExtraInfo();
Robert Greenwalt0659da32009-07-16 17:21:39 -07001216
Robert Greenwalte981bc52010-10-08 16:35:52 -07001217 String reasonText;
1218 if (reason == null) {
1219 reasonText = ".";
1220 } else {
1221 reasonText = " (" + reason + ").";
The Android Open Source Project28527d22009-03-03 19:31:44 -08001222 }
Wink Savillee70c6f52010-12-03 12:01:38 -08001223 loge("Attempt to connect to " + info.getTypeName() + " failed" + reasonText);
Robert Greenwalt2034b912009-08-12 16:08:25 -07001224
1225 Intent intent = new Intent(ConnectivityManager.CONNECTIVITY_ACTION);
1226 intent.putExtra(ConnectivityManager.EXTRA_NETWORK_INFO, info);
1227 if (getActiveNetworkInfo() == null) {
1228 intent.putExtra(ConnectivityManager.EXTRA_NO_CONNECTIVITY, true);
1229 }
1230 if (reason != null) {
1231 intent.putExtra(ConnectivityManager.EXTRA_REASON, reason);
1232 }
1233 if (extraInfo != null) {
1234 intent.putExtra(ConnectivityManager.EXTRA_EXTRA_INFO, extraInfo);
1235 }
1236 if (info.isFailover()) {
1237 intent.putExtra(ConnectivityManager.EXTRA_IS_FAILOVER, true);
1238 info.setFailover(false);
1239 }
Robert Greenwaltf55ced92010-01-20 19:29:41 -08001240
Robert Greenwalt34848c02011-03-25 13:09:25 -07001241 if (mNetConfigs[info.getType()].isDefault()) {
Robert Greenwalt4b7c55e2011-01-11 13:56:33 -08001242 tryFailover(info.getType());
1243 if (mActiveDefaultNetwork != -1) {
1244 NetworkInfo switchTo = mNetTrackers[mActiveDefaultNetwork].getNetworkInfo();
Robert Greenwalt3cc68d32010-01-25 17:54:29 -08001245 intent.putExtra(ConnectivityManager.EXTRA_OTHER_NETWORK_INFO, switchTo);
1246 } else {
Robert Greenwaltb2a9e492010-09-20 18:01:43 -07001247 mDefaultInetConditionPublished = 0;
Robert Greenwalt3cc68d32010-01-25 17:54:29 -08001248 intent.putExtra(ConnectivityManager.EXTRA_NO_CONNECTIVITY, true);
1249 }
Robert Greenwaltf55ced92010-01-20 19:29:41 -08001250 }
Robert Greenwalt3cc68d32010-01-25 17:54:29 -08001251
Robert Greenwaltb2a9e492010-09-20 18:01:43 -07001252 intent.putExtra(ConnectivityManager.EXTRA_INET_CONDITION, mDefaultInetConditionPublished);
Mike Lockwoodfde2b762009-08-14 14:18:49 -04001253 sendStickyBroadcast(intent);
Robert Greenwaltf55ced92010-01-20 19:29:41 -08001254 /*
1255 * If the failover network is already connected, then immediately send
1256 * out a followup broadcast indicating successful failover
1257 */
Robert Greenwalt4b7c55e2011-01-11 13:56:33 -08001258 if (mActiveDefaultNetwork != -1) {
1259 sendConnectedBroadcast(mNetTrackers[mActiveDefaultNetwork].getNetworkInfo());
Robert Greenwaltf55ced92010-01-20 19:29:41 -08001260 }
Mike Lockwoodfde2b762009-08-14 14:18:49 -04001261 }
1262
1263 private void sendStickyBroadcast(Intent intent) {
1264 synchronized(this) {
Dianne Hackborna417ff82009-12-08 19:45:14 -08001265 if (!mSystemReady) {
1266 mInitialBroadcast = new Intent(intent);
Mike Lockwoodfde2b762009-08-14 14:18:49 -04001267 }
Dianne Hackborna417ff82009-12-08 19:45:14 -08001268 intent.addFlags(Intent.FLAG_RECEIVER_REGISTERED_ONLY_BEFORE_BOOT);
1269 mContext.sendStickyBroadcast(intent);
Mike Lockwoodfde2b762009-08-14 14:18:49 -04001270 }
1271 }
1272
1273 void systemReady() {
1274 synchronized(this) {
1275 mSystemReady = true;
Dianne Hackborna417ff82009-12-08 19:45:14 -08001276 if (mInitialBroadcast != null) {
1277 mContext.sendStickyBroadcast(mInitialBroadcast);
1278 mInitialBroadcast = null;
Mike Lockwoodfde2b762009-08-14 14:18:49 -04001279 }
1280 }
Robert Greenwaltc3c5f862010-10-11 16:00:27 -07001281 // load the global proxy at startup
1282 mHandler.sendMessage(mHandler.obtainMessage(EVENT_APPLY_GLOBAL_HTTP_PROXY));
The Android Open Source Project28527d22009-03-03 19:31:44 -08001283 }
1284
1285 private void handleConnect(NetworkInfo info) {
Robert Greenwalt2034b912009-08-12 16:08:25 -07001286 int type = info.getType();
The Android Open Source Project28527d22009-03-03 19:31:44 -08001287
1288 // snapshot isFailover, because sendConnectedBroadcast() resets it
1289 boolean isFailover = info.isFailover();
Robert Greenwalt2034b912009-08-12 16:08:25 -07001290 NetworkStateTracker thisNet = mNetTrackers[type];
The Android Open Source Project28527d22009-03-03 19:31:44 -08001291
Robert Greenwalt2034b912009-08-12 16:08:25 -07001292 // if this is a default net and other default is running
1293 // kill the one not preferred
Robert Greenwalt34848c02011-03-25 13:09:25 -07001294 if (mNetConfigs[type].isDefault()) {
Robert Greenwalt2034b912009-08-12 16:08:25 -07001295 if (mActiveDefaultNetwork != -1 && mActiveDefaultNetwork != type) {
1296 if ((type != mNetworkPreference &&
Wink Savillef2a62832011-04-07 14:23:45 -07001297 mNetConfigs[mActiveDefaultNetwork].priority >
1298 mNetConfigs[type].priority) ||
Robert Greenwalt2034b912009-08-12 16:08:25 -07001299 mNetworkPreference == mActiveDefaultNetwork) {
1300 // don't accept this one
Wink Savillee70c6f52010-12-03 12:01:38 -08001301 if (DBG) {
1302 log("Not broadcasting CONNECT_ACTION " +
Robert Greenwalt2034b912009-08-12 16:08:25 -07001303 "to torn down network " + info.getTypeName());
Wink Savillee70c6f52010-12-03 12:01:38 -08001304 }
Robert Greenwalt2034b912009-08-12 16:08:25 -07001305 teardown(thisNet);
1306 return;
1307 } else {
1308 // tear down the other
1309 NetworkStateTracker otherNet =
1310 mNetTrackers[mActiveDefaultNetwork];
Wink Savillee70c6f52010-12-03 12:01:38 -08001311 if (DBG) {
1312 log("Policy requires " + otherNet.getNetworkInfo().getTypeName() +
Robert Greenwalt2034b912009-08-12 16:08:25 -07001313 " teardown");
Wink Savillee70c6f52010-12-03 12:01:38 -08001314 }
Robert Greenwalt2034b912009-08-12 16:08:25 -07001315 if (!teardown(otherNet)) {
Wink Savillee70c6f52010-12-03 12:01:38 -08001316 loge("Network declined teardown request");
Robert Greenwalt2034b912009-08-12 16:08:25 -07001317 return;
1318 }
Robert Greenwalt93dc1042010-06-15 12:19:37 -07001319 }
1320 }
1321 synchronized (ConnectivityService.this) {
1322 // have a new default network, release the transition wakelock in a second
1323 // if it's held. The second pause is to allow apps to reconnect over the
1324 // new network
1325 if (mNetTransitionWakeLock.isHeld()) {
1326 mHandler.sendMessageDelayed(mHandler.obtainMessage(
Robert Greenwaltccb36f92010-09-24 14:32:21 -07001327 EVENT_CLEAR_NET_TRANSITION_WAKELOCK,
Robert Greenwalt93dc1042010-06-15 12:19:37 -07001328 mNetTransitionWakeLockSerialNumber, 0),
1329 1000);
Robert Greenwalt2034b912009-08-12 16:08:25 -07001330 }
1331 }
1332 mActiveDefaultNetwork = type;
Robert Greenwalt986c7412010-09-08 15:24:47 -07001333 // this will cause us to come up initially as unconnected and switching
1334 // to connected after our normal pause unless somebody reports us as reall
1335 // disconnected
1336 mDefaultInetConditionPublished = 0;
1337 mDefaultConnectionSequence++;
1338 mInetConditionChangeInFlight = false;
1339 // Don't do this - if we never sign in stay, grey
1340 //reportNetworkCondition(mActiveDefaultNetwork, 100);
Robert Greenwalt2034b912009-08-12 16:08:25 -07001341 }
The Android Open Source Project28527d22009-03-03 19:31:44 -08001342 thisNet.setTeardownRequested(false);
Irfan Sheriff7f132d92010-06-09 15:39:36 -07001343 updateNetworkSettings(thisNet);
Robert Greenwalt3afbead2010-07-23 15:46:26 -07001344 handleConnectivityChange(type);
Robert Greenwalt2034b912009-08-12 16:08:25 -07001345 sendConnectedBroadcast(info);
The Android Open Source Project28527d22009-03-03 19:31:44 -08001346 }
1347
The Android Open Source Project28527d22009-03-03 19:31:44 -08001348 /**
Robert Greenwalt3afbead2010-07-23 15:46:26 -07001349 * After a change in the connectivity state of a network. We're mainly
1350 * concerned with making sure that the list of DNS servers is set up
1351 * according to which networks are connected, and ensuring that the
1352 * right routing table entries exist.
The Android Open Source Project28527d22009-03-03 19:31:44 -08001353 */
Robert Greenwalt3afbead2010-07-23 15:46:26 -07001354 private void handleConnectivityChange(int netType) {
The Android Open Source Project28527d22009-03-03 19:31:44 -08001355 /*
Robert Greenwalt2034b912009-08-12 16:08:25 -07001356 * If a non-default network is enabled, add the host routes that
Robert Greenwalt3afbead2010-07-23 15:46:26 -07001357 * will allow it's DNS servers to be accessed.
The Android Open Source Project28527d22009-03-03 19:31:44 -08001358 */
Robert Greenwalt3afbead2010-07-23 15:46:26 -07001359 handleDnsConfigurationChange(netType);
Robert Greenwalt2034b912009-08-12 16:08:25 -07001360
Robert Greenwalt3afbead2010-07-23 15:46:26 -07001361 if (mNetTrackers[netType].getNetworkInfo().isConnected()) {
Robert Greenwalt34848c02011-03-25 13:09:25 -07001362 if (mNetConfigs[netType].isDefault()) {
Robert Greenwaltc3c5f862010-10-11 16:00:27 -07001363 handleApplyDefaultProxy(netType);
Robert Greenwalt3afbead2010-07-23 15:46:26 -07001364 addDefaultRoute(mNetTrackers[netType]);
Robert Greenwalt2034b912009-08-12 16:08:25 -07001365 } else {
Robert Greenwalt3afbead2010-07-23 15:46:26 -07001366 addPrivateDnsRoutes(mNetTrackers[netType]);
1367 }
1368 } else {
Robert Greenwalt34848c02011-03-25 13:09:25 -07001369 if (mNetConfigs[netType].isDefault()) {
Robert Greenwalt3afbead2010-07-23 15:46:26 -07001370 removeDefaultRoute(mNetTrackers[netType]);
1371 } else {
1372 removePrivateDnsRoutes(mNetTrackers[netType]);
Robert Greenwalt2034b912009-08-12 16:08:25 -07001373 }
The Android Open Source Project28527d22009-03-03 19:31:44 -08001374 }
1375 }
1376
Irfan Sheriff7f132d92010-06-09 15:39:36 -07001377 private void addPrivateDnsRoutes(NetworkStateTracker nt) {
Irfan Sheriff7f132d92010-06-09 15:39:36 -07001378 boolean privateDnsRouteSet = nt.isPrivateDnsRouteSet();
Robert Greenwalt1f1bcfe2010-08-30 10:56:47 -07001379 LinkProperties p = nt.getLinkProperties();
Robert Greenwalta7dfbd32010-06-15 15:43:39 -07001380 if (p == null) return;
1381 String interfaceName = p.getInterfaceName();
Irfan Sheriff7f132d92010-06-09 15:39:36 -07001382
1383 if (DBG) {
Wink Savillee70c6f52010-12-03 12:01:38 -08001384 log("addPrivateDnsRoutes for " + nt +
Irfan Sheriff7f132d92010-06-09 15:39:36 -07001385 "(" + interfaceName + ") - mPrivateDnsRouteSet = " + privateDnsRouteSet);
1386 }
Irfan Sheriff7f132d92010-06-09 15:39:36 -07001387 if (interfaceName != null && !privateDnsRouteSet) {
Robert Greenwalta7dfbd32010-06-15 15:43:39 -07001388 Collection<InetAddress> dnsList = p.getDnses();
1389 for (InetAddress dns : dnsList) {
Wink Savillee70c6f52010-12-03 12:01:38 -08001390 if (DBG) log(" adding " + dns);
Robert Greenwalt7fe44cb2010-08-27 09:24:29 -07001391 NetworkUtils.addHostRoute(interfaceName, dns, null);
Irfan Sheriff7f132d92010-06-09 15:39:36 -07001392 }
1393 nt.privateDnsRouteSet(true);
1394 }
1395 }
1396
1397 private void removePrivateDnsRoutes(NetworkStateTracker nt) {
1398 // TODO - we should do this explicitly but the NetUtils api doesnt
1399 // support this yet - must remove all. No worse than before
Robert Greenwalt1f1bcfe2010-08-30 10:56:47 -07001400 LinkProperties p = nt.getLinkProperties();
Robert Greenwalta7dfbd32010-06-15 15:43:39 -07001401 if (p == null) return;
1402 String interfaceName = p.getInterfaceName();
Irfan Sheriff7f132d92010-06-09 15:39:36 -07001403 boolean privateDnsRouteSet = nt.isPrivateDnsRouteSet();
1404 if (interfaceName != null && privateDnsRouteSet) {
1405 if (DBG) {
Wink Savillee70c6f52010-12-03 12:01:38 -08001406 log("removePrivateDnsRoutes for " + nt.getNetworkInfo().getTypeName() +
Irfan Sheriff7f132d92010-06-09 15:39:36 -07001407 " (" + interfaceName + ")");
1408 }
1409 NetworkUtils.removeHostRoutes(interfaceName);
1410 nt.privateDnsRouteSet(false);
1411 }
1412 }
1413
Irfan Sheriff7f132d92010-06-09 15:39:36 -07001414
1415 private void addDefaultRoute(NetworkStateTracker nt) {
Robert Greenwalt1f1bcfe2010-08-30 10:56:47 -07001416 LinkProperties p = nt.getLinkProperties();
Robert Greenwalta7dfbd32010-06-15 15:43:39 -07001417 if (p == null) return;
1418 String interfaceName = p.getInterfaceName();
Robert Greenwalt5c733972011-02-09 13:56:06 -08001419 if (TextUtils.isEmpty(interfaceName)) return;
1420 for (InetAddress gateway : p.getGateways()) {
Irfan Sheriff7f132d92010-06-09 15:39:36 -07001421
Robert Greenwalt03d53da2011-03-22 18:47:42 -07001422 if (NetworkUtils.addHostRoute(interfaceName, gateway, null) &&
1423 NetworkUtils.addDefaultRoute(interfaceName, gateway)) {
1424 if (DBG) {
1425 NetworkInfo networkInfo = nt.getNetworkInfo();
1426 log("addDefaultRoute for " + networkInfo.getTypeName() +
1427 " (" + interfaceName + "), GatewayAddr=" + gateway.getHostAddress());
1428 }
Irfan Sheriff7f132d92010-06-09 15:39:36 -07001429 }
Irfan Sheriff7f132d92010-06-09 15:39:36 -07001430 }
1431 }
1432
1433
1434 public void removeDefaultRoute(NetworkStateTracker nt) {
Robert Greenwalt1f1bcfe2010-08-30 10:56:47 -07001435 LinkProperties p = nt.getLinkProperties();
Robert Greenwalta7dfbd32010-06-15 15:43:39 -07001436 if (p == null) return;
1437 String interfaceName = p.getInterfaceName();
Irfan Sheriff7f132d92010-06-09 15:39:36 -07001438
Robert Greenwalt3afbead2010-07-23 15:46:26 -07001439 if (interfaceName != null) {
Robert Greenwalt03d53da2011-03-22 18:47:42 -07001440 if (NetworkUtils.removeDefaultRoute(interfaceName) >= 0) {
1441 if (DBG) {
1442 NetworkInfo networkInfo = nt.getNetworkInfo();
1443 log("removeDefaultRoute for " + networkInfo.getTypeName() + " (" +
1444 interfaceName + ")");
1445 }
Irfan Sheriff7f132d92010-06-09 15:39:36 -07001446 }
Irfan Sheriff7f132d92010-06-09 15:39:36 -07001447 }
1448 }
1449
1450 /**
1451 * Reads the network specific TCP buffer sizes from SystemProperties
1452 * net.tcp.buffersize.[default|wifi|umts|edge|gprs] and set them for system
1453 * wide use
1454 */
1455 public void updateNetworkSettings(NetworkStateTracker nt) {
1456 String key = nt.getTcpBufferSizesPropName();
1457 String bufferSizes = SystemProperties.get(key);
1458
1459 if (bufferSizes.length() == 0) {
Wink Savillee70c6f52010-12-03 12:01:38 -08001460 loge(key + " not found in system properties. Using defaults");
Irfan Sheriff7f132d92010-06-09 15:39:36 -07001461
1462 // Setting to default values so we won't be stuck to previous values
1463 key = "net.tcp.buffersize.default";
1464 bufferSizes = SystemProperties.get(key);
1465 }
1466
1467 // Set values in kernel
1468 if (bufferSizes.length() != 0) {
1469 if (DBG) {
Wink Savillee70c6f52010-12-03 12:01:38 -08001470 log("Setting TCP values: [" + bufferSizes
Irfan Sheriff7f132d92010-06-09 15:39:36 -07001471 + "] which comes from [" + key + "]");
1472 }
1473 setBufferSize(bufferSizes);
1474 }
1475 }
1476
1477 /**
1478 * Writes TCP buffer sizes to /sys/kernel/ipv4/tcp_[r/w]mem_[min/def/max]
1479 * which maps to /proc/sys/net/ipv4/tcp_rmem and tcpwmem
1480 *
1481 * @param bufferSizes in the format of "readMin, readInitial, readMax,
1482 * writeMin, writeInitial, writeMax"
1483 */
1484 private void setBufferSize(String bufferSizes) {
1485 try {
1486 String[] values = bufferSizes.split(",");
1487
1488 if (values.length == 6) {
1489 final String prefix = "/sys/kernel/ipv4/tcp_";
1490 stringToFile(prefix + "rmem_min", values[0]);
1491 stringToFile(prefix + "rmem_def", values[1]);
1492 stringToFile(prefix + "rmem_max", values[2]);
1493 stringToFile(prefix + "wmem_min", values[3]);
1494 stringToFile(prefix + "wmem_def", values[4]);
1495 stringToFile(prefix + "wmem_max", values[5]);
1496 } else {
Wink Savillee70c6f52010-12-03 12:01:38 -08001497 loge("Invalid buffersize string: " + bufferSizes);
Irfan Sheriff7f132d92010-06-09 15:39:36 -07001498 }
1499 } catch (IOException e) {
Wink Savillee70c6f52010-12-03 12:01:38 -08001500 loge("Can't set tcp buffer sizes:" + e);
Irfan Sheriff7f132d92010-06-09 15:39:36 -07001501 }
1502 }
1503
1504 /**
1505 * Writes string to file. Basically same as "echo -n $string > $filename"
1506 *
1507 * @param filename
1508 * @param string
1509 * @throws IOException
1510 */
1511 private void stringToFile(String filename, String string) throws IOException {
1512 FileWriter out = new FileWriter(filename);
1513 try {
1514 out.write(string);
1515 } finally {
1516 out.close();
1517 }
1518 }
1519
1520
Robert Greenwalt2034b912009-08-12 16:08:25 -07001521 /**
1522 * Adjust the per-process dns entries (net.dns<x>.<pid>) based
1523 * on the highest priority active net which this process requested.
1524 * If there aren't any, clear it out
1525 */
1526 private void reassessPidDns(int myPid, boolean doBump)
1527 {
Wink Savillee70c6f52010-12-03 12:01:38 -08001528 if (DBG) log("reassessPidDns for pid " + myPid);
Robert Greenwalt2034b912009-08-12 16:08:25 -07001529 for(int i : mPriorityList) {
Robert Greenwalt34848c02011-03-25 13:09:25 -07001530 if (mNetConfigs[i].isDefault()) {
Robert Greenwalt2034b912009-08-12 16:08:25 -07001531 continue;
1532 }
1533 NetworkStateTracker nt = mNetTrackers[i];
Robert Greenwalt0659da32009-07-16 17:21:39 -07001534 if (nt.getNetworkInfo().isConnected() &&
1535 !nt.isTeardownRequested()) {
Robert Greenwalt1f1bcfe2010-08-30 10:56:47 -07001536 LinkProperties p = nt.getLinkProperties();
Robert Greenwalta7dfbd32010-06-15 15:43:39 -07001537 if (p == null) continue;
Robert Greenwalt2034b912009-08-12 16:08:25 -07001538 List pids = mNetRequestersPids[i];
1539 for (int j=0; j<pids.size(); j++) {
1540 Integer pid = (Integer)pids.get(j);
1541 if (pid.intValue() == myPid) {
Robert Greenwalta7dfbd32010-06-15 15:43:39 -07001542 Collection<InetAddress> dnses = p.getDnses();
1543 writePidDns(dnses, myPid);
Robert Greenwalt2034b912009-08-12 16:08:25 -07001544 if (doBump) {
1545 bumpDns();
1546 }
1547 return;
1548 }
1549 }
1550 }
1551 }
1552 // nothing found - delete
1553 for (int i = 1; ; i++) {
1554 String prop = "net.dns" + i + "." + myPid;
1555 if (SystemProperties.get(prop).length() == 0) {
1556 if (doBump) {
1557 bumpDns();
1558 }
1559 return;
1560 }
1561 SystemProperties.set(prop, "");
1562 }
1563 }
1564
Robert Greenwalt8ca88762010-12-17 15:20:36 -08001565 // return true if results in a change
1566 private boolean writePidDns(Collection <InetAddress> dnses, int pid) {
Robert Greenwalt2034b912009-08-12 16:08:25 -07001567 int j = 1;
Robert Greenwalt8ca88762010-12-17 15:20:36 -08001568 boolean changed = false;
Robert Greenwalta7dfbd32010-06-15 15:43:39 -07001569 for (InetAddress dns : dnses) {
Robert Greenwalt8ca88762010-12-17 15:20:36 -08001570 String dnsString = dns.getHostAddress();
1571 if (changed || !dnsString.equals(SystemProperties.get("net.dns" + j + "." + pid))) {
1572 changed = true;
1573 SystemProperties.set("net.dns" + j++ + "." + pid, dns.getHostAddress());
1574 }
Robert Greenwalt2034b912009-08-12 16:08:25 -07001575 }
Robert Greenwalt8ca88762010-12-17 15:20:36 -08001576 return changed;
Robert Greenwalt2034b912009-08-12 16:08:25 -07001577 }
1578
1579 private void bumpDns() {
1580 /*
1581 * Bump the property that tells the name resolver library to reread
1582 * the DNS server list from the properties.
1583 */
1584 String propVal = SystemProperties.get("net.dnschange");
1585 int n = 0;
1586 if (propVal.length() != 0) {
1587 try {
1588 n = Integer.parseInt(propVal);
1589 } catch (NumberFormatException e) {}
1590 }
1591 SystemProperties.set("net.dnschange", "" + (n+1));
Robert Greenwalt051642b2010-11-02 14:08:23 -07001592 /*
1593 * Tell the VMs to toss their DNS caches
1594 */
1595 Intent intent = new Intent(Intent.ACTION_CLEAR_DNS_CACHE);
1596 intent.addFlags(Intent.FLAG_RECEIVER_REPLACE_PENDING);
Stan Chesnuttf444f502011-01-05 17:14:03 -08001597 /*
1598 * Connectivity events can happen before boot has completed ...
1599 */
1600 intent.addFlags(Intent.FLAG_RECEIVER_REGISTERED_ONLY_BEFORE_BOOT);
Robert Greenwalt051642b2010-11-02 14:08:23 -07001601 mContext.sendBroadcast(intent);
Robert Greenwalt2034b912009-08-12 16:08:25 -07001602 }
1603
Robert Greenwalt3afbead2010-07-23 15:46:26 -07001604 private void handleDnsConfigurationChange(int netType) {
Robert Greenwalt2034b912009-08-12 16:08:25 -07001605 // add default net's dns entries
Robert Greenwalt3afbead2010-07-23 15:46:26 -07001606 NetworkStateTracker nt = mNetTrackers[netType];
1607 if (nt != null && nt.getNetworkInfo().isConnected() && !nt.isTeardownRequested()) {
Robert Greenwalt1f1bcfe2010-08-30 10:56:47 -07001608 LinkProperties p = nt.getLinkProperties();
Robert Greenwalt3afbead2010-07-23 15:46:26 -07001609 if (p == null) return;
1610 Collection<InetAddress> dnses = p.getDnses();
Robert Greenwalt8ca88762010-12-17 15:20:36 -08001611 boolean changed = false;
Robert Greenwalt34848c02011-03-25 13:09:25 -07001612 if (mNetConfigs[netType].isDefault()) {
Robert Greenwalt3afbead2010-07-23 15:46:26 -07001613 int j = 1;
Robert Greenwalt94daa182010-09-01 11:34:05 -07001614 if (dnses.size() == 0 && mDefaultDns != null) {
Robert Greenwalt8ca88762010-12-17 15:20:36 -08001615 String dnsString = mDefaultDns.getHostAddress();
1616 if (!dnsString.equals(SystemProperties.get("net.dns1"))) {
1617 if (DBG) {
1618 log("no dns provided - using " + dnsString);
1619 }
1620 changed = true;
1621 SystemProperties.set("net.dns1", dnsString);
Robert Greenwalt2034b912009-08-12 16:08:25 -07001622 }
Robert Greenwalt94daa182010-09-01 11:34:05 -07001623 j++;
1624 } else {
1625 for (InetAddress dns : dnses) {
Robert Greenwalt8ca88762010-12-17 15:20:36 -08001626 String dnsString = dns.getHostAddress();
1627 if (!changed && dnsString.equals(SystemProperties.get("net.dns" + j))) {
1628 j++;
1629 continue;
1630 }
Robert Greenwalt94daa182010-09-01 11:34:05 -07001631 if (DBG) {
Wink Savillee70c6f52010-12-03 12:01:38 -08001632 log("adding dns " + dns + " for " +
Robert Greenwalt94daa182010-09-01 11:34:05 -07001633 nt.getNetworkInfo().getTypeName());
1634 }
Robert Greenwalt8ca88762010-12-17 15:20:36 -08001635 changed = true;
1636 SystemProperties.set("net.dns" + j++, dnsString);
Robert Greenwalt94daa182010-09-01 11:34:05 -07001637 }
Robert Greenwalt3afbead2010-07-23 15:46:26 -07001638 }
1639 for (int k=j ; k<mNumDnsEntries; k++) {
Robert Greenwalt8ca88762010-12-17 15:20:36 -08001640 if (changed || !TextUtils.isEmpty(SystemProperties.get("net.dns" + k))) {
1641 if (DBG) log("erasing net.dns" + k);
1642 changed = true;
1643 SystemProperties.set("net.dns" + k, "");
1644 }
Robert Greenwalt3afbead2010-07-23 15:46:26 -07001645 }
1646 mNumDnsEntries = j;
1647 } else {
1648 // set per-pid dns for attached secondary nets
1649 List pids = mNetRequestersPids[netType];
1650 for (int y=0; y< pids.size(); y++) {
1651 Integer pid = (Integer)pids.get(y);
Robert Greenwalt8ca88762010-12-17 15:20:36 -08001652 changed = writePidDns(dnses, pid.intValue());
The Android Open Source Project28527d22009-03-03 19:31:44 -08001653 }
1654 }
Robert Greenwalt8ca88762010-12-17 15:20:36 -08001655 if (changed) bumpDns();
The Android Open Source Project28527d22009-03-03 19:31:44 -08001656 }
Robert Greenwalt2034b912009-08-12 16:08:25 -07001657 }
1658
Robert Greenwalt20f819c2011-05-03 19:02:44 -07001659 private int getRestoreDefaultNetworkDelay(int networkType) {
Robert Greenwalt2034b912009-08-12 16:08:25 -07001660 String restoreDefaultNetworkDelayStr = SystemProperties.get(
1661 NETWORK_RESTORE_DELAY_PROP_NAME);
1662 if(restoreDefaultNetworkDelayStr != null &&
1663 restoreDefaultNetworkDelayStr.length() != 0) {
1664 try {
1665 return Integer.valueOf(restoreDefaultNetworkDelayStr);
1666 } catch (NumberFormatException e) {
1667 }
The Android Open Source Project28527d22009-03-03 19:31:44 -08001668 }
Robert Greenwalt20f819c2011-05-03 19:02:44 -07001669 // if the system property isn't set, use the value for the apn type
1670 int ret = RESTORE_DEFAULT_NETWORK_DELAY;
1671
1672 if ((networkType <= ConnectivityManager.MAX_NETWORK_TYPE) &&
1673 (mNetConfigs[networkType] != null)) {
1674 ret = mNetConfigs[networkType].restoreTime;
1675 }
1676 return ret;
The Android Open Source Project28527d22009-03-03 19:31:44 -08001677 }
1678
1679 @Override
1680 protected void dump(FileDescriptor fd, PrintWriter pw, String[] args) {
Robert Greenwalt0659da32009-07-16 17:21:39 -07001681 if (mContext.checkCallingOrSelfPermission(
1682 android.Manifest.permission.DUMP)
The Android Open Source Project28527d22009-03-03 19:31:44 -08001683 != PackageManager.PERMISSION_GRANTED) {
Robert Greenwalt0659da32009-07-16 17:21:39 -07001684 pw.println("Permission Denial: can't dump ConnectivityService " +
1685 "from from pid=" + Binder.getCallingPid() + ", uid=" +
1686 Binder.getCallingUid());
The Android Open Source Project28527d22009-03-03 19:31:44 -08001687 return;
1688 }
The Android Open Source Project28527d22009-03-03 19:31:44 -08001689 pw.println();
1690 for (NetworkStateTracker nst : mNetTrackers) {
Robert Greenwalt3eeb6032009-12-21 18:24:07 -08001691 if (nst != null) {
1692 if (nst.getNetworkInfo().isConnected()) {
1693 pw.println("Active network: " + nst.getNetworkInfo().
1694 getTypeName());
1695 }
1696 pw.println(nst.getNetworkInfo());
1697 pw.println(nst);
1698 pw.println();
Robert Greenwalt2034b912009-08-12 16:08:25 -07001699 }
The Android Open Source Project28527d22009-03-03 19:31:44 -08001700 }
Robert Greenwalt3eeb6032009-12-21 18:24:07 -08001701
1702 pw.println("Network Requester Pids:");
1703 for (int net : mPriorityList) {
1704 String pidString = net + ": ";
1705 for (Object pid : mNetRequestersPids[net]) {
1706 pidString = pidString + pid.toString() + ", ";
1707 }
1708 pw.println(pidString);
1709 }
1710 pw.println();
1711
1712 pw.println("FeatureUsers:");
1713 for (Object requester : mFeatureUsers) {
1714 pw.println(requester.toString());
1715 }
1716 pw.println();
Robert Greenwalt8e87f122010-02-11 18:18:40 -08001717
Robert Greenwalt93dc1042010-06-15 12:19:37 -07001718 synchronized (this) {
1719 pw.println("NetworkTranstionWakeLock is currently " +
1720 (mNetTransitionWakeLock.isHeld() ? "" : "not ") + "held.");
1721 pw.println("It was last requested for "+mNetTransitionWakeLockCausedBy);
1722 }
1723 pw.println();
1724
Robert Greenwalt8e87f122010-02-11 18:18:40 -08001725 mTethering.dump(fd, pw, args);
Robert Greenwalt0e80be12010-09-20 14:35:25 -07001726
1727 if (mInetLog != null) {
1728 pw.println();
1729 pw.println("Inet condition reports:");
1730 for(int i = 0; i < mInetLog.size(); i++) {
1731 pw.println(mInetLog.get(i));
1732 }
1733 }
The Android Open Source Project28527d22009-03-03 19:31:44 -08001734 }
1735
Robert Greenwalt2034b912009-08-12 16:08:25 -07001736 // must be stateless - things change under us.
The Android Open Source Project28527d22009-03-03 19:31:44 -08001737 private class MyHandler extends Handler {
Wink Saville775aad62010-09-02 19:23:52 -07001738 public MyHandler(Looper looper) {
1739 super(looper);
1740 }
1741
The Android Open Source Project28527d22009-03-03 19:31:44 -08001742 @Override
1743 public void handleMessage(Message msg) {
1744 NetworkInfo info;
1745 switch (msg.what) {
1746 case NetworkStateTracker.EVENT_STATE_CHANGED:
1747 info = (NetworkInfo) msg.obj;
Robert Greenwalt12c44552009-12-07 11:33:18 -08001748 int type = info.getType();
1749 NetworkInfo.State state = info.getState();
Robert Greenwalt12c44552009-12-07 11:33:18 -08001750
Wink Savillee70c6f52010-12-03 12:01:38 -08001751 if (DBG) log("ConnectivityChange for " +
Robert Greenwalt0659da32009-07-16 17:21:39 -07001752 info.getTypeName() + ": " +
Robert Greenwalt12c44552009-12-07 11:33:18 -08001753 state + "/" + info.getDetailedState());
The Android Open Source Project28527d22009-03-03 19:31:44 -08001754
1755 // Connectivity state changed:
1756 // [31-13] Reserved for future use
Robert Greenwalt0659da32009-07-16 17:21:39 -07001757 // [12-9] Network subtype (for mobile network, as defined
1758 // by TelephonyManager)
1759 // [8-3] Detailed state ordinal (as defined by
1760 // NetworkInfo.DetailedState)
The Android Open Source Project28527d22009-03-03 19:31:44 -08001761 // [2-0] Network type (as defined by ConnectivityManager)
1762 int eventLogParam = (info.getType() & 0x7) |
1763 ((info.getDetailedState().ordinal() & 0x3f) << 3) |
1764 (info.getSubtype() << 9);
Doug Zongker2fc96232009-12-04 10:31:43 -08001765 EventLog.writeEvent(EventLogTags.CONNECTIVITY_STATE_CHANGED,
Robert Greenwalt0659da32009-07-16 17:21:39 -07001766 eventLogParam);
1767
1768 if (info.getDetailedState() ==
1769 NetworkInfo.DetailedState.FAILED) {
The Android Open Source Project28527d22009-03-03 19:31:44 -08001770 handleConnectionFailure(info);
Robert Greenwalt12c44552009-12-07 11:33:18 -08001771 } else if (state == NetworkInfo.State.DISCONNECTED) {
The Android Open Source Project28527d22009-03-03 19:31:44 -08001772 handleDisconnect(info);
Robert Greenwalt12c44552009-12-07 11:33:18 -08001773 } else if (state == NetworkInfo.State.SUSPENDED) {
The Android Open Source Project28527d22009-03-03 19:31:44 -08001774 // TODO: need to think this over.
Robert Greenwalt0659da32009-07-16 17:21:39 -07001775 // the logic here is, handle SUSPENDED the same as
1776 // DISCONNECTED. The only difference being we are
1777 // broadcasting an intent with NetworkInfo that's
1778 // suspended. This allows the applications an
1779 // opportunity to handle DISCONNECTED and SUSPENDED
1780 // differently, or not.
The Android Open Source Project28527d22009-03-03 19:31:44 -08001781 handleDisconnect(info);
Robert Greenwalt12c44552009-12-07 11:33:18 -08001782 } else if (state == NetworkInfo.State.CONNECTED) {
The Android Open Source Project28527d22009-03-03 19:31:44 -08001783 handleConnect(info);
1784 }
The Android Open Source Project28527d22009-03-03 19:31:44 -08001785 break;
The Android Open Source Project28527d22009-03-03 19:31:44 -08001786 case NetworkStateTracker.EVENT_CONFIGURATION_CHANGED:
Robert Greenwalt3afbead2010-07-23 15:46:26 -07001787 info = (NetworkInfo) msg.obj;
Robert Greenwalt34848c02011-03-25 13:09:25 -07001788 handleConnectivityChange(info.getType());
The Android Open Source Project28527d22009-03-03 19:31:44 -08001789 break;
Robert Greenwaltccb36f92010-09-24 14:32:21 -07001790 case EVENT_CLEAR_NET_TRANSITION_WAKELOCK:
Robert Greenwalt93dc1042010-06-15 12:19:37 -07001791 String causedBy = null;
1792 synchronized (ConnectivityService.this) {
1793 if (msg.arg1 == mNetTransitionWakeLockSerialNumber &&
1794 mNetTransitionWakeLock.isHeld()) {
1795 mNetTransitionWakeLock.release();
1796 causedBy = mNetTransitionWakeLockCausedBy;
1797 }
1798 }
1799 if (causedBy != null) {
Wink Savillee70c6f52010-12-03 12:01:38 -08001800 log("NetTransition Wakelock for " + causedBy + " released by timeout");
Robert Greenwalt93dc1042010-06-15 12:19:37 -07001801 }
Robert Greenwaltcf1a56c2010-09-09 14:05:10 -07001802 break;
Robert Greenwalt6a2db8a2010-09-23 10:05:56 -07001803 case EVENT_RESTORE_DEFAULT_NETWORK:
Robert Greenwaltaffc3a12009-09-27 17:27:04 -07001804 FeatureUser u = (FeatureUser)msg.obj;
1805 u.expire();
Robert Greenwalt986c7412010-09-08 15:24:47 -07001806 break;
Robert Greenwalt6a2db8a2010-09-23 10:05:56 -07001807 case EVENT_INET_CONDITION_CHANGE:
1808 {
1809 int netType = msg.arg1;
1810 int condition = msg.arg2;
1811 handleInetConditionChange(netType, condition);
Robert Greenwalt93dc1042010-06-15 12:19:37 -07001812 break;
Robert Greenwalt6a2db8a2010-09-23 10:05:56 -07001813 }
1814 case EVENT_INET_CONDITION_HOLD_END:
1815 {
1816 int netType = msg.arg1;
1817 int sequence = msg.arg2;
1818 handleInetConditionHoldEnd(netType, sequence);
Robert Greenwalt986c7412010-09-08 15:24:47 -07001819 break;
Robert Greenwalt6a2db8a2010-09-23 10:05:56 -07001820 }
1821 case EVENT_SET_NETWORK_PREFERENCE:
1822 {
1823 int preference = msg.arg1;
1824 handleSetNetworkPreference(preference);
1825 break;
1826 }
1827 case EVENT_SET_BACKGROUND_DATA:
1828 {
1829 boolean enabled = (msg.arg1 == ENABLED);
1830 handleSetBackgroundData(enabled);
1831 break;
1832 }
1833 case EVENT_SET_MOBILE_DATA:
1834 {
1835 boolean enabled = (msg.arg1 == ENABLED);
1836 handleSetMobileData(enabled);
1837 break;
1838 }
Robert Greenwaltc3c5f862010-10-11 16:00:27 -07001839 case EVENT_APPLY_GLOBAL_HTTP_PROXY:
1840 {
1841 handleDeprecatedGlobalHttpProxy();
Robert Greenwalt34848c02011-03-25 13:09:25 -07001842 break;
1843 }
1844 case EVENT_SET_DEPENDENCY_MET:
1845 {
1846 boolean met = (msg.arg1 == ENABLED);
1847 handleSetDependencyMet(msg.arg2, met);
1848 break;
Robert Greenwaltc3c5f862010-10-11 16:00:27 -07001849 }
The Android Open Source Project28527d22009-03-03 19:31:44 -08001850 }
1851 }
1852 }
Robert Greenwalt0c4828c2010-01-26 11:40:34 -08001853
1854 // javadoc from interface
Robert Greenwalt4283ded2010-03-02 17:25:02 -08001855 public int tether(String iface) {
Robert Greenwalt0c4828c2010-01-26 11:40:34 -08001856 enforceTetherChangePermission();
Robert Greenwalt4283ded2010-03-02 17:25:02 -08001857
1858 if (isTetheringSupported()) {
1859 return mTethering.tether(iface);
1860 } else {
1861 return ConnectivityManager.TETHER_ERROR_UNSUPPORTED;
1862 }
Robert Greenwalt0c4828c2010-01-26 11:40:34 -08001863 }
1864
1865 // javadoc from interface
Robert Greenwalt4283ded2010-03-02 17:25:02 -08001866 public int untether(String iface) {
Robert Greenwalt0c4828c2010-01-26 11:40:34 -08001867 enforceTetherChangePermission();
Robert Greenwalt4283ded2010-03-02 17:25:02 -08001868
1869 if (isTetheringSupported()) {
1870 return mTethering.untether(iface);
1871 } else {
1872 return ConnectivityManager.TETHER_ERROR_UNSUPPORTED;
1873 }
1874 }
1875
1876 // javadoc from interface
1877 public int getLastTetherError(String iface) {
1878 enforceTetherAccessPermission();
1879
1880 if (isTetheringSupported()) {
1881 return mTethering.getLastTetherError(iface);
1882 } else {
1883 return ConnectivityManager.TETHER_ERROR_UNSUPPORTED;
1884 }
Robert Greenwalt8e87f122010-02-11 18:18:40 -08001885 }
1886
1887 // TODO - proper iface API for selection by property, inspection, etc
1888 public String[] getTetherableUsbRegexs() {
1889 enforceTetherAccessPermission();
1890 if (isTetheringSupported()) {
1891 return mTethering.getTetherableUsbRegexs();
1892 } else {
1893 return new String[0];
1894 }
1895 }
1896
1897 public String[] getTetherableWifiRegexs() {
1898 enforceTetherAccessPermission();
1899 if (isTetheringSupported()) {
1900 return mTethering.getTetherableWifiRegexs();
1901 } else {
1902 return new String[0];
1903 }
Robert Greenwalt0c4828c2010-01-26 11:40:34 -08001904 }
1905
Danica Chang96567052010-08-11 14:54:43 -07001906 public String[] getTetherableBluetoothRegexs() {
1907 enforceTetherAccessPermission();
1908 if (isTetheringSupported()) {
1909 return mTethering.getTetherableBluetoothRegexs();
1910 } else {
1911 return new String[0];
1912 }
1913 }
1914
Robert Greenwalt0c4828c2010-01-26 11:40:34 -08001915 // TODO - move iface listing, queries, etc to new module
1916 // javadoc from interface
1917 public String[] getTetherableIfaces() {
Robert Greenwalt8e87f122010-02-11 18:18:40 -08001918 enforceTetherAccessPermission();
Robert Greenwalt0c4828c2010-01-26 11:40:34 -08001919 return mTethering.getTetherableIfaces();
1920 }
1921
1922 public String[] getTetheredIfaces() {
Robert Greenwalt8e87f122010-02-11 18:18:40 -08001923 enforceTetherAccessPermission();
Robert Greenwalt0c4828c2010-01-26 11:40:34 -08001924 return mTethering.getTetheredIfaces();
1925 }
Robert Greenwalt8e87f122010-02-11 18:18:40 -08001926
Robert Greenwalt4283ded2010-03-02 17:25:02 -08001927 public String[] getTetheringErroredIfaces() {
1928 enforceTetherAccessPermission();
1929 return mTethering.getErroredIfaces();
1930 }
1931
Robert Greenwalt8e87f122010-02-11 18:18:40 -08001932 // if ro.tether.denied = true we default to no tethering
1933 // gservices could set the secure setting to 1 though to enable it on a build where it
1934 // had previously been turned off.
1935 public boolean isTetheringSupported() {
1936 enforceTetherAccessPermission();
1937 int defaultVal = (SystemProperties.get("ro.tether.denied").equals("true") ? 0 : 1);
Robert Greenwaltf1b66e12010-02-25 12:29:30 -08001938 boolean tetherEnabledInSettings = (Settings.Secure.getInt(mContext.getContentResolver(),
1939 Settings.Secure.TETHER_SUPPORTED, defaultVal) != 0);
1940 return tetherEnabledInSettings && mTetheringConfigValid;
Robert Greenwalt8e87f122010-02-11 18:18:40 -08001941 }
Robert Greenwalt93dc1042010-06-15 12:19:37 -07001942
1943 // An API NetworkStateTrackers can call when they lose their network.
1944 // This will automatically be cleared after X seconds or a network becomes CONNECTED,
1945 // whichever happens first. The timer is started by the first caller and not
1946 // restarted by subsequent callers.
1947 public void requestNetworkTransitionWakelock(String forWhom) {
1948 enforceConnectivityInternalPermission();
1949 synchronized (this) {
1950 if (mNetTransitionWakeLock.isHeld()) return;
1951 mNetTransitionWakeLockSerialNumber++;
1952 mNetTransitionWakeLock.acquire();
1953 mNetTransitionWakeLockCausedBy = forWhom;
1954 }
1955 mHandler.sendMessageDelayed(mHandler.obtainMessage(
Robert Greenwaltccb36f92010-09-24 14:32:21 -07001956 EVENT_CLEAR_NET_TRANSITION_WAKELOCK,
Robert Greenwalt93dc1042010-06-15 12:19:37 -07001957 mNetTransitionWakeLockSerialNumber, 0),
1958 mNetTransitionWakeLockTimeout);
1959 return;
1960 }
Robert Greenwalt24118e82010-09-09 13:15:32 -07001961
Robert Greenwalt986c7412010-09-08 15:24:47 -07001962 // 100 percent is full good, 0 is full bad.
1963 public void reportInetCondition(int networkType, int percentage) {
Wink Savillee70c6f52010-12-03 12:01:38 -08001964 if (DBG) log("reportNetworkCondition(" + networkType + ", " + percentage + ")");
Robert Greenwalt986c7412010-09-08 15:24:47 -07001965 mContext.enforceCallingOrSelfPermission(
1966 android.Manifest.permission.STATUS_BAR,
1967 "ConnectivityService");
1968
Robert Greenwalt0e80be12010-09-20 14:35:25 -07001969 if (DBG) {
1970 int pid = getCallingPid();
1971 int uid = getCallingUid();
1972 String s = pid + "(" + uid + ") reports inet is " +
1973 (percentage > 50 ? "connected" : "disconnected") + " (" + percentage + ") on " +
1974 "network Type " + networkType + " at " + GregorianCalendar.getInstance().getTime();
1975 mInetLog.add(s);
1976 while(mInetLog.size() > INET_CONDITION_LOG_MAX_SIZE) {
1977 mInetLog.remove(0);
1978 }
1979 }
Robert Greenwalt986c7412010-09-08 15:24:47 -07001980 mHandler.sendMessage(mHandler.obtainMessage(
Robert Greenwalt6a2db8a2010-09-23 10:05:56 -07001981 EVENT_INET_CONDITION_CHANGE, networkType, percentage));
1982 }
1983
1984 private void handleInetConditionChange(int netType, int condition) {
1985 if (DBG) {
Wink Savillee70c6f52010-12-03 12:01:38 -08001986 log("Inet connectivity change, net=" +
Robert Greenwalt6a2db8a2010-09-23 10:05:56 -07001987 netType + ", condition=" + condition +
1988 ",mActiveDefaultNetwork=" + mActiveDefaultNetwork);
1989 }
1990 if (mActiveDefaultNetwork == -1) {
Wink Savillee70c6f52010-12-03 12:01:38 -08001991 if (DBG) log("no active default network - aborting");
Robert Greenwalt6a2db8a2010-09-23 10:05:56 -07001992 return;
1993 }
1994 if (mActiveDefaultNetwork != netType) {
Wink Savillee70c6f52010-12-03 12:01:38 -08001995 if (DBG) log("given net not default - aborting");
Robert Greenwalt6a2db8a2010-09-23 10:05:56 -07001996 return;
1997 }
1998 mDefaultInetCondition = condition;
1999 int delay;
2000 if (mInetConditionChangeInFlight == false) {
Wink Savillee70c6f52010-12-03 12:01:38 -08002001 if (DBG) log("starting a change hold");
Robert Greenwalt6a2db8a2010-09-23 10:05:56 -07002002 // setup a new hold to debounce this
2003 if (mDefaultInetCondition > 50) {
2004 delay = Settings.Secure.getInt(mContext.getContentResolver(),
2005 Settings.Secure.INET_CONDITION_DEBOUNCE_UP_DELAY, 500);
2006 } else {
2007 delay = Settings.Secure.getInt(mContext.getContentResolver(),
2008 Settings.Secure.INET_CONDITION_DEBOUNCE_DOWN_DELAY, 3000);
2009 }
2010 mInetConditionChangeInFlight = true;
2011 mHandler.sendMessageDelayed(mHandler.obtainMessage(EVENT_INET_CONDITION_HOLD_END,
2012 mActiveDefaultNetwork, mDefaultConnectionSequence), delay);
2013 } else {
2014 // we've set the new condition, when this hold ends that will get
2015 // picked up
Wink Savillee70c6f52010-12-03 12:01:38 -08002016 if (DBG) log("currently in hold - not setting new end evt");
Robert Greenwalt6a2db8a2010-09-23 10:05:56 -07002017 }
2018 }
2019
2020 private void handleInetConditionHoldEnd(int netType, int sequence) {
2021 if (DBG) {
Wink Savillee70c6f52010-12-03 12:01:38 -08002022 log("Inet hold end, net=" + netType +
Robert Greenwalt6a2db8a2010-09-23 10:05:56 -07002023 ", condition =" + mDefaultInetCondition +
2024 ", published condition =" + mDefaultInetConditionPublished);
2025 }
2026 mInetConditionChangeInFlight = false;
2027
2028 if (mActiveDefaultNetwork == -1) {
Wink Savillee70c6f52010-12-03 12:01:38 -08002029 if (DBG) log("no active default network - aborting");
Robert Greenwalt6a2db8a2010-09-23 10:05:56 -07002030 return;
2031 }
2032 if (mDefaultConnectionSequence != sequence) {
Wink Savillee70c6f52010-12-03 12:01:38 -08002033 if (DBG) log("event hold for obsolete network - aborting");
Robert Greenwalt6a2db8a2010-09-23 10:05:56 -07002034 return;
2035 }
2036 if (mDefaultInetConditionPublished == mDefaultInetCondition) {
Wink Savillee70c6f52010-12-03 12:01:38 -08002037 if (DBG) log("no change in condition - aborting");
Robert Greenwalt6a2db8a2010-09-23 10:05:56 -07002038 return;
2039 }
2040 NetworkInfo networkInfo = mNetTrackers[mActiveDefaultNetwork].getNetworkInfo();
2041 if (networkInfo.isConnected() == false) {
Wink Savillee70c6f52010-12-03 12:01:38 -08002042 if (DBG) log("default network not connected - aborting");
Robert Greenwalt6a2db8a2010-09-23 10:05:56 -07002043 return;
2044 }
2045 mDefaultInetConditionPublished = mDefaultInetCondition;
2046 sendInetConditionBroadcast(networkInfo);
2047 return;
Robert Greenwalt986c7412010-09-08 15:24:47 -07002048 }
Robert Greenwaltc3c5f862010-10-11 16:00:27 -07002049
2050 public synchronized ProxyProperties getProxy() {
2051 if (mGlobalProxy != null) return mGlobalProxy;
2052 if (mDefaultProxy != null) return mDefaultProxy;
2053 return null;
2054 }
2055
2056 public void setGlobalProxy(ProxyProperties proxyProperties) {
2057 enforceChangePermission();
2058 synchronized (mGlobalProxyLock) {
2059 if (proxyProperties == mGlobalProxy) return;
2060 if (proxyProperties != null && proxyProperties.equals(mGlobalProxy)) return;
2061 if (mGlobalProxy != null && mGlobalProxy.equals(proxyProperties)) return;
2062
2063 String host = "";
2064 int port = 0;
2065 String exclList = "";
2066 if (proxyProperties != null && !TextUtils.isEmpty(proxyProperties.getHost())) {
2067 mGlobalProxy = new ProxyProperties(proxyProperties);
2068 host = mGlobalProxy.getHost();
2069 port = mGlobalProxy.getPort();
2070 exclList = mGlobalProxy.getExclusionList();
2071 } else {
2072 mGlobalProxy = null;
2073 }
2074 ContentResolver res = mContext.getContentResolver();
2075 Settings.Secure.putString(res, Settings.Secure.GLOBAL_HTTP_PROXY_HOST, host);
2076 Settings.Secure.putInt(res, Settings.Secure.GLOBAL_HTTP_PROXY_PORT, port);
Robert Greenwalt6f7c6092010-12-02 11:31:00 -08002077 Settings.Secure.putString(res, Settings.Secure.GLOBAL_HTTP_PROXY_EXCLUSION_LIST,
Robert Greenwaltc3c5f862010-10-11 16:00:27 -07002078 exclList);
2079 }
2080
2081 if (mGlobalProxy == null) {
2082 proxyProperties = mDefaultProxy;
2083 }
2084 sendProxyBroadcast(proxyProperties);
2085 }
2086
Robert Greenwalt6f7c6092010-12-02 11:31:00 -08002087 private void loadGlobalProxy() {
2088 ContentResolver res = mContext.getContentResolver();
2089 String host = Settings.Secure.getString(res, Settings.Secure.GLOBAL_HTTP_PROXY_HOST);
2090 int port = Settings.Secure.getInt(res, Settings.Secure.GLOBAL_HTTP_PROXY_PORT, 0);
2091 String exclList = Settings.Secure.getString(res,
2092 Settings.Secure.GLOBAL_HTTP_PROXY_EXCLUSION_LIST);
2093 if (!TextUtils.isEmpty(host)) {
2094 ProxyProperties proxyProperties = new ProxyProperties(host, port, exclList);
2095 synchronized (mGlobalProxyLock) {
2096 mGlobalProxy = proxyProperties;
2097 }
2098 }
2099 }
2100
Robert Greenwaltc3c5f862010-10-11 16:00:27 -07002101 public ProxyProperties getGlobalProxy() {
2102 synchronized (mGlobalProxyLock) {
2103 return mGlobalProxy;
2104 }
2105 }
2106
2107 private void handleApplyDefaultProxy(int type) {
2108 // check if new default - push it out to all VM if so
2109 ProxyProperties proxy = mNetTrackers[type].getLinkProperties().getHttpProxy();
2110 synchronized (this) {
2111 if (mDefaultProxy != null && mDefaultProxy.equals(proxy)) return;
2112 if (mDefaultProxy == proxy) return;
2113 if (!TextUtils.isEmpty(proxy.getHost())) {
2114 mDefaultProxy = proxy;
2115 } else {
2116 mDefaultProxy = null;
2117 }
2118 }
Wink Savillee70c6f52010-12-03 12:01:38 -08002119 if (DBG) log("changing default proxy to " + proxy);
Robert Greenwaltc3c5f862010-10-11 16:00:27 -07002120 if ((proxy == null && mGlobalProxy == null) || proxy.equals(mGlobalProxy)) return;
2121 if (mGlobalProxy != null) return;
2122 sendProxyBroadcast(proxy);
2123 }
2124
2125 private void handleDeprecatedGlobalHttpProxy() {
2126 String proxy = Settings.Secure.getString(mContext.getContentResolver(),
2127 Settings.Secure.HTTP_PROXY);
2128 if (!TextUtils.isEmpty(proxy)) {
2129 String data[] = proxy.split(":");
2130 String proxyHost = data[0];
2131 int proxyPort = 8080;
2132 if (data.length > 1) {
2133 try {
2134 proxyPort = Integer.parseInt(data[1]);
2135 } catch (NumberFormatException e) {
2136 return;
2137 }
2138 }
2139 ProxyProperties p = new ProxyProperties(data[0], proxyPort, "");
2140 setGlobalProxy(p);
2141 }
2142 }
2143
2144 private void sendProxyBroadcast(ProxyProperties proxy) {
Robert Greenwalt611291c2010-12-23 15:51:10 -08002145 if (proxy == null) proxy = new ProxyProperties("", 0, "");
Wink Savillee70c6f52010-12-03 12:01:38 -08002146 log("sending Proxy Broadcast for " + proxy);
Robert Greenwaltc3c5f862010-10-11 16:00:27 -07002147 Intent intent = new Intent(Proxy.PROXY_CHANGE_ACTION);
Stan Chesnutt1f2a2ac2011-01-06 11:00:19 -08002148 intent.addFlags(Intent.FLAG_RECEIVER_REPLACE_PENDING |
2149 Intent.FLAG_RECEIVER_REGISTERED_ONLY_BEFORE_BOOT);
Robert Greenwaltc3c5f862010-10-11 16:00:27 -07002150 intent.putExtra(Proxy.EXTRA_PROXY_INFO, proxy);
Robert Greenwaltd93dc8f2010-12-06 11:29:17 -08002151 mContext.sendStickyBroadcast(intent);
Robert Greenwaltc3c5f862010-10-11 16:00:27 -07002152 }
2153
2154 private static class SettingsObserver extends ContentObserver {
2155 private int mWhat;
2156 private Handler mHandler;
2157 SettingsObserver(Handler handler, int what) {
2158 super(handler);
2159 mHandler = handler;
2160 mWhat = what;
2161 }
2162
2163 void observe(Context context) {
2164 ContentResolver resolver = context.getContentResolver();
2165 resolver.registerContentObserver(Settings.Secure.getUriFor(
2166 Settings.Secure.HTTP_PROXY), false, this);
2167 }
2168
2169 @Override
2170 public void onChange(boolean selfChange) {
2171 mHandler.obtainMessage(mWhat).sendToTarget();
2172 }
2173 }
Wink Savillee70c6f52010-12-03 12:01:38 -08002174
2175 private void log(String s) {
2176 Slog.d(TAG, s);
2177 }
2178
2179 private void loge(String s) {
2180 Slog.e(TAG, s);
2181 }
Wink Savilleb7c92c72011-03-12 14:52:01 -08002182 int convertFeatureToNetworkType(String feature){
2183 int networkType = -1;
2184 if (TextUtils.equals(feature, Phone.FEATURE_ENABLE_MMS)) {
2185 networkType = ConnectivityManager.TYPE_MOBILE_MMS;
2186 } else if (TextUtils.equals(feature, Phone.FEATURE_ENABLE_SUPL)) {
2187 networkType = ConnectivityManager.TYPE_MOBILE_SUPL;
2188 } else if (TextUtils.equals(feature, Phone.FEATURE_ENABLE_DUN) ||
2189 TextUtils.equals(feature, Phone.FEATURE_ENABLE_DUN_ALWAYS)) {
2190 networkType = ConnectivityManager.TYPE_MOBILE_DUN;
2191 } else if (TextUtils.equals(feature, Phone.FEATURE_ENABLE_HIPRI)) {
2192 networkType = ConnectivityManager.TYPE_MOBILE_HIPRI;
2193 } else if (TextUtils.equals(feature, Phone.FEATURE_ENABLE_FOTA)) {
2194 networkType = ConnectivityManager.TYPE_MOBILE_FOTA;
2195 } else if (TextUtils.equals(feature, Phone.FEATURE_ENABLE_IMS)) {
2196 networkType = ConnectivityManager.TYPE_MOBILE_IMS;
2197 } else if (TextUtils.equals(feature, Phone.FEATURE_ENABLE_CBS)) {
2198 networkType = ConnectivityManager.TYPE_MOBILE_CBS;
2199 }
2200 return networkType;
2201 }
The Android Open Source Project28527d22009-03-03 19:31:44 -08002202}