blob: 57e8e021fe207c4bf445a567927ee55c6f5ded1e [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 Greenwalta7dfbd32010-06-15 15:43:39 -070029import android.net.NetworkProperties;
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;
108 private static ConnectivityService sServiceInstance;
109
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 Greenwalt12c44552009-12-07 11:33:18 -0800124 private static class NetworkAttributes {
Robert Greenwalt2034b912009-08-12 16:08:25 -0700125 /**
126 * Class for holding settings read from resources.
127 */
128 public String mName;
129 public int mType;
130 public int mRadio;
131 public int mPriority;
Robert Greenwalt12c44552009-12-07 11:33:18 -0800132 public NetworkInfo.State mLastState;
Robert Greenwalt2034b912009-08-12 16:08:25 -0700133 public NetworkAttributes(String init) {
134 String fragments[] = init.split(",");
135 mName = fragments[0].toLowerCase();
Robert Greenwaltec05b3c2009-10-30 14:17:42 -0700136 mType = Integer.parseInt(fragments[1]);
137 mRadio = Integer.parseInt(fragments[2]);
138 mPriority = Integer.parseInt(fragments[3]);
Robert Greenwalt12c44552009-12-07 11:33:18 -0800139 mLastState = NetworkInfo.State.UNKNOWN;
Robert Greenwalt2034b912009-08-12 16:08:25 -0700140 }
141 public boolean isDefault() {
142 return (mType == mRadio);
143 }
144 }
145 NetworkAttributes[] mNetAttributes;
Robert Greenwaltec05b3c2009-10-30 14:17:42 -0700146 int mNetworksDefined;
Robert Greenwalt2034b912009-08-12 16:08:25 -0700147
Robert Greenwalt12c44552009-12-07 11:33:18 -0800148 private static class RadioAttributes {
Robert Greenwalt2034b912009-08-12 16:08:25 -0700149 public int mSimultaneity;
150 public int mType;
151 public RadioAttributes(String init) {
152 String fragments[] = init.split(",");
Robert Greenwaltec05b3c2009-10-30 14:17:42 -0700153 mType = Integer.parseInt(fragments[0]);
154 mSimultaneity = Integer.parseInt(fragments[1]);
Robert Greenwalt2034b912009-08-12 16:08:25 -0700155 }
156 }
157 RadioAttributes[] mRadioAttributes;
158
The Android Open Source Project28527d22009-03-03 19:31:44 -0800159 private static class ConnectivityThread extends Thread {
160 private Context mContext;
Robert Greenwalt0659da32009-07-16 17:21:39 -0700161
The Android Open Source Project28527d22009-03-03 19:31:44 -0800162 private ConnectivityThread(Context context) {
163 super("ConnectivityThread");
164 mContext = context;
165 }
166
167 @Override
168 public void run() {
169 Looper.prepare();
170 synchronized (this) {
171 sServiceInstance = new ConnectivityService(mContext);
172 notifyAll();
173 }
174 Looper.loop();
175 }
Robert Greenwalt0659da32009-07-16 17:21:39 -0700176
The Android Open Source Project28527d22009-03-03 19:31:44 -0800177 public static ConnectivityService getServiceInstance(Context context) {
178 ConnectivityThread thread = new ConnectivityThread(context);
179 thread.start();
Robert Greenwalt0659da32009-07-16 17:21:39 -0700180
The Android Open Source Project28527d22009-03-03 19:31:44 -0800181 synchronized (thread) {
182 while (sServiceInstance == null) {
183 try {
184 // Wait until sServiceInstance has been initialized.
185 thread.wait();
186 } catch (InterruptedException ignore) {
Joe Onoratoc2386bb2010-02-26 18:56:32 -0800187 Slog.e(TAG,
Robert Greenwalt0659da32009-07-16 17:21:39 -0700188 "Unexpected InterruptedException while waiting"+
189 " for ConnectivityService thread");
The Android Open Source Project28527d22009-03-03 19:31:44 -0800190 }
191 }
192 }
Robert Greenwalt0659da32009-07-16 17:21:39 -0700193
The Android Open Source Project28527d22009-03-03 19:31:44 -0800194 return sServiceInstance;
195 }
196 }
Robert Greenwalt0659da32009-07-16 17:21:39 -0700197
The Android Open Source Project28527d22009-03-03 19:31:44 -0800198 public static ConnectivityService getInstance(Context context) {
199 return ConnectivityThread.getServiceInstance(context);
200 }
Robert Greenwalt0659da32009-07-16 17:21:39 -0700201
The Android Open Source Project28527d22009-03-03 19:31:44 -0800202 private ConnectivityService(Context context) {
Joe Onoratoc2386bb2010-02-26 18:56:32 -0800203 if (DBG) Slog.v(TAG, "ConnectivityService starting up");
Robert Greenwaltd48f8ee2010-01-14 17:47:58 -0800204
205 // setup our unique device name
206 String id = Settings.Secure.getString(context.getContentResolver(),
207 Settings.Secure.ANDROID_ID);
208 if (id != null && id.length() > 0) {
209 String name = new String("android_").concat(id);
210 SystemProperties.set("net.hostname", name);
211 }
212
The Android Open Source Project28527d22009-03-03 19:31:44 -0800213 mContext = context;
Robert Greenwalt93dc1042010-06-15 12:19:37 -0700214
215 PowerManager powerManager = (PowerManager)mContext.getSystemService(Context.POWER_SERVICE);
216 mNetTransitionWakeLock = powerManager.newWakeLock(PowerManager.PARTIAL_WAKE_LOCK, TAG);
217 mNetTransitionWakeLockTimeout = mContext.getResources().getInteger(
218 com.android.internal.R.integer.config_networkTransitionTimeout);
219
Robert Greenwalt2034b912009-08-12 16:08:25 -0700220 mNetTrackers = new NetworkStateTracker[
221 ConnectivityManager.MAX_NETWORK_TYPE+1];
222 mHandler = new MyHandler();
Robert Greenwalt0659da32009-07-16 17:21:39 -0700223
The Android Open Source Project28527d22009-03-03 19:31:44 -0800224 mNetworkPreference = getPersistedNetworkPreference();
Robert Greenwalt0659da32009-07-16 17:21:39 -0700225
Robert Greenwaltec05b3c2009-10-30 14:17:42 -0700226 mRadioAttributes = new RadioAttributes[ConnectivityManager.MAX_RADIO_TYPE+1];
227 mNetAttributes = new NetworkAttributes[ConnectivityManager.MAX_NETWORK_TYPE+1];
228
Robert Greenwalt2034b912009-08-12 16:08:25 -0700229 // Load device network attributes from resources
Robert Greenwalt2034b912009-08-12 16:08:25 -0700230 String[] raStrings = context.getResources().getStringArray(
231 com.android.internal.R.array.radioAttributes);
Robert Greenwaltec05b3c2009-10-30 14:17:42 -0700232 for (String raString : raStrings) {
233 RadioAttributes r = new RadioAttributes(raString);
234 if (r.mType > ConnectivityManager.MAX_RADIO_TYPE) {
Joe Onoratoc2386bb2010-02-26 18:56:32 -0800235 Slog.e(TAG, "Error in radioAttributes - ignoring attempt to define type " + r.mType);
Robert Greenwaltec05b3c2009-10-30 14:17:42 -0700236 continue;
237 }
238 if (mRadioAttributes[r.mType] != null) {
Joe Onoratoc2386bb2010-02-26 18:56:32 -0800239 Slog.e(TAG, "Error in radioAttributes - ignoring attempt to redefine type " +
Robert Greenwaltec05b3c2009-10-30 14:17:42 -0700240 r.mType);
241 continue;
242 }
Robert Greenwalt2034b912009-08-12 16:08:25 -0700243 mRadioAttributes[r.mType] = r;
244 }
245
Robert Greenwaltec05b3c2009-10-30 14:17:42 -0700246 String[] naStrings = context.getResources().getStringArray(
247 com.android.internal.R.array.networkAttributes);
248 for (String naString : naStrings) {
249 try {
250 NetworkAttributes n = new NetworkAttributes(naString);
251 if (n.mType > ConnectivityManager.MAX_NETWORK_TYPE) {
Joe Onoratoc2386bb2010-02-26 18:56:32 -0800252 Slog.e(TAG, "Error in networkAttributes - ignoring attempt to define type " +
Robert Greenwaltec05b3c2009-10-30 14:17:42 -0700253 n.mType);
254 continue;
Robert Greenwalt2034b912009-08-12 16:08:25 -0700255 }
Robert Greenwaltec05b3c2009-10-30 14:17:42 -0700256 if (mNetAttributes[n.mType] != null) {
Joe Onoratoc2386bb2010-02-26 18:56:32 -0800257 Slog.e(TAG, "Error in networkAttributes - ignoring attempt to redefine type " +
Robert Greenwaltec05b3c2009-10-30 14:17:42 -0700258 n.mType);
259 continue;
260 }
261 if (mRadioAttributes[n.mRadio] == null) {
Joe Onoratoc2386bb2010-02-26 18:56:32 -0800262 Slog.e(TAG, "Error in networkAttributes - ignoring attempt to use undefined " +
Robert Greenwaltec05b3c2009-10-30 14:17:42 -0700263 "radio " + n.mRadio + " in network type " + n.mType);
264 continue;
265 }
266 mNetAttributes[n.mType] = n;
267 mNetworksDefined++;
268 } catch(Exception e) {
269 // ignore it - leave the entry null
Robert Greenwalt2034b912009-08-12 16:08:25 -0700270 }
271 }
272
Robert Greenwaltec05b3c2009-10-30 14:17:42 -0700273 // high priority first
274 mPriorityList = new int[mNetworksDefined];
275 {
276 int insertionPoint = mNetworksDefined-1;
277 int currentLowest = 0;
278 int nextLowest = 0;
279 while (insertionPoint > -1) {
280 for (NetworkAttributes na : mNetAttributes) {
281 if (na == null) continue;
282 if (na.mPriority < currentLowest) continue;
283 if (na.mPriority > currentLowest) {
284 if (na.mPriority < nextLowest || nextLowest == 0) {
285 nextLowest = na.mPriority;
286 }
287 continue;
288 }
289 mPriorityList[insertionPoint--] = na.mType;
290 }
291 currentLowest = nextLowest;
292 nextLowest = 0;
293 }
294 }
295
296 mNetRequestersPids = new ArrayList[ConnectivityManager.MAX_NETWORK_TYPE+1];
297 for (int i : mPriorityList) {
Robert Greenwalt2034b912009-08-12 16:08:25 -0700298 mNetRequestersPids[i] = new ArrayList();
299 }
300
301 mFeatureUsers = new ArrayList();
302
Robert Greenwaltec05b3c2009-10-30 14:17:42 -0700303 mNumDnsEntries = 0;
304
305 mTestMode = SystemProperties.get("cm.test.mode").equals("true")
306 && SystemProperties.get("ro.build.type").equals("eng");
The Android Open Source Project28527d22009-03-03 19:31:44 -0800307 /*
308 * Create the network state trackers for Wi-Fi and mobile
309 * data. Maybe this could be done with a factory class,
310 * but it's not clear that it's worth it, given that
311 * the number of different network types is not going
312 * to change very often.
313 */
Robert Greenwalt1b21f6c2010-02-23 18:58:05 -0800314 boolean noMobileData = !getMobileDataEnabled();
Robert Greenwaltec05b3c2009-10-30 14:17:42 -0700315 for (int netType : mPriorityList) {
316 switch (mNetAttributes[netType].mRadio) {
317 case ConnectivityManager.TYPE_WIFI:
Joe Onoratoc2386bb2010-02-26 18:56:32 -0800318 if (DBG) Slog.v(TAG, "Starting Wifi Service.");
Wink Saville7fabfa22010-08-13 16:11:42 -0700319 WifiStateTracker wst = new WifiStateTracker();
Irfan Sheriff25be0762010-07-28 09:35:20 -0700320 WifiService wifiService = new WifiService(context);
Robert Greenwaltec05b3c2009-10-30 14:17:42 -0700321 ServiceManager.addService(Context.WIFI_SERVICE, wifiService);
Irfan Sheriff25be0762010-07-28 09:35:20 -0700322 wifiService.checkAndStartWifi();
Robert Greenwaltec05b3c2009-10-30 14:17:42 -0700323 mNetTrackers[ConnectivityManager.TYPE_WIFI] = wst;
Wink Saville7fabfa22010-08-13 16:11:42 -0700324 wst.startMonitoring(context, mHandler);
The Android Open Source Project28527d22009-03-03 19:31:44 -0800325
Irfan Sheriff653e2a22010-06-07 09:03:04 -0700326 //TODO: as part of WWS refactor, create only when needed
Irfan Sheriff25be0762010-07-28 09:35:20 -0700327 mWifiWatchdogService = new WifiWatchdogService(context);
Irfan Sheriff653e2a22010-06-07 09:03:04 -0700328
Robert Greenwaltec05b3c2009-10-30 14:17:42 -0700329 break;
330 case ConnectivityManager.TYPE_MOBILE:
Wink Saville7fabfa22010-08-13 16:11:42 -0700331 mNetTrackers[netType] = new MobileDataStateTracker(netType,
332 mNetAttributes[netType].mName);
333 mNetTrackers[netType].startMonitoring(context, mHandler);
Robert Greenwalt1b21f6c2010-02-23 18:58:05 -0800334 if (noMobileData) {
Joe Onoratoc2386bb2010-02-26 18:56:32 -0800335 if (DBG) Slog.d(TAG, "tearing down Mobile networks due to setting");
Robert Greenwalt1b21f6c2010-02-23 18:58:05 -0800336 mNetTrackers[netType].teardown();
337 }
Robert Greenwaltec05b3c2009-10-30 14:17:42 -0700338 break;
339 default:
Joe Onoratoc2386bb2010-02-26 18:56:32 -0800340 Slog.e(TAG, "Trying to create a DataStateTracker for an unknown radio type " +
Robert Greenwaltec05b3c2009-10-30 14:17:42 -0700341 mNetAttributes[netType].mRadio);
342 continue;
343 }
344 }
Robert Greenwalt0c4828c2010-01-26 11:40:34 -0800345
Robert Greenwaltc0b6c602010-03-11 15:03:08 -0800346 mTethering = new Tethering(mContext, mHandler.getLooper());
Robert Greenwaltf1b66e12010-02-25 12:29:30 -0800347 mTetheringConfigValid = (((mNetTrackers[ConnectivityManager.TYPE_MOBILE_DUN] != null) ||
348 !mTethering.isDunRequired()) &&
349 (mTethering.getTetherableUsbRegexs().length != 0 ||
Danica Chang96567052010-08-11 14:54:43 -0700350 mTethering.getTetherableWifiRegexs().length != 0 ||
351 mTethering.getTetherableBluetoothRegexs().length != 0) &&
Robert Greenwaltf1b66e12010-02-25 12:29:30 -0800352 mTethering.getUpstreamIfaceRegexs().length != 0);
353
The Android Open Source Project28527d22009-03-03 19:31:44 -0800354 }
355
Robert Greenwaltec05b3c2009-10-30 14:17:42 -0700356
The Android Open Source Project28527d22009-03-03 19:31:44 -0800357 /**
Robert Greenwalt0659da32009-07-16 17:21:39 -0700358 * Sets the preferred network.
The Android Open Source Project28527d22009-03-03 19:31:44 -0800359 * @param preference the new preference
360 */
361 public synchronized void setNetworkPreference(int preference) {
362 enforceChangePermission();
Robert Greenwalt2034b912009-08-12 16:08:25 -0700363 if (ConnectivityManager.isNetworkTypeValid(preference) &&
Robert Greenwaltec05b3c2009-10-30 14:17:42 -0700364 mNetAttributes[preference] != null &&
Robert Greenwalt2034b912009-08-12 16:08:25 -0700365 mNetAttributes[preference].isDefault()) {
The Android Open Source Project28527d22009-03-03 19:31:44 -0800366 if (mNetworkPreference != preference) {
367 persistNetworkPreference(preference);
368 mNetworkPreference = preference;
369 enforcePreference();
370 }
371 }
372 }
373
374 public int getNetworkPreference() {
375 enforceAccessPermission();
376 return mNetworkPreference;
377 }
378
379 private void persistNetworkPreference(int networkPreference) {
380 final ContentResolver cr = mContext.getContentResolver();
Robert Greenwalt0659da32009-07-16 17:21:39 -0700381 Settings.Secure.putInt(cr, Settings.Secure.NETWORK_PREFERENCE,
382 networkPreference);
The Android Open Source Project28527d22009-03-03 19:31:44 -0800383 }
Robert Greenwalt0659da32009-07-16 17:21:39 -0700384
The Android Open Source Project28527d22009-03-03 19:31:44 -0800385 private int getPersistedNetworkPreference() {
386 final ContentResolver cr = mContext.getContentResolver();
387
388 final int networkPrefSetting = Settings.Secure
389 .getInt(cr, Settings.Secure.NETWORK_PREFERENCE, -1);
390 if (networkPrefSetting != -1) {
391 return networkPrefSetting;
392 }
393
394 return ConnectivityManager.DEFAULT_NETWORK_PREFERENCE;
395 }
Robert Greenwalt0659da32009-07-16 17:21:39 -0700396
The Android Open Source Project28527d22009-03-03 19:31:44 -0800397 /**
Robert Greenwalt0659da32009-07-16 17:21:39 -0700398 * Make the state of network connectivity conform to the preference settings
The Android Open Source Project28527d22009-03-03 19:31:44 -0800399 * In this method, we only tear down a non-preferred network. Establishing
400 * a connection to the preferred network is taken care of when we handle
401 * the disconnect event from the non-preferred network
402 * (see {@link #handleDisconnect(NetworkInfo)}).
403 */
404 private void enforcePreference() {
Robert Greenwalt2034b912009-08-12 16:08:25 -0700405 if (mNetTrackers[mNetworkPreference].getNetworkInfo().isConnected())
The Android Open Source Project28527d22009-03-03 19:31:44 -0800406 return;
407
Robert Greenwalt2034b912009-08-12 16:08:25 -0700408 if (!mNetTrackers[mNetworkPreference].isAvailable())
409 return;
The Android Open Source Project28527d22009-03-03 19:31:44 -0800410
Robert Greenwalt2034b912009-08-12 16:08:25 -0700411 for (int t=0; t <= ConnectivityManager.MAX_RADIO_TYPE; t++) {
Robert Greenwaltec05b3c2009-10-30 14:17:42 -0700412 if (t != mNetworkPreference && mNetTrackers[t] != null &&
Robert Greenwalt2034b912009-08-12 16:08:25 -0700413 mNetTrackers[t].getNetworkInfo().isConnected()) {
Robert Greenwaltf3f045b2009-08-20 15:25:14 -0700414 if (DBG) {
Joe Onoratoc2386bb2010-02-26 18:56:32 -0800415 Slog.d(TAG, "tearing down " +
Robert Greenwaltf3f045b2009-08-20 15:25:14 -0700416 mNetTrackers[t].getNetworkInfo() +
417 " in enforcePreference");
418 }
Robert Greenwalt2034b912009-08-12 16:08:25 -0700419 teardown(mNetTrackers[t]);
The Android Open Source Project28527d22009-03-03 19:31:44 -0800420 }
421 }
422 }
423
424 private boolean teardown(NetworkStateTracker netTracker) {
425 if (netTracker.teardown()) {
426 netTracker.setTeardownRequested(true);
427 return true;
428 } else {
429 return false;
430 }
431 }
432
433 /**
434 * Return NetworkInfo for the active (i.e., connected) network interface.
435 * It is assumed that at most one network is active at a time. If more
436 * than one is active, it is indeterminate which will be returned.
Robert Greenwalt0659da32009-07-16 17:21:39 -0700437 * @return the info for the active network, or {@code null} if none is
438 * active
The Android Open Source Project28527d22009-03-03 19:31:44 -0800439 */
440 public NetworkInfo getActiveNetworkInfo() {
441 enforceAccessPermission();
Robert Greenwalt2034b912009-08-12 16:08:25 -0700442 for (int type=0; type <= ConnectivityManager.MAX_NETWORK_TYPE; type++) {
Robert Greenwaltec05b3c2009-10-30 14:17:42 -0700443 if (mNetAttributes[type] == null || !mNetAttributes[type].isDefault()) {
Robert Greenwalt2034b912009-08-12 16:08:25 -0700444 continue;
445 }
446 NetworkStateTracker t = mNetTrackers[type];
The Android Open Source Project28527d22009-03-03 19:31:44 -0800447 NetworkInfo info = t.getNetworkInfo();
448 if (info.isConnected()) {
Joe Onoratoc2386bb2010-02-26 18:56:32 -0800449 if (DBG && type != mActiveDefaultNetwork) Slog.e(TAG,
Robert Greenwalt2034b912009-08-12 16:08:25 -0700450 "connected default network is not " +
451 "mActiveDefaultNetwork!");
The Android Open Source Project28527d22009-03-03 19:31:44 -0800452 return info;
453 }
454 }
455 return null;
456 }
457
458 public NetworkInfo getNetworkInfo(int networkType) {
459 enforceAccessPermission();
460 if (ConnectivityManager.isNetworkTypeValid(networkType)) {
461 NetworkStateTracker t = mNetTrackers[networkType];
462 if (t != null)
463 return t.getNetworkInfo();
464 }
465 return null;
466 }
467
468 public NetworkInfo[] getAllNetworkInfo() {
469 enforceAccessPermission();
Robert Greenwaltec05b3c2009-10-30 14:17:42 -0700470 NetworkInfo[] result = new NetworkInfo[mNetworksDefined];
The Android Open Source Project28527d22009-03-03 19:31:44 -0800471 int i = 0;
472 for (NetworkStateTracker t : mNetTrackers) {
Robert Greenwaltec05b3c2009-10-30 14:17:42 -0700473 if(t != null) result[i++] = t.getNetworkInfo();
The Android Open Source Project28527d22009-03-03 19:31:44 -0800474 }
475 return result;
476 }
477
478 public boolean setRadios(boolean turnOn) {
479 boolean result = true;
480 enforceChangePermission();
481 for (NetworkStateTracker t : mNetTrackers) {
Robert Greenwaltec05b3c2009-10-30 14:17:42 -0700482 if (t != null) result = t.setRadio(turnOn) && result;
The Android Open Source Project28527d22009-03-03 19:31:44 -0800483 }
484 return result;
485 }
486
487 public boolean setRadio(int netType, boolean turnOn) {
488 enforceChangePermission();
489 if (!ConnectivityManager.isNetworkTypeValid(netType)) {
490 return false;
491 }
492 NetworkStateTracker tracker = mNetTrackers[netType];
493 return tracker != null && tracker.setRadio(turnOn);
494 }
495
Robert Greenwaltaffc3a12009-09-27 17:27:04 -0700496 /**
497 * Used to notice when the calling process dies so we can self-expire
498 *
499 * Also used to know if the process has cleaned up after itself when
500 * our auto-expire timer goes off. The timer has a link to an object.
501 *
502 */
Robert Greenwalt2034b912009-08-12 16:08:25 -0700503 private class FeatureUser implements IBinder.DeathRecipient {
504 int mNetworkType;
505 String mFeature;
506 IBinder mBinder;
507 int mPid;
508 int mUid;
Robert Greenwalt3eeb6032009-12-21 18:24:07 -0800509 long mCreateTime;
Robert Greenwalt2034b912009-08-12 16:08:25 -0700510
511 FeatureUser(int type, String feature, IBinder binder) {
512 super();
513 mNetworkType = type;
514 mFeature = feature;
515 mBinder = binder;
516 mPid = getCallingPid();
517 mUid = getCallingUid();
Robert Greenwalt3eeb6032009-12-21 18:24:07 -0800518 mCreateTime = System.currentTimeMillis();
Robert Greenwaltaffc3a12009-09-27 17:27:04 -0700519
Robert Greenwalt2034b912009-08-12 16:08:25 -0700520 try {
521 mBinder.linkToDeath(this, 0);
522 } catch (RemoteException e) {
523 binderDied();
524 }
525 }
526
527 void unlinkDeathRecipient() {
528 mBinder.unlinkToDeath(this, 0);
529 }
530
531 public void binderDied() {
Joe Onoratoc2386bb2010-02-26 18:56:32 -0800532 Slog.d(TAG, "ConnectivityService FeatureUser binderDied(" +
Robert Greenwalt3eeb6032009-12-21 18:24:07 -0800533 mNetworkType + ", " + mFeature + ", " + mBinder + "), created " +
534 (System.currentTimeMillis() - mCreateTime) + " mSec ago");
Robert Greenwaltaffc3a12009-09-27 17:27:04 -0700535 stopUsingNetworkFeature(this, false);
Robert Greenwalt2034b912009-08-12 16:08:25 -0700536 }
537
Robert Greenwaltaffc3a12009-09-27 17:27:04 -0700538 public void expire() {
Joe Onoratoc2386bb2010-02-26 18:56:32 -0800539 Slog.d(TAG, "ConnectivityService FeatureUser expire(" +
Robert Greenwalt3eeb6032009-12-21 18:24:07 -0800540 mNetworkType + ", " + mFeature + ", " + mBinder +"), created " +
541 (System.currentTimeMillis() - mCreateTime) + " mSec ago");
Robert Greenwaltaffc3a12009-09-27 17:27:04 -0700542 stopUsingNetworkFeature(this, false);
543 }
Robert Greenwalt3eeb6032009-12-21 18:24:07 -0800544
545 public String toString() {
546 return "FeatureUser("+mNetworkType+","+mFeature+","+mPid+","+mUid+"), created " +
547 (System.currentTimeMillis() - mCreateTime) + " mSec ago";
548 }
Robert Greenwalt2034b912009-08-12 16:08:25 -0700549 }
550
Robert Greenwaltaffc3a12009-09-27 17:27:04 -0700551 // javadoc from interface
Robert Greenwalt2034b912009-08-12 16:08:25 -0700552 public int startUsingNetworkFeature(int networkType, String feature,
553 IBinder binder) {
554 if (DBG) {
Joe Onoratoc2386bb2010-02-26 18:56:32 -0800555 Slog.d(TAG, "startUsingNetworkFeature for net " + networkType +
Robert Greenwalt2034b912009-08-12 16:08:25 -0700556 ": " + feature);
557 }
The Android Open Source Project28527d22009-03-03 19:31:44 -0800558 enforceChangePermission();
Robert Greenwaltec05b3c2009-10-30 14:17:42 -0700559 if (!ConnectivityManager.isNetworkTypeValid(networkType) ||
560 mNetAttributes[networkType] == null) {
Robert Greenwalt2034b912009-08-12 16:08:25 -0700561 return Phone.APN_REQUEST_FAILED;
The Android Open Source Project28527d22009-03-03 19:31:44 -0800562 }
Robert Greenwalt2034b912009-08-12 16:08:25 -0700563
Robert Greenwaltaffc3a12009-09-27 17:27:04 -0700564 FeatureUser f = new FeatureUser(networkType, feature, binder);
Robert Greenwalt2034b912009-08-12 16:08:25 -0700565
566 // TODO - move this into the MobileDataStateTracker
567 int usedNetworkType = networkType;
568 if(networkType == ConnectivityManager.TYPE_MOBILE) {
Robert Greenwalt1b21f6c2010-02-23 18:58:05 -0800569 if (!getMobileDataEnabled()) {
Joe Onoratoc2386bb2010-02-26 18:56:32 -0800570 if (DBG) Slog.d(TAG, "requested special network with data disabled - rejected");
Robert Greenwalt1b21f6c2010-02-23 18:58:05 -0800571 return Phone.APN_TYPE_NOT_AVAILABLE;
572 }
Robert Greenwalt2034b912009-08-12 16:08:25 -0700573 if (TextUtils.equals(feature, Phone.FEATURE_ENABLE_MMS)) {
574 usedNetworkType = ConnectivityManager.TYPE_MOBILE_MMS;
575 } else if (TextUtils.equals(feature, Phone.FEATURE_ENABLE_SUPL)) {
576 usedNetworkType = ConnectivityManager.TYPE_MOBILE_SUPL;
577 } else if (TextUtils.equals(feature, Phone.FEATURE_ENABLE_DUN)) {
578 usedNetworkType = ConnectivityManager.TYPE_MOBILE_DUN;
579 } else if (TextUtils.equals(feature, Phone.FEATURE_ENABLE_HIPRI)) {
580 usedNetworkType = ConnectivityManager.TYPE_MOBILE_HIPRI;
581 }
582 }
583 NetworkStateTracker network = mNetTrackers[usedNetworkType];
584 if (network != null) {
585 if (usedNetworkType != networkType) {
586 Integer currentPid = new Integer(getCallingPid());
587
588 NetworkStateTracker radio = mNetTrackers[networkType];
589 NetworkInfo ni = network.getNetworkInfo();
590
591 if (ni.isAvailable() == false) {
Joe Onoratoc2386bb2010-02-26 18:56:32 -0800592 if (DBG) Slog.d(TAG, "special network not available");
Robert Greenwalt2034b912009-08-12 16:08:25 -0700593 return Phone.APN_TYPE_NOT_AVAILABLE;
594 }
595
Robert Greenwaltaffc3a12009-09-27 17:27:04 -0700596 synchronized(this) {
597 mFeatureUsers.add(f);
598 if (!mNetRequestersPids[usedNetworkType].contains(currentPid)) {
599 // this gets used for per-pid dns when connected
600 mNetRequestersPids[usedNetworkType].add(currentPid);
601 }
Robert Greenwalt2034b912009-08-12 16:08:25 -0700602 }
Robert Greenwaltaffc3a12009-09-27 17:27:04 -0700603 mHandler.sendMessageDelayed(mHandler.obtainMessage(
604 NetworkStateTracker.EVENT_RESTORE_DEFAULT_NETWORK,
605 f), getRestoreDefaultNetworkDelay());
606
Robert Greenwalt2034b912009-08-12 16:08:25 -0700607
Robert Greenwalta52c75a2009-08-19 20:19:33 -0700608 if ((ni.isConnectedOrConnecting() == true) &&
609 !network.isTeardownRequested()) {
Robert Greenwalt2034b912009-08-12 16:08:25 -0700610 if (ni.isConnected() == true) {
611 // add the pid-specific dns
Robert Greenwalt3afbead2010-07-23 15:46:26 -0700612 handleDnsConfigurationChange(networkType);
Joe Onoratoc2386bb2010-02-26 18:56:32 -0800613 if (DBG) Slog.d(TAG, "special network already active");
Robert Greenwalt2034b912009-08-12 16:08:25 -0700614 return Phone.APN_ALREADY_ACTIVE;
615 }
Joe Onoratoc2386bb2010-02-26 18:56:32 -0800616 if (DBG) Slog.d(TAG, "special network already connecting");
Robert Greenwalt2034b912009-08-12 16:08:25 -0700617 return Phone.APN_REQUEST_STARTED;
618 }
619
620 // check if the radio in play can make another contact
621 // assume if cannot for now
622
Joe Onoratoc2386bb2010-02-26 18:56:32 -0800623 if (DBG) Slog.d(TAG, "reconnecting to special network");
Robert Greenwalt2034b912009-08-12 16:08:25 -0700624 network.reconnect();
625 return Phone.APN_REQUEST_STARTED;
626 } else {
Robert Greenwaltd391e892010-05-18 10:52:51 -0700627 return -1;
Robert Greenwalt2034b912009-08-12 16:08:25 -0700628 }
629 }
630 return Phone.APN_TYPE_NOT_AVAILABLE;
The Android Open Source Project28527d22009-03-03 19:31:44 -0800631 }
632
Robert Greenwaltaffc3a12009-09-27 17:27:04 -0700633 // javadoc from interface
The Android Open Source Project28527d22009-03-03 19:31:44 -0800634 public int stopUsingNetworkFeature(int networkType, String feature) {
Robert Greenwalt28f43012009-10-06 17:52:40 -0700635 enforceChangePermission();
636
Robert Greenwaltaffc3a12009-09-27 17:27:04 -0700637 int pid = getCallingPid();
638 int uid = getCallingUid();
639
640 FeatureUser u = null;
641 boolean found = false;
642
643 synchronized(this) {
644 for (int i = 0; i < mFeatureUsers.size() ; i++) {
645 u = (FeatureUser)mFeatureUsers.get(i);
646 if (uid == u.mUid && pid == u.mPid &&
647 networkType == u.mNetworkType &&
648 TextUtils.equals(feature, u.mFeature)) {
649 found = true;
650 break;
651 }
652 }
653 }
654 if (found && u != null) {
655 // stop regardless of how many other time this proc had called start
656 return stopUsingNetworkFeature(u, true);
657 } else {
658 // none found!
Joe Onoratoc2386bb2010-02-26 18:56:32 -0800659 if (DBG) Slog.d(TAG, "ignoring stopUsingNetworkFeature - not a live request");
Robert Greenwaltaffc3a12009-09-27 17:27:04 -0700660 return 1;
661 }
Robert Greenwalt2034b912009-08-12 16:08:25 -0700662 }
663
Robert Greenwaltaffc3a12009-09-27 17:27:04 -0700664 private int stopUsingNetworkFeature(FeatureUser u, boolean ignoreDups) {
665 int networkType = u.mNetworkType;
666 String feature = u.mFeature;
667 int pid = u.mPid;
668 int uid = u.mUid;
669
670 NetworkStateTracker tracker = null;
671 boolean callTeardown = false; // used to carry our decision outside of sync block
672
Robert Greenwalt2034b912009-08-12 16:08:25 -0700673 if (DBG) {
Joe Onoratoc2386bb2010-02-26 18:56:32 -0800674 Slog.d(TAG, "stopUsingNetworkFeature for net " + networkType +
Robert Greenwalt2034b912009-08-12 16:08:25 -0700675 ": " + feature);
676 }
Robert Greenwalt28f43012009-10-06 17:52:40 -0700677
The Android Open Source Project28527d22009-03-03 19:31:44 -0800678 if (!ConnectivityManager.isNetworkTypeValid(networkType)) {
679 return -1;
680 }
Robert Greenwalt2034b912009-08-12 16:08:25 -0700681
Robert Greenwaltaffc3a12009-09-27 17:27:04 -0700682 // need to link the mFeatureUsers list with the mNetRequestersPids state in this
683 // sync block
684 synchronized(this) {
685 // check if this process still has an outstanding start request
686 if (!mFeatureUsers.contains(u)) {
Robert Greenwalt46ceefa2010-03-10 16:10:43 -0800687 if (DBG) Slog.d(TAG, "ignoring - this process has no outstanding requests");
Robert Greenwalt2034b912009-08-12 16:08:25 -0700688 return 1;
689 }
Robert Greenwaltaffc3a12009-09-27 17:27:04 -0700690 u.unlinkDeathRecipient();
691 mFeatureUsers.remove(mFeatureUsers.indexOf(u));
692 // If we care about duplicate requests, check for that here.
693 //
694 // This is done to support the extension of a request - the app
695 // can request we start the network feature again and renew the
696 // auto-shutoff delay. Normal "stop" calls from the app though
697 // do not pay attention to duplicate requests - in effect the
698 // API does not refcount and a single stop will counter multiple starts.
699 if (ignoreDups == false) {
700 for (int i = 0; i < mFeatureUsers.size() ; i++) {
701 FeatureUser x = (FeatureUser)mFeatureUsers.get(i);
702 if (x.mUid == u.mUid && x.mPid == u.mPid &&
703 x.mNetworkType == u.mNetworkType &&
704 TextUtils.equals(x.mFeature, u.mFeature)) {
Joe Onoratoc2386bb2010-02-26 18:56:32 -0800705 if (DBG) Slog.d(TAG, "ignoring stopUsingNetworkFeature as dup is found");
Robert Greenwaltaffc3a12009-09-27 17:27:04 -0700706 return 1;
707 }
708 }
Robert Greenwalt2034b912009-08-12 16:08:25 -0700709 }
Robert Greenwaltaffc3a12009-09-27 17:27:04 -0700710
711 // TODO - move to MobileDataStateTracker
712 int usedNetworkType = networkType;
713 if (networkType == ConnectivityManager.TYPE_MOBILE) {
714 if (TextUtils.equals(feature, Phone.FEATURE_ENABLE_MMS)) {
715 usedNetworkType = ConnectivityManager.TYPE_MOBILE_MMS;
716 } else if (TextUtils.equals(feature, Phone.FEATURE_ENABLE_SUPL)) {
717 usedNetworkType = ConnectivityManager.TYPE_MOBILE_SUPL;
718 } else if (TextUtils.equals(feature, Phone.FEATURE_ENABLE_DUN)) {
719 usedNetworkType = ConnectivityManager.TYPE_MOBILE_DUN;
720 } else if (TextUtils.equals(feature, Phone.FEATURE_ENABLE_HIPRI)) {
721 usedNetworkType = ConnectivityManager.TYPE_MOBILE_HIPRI;
722 }
723 }
724 tracker = mNetTrackers[usedNetworkType];
Robert Greenwaltec05b3c2009-10-30 14:17:42 -0700725 if (tracker == null) {
Robert Greenwalt46ceefa2010-03-10 16:10:43 -0800726 if (DBG) Slog.d(TAG, "ignoring - no known tracker for net type " + usedNetworkType);
Robert Greenwaltec05b3c2009-10-30 14:17:42 -0700727 return -1;
728 }
729 if (usedNetworkType != networkType) {
Robert Greenwaltaffc3a12009-09-27 17:27:04 -0700730 Integer currentPid = new Integer(pid);
Robert Greenwaltaffc3a12009-09-27 17:27:04 -0700731 mNetRequestersPids[usedNetworkType].remove(currentPid);
Robert Greenwalt0ca68a02009-12-17 14:54:59 -0800732 reassessPidDns(pid, true);
Robert Greenwaltaffc3a12009-09-27 17:27:04 -0700733 if (mNetRequestersPids[usedNetworkType].size() != 0) {
Joe Onoratoc2386bb2010-02-26 18:56:32 -0800734 if (DBG) Slog.d(TAG, "not tearing down special network - " +
Robert Greenwaltaffc3a12009-09-27 17:27:04 -0700735 "others still using it");
736 return 1;
737 }
738 callTeardown = true;
739 }
740 }
Robert Greenwalt46ceefa2010-03-10 16:10:43 -0800741 if (DBG) Slog.d(TAG, "Doing network teardown");
Robert Greenwaltaffc3a12009-09-27 17:27:04 -0700742 if (callTeardown) {
743 tracker.teardown();
Robert Greenwalt2034b912009-08-12 16:08:25 -0700744 return 1;
745 } else {
Robert Greenwaltd391e892010-05-18 10:52:51 -0700746 return -1;
Robert Greenwalt2034b912009-08-12 16:08:25 -0700747 }
The Android Open Source Project28527d22009-03-03 19:31:44 -0800748 }
749
750 /**
Robert Greenwalt7fe44cb2010-08-27 09:24:29 -0700751 * @deprecated use requestRouteToHostAddress instead
752 *
The Android Open Source Project28527d22009-03-03 19:31:44 -0800753 * Ensure that a network route exists to deliver traffic to the specified
754 * host via the specified network interface.
Robert Greenwalt0659da32009-07-16 17:21:39 -0700755 * @param networkType the type of the network over which traffic to the
756 * specified host is to be routed
757 * @param hostAddress the IP address of the host to which the route is
758 * desired
Robert Greenwalta7dfbd32010-06-15 15:43:39 -0700759 * todo - deprecate (only v4!)
The Android Open Source Project28527d22009-03-03 19:31:44 -0800760 * @return {@code true} on success, {@code false} on failure
761 */
762 public boolean requestRouteToHost(int networkType, int hostAddress) {
Robert Greenwalt7fe44cb2010-08-27 09:24:29 -0700763 InetAddress inetAddress = NetworkUtils.intToInetAddress(hostAddress);
764
765 if (inetAddress == null) {
766 return false;
767 }
768
769 return requestRouteToHostAddress(networkType, inetAddress.getAddress());
770 }
771
772 /**
773 * Ensure that a network route exists to deliver traffic to the specified
774 * host via the specified network interface.
775 * @param networkType the type of the network over which traffic to the
776 * specified host is to be routed
777 * @param hostAddress the IP address of the host to which the route is
778 * desired
779 * @return {@code true} on success, {@code false} on failure
780 */
781 public boolean requestRouteToHostAddress(int networkType, byte[] hostAddress) {
The Android Open Source Project28527d22009-03-03 19:31:44 -0800782 enforceChangePermission();
783 if (!ConnectivityManager.isNetworkTypeValid(networkType)) {
784 return false;
785 }
786 NetworkStateTracker tracker = mNetTrackers[networkType];
Robert Greenwalt4666ed02009-09-10 15:06:20 -0700787
Robert Greenwaltec05b3c2009-10-30 14:17:42 -0700788 if (tracker == null || !tracker.getNetworkInfo().isConnected() ||
789 tracker.isTeardownRequested()) {
Robert Greenwalt4666ed02009-09-10 15:06:20 -0700790 if (DBG) {
Robert Greenwalt7fe44cb2010-08-27 09:24:29 -0700791 Slog.d(TAG, "requestRouteToHostAddress on down network " +
792 "(" + networkType + ") - dropped");
Robert Greenwalt4666ed02009-09-10 15:06:20 -0700793 }
794 return false;
The Android Open Source Project28527d22009-03-03 19:31:44 -0800795 }
Robert Greenwalta7dfbd32010-06-15 15:43:39 -0700796 try {
Robert Greenwalt7fe44cb2010-08-27 09:24:29 -0700797 InetAddress addr = InetAddress.getByAddress(hostAddress);
Robert Greenwalta7dfbd32010-06-15 15:43:39 -0700798 return addHostRoute(tracker, addr);
799 } catch (UnknownHostException e) {}
800 return false;
Irfan Sheriff7f132d92010-06-09 15:39:36 -0700801 }
802
803 /**
804 * Ensure that a network route exists to deliver traffic to the specified
805 * host via the mobile data network.
806 * @param hostAddress the IP address of the host to which the route is desired,
807 * in network byte order.
Robert Greenwalta7dfbd32010-06-15 15:43:39 -0700808 * TODO - deprecate
Irfan Sheriff7f132d92010-06-09 15:39:36 -0700809 * @return {@code true} on success, {@code false} on failure
810 */
Robert Greenwalta7dfbd32010-06-15 15:43:39 -0700811 private boolean addHostRoute(NetworkStateTracker nt, InetAddress hostAddress) {
Irfan Sheriff7f132d92010-06-09 15:39:36 -0700812 if (nt.getNetworkInfo().getType() == ConnectivityManager.TYPE_WIFI) {
813 return false;
814 }
815
Robert Greenwalta7dfbd32010-06-15 15:43:39 -0700816 NetworkProperties p = nt.getNetworkProperties();
817 if (p == null) return false;
818 String interfaceName = p.getInterfaceName();
Irfan Sheriff7f132d92010-06-09 15:39:36 -0700819
820 if (DBG) {
Robert Greenwalta7dfbd32010-06-15 15:43:39 -0700821 Slog.d(TAG, "Requested host route to " + hostAddress + "(" + interfaceName + ")");
Irfan Sheriff7f132d92010-06-09 15:39:36 -0700822 }
Robert Greenwalta7dfbd32010-06-15 15:43:39 -0700823 if (interfaceName != null) {
Robert Greenwalt7fe44cb2010-08-27 09:24:29 -0700824 return NetworkUtils.addHostRoute(interfaceName, hostAddress, null);
Irfan Sheriff7f132d92010-06-09 15:39:36 -0700825 } else {
Robert Greenwalta7dfbd32010-06-15 15:43:39 -0700826 if (DBG) Slog.e(TAG, "addHostRoute failed due to null interface name");
Irfan Sheriff7f132d92010-06-09 15:39:36 -0700827 return false;
828 }
The Android Open Source Project28527d22009-03-03 19:31:44 -0800829 }
830
831 /**
832 * @see ConnectivityManager#getBackgroundDataSetting()
833 */
834 public boolean getBackgroundDataSetting() {
835 return Settings.Secure.getInt(mContext.getContentResolver(),
836 Settings.Secure.BACKGROUND_DATA, 1) == 1;
837 }
Robert Greenwalt0659da32009-07-16 17:21:39 -0700838
The Android Open Source Project28527d22009-03-03 19:31:44 -0800839 /**
840 * @see ConnectivityManager#setBackgroundDataSetting(boolean)
841 */
842 public void setBackgroundDataSetting(boolean allowBackgroundDataUsage) {
843 mContext.enforceCallingOrSelfPermission(
844 android.Manifest.permission.CHANGE_BACKGROUND_DATA_SETTING,
845 "ConnectivityService");
Robert Greenwalt0659da32009-07-16 17:21:39 -0700846
The Android Open Source Project28527d22009-03-03 19:31:44 -0800847 if (getBackgroundDataSetting() == allowBackgroundDataUsage) return;
848
849 Settings.Secure.putInt(mContext.getContentResolver(),
Robert Greenwalt0659da32009-07-16 17:21:39 -0700850 Settings.Secure.BACKGROUND_DATA,
851 allowBackgroundDataUsage ? 1 : 0);
852
The Android Open Source Project28527d22009-03-03 19:31:44 -0800853 Intent broadcast = new Intent(
854 ConnectivityManager.ACTION_BACKGROUND_DATA_SETTING_CHANGED);
855 mContext.sendBroadcast(broadcast);
Robert Greenwalt0659da32009-07-16 17:21:39 -0700856 }
857
Robert Greenwalt1b21f6c2010-02-23 18:58:05 -0800858 /**
859 * @see ConnectivityManager#getMobileDataEnabled()
860 */
861 public boolean getMobileDataEnabled() {
862 enforceAccessPermission();
863 boolean retVal = Settings.Secure.getInt(mContext.getContentResolver(),
864 Settings.Secure.MOBILE_DATA, 1) == 1;
Joe Onoratoc2386bb2010-02-26 18:56:32 -0800865 if (DBG) Slog.d(TAG, "getMobileDataEnabled returning " + retVal);
Robert Greenwalt1b21f6c2010-02-23 18:58:05 -0800866 return retVal;
867 }
868
869 /**
870 * @see ConnectivityManager#setMobileDataEnabled(boolean)
871 */
872 public synchronized void setMobileDataEnabled(boolean enabled) {
873 enforceChangePermission();
Joe Onoratoc2386bb2010-02-26 18:56:32 -0800874 if (DBG) Slog.d(TAG, "setMobileDataEnabled(" + enabled + ")");
Robert Greenwalt1b21f6c2010-02-23 18:58:05 -0800875
876 if (getMobileDataEnabled() == enabled) return;
877
878 Settings.Secure.putInt(mContext.getContentResolver(),
879 Settings.Secure.MOBILE_DATA, enabled ? 1 : 0);
880
881 if (enabled) {
882 if (mNetTrackers[ConnectivityManager.TYPE_MOBILE] != null) {
Joe Onoratoc2386bb2010-02-26 18:56:32 -0800883 if (DBG) Slog.d(TAG, "starting up " + mNetTrackers[ConnectivityManager.TYPE_MOBILE]);
Robert Greenwalt1b21f6c2010-02-23 18:58:05 -0800884 mNetTrackers[ConnectivityManager.TYPE_MOBILE].reconnect();
885 }
886 } else {
887 for (NetworkStateTracker nt : mNetTrackers) {
888 if (nt == null) continue;
889 int netType = nt.getNetworkInfo().getType();
890 if (mNetAttributes[netType].mRadio == ConnectivityManager.TYPE_MOBILE) {
Joe Onoratoc2386bb2010-02-26 18:56:32 -0800891 if (DBG) Slog.d(TAG, "tearing down " + nt);
Robert Greenwalt1b21f6c2010-02-23 18:58:05 -0800892 nt.teardown();
893 }
894 }
895 }
896 }
897
The Android Open Source Project28527d22009-03-03 19:31:44 -0800898 private int getNumConnectedNetworks() {
899 int numConnectedNets = 0;
900
901 for (NetworkStateTracker nt : mNetTrackers) {
Robert Greenwaltec05b3c2009-10-30 14:17:42 -0700902 if (nt != null && nt.getNetworkInfo().isConnected() &&
Robert Greenwalt0659da32009-07-16 17:21:39 -0700903 !nt.isTeardownRequested()) {
The Android Open Source Project28527d22009-03-03 19:31:44 -0800904 ++numConnectedNets;
905 }
906 }
907 return numConnectedNets;
908 }
909
910 private void enforceAccessPermission() {
Robert Greenwalt0659da32009-07-16 17:21:39 -0700911 mContext.enforceCallingOrSelfPermission(
912 android.Manifest.permission.ACCESS_NETWORK_STATE,
913 "ConnectivityService");
The Android Open Source Project28527d22009-03-03 19:31:44 -0800914 }
915
916 private void enforceChangePermission() {
Robert Greenwalt0659da32009-07-16 17:21:39 -0700917 mContext.enforceCallingOrSelfPermission(
918 android.Manifest.permission.CHANGE_NETWORK_STATE,
919 "ConnectivityService");
The Android Open Source Project28527d22009-03-03 19:31:44 -0800920 }
921
Robert Greenwalt0c4828c2010-01-26 11:40:34 -0800922 // TODO Make this a special check when it goes public
923 private void enforceTetherChangePermission() {
924 mContext.enforceCallingOrSelfPermission(
925 android.Manifest.permission.CHANGE_NETWORK_STATE,
926 "ConnectivityService");
927 }
928
Robert Greenwalt8e87f122010-02-11 18:18:40 -0800929 private void enforceTetherAccessPermission() {
930 mContext.enforceCallingOrSelfPermission(
931 android.Manifest.permission.ACCESS_NETWORK_STATE,
932 "ConnectivityService");
933 }
934
Robert Greenwalt93dc1042010-06-15 12:19:37 -0700935 private void enforceConnectivityInternalPermission() {
936 mContext.enforceCallingOrSelfPermission(
937 android.Manifest.permission.CONNECTIVITY_INTERNAL,
938 "ConnectivityService");
939 }
940
The Android Open Source Project28527d22009-03-03 19:31:44 -0800941 /**
Robert Greenwalt0659da32009-07-16 17:21:39 -0700942 * Handle a {@code DISCONNECTED} event. If this pertains to the non-active
943 * network, we ignore it. If it is for the active network, we send out a
944 * broadcast. But first, we check whether it might be possible to connect
945 * to a different network.
The Android Open Source Project28527d22009-03-03 19:31:44 -0800946 * @param info the {@code NetworkInfo} for the network
947 */
948 private void handleDisconnect(NetworkInfo info) {
949
Robert Greenwalt2034b912009-08-12 16:08:25 -0700950 int prevNetType = info.getType();
The Android Open Source Project28527d22009-03-03 19:31:44 -0800951
Robert Greenwalt2034b912009-08-12 16:08:25 -0700952 mNetTrackers[prevNetType].setTeardownRequested(false);
The Android Open Source Project28527d22009-03-03 19:31:44 -0800953 /*
954 * If the disconnected network is not the active one, then don't report
955 * this as a loss of connectivity. What probably happened is that we're
956 * getting the disconnect for a network that we explicitly disabled
957 * in accordance with network preference policies.
958 */
Robert Greenwalt2034b912009-08-12 16:08:25 -0700959 if (!mNetAttributes[prevNetType].isDefault()) {
960 List pids = mNetRequestersPids[prevNetType];
961 for (int i = 0; i<pids.size(); i++) {
962 Integer pid = (Integer)pids.get(i);
963 // will remove them because the net's no longer connected
964 // need to do this now as only now do we know the pids and
965 // can properly null things that are no longer referenced.
966 reassessPidDns(pid.intValue(), false);
The Android Open Source Project28527d22009-03-03 19:31:44 -0800967 }
The Android Open Source Project28527d22009-03-03 19:31:44 -0800968 }
969
The Android Open Source Project28527d22009-03-03 19:31:44 -0800970 Intent intent = new Intent(ConnectivityManager.CONNECTIVITY_ACTION);
971 intent.putExtra(ConnectivityManager.EXTRA_NETWORK_INFO, info);
972 if (info.isFailover()) {
973 intent.putExtra(ConnectivityManager.EXTRA_IS_FAILOVER, true);
974 info.setFailover(false);
975 }
976 if (info.getReason() != null) {
977 intent.putExtra(ConnectivityManager.EXTRA_REASON, info.getReason());
978 }
979 if (info.getExtraInfo() != null) {
Robert Greenwalt0659da32009-07-16 17:21:39 -0700980 intent.putExtra(ConnectivityManager.EXTRA_EXTRA_INFO,
981 info.getExtraInfo());
The Android Open Source Project28527d22009-03-03 19:31:44 -0800982 }
Robert Greenwalt2034b912009-08-12 16:08:25 -0700983
Robert Greenwalt3cc68d32010-01-25 17:54:29 -0800984 NetworkStateTracker newNet = null;
985 if (mNetAttributes[prevNetType].isDefault()) {
986 newNet = tryFailover(prevNetType);
987 if (newNet != null) {
988 NetworkInfo switchTo = newNet.getNetworkInfo();
989 intent.putExtra(ConnectivityManager.EXTRA_OTHER_NETWORK_INFO, switchTo);
990 } else {
991 intent.putExtra(ConnectivityManager.EXTRA_NO_CONNECTIVITY, true);
992 }
Robert Greenwaltf55ced92010-01-20 19:29:41 -0800993 }
994 // do this before we broadcast the change
Robert Greenwalt3afbead2010-07-23 15:46:26 -0700995 handleConnectivityChange(prevNetType);
Robert Greenwaltf55ced92010-01-20 19:29:41 -0800996
997 sendStickyBroadcast(intent);
998 /*
999 * If the failover network is already connected, then immediately send
1000 * out a followup broadcast indicating successful failover
1001 */
1002 if (newNet != null && newNet.getNetworkInfo().isConnected()) {
1003 sendConnectedBroadcast(newNet.getNetworkInfo());
1004 }
1005 }
1006
Robert Greenwalt3cc68d32010-01-25 17:54:29 -08001007 // returns null if no failover available
Robert Greenwaltf55ced92010-01-20 19:29:41 -08001008 private NetworkStateTracker tryFailover(int prevNetType) {
Robert Greenwalt2034b912009-08-12 16:08:25 -07001009 /*
1010 * If this is a default network, check if other defaults are available
1011 * or active
1012 */
1013 NetworkStateTracker newNet = null;
1014 if (mNetAttributes[prevNetType].isDefault()) {
Robert Greenwalt2034b912009-08-12 16:08:25 -07001015 if (mActiveDefaultNetwork == prevNetType) {
1016 mActiveDefaultNetwork = -1;
1017 }
1018
1019 int newType = -1;
1020 int newPriority = -1;
Robert Greenwalt72451bf2010-02-25 12:04:29 -08001021 boolean noMobileData = !getMobileDataEnabled();
Robert Greenwaltf55ced92010-01-20 19:29:41 -08001022 for (int checkType=0; checkType <= ConnectivityManager.MAX_NETWORK_TYPE; checkType++) {
Robert Greenwaltec05b3c2009-10-30 14:17:42 -07001023 if (checkType == prevNetType) continue;
1024 if (mNetAttributes[checkType] == null) continue;
Robert Greenwalt72451bf2010-02-25 12:04:29 -08001025 if (mNetAttributes[checkType].mRadio == ConnectivityManager.TYPE_MOBILE &&
1026 noMobileData) {
1027 if (DBG) {
Joe Onoratoc2386bb2010-02-26 18:56:32 -08001028 Slog.d(TAG, "not failing over to mobile type " + checkType +
Robert Greenwalt72451bf2010-02-25 12:04:29 -08001029 " because Mobile Data Disabled");
1030 }
1031 continue;
1032 }
Robert Greenwalt2034b912009-08-12 16:08:25 -07001033 if (mNetAttributes[checkType].isDefault()) {
1034 /* TODO - if we have multiple nets we could use
1035 * we may want to put more thought into which we choose
1036 */
1037 if (checkType == mNetworkPreference) {
1038 newType = checkType;
1039 break;
1040 }
Robert Greenwaltec05b3c2009-10-30 14:17:42 -07001041 if (mNetAttributes[checkType].mPriority > newPriority) {
Robert Greenwalt2034b912009-08-12 16:08:25 -07001042 newType = checkType;
Robert Greenwaltec05b3c2009-10-30 14:17:42 -07001043 newPriority = mNetAttributes[newType].mPriority;
Robert Greenwalt2034b912009-08-12 16:08:25 -07001044 }
The Android Open Source Project28527d22009-03-03 19:31:44 -08001045 }
1046 }
Robert Greenwalt2034b912009-08-12 16:08:25 -07001047
1048 if (newType != -1) {
1049 newNet = mNetTrackers[newType];
1050 /**
1051 * See if the other network is available to fail over to.
1052 * If is not available, we enable it anyway, so that it
1053 * will be able to connect when it does become available,
1054 * but we report a total loss of connectivity rather than
1055 * report that we are attempting to fail over.
1056 */
1057 if (newNet.isAvailable()) {
1058 NetworkInfo switchTo = newNet.getNetworkInfo();
1059 switchTo.setFailover(true);
Robert Greenwalta52c75a2009-08-19 20:19:33 -07001060 if (!switchTo.isConnectedOrConnecting() ||
1061 newNet.isTeardownRequested()) {
Robert Greenwalt2034b912009-08-12 16:08:25 -07001062 newNet.reconnect();
1063 }
1064 if (DBG) {
1065 if (switchTo.isConnected()) {
Joe Onoratoc2386bb2010-02-26 18:56:32 -08001066 Slog.v(TAG, "Switching to already connected " +
Robert Greenwalt2034b912009-08-12 16:08:25 -07001067 switchTo.getTypeName());
1068 } else {
Joe Onoratoc2386bb2010-02-26 18:56:32 -08001069 Slog.v(TAG, "Attempting to switch to " +
Robert Greenwalt2034b912009-08-12 16:08:25 -07001070 switchTo.getTypeName());
1071 }
1072 }
Robert Greenwalt2034b912009-08-12 16:08:25 -07001073 } else {
1074 newNet.reconnect();
Robert Greenwalt12984322010-03-09 14:55:08 -08001075 newNet = null; // not officially avail.. try anyway, but
1076 // report no failover
Robert Greenwalt2034b912009-08-12 16:08:25 -07001077 }
Robert Greenwalt2034b912009-08-12 16:08:25 -07001078 }
The Android Open Source Project28527d22009-03-03 19:31:44 -08001079 }
Robert Greenwalt2034b912009-08-12 16:08:25 -07001080
Robert Greenwaltf55ced92010-01-20 19:29:41 -08001081 return newNet;
The Android Open Source Project28527d22009-03-03 19:31:44 -08001082 }
1083
1084 private void sendConnectedBroadcast(NetworkInfo info) {
1085 Intent intent = new Intent(ConnectivityManager.CONNECTIVITY_ACTION);
1086 intent.putExtra(ConnectivityManager.EXTRA_NETWORK_INFO, info);
1087 if (info.isFailover()) {
1088 intent.putExtra(ConnectivityManager.EXTRA_IS_FAILOVER, true);
1089 info.setFailover(false);
1090 }
1091 if (info.getReason() != null) {
1092 intent.putExtra(ConnectivityManager.EXTRA_REASON, info.getReason());
1093 }
1094 if (info.getExtraInfo() != null) {
Robert Greenwalt0659da32009-07-16 17:21:39 -07001095 intent.putExtra(ConnectivityManager.EXTRA_EXTRA_INFO,
1096 info.getExtraInfo());
The Android Open Source Project28527d22009-03-03 19:31:44 -08001097 }
Mike Lockwoodfde2b762009-08-14 14:18:49 -04001098 sendStickyBroadcast(intent);
The Android Open Source Project28527d22009-03-03 19:31:44 -08001099 }
1100
1101 /**
1102 * Called when an attempt to fail over to another network has failed.
1103 * @param info the {@link NetworkInfo} for the failed network
1104 */
1105 private void handleConnectionFailure(NetworkInfo info) {
1106 mNetTrackers[info.getType()].setTeardownRequested(false);
The Android Open Source Project28527d22009-03-03 19:31:44 -08001107
Robert Greenwalt2034b912009-08-12 16:08:25 -07001108 String reason = info.getReason();
1109 String extraInfo = info.getExtraInfo();
Robert Greenwalt0659da32009-07-16 17:21:39 -07001110
Robert Greenwalt2034b912009-08-12 16:08:25 -07001111 if (DBG) {
1112 String reasonText;
1113 if (reason == null) {
1114 reasonText = ".";
1115 } else {
1116 reasonText = " (" + reason + ").";
The Android Open Source Project28527d22009-03-03 19:31:44 -08001117 }
Joe Onoratoc2386bb2010-02-26 18:56:32 -08001118 Slog.v(TAG, "Attempt to connect to " + info.getTypeName() +
Robert Greenwalt2034b912009-08-12 16:08:25 -07001119 " failed" + reasonText);
The Android Open Source Project28527d22009-03-03 19:31:44 -08001120 }
Robert Greenwalt2034b912009-08-12 16:08:25 -07001121
1122 Intent intent = new Intent(ConnectivityManager.CONNECTIVITY_ACTION);
1123 intent.putExtra(ConnectivityManager.EXTRA_NETWORK_INFO, info);
1124 if (getActiveNetworkInfo() == null) {
1125 intent.putExtra(ConnectivityManager.EXTRA_NO_CONNECTIVITY, true);
1126 }
1127 if (reason != null) {
1128 intent.putExtra(ConnectivityManager.EXTRA_REASON, reason);
1129 }
1130 if (extraInfo != null) {
1131 intent.putExtra(ConnectivityManager.EXTRA_EXTRA_INFO, extraInfo);
1132 }
1133 if (info.isFailover()) {
1134 intent.putExtra(ConnectivityManager.EXTRA_IS_FAILOVER, true);
1135 info.setFailover(false);
1136 }
Robert Greenwaltf55ced92010-01-20 19:29:41 -08001137
Robert Greenwalt3cc68d32010-01-25 17:54:29 -08001138 NetworkStateTracker newNet = null;
1139 if (mNetAttributes[info.getType()].isDefault()) {
1140 newNet = tryFailover(info.getType());
1141 if (newNet != null) {
1142 NetworkInfo switchTo = newNet.getNetworkInfo();
1143 intent.putExtra(ConnectivityManager.EXTRA_OTHER_NETWORK_INFO, switchTo);
1144 } else {
1145 intent.putExtra(ConnectivityManager.EXTRA_NO_CONNECTIVITY, true);
1146 }
Robert Greenwaltf55ced92010-01-20 19:29:41 -08001147 }
Robert Greenwalt3cc68d32010-01-25 17:54:29 -08001148
Mike Lockwoodfde2b762009-08-14 14:18:49 -04001149 sendStickyBroadcast(intent);
Robert Greenwaltf55ced92010-01-20 19:29:41 -08001150 /*
1151 * If the failover network is already connected, then immediately send
1152 * out a followup broadcast indicating successful failover
1153 */
1154 if (newNet != null && newNet.getNetworkInfo().isConnected()) {
1155 sendConnectedBroadcast(newNet.getNetworkInfo());
1156 }
Mike Lockwoodfde2b762009-08-14 14:18:49 -04001157 }
1158
1159 private void sendStickyBroadcast(Intent intent) {
1160 synchronized(this) {
Dianne Hackborna417ff82009-12-08 19:45:14 -08001161 if (!mSystemReady) {
1162 mInitialBroadcast = new Intent(intent);
Mike Lockwoodfde2b762009-08-14 14:18:49 -04001163 }
Dianne Hackborna417ff82009-12-08 19:45:14 -08001164 intent.addFlags(Intent.FLAG_RECEIVER_REGISTERED_ONLY_BEFORE_BOOT);
1165 mContext.sendStickyBroadcast(intent);
Mike Lockwoodfde2b762009-08-14 14:18:49 -04001166 }
1167 }
1168
1169 void systemReady() {
1170 synchronized(this) {
1171 mSystemReady = true;
Dianne Hackborna417ff82009-12-08 19:45:14 -08001172 if (mInitialBroadcast != null) {
1173 mContext.sendStickyBroadcast(mInitialBroadcast);
1174 mInitialBroadcast = null;
Mike Lockwoodfde2b762009-08-14 14:18:49 -04001175 }
1176 }
The Android Open Source Project28527d22009-03-03 19:31:44 -08001177 }
1178
1179 private void handleConnect(NetworkInfo info) {
Robert Greenwalt2034b912009-08-12 16:08:25 -07001180 int type = info.getType();
The Android Open Source Project28527d22009-03-03 19:31:44 -08001181
1182 // snapshot isFailover, because sendConnectedBroadcast() resets it
1183 boolean isFailover = info.isFailover();
Robert Greenwalt2034b912009-08-12 16:08:25 -07001184 NetworkStateTracker thisNet = mNetTrackers[type];
The Android Open Source Project28527d22009-03-03 19:31:44 -08001185
Robert Greenwalt2034b912009-08-12 16:08:25 -07001186 // if this is a default net and other default is running
1187 // kill the one not preferred
1188 if (mNetAttributes[type].isDefault()) {
Robert Greenwalt2034b912009-08-12 16:08:25 -07001189 if (mActiveDefaultNetwork != -1 && mActiveDefaultNetwork != type) {
1190 if ((type != mNetworkPreference &&
1191 mNetAttributes[mActiveDefaultNetwork].mPriority >
1192 mNetAttributes[type].mPriority) ||
1193 mNetworkPreference == mActiveDefaultNetwork) {
1194 // don't accept this one
Joe Onoratoc2386bb2010-02-26 18:56:32 -08001195 if (DBG) Slog.v(TAG, "Not broadcasting CONNECT_ACTION " +
Robert Greenwalt2034b912009-08-12 16:08:25 -07001196 "to torn down network " + info.getTypeName());
1197 teardown(thisNet);
1198 return;
1199 } else {
1200 // tear down the other
1201 NetworkStateTracker otherNet =
1202 mNetTrackers[mActiveDefaultNetwork];
Joe Onoratoc2386bb2010-02-26 18:56:32 -08001203 if (DBG) Slog.v(TAG, "Policy requires " +
Robert Greenwalt2034b912009-08-12 16:08:25 -07001204 otherNet.getNetworkInfo().getTypeName() +
1205 " teardown");
1206 if (!teardown(otherNet)) {
Joe Onoratoc2386bb2010-02-26 18:56:32 -08001207 Slog.e(TAG, "Network declined teardown request");
Robert Greenwalt2034b912009-08-12 16:08:25 -07001208 return;
1209 }
Robert Greenwalt93dc1042010-06-15 12:19:37 -07001210 }
1211 }
1212 synchronized (ConnectivityService.this) {
1213 // have a new default network, release the transition wakelock in a second
1214 // if it's held. The second pause is to allow apps to reconnect over the
1215 // new network
1216 if (mNetTransitionWakeLock.isHeld()) {
1217 mHandler.sendMessageDelayed(mHandler.obtainMessage(
1218 NetworkStateTracker.EVENT_CLEAR_NET_TRANSITION_WAKELOCK,
1219 mNetTransitionWakeLockSerialNumber, 0),
1220 1000);
Robert Greenwalt2034b912009-08-12 16:08:25 -07001221 }
1222 }
1223 mActiveDefaultNetwork = type;
1224 }
The Android Open Source Project28527d22009-03-03 19:31:44 -08001225 thisNet.setTeardownRequested(false);
Irfan Sheriff7f132d92010-06-09 15:39:36 -07001226 updateNetworkSettings(thisNet);
Robert Greenwalt3afbead2010-07-23 15:46:26 -07001227 handleConnectivityChange(type);
Robert Greenwalt2034b912009-08-12 16:08:25 -07001228 sendConnectedBroadcast(info);
The Android Open Source Project28527d22009-03-03 19:31:44 -08001229 }
1230
The Android Open Source Project28527d22009-03-03 19:31:44 -08001231 /**
Robert Greenwalt3afbead2010-07-23 15:46:26 -07001232 * After a change in the connectivity state of a network. We're mainly
1233 * concerned with making sure that the list of DNS servers is set up
1234 * according to which networks are connected, and ensuring that the
1235 * right routing table entries exist.
The Android Open Source Project28527d22009-03-03 19:31:44 -08001236 */
Robert Greenwalt3afbead2010-07-23 15:46:26 -07001237 private void handleConnectivityChange(int netType) {
The Android Open Source Project28527d22009-03-03 19:31:44 -08001238 /*
Robert Greenwalt2034b912009-08-12 16:08:25 -07001239 * If a non-default network is enabled, add the host routes that
Robert Greenwalt3afbead2010-07-23 15:46:26 -07001240 * will allow it's DNS servers to be accessed.
The Android Open Source Project28527d22009-03-03 19:31:44 -08001241 */
Robert Greenwalt3afbead2010-07-23 15:46:26 -07001242 handleDnsConfigurationChange(netType);
Robert Greenwalt2034b912009-08-12 16:08:25 -07001243
Robert Greenwalt3afbead2010-07-23 15:46:26 -07001244 if (mNetTrackers[netType].getNetworkInfo().isConnected()) {
1245 if (mNetAttributes[netType].isDefault()) {
1246 addDefaultRoute(mNetTrackers[netType]);
Robert Greenwalt2034b912009-08-12 16:08:25 -07001247 } else {
Robert Greenwalt3afbead2010-07-23 15:46:26 -07001248 addPrivateDnsRoutes(mNetTrackers[netType]);
1249 }
1250 } else {
1251 if (mNetAttributes[netType].isDefault()) {
1252 removeDefaultRoute(mNetTrackers[netType]);
1253 } else {
1254 removePrivateDnsRoutes(mNetTrackers[netType]);
Robert Greenwalt2034b912009-08-12 16:08:25 -07001255 }
The Android Open Source Project28527d22009-03-03 19:31:44 -08001256 }
1257 }
1258
Irfan Sheriff7f132d92010-06-09 15:39:36 -07001259 private void addPrivateDnsRoutes(NetworkStateTracker nt) {
Irfan Sheriff7f132d92010-06-09 15:39:36 -07001260 boolean privateDnsRouteSet = nt.isPrivateDnsRouteSet();
Robert Greenwalta7dfbd32010-06-15 15:43:39 -07001261 NetworkProperties p = nt.getNetworkProperties();
1262 if (p == null) return;
1263 String interfaceName = p.getInterfaceName();
Irfan Sheriff7f132d92010-06-09 15:39:36 -07001264
1265 if (DBG) {
1266 Slog.d(TAG, "addPrivateDnsRoutes for " + nt +
1267 "(" + interfaceName + ") - mPrivateDnsRouteSet = " + privateDnsRouteSet);
1268 }
Irfan Sheriff7f132d92010-06-09 15:39:36 -07001269 if (interfaceName != null && !privateDnsRouteSet) {
Robert Greenwalta7dfbd32010-06-15 15:43:39 -07001270 Collection<InetAddress> dnsList = p.getDnses();
1271 for (InetAddress dns : dnsList) {
1272 if (DBG) Slog.d(TAG, " adding " + dns);
Robert Greenwalt7fe44cb2010-08-27 09:24:29 -07001273 NetworkUtils.addHostRoute(interfaceName, dns, null);
Irfan Sheriff7f132d92010-06-09 15:39:36 -07001274 }
1275 nt.privateDnsRouteSet(true);
1276 }
1277 }
1278
1279 private void removePrivateDnsRoutes(NetworkStateTracker nt) {
1280 // TODO - we should do this explicitly but the NetUtils api doesnt
1281 // support this yet - must remove all. No worse than before
Robert Greenwalta7dfbd32010-06-15 15:43:39 -07001282 NetworkProperties p = nt.getNetworkProperties();
1283 if (p == null) return;
1284 String interfaceName = p.getInterfaceName();
Irfan Sheriff7f132d92010-06-09 15:39:36 -07001285 boolean privateDnsRouteSet = nt.isPrivateDnsRouteSet();
1286 if (interfaceName != null && privateDnsRouteSet) {
1287 if (DBG) {
1288 Slog.d(TAG, "removePrivateDnsRoutes for " + nt.getNetworkInfo().getTypeName() +
1289 " (" + interfaceName + ")");
1290 }
1291 NetworkUtils.removeHostRoutes(interfaceName);
1292 nt.privateDnsRouteSet(false);
1293 }
1294 }
1295
Irfan Sheriff7f132d92010-06-09 15:39:36 -07001296
1297 private void addDefaultRoute(NetworkStateTracker nt) {
Robert Greenwalta7dfbd32010-06-15 15:43:39 -07001298 NetworkProperties p = nt.getNetworkProperties();
1299 if (p == null) return;
1300 String interfaceName = p.getInterfaceName();
1301 InetAddress defaultGatewayAddr = p.getGateway();
Irfan Sheriff7f132d92010-06-09 15:39:36 -07001302
Robert Greenwalt3afbead2010-07-23 15:46:26 -07001303 if ((interfaceName != null) && (defaultGatewayAddr != null )) {
Robert Greenwalt7fe44cb2010-08-27 09:24:29 -07001304 if (!NetworkUtils.addDefaultRoute(interfaceName, defaultGatewayAddr) && DBG) {
Robert Greenwalta7dfbd32010-06-15 15:43:39 -07001305 NetworkInfo networkInfo = nt.getNetworkInfo();
Irfan Sheriff7f132d92010-06-09 15:39:36 -07001306 Slog.d(TAG, "addDefaultRoute for " + networkInfo.getTypeName() +
1307 " (" + interfaceName + "), GatewayAddr=" + defaultGatewayAddr);
1308 }
Irfan Sheriff7f132d92010-06-09 15:39:36 -07001309 }
1310 }
1311
1312
1313 public void removeDefaultRoute(NetworkStateTracker nt) {
Robert Greenwalta7dfbd32010-06-15 15:43:39 -07001314 NetworkProperties p = nt.getNetworkProperties();
1315 if (p == null) return;
1316 String interfaceName = p.getInterfaceName();
Irfan Sheriff7f132d92010-06-09 15:39:36 -07001317
Robert Greenwalt3afbead2010-07-23 15:46:26 -07001318 if (interfaceName != null) {
1319 if ((NetworkUtils.removeDefaultRoute(interfaceName) >= 0) && DBG) {
Robert Greenwalta7dfbd32010-06-15 15:43:39 -07001320 NetworkInfo networkInfo = nt.getNetworkInfo();
Irfan Sheriff7f132d92010-06-09 15:39:36 -07001321 Slog.d(TAG, "removeDefaultRoute for " + networkInfo.getTypeName() + " (" +
1322 interfaceName + ")");
1323 }
Irfan Sheriff7f132d92010-06-09 15:39:36 -07001324 }
1325 }
1326
1327 /**
1328 * Reads the network specific TCP buffer sizes from SystemProperties
1329 * net.tcp.buffersize.[default|wifi|umts|edge|gprs] and set them for system
1330 * wide use
1331 */
1332 public void updateNetworkSettings(NetworkStateTracker nt) {
1333 String key = nt.getTcpBufferSizesPropName();
1334 String bufferSizes = SystemProperties.get(key);
1335
1336 if (bufferSizes.length() == 0) {
1337 Slog.e(TAG, key + " not found in system properties. Using defaults");
1338
1339 // Setting to default values so we won't be stuck to previous values
1340 key = "net.tcp.buffersize.default";
1341 bufferSizes = SystemProperties.get(key);
1342 }
1343
1344 // Set values in kernel
1345 if (bufferSizes.length() != 0) {
1346 if (DBG) {
1347 Slog.v(TAG, "Setting TCP values: [" + bufferSizes
1348 + "] which comes from [" + key + "]");
1349 }
1350 setBufferSize(bufferSizes);
1351 }
1352 }
1353
1354 /**
1355 * Writes TCP buffer sizes to /sys/kernel/ipv4/tcp_[r/w]mem_[min/def/max]
1356 * which maps to /proc/sys/net/ipv4/tcp_rmem and tcpwmem
1357 *
1358 * @param bufferSizes in the format of "readMin, readInitial, readMax,
1359 * writeMin, writeInitial, writeMax"
1360 */
1361 private void setBufferSize(String bufferSizes) {
1362 try {
1363 String[] values = bufferSizes.split(",");
1364
1365 if (values.length == 6) {
1366 final String prefix = "/sys/kernel/ipv4/tcp_";
1367 stringToFile(prefix + "rmem_min", values[0]);
1368 stringToFile(prefix + "rmem_def", values[1]);
1369 stringToFile(prefix + "rmem_max", values[2]);
1370 stringToFile(prefix + "wmem_min", values[3]);
1371 stringToFile(prefix + "wmem_def", values[4]);
1372 stringToFile(prefix + "wmem_max", values[5]);
1373 } else {
1374 Slog.e(TAG, "Invalid buffersize string: " + bufferSizes);
1375 }
1376 } catch (IOException e) {
1377 Slog.e(TAG, "Can't set tcp buffer sizes:" + e);
1378 }
1379 }
1380
1381 /**
1382 * Writes string to file. Basically same as "echo -n $string > $filename"
1383 *
1384 * @param filename
1385 * @param string
1386 * @throws IOException
1387 */
1388 private void stringToFile(String filename, String string) throws IOException {
1389 FileWriter out = new FileWriter(filename);
1390 try {
1391 out.write(string);
1392 } finally {
1393 out.close();
1394 }
1395 }
1396
1397
Robert Greenwalt2034b912009-08-12 16:08:25 -07001398 /**
1399 * Adjust the per-process dns entries (net.dns<x>.<pid>) based
1400 * on the highest priority active net which this process requested.
1401 * If there aren't any, clear it out
1402 */
1403 private void reassessPidDns(int myPid, boolean doBump)
1404 {
Joe Onoratoc2386bb2010-02-26 18:56:32 -08001405 if (DBG) Slog.d(TAG, "reassessPidDns for pid " + myPid);
Robert Greenwalt2034b912009-08-12 16:08:25 -07001406 for(int i : mPriorityList) {
1407 if (mNetAttributes[i].isDefault()) {
1408 continue;
1409 }
1410 NetworkStateTracker nt = mNetTrackers[i];
Robert Greenwalt0659da32009-07-16 17:21:39 -07001411 if (nt.getNetworkInfo().isConnected() &&
1412 !nt.isTeardownRequested()) {
Robert Greenwalta7dfbd32010-06-15 15:43:39 -07001413 NetworkProperties p = nt.getNetworkProperties();
1414 if (p == null) continue;
Robert Greenwalt2034b912009-08-12 16:08:25 -07001415 List pids = mNetRequestersPids[i];
1416 for (int j=0; j<pids.size(); j++) {
1417 Integer pid = (Integer)pids.get(j);
1418 if (pid.intValue() == myPid) {
Robert Greenwalta7dfbd32010-06-15 15:43:39 -07001419 Collection<InetAddress> dnses = p.getDnses();
1420 writePidDns(dnses, myPid);
Robert Greenwalt2034b912009-08-12 16:08:25 -07001421 if (doBump) {
1422 bumpDns();
1423 }
1424 return;
1425 }
1426 }
1427 }
1428 }
1429 // nothing found - delete
1430 for (int i = 1; ; i++) {
1431 String prop = "net.dns" + i + "." + myPid;
1432 if (SystemProperties.get(prop).length() == 0) {
1433 if (doBump) {
1434 bumpDns();
1435 }
1436 return;
1437 }
1438 SystemProperties.set(prop, "");
1439 }
1440 }
1441
Robert Greenwalta7dfbd32010-06-15 15:43:39 -07001442 private void writePidDns(Collection <InetAddress> dnses, int pid) {
Robert Greenwalt2034b912009-08-12 16:08:25 -07001443 int j = 1;
Robert Greenwalta7dfbd32010-06-15 15:43:39 -07001444 for (InetAddress dns : dnses) {
1445 SystemProperties.set("net.dns" + j++ + "." + pid, dns.getHostAddress());
Robert Greenwalt2034b912009-08-12 16:08:25 -07001446 }
1447 }
1448
1449 private void bumpDns() {
1450 /*
1451 * Bump the property that tells the name resolver library to reread
1452 * the DNS server list from the properties.
1453 */
1454 String propVal = SystemProperties.get("net.dnschange");
1455 int n = 0;
1456 if (propVal.length() != 0) {
1457 try {
1458 n = Integer.parseInt(propVal);
1459 } catch (NumberFormatException e) {}
1460 }
1461 SystemProperties.set("net.dnschange", "" + (n+1));
1462 }
1463
Robert Greenwalt3afbead2010-07-23 15:46:26 -07001464 private void handleDnsConfigurationChange(int netType) {
Robert Greenwalt2034b912009-08-12 16:08:25 -07001465 // add default net's dns entries
Robert Greenwalt3afbead2010-07-23 15:46:26 -07001466 NetworkStateTracker nt = mNetTrackers[netType];
1467 if (nt != null && nt.getNetworkInfo().isConnected() && !nt.isTeardownRequested()) {
1468 NetworkProperties p = nt.getNetworkProperties();
1469 if (p == null) return;
1470 Collection<InetAddress> dnses = p.getDnses();
1471 if (mNetAttributes[netType].isDefault()) {
1472 int j = 1;
1473 for (InetAddress dns : dnses) {
1474 if (DBG) {
1475 Slog.d(TAG, "adding dns " + dns + " for " +
1476 nt.getNetworkInfo().getTypeName());
Robert Greenwalt2034b912009-08-12 16:08:25 -07001477 }
Robert Greenwalt3afbead2010-07-23 15:46:26 -07001478 SystemProperties.set("net.dns" + j++, dns.getHostAddress());
1479 }
1480 for (int k=j ; k<mNumDnsEntries; k++) {
1481 if (DBG) Slog.d(TAG, "erasing net.dns" + k);
1482 SystemProperties.set("net.dns" + k, "");
1483 }
1484 mNumDnsEntries = j;
1485 } else {
1486 // set per-pid dns for attached secondary nets
1487 List pids = mNetRequestersPids[netType];
1488 for (int y=0; y< pids.size(); y++) {
1489 Integer pid = (Integer)pids.get(y);
1490 writePidDns(dnses, pid.intValue());
The Android Open Source Project28527d22009-03-03 19:31:44 -08001491 }
1492 }
Robert Greenwalt3afbead2010-07-23 15:46:26 -07001493 bumpDns();
The Android Open Source Project28527d22009-03-03 19:31:44 -08001494 }
Robert Greenwalt2034b912009-08-12 16:08:25 -07001495 }
1496
1497 private int getRestoreDefaultNetworkDelay() {
1498 String restoreDefaultNetworkDelayStr = SystemProperties.get(
1499 NETWORK_RESTORE_DELAY_PROP_NAME);
1500 if(restoreDefaultNetworkDelayStr != null &&
1501 restoreDefaultNetworkDelayStr.length() != 0) {
1502 try {
1503 return Integer.valueOf(restoreDefaultNetworkDelayStr);
1504 } catch (NumberFormatException e) {
1505 }
The Android Open Source Project28527d22009-03-03 19:31:44 -08001506 }
Robert Greenwalt2034b912009-08-12 16:08:25 -07001507 return RESTORE_DEFAULT_NETWORK_DELAY;
The Android Open Source Project28527d22009-03-03 19:31:44 -08001508 }
1509
1510 @Override
1511 protected void dump(FileDescriptor fd, PrintWriter pw, String[] args) {
Robert Greenwalt0659da32009-07-16 17:21:39 -07001512 if (mContext.checkCallingOrSelfPermission(
1513 android.Manifest.permission.DUMP)
The Android Open Source Project28527d22009-03-03 19:31:44 -08001514 != PackageManager.PERMISSION_GRANTED) {
Robert Greenwalt0659da32009-07-16 17:21:39 -07001515 pw.println("Permission Denial: can't dump ConnectivityService " +
1516 "from from pid=" + Binder.getCallingPid() + ", uid=" +
1517 Binder.getCallingUid());
The Android Open Source Project28527d22009-03-03 19:31:44 -08001518 return;
1519 }
The Android Open Source Project28527d22009-03-03 19:31:44 -08001520 pw.println();
1521 for (NetworkStateTracker nst : mNetTrackers) {
Robert Greenwalt3eeb6032009-12-21 18:24:07 -08001522 if (nst != null) {
1523 if (nst.getNetworkInfo().isConnected()) {
1524 pw.println("Active network: " + nst.getNetworkInfo().
1525 getTypeName());
1526 }
1527 pw.println(nst.getNetworkInfo());
1528 pw.println(nst);
1529 pw.println();
Robert Greenwalt2034b912009-08-12 16:08:25 -07001530 }
The Android Open Source Project28527d22009-03-03 19:31:44 -08001531 }
Robert Greenwalt3eeb6032009-12-21 18:24:07 -08001532
1533 pw.println("Network Requester Pids:");
1534 for (int net : mPriorityList) {
1535 String pidString = net + ": ";
1536 for (Object pid : mNetRequestersPids[net]) {
1537 pidString = pidString + pid.toString() + ", ";
1538 }
1539 pw.println(pidString);
1540 }
1541 pw.println();
1542
1543 pw.println("FeatureUsers:");
1544 for (Object requester : mFeatureUsers) {
1545 pw.println(requester.toString());
1546 }
1547 pw.println();
Robert Greenwalt8e87f122010-02-11 18:18:40 -08001548
Robert Greenwalt93dc1042010-06-15 12:19:37 -07001549 synchronized (this) {
1550 pw.println("NetworkTranstionWakeLock is currently " +
1551 (mNetTransitionWakeLock.isHeld() ? "" : "not ") + "held.");
1552 pw.println("It was last requested for "+mNetTransitionWakeLockCausedBy);
1553 }
1554 pw.println();
1555
Robert Greenwalt8e87f122010-02-11 18:18:40 -08001556 mTethering.dump(fd, pw, args);
The Android Open Source Project28527d22009-03-03 19:31:44 -08001557 }
1558
Robert Greenwalt2034b912009-08-12 16:08:25 -07001559 // must be stateless - things change under us.
The Android Open Source Project28527d22009-03-03 19:31:44 -08001560 private class MyHandler extends Handler {
1561 @Override
1562 public void handleMessage(Message msg) {
1563 NetworkInfo info;
1564 switch (msg.what) {
1565 case NetworkStateTracker.EVENT_STATE_CHANGED:
1566 info = (NetworkInfo) msg.obj;
Robert Greenwalt12c44552009-12-07 11:33:18 -08001567 int type = info.getType();
1568 NetworkInfo.State state = info.getState();
Robert Greenwalt24e2d2b2010-01-25 16:14:00 -08001569 // only do this optimization for wifi. It going into scan mode for location
1570 // services generates alot of noise. Meanwhile the mms apn won't send out
1571 // subsequent notifications when on default cellular because it never
1572 // disconnects.. so only do this to wifi notifications. Fixed better when the
1573 // APN notifications are standardized.
1574 if (mNetAttributes[type].mLastState == state &&
1575 mNetAttributes[type].mRadio == ConnectivityManager.TYPE_WIFI) {
Robert Greenwalt12c44552009-12-07 11:33:18 -08001576 if (DBG) {
Robert Greenwalt24e2d2b2010-01-25 16:14:00 -08001577 // TODO - remove this after we validate the dropping doesn't break
1578 // anything
Joe Onoratoc2386bb2010-02-26 18:56:32 -08001579 Slog.d(TAG, "Dropping ConnectivityChange for " +
Robert Greenwalt2adbc7f2010-01-13 09:36:31 -08001580 info.getTypeName() + ": " +
Robert Greenwalt12c44552009-12-07 11:33:18 -08001581 state + "/" + info.getDetailedState());
1582 }
1583 return;
1584 }
1585 mNetAttributes[type].mLastState = state;
1586
Joe Onoratoc2386bb2010-02-26 18:56:32 -08001587 if (DBG) Slog.d(TAG, "ConnectivityChange for " +
Robert Greenwalt0659da32009-07-16 17:21:39 -07001588 info.getTypeName() + ": " +
Robert Greenwalt12c44552009-12-07 11:33:18 -08001589 state + "/" + info.getDetailedState());
The Android Open Source Project28527d22009-03-03 19:31:44 -08001590
1591 // Connectivity state changed:
1592 // [31-13] Reserved for future use
Robert Greenwalt0659da32009-07-16 17:21:39 -07001593 // [12-9] Network subtype (for mobile network, as defined
1594 // by TelephonyManager)
1595 // [8-3] Detailed state ordinal (as defined by
1596 // NetworkInfo.DetailedState)
The Android Open Source Project28527d22009-03-03 19:31:44 -08001597 // [2-0] Network type (as defined by ConnectivityManager)
1598 int eventLogParam = (info.getType() & 0x7) |
1599 ((info.getDetailedState().ordinal() & 0x3f) << 3) |
1600 (info.getSubtype() << 9);
Doug Zongker2fc96232009-12-04 10:31:43 -08001601 EventLog.writeEvent(EventLogTags.CONNECTIVITY_STATE_CHANGED,
Robert Greenwalt0659da32009-07-16 17:21:39 -07001602 eventLogParam);
1603
1604 if (info.getDetailedState() ==
1605 NetworkInfo.DetailedState.FAILED) {
The Android Open Source Project28527d22009-03-03 19:31:44 -08001606 handleConnectionFailure(info);
Robert Greenwalt12c44552009-12-07 11:33:18 -08001607 } else if (state == NetworkInfo.State.DISCONNECTED) {
The Android Open Source Project28527d22009-03-03 19:31:44 -08001608 handleDisconnect(info);
Robert Greenwalt12c44552009-12-07 11:33:18 -08001609 } else if (state == NetworkInfo.State.SUSPENDED) {
The Android Open Source Project28527d22009-03-03 19:31:44 -08001610 // TODO: need to think this over.
Robert Greenwalt0659da32009-07-16 17:21:39 -07001611 // the logic here is, handle SUSPENDED the same as
1612 // DISCONNECTED. The only difference being we are
1613 // broadcasting an intent with NetworkInfo that's
1614 // suspended. This allows the applications an
1615 // opportunity to handle DISCONNECTED and SUSPENDED
1616 // differently, or not.
The Android Open Source Project28527d22009-03-03 19:31:44 -08001617 handleDisconnect(info);
Robert Greenwalt12c44552009-12-07 11:33:18 -08001618 } else if (state == NetworkInfo.State.CONNECTED) {
The Android Open Source Project28527d22009-03-03 19:31:44 -08001619 handleConnect(info);
1620 }
The Android Open Source Project28527d22009-03-03 19:31:44 -08001621 break;
The Android Open Source Project28527d22009-03-03 19:31:44 -08001622 case NetworkStateTracker.EVENT_CONFIGURATION_CHANGED:
Robert Greenwalt3afbead2010-07-23 15:46:26 -07001623 // TODO - make this handle ip/proxy/gateway/dns changes
1624 info = (NetworkInfo) msg.obj;
1625 type = info.getType();
1626 handleDnsConfigurationChange(type);
The Android Open Source Project28527d22009-03-03 19:31:44 -08001627 break;
Robert Greenwalt2034b912009-08-12 16:08:25 -07001628 case NetworkStateTracker.EVENT_RESTORE_DEFAULT_NETWORK:
Robert Greenwaltaffc3a12009-09-27 17:27:04 -07001629 FeatureUser u = (FeatureUser)msg.obj;
1630 u.expire();
Robert Greenwalt2034b912009-08-12 16:08:25 -07001631 break;
Robert Greenwalt93dc1042010-06-15 12:19:37 -07001632 case NetworkStateTracker.EVENT_CLEAR_NET_TRANSITION_WAKELOCK:
1633 String causedBy = null;
1634 synchronized (ConnectivityService.this) {
1635 if (msg.arg1 == mNetTransitionWakeLockSerialNumber &&
1636 mNetTransitionWakeLock.isHeld()) {
1637 mNetTransitionWakeLock.release();
1638 causedBy = mNetTransitionWakeLockCausedBy;
1639 }
1640 }
1641 if (causedBy != null) {
1642 Slog.d(TAG, "NetTransition Wakelock for " +
1643 causedBy + " released by timeout");
1644 }
1645 break;
The Android Open Source Project28527d22009-03-03 19:31:44 -08001646 }
1647 }
1648 }
Robert Greenwalt0c4828c2010-01-26 11:40:34 -08001649
1650 // javadoc from interface
Robert Greenwalt4283ded2010-03-02 17:25:02 -08001651 public int tether(String iface) {
Robert Greenwalt0c4828c2010-01-26 11:40:34 -08001652 enforceTetherChangePermission();
Robert Greenwalt4283ded2010-03-02 17:25:02 -08001653
1654 if (isTetheringSupported()) {
1655 return mTethering.tether(iface);
1656 } else {
1657 return ConnectivityManager.TETHER_ERROR_UNSUPPORTED;
1658 }
Robert Greenwalt0c4828c2010-01-26 11:40:34 -08001659 }
1660
1661 // javadoc from interface
Robert Greenwalt4283ded2010-03-02 17:25:02 -08001662 public int untether(String iface) {
Robert Greenwalt0c4828c2010-01-26 11:40:34 -08001663 enforceTetherChangePermission();
Robert Greenwalt4283ded2010-03-02 17:25:02 -08001664
1665 if (isTetheringSupported()) {
1666 return mTethering.untether(iface);
1667 } else {
1668 return ConnectivityManager.TETHER_ERROR_UNSUPPORTED;
1669 }
1670 }
1671
1672 // javadoc from interface
1673 public int getLastTetherError(String iface) {
1674 enforceTetherAccessPermission();
1675
1676 if (isTetheringSupported()) {
1677 return mTethering.getLastTetherError(iface);
1678 } else {
1679 return ConnectivityManager.TETHER_ERROR_UNSUPPORTED;
1680 }
Robert Greenwalt8e87f122010-02-11 18:18:40 -08001681 }
1682
1683 // TODO - proper iface API for selection by property, inspection, etc
1684 public String[] getTetherableUsbRegexs() {
1685 enforceTetherAccessPermission();
1686 if (isTetheringSupported()) {
1687 return mTethering.getTetherableUsbRegexs();
1688 } else {
1689 return new String[0];
1690 }
1691 }
1692
1693 public String[] getTetherableWifiRegexs() {
1694 enforceTetherAccessPermission();
1695 if (isTetheringSupported()) {
1696 return mTethering.getTetherableWifiRegexs();
1697 } else {
1698 return new String[0];
1699 }
Robert Greenwalt0c4828c2010-01-26 11:40:34 -08001700 }
1701
Danica Chang96567052010-08-11 14:54:43 -07001702 public String[] getTetherableBluetoothRegexs() {
1703 enforceTetherAccessPermission();
1704 if (isTetheringSupported()) {
1705 return mTethering.getTetherableBluetoothRegexs();
1706 } else {
1707 return new String[0];
1708 }
1709 }
1710
Robert Greenwalt0c4828c2010-01-26 11:40:34 -08001711 // TODO - move iface listing, queries, etc to new module
1712 // javadoc from interface
1713 public String[] getTetherableIfaces() {
Robert Greenwalt8e87f122010-02-11 18:18:40 -08001714 enforceTetherAccessPermission();
Robert Greenwalt0c4828c2010-01-26 11:40:34 -08001715 return mTethering.getTetherableIfaces();
1716 }
1717
1718 public String[] getTetheredIfaces() {
Robert Greenwalt8e87f122010-02-11 18:18:40 -08001719 enforceTetherAccessPermission();
Robert Greenwalt0c4828c2010-01-26 11:40:34 -08001720 return mTethering.getTetheredIfaces();
1721 }
Robert Greenwalt8e87f122010-02-11 18:18:40 -08001722
Robert Greenwalt4283ded2010-03-02 17:25:02 -08001723 public String[] getTetheringErroredIfaces() {
1724 enforceTetherAccessPermission();
1725 return mTethering.getErroredIfaces();
1726 }
1727
Robert Greenwalt8e87f122010-02-11 18:18:40 -08001728 // if ro.tether.denied = true we default to no tethering
1729 // gservices could set the secure setting to 1 though to enable it on a build where it
1730 // had previously been turned off.
1731 public boolean isTetheringSupported() {
1732 enforceTetherAccessPermission();
1733 int defaultVal = (SystemProperties.get("ro.tether.denied").equals("true") ? 0 : 1);
Robert Greenwaltf1b66e12010-02-25 12:29:30 -08001734 boolean tetherEnabledInSettings = (Settings.Secure.getInt(mContext.getContentResolver(),
1735 Settings.Secure.TETHER_SUPPORTED, defaultVal) != 0);
1736 return tetherEnabledInSettings && mTetheringConfigValid;
Robert Greenwalt8e87f122010-02-11 18:18:40 -08001737 }
Robert Greenwalt93dc1042010-06-15 12:19:37 -07001738
1739 // An API NetworkStateTrackers can call when they lose their network.
1740 // This will automatically be cleared after X seconds or a network becomes CONNECTED,
1741 // whichever happens first. The timer is started by the first caller and not
1742 // restarted by subsequent callers.
1743 public void requestNetworkTransitionWakelock(String forWhom) {
1744 enforceConnectivityInternalPermission();
1745 synchronized (this) {
1746 if (mNetTransitionWakeLock.isHeld()) return;
1747 mNetTransitionWakeLockSerialNumber++;
1748 mNetTransitionWakeLock.acquire();
1749 mNetTransitionWakeLockCausedBy = forWhom;
1750 }
1751 mHandler.sendMessageDelayed(mHandler.obtainMessage(
1752 NetworkStateTracker.EVENT_CLEAR_NET_TRANSITION_WAKELOCK,
1753 mNetTransitionWakeLockSerialNumber, 0),
1754 mNetTransitionWakeLockTimeout);
1755 return;
1756 }
The Android Open Source Project28527d22009-03-03 19:31:44 -08001757}