blob: b6d725ff43ff3aa504fc867e7e0c5d14d95934c0 [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
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 Greenwalt37e65eb2010-08-30 10:56:47 -070029import android.net.LinkProperties;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080030import android.net.NetworkStateTracker;
Robert Greenwalt585ac0f2010-08-27 09:24:29 -070031import android.net.NetworkUtils;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080032import android.net.wifi.WifiStateTracker;
Irfan Sheriffd649c122010-06-09 15:39:36 -070033import android.net.NetworkUtils;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080034import android.os.Binder;
35import android.os.Handler;
Wink Savillebb08caf2010-09-02 19:23:52 -070036import android.os.HandlerThread;
Robert Greenwalt42acef32009-08-12 16:08:25 -070037import android.os.IBinder;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080038import android.os.Looper;
39import android.os.Message;
Robert Greenwalt14f2ef42010-06-15 12:19:37 -070040import android.os.PowerManager;
Robert Greenwalt42acef32009-08-12 16:08:25 -070041import android.os.RemoteException;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080042import android.os.ServiceManager;
43import android.os.SystemProperties;
44import android.provider.Settings;
Robert Greenwalt42acef32009-08-12 16:08:25 -070045import android.text.TextUtils;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080046import android.util.EventLog;
Joe Onorato8a9b2202010-02-26 18:56:32 -080047import android.util.Slog;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080048
Robert Greenwalt42acef32009-08-12 16:08:25 -070049import com.android.internal.telephony.Phone;
50
Robert Greenwaltd0e18ff2010-01-26 11:40:34 -080051import com.android.server.connectivity.Tethering;
52
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080053import java.io.FileDescriptor;
Irfan Sheriffd649c122010-06-09 15:39:36 -070054import java.io.FileWriter;
55import java.io.IOException;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080056import java.io.PrintWriter;
Robert Greenwalt47f69fe2010-06-15 15:43:39 -070057import java.net.InetAddress;
58import java.net.UnknownHostException;
Robert Greenwalt42acef32009-08-12 16:08:25 -070059import java.util.ArrayList;
Robert Greenwalt47f69fe2010-06-15 15:43:39 -070060import java.util.Collection;
Robert Greenwalt42acef32009-08-12 16:08:25 -070061import java.util.List;
Robert Greenwalt585ac0f2010-08-27 09:24:29 -070062import java.net.InetAddress;
63import java.net.UnknownHostException;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080064
65/**
66 * @hide
67 */
68public class ConnectivityService extends IConnectivityManager.Stub {
69
Robert Greenwaltd8df1492009-10-06 14:12:53 -070070 private static final boolean DBG = true;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080071 private static final String TAG = "ConnectivityService";
72
Robert Greenwalt42acef32009-08-12 16:08:25 -070073 // how long to wait before switching back to a radio's default network
74 private static final int RESTORE_DEFAULT_NETWORK_DELAY = 1 * 60 * 1000;
75 // system property that can override the above value
76 private static final String NETWORK_RESTORE_DELAY_PROP_NAME =
77 "android.telephony.apn-restore";
78
Robert Greenwaltd0e18ff2010-01-26 11:40:34 -080079 private Tethering mTethering;
Robert Greenwaltc9d5fb72010-02-25 12:29:30 -080080 private boolean mTetheringConfigValid = false;
Robert Greenwaltd0e18ff2010-01-26 11:40:34 -080081
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080082 /**
83 * Sometimes we want to refer to the individual network state
84 * trackers separately, and sometimes we just want to treat them
85 * abstractly.
86 */
87 private NetworkStateTracker mNetTrackers[];
Robert Greenwalt42acef32009-08-12 16:08:25 -070088
89 /**
90 * A per Net list of the PID's that requested access to the net
91 * used both as a refcount and for per-PID DNS selection
92 */
93 private List mNetRequestersPids[];
94
Irfan Sheriffa2a1b912010-06-07 09:03:04 -070095 private WifiWatchdogService mWifiWatchdogService;
96
Robert Greenwalt42acef32009-08-12 16:08:25 -070097 // priority order of the nettrackers
98 // (excluding dynamically set mNetworkPreference)
99 // TODO - move mNetworkTypePreference into this
100 private int[] mPriorityList;
101
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800102 private Context mContext;
103 private int mNetworkPreference;
Robert Greenwalt42acef32009-08-12 16:08:25 -0700104 private int mActiveDefaultNetwork = -1;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800105
106 private int mNumDnsEntries;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800107
108 private boolean mTestMode;
Joe Onorato00092872010-09-01 21:18:22 -0700109 private static ConnectivityService sServiceInstance;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800110
Robert Greenwalt42acef32009-08-12 16:08:25 -0700111 private Handler mHandler;
112
113 // list of DeathRecipients used to make sure features are turned off when
114 // a process dies
115 private List mFeatureUsers;
116
Mike Lockwood0f79b542009-08-14 14:18:49 -0400117 private boolean mSystemReady;
Dianne Hackborn1c633fc2009-12-08 19:45:14 -0800118 private Intent mInitialBroadcast;
Mike Lockwood0f79b542009-08-14 14:18:49 -0400119
Robert Greenwalt14f2ef42010-06-15 12:19:37 -0700120 private PowerManager.WakeLock mNetTransitionWakeLock;
121 private String mNetTransitionWakeLockCausedBy = "";
122 private int mNetTransitionWakeLockSerialNumber;
123 private int mNetTransitionWakeLockTimeout;
124
Robert Greenwalte90aa5e2010-09-01 11:34:05 -0700125 private InetAddress mDefaultDns;
126
Robert Greenwalt511288a2009-12-07 11:33:18 -0800127 private static class NetworkAttributes {
Robert Greenwalt42acef32009-08-12 16:08:25 -0700128 /**
129 * Class for holding settings read from resources.
130 */
131 public String mName;
132 public int mType;
133 public int mRadio;
134 public int mPriority;
Robert Greenwalt511288a2009-12-07 11:33:18 -0800135 public NetworkInfo.State mLastState;
Robert Greenwalt42acef32009-08-12 16:08:25 -0700136 public NetworkAttributes(String init) {
137 String fragments[] = init.split(",");
138 mName = fragments[0].toLowerCase();
Robert Greenwalt5154ae762009-10-30 14:17:42 -0700139 mType = Integer.parseInt(fragments[1]);
140 mRadio = Integer.parseInt(fragments[2]);
141 mPriority = Integer.parseInt(fragments[3]);
Robert Greenwalt511288a2009-12-07 11:33:18 -0800142 mLastState = NetworkInfo.State.UNKNOWN;
Robert Greenwalt42acef32009-08-12 16:08:25 -0700143 }
144 public boolean isDefault() {
145 return (mType == mRadio);
146 }
147 }
148 NetworkAttributes[] mNetAttributes;
Robert Greenwalt5154ae762009-10-30 14:17:42 -0700149 int mNetworksDefined;
Robert Greenwalt42acef32009-08-12 16:08:25 -0700150
Robert Greenwalt511288a2009-12-07 11:33:18 -0800151 private static class RadioAttributes {
Robert Greenwalt42acef32009-08-12 16:08:25 -0700152 public int mSimultaneity;
153 public int mType;
154 public RadioAttributes(String init) {
155 String fragments[] = init.split(",");
Robert Greenwalt5154ae762009-10-30 14:17:42 -0700156 mType = Integer.parseInt(fragments[0]);
157 mSimultaneity = Integer.parseInt(fragments[1]);
Robert Greenwalt42acef32009-08-12 16:08:25 -0700158 }
159 }
160 RadioAttributes[] mRadioAttributes;
161
Wink Savillebb08caf2010-09-02 19:23:52 -0700162 public static synchronized ConnectivityService getInstance(Context context) {
163 if (sServiceInstance == null) {
164 sServiceInstance = new ConnectivityService(context);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800165 }
Wink Savillebb08caf2010-09-02 19:23:52 -0700166 return sServiceInstance;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800167 }
Robert Greenwalt86e9e552009-07-16 17:21:39 -0700168
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800169 private ConnectivityService(Context context) {
Joe Onorato8a9b2202010-02-26 18:56:32 -0800170 if (DBG) Slog.v(TAG, "ConnectivityService starting up");
Robert Greenwaltde8383c2010-01-14 17:47:58 -0800171
Wink Savillebb08caf2010-09-02 19:23:52 -0700172 HandlerThread handlerThread = new HandlerThread("ConnectivityServiceThread");
173 handlerThread.start();
174 mHandler = new MyHandler(handlerThread.getLooper());
175
Robert Greenwaltde8383c2010-01-14 17:47:58 -0800176 // setup our unique device name
177 String id = Settings.Secure.getString(context.getContentResolver(),
178 Settings.Secure.ANDROID_ID);
179 if (id != null && id.length() > 0) {
180 String name = new String("android_").concat(id);
181 SystemProperties.set("net.hostname", name);
182 }
183
Robert Greenwalte90aa5e2010-09-01 11:34:05 -0700184 // read our default dns server ip
185 String dns = Settings.Secure.getString(context.getContentResolver(),
186 Settings.Secure.DEFAULT_DNS_SERVER);
187 if (dns == null || dns.length() == 0) {
188 dns = context.getResources().getString(
189 com.android.internal.R.string.config_default_dns_server);
190 }
191 try {
192 mDefaultDns = InetAddress.getByName(dns);
193 } catch (UnknownHostException e) {
194 Slog.e(TAG, "Error setting defaultDns using " + dns);
195 }
196
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800197 mContext = context;
Robert Greenwalt14f2ef42010-06-15 12:19:37 -0700198
199 PowerManager powerManager = (PowerManager)mContext.getSystemService(Context.POWER_SERVICE);
200 mNetTransitionWakeLock = powerManager.newWakeLock(PowerManager.PARTIAL_WAKE_LOCK, TAG);
201 mNetTransitionWakeLockTimeout = mContext.getResources().getInteger(
202 com.android.internal.R.integer.config_networkTransitionTimeout);
203
Robert Greenwalt42acef32009-08-12 16:08:25 -0700204 mNetTrackers = new NetworkStateTracker[
205 ConnectivityManager.MAX_NETWORK_TYPE+1];
Robert Greenwalt86e9e552009-07-16 17:21:39 -0700206
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800207 mNetworkPreference = getPersistedNetworkPreference();
Robert Greenwalt86e9e552009-07-16 17:21:39 -0700208
Robert Greenwalt5154ae762009-10-30 14:17:42 -0700209 mRadioAttributes = new RadioAttributes[ConnectivityManager.MAX_RADIO_TYPE+1];
210 mNetAttributes = new NetworkAttributes[ConnectivityManager.MAX_NETWORK_TYPE+1];
211
Robert Greenwalt42acef32009-08-12 16:08:25 -0700212 // Load device network attributes from resources
Robert Greenwalt42acef32009-08-12 16:08:25 -0700213 String[] raStrings = context.getResources().getStringArray(
214 com.android.internal.R.array.radioAttributes);
Robert Greenwalt5154ae762009-10-30 14:17:42 -0700215 for (String raString : raStrings) {
216 RadioAttributes r = new RadioAttributes(raString);
217 if (r.mType > ConnectivityManager.MAX_RADIO_TYPE) {
Joe Onorato8a9b2202010-02-26 18:56:32 -0800218 Slog.e(TAG, "Error in radioAttributes - ignoring attempt to define type " + r.mType);
Robert Greenwalt5154ae762009-10-30 14:17:42 -0700219 continue;
220 }
221 if (mRadioAttributes[r.mType] != null) {
Joe Onorato8a9b2202010-02-26 18:56:32 -0800222 Slog.e(TAG, "Error in radioAttributes - ignoring attempt to redefine type " +
Robert Greenwalt5154ae762009-10-30 14:17:42 -0700223 r.mType);
224 continue;
225 }
Robert Greenwalt42acef32009-08-12 16:08:25 -0700226 mRadioAttributes[r.mType] = r;
227 }
228
Robert Greenwalt5154ae762009-10-30 14:17:42 -0700229 String[] naStrings = context.getResources().getStringArray(
230 com.android.internal.R.array.networkAttributes);
231 for (String naString : naStrings) {
232 try {
233 NetworkAttributes n = new NetworkAttributes(naString);
234 if (n.mType > ConnectivityManager.MAX_NETWORK_TYPE) {
Joe Onorato8a9b2202010-02-26 18:56:32 -0800235 Slog.e(TAG, "Error in networkAttributes - ignoring attempt to define type " +
Robert Greenwalt5154ae762009-10-30 14:17:42 -0700236 n.mType);
237 continue;
Robert Greenwalt42acef32009-08-12 16:08:25 -0700238 }
Robert Greenwalt5154ae762009-10-30 14:17:42 -0700239 if (mNetAttributes[n.mType] != null) {
Joe Onorato8a9b2202010-02-26 18:56:32 -0800240 Slog.e(TAG, "Error in networkAttributes - ignoring attempt to redefine type " +
Robert Greenwalt5154ae762009-10-30 14:17:42 -0700241 n.mType);
242 continue;
243 }
244 if (mRadioAttributes[n.mRadio] == null) {
Joe Onorato8a9b2202010-02-26 18:56:32 -0800245 Slog.e(TAG, "Error in networkAttributes - ignoring attempt to use undefined " +
Robert Greenwalt5154ae762009-10-30 14:17:42 -0700246 "radio " + n.mRadio + " in network type " + n.mType);
247 continue;
248 }
249 mNetAttributes[n.mType] = n;
250 mNetworksDefined++;
251 } catch(Exception e) {
252 // ignore it - leave the entry null
Robert Greenwalt42acef32009-08-12 16:08:25 -0700253 }
254 }
255
Robert Greenwalt5154ae762009-10-30 14:17:42 -0700256 // high priority first
257 mPriorityList = new int[mNetworksDefined];
258 {
259 int insertionPoint = mNetworksDefined-1;
260 int currentLowest = 0;
261 int nextLowest = 0;
262 while (insertionPoint > -1) {
263 for (NetworkAttributes na : mNetAttributes) {
264 if (na == null) continue;
265 if (na.mPriority < currentLowest) continue;
266 if (na.mPriority > currentLowest) {
267 if (na.mPriority < nextLowest || nextLowest == 0) {
268 nextLowest = na.mPriority;
269 }
270 continue;
271 }
272 mPriorityList[insertionPoint--] = na.mType;
273 }
274 currentLowest = nextLowest;
275 nextLowest = 0;
276 }
277 }
278
279 mNetRequestersPids = new ArrayList[ConnectivityManager.MAX_NETWORK_TYPE+1];
280 for (int i : mPriorityList) {
Robert Greenwalt42acef32009-08-12 16:08:25 -0700281 mNetRequestersPids[i] = new ArrayList();
282 }
283
284 mFeatureUsers = new ArrayList();
285
Robert Greenwalt5154ae762009-10-30 14:17:42 -0700286 mNumDnsEntries = 0;
287
288 mTestMode = SystemProperties.get("cm.test.mode").equals("true")
289 && SystemProperties.get("ro.build.type").equals("eng");
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800290 /*
291 * Create the network state trackers for Wi-Fi and mobile
292 * data. Maybe this could be done with a factory class,
293 * but it's not clear that it's worth it, given that
294 * the number of different network types is not going
295 * to change very often.
296 */
Robert Greenwaltc03fa502010-02-23 18:58:05 -0800297 boolean noMobileData = !getMobileDataEnabled();
Robert Greenwalt5154ae762009-10-30 14:17:42 -0700298 for (int netType : mPriorityList) {
299 switch (mNetAttributes[netType].mRadio) {
300 case ConnectivityManager.TYPE_WIFI:
Joe Onorato8a9b2202010-02-26 18:56:32 -0800301 if (DBG) Slog.v(TAG, "Starting Wifi Service.");
Wink Savillec7a98342010-08-13 16:11:42 -0700302 WifiStateTracker wst = new WifiStateTracker();
Irfan Sheriff0d255342010-07-28 09:35:20 -0700303 WifiService wifiService = new WifiService(context);
Robert Greenwalt5154ae762009-10-30 14:17:42 -0700304 ServiceManager.addService(Context.WIFI_SERVICE, wifiService);
Irfan Sheriff0d255342010-07-28 09:35:20 -0700305 wifiService.checkAndStartWifi();
Robert Greenwalt5154ae762009-10-30 14:17:42 -0700306 mNetTrackers[ConnectivityManager.TYPE_WIFI] = wst;
Wink Savillec7a98342010-08-13 16:11:42 -0700307 wst.startMonitoring(context, mHandler);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800308
Irfan Sheriffa2a1b912010-06-07 09:03:04 -0700309 //TODO: as part of WWS refactor, create only when needed
Irfan Sheriff0d255342010-07-28 09:35:20 -0700310 mWifiWatchdogService = new WifiWatchdogService(context);
Irfan Sheriffa2a1b912010-06-07 09:03:04 -0700311
Robert Greenwalt5154ae762009-10-30 14:17:42 -0700312 break;
313 case ConnectivityManager.TYPE_MOBILE:
Wink Savillec7a98342010-08-13 16:11:42 -0700314 mNetTrackers[netType] = new MobileDataStateTracker(netType,
315 mNetAttributes[netType].mName);
316 mNetTrackers[netType].startMonitoring(context, mHandler);
Robert Greenwaltc03fa502010-02-23 18:58:05 -0800317 if (noMobileData) {
Joe Onorato8a9b2202010-02-26 18:56:32 -0800318 if (DBG) Slog.d(TAG, "tearing down Mobile networks due to setting");
Robert Greenwaltc03fa502010-02-23 18:58:05 -0800319 mNetTrackers[netType].teardown();
320 }
Robert Greenwalt5154ae762009-10-30 14:17:42 -0700321 break;
322 default:
Joe Onorato8a9b2202010-02-26 18:56:32 -0800323 Slog.e(TAG, "Trying to create a DataStateTracker for an unknown radio type " +
Robert Greenwalt5154ae762009-10-30 14:17:42 -0700324 mNetAttributes[netType].mRadio);
325 continue;
326 }
327 }
Robert Greenwaltd0e18ff2010-01-26 11:40:34 -0800328
Robert Greenwaltdfadaea2010-03-11 15:03:08 -0800329 mTethering = new Tethering(mContext, mHandler.getLooper());
Robert Greenwaltc9d5fb72010-02-25 12:29:30 -0800330 mTetheringConfigValid = (((mNetTrackers[ConnectivityManager.TYPE_MOBILE_DUN] != null) ||
331 !mTethering.isDunRequired()) &&
332 (mTethering.getTetherableUsbRegexs().length != 0 ||
Danica Chang6fdd0c62010-08-11 14:54:43 -0700333 mTethering.getTetherableWifiRegexs().length != 0 ||
334 mTethering.getTetherableBluetoothRegexs().length != 0) &&
Robert Greenwaltc9d5fb72010-02-25 12:29:30 -0800335 mTethering.getUpstreamIfaceRegexs().length != 0);
336
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800337 }
338
Robert Greenwalt5154ae762009-10-30 14:17:42 -0700339
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800340 /**
Robert Greenwalt86e9e552009-07-16 17:21:39 -0700341 * Sets the preferred network.
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800342 * @param preference the new preference
343 */
344 public synchronized void setNetworkPreference(int preference) {
345 enforceChangePermission();
Robert Greenwalt42acef32009-08-12 16:08:25 -0700346 if (ConnectivityManager.isNetworkTypeValid(preference) &&
Robert Greenwalt5154ae762009-10-30 14:17:42 -0700347 mNetAttributes[preference] != null &&
Robert Greenwalt42acef32009-08-12 16:08:25 -0700348 mNetAttributes[preference].isDefault()) {
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800349 if (mNetworkPreference != preference) {
350 persistNetworkPreference(preference);
351 mNetworkPreference = preference;
352 enforcePreference();
353 }
354 }
355 }
356
357 public int getNetworkPreference() {
358 enforceAccessPermission();
359 return mNetworkPreference;
360 }
361
362 private void persistNetworkPreference(int networkPreference) {
363 final ContentResolver cr = mContext.getContentResolver();
Robert Greenwalt86e9e552009-07-16 17:21:39 -0700364 Settings.Secure.putInt(cr, Settings.Secure.NETWORK_PREFERENCE,
365 networkPreference);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800366 }
Robert Greenwalt86e9e552009-07-16 17:21:39 -0700367
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800368 private int getPersistedNetworkPreference() {
369 final ContentResolver cr = mContext.getContentResolver();
370
371 final int networkPrefSetting = Settings.Secure
372 .getInt(cr, Settings.Secure.NETWORK_PREFERENCE, -1);
373 if (networkPrefSetting != -1) {
374 return networkPrefSetting;
375 }
376
377 return ConnectivityManager.DEFAULT_NETWORK_PREFERENCE;
378 }
Robert Greenwalt86e9e552009-07-16 17:21:39 -0700379
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800380 /**
Robert Greenwalt86e9e552009-07-16 17:21:39 -0700381 * Make the state of network connectivity conform to the preference settings
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800382 * In this method, we only tear down a non-preferred network. Establishing
383 * a connection to the preferred network is taken care of when we handle
384 * the disconnect event from the non-preferred network
385 * (see {@link #handleDisconnect(NetworkInfo)}).
386 */
387 private void enforcePreference() {
Robert Greenwalt42acef32009-08-12 16:08:25 -0700388 if (mNetTrackers[mNetworkPreference].getNetworkInfo().isConnected())
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800389 return;
390
Robert Greenwalt42acef32009-08-12 16:08:25 -0700391 if (!mNetTrackers[mNetworkPreference].isAvailable())
392 return;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800393
Robert Greenwalt42acef32009-08-12 16:08:25 -0700394 for (int t=0; t <= ConnectivityManager.MAX_RADIO_TYPE; t++) {
Robert Greenwalt5154ae762009-10-30 14:17:42 -0700395 if (t != mNetworkPreference && mNetTrackers[t] != null &&
Robert Greenwalt42acef32009-08-12 16:08:25 -0700396 mNetTrackers[t].getNetworkInfo().isConnected()) {
Robert Greenwaltec9fe462009-08-20 15:25:14 -0700397 if (DBG) {
Joe Onorato8a9b2202010-02-26 18:56:32 -0800398 Slog.d(TAG, "tearing down " +
Robert Greenwaltec9fe462009-08-20 15:25:14 -0700399 mNetTrackers[t].getNetworkInfo() +
400 " in enforcePreference");
401 }
Robert Greenwalt42acef32009-08-12 16:08:25 -0700402 teardown(mNetTrackers[t]);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800403 }
404 }
405 }
406
407 private boolean teardown(NetworkStateTracker netTracker) {
408 if (netTracker.teardown()) {
409 netTracker.setTeardownRequested(true);
410 return true;
411 } else {
412 return false;
413 }
414 }
415
416 /**
417 * Return NetworkInfo for the active (i.e., connected) network interface.
418 * It is assumed that at most one network is active at a time. If more
419 * than one is active, it is indeterminate which will be returned.
Robert Greenwalt86e9e552009-07-16 17:21:39 -0700420 * @return the info for the active network, or {@code null} if none is
421 * active
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800422 */
423 public NetworkInfo getActiveNetworkInfo() {
424 enforceAccessPermission();
Robert Greenwalt42acef32009-08-12 16:08:25 -0700425 for (int type=0; type <= ConnectivityManager.MAX_NETWORK_TYPE; type++) {
Robert Greenwalt5154ae762009-10-30 14:17:42 -0700426 if (mNetAttributes[type] == null || !mNetAttributes[type].isDefault()) {
Robert Greenwalt42acef32009-08-12 16:08:25 -0700427 continue;
428 }
429 NetworkStateTracker t = mNetTrackers[type];
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800430 NetworkInfo info = t.getNetworkInfo();
431 if (info.isConnected()) {
Joe Onorato8a9b2202010-02-26 18:56:32 -0800432 if (DBG && type != mActiveDefaultNetwork) Slog.e(TAG,
Robert Greenwalt42acef32009-08-12 16:08:25 -0700433 "connected default network is not " +
434 "mActiveDefaultNetwork!");
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800435 return info;
436 }
437 }
438 return null;
439 }
440
441 public NetworkInfo getNetworkInfo(int networkType) {
442 enforceAccessPermission();
443 if (ConnectivityManager.isNetworkTypeValid(networkType)) {
444 NetworkStateTracker t = mNetTrackers[networkType];
445 if (t != null)
446 return t.getNetworkInfo();
447 }
448 return null;
449 }
450
451 public NetworkInfo[] getAllNetworkInfo() {
452 enforceAccessPermission();
Robert Greenwalt5154ae762009-10-30 14:17:42 -0700453 NetworkInfo[] result = new NetworkInfo[mNetworksDefined];
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800454 int i = 0;
455 for (NetworkStateTracker t : mNetTrackers) {
Robert Greenwalt5154ae762009-10-30 14:17:42 -0700456 if(t != null) result[i++] = t.getNetworkInfo();
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800457 }
458 return result;
459 }
460
461 public boolean setRadios(boolean turnOn) {
462 boolean result = true;
463 enforceChangePermission();
464 for (NetworkStateTracker t : mNetTrackers) {
Robert Greenwalt5154ae762009-10-30 14:17:42 -0700465 if (t != null) result = t.setRadio(turnOn) && result;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800466 }
467 return result;
468 }
469
470 public boolean setRadio(int netType, boolean turnOn) {
471 enforceChangePermission();
472 if (!ConnectivityManager.isNetworkTypeValid(netType)) {
473 return false;
474 }
475 NetworkStateTracker tracker = mNetTrackers[netType];
476 return tracker != null && tracker.setRadio(turnOn);
477 }
478
Robert Greenwalt9c75d4a2009-09-27 17:27:04 -0700479 /**
480 * Used to notice when the calling process dies so we can self-expire
481 *
482 * Also used to know if the process has cleaned up after itself when
483 * our auto-expire timer goes off. The timer has a link to an object.
484 *
485 */
Robert Greenwalt42acef32009-08-12 16:08:25 -0700486 private class FeatureUser implements IBinder.DeathRecipient {
487 int mNetworkType;
488 String mFeature;
489 IBinder mBinder;
490 int mPid;
491 int mUid;
Robert Greenwaltb9285352009-12-21 18:24:07 -0800492 long mCreateTime;
Robert Greenwalt42acef32009-08-12 16:08:25 -0700493
494 FeatureUser(int type, String feature, IBinder binder) {
495 super();
496 mNetworkType = type;
497 mFeature = feature;
498 mBinder = binder;
499 mPid = getCallingPid();
500 mUid = getCallingUid();
Robert Greenwaltb9285352009-12-21 18:24:07 -0800501 mCreateTime = System.currentTimeMillis();
Robert Greenwalt9c75d4a2009-09-27 17:27:04 -0700502
Robert Greenwalt42acef32009-08-12 16:08:25 -0700503 try {
504 mBinder.linkToDeath(this, 0);
505 } catch (RemoteException e) {
506 binderDied();
507 }
508 }
509
510 void unlinkDeathRecipient() {
511 mBinder.unlinkToDeath(this, 0);
512 }
513
514 public void binderDied() {
Joe Onorato8a9b2202010-02-26 18:56:32 -0800515 Slog.d(TAG, "ConnectivityService FeatureUser binderDied(" +
Robert Greenwaltb9285352009-12-21 18:24:07 -0800516 mNetworkType + ", " + mFeature + ", " + mBinder + "), created " +
517 (System.currentTimeMillis() - mCreateTime) + " mSec ago");
Robert Greenwalt9c75d4a2009-09-27 17:27:04 -0700518 stopUsingNetworkFeature(this, false);
Robert Greenwalt42acef32009-08-12 16:08:25 -0700519 }
520
Robert Greenwalt9c75d4a2009-09-27 17:27:04 -0700521 public void expire() {
Joe Onorato8a9b2202010-02-26 18:56:32 -0800522 Slog.d(TAG, "ConnectivityService FeatureUser expire(" +
Robert Greenwaltb9285352009-12-21 18:24:07 -0800523 mNetworkType + ", " + mFeature + ", " + mBinder +"), created " +
524 (System.currentTimeMillis() - mCreateTime) + " mSec ago");
Robert Greenwalt9c75d4a2009-09-27 17:27:04 -0700525 stopUsingNetworkFeature(this, false);
526 }
Robert Greenwaltb9285352009-12-21 18:24:07 -0800527
528 public String toString() {
529 return "FeatureUser("+mNetworkType+","+mFeature+","+mPid+","+mUid+"), created " +
530 (System.currentTimeMillis() - mCreateTime) + " mSec ago";
531 }
Robert Greenwalt42acef32009-08-12 16:08:25 -0700532 }
533
Robert Greenwalt9c75d4a2009-09-27 17:27:04 -0700534 // javadoc from interface
Robert Greenwalt42acef32009-08-12 16:08:25 -0700535 public int startUsingNetworkFeature(int networkType, String feature,
536 IBinder binder) {
537 if (DBG) {
Joe Onorato8a9b2202010-02-26 18:56:32 -0800538 Slog.d(TAG, "startUsingNetworkFeature for net " + networkType +
Robert Greenwalt42acef32009-08-12 16:08:25 -0700539 ": " + feature);
540 }
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800541 enforceChangePermission();
Robert Greenwalt5154ae762009-10-30 14:17:42 -0700542 if (!ConnectivityManager.isNetworkTypeValid(networkType) ||
543 mNetAttributes[networkType] == null) {
Robert Greenwalt42acef32009-08-12 16:08:25 -0700544 return Phone.APN_REQUEST_FAILED;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800545 }
Robert Greenwalt42acef32009-08-12 16:08:25 -0700546
Robert Greenwalt9c75d4a2009-09-27 17:27:04 -0700547 FeatureUser f = new FeatureUser(networkType, feature, binder);
Robert Greenwalt42acef32009-08-12 16:08:25 -0700548
549 // TODO - move this into the MobileDataStateTracker
550 int usedNetworkType = networkType;
551 if(networkType == ConnectivityManager.TYPE_MOBILE) {
Robert Greenwaltc03fa502010-02-23 18:58:05 -0800552 if (!getMobileDataEnabled()) {
Joe Onorato8a9b2202010-02-26 18:56:32 -0800553 if (DBG) Slog.d(TAG, "requested special network with data disabled - rejected");
Robert Greenwaltc03fa502010-02-23 18:58:05 -0800554 return Phone.APN_TYPE_NOT_AVAILABLE;
555 }
Robert Greenwalt42acef32009-08-12 16:08:25 -0700556 if (TextUtils.equals(feature, Phone.FEATURE_ENABLE_MMS)) {
557 usedNetworkType = ConnectivityManager.TYPE_MOBILE_MMS;
558 } else if (TextUtils.equals(feature, Phone.FEATURE_ENABLE_SUPL)) {
559 usedNetworkType = ConnectivityManager.TYPE_MOBILE_SUPL;
560 } else if (TextUtils.equals(feature, Phone.FEATURE_ENABLE_DUN)) {
561 usedNetworkType = ConnectivityManager.TYPE_MOBILE_DUN;
562 } else if (TextUtils.equals(feature, Phone.FEATURE_ENABLE_HIPRI)) {
563 usedNetworkType = ConnectivityManager.TYPE_MOBILE_HIPRI;
564 }
565 }
566 NetworkStateTracker network = mNetTrackers[usedNetworkType];
567 if (network != null) {
568 if (usedNetworkType != networkType) {
569 Integer currentPid = new Integer(getCallingPid());
570
571 NetworkStateTracker radio = mNetTrackers[networkType];
572 NetworkInfo ni = network.getNetworkInfo();
573
574 if (ni.isAvailable() == false) {
Joe Onorato8a9b2202010-02-26 18:56:32 -0800575 if (DBG) Slog.d(TAG, "special network not available");
Robert Greenwalt42acef32009-08-12 16:08:25 -0700576 return Phone.APN_TYPE_NOT_AVAILABLE;
577 }
578
Robert Greenwalt9c75d4a2009-09-27 17:27:04 -0700579 synchronized(this) {
580 mFeatureUsers.add(f);
581 if (!mNetRequestersPids[usedNetworkType].contains(currentPid)) {
582 // this gets used for per-pid dns when connected
583 mNetRequestersPids[usedNetworkType].add(currentPid);
584 }
Robert Greenwalt42acef32009-08-12 16:08:25 -0700585 }
Robert Greenwalt9c75d4a2009-09-27 17:27:04 -0700586 mHandler.sendMessageDelayed(mHandler.obtainMessage(
587 NetworkStateTracker.EVENT_RESTORE_DEFAULT_NETWORK,
588 f), getRestoreDefaultNetworkDelay());
589
Robert Greenwalt42acef32009-08-12 16:08:25 -0700590
Robert Greenwalta64bf832009-08-19 20:19:33 -0700591 if ((ni.isConnectedOrConnecting() == true) &&
592 !network.isTeardownRequested()) {
Robert Greenwalt42acef32009-08-12 16:08:25 -0700593 if (ni.isConnected() == true) {
594 // add the pid-specific dns
Robert Greenwaltc76b8fa2010-07-23 15:46:26 -0700595 handleDnsConfigurationChange(networkType);
Joe Onorato8a9b2202010-02-26 18:56:32 -0800596 if (DBG) Slog.d(TAG, "special network already active");
Robert Greenwalt42acef32009-08-12 16:08:25 -0700597 return Phone.APN_ALREADY_ACTIVE;
598 }
Joe Onorato8a9b2202010-02-26 18:56:32 -0800599 if (DBG) Slog.d(TAG, "special network already connecting");
Robert Greenwalt42acef32009-08-12 16:08:25 -0700600 return Phone.APN_REQUEST_STARTED;
601 }
602
603 // check if the radio in play can make another contact
604 // assume if cannot for now
605
Joe Onorato8a9b2202010-02-26 18:56:32 -0800606 if (DBG) Slog.d(TAG, "reconnecting to special network");
Robert Greenwalt42acef32009-08-12 16:08:25 -0700607 network.reconnect();
608 return Phone.APN_REQUEST_STARTED;
609 } else {
Robert Greenwalt02648a42010-05-18 10:52:51 -0700610 return -1;
Robert Greenwalt42acef32009-08-12 16:08:25 -0700611 }
612 }
613 return Phone.APN_TYPE_NOT_AVAILABLE;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800614 }
615
Robert Greenwalt9c75d4a2009-09-27 17:27:04 -0700616 // javadoc from interface
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800617 public int stopUsingNetworkFeature(int networkType, String feature) {
Robert Greenwaltb8f16342009-10-06 17:52:40 -0700618 enforceChangePermission();
619
Robert Greenwalt9c75d4a2009-09-27 17:27:04 -0700620 int pid = getCallingPid();
621 int uid = getCallingUid();
622
623 FeatureUser u = null;
624 boolean found = false;
625
626 synchronized(this) {
627 for (int i = 0; i < mFeatureUsers.size() ; i++) {
628 u = (FeatureUser)mFeatureUsers.get(i);
629 if (uid == u.mUid && pid == u.mPid &&
630 networkType == u.mNetworkType &&
631 TextUtils.equals(feature, u.mFeature)) {
632 found = true;
633 break;
634 }
635 }
636 }
637 if (found && u != null) {
638 // stop regardless of how many other time this proc had called start
639 return stopUsingNetworkFeature(u, true);
640 } else {
641 // none found!
Joe Onorato8a9b2202010-02-26 18:56:32 -0800642 if (DBG) Slog.d(TAG, "ignoring stopUsingNetworkFeature - not a live request");
Robert Greenwalt9c75d4a2009-09-27 17:27:04 -0700643 return 1;
644 }
Robert Greenwalt42acef32009-08-12 16:08:25 -0700645 }
646
Robert Greenwalt9c75d4a2009-09-27 17:27:04 -0700647 private int stopUsingNetworkFeature(FeatureUser u, boolean ignoreDups) {
648 int networkType = u.mNetworkType;
649 String feature = u.mFeature;
650 int pid = u.mPid;
651 int uid = u.mUid;
652
653 NetworkStateTracker tracker = null;
654 boolean callTeardown = false; // used to carry our decision outside of sync block
655
Robert Greenwalt42acef32009-08-12 16:08:25 -0700656 if (DBG) {
Joe Onorato8a9b2202010-02-26 18:56:32 -0800657 Slog.d(TAG, "stopUsingNetworkFeature for net " + networkType +
Robert Greenwalt42acef32009-08-12 16:08:25 -0700658 ": " + feature);
659 }
Robert Greenwaltb8f16342009-10-06 17:52:40 -0700660
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800661 if (!ConnectivityManager.isNetworkTypeValid(networkType)) {
662 return -1;
663 }
Robert Greenwalt42acef32009-08-12 16:08:25 -0700664
Robert Greenwalt9c75d4a2009-09-27 17:27:04 -0700665 // need to link the mFeatureUsers list with the mNetRequestersPids state in this
666 // sync block
667 synchronized(this) {
668 // check if this process still has an outstanding start request
669 if (!mFeatureUsers.contains(u)) {
Robert Greenwalt78a640a2010-03-10 16:10:43 -0800670 if (DBG) Slog.d(TAG, "ignoring - this process has no outstanding requests");
Robert Greenwalt42acef32009-08-12 16:08:25 -0700671 return 1;
672 }
Robert Greenwalt9c75d4a2009-09-27 17:27:04 -0700673 u.unlinkDeathRecipient();
674 mFeatureUsers.remove(mFeatureUsers.indexOf(u));
675 // If we care about duplicate requests, check for that here.
676 //
677 // This is done to support the extension of a request - the app
678 // can request we start the network feature again and renew the
679 // auto-shutoff delay. Normal "stop" calls from the app though
680 // do not pay attention to duplicate requests - in effect the
681 // API does not refcount and a single stop will counter multiple starts.
682 if (ignoreDups == false) {
683 for (int i = 0; i < mFeatureUsers.size() ; i++) {
684 FeatureUser x = (FeatureUser)mFeatureUsers.get(i);
685 if (x.mUid == u.mUid && x.mPid == u.mPid &&
686 x.mNetworkType == u.mNetworkType &&
687 TextUtils.equals(x.mFeature, u.mFeature)) {
Joe Onorato8a9b2202010-02-26 18:56:32 -0800688 if (DBG) Slog.d(TAG, "ignoring stopUsingNetworkFeature as dup is found");
Robert Greenwalt9c75d4a2009-09-27 17:27:04 -0700689 return 1;
690 }
691 }
Robert Greenwalt42acef32009-08-12 16:08:25 -0700692 }
Robert Greenwalt9c75d4a2009-09-27 17:27:04 -0700693
694 // TODO - move to MobileDataStateTracker
695 int usedNetworkType = networkType;
696 if (networkType == ConnectivityManager.TYPE_MOBILE) {
697 if (TextUtils.equals(feature, Phone.FEATURE_ENABLE_MMS)) {
698 usedNetworkType = ConnectivityManager.TYPE_MOBILE_MMS;
699 } else if (TextUtils.equals(feature, Phone.FEATURE_ENABLE_SUPL)) {
700 usedNetworkType = ConnectivityManager.TYPE_MOBILE_SUPL;
701 } else if (TextUtils.equals(feature, Phone.FEATURE_ENABLE_DUN)) {
702 usedNetworkType = ConnectivityManager.TYPE_MOBILE_DUN;
703 } else if (TextUtils.equals(feature, Phone.FEATURE_ENABLE_HIPRI)) {
704 usedNetworkType = ConnectivityManager.TYPE_MOBILE_HIPRI;
705 }
706 }
707 tracker = mNetTrackers[usedNetworkType];
Robert Greenwalt5154ae762009-10-30 14:17:42 -0700708 if (tracker == null) {
Robert Greenwalt78a640a2010-03-10 16:10:43 -0800709 if (DBG) Slog.d(TAG, "ignoring - no known tracker for net type " + usedNetworkType);
Robert Greenwalt5154ae762009-10-30 14:17:42 -0700710 return -1;
711 }
712 if (usedNetworkType != networkType) {
Robert Greenwalt9c75d4a2009-09-27 17:27:04 -0700713 Integer currentPid = new Integer(pid);
Robert Greenwalt9c75d4a2009-09-27 17:27:04 -0700714 mNetRequestersPids[usedNetworkType].remove(currentPid);
Robert Greenwalt421c72b2009-12-17 14:54:59 -0800715 reassessPidDns(pid, true);
Robert Greenwalt9c75d4a2009-09-27 17:27:04 -0700716 if (mNetRequestersPids[usedNetworkType].size() != 0) {
Joe Onorato8a9b2202010-02-26 18:56:32 -0800717 if (DBG) Slog.d(TAG, "not tearing down special network - " +
Robert Greenwalt9c75d4a2009-09-27 17:27:04 -0700718 "others still using it");
719 return 1;
720 }
721 callTeardown = true;
722 }
723 }
Robert Greenwalt78a640a2010-03-10 16:10:43 -0800724 if (DBG) Slog.d(TAG, "Doing network teardown");
Robert Greenwalt9c75d4a2009-09-27 17:27:04 -0700725 if (callTeardown) {
726 tracker.teardown();
Robert Greenwalt42acef32009-08-12 16:08:25 -0700727 return 1;
728 } else {
Robert Greenwalt02648a42010-05-18 10:52:51 -0700729 return -1;
Robert Greenwalt42acef32009-08-12 16:08:25 -0700730 }
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800731 }
732
733 /**
Robert Greenwalt585ac0f2010-08-27 09:24:29 -0700734 * @deprecated use requestRouteToHostAddress instead
735 *
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800736 * Ensure that a network route exists to deliver traffic to the specified
737 * host via the specified network interface.
Robert Greenwalt86e9e552009-07-16 17:21:39 -0700738 * @param networkType the type of the network over which traffic to the
739 * specified host is to be routed
740 * @param hostAddress the IP address of the host to which the route is
741 * desired
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800742 * @return {@code true} on success, {@code false} on failure
743 */
744 public boolean requestRouteToHost(int networkType, int hostAddress) {
Robert Greenwalt585ac0f2010-08-27 09:24:29 -0700745 InetAddress inetAddress = NetworkUtils.intToInetAddress(hostAddress);
746
747 if (inetAddress == null) {
748 return false;
749 }
750
751 return requestRouteToHostAddress(networkType, inetAddress.getAddress());
752 }
753
754 /**
755 * Ensure that a network route exists to deliver traffic to the specified
756 * host via the specified network interface.
757 * @param networkType the type of the network over which traffic to the
758 * specified host is to be routed
759 * @param hostAddress the IP address of the host to which the route is
760 * desired
761 * @return {@code true} on success, {@code false} on failure
762 */
763 public boolean requestRouteToHostAddress(int networkType, byte[] hostAddress) {
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800764 enforceChangePermission();
765 if (!ConnectivityManager.isNetworkTypeValid(networkType)) {
766 return false;
767 }
768 NetworkStateTracker tracker = mNetTrackers[networkType];
Robert Greenwalt8206ff32009-09-10 15:06:20 -0700769
Robert Greenwalt5154ae762009-10-30 14:17:42 -0700770 if (tracker == null || !tracker.getNetworkInfo().isConnected() ||
771 tracker.isTeardownRequested()) {
Robert Greenwalt8206ff32009-09-10 15:06:20 -0700772 if (DBG) {
Robert Greenwalt585ac0f2010-08-27 09:24:29 -0700773 Slog.d(TAG, "requestRouteToHostAddress on down network " +
774 "(" + networkType + ") - dropped");
Robert Greenwalt8206ff32009-09-10 15:06:20 -0700775 }
776 return false;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800777 }
Robert Greenwalt47f69fe2010-06-15 15:43:39 -0700778 try {
Robert Greenwalt585ac0f2010-08-27 09:24:29 -0700779 InetAddress addr = InetAddress.getByAddress(hostAddress);
Robert Greenwalt47f69fe2010-06-15 15:43:39 -0700780 return addHostRoute(tracker, addr);
781 } catch (UnknownHostException e) {}
782 return false;
Irfan Sheriffd649c122010-06-09 15:39:36 -0700783 }
784
785 /**
786 * Ensure that a network route exists to deliver traffic to the specified
787 * host via the mobile data network.
788 * @param hostAddress the IP address of the host to which the route is desired,
789 * in network byte order.
Robert Greenwalt47f69fe2010-06-15 15:43:39 -0700790 * TODO - deprecate
Irfan Sheriffd649c122010-06-09 15:39:36 -0700791 * @return {@code true} on success, {@code false} on failure
792 */
Robert Greenwalt47f69fe2010-06-15 15:43:39 -0700793 private boolean addHostRoute(NetworkStateTracker nt, InetAddress hostAddress) {
Irfan Sheriffd649c122010-06-09 15:39:36 -0700794 if (nt.getNetworkInfo().getType() == ConnectivityManager.TYPE_WIFI) {
795 return false;
796 }
797
Robert Greenwalt37e65eb2010-08-30 10:56:47 -0700798 LinkProperties p = nt.getLinkProperties();
Robert Greenwalt47f69fe2010-06-15 15:43:39 -0700799 if (p == null) return false;
800 String interfaceName = p.getInterfaceName();
Irfan Sheriffd649c122010-06-09 15:39:36 -0700801
802 if (DBG) {
Robert Greenwalt47f69fe2010-06-15 15:43:39 -0700803 Slog.d(TAG, "Requested host route to " + hostAddress + "(" + interfaceName + ")");
Irfan Sheriffd649c122010-06-09 15:39:36 -0700804 }
Robert Greenwalt47f69fe2010-06-15 15:43:39 -0700805 if (interfaceName != null) {
Robert Greenwalt585ac0f2010-08-27 09:24:29 -0700806 return NetworkUtils.addHostRoute(interfaceName, hostAddress, null);
Irfan Sheriffd649c122010-06-09 15:39:36 -0700807 } else {
Robert Greenwalt47f69fe2010-06-15 15:43:39 -0700808 if (DBG) Slog.e(TAG, "addHostRoute failed due to null interface name");
Irfan Sheriffd649c122010-06-09 15:39:36 -0700809 return false;
810 }
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800811 }
812
813 /**
814 * @see ConnectivityManager#getBackgroundDataSetting()
815 */
816 public boolean getBackgroundDataSetting() {
817 return Settings.Secure.getInt(mContext.getContentResolver(),
818 Settings.Secure.BACKGROUND_DATA, 1) == 1;
819 }
Robert Greenwalt86e9e552009-07-16 17:21:39 -0700820
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800821 /**
822 * @see ConnectivityManager#setBackgroundDataSetting(boolean)
823 */
824 public void setBackgroundDataSetting(boolean allowBackgroundDataUsage) {
825 mContext.enforceCallingOrSelfPermission(
826 android.Manifest.permission.CHANGE_BACKGROUND_DATA_SETTING,
827 "ConnectivityService");
Robert Greenwalt86e9e552009-07-16 17:21:39 -0700828
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800829 if (getBackgroundDataSetting() == allowBackgroundDataUsage) return;
830
831 Settings.Secure.putInt(mContext.getContentResolver(),
Robert Greenwalt86e9e552009-07-16 17:21:39 -0700832 Settings.Secure.BACKGROUND_DATA,
833 allowBackgroundDataUsage ? 1 : 0);
834
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800835 Intent broadcast = new Intent(
836 ConnectivityManager.ACTION_BACKGROUND_DATA_SETTING_CHANGED);
837 mContext.sendBroadcast(broadcast);
Robert Greenwalt86e9e552009-07-16 17:21:39 -0700838 }
839
Robert Greenwaltc03fa502010-02-23 18:58:05 -0800840 /**
841 * @see ConnectivityManager#getMobileDataEnabled()
842 */
843 public boolean getMobileDataEnabled() {
844 enforceAccessPermission();
845 boolean retVal = Settings.Secure.getInt(mContext.getContentResolver(),
846 Settings.Secure.MOBILE_DATA, 1) == 1;
Joe Onorato8a9b2202010-02-26 18:56:32 -0800847 if (DBG) Slog.d(TAG, "getMobileDataEnabled returning " + retVal);
Robert Greenwaltc03fa502010-02-23 18:58:05 -0800848 return retVal;
849 }
850
851 /**
852 * @see ConnectivityManager#setMobileDataEnabled(boolean)
853 */
854 public synchronized void setMobileDataEnabled(boolean enabled) {
855 enforceChangePermission();
Joe Onorato8a9b2202010-02-26 18:56:32 -0800856 if (DBG) Slog.d(TAG, "setMobileDataEnabled(" + enabled + ")");
Robert Greenwaltc03fa502010-02-23 18:58:05 -0800857
858 if (getMobileDataEnabled() == enabled) return;
859
860 Settings.Secure.putInt(mContext.getContentResolver(),
861 Settings.Secure.MOBILE_DATA, enabled ? 1 : 0);
862
863 if (enabled) {
864 if (mNetTrackers[ConnectivityManager.TYPE_MOBILE] != null) {
Joe Onorato8a9b2202010-02-26 18:56:32 -0800865 if (DBG) Slog.d(TAG, "starting up " + mNetTrackers[ConnectivityManager.TYPE_MOBILE]);
Robert Greenwaltc03fa502010-02-23 18:58:05 -0800866 mNetTrackers[ConnectivityManager.TYPE_MOBILE].reconnect();
867 }
868 } else {
869 for (NetworkStateTracker nt : mNetTrackers) {
870 if (nt == null) continue;
871 int netType = nt.getNetworkInfo().getType();
872 if (mNetAttributes[netType].mRadio == ConnectivityManager.TYPE_MOBILE) {
Joe Onorato8a9b2202010-02-26 18:56:32 -0800873 if (DBG) Slog.d(TAG, "tearing down " + nt);
Robert Greenwaltc03fa502010-02-23 18:58:05 -0800874 nt.teardown();
875 }
876 }
877 }
878 }
879
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800880 private int getNumConnectedNetworks() {
881 int numConnectedNets = 0;
882
883 for (NetworkStateTracker nt : mNetTrackers) {
Robert Greenwalt5154ae762009-10-30 14:17:42 -0700884 if (nt != null && nt.getNetworkInfo().isConnected() &&
Robert Greenwalt86e9e552009-07-16 17:21:39 -0700885 !nt.isTeardownRequested()) {
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800886 ++numConnectedNets;
887 }
888 }
889 return numConnectedNets;
890 }
891
892 private void enforceAccessPermission() {
Robert Greenwalt86e9e552009-07-16 17:21:39 -0700893 mContext.enforceCallingOrSelfPermission(
894 android.Manifest.permission.ACCESS_NETWORK_STATE,
895 "ConnectivityService");
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800896 }
897
898 private void enforceChangePermission() {
Robert Greenwalt86e9e552009-07-16 17:21:39 -0700899 mContext.enforceCallingOrSelfPermission(
900 android.Manifest.permission.CHANGE_NETWORK_STATE,
901 "ConnectivityService");
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800902 }
903
Robert Greenwaltd0e18ff2010-01-26 11:40:34 -0800904 // TODO Make this a special check when it goes public
905 private void enforceTetherChangePermission() {
906 mContext.enforceCallingOrSelfPermission(
907 android.Manifest.permission.CHANGE_NETWORK_STATE,
908 "ConnectivityService");
909 }
910
Robert Greenwalt2a091d72010-02-11 18:18:40 -0800911 private void enforceTetherAccessPermission() {
912 mContext.enforceCallingOrSelfPermission(
913 android.Manifest.permission.ACCESS_NETWORK_STATE,
914 "ConnectivityService");
915 }
916
Robert Greenwalt14f2ef42010-06-15 12:19:37 -0700917 private void enforceConnectivityInternalPermission() {
918 mContext.enforceCallingOrSelfPermission(
919 android.Manifest.permission.CONNECTIVITY_INTERNAL,
920 "ConnectivityService");
921 }
922
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800923 /**
Robert Greenwalt86e9e552009-07-16 17:21:39 -0700924 * Handle a {@code DISCONNECTED} event. If this pertains to the non-active
925 * network, we ignore it. If it is for the active network, we send out a
926 * broadcast. But first, we check whether it might be possible to connect
927 * to a different network.
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800928 * @param info the {@code NetworkInfo} for the network
929 */
930 private void handleDisconnect(NetworkInfo info) {
931
Robert Greenwalt42acef32009-08-12 16:08:25 -0700932 int prevNetType = info.getType();
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800933
Robert Greenwalt42acef32009-08-12 16:08:25 -0700934 mNetTrackers[prevNetType].setTeardownRequested(false);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800935 /*
936 * If the disconnected network is not the active one, then don't report
937 * this as a loss of connectivity. What probably happened is that we're
938 * getting the disconnect for a network that we explicitly disabled
939 * in accordance with network preference policies.
940 */
Robert Greenwalt42acef32009-08-12 16:08:25 -0700941 if (!mNetAttributes[prevNetType].isDefault()) {
942 List pids = mNetRequestersPids[prevNetType];
943 for (int i = 0; i<pids.size(); i++) {
944 Integer pid = (Integer)pids.get(i);
945 // will remove them because the net's no longer connected
946 // need to do this now as only now do we know the pids and
947 // can properly null things that are no longer referenced.
948 reassessPidDns(pid.intValue(), false);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800949 }
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800950 }
951
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800952 Intent intent = new Intent(ConnectivityManager.CONNECTIVITY_ACTION);
953 intent.putExtra(ConnectivityManager.EXTRA_NETWORK_INFO, info);
954 if (info.isFailover()) {
955 intent.putExtra(ConnectivityManager.EXTRA_IS_FAILOVER, true);
956 info.setFailover(false);
957 }
958 if (info.getReason() != null) {
959 intent.putExtra(ConnectivityManager.EXTRA_REASON, info.getReason());
960 }
961 if (info.getExtraInfo() != null) {
Robert Greenwalt86e9e552009-07-16 17:21:39 -0700962 intent.putExtra(ConnectivityManager.EXTRA_EXTRA_INFO,
963 info.getExtraInfo());
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800964 }
Robert Greenwalt42acef32009-08-12 16:08:25 -0700965
Robert Greenwaltcc4b4012010-01-25 17:54:29 -0800966 NetworkStateTracker newNet = null;
967 if (mNetAttributes[prevNetType].isDefault()) {
968 newNet = tryFailover(prevNetType);
969 if (newNet != null) {
970 NetworkInfo switchTo = newNet.getNetworkInfo();
971 intent.putExtra(ConnectivityManager.EXTRA_OTHER_NETWORK_INFO, switchTo);
972 } else {
973 intent.putExtra(ConnectivityManager.EXTRA_NO_CONNECTIVITY, true);
974 }
Robert Greenwaltda03c4e2010-01-20 19:29:41 -0800975 }
976 // do this before we broadcast the change
Robert Greenwaltc76b8fa2010-07-23 15:46:26 -0700977 handleConnectivityChange(prevNetType);
Robert Greenwaltda03c4e2010-01-20 19:29:41 -0800978
979 sendStickyBroadcast(intent);
980 /*
981 * If the failover network is already connected, then immediately send
982 * out a followup broadcast indicating successful failover
983 */
984 if (newNet != null && newNet.getNetworkInfo().isConnected()) {
985 sendConnectedBroadcast(newNet.getNetworkInfo());
986 }
987 }
988
Robert Greenwaltcc4b4012010-01-25 17:54:29 -0800989 // returns null if no failover available
Robert Greenwaltda03c4e2010-01-20 19:29:41 -0800990 private NetworkStateTracker tryFailover(int prevNetType) {
Robert Greenwalt42acef32009-08-12 16:08:25 -0700991 /*
992 * If this is a default network, check if other defaults are available
993 * or active
994 */
995 NetworkStateTracker newNet = null;
996 if (mNetAttributes[prevNetType].isDefault()) {
Robert Greenwalt42acef32009-08-12 16:08:25 -0700997 if (mActiveDefaultNetwork == prevNetType) {
998 mActiveDefaultNetwork = -1;
999 }
1000
1001 int newType = -1;
1002 int newPriority = -1;
Robert Greenwalt35429592010-02-25 12:04:29 -08001003 boolean noMobileData = !getMobileDataEnabled();
Robert Greenwaltda03c4e2010-01-20 19:29:41 -08001004 for (int checkType=0; checkType <= ConnectivityManager.MAX_NETWORK_TYPE; checkType++) {
Robert Greenwalt5154ae762009-10-30 14:17:42 -07001005 if (checkType == prevNetType) continue;
1006 if (mNetAttributes[checkType] == null) continue;
Robert Greenwalt35429592010-02-25 12:04:29 -08001007 if (mNetAttributes[checkType].mRadio == ConnectivityManager.TYPE_MOBILE &&
1008 noMobileData) {
1009 if (DBG) {
Joe Onorato8a9b2202010-02-26 18:56:32 -08001010 Slog.d(TAG, "not failing over to mobile type " + checkType +
Robert Greenwalt35429592010-02-25 12:04:29 -08001011 " because Mobile Data Disabled");
1012 }
1013 continue;
1014 }
Robert Greenwalt42acef32009-08-12 16:08:25 -07001015 if (mNetAttributes[checkType].isDefault()) {
1016 /* TODO - if we have multiple nets we could use
1017 * we may want to put more thought into which we choose
1018 */
1019 if (checkType == mNetworkPreference) {
1020 newType = checkType;
1021 break;
1022 }
Robert Greenwalt5154ae762009-10-30 14:17:42 -07001023 if (mNetAttributes[checkType].mPriority > newPriority) {
Robert Greenwalt42acef32009-08-12 16:08:25 -07001024 newType = checkType;
Robert Greenwalt5154ae762009-10-30 14:17:42 -07001025 newPriority = mNetAttributes[newType].mPriority;
Robert Greenwalt42acef32009-08-12 16:08:25 -07001026 }
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001027 }
1028 }
Robert Greenwalt42acef32009-08-12 16:08:25 -07001029
1030 if (newType != -1) {
1031 newNet = mNetTrackers[newType];
1032 /**
1033 * See if the other network is available to fail over to.
1034 * If is not available, we enable it anyway, so that it
1035 * will be able to connect when it does become available,
1036 * but we report a total loss of connectivity rather than
1037 * report that we are attempting to fail over.
1038 */
1039 if (newNet.isAvailable()) {
1040 NetworkInfo switchTo = newNet.getNetworkInfo();
1041 switchTo.setFailover(true);
Robert Greenwalta64bf832009-08-19 20:19:33 -07001042 if (!switchTo.isConnectedOrConnecting() ||
1043 newNet.isTeardownRequested()) {
Robert Greenwalt42acef32009-08-12 16:08:25 -07001044 newNet.reconnect();
1045 }
1046 if (DBG) {
1047 if (switchTo.isConnected()) {
Joe Onorato8a9b2202010-02-26 18:56:32 -08001048 Slog.v(TAG, "Switching to already connected " +
Robert Greenwalt42acef32009-08-12 16:08:25 -07001049 switchTo.getTypeName());
1050 } else {
Joe Onorato8a9b2202010-02-26 18:56:32 -08001051 Slog.v(TAG, "Attempting to switch to " +
Robert Greenwalt42acef32009-08-12 16:08:25 -07001052 switchTo.getTypeName());
1053 }
1054 }
Robert Greenwalt42acef32009-08-12 16:08:25 -07001055 } else {
1056 newNet.reconnect();
Robert Greenwaltf0fa39e2010-03-09 14:55:08 -08001057 newNet = null; // not officially avail.. try anyway, but
1058 // report no failover
Robert Greenwalt42acef32009-08-12 16:08:25 -07001059 }
Robert Greenwalt42acef32009-08-12 16:08:25 -07001060 }
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001061 }
Robert Greenwalt42acef32009-08-12 16:08:25 -07001062
Robert Greenwaltda03c4e2010-01-20 19:29:41 -08001063 return newNet;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001064 }
1065
1066 private void sendConnectedBroadcast(NetworkInfo info) {
1067 Intent intent = new Intent(ConnectivityManager.CONNECTIVITY_ACTION);
1068 intent.putExtra(ConnectivityManager.EXTRA_NETWORK_INFO, info);
1069 if (info.isFailover()) {
1070 intent.putExtra(ConnectivityManager.EXTRA_IS_FAILOVER, true);
1071 info.setFailover(false);
1072 }
1073 if (info.getReason() != null) {
1074 intent.putExtra(ConnectivityManager.EXTRA_REASON, info.getReason());
1075 }
1076 if (info.getExtraInfo() != null) {
Robert Greenwalt86e9e552009-07-16 17:21:39 -07001077 intent.putExtra(ConnectivityManager.EXTRA_EXTRA_INFO,
1078 info.getExtraInfo());
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001079 }
Mike Lockwood0f79b542009-08-14 14:18:49 -04001080 sendStickyBroadcast(intent);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001081 }
1082
1083 /**
1084 * Called when an attempt to fail over to another network has failed.
1085 * @param info the {@link NetworkInfo} for the failed network
1086 */
1087 private void handleConnectionFailure(NetworkInfo info) {
1088 mNetTrackers[info.getType()].setTeardownRequested(false);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001089
Robert Greenwalt42acef32009-08-12 16:08:25 -07001090 String reason = info.getReason();
1091 String extraInfo = info.getExtraInfo();
Robert Greenwalt86e9e552009-07-16 17:21:39 -07001092
Robert Greenwalt42acef32009-08-12 16:08:25 -07001093 if (DBG) {
1094 String reasonText;
1095 if (reason == null) {
1096 reasonText = ".";
1097 } else {
1098 reasonText = " (" + reason + ").";
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001099 }
Joe Onorato8a9b2202010-02-26 18:56:32 -08001100 Slog.v(TAG, "Attempt to connect to " + info.getTypeName() +
Robert Greenwalt42acef32009-08-12 16:08:25 -07001101 " failed" + reasonText);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001102 }
Robert Greenwalt42acef32009-08-12 16:08:25 -07001103
1104 Intent intent = new Intent(ConnectivityManager.CONNECTIVITY_ACTION);
1105 intent.putExtra(ConnectivityManager.EXTRA_NETWORK_INFO, info);
1106 if (getActiveNetworkInfo() == null) {
1107 intent.putExtra(ConnectivityManager.EXTRA_NO_CONNECTIVITY, true);
1108 }
1109 if (reason != null) {
1110 intent.putExtra(ConnectivityManager.EXTRA_REASON, reason);
1111 }
1112 if (extraInfo != null) {
1113 intent.putExtra(ConnectivityManager.EXTRA_EXTRA_INFO, extraInfo);
1114 }
1115 if (info.isFailover()) {
1116 intent.putExtra(ConnectivityManager.EXTRA_IS_FAILOVER, true);
1117 info.setFailover(false);
1118 }
Robert Greenwaltda03c4e2010-01-20 19:29:41 -08001119
Robert Greenwaltcc4b4012010-01-25 17:54:29 -08001120 NetworkStateTracker newNet = null;
1121 if (mNetAttributes[info.getType()].isDefault()) {
1122 newNet = tryFailover(info.getType());
1123 if (newNet != null) {
1124 NetworkInfo switchTo = newNet.getNetworkInfo();
1125 intent.putExtra(ConnectivityManager.EXTRA_OTHER_NETWORK_INFO, switchTo);
1126 } else {
1127 intent.putExtra(ConnectivityManager.EXTRA_NO_CONNECTIVITY, true);
1128 }
Robert Greenwaltda03c4e2010-01-20 19:29:41 -08001129 }
Robert Greenwaltcc4b4012010-01-25 17:54:29 -08001130
Mike Lockwood0f79b542009-08-14 14:18:49 -04001131 sendStickyBroadcast(intent);
Robert Greenwaltda03c4e2010-01-20 19:29:41 -08001132 /*
1133 * If the failover network is already connected, then immediately send
1134 * out a followup broadcast indicating successful failover
1135 */
1136 if (newNet != null && newNet.getNetworkInfo().isConnected()) {
1137 sendConnectedBroadcast(newNet.getNetworkInfo());
1138 }
Mike Lockwood0f79b542009-08-14 14:18:49 -04001139 }
1140
1141 private void sendStickyBroadcast(Intent intent) {
1142 synchronized(this) {
Dianne Hackborn1c633fc2009-12-08 19:45:14 -08001143 if (!mSystemReady) {
1144 mInitialBroadcast = new Intent(intent);
Mike Lockwood0f79b542009-08-14 14:18:49 -04001145 }
Dianne Hackborn1c633fc2009-12-08 19:45:14 -08001146 intent.addFlags(Intent.FLAG_RECEIVER_REGISTERED_ONLY_BEFORE_BOOT);
1147 mContext.sendStickyBroadcast(intent);
Mike Lockwood0f79b542009-08-14 14:18:49 -04001148 }
1149 }
1150
1151 void systemReady() {
1152 synchronized(this) {
1153 mSystemReady = true;
Dianne Hackborn1c633fc2009-12-08 19:45:14 -08001154 if (mInitialBroadcast != null) {
1155 mContext.sendStickyBroadcast(mInitialBroadcast);
1156 mInitialBroadcast = null;
Mike Lockwood0f79b542009-08-14 14:18:49 -04001157 }
1158 }
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001159 }
1160
1161 private void handleConnect(NetworkInfo info) {
Robert Greenwalt42acef32009-08-12 16:08:25 -07001162 int type = info.getType();
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001163
1164 // snapshot isFailover, because sendConnectedBroadcast() resets it
1165 boolean isFailover = info.isFailover();
Robert Greenwalt42acef32009-08-12 16:08:25 -07001166 NetworkStateTracker thisNet = mNetTrackers[type];
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001167
Robert Greenwalt42acef32009-08-12 16:08:25 -07001168 // if this is a default net and other default is running
1169 // kill the one not preferred
1170 if (mNetAttributes[type].isDefault()) {
Robert Greenwalt42acef32009-08-12 16:08:25 -07001171 if (mActiveDefaultNetwork != -1 && mActiveDefaultNetwork != type) {
1172 if ((type != mNetworkPreference &&
1173 mNetAttributes[mActiveDefaultNetwork].mPriority >
1174 mNetAttributes[type].mPriority) ||
1175 mNetworkPreference == mActiveDefaultNetwork) {
1176 // don't accept this one
Joe Onorato8a9b2202010-02-26 18:56:32 -08001177 if (DBG) Slog.v(TAG, "Not broadcasting CONNECT_ACTION " +
Robert Greenwalt42acef32009-08-12 16:08:25 -07001178 "to torn down network " + info.getTypeName());
1179 teardown(thisNet);
1180 return;
1181 } else {
1182 // tear down the other
1183 NetworkStateTracker otherNet =
1184 mNetTrackers[mActiveDefaultNetwork];
Joe Onorato8a9b2202010-02-26 18:56:32 -08001185 if (DBG) Slog.v(TAG, "Policy requires " +
Robert Greenwalt42acef32009-08-12 16:08:25 -07001186 otherNet.getNetworkInfo().getTypeName() +
1187 " teardown");
1188 if (!teardown(otherNet)) {
Joe Onorato8a9b2202010-02-26 18:56:32 -08001189 Slog.e(TAG, "Network declined teardown request");
Robert Greenwalt42acef32009-08-12 16:08:25 -07001190 return;
1191 }
Robert Greenwalt14f2ef42010-06-15 12:19:37 -07001192 }
1193 }
1194 synchronized (ConnectivityService.this) {
1195 // have a new default network, release the transition wakelock in a second
1196 // if it's held. The second pause is to allow apps to reconnect over the
1197 // new network
1198 if (mNetTransitionWakeLock.isHeld()) {
1199 mHandler.sendMessageDelayed(mHandler.obtainMessage(
1200 NetworkStateTracker.EVENT_CLEAR_NET_TRANSITION_WAKELOCK,
1201 mNetTransitionWakeLockSerialNumber, 0),
1202 1000);
Robert Greenwalt42acef32009-08-12 16:08:25 -07001203 }
1204 }
1205 mActiveDefaultNetwork = type;
1206 }
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001207 thisNet.setTeardownRequested(false);
Irfan Sheriffd649c122010-06-09 15:39:36 -07001208 updateNetworkSettings(thisNet);
Robert Greenwaltc76b8fa2010-07-23 15:46:26 -07001209 handleConnectivityChange(type);
Robert Greenwalt42acef32009-08-12 16:08:25 -07001210 sendConnectedBroadcast(info);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001211 }
1212
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001213 /**
Robert Greenwaltc76b8fa2010-07-23 15:46:26 -07001214 * After a change in the connectivity state of a network. We're mainly
1215 * concerned with making sure that the list of DNS servers is set up
1216 * according to which networks are connected, and ensuring that the
1217 * right routing table entries exist.
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001218 */
Robert Greenwaltc76b8fa2010-07-23 15:46:26 -07001219 private void handleConnectivityChange(int netType) {
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001220 /*
Robert Greenwalt42acef32009-08-12 16:08:25 -07001221 * If a non-default network is enabled, add the host routes that
Robert Greenwaltc76b8fa2010-07-23 15:46:26 -07001222 * will allow it's DNS servers to be accessed.
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001223 */
Robert Greenwaltc76b8fa2010-07-23 15:46:26 -07001224 handleDnsConfigurationChange(netType);
Robert Greenwalt42acef32009-08-12 16:08:25 -07001225
Robert Greenwaltc76b8fa2010-07-23 15:46:26 -07001226 if (mNetTrackers[netType].getNetworkInfo().isConnected()) {
1227 if (mNetAttributes[netType].isDefault()) {
1228 addDefaultRoute(mNetTrackers[netType]);
Robert Greenwalt42acef32009-08-12 16:08:25 -07001229 } else {
Robert Greenwaltc76b8fa2010-07-23 15:46:26 -07001230 addPrivateDnsRoutes(mNetTrackers[netType]);
1231 }
1232 } else {
1233 if (mNetAttributes[netType].isDefault()) {
1234 removeDefaultRoute(mNetTrackers[netType]);
1235 } else {
1236 removePrivateDnsRoutes(mNetTrackers[netType]);
Robert Greenwalt42acef32009-08-12 16:08:25 -07001237 }
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001238 }
1239 }
1240
Irfan Sheriffd649c122010-06-09 15:39:36 -07001241 private void addPrivateDnsRoutes(NetworkStateTracker nt) {
Irfan Sheriffd649c122010-06-09 15:39:36 -07001242 boolean privateDnsRouteSet = nt.isPrivateDnsRouteSet();
Robert Greenwalt37e65eb2010-08-30 10:56:47 -07001243 LinkProperties p = nt.getLinkProperties();
Robert Greenwalt47f69fe2010-06-15 15:43:39 -07001244 if (p == null) return;
1245 String interfaceName = p.getInterfaceName();
Irfan Sheriffd649c122010-06-09 15:39:36 -07001246
1247 if (DBG) {
1248 Slog.d(TAG, "addPrivateDnsRoutes for " + nt +
1249 "(" + interfaceName + ") - mPrivateDnsRouteSet = " + privateDnsRouteSet);
1250 }
Irfan Sheriffd649c122010-06-09 15:39:36 -07001251 if (interfaceName != null && !privateDnsRouteSet) {
Robert Greenwalt47f69fe2010-06-15 15:43:39 -07001252 Collection<InetAddress> dnsList = p.getDnses();
1253 for (InetAddress dns : dnsList) {
1254 if (DBG) Slog.d(TAG, " adding " + dns);
Robert Greenwalt585ac0f2010-08-27 09:24:29 -07001255 NetworkUtils.addHostRoute(interfaceName, dns, null);
Irfan Sheriffd649c122010-06-09 15:39:36 -07001256 }
1257 nt.privateDnsRouteSet(true);
1258 }
1259 }
1260
1261 private void removePrivateDnsRoutes(NetworkStateTracker nt) {
1262 // TODO - we should do this explicitly but the NetUtils api doesnt
1263 // support this yet - must remove all. No worse than before
Robert Greenwalt37e65eb2010-08-30 10:56:47 -07001264 LinkProperties p = nt.getLinkProperties();
Robert Greenwalt47f69fe2010-06-15 15:43:39 -07001265 if (p == null) return;
1266 String interfaceName = p.getInterfaceName();
Irfan Sheriffd649c122010-06-09 15:39:36 -07001267 boolean privateDnsRouteSet = nt.isPrivateDnsRouteSet();
1268 if (interfaceName != null && privateDnsRouteSet) {
1269 if (DBG) {
1270 Slog.d(TAG, "removePrivateDnsRoutes for " + nt.getNetworkInfo().getTypeName() +
1271 " (" + interfaceName + ")");
1272 }
1273 NetworkUtils.removeHostRoutes(interfaceName);
1274 nt.privateDnsRouteSet(false);
1275 }
1276 }
1277
Irfan Sheriffd649c122010-06-09 15:39:36 -07001278
1279 private void addDefaultRoute(NetworkStateTracker nt) {
Robert Greenwalt37e65eb2010-08-30 10:56:47 -07001280 LinkProperties p = nt.getLinkProperties();
Robert Greenwalt47f69fe2010-06-15 15:43:39 -07001281 if (p == null) return;
1282 String interfaceName = p.getInterfaceName();
1283 InetAddress defaultGatewayAddr = p.getGateway();
Irfan Sheriffd649c122010-06-09 15:39:36 -07001284
Robert Greenwaltc76b8fa2010-07-23 15:46:26 -07001285 if ((interfaceName != null) && (defaultGatewayAddr != null )) {
Robert Greenwalt585ac0f2010-08-27 09:24:29 -07001286 if (!NetworkUtils.addDefaultRoute(interfaceName, defaultGatewayAddr) && DBG) {
Robert Greenwalt47f69fe2010-06-15 15:43:39 -07001287 NetworkInfo networkInfo = nt.getNetworkInfo();
Irfan Sheriffd649c122010-06-09 15:39:36 -07001288 Slog.d(TAG, "addDefaultRoute for " + networkInfo.getTypeName() +
1289 " (" + interfaceName + "), GatewayAddr=" + defaultGatewayAddr);
1290 }
Irfan Sheriffd649c122010-06-09 15:39:36 -07001291 }
1292 }
1293
1294
1295 public void removeDefaultRoute(NetworkStateTracker nt) {
Robert Greenwalt37e65eb2010-08-30 10:56:47 -07001296 LinkProperties p = nt.getLinkProperties();
Robert Greenwalt47f69fe2010-06-15 15:43:39 -07001297 if (p == null) return;
1298 String interfaceName = p.getInterfaceName();
Irfan Sheriffd649c122010-06-09 15:39:36 -07001299
Robert Greenwaltc76b8fa2010-07-23 15:46:26 -07001300 if (interfaceName != null) {
1301 if ((NetworkUtils.removeDefaultRoute(interfaceName) >= 0) && DBG) {
Robert Greenwalt47f69fe2010-06-15 15:43:39 -07001302 NetworkInfo networkInfo = nt.getNetworkInfo();
Irfan Sheriffd649c122010-06-09 15:39:36 -07001303 Slog.d(TAG, "removeDefaultRoute for " + networkInfo.getTypeName() + " (" +
1304 interfaceName + ")");
1305 }
Irfan Sheriffd649c122010-06-09 15:39:36 -07001306 }
1307 }
1308
1309 /**
1310 * Reads the network specific TCP buffer sizes from SystemProperties
1311 * net.tcp.buffersize.[default|wifi|umts|edge|gprs] and set them for system
1312 * wide use
1313 */
1314 public void updateNetworkSettings(NetworkStateTracker nt) {
1315 String key = nt.getTcpBufferSizesPropName();
1316 String bufferSizes = SystemProperties.get(key);
1317
1318 if (bufferSizes.length() == 0) {
1319 Slog.e(TAG, key + " not found in system properties. Using defaults");
1320
1321 // Setting to default values so we won't be stuck to previous values
1322 key = "net.tcp.buffersize.default";
1323 bufferSizes = SystemProperties.get(key);
1324 }
1325
1326 // Set values in kernel
1327 if (bufferSizes.length() != 0) {
1328 if (DBG) {
1329 Slog.v(TAG, "Setting TCP values: [" + bufferSizes
1330 + "] which comes from [" + key + "]");
1331 }
1332 setBufferSize(bufferSizes);
1333 }
1334 }
1335
1336 /**
1337 * Writes TCP buffer sizes to /sys/kernel/ipv4/tcp_[r/w]mem_[min/def/max]
1338 * which maps to /proc/sys/net/ipv4/tcp_rmem and tcpwmem
1339 *
1340 * @param bufferSizes in the format of "readMin, readInitial, readMax,
1341 * writeMin, writeInitial, writeMax"
1342 */
1343 private void setBufferSize(String bufferSizes) {
1344 try {
1345 String[] values = bufferSizes.split(",");
1346
1347 if (values.length == 6) {
1348 final String prefix = "/sys/kernel/ipv4/tcp_";
1349 stringToFile(prefix + "rmem_min", values[0]);
1350 stringToFile(prefix + "rmem_def", values[1]);
1351 stringToFile(prefix + "rmem_max", values[2]);
1352 stringToFile(prefix + "wmem_min", values[3]);
1353 stringToFile(prefix + "wmem_def", values[4]);
1354 stringToFile(prefix + "wmem_max", values[5]);
1355 } else {
1356 Slog.e(TAG, "Invalid buffersize string: " + bufferSizes);
1357 }
1358 } catch (IOException e) {
1359 Slog.e(TAG, "Can't set tcp buffer sizes:" + e);
1360 }
1361 }
1362
1363 /**
1364 * Writes string to file. Basically same as "echo -n $string > $filename"
1365 *
1366 * @param filename
1367 * @param string
1368 * @throws IOException
1369 */
1370 private void stringToFile(String filename, String string) throws IOException {
1371 FileWriter out = new FileWriter(filename);
1372 try {
1373 out.write(string);
1374 } finally {
1375 out.close();
1376 }
1377 }
1378
1379
Robert Greenwalt42acef32009-08-12 16:08:25 -07001380 /**
1381 * Adjust the per-process dns entries (net.dns<x>.<pid>) based
1382 * on the highest priority active net which this process requested.
1383 * If there aren't any, clear it out
1384 */
1385 private void reassessPidDns(int myPid, boolean doBump)
1386 {
Joe Onorato8a9b2202010-02-26 18:56:32 -08001387 if (DBG) Slog.d(TAG, "reassessPidDns for pid " + myPid);
Robert Greenwalt42acef32009-08-12 16:08:25 -07001388 for(int i : mPriorityList) {
1389 if (mNetAttributes[i].isDefault()) {
1390 continue;
1391 }
1392 NetworkStateTracker nt = mNetTrackers[i];
Robert Greenwalt86e9e552009-07-16 17:21:39 -07001393 if (nt.getNetworkInfo().isConnected() &&
1394 !nt.isTeardownRequested()) {
Robert Greenwalt37e65eb2010-08-30 10:56:47 -07001395 LinkProperties p = nt.getLinkProperties();
Robert Greenwalt47f69fe2010-06-15 15:43:39 -07001396 if (p == null) continue;
Robert Greenwalt42acef32009-08-12 16:08:25 -07001397 List pids = mNetRequestersPids[i];
1398 for (int j=0; j<pids.size(); j++) {
1399 Integer pid = (Integer)pids.get(j);
1400 if (pid.intValue() == myPid) {
Robert Greenwalt47f69fe2010-06-15 15:43:39 -07001401 Collection<InetAddress> dnses = p.getDnses();
1402 writePidDns(dnses, myPid);
Robert Greenwalt42acef32009-08-12 16:08:25 -07001403 if (doBump) {
1404 bumpDns();
1405 }
1406 return;
1407 }
1408 }
1409 }
1410 }
1411 // nothing found - delete
1412 for (int i = 1; ; i++) {
1413 String prop = "net.dns" + i + "." + myPid;
1414 if (SystemProperties.get(prop).length() == 0) {
1415 if (doBump) {
1416 bumpDns();
1417 }
1418 return;
1419 }
1420 SystemProperties.set(prop, "");
1421 }
1422 }
1423
Robert Greenwalt47f69fe2010-06-15 15:43:39 -07001424 private void writePidDns(Collection <InetAddress> dnses, int pid) {
Robert Greenwalt42acef32009-08-12 16:08:25 -07001425 int j = 1;
Robert Greenwalt47f69fe2010-06-15 15:43:39 -07001426 for (InetAddress dns : dnses) {
1427 SystemProperties.set("net.dns" + j++ + "." + pid, dns.getHostAddress());
Robert Greenwalt42acef32009-08-12 16:08:25 -07001428 }
1429 }
1430
1431 private void bumpDns() {
1432 /*
1433 * Bump the property that tells the name resolver library to reread
1434 * the DNS server list from the properties.
1435 */
1436 String propVal = SystemProperties.get("net.dnschange");
1437 int n = 0;
1438 if (propVal.length() != 0) {
1439 try {
1440 n = Integer.parseInt(propVal);
1441 } catch (NumberFormatException e) {}
1442 }
1443 SystemProperties.set("net.dnschange", "" + (n+1));
1444 }
1445
Robert Greenwaltc76b8fa2010-07-23 15:46:26 -07001446 private void handleDnsConfigurationChange(int netType) {
Robert Greenwalt42acef32009-08-12 16:08:25 -07001447 // add default net's dns entries
Robert Greenwaltc76b8fa2010-07-23 15:46:26 -07001448 NetworkStateTracker nt = mNetTrackers[netType];
1449 if (nt != null && nt.getNetworkInfo().isConnected() && !nt.isTeardownRequested()) {
Robert Greenwalt37e65eb2010-08-30 10:56:47 -07001450 LinkProperties p = nt.getLinkProperties();
Robert Greenwaltc76b8fa2010-07-23 15:46:26 -07001451 if (p == null) return;
1452 Collection<InetAddress> dnses = p.getDnses();
1453 if (mNetAttributes[netType].isDefault()) {
1454 int j = 1;
Robert Greenwalte90aa5e2010-09-01 11:34:05 -07001455 if (dnses.size() == 0 && mDefaultDns != null) {
Robert Greenwaltc76b8fa2010-07-23 15:46:26 -07001456 if (DBG) {
Robert Greenwalte90aa5e2010-09-01 11:34:05 -07001457 Slog.d(TAG, "no dns provided - using " + mDefaultDns.getHostAddress());
Robert Greenwalt42acef32009-08-12 16:08:25 -07001458 }
Robert Greenwalte90aa5e2010-09-01 11:34:05 -07001459 SystemProperties.set("net.dns1", mDefaultDns.getHostAddress());
1460 j++;
1461 } else {
1462 for (InetAddress dns : dnses) {
1463 if (DBG) {
1464 Slog.d(TAG, "adding dns " + dns + " for " +
1465 nt.getNetworkInfo().getTypeName());
1466 }
1467 SystemProperties.set("net.dns" + j++, dns.getHostAddress());
1468 }
Robert Greenwaltc76b8fa2010-07-23 15:46:26 -07001469 }
1470 for (int k=j ; k<mNumDnsEntries; k++) {
1471 if (DBG) Slog.d(TAG, "erasing net.dns" + k);
1472 SystemProperties.set("net.dns" + k, "");
1473 }
1474 mNumDnsEntries = j;
1475 } else {
1476 // set per-pid dns for attached secondary nets
1477 List pids = mNetRequestersPids[netType];
1478 for (int y=0; y< pids.size(); y++) {
1479 Integer pid = (Integer)pids.get(y);
1480 writePidDns(dnses, pid.intValue());
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001481 }
1482 }
Robert Greenwaltc76b8fa2010-07-23 15:46:26 -07001483 bumpDns();
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001484 }
Robert Greenwalt42acef32009-08-12 16:08:25 -07001485 }
1486
1487 private int getRestoreDefaultNetworkDelay() {
1488 String restoreDefaultNetworkDelayStr = SystemProperties.get(
1489 NETWORK_RESTORE_DELAY_PROP_NAME);
1490 if(restoreDefaultNetworkDelayStr != null &&
1491 restoreDefaultNetworkDelayStr.length() != 0) {
1492 try {
1493 return Integer.valueOf(restoreDefaultNetworkDelayStr);
1494 } catch (NumberFormatException e) {
1495 }
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001496 }
Robert Greenwalt42acef32009-08-12 16:08:25 -07001497 return RESTORE_DEFAULT_NETWORK_DELAY;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001498 }
1499
1500 @Override
1501 protected void dump(FileDescriptor fd, PrintWriter pw, String[] args) {
Robert Greenwalt86e9e552009-07-16 17:21:39 -07001502 if (mContext.checkCallingOrSelfPermission(
1503 android.Manifest.permission.DUMP)
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001504 != PackageManager.PERMISSION_GRANTED) {
Robert Greenwalt86e9e552009-07-16 17:21:39 -07001505 pw.println("Permission Denial: can't dump ConnectivityService " +
1506 "from from pid=" + Binder.getCallingPid() + ", uid=" +
1507 Binder.getCallingUid());
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001508 return;
1509 }
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001510 pw.println();
1511 for (NetworkStateTracker nst : mNetTrackers) {
Robert Greenwaltb9285352009-12-21 18:24:07 -08001512 if (nst != null) {
1513 if (nst.getNetworkInfo().isConnected()) {
1514 pw.println("Active network: " + nst.getNetworkInfo().
1515 getTypeName());
1516 }
1517 pw.println(nst.getNetworkInfo());
1518 pw.println(nst);
1519 pw.println();
Robert Greenwalt42acef32009-08-12 16:08:25 -07001520 }
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001521 }
Robert Greenwaltb9285352009-12-21 18:24:07 -08001522
1523 pw.println("Network Requester Pids:");
1524 for (int net : mPriorityList) {
1525 String pidString = net + ": ";
1526 for (Object pid : mNetRequestersPids[net]) {
1527 pidString = pidString + pid.toString() + ", ";
1528 }
1529 pw.println(pidString);
1530 }
1531 pw.println();
1532
1533 pw.println("FeatureUsers:");
1534 for (Object requester : mFeatureUsers) {
1535 pw.println(requester.toString());
1536 }
1537 pw.println();
Robert Greenwalt2a091d72010-02-11 18:18:40 -08001538
Robert Greenwalt14f2ef42010-06-15 12:19:37 -07001539 synchronized (this) {
1540 pw.println("NetworkTranstionWakeLock is currently " +
1541 (mNetTransitionWakeLock.isHeld() ? "" : "not ") + "held.");
1542 pw.println("It was last requested for "+mNetTransitionWakeLockCausedBy);
1543 }
1544 pw.println();
1545
Robert Greenwalt2a091d72010-02-11 18:18:40 -08001546 mTethering.dump(fd, pw, args);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001547 }
1548
Robert Greenwalt42acef32009-08-12 16:08:25 -07001549 // must be stateless - things change under us.
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001550 private class MyHandler extends Handler {
Wink Savillebb08caf2010-09-02 19:23:52 -07001551 public MyHandler(Looper looper) {
1552 super(looper);
1553 }
1554
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001555 @Override
1556 public void handleMessage(Message msg) {
1557 NetworkInfo info;
1558 switch (msg.what) {
1559 case NetworkStateTracker.EVENT_STATE_CHANGED:
1560 info = (NetworkInfo) msg.obj;
Robert Greenwalt511288a2009-12-07 11:33:18 -08001561 int type = info.getType();
1562 NetworkInfo.State state = info.getState();
Robert Greenwalt6e6dec22010-01-25 16:14:00 -08001563 // only do this optimization for wifi. It going into scan mode for location
1564 // services generates alot of noise. Meanwhile the mms apn won't send out
1565 // subsequent notifications when on default cellular because it never
1566 // disconnects.. so only do this to wifi notifications. Fixed better when the
1567 // APN notifications are standardized.
1568 if (mNetAttributes[type].mLastState == state &&
1569 mNetAttributes[type].mRadio == ConnectivityManager.TYPE_WIFI) {
Robert Greenwalt511288a2009-12-07 11:33:18 -08001570 if (DBG) {
Robert Greenwalt6e6dec22010-01-25 16:14:00 -08001571 // TODO - remove this after we validate the dropping doesn't break
1572 // anything
Joe Onorato8a9b2202010-02-26 18:56:32 -08001573 Slog.d(TAG, "Dropping ConnectivityChange for " +
Robert Greenwalt1193ae42010-01-13 09:36:31 -08001574 info.getTypeName() + ": " +
Robert Greenwalt511288a2009-12-07 11:33:18 -08001575 state + "/" + info.getDetailedState());
1576 }
1577 return;
1578 }
1579 mNetAttributes[type].mLastState = state;
1580
Joe Onorato8a9b2202010-02-26 18:56:32 -08001581 if (DBG) Slog.d(TAG, "ConnectivityChange for " +
Robert Greenwalt86e9e552009-07-16 17:21:39 -07001582 info.getTypeName() + ": " +
Robert Greenwalt511288a2009-12-07 11:33:18 -08001583 state + "/" + info.getDetailedState());
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001584
1585 // Connectivity state changed:
1586 // [31-13] Reserved for future use
Robert Greenwalt86e9e552009-07-16 17:21:39 -07001587 // [12-9] Network subtype (for mobile network, as defined
1588 // by TelephonyManager)
1589 // [8-3] Detailed state ordinal (as defined by
1590 // NetworkInfo.DetailedState)
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001591 // [2-0] Network type (as defined by ConnectivityManager)
1592 int eventLogParam = (info.getType() & 0x7) |
1593 ((info.getDetailedState().ordinal() & 0x3f) << 3) |
1594 (info.getSubtype() << 9);
Doug Zongkerab5c49c2009-12-04 10:31:43 -08001595 EventLog.writeEvent(EventLogTags.CONNECTIVITY_STATE_CHANGED,
Robert Greenwalt86e9e552009-07-16 17:21:39 -07001596 eventLogParam);
1597
1598 if (info.getDetailedState() ==
1599 NetworkInfo.DetailedState.FAILED) {
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001600 handleConnectionFailure(info);
Robert Greenwalt511288a2009-12-07 11:33:18 -08001601 } else if (state == NetworkInfo.State.DISCONNECTED) {
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001602 handleDisconnect(info);
Robert Greenwalt511288a2009-12-07 11:33:18 -08001603 } else if (state == NetworkInfo.State.SUSPENDED) {
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001604 // TODO: need to think this over.
Robert Greenwalt86e9e552009-07-16 17:21:39 -07001605 // the logic here is, handle SUSPENDED the same as
1606 // DISCONNECTED. The only difference being we are
1607 // broadcasting an intent with NetworkInfo that's
1608 // suspended. This allows the applications an
1609 // opportunity to handle DISCONNECTED and SUSPENDED
1610 // differently, or not.
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001611 handleDisconnect(info);
Robert Greenwalt511288a2009-12-07 11:33:18 -08001612 } else if (state == NetworkInfo.State.CONNECTED) {
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001613 handleConnect(info);
1614 }
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001615 break;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001616 case NetworkStateTracker.EVENT_CONFIGURATION_CHANGED:
Robert Greenwaltc76b8fa2010-07-23 15:46:26 -07001617 // TODO - make this handle ip/proxy/gateway/dns changes
1618 info = (NetworkInfo) msg.obj;
1619 type = info.getType();
1620 handleDnsConfigurationChange(type);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001621 break;
Robert Greenwalt42acef32009-08-12 16:08:25 -07001622 case NetworkStateTracker.EVENT_RESTORE_DEFAULT_NETWORK:
Robert Greenwalt9c75d4a2009-09-27 17:27:04 -07001623 FeatureUser u = (FeatureUser)msg.obj;
1624 u.expire();
Robert Greenwalt42acef32009-08-12 16:08:25 -07001625 break;
Robert Greenwalt14f2ef42010-06-15 12:19:37 -07001626 case NetworkStateTracker.EVENT_CLEAR_NET_TRANSITION_WAKELOCK:
1627 String causedBy = null;
1628 synchronized (ConnectivityService.this) {
1629 if (msg.arg1 == mNetTransitionWakeLockSerialNumber &&
1630 mNetTransitionWakeLock.isHeld()) {
1631 mNetTransitionWakeLock.release();
1632 causedBy = mNetTransitionWakeLockCausedBy;
1633 }
1634 }
1635 if (causedBy != null) {
1636 Slog.d(TAG, "NetTransition Wakelock for " +
1637 causedBy + " released by timeout");
1638 }
1639 break;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001640 }
1641 }
1642 }
Robert Greenwaltd0e18ff2010-01-26 11:40:34 -08001643
1644 // javadoc from interface
Robert Greenwalt5a735062010-03-02 17:25:02 -08001645 public int tether(String iface) {
Robert Greenwaltd0e18ff2010-01-26 11:40:34 -08001646 enforceTetherChangePermission();
Robert Greenwalt5a735062010-03-02 17:25:02 -08001647
1648 if (isTetheringSupported()) {
1649 return mTethering.tether(iface);
1650 } else {
1651 return ConnectivityManager.TETHER_ERROR_UNSUPPORTED;
1652 }
Robert Greenwaltd0e18ff2010-01-26 11:40:34 -08001653 }
1654
1655 // javadoc from interface
Robert Greenwalt5a735062010-03-02 17:25:02 -08001656 public int untether(String iface) {
Robert Greenwaltd0e18ff2010-01-26 11:40:34 -08001657 enforceTetherChangePermission();
Robert Greenwalt5a735062010-03-02 17:25:02 -08001658
1659 if (isTetheringSupported()) {
1660 return mTethering.untether(iface);
1661 } else {
1662 return ConnectivityManager.TETHER_ERROR_UNSUPPORTED;
1663 }
1664 }
1665
1666 // javadoc from interface
1667 public int getLastTetherError(String iface) {
1668 enforceTetherAccessPermission();
1669
1670 if (isTetheringSupported()) {
1671 return mTethering.getLastTetherError(iface);
1672 } else {
1673 return ConnectivityManager.TETHER_ERROR_UNSUPPORTED;
1674 }
Robert Greenwalt2a091d72010-02-11 18:18:40 -08001675 }
1676
1677 // TODO - proper iface API for selection by property, inspection, etc
1678 public String[] getTetherableUsbRegexs() {
1679 enforceTetherAccessPermission();
1680 if (isTetheringSupported()) {
1681 return mTethering.getTetherableUsbRegexs();
1682 } else {
1683 return new String[0];
1684 }
1685 }
1686
1687 public String[] getTetherableWifiRegexs() {
1688 enforceTetherAccessPermission();
1689 if (isTetheringSupported()) {
1690 return mTethering.getTetherableWifiRegexs();
1691 } else {
1692 return new String[0];
1693 }
Robert Greenwaltd0e18ff2010-01-26 11:40:34 -08001694 }
1695
Danica Chang6fdd0c62010-08-11 14:54:43 -07001696 public String[] getTetherableBluetoothRegexs() {
1697 enforceTetherAccessPermission();
1698 if (isTetheringSupported()) {
1699 return mTethering.getTetherableBluetoothRegexs();
1700 } else {
1701 return new String[0];
1702 }
1703 }
1704
Robert Greenwaltd0e18ff2010-01-26 11:40:34 -08001705 // TODO - move iface listing, queries, etc to new module
1706 // javadoc from interface
1707 public String[] getTetherableIfaces() {
Robert Greenwalt2a091d72010-02-11 18:18:40 -08001708 enforceTetherAccessPermission();
Robert Greenwaltd0e18ff2010-01-26 11:40:34 -08001709 return mTethering.getTetherableIfaces();
1710 }
1711
1712 public String[] getTetheredIfaces() {
Robert Greenwalt2a091d72010-02-11 18:18:40 -08001713 enforceTetherAccessPermission();
Robert Greenwaltd0e18ff2010-01-26 11:40:34 -08001714 return mTethering.getTetheredIfaces();
1715 }
Robert Greenwalt2a091d72010-02-11 18:18:40 -08001716
Robert Greenwalt5a735062010-03-02 17:25:02 -08001717 public String[] getTetheringErroredIfaces() {
1718 enforceTetherAccessPermission();
1719 return mTethering.getErroredIfaces();
1720 }
1721
Robert Greenwalt2a091d72010-02-11 18:18:40 -08001722 // if ro.tether.denied = true we default to no tethering
1723 // gservices could set the secure setting to 1 though to enable it on a build where it
1724 // had previously been turned off.
1725 public boolean isTetheringSupported() {
1726 enforceTetherAccessPermission();
1727 int defaultVal = (SystemProperties.get("ro.tether.denied").equals("true") ? 0 : 1);
Robert Greenwaltc9d5fb72010-02-25 12:29:30 -08001728 boolean tetherEnabledInSettings = (Settings.Secure.getInt(mContext.getContentResolver(),
1729 Settings.Secure.TETHER_SUPPORTED, defaultVal) != 0);
1730 return tetherEnabledInSettings && mTetheringConfigValid;
Robert Greenwalt2a091d72010-02-11 18:18:40 -08001731 }
Robert Greenwalt14f2ef42010-06-15 12:19:37 -07001732
1733 // An API NetworkStateTrackers can call when they lose their network.
1734 // This will automatically be cleared after X seconds or a network becomes CONNECTED,
1735 // whichever happens first. The timer is started by the first caller and not
1736 // restarted by subsequent callers.
1737 public void requestNetworkTransitionWakelock(String forWhom) {
1738 enforceConnectivityInternalPermission();
1739 synchronized (this) {
1740 if (mNetTransitionWakeLock.isHeld()) return;
1741 mNetTransitionWakeLockSerialNumber++;
1742 mNetTransitionWakeLock.acquire();
1743 mNetTransitionWakeLockCausedBy = forWhom;
1744 }
1745 mHandler.sendMessageDelayed(mHandler.obtainMessage(
1746 NetworkStateTracker.EVENT_CLEAR_NET_TRANSITION_WAKELOCK,
1747 mNetTransitionWakeLockSerialNumber, 0),
1748 mNetTransitionWakeLockTimeout);
1749 return;
1750 }
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001751}