blob: 998382cbf23106d79270efbe514eb9a5854b9b2b [file] [log] [blame]
The Android Open Source Project9066cfe2009-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 Ganesh15c74392010-12-21 22:31:44 -080019import android.bluetooth.BluetoothTetheringDataTracker;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080020import android.content.ContentResolver;
21import android.content.Context;
22import android.content.Intent;
23import android.content.pm.PackageManager;
Robert Greenwalt434203a2010-10-11 16:00:27 -070024import android.database.ContentObserver;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080025import android.net.ConnectivityManager;
Robert Greenwaltda3d5e62010-12-06 13:56:24 -080026import android.net.DummyDataStateTracker;
Benoit Goby08c39c62010-12-22 14:29:40 -080027import android.net.EthernetDataTracker;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080028import android.net.IConnectivityManager;
Jaikumar Ganesh15c74392010-12-21 22:31:44 -080029import android.net.LinkProperties;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080030import android.net.MobileDataStateTracker;
Robert Greenwaltd55a6b42011-03-25 13:09:25 -070031import android.net.NetworkConfig;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080032import android.net.NetworkInfo;
33import android.net.NetworkStateTracker;
Robert Greenwalt585ac0f2010-08-27 09:24:29 -070034import android.net.NetworkUtils;
Robert Greenwalt434203a2010-10-11 16:00:27 -070035import android.net.Proxy;
36import android.net.ProxyProperties;
Hung-ying Tyan6b818de2011-01-19 16:48:38 +080037import android.net.vpn.VpnManager;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080038import android.net.wifi.WifiStateTracker;
39import android.os.Binder;
40import android.os.Handler;
Wink Savillebb08caf2010-09-02 19:23:52 -070041import android.os.HandlerThread;
Robert Greenwalt42acef32009-08-12 16:08:25 -070042import android.os.IBinder;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080043import android.os.Looper;
44import android.os.Message;
Robert Greenwalt14f2ef42010-06-15 12:19:37 -070045import android.os.PowerManager;
Robert Greenwalt42acef32009-08-12 16:08:25 -070046import android.os.RemoteException;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080047import android.os.ServiceManager;
48import android.os.SystemProperties;
49import android.provider.Settings;
Robert Greenwalt42acef32009-08-12 16:08:25 -070050import android.text.TextUtils;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080051import android.util.EventLog;
Joe Onorato8a9b2202010-02-26 18:56:32 -080052import android.util.Slog;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080053
Robert Greenwalt42acef32009-08-12 16:08:25 -070054import com.android.internal.telephony.Phone;
Robert Greenwaltd0e18ff2010-01-26 11:40:34 -080055import com.android.server.connectivity.Tethering;
56
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080057import java.io.FileDescriptor;
Irfan Sheriffd649c122010-06-09 15:39:36 -070058import java.io.FileWriter;
59import java.io.IOException;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080060import java.io.PrintWriter;
Robert Greenwalt47f69fe2010-06-15 15:43:39 -070061import java.net.InetAddress;
62import java.net.UnknownHostException;
Robert Greenwalt42acef32009-08-12 16:08:25 -070063import java.util.ArrayList;
Robert Greenwalt47f69fe2010-06-15 15:43:39 -070064import java.util.Collection;
Robert Greenwaltd825ea42010-12-29 16:15:02 -080065import java.util.concurrent.atomic.AtomicBoolean;
Robert Greenwalt4e8dfef2010-09-20 14:35:25 -070066import java.util.GregorianCalendar;
Robert Greenwalt42acef32009-08-12 16:08:25 -070067import java.util.List;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080068
69/**
70 * @hide
71 */
72public class ConnectivityService extends IConnectivityManager.Stub {
73
Robert Greenwaltba175a52010-10-05 19:12:26 -070074 private static final boolean DBG = true;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080075 private static final String TAG = "ConnectivityService";
76
Robert Greenwalt42acef32009-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 Greenwaltd0e18ff2010-01-26 11:40:34 -080083 private Tethering mTethering;
Robert Greenwaltc9d5fb72010-02-25 12:29:30 -080084 private boolean mTetheringConfigValid = false;
Robert Greenwaltd0e18ff2010-01-26 11:40:34 -080085
The Android Open Source Project9066cfe2009-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 Greenwalt42acef32009-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 Sheriffa2a1b912010-06-07 09:03:04 -070099 private WifiWatchdogService mWifiWatchdogService;
100
Robert Greenwalt42acef32009-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 Project9066cfe2009-03-03 19:31:44 -0800106 private Context mContext;
107 private int mNetworkPreference;
Robert Greenwalt42acef32009-08-12 16:08:25 -0700108 private int mActiveDefaultNetwork = -1;
Robert Greenwaltd7085fc2010-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 Project9066cfe2009-03-03 19:31:44 -0800114
115 private int mNumDnsEntries;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800116
117 private boolean mTestMode;
Joe Onorato00092872010-09-01 21:18:22 -0700118 private static ConnectivityService sServiceInstance;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800119
Robert Greenwaltd825ea42010-12-29 16:15:02 -0800120 private AtomicBoolean mBackgroundDataEnabled = new AtomicBoolean(true);
121
Robert Greenwalt8dcc28b2010-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 Greenwaltf3331232010-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 Greenwalt434203a2010-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 Greenwaltd55a6b42011-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 Greenwalt42acef32009-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 Lockwood0f79b542009-08-14 14:18:49 -0400207 private boolean mSystemReady;
Dianne Hackborn1c633fc2009-12-08 19:45:14 -0800208 private Intent mInitialBroadcast;
Mike Lockwood0f79b542009-08-14 14:18:49 -0400209
Robert Greenwalt14f2ef42010-06-15 12:19:37 -0700210 private PowerManager.WakeLock mNetTransitionWakeLock;
211 private String mNetTransitionWakeLockCausedBy = "";
212 private int mNetTransitionWakeLockSerialNumber;
213 private int mNetTransitionWakeLockTimeout;
214
Robert Greenwalte90aa5e2010-09-01 11:34:05 -0700215 private InetAddress mDefaultDns;
216
Robert Greenwalt4e8dfef2010-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 Greenwalt434203a2010-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 Greenwaltd55a6b42011-03-25 13:09:25 -0700229 NetworkConfig[] mNetConfigs;
Robert Greenwalt5154ae762009-10-30 14:17:42 -0700230 int mNetworksDefined;
Robert Greenwalt42acef32009-08-12 16:08:25 -0700231
Robert Greenwalt511288a2009-12-07 11:33:18 -0800232 private static class RadioAttributes {
Robert Greenwalt42acef32009-08-12 16:08:25 -0700233 public int mSimultaneity;
234 public int mType;
235 public RadioAttributes(String init) {
236 String fragments[] = init.split(",");
Robert Greenwalt5154ae762009-10-30 14:17:42 -0700237 mType = Integer.parseInt(fragments[0]);
238 mSimultaneity = Integer.parseInt(fragments[1]);
Robert Greenwalt42acef32009-08-12 16:08:25 -0700239 }
240 }
241 RadioAttributes[] mRadioAttributes;
242
Wink Savillebb08caf2010-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 Project9066cfe2009-03-03 19:31:44 -0800246 }
Wink Savillebb08caf2010-09-02 19:23:52 -0700247 return sServiceInstance;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800248 }
Robert Greenwalt86e9e552009-07-16 17:21:39 -0700249
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800250 private ConnectivityService(Context context) {
Wink Savilleed9c02b2010-12-03 12:01:38 -0800251 if (DBG) log("ConnectivityService starting up");
Robert Greenwaltde8383c2010-01-14 17:47:58 -0800252
Wink Savillebb08caf2010-09-02 19:23:52 -0700253 HandlerThread handlerThread = new HandlerThread("ConnectivityServiceThread");
254 handlerThread.start();
255 mHandler = new MyHandler(handlerThread.getLooper());
256
Robert Greenwaltd825ea42010-12-29 16:15:02 -0800257 mBackgroundDataEnabled.set(Settings.Secure.getInt(context.getContentResolver(),
258 Settings.Secure.BACKGROUND_DATA, 1) == 1);
259
Robert Greenwaltde8383c2010-01-14 17:47:58 -0800260 // setup our unique device name
Robert Greenwalt733c6292010-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 Greenwaltde8383c2010-01-14 17:47:58 -0800268 }
269
Robert Greenwalte90aa5e2010-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 Greenwalte5903732011-02-22 16:00:42 -0800278 mDefaultDns = NetworkUtils.numericToInetAddress(dns);
279 } catch (IllegalArgumentException e) {
Wink Savilleed9c02b2010-12-03 12:01:38 -0800280 loge("Error setting defaultDns using " + dns);
Robert Greenwalte90aa5e2010-09-01 11:34:05 -0700281 }
282
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800283 mContext = context;
Robert Greenwalt14f2ef42010-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 Greenwalt42acef32009-08-12 16:08:25 -0700290 mNetTrackers = new NetworkStateTracker[
291 ConnectivityManager.MAX_NETWORK_TYPE+1];
Robert Greenwalt86e9e552009-07-16 17:21:39 -0700292
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800293 mNetworkPreference = getPersistedNetworkPreference();
Robert Greenwalt86e9e552009-07-16 17:21:39 -0700294
Robert Greenwalt5154ae762009-10-30 14:17:42 -0700295 mRadioAttributes = new RadioAttributes[ConnectivityManager.MAX_RADIO_TYPE+1];
Robert Greenwaltd55a6b42011-03-25 13:09:25 -0700296 mNetConfigs = new NetworkConfig[ConnectivityManager.MAX_NETWORK_TYPE+1];
Robert Greenwalt5154ae762009-10-30 14:17:42 -0700297
Robert Greenwalt42acef32009-08-12 16:08:25 -0700298 // Load device network attributes from resources
Robert Greenwalt42acef32009-08-12 16:08:25 -0700299 String[] raStrings = context.getResources().getStringArray(
300 com.android.internal.R.array.radioAttributes);
Robert Greenwalt5154ae762009-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 Savilleed9c02b2010-12-03 12:01:38 -0800304 loge("Error in radioAttributes - ignoring attempt to define type " + r.mType);
Robert Greenwalt5154ae762009-10-30 14:17:42 -0700305 continue;
306 }
307 if (mRadioAttributes[r.mType] != null) {
Wink Savilleed9c02b2010-12-03 12:01:38 -0800308 loge("Error in radioAttributes - ignoring attempt to redefine type " +
Robert Greenwalt5154ae762009-10-30 14:17:42 -0700309 r.mType);
310 continue;
311 }
Robert Greenwalt42acef32009-08-12 16:08:25 -0700312 mRadioAttributes[r.mType] = r;
313 }
314
Robert Greenwalt5154ae762009-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 Greenwaltd55a6b42011-03-25 13:09:25 -0700319 NetworkConfig n = new NetworkConfig(naString);
Wink Saville975c8482011-04-07 14:23:45 -0700320 if (n.type > ConnectivityManager.MAX_NETWORK_TYPE) {
Wink Savilleed9c02b2010-12-03 12:01:38 -0800321 loge("Error in networkAttributes - ignoring attempt to define type " +
Wink Saville975c8482011-04-07 14:23:45 -0700322 n.type);
Robert Greenwalt5154ae762009-10-30 14:17:42 -0700323 continue;
Robert Greenwalt42acef32009-08-12 16:08:25 -0700324 }
Wink Saville975c8482011-04-07 14:23:45 -0700325 if (mNetConfigs[n.type] != null) {
Wink Savilleed9c02b2010-12-03 12:01:38 -0800326 loge("Error in networkAttributes - ignoring attempt to redefine type " +
Wink Saville975c8482011-04-07 14:23:45 -0700327 n.type);
Robert Greenwalt5154ae762009-10-30 14:17:42 -0700328 continue;
329 }
Wink Saville975c8482011-04-07 14:23:45 -0700330 if (mRadioAttributes[n.radio] == null) {
Wink Savilleed9c02b2010-12-03 12:01:38 -0800331 loge("Error in networkAttributes - ignoring attempt to use undefined " +
Wink Saville975c8482011-04-07 14:23:45 -0700332 "radio " + n.radio + " in network type " + n.type);
Robert Greenwalt5154ae762009-10-30 14:17:42 -0700333 continue;
334 }
Wink Saville975c8482011-04-07 14:23:45 -0700335 mNetConfigs[n.type] = n;
Robert Greenwalt5154ae762009-10-30 14:17:42 -0700336 mNetworksDefined++;
337 } catch(Exception e) {
338 // ignore it - leave the entry null
Robert Greenwalt42acef32009-08-12 16:08:25 -0700339 }
340 }
341
Robert Greenwalt5154ae762009-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 Greenwaltd55a6b42011-03-25 13:09:25 -0700349 for (NetworkConfig na : mNetConfigs) {
Robert Greenwalt5154ae762009-10-30 14:17:42 -0700350 if (na == null) continue;
Wink Saville975c8482011-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 Greenwalt5154ae762009-10-30 14:17:42 -0700355 }
356 continue;
357 }
Wink Saville975c8482011-04-07 14:23:45 -0700358 mPriorityList[insertionPoint--] = na.type;
Robert Greenwalt5154ae762009-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 Greenwalt42acef32009-08-12 16:08:25 -0700367 mNetRequestersPids[i] = new ArrayList();
368 }
369
370 mFeatureUsers = new ArrayList();
371
Robert Greenwalt5154ae762009-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 Project9066cfe2009-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 Greenwalt5154ae762009-10-30 14:17:42 -0700383 for (int netType : mPriorityList) {
Wink Saville975c8482011-04-07 14:23:45 -0700384 switch (mNetConfigs[netType].radio) {
Robert Greenwalt5154ae762009-10-30 14:17:42 -0700385 case ConnectivityManager.TYPE_WIFI:
Wink Savilleed9c02b2010-12-03 12:01:38 -0800386 if (DBG) log("Starting Wifi Service.");
Wink Savillec7a98342010-08-13 16:11:42 -0700387 WifiStateTracker wst = new WifiStateTracker();
Irfan Sheriff0d255342010-07-28 09:35:20 -0700388 WifiService wifiService = new WifiService(context);
Robert Greenwalt5154ae762009-10-30 14:17:42 -0700389 ServiceManager.addService(Context.WIFI_SERVICE, wifiService);
Irfan Sheriff0d255342010-07-28 09:35:20 -0700390 wifiService.checkAndStartWifi();
Robert Greenwalt5154ae762009-10-30 14:17:42 -0700391 mNetTrackers[ConnectivityManager.TYPE_WIFI] = wst;
Wink Savillec7a98342010-08-13 16:11:42 -0700392 wst.startMonitoring(context, mHandler);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800393
Irfan Sheriffa2a1b912010-06-07 09:03:04 -0700394 //TODO: as part of WWS refactor, create only when needed
Irfan Sheriff0d255342010-07-28 09:35:20 -0700395 mWifiWatchdogService = new WifiWatchdogService(context);
Irfan Sheriffa2a1b912010-06-07 09:03:04 -0700396
Robert Greenwalt5154ae762009-10-30 14:17:42 -0700397 break;
398 case ConnectivityManager.TYPE_MOBILE:
Wink Savillec7a98342010-08-13 16:11:42 -0700399 mNetTrackers[netType] = new MobileDataStateTracker(netType,
Wink Saville975c8482011-04-07 14:23:45 -0700400 mNetConfigs[netType].name);
Wink Savillec7a98342010-08-13 16:11:42 -0700401 mNetTrackers[netType].startMonitoring(context, mHandler);
Robert Greenwalt5154ae762009-10-30 14:17:42 -0700402 break;
Robert Greenwaltda3d5e62010-12-06 13:56:24 -0800403 case ConnectivityManager.TYPE_DUMMY:
404 mNetTrackers[netType] = new DummyDataStateTracker(netType,
Wink Saville975c8482011-04-07 14:23:45 -0700405 mNetConfigs[netType].name);
Robert Greenwaltda3d5e62010-12-06 13:56:24 -0800406 mNetTrackers[netType].startMonitoring(context, mHandler);
407 break;
Jaikumar Ganesh15c74392010-12-21 22:31:44 -0800408 case ConnectivityManager.TYPE_BLUETOOTH:
409 mNetTrackers[netType] = BluetoothTetheringDataTracker.getInstance();
410 mNetTrackers[netType].startMonitoring(context, mHandler);
411 break;
Benoit Goby08c39c62010-12-22 14:29:40 -0800412 case ConnectivityManager.TYPE_ETHERNET:
413 mNetTrackers[netType] = EthernetDataTracker.getInstance();
414 mNetTrackers[netType].startMonitoring(context, mHandler);
415 break;
Robert Greenwalt5154ae762009-10-30 14:17:42 -0700416 default:
Wink Savilleed9c02b2010-12-03 12:01:38 -0800417 loge("Trying to create a DataStateTracker for an unknown radio type " +
Wink Saville975c8482011-04-07 14:23:45 -0700418 mNetConfigs[netType].radio);
Robert Greenwalt5154ae762009-10-30 14:17:42 -0700419 continue;
420 }
421 }
Robert Greenwaltd0e18ff2010-01-26 11:40:34 -0800422
Robert Greenwaltdfadaea2010-03-11 15:03:08 -0800423 mTethering = new Tethering(mContext, mHandler.getLooper());
Robert Greenwaltc9d5fb72010-02-25 12:29:30 -0800424 mTetheringConfigValid = (((mNetTrackers[ConnectivityManager.TYPE_MOBILE_DUN] != null) ||
425 !mTethering.isDunRequired()) &&
426 (mTethering.getTetherableUsbRegexs().length != 0 ||
Danica Chang6fdd0c62010-08-11 14:54:43 -0700427 mTethering.getTetherableWifiRegexs().length != 0 ||
428 mTethering.getTetherableBluetoothRegexs().length != 0) &&
Robert Greenwaltc9d5fb72010-02-25 12:29:30 -0800429 mTethering.getUpstreamIfaceRegexs().length != 0);
430
Robert Greenwalt4e8dfef2010-09-20 14:35:25 -0700431 if (DBG) {
432 mInetLog = new ArrayList();
433 }
Robert Greenwalt434203a2010-10-11 16:00:27 -0700434
435 mSettingsObserver = new SettingsObserver(mHandler, EVENT_APPLY_GLOBAL_HTTP_PROXY);
436 mSettingsObserver.observe(mContext);
Robert Greenwaltb7090d62010-12-02 11:31:00 -0800437
438 loadGlobalProxy();
Hung-ying Tyan6b818de2011-01-19 16:48:38 +0800439
440 VpnManager.startVpnService(context);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800441 }
442
Robert Greenwalt5154ae762009-10-30 14:17:42 -0700443
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800444 /**
Robert Greenwalt86e9e552009-07-16 17:21:39 -0700445 * Sets the preferred network.
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800446 * @param preference the new preference
447 */
Robert Greenwalt8dcc28b2010-09-23 10:05:56 -0700448 public void setNetworkPreference(int preference) {
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800449 enforceChangePermission();
Robert Greenwalt8dcc28b2010-09-23 10:05:56 -0700450
451 mHandler.sendMessage(mHandler.obtainMessage(EVENT_SET_NETWORK_PREFERENCE, preference, 0));
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800452 }
453
454 public int getNetworkPreference() {
455 enforceAccessPermission();
Robert Greenwalt8dcc28b2010-09-23 10:05:56 -0700456 int preference;
457 synchronized(this) {
458 preference = mNetworkPreference;
459 }
460 return preference;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800461 }
462
Robert Greenwalt8dcc28b2010-09-23 10:05:56 -0700463 private void handleSetNetworkPreference(int preference) {
464 if (ConnectivityManager.isNetworkTypeValid(preference) &&
Robert Greenwaltd55a6b42011-03-25 13:09:25 -0700465 mNetConfigs[preference] != null &&
466 mNetConfigs[preference].isDefault()) {
Robert Greenwalt8dcc28b2010-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 Project9066cfe2009-03-03 19:31:44 -0800476 }
Robert Greenwalt86e9e552009-07-16 17:21:39 -0700477
The Android Open Source Project9066cfe2009-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 Greenwalt86e9e552009-07-16 17:21:39 -0700489
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800490 /**
Robert Greenwalt86e9e552009-07-16 17:21:39 -0700491 * Make the state of network connectivity conform to the preference settings
The Android Open Source Project9066cfe2009-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 Greenwalt42acef32009-08-12 16:08:25 -0700498 if (mNetTrackers[mNetworkPreference].getNetworkInfo().isConnected())
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800499 return;
500
Robert Greenwalt42acef32009-08-12 16:08:25 -0700501 if (!mNetTrackers[mNetworkPreference].isAvailable())
502 return;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800503
Robert Greenwalt42acef32009-08-12 16:08:25 -0700504 for (int t=0; t <= ConnectivityManager.MAX_RADIO_TYPE; t++) {
Robert Greenwalt5154ae762009-10-30 14:17:42 -0700505 if (t != mNetworkPreference && mNetTrackers[t] != null &&
Robert Greenwalt42acef32009-08-12 16:08:25 -0700506 mNetTrackers[t].getNetworkInfo().isConnected()) {
Robert Greenwaltec9fe462009-08-20 15:25:14 -0700507 if (DBG) {
Wink Savilleed9c02b2010-12-03 12:01:38 -0800508 log("tearing down " + mNetTrackers[t].getNetworkInfo() +
Robert Greenwaltec9fe462009-08-20 15:25:14 -0700509 " in enforcePreference");
510 }
Robert Greenwalt42acef32009-08-12 16:08:25 -0700511 teardown(mNetTrackers[t]);
The Android Open Source Project9066cfe2009-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 Greenwalt86e9e552009-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 Project9066cfe2009-03-03 19:31:44 -0800531 */
532 public NetworkInfo getActiveNetworkInfo() {
533 enforceAccessPermission();
Robert Greenwalt42acef32009-08-12 16:08:25 -0700534 for (int type=0; type <= ConnectivityManager.MAX_NETWORK_TYPE; type++) {
Robert Greenwaltd55a6b42011-03-25 13:09:25 -0700535 if (mNetConfigs[type] == null || !mNetConfigs[type].isDefault()) {
Robert Greenwalt42acef32009-08-12 16:08:25 -0700536 continue;
537 }
538 NetworkStateTracker t = mNetTrackers[type];
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800539 NetworkInfo info = t.getNetworkInfo();
540 if (info.isConnected()) {
Wink Savilleed9c02b2010-12-03 12:01:38 -0800541 if (DBG && type != mActiveDefaultNetwork) {
542 loge("connected default network is not mActiveDefaultNetwork!");
543 }
The Android Open Source Project9066cfe2009-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 Greenwalt5154ae762009-10-30 14:17:42 -0700562 NetworkInfo[] result = new NetworkInfo[mNetworksDefined];
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800563 int i = 0;
564 for (NetworkStateTracker t : mNetTrackers) {
Robert Greenwalt5154ae762009-10-30 14:17:42 -0700565 if(t != null) result[i++] = t.getNetworkInfo();
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800566 }
567 return result;
568 }
569
Robert Greenwaltd192dad2010-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 Greenwaltd55a6b42011-03-25 13:09:25 -0700581 if (mNetConfigs[type] == null || !mNetConfigs[type].isDefault()) {
Robert Greenwaltd192dad2010-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 Project9066cfe2009-03-03 19:31:44 -0800602 public boolean setRadios(boolean turnOn) {
603 boolean result = true;
604 enforceChangePermission();
605 for (NetworkStateTracker t : mNetTrackers) {
Robert Greenwalt5154ae762009-10-30 14:17:42 -0700606 if (t != null) result = t.setRadio(turnOn) && result;
The Android Open Source Project9066cfe2009-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 Greenwalt9c75d4a2009-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 Greenwalt42acef32009-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 Greenwaltb9285352009-12-21 18:24:07 -0800633 long mCreateTime;
Robert Greenwalt42acef32009-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 Greenwaltb9285352009-12-21 18:24:07 -0800642 mCreateTime = System.currentTimeMillis();
Robert Greenwalt9c75d4a2009-09-27 17:27:04 -0700643
Robert Greenwalt42acef32009-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 Savilleed9c02b2010-12-03 12:01:38 -0800656 log("ConnectivityService FeatureUser binderDied(" +
Robert Greenwaltb9285352009-12-21 18:24:07 -0800657 mNetworkType + ", " + mFeature + ", " + mBinder + "), created " +
658 (System.currentTimeMillis() - mCreateTime) + " mSec ago");
Robert Greenwalt9c75d4a2009-09-27 17:27:04 -0700659 stopUsingNetworkFeature(this, false);
Robert Greenwalt42acef32009-08-12 16:08:25 -0700660 }
661
Robert Greenwalt9c75d4a2009-09-27 17:27:04 -0700662 public void expire() {
Wink Savilleed9c02b2010-12-03 12:01:38 -0800663 log("ConnectivityService FeatureUser expire(" +
Robert Greenwaltb9285352009-12-21 18:24:07 -0800664 mNetworkType + ", " + mFeature + ", " + mBinder +"), created " +
665 (System.currentTimeMillis() - mCreateTime) + " mSec ago");
Robert Greenwalt9c75d4a2009-09-27 17:27:04 -0700666 stopUsingNetworkFeature(this, false);
667 }
Robert Greenwaltb9285352009-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 Greenwalt42acef32009-08-12 16:08:25 -0700673 }
674
Robert Greenwalt9c75d4a2009-09-27 17:27:04 -0700675 // javadoc from interface
Robert Greenwalt42acef32009-08-12 16:08:25 -0700676 public int startUsingNetworkFeature(int networkType, String feature,
677 IBinder binder) {
678 if (DBG) {
Wink Savilleed9c02b2010-12-03 12:01:38 -0800679 log("startUsingNetworkFeature for net " + networkType + ": " + feature);
Robert Greenwalt42acef32009-08-12 16:08:25 -0700680 }
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800681 enforceChangePermission();
Robert Greenwalt5154ae762009-10-30 14:17:42 -0700682 if (!ConnectivityManager.isNetworkTypeValid(networkType) ||
Robert Greenwaltd55a6b42011-03-25 13:09:25 -0700683 mNetConfigs[networkType] == null) {
Robert Greenwalt42acef32009-08-12 16:08:25 -0700684 return Phone.APN_REQUEST_FAILED;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800685 }
Robert Greenwalt42acef32009-08-12 16:08:25 -0700686
Robert Greenwalt9c75d4a2009-09-27 17:27:04 -0700687 FeatureUser f = new FeatureUser(networkType, feature, binder);
Robert Greenwalt42acef32009-08-12 16:08:25 -0700688
689 // TODO - move this into the MobileDataStateTracker
690 int usedNetworkType = networkType;
691 if(networkType == ConnectivityManager.TYPE_MOBILE) {
Wink Saville9d7d6282011-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 Greenwalt42acef32009-08-12 16:08:25 -0700696 }
697 }
698 NetworkStateTracker network = mNetTrackers[usedNetworkType];
699 if (network != null) {
Robert Greenwalt0be1e982010-12-15 13:26:33 -0800700 Integer currentPid = new Integer(getCallingPid());
Robert Greenwalt42acef32009-08-12 16:08:25 -0700701 if (usedNetworkType != networkType) {
Robert Greenwalt42acef32009-08-12 16:08:25 -0700702 NetworkStateTracker radio = mNetTrackers[networkType];
703 NetworkInfo ni = network.getNetworkInfo();
704
705 if (ni.isAvailable() == false) {
Wink Savilleed9c02b2010-12-03 12:01:38 -0800706 if (DBG) log("special network not available");
Robert Greenwalte32e8122010-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 Greenwalt42acef32009-08-12 16:08:25 -0700712 }
713
Robert Greenwalt9c75d4a2009-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 Greenwalt42acef32009-08-12 16:08:25 -0700720 }
Robert Greenwalt8dcc28b2010-09-23 10:05:56 -0700721 mHandler.sendMessageDelayed(mHandler.obtainMessage(EVENT_RESTORE_DEFAULT_NETWORK,
Robert Greenwalt9c75d4a2009-09-27 17:27:04 -0700722 f), getRestoreDefaultNetworkDelay());
723
Robert Greenwalt42acef32009-08-12 16:08:25 -0700724
Robert Greenwalta64bf832009-08-19 20:19:33 -0700725 if ((ni.isConnectedOrConnecting() == true) &&
726 !network.isTeardownRequested()) {
Robert Greenwalt42acef32009-08-12 16:08:25 -0700727 if (ni.isConnected() == true) {
728 // add the pid-specific dns
Robert Greenwaltc76b8fa2010-07-23 15:46:26 -0700729 handleDnsConfigurationChange(networkType);
Wink Savilleed9c02b2010-12-03 12:01:38 -0800730 if (DBG) log("special network already active");
Robert Greenwalt42acef32009-08-12 16:08:25 -0700731 return Phone.APN_ALREADY_ACTIVE;
732 }
Wink Savilleed9c02b2010-12-03 12:01:38 -0800733 if (DBG) log("special network already connecting");
Robert Greenwalt42acef32009-08-12 16:08:25 -0700734 return Phone.APN_REQUEST_STARTED;
735 }
736
737 // check if the radio in play can make another contact
738 // assume if cannot for now
739
Wink Savilleed9c02b2010-12-03 12:01:38 -0800740 if (DBG) log("reconnecting to special network");
Robert Greenwalt42acef32009-08-12 16:08:25 -0700741 network.reconnect();
742 return Phone.APN_REQUEST_STARTED;
743 } else {
Robert Greenwalt0be1e982010-12-15 13:26:33 -0800744 // need to remember this unsupported request so we respond appropriately on stop
745 synchronized(this) {
746 mFeatureUsers.add(f);
747 if (!mNetRequestersPids[usedNetworkType].contains(currentPid)) {
748 // this gets used for per-pid dns when connected
749 mNetRequestersPids[usedNetworkType].add(currentPid);
750 }
751 }
Robert Greenwalt02648a42010-05-18 10:52:51 -0700752 return -1;
Robert Greenwalt42acef32009-08-12 16:08:25 -0700753 }
754 }
755 return Phone.APN_TYPE_NOT_AVAILABLE;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800756 }
757
Robert Greenwalt9c75d4a2009-09-27 17:27:04 -0700758 // javadoc from interface
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800759 public int stopUsingNetworkFeature(int networkType, String feature) {
Robert Greenwaltb8f16342009-10-06 17:52:40 -0700760 enforceChangePermission();
761
Robert Greenwalt9c75d4a2009-09-27 17:27:04 -0700762 int pid = getCallingPid();
763 int uid = getCallingUid();
764
765 FeatureUser u = null;
766 boolean found = false;
767
768 synchronized(this) {
769 for (int i = 0; i < mFeatureUsers.size() ; i++) {
770 u = (FeatureUser)mFeatureUsers.get(i);
771 if (uid == u.mUid && pid == u.mPid &&
772 networkType == u.mNetworkType &&
773 TextUtils.equals(feature, u.mFeature)) {
774 found = true;
775 break;
776 }
777 }
778 }
779 if (found && u != null) {
780 // stop regardless of how many other time this proc had called start
781 return stopUsingNetworkFeature(u, true);
782 } else {
783 // none found!
Wink Savilleed9c02b2010-12-03 12:01:38 -0800784 if (DBG) log("ignoring stopUsingNetworkFeature - not a live request");
Robert Greenwalt9c75d4a2009-09-27 17:27:04 -0700785 return 1;
786 }
Robert Greenwalt42acef32009-08-12 16:08:25 -0700787 }
788
Robert Greenwalt9c75d4a2009-09-27 17:27:04 -0700789 private int stopUsingNetworkFeature(FeatureUser u, boolean ignoreDups) {
790 int networkType = u.mNetworkType;
791 String feature = u.mFeature;
792 int pid = u.mPid;
793 int uid = u.mUid;
794
795 NetworkStateTracker tracker = null;
796 boolean callTeardown = false; // used to carry our decision outside of sync block
797
Robert Greenwalt42acef32009-08-12 16:08:25 -0700798 if (DBG) {
Wink Savilleed9c02b2010-12-03 12:01:38 -0800799 log("stopUsingNetworkFeature for net " + networkType +
Robert Greenwalt42acef32009-08-12 16:08:25 -0700800 ": " + feature);
801 }
Robert Greenwaltb8f16342009-10-06 17:52:40 -0700802
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800803 if (!ConnectivityManager.isNetworkTypeValid(networkType)) {
804 return -1;
805 }
Robert Greenwalt42acef32009-08-12 16:08:25 -0700806
Robert Greenwalt9c75d4a2009-09-27 17:27:04 -0700807 // need to link the mFeatureUsers list with the mNetRequestersPids state in this
808 // sync block
809 synchronized(this) {
810 // check if this process still has an outstanding start request
811 if (!mFeatureUsers.contains(u)) {
Wink Savilleed9c02b2010-12-03 12:01:38 -0800812 if (DBG) log("ignoring - this process has no outstanding requests");
Robert Greenwalt42acef32009-08-12 16:08:25 -0700813 return 1;
814 }
Robert Greenwalt9c75d4a2009-09-27 17:27:04 -0700815 u.unlinkDeathRecipient();
816 mFeatureUsers.remove(mFeatureUsers.indexOf(u));
817 // If we care about duplicate requests, check for that here.
818 //
819 // This is done to support the extension of a request - the app
820 // can request we start the network feature again and renew the
821 // auto-shutoff delay. Normal "stop" calls from the app though
822 // do not pay attention to duplicate requests - in effect the
823 // API does not refcount and a single stop will counter multiple starts.
824 if (ignoreDups == false) {
825 for (int i = 0; i < mFeatureUsers.size() ; i++) {
826 FeatureUser x = (FeatureUser)mFeatureUsers.get(i);
827 if (x.mUid == u.mUid && x.mPid == u.mPid &&
828 x.mNetworkType == u.mNetworkType &&
829 TextUtils.equals(x.mFeature, u.mFeature)) {
Wink Savilleed9c02b2010-12-03 12:01:38 -0800830 if (DBG) log("ignoring stopUsingNetworkFeature as dup is found");
Robert Greenwalt9c75d4a2009-09-27 17:27:04 -0700831 return 1;
832 }
833 }
Robert Greenwalt42acef32009-08-12 16:08:25 -0700834 }
Robert Greenwalt9c75d4a2009-09-27 17:27:04 -0700835
836 // TODO - move to MobileDataStateTracker
837 int usedNetworkType = networkType;
838 if (networkType == ConnectivityManager.TYPE_MOBILE) {
Wink Saville9d7d6282011-03-12 14:52:01 -0800839 usedNetworkType = convertFeatureToNetworkType(feature);
840 if (usedNetworkType < 0) {
841 usedNetworkType = networkType;
Robert Greenwalt9c75d4a2009-09-27 17:27:04 -0700842 }
843 }
844 tracker = mNetTrackers[usedNetworkType];
Robert Greenwalt5154ae762009-10-30 14:17:42 -0700845 if (tracker == null) {
Wink Savilleed9c02b2010-12-03 12:01:38 -0800846 if (DBG) log("ignoring - no known tracker for net type " + usedNetworkType);
Robert Greenwalt5154ae762009-10-30 14:17:42 -0700847 return -1;
848 }
849 if (usedNetworkType != networkType) {
Robert Greenwalt9c75d4a2009-09-27 17:27:04 -0700850 Integer currentPid = new Integer(pid);
Robert Greenwalt9c75d4a2009-09-27 17:27:04 -0700851 mNetRequestersPids[usedNetworkType].remove(currentPid);
Robert Greenwalt421c72b2009-12-17 14:54:59 -0800852 reassessPidDns(pid, true);
Robert Greenwalt9c75d4a2009-09-27 17:27:04 -0700853 if (mNetRequestersPids[usedNetworkType].size() != 0) {
Wink Savilleed9c02b2010-12-03 12:01:38 -0800854 if (DBG) log("not tearing down special network - " +
Robert Greenwalt9c75d4a2009-09-27 17:27:04 -0700855 "others still using it");
856 return 1;
857 }
858 callTeardown = true;
Robert Greenwalt19b9ab42011-01-10 11:58:31 -0800859 } else {
860 if (DBG) log("not a known feature - dropping");
Robert Greenwalt9c75d4a2009-09-27 17:27:04 -0700861 }
862 }
Wink Savilleed9c02b2010-12-03 12:01:38 -0800863 if (DBG) log("Doing network teardown");
Robert Greenwalt9c75d4a2009-09-27 17:27:04 -0700864 if (callTeardown) {
865 tracker.teardown();
Robert Greenwalt42acef32009-08-12 16:08:25 -0700866 return 1;
867 } else {
Robert Greenwalt02648a42010-05-18 10:52:51 -0700868 return -1;
Robert Greenwalt42acef32009-08-12 16:08:25 -0700869 }
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800870 }
871
872 /**
Robert Greenwalt585ac0f2010-08-27 09:24:29 -0700873 * @deprecated use requestRouteToHostAddress instead
874 *
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800875 * Ensure that a network route exists to deliver traffic to the specified
876 * host via the specified network interface.
Robert Greenwalt86e9e552009-07-16 17:21:39 -0700877 * @param networkType the type of the network over which traffic to the
878 * specified host is to be routed
879 * @param hostAddress the IP address of the host to which the route is
880 * desired
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800881 * @return {@code true} on success, {@code false} on failure
882 */
883 public boolean requestRouteToHost(int networkType, int hostAddress) {
Robert Greenwalt585ac0f2010-08-27 09:24:29 -0700884 InetAddress inetAddress = NetworkUtils.intToInetAddress(hostAddress);
885
886 if (inetAddress == null) {
887 return false;
888 }
889
890 return requestRouteToHostAddress(networkType, inetAddress.getAddress());
891 }
892
893 /**
894 * Ensure that a network route exists to deliver traffic to the specified
895 * host via the specified network interface.
896 * @param networkType the type of the network over which traffic to the
897 * specified host is to be routed
898 * @param hostAddress the IP address of the host to which the route is
899 * desired
900 * @return {@code true} on success, {@code false} on failure
901 */
902 public boolean requestRouteToHostAddress(int networkType, byte[] hostAddress) {
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800903 enforceChangePermission();
904 if (!ConnectivityManager.isNetworkTypeValid(networkType)) {
905 return false;
906 }
907 NetworkStateTracker tracker = mNetTrackers[networkType];
Robert Greenwalt8206ff32009-09-10 15:06:20 -0700908
Robert Greenwalt5154ae762009-10-30 14:17:42 -0700909 if (tracker == null || !tracker.getNetworkInfo().isConnected() ||
910 tracker.isTeardownRequested()) {
Robert Greenwalt8206ff32009-09-10 15:06:20 -0700911 if (DBG) {
Wink Savilleed9c02b2010-12-03 12:01:38 -0800912 log("requestRouteToHostAddress on down network " +
Robert Greenwalt585ac0f2010-08-27 09:24:29 -0700913 "(" + networkType + ") - dropped");
Robert Greenwalt8206ff32009-09-10 15:06:20 -0700914 }
915 return false;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800916 }
Robert Greenwalt47f69fe2010-06-15 15:43:39 -0700917 try {
Robert Greenwalt585ac0f2010-08-27 09:24:29 -0700918 InetAddress addr = InetAddress.getByAddress(hostAddress);
Robert Greenwalt47f69fe2010-06-15 15:43:39 -0700919 return addHostRoute(tracker, addr);
920 } catch (UnknownHostException e) {}
921 return false;
Irfan Sheriffd649c122010-06-09 15:39:36 -0700922 }
923
924 /**
925 * Ensure that a network route exists to deliver traffic to the specified
926 * host via the mobile data network.
927 * @param hostAddress the IP address of the host to which the route is desired,
928 * in network byte order.
Robert Greenwalt47f69fe2010-06-15 15:43:39 -0700929 * TODO - deprecate
Irfan Sheriffd649c122010-06-09 15:39:36 -0700930 * @return {@code true} on success, {@code false} on failure
931 */
Robert Greenwalt47f69fe2010-06-15 15:43:39 -0700932 private boolean addHostRoute(NetworkStateTracker nt, InetAddress hostAddress) {
Irfan Sheriffd649c122010-06-09 15:39:36 -0700933 if (nt.getNetworkInfo().getType() == ConnectivityManager.TYPE_WIFI) {
934 return false;
935 }
936
Robert Greenwalt37e65eb2010-08-30 10:56:47 -0700937 LinkProperties p = nt.getLinkProperties();
Robert Greenwalt47f69fe2010-06-15 15:43:39 -0700938 if (p == null) return false;
939 String interfaceName = p.getInterfaceName();
Irfan Sheriffd649c122010-06-09 15:39:36 -0700940
941 if (DBG) {
Wink Savilleed9c02b2010-12-03 12:01:38 -0800942 log("Requested host route to " + hostAddress + "(" + interfaceName + ")");
Irfan Sheriffd649c122010-06-09 15:39:36 -0700943 }
Robert Greenwalt47f69fe2010-06-15 15:43:39 -0700944 if (interfaceName != null) {
Robert Greenwalt585ac0f2010-08-27 09:24:29 -0700945 return NetworkUtils.addHostRoute(interfaceName, hostAddress, null);
Irfan Sheriffd649c122010-06-09 15:39:36 -0700946 } else {
Wink Savilleed9c02b2010-12-03 12:01:38 -0800947 if (DBG) loge("addHostRoute failed due to null interface name");
Irfan Sheriffd649c122010-06-09 15:39:36 -0700948 return false;
949 }
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800950 }
951
952 /**
953 * @see ConnectivityManager#getBackgroundDataSetting()
954 */
955 public boolean getBackgroundDataSetting() {
Robert Greenwaltd825ea42010-12-29 16:15:02 -0800956 return mBackgroundDataEnabled.get();
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800957 }
Robert Greenwalt86e9e552009-07-16 17:21:39 -0700958
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800959 /**
960 * @see ConnectivityManager#setBackgroundDataSetting(boolean)
961 */
962 public void setBackgroundDataSetting(boolean allowBackgroundDataUsage) {
963 mContext.enforceCallingOrSelfPermission(
964 android.Manifest.permission.CHANGE_BACKGROUND_DATA_SETTING,
965 "ConnectivityService");
Robert Greenwalt86e9e552009-07-16 17:21:39 -0700966
Robert Greenwaltd825ea42010-12-29 16:15:02 -0800967 mBackgroundDataEnabled.set(allowBackgroundDataUsage);
968
Robert Greenwalt8dcc28b2010-09-23 10:05:56 -0700969 mHandler.sendMessage(mHandler.obtainMessage(EVENT_SET_BACKGROUND_DATA,
970 (allowBackgroundDataUsage ? ENABLED : DISABLED), 0));
971 }
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800972
Robert Greenwalt8dcc28b2010-09-23 10:05:56 -0700973 private void handleSetBackgroundData(boolean enabled) {
Robert Greenwaltdb4afae2011-02-25 13:44:09 -0800974 Settings.Secure.putInt(mContext.getContentResolver(),
975 Settings.Secure.BACKGROUND_DATA, enabled ? 1 : 0);
976 Intent broadcast = new Intent(
977 ConnectivityManager.ACTION_BACKGROUND_DATA_SETTING_CHANGED);
978 mContext.sendBroadcast(broadcast);
Robert Greenwalt86e9e552009-07-16 17:21:39 -0700979 }
980
Robert Greenwaltc03fa502010-02-23 18:58:05 -0800981 /**
982 * @see ConnectivityManager#getMobileDataEnabled()
983 */
984 public boolean getMobileDataEnabled() {
Wink Savillee7982682010-12-07 10:31:02 -0800985 // TODO: This detail should probably be in DataConnectionTracker's
986 // which is where we store the value and maybe make this
987 // asynchronous.
Robert Greenwaltc03fa502010-02-23 18:58:05 -0800988 enforceAccessPermission();
989 boolean retVal = Settings.Secure.getInt(mContext.getContentResolver(),
990 Settings.Secure.MOBILE_DATA, 1) == 1;
Wink Savilleed9c02b2010-12-03 12:01:38 -0800991 if (DBG) log("getMobileDataEnabled returning " + retVal);
Robert Greenwaltc03fa502010-02-23 18:58:05 -0800992 return retVal;
993 }
994
Robert Greenwaltd55a6b42011-03-25 13:09:25 -0700995 public void setDataDependency(int networkType, boolean met) {
996 enforceChangePermission();
997 if (DBG) {
998 log("setDataDependency(" + networkType + ", " + met + ")");
999 }
1000 mHandler.sendMessage(mHandler.obtainMessage(EVENT_SET_DEPENDENCY_MET,
1001 (met ? ENABLED : DISABLED), networkType));
1002 }
1003
1004 private void handleSetDependencyMet(int networkType, boolean met) {
1005 if (mNetTrackers[networkType] != null) {
1006 if (DBG) {
1007 log("handleSetDependencyMet(" + networkType + ", " + met + ")");
1008 }
1009 mNetTrackers[networkType].setDependencyMet(met);
1010 }
1011 }
1012
Robert Greenwaltc03fa502010-02-23 18:58:05 -08001013 /**
1014 * @see ConnectivityManager#setMobileDataEnabled(boolean)
1015 */
Robert Greenwalt8dcc28b2010-09-23 10:05:56 -07001016 public void setMobileDataEnabled(boolean enabled) {
Robert Greenwaltc03fa502010-02-23 18:58:05 -08001017 enforceChangePermission();
Wink Savilleed9c02b2010-12-03 12:01:38 -08001018 if (DBG) log("setMobileDataEnabled(" + enabled + ")");
Robert Greenwaltc03fa502010-02-23 18:58:05 -08001019
Robert Greenwalt8dcc28b2010-09-23 10:05:56 -07001020 mHandler.sendMessage(mHandler.obtainMessage(EVENT_SET_MOBILE_DATA,
Robert Greenwaltd55a6b42011-03-25 13:09:25 -07001021 (enabled ? ENABLED : DISABLED), 0));
Robert Greenwalt8dcc28b2010-09-23 10:05:56 -07001022 }
1023
1024 private void handleSetMobileData(boolean enabled) {
Wink Savillee7982682010-12-07 10:31:02 -08001025 if (mNetTrackers[ConnectivityManager.TYPE_MOBILE] != null) {
1026 if (DBG) {
1027 Slog.d(TAG, mNetTrackers[ConnectivityManager.TYPE_MOBILE].toString() + enabled);
Robert Greenwaltc03fa502010-02-23 18:58:05 -08001028 }
Wink Savillee7982682010-12-07 10:31:02 -08001029 mNetTrackers[ConnectivityManager.TYPE_MOBILE].setDataEnable(enabled);
Robert Greenwaltc03fa502010-02-23 18:58:05 -08001030 }
1031 }
1032
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001033 private void enforceAccessPermission() {
Robert Greenwalt86e9e552009-07-16 17:21:39 -07001034 mContext.enforceCallingOrSelfPermission(
1035 android.Manifest.permission.ACCESS_NETWORK_STATE,
1036 "ConnectivityService");
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001037 }
1038
1039 private void enforceChangePermission() {
Robert Greenwalt86e9e552009-07-16 17:21:39 -07001040 mContext.enforceCallingOrSelfPermission(
1041 android.Manifest.permission.CHANGE_NETWORK_STATE,
1042 "ConnectivityService");
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001043 }
1044
Robert Greenwaltd0e18ff2010-01-26 11:40:34 -08001045 // TODO Make this a special check when it goes public
1046 private void enforceTetherChangePermission() {
1047 mContext.enforceCallingOrSelfPermission(
1048 android.Manifest.permission.CHANGE_NETWORK_STATE,
1049 "ConnectivityService");
1050 }
1051
Robert Greenwalt2a091d72010-02-11 18:18:40 -08001052 private void enforceTetherAccessPermission() {
1053 mContext.enforceCallingOrSelfPermission(
1054 android.Manifest.permission.ACCESS_NETWORK_STATE,
1055 "ConnectivityService");
1056 }
1057
Robert Greenwalt14f2ef42010-06-15 12:19:37 -07001058 private void enforceConnectivityInternalPermission() {
1059 mContext.enforceCallingOrSelfPermission(
1060 android.Manifest.permission.CONNECTIVITY_INTERNAL,
1061 "ConnectivityService");
1062 }
1063
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001064 /**
Robert Greenwalt86e9e552009-07-16 17:21:39 -07001065 * Handle a {@code DISCONNECTED} event. If this pertains to the non-active
1066 * network, we ignore it. If it is for the active network, we send out a
1067 * broadcast. But first, we check whether it might be possible to connect
1068 * to a different network.
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001069 * @param info the {@code NetworkInfo} for the network
1070 */
1071 private void handleDisconnect(NetworkInfo info) {
1072
Robert Greenwalt42acef32009-08-12 16:08:25 -07001073 int prevNetType = info.getType();
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001074
Robert Greenwalt42acef32009-08-12 16:08:25 -07001075 mNetTrackers[prevNetType].setTeardownRequested(false);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001076 /*
1077 * If the disconnected network is not the active one, then don't report
1078 * this as a loss of connectivity. What probably happened is that we're
1079 * getting the disconnect for a network that we explicitly disabled
1080 * in accordance with network preference policies.
1081 */
Robert Greenwaltd55a6b42011-03-25 13:09:25 -07001082 if (!mNetConfigs[prevNetType].isDefault()) {
Robert Greenwalt42acef32009-08-12 16:08:25 -07001083 List pids = mNetRequestersPids[prevNetType];
1084 for (int i = 0; i<pids.size(); i++) {
1085 Integer pid = (Integer)pids.get(i);
1086 // will remove them because the net's no longer connected
1087 // need to do this now as only now do we know the pids and
1088 // can properly null things that are no longer referenced.
1089 reassessPidDns(pid.intValue(), false);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001090 }
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001091 }
1092
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001093 Intent intent = new Intent(ConnectivityManager.CONNECTIVITY_ACTION);
1094 intent.putExtra(ConnectivityManager.EXTRA_NETWORK_INFO, info);
1095 if (info.isFailover()) {
1096 intent.putExtra(ConnectivityManager.EXTRA_IS_FAILOVER, true);
1097 info.setFailover(false);
1098 }
1099 if (info.getReason() != null) {
1100 intent.putExtra(ConnectivityManager.EXTRA_REASON, info.getReason());
1101 }
1102 if (info.getExtraInfo() != null) {
Robert Greenwalt86e9e552009-07-16 17:21:39 -07001103 intent.putExtra(ConnectivityManager.EXTRA_EXTRA_INFO,
1104 info.getExtraInfo());
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001105 }
Robert Greenwalt42acef32009-08-12 16:08:25 -07001106
Robert Greenwaltd55a6b42011-03-25 13:09:25 -07001107 if (mNetConfigs[prevNetType].isDefault()) {
Robert Greenwaltf21ef7d2011-01-11 13:56:33 -08001108 tryFailover(prevNetType);
1109 if (mActiveDefaultNetwork != -1) {
1110 NetworkInfo switchTo = mNetTrackers[mActiveDefaultNetwork].getNetworkInfo();
Robert Greenwaltcc4b4012010-01-25 17:54:29 -08001111 intent.putExtra(ConnectivityManager.EXTRA_OTHER_NETWORK_INFO, switchTo);
1112 } else {
Robert Greenwalt029be812010-09-20 18:01:43 -07001113 mDefaultInetConditionPublished = 0; // we're not connected anymore
Robert Greenwaltcc4b4012010-01-25 17:54:29 -08001114 intent.putExtra(ConnectivityManager.EXTRA_NO_CONNECTIVITY, true);
1115 }
Robert Greenwaltda03c4e2010-01-20 19:29:41 -08001116 }
Robert Greenwalt029be812010-09-20 18:01:43 -07001117 intent.putExtra(ConnectivityManager.EXTRA_INET_CONDITION, mDefaultInetConditionPublished);
Robert Greenwaltda03c4e2010-01-20 19:29:41 -08001118 // do this before we broadcast the change
Robert Greenwaltc76b8fa2010-07-23 15:46:26 -07001119 handleConnectivityChange(prevNetType);
Robert Greenwaltda03c4e2010-01-20 19:29:41 -08001120
1121 sendStickyBroadcast(intent);
1122 /*
1123 * If the failover network is already connected, then immediately send
1124 * out a followup broadcast indicating successful failover
1125 */
Robert Greenwaltf21ef7d2011-01-11 13:56:33 -08001126 if (mActiveDefaultNetwork != -1) {
1127 sendConnectedBroadcast(mNetTrackers[mActiveDefaultNetwork].getNetworkInfo());
Robert Greenwaltda03c4e2010-01-20 19:29:41 -08001128 }
1129 }
1130
Robert Greenwaltf21ef7d2011-01-11 13:56:33 -08001131 private void tryFailover(int prevNetType) {
Robert Greenwalt42acef32009-08-12 16:08:25 -07001132 /*
Robert Greenwaltbff90182011-01-06 15:41:07 -08001133 * If this is a default network, check if other defaults are available.
1134 * Try to reconnect on all available and let them hash it out when
1135 * more than one connects.
Robert Greenwalt42acef32009-08-12 16:08:25 -07001136 */
Robert Greenwaltd55a6b42011-03-25 13:09:25 -07001137 if (mNetConfigs[prevNetType].isDefault()) {
Robert Greenwalt42acef32009-08-12 16:08:25 -07001138 if (mActiveDefaultNetwork == prevNetType) {
1139 mActiveDefaultNetwork = -1;
1140 }
1141
Robert Greenwaltf21ef7d2011-01-11 13:56:33 -08001142 // don't signal a reconnect for anything lower or equal priority than our
1143 // current connected default
1144 // TODO - don't filter by priority now - nice optimization but risky
1145// int currentPriority = -1;
1146// if (mActiveDefaultNetwork != -1) {
Robert Greenwaltd55a6b42011-03-25 13:09:25 -07001147// currentPriority = mNetConfigs[mActiveDefaultNetwork].mPriority;
Robert Greenwaltf21ef7d2011-01-11 13:56:33 -08001148// }
Robert Greenwaltda03c4e2010-01-20 19:29:41 -08001149 for (int checkType=0; checkType <= ConnectivityManager.MAX_NETWORK_TYPE; checkType++) {
Robert Greenwalt5154ae762009-10-30 14:17:42 -07001150 if (checkType == prevNetType) continue;
Robert Greenwaltd55a6b42011-03-25 13:09:25 -07001151 if (mNetConfigs[checkType] == null) continue;
1152 if (!mNetConfigs[checkType].isDefault()) continue;
Wink Saville9f7a0b22011-01-26 15:43:49 -08001153
1154// Enabling the isAvailable() optimization caused mobile to not get
1155// selected if it was in the middle of error handling. Specifically
1156// a moble connection that took 30 seconds to complete the DEACTIVATE_DATA_CALL
1157// would not be available and we wouldn't get connected to anything.
1158// So removing the isAvailable() optimization below for now. TODO: This
1159// optimization should work and we need to investigate why it doesn't work.
1160// This could be related to how DEACTIVATE_DATA_CALL is reporting its
1161// complete before it is really complete.
1162// if (!mNetTrackers[checkType].isAvailable()) continue;
1163
Robert Greenwaltd55a6b42011-03-25 13:09:25 -07001164// if (currentPriority >= mNetConfigs[checkType].mPriority) continue;
Robert Greenwalt42acef32009-08-12 16:08:25 -07001165
Robert Greenwaltf21ef7d2011-01-11 13:56:33 -08001166 NetworkStateTracker checkTracker = mNetTrackers[checkType];
1167 NetworkInfo checkInfo = checkTracker.getNetworkInfo();
1168 if (!checkInfo.isConnectedOrConnecting() || checkTracker.isTeardownRequested()) {
1169 checkInfo.setFailover(true);
1170 checkTracker.reconnect();
Robert Greenwalt42acef32009-08-12 16:08:25 -07001171 }
Robert Greenwaltf21ef7d2011-01-11 13:56:33 -08001172 if (DBG) log("Attempting to switch to " + checkInfo.getTypeName());
Robert Greenwalt42acef32009-08-12 16:08:25 -07001173 }
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001174 }
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001175 }
1176
1177 private void sendConnectedBroadcast(NetworkInfo info) {
Robert Greenwalt1e9aac22010-09-15 17:36:33 -07001178 sendGeneralBroadcast(info, ConnectivityManager.CONNECTIVITY_ACTION);
1179 }
1180
1181 private void sendInetConditionBroadcast(NetworkInfo info) {
1182 sendGeneralBroadcast(info, ConnectivityManager.INET_CONDITION_ACTION);
1183 }
1184
1185 private void sendGeneralBroadcast(NetworkInfo info, String bcastType) {
1186 Intent intent = new Intent(bcastType);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001187 intent.putExtra(ConnectivityManager.EXTRA_NETWORK_INFO, info);
1188 if (info.isFailover()) {
1189 intent.putExtra(ConnectivityManager.EXTRA_IS_FAILOVER, true);
1190 info.setFailover(false);
1191 }
1192 if (info.getReason() != null) {
1193 intent.putExtra(ConnectivityManager.EXTRA_REASON, info.getReason());
1194 }
1195 if (info.getExtraInfo() != null) {
Robert Greenwalt86e9e552009-07-16 17:21:39 -07001196 intent.putExtra(ConnectivityManager.EXTRA_EXTRA_INFO,
1197 info.getExtraInfo());
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001198 }
Robert Greenwaltd7085fc2010-09-08 15:24:47 -07001199 intent.putExtra(ConnectivityManager.EXTRA_INET_CONDITION, mDefaultInetConditionPublished);
Mike Lockwood0f79b542009-08-14 14:18:49 -04001200 sendStickyBroadcast(intent);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001201 }
1202
1203 /**
1204 * Called when an attempt to fail over to another network has failed.
1205 * @param info the {@link NetworkInfo} for the failed network
1206 */
1207 private void handleConnectionFailure(NetworkInfo info) {
1208 mNetTrackers[info.getType()].setTeardownRequested(false);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001209
Robert Greenwalt42acef32009-08-12 16:08:25 -07001210 String reason = info.getReason();
1211 String extraInfo = info.getExtraInfo();
Robert Greenwalt86e9e552009-07-16 17:21:39 -07001212
Robert Greenwalt572172b2010-10-08 16:35:52 -07001213 String reasonText;
1214 if (reason == null) {
1215 reasonText = ".";
1216 } else {
1217 reasonText = " (" + reason + ").";
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001218 }
Wink Savilleed9c02b2010-12-03 12:01:38 -08001219 loge("Attempt to connect to " + info.getTypeName() + " failed" + reasonText);
Robert Greenwalt42acef32009-08-12 16:08:25 -07001220
1221 Intent intent = new Intent(ConnectivityManager.CONNECTIVITY_ACTION);
1222 intent.putExtra(ConnectivityManager.EXTRA_NETWORK_INFO, info);
1223 if (getActiveNetworkInfo() == null) {
1224 intent.putExtra(ConnectivityManager.EXTRA_NO_CONNECTIVITY, true);
1225 }
1226 if (reason != null) {
1227 intent.putExtra(ConnectivityManager.EXTRA_REASON, reason);
1228 }
1229 if (extraInfo != null) {
1230 intent.putExtra(ConnectivityManager.EXTRA_EXTRA_INFO, extraInfo);
1231 }
1232 if (info.isFailover()) {
1233 intent.putExtra(ConnectivityManager.EXTRA_IS_FAILOVER, true);
1234 info.setFailover(false);
1235 }
Robert Greenwaltda03c4e2010-01-20 19:29:41 -08001236
Robert Greenwaltd55a6b42011-03-25 13:09:25 -07001237 if (mNetConfigs[info.getType()].isDefault()) {
Robert Greenwaltf21ef7d2011-01-11 13:56:33 -08001238 tryFailover(info.getType());
1239 if (mActiveDefaultNetwork != -1) {
1240 NetworkInfo switchTo = mNetTrackers[mActiveDefaultNetwork].getNetworkInfo();
Robert Greenwaltcc4b4012010-01-25 17:54:29 -08001241 intent.putExtra(ConnectivityManager.EXTRA_OTHER_NETWORK_INFO, switchTo);
1242 } else {
Robert Greenwalt029be812010-09-20 18:01:43 -07001243 mDefaultInetConditionPublished = 0;
Robert Greenwaltcc4b4012010-01-25 17:54:29 -08001244 intent.putExtra(ConnectivityManager.EXTRA_NO_CONNECTIVITY, true);
1245 }
Robert Greenwaltda03c4e2010-01-20 19:29:41 -08001246 }
Robert Greenwaltcc4b4012010-01-25 17:54:29 -08001247
Robert Greenwalt029be812010-09-20 18:01:43 -07001248 intent.putExtra(ConnectivityManager.EXTRA_INET_CONDITION, mDefaultInetConditionPublished);
Mike Lockwood0f79b542009-08-14 14:18:49 -04001249 sendStickyBroadcast(intent);
Robert Greenwaltda03c4e2010-01-20 19:29:41 -08001250 /*
1251 * If the failover network is already connected, then immediately send
1252 * out a followup broadcast indicating successful failover
1253 */
Robert Greenwaltf21ef7d2011-01-11 13:56:33 -08001254 if (mActiveDefaultNetwork != -1) {
1255 sendConnectedBroadcast(mNetTrackers[mActiveDefaultNetwork].getNetworkInfo());
Robert Greenwaltda03c4e2010-01-20 19:29:41 -08001256 }
Mike Lockwood0f79b542009-08-14 14:18:49 -04001257 }
1258
1259 private void sendStickyBroadcast(Intent intent) {
1260 synchronized(this) {
Dianne Hackborn1c633fc2009-12-08 19:45:14 -08001261 if (!mSystemReady) {
1262 mInitialBroadcast = new Intent(intent);
Mike Lockwood0f79b542009-08-14 14:18:49 -04001263 }
Dianne Hackborn1c633fc2009-12-08 19:45:14 -08001264 intent.addFlags(Intent.FLAG_RECEIVER_REGISTERED_ONLY_BEFORE_BOOT);
1265 mContext.sendStickyBroadcast(intent);
Mike Lockwood0f79b542009-08-14 14:18:49 -04001266 }
1267 }
1268
1269 void systemReady() {
1270 synchronized(this) {
1271 mSystemReady = true;
Dianne Hackborn1c633fc2009-12-08 19:45:14 -08001272 if (mInitialBroadcast != null) {
1273 mContext.sendStickyBroadcast(mInitialBroadcast);
1274 mInitialBroadcast = null;
Mike Lockwood0f79b542009-08-14 14:18:49 -04001275 }
1276 }
Robert Greenwalt434203a2010-10-11 16:00:27 -07001277 // load the global proxy at startup
1278 mHandler.sendMessage(mHandler.obtainMessage(EVENT_APPLY_GLOBAL_HTTP_PROXY));
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001279 }
1280
1281 private void handleConnect(NetworkInfo info) {
Robert Greenwalt42acef32009-08-12 16:08:25 -07001282 int type = info.getType();
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001283
1284 // snapshot isFailover, because sendConnectedBroadcast() resets it
1285 boolean isFailover = info.isFailover();
Robert Greenwalt42acef32009-08-12 16:08:25 -07001286 NetworkStateTracker thisNet = mNetTrackers[type];
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001287
Robert Greenwalt42acef32009-08-12 16:08:25 -07001288 // if this is a default net and other default is running
1289 // kill the one not preferred
Robert Greenwaltd55a6b42011-03-25 13:09:25 -07001290 if (mNetConfigs[type].isDefault()) {
Robert Greenwalt42acef32009-08-12 16:08:25 -07001291 if (mActiveDefaultNetwork != -1 && mActiveDefaultNetwork != type) {
1292 if ((type != mNetworkPreference &&
Wink Saville975c8482011-04-07 14:23:45 -07001293 mNetConfigs[mActiveDefaultNetwork].priority >
1294 mNetConfigs[type].priority) ||
Robert Greenwalt42acef32009-08-12 16:08:25 -07001295 mNetworkPreference == mActiveDefaultNetwork) {
1296 // don't accept this one
Wink Savilleed9c02b2010-12-03 12:01:38 -08001297 if (DBG) {
1298 log("Not broadcasting CONNECT_ACTION " +
Robert Greenwalt42acef32009-08-12 16:08:25 -07001299 "to torn down network " + info.getTypeName());
Wink Savilleed9c02b2010-12-03 12:01:38 -08001300 }
Robert Greenwalt42acef32009-08-12 16:08:25 -07001301 teardown(thisNet);
1302 return;
1303 } else {
1304 // tear down the other
1305 NetworkStateTracker otherNet =
1306 mNetTrackers[mActiveDefaultNetwork];
Wink Savilleed9c02b2010-12-03 12:01:38 -08001307 if (DBG) {
1308 log("Policy requires " + otherNet.getNetworkInfo().getTypeName() +
Robert Greenwalt42acef32009-08-12 16:08:25 -07001309 " teardown");
Wink Savilleed9c02b2010-12-03 12:01:38 -08001310 }
Robert Greenwalt42acef32009-08-12 16:08:25 -07001311 if (!teardown(otherNet)) {
Wink Savilleed9c02b2010-12-03 12:01:38 -08001312 loge("Network declined teardown request");
Robert Greenwalt42acef32009-08-12 16:08:25 -07001313 return;
1314 }
Robert Greenwalt14f2ef42010-06-15 12:19:37 -07001315 }
1316 }
1317 synchronized (ConnectivityService.this) {
1318 // have a new default network, release the transition wakelock in a second
1319 // if it's held. The second pause is to allow apps to reconnect over the
1320 // new network
1321 if (mNetTransitionWakeLock.isHeld()) {
1322 mHandler.sendMessageDelayed(mHandler.obtainMessage(
Robert Greenwaltf3331232010-09-24 14:32:21 -07001323 EVENT_CLEAR_NET_TRANSITION_WAKELOCK,
Robert Greenwalt14f2ef42010-06-15 12:19:37 -07001324 mNetTransitionWakeLockSerialNumber, 0),
1325 1000);
Robert Greenwalt42acef32009-08-12 16:08:25 -07001326 }
1327 }
1328 mActiveDefaultNetwork = type;
Robert Greenwaltd7085fc2010-09-08 15:24:47 -07001329 // this will cause us to come up initially as unconnected and switching
1330 // to connected after our normal pause unless somebody reports us as reall
1331 // disconnected
1332 mDefaultInetConditionPublished = 0;
1333 mDefaultConnectionSequence++;
1334 mInetConditionChangeInFlight = false;
1335 // Don't do this - if we never sign in stay, grey
1336 //reportNetworkCondition(mActiveDefaultNetwork, 100);
Robert Greenwalt42acef32009-08-12 16:08:25 -07001337 }
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001338 thisNet.setTeardownRequested(false);
Irfan Sheriffd649c122010-06-09 15:39:36 -07001339 updateNetworkSettings(thisNet);
Robert Greenwaltc76b8fa2010-07-23 15:46:26 -07001340 handleConnectivityChange(type);
Robert Greenwalt42acef32009-08-12 16:08:25 -07001341 sendConnectedBroadcast(info);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001342 }
1343
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001344 /**
Robert Greenwaltc76b8fa2010-07-23 15:46:26 -07001345 * After a change in the connectivity state of a network. We're mainly
1346 * concerned with making sure that the list of DNS servers is set up
1347 * according to which networks are connected, and ensuring that the
1348 * right routing table entries exist.
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001349 */
Robert Greenwaltc76b8fa2010-07-23 15:46:26 -07001350 private void handleConnectivityChange(int netType) {
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001351 /*
Robert Greenwalt42acef32009-08-12 16:08:25 -07001352 * If a non-default network is enabled, add the host routes that
Robert Greenwaltc76b8fa2010-07-23 15:46:26 -07001353 * will allow it's DNS servers to be accessed.
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001354 */
Robert Greenwaltc76b8fa2010-07-23 15:46:26 -07001355 handleDnsConfigurationChange(netType);
Robert Greenwalt42acef32009-08-12 16:08:25 -07001356
Robert Greenwaltc76b8fa2010-07-23 15:46:26 -07001357 if (mNetTrackers[netType].getNetworkInfo().isConnected()) {
Robert Greenwaltd55a6b42011-03-25 13:09:25 -07001358 if (mNetConfigs[netType].isDefault()) {
Robert Greenwalt434203a2010-10-11 16:00:27 -07001359 handleApplyDefaultProxy(netType);
Robert Greenwaltc76b8fa2010-07-23 15:46:26 -07001360 addDefaultRoute(mNetTrackers[netType]);
Robert Greenwalt42acef32009-08-12 16:08:25 -07001361 } else {
Robert Greenwaltc76b8fa2010-07-23 15:46:26 -07001362 addPrivateDnsRoutes(mNetTrackers[netType]);
1363 }
1364 } else {
Robert Greenwaltd55a6b42011-03-25 13:09:25 -07001365 if (mNetConfigs[netType].isDefault()) {
Robert Greenwaltc76b8fa2010-07-23 15:46:26 -07001366 removeDefaultRoute(mNetTrackers[netType]);
1367 } else {
1368 removePrivateDnsRoutes(mNetTrackers[netType]);
Robert Greenwalt42acef32009-08-12 16:08:25 -07001369 }
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001370 }
1371 }
1372
Irfan Sheriffd649c122010-06-09 15:39:36 -07001373 private void addPrivateDnsRoutes(NetworkStateTracker nt) {
Irfan Sheriffd649c122010-06-09 15:39:36 -07001374 boolean privateDnsRouteSet = nt.isPrivateDnsRouteSet();
Robert Greenwalt37e65eb2010-08-30 10:56:47 -07001375 LinkProperties p = nt.getLinkProperties();
Robert Greenwalt47f69fe2010-06-15 15:43:39 -07001376 if (p == null) return;
1377 String interfaceName = p.getInterfaceName();
Irfan Sheriffd649c122010-06-09 15:39:36 -07001378
1379 if (DBG) {
Wink Savilleed9c02b2010-12-03 12:01:38 -08001380 log("addPrivateDnsRoutes for " + nt +
Irfan Sheriffd649c122010-06-09 15:39:36 -07001381 "(" + interfaceName + ") - mPrivateDnsRouteSet = " + privateDnsRouteSet);
1382 }
Irfan Sheriffd649c122010-06-09 15:39:36 -07001383 if (interfaceName != null && !privateDnsRouteSet) {
Robert Greenwalt47f69fe2010-06-15 15:43:39 -07001384 Collection<InetAddress> dnsList = p.getDnses();
1385 for (InetAddress dns : dnsList) {
Wink Savilleed9c02b2010-12-03 12:01:38 -08001386 if (DBG) log(" adding " + dns);
Robert Greenwalt585ac0f2010-08-27 09:24:29 -07001387 NetworkUtils.addHostRoute(interfaceName, dns, null);
Irfan Sheriffd649c122010-06-09 15:39:36 -07001388 }
1389 nt.privateDnsRouteSet(true);
1390 }
1391 }
1392
1393 private void removePrivateDnsRoutes(NetworkStateTracker nt) {
1394 // TODO - we should do this explicitly but the NetUtils api doesnt
1395 // support this yet - must remove all. No worse than before
Robert Greenwalt37e65eb2010-08-30 10:56:47 -07001396 LinkProperties p = nt.getLinkProperties();
Robert Greenwalt47f69fe2010-06-15 15:43:39 -07001397 if (p == null) return;
1398 String interfaceName = p.getInterfaceName();
Irfan Sheriffd649c122010-06-09 15:39:36 -07001399 boolean privateDnsRouteSet = nt.isPrivateDnsRouteSet();
1400 if (interfaceName != null && privateDnsRouteSet) {
1401 if (DBG) {
Wink Savilleed9c02b2010-12-03 12:01:38 -08001402 log("removePrivateDnsRoutes for " + nt.getNetworkInfo().getTypeName() +
Irfan Sheriffd649c122010-06-09 15:39:36 -07001403 " (" + interfaceName + ")");
1404 }
1405 NetworkUtils.removeHostRoutes(interfaceName);
1406 nt.privateDnsRouteSet(false);
1407 }
1408 }
1409
Irfan Sheriffd649c122010-06-09 15:39:36 -07001410
1411 private void addDefaultRoute(NetworkStateTracker nt) {
Robert Greenwalt37e65eb2010-08-30 10:56:47 -07001412 LinkProperties p = nt.getLinkProperties();
Robert Greenwalt47f69fe2010-06-15 15:43:39 -07001413 if (p == null) return;
1414 String interfaceName = p.getInterfaceName();
Robert Greenwalt992564e2011-02-09 13:56:06 -08001415 if (TextUtils.isEmpty(interfaceName)) return;
1416 for (InetAddress gateway : p.getGateways()) {
Irfan Sheriffd649c122010-06-09 15:39:36 -07001417
Robert Greenwaltedcb4f92011-03-22 18:47:42 -07001418 if (NetworkUtils.addHostRoute(interfaceName, gateway, null) &&
1419 NetworkUtils.addDefaultRoute(interfaceName, gateway)) {
1420 if (DBG) {
1421 NetworkInfo networkInfo = nt.getNetworkInfo();
1422 log("addDefaultRoute for " + networkInfo.getTypeName() +
1423 " (" + interfaceName + "), GatewayAddr=" + gateway.getHostAddress());
1424 }
Irfan Sheriffd649c122010-06-09 15:39:36 -07001425 }
Irfan Sheriffd649c122010-06-09 15:39:36 -07001426 }
1427 }
1428
1429
1430 public void removeDefaultRoute(NetworkStateTracker nt) {
Robert Greenwalt37e65eb2010-08-30 10:56:47 -07001431 LinkProperties p = nt.getLinkProperties();
Robert Greenwalt47f69fe2010-06-15 15:43:39 -07001432 if (p == null) return;
1433 String interfaceName = p.getInterfaceName();
Irfan Sheriffd649c122010-06-09 15:39:36 -07001434
Robert Greenwaltc76b8fa2010-07-23 15:46:26 -07001435 if (interfaceName != null) {
Robert Greenwaltedcb4f92011-03-22 18:47:42 -07001436 if (NetworkUtils.removeDefaultRoute(interfaceName) >= 0) {
1437 if (DBG) {
1438 NetworkInfo networkInfo = nt.getNetworkInfo();
1439 log("removeDefaultRoute for " + networkInfo.getTypeName() + " (" +
1440 interfaceName + ")");
1441 }
Irfan Sheriffd649c122010-06-09 15:39:36 -07001442 }
Irfan Sheriffd649c122010-06-09 15:39:36 -07001443 }
1444 }
1445
1446 /**
1447 * Reads the network specific TCP buffer sizes from SystemProperties
1448 * net.tcp.buffersize.[default|wifi|umts|edge|gprs] and set them for system
1449 * wide use
1450 */
1451 public void updateNetworkSettings(NetworkStateTracker nt) {
1452 String key = nt.getTcpBufferSizesPropName();
1453 String bufferSizes = SystemProperties.get(key);
1454
1455 if (bufferSizes.length() == 0) {
Wink Savilleed9c02b2010-12-03 12:01:38 -08001456 loge(key + " not found in system properties. Using defaults");
Irfan Sheriffd649c122010-06-09 15:39:36 -07001457
1458 // Setting to default values so we won't be stuck to previous values
1459 key = "net.tcp.buffersize.default";
1460 bufferSizes = SystemProperties.get(key);
1461 }
1462
1463 // Set values in kernel
1464 if (bufferSizes.length() != 0) {
1465 if (DBG) {
Wink Savilleed9c02b2010-12-03 12:01:38 -08001466 log("Setting TCP values: [" + bufferSizes
Irfan Sheriffd649c122010-06-09 15:39:36 -07001467 + "] which comes from [" + key + "]");
1468 }
1469 setBufferSize(bufferSizes);
1470 }
1471 }
1472
1473 /**
1474 * Writes TCP buffer sizes to /sys/kernel/ipv4/tcp_[r/w]mem_[min/def/max]
1475 * which maps to /proc/sys/net/ipv4/tcp_rmem and tcpwmem
1476 *
1477 * @param bufferSizes in the format of "readMin, readInitial, readMax,
1478 * writeMin, writeInitial, writeMax"
1479 */
1480 private void setBufferSize(String bufferSizes) {
1481 try {
1482 String[] values = bufferSizes.split(",");
1483
1484 if (values.length == 6) {
1485 final String prefix = "/sys/kernel/ipv4/tcp_";
1486 stringToFile(prefix + "rmem_min", values[0]);
1487 stringToFile(prefix + "rmem_def", values[1]);
1488 stringToFile(prefix + "rmem_max", values[2]);
1489 stringToFile(prefix + "wmem_min", values[3]);
1490 stringToFile(prefix + "wmem_def", values[4]);
1491 stringToFile(prefix + "wmem_max", values[5]);
1492 } else {
Wink Savilleed9c02b2010-12-03 12:01:38 -08001493 loge("Invalid buffersize string: " + bufferSizes);
Irfan Sheriffd649c122010-06-09 15:39:36 -07001494 }
1495 } catch (IOException e) {
Wink Savilleed9c02b2010-12-03 12:01:38 -08001496 loge("Can't set tcp buffer sizes:" + e);
Irfan Sheriffd649c122010-06-09 15:39:36 -07001497 }
1498 }
1499
1500 /**
1501 * Writes string to file. Basically same as "echo -n $string > $filename"
1502 *
1503 * @param filename
1504 * @param string
1505 * @throws IOException
1506 */
1507 private void stringToFile(String filename, String string) throws IOException {
1508 FileWriter out = new FileWriter(filename);
1509 try {
1510 out.write(string);
1511 } finally {
1512 out.close();
1513 }
1514 }
1515
1516
Robert Greenwalt42acef32009-08-12 16:08:25 -07001517 /**
1518 * Adjust the per-process dns entries (net.dns<x>.<pid>) based
1519 * on the highest priority active net which this process requested.
1520 * If there aren't any, clear it out
1521 */
1522 private void reassessPidDns(int myPid, boolean doBump)
1523 {
Wink Savilleed9c02b2010-12-03 12:01:38 -08001524 if (DBG) log("reassessPidDns for pid " + myPid);
Robert Greenwalt42acef32009-08-12 16:08:25 -07001525 for(int i : mPriorityList) {
Robert Greenwaltd55a6b42011-03-25 13:09:25 -07001526 if (mNetConfigs[i].isDefault()) {
Robert Greenwalt42acef32009-08-12 16:08:25 -07001527 continue;
1528 }
1529 NetworkStateTracker nt = mNetTrackers[i];
Robert Greenwalt86e9e552009-07-16 17:21:39 -07001530 if (nt.getNetworkInfo().isConnected() &&
1531 !nt.isTeardownRequested()) {
Robert Greenwalt37e65eb2010-08-30 10:56:47 -07001532 LinkProperties p = nt.getLinkProperties();
Robert Greenwalt47f69fe2010-06-15 15:43:39 -07001533 if (p == null) continue;
Robert Greenwalt42acef32009-08-12 16:08:25 -07001534 List pids = mNetRequestersPids[i];
1535 for (int j=0; j<pids.size(); j++) {
1536 Integer pid = (Integer)pids.get(j);
1537 if (pid.intValue() == myPid) {
Robert Greenwalt47f69fe2010-06-15 15:43:39 -07001538 Collection<InetAddress> dnses = p.getDnses();
1539 writePidDns(dnses, myPid);
Robert Greenwalt42acef32009-08-12 16:08:25 -07001540 if (doBump) {
1541 bumpDns();
1542 }
1543 return;
1544 }
1545 }
1546 }
1547 }
1548 // nothing found - delete
1549 for (int i = 1; ; i++) {
1550 String prop = "net.dns" + i + "." + myPid;
1551 if (SystemProperties.get(prop).length() == 0) {
1552 if (doBump) {
1553 bumpDns();
1554 }
1555 return;
1556 }
1557 SystemProperties.set(prop, "");
1558 }
1559 }
1560
Robert Greenwalt10398722010-12-17 15:20:36 -08001561 // return true if results in a change
1562 private boolean writePidDns(Collection <InetAddress> dnses, int pid) {
Robert Greenwalt42acef32009-08-12 16:08:25 -07001563 int j = 1;
Robert Greenwalt10398722010-12-17 15:20:36 -08001564 boolean changed = false;
Robert Greenwalt47f69fe2010-06-15 15:43:39 -07001565 for (InetAddress dns : dnses) {
Robert Greenwalt10398722010-12-17 15:20:36 -08001566 String dnsString = dns.getHostAddress();
1567 if (changed || !dnsString.equals(SystemProperties.get("net.dns" + j + "." + pid))) {
1568 changed = true;
1569 SystemProperties.set("net.dns" + j++ + "." + pid, dns.getHostAddress());
1570 }
Robert Greenwalt42acef32009-08-12 16:08:25 -07001571 }
Robert Greenwalt10398722010-12-17 15:20:36 -08001572 return changed;
Robert Greenwalt42acef32009-08-12 16:08:25 -07001573 }
1574
1575 private void bumpDns() {
1576 /*
1577 * Bump the property that tells the name resolver library to reread
1578 * the DNS server list from the properties.
1579 */
1580 String propVal = SystemProperties.get("net.dnschange");
1581 int n = 0;
1582 if (propVal.length() != 0) {
1583 try {
1584 n = Integer.parseInt(propVal);
1585 } catch (NumberFormatException e) {}
1586 }
1587 SystemProperties.set("net.dnschange", "" + (n+1));
Robert Greenwalt03595d02010-11-02 14:08:23 -07001588 /*
1589 * Tell the VMs to toss their DNS caches
1590 */
1591 Intent intent = new Intent(Intent.ACTION_CLEAR_DNS_CACHE);
1592 intent.addFlags(Intent.FLAG_RECEIVER_REPLACE_PENDING);
Stan Chesnutt3d1db862011-01-05 17:14:03 -08001593 /*
1594 * Connectivity events can happen before boot has completed ...
1595 */
1596 intent.addFlags(Intent.FLAG_RECEIVER_REGISTERED_ONLY_BEFORE_BOOT);
Robert Greenwalt03595d02010-11-02 14:08:23 -07001597 mContext.sendBroadcast(intent);
Robert Greenwalt42acef32009-08-12 16:08:25 -07001598 }
1599
Robert Greenwaltc76b8fa2010-07-23 15:46:26 -07001600 private void handleDnsConfigurationChange(int netType) {
Robert Greenwalt42acef32009-08-12 16:08:25 -07001601 // add default net's dns entries
Robert Greenwaltc76b8fa2010-07-23 15:46:26 -07001602 NetworkStateTracker nt = mNetTrackers[netType];
1603 if (nt != null && nt.getNetworkInfo().isConnected() && !nt.isTeardownRequested()) {
Robert Greenwalt37e65eb2010-08-30 10:56:47 -07001604 LinkProperties p = nt.getLinkProperties();
Robert Greenwaltc76b8fa2010-07-23 15:46:26 -07001605 if (p == null) return;
1606 Collection<InetAddress> dnses = p.getDnses();
Robert Greenwalt10398722010-12-17 15:20:36 -08001607 boolean changed = false;
Robert Greenwaltd55a6b42011-03-25 13:09:25 -07001608 if (mNetConfigs[netType].isDefault()) {
Robert Greenwaltc76b8fa2010-07-23 15:46:26 -07001609 int j = 1;
Robert Greenwalte90aa5e2010-09-01 11:34:05 -07001610 if (dnses.size() == 0 && mDefaultDns != null) {
Robert Greenwalt10398722010-12-17 15:20:36 -08001611 String dnsString = mDefaultDns.getHostAddress();
1612 if (!dnsString.equals(SystemProperties.get("net.dns1"))) {
1613 if (DBG) {
1614 log("no dns provided - using " + dnsString);
1615 }
1616 changed = true;
1617 SystemProperties.set("net.dns1", dnsString);
Robert Greenwalt42acef32009-08-12 16:08:25 -07001618 }
Robert Greenwalte90aa5e2010-09-01 11:34:05 -07001619 j++;
1620 } else {
1621 for (InetAddress dns : dnses) {
Robert Greenwalt10398722010-12-17 15:20:36 -08001622 String dnsString = dns.getHostAddress();
1623 if (!changed && dnsString.equals(SystemProperties.get("net.dns" + j))) {
1624 j++;
1625 continue;
1626 }
Robert Greenwalte90aa5e2010-09-01 11:34:05 -07001627 if (DBG) {
Wink Savilleed9c02b2010-12-03 12:01:38 -08001628 log("adding dns " + dns + " for " +
Robert Greenwalte90aa5e2010-09-01 11:34:05 -07001629 nt.getNetworkInfo().getTypeName());
1630 }
Robert Greenwalt10398722010-12-17 15:20:36 -08001631 changed = true;
1632 SystemProperties.set("net.dns" + j++, dnsString);
Robert Greenwalte90aa5e2010-09-01 11:34:05 -07001633 }
Robert Greenwaltc76b8fa2010-07-23 15:46:26 -07001634 }
1635 for (int k=j ; k<mNumDnsEntries; k++) {
Robert Greenwalt10398722010-12-17 15:20:36 -08001636 if (changed || !TextUtils.isEmpty(SystemProperties.get("net.dns" + k))) {
1637 if (DBG) log("erasing net.dns" + k);
1638 changed = true;
1639 SystemProperties.set("net.dns" + k, "");
1640 }
Robert Greenwaltc76b8fa2010-07-23 15:46:26 -07001641 }
1642 mNumDnsEntries = j;
1643 } else {
1644 // set per-pid dns for attached secondary nets
1645 List pids = mNetRequestersPids[netType];
1646 for (int y=0; y< pids.size(); y++) {
1647 Integer pid = (Integer)pids.get(y);
Robert Greenwalt10398722010-12-17 15:20:36 -08001648 changed = writePidDns(dnses, pid.intValue());
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001649 }
1650 }
Robert Greenwalt10398722010-12-17 15:20:36 -08001651 if (changed) bumpDns();
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001652 }
Robert Greenwalt42acef32009-08-12 16:08:25 -07001653 }
1654
1655 private int getRestoreDefaultNetworkDelay() {
1656 String restoreDefaultNetworkDelayStr = SystemProperties.get(
1657 NETWORK_RESTORE_DELAY_PROP_NAME);
1658 if(restoreDefaultNetworkDelayStr != null &&
1659 restoreDefaultNetworkDelayStr.length() != 0) {
1660 try {
1661 return Integer.valueOf(restoreDefaultNetworkDelayStr);
1662 } catch (NumberFormatException e) {
1663 }
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001664 }
Robert Greenwalt42acef32009-08-12 16:08:25 -07001665 return RESTORE_DEFAULT_NETWORK_DELAY;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001666 }
1667
1668 @Override
1669 protected void dump(FileDescriptor fd, PrintWriter pw, String[] args) {
Robert Greenwalt86e9e552009-07-16 17:21:39 -07001670 if (mContext.checkCallingOrSelfPermission(
1671 android.Manifest.permission.DUMP)
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001672 != PackageManager.PERMISSION_GRANTED) {
Robert Greenwalt86e9e552009-07-16 17:21:39 -07001673 pw.println("Permission Denial: can't dump ConnectivityService " +
1674 "from from pid=" + Binder.getCallingPid() + ", uid=" +
1675 Binder.getCallingUid());
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001676 return;
1677 }
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001678 pw.println();
1679 for (NetworkStateTracker nst : mNetTrackers) {
Robert Greenwaltb9285352009-12-21 18:24:07 -08001680 if (nst != null) {
1681 if (nst.getNetworkInfo().isConnected()) {
1682 pw.println("Active network: " + nst.getNetworkInfo().
1683 getTypeName());
1684 }
1685 pw.println(nst.getNetworkInfo());
1686 pw.println(nst);
1687 pw.println();
Robert Greenwalt42acef32009-08-12 16:08:25 -07001688 }
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001689 }
Robert Greenwaltb9285352009-12-21 18:24:07 -08001690
1691 pw.println("Network Requester Pids:");
1692 for (int net : mPriorityList) {
1693 String pidString = net + ": ";
1694 for (Object pid : mNetRequestersPids[net]) {
1695 pidString = pidString + pid.toString() + ", ";
1696 }
1697 pw.println(pidString);
1698 }
1699 pw.println();
1700
1701 pw.println("FeatureUsers:");
1702 for (Object requester : mFeatureUsers) {
1703 pw.println(requester.toString());
1704 }
1705 pw.println();
Robert Greenwalt2a091d72010-02-11 18:18:40 -08001706
Robert Greenwalt14f2ef42010-06-15 12:19:37 -07001707 synchronized (this) {
1708 pw.println("NetworkTranstionWakeLock is currently " +
1709 (mNetTransitionWakeLock.isHeld() ? "" : "not ") + "held.");
1710 pw.println("It was last requested for "+mNetTransitionWakeLockCausedBy);
1711 }
1712 pw.println();
1713
Robert Greenwalt2a091d72010-02-11 18:18:40 -08001714 mTethering.dump(fd, pw, args);
Robert Greenwalt4e8dfef2010-09-20 14:35:25 -07001715
1716 if (mInetLog != null) {
1717 pw.println();
1718 pw.println("Inet condition reports:");
1719 for(int i = 0; i < mInetLog.size(); i++) {
1720 pw.println(mInetLog.get(i));
1721 }
1722 }
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001723 }
1724
Robert Greenwalt42acef32009-08-12 16:08:25 -07001725 // must be stateless - things change under us.
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001726 private class MyHandler extends Handler {
Wink Savillebb08caf2010-09-02 19:23:52 -07001727 public MyHandler(Looper looper) {
1728 super(looper);
1729 }
1730
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001731 @Override
1732 public void handleMessage(Message msg) {
1733 NetworkInfo info;
1734 switch (msg.what) {
1735 case NetworkStateTracker.EVENT_STATE_CHANGED:
1736 info = (NetworkInfo) msg.obj;
Robert Greenwalt511288a2009-12-07 11:33:18 -08001737 int type = info.getType();
1738 NetworkInfo.State state = info.getState();
Robert Greenwalt511288a2009-12-07 11:33:18 -08001739
Wink Savilleed9c02b2010-12-03 12:01:38 -08001740 if (DBG) log("ConnectivityChange for " +
Robert Greenwalt86e9e552009-07-16 17:21:39 -07001741 info.getTypeName() + ": " +
Robert Greenwalt511288a2009-12-07 11:33:18 -08001742 state + "/" + info.getDetailedState());
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001743
1744 // Connectivity state changed:
1745 // [31-13] Reserved for future use
Robert Greenwalt86e9e552009-07-16 17:21:39 -07001746 // [12-9] Network subtype (for mobile network, as defined
1747 // by TelephonyManager)
1748 // [8-3] Detailed state ordinal (as defined by
1749 // NetworkInfo.DetailedState)
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001750 // [2-0] Network type (as defined by ConnectivityManager)
1751 int eventLogParam = (info.getType() & 0x7) |
1752 ((info.getDetailedState().ordinal() & 0x3f) << 3) |
1753 (info.getSubtype() << 9);
Doug Zongkerab5c49c2009-12-04 10:31:43 -08001754 EventLog.writeEvent(EventLogTags.CONNECTIVITY_STATE_CHANGED,
Robert Greenwalt86e9e552009-07-16 17:21:39 -07001755 eventLogParam);
1756
1757 if (info.getDetailedState() ==
1758 NetworkInfo.DetailedState.FAILED) {
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001759 handleConnectionFailure(info);
Robert Greenwalt511288a2009-12-07 11:33:18 -08001760 } else if (state == NetworkInfo.State.DISCONNECTED) {
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001761 handleDisconnect(info);
Robert Greenwalt511288a2009-12-07 11:33:18 -08001762 } else if (state == NetworkInfo.State.SUSPENDED) {
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001763 // TODO: need to think this over.
Robert Greenwalt86e9e552009-07-16 17:21:39 -07001764 // the logic here is, handle SUSPENDED the same as
1765 // DISCONNECTED. The only difference being we are
1766 // broadcasting an intent with NetworkInfo that's
1767 // suspended. This allows the applications an
1768 // opportunity to handle DISCONNECTED and SUSPENDED
1769 // differently, or not.
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001770 handleDisconnect(info);
Robert Greenwalt511288a2009-12-07 11:33:18 -08001771 } else if (state == NetworkInfo.State.CONNECTED) {
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001772 handleConnect(info);
1773 }
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001774 break;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001775 case NetworkStateTracker.EVENT_CONFIGURATION_CHANGED:
Robert Greenwaltc76b8fa2010-07-23 15:46:26 -07001776 info = (NetworkInfo) msg.obj;
Robert Greenwaltd55a6b42011-03-25 13:09:25 -07001777 handleConnectivityChange(info.getType());
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001778 break;
Robert Greenwaltf3331232010-09-24 14:32:21 -07001779 case EVENT_CLEAR_NET_TRANSITION_WAKELOCK:
Robert Greenwalt14f2ef42010-06-15 12:19:37 -07001780 String causedBy = null;
1781 synchronized (ConnectivityService.this) {
1782 if (msg.arg1 == mNetTransitionWakeLockSerialNumber &&
1783 mNetTransitionWakeLock.isHeld()) {
1784 mNetTransitionWakeLock.release();
1785 causedBy = mNetTransitionWakeLockCausedBy;
1786 }
1787 }
1788 if (causedBy != null) {
Wink Savilleed9c02b2010-12-03 12:01:38 -08001789 log("NetTransition Wakelock for " + causedBy + " released by timeout");
Robert Greenwalt14f2ef42010-06-15 12:19:37 -07001790 }
Robert Greenwalt057d5e92010-09-09 14:05:10 -07001791 break;
Robert Greenwalt8dcc28b2010-09-23 10:05:56 -07001792 case EVENT_RESTORE_DEFAULT_NETWORK:
Robert Greenwalt9c75d4a2009-09-27 17:27:04 -07001793 FeatureUser u = (FeatureUser)msg.obj;
1794 u.expire();
Robert Greenwaltd7085fc2010-09-08 15:24:47 -07001795 break;
Robert Greenwalt8dcc28b2010-09-23 10:05:56 -07001796 case EVENT_INET_CONDITION_CHANGE:
1797 {
1798 int netType = msg.arg1;
1799 int condition = msg.arg2;
1800 handleInetConditionChange(netType, condition);
Robert Greenwalt14f2ef42010-06-15 12:19:37 -07001801 break;
Robert Greenwalt8dcc28b2010-09-23 10:05:56 -07001802 }
1803 case EVENT_INET_CONDITION_HOLD_END:
1804 {
1805 int netType = msg.arg1;
1806 int sequence = msg.arg2;
1807 handleInetConditionHoldEnd(netType, sequence);
Robert Greenwaltd7085fc2010-09-08 15:24:47 -07001808 break;
Robert Greenwalt8dcc28b2010-09-23 10:05:56 -07001809 }
1810 case EVENT_SET_NETWORK_PREFERENCE:
1811 {
1812 int preference = msg.arg1;
1813 handleSetNetworkPreference(preference);
1814 break;
1815 }
1816 case EVENT_SET_BACKGROUND_DATA:
1817 {
1818 boolean enabled = (msg.arg1 == ENABLED);
1819 handleSetBackgroundData(enabled);
1820 break;
1821 }
1822 case EVENT_SET_MOBILE_DATA:
1823 {
1824 boolean enabled = (msg.arg1 == ENABLED);
1825 handleSetMobileData(enabled);
1826 break;
1827 }
Robert Greenwalt434203a2010-10-11 16:00:27 -07001828 case EVENT_APPLY_GLOBAL_HTTP_PROXY:
1829 {
1830 handleDeprecatedGlobalHttpProxy();
Robert Greenwaltd55a6b42011-03-25 13:09:25 -07001831 break;
1832 }
1833 case EVENT_SET_DEPENDENCY_MET:
1834 {
1835 boolean met = (msg.arg1 == ENABLED);
1836 handleSetDependencyMet(msg.arg2, met);
1837 break;
Robert Greenwalt434203a2010-10-11 16:00:27 -07001838 }
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001839 }
1840 }
1841 }
Robert Greenwaltd0e18ff2010-01-26 11:40:34 -08001842
1843 // javadoc from interface
Robert Greenwalt5a735062010-03-02 17:25:02 -08001844 public int tether(String iface) {
Robert Greenwaltd0e18ff2010-01-26 11:40:34 -08001845 enforceTetherChangePermission();
Robert Greenwalt5a735062010-03-02 17:25:02 -08001846
1847 if (isTetheringSupported()) {
1848 return mTethering.tether(iface);
1849 } else {
1850 return ConnectivityManager.TETHER_ERROR_UNSUPPORTED;
1851 }
Robert Greenwaltd0e18ff2010-01-26 11:40:34 -08001852 }
1853
1854 // javadoc from interface
Robert Greenwalt5a735062010-03-02 17:25:02 -08001855 public int untether(String iface) {
Robert Greenwaltd0e18ff2010-01-26 11:40:34 -08001856 enforceTetherChangePermission();
Robert Greenwalt5a735062010-03-02 17:25:02 -08001857
1858 if (isTetheringSupported()) {
1859 return mTethering.untether(iface);
1860 } else {
1861 return ConnectivityManager.TETHER_ERROR_UNSUPPORTED;
1862 }
1863 }
1864
1865 // javadoc from interface
1866 public int getLastTetherError(String iface) {
1867 enforceTetherAccessPermission();
1868
1869 if (isTetheringSupported()) {
1870 return mTethering.getLastTetherError(iface);
1871 } else {
1872 return ConnectivityManager.TETHER_ERROR_UNSUPPORTED;
1873 }
Robert Greenwalt2a091d72010-02-11 18:18:40 -08001874 }
1875
1876 // TODO - proper iface API for selection by property, inspection, etc
1877 public String[] getTetherableUsbRegexs() {
1878 enforceTetherAccessPermission();
1879 if (isTetheringSupported()) {
1880 return mTethering.getTetherableUsbRegexs();
1881 } else {
1882 return new String[0];
1883 }
1884 }
1885
1886 public String[] getTetherableWifiRegexs() {
1887 enforceTetherAccessPermission();
1888 if (isTetheringSupported()) {
1889 return mTethering.getTetherableWifiRegexs();
1890 } else {
1891 return new String[0];
1892 }
Robert Greenwaltd0e18ff2010-01-26 11:40:34 -08001893 }
1894
Danica Chang6fdd0c62010-08-11 14:54:43 -07001895 public String[] getTetherableBluetoothRegexs() {
1896 enforceTetherAccessPermission();
1897 if (isTetheringSupported()) {
1898 return mTethering.getTetherableBluetoothRegexs();
1899 } else {
1900 return new String[0];
1901 }
1902 }
1903
Robert Greenwaltd0e18ff2010-01-26 11:40:34 -08001904 // TODO - move iface listing, queries, etc to new module
1905 // javadoc from interface
1906 public String[] getTetherableIfaces() {
Robert Greenwalt2a091d72010-02-11 18:18:40 -08001907 enforceTetherAccessPermission();
Robert Greenwaltd0e18ff2010-01-26 11:40:34 -08001908 return mTethering.getTetherableIfaces();
1909 }
1910
1911 public String[] getTetheredIfaces() {
Robert Greenwalt2a091d72010-02-11 18:18:40 -08001912 enforceTetherAccessPermission();
Robert Greenwaltd0e18ff2010-01-26 11:40:34 -08001913 return mTethering.getTetheredIfaces();
1914 }
Robert Greenwalt2a091d72010-02-11 18:18:40 -08001915
Robert Greenwalt5a735062010-03-02 17:25:02 -08001916 public String[] getTetheringErroredIfaces() {
1917 enforceTetherAccessPermission();
1918 return mTethering.getErroredIfaces();
1919 }
1920
Robert Greenwalt2a091d72010-02-11 18:18:40 -08001921 // if ro.tether.denied = true we default to no tethering
1922 // gservices could set the secure setting to 1 though to enable it on a build where it
1923 // had previously been turned off.
1924 public boolean isTetheringSupported() {
1925 enforceTetherAccessPermission();
1926 int defaultVal = (SystemProperties.get("ro.tether.denied").equals("true") ? 0 : 1);
Robert Greenwaltc9d5fb72010-02-25 12:29:30 -08001927 boolean tetherEnabledInSettings = (Settings.Secure.getInt(mContext.getContentResolver(),
1928 Settings.Secure.TETHER_SUPPORTED, defaultVal) != 0);
1929 return tetherEnabledInSettings && mTetheringConfigValid;
Robert Greenwalt2a091d72010-02-11 18:18:40 -08001930 }
Robert Greenwalt14f2ef42010-06-15 12:19:37 -07001931
1932 // An API NetworkStateTrackers can call when they lose their network.
1933 // This will automatically be cleared after X seconds or a network becomes CONNECTED,
1934 // whichever happens first. The timer is started by the first caller and not
1935 // restarted by subsequent callers.
1936 public void requestNetworkTransitionWakelock(String forWhom) {
1937 enforceConnectivityInternalPermission();
1938 synchronized (this) {
1939 if (mNetTransitionWakeLock.isHeld()) return;
1940 mNetTransitionWakeLockSerialNumber++;
1941 mNetTransitionWakeLock.acquire();
1942 mNetTransitionWakeLockCausedBy = forWhom;
1943 }
1944 mHandler.sendMessageDelayed(mHandler.obtainMessage(
Robert Greenwaltf3331232010-09-24 14:32:21 -07001945 EVENT_CLEAR_NET_TRANSITION_WAKELOCK,
Robert Greenwalt14f2ef42010-06-15 12:19:37 -07001946 mNetTransitionWakeLockSerialNumber, 0),
1947 mNetTransitionWakeLockTimeout);
1948 return;
1949 }
Robert Greenwaltca4306c2010-09-09 13:15:32 -07001950
Robert Greenwaltd7085fc2010-09-08 15:24:47 -07001951 // 100 percent is full good, 0 is full bad.
1952 public void reportInetCondition(int networkType, int percentage) {
Wink Savilleed9c02b2010-12-03 12:01:38 -08001953 if (DBG) log("reportNetworkCondition(" + networkType + ", " + percentage + ")");
Robert Greenwaltd7085fc2010-09-08 15:24:47 -07001954 mContext.enforceCallingOrSelfPermission(
1955 android.Manifest.permission.STATUS_BAR,
1956 "ConnectivityService");
1957
Robert Greenwalt4e8dfef2010-09-20 14:35:25 -07001958 if (DBG) {
1959 int pid = getCallingPid();
1960 int uid = getCallingUid();
1961 String s = pid + "(" + uid + ") reports inet is " +
1962 (percentage > 50 ? "connected" : "disconnected") + " (" + percentage + ") on " +
1963 "network Type " + networkType + " at " + GregorianCalendar.getInstance().getTime();
1964 mInetLog.add(s);
1965 while(mInetLog.size() > INET_CONDITION_LOG_MAX_SIZE) {
1966 mInetLog.remove(0);
1967 }
1968 }
Robert Greenwaltd7085fc2010-09-08 15:24:47 -07001969 mHandler.sendMessage(mHandler.obtainMessage(
Robert Greenwalt8dcc28b2010-09-23 10:05:56 -07001970 EVENT_INET_CONDITION_CHANGE, networkType, percentage));
1971 }
1972
1973 private void handleInetConditionChange(int netType, int condition) {
1974 if (DBG) {
Wink Savilleed9c02b2010-12-03 12:01:38 -08001975 log("Inet connectivity change, net=" +
Robert Greenwalt8dcc28b2010-09-23 10:05:56 -07001976 netType + ", condition=" + condition +
1977 ",mActiveDefaultNetwork=" + mActiveDefaultNetwork);
1978 }
1979 if (mActiveDefaultNetwork == -1) {
Wink Savilleed9c02b2010-12-03 12:01:38 -08001980 if (DBG) log("no active default network - aborting");
Robert Greenwalt8dcc28b2010-09-23 10:05:56 -07001981 return;
1982 }
1983 if (mActiveDefaultNetwork != netType) {
Wink Savilleed9c02b2010-12-03 12:01:38 -08001984 if (DBG) log("given net not default - aborting");
Robert Greenwalt8dcc28b2010-09-23 10:05:56 -07001985 return;
1986 }
1987 mDefaultInetCondition = condition;
1988 int delay;
1989 if (mInetConditionChangeInFlight == false) {
Wink Savilleed9c02b2010-12-03 12:01:38 -08001990 if (DBG) log("starting a change hold");
Robert Greenwalt8dcc28b2010-09-23 10:05:56 -07001991 // setup a new hold to debounce this
1992 if (mDefaultInetCondition > 50) {
1993 delay = Settings.Secure.getInt(mContext.getContentResolver(),
1994 Settings.Secure.INET_CONDITION_DEBOUNCE_UP_DELAY, 500);
1995 } else {
1996 delay = Settings.Secure.getInt(mContext.getContentResolver(),
1997 Settings.Secure.INET_CONDITION_DEBOUNCE_DOWN_DELAY, 3000);
1998 }
1999 mInetConditionChangeInFlight = true;
2000 mHandler.sendMessageDelayed(mHandler.obtainMessage(EVENT_INET_CONDITION_HOLD_END,
2001 mActiveDefaultNetwork, mDefaultConnectionSequence), delay);
2002 } else {
2003 // we've set the new condition, when this hold ends that will get
2004 // picked up
Wink Savilleed9c02b2010-12-03 12:01:38 -08002005 if (DBG) log("currently in hold - not setting new end evt");
Robert Greenwalt8dcc28b2010-09-23 10:05:56 -07002006 }
2007 }
2008
2009 private void handleInetConditionHoldEnd(int netType, int sequence) {
2010 if (DBG) {
Wink Savilleed9c02b2010-12-03 12:01:38 -08002011 log("Inet hold end, net=" + netType +
Robert Greenwalt8dcc28b2010-09-23 10:05:56 -07002012 ", condition =" + mDefaultInetCondition +
2013 ", published condition =" + mDefaultInetConditionPublished);
2014 }
2015 mInetConditionChangeInFlight = false;
2016
2017 if (mActiveDefaultNetwork == -1) {
Wink Savilleed9c02b2010-12-03 12:01:38 -08002018 if (DBG) log("no active default network - aborting");
Robert Greenwalt8dcc28b2010-09-23 10:05:56 -07002019 return;
2020 }
2021 if (mDefaultConnectionSequence != sequence) {
Wink Savilleed9c02b2010-12-03 12:01:38 -08002022 if (DBG) log("event hold for obsolete network - aborting");
Robert Greenwalt8dcc28b2010-09-23 10:05:56 -07002023 return;
2024 }
2025 if (mDefaultInetConditionPublished == mDefaultInetCondition) {
Wink Savilleed9c02b2010-12-03 12:01:38 -08002026 if (DBG) log("no change in condition - aborting");
Robert Greenwalt8dcc28b2010-09-23 10:05:56 -07002027 return;
2028 }
2029 NetworkInfo networkInfo = mNetTrackers[mActiveDefaultNetwork].getNetworkInfo();
2030 if (networkInfo.isConnected() == false) {
Wink Savilleed9c02b2010-12-03 12:01:38 -08002031 if (DBG) log("default network not connected - aborting");
Robert Greenwalt8dcc28b2010-09-23 10:05:56 -07002032 return;
2033 }
2034 mDefaultInetConditionPublished = mDefaultInetCondition;
2035 sendInetConditionBroadcast(networkInfo);
2036 return;
Robert Greenwaltd7085fc2010-09-08 15:24:47 -07002037 }
Robert Greenwalt434203a2010-10-11 16:00:27 -07002038
2039 public synchronized ProxyProperties getProxy() {
2040 if (mGlobalProxy != null) return mGlobalProxy;
2041 if (mDefaultProxy != null) return mDefaultProxy;
2042 return null;
2043 }
2044
2045 public void setGlobalProxy(ProxyProperties proxyProperties) {
2046 enforceChangePermission();
2047 synchronized (mGlobalProxyLock) {
2048 if (proxyProperties == mGlobalProxy) return;
2049 if (proxyProperties != null && proxyProperties.equals(mGlobalProxy)) return;
2050 if (mGlobalProxy != null && mGlobalProxy.equals(proxyProperties)) return;
2051
2052 String host = "";
2053 int port = 0;
2054 String exclList = "";
2055 if (proxyProperties != null && !TextUtils.isEmpty(proxyProperties.getHost())) {
2056 mGlobalProxy = new ProxyProperties(proxyProperties);
2057 host = mGlobalProxy.getHost();
2058 port = mGlobalProxy.getPort();
2059 exclList = mGlobalProxy.getExclusionList();
2060 } else {
2061 mGlobalProxy = null;
2062 }
2063 ContentResolver res = mContext.getContentResolver();
2064 Settings.Secure.putString(res, Settings.Secure.GLOBAL_HTTP_PROXY_HOST, host);
2065 Settings.Secure.putInt(res, Settings.Secure.GLOBAL_HTTP_PROXY_PORT, port);
Robert Greenwaltb7090d62010-12-02 11:31:00 -08002066 Settings.Secure.putString(res, Settings.Secure.GLOBAL_HTTP_PROXY_EXCLUSION_LIST,
Robert Greenwalt434203a2010-10-11 16:00:27 -07002067 exclList);
2068 }
2069
2070 if (mGlobalProxy == null) {
2071 proxyProperties = mDefaultProxy;
2072 }
2073 sendProxyBroadcast(proxyProperties);
2074 }
2075
Robert Greenwaltb7090d62010-12-02 11:31:00 -08002076 private void loadGlobalProxy() {
2077 ContentResolver res = mContext.getContentResolver();
2078 String host = Settings.Secure.getString(res, Settings.Secure.GLOBAL_HTTP_PROXY_HOST);
2079 int port = Settings.Secure.getInt(res, Settings.Secure.GLOBAL_HTTP_PROXY_PORT, 0);
2080 String exclList = Settings.Secure.getString(res,
2081 Settings.Secure.GLOBAL_HTTP_PROXY_EXCLUSION_LIST);
2082 if (!TextUtils.isEmpty(host)) {
2083 ProxyProperties proxyProperties = new ProxyProperties(host, port, exclList);
2084 synchronized (mGlobalProxyLock) {
2085 mGlobalProxy = proxyProperties;
2086 }
2087 }
2088 }
2089
Robert Greenwalt434203a2010-10-11 16:00:27 -07002090 public ProxyProperties getGlobalProxy() {
2091 synchronized (mGlobalProxyLock) {
2092 return mGlobalProxy;
2093 }
2094 }
2095
2096 private void handleApplyDefaultProxy(int type) {
2097 // check if new default - push it out to all VM if so
2098 ProxyProperties proxy = mNetTrackers[type].getLinkProperties().getHttpProxy();
2099 synchronized (this) {
2100 if (mDefaultProxy != null && mDefaultProxy.equals(proxy)) return;
2101 if (mDefaultProxy == proxy) return;
2102 if (!TextUtils.isEmpty(proxy.getHost())) {
2103 mDefaultProxy = proxy;
2104 } else {
2105 mDefaultProxy = null;
2106 }
2107 }
Wink Savilleed9c02b2010-12-03 12:01:38 -08002108 if (DBG) log("changing default proxy to " + proxy);
Robert Greenwalt434203a2010-10-11 16:00:27 -07002109 if ((proxy == null && mGlobalProxy == null) || proxy.equals(mGlobalProxy)) return;
2110 if (mGlobalProxy != null) return;
2111 sendProxyBroadcast(proxy);
2112 }
2113
2114 private void handleDeprecatedGlobalHttpProxy() {
2115 String proxy = Settings.Secure.getString(mContext.getContentResolver(),
2116 Settings.Secure.HTTP_PROXY);
2117 if (!TextUtils.isEmpty(proxy)) {
2118 String data[] = proxy.split(":");
2119 String proxyHost = data[0];
2120 int proxyPort = 8080;
2121 if (data.length > 1) {
2122 try {
2123 proxyPort = Integer.parseInt(data[1]);
2124 } catch (NumberFormatException e) {
2125 return;
2126 }
2127 }
2128 ProxyProperties p = new ProxyProperties(data[0], proxyPort, "");
2129 setGlobalProxy(p);
2130 }
2131 }
2132
2133 private void sendProxyBroadcast(ProxyProperties proxy) {
Robert Greenwalt55985be2010-12-23 15:51:10 -08002134 if (proxy == null) proxy = new ProxyProperties("", 0, "");
Wink Savilleed9c02b2010-12-03 12:01:38 -08002135 log("sending Proxy Broadcast for " + proxy);
Robert Greenwalt434203a2010-10-11 16:00:27 -07002136 Intent intent = new Intent(Proxy.PROXY_CHANGE_ACTION);
Stan Chesnuttb35d67a2011-01-06 11:00:19 -08002137 intent.addFlags(Intent.FLAG_RECEIVER_REPLACE_PENDING |
2138 Intent.FLAG_RECEIVER_REGISTERED_ONLY_BEFORE_BOOT);
Robert Greenwalt434203a2010-10-11 16:00:27 -07002139 intent.putExtra(Proxy.EXTRA_PROXY_INFO, proxy);
Robert Greenwalta2e13392010-12-06 11:29:17 -08002140 mContext.sendStickyBroadcast(intent);
Robert Greenwalt434203a2010-10-11 16:00:27 -07002141 }
2142
2143 private static class SettingsObserver extends ContentObserver {
2144 private int mWhat;
2145 private Handler mHandler;
2146 SettingsObserver(Handler handler, int what) {
2147 super(handler);
2148 mHandler = handler;
2149 mWhat = what;
2150 }
2151
2152 void observe(Context context) {
2153 ContentResolver resolver = context.getContentResolver();
2154 resolver.registerContentObserver(Settings.Secure.getUriFor(
2155 Settings.Secure.HTTP_PROXY), false, this);
2156 }
2157
2158 @Override
2159 public void onChange(boolean selfChange) {
2160 mHandler.obtainMessage(mWhat).sendToTarget();
2161 }
2162 }
Wink Savilleed9c02b2010-12-03 12:01:38 -08002163
2164 private void log(String s) {
2165 Slog.d(TAG, s);
2166 }
2167
2168 private void loge(String s) {
2169 Slog.e(TAG, s);
2170 }
Wink Saville9d7d6282011-03-12 14:52:01 -08002171 int convertFeatureToNetworkType(String feature){
2172 int networkType = -1;
2173 if (TextUtils.equals(feature, Phone.FEATURE_ENABLE_MMS)) {
2174 networkType = ConnectivityManager.TYPE_MOBILE_MMS;
2175 } else if (TextUtils.equals(feature, Phone.FEATURE_ENABLE_SUPL)) {
2176 networkType = ConnectivityManager.TYPE_MOBILE_SUPL;
2177 } else if (TextUtils.equals(feature, Phone.FEATURE_ENABLE_DUN) ||
2178 TextUtils.equals(feature, Phone.FEATURE_ENABLE_DUN_ALWAYS)) {
2179 networkType = ConnectivityManager.TYPE_MOBILE_DUN;
2180 } else if (TextUtils.equals(feature, Phone.FEATURE_ENABLE_HIPRI)) {
2181 networkType = ConnectivityManager.TYPE_MOBILE_HIPRI;
2182 } else if (TextUtils.equals(feature, Phone.FEATURE_ENABLE_FOTA)) {
2183 networkType = ConnectivityManager.TYPE_MOBILE_FOTA;
2184 } else if (TextUtils.equals(feature, Phone.FEATURE_ENABLE_IMS)) {
2185 networkType = ConnectivityManager.TYPE_MOBILE_IMS;
2186 } else if (TextUtils.equals(feature, Phone.FEATURE_ENABLE_CBS)) {
2187 networkType = ConnectivityManager.TYPE_MOBILE_CBS;
2188 }
2189 return networkType;
2190 }
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002191}