blob: 54ff69f60d9086e95aeb1d2c6f805677e9dc9696 [file] [log] [blame]
The Android Open Source Project28527d22009-03-03 19:31:44 -08001/*
2 * Copyright (C) 2008 The Android Open Source Project
3 *
4 * Licensed under the Apache License, Version 2.0 (the "License");
5 * you may not use this file except in compliance with the License.
6 * You may obtain a copy of the License at
7 *
8 * http://www.apache.org/licenses/LICENSE-2.0
9 *
10 * Unless required by applicable law or agreed to in writing, software
11 * distributed under the License is distributed on an "AS IS" BASIS,
12 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13 * See the License for the specific language governing permissions and
14 * limitations under the License.
15 */
16
17package com.android.server;
18
19import android.app.Notification;
20import android.app.NotificationManager;
21import android.content.ContentResolver;
22import android.content.Context;
23import android.content.Intent;
24import android.content.pm.PackageManager;
25import android.net.ConnectivityManager;
26import android.net.IConnectivityManager;
27import android.net.MobileDataStateTracker;
28import android.net.NetworkInfo;
Robert Greenwalt1f1bcfe2010-08-30 10:56:47 -070029import android.net.LinkProperties;
The Android Open Source Project28527d22009-03-03 19:31:44 -080030import android.net.NetworkStateTracker;
Robert Greenwalt7fe44cb2010-08-27 09:24:29 -070031import android.net.NetworkUtils;
The Android Open Source Project28527d22009-03-03 19:31:44 -080032import android.net.wifi.WifiStateTracker;
Irfan Sheriff7f132d92010-06-09 15:39:36 -070033import android.net.NetworkUtils;
The Android Open Source Project28527d22009-03-03 19:31:44 -080034import android.os.Binder;
35import android.os.Handler;
Robert Greenwalt2034b912009-08-12 16:08:25 -070036import android.os.IBinder;
The Android Open Source Project28527d22009-03-03 19:31:44 -080037import android.os.Looper;
38import android.os.Message;
Robert Greenwalt93dc1042010-06-15 12:19:37 -070039import android.os.PowerManager;
Robert Greenwalt2034b912009-08-12 16:08:25 -070040import android.os.RemoteException;
The Android Open Source Project28527d22009-03-03 19:31:44 -080041import android.os.ServiceManager;
42import android.os.SystemProperties;
43import android.provider.Settings;
Robert Greenwalt2034b912009-08-12 16:08:25 -070044import android.text.TextUtils;
The Android Open Source Project28527d22009-03-03 19:31:44 -080045import android.util.EventLog;
Joe Onoratoc2386bb2010-02-26 18:56:32 -080046import android.util.Slog;
The Android Open Source Project28527d22009-03-03 19:31:44 -080047
Robert Greenwalt2034b912009-08-12 16:08:25 -070048import com.android.internal.telephony.Phone;
49
Robert Greenwalt0c4828c2010-01-26 11:40:34 -080050import com.android.server.connectivity.Tethering;
51
The Android Open Source Project28527d22009-03-03 19:31:44 -080052import java.io.FileDescriptor;
Irfan Sheriff7f132d92010-06-09 15:39:36 -070053import java.io.FileWriter;
54import java.io.IOException;
The Android Open Source Project28527d22009-03-03 19:31:44 -080055import java.io.PrintWriter;
Robert Greenwalta7dfbd32010-06-15 15:43:39 -070056import java.net.InetAddress;
57import java.net.UnknownHostException;
Robert Greenwalt2034b912009-08-12 16:08:25 -070058import java.util.ArrayList;
Robert Greenwalta7dfbd32010-06-15 15:43:39 -070059import java.util.Collection;
Robert Greenwalt2034b912009-08-12 16:08:25 -070060import java.util.List;
Robert Greenwalt7fe44cb2010-08-27 09:24:29 -070061import java.net.InetAddress;
62import java.net.UnknownHostException;
The Android Open Source Project28527d22009-03-03 19:31:44 -080063
64/**
65 * @hide
66 */
67public class ConnectivityService extends IConnectivityManager.Stub {
68
Robert Greenwalta25fd712009-10-06 14:12:53 -070069 private static final boolean DBG = true;
The Android Open Source Project28527d22009-03-03 19:31:44 -080070 private static final String TAG = "ConnectivityService";
71
Robert Greenwalt2034b912009-08-12 16:08:25 -070072 // how long to wait before switching back to a radio's default network
73 private static final int RESTORE_DEFAULT_NETWORK_DELAY = 1 * 60 * 1000;
74 // system property that can override the above value
75 private static final String NETWORK_RESTORE_DELAY_PROP_NAME =
76 "android.telephony.apn-restore";
77
Robert Greenwalt0c4828c2010-01-26 11:40:34 -080078 private Tethering mTethering;
Robert Greenwaltf1b66e12010-02-25 12:29:30 -080079 private boolean mTetheringConfigValid = false;
Robert Greenwalt0c4828c2010-01-26 11:40:34 -080080
The Android Open Source Project28527d22009-03-03 19:31:44 -080081 /**
82 * Sometimes we want to refer to the individual network state
83 * trackers separately, and sometimes we just want to treat them
84 * abstractly.
85 */
86 private NetworkStateTracker mNetTrackers[];
Robert Greenwalt2034b912009-08-12 16:08:25 -070087
88 /**
89 * A per Net list of the PID's that requested access to the net
90 * used both as a refcount and for per-PID DNS selection
91 */
92 private List mNetRequestersPids[];
93
Irfan Sheriff653e2a22010-06-07 09:03:04 -070094 private WifiWatchdogService mWifiWatchdogService;
95
Robert Greenwalt2034b912009-08-12 16:08:25 -070096 // priority order of the nettrackers
97 // (excluding dynamically set mNetworkPreference)
98 // TODO - move mNetworkTypePreference into this
99 private int[] mPriorityList;
100
The Android Open Source Project28527d22009-03-03 19:31:44 -0800101 private Context mContext;
102 private int mNetworkPreference;
Robert Greenwalt2034b912009-08-12 16:08:25 -0700103 private int mActiveDefaultNetwork = -1;
The Android Open Source Project28527d22009-03-03 19:31:44 -0800104
105 private int mNumDnsEntries;
The Android Open Source Project28527d22009-03-03 19:31:44 -0800106
107 private boolean mTestMode;
Wink Saville7323ea12010-09-01 14:48:29 -0700108 private static volatile ConnectivityService sServiceInstance;
The Android Open Source Project28527d22009-03-03 19:31:44 -0800109
Robert Greenwalt2034b912009-08-12 16:08:25 -0700110 private Handler mHandler;
111
112 // list of DeathRecipients used to make sure features are turned off when
113 // a process dies
114 private List mFeatureUsers;
115
Mike Lockwoodfde2b762009-08-14 14:18:49 -0400116 private boolean mSystemReady;
Dianne Hackborna417ff82009-12-08 19:45:14 -0800117 private Intent mInitialBroadcast;
Mike Lockwoodfde2b762009-08-14 14:18:49 -0400118
Robert Greenwalt93dc1042010-06-15 12:19:37 -0700119 private PowerManager.WakeLock mNetTransitionWakeLock;
120 private String mNetTransitionWakeLockCausedBy = "";
121 private int mNetTransitionWakeLockSerialNumber;
122 private int mNetTransitionWakeLockTimeout;
123
Robert Greenwalt94daa182010-09-01 11:34:05 -0700124 private InetAddress mDefaultDns;
125
Robert Greenwalt12c44552009-12-07 11:33:18 -0800126 private static class NetworkAttributes {
Robert Greenwalt2034b912009-08-12 16:08:25 -0700127 /**
128 * Class for holding settings read from resources.
129 */
130 public String mName;
131 public int mType;
132 public int mRadio;
133 public int mPriority;
Robert Greenwalt12c44552009-12-07 11:33:18 -0800134 public NetworkInfo.State mLastState;
Robert Greenwalt2034b912009-08-12 16:08:25 -0700135 public NetworkAttributes(String init) {
136 String fragments[] = init.split(",");
137 mName = fragments[0].toLowerCase();
Robert Greenwaltec05b3c2009-10-30 14:17:42 -0700138 mType = Integer.parseInt(fragments[1]);
139 mRadio = Integer.parseInt(fragments[2]);
140 mPriority = Integer.parseInt(fragments[3]);
Robert Greenwalt12c44552009-12-07 11:33:18 -0800141 mLastState = NetworkInfo.State.UNKNOWN;
Robert Greenwalt2034b912009-08-12 16:08:25 -0700142 }
143 public boolean isDefault() {
144 return (mType == mRadio);
145 }
146 }
147 NetworkAttributes[] mNetAttributes;
Robert Greenwaltec05b3c2009-10-30 14:17:42 -0700148 int mNetworksDefined;
Robert Greenwalt2034b912009-08-12 16:08:25 -0700149
Robert Greenwalt12c44552009-12-07 11:33:18 -0800150 private static class RadioAttributes {
Robert Greenwalt2034b912009-08-12 16:08:25 -0700151 public int mSimultaneity;
152 public int mType;
153 public RadioAttributes(String init) {
154 String fragments[] = init.split(",");
Robert Greenwaltec05b3c2009-10-30 14:17:42 -0700155 mType = Integer.parseInt(fragments[0]);
156 mSimultaneity = Integer.parseInt(fragments[1]);
Robert Greenwalt2034b912009-08-12 16:08:25 -0700157 }
158 }
159 RadioAttributes[] mRadioAttributes;
160
The Android Open Source Project28527d22009-03-03 19:31:44 -0800161 private static class ConnectivityThread extends Thread {
162 private Context mContext;
Robert Greenwalt0659da32009-07-16 17:21:39 -0700163
The Android Open Source Project28527d22009-03-03 19:31:44 -0800164 private ConnectivityThread(Context context) {
165 super("ConnectivityThread");
166 mContext = context;
167 }
168
169 @Override
170 public void run() {
171 Looper.prepare();
172 synchronized (this) {
173 sServiceInstance = new ConnectivityService(mContext);
174 notifyAll();
175 }
176 Looper.loop();
177 }
Robert Greenwalt0659da32009-07-16 17:21:39 -0700178
Wink Saville7323ea12010-09-01 14:48:29 -0700179 public static synchronized ConnectivityService getServiceInstance(Context context) {
180 if (sServiceInstance == null) {
181 ConnectivityThread thread = new ConnectivityThread(context);
182 thread.start();
The Android Open Source Project28527d22009-03-03 19:31:44 -0800183 while (sServiceInstance == null) {
184 try {
185 // Wait until sServiceInstance has been initialized.
186 thread.wait();
187 } catch (InterruptedException ignore) {
Joe Onoratoc2386bb2010-02-26 18:56:32 -0800188 Slog.e(TAG,
Robert Greenwalt0659da32009-07-16 17:21:39 -0700189 "Unexpected InterruptedException while waiting"+
190 " for ConnectivityService thread");
The Android Open Source Project28527d22009-03-03 19:31:44 -0800191 }
192 }
193 }
Robert Greenwalt0659da32009-07-16 17:21:39 -0700194
The Android Open Source Project28527d22009-03-03 19:31:44 -0800195 return sServiceInstance;
196 }
197 }
Robert Greenwalt0659da32009-07-16 17:21:39 -0700198
The Android Open Source Project28527d22009-03-03 19:31:44 -0800199 public static ConnectivityService getInstance(Context context) {
200 return ConnectivityThread.getServiceInstance(context);
201 }
Robert Greenwalt0659da32009-07-16 17:21:39 -0700202
The Android Open Source Project28527d22009-03-03 19:31:44 -0800203 private ConnectivityService(Context context) {
Joe Onoratoc2386bb2010-02-26 18:56:32 -0800204 if (DBG) Slog.v(TAG, "ConnectivityService starting up");
Robert Greenwaltd48f8ee2010-01-14 17:47:58 -0800205
206 // setup our unique device name
207 String id = Settings.Secure.getString(context.getContentResolver(),
208 Settings.Secure.ANDROID_ID);
209 if (id != null && id.length() > 0) {
210 String name = new String("android_").concat(id);
211 SystemProperties.set("net.hostname", name);
212 }
213
Robert Greenwalt94daa182010-09-01 11:34:05 -0700214 // read our default dns server ip
215 String dns = Settings.Secure.getString(context.getContentResolver(),
216 Settings.Secure.DEFAULT_DNS_SERVER);
217 if (dns == null || dns.length() == 0) {
218 dns = context.getResources().getString(
219 com.android.internal.R.string.config_default_dns_server);
220 }
221 try {
222 mDefaultDns = InetAddress.getByName(dns);
223 } catch (UnknownHostException e) {
224 Slog.e(TAG, "Error setting defaultDns using " + dns);
225 }
226
The Android Open Source Project28527d22009-03-03 19:31:44 -0800227 mContext = context;
Robert Greenwalt93dc1042010-06-15 12:19:37 -0700228
229 PowerManager powerManager = (PowerManager)mContext.getSystemService(Context.POWER_SERVICE);
230 mNetTransitionWakeLock = powerManager.newWakeLock(PowerManager.PARTIAL_WAKE_LOCK, TAG);
231 mNetTransitionWakeLockTimeout = mContext.getResources().getInteger(
232 com.android.internal.R.integer.config_networkTransitionTimeout);
233
Robert Greenwalt2034b912009-08-12 16:08:25 -0700234 mNetTrackers = new NetworkStateTracker[
235 ConnectivityManager.MAX_NETWORK_TYPE+1];
236 mHandler = new MyHandler();
Robert Greenwalt0659da32009-07-16 17:21:39 -0700237
The Android Open Source Project28527d22009-03-03 19:31:44 -0800238 mNetworkPreference = getPersistedNetworkPreference();
Robert Greenwalt0659da32009-07-16 17:21:39 -0700239
Robert Greenwaltec05b3c2009-10-30 14:17:42 -0700240 mRadioAttributes = new RadioAttributes[ConnectivityManager.MAX_RADIO_TYPE+1];
241 mNetAttributes = new NetworkAttributes[ConnectivityManager.MAX_NETWORK_TYPE+1];
242
Robert Greenwalt2034b912009-08-12 16:08:25 -0700243 // Load device network attributes from resources
Robert Greenwalt2034b912009-08-12 16:08:25 -0700244 String[] raStrings = context.getResources().getStringArray(
245 com.android.internal.R.array.radioAttributes);
Robert Greenwaltec05b3c2009-10-30 14:17:42 -0700246 for (String raString : raStrings) {
247 RadioAttributes r = new RadioAttributes(raString);
248 if (r.mType > ConnectivityManager.MAX_RADIO_TYPE) {
Joe Onoratoc2386bb2010-02-26 18:56:32 -0800249 Slog.e(TAG, "Error in radioAttributes - ignoring attempt to define type " + r.mType);
Robert Greenwaltec05b3c2009-10-30 14:17:42 -0700250 continue;
251 }
252 if (mRadioAttributes[r.mType] != null) {
Joe Onoratoc2386bb2010-02-26 18:56:32 -0800253 Slog.e(TAG, "Error in radioAttributes - ignoring attempt to redefine type " +
Robert Greenwaltec05b3c2009-10-30 14:17:42 -0700254 r.mType);
255 continue;
256 }
Robert Greenwalt2034b912009-08-12 16:08:25 -0700257 mRadioAttributes[r.mType] = r;
258 }
259
Robert Greenwaltec05b3c2009-10-30 14:17:42 -0700260 String[] naStrings = context.getResources().getStringArray(
261 com.android.internal.R.array.networkAttributes);
262 for (String naString : naStrings) {
263 try {
264 NetworkAttributes n = new NetworkAttributes(naString);
265 if (n.mType > ConnectivityManager.MAX_NETWORK_TYPE) {
Joe Onoratoc2386bb2010-02-26 18:56:32 -0800266 Slog.e(TAG, "Error in networkAttributes - ignoring attempt to define type " +
Robert Greenwaltec05b3c2009-10-30 14:17:42 -0700267 n.mType);
268 continue;
Robert Greenwalt2034b912009-08-12 16:08:25 -0700269 }
Robert Greenwaltec05b3c2009-10-30 14:17:42 -0700270 if (mNetAttributes[n.mType] != null) {
Joe Onoratoc2386bb2010-02-26 18:56:32 -0800271 Slog.e(TAG, "Error in networkAttributes - ignoring attempt to redefine type " +
Robert Greenwaltec05b3c2009-10-30 14:17:42 -0700272 n.mType);
273 continue;
274 }
275 if (mRadioAttributes[n.mRadio] == null) {
Joe Onoratoc2386bb2010-02-26 18:56:32 -0800276 Slog.e(TAG, "Error in networkAttributes - ignoring attempt to use undefined " +
Robert Greenwaltec05b3c2009-10-30 14:17:42 -0700277 "radio " + n.mRadio + " in network type " + n.mType);
278 continue;
279 }
280 mNetAttributes[n.mType] = n;
281 mNetworksDefined++;
282 } catch(Exception e) {
283 // ignore it - leave the entry null
Robert Greenwalt2034b912009-08-12 16:08:25 -0700284 }
285 }
286
Robert Greenwaltec05b3c2009-10-30 14:17:42 -0700287 // high priority first
288 mPriorityList = new int[mNetworksDefined];
289 {
290 int insertionPoint = mNetworksDefined-1;
291 int currentLowest = 0;
292 int nextLowest = 0;
293 while (insertionPoint > -1) {
294 for (NetworkAttributes na : mNetAttributes) {
295 if (na == null) continue;
296 if (na.mPriority < currentLowest) continue;
297 if (na.mPriority > currentLowest) {
298 if (na.mPriority < nextLowest || nextLowest == 0) {
299 nextLowest = na.mPriority;
300 }
301 continue;
302 }
303 mPriorityList[insertionPoint--] = na.mType;
304 }
305 currentLowest = nextLowest;
306 nextLowest = 0;
307 }
308 }
309
310 mNetRequestersPids = new ArrayList[ConnectivityManager.MAX_NETWORK_TYPE+1];
311 for (int i : mPriorityList) {
Robert Greenwalt2034b912009-08-12 16:08:25 -0700312 mNetRequestersPids[i] = new ArrayList();
313 }
314
315 mFeatureUsers = new ArrayList();
316
Robert Greenwaltec05b3c2009-10-30 14:17:42 -0700317 mNumDnsEntries = 0;
318
319 mTestMode = SystemProperties.get("cm.test.mode").equals("true")
320 && SystemProperties.get("ro.build.type").equals("eng");
The Android Open Source Project28527d22009-03-03 19:31:44 -0800321 /*
322 * Create the network state trackers for Wi-Fi and mobile
323 * data. Maybe this could be done with a factory class,
324 * but it's not clear that it's worth it, given that
325 * the number of different network types is not going
326 * to change very often.
327 */
Robert Greenwalt1b21f6c2010-02-23 18:58:05 -0800328 boolean noMobileData = !getMobileDataEnabled();
Robert Greenwaltec05b3c2009-10-30 14:17:42 -0700329 for (int netType : mPriorityList) {
330 switch (mNetAttributes[netType].mRadio) {
331 case ConnectivityManager.TYPE_WIFI:
Joe Onoratoc2386bb2010-02-26 18:56:32 -0800332 if (DBG) Slog.v(TAG, "Starting Wifi Service.");
Wink Saville7fabfa22010-08-13 16:11:42 -0700333 WifiStateTracker wst = new WifiStateTracker();
Irfan Sheriff25be0762010-07-28 09:35:20 -0700334 WifiService wifiService = new WifiService(context);
Robert Greenwaltec05b3c2009-10-30 14:17:42 -0700335 ServiceManager.addService(Context.WIFI_SERVICE, wifiService);
Irfan Sheriff25be0762010-07-28 09:35:20 -0700336 wifiService.checkAndStartWifi();
Robert Greenwaltec05b3c2009-10-30 14:17:42 -0700337 mNetTrackers[ConnectivityManager.TYPE_WIFI] = wst;
Wink Saville7fabfa22010-08-13 16:11:42 -0700338 wst.startMonitoring(context, mHandler);
The Android Open Source Project28527d22009-03-03 19:31:44 -0800339
Irfan Sheriff653e2a22010-06-07 09:03:04 -0700340 //TODO: as part of WWS refactor, create only when needed
Irfan Sheriff25be0762010-07-28 09:35:20 -0700341 mWifiWatchdogService = new WifiWatchdogService(context);
Irfan Sheriff653e2a22010-06-07 09:03:04 -0700342
Robert Greenwaltec05b3c2009-10-30 14:17:42 -0700343 break;
344 case ConnectivityManager.TYPE_MOBILE:
Wink Saville7fabfa22010-08-13 16:11:42 -0700345 mNetTrackers[netType] = new MobileDataStateTracker(netType,
346 mNetAttributes[netType].mName);
347 mNetTrackers[netType].startMonitoring(context, mHandler);
Robert Greenwalt1b21f6c2010-02-23 18:58:05 -0800348 if (noMobileData) {
Joe Onoratoc2386bb2010-02-26 18:56:32 -0800349 if (DBG) Slog.d(TAG, "tearing down Mobile networks due to setting");
Robert Greenwalt1b21f6c2010-02-23 18:58:05 -0800350 mNetTrackers[netType].teardown();
351 }
Robert Greenwaltec05b3c2009-10-30 14:17:42 -0700352 break;
353 default:
Joe Onoratoc2386bb2010-02-26 18:56:32 -0800354 Slog.e(TAG, "Trying to create a DataStateTracker for an unknown radio type " +
Robert Greenwaltec05b3c2009-10-30 14:17:42 -0700355 mNetAttributes[netType].mRadio);
356 continue;
357 }
358 }
Robert Greenwalt0c4828c2010-01-26 11:40:34 -0800359
Robert Greenwaltc0b6c602010-03-11 15:03:08 -0800360 mTethering = new Tethering(mContext, mHandler.getLooper());
Robert Greenwaltf1b66e12010-02-25 12:29:30 -0800361 mTetheringConfigValid = (((mNetTrackers[ConnectivityManager.TYPE_MOBILE_DUN] != null) ||
362 !mTethering.isDunRequired()) &&
363 (mTethering.getTetherableUsbRegexs().length != 0 ||
Danica Chang96567052010-08-11 14:54:43 -0700364 mTethering.getTetherableWifiRegexs().length != 0 ||
365 mTethering.getTetherableBluetoothRegexs().length != 0) &&
Robert Greenwaltf1b66e12010-02-25 12:29:30 -0800366 mTethering.getUpstreamIfaceRegexs().length != 0);
367
The Android Open Source Project28527d22009-03-03 19:31:44 -0800368 }
369
Robert Greenwaltec05b3c2009-10-30 14:17:42 -0700370
The Android Open Source Project28527d22009-03-03 19:31:44 -0800371 /**
Robert Greenwalt0659da32009-07-16 17:21:39 -0700372 * Sets the preferred network.
The Android Open Source Project28527d22009-03-03 19:31:44 -0800373 * @param preference the new preference
374 */
375 public synchronized void setNetworkPreference(int preference) {
376 enforceChangePermission();
Robert Greenwalt2034b912009-08-12 16:08:25 -0700377 if (ConnectivityManager.isNetworkTypeValid(preference) &&
Robert Greenwaltec05b3c2009-10-30 14:17:42 -0700378 mNetAttributes[preference] != null &&
Robert Greenwalt2034b912009-08-12 16:08:25 -0700379 mNetAttributes[preference].isDefault()) {
The Android Open Source Project28527d22009-03-03 19:31:44 -0800380 if (mNetworkPreference != preference) {
381 persistNetworkPreference(preference);
382 mNetworkPreference = preference;
383 enforcePreference();
384 }
385 }
386 }
387
388 public int getNetworkPreference() {
389 enforceAccessPermission();
390 return mNetworkPreference;
391 }
392
393 private void persistNetworkPreference(int networkPreference) {
394 final ContentResolver cr = mContext.getContentResolver();
Robert Greenwalt0659da32009-07-16 17:21:39 -0700395 Settings.Secure.putInt(cr, Settings.Secure.NETWORK_PREFERENCE,
396 networkPreference);
The Android Open Source Project28527d22009-03-03 19:31:44 -0800397 }
Robert Greenwalt0659da32009-07-16 17:21:39 -0700398
The Android Open Source Project28527d22009-03-03 19:31:44 -0800399 private int getPersistedNetworkPreference() {
400 final ContentResolver cr = mContext.getContentResolver();
401
402 final int networkPrefSetting = Settings.Secure
403 .getInt(cr, Settings.Secure.NETWORK_PREFERENCE, -1);
404 if (networkPrefSetting != -1) {
405 return networkPrefSetting;
406 }
407
408 return ConnectivityManager.DEFAULT_NETWORK_PREFERENCE;
409 }
Robert Greenwalt0659da32009-07-16 17:21:39 -0700410
The Android Open Source Project28527d22009-03-03 19:31:44 -0800411 /**
Robert Greenwalt0659da32009-07-16 17:21:39 -0700412 * Make the state of network connectivity conform to the preference settings
The Android Open Source Project28527d22009-03-03 19:31:44 -0800413 * In this method, we only tear down a non-preferred network. Establishing
414 * a connection to the preferred network is taken care of when we handle
415 * the disconnect event from the non-preferred network
416 * (see {@link #handleDisconnect(NetworkInfo)}).
417 */
418 private void enforcePreference() {
Robert Greenwalt2034b912009-08-12 16:08:25 -0700419 if (mNetTrackers[mNetworkPreference].getNetworkInfo().isConnected())
The Android Open Source Project28527d22009-03-03 19:31:44 -0800420 return;
421
Robert Greenwalt2034b912009-08-12 16:08:25 -0700422 if (!mNetTrackers[mNetworkPreference].isAvailable())
423 return;
The Android Open Source Project28527d22009-03-03 19:31:44 -0800424
Robert Greenwalt2034b912009-08-12 16:08:25 -0700425 for (int t=0; t <= ConnectivityManager.MAX_RADIO_TYPE; t++) {
Robert Greenwaltec05b3c2009-10-30 14:17:42 -0700426 if (t != mNetworkPreference && mNetTrackers[t] != null &&
Robert Greenwalt2034b912009-08-12 16:08:25 -0700427 mNetTrackers[t].getNetworkInfo().isConnected()) {
Robert Greenwaltf3f045b2009-08-20 15:25:14 -0700428 if (DBG) {
Joe Onoratoc2386bb2010-02-26 18:56:32 -0800429 Slog.d(TAG, "tearing down " +
Robert Greenwaltf3f045b2009-08-20 15:25:14 -0700430 mNetTrackers[t].getNetworkInfo() +
431 " in enforcePreference");
432 }
Robert Greenwalt2034b912009-08-12 16:08:25 -0700433 teardown(mNetTrackers[t]);
The Android Open Source Project28527d22009-03-03 19:31:44 -0800434 }
435 }
436 }
437
438 private boolean teardown(NetworkStateTracker netTracker) {
439 if (netTracker.teardown()) {
440 netTracker.setTeardownRequested(true);
441 return true;
442 } else {
443 return false;
444 }
445 }
446
447 /**
448 * Return NetworkInfo for the active (i.e., connected) network interface.
449 * It is assumed that at most one network is active at a time. If more
450 * than one is active, it is indeterminate which will be returned.
Robert Greenwalt0659da32009-07-16 17:21:39 -0700451 * @return the info for the active network, or {@code null} if none is
452 * active
The Android Open Source Project28527d22009-03-03 19:31:44 -0800453 */
454 public NetworkInfo getActiveNetworkInfo() {
455 enforceAccessPermission();
Robert Greenwalt2034b912009-08-12 16:08:25 -0700456 for (int type=0; type <= ConnectivityManager.MAX_NETWORK_TYPE; type++) {
Robert Greenwaltec05b3c2009-10-30 14:17:42 -0700457 if (mNetAttributes[type] == null || !mNetAttributes[type].isDefault()) {
Robert Greenwalt2034b912009-08-12 16:08:25 -0700458 continue;
459 }
460 NetworkStateTracker t = mNetTrackers[type];
The Android Open Source Project28527d22009-03-03 19:31:44 -0800461 NetworkInfo info = t.getNetworkInfo();
462 if (info.isConnected()) {
Joe Onoratoc2386bb2010-02-26 18:56:32 -0800463 if (DBG && type != mActiveDefaultNetwork) Slog.e(TAG,
Robert Greenwalt2034b912009-08-12 16:08:25 -0700464 "connected default network is not " +
465 "mActiveDefaultNetwork!");
The Android Open Source Project28527d22009-03-03 19:31:44 -0800466 return info;
467 }
468 }
469 return null;
470 }
471
472 public NetworkInfo getNetworkInfo(int networkType) {
473 enforceAccessPermission();
474 if (ConnectivityManager.isNetworkTypeValid(networkType)) {
475 NetworkStateTracker t = mNetTrackers[networkType];
476 if (t != null)
477 return t.getNetworkInfo();
478 }
479 return null;
480 }
481
482 public NetworkInfo[] getAllNetworkInfo() {
483 enforceAccessPermission();
Robert Greenwaltec05b3c2009-10-30 14:17:42 -0700484 NetworkInfo[] result = new NetworkInfo[mNetworksDefined];
The Android Open Source Project28527d22009-03-03 19:31:44 -0800485 int i = 0;
486 for (NetworkStateTracker t : mNetTrackers) {
Robert Greenwaltec05b3c2009-10-30 14:17:42 -0700487 if(t != null) result[i++] = t.getNetworkInfo();
The Android Open Source Project28527d22009-03-03 19:31:44 -0800488 }
489 return result;
490 }
491
492 public boolean setRadios(boolean turnOn) {
493 boolean result = true;
494 enforceChangePermission();
495 for (NetworkStateTracker t : mNetTrackers) {
Robert Greenwaltec05b3c2009-10-30 14:17:42 -0700496 if (t != null) result = t.setRadio(turnOn) && result;
The Android Open Source Project28527d22009-03-03 19:31:44 -0800497 }
498 return result;
499 }
500
501 public boolean setRadio(int netType, boolean turnOn) {
502 enforceChangePermission();
503 if (!ConnectivityManager.isNetworkTypeValid(netType)) {
504 return false;
505 }
506 NetworkStateTracker tracker = mNetTrackers[netType];
507 return tracker != null && tracker.setRadio(turnOn);
508 }
509
Robert Greenwaltaffc3a12009-09-27 17:27:04 -0700510 /**
511 * Used to notice when the calling process dies so we can self-expire
512 *
513 * Also used to know if the process has cleaned up after itself when
514 * our auto-expire timer goes off. The timer has a link to an object.
515 *
516 */
Robert Greenwalt2034b912009-08-12 16:08:25 -0700517 private class FeatureUser implements IBinder.DeathRecipient {
518 int mNetworkType;
519 String mFeature;
520 IBinder mBinder;
521 int mPid;
522 int mUid;
Robert Greenwalt3eeb6032009-12-21 18:24:07 -0800523 long mCreateTime;
Robert Greenwalt2034b912009-08-12 16:08:25 -0700524
525 FeatureUser(int type, String feature, IBinder binder) {
526 super();
527 mNetworkType = type;
528 mFeature = feature;
529 mBinder = binder;
530 mPid = getCallingPid();
531 mUid = getCallingUid();
Robert Greenwalt3eeb6032009-12-21 18:24:07 -0800532 mCreateTime = System.currentTimeMillis();
Robert Greenwaltaffc3a12009-09-27 17:27:04 -0700533
Robert Greenwalt2034b912009-08-12 16:08:25 -0700534 try {
535 mBinder.linkToDeath(this, 0);
536 } catch (RemoteException e) {
537 binderDied();
538 }
539 }
540
541 void unlinkDeathRecipient() {
542 mBinder.unlinkToDeath(this, 0);
543 }
544
545 public void binderDied() {
Joe Onoratoc2386bb2010-02-26 18:56:32 -0800546 Slog.d(TAG, "ConnectivityService FeatureUser binderDied(" +
Robert Greenwalt3eeb6032009-12-21 18:24:07 -0800547 mNetworkType + ", " + mFeature + ", " + mBinder + "), created " +
548 (System.currentTimeMillis() - mCreateTime) + " mSec ago");
Robert Greenwaltaffc3a12009-09-27 17:27:04 -0700549 stopUsingNetworkFeature(this, false);
Robert Greenwalt2034b912009-08-12 16:08:25 -0700550 }
551
Robert Greenwaltaffc3a12009-09-27 17:27:04 -0700552 public void expire() {
Joe Onoratoc2386bb2010-02-26 18:56:32 -0800553 Slog.d(TAG, "ConnectivityService FeatureUser expire(" +
Robert Greenwalt3eeb6032009-12-21 18:24:07 -0800554 mNetworkType + ", " + mFeature + ", " + mBinder +"), created " +
555 (System.currentTimeMillis() - mCreateTime) + " mSec ago");
Robert Greenwaltaffc3a12009-09-27 17:27:04 -0700556 stopUsingNetworkFeature(this, false);
557 }
Robert Greenwalt3eeb6032009-12-21 18:24:07 -0800558
559 public String toString() {
560 return "FeatureUser("+mNetworkType+","+mFeature+","+mPid+","+mUid+"), created " +
561 (System.currentTimeMillis() - mCreateTime) + " mSec ago";
562 }
Robert Greenwalt2034b912009-08-12 16:08:25 -0700563 }
564
Robert Greenwaltaffc3a12009-09-27 17:27:04 -0700565 // javadoc from interface
Robert Greenwalt2034b912009-08-12 16:08:25 -0700566 public int startUsingNetworkFeature(int networkType, String feature,
567 IBinder binder) {
568 if (DBG) {
Joe Onoratoc2386bb2010-02-26 18:56:32 -0800569 Slog.d(TAG, "startUsingNetworkFeature for net " + networkType +
Robert Greenwalt2034b912009-08-12 16:08:25 -0700570 ": " + feature);
571 }
The Android Open Source Project28527d22009-03-03 19:31:44 -0800572 enforceChangePermission();
Robert Greenwaltec05b3c2009-10-30 14:17:42 -0700573 if (!ConnectivityManager.isNetworkTypeValid(networkType) ||
574 mNetAttributes[networkType] == null) {
Robert Greenwalt2034b912009-08-12 16:08:25 -0700575 return Phone.APN_REQUEST_FAILED;
The Android Open Source Project28527d22009-03-03 19:31:44 -0800576 }
Robert Greenwalt2034b912009-08-12 16:08:25 -0700577
Robert Greenwaltaffc3a12009-09-27 17:27:04 -0700578 FeatureUser f = new FeatureUser(networkType, feature, binder);
Robert Greenwalt2034b912009-08-12 16:08:25 -0700579
580 // TODO - move this into the MobileDataStateTracker
581 int usedNetworkType = networkType;
582 if(networkType == ConnectivityManager.TYPE_MOBILE) {
Robert Greenwalt1b21f6c2010-02-23 18:58:05 -0800583 if (!getMobileDataEnabled()) {
Joe Onoratoc2386bb2010-02-26 18:56:32 -0800584 if (DBG) Slog.d(TAG, "requested special network with data disabled - rejected");
Robert Greenwalt1b21f6c2010-02-23 18:58:05 -0800585 return Phone.APN_TYPE_NOT_AVAILABLE;
586 }
Robert Greenwalt2034b912009-08-12 16:08:25 -0700587 if (TextUtils.equals(feature, Phone.FEATURE_ENABLE_MMS)) {
588 usedNetworkType = ConnectivityManager.TYPE_MOBILE_MMS;
589 } else if (TextUtils.equals(feature, Phone.FEATURE_ENABLE_SUPL)) {
590 usedNetworkType = ConnectivityManager.TYPE_MOBILE_SUPL;
591 } else if (TextUtils.equals(feature, Phone.FEATURE_ENABLE_DUN)) {
592 usedNetworkType = ConnectivityManager.TYPE_MOBILE_DUN;
593 } else if (TextUtils.equals(feature, Phone.FEATURE_ENABLE_HIPRI)) {
594 usedNetworkType = ConnectivityManager.TYPE_MOBILE_HIPRI;
595 }
596 }
597 NetworkStateTracker network = mNetTrackers[usedNetworkType];
598 if (network != null) {
599 if (usedNetworkType != networkType) {
600 Integer currentPid = new Integer(getCallingPid());
601
602 NetworkStateTracker radio = mNetTrackers[networkType];
603 NetworkInfo ni = network.getNetworkInfo();
604
605 if (ni.isAvailable() == false) {
Joe Onoratoc2386bb2010-02-26 18:56:32 -0800606 if (DBG) Slog.d(TAG, "special network not available");
Robert Greenwalt2034b912009-08-12 16:08:25 -0700607 return Phone.APN_TYPE_NOT_AVAILABLE;
608 }
609
Robert Greenwaltaffc3a12009-09-27 17:27:04 -0700610 synchronized(this) {
611 mFeatureUsers.add(f);
612 if (!mNetRequestersPids[usedNetworkType].contains(currentPid)) {
613 // this gets used for per-pid dns when connected
614 mNetRequestersPids[usedNetworkType].add(currentPid);
615 }
Robert Greenwalt2034b912009-08-12 16:08:25 -0700616 }
Robert Greenwaltaffc3a12009-09-27 17:27:04 -0700617 mHandler.sendMessageDelayed(mHandler.obtainMessage(
618 NetworkStateTracker.EVENT_RESTORE_DEFAULT_NETWORK,
619 f), getRestoreDefaultNetworkDelay());
620
Robert Greenwalt2034b912009-08-12 16:08:25 -0700621
Robert Greenwalta52c75a2009-08-19 20:19:33 -0700622 if ((ni.isConnectedOrConnecting() == true) &&
623 !network.isTeardownRequested()) {
Robert Greenwalt2034b912009-08-12 16:08:25 -0700624 if (ni.isConnected() == true) {
625 // add the pid-specific dns
Robert Greenwalt3afbead2010-07-23 15:46:26 -0700626 handleDnsConfigurationChange(networkType);
Joe Onoratoc2386bb2010-02-26 18:56:32 -0800627 if (DBG) Slog.d(TAG, "special network already active");
Robert Greenwalt2034b912009-08-12 16:08:25 -0700628 return Phone.APN_ALREADY_ACTIVE;
629 }
Joe Onoratoc2386bb2010-02-26 18:56:32 -0800630 if (DBG) Slog.d(TAG, "special network already connecting");
Robert Greenwalt2034b912009-08-12 16:08:25 -0700631 return Phone.APN_REQUEST_STARTED;
632 }
633
634 // check if the radio in play can make another contact
635 // assume if cannot for now
636
Joe Onoratoc2386bb2010-02-26 18:56:32 -0800637 if (DBG) Slog.d(TAG, "reconnecting to special network");
Robert Greenwalt2034b912009-08-12 16:08:25 -0700638 network.reconnect();
639 return Phone.APN_REQUEST_STARTED;
640 } else {
Robert Greenwaltd391e892010-05-18 10:52:51 -0700641 return -1;
Robert Greenwalt2034b912009-08-12 16:08:25 -0700642 }
643 }
644 return Phone.APN_TYPE_NOT_AVAILABLE;
The Android Open Source Project28527d22009-03-03 19:31:44 -0800645 }
646
Robert Greenwaltaffc3a12009-09-27 17:27:04 -0700647 // javadoc from interface
The Android Open Source Project28527d22009-03-03 19:31:44 -0800648 public int stopUsingNetworkFeature(int networkType, String feature) {
Robert Greenwalt28f43012009-10-06 17:52:40 -0700649 enforceChangePermission();
650
Robert Greenwaltaffc3a12009-09-27 17:27:04 -0700651 int pid = getCallingPid();
652 int uid = getCallingUid();
653
654 FeatureUser u = null;
655 boolean found = false;
656
657 synchronized(this) {
658 for (int i = 0; i < mFeatureUsers.size() ; i++) {
659 u = (FeatureUser)mFeatureUsers.get(i);
660 if (uid == u.mUid && pid == u.mPid &&
661 networkType == u.mNetworkType &&
662 TextUtils.equals(feature, u.mFeature)) {
663 found = true;
664 break;
665 }
666 }
667 }
668 if (found && u != null) {
669 // stop regardless of how many other time this proc had called start
670 return stopUsingNetworkFeature(u, true);
671 } else {
672 // none found!
Joe Onoratoc2386bb2010-02-26 18:56:32 -0800673 if (DBG) Slog.d(TAG, "ignoring stopUsingNetworkFeature - not a live request");
Robert Greenwaltaffc3a12009-09-27 17:27:04 -0700674 return 1;
675 }
Robert Greenwalt2034b912009-08-12 16:08:25 -0700676 }
677
Robert Greenwaltaffc3a12009-09-27 17:27:04 -0700678 private int stopUsingNetworkFeature(FeatureUser u, boolean ignoreDups) {
679 int networkType = u.mNetworkType;
680 String feature = u.mFeature;
681 int pid = u.mPid;
682 int uid = u.mUid;
683
684 NetworkStateTracker tracker = null;
685 boolean callTeardown = false; // used to carry our decision outside of sync block
686
Robert Greenwalt2034b912009-08-12 16:08:25 -0700687 if (DBG) {
Joe Onoratoc2386bb2010-02-26 18:56:32 -0800688 Slog.d(TAG, "stopUsingNetworkFeature for net " + networkType +
Robert Greenwalt2034b912009-08-12 16:08:25 -0700689 ": " + feature);
690 }
Robert Greenwalt28f43012009-10-06 17:52:40 -0700691
The Android Open Source Project28527d22009-03-03 19:31:44 -0800692 if (!ConnectivityManager.isNetworkTypeValid(networkType)) {
693 return -1;
694 }
Robert Greenwalt2034b912009-08-12 16:08:25 -0700695
Robert Greenwaltaffc3a12009-09-27 17:27:04 -0700696 // need to link the mFeatureUsers list with the mNetRequestersPids state in this
697 // sync block
698 synchronized(this) {
699 // check if this process still has an outstanding start request
700 if (!mFeatureUsers.contains(u)) {
Robert Greenwalt46ceefa2010-03-10 16:10:43 -0800701 if (DBG) Slog.d(TAG, "ignoring - this process has no outstanding requests");
Robert Greenwalt2034b912009-08-12 16:08:25 -0700702 return 1;
703 }
Robert Greenwaltaffc3a12009-09-27 17:27:04 -0700704 u.unlinkDeathRecipient();
705 mFeatureUsers.remove(mFeatureUsers.indexOf(u));
706 // If we care about duplicate requests, check for that here.
707 //
708 // This is done to support the extension of a request - the app
709 // can request we start the network feature again and renew the
710 // auto-shutoff delay. Normal "stop" calls from the app though
711 // do not pay attention to duplicate requests - in effect the
712 // API does not refcount and a single stop will counter multiple starts.
713 if (ignoreDups == false) {
714 for (int i = 0; i < mFeatureUsers.size() ; i++) {
715 FeatureUser x = (FeatureUser)mFeatureUsers.get(i);
716 if (x.mUid == u.mUid && x.mPid == u.mPid &&
717 x.mNetworkType == u.mNetworkType &&
718 TextUtils.equals(x.mFeature, u.mFeature)) {
Joe Onoratoc2386bb2010-02-26 18:56:32 -0800719 if (DBG) Slog.d(TAG, "ignoring stopUsingNetworkFeature as dup is found");
Robert Greenwaltaffc3a12009-09-27 17:27:04 -0700720 return 1;
721 }
722 }
Robert Greenwalt2034b912009-08-12 16:08:25 -0700723 }
Robert Greenwaltaffc3a12009-09-27 17:27:04 -0700724
725 // TODO - move to MobileDataStateTracker
726 int usedNetworkType = networkType;
727 if (networkType == ConnectivityManager.TYPE_MOBILE) {
728 if (TextUtils.equals(feature, Phone.FEATURE_ENABLE_MMS)) {
729 usedNetworkType = ConnectivityManager.TYPE_MOBILE_MMS;
730 } else if (TextUtils.equals(feature, Phone.FEATURE_ENABLE_SUPL)) {
731 usedNetworkType = ConnectivityManager.TYPE_MOBILE_SUPL;
732 } else if (TextUtils.equals(feature, Phone.FEATURE_ENABLE_DUN)) {
733 usedNetworkType = ConnectivityManager.TYPE_MOBILE_DUN;
734 } else if (TextUtils.equals(feature, Phone.FEATURE_ENABLE_HIPRI)) {
735 usedNetworkType = ConnectivityManager.TYPE_MOBILE_HIPRI;
736 }
737 }
738 tracker = mNetTrackers[usedNetworkType];
Robert Greenwaltec05b3c2009-10-30 14:17:42 -0700739 if (tracker == null) {
Robert Greenwalt46ceefa2010-03-10 16:10:43 -0800740 if (DBG) Slog.d(TAG, "ignoring - no known tracker for net type " + usedNetworkType);
Robert Greenwaltec05b3c2009-10-30 14:17:42 -0700741 return -1;
742 }
743 if (usedNetworkType != networkType) {
Robert Greenwaltaffc3a12009-09-27 17:27:04 -0700744 Integer currentPid = new Integer(pid);
Robert Greenwaltaffc3a12009-09-27 17:27:04 -0700745 mNetRequestersPids[usedNetworkType].remove(currentPid);
Robert Greenwalt0ca68a02009-12-17 14:54:59 -0800746 reassessPidDns(pid, true);
Robert Greenwaltaffc3a12009-09-27 17:27:04 -0700747 if (mNetRequestersPids[usedNetworkType].size() != 0) {
Joe Onoratoc2386bb2010-02-26 18:56:32 -0800748 if (DBG) Slog.d(TAG, "not tearing down special network - " +
Robert Greenwaltaffc3a12009-09-27 17:27:04 -0700749 "others still using it");
750 return 1;
751 }
752 callTeardown = true;
753 }
754 }
Robert Greenwalt46ceefa2010-03-10 16:10:43 -0800755 if (DBG) Slog.d(TAG, "Doing network teardown");
Robert Greenwaltaffc3a12009-09-27 17:27:04 -0700756 if (callTeardown) {
757 tracker.teardown();
Robert Greenwalt2034b912009-08-12 16:08:25 -0700758 return 1;
759 } else {
Robert Greenwaltd391e892010-05-18 10:52:51 -0700760 return -1;
Robert Greenwalt2034b912009-08-12 16:08:25 -0700761 }
The Android Open Source Project28527d22009-03-03 19:31:44 -0800762 }
763
764 /**
Robert Greenwalt7fe44cb2010-08-27 09:24:29 -0700765 * @deprecated use requestRouteToHostAddress instead
766 *
The Android Open Source Project28527d22009-03-03 19:31:44 -0800767 * Ensure that a network route exists to deliver traffic to the specified
768 * host via the specified network interface.
Robert Greenwalt0659da32009-07-16 17:21:39 -0700769 * @param networkType the type of the network over which traffic to the
770 * specified host is to be routed
771 * @param hostAddress the IP address of the host to which the route is
772 * desired
The Android Open Source Project28527d22009-03-03 19:31:44 -0800773 * @return {@code true} on success, {@code false} on failure
774 */
775 public boolean requestRouteToHost(int networkType, int hostAddress) {
Robert Greenwalt7fe44cb2010-08-27 09:24:29 -0700776 InetAddress inetAddress = NetworkUtils.intToInetAddress(hostAddress);
777
778 if (inetAddress == null) {
779 return false;
780 }
781
782 return requestRouteToHostAddress(networkType, inetAddress.getAddress());
783 }
784
785 /**
786 * Ensure that a network route exists to deliver traffic to the specified
787 * host via the specified network interface.
788 * @param networkType the type of the network over which traffic to the
789 * specified host is to be routed
790 * @param hostAddress the IP address of the host to which the route is
791 * desired
792 * @return {@code true} on success, {@code false} on failure
793 */
794 public boolean requestRouteToHostAddress(int networkType, byte[] hostAddress) {
The Android Open Source Project28527d22009-03-03 19:31:44 -0800795 enforceChangePermission();
796 if (!ConnectivityManager.isNetworkTypeValid(networkType)) {
797 return false;
798 }
799 NetworkStateTracker tracker = mNetTrackers[networkType];
Robert Greenwalt4666ed02009-09-10 15:06:20 -0700800
Robert Greenwaltec05b3c2009-10-30 14:17:42 -0700801 if (tracker == null || !tracker.getNetworkInfo().isConnected() ||
802 tracker.isTeardownRequested()) {
Robert Greenwalt4666ed02009-09-10 15:06:20 -0700803 if (DBG) {
Robert Greenwalt7fe44cb2010-08-27 09:24:29 -0700804 Slog.d(TAG, "requestRouteToHostAddress on down network " +
805 "(" + networkType + ") - dropped");
Robert Greenwalt4666ed02009-09-10 15:06:20 -0700806 }
807 return false;
The Android Open Source Project28527d22009-03-03 19:31:44 -0800808 }
Robert Greenwalta7dfbd32010-06-15 15:43:39 -0700809 try {
Robert Greenwalt7fe44cb2010-08-27 09:24:29 -0700810 InetAddress addr = InetAddress.getByAddress(hostAddress);
Robert Greenwalta7dfbd32010-06-15 15:43:39 -0700811 return addHostRoute(tracker, addr);
812 } catch (UnknownHostException e) {}
813 return false;
Irfan Sheriff7f132d92010-06-09 15:39:36 -0700814 }
815
816 /**
817 * Ensure that a network route exists to deliver traffic to the specified
818 * host via the mobile data network.
819 * @param hostAddress the IP address of the host to which the route is desired,
820 * in network byte order.
Robert Greenwalta7dfbd32010-06-15 15:43:39 -0700821 * TODO - deprecate
Irfan Sheriff7f132d92010-06-09 15:39:36 -0700822 * @return {@code true} on success, {@code false} on failure
823 */
Robert Greenwalta7dfbd32010-06-15 15:43:39 -0700824 private boolean addHostRoute(NetworkStateTracker nt, InetAddress hostAddress) {
Irfan Sheriff7f132d92010-06-09 15:39:36 -0700825 if (nt.getNetworkInfo().getType() == ConnectivityManager.TYPE_WIFI) {
826 return false;
827 }
828
Robert Greenwalt1f1bcfe2010-08-30 10:56:47 -0700829 LinkProperties p = nt.getLinkProperties();
Robert Greenwalta7dfbd32010-06-15 15:43:39 -0700830 if (p == null) return false;
831 String interfaceName = p.getInterfaceName();
Irfan Sheriff7f132d92010-06-09 15:39:36 -0700832
833 if (DBG) {
Robert Greenwalta7dfbd32010-06-15 15:43:39 -0700834 Slog.d(TAG, "Requested host route to " + hostAddress + "(" + interfaceName + ")");
Irfan Sheriff7f132d92010-06-09 15:39:36 -0700835 }
Robert Greenwalta7dfbd32010-06-15 15:43:39 -0700836 if (interfaceName != null) {
Robert Greenwalt7fe44cb2010-08-27 09:24:29 -0700837 return NetworkUtils.addHostRoute(interfaceName, hostAddress, null);
Irfan Sheriff7f132d92010-06-09 15:39:36 -0700838 } else {
Robert Greenwalta7dfbd32010-06-15 15:43:39 -0700839 if (DBG) Slog.e(TAG, "addHostRoute failed due to null interface name");
Irfan Sheriff7f132d92010-06-09 15:39:36 -0700840 return false;
841 }
The Android Open Source Project28527d22009-03-03 19:31:44 -0800842 }
843
844 /**
845 * @see ConnectivityManager#getBackgroundDataSetting()
846 */
847 public boolean getBackgroundDataSetting() {
848 return Settings.Secure.getInt(mContext.getContentResolver(),
849 Settings.Secure.BACKGROUND_DATA, 1) == 1;
850 }
Robert Greenwalt0659da32009-07-16 17:21:39 -0700851
The Android Open Source Project28527d22009-03-03 19:31:44 -0800852 /**
853 * @see ConnectivityManager#setBackgroundDataSetting(boolean)
854 */
855 public void setBackgroundDataSetting(boolean allowBackgroundDataUsage) {
856 mContext.enforceCallingOrSelfPermission(
857 android.Manifest.permission.CHANGE_BACKGROUND_DATA_SETTING,
858 "ConnectivityService");
Robert Greenwalt0659da32009-07-16 17:21:39 -0700859
The Android Open Source Project28527d22009-03-03 19:31:44 -0800860 if (getBackgroundDataSetting() == allowBackgroundDataUsage) return;
861
862 Settings.Secure.putInt(mContext.getContentResolver(),
Robert Greenwalt0659da32009-07-16 17:21:39 -0700863 Settings.Secure.BACKGROUND_DATA,
864 allowBackgroundDataUsage ? 1 : 0);
865
The Android Open Source Project28527d22009-03-03 19:31:44 -0800866 Intent broadcast = new Intent(
867 ConnectivityManager.ACTION_BACKGROUND_DATA_SETTING_CHANGED);
868 mContext.sendBroadcast(broadcast);
Robert Greenwalt0659da32009-07-16 17:21:39 -0700869 }
870
Robert Greenwalt1b21f6c2010-02-23 18:58:05 -0800871 /**
872 * @see ConnectivityManager#getMobileDataEnabled()
873 */
874 public boolean getMobileDataEnabled() {
875 enforceAccessPermission();
876 boolean retVal = Settings.Secure.getInt(mContext.getContentResolver(),
877 Settings.Secure.MOBILE_DATA, 1) == 1;
Joe Onoratoc2386bb2010-02-26 18:56:32 -0800878 if (DBG) Slog.d(TAG, "getMobileDataEnabled returning " + retVal);
Robert Greenwalt1b21f6c2010-02-23 18:58:05 -0800879 return retVal;
880 }
881
882 /**
883 * @see ConnectivityManager#setMobileDataEnabled(boolean)
884 */
885 public synchronized void setMobileDataEnabled(boolean enabled) {
886 enforceChangePermission();
Joe Onoratoc2386bb2010-02-26 18:56:32 -0800887 if (DBG) Slog.d(TAG, "setMobileDataEnabled(" + enabled + ")");
Robert Greenwalt1b21f6c2010-02-23 18:58:05 -0800888
889 if (getMobileDataEnabled() == enabled) return;
890
891 Settings.Secure.putInt(mContext.getContentResolver(),
892 Settings.Secure.MOBILE_DATA, enabled ? 1 : 0);
893
894 if (enabled) {
895 if (mNetTrackers[ConnectivityManager.TYPE_MOBILE] != null) {
Joe Onoratoc2386bb2010-02-26 18:56:32 -0800896 if (DBG) Slog.d(TAG, "starting up " + mNetTrackers[ConnectivityManager.TYPE_MOBILE]);
Robert Greenwalt1b21f6c2010-02-23 18:58:05 -0800897 mNetTrackers[ConnectivityManager.TYPE_MOBILE].reconnect();
898 }
899 } else {
900 for (NetworkStateTracker nt : mNetTrackers) {
901 if (nt == null) continue;
902 int netType = nt.getNetworkInfo().getType();
903 if (mNetAttributes[netType].mRadio == ConnectivityManager.TYPE_MOBILE) {
Joe Onoratoc2386bb2010-02-26 18:56:32 -0800904 if (DBG) Slog.d(TAG, "tearing down " + nt);
Robert Greenwalt1b21f6c2010-02-23 18:58:05 -0800905 nt.teardown();
906 }
907 }
908 }
909 }
910
The Android Open Source Project28527d22009-03-03 19:31:44 -0800911 private int getNumConnectedNetworks() {
912 int numConnectedNets = 0;
913
914 for (NetworkStateTracker nt : mNetTrackers) {
Robert Greenwaltec05b3c2009-10-30 14:17:42 -0700915 if (nt != null && nt.getNetworkInfo().isConnected() &&
Robert Greenwalt0659da32009-07-16 17:21:39 -0700916 !nt.isTeardownRequested()) {
The Android Open Source Project28527d22009-03-03 19:31:44 -0800917 ++numConnectedNets;
918 }
919 }
920 return numConnectedNets;
921 }
922
923 private void enforceAccessPermission() {
Robert Greenwalt0659da32009-07-16 17:21:39 -0700924 mContext.enforceCallingOrSelfPermission(
925 android.Manifest.permission.ACCESS_NETWORK_STATE,
926 "ConnectivityService");
The Android Open Source Project28527d22009-03-03 19:31:44 -0800927 }
928
929 private void enforceChangePermission() {
Robert Greenwalt0659da32009-07-16 17:21:39 -0700930 mContext.enforceCallingOrSelfPermission(
931 android.Manifest.permission.CHANGE_NETWORK_STATE,
932 "ConnectivityService");
The Android Open Source Project28527d22009-03-03 19:31:44 -0800933 }
934
Robert Greenwalt0c4828c2010-01-26 11:40:34 -0800935 // TODO Make this a special check when it goes public
936 private void enforceTetherChangePermission() {
937 mContext.enforceCallingOrSelfPermission(
938 android.Manifest.permission.CHANGE_NETWORK_STATE,
939 "ConnectivityService");
940 }
941
Robert Greenwalt8e87f122010-02-11 18:18:40 -0800942 private void enforceTetherAccessPermission() {
943 mContext.enforceCallingOrSelfPermission(
944 android.Manifest.permission.ACCESS_NETWORK_STATE,
945 "ConnectivityService");
946 }
947
Robert Greenwalt93dc1042010-06-15 12:19:37 -0700948 private void enforceConnectivityInternalPermission() {
949 mContext.enforceCallingOrSelfPermission(
950 android.Manifest.permission.CONNECTIVITY_INTERNAL,
951 "ConnectivityService");
952 }
953
The Android Open Source Project28527d22009-03-03 19:31:44 -0800954 /**
Robert Greenwalt0659da32009-07-16 17:21:39 -0700955 * Handle a {@code DISCONNECTED} event. If this pertains to the non-active
956 * network, we ignore it. If it is for the active network, we send out a
957 * broadcast. But first, we check whether it might be possible to connect
958 * to a different network.
The Android Open Source Project28527d22009-03-03 19:31:44 -0800959 * @param info the {@code NetworkInfo} for the network
960 */
961 private void handleDisconnect(NetworkInfo info) {
962
Robert Greenwalt2034b912009-08-12 16:08:25 -0700963 int prevNetType = info.getType();
The Android Open Source Project28527d22009-03-03 19:31:44 -0800964
Robert Greenwalt2034b912009-08-12 16:08:25 -0700965 mNetTrackers[prevNetType].setTeardownRequested(false);
The Android Open Source Project28527d22009-03-03 19:31:44 -0800966 /*
967 * If the disconnected network is not the active one, then don't report
968 * this as a loss of connectivity. What probably happened is that we're
969 * getting the disconnect for a network that we explicitly disabled
970 * in accordance with network preference policies.
971 */
Robert Greenwalt2034b912009-08-12 16:08:25 -0700972 if (!mNetAttributes[prevNetType].isDefault()) {
973 List pids = mNetRequestersPids[prevNetType];
974 for (int i = 0; i<pids.size(); i++) {
975 Integer pid = (Integer)pids.get(i);
976 // will remove them because the net's no longer connected
977 // need to do this now as only now do we know the pids and
978 // can properly null things that are no longer referenced.
979 reassessPidDns(pid.intValue(), false);
The Android Open Source Project28527d22009-03-03 19:31:44 -0800980 }
The Android Open Source Project28527d22009-03-03 19:31:44 -0800981 }
982
The Android Open Source Project28527d22009-03-03 19:31:44 -0800983 Intent intent = new Intent(ConnectivityManager.CONNECTIVITY_ACTION);
984 intent.putExtra(ConnectivityManager.EXTRA_NETWORK_INFO, info);
985 if (info.isFailover()) {
986 intent.putExtra(ConnectivityManager.EXTRA_IS_FAILOVER, true);
987 info.setFailover(false);
988 }
989 if (info.getReason() != null) {
990 intent.putExtra(ConnectivityManager.EXTRA_REASON, info.getReason());
991 }
992 if (info.getExtraInfo() != null) {
Robert Greenwalt0659da32009-07-16 17:21:39 -0700993 intent.putExtra(ConnectivityManager.EXTRA_EXTRA_INFO,
994 info.getExtraInfo());
The Android Open Source Project28527d22009-03-03 19:31:44 -0800995 }
Robert Greenwalt2034b912009-08-12 16:08:25 -0700996
Robert Greenwalt3cc68d32010-01-25 17:54:29 -0800997 NetworkStateTracker newNet = null;
998 if (mNetAttributes[prevNetType].isDefault()) {
999 newNet = tryFailover(prevNetType);
1000 if (newNet != null) {
1001 NetworkInfo switchTo = newNet.getNetworkInfo();
1002 intent.putExtra(ConnectivityManager.EXTRA_OTHER_NETWORK_INFO, switchTo);
1003 } else {
1004 intent.putExtra(ConnectivityManager.EXTRA_NO_CONNECTIVITY, true);
1005 }
Robert Greenwaltf55ced92010-01-20 19:29:41 -08001006 }
1007 // do this before we broadcast the change
Robert Greenwalt3afbead2010-07-23 15:46:26 -07001008 handleConnectivityChange(prevNetType);
Robert Greenwaltf55ced92010-01-20 19:29:41 -08001009
1010 sendStickyBroadcast(intent);
1011 /*
1012 * If the failover network is already connected, then immediately send
1013 * out a followup broadcast indicating successful failover
1014 */
1015 if (newNet != null && newNet.getNetworkInfo().isConnected()) {
1016 sendConnectedBroadcast(newNet.getNetworkInfo());
1017 }
1018 }
1019
Robert Greenwalt3cc68d32010-01-25 17:54:29 -08001020 // returns null if no failover available
Robert Greenwaltf55ced92010-01-20 19:29:41 -08001021 private NetworkStateTracker tryFailover(int prevNetType) {
Robert Greenwalt2034b912009-08-12 16:08:25 -07001022 /*
1023 * If this is a default network, check if other defaults are available
1024 * or active
1025 */
1026 NetworkStateTracker newNet = null;
1027 if (mNetAttributes[prevNetType].isDefault()) {
Robert Greenwalt2034b912009-08-12 16:08:25 -07001028 if (mActiveDefaultNetwork == prevNetType) {
1029 mActiveDefaultNetwork = -1;
1030 }
1031
1032 int newType = -1;
1033 int newPriority = -1;
Robert Greenwalt72451bf2010-02-25 12:04:29 -08001034 boolean noMobileData = !getMobileDataEnabled();
Robert Greenwaltf55ced92010-01-20 19:29:41 -08001035 for (int checkType=0; checkType <= ConnectivityManager.MAX_NETWORK_TYPE; checkType++) {
Robert Greenwaltec05b3c2009-10-30 14:17:42 -07001036 if (checkType == prevNetType) continue;
1037 if (mNetAttributes[checkType] == null) continue;
Robert Greenwalt72451bf2010-02-25 12:04:29 -08001038 if (mNetAttributes[checkType].mRadio == ConnectivityManager.TYPE_MOBILE &&
1039 noMobileData) {
1040 if (DBG) {
Joe Onoratoc2386bb2010-02-26 18:56:32 -08001041 Slog.d(TAG, "not failing over to mobile type " + checkType +
Robert Greenwalt72451bf2010-02-25 12:04:29 -08001042 " because Mobile Data Disabled");
1043 }
1044 continue;
1045 }
Robert Greenwalt2034b912009-08-12 16:08:25 -07001046 if (mNetAttributes[checkType].isDefault()) {
1047 /* TODO - if we have multiple nets we could use
1048 * we may want to put more thought into which we choose
1049 */
1050 if (checkType == mNetworkPreference) {
1051 newType = checkType;
1052 break;
1053 }
Robert Greenwaltec05b3c2009-10-30 14:17:42 -07001054 if (mNetAttributes[checkType].mPriority > newPriority) {
Robert Greenwalt2034b912009-08-12 16:08:25 -07001055 newType = checkType;
Robert Greenwaltec05b3c2009-10-30 14:17:42 -07001056 newPriority = mNetAttributes[newType].mPriority;
Robert Greenwalt2034b912009-08-12 16:08:25 -07001057 }
The Android Open Source Project28527d22009-03-03 19:31:44 -08001058 }
1059 }
Robert Greenwalt2034b912009-08-12 16:08:25 -07001060
1061 if (newType != -1) {
1062 newNet = mNetTrackers[newType];
1063 /**
1064 * See if the other network is available to fail over to.
1065 * If is not available, we enable it anyway, so that it
1066 * will be able to connect when it does become available,
1067 * but we report a total loss of connectivity rather than
1068 * report that we are attempting to fail over.
1069 */
1070 if (newNet.isAvailable()) {
1071 NetworkInfo switchTo = newNet.getNetworkInfo();
1072 switchTo.setFailover(true);
Robert Greenwalta52c75a2009-08-19 20:19:33 -07001073 if (!switchTo.isConnectedOrConnecting() ||
1074 newNet.isTeardownRequested()) {
Robert Greenwalt2034b912009-08-12 16:08:25 -07001075 newNet.reconnect();
1076 }
1077 if (DBG) {
1078 if (switchTo.isConnected()) {
Joe Onoratoc2386bb2010-02-26 18:56:32 -08001079 Slog.v(TAG, "Switching to already connected " +
Robert Greenwalt2034b912009-08-12 16:08:25 -07001080 switchTo.getTypeName());
1081 } else {
Joe Onoratoc2386bb2010-02-26 18:56:32 -08001082 Slog.v(TAG, "Attempting to switch to " +
Robert Greenwalt2034b912009-08-12 16:08:25 -07001083 switchTo.getTypeName());
1084 }
1085 }
Robert Greenwalt2034b912009-08-12 16:08:25 -07001086 } else {
1087 newNet.reconnect();
Robert Greenwalt12984322010-03-09 14:55:08 -08001088 newNet = null; // not officially avail.. try anyway, but
1089 // report no failover
Robert Greenwalt2034b912009-08-12 16:08:25 -07001090 }
Robert Greenwalt2034b912009-08-12 16:08:25 -07001091 }
The Android Open Source Project28527d22009-03-03 19:31:44 -08001092 }
Robert Greenwalt2034b912009-08-12 16:08:25 -07001093
Robert Greenwaltf55ced92010-01-20 19:29:41 -08001094 return newNet;
The Android Open Source Project28527d22009-03-03 19:31:44 -08001095 }
1096
1097 private void sendConnectedBroadcast(NetworkInfo info) {
1098 Intent intent = new Intent(ConnectivityManager.CONNECTIVITY_ACTION);
1099 intent.putExtra(ConnectivityManager.EXTRA_NETWORK_INFO, info);
1100 if (info.isFailover()) {
1101 intent.putExtra(ConnectivityManager.EXTRA_IS_FAILOVER, true);
1102 info.setFailover(false);
1103 }
1104 if (info.getReason() != null) {
1105 intent.putExtra(ConnectivityManager.EXTRA_REASON, info.getReason());
1106 }
1107 if (info.getExtraInfo() != null) {
Robert Greenwalt0659da32009-07-16 17:21:39 -07001108 intent.putExtra(ConnectivityManager.EXTRA_EXTRA_INFO,
1109 info.getExtraInfo());
The Android Open Source Project28527d22009-03-03 19:31:44 -08001110 }
Mike Lockwoodfde2b762009-08-14 14:18:49 -04001111 sendStickyBroadcast(intent);
The Android Open Source Project28527d22009-03-03 19:31:44 -08001112 }
1113
1114 /**
1115 * Called when an attempt to fail over to another network has failed.
1116 * @param info the {@link NetworkInfo} for the failed network
1117 */
1118 private void handleConnectionFailure(NetworkInfo info) {
1119 mNetTrackers[info.getType()].setTeardownRequested(false);
The Android Open Source Project28527d22009-03-03 19:31:44 -08001120
Robert Greenwalt2034b912009-08-12 16:08:25 -07001121 String reason = info.getReason();
1122 String extraInfo = info.getExtraInfo();
Robert Greenwalt0659da32009-07-16 17:21:39 -07001123
Robert Greenwalt2034b912009-08-12 16:08:25 -07001124 if (DBG) {
1125 String reasonText;
1126 if (reason == null) {
1127 reasonText = ".";
1128 } else {
1129 reasonText = " (" + reason + ").";
The Android Open Source Project28527d22009-03-03 19:31:44 -08001130 }
Joe Onoratoc2386bb2010-02-26 18:56:32 -08001131 Slog.v(TAG, "Attempt to connect to " + info.getTypeName() +
Robert Greenwalt2034b912009-08-12 16:08:25 -07001132 " failed" + reasonText);
The Android Open Source Project28527d22009-03-03 19:31:44 -08001133 }
Robert Greenwalt2034b912009-08-12 16:08:25 -07001134
1135 Intent intent = new Intent(ConnectivityManager.CONNECTIVITY_ACTION);
1136 intent.putExtra(ConnectivityManager.EXTRA_NETWORK_INFO, info);
1137 if (getActiveNetworkInfo() == null) {
1138 intent.putExtra(ConnectivityManager.EXTRA_NO_CONNECTIVITY, true);
1139 }
1140 if (reason != null) {
1141 intent.putExtra(ConnectivityManager.EXTRA_REASON, reason);
1142 }
1143 if (extraInfo != null) {
1144 intent.putExtra(ConnectivityManager.EXTRA_EXTRA_INFO, extraInfo);
1145 }
1146 if (info.isFailover()) {
1147 intent.putExtra(ConnectivityManager.EXTRA_IS_FAILOVER, true);
1148 info.setFailover(false);
1149 }
Robert Greenwaltf55ced92010-01-20 19:29:41 -08001150
Robert Greenwalt3cc68d32010-01-25 17:54:29 -08001151 NetworkStateTracker newNet = null;
1152 if (mNetAttributes[info.getType()].isDefault()) {
1153 newNet = tryFailover(info.getType());
1154 if (newNet != null) {
1155 NetworkInfo switchTo = newNet.getNetworkInfo();
1156 intent.putExtra(ConnectivityManager.EXTRA_OTHER_NETWORK_INFO, switchTo);
1157 } else {
1158 intent.putExtra(ConnectivityManager.EXTRA_NO_CONNECTIVITY, true);
1159 }
Robert Greenwaltf55ced92010-01-20 19:29:41 -08001160 }
Robert Greenwalt3cc68d32010-01-25 17:54:29 -08001161
Mike Lockwoodfde2b762009-08-14 14:18:49 -04001162 sendStickyBroadcast(intent);
Robert Greenwaltf55ced92010-01-20 19:29:41 -08001163 /*
1164 * If the failover network is already connected, then immediately send
1165 * out a followup broadcast indicating successful failover
1166 */
1167 if (newNet != null && newNet.getNetworkInfo().isConnected()) {
1168 sendConnectedBroadcast(newNet.getNetworkInfo());
1169 }
Mike Lockwoodfde2b762009-08-14 14:18:49 -04001170 }
1171
1172 private void sendStickyBroadcast(Intent intent) {
1173 synchronized(this) {
Dianne Hackborna417ff82009-12-08 19:45:14 -08001174 if (!mSystemReady) {
1175 mInitialBroadcast = new Intent(intent);
Mike Lockwoodfde2b762009-08-14 14:18:49 -04001176 }
Dianne Hackborna417ff82009-12-08 19:45:14 -08001177 intent.addFlags(Intent.FLAG_RECEIVER_REGISTERED_ONLY_BEFORE_BOOT);
1178 mContext.sendStickyBroadcast(intent);
Mike Lockwoodfde2b762009-08-14 14:18:49 -04001179 }
1180 }
1181
1182 void systemReady() {
1183 synchronized(this) {
1184 mSystemReady = true;
Dianne Hackborna417ff82009-12-08 19:45:14 -08001185 if (mInitialBroadcast != null) {
1186 mContext.sendStickyBroadcast(mInitialBroadcast);
1187 mInitialBroadcast = null;
Mike Lockwoodfde2b762009-08-14 14:18:49 -04001188 }
1189 }
The Android Open Source Project28527d22009-03-03 19:31:44 -08001190 }
1191
1192 private void handleConnect(NetworkInfo info) {
Robert Greenwalt2034b912009-08-12 16:08:25 -07001193 int type = info.getType();
The Android Open Source Project28527d22009-03-03 19:31:44 -08001194
1195 // snapshot isFailover, because sendConnectedBroadcast() resets it
1196 boolean isFailover = info.isFailover();
Robert Greenwalt2034b912009-08-12 16:08:25 -07001197 NetworkStateTracker thisNet = mNetTrackers[type];
The Android Open Source Project28527d22009-03-03 19:31:44 -08001198
Robert Greenwalt2034b912009-08-12 16:08:25 -07001199 // if this is a default net and other default is running
1200 // kill the one not preferred
1201 if (mNetAttributes[type].isDefault()) {
Robert Greenwalt2034b912009-08-12 16:08:25 -07001202 if (mActiveDefaultNetwork != -1 && mActiveDefaultNetwork != type) {
1203 if ((type != mNetworkPreference &&
1204 mNetAttributes[mActiveDefaultNetwork].mPriority >
1205 mNetAttributes[type].mPriority) ||
1206 mNetworkPreference == mActiveDefaultNetwork) {
1207 // don't accept this one
Joe Onoratoc2386bb2010-02-26 18:56:32 -08001208 if (DBG) Slog.v(TAG, "Not broadcasting CONNECT_ACTION " +
Robert Greenwalt2034b912009-08-12 16:08:25 -07001209 "to torn down network " + info.getTypeName());
1210 teardown(thisNet);
1211 return;
1212 } else {
1213 // tear down the other
1214 NetworkStateTracker otherNet =
1215 mNetTrackers[mActiveDefaultNetwork];
Joe Onoratoc2386bb2010-02-26 18:56:32 -08001216 if (DBG) Slog.v(TAG, "Policy requires " +
Robert Greenwalt2034b912009-08-12 16:08:25 -07001217 otherNet.getNetworkInfo().getTypeName() +
1218 " teardown");
1219 if (!teardown(otherNet)) {
Joe Onoratoc2386bb2010-02-26 18:56:32 -08001220 Slog.e(TAG, "Network declined teardown request");
Robert Greenwalt2034b912009-08-12 16:08:25 -07001221 return;
1222 }
Robert Greenwalt93dc1042010-06-15 12:19:37 -07001223 }
1224 }
1225 synchronized (ConnectivityService.this) {
1226 // have a new default network, release the transition wakelock in a second
1227 // if it's held. The second pause is to allow apps to reconnect over the
1228 // new network
1229 if (mNetTransitionWakeLock.isHeld()) {
1230 mHandler.sendMessageDelayed(mHandler.obtainMessage(
1231 NetworkStateTracker.EVENT_CLEAR_NET_TRANSITION_WAKELOCK,
1232 mNetTransitionWakeLockSerialNumber, 0),
1233 1000);
Robert Greenwalt2034b912009-08-12 16:08:25 -07001234 }
1235 }
1236 mActiveDefaultNetwork = type;
1237 }
The Android Open Source Project28527d22009-03-03 19:31:44 -08001238 thisNet.setTeardownRequested(false);
Irfan Sheriff7f132d92010-06-09 15:39:36 -07001239 updateNetworkSettings(thisNet);
Robert Greenwalt3afbead2010-07-23 15:46:26 -07001240 handleConnectivityChange(type);
Robert Greenwalt2034b912009-08-12 16:08:25 -07001241 sendConnectedBroadcast(info);
The Android Open Source Project28527d22009-03-03 19:31:44 -08001242 }
1243
The Android Open Source Project28527d22009-03-03 19:31:44 -08001244 /**
Robert Greenwalt3afbead2010-07-23 15:46:26 -07001245 * After a change in the connectivity state of a network. We're mainly
1246 * concerned with making sure that the list of DNS servers is set up
1247 * according to which networks are connected, and ensuring that the
1248 * right routing table entries exist.
The Android Open Source Project28527d22009-03-03 19:31:44 -08001249 */
Robert Greenwalt3afbead2010-07-23 15:46:26 -07001250 private void handleConnectivityChange(int netType) {
The Android Open Source Project28527d22009-03-03 19:31:44 -08001251 /*
Robert Greenwalt2034b912009-08-12 16:08:25 -07001252 * If a non-default network is enabled, add the host routes that
Robert Greenwalt3afbead2010-07-23 15:46:26 -07001253 * will allow it's DNS servers to be accessed.
The Android Open Source Project28527d22009-03-03 19:31:44 -08001254 */
Robert Greenwalt3afbead2010-07-23 15:46:26 -07001255 handleDnsConfigurationChange(netType);
Robert Greenwalt2034b912009-08-12 16:08:25 -07001256
Robert Greenwalt3afbead2010-07-23 15:46:26 -07001257 if (mNetTrackers[netType].getNetworkInfo().isConnected()) {
1258 if (mNetAttributes[netType].isDefault()) {
1259 addDefaultRoute(mNetTrackers[netType]);
Robert Greenwalt2034b912009-08-12 16:08:25 -07001260 } else {
Robert Greenwalt3afbead2010-07-23 15:46:26 -07001261 addPrivateDnsRoutes(mNetTrackers[netType]);
1262 }
1263 } else {
1264 if (mNetAttributes[netType].isDefault()) {
1265 removeDefaultRoute(mNetTrackers[netType]);
1266 } else {
1267 removePrivateDnsRoutes(mNetTrackers[netType]);
Robert Greenwalt2034b912009-08-12 16:08:25 -07001268 }
The Android Open Source Project28527d22009-03-03 19:31:44 -08001269 }
1270 }
1271
Irfan Sheriff7f132d92010-06-09 15:39:36 -07001272 private void addPrivateDnsRoutes(NetworkStateTracker nt) {
Irfan Sheriff7f132d92010-06-09 15:39:36 -07001273 boolean privateDnsRouteSet = nt.isPrivateDnsRouteSet();
Robert Greenwalt1f1bcfe2010-08-30 10:56:47 -07001274 LinkProperties p = nt.getLinkProperties();
Robert Greenwalta7dfbd32010-06-15 15:43:39 -07001275 if (p == null) return;
1276 String interfaceName = p.getInterfaceName();
Irfan Sheriff7f132d92010-06-09 15:39:36 -07001277
1278 if (DBG) {
1279 Slog.d(TAG, "addPrivateDnsRoutes for " + nt +
1280 "(" + interfaceName + ") - mPrivateDnsRouteSet = " + privateDnsRouteSet);
1281 }
Irfan Sheriff7f132d92010-06-09 15:39:36 -07001282 if (interfaceName != null && !privateDnsRouteSet) {
Robert Greenwalta7dfbd32010-06-15 15:43:39 -07001283 Collection<InetAddress> dnsList = p.getDnses();
1284 for (InetAddress dns : dnsList) {
1285 if (DBG) Slog.d(TAG, " adding " + dns);
Robert Greenwalt7fe44cb2010-08-27 09:24:29 -07001286 NetworkUtils.addHostRoute(interfaceName, dns, null);
Irfan Sheriff7f132d92010-06-09 15:39:36 -07001287 }
1288 nt.privateDnsRouteSet(true);
1289 }
1290 }
1291
1292 private void removePrivateDnsRoutes(NetworkStateTracker nt) {
1293 // TODO - we should do this explicitly but the NetUtils api doesnt
1294 // support this yet - must remove all. No worse than before
Robert Greenwalt1f1bcfe2010-08-30 10:56:47 -07001295 LinkProperties p = nt.getLinkProperties();
Robert Greenwalta7dfbd32010-06-15 15:43:39 -07001296 if (p == null) return;
1297 String interfaceName = p.getInterfaceName();
Irfan Sheriff7f132d92010-06-09 15:39:36 -07001298 boolean privateDnsRouteSet = nt.isPrivateDnsRouteSet();
1299 if (interfaceName != null && privateDnsRouteSet) {
1300 if (DBG) {
1301 Slog.d(TAG, "removePrivateDnsRoutes for " + nt.getNetworkInfo().getTypeName() +
1302 " (" + interfaceName + ")");
1303 }
1304 NetworkUtils.removeHostRoutes(interfaceName);
1305 nt.privateDnsRouteSet(false);
1306 }
1307 }
1308
Irfan Sheriff7f132d92010-06-09 15:39:36 -07001309
1310 private void addDefaultRoute(NetworkStateTracker nt) {
Robert Greenwalt1f1bcfe2010-08-30 10:56:47 -07001311 LinkProperties p = nt.getLinkProperties();
Robert Greenwalta7dfbd32010-06-15 15:43:39 -07001312 if (p == null) return;
1313 String interfaceName = p.getInterfaceName();
1314 InetAddress defaultGatewayAddr = p.getGateway();
Irfan Sheriff7f132d92010-06-09 15:39:36 -07001315
Robert Greenwalt3afbead2010-07-23 15:46:26 -07001316 if ((interfaceName != null) && (defaultGatewayAddr != null )) {
Robert Greenwalt7fe44cb2010-08-27 09:24:29 -07001317 if (!NetworkUtils.addDefaultRoute(interfaceName, defaultGatewayAddr) && DBG) {
Robert Greenwalta7dfbd32010-06-15 15:43:39 -07001318 NetworkInfo networkInfo = nt.getNetworkInfo();
Irfan Sheriff7f132d92010-06-09 15:39:36 -07001319 Slog.d(TAG, "addDefaultRoute for " + networkInfo.getTypeName() +
1320 " (" + interfaceName + "), GatewayAddr=" + defaultGatewayAddr);
1321 }
Irfan Sheriff7f132d92010-06-09 15:39:36 -07001322 }
1323 }
1324
1325
1326 public void removeDefaultRoute(NetworkStateTracker nt) {
Robert Greenwalt1f1bcfe2010-08-30 10:56:47 -07001327 LinkProperties p = nt.getLinkProperties();
Robert Greenwalta7dfbd32010-06-15 15:43:39 -07001328 if (p == null) return;
1329 String interfaceName = p.getInterfaceName();
Irfan Sheriff7f132d92010-06-09 15:39:36 -07001330
Robert Greenwalt3afbead2010-07-23 15:46:26 -07001331 if (interfaceName != null) {
1332 if ((NetworkUtils.removeDefaultRoute(interfaceName) >= 0) && DBG) {
Robert Greenwalta7dfbd32010-06-15 15:43:39 -07001333 NetworkInfo networkInfo = nt.getNetworkInfo();
Irfan Sheriff7f132d92010-06-09 15:39:36 -07001334 Slog.d(TAG, "removeDefaultRoute for " + networkInfo.getTypeName() + " (" +
1335 interfaceName + ")");
1336 }
Irfan Sheriff7f132d92010-06-09 15:39:36 -07001337 }
1338 }
1339
1340 /**
1341 * Reads the network specific TCP buffer sizes from SystemProperties
1342 * net.tcp.buffersize.[default|wifi|umts|edge|gprs] and set them for system
1343 * wide use
1344 */
1345 public void updateNetworkSettings(NetworkStateTracker nt) {
1346 String key = nt.getTcpBufferSizesPropName();
1347 String bufferSizes = SystemProperties.get(key);
1348
1349 if (bufferSizes.length() == 0) {
1350 Slog.e(TAG, key + " not found in system properties. Using defaults");
1351
1352 // Setting to default values so we won't be stuck to previous values
1353 key = "net.tcp.buffersize.default";
1354 bufferSizes = SystemProperties.get(key);
1355 }
1356
1357 // Set values in kernel
1358 if (bufferSizes.length() != 0) {
1359 if (DBG) {
1360 Slog.v(TAG, "Setting TCP values: [" + bufferSizes
1361 + "] which comes from [" + key + "]");
1362 }
1363 setBufferSize(bufferSizes);
1364 }
1365 }
1366
1367 /**
1368 * Writes TCP buffer sizes to /sys/kernel/ipv4/tcp_[r/w]mem_[min/def/max]
1369 * which maps to /proc/sys/net/ipv4/tcp_rmem and tcpwmem
1370 *
1371 * @param bufferSizes in the format of "readMin, readInitial, readMax,
1372 * writeMin, writeInitial, writeMax"
1373 */
1374 private void setBufferSize(String bufferSizes) {
1375 try {
1376 String[] values = bufferSizes.split(",");
1377
1378 if (values.length == 6) {
1379 final String prefix = "/sys/kernel/ipv4/tcp_";
1380 stringToFile(prefix + "rmem_min", values[0]);
1381 stringToFile(prefix + "rmem_def", values[1]);
1382 stringToFile(prefix + "rmem_max", values[2]);
1383 stringToFile(prefix + "wmem_min", values[3]);
1384 stringToFile(prefix + "wmem_def", values[4]);
1385 stringToFile(prefix + "wmem_max", values[5]);
1386 } else {
1387 Slog.e(TAG, "Invalid buffersize string: " + bufferSizes);
1388 }
1389 } catch (IOException e) {
1390 Slog.e(TAG, "Can't set tcp buffer sizes:" + e);
1391 }
1392 }
1393
1394 /**
1395 * Writes string to file. Basically same as "echo -n $string > $filename"
1396 *
1397 * @param filename
1398 * @param string
1399 * @throws IOException
1400 */
1401 private void stringToFile(String filename, String string) throws IOException {
1402 FileWriter out = new FileWriter(filename);
1403 try {
1404 out.write(string);
1405 } finally {
1406 out.close();
1407 }
1408 }
1409
1410
Robert Greenwalt2034b912009-08-12 16:08:25 -07001411 /**
1412 * Adjust the per-process dns entries (net.dns<x>.<pid>) based
1413 * on the highest priority active net which this process requested.
1414 * If there aren't any, clear it out
1415 */
1416 private void reassessPidDns(int myPid, boolean doBump)
1417 {
Joe Onoratoc2386bb2010-02-26 18:56:32 -08001418 if (DBG) Slog.d(TAG, "reassessPidDns for pid " + myPid);
Robert Greenwalt2034b912009-08-12 16:08:25 -07001419 for(int i : mPriorityList) {
1420 if (mNetAttributes[i].isDefault()) {
1421 continue;
1422 }
1423 NetworkStateTracker nt = mNetTrackers[i];
Robert Greenwalt0659da32009-07-16 17:21:39 -07001424 if (nt.getNetworkInfo().isConnected() &&
1425 !nt.isTeardownRequested()) {
Robert Greenwalt1f1bcfe2010-08-30 10:56:47 -07001426 LinkProperties p = nt.getLinkProperties();
Robert Greenwalta7dfbd32010-06-15 15:43:39 -07001427 if (p == null) continue;
Robert Greenwalt2034b912009-08-12 16:08:25 -07001428 List pids = mNetRequestersPids[i];
1429 for (int j=0; j<pids.size(); j++) {
1430 Integer pid = (Integer)pids.get(j);
1431 if (pid.intValue() == myPid) {
Robert Greenwalta7dfbd32010-06-15 15:43:39 -07001432 Collection<InetAddress> dnses = p.getDnses();
1433 writePidDns(dnses, myPid);
Robert Greenwalt2034b912009-08-12 16:08:25 -07001434 if (doBump) {
1435 bumpDns();
1436 }
1437 return;
1438 }
1439 }
1440 }
1441 }
1442 // nothing found - delete
1443 for (int i = 1; ; i++) {
1444 String prop = "net.dns" + i + "." + myPid;
1445 if (SystemProperties.get(prop).length() == 0) {
1446 if (doBump) {
1447 bumpDns();
1448 }
1449 return;
1450 }
1451 SystemProperties.set(prop, "");
1452 }
1453 }
1454
Robert Greenwalta7dfbd32010-06-15 15:43:39 -07001455 private void writePidDns(Collection <InetAddress> dnses, int pid) {
Robert Greenwalt2034b912009-08-12 16:08:25 -07001456 int j = 1;
Robert Greenwalta7dfbd32010-06-15 15:43:39 -07001457 for (InetAddress dns : dnses) {
1458 SystemProperties.set("net.dns" + j++ + "." + pid, dns.getHostAddress());
Robert Greenwalt2034b912009-08-12 16:08:25 -07001459 }
1460 }
1461
1462 private void bumpDns() {
1463 /*
1464 * Bump the property that tells the name resolver library to reread
1465 * the DNS server list from the properties.
1466 */
1467 String propVal = SystemProperties.get("net.dnschange");
1468 int n = 0;
1469 if (propVal.length() != 0) {
1470 try {
1471 n = Integer.parseInt(propVal);
1472 } catch (NumberFormatException e) {}
1473 }
1474 SystemProperties.set("net.dnschange", "" + (n+1));
1475 }
1476
Robert Greenwalt3afbead2010-07-23 15:46:26 -07001477 private void handleDnsConfigurationChange(int netType) {
Robert Greenwalt2034b912009-08-12 16:08:25 -07001478 // add default net's dns entries
Robert Greenwalt3afbead2010-07-23 15:46:26 -07001479 NetworkStateTracker nt = mNetTrackers[netType];
1480 if (nt != null && nt.getNetworkInfo().isConnected() && !nt.isTeardownRequested()) {
Robert Greenwalt1f1bcfe2010-08-30 10:56:47 -07001481 LinkProperties p = nt.getLinkProperties();
Robert Greenwalt3afbead2010-07-23 15:46:26 -07001482 if (p == null) return;
1483 Collection<InetAddress> dnses = p.getDnses();
1484 if (mNetAttributes[netType].isDefault()) {
1485 int j = 1;
Robert Greenwalt94daa182010-09-01 11:34:05 -07001486 if (dnses.size() == 0 && mDefaultDns != null) {
Robert Greenwalt3afbead2010-07-23 15:46:26 -07001487 if (DBG) {
Robert Greenwalt94daa182010-09-01 11:34:05 -07001488 Slog.d(TAG, "no dns provided - using " + mDefaultDns.getHostAddress());
Robert Greenwalt2034b912009-08-12 16:08:25 -07001489 }
Robert Greenwalt94daa182010-09-01 11:34:05 -07001490 SystemProperties.set("net.dns1", mDefaultDns.getHostAddress());
1491 j++;
1492 } else {
1493 for (InetAddress dns : dnses) {
1494 if (DBG) {
1495 Slog.d(TAG, "adding dns " + dns + " for " +
1496 nt.getNetworkInfo().getTypeName());
1497 }
1498 SystemProperties.set("net.dns" + j++, dns.getHostAddress());
1499 }
Robert Greenwalt3afbead2010-07-23 15:46:26 -07001500 }
1501 for (int k=j ; k<mNumDnsEntries; k++) {
1502 if (DBG) Slog.d(TAG, "erasing net.dns" + k);
1503 SystemProperties.set("net.dns" + k, "");
1504 }
1505 mNumDnsEntries = j;
1506 } else {
1507 // set per-pid dns for attached secondary nets
1508 List pids = mNetRequestersPids[netType];
1509 for (int y=0; y< pids.size(); y++) {
1510 Integer pid = (Integer)pids.get(y);
1511 writePidDns(dnses, pid.intValue());
The Android Open Source Project28527d22009-03-03 19:31:44 -08001512 }
1513 }
Robert Greenwalt3afbead2010-07-23 15:46:26 -07001514 bumpDns();
The Android Open Source Project28527d22009-03-03 19:31:44 -08001515 }
Robert Greenwalt2034b912009-08-12 16:08:25 -07001516 }
1517
1518 private int getRestoreDefaultNetworkDelay() {
1519 String restoreDefaultNetworkDelayStr = SystemProperties.get(
1520 NETWORK_RESTORE_DELAY_PROP_NAME);
1521 if(restoreDefaultNetworkDelayStr != null &&
1522 restoreDefaultNetworkDelayStr.length() != 0) {
1523 try {
1524 return Integer.valueOf(restoreDefaultNetworkDelayStr);
1525 } catch (NumberFormatException e) {
1526 }
The Android Open Source Project28527d22009-03-03 19:31:44 -08001527 }
Robert Greenwalt2034b912009-08-12 16:08:25 -07001528 return RESTORE_DEFAULT_NETWORK_DELAY;
The Android Open Source Project28527d22009-03-03 19:31:44 -08001529 }
1530
1531 @Override
1532 protected void dump(FileDescriptor fd, PrintWriter pw, String[] args) {
Robert Greenwalt0659da32009-07-16 17:21:39 -07001533 if (mContext.checkCallingOrSelfPermission(
1534 android.Manifest.permission.DUMP)
The Android Open Source Project28527d22009-03-03 19:31:44 -08001535 != PackageManager.PERMISSION_GRANTED) {
Robert Greenwalt0659da32009-07-16 17:21:39 -07001536 pw.println("Permission Denial: can't dump ConnectivityService " +
1537 "from from pid=" + Binder.getCallingPid() + ", uid=" +
1538 Binder.getCallingUid());
The Android Open Source Project28527d22009-03-03 19:31:44 -08001539 return;
1540 }
The Android Open Source Project28527d22009-03-03 19:31:44 -08001541 pw.println();
1542 for (NetworkStateTracker nst : mNetTrackers) {
Robert Greenwalt3eeb6032009-12-21 18:24:07 -08001543 if (nst != null) {
1544 if (nst.getNetworkInfo().isConnected()) {
1545 pw.println("Active network: " + nst.getNetworkInfo().
1546 getTypeName());
1547 }
1548 pw.println(nst.getNetworkInfo());
1549 pw.println(nst);
1550 pw.println();
Robert Greenwalt2034b912009-08-12 16:08:25 -07001551 }
The Android Open Source Project28527d22009-03-03 19:31:44 -08001552 }
Robert Greenwalt3eeb6032009-12-21 18:24:07 -08001553
1554 pw.println("Network Requester Pids:");
1555 for (int net : mPriorityList) {
1556 String pidString = net + ": ";
1557 for (Object pid : mNetRequestersPids[net]) {
1558 pidString = pidString + pid.toString() + ", ";
1559 }
1560 pw.println(pidString);
1561 }
1562 pw.println();
1563
1564 pw.println("FeatureUsers:");
1565 for (Object requester : mFeatureUsers) {
1566 pw.println(requester.toString());
1567 }
1568 pw.println();
Robert Greenwalt8e87f122010-02-11 18:18:40 -08001569
Robert Greenwalt93dc1042010-06-15 12:19:37 -07001570 synchronized (this) {
1571 pw.println("NetworkTranstionWakeLock is currently " +
1572 (mNetTransitionWakeLock.isHeld() ? "" : "not ") + "held.");
1573 pw.println("It was last requested for "+mNetTransitionWakeLockCausedBy);
1574 }
1575 pw.println();
1576
Robert Greenwalt8e87f122010-02-11 18:18:40 -08001577 mTethering.dump(fd, pw, args);
The Android Open Source Project28527d22009-03-03 19:31:44 -08001578 }
1579
Robert Greenwalt2034b912009-08-12 16:08:25 -07001580 // must be stateless - things change under us.
The Android Open Source Project28527d22009-03-03 19:31:44 -08001581 private class MyHandler extends Handler {
1582 @Override
1583 public void handleMessage(Message msg) {
1584 NetworkInfo info;
1585 switch (msg.what) {
1586 case NetworkStateTracker.EVENT_STATE_CHANGED:
1587 info = (NetworkInfo) msg.obj;
Robert Greenwalt12c44552009-12-07 11:33:18 -08001588 int type = info.getType();
1589 NetworkInfo.State state = info.getState();
Robert Greenwalt24e2d2b2010-01-25 16:14:00 -08001590 // only do this optimization for wifi. It going into scan mode for location
1591 // services generates alot of noise. Meanwhile the mms apn won't send out
1592 // subsequent notifications when on default cellular because it never
1593 // disconnects.. so only do this to wifi notifications. Fixed better when the
1594 // APN notifications are standardized.
1595 if (mNetAttributes[type].mLastState == state &&
1596 mNetAttributes[type].mRadio == ConnectivityManager.TYPE_WIFI) {
Robert Greenwalt12c44552009-12-07 11:33:18 -08001597 if (DBG) {
Robert Greenwalt24e2d2b2010-01-25 16:14:00 -08001598 // TODO - remove this after we validate the dropping doesn't break
1599 // anything
Joe Onoratoc2386bb2010-02-26 18:56:32 -08001600 Slog.d(TAG, "Dropping ConnectivityChange for " +
Robert Greenwalt2adbc7f2010-01-13 09:36:31 -08001601 info.getTypeName() + ": " +
Robert Greenwalt12c44552009-12-07 11:33:18 -08001602 state + "/" + info.getDetailedState());
1603 }
1604 return;
1605 }
1606 mNetAttributes[type].mLastState = state;
1607
Joe Onoratoc2386bb2010-02-26 18:56:32 -08001608 if (DBG) Slog.d(TAG, "ConnectivityChange for " +
Robert Greenwalt0659da32009-07-16 17:21:39 -07001609 info.getTypeName() + ": " +
Robert Greenwalt12c44552009-12-07 11:33:18 -08001610 state + "/" + info.getDetailedState());
The Android Open Source Project28527d22009-03-03 19:31:44 -08001611
1612 // Connectivity state changed:
1613 // [31-13] Reserved for future use
Robert Greenwalt0659da32009-07-16 17:21:39 -07001614 // [12-9] Network subtype (for mobile network, as defined
1615 // by TelephonyManager)
1616 // [8-3] Detailed state ordinal (as defined by
1617 // NetworkInfo.DetailedState)
The Android Open Source Project28527d22009-03-03 19:31:44 -08001618 // [2-0] Network type (as defined by ConnectivityManager)
1619 int eventLogParam = (info.getType() & 0x7) |
1620 ((info.getDetailedState().ordinal() & 0x3f) << 3) |
1621 (info.getSubtype() << 9);
Doug Zongker2fc96232009-12-04 10:31:43 -08001622 EventLog.writeEvent(EventLogTags.CONNECTIVITY_STATE_CHANGED,
Robert Greenwalt0659da32009-07-16 17:21:39 -07001623 eventLogParam);
1624
1625 if (info.getDetailedState() ==
1626 NetworkInfo.DetailedState.FAILED) {
The Android Open Source Project28527d22009-03-03 19:31:44 -08001627 handleConnectionFailure(info);
Robert Greenwalt12c44552009-12-07 11:33:18 -08001628 } else if (state == NetworkInfo.State.DISCONNECTED) {
The Android Open Source Project28527d22009-03-03 19:31:44 -08001629 handleDisconnect(info);
Robert Greenwalt12c44552009-12-07 11:33:18 -08001630 } else if (state == NetworkInfo.State.SUSPENDED) {
The Android Open Source Project28527d22009-03-03 19:31:44 -08001631 // TODO: need to think this over.
Robert Greenwalt0659da32009-07-16 17:21:39 -07001632 // the logic here is, handle SUSPENDED the same as
1633 // DISCONNECTED. The only difference being we are
1634 // broadcasting an intent with NetworkInfo that's
1635 // suspended. This allows the applications an
1636 // opportunity to handle DISCONNECTED and SUSPENDED
1637 // differently, or not.
The Android Open Source Project28527d22009-03-03 19:31:44 -08001638 handleDisconnect(info);
Robert Greenwalt12c44552009-12-07 11:33:18 -08001639 } else if (state == NetworkInfo.State.CONNECTED) {
The Android Open Source Project28527d22009-03-03 19:31:44 -08001640 handleConnect(info);
1641 }
The Android Open Source Project28527d22009-03-03 19:31:44 -08001642 break;
The Android Open Source Project28527d22009-03-03 19:31:44 -08001643 case NetworkStateTracker.EVENT_CONFIGURATION_CHANGED:
Robert Greenwalt3afbead2010-07-23 15:46:26 -07001644 // TODO - make this handle ip/proxy/gateway/dns changes
1645 info = (NetworkInfo) msg.obj;
1646 type = info.getType();
1647 handleDnsConfigurationChange(type);
The Android Open Source Project28527d22009-03-03 19:31:44 -08001648 break;
Robert Greenwalt2034b912009-08-12 16:08:25 -07001649 case NetworkStateTracker.EVENT_RESTORE_DEFAULT_NETWORK:
Robert Greenwaltaffc3a12009-09-27 17:27:04 -07001650 FeatureUser u = (FeatureUser)msg.obj;
1651 u.expire();
Robert Greenwalt2034b912009-08-12 16:08:25 -07001652 break;
Robert Greenwalt93dc1042010-06-15 12:19:37 -07001653 case NetworkStateTracker.EVENT_CLEAR_NET_TRANSITION_WAKELOCK:
1654 String causedBy = null;
1655 synchronized (ConnectivityService.this) {
1656 if (msg.arg1 == mNetTransitionWakeLockSerialNumber &&
1657 mNetTransitionWakeLock.isHeld()) {
1658 mNetTransitionWakeLock.release();
1659 causedBy = mNetTransitionWakeLockCausedBy;
1660 }
1661 }
1662 if (causedBy != null) {
1663 Slog.d(TAG, "NetTransition Wakelock for " +
1664 causedBy + " released by timeout");
1665 }
1666 break;
The Android Open Source Project28527d22009-03-03 19:31:44 -08001667 }
1668 }
1669 }
Robert Greenwalt0c4828c2010-01-26 11:40:34 -08001670
1671 // javadoc from interface
Robert Greenwalt4283ded2010-03-02 17:25:02 -08001672 public int tether(String iface) {
Robert Greenwalt0c4828c2010-01-26 11:40:34 -08001673 enforceTetherChangePermission();
Robert Greenwalt4283ded2010-03-02 17:25:02 -08001674
1675 if (isTetheringSupported()) {
1676 return mTethering.tether(iface);
1677 } else {
1678 return ConnectivityManager.TETHER_ERROR_UNSUPPORTED;
1679 }
Robert Greenwalt0c4828c2010-01-26 11:40:34 -08001680 }
1681
1682 // javadoc from interface
Robert Greenwalt4283ded2010-03-02 17:25:02 -08001683 public int untether(String iface) {
Robert Greenwalt0c4828c2010-01-26 11:40:34 -08001684 enforceTetherChangePermission();
Robert Greenwalt4283ded2010-03-02 17:25:02 -08001685
1686 if (isTetheringSupported()) {
1687 return mTethering.untether(iface);
1688 } else {
1689 return ConnectivityManager.TETHER_ERROR_UNSUPPORTED;
1690 }
1691 }
1692
1693 // javadoc from interface
1694 public int getLastTetherError(String iface) {
1695 enforceTetherAccessPermission();
1696
1697 if (isTetheringSupported()) {
1698 return mTethering.getLastTetherError(iface);
1699 } else {
1700 return ConnectivityManager.TETHER_ERROR_UNSUPPORTED;
1701 }
Robert Greenwalt8e87f122010-02-11 18:18:40 -08001702 }
1703
1704 // TODO - proper iface API for selection by property, inspection, etc
1705 public String[] getTetherableUsbRegexs() {
1706 enforceTetherAccessPermission();
1707 if (isTetheringSupported()) {
1708 return mTethering.getTetherableUsbRegexs();
1709 } else {
1710 return new String[0];
1711 }
1712 }
1713
1714 public String[] getTetherableWifiRegexs() {
1715 enforceTetherAccessPermission();
1716 if (isTetheringSupported()) {
1717 return mTethering.getTetherableWifiRegexs();
1718 } else {
1719 return new String[0];
1720 }
Robert Greenwalt0c4828c2010-01-26 11:40:34 -08001721 }
1722
Danica Chang96567052010-08-11 14:54:43 -07001723 public String[] getTetherableBluetoothRegexs() {
1724 enforceTetherAccessPermission();
1725 if (isTetheringSupported()) {
1726 return mTethering.getTetherableBluetoothRegexs();
1727 } else {
1728 return new String[0];
1729 }
1730 }
1731
Robert Greenwalt0c4828c2010-01-26 11:40:34 -08001732 // TODO - move iface listing, queries, etc to new module
1733 // javadoc from interface
1734 public String[] getTetherableIfaces() {
Robert Greenwalt8e87f122010-02-11 18:18:40 -08001735 enforceTetherAccessPermission();
Robert Greenwalt0c4828c2010-01-26 11:40:34 -08001736 return mTethering.getTetherableIfaces();
1737 }
1738
1739 public String[] getTetheredIfaces() {
Robert Greenwalt8e87f122010-02-11 18:18:40 -08001740 enforceTetherAccessPermission();
Robert Greenwalt0c4828c2010-01-26 11:40:34 -08001741 return mTethering.getTetheredIfaces();
1742 }
Robert Greenwalt8e87f122010-02-11 18:18:40 -08001743
Robert Greenwalt4283ded2010-03-02 17:25:02 -08001744 public String[] getTetheringErroredIfaces() {
1745 enforceTetherAccessPermission();
1746 return mTethering.getErroredIfaces();
1747 }
1748
Robert Greenwalt8e87f122010-02-11 18:18:40 -08001749 // if ro.tether.denied = true we default to no tethering
1750 // gservices could set the secure setting to 1 though to enable it on a build where it
1751 // had previously been turned off.
1752 public boolean isTetheringSupported() {
1753 enforceTetherAccessPermission();
1754 int defaultVal = (SystemProperties.get("ro.tether.denied").equals("true") ? 0 : 1);
Robert Greenwaltf1b66e12010-02-25 12:29:30 -08001755 boolean tetherEnabledInSettings = (Settings.Secure.getInt(mContext.getContentResolver(),
1756 Settings.Secure.TETHER_SUPPORTED, defaultVal) != 0);
1757 return tetherEnabledInSettings && mTetheringConfigValid;
Robert Greenwalt8e87f122010-02-11 18:18:40 -08001758 }
Robert Greenwalt93dc1042010-06-15 12:19:37 -07001759
1760 // An API NetworkStateTrackers can call when they lose their network.
1761 // This will automatically be cleared after X seconds or a network becomes CONNECTED,
1762 // whichever happens first. The timer is started by the first caller and not
1763 // restarted by subsequent callers.
1764 public void requestNetworkTransitionWakelock(String forWhom) {
1765 enforceConnectivityInternalPermission();
1766 synchronized (this) {
1767 if (mNetTransitionWakeLock.isHeld()) return;
1768 mNetTransitionWakeLockSerialNumber++;
1769 mNetTransitionWakeLock.acquire();
1770 mNetTransitionWakeLockCausedBy = forWhom;
1771 }
1772 mHandler.sendMessageDelayed(mHandler.obtainMessage(
1773 NetworkStateTracker.EVENT_CLEAR_NET_TRANSITION_WAKELOCK,
1774 mNetTransitionWakeLockSerialNumber, 0),
1775 mNetTransitionWakeLockTimeout);
1776 return;
1777 }
The Android Open Source Project28527d22009-03-03 19:31:44 -08001778}