blob: 6f23805c0e9a4b383c7f063c63a9000228307cce [file] [log] [blame]
The Android Open Source Project28527d22009-03-03 19:31:44 -08001/*
2 * Copyright (C) 2008 The Android Open Source Project
3 *
4 * Licensed under the Apache License, Version 2.0 (the "License");
5 * you may not use this file except in compliance with the License.
6 * You may obtain a copy of the License at
7 *
8 * http://www.apache.org/licenses/LICENSE-2.0
9 *
10 * Unless required by applicable law or agreed to in writing, software
11 * distributed under the License is distributed on an "AS IS" BASIS,
12 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13 * See the License for the specific language governing permissions and
14 * limitations under the License.
15 */
16
17package com.android.server;
18
19import android.app.Notification;
20import android.app.NotificationManager;
21import android.content.ContentResolver;
22import android.content.Context;
23import android.content.Intent;
24import android.content.pm.PackageManager;
25import android.net.ConnectivityManager;
26import android.net.IConnectivityManager;
27import android.net.MobileDataStateTracker;
28import android.net.NetworkInfo;
Robert Greenwalt1f1bcfe2010-08-30 10:56:47 -070029import android.net.LinkProperties;
The Android Open Source Project28527d22009-03-03 19:31:44 -080030import android.net.NetworkStateTracker;
Robert Greenwalt7fe44cb2010-08-27 09:24:29 -070031import android.net.NetworkUtils;
The Android Open Source Project28527d22009-03-03 19:31:44 -080032import android.net.wifi.WifiStateTracker;
Irfan Sheriff7f132d92010-06-09 15:39:36 -070033import android.net.NetworkUtils;
The Android Open Source Project28527d22009-03-03 19:31:44 -080034import android.os.Binder;
35import android.os.Handler;
Robert Greenwalt2034b912009-08-12 16:08:25 -070036import android.os.IBinder;
The Android Open Source Project28527d22009-03-03 19:31:44 -080037import android.os.Looper;
38import android.os.Message;
Robert Greenwalt93dc1042010-06-15 12:19:37 -070039import android.os.PowerManager;
Robert Greenwalt2034b912009-08-12 16:08:25 -070040import android.os.RemoteException;
The Android Open Source Project28527d22009-03-03 19:31:44 -080041import android.os.ServiceManager;
42import android.os.SystemProperties;
43import android.provider.Settings;
Robert Greenwalt2034b912009-08-12 16:08:25 -070044import android.text.TextUtils;
The Android Open Source Project28527d22009-03-03 19:31:44 -080045import android.util.EventLog;
Joe Onoratoc2386bb2010-02-26 18:56:32 -080046import android.util.Slog;
The Android Open Source Project28527d22009-03-03 19:31:44 -080047
Robert Greenwalt2034b912009-08-12 16:08:25 -070048import com.android.internal.telephony.Phone;
49
Robert Greenwalt0c4828c2010-01-26 11:40:34 -080050import com.android.server.connectivity.Tethering;
51
The Android Open Source Project28527d22009-03-03 19:31:44 -080052import java.io.FileDescriptor;
Irfan Sheriff7f132d92010-06-09 15:39:36 -070053import java.io.FileWriter;
54import java.io.IOException;
The Android Open Source Project28527d22009-03-03 19:31:44 -080055import java.io.PrintWriter;
Robert Greenwalta7dfbd32010-06-15 15:43:39 -070056import java.net.InetAddress;
57import java.net.UnknownHostException;
Robert Greenwalt2034b912009-08-12 16:08:25 -070058import java.util.ArrayList;
Robert Greenwalta7dfbd32010-06-15 15:43:39 -070059import java.util.Collection;
Robert Greenwalt2034b912009-08-12 16:08:25 -070060import java.util.List;
Robert Greenwalt7fe44cb2010-08-27 09:24:29 -070061import java.net.InetAddress;
62import java.net.UnknownHostException;
The Android Open Source Project28527d22009-03-03 19:31:44 -080063
64/**
65 * @hide
66 */
67public class ConnectivityService extends IConnectivityManager.Stub {
68
Robert Greenwalta25fd712009-10-06 14:12:53 -070069 private static final boolean DBG = true;
The Android Open Source Project28527d22009-03-03 19:31:44 -080070 private static final String TAG = "ConnectivityService";
71
Robert Greenwalt2034b912009-08-12 16:08:25 -070072 // how long to wait before switching back to a radio's default network
73 private static final int RESTORE_DEFAULT_NETWORK_DELAY = 1 * 60 * 1000;
74 // system property that can override the above value
75 private static final String NETWORK_RESTORE_DELAY_PROP_NAME =
76 "android.telephony.apn-restore";
77
Robert Greenwalt0c4828c2010-01-26 11:40:34 -080078 private Tethering mTethering;
Robert Greenwaltf1b66e12010-02-25 12:29:30 -080079 private boolean mTetheringConfigValid = false;
Robert Greenwalt0c4828c2010-01-26 11:40:34 -080080
The Android Open Source Project28527d22009-03-03 19:31:44 -080081 /**
82 * Sometimes we want to refer to the individual network state
83 * trackers separately, and sometimes we just want to treat them
84 * abstractly.
85 */
86 private NetworkStateTracker mNetTrackers[];
Robert Greenwalt2034b912009-08-12 16:08:25 -070087
88 /**
89 * A per Net list of the PID's that requested access to the net
90 * used both as a refcount and for per-PID DNS selection
91 */
92 private List mNetRequestersPids[];
93
Irfan Sheriff653e2a22010-06-07 09:03:04 -070094 private WifiWatchdogService mWifiWatchdogService;
95
Robert Greenwalt2034b912009-08-12 16:08:25 -070096 // priority order of the nettrackers
97 // (excluding dynamically set mNetworkPreference)
98 // TODO - move mNetworkTypePreference into this
99 private int[] mPriorityList;
100
The Android Open Source Project28527d22009-03-03 19:31:44 -0800101 private Context mContext;
102 private int mNetworkPreference;
Robert Greenwalt2034b912009-08-12 16:08:25 -0700103 private int mActiveDefaultNetwork = -1;
The Android Open Source Project28527d22009-03-03 19:31:44 -0800104
105 private int mNumDnsEntries;
The Android Open Source Project28527d22009-03-03 19:31:44 -0800106
107 private boolean mTestMode;
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
The Android Open Source Project28527d22009-03-03 19:31:44 -0800759 * @return {@code true} on success, {@code false} on failure
760 */
761 public boolean requestRouteToHost(int networkType, int hostAddress) {
Robert Greenwalt7fe44cb2010-08-27 09:24:29 -0700762 InetAddress inetAddress = NetworkUtils.intToInetAddress(hostAddress);
763
764 if (inetAddress == null) {
765 return false;
766 }
767
768 return requestRouteToHostAddress(networkType, inetAddress.getAddress());
769 }
770
771 /**
772 * Ensure that a network route exists to deliver traffic to the specified
773 * host via the specified network interface.
774 * @param networkType the type of the network over which traffic to the
775 * specified host is to be routed
776 * @param hostAddress the IP address of the host to which the route is
777 * desired
778 * @return {@code true} on success, {@code false} on failure
779 */
780 public boolean requestRouteToHostAddress(int networkType, byte[] hostAddress) {
The Android Open Source Project28527d22009-03-03 19:31:44 -0800781 enforceChangePermission();
782 if (!ConnectivityManager.isNetworkTypeValid(networkType)) {
783 return false;
784 }
785 NetworkStateTracker tracker = mNetTrackers[networkType];
Robert Greenwalt4666ed02009-09-10 15:06:20 -0700786
Robert Greenwaltec05b3c2009-10-30 14:17:42 -0700787 if (tracker == null || !tracker.getNetworkInfo().isConnected() ||
788 tracker.isTeardownRequested()) {
Robert Greenwalt4666ed02009-09-10 15:06:20 -0700789 if (DBG) {
Robert Greenwalt7fe44cb2010-08-27 09:24:29 -0700790 Slog.d(TAG, "requestRouteToHostAddress on down network " +
791 "(" + networkType + ") - dropped");
Robert Greenwalt4666ed02009-09-10 15:06:20 -0700792 }
793 return false;
The Android Open Source Project28527d22009-03-03 19:31:44 -0800794 }
Robert Greenwalta7dfbd32010-06-15 15:43:39 -0700795 try {
Robert Greenwalt7fe44cb2010-08-27 09:24:29 -0700796 InetAddress addr = InetAddress.getByAddress(hostAddress);
Robert Greenwalta7dfbd32010-06-15 15:43:39 -0700797 return addHostRoute(tracker, addr);
798 } catch (UnknownHostException e) {}
799 return false;
Irfan Sheriff7f132d92010-06-09 15:39:36 -0700800 }
801
802 /**
803 * Ensure that a network route exists to deliver traffic to the specified
804 * host via the mobile data network.
805 * @param hostAddress the IP address of the host to which the route is desired,
806 * in network byte order.
Robert Greenwalta7dfbd32010-06-15 15:43:39 -0700807 * TODO - deprecate
Irfan Sheriff7f132d92010-06-09 15:39:36 -0700808 * @return {@code true} on success, {@code false} on failure
809 */
Robert Greenwalta7dfbd32010-06-15 15:43:39 -0700810 private boolean addHostRoute(NetworkStateTracker nt, InetAddress hostAddress) {
Irfan Sheriff7f132d92010-06-09 15:39:36 -0700811 if (nt.getNetworkInfo().getType() == ConnectivityManager.TYPE_WIFI) {
812 return false;
813 }
814
Robert Greenwalt1f1bcfe2010-08-30 10:56:47 -0700815 LinkProperties p = nt.getLinkProperties();
Robert Greenwalta7dfbd32010-06-15 15:43:39 -0700816 if (p == null) return false;
817 String interfaceName = p.getInterfaceName();
Irfan Sheriff7f132d92010-06-09 15:39:36 -0700818
819 if (DBG) {
Robert Greenwalta7dfbd32010-06-15 15:43:39 -0700820 Slog.d(TAG, "Requested host route to " + hostAddress + "(" + interfaceName + ")");
Irfan Sheriff7f132d92010-06-09 15:39:36 -0700821 }
Robert Greenwalta7dfbd32010-06-15 15:43:39 -0700822 if (interfaceName != null) {
Robert Greenwalt7fe44cb2010-08-27 09:24:29 -0700823 return NetworkUtils.addHostRoute(interfaceName, hostAddress, null);
Irfan Sheriff7f132d92010-06-09 15:39:36 -0700824 } else {
Robert Greenwalta7dfbd32010-06-15 15:43:39 -0700825 if (DBG) Slog.e(TAG, "addHostRoute failed due to null interface name");
Irfan Sheriff7f132d92010-06-09 15:39:36 -0700826 return false;
827 }
The Android Open Source Project28527d22009-03-03 19:31:44 -0800828 }
829
830 /**
831 * @see ConnectivityManager#getBackgroundDataSetting()
832 */
833 public boolean getBackgroundDataSetting() {
834 return Settings.Secure.getInt(mContext.getContentResolver(),
835 Settings.Secure.BACKGROUND_DATA, 1) == 1;
836 }
Robert Greenwalt0659da32009-07-16 17:21:39 -0700837
The Android Open Source Project28527d22009-03-03 19:31:44 -0800838 /**
839 * @see ConnectivityManager#setBackgroundDataSetting(boolean)
840 */
841 public void setBackgroundDataSetting(boolean allowBackgroundDataUsage) {
842 mContext.enforceCallingOrSelfPermission(
843 android.Manifest.permission.CHANGE_BACKGROUND_DATA_SETTING,
844 "ConnectivityService");
Robert Greenwalt0659da32009-07-16 17:21:39 -0700845
The Android Open Source Project28527d22009-03-03 19:31:44 -0800846 if (getBackgroundDataSetting() == allowBackgroundDataUsage) return;
847
848 Settings.Secure.putInt(mContext.getContentResolver(),
Robert Greenwalt0659da32009-07-16 17:21:39 -0700849 Settings.Secure.BACKGROUND_DATA,
850 allowBackgroundDataUsage ? 1 : 0);
851
The Android Open Source Project28527d22009-03-03 19:31:44 -0800852 Intent broadcast = new Intent(
853 ConnectivityManager.ACTION_BACKGROUND_DATA_SETTING_CHANGED);
854 mContext.sendBroadcast(broadcast);
Robert Greenwalt0659da32009-07-16 17:21:39 -0700855 }
856
Robert Greenwalt1b21f6c2010-02-23 18:58:05 -0800857 /**
858 * @see ConnectivityManager#getMobileDataEnabled()
859 */
860 public boolean getMobileDataEnabled() {
861 enforceAccessPermission();
862 boolean retVal = Settings.Secure.getInt(mContext.getContentResolver(),
863 Settings.Secure.MOBILE_DATA, 1) == 1;
Joe Onoratoc2386bb2010-02-26 18:56:32 -0800864 if (DBG) Slog.d(TAG, "getMobileDataEnabled returning " + retVal);
Robert Greenwalt1b21f6c2010-02-23 18:58:05 -0800865 return retVal;
866 }
867
868 /**
869 * @see ConnectivityManager#setMobileDataEnabled(boolean)
870 */
871 public synchronized void setMobileDataEnabled(boolean enabled) {
872 enforceChangePermission();
Joe Onoratoc2386bb2010-02-26 18:56:32 -0800873 if (DBG) Slog.d(TAG, "setMobileDataEnabled(" + enabled + ")");
Robert Greenwalt1b21f6c2010-02-23 18:58:05 -0800874
875 if (getMobileDataEnabled() == enabled) return;
876
877 Settings.Secure.putInt(mContext.getContentResolver(),
878 Settings.Secure.MOBILE_DATA, enabled ? 1 : 0);
879
880 if (enabled) {
881 if (mNetTrackers[ConnectivityManager.TYPE_MOBILE] != null) {
Joe Onoratoc2386bb2010-02-26 18:56:32 -0800882 if (DBG) Slog.d(TAG, "starting up " + mNetTrackers[ConnectivityManager.TYPE_MOBILE]);
Robert Greenwalt1b21f6c2010-02-23 18:58:05 -0800883 mNetTrackers[ConnectivityManager.TYPE_MOBILE].reconnect();
884 }
885 } else {
886 for (NetworkStateTracker nt : mNetTrackers) {
887 if (nt == null) continue;
888 int netType = nt.getNetworkInfo().getType();
889 if (mNetAttributes[netType].mRadio == ConnectivityManager.TYPE_MOBILE) {
Joe Onoratoc2386bb2010-02-26 18:56:32 -0800890 if (DBG) Slog.d(TAG, "tearing down " + nt);
Robert Greenwalt1b21f6c2010-02-23 18:58:05 -0800891 nt.teardown();
892 }
893 }
894 }
895 }
896
The Android Open Source Project28527d22009-03-03 19:31:44 -0800897 private int getNumConnectedNetworks() {
898 int numConnectedNets = 0;
899
900 for (NetworkStateTracker nt : mNetTrackers) {
Robert Greenwaltec05b3c2009-10-30 14:17:42 -0700901 if (nt != null && nt.getNetworkInfo().isConnected() &&
Robert Greenwalt0659da32009-07-16 17:21:39 -0700902 !nt.isTeardownRequested()) {
The Android Open Source Project28527d22009-03-03 19:31:44 -0800903 ++numConnectedNets;
904 }
905 }
906 return numConnectedNets;
907 }
908
909 private void enforceAccessPermission() {
Robert Greenwalt0659da32009-07-16 17:21:39 -0700910 mContext.enforceCallingOrSelfPermission(
911 android.Manifest.permission.ACCESS_NETWORK_STATE,
912 "ConnectivityService");
The Android Open Source Project28527d22009-03-03 19:31:44 -0800913 }
914
915 private void enforceChangePermission() {
Robert Greenwalt0659da32009-07-16 17:21:39 -0700916 mContext.enforceCallingOrSelfPermission(
917 android.Manifest.permission.CHANGE_NETWORK_STATE,
918 "ConnectivityService");
The Android Open Source Project28527d22009-03-03 19:31:44 -0800919 }
920
Robert Greenwalt0c4828c2010-01-26 11:40:34 -0800921 // TODO Make this a special check when it goes public
922 private void enforceTetherChangePermission() {
923 mContext.enforceCallingOrSelfPermission(
924 android.Manifest.permission.CHANGE_NETWORK_STATE,
925 "ConnectivityService");
926 }
927
Robert Greenwalt8e87f122010-02-11 18:18:40 -0800928 private void enforceTetherAccessPermission() {
929 mContext.enforceCallingOrSelfPermission(
930 android.Manifest.permission.ACCESS_NETWORK_STATE,
931 "ConnectivityService");
932 }
933
Robert Greenwalt93dc1042010-06-15 12:19:37 -0700934 private void enforceConnectivityInternalPermission() {
935 mContext.enforceCallingOrSelfPermission(
936 android.Manifest.permission.CONNECTIVITY_INTERNAL,
937 "ConnectivityService");
938 }
939
The Android Open Source Project28527d22009-03-03 19:31:44 -0800940 /**
Robert Greenwalt0659da32009-07-16 17:21:39 -0700941 * Handle a {@code DISCONNECTED} event. If this pertains to the non-active
942 * network, we ignore it. If it is for the active network, we send out a
943 * broadcast. But first, we check whether it might be possible to connect
944 * to a different network.
The Android Open Source Project28527d22009-03-03 19:31:44 -0800945 * @param info the {@code NetworkInfo} for the network
946 */
947 private void handleDisconnect(NetworkInfo info) {
948
Robert Greenwalt2034b912009-08-12 16:08:25 -0700949 int prevNetType = info.getType();
The Android Open Source Project28527d22009-03-03 19:31:44 -0800950
Robert Greenwalt2034b912009-08-12 16:08:25 -0700951 mNetTrackers[prevNetType].setTeardownRequested(false);
The Android Open Source Project28527d22009-03-03 19:31:44 -0800952 /*
953 * If the disconnected network is not the active one, then don't report
954 * this as a loss of connectivity. What probably happened is that we're
955 * getting the disconnect for a network that we explicitly disabled
956 * in accordance with network preference policies.
957 */
Robert Greenwalt2034b912009-08-12 16:08:25 -0700958 if (!mNetAttributes[prevNetType].isDefault()) {
959 List pids = mNetRequestersPids[prevNetType];
960 for (int i = 0; i<pids.size(); i++) {
961 Integer pid = (Integer)pids.get(i);
962 // will remove them because the net's no longer connected
963 // need to do this now as only now do we know the pids and
964 // can properly null things that are no longer referenced.
965 reassessPidDns(pid.intValue(), false);
The Android Open Source Project28527d22009-03-03 19:31:44 -0800966 }
The Android Open Source Project28527d22009-03-03 19:31:44 -0800967 }
968
The Android Open Source Project28527d22009-03-03 19:31:44 -0800969 Intent intent = new Intent(ConnectivityManager.CONNECTIVITY_ACTION);
970 intent.putExtra(ConnectivityManager.EXTRA_NETWORK_INFO, info);
971 if (info.isFailover()) {
972 intent.putExtra(ConnectivityManager.EXTRA_IS_FAILOVER, true);
973 info.setFailover(false);
974 }
975 if (info.getReason() != null) {
976 intent.putExtra(ConnectivityManager.EXTRA_REASON, info.getReason());
977 }
978 if (info.getExtraInfo() != null) {
Robert Greenwalt0659da32009-07-16 17:21:39 -0700979 intent.putExtra(ConnectivityManager.EXTRA_EXTRA_INFO,
980 info.getExtraInfo());
The Android Open Source Project28527d22009-03-03 19:31:44 -0800981 }
Robert Greenwalt2034b912009-08-12 16:08:25 -0700982
Robert Greenwalt3cc68d32010-01-25 17:54:29 -0800983 NetworkStateTracker newNet = null;
984 if (mNetAttributes[prevNetType].isDefault()) {
985 newNet = tryFailover(prevNetType);
986 if (newNet != null) {
987 NetworkInfo switchTo = newNet.getNetworkInfo();
988 intent.putExtra(ConnectivityManager.EXTRA_OTHER_NETWORK_INFO, switchTo);
989 } else {
990 intent.putExtra(ConnectivityManager.EXTRA_NO_CONNECTIVITY, true);
991 }
Robert Greenwaltf55ced92010-01-20 19:29:41 -0800992 }
993 // do this before we broadcast the change
Robert Greenwalt3afbead2010-07-23 15:46:26 -0700994 handleConnectivityChange(prevNetType);
Robert Greenwaltf55ced92010-01-20 19:29:41 -0800995
996 sendStickyBroadcast(intent);
997 /*
998 * If the failover network is already connected, then immediately send
999 * out a followup broadcast indicating successful failover
1000 */
1001 if (newNet != null && newNet.getNetworkInfo().isConnected()) {
1002 sendConnectedBroadcast(newNet.getNetworkInfo());
1003 }
1004 }
1005
Robert Greenwalt3cc68d32010-01-25 17:54:29 -08001006 // returns null if no failover available
Robert Greenwaltf55ced92010-01-20 19:29:41 -08001007 private NetworkStateTracker tryFailover(int prevNetType) {
Robert Greenwalt2034b912009-08-12 16:08:25 -07001008 /*
1009 * If this is a default network, check if other defaults are available
1010 * or active
1011 */
1012 NetworkStateTracker newNet = null;
1013 if (mNetAttributes[prevNetType].isDefault()) {
Robert Greenwalt2034b912009-08-12 16:08:25 -07001014 if (mActiveDefaultNetwork == prevNetType) {
1015 mActiveDefaultNetwork = -1;
1016 }
1017
1018 int newType = -1;
1019 int newPriority = -1;
Robert Greenwalt72451bf2010-02-25 12:04:29 -08001020 boolean noMobileData = !getMobileDataEnabled();
Robert Greenwaltf55ced92010-01-20 19:29:41 -08001021 for (int checkType=0; checkType <= ConnectivityManager.MAX_NETWORK_TYPE; checkType++) {
Robert Greenwaltec05b3c2009-10-30 14:17:42 -07001022 if (checkType == prevNetType) continue;
1023 if (mNetAttributes[checkType] == null) continue;
Robert Greenwalt72451bf2010-02-25 12:04:29 -08001024 if (mNetAttributes[checkType].mRadio == ConnectivityManager.TYPE_MOBILE &&
1025 noMobileData) {
1026 if (DBG) {
Joe Onoratoc2386bb2010-02-26 18:56:32 -08001027 Slog.d(TAG, "not failing over to mobile type " + checkType +
Robert Greenwalt72451bf2010-02-25 12:04:29 -08001028 " because Mobile Data Disabled");
1029 }
1030 continue;
1031 }
Robert Greenwalt2034b912009-08-12 16:08:25 -07001032 if (mNetAttributes[checkType].isDefault()) {
1033 /* TODO - if we have multiple nets we could use
1034 * we may want to put more thought into which we choose
1035 */
1036 if (checkType == mNetworkPreference) {
1037 newType = checkType;
1038 break;
1039 }
Robert Greenwaltec05b3c2009-10-30 14:17:42 -07001040 if (mNetAttributes[checkType].mPriority > newPriority) {
Robert Greenwalt2034b912009-08-12 16:08:25 -07001041 newType = checkType;
Robert Greenwaltec05b3c2009-10-30 14:17:42 -07001042 newPriority = mNetAttributes[newType].mPriority;
Robert Greenwalt2034b912009-08-12 16:08:25 -07001043 }
The Android Open Source Project28527d22009-03-03 19:31:44 -08001044 }
1045 }
Robert Greenwalt2034b912009-08-12 16:08:25 -07001046
1047 if (newType != -1) {
1048 newNet = mNetTrackers[newType];
1049 /**
1050 * See if the other network is available to fail over to.
1051 * If is not available, we enable it anyway, so that it
1052 * will be able to connect when it does become available,
1053 * but we report a total loss of connectivity rather than
1054 * report that we are attempting to fail over.
1055 */
1056 if (newNet.isAvailable()) {
1057 NetworkInfo switchTo = newNet.getNetworkInfo();
1058 switchTo.setFailover(true);
Robert Greenwalta52c75a2009-08-19 20:19:33 -07001059 if (!switchTo.isConnectedOrConnecting() ||
1060 newNet.isTeardownRequested()) {
Robert Greenwalt2034b912009-08-12 16:08:25 -07001061 newNet.reconnect();
1062 }
1063 if (DBG) {
1064 if (switchTo.isConnected()) {
Joe Onoratoc2386bb2010-02-26 18:56:32 -08001065 Slog.v(TAG, "Switching to already connected " +
Robert Greenwalt2034b912009-08-12 16:08:25 -07001066 switchTo.getTypeName());
1067 } else {
Joe Onoratoc2386bb2010-02-26 18:56:32 -08001068 Slog.v(TAG, "Attempting to switch to " +
Robert Greenwalt2034b912009-08-12 16:08:25 -07001069 switchTo.getTypeName());
1070 }
1071 }
Robert Greenwalt2034b912009-08-12 16:08:25 -07001072 } else {
1073 newNet.reconnect();
Robert Greenwalt12984322010-03-09 14:55:08 -08001074 newNet = null; // not officially avail.. try anyway, but
1075 // report no failover
Robert Greenwalt2034b912009-08-12 16:08:25 -07001076 }
Robert Greenwalt2034b912009-08-12 16:08:25 -07001077 }
The Android Open Source Project28527d22009-03-03 19:31:44 -08001078 }
Robert Greenwalt2034b912009-08-12 16:08:25 -07001079
Robert Greenwaltf55ced92010-01-20 19:29:41 -08001080 return newNet;
The Android Open Source Project28527d22009-03-03 19:31:44 -08001081 }
1082
1083 private void sendConnectedBroadcast(NetworkInfo info) {
1084 Intent intent = new Intent(ConnectivityManager.CONNECTIVITY_ACTION);
1085 intent.putExtra(ConnectivityManager.EXTRA_NETWORK_INFO, info);
1086 if (info.isFailover()) {
1087 intent.putExtra(ConnectivityManager.EXTRA_IS_FAILOVER, true);
1088 info.setFailover(false);
1089 }
1090 if (info.getReason() != null) {
1091 intent.putExtra(ConnectivityManager.EXTRA_REASON, info.getReason());
1092 }
1093 if (info.getExtraInfo() != null) {
Robert Greenwalt0659da32009-07-16 17:21:39 -07001094 intent.putExtra(ConnectivityManager.EXTRA_EXTRA_INFO,
1095 info.getExtraInfo());
The Android Open Source Project28527d22009-03-03 19:31:44 -08001096 }
Mike Lockwoodfde2b762009-08-14 14:18:49 -04001097 sendStickyBroadcast(intent);
The Android Open Source Project28527d22009-03-03 19:31:44 -08001098 }
1099
1100 /**
1101 * Called when an attempt to fail over to another network has failed.
1102 * @param info the {@link NetworkInfo} for the failed network
1103 */
1104 private void handleConnectionFailure(NetworkInfo info) {
1105 mNetTrackers[info.getType()].setTeardownRequested(false);
The Android Open Source Project28527d22009-03-03 19:31:44 -08001106
Robert Greenwalt2034b912009-08-12 16:08:25 -07001107 String reason = info.getReason();
1108 String extraInfo = info.getExtraInfo();
Robert Greenwalt0659da32009-07-16 17:21:39 -07001109
Robert Greenwalt2034b912009-08-12 16:08:25 -07001110 if (DBG) {
1111 String reasonText;
1112 if (reason == null) {
1113 reasonText = ".";
1114 } else {
1115 reasonText = " (" + reason + ").";
The Android Open Source Project28527d22009-03-03 19:31:44 -08001116 }
Joe Onoratoc2386bb2010-02-26 18:56:32 -08001117 Slog.v(TAG, "Attempt to connect to " + info.getTypeName() +
Robert Greenwalt2034b912009-08-12 16:08:25 -07001118 " failed" + reasonText);
The Android Open Source Project28527d22009-03-03 19:31:44 -08001119 }
Robert Greenwalt2034b912009-08-12 16:08:25 -07001120
1121 Intent intent = new Intent(ConnectivityManager.CONNECTIVITY_ACTION);
1122 intent.putExtra(ConnectivityManager.EXTRA_NETWORK_INFO, info);
1123 if (getActiveNetworkInfo() == null) {
1124 intent.putExtra(ConnectivityManager.EXTRA_NO_CONNECTIVITY, true);
1125 }
1126 if (reason != null) {
1127 intent.putExtra(ConnectivityManager.EXTRA_REASON, reason);
1128 }
1129 if (extraInfo != null) {
1130 intent.putExtra(ConnectivityManager.EXTRA_EXTRA_INFO, extraInfo);
1131 }
1132 if (info.isFailover()) {
1133 intent.putExtra(ConnectivityManager.EXTRA_IS_FAILOVER, true);
1134 info.setFailover(false);
1135 }
Robert Greenwaltf55ced92010-01-20 19:29:41 -08001136
Robert Greenwalt3cc68d32010-01-25 17:54:29 -08001137 NetworkStateTracker newNet = null;
1138 if (mNetAttributes[info.getType()].isDefault()) {
1139 newNet = tryFailover(info.getType());
1140 if (newNet != null) {
1141 NetworkInfo switchTo = newNet.getNetworkInfo();
1142 intent.putExtra(ConnectivityManager.EXTRA_OTHER_NETWORK_INFO, switchTo);
1143 } else {
1144 intent.putExtra(ConnectivityManager.EXTRA_NO_CONNECTIVITY, true);
1145 }
Robert Greenwaltf55ced92010-01-20 19:29:41 -08001146 }
Robert Greenwalt3cc68d32010-01-25 17:54:29 -08001147
Mike Lockwoodfde2b762009-08-14 14:18:49 -04001148 sendStickyBroadcast(intent);
Robert Greenwaltf55ced92010-01-20 19:29:41 -08001149 /*
1150 * If the failover network is already connected, then immediately send
1151 * out a followup broadcast indicating successful failover
1152 */
1153 if (newNet != null && newNet.getNetworkInfo().isConnected()) {
1154 sendConnectedBroadcast(newNet.getNetworkInfo());
1155 }
Mike Lockwoodfde2b762009-08-14 14:18:49 -04001156 }
1157
1158 private void sendStickyBroadcast(Intent intent) {
1159 synchronized(this) {
Dianne Hackborna417ff82009-12-08 19:45:14 -08001160 if (!mSystemReady) {
1161 mInitialBroadcast = new Intent(intent);
Mike Lockwoodfde2b762009-08-14 14:18:49 -04001162 }
Dianne Hackborna417ff82009-12-08 19:45:14 -08001163 intent.addFlags(Intent.FLAG_RECEIVER_REGISTERED_ONLY_BEFORE_BOOT);
1164 mContext.sendStickyBroadcast(intent);
Mike Lockwoodfde2b762009-08-14 14:18:49 -04001165 }
1166 }
1167
1168 void systemReady() {
1169 synchronized(this) {
1170 mSystemReady = true;
Dianne Hackborna417ff82009-12-08 19:45:14 -08001171 if (mInitialBroadcast != null) {
1172 mContext.sendStickyBroadcast(mInitialBroadcast);
1173 mInitialBroadcast = null;
Mike Lockwoodfde2b762009-08-14 14:18:49 -04001174 }
1175 }
The Android Open Source Project28527d22009-03-03 19:31:44 -08001176 }
1177
1178 private void handleConnect(NetworkInfo info) {
Robert Greenwalt2034b912009-08-12 16:08:25 -07001179 int type = info.getType();
The Android Open Source Project28527d22009-03-03 19:31:44 -08001180
1181 // snapshot isFailover, because sendConnectedBroadcast() resets it
1182 boolean isFailover = info.isFailover();
Robert Greenwalt2034b912009-08-12 16:08:25 -07001183 NetworkStateTracker thisNet = mNetTrackers[type];
The Android Open Source Project28527d22009-03-03 19:31:44 -08001184
Robert Greenwalt2034b912009-08-12 16:08:25 -07001185 // if this is a default net and other default is running
1186 // kill the one not preferred
1187 if (mNetAttributes[type].isDefault()) {
Robert Greenwalt2034b912009-08-12 16:08:25 -07001188 if (mActiveDefaultNetwork != -1 && mActiveDefaultNetwork != type) {
1189 if ((type != mNetworkPreference &&
1190 mNetAttributes[mActiveDefaultNetwork].mPriority >
1191 mNetAttributes[type].mPriority) ||
1192 mNetworkPreference == mActiveDefaultNetwork) {
1193 // don't accept this one
Joe Onoratoc2386bb2010-02-26 18:56:32 -08001194 if (DBG) Slog.v(TAG, "Not broadcasting CONNECT_ACTION " +
Robert Greenwalt2034b912009-08-12 16:08:25 -07001195 "to torn down network " + info.getTypeName());
1196 teardown(thisNet);
1197 return;
1198 } else {
1199 // tear down the other
1200 NetworkStateTracker otherNet =
1201 mNetTrackers[mActiveDefaultNetwork];
Joe Onoratoc2386bb2010-02-26 18:56:32 -08001202 if (DBG) Slog.v(TAG, "Policy requires " +
Robert Greenwalt2034b912009-08-12 16:08:25 -07001203 otherNet.getNetworkInfo().getTypeName() +
1204 " teardown");
1205 if (!teardown(otherNet)) {
Joe Onoratoc2386bb2010-02-26 18:56:32 -08001206 Slog.e(TAG, "Network declined teardown request");
Robert Greenwalt2034b912009-08-12 16:08:25 -07001207 return;
1208 }
Robert Greenwalt93dc1042010-06-15 12:19:37 -07001209 }
1210 }
1211 synchronized (ConnectivityService.this) {
1212 // have a new default network, release the transition wakelock in a second
1213 // if it's held. The second pause is to allow apps to reconnect over the
1214 // new network
1215 if (mNetTransitionWakeLock.isHeld()) {
1216 mHandler.sendMessageDelayed(mHandler.obtainMessage(
1217 NetworkStateTracker.EVENT_CLEAR_NET_TRANSITION_WAKELOCK,
1218 mNetTransitionWakeLockSerialNumber, 0),
1219 1000);
Robert Greenwalt2034b912009-08-12 16:08:25 -07001220 }
1221 }
1222 mActiveDefaultNetwork = type;
1223 }
The Android Open Source Project28527d22009-03-03 19:31:44 -08001224 thisNet.setTeardownRequested(false);
Irfan Sheriff7f132d92010-06-09 15:39:36 -07001225 updateNetworkSettings(thisNet);
Robert Greenwalt3afbead2010-07-23 15:46:26 -07001226 handleConnectivityChange(type);
Robert Greenwalt2034b912009-08-12 16:08:25 -07001227 sendConnectedBroadcast(info);
The Android Open Source Project28527d22009-03-03 19:31:44 -08001228 }
1229
The Android Open Source Project28527d22009-03-03 19:31:44 -08001230 /**
Robert Greenwalt3afbead2010-07-23 15:46:26 -07001231 * After a change in the connectivity state of a network. We're mainly
1232 * concerned with making sure that the list of DNS servers is set up
1233 * according to which networks are connected, and ensuring that the
1234 * right routing table entries exist.
The Android Open Source Project28527d22009-03-03 19:31:44 -08001235 */
Robert Greenwalt3afbead2010-07-23 15:46:26 -07001236 private void handleConnectivityChange(int netType) {
The Android Open Source Project28527d22009-03-03 19:31:44 -08001237 /*
Robert Greenwalt2034b912009-08-12 16:08:25 -07001238 * If a non-default network is enabled, add the host routes that
Robert Greenwalt3afbead2010-07-23 15:46:26 -07001239 * will allow it's DNS servers to be accessed.
The Android Open Source Project28527d22009-03-03 19:31:44 -08001240 */
Robert Greenwalt3afbead2010-07-23 15:46:26 -07001241 handleDnsConfigurationChange(netType);
Robert Greenwalt2034b912009-08-12 16:08:25 -07001242
Robert Greenwalt3afbead2010-07-23 15:46:26 -07001243 if (mNetTrackers[netType].getNetworkInfo().isConnected()) {
1244 if (mNetAttributes[netType].isDefault()) {
1245 addDefaultRoute(mNetTrackers[netType]);
Robert Greenwalt2034b912009-08-12 16:08:25 -07001246 } else {
Robert Greenwalt3afbead2010-07-23 15:46:26 -07001247 addPrivateDnsRoutes(mNetTrackers[netType]);
1248 }
1249 } else {
1250 if (mNetAttributes[netType].isDefault()) {
1251 removeDefaultRoute(mNetTrackers[netType]);
1252 } else {
1253 removePrivateDnsRoutes(mNetTrackers[netType]);
Robert Greenwalt2034b912009-08-12 16:08:25 -07001254 }
The Android Open Source Project28527d22009-03-03 19:31:44 -08001255 }
1256 }
1257
Irfan Sheriff7f132d92010-06-09 15:39:36 -07001258 private void addPrivateDnsRoutes(NetworkStateTracker nt) {
Irfan Sheriff7f132d92010-06-09 15:39:36 -07001259 boolean privateDnsRouteSet = nt.isPrivateDnsRouteSet();
Robert Greenwalt1f1bcfe2010-08-30 10:56:47 -07001260 LinkProperties p = nt.getLinkProperties();
Robert Greenwalta7dfbd32010-06-15 15:43:39 -07001261 if (p == null) return;
1262 String interfaceName = p.getInterfaceName();
Irfan Sheriff7f132d92010-06-09 15:39:36 -07001263
1264 if (DBG) {
1265 Slog.d(TAG, "addPrivateDnsRoutes for " + nt +
1266 "(" + interfaceName + ") - mPrivateDnsRouteSet = " + privateDnsRouteSet);
1267 }
Irfan Sheriff7f132d92010-06-09 15:39:36 -07001268 if (interfaceName != null && !privateDnsRouteSet) {
Robert Greenwalta7dfbd32010-06-15 15:43:39 -07001269 Collection<InetAddress> dnsList = p.getDnses();
1270 for (InetAddress dns : dnsList) {
1271 if (DBG) Slog.d(TAG, " adding " + dns);
Robert Greenwalt7fe44cb2010-08-27 09:24:29 -07001272 NetworkUtils.addHostRoute(interfaceName, dns, null);
Irfan Sheriff7f132d92010-06-09 15:39:36 -07001273 }
1274 nt.privateDnsRouteSet(true);
1275 }
1276 }
1277
1278 private void removePrivateDnsRoutes(NetworkStateTracker nt) {
1279 // TODO - we should do this explicitly but the NetUtils api doesnt
1280 // support this yet - must remove all. No worse than before
Robert Greenwalt1f1bcfe2010-08-30 10:56:47 -07001281 LinkProperties p = nt.getLinkProperties();
Robert Greenwalta7dfbd32010-06-15 15:43:39 -07001282 if (p == null) return;
1283 String interfaceName = p.getInterfaceName();
Irfan Sheriff7f132d92010-06-09 15:39:36 -07001284 boolean privateDnsRouteSet = nt.isPrivateDnsRouteSet();
1285 if (interfaceName != null && privateDnsRouteSet) {
1286 if (DBG) {
1287 Slog.d(TAG, "removePrivateDnsRoutes for " + nt.getNetworkInfo().getTypeName() +
1288 " (" + interfaceName + ")");
1289 }
1290 NetworkUtils.removeHostRoutes(interfaceName);
1291 nt.privateDnsRouteSet(false);
1292 }
1293 }
1294
Irfan Sheriff7f132d92010-06-09 15:39:36 -07001295
1296 private void addDefaultRoute(NetworkStateTracker nt) {
Robert Greenwalt1f1bcfe2010-08-30 10:56:47 -07001297 LinkProperties p = nt.getLinkProperties();
Robert Greenwalta7dfbd32010-06-15 15:43:39 -07001298 if (p == null) return;
1299 String interfaceName = p.getInterfaceName();
1300 InetAddress defaultGatewayAddr = p.getGateway();
Irfan Sheriff7f132d92010-06-09 15:39:36 -07001301
Robert Greenwalt3afbead2010-07-23 15:46:26 -07001302 if ((interfaceName != null) && (defaultGatewayAddr != null )) {
Robert Greenwalt7fe44cb2010-08-27 09:24:29 -07001303 if (!NetworkUtils.addDefaultRoute(interfaceName, defaultGatewayAddr) && DBG) {
Robert Greenwalta7dfbd32010-06-15 15:43:39 -07001304 NetworkInfo networkInfo = nt.getNetworkInfo();
Irfan Sheriff7f132d92010-06-09 15:39:36 -07001305 Slog.d(TAG, "addDefaultRoute for " + networkInfo.getTypeName() +
1306 " (" + interfaceName + "), GatewayAddr=" + defaultGatewayAddr);
1307 }
Irfan Sheriff7f132d92010-06-09 15:39:36 -07001308 }
1309 }
1310
1311
1312 public void removeDefaultRoute(NetworkStateTracker nt) {
Robert Greenwalt1f1bcfe2010-08-30 10:56:47 -07001313 LinkProperties p = nt.getLinkProperties();
Robert Greenwalta7dfbd32010-06-15 15:43:39 -07001314 if (p == null) return;
1315 String interfaceName = p.getInterfaceName();
Irfan Sheriff7f132d92010-06-09 15:39:36 -07001316
Robert Greenwalt3afbead2010-07-23 15:46:26 -07001317 if (interfaceName != null) {
1318 if ((NetworkUtils.removeDefaultRoute(interfaceName) >= 0) && DBG) {
Robert Greenwalta7dfbd32010-06-15 15:43:39 -07001319 NetworkInfo networkInfo = nt.getNetworkInfo();
Irfan Sheriff7f132d92010-06-09 15:39:36 -07001320 Slog.d(TAG, "removeDefaultRoute for " + networkInfo.getTypeName() + " (" +
1321 interfaceName + ")");
1322 }
Irfan Sheriff7f132d92010-06-09 15:39:36 -07001323 }
1324 }
1325
1326 /**
1327 * Reads the network specific TCP buffer sizes from SystemProperties
1328 * net.tcp.buffersize.[default|wifi|umts|edge|gprs] and set them for system
1329 * wide use
1330 */
1331 public void updateNetworkSettings(NetworkStateTracker nt) {
1332 String key = nt.getTcpBufferSizesPropName();
1333 String bufferSizes = SystemProperties.get(key);
1334
1335 if (bufferSizes.length() == 0) {
1336 Slog.e(TAG, key + " not found in system properties. Using defaults");
1337
1338 // Setting to default values so we won't be stuck to previous values
1339 key = "net.tcp.buffersize.default";
1340 bufferSizes = SystemProperties.get(key);
1341 }
1342
1343 // Set values in kernel
1344 if (bufferSizes.length() != 0) {
1345 if (DBG) {
1346 Slog.v(TAG, "Setting TCP values: [" + bufferSizes
1347 + "] which comes from [" + key + "]");
1348 }
1349 setBufferSize(bufferSizes);
1350 }
1351 }
1352
1353 /**
1354 * Writes TCP buffer sizes to /sys/kernel/ipv4/tcp_[r/w]mem_[min/def/max]
1355 * which maps to /proc/sys/net/ipv4/tcp_rmem and tcpwmem
1356 *
1357 * @param bufferSizes in the format of "readMin, readInitial, readMax,
1358 * writeMin, writeInitial, writeMax"
1359 */
1360 private void setBufferSize(String bufferSizes) {
1361 try {
1362 String[] values = bufferSizes.split(",");
1363
1364 if (values.length == 6) {
1365 final String prefix = "/sys/kernel/ipv4/tcp_";
1366 stringToFile(prefix + "rmem_min", values[0]);
1367 stringToFile(prefix + "rmem_def", values[1]);
1368 stringToFile(prefix + "rmem_max", values[2]);
1369 stringToFile(prefix + "wmem_min", values[3]);
1370 stringToFile(prefix + "wmem_def", values[4]);
1371 stringToFile(prefix + "wmem_max", values[5]);
1372 } else {
1373 Slog.e(TAG, "Invalid buffersize string: " + bufferSizes);
1374 }
1375 } catch (IOException e) {
1376 Slog.e(TAG, "Can't set tcp buffer sizes:" + e);
1377 }
1378 }
1379
1380 /**
1381 * Writes string to file. Basically same as "echo -n $string > $filename"
1382 *
1383 * @param filename
1384 * @param string
1385 * @throws IOException
1386 */
1387 private void stringToFile(String filename, String string) throws IOException {
1388 FileWriter out = new FileWriter(filename);
1389 try {
1390 out.write(string);
1391 } finally {
1392 out.close();
1393 }
1394 }
1395
1396
Robert Greenwalt2034b912009-08-12 16:08:25 -07001397 /**
1398 * Adjust the per-process dns entries (net.dns<x>.<pid>) based
1399 * on the highest priority active net which this process requested.
1400 * If there aren't any, clear it out
1401 */
1402 private void reassessPidDns(int myPid, boolean doBump)
1403 {
Joe Onoratoc2386bb2010-02-26 18:56:32 -08001404 if (DBG) Slog.d(TAG, "reassessPidDns for pid " + myPid);
Robert Greenwalt2034b912009-08-12 16:08:25 -07001405 for(int i : mPriorityList) {
1406 if (mNetAttributes[i].isDefault()) {
1407 continue;
1408 }
1409 NetworkStateTracker nt = mNetTrackers[i];
Robert Greenwalt0659da32009-07-16 17:21:39 -07001410 if (nt.getNetworkInfo().isConnected() &&
1411 !nt.isTeardownRequested()) {
Robert Greenwalt1f1bcfe2010-08-30 10:56:47 -07001412 LinkProperties p = nt.getLinkProperties();
Robert Greenwalta7dfbd32010-06-15 15:43:39 -07001413 if (p == null) continue;
Robert Greenwalt2034b912009-08-12 16:08:25 -07001414 List pids = mNetRequestersPids[i];
1415 for (int j=0; j<pids.size(); j++) {
1416 Integer pid = (Integer)pids.get(j);
1417 if (pid.intValue() == myPid) {
Robert Greenwalta7dfbd32010-06-15 15:43:39 -07001418 Collection<InetAddress> dnses = p.getDnses();
1419 writePidDns(dnses, myPid);
Robert Greenwalt2034b912009-08-12 16:08:25 -07001420 if (doBump) {
1421 bumpDns();
1422 }
1423 return;
1424 }
1425 }
1426 }
1427 }
1428 // nothing found - delete
1429 for (int i = 1; ; i++) {
1430 String prop = "net.dns" + i + "." + myPid;
1431 if (SystemProperties.get(prop).length() == 0) {
1432 if (doBump) {
1433 bumpDns();
1434 }
1435 return;
1436 }
1437 SystemProperties.set(prop, "");
1438 }
1439 }
1440
Robert Greenwalta7dfbd32010-06-15 15:43:39 -07001441 private void writePidDns(Collection <InetAddress> dnses, int pid) {
Robert Greenwalt2034b912009-08-12 16:08:25 -07001442 int j = 1;
Robert Greenwalta7dfbd32010-06-15 15:43:39 -07001443 for (InetAddress dns : dnses) {
1444 SystemProperties.set("net.dns" + j++ + "." + pid, dns.getHostAddress());
Robert Greenwalt2034b912009-08-12 16:08:25 -07001445 }
1446 }
1447
1448 private void bumpDns() {
1449 /*
1450 * Bump the property that tells the name resolver library to reread
1451 * the DNS server list from the properties.
1452 */
1453 String propVal = SystemProperties.get("net.dnschange");
1454 int n = 0;
1455 if (propVal.length() != 0) {
1456 try {
1457 n = Integer.parseInt(propVal);
1458 } catch (NumberFormatException e) {}
1459 }
1460 SystemProperties.set("net.dnschange", "" + (n+1));
1461 }
1462
Robert Greenwalt3afbead2010-07-23 15:46:26 -07001463 private void handleDnsConfigurationChange(int netType) {
Robert Greenwalt2034b912009-08-12 16:08:25 -07001464 // add default net's dns entries
Robert Greenwalt3afbead2010-07-23 15:46:26 -07001465 NetworkStateTracker nt = mNetTrackers[netType];
1466 if (nt != null && nt.getNetworkInfo().isConnected() && !nt.isTeardownRequested()) {
Robert Greenwalt1f1bcfe2010-08-30 10:56:47 -07001467 LinkProperties p = nt.getLinkProperties();
Robert Greenwalt3afbead2010-07-23 15:46:26 -07001468 if (p == null) return;
1469 Collection<InetAddress> dnses = p.getDnses();
1470 if (mNetAttributes[netType].isDefault()) {
1471 int j = 1;
1472 for (InetAddress dns : dnses) {
1473 if (DBG) {
1474 Slog.d(TAG, "adding dns " + dns + " for " +
1475 nt.getNetworkInfo().getTypeName());
Robert Greenwalt2034b912009-08-12 16:08:25 -07001476 }
Robert Greenwalt3afbead2010-07-23 15:46:26 -07001477 SystemProperties.set("net.dns" + j++, dns.getHostAddress());
1478 }
1479 for (int k=j ; k<mNumDnsEntries; k++) {
1480 if (DBG) Slog.d(TAG, "erasing net.dns" + k);
1481 SystemProperties.set("net.dns" + k, "");
1482 }
1483 mNumDnsEntries = j;
1484 } else {
1485 // set per-pid dns for attached secondary nets
1486 List pids = mNetRequestersPids[netType];
1487 for (int y=0; y< pids.size(); y++) {
1488 Integer pid = (Integer)pids.get(y);
1489 writePidDns(dnses, pid.intValue());
The Android Open Source Project28527d22009-03-03 19:31:44 -08001490 }
1491 }
Robert Greenwalt3afbead2010-07-23 15:46:26 -07001492 bumpDns();
The Android Open Source Project28527d22009-03-03 19:31:44 -08001493 }
Robert Greenwalt2034b912009-08-12 16:08:25 -07001494 }
1495
1496 private int getRestoreDefaultNetworkDelay() {
1497 String restoreDefaultNetworkDelayStr = SystemProperties.get(
1498 NETWORK_RESTORE_DELAY_PROP_NAME);
1499 if(restoreDefaultNetworkDelayStr != null &&
1500 restoreDefaultNetworkDelayStr.length() != 0) {
1501 try {
1502 return Integer.valueOf(restoreDefaultNetworkDelayStr);
1503 } catch (NumberFormatException e) {
1504 }
The Android Open Source Project28527d22009-03-03 19:31:44 -08001505 }
Robert Greenwalt2034b912009-08-12 16:08:25 -07001506 return RESTORE_DEFAULT_NETWORK_DELAY;
The Android Open Source Project28527d22009-03-03 19:31:44 -08001507 }
1508
1509 @Override
1510 protected void dump(FileDescriptor fd, PrintWriter pw, String[] args) {
Robert Greenwalt0659da32009-07-16 17:21:39 -07001511 if (mContext.checkCallingOrSelfPermission(
1512 android.Manifest.permission.DUMP)
The Android Open Source Project28527d22009-03-03 19:31:44 -08001513 != PackageManager.PERMISSION_GRANTED) {
Robert Greenwalt0659da32009-07-16 17:21:39 -07001514 pw.println("Permission Denial: can't dump ConnectivityService " +
1515 "from from pid=" + Binder.getCallingPid() + ", uid=" +
1516 Binder.getCallingUid());
The Android Open Source Project28527d22009-03-03 19:31:44 -08001517 return;
1518 }
The Android Open Source Project28527d22009-03-03 19:31:44 -08001519 pw.println();
1520 for (NetworkStateTracker nst : mNetTrackers) {
Robert Greenwalt3eeb6032009-12-21 18:24:07 -08001521 if (nst != null) {
1522 if (nst.getNetworkInfo().isConnected()) {
1523 pw.println("Active network: " + nst.getNetworkInfo().
1524 getTypeName());
1525 }
1526 pw.println(nst.getNetworkInfo());
1527 pw.println(nst);
1528 pw.println();
Robert Greenwalt2034b912009-08-12 16:08:25 -07001529 }
The Android Open Source Project28527d22009-03-03 19:31:44 -08001530 }
Robert Greenwalt3eeb6032009-12-21 18:24:07 -08001531
1532 pw.println("Network Requester Pids:");
1533 for (int net : mPriorityList) {
1534 String pidString = net + ": ";
1535 for (Object pid : mNetRequestersPids[net]) {
1536 pidString = pidString + pid.toString() + ", ";
1537 }
1538 pw.println(pidString);
1539 }
1540 pw.println();
1541
1542 pw.println("FeatureUsers:");
1543 for (Object requester : mFeatureUsers) {
1544 pw.println(requester.toString());
1545 }
1546 pw.println();
Robert Greenwalt8e87f122010-02-11 18:18:40 -08001547
Robert Greenwalt93dc1042010-06-15 12:19:37 -07001548 synchronized (this) {
1549 pw.println("NetworkTranstionWakeLock is currently " +
1550 (mNetTransitionWakeLock.isHeld() ? "" : "not ") + "held.");
1551 pw.println("It was last requested for "+mNetTransitionWakeLockCausedBy);
1552 }
1553 pw.println();
1554
Robert Greenwalt8e87f122010-02-11 18:18:40 -08001555 mTethering.dump(fd, pw, args);
The Android Open Source Project28527d22009-03-03 19:31:44 -08001556 }
1557
Robert Greenwalt2034b912009-08-12 16:08:25 -07001558 // must be stateless - things change under us.
The Android Open Source Project28527d22009-03-03 19:31:44 -08001559 private class MyHandler extends Handler {
1560 @Override
1561 public void handleMessage(Message msg) {
1562 NetworkInfo info;
1563 switch (msg.what) {
1564 case NetworkStateTracker.EVENT_STATE_CHANGED:
1565 info = (NetworkInfo) msg.obj;
Robert Greenwalt12c44552009-12-07 11:33:18 -08001566 int type = info.getType();
1567 NetworkInfo.State state = info.getState();
Robert Greenwalt24e2d2b2010-01-25 16:14:00 -08001568 // only do this optimization for wifi. It going into scan mode for location
1569 // services generates alot of noise. Meanwhile the mms apn won't send out
1570 // subsequent notifications when on default cellular because it never
1571 // disconnects.. so only do this to wifi notifications. Fixed better when the
1572 // APN notifications are standardized.
1573 if (mNetAttributes[type].mLastState == state &&
1574 mNetAttributes[type].mRadio == ConnectivityManager.TYPE_WIFI) {
Robert Greenwalt12c44552009-12-07 11:33:18 -08001575 if (DBG) {
Robert Greenwalt24e2d2b2010-01-25 16:14:00 -08001576 // TODO - remove this after we validate the dropping doesn't break
1577 // anything
Joe Onoratoc2386bb2010-02-26 18:56:32 -08001578 Slog.d(TAG, "Dropping ConnectivityChange for " +
Robert Greenwalt2adbc7f2010-01-13 09:36:31 -08001579 info.getTypeName() + ": " +
Robert Greenwalt12c44552009-12-07 11:33:18 -08001580 state + "/" + info.getDetailedState());
1581 }
1582 return;
1583 }
1584 mNetAttributes[type].mLastState = state;
1585
Joe Onoratoc2386bb2010-02-26 18:56:32 -08001586 if (DBG) Slog.d(TAG, "ConnectivityChange for " +
Robert Greenwalt0659da32009-07-16 17:21:39 -07001587 info.getTypeName() + ": " +
Robert Greenwalt12c44552009-12-07 11:33:18 -08001588 state + "/" + info.getDetailedState());
The Android Open Source Project28527d22009-03-03 19:31:44 -08001589
1590 // Connectivity state changed:
1591 // [31-13] Reserved for future use
Robert Greenwalt0659da32009-07-16 17:21:39 -07001592 // [12-9] Network subtype (for mobile network, as defined
1593 // by TelephonyManager)
1594 // [8-3] Detailed state ordinal (as defined by
1595 // NetworkInfo.DetailedState)
The Android Open Source Project28527d22009-03-03 19:31:44 -08001596 // [2-0] Network type (as defined by ConnectivityManager)
1597 int eventLogParam = (info.getType() & 0x7) |
1598 ((info.getDetailedState().ordinal() & 0x3f) << 3) |
1599 (info.getSubtype() << 9);
Doug Zongker2fc96232009-12-04 10:31:43 -08001600 EventLog.writeEvent(EventLogTags.CONNECTIVITY_STATE_CHANGED,
Robert Greenwalt0659da32009-07-16 17:21:39 -07001601 eventLogParam);
1602
1603 if (info.getDetailedState() ==
1604 NetworkInfo.DetailedState.FAILED) {
The Android Open Source Project28527d22009-03-03 19:31:44 -08001605 handleConnectionFailure(info);
Robert Greenwalt12c44552009-12-07 11:33:18 -08001606 } else if (state == NetworkInfo.State.DISCONNECTED) {
The Android Open Source Project28527d22009-03-03 19:31:44 -08001607 handleDisconnect(info);
Robert Greenwalt12c44552009-12-07 11:33:18 -08001608 } else if (state == NetworkInfo.State.SUSPENDED) {
The Android Open Source Project28527d22009-03-03 19:31:44 -08001609 // TODO: need to think this over.
Robert Greenwalt0659da32009-07-16 17:21:39 -07001610 // the logic here is, handle SUSPENDED the same as
1611 // DISCONNECTED. The only difference being we are
1612 // broadcasting an intent with NetworkInfo that's
1613 // suspended. This allows the applications an
1614 // opportunity to handle DISCONNECTED and SUSPENDED
1615 // differently, or not.
The Android Open Source Project28527d22009-03-03 19:31:44 -08001616 handleDisconnect(info);
Robert Greenwalt12c44552009-12-07 11:33:18 -08001617 } else if (state == NetworkInfo.State.CONNECTED) {
The Android Open Source Project28527d22009-03-03 19:31:44 -08001618 handleConnect(info);
1619 }
The Android Open Source Project28527d22009-03-03 19:31:44 -08001620 break;
The Android Open Source Project28527d22009-03-03 19:31:44 -08001621 case NetworkStateTracker.EVENT_CONFIGURATION_CHANGED:
Robert Greenwalt3afbead2010-07-23 15:46:26 -07001622 // TODO - make this handle ip/proxy/gateway/dns changes
1623 info = (NetworkInfo) msg.obj;
1624 type = info.getType();
1625 handleDnsConfigurationChange(type);
The Android Open Source Project28527d22009-03-03 19:31:44 -08001626 break;
Robert Greenwalt2034b912009-08-12 16:08:25 -07001627 case NetworkStateTracker.EVENT_RESTORE_DEFAULT_NETWORK:
Robert Greenwaltaffc3a12009-09-27 17:27:04 -07001628 FeatureUser u = (FeatureUser)msg.obj;
1629 u.expire();
Robert Greenwalt2034b912009-08-12 16:08:25 -07001630 break;
Robert Greenwalt93dc1042010-06-15 12:19:37 -07001631 case NetworkStateTracker.EVENT_CLEAR_NET_TRANSITION_WAKELOCK:
1632 String causedBy = null;
1633 synchronized (ConnectivityService.this) {
1634 if (msg.arg1 == mNetTransitionWakeLockSerialNumber &&
1635 mNetTransitionWakeLock.isHeld()) {
1636 mNetTransitionWakeLock.release();
1637 causedBy = mNetTransitionWakeLockCausedBy;
1638 }
1639 }
1640 if (causedBy != null) {
1641 Slog.d(TAG, "NetTransition Wakelock for " +
1642 causedBy + " released by timeout");
1643 }
1644 break;
The Android Open Source Project28527d22009-03-03 19:31:44 -08001645 }
1646 }
1647 }
Robert Greenwalt0c4828c2010-01-26 11:40:34 -08001648
1649 // javadoc from interface
Robert Greenwalt4283ded2010-03-02 17:25:02 -08001650 public int tether(String iface) {
Robert Greenwalt0c4828c2010-01-26 11:40:34 -08001651 enforceTetherChangePermission();
Robert Greenwalt4283ded2010-03-02 17:25:02 -08001652
1653 if (isTetheringSupported()) {
1654 return mTethering.tether(iface);
1655 } else {
1656 return ConnectivityManager.TETHER_ERROR_UNSUPPORTED;
1657 }
Robert Greenwalt0c4828c2010-01-26 11:40:34 -08001658 }
1659
1660 // javadoc from interface
Robert Greenwalt4283ded2010-03-02 17:25:02 -08001661 public int untether(String iface) {
Robert Greenwalt0c4828c2010-01-26 11:40:34 -08001662 enforceTetherChangePermission();
Robert Greenwalt4283ded2010-03-02 17:25:02 -08001663
1664 if (isTetheringSupported()) {
1665 return mTethering.untether(iface);
1666 } else {
1667 return ConnectivityManager.TETHER_ERROR_UNSUPPORTED;
1668 }
1669 }
1670
1671 // javadoc from interface
1672 public int getLastTetherError(String iface) {
1673 enforceTetherAccessPermission();
1674
1675 if (isTetheringSupported()) {
1676 return mTethering.getLastTetherError(iface);
1677 } else {
1678 return ConnectivityManager.TETHER_ERROR_UNSUPPORTED;
1679 }
Robert Greenwalt8e87f122010-02-11 18:18:40 -08001680 }
1681
1682 // TODO - proper iface API for selection by property, inspection, etc
1683 public String[] getTetherableUsbRegexs() {
1684 enforceTetherAccessPermission();
1685 if (isTetheringSupported()) {
1686 return mTethering.getTetherableUsbRegexs();
1687 } else {
1688 return new String[0];
1689 }
1690 }
1691
1692 public String[] getTetherableWifiRegexs() {
1693 enforceTetherAccessPermission();
1694 if (isTetheringSupported()) {
1695 return mTethering.getTetherableWifiRegexs();
1696 } else {
1697 return new String[0];
1698 }
Robert Greenwalt0c4828c2010-01-26 11:40:34 -08001699 }
1700
Danica Chang96567052010-08-11 14:54:43 -07001701 public String[] getTetherableBluetoothRegexs() {
1702 enforceTetherAccessPermission();
1703 if (isTetheringSupported()) {
1704 return mTethering.getTetherableBluetoothRegexs();
1705 } else {
1706 return new String[0];
1707 }
1708 }
1709
Robert Greenwalt0c4828c2010-01-26 11:40:34 -08001710 // TODO - move iface listing, queries, etc to new module
1711 // javadoc from interface
1712 public String[] getTetherableIfaces() {
Robert Greenwalt8e87f122010-02-11 18:18:40 -08001713 enforceTetherAccessPermission();
Robert Greenwalt0c4828c2010-01-26 11:40:34 -08001714 return mTethering.getTetherableIfaces();
1715 }
1716
1717 public String[] getTetheredIfaces() {
Robert Greenwalt8e87f122010-02-11 18:18:40 -08001718 enforceTetherAccessPermission();
Robert Greenwalt0c4828c2010-01-26 11:40:34 -08001719 return mTethering.getTetheredIfaces();
1720 }
Robert Greenwalt8e87f122010-02-11 18:18:40 -08001721
Robert Greenwalt4283ded2010-03-02 17:25:02 -08001722 public String[] getTetheringErroredIfaces() {
1723 enforceTetherAccessPermission();
1724 return mTethering.getErroredIfaces();
1725 }
1726
Robert Greenwalt8e87f122010-02-11 18:18:40 -08001727 // if ro.tether.denied = true we default to no tethering
1728 // gservices could set the secure setting to 1 though to enable it on a build where it
1729 // had previously been turned off.
1730 public boolean isTetheringSupported() {
1731 enforceTetherAccessPermission();
1732 int defaultVal = (SystemProperties.get("ro.tether.denied").equals("true") ? 0 : 1);
Robert Greenwaltf1b66e12010-02-25 12:29:30 -08001733 boolean tetherEnabledInSettings = (Settings.Secure.getInt(mContext.getContentResolver(),
1734 Settings.Secure.TETHER_SUPPORTED, defaultVal) != 0);
1735 return tetherEnabledInSettings && mTetheringConfigValid;
Robert Greenwalt8e87f122010-02-11 18:18:40 -08001736 }
Robert Greenwalt93dc1042010-06-15 12:19:37 -07001737
1738 // An API NetworkStateTrackers can call when they lose their network.
1739 // This will automatically be cleared after X seconds or a network becomes CONNECTED,
1740 // whichever happens first. The timer is started by the first caller and not
1741 // restarted by subsequent callers.
1742 public void requestNetworkTransitionWakelock(String forWhom) {
1743 enforceConnectivityInternalPermission();
1744 synchronized (this) {
1745 if (mNetTransitionWakeLock.isHeld()) return;
1746 mNetTransitionWakeLockSerialNumber++;
1747 mNetTransitionWakeLock.acquire();
1748 mNetTransitionWakeLockCausedBy = forWhom;
1749 }
1750 mHandler.sendMessageDelayed(mHandler.obtainMessage(
1751 NetworkStateTracker.EVENT_CLEAR_NET_TRANSITION_WAKELOCK,
1752 mNetTransitionWakeLockSerialNumber, 0),
1753 mNetTransitionWakeLockTimeout);
1754 return;
1755 }
The Android Open Source Project28527d22009-03-03 19:31:44 -08001756}