blob: 4da0bac4ea52188d11a0db7ed8840440de7f379c [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;
31import android.net.wifi.WifiStateTracker;
Irfan Sheriff7f132d92010-06-09 15:39:36 -070032import android.net.NetworkUtils;
The Android Open Source Project28527d22009-03-03 19:31:44 -080033import android.os.Binder;
34import android.os.Handler;
Robert Greenwalt2034b912009-08-12 16:08:25 -070035import android.os.IBinder;
The Android Open Source Project28527d22009-03-03 19:31:44 -080036import android.os.Looper;
37import android.os.Message;
Robert Greenwalt93dc1042010-06-15 12:19:37 -070038import android.os.PowerManager;
Robert Greenwalt2034b912009-08-12 16:08:25 -070039import android.os.RemoteException;
The Android Open Source Project28527d22009-03-03 19:31:44 -080040import android.os.ServiceManager;
41import android.os.SystemProperties;
42import android.provider.Settings;
Robert Greenwalt2034b912009-08-12 16:08:25 -070043import android.text.TextUtils;
The Android Open Source Project28527d22009-03-03 19:31:44 -080044import android.util.EventLog;
Joe Onoratoc2386bb2010-02-26 18:56:32 -080045import android.util.Slog;
The Android Open Source Project28527d22009-03-03 19:31:44 -080046
Robert Greenwalt2034b912009-08-12 16:08:25 -070047import com.android.internal.telephony.Phone;
48
Robert Greenwalt0c4828c2010-01-26 11:40:34 -080049import com.android.server.connectivity.Tethering;
50
The Android Open Source Project28527d22009-03-03 19:31:44 -080051import java.io.FileDescriptor;
Irfan Sheriff7f132d92010-06-09 15:39:36 -070052import java.io.FileWriter;
53import java.io.IOException;
The Android Open Source Project28527d22009-03-03 19:31:44 -080054import java.io.PrintWriter;
Robert Greenwalta7dfbd32010-06-15 15:43:39 -070055import java.net.InetAddress;
56import java.net.UnknownHostException;
Robert Greenwalt2034b912009-08-12 16:08:25 -070057import java.util.ArrayList;
Robert Greenwalta7dfbd32010-06-15 15:43:39 -070058import java.util.Collection;
Robert Greenwalt2034b912009-08-12 16:08:25 -070059import java.util.List;
The Android Open Source Project28527d22009-03-03 19:31:44 -080060
61/**
62 * @hide
63 */
64public class ConnectivityService extends IConnectivityManager.Stub {
65
Robert Greenwalta25fd712009-10-06 14:12:53 -070066 private static final boolean DBG = true;
The Android Open Source Project28527d22009-03-03 19:31:44 -080067 private static final String TAG = "ConnectivityService";
68
Robert Greenwalt2034b912009-08-12 16:08:25 -070069 // how long to wait before switching back to a radio's default network
70 private static final int RESTORE_DEFAULT_NETWORK_DELAY = 1 * 60 * 1000;
71 // system property that can override the above value
72 private static final String NETWORK_RESTORE_DELAY_PROP_NAME =
73 "android.telephony.apn-restore";
74
Robert Greenwalt0c4828c2010-01-26 11:40:34 -080075 private Tethering mTethering;
Robert Greenwaltf1b66e12010-02-25 12:29:30 -080076 private boolean mTetheringConfigValid = false;
Robert Greenwalt0c4828c2010-01-26 11:40:34 -080077
The Android Open Source Project28527d22009-03-03 19:31:44 -080078 /**
79 * Sometimes we want to refer to the individual network state
80 * trackers separately, and sometimes we just want to treat them
81 * abstractly.
82 */
83 private NetworkStateTracker mNetTrackers[];
Robert Greenwalt2034b912009-08-12 16:08:25 -070084
85 /**
86 * A per Net list of the PID's that requested access to the net
87 * used both as a refcount and for per-PID DNS selection
88 */
89 private List mNetRequestersPids[];
90
Irfan Sheriff653e2a22010-06-07 09:03:04 -070091 private WifiWatchdogService mWifiWatchdogService;
92
Robert Greenwalt2034b912009-08-12 16:08:25 -070093 // priority order of the nettrackers
94 // (excluding dynamically set mNetworkPreference)
95 // TODO - move mNetworkTypePreference into this
96 private int[] mPriorityList;
97
The Android Open Source Project28527d22009-03-03 19:31:44 -080098 private Context mContext;
99 private int mNetworkPreference;
Robert Greenwalt2034b912009-08-12 16:08:25 -0700100 private int mActiveDefaultNetwork = -1;
The Android Open Source Project28527d22009-03-03 19:31:44 -0800101
102 private int mNumDnsEntries;
The Android Open Source Project28527d22009-03-03 19:31:44 -0800103
104 private boolean mTestMode;
105 private static ConnectivityService sServiceInstance;
106
Robert Greenwalt2034b912009-08-12 16:08:25 -0700107 private Handler mHandler;
108
109 // list of DeathRecipients used to make sure features are turned off when
110 // a process dies
111 private List mFeatureUsers;
112
Mike Lockwoodfde2b762009-08-14 14:18:49 -0400113 private boolean mSystemReady;
Dianne Hackborna417ff82009-12-08 19:45:14 -0800114 private Intent mInitialBroadcast;
Mike Lockwoodfde2b762009-08-14 14:18:49 -0400115
Robert Greenwalt93dc1042010-06-15 12:19:37 -0700116 private PowerManager.WakeLock mNetTransitionWakeLock;
117 private String mNetTransitionWakeLockCausedBy = "";
118 private int mNetTransitionWakeLockSerialNumber;
119 private int mNetTransitionWakeLockTimeout;
120
Robert Greenwalt12c44552009-12-07 11:33:18 -0800121 private static class NetworkAttributes {
Robert Greenwalt2034b912009-08-12 16:08:25 -0700122 /**
123 * Class for holding settings read from resources.
124 */
125 public String mName;
126 public int mType;
127 public int mRadio;
128 public int mPriority;
Robert Greenwalt12c44552009-12-07 11:33:18 -0800129 public NetworkInfo.State mLastState;
Robert Greenwalt2034b912009-08-12 16:08:25 -0700130 public NetworkAttributes(String init) {
131 String fragments[] = init.split(",");
132 mName = fragments[0].toLowerCase();
Robert Greenwaltec05b3c2009-10-30 14:17:42 -0700133 mType = Integer.parseInt(fragments[1]);
134 mRadio = Integer.parseInt(fragments[2]);
135 mPriority = Integer.parseInt(fragments[3]);
Robert Greenwalt12c44552009-12-07 11:33:18 -0800136 mLastState = NetworkInfo.State.UNKNOWN;
Robert Greenwalt2034b912009-08-12 16:08:25 -0700137 }
138 public boolean isDefault() {
139 return (mType == mRadio);
140 }
141 }
142 NetworkAttributes[] mNetAttributes;
Robert Greenwaltec05b3c2009-10-30 14:17:42 -0700143 int mNetworksDefined;
Robert Greenwalt2034b912009-08-12 16:08:25 -0700144
Robert Greenwalt12c44552009-12-07 11:33:18 -0800145 private static class RadioAttributes {
Robert Greenwalt2034b912009-08-12 16:08:25 -0700146 public int mSimultaneity;
147 public int mType;
148 public RadioAttributes(String init) {
149 String fragments[] = init.split(",");
Robert Greenwaltec05b3c2009-10-30 14:17:42 -0700150 mType = Integer.parseInt(fragments[0]);
151 mSimultaneity = Integer.parseInt(fragments[1]);
Robert Greenwalt2034b912009-08-12 16:08:25 -0700152 }
153 }
154 RadioAttributes[] mRadioAttributes;
155
The Android Open Source Project28527d22009-03-03 19:31:44 -0800156 private static class ConnectivityThread extends Thread {
157 private Context mContext;
Robert Greenwalt0659da32009-07-16 17:21:39 -0700158
The Android Open Source Project28527d22009-03-03 19:31:44 -0800159 private ConnectivityThread(Context context) {
160 super("ConnectivityThread");
161 mContext = context;
162 }
163
164 @Override
165 public void run() {
166 Looper.prepare();
167 synchronized (this) {
168 sServiceInstance = new ConnectivityService(mContext);
169 notifyAll();
170 }
171 Looper.loop();
172 }
Robert Greenwalt0659da32009-07-16 17:21:39 -0700173
The Android Open Source Project28527d22009-03-03 19:31:44 -0800174 public static ConnectivityService getServiceInstance(Context context) {
175 ConnectivityThread thread = new ConnectivityThread(context);
176 thread.start();
Robert Greenwalt0659da32009-07-16 17:21:39 -0700177
The Android Open Source Project28527d22009-03-03 19:31:44 -0800178 synchronized (thread) {
179 while (sServiceInstance == null) {
180 try {
181 // Wait until sServiceInstance has been initialized.
182 thread.wait();
183 } catch (InterruptedException ignore) {
Joe Onoratoc2386bb2010-02-26 18:56:32 -0800184 Slog.e(TAG,
Robert Greenwalt0659da32009-07-16 17:21:39 -0700185 "Unexpected InterruptedException while waiting"+
186 " for ConnectivityService thread");
The Android Open Source Project28527d22009-03-03 19:31:44 -0800187 }
188 }
189 }
Robert Greenwalt0659da32009-07-16 17:21:39 -0700190
The Android Open Source Project28527d22009-03-03 19:31:44 -0800191 return sServiceInstance;
192 }
193 }
Robert Greenwalt0659da32009-07-16 17:21:39 -0700194
The Android Open Source Project28527d22009-03-03 19:31:44 -0800195 public static ConnectivityService getInstance(Context context) {
196 return ConnectivityThread.getServiceInstance(context);
197 }
Robert Greenwalt0659da32009-07-16 17:21:39 -0700198
The Android Open Source Project28527d22009-03-03 19:31:44 -0800199 private ConnectivityService(Context context) {
Joe Onoratoc2386bb2010-02-26 18:56:32 -0800200 if (DBG) Slog.v(TAG, "ConnectivityService starting up");
Robert Greenwaltd48f8ee2010-01-14 17:47:58 -0800201
202 // setup our unique device name
203 String id = Settings.Secure.getString(context.getContentResolver(),
204 Settings.Secure.ANDROID_ID);
205 if (id != null && id.length() > 0) {
206 String name = new String("android_").concat(id);
207 SystemProperties.set("net.hostname", name);
208 }
209
The Android Open Source Project28527d22009-03-03 19:31:44 -0800210 mContext = context;
Robert Greenwalt93dc1042010-06-15 12:19:37 -0700211
212 PowerManager powerManager = (PowerManager)mContext.getSystemService(Context.POWER_SERVICE);
213 mNetTransitionWakeLock = powerManager.newWakeLock(PowerManager.PARTIAL_WAKE_LOCK, TAG);
214 mNetTransitionWakeLockTimeout = mContext.getResources().getInteger(
215 com.android.internal.R.integer.config_networkTransitionTimeout);
216
Robert Greenwalt2034b912009-08-12 16:08:25 -0700217 mNetTrackers = new NetworkStateTracker[
218 ConnectivityManager.MAX_NETWORK_TYPE+1];
219 mHandler = new MyHandler();
Robert Greenwalt0659da32009-07-16 17:21:39 -0700220
The Android Open Source Project28527d22009-03-03 19:31:44 -0800221 mNetworkPreference = getPersistedNetworkPreference();
Robert Greenwalt0659da32009-07-16 17:21:39 -0700222
Robert Greenwaltec05b3c2009-10-30 14:17:42 -0700223 mRadioAttributes = new RadioAttributes[ConnectivityManager.MAX_RADIO_TYPE+1];
224 mNetAttributes = new NetworkAttributes[ConnectivityManager.MAX_NETWORK_TYPE+1];
225
Robert Greenwalt2034b912009-08-12 16:08:25 -0700226 // Load device network attributes from resources
Robert Greenwalt2034b912009-08-12 16:08:25 -0700227 String[] raStrings = context.getResources().getStringArray(
228 com.android.internal.R.array.radioAttributes);
Robert Greenwaltec05b3c2009-10-30 14:17:42 -0700229 for (String raString : raStrings) {
230 RadioAttributes r = new RadioAttributes(raString);
231 if (r.mType > ConnectivityManager.MAX_RADIO_TYPE) {
Joe Onoratoc2386bb2010-02-26 18:56:32 -0800232 Slog.e(TAG, "Error in radioAttributes - ignoring attempt to define type " + r.mType);
Robert Greenwaltec05b3c2009-10-30 14:17:42 -0700233 continue;
234 }
235 if (mRadioAttributes[r.mType] != null) {
Joe Onoratoc2386bb2010-02-26 18:56:32 -0800236 Slog.e(TAG, "Error in radioAttributes - ignoring attempt to redefine type " +
Robert Greenwaltec05b3c2009-10-30 14:17:42 -0700237 r.mType);
238 continue;
239 }
Robert Greenwalt2034b912009-08-12 16:08:25 -0700240 mRadioAttributes[r.mType] = r;
241 }
242
Robert Greenwaltec05b3c2009-10-30 14:17:42 -0700243 String[] naStrings = context.getResources().getStringArray(
244 com.android.internal.R.array.networkAttributes);
245 for (String naString : naStrings) {
246 try {
247 NetworkAttributes n = new NetworkAttributes(naString);
248 if (n.mType > ConnectivityManager.MAX_NETWORK_TYPE) {
Joe Onoratoc2386bb2010-02-26 18:56:32 -0800249 Slog.e(TAG, "Error in networkAttributes - ignoring attempt to define type " +
Robert Greenwaltec05b3c2009-10-30 14:17:42 -0700250 n.mType);
251 continue;
Robert Greenwalt2034b912009-08-12 16:08:25 -0700252 }
Robert Greenwaltec05b3c2009-10-30 14:17:42 -0700253 if (mNetAttributes[n.mType] != null) {
Joe Onoratoc2386bb2010-02-26 18:56:32 -0800254 Slog.e(TAG, "Error in networkAttributes - ignoring attempt to redefine type " +
Robert Greenwaltec05b3c2009-10-30 14:17:42 -0700255 n.mType);
256 continue;
257 }
258 if (mRadioAttributes[n.mRadio] == null) {
Joe Onoratoc2386bb2010-02-26 18:56:32 -0800259 Slog.e(TAG, "Error in networkAttributes - ignoring attempt to use undefined " +
Robert Greenwaltec05b3c2009-10-30 14:17:42 -0700260 "radio " + n.mRadio + " in network type " + n.mType);
261 continue;
262 }
263 mNetAttributes[n.mType] = n;
264 mNetworksDefined++;
265 } catch(Exception e) {
266 // ignore it - leave the entry null
Robert Greenwalt2034b912009-08-12 16:08:25 -0700267 }
268 }
269
Robert Greenwaltec05b3c2009-10-30 14:17:42 -0700270 // high priority first
271 mPriorityList = new int[mNetworksDefined];
272 {
273 int insertionPoint = mNetworksDefined-1;
274 int currentLowest = 0;
275 int nextLowest = 0;
276 while (insertionPoint > -1) {
277 for (NetworkAttributes na : mNetAttributes) {
278 if (na == null) continue;
279 if (na.mPriority < currentLowest) continue;
280 if (na.mPriority > currentLowest) {
281 if (na.mPriority < nextLowest || nextLowest == 0) {
282 nextLowest = na.mPriority;
283 }
284 continue;
285 }
286 mPriorityList[insertionPoint--] = na.mType;
287 }
288 currentLowest = nextLowest;
289 nextLowest = 0;
290 }
291 }
292
293 mNetRequestersPids = new ArrayList[ConnectivityManager.MAX_NETWORK_TYPE+1];
294 for (int i : mPriorityList) {
Robert Greenwalt2034b912009-08-12 16:08:25 -0700295 mNetRequestersPids[i] = new ArrayList();
296 }
297
298 mFeatureUsers = new ArrayList();
299
Robert Greenwaltec05b3c2009-10-30 14:17:42 -0700300 mNumDnsEntries = 0;
301
302 mTestMode = SystemProperties.get("cm.test.mode").equals("true")
303 && SystemProperties.get("ro.build.type").equals("eng");
The Android Open Source Project28527d22009-03-03 19:31:44 -0800304 /*
305 * Create the network state trackers for Wi-Fi and mobile
306 * data. Maybe this could be done with a factory class,
307 * but it's not clear that it's worth it, given that
308 * the number of different network types is not going
309 * to change very often.
310 */
Robert Greenwalt1b21f6c2010-02-23 18:58:05 -0800311 boolean noMobileData = !getMobileDataEnabled();
Robert Greenwaltec05b3c2009-10-30 14:17:42 -0700312 for (int netType : mPriorityList) {
313 switch (mNetAttributes[netType].mRadio) {
314 case ConnectivityManager.TYPE_WIFI:
Joe Onoratoc2386bb2010-02-26 18:56:32 -0800315 if (DBG) Slog.v(TAG, "Starting Wifi Service.");
Wink Saville7fabfa22010-08-13 16:11:42 -0700316 WifiStateTracker wst = new WifiStateTracker();
Irfan Sheriff25be0762010-07-28 09:35:20 -0700317 WifiService wifiService = new WifiService(context);
Robert Greenwaltec05b3c2009-10-30 14:17:42 -0700318 ServiceManager.addService(Context.WIFI_SERVICE, wifiService);
Irfan Sheriff25be0762010-07-28 09:35:20 -0700319 wifiService.checkAndStartWifi();
Robert Greenwaltec05b3c2009-10-30 14:17:42 -0700320 mNetTrackers[ConnectivityManager.TYPE_WIFI] = wst;
Wink Saville7fabfa22010-08-13 16:11:42 -0700321 wst.startMonitoring(context, mHandler);
The Android Open Source Project28527d22009-03-03 19:31:44 -0800322
Irfan Sheriff653e2a22010-06-07 09:03:04 -0700323 //TODO: as part of WWS refactor, create only when needed
Irfan Sheriff25be0762010-07-28 09:35:20 -0700324 mWifiWatchdogService = new WifiWatchdogService(context);
Irfan Sheriff653e2a22010-06-07 09:03:04 -0700325
Robert Greenwaltec05b3c2009-10-30 14:17:42 -0700326 break;
327 case ConnectivityManager.TYPE_MOBILE:
Wink Saville7fabfa22010-08-13 16:11:42 -0700328 mNetTrackers[netType] = new MobileDataStateTracker(netType,
329 mNetAttributes[netType].mName);
330 mNetTrackers[netType].startMonitoring(context, mHandler);
Robert Greenwalt1b21f6c2010-02-23 18:58:05 -0800331 if (noMobileData) {
Joe Onoratoc2386bb2010-02-26 18:56:32 -0800332 if (DBG) Slog.d(TAG, "tearing down Mobile networks due to setting");
Robert Greenwalt1b21f6c2010-02-23 18:58:05 -0800333 mNetTrackers[netType].teardown();
334 }
Robert Greenwaltec05b3c2009-10-30 14:17:42 -0700335 break;
336 default:
Joe Onoratoc2386bb2010-02-26 18:56:32 -0800337 Slog.e(TAG, "Trying to create a DataStateTracker for an unknown radio type " +
Robert Greenwaltec05b3c2009-10-30 14:17:42 -0700338 mNetAttributes[netType].mRadio);
339 continue;
340 }
341 }
Robert Greenwalt0c4828c2010-01-26 11:40:34 -0800342
Robert Greenwaltc0b6c602010-03-11 15:03:08 -0800343 mTethering = new Tethering(mContext, mHandler.getLooper());
Robert Greenwaltf1b66e12010-02-25 12:29:30 -0800344 mTetheringConfigValid = (((mNetTrackers[ConnectivityManager.TYPE_MOBILE_DUN] != null) ||
345 !mTethering.isDunRequired()) &&
346 (mTethering.getTetherableUsbRegexs().length != 0 ||
Danica Chang96567052010-08-11 14:54:43 -0700347 mTethering.getTetherableWifiRegexs().length != 0 ||
348 mTethering.getTetherableBluetoothRegexs().length != 0) &&
Robert Greenwaltf1b66e12010-02-25 12:29:30 -0800349 mTethering.getUpstreamIfaceRegexs().length != 0);
350
The Android Open Source Project28527d22009-03-03 19:31:44 -0800351 }
352
Robert Greenwaltec05b3c2009-10-30 14:17:42 -0700353
The Android Open Source Project28527d22009-03-03 19:31:44 -0800354 /**
Robert Greenwalt0659da32009-07-16 17:21:39 -0700355 * Sets the preferred network.
The Android Open Source Project28527d22009-03-03 19:31:44 -0800356 * @param preference the new preference
357 */
358 public synchronized void setNetworkPreference(int preference) {
359 enforceChangePermission();
Robert Greenwalt2034b912009-08-12 16:08:25 -0700360 if (ConnectivityManager.isNetworkTypeValid(preference) &&
Robert Greenwaltec05b3c2009-10-30 14:17:42 -0700361 mNetAttributes[preference] != null &&
Robert Greenwalt2034b912009-08-12 16:08:25 -0700362 mNetAttributes[preference].isDefault()) {
The Android Open Source Project28527d22009-03-03 19:31:44 -0800363 if (mNetworkPreference != preference) {
364 persistNetworkPreference(preference);
365 mNetworkPreference = preference;
366 enforcePreference();
367 }
368 }
369 }
370
371 public int getNetworkPreference() {
372 enforceAccessPermission();
373 return mNetworkPreference;
374 }
375
376 private void persistNetworkPreference(int networkPreference) {
377 final ContentResolver cr = mContext.getContentResolver();
Robert Greenwalt0659da32009-07-16 17:21:39 -0700378 Settings.Secure.putInt(cr, Settings.Secure.NETWORK_PREFERENCE,
379 networkPreference);
The Android Open Source Project28527d22009-03-03 19:31:44 -0800380 }
Robert Greenwalt0659da32009-07-16 17:21:39 -0700381
The Android Open Source Project28527d22009-03-03 19:31:44 -0800382 private int getPersistedNetworkPreference() {
383 final ContentResolver cr = mContext.getContentResolver();
384
385 final int networkPrefSetting = Settings.Secure
386 .getInt(cr, Settings.Secure.NETWORK_PREFERENCE, -1);
387 if (networkPrefSetting != -1) {
388 return networkPrefSetting;
389 }
390
391 return ConnectivityManager.DEFAULT_NETWORK_PREFERENCE;
392 }
Robert Greenwalt0659da32009-07-16 17:21:39 -0700393
The Android Open Source Project28527d22009-03-03 19:31:44 -0800394 /**
Robert Greenwalt0659da32009-07-16 17:21:39 -0700395 * Make the state of network connectivity conform to the preference settings
The Android Open Source Project28527d22009-03-03 19:31:44 -0800396 * In this method, we only tear down a non-preferred network. Establishing
397 * a connection to the preferred network is taken care of when we handle
398 * the disconnect event from the non-preferred network
399 * (see {@link #handleDisconnect(NetworkInfo)}).
400 */
401 private void enforcePreference() {
Robert Greenwalt2034b912009-08-12 16:08:25 -0700402 if (mNetTrackers[mNetworkPreference].getNetworkInfo().isConnected())
The Android Open Source Project28527d22009-03-03 19:31:44 -0800403 return;
404
Robert Greenwalt2034b912009-08-12 16:08:25 -0700405 if (!mNetTrackers[mNetworkPreference].isAvailable())
406 return;
The Android Open Source Project28527d22009-03-03 19:31:44 -0800407
Robert Greenwalt2034b912009-08-12 16:08:25 -0700408 for (int t=0; t <= ConnectivityManager.MAX_RADIO_TYPE; t++) {
Robert Greenwaltec05b3c2009-10-30 14:17:42 -0700409 if (t != mNetworkPreference && mNetTrackers[t] != null &&
Robert Greenwalt2034b912009-08-12 16:08:25 -0700410 mNetTrackers[t].getNetworkInfo().isConnected()) {
Robert Greenwaltf3f045b2009-08-20 15:25:14 -0700411 if (DBG) {
Joe Onoratoc2386bb2010-02-26 18:56:32 -0800412 Slog.d(TAG, "tearing down " +
Robert Greenwaltf3f045b2009-08-20 15:25:14 -0700413 mNetTrackers[t].getNetworkInfo() +
414 " in enforcePreference");
415 }
Robert Greenwalt2034b912009-08-12 16:08:25 -0700416 teardown(mNetTrackers[t]);
The Android Open Source Project28527d22009-03-03 19:31:44 -0800417 }
418 }
419 }
420
421 private boolean teardown(NetworkStateTracker netTracker) {
422 if (netTracker.teardown()) {
423 netTracker.setTeardownRequested(true);
424 return true;
425 } else {
426 return false;
427 }
428 }
429
430 /**
431 * Return NetworkInfo for the active (i.e., connected) network interface.
432 * It is assumed that at most one network is active at a time. If more
433 * than one is active, it is indeterminate which will be returned.
Robert Greenwalt0659da32009-07-16 17:21:39 -0700434 * @return the info for the active network, or {@code null} if none is
435 * active
The Android Open Source Project28527d22009-03-03 19:31:44 -0800436 */
437 public NetworkInfo getActiveNetworkInfo() {
438 enforceAccessPermission();
Robert Greenwalt2034b912009-08-12 16:08:25 -0700439 for (int type=0; type <= ConnectivityManager.MAX_NETWORK_TYPE; type++) {
Robert Greenwaltec05b3c2009-10-30 14:17:42 -0700440 if (mNetAttributes[type] == null || !mNetAttributes[type].isDefault()) {
Robert Greenwalt2034b912009-08-12 16:08:25 -0700441 continue;
442 }
443 NetworkStateTracker t = mNetTrackers[type];
The Android Open Source Project28527d22009-03-03 19:31:44 -0800444 NetworkInfo info = t.getNetworkInfo();
445 if (info.isConnected()) {
Joe Onoratoc2386bb2010-02-26 18:56:32 -0800446 if (DBG && type != mActiveDefaultNetwork) Slog.e(TAG,
Robert Greenwalt2034b912009-08-12 16:08:25 -0700447 "connected default network is not " +
448 "mActiveDefaultNetwork!");
The Android Open Source Project28527d22009-03-03 19:31:44 -0800449 return info;
450 }
451 }
452 return null;
453 }
454
455 public NetworkInfo getNetworkInfo(int networkType) {
456 enforceAccessPermission();
457 if (ConnectivityManager.isNetworkTypeValid(networkType)) {
458 NetworkStateTracker t = mNetTrackers[networkType];
459 if (t != null)
460 return t.getNetworkInfo();
461 }
462 return null;
463 }
464
465 public NetworkInfo[] getAllNetworkInfo() {
466 enforceAccessPermission();
Robert Greenwaltec05b3c2009-10-30 14:17:42 -0700467 NetworkInfo[] result = new NetworkInfo[mNetworksDefined];
The Android Open Source Project28527d22009-03-03 19:31:44 -0800468 int i = 0;
469 for (NetworkStateTracker t : mNetTrackers) {
Robert Greenwaltec05b3c2009-10-30 14:17:42 -0700470 if(t != null) result[i++] = t.getNetworkInfo();
The Android Open Source Project28527d22009-03-03 19:31:44 -0800471 }
472 return result;
473 }
474
475 public boolean setRadios(boolean turnOn) {
476 boolean result = true;
477 enforceChangePermission();
478 for (NetworkStateTracker t : mNetTrackers) {
Robert Greenwaltec05b3c2009-10-30 14:17:42 -0700479 if (t != null) result = t.setRadio(turnOn) && result;
The Android Open Source Project28527d22009-03-03 19:31:44 -0800480 }
481 return result;
482 }
483
484 public boolean setRadio(int netType, boolean turnOn) {
485 enforceChangePermission();
486 if (!ConnectivityManager.isNetworkTypeValid(netType)) {
487 return false;
488 }
489 NetworkStateTracker tracker = mNetTrackers[netType];
490 return tracker != null && tracker.setRadio(turnOn);
491 }
492
Robert Greenwaltaffc3a12009-09-27 17:27:04 -0700493 /**
494 * Used to notice when the calling process dies so we can self-expire
495 *
496 * Also used to know if the process has cleaned up after itself when
497 * our auto-expire timer goes off. The timer has a link to an object.
498 *
499 */
Robert Greenwalt2034b912009-08-12 16:08:25 -0700500 private class FeatureUser implements IBinder.DeathRecipient {
501 int mNetworkType;
502 String mFeature;
503 IBinder mBinder;
504 int mPid;
505 int mUid;
Robert Greenwalt3eeb6032009-12-21 18:24:07 -0800506 long mCreateTime;
Robert Greenwalt2034b912009-08-12 16:08:25 -0700507
508 FeatureUser(int type, String feature, IBinder binder) {
509 super();
510 mNetworkType = type;
511 mFeature = feature;
512 mBinder = binder;
513 mPid = getCallingPid();
514 mUid = getCallingUid();
Robert Greenwalt3eeb6032009-12-21 18:24:07 -0800515 mCreateTime = System.currentTimeMillis();
Robert Greenwaltaffc3a12009-09-27 17:27:04 -0700516
Robert Greenwalt2034b912009-08-12 16:08:25 -0700517 try {
518 mBinder.linkToDeath(this, 0);
519 } catch (RemoteException e) {
520 binderDied();
521 }
522 }
523
524 void unlinkDeathRecipient() {
525 mBinder.unlinkToDeath(this, 0);
526 }
527
528 public void binderDied() {
Joe Onoratoc2386bb2010-02-26 18:56:32 -0800529 Slog.d(TAG, "ConnectivityService FeatureUser binderDied(" +
Robert Greenwalt3eeb6032009-12-21 18:24:07 -0800530 mNetworkType + ", " + mFeature + ", " + mBinder + "), created " +
531 (System.currentTimeMillis() - mCreateTime) + " mSec ago");
Robert Greenwaltaffc3a12009-09-27 17:27:04 -0700532 stopUsingNetworkFeature(this, false);
Robert Greenwalt2034b912009-08-12 16:08:25 -0700533 }
534
Robert Greenwaltaffc3a12009-09-27 17:27:04 -0700535 public void expire() {
Joe Onoratoc2386bb2010-02-26 18:56:32 -0800536 Slog.d(TAG, "ConnectivityService FeatureUser expire(" +
Robert Greenwalt3eeb6032009-12-21 18:24:07 -0800537 mNetworkType + ", " + mFeature + ", " + mBinder +"), created " +
538 (System.currentTimeMillis() - mCreateTime) + " mSec ago");
Robert Greenwaltaffc3a12009-09-27 17:27:04 -0700539 stopUsingNetworkFeature(this, false);
540 }
Robert Greenwalt3eeb6032009-12-21 18:24:07 -0800541
542 public String toString() {
543 return "FeatureUser("+mNetworkType+","+mFeature+","+mPid+","+mUid+"), created " +
544 (System.currentTimeMillis() - mCreateTime) + " mSec ago";
545 }
Robert Greenwalt2034b912009-08-12 16:08:25 -0700546 }
547
Robert Greenwaltaffc3a12009-09-27 17:27:04 -0700548 // javadoc from interface
Robert Greenwalt2034b912009-08-12 16:08:25 -0700549 public int startUsingNetworkFeature(int networkType, String feature,
550 IBinder binder) {
551 if (DBG) {
Joe Onoratoc2386bb2010-02-26 18:56:32 -0800552 Slog.d(TAG, "startUsingNetworkFeature for net " + networkType +
Robert Greenwalt2034b912009-08-12 16:08:25 -0700553 ": " + feature);
554 }
The Android Open Source Project28527d22009-03-03 19:31:44 -0800555 enforceChangePermission();
Robert Greenwaltec05b3c2009-10-30 14:17:42 -0700556 if (!ConnectivityManager.isNetworkTypeValid(networkType) ||
557 mNetAttributes[networkType] == null) {
Robert Greenwalt2034b912009-08-12 16:08:25 -0700558 return Phone.APN_REQUEST_FAILED;
The Android Open Source Project28527d22009-03-03 19:31:44 -0800559 }
Robert Greenwalt2034b912009-08-12 16:08:25 -0700560
Robert Greenwaltaffc3a12009-09-27 17:27:04 -0700561 FeatureUser f = new FeatureUser(networkType, feature, binder);
Robert Greenwalt2034b912009-08-12 16:08:25 -0700562
563 // TODO - move this into the MobileDataStateTracker
564 int usedNetworkType = networkType;
565 if(networkType == ConnectivityManager.TYPE_MOBILE) {
Robert Greenwalt1b21f6c2010-02-23 18:58:05 -0800566 if (!getMobileDataEnabled()) {
Joe Onoratoc2386bb2010-02-26 18:56:32 -0800567 if (DBG) Slog.d(TAG, "requested special network with data disabled - rejected");
Robert Greenwalt1b21f6c2010-02-23 18:58:05 -0800568 return Phone.APN_TYPE_NOT_AVAILABLE;
569 }
Robert Greenwalt2034b912009-08-12 16:08:25 -0700570 if (TextUtils.equals(feature, Phone.FEATURE_ENABLE_MMS)) {
571 usedNetworkType = ConnectivityManager.TYPE_MOBILE_MMS;
572 } else if (TextUtils.equals(feature, Phone.FEATURE_ENABLE_SUPL)) {
573 usedNetworkType = ConnectivityManager.TYPE_MOBILE_SUPL;
574 } else if (TextUtils.equals(feature, Phone.FEATURE_ENABLE_DUN)) {
575 usedNetworkType = ConnectivityManager.TYPE_MOBILE_DUN;
576 } else if (TextUtils.equals(feature, Phone.FEATURE_ENABLE_HIPRI)) {
577 usedNetworkType = ConnectivityManager.TYPE_MOBILE_HIPRI;
578 }
579 }
580 NetworkStateTracker network = mNetTrackers[usedNetworkType];
581 if (network != null) {
582 if (usedNetworkType != networkType) {
583 Integer currentPid = new Integer(getCallingPid());
584
585 NetworkStateTracker radio = mNetTrackers[networkType];
586 NetworkInfo ni = network.getNetworkInfo();
587
588 if (ni.isAvailable() == false) {
Joe Onoratoc2386bb2010-02-26 18:56:32 -0800589 if (DBG) Slog.d(TAG, "special network not available");
Robert Greenwalt2034b912009-08-12 16:08:25 -0700590 return Phone.APN_TYPE_NOT_AVAILABLE;
591 }
592
Robert Greenwaltaffc3a12009-09-27 17:27:04 -0700593 synchronized(this) {
594 mFeatureUsers.add(f);
595 if (!mNetRequestersPids[usedNetworkType].contains(currentPid)) {
596 // this gets used for per-pid dns when connected
597 mNetRequestersPids[usedNetworkType].add(currentPid);
598 }
Robert Greenwalt2034b912009-08-12 16:08:25 -0700599 }
Robert Greenwaltaffc3a12009-09-27 17:27:04 -0700600 mHandler.sendMessageDelayed(mHandler.obtainMessage(
601 NetworkStateTracker.EVENT_RESTORE_DEFAULT_NETWORK,
602 f), getRestoreDefaultNetworkDelay());
603
Robert Greenwalt2034b912009-08-12 16:08:25 -0700604
Robert Greenwalta52c75a2009-08-19 20:19:33 -0700605 if ((ni.isConnectedOrConnecting() == true) &&
606 !network.isTeardownRequested()) {
Robert Greenwalt2034b912009-08-12 16:08:25 -0700607 if (ni.isConnected() == true) {
608 // add the pid-specific dns
Robert Greenwalt3afbead2010-07-23 15:46:26 -0700609 handleDnsConfigurationChange(networkType);
Joe Onoratoc2386bb2010-02-26 18:56:32 -0800610 if (DBG) Slog.d(TAG, "special network already active");
Robert Greenwalt2034b912009-08-12 16:08:25 -0700611 return Phone.APN_ALREADY_ACTIVE;
612 }
Joe Onoratoc2386bb2010-02-26 18:56:32 -0800613 if (DBG) Slog.d(TAG, "special network already connecting");
Robert Greenwalt2034b912009-08-12 16:08:25 -0700614 return Phone.APN_REQUEST_STARTED;
615 }
616
617 // check if the radio in play can make another contact
618 // assume if cannot for now
619
Joe Onoratoc2386bb2010-02-26 18:56:32 -0800620 if (DBG) Slog.d(TAG, "reconnecting to special network");
Robert Greenwalt2034b912009-08-12 16:08:25 -0700621 network.reconnect();
622 return Phone.APN_REQUEST_STARTED;
623 } else {
Robert Greenwaltd391e892010-05-18 10:52:51 -0700624 return -1;
Robert Greenwalt2034b912009-08-12 16:08:25 -0700625 }
626 }
627 return Phone.APN_TYPE_NOT_AVAILABLE;
The Android Open Source Project28527d22009-03-03 19:31:44 -0800628 }
629
Robert Greenwaltaffc3a12009-09-27 17:27:04 -0700630 // javadoc from interface
The Android Open Source Project28527d22009-03-03 19:31:44 -0800631 public int stopUsingNetworkFeature(int networkType, String feature) {
Robert Greenwalt28f43012009-10-06 17:52:40 -0700632 enforceChangePermission();
633
Robert Greenwaltaffc3a12009-09-27 17:27:04 -0700634 int pid = getCallingPid();
635 int uid = getCallingUid();
636
637 FeatureUser u = null;
638 boolean found = false;
639
640 synchronized(this) {
641 for (int i = 0; i < mFeatureUsers.size() ; i++) {
642 u = (FeatureUser)mFeatureUsers.get(i);
643 if (uid == u.mUid && pid == u.mPid &&
644 networkType == u.mNetworkType &&
645 TextUtils.equals(feature, u.mFeature)) {
646 found = true;
647 break;
648 }
649 }
650 }
651 if (found && u != null) {
652 // stop regardless of how many other time this proc had called start
653 return stopUsingNetworkFeature(u, true);
654 } else {
655 // none found!
Joe Onoratoc2386bb2010-02-26 18:56:32 -0800656 if (DBG) Slog.d(TAG, "ignoring stopUsingNetworkFeature - not a live request");
Robert Greenwaltaffc3a12009-09-27 17:27:04 -0700657 return 1;
658 }
Robert Greenwalt2034b912009-08-12 16:08:25 -0700659 }
660
Robert Greenwaltaffc3a12009-09-27 17:27:04 -0700661 private int stopUsingNetworkFeature(FeatureUser u, boolean ignoreDups) {
662 int networkType = u.mNetworkType;
663 String feature = u.mFeature;
664 int pid = u.mPid;
665 int uid = u.mUid;
666
667 NetworkStateTracker tracker = null;
668 boolean callTeardown = false; // used to carry our decision outside of sync block
669
Robert Greenwalt2034b912009-08-12 16:08:25 -0700670 if (DBG) {
Joe Onoratoc2386bb2010-02-26 18:56:32 -0800671 Slog.d(TAG, "stopUsingNetworkFeature for net " + networkType +
Robert Greenwalt2034b912009-08-12 16:08:25 -0700672 ": " + feature);
673 }
Robert Greenwalt28f43012009-10-06 17:52:40 -0700674
The Android Open Source Project28527d22009-03-03 19:31:44 -0800675 if (!ConnectivityManager.isNetworkTypeValid(networkType)) {
676 return -1;
677 }
Robert Greenwalt2034b912009-08-12 16:08:25 -0700678
Robert Greenwaltaffc3a12009-09-27 17:27:04 -0700679 // need to link the mFeatureUsers list with the mNetRequestersPids state in this
680 // sync block
681 synchronized(this) {
682 // check if this process still has an outstanding start request
683 if (!mFeatureUsers.contains(u)) {
Robert Greenwalt46ceefa2010-03-10 16:10:43 -0800684 if (DBG) Slog.d(TAG, "ignoring - this process has no outstanding requests");
Robert Greenwalt2034b912009-08-12 16:08:25 -0700685 return 1;
686 }
Robert Greenwaltaffc3a12009-09-27 17:27:04 -0700687 u.unlinkDeathRecipient();
688 mFeatureUsers.remove(mFeatureUsers.indexOf(u));
689 // If we care about duplicate requests, check for that here.
690 //
691 // This is done to support the extension of a request - the app
692 // can request we start the network feature again and renew the
693 // auto-shutoff delay. Normal "stop" calls from the app though
694 // do not pay attention to duplicate requests - in effect the
695 // API does not refcount and a single stop will counter multiple starts.
696 if (ignoreDups == false) {
697 for (int i = 0; i < mFeatureUsers.size() ; i++) {
698 FeatureUser x = (FeatureUser)mFeatureUsers.get(i);
699 if (x.mUid == u.mUid && x.mPid == u.mPid &&
700 x.mNetworkType == u.mNetworkType &&
701 TextUtils.equals(x.mFeature, u.mFeature)) {
Joe Onoratoc2386bb2010-02-26 18:56:32 -0800702 if (DBG) Slog.d(TAG, "ignoring stopUsingNetworkFeature as dup is found");
Robert Greenwaltaffc3a12009-09-27 17:27:04 -0700703 return 1;
704 }
705 }
Robert Greenwalt2034b912009-08-12 16:08:25 -0700706 }
Robert Greenwaltaffc3a12009-09-27 17:27:04 -0700707
708 // TODO - move to MobileDataStateTracker
709 int usedNetworkType = networkType;
710 if (networkType == ConnectivityManager.TYPE_MOBILE) {
711 if (TextUtils.equals(feature, Phone.FEATURE_ENABLE_MMS)) {
712 usedNetworkType = ConnectivityManager.TYPE_MOBILE_MMS;
713 } else if (TextUtils.equals(feature, Phone.FEATURE_ENABLE_SUPL)) {
714 usedNetworkType = ConnectivityManager.TYPE_MOBILE_SUPL;
715 } else if (TextUtils.equals(feature, Phone.FEATURE_ENABLE_DUN)) {
716 usedNetworkType = ConnectivityManager.TYPE_MOBILE_DUN;
717 } else if (TextUtils.equals(feature, Phone.FEATURE_ENABLE_HIPRI)) {
718 usedNetworkType = ConnectivityManager.TYPE_MOBILE_HIPRI;
719 }
720 }
721 tracker = mNetTrackers[usedNetworkType];
Robert Greenwaltec05b3c2009-10-30 14:17:42 -0700722 if (tracker == null) {
Robert Greenwalt46ceefa2010-03-10 16:10:43 -0800723 if (DBG) Slog.d(TAG, "ignoring - no known tracker for net type " + usedNetworkType);
Robert Greenwaltec05b3c2009-10-30 14:17:42 -0700724 return -1;
725 }
726 if (usedNetworkType != networkType) {
Robert Greenwaltaffc3a12009-09-27 17:27:04 -0700727 Integer currentPid = new Integer(pid);
Robert Greenwaltaffc3a12009-09-27 17:27:04 -0700728 mNetRequestersPids[usedNetworkType].remove(currentPid);
Robert Greenwalt0ca68a02009-12-17 14:54:59 -0800729 reassessPidDns(pid, true);
Robert Greenwaltaffc3a12009-09-27 17:27:04 -0700730 if (mNetRequestersPids[usedNetworkType].size() != 0) {
Joe Onoratoc2386bb2010-02-26 18:56:32 -0800731 if (DBG) Slog.d(TAG, "not tearing down special network - " +
Robert Greenwaltaffc3a12009-09-27 17:27:04 -0700732 "others still using it");
733 return 1;
734 }
735 callTeardown = true;
736 }
737 }
Robert Greenwalt46ceefa2010-03-10 16:10:43 -0800738 if (DBG) Slog.d(TAG, "Doing network teardown");
Robert Greenwaltaffc3a12009-09-27 17:27:04 -0700739 if (callTeardown) {
740 tracker.teardown();
Robert Greenwalt2034b912009-08-12 16:08:25 -0700741 return 1;
742 } else {
Robert Greenwaltd391e892010-05-18 10:52:51 -0700743 return -1;
Robert Greenwalt2034b912009-08-12 16:08:25 -0700744 }
The Android Open Source Project28527d22009-03-03 19:31:44 -0800745 }
746
747 /**
748 * Ensure that a network route exists to deliver traffic to the specified
749 * host via the specified network interface.
Robert Greenwalt0659da32009-07-16 17:21:39 -0700750 * @param networkType the type of the network over which traffic to the
751 * specified host is to be routed
752 * @param hostAddress the IP address of the host to which the route is
753 * desired
Robert Greenwalta7dfbd32010-06-15 15:43:39 -0700754 * todo - deprecate (only v4!)
The Android Open Source Project28527d22009-03-03 19:31:44 -0800755 * @return {@code true} on success, {@code false} on failure
756 */
757 public boolean requestRouteToHost(int networkType, int hostAddress) {
758 enforceChangePermission();
759 if (!ConnectivityManager.isNetworkTypeValid(networkType)) {
760 return false;
761 }
762 NetworkStateTracker tracker = mNetTrackers[networkType];
Robert Greenwalt4666ed02009-09-10 15:06:20 -0700763
Robert Greenwaltec05b3c2009-10-30 14:17:42 -0700764 if (tracker == null || !tracker.getNetworkInfo().isConnected() ||
765 tracker.isTeardownRequested()) {
Robert Greenwalt4666ed02009-09-10 15:06:20 -0700766 if (DBG) {
Joe Onoratoc2386bb2010-02-26 18:56:32 -0800767 Slog.d(TAG, "requestRouteToHost on down network (" + networkType + ") - dropped");
Robert Greenwalt4666ed02009-09-10 15:06:20 -0700768 }
769 return false;
The Android Open Source Project28527d22009-03-03 19:31:44 -0800770 }
Robert Greenwalta7dfbd32010-06-15 15:43:39 -0700771 try {
772 InetAddress addr = InetAddress.getByAddress(NetworkUtils.v4IntToArray(hostAddress));
773 return addHostRoute(tracker, addr);
774 } catch (UnknownHostException e) {}
775 return false;
Irfan Sheriff7f132d92010-06-09 15:39:36 -0700776 }
777
778 /**
779 * Ensure that a network route exists to deliver traffic to the specified
780 * host via the mobile data network.
781 * @param hostAddress the IP address of the host to which the route is desired,
782 * in network byte order.
Robert Greenwalta7dfbd32010-06-15 15:43:39 -0700783 * TODO - deprecate
Irfan Sheriff7f132d92010-06-09 15:39:36 -0700784 * @return {@code true} on success, {@code false} on failure
785 */
Robert Greenwalta7dfbd32010-06-15 15:43:39 -0700786 private boolean addHostRoute(NetworkStateTracker nt, InetAddress hostAddress) {
Irfan Sheriff7f132d92010-06-09 15:39:36 -0700787 if (nt.getNetworkInfo().getType() == ConnectivityManager.TYPE_WIFI) {
788 return false;
789 }
790
Robert Greenwalta7dfbd32010-06-15 15:43:39 -0700791 NetworkProperties p = nt.getNetworkProperties();
792 if (p == null) return false;
793 String interfaceName = p.getInterfaceName();
Irfan Sheriff7f132d92010-06-09 15:39:36 -0700794
795 if (DBG) {
Robert Greenwalta7dfbd32010-06-15 15:43:39 -0700796 Slog.d(TAG, "Requested host route to " + hostAddress + "(" + interfaceName + ")");
Irfan Sheriff7f132d92010-06-09 15:39:36 -0700797 }
Robert Greenwalta7dfbd32010-06-15 15:43:39 -0700798 if (interfaceName != null) {
Irfan Sheriff7f132d92010-06-09 15:39:36 -0700799 return NetworkUtils.addHostRoute(interfaceName, hostAddress) == 0;
800 } else {
Robert Greenwalta7dfbd32010-06-15 15:43:39 -0700801 if (DBG) Slog.e(TAG, "addHostRoute failed due to null interface name");
Irfan Sheriff7f132d92010-06-09 15:39:36 -0700802 return false;
803 }
The Android Open Source Project28527d22009-03-03 19:31:44 -0800804 }
805
806 /**
807 * @see ConnectivityManager#getBackgroundDataSetting()
808 */
809 public boolean getBackgroundDataSetting() {
810 return Settings.Secure.getInt(mContext.getContentResolver(),
811 Settings.Secure.BACKGROUND_DATA, 1) == 1;
812 }
Robert Greenwalt0659da32009-07-16 17:21:39 -0700813
The Android Open Source Project28527d22009-03-03 19:31:44 -0800814 /**
815 * @see ConnectivityManager#setBackgroundDataSetting(boolean)
816 */
817 public void setBackgroundDataSetting(boolean allowBackgroundDataUsage) {
818 mContext.enforceCallingOrSelfPermission(
819 android.Manifest.permission.CHANGE_BACKGROUND_DATA_SETTING,
820 "ConnectivityService");
Robert Greenwalt0659da32009-07-16 17:21:39 -0700821
The Android Open Source Project28527d22009-03-03 19:31:44 -0800822 if (getBackgroundDataSetting() == allowBackgroundDataUsage) return;
823
824 Settings.Secure.putInt(mContext.getContentResolver(),
Robert Greenwalt0659da32009-07-16 17:21:39 -0700825 Settings.Secure.BACKGROUND_DATA,
826 allowBackgroundDataUsage ? 1 : 0);
827
The Android Open Source Project28527d22009-03-03 19:31:44 -0800828 Intent broadcast = new Intent(
829 ConnectivityManager.ACTION_BACKGROUND_DATA_SETTING_CHANGED);
830 mContext.sendBroadcast(broadcast);
Robert Greenwalt0659da32009-07-16 17:21:39 -0700831 }
832
Robert Greenwalt1b21f6c2010-02-23 18:58:05 -0800833 /**
834 * @see ConnectivityManager#getMobileDataEnabled()
835 */
836 public boolean getMobileDataEnabled() {
837 enforceAccessPermission();
838 boolean retVal = Settings.Secure.getInt(mContext.getContentResolver(),
839 Settings.Secure.MOBILE_DATA, 1) == 1;
Joe Onoratoc2386bb2010-02-26 18:56:32 -0800840 if (DBG) Slog.d(TAG, "getMobileDataEnabled returning " + retVal);
Robert Greenwalt1b21f6c2010-02-23 18:58:05 -0800841 return retVal;
842 }
843
844 /**
845 * @see ConnectivityManager#setMobileDataEnabled(boolean)
846 */
847 public synchronized void setMobileDataEnabled(boolean enabled) {
848 enforceChangePermission();
Joe Onoratoc2386bb2010-02-26 18:56:32 -0800849 if (DBG) Slog.d(TAG, "setMobileDataEnabled(" + enabled + ")");
Robert Greenwalt1b21f6c2010-02-23 18:58:05 -0800850
851 if (getMobileDataEnabled() == enabled) return;
852
853 Settings.Secure.putInt(mContext.getContentResolver(),
854 Settings.Secure.MOBILE_DATA, enabled ? 1 : 0);
855
856 if (enabled) {
857 if (mNetTrackers[ConnectivityManager.TYPE_MOBILE] != null) {
Joe Onoratoc2386bb2010-02-26 18:56:32 -0800858 if (DBG) Slog.d(TAG, "starting up " + mNetTrackers[ConnectivityManager.TYPE_MOBILE]);
Robert Greenwalt1b21f6c2010-02-23 18:58:05 -0800859 mNetTrackers[ConnectivityManager.TYPE_MOBILE].reconnect();
860 }
861 } else {
862 for (NetworkStateTracker nt : mNetTrackers) {
863 if (nt == null) continue;
864 int netType = nt.getNetworkInfo().getType();
865 if (mNetAttributes[netType].mRadio == ConnectivityManager.TYPE_MOBILE) {
Joe Onoratoc2386bb2010-02-26 18:56:32 -0800866 if (DBG) Slog.d(TAG, "tearing down " + nt);
Robert Greenwalt1b21f6c2010-02-23 18:58:05 -0800867 nt.teardown();
868 }
869 }
870 }
871 }
872
The Android Open Source Project28527d22009-03-03 19:31:44 -0800873 private int getNumConnectedNetworks() {
874 int numConnectedNets = 0;
875
876 for (NetworkStateTracker nt : mNetTrackers) {
Robert Greenwaltec05b3c2009-10-30 14:17:42 -0700877 if (nt != null && nt.getNetworkInfo().isConnected() &&
Robert Greenwalt0659da32009-07-16 17:21:39 -0700878 !nt.isTeardownRequested()) {
The Android Open Source Project28527d22009-03-03 19:31:44 -0800879 ++numConnectedNets;
880 }
881 }
882 return numConnectedNets;
883 }
884
885 private void enforceAccessPermission() {
Robert Greenwalt0659da32009-07-16 17:21:39 -0700886 mContext.enforceCallingOrSelfPermission(
887 android.Manifest.permission.ACCESS_NETWORK_STATE,
888 "ConnectivityService");
The Android Open Source Project28527d22009-03-03 19:31:44 -0800889 }
890
891 private void enforceChangePermission() {
Robert Greenwalt0659da32009-07-16 17:21:39 -0700892 mContext.enforceCallingOrSelfPermission(
893 android.Manifest.permission.CHANGE_NETWORK_STATE,
894 "ConnectivityService");
The Android Open Source Project28527d22009-03-03 19:31:44 -0800895 }
896
Robert Greenwalt0c4828c2010-01-26 11:40:34 -0800897 // TODO Make this a special check when it goes public
898 private void enforceTetherChangePermission() {
899 mContext.enforceCallingOrSelfPermission(
900 android.Manifest.permission.CHANGE_NETWORK_STATE,
901 "ConnectivityService");
902 }
903
Robert Greenwalt8e87f122010-02-11 18:18:40 -0800904 private void enforceTetherAccessPermission() {
905 mContext.enforceCallingOrSelfPermission(
906 android.Manifest.permission.ACCESS_NETWORK_STATE,
907 "ConnectivityService");
908 }
909
Robert Greenwalt93dc1042010-06-15 12:19:37 -0700910 private void enforceConnectivityInternalPermission() {
911 mContext.enforceCallingOrSelfPermission(
912 android.Manifest.permission.CONNECTIVITY_INTERNAL,
913 "ConnectivityService");
914 }
915
The Android Open Source Project28527d22009-03-03 19:31:44 -0800916 /**
Robert Greenwalt0659da32009-07-16 17:21:39 -0700917 * Handle a {@code DISCONNECTED} event. If this pertains to the non-active
918 * network, we ignore it. If it is for the active network, we send out a
919 * broadcast. But first, we check whether it might be possible to connect
920 * to a different network.
The Android Open Source Project28527d22009-03-03 19:31:44 -0800921 * @param info the {@code NetworkInfo} for the network
922 */
923 private void handleDisconnect(NetworkInfo info) {
924
Robert Greenwalt2034b912009-08-12 16:08:25 -0700925 int prevNetType = info.getType();
The Android Open Source Project28527d22009-03-03 19:31:44 -0800926
Robert Greenwalt2034b912009-08-12 16:08:25 -0700927 mNetTrackers[prevNetType].setTeardownRequested(false);
The Android Open Source Project28527d22009-03-03 19:31:44 -0800928 /*
929 * If the disconnected network is not the active one, then don't report
930 * this as a loss of connectivity. What probably happened is that we're
931 * getting the disconnect for a network that we explicitly disabled
932 * in accordance with network preference policies.
933 */
Robert Greenwalt2034b912009-08-12 16:08:25 -0700934 if (!mNetAttributes[prevNetType].isDefault()) {
935 List pids = mNetRequestersPids[prevNetType];
936 for (int i = 0; i<pids.size(); i++) {
937 Integer pid = (Integer)pids.get(i);
938 // will remove them because the net's no longer connected
939 // need to do this now as only now do we know the pids and
940 // can properly null things that are no longer referenced.
941 reassessPidDns(pid.intValue(), false);
The Android Open Source Project28527d22009-03-03 19:31:44 -0800942 }
The Android Open Source Project28527d22009-03-03 19:31:44 -0800943 }
944
The Android Open Source Project28527d22009-03-03 19:31:44 -0800945 Intent intent = new Intent(ConnectivityManager.CONNECTIVITY_ACTION);
Dianne Hackborna417ff82009-12-08 19:45:14 -0800946 intent.addFlags(Intent.FLAG_RECEIVER_REPLACE_PENDING);
The Android Open Source Project28527d22009-03-03 19:31:44 -0800947 intent.putExtra(ConnectivityManager.EXTRA_NETWORK_INFO, info);
948 if (info.isFailover()) {
949 intent.putExtra(ConnectivityManager.EXTRA_IS_FAILOVER, true);
950 info.setFailover(false);
951 }
952 if (info.getReason() != null) {
953 intent.putExtra(ConnectivityManager.EXTRA_REASON, info.getReason());
954 }
955 if (info.getExtraInfo() != null) {
Robert Greenwalt0659da32009-07-16 17:21:39 -0700956 intent.putExtra(ConnectivityManager.EXTRA_EXTRA_INFO,
957 info.getExtraInfo());
The Android Open Source Project28527d22009-03-03 19:31:44 -0800958 }
Robert Greenwalt2034b912009-08-12 16:08:25 -0700959
Robert Greenwalt3cc68d32010-01-25 17:54:29 -0800960 NetworkStateTracker newNet = null;
961 if (mNetAttributes[prevNetType].isDefault()) {
962 newNet = tryFailover(prevNetType);
963 if (newNet != null) {
964 NetworkInfo switchTo = newNet.getNetworkInfo();
965 intent.putExtra(ConnectivityManager.EXTRA_OTHER_NETWORK_INFO, switchTo);
966 } else {
967 intent.putExtra(ConnectivityManager.EXTRA_NO_CONNECTIVITY, true);
968 }
Robert Greenwaltf55ced92010-01-20 19:29:41 -0800969 }
970 // do this before we broadcast the change
Robert Greenwalt3afbead2010-07-23 15:46:26 -0700971 handleConnectivityChange(prevNetType);
Robert Greenwaltf55ced92010-01-20 19:29:41 -0800972
973 sendStickyBroadcast(intent);
974 /*
975 * If the failover network is already connected, then immediately send
976 * out a followup broadcast indicating successful failover
977 */
978 if (newNet != null && newNet.getNetworkInfo().isConnected()) {
979 sendConnectedBroadcast(newNet.getNetworkInfo());
980 }
981 }
982
Robert Greenwalt3cc68d32010-01-25 17:54:29 -0800983 // returns null if no failover available
Robert Greenwaltf55ced92010-01-20 19:29:41 -0800984 private NetworkStateTracker tryFailover(int prevNetType) {
Robert Greenwalt2034b912009-08-12 16:08:25 -0700985 /*
986 * If this is a default network, check if other defaults are available
987 * or active
988 */
989 NetworkStateTracker newNet = null;
990 if (mNetAttributes[prevNetType].isDefault()) {
Robert Greenwalt2034b912009-08-12 16:08:25 -0700991 if (mActiveDefaultNetwork == prevNetType) {
992 mActiveDefaultNetwork = -1;
993 }
994
995 int newType = -1;
996 int newPriority = -1;
Robert Greenwalt72451bf2010-02-25 12:04:29 -0800997 boolean noMobileData = !getMobileDataEnabled();
Robert Greenwaltf55ced92010-01-20 19:29:41 -0800998 for (int checkType=0; checkType <= ConnectivityManager.MAX_NETWORK_TYPE; checkType++) {
Robert Greenwaltec05b3c2009-10-30 14:17:42 -0700999 if (checkType == prevNetType) continue;
1000 if (mNetAttributes[checkType] == null) continue;
Robert Greenwalt72451bf2010-02-25 12:04:29 -08001001 if (mNetAttributes[checkType].mRadio == ConnectivityManager.TYPE_MOBILE &&
1002 noMobileData) {
1003 if (DBG) {
Joe Onoratoc2386bb2010-02-26 18:56:32 -08001004 Slog.d(TAG, "not failing over to mobile type " + checkType +
Robert Greenwalt72451bf2010-02-25 12:04:29 -08001005 " because Mobile Data Disabled");
1006 }
1007 continue;
1008 }
Robert Greenwalt2034b912009-08-12 16:08:25 -07001009 if (mNetAttributes[checkType].isDefault()) {
1010 /* TODO - if we have multiple nets we could use
1011 * we may want to put more thought into which we choose
1012 */
1013 if (checkType == mNetworkPreference) {
1014 newType = checkType;
1015 break;
1016 }
Robert Greenwaltec05b3c2009-10-30 14:17:42 -07001017 if (mNetAttributes[checkType].mPriority > newPriority) {
Robert Greenwalt2034b912009-08-12 16:08:25 -07001018 newType = checkType;
Robert Greenwaltec05b3c2009-10-30 14:17:42 -07001019 newPriority = mNetAttributes[newType].mPriority;
Robert Greenwalt2034b912009-08-12 16:08:25 -07001020 }
The Android Open Source Project28527d22009-03-03 19:31:44 -08001021 }
1022 }
Robert Greenwalt2034b912009-08-12 16:08:25 -07001023
1024 if (newType != -1) {
1025 newNet = mNetTrackers[newType];
1026 /**
1027 * See if the other network is available to fail over to.
1028 * If is not available, we enable it anyway, so that it
1029 * will be able to connect when it does become available,
1030 * but we report a total loss of connectivity rather than
1031 * report that we are attempting to fail over.
1032 */
1033 if (newNet.isAvailable()) {
1034 NetworkInfo switchTo = newNet.getNetworkInfo();
1035 switchTo.setFailover(true);
Robert Greenwalta52c75a2009-08-19 20:19:33 -07001036 if (!switchTo.isConnectedOrConnecting() ||
1037 newNet.isTeardownRequested()) {
Robert Greenwalt2034b912009-08-12 16:08:25 -07001038 newNet.reconnect();
1039 }
1040 if (DBG) {
1041 if (switchTo.isConnected()) {
Joe Onoratoc2386bb2010-02-26 18:56:32 -08001042 Slog.v(TAG, "Switching to already connected " +
Robert Greenwalt2034b912009-08-12 16:08:25 -07001043 switchTo.getTypeName());
1044 } else {
Joe Onoratoc2386bb2010-02-26 18:56:32 -08001045 Slog.v(TAG, "Attempting to switch to " +
Robert Greenwalt2034b912009-08-12 16:08:25 -07001046 switchTo.getTypeName());
1047 }
1048 }
Robert Greenwalt2034b912009-08-12 16:08:25 -07001049 } else {
1050 newNet.reconnect();
Robert Greenwalt12984322010-03-09 14:55:08 -08001051 newNet = null; // not officially avail.. try anyway, but
1052 // report no failover
Robert Greenwalt2034b912009-08-12 16:08:25 -07001053 }
Robert Greenwalt2034b912009-08-12 16:08:25 -07001054 }
The Android Open Source Project28527d22009-03-03 19:31:44 -08001055 }
Robert Greenwalt2034b912009-08-12 16:08:25 -07001056
Robert Greenwaltf55ced92010-01-20 19:29:41 -08001057 return newNet;
The Android Open Source Project28527d22009-03-03 19:31:44 -08001058 }
1059
1060 private void sendConnectedBroadcast(NetworkInfo info) {
1061 Intent intent = new Intent(ConnectivityManager.CONNECTIVITY_ACTION);
Dianne Hackborna417ff82009-12-08 19:45:14 -08001062 intent.addFlags(Intent.FLAG_RECEIVER_REPLACE_PENDING);
The Android Open Source Project28527d22009-03-03 19:31:44 -08001063 intent.putExtra(ConnectivityManager.EXTRA_NETWORK_INFO, info);
1064 if (info.isFailover()) {
1065 intent.putExtra(ConnectivityManager.EXTRA_IS_FAILOVER, true);
1066 info.setFailover(false);
1067 }
1068 if (info.getReason() != null) {
1069 intent.putExtra(ConnectivityManager.EXTRA_REASON, info.getReason());
1070 }
1071 if (info.getExtraInfo() != null) {
Robert Greenwalt0659da32009-07-16 17:21:39 -07001072 intent.putExtra(ConnectivityManager.EXTRA_EXTRA_INFO,
1073 info.getExtraInfo());
The Android Open Source Project28527d22009-03-03 19:31:44 -08001074 }
Mike Lockwoodfde2b762009-08-14 14:18:49 -04001075 sendStickyBroadcast(intent);
The Android Open Source Project28527d22009-03-03 19:31:44 -08001076 }
1077
1078 /**
1079 * Called when an attempt to fail over to another network has failed.
1080 * @param info the {@link NetworkInfo} for the failed network
1081 */
1082 private void handleConnectionFailure(NetworkInfo info) {
1083 mNetTrackers[info.getType()].setTeardownRequested(false);
The Android Open Source Project28527d22009-03-03 19:31:44 -08001084
Robert Greenwalt2034b912009-08-12 16:08:25 -07001085 String reason = info.getReason();
1086 String extraInfo = info.getExtraInfo();
Robert Greenwalt0659da32009-07-16 17:21:39 -07001087
Robert Greenwalt2034b912009-08-12 16:08:25 -07001088 if (DBG) {
1089 String reasonText;
1090 if (reason == null) {
1091 reasonText = ".";
1092 } else {
1093 reasonText = " (" + reason + ").";
The Android Open Source Project28527d22009-03-03 19:31:44 -08001094 }
Joe Onoratoc2386bb2010-02-26 18:56:32 -08001095 Slog.v(TAG, "Attempt to connect to " + info.getTypeName() +
Robert Greenwalt2034b912009-08-12 16:08:25 -07001096 " failed" + reasonText);
The Android Open Source Project28527d22009-03-03 19:31:44 -08001097 }
Robert Greenwalt2034b912009-08-12 16:08:25 -07001098
1099 Intent intent = new Intent(ConnectivityManager.CONNECTIVITY_ACTION);
Dianne Hackborna417ff82009-12-08 19:45:14 -08001100 intent.addFlags(Intent.FLAG_RECEIVER_REPLACE_PENDING);
Robert Greenwalt2034b912009-08-12 16:08:25 -07001101 intent.putExtra(ConnectivityManager.EXTRA_NETWORK_INFO, info);
1102 if (getActiveNetworkInfo() == null) {
1103 intent.putExtra(ConnectivityManager.EXTRA_NO_CONNECTIVITY, true);
1104 }
1105 if (reason != null) {
1106 intent.putExtra(ConnectivityManager.EXTRA_REASON, reason);
1107 }
1108 if (extraInfo != null) {
1109 intent.putExtra(ConnectivityManager.EXTRA_EXTRA_INFO, extraInfo);
1110 }
1111 if (info.isFailover()) {
1112 intent.putExtra(ConnectivityManager.EXTRA_IS_FAILOVER, true);
1113 info.setFailover(false);
1114 }
Robert Greenwaltf55ced92010-01-20 19:29:41 -08001115
Robert Greenwalt3cc68d32010-01-25 17:54:29 -08001116 NetworkStateTracker newNet = null;
1117 if (mNetAttributes[info.getType()].isDefault()) {
1118 newNet = tryFailover(info.getType());
1119 if (newNet != null) {
1120 NetworkInfo switchTo = newNet.getNetworkInfo();
1121 intent.putExtra(ConnectivityManager.EXTRA_OTHER_NETWORK_INFO, switchTo);
1122 } else {
1123 intent.putExtra(ConnectivityManager.EXTRA_NO_CONNECTIVITY, true);
1124 }
Robert Greenwaltf55ced92010-01-20 19:29:41 -08001125 }
Robert Greenwalt3cc68d32010-01-25 17:54:29 -08001126
Mike Lockwoodfde2b762009-08-14 14:18:49 -04001127 sendStickyBroadcast(intent);
Robert Greenwaltf55ced92010-01-20 19:29:41 -08001128 /*
1129 * If the failover network is already connected, then immediately send
1130 * out a followup broadcast indicating successful failover
1131 */
1132 if (newNet != null && newNet.getNetworkInfo().isConnected()) {
1133 sendConnectedBroadcast(newNet.getNetworkInfo());
1134 }
Mike Lockwoodfde2b762009-08-14 14:18:49 -04001135 }
1136
1137 private void sendStickyBroadcast(Intent intent) {
1138 synchronized(this) {
Dianne Hackborna417ff82009-12-08 19:45:14 -08001139 if (!mSystemReady) {
1140 mInitialBroadcast = new Intent(intent);
Mike Lockwoodfde2b762009-08-14 14:18:49 -04001141 }
Dianne Hackborna417ff82009-12-08 19:45:14 -08001142 intent.addFlags(Intent.FLAG_RECEIVER_REGISTERED_ONLY_BEFORE_BOOT);
1143 mContext.sendStickyBroadcast(intent);
Mike Lockwoodfde2b762009-08-14 14:18:49 -04001144 }
1145 }
1146
1147 void systemReady() {
1148 synchronized(this) {
1149 mSystemReady = true;
Dianne Hackborna417ff82009-12-08 19:45:14 -08001150 if (mInitialBroadcast != null) {
1151 mContext.sendStickyBroadcast(mInitialBroadcast);
1152 mInitialBroadcast = null;
Mike Lockwoodfde2b762009-08-14 14:18:49 -04001153 }
1154 }
The Android Open Source Project28527d22009-03-03 19:31:44 -08001155 }
1156
1157 private void handleConnect(NetworkInfo info) {
Robert Greenwalt2034b912009-08-12 16:08:25 -07001158 int type = info.getType();
The Android Open Source Project28527d22009-03-03 19:31:44 -08001159
1160 // snapshot isFailover, because sendConnectedBroadcast() resets it
1161 boolean isFailover = info.isFailover();
Robert Greenwalt2034b912009-08-12 16:08:25 -07001162 NetworkStateTracker thisNet = mNetTrackers[type];
The Android Open Source Project28527d22009-03-03 19:31:44 -08001163
Robert Greenwalt2034b912009-08-12 16:08:25 -07001164 // if this is a default net and other default is running
1165 // kill the one not preferred
1166 if (mNetAttributes[type].isDefault()) {
Robert Greenwalt2034b912009-08-12 16:08:25 -07001167 if (mActiveDefaultNetwork != -1 && mActiveDefaultNetwork != type) {
1168 if ((type != mNetworkPreference &&
1169 mNetAttributes[mActiveDefaultNetwork].mPriority >
1170 mNetAttributes[type].mPriority) ||
1171 mNetworkPreference == mActiveDefaultNetwork) {
1172 // don't accept this one
Joe Onoratoc2386bb2010-02-26 18:56:32 -08001173 if (DBG) Slog.v(TAG, "Not broadcasting CONNECT_ACTION " +
Robert Greenwalt2034b912009-08-12 16:08:25 -07001174 "to torn down network " + info.getTypeName());
1175 teardown(thisNet);
1176 return;
1177 } else {
1178 // tear down the other
1179 NetworkStateTracker otherNet =
1180 mNetTrackers[mActiveDefaultNetwork];
Joe Onoratoc2386bb2010-02-26 18:56:32 -08001181 if (DBG) Slog.v(TAG, "Policy requires " +
Robert Greenwalt2034b912009-08-12 16:08:25 -07001182 otherNet.getNetworkInfo().getTypeName() +
1183 " teardown");
1184 if (!teardown(otherNet)) {
Joe Onoratoc2386bb2010-02-26 18:56:32 -08001185 Slog.e(TAG, "Network declined teardown request");
Robert Greenwalt2034b912009-08-12 16:08:25 -07001186 return;
1187 }
Robert Greenwalt93dc1042010-06-15 12:19:37 -07001188 }
1189 }
1190 synchronized (ConnectivityService.this) {
1191 // have a new default network, release the transition wakelock in a second
1192 // if it's held. The second pause is to allow apps to reconnect over the
1193 // new network
1194 if (mNetTransitionWakeLock.isHeld()) {
1195 mHandler.sendMessageDelayed(mHandler.obtainMessage(
1196 NetworkStateTracker.EVENT_CLEAR_NET_TRANSITION_WAKELOCK,
1197 mNetTransitionWakeLockSerialNumber, 0),
1198 1000);
Robert Greenwalt2034b912009-08-12 16:08:25 -07001199 }
1200 }
1201 mActiveDefaultNetwork = type;
1202 }
The Android Open Source Project28527d22009-03-03 19:31:44 -08001203 thisNet.setTeardownRequested(false);
Irfan Sheriff7f132d92010-06-09 15:39:36 -07001204 updateNetworkSettings(thisNet);
Robert Greenwalt3afbead2010-07-23 15:46:26 -07001205 handleConnectivityChange(type);
Robert Greenwalt2034b912009-08-12 16:08:25 -07001206 sendConnectedBroadcast(info);
The Android Open Source Project28527d22009-03-03 19:31:44 -08001207 }
1208
The Android Open Source Project28527d22009-03-03 19:31:44 -08001209 /**
Robert Greenwalt3afbead2010-07-23 15:46:26 -07001210 * After a change in the connectivity state of a network. We're mainly
1211 * concerned with making sure that the list of DNS servers is set up
1212 * according to which networks are connected, and ensuring that the
1213 * right routing table entries exist.
The Android Open Source Project28527d22009-03-03 19:31:44 -08001214 */
Robert Greenwalt3afbead2010-07-23 15:46:26 -07001215 private void handleConnectivityChange(int netType) {
The Android Open Source Project28527d22009-03-03 19:31:44 -08001216 /*
Robert Greenwalt2034b912009-08-12 16:08:25 -07001217 * If a non-default network is enabled, add the host routes that
Robert Greenwalt3afbead2010-07-23 15:46:26 -07001218 * will allow it's DNS servers to be accessed.
The Android Open Source Project28527d22009-03-03 19:31:44 -08001219 */
Robert Greenwalt3afbead2010-07-23 15:46:26 -07001220 handleDnsConfigurationChange(netType);
Robert Greenwalt2034b912009-08-12 16:08:25 -07001221
Robert Greenwalt3afbead2010-07-23 15:46:26 -07001222 if (mNetTrackers[netType].getNetworkInfo().isConnected()) {
1223 if (mNetAttributes[netType].isDefault()) {
1224 addDefaultRoute(mNetTrackers[netType]);
Robert Greenwalt2034b912009-08-12 16:08:25 -07001225 } else {
Robert Greenwalt3afbead2010-07-23 15:46:26 -07001226 addPrivateDnsRoutes(mNetTrackers[netType]);
1227 }
1228 } else {
1229 if (mNetAttributes[netType].isDefault()) {
1230 removeDefaultRoute(mNetTrackers[netType]);
1231 } else {
1232 removePrivateDnsRoutes(mNetTrackers[netType]);
Robert Greenwalt2034b912009-08-12 16:08:25 -07001233 }
The Android Open Source Project28527d22009-03-03 19:31:44 -08001234 }
1235 }
1236
Irfan Sheriff7f132d92010-06-09 15:39:36 -07001237 private void addPrivateDnsRoutes(NetworkStateTracker nt) {
Irfan Sheriff7f132d92010-06-09 15:39:36 -07001238 boolean privateDnsRouteSet = nt.isPrivateDnsRouteSet();
Robert Greenwalta7dfbd32010-06-15 15:43:39 -07001239 NetworkProperties p = nt.getNetworkProperties();
1240 if (p == null) return;
1241 String interfaceName = p.getInterfaceName();
Irfan Sheriff7f132d92010-06-09 15:39:36 -07001242
1243 if (DBG) {
1244 Slog.d(TAG, "addPrivateDnsRoutes for " + nt +
1245 "(" + interfaceName + ") - mPrivateDnsRouteSet = " + privateDnsRouteSet);
1246 }
Irfan Sheriff7f132d92010-06-09 15:39:36 -07001247 if (interfaceName != null && !privateDnsRouteSet) {
Robert Greenwalta7dfbd32010-06-15 15:43:39 -07001248 Collection<InetAddress> dnsList = p.getDnses();
1249 for (InetAddress dns : dnsList) {
1250 if (DBG) Slog.d(TAG, " adding " + dns);
1251 NetworkUtils.addHostRoute(interfaceName, dns);
Irfan Sheriff7f132d92010-06-09 15:39:36 -07001252 }
1253 nt.privateDnsRouteSet(true);
1254 }
1255 }
1256
1257 private void removePrivateDnsRoutes(NetworkStateTracker nt) {
1258 // TODO - we should do this explicitly but the NetUtils api doesnt
1259 // support this yet - must remove all. No worse than before
Robert Greenwalta7dfbd32010-06-15 15:43:39 -07001260 NetworkProperties p = nt.getNetworkProperties();
1261 if (p == null) return;
1262 String interfaceName = p.getInterfaceName();
Irfan Sheriff7f132d92010-06-09 15:39:36 -07001263 boolean privateDnsRouteSet = nt.isPrivateDnsRouteSet();
1264 if (interfaceName != null && privateDnsRouteSet) {
1265 if (DBG) {
1266 Slog.d(TAG, "removePrivateDnsRoutes for " + nt.getNetworkInfo().getTypeName() +
1267 " (" + interfaceName + ")");
1268 }
1269 NetworkUtils.removeHostRoutes(interfaceName);
1270 nt.privateDnsRouteSet(false);
1271 }
1272 }
1273
Irfan Sheriff7f132d92010-06-09 15:39:36 -07001274
1275 private void addDefaultRoute(NetworkStateTracker nt) {
Robert Greenwalta7dfbd32010-06-15 15:43:39 -07001276 NetworkProperties p = nt.getNetworkProperties();
1277 if (p == null) return;
1278 String interfaceName = p.getInterfaceName();
1279 InetAddress defaultGatewayAddr = p.getGateway();
Irfan Sheriff7f132d92010-06-09 15:39:36 -07001280
Robert Greenwalt3afbead2010-07-23 15:46:26 -07001281 if ((interfaceName != null) && (defaultGatewayAddr != null )) {
1282 if ((NetworkUtils.setDefaultRoute(interfaceName, defaultGatewayAddr) >= 0) && DBG) {
Robert Greenwalta7dfbd32010-06-15 15:43:39 -07001283 NetworkInfo networkInfo = nt.getNetworkInfo();
Irfan Sheriff7f132d92010-06-09 15:39:36 -07001284 Slog.d(TAG, "addDefaultRoute for " + networkInfo.getTypeName() +
1285 " (" + interfaceName + "), GatewayAddr=" + defaultGatewayAddr);
1286 }
Irfan Sheriff7f132d92010-06-09 15:39:36 -07001287 }
1288 }
1289
1290
1291 public void removeDefaultRoute(NetworkStateTracker nt) {
Robert Greenwalta7dfbd32010-06-15 15:43:39 -07001292 NetworkProperties p = nt.getNetworkProperties();
1293 if (p == null) return;
1294 String interfaceName = p.getInterfaceName();
Irfan Sheriff7f132d92010-06-09 15:39:36 -07001295
Robert Greenwalt3afbead2010-07-23 15:46:26 -07001296 if (interfaceName != null) {
1297 if ((NetworkUtils.removeDefaultRoute(interfaceName) >= 0) && DBG) {
Robert Greenwalta7dfbd32010-06-15 15:43:39 -07001298 NetworkInfo networkInfo = nt.getNetworkInfo();
Irfan Sheriff7f132d92010-06-09 15:39:36 -07001299 Slog.d(TAG, "removeDefaultRoute for " + networkInfo.getTypeName() + " (" +
1300 interfaceName + ")");
1301 }
Irfan Sheriff7f132d92010-06-09 15:39:36 -07001302 }
1303 }
1304
1305 /**
1306 * Reads the network specific TCP buffer sizes from SystemProperties
1307 * net.tcp.buffersize.[default|wifi|umts|edge|gprs] and set them for system
1308 * wide use
1309 */
1310 public void updateNetworkSettings(NetworkStateTracker nt) {
1311 String key = nt.getTcpBufferSizesPropName();
1312 String bufferSizes = SystemProperties.get(key);
1313
1314 if (bufferSizes.length() == 0) {
1315 Slog.e(TAG, key + " not found in system properties. Using defaults");
1316
1317 // Setting to default values so we won't be stuck to previous values
1318 key = "net.tcp.buffersize.default";
1319 bufferSizes = SystemProperties.get(key);
1320 }
1321
1322 // Set values in kernel
1323 if (bufferSizes.length() != 0) {
1324 if (DBG) {
1325 Slog.v(TAG, "Setting TCP values: [" + bufferSizes
1326 + "] which comes from [" + key + "]");
1327 }
1328 setBufferSize(bufferSizes);
1329 }
1330 }
1331
1332 /**
1333 * Writes TCP buffer sizes to /sys/kernel/ipv4/tcp_[r/w]mem_[min/def/max]
1334 * which maps to /proc/sys/net/ipv4/tcp_rmem and tcpwmem
1335 *
1336 * @param bufferSizes in the format of "readMin, readInitial, readMax,
1337 * writeMin, writeInitial, writeMax"
1338 */
1339 private void setBufferSize(String bufferSizes) {
1340 try {
1341 String[] values = bufferSizes.split(",");
1342
1343 if (values.length == 6) {
1344 final String prefix = "/sys/kernel/ipv4/tcp_";
1345 stringToFile(prefix + "rmem_min", values[0]);
1346 stringToFile(prefix + "rmem_def", values[1]);
1347 stringToFile(prefix + "rmem_max", values[2]);
1348 stringToFile(prefix + "wmem_min", values[3]);
1349 stringToFile(prefix + "wmem_def", values[4]);
1350 stringToFile(prefix + "wmem_max", values[5]);
1351 } else {
1352 Slog.e(TAG, "Invalid buffersize string: " + bufferSizes);
1353 }
1354 } catch (IOException e) {
1355 Slog.e(TAG, "Can't set tcp buffer sizes:" + e);
1356 }
1357 }
1358
1359 /**
1360 * Writes string to file. Basically same as "echo -n $string > $filename"
1361 *
1362 * @param filename
1363 * @param string
1364 * @throws IOException
1365 */
1366 private void stringToFile(String filename, String string) throws IOException {
1367 FileWriter out = new FileWriter(filename);
1368 try {
1369 out.write(string);
1370 } finally {
1371 out.close();
1372 }
1373 }
1374
1375
Robert Greenwalt2034b912009-08-12 16:08:25 -07001376 /**
1377 * Adjust the per-process dns entries (net.dns<x>.<pid>) based
1378 * on the highest priority active net which this process requested.
1379 * If there aren't any, clear it out
1380 */
1381 private void reassessPidDns(int myPid, boolean doBump)
1382 {
Joe Onoratoc2386bb2010-02-26 18:56:32 -08001383 if (DBG) Slog.d(TAG, "reassessPidDns for pid " + myPid);
Robert Greenwalt2034b912009-08-12 16:08:25 -07001384 for(int i : mPriorityList) {
1385 if (mNetAttributes[i].isDefault()) {
1386 continue;
1387 }
1388 NetworkStateTracker nt = mNetTrackers[i];
Robert Greenwalt0659da32009-07-16 17:21:39 -07001389 if (nt.getNetworkInfo().isConnected() &&
1390 !nt.isTeardownRequested()) {
Robert Greenwalta7dfbd32010-06-15 15:43:39 -07001391 NetworkProperties p = nt.getNetworkProperties();
1392 if (p == null) continue;
Robert Greenwalt2034b912009-08-12 16:08:25 -07001393 List pids = mNetRequestersPids[i];
1394 for (int j=0; j<pids.size(); j++) {
1395 Integer pid = (Integer)pids.get(j);
1396 if (pid.intValue() == myPid) {
Robert Greenwalta7dfbd32010-06-15 15:43:39 -07001397 Collection<InetAddress> dnses = p.getDnses();
1398 writePidDns(dnses, myPid);
Robert Greenwalt2034b912009-08-12 16:08:25 -07001399 if (doBump) {
1400 bumpDns();
1401 }
1402 return;
1403 }
1404 }
1405 }
1406 }
1407 // nothing found - delete
1408 for (int i = 1; ; i++) {
1409 String prop = "net.dns" + i + "." + myPid;
1410 if (SystemProperties.get(prop).length() == 0) {
1411 if (doBump) {
1412 bumpDns();
1413 }
1414 return;
1415 }
1416 SystemProperties.set(prop, "");
1417 }
1418 }
1419
Robert Greenwalta7dfbd32010-06-15 15:43:39 -07001420 private void writePidDns(Collection <InetAddress> dnses, int pid) {
Robert Greenwalt2034b912009-08-12 16:08:25 -07001421 int j = 1;
Robert Greenwalta7dfbd32010-06-15 15:43:39 -07001422 for (InetAddress dns : dnses) {
1423 SystemProperties.set("net.dns" + j++ + "." + pid, dns.getHostAddress());
Robert Greenwalt2034b912009-08-12 16:08:25 -07001424 }
1425 }
1426
1427 private void bumpDns() {
1428 /*
1429 * Bump the property that tells the name resolver library to reread
1430 * the DNS server list from the properties.
1431 */
1432 String propVal = SystemProperties.get("net.dnschange");
1433 int n = 0;
1434 if (propVal.length() != 0) {
1435 try {
1436 n = Integer.parseInt(propVal);
1437 } catch (NumberFormatException e) {}
1438 }
1439 SystemProperties.set("net.dnschange", "" + (n+1));
1440 }
1441
Robert Greenwalt3afbead2010-07-23 15:46:26 -07001442 private void handleDnsConfigurationChange(int netType) {
Robert Greenwalt2034b912009-08-12 16:08:25 -07001443 // add default net's dns entries
Robert Greenwalt3afbead2010-07-23 15:46:26 -07001444 NetworkStateTracker nt = mNetTrackers[netType];
1445 if (nt != null && nt.getNetworkInfo().isConnected() && !nt.isTeardownRequested()) {
1446 NetworkProperties p = nt.getNetworkProperties();
1447 if (p == null) return;
1448 Collection<InetAddress> dnses = p.getDnses();
1449 if (mNetAttributes[netType].isDefault()) {
1450 int j = 1;
1451 for (InetAddress dns : dnses) {
1452 if (DBG) {
1453 Slog.d(TAG, "adding dns " + dns + " for " +
1454 nt.getNetworkInfo().getTypeName());
Robert Greenwalt2034b912009-08-12 16:08:25 -07001455 }
Robert Greenwalt3afbead2010-07-23 15:46:26 -07001456 SystemProperties.set("net.dns" + j++, dns.getHostAddress());
1457 }
1458 for (int k=j ; k<mNumDnsEntries; k++) {
1459 if (DBG) Slog.d(TAG, "erasing net.dns" + k);
1460 SystemProperties.set("net.dns" + k, "");
1461 }
1462 mNumDnsEntries = j;
1463 } else {
1464 // set per-pid dns for attached secondary nets
1465 List pids = mNetRequestersPids[netType];
1466 for (int y=0; y< pids.size(); y++) {
1467 Integer pid = (Integer)pids.get(y);
1468 writePidDns(dnses, pid.intValue());
The Android Open Source Project28527d22009-03-03 19:31:44 -08001469 }
1470 }
Robert Greenwalt3afbead2010-07-23 15:46:26 -07001471 bumpDns();
The Android Open Source Project28527d22009-03-03 19:31:44 -08001472 }
Robert Greenwalt2034b912009-08-12 16:08:25 -07001473 }
1474
1475 private int getRestoreDefaultNetworkDelay() {
1476 String restoreDefaultNetworkDelayStr = SystemProperties.get(
1477 NETWORK_RESTORE_DELAY_PROP_NAME);
1478 if(restoreDefaultNetworkDelayStr != null &&
1479 restoreDefaultNetworkDelayStr.length() != 0) {
1480 try {
1481 return Integer.valueOf(restoreDefaultNetworkDelayStr);
1482 } catch (NumberFormatException e) {
1483 }
The Android Open Source Project28527d22009-03-03 19:31:44 -08001484 }
Robert Greenwalt2034b912009-08-12 16:08:25 -07001485 return RESTORE_DEFAULT_NETWORK_DELAY;
The Android Open Source Project28527d22009-03-03 19:31:44 -08001486 }
1487
1488 @Override
1489 protected void dump(FileDescriptor fd, PrintWriter pw, String[] args) {
Robert Greenwalt0659da32009-07-16 17:21:39 -07001490 if (mContext.checkCallingOrSelfPermission(
1491 android.Manifest.permission.DUMP)
The Android Open Source Project28527d22009-03-03 19:31:44 -08001492 != PackageManager.PERMISSION_GRANTED) {
Robert Greenwalt0659da32009-07-16 17:21:39 -07001493 pw.println("Permission Denial: can't dump ConnectivityService " +
1494 "from from pid=" + Binder.getCallingPid() + ", uid=" +
1495 Binder.getCallingUid());
The Android Open Source Project28527d22009-03-03 19:31:44 -08001496 return;
1497 }
The Android Open Source Project28527d22009-03-03 19:31:44 -08001498 pw.println();
1499 for (NetworkStateTracker nst : mNetTrackers) {
Robert Greenwalt3eeb6032009-12-21 18:24:07 -08001500 if (nst != null) {
1501 if (nst.getNetworkInfo().isConnected()) {
1502 pw.println("Active network: " + nst.getNetworkInfo().
1503 getTypeName());
1504 }
1505 pw.println(nst.getNetworkInfo());
1506 pw.println(nst);
1507 pw.println();
Robert Greenwalt2034b912009-08-12 16:08:25 -07001508 }
The Android Open Source Project28527d22009-03-03 19:31:44 -08001509 }
Robert Greenwalt3eeb6032009-12-21 18:24:07 -08001510
1511 pw.println("Network Requester Pids:");
1512 for (int net : mPriorityList) {
1513 String pidString = net + ": ";
1514 for (Object pid : mNetRequestersPids[net]) {
1515 pidString = pidString + pid.toString() + ", ";
1516 }
1517 pw.println(pidString);
1518 }
1519 pw.println();
1520
1521 pw.println("FeatureUsers:");
1522 for (Object requester : mFeatureUsers) {
1523 pw.println(requester.toString());
1524 }
1525 pw.println();
Robert Greenwalt8e87f122010-02-11 18:18:40 -08001526
Robert Greenwalt93dc1042010-06-15 12:19:37 -07001527 synchronized (this) {
1528 pw.println("NetworkTranstionWakeLock is currently " +
1529 (mNetTransitionWakeLock.isHeld() ? "" : "not ") + "held.");
1530 pw.println("It was last requested for "+mNetTransitionWakeLockCausedBy);
1531 }
1532 pw.println();
1533
Robert Greenwalt8e87f122010-02-11 18:18:40 -08001534 mTethering.dump(fd, pw, args);
The Android Open Source Project28527d22009-03-03 19:31:44 -08001535 }
1536
Robert Greenwalt2034b912009-08-12 16:08:25 -07001537 // must be stateless - things change under us.
The Android Open Source Project28527d22009-03-03 19:31:44 -08001538 private class MyHandler extends Handler {
1539 @Override
1540 public void handleMessage(Message msg) {
1541 NetworkInfo info;
1542 switch (msg.what) {
1543 case NetworkStateTracker.EVENT_STATE_CHANGED:
1544 info = (NetworkInfo) msg.obj;
Robert Greenwalt12c44552009-12-07 11:33:18 -08001545 int type = info.getType();
1546 NetworkInfo.State state = info.getState();
Robert Greenwalt24e2d2b2010-01-25 16:14:00 -08001547 // only do this optimization for wifi. It going into scan mode for location
1548 // services generates alot of noise. Meanwhile the mms apn won't send out
1549 // subsequent notifications when on default cellular because it never
1550 // disconnects.. so only do this to wifi notifications. Fixed better when the
1551 // APN notifications are standardized.
1552 if (mNetAttributes[type].mLastState == state &&
1553 mNetAttributes[type].mRadio == ConnectivityManager.TYPE_WIFI) {
Robert Greenwalt12c44552009-12-07 11:33:18 -08001554 if (DBG) {
Robert Greenwalt24e2d2b2010-01-25 16:14:00 -08001555 // TODO - remove this after we validate the dropping doesn't break
1556 // anything
Joe Onoratoc2386bb2010-02-26 18:56:32 -08001557 Slog.d(TAG, "Dropping ConnectivityChange for " +
Robert Greenwalt2adbc7f2010-01-13 09:36:31 -08001558 info.getTypeName() + ": " +
Robert Greenwalt12c44552009-12-07 11:33:18 -08001559 state + "/" + info.getDetailedState());
1560 }
1561 return;
1562 }
1563 mNetAttributes[type].mLastState = state;
1564
Joe Onoratoc2386bb2010-02-26 18:56:32 -08001565 if (DBG) Slog.d(TAG, "ConnectivityChange for " +
Robert Greenwalt0659da32009-07-16 17:21:39 -07001566 info.getTypeName() + ": " +
Robert Greenwalt12c44552009-12-07 11:33:18 -08001567 state + "/" + info.getDetailedState());
The Android Open Source Project28527d22009-03-03 19:31:44 -08001568
1569 // Connectivity state changed:
1570 // [31-13] Reserved for future use
Robert Greenwalt0659da32009-07-16 17:21:39 -07001571 // [12-9] Network subtype (for mobile network, as defined
1572 // by TelephonyManager)
1573 // [8-3] Detailed state ordinal (as defined by
1574 // NetworkInfo.DetailedState)
The Android Open Source Project28527d22009-03-03 19:31:44 -08001575 // [2-0] Network type (as defined by ConnectivityManager)
1576 int eventLogParam = (info.getType() & 0x7) |
1577 ((info.getDetailedState().ordinal() & 0x3f) << 3) |
1578 (info.getSubtype() << 9);
Doug Zongker2fc96232009-12-04 10:31:43 -08001579 EventLog.writeEvent(EventLogTags.CONNECTIVITY_STATE_CHANGED,
Robert Greenwalt0659da32009-07-16 17:21:39 -07001580 eventLogParam);
1581
1582 if (info.getDetailedState() ==
1583 NetworkInfo.DetailedState.FAILED) {
The Android Open Source Project28527d22009-03-03 19:31:44 -08001584 handleConnectionFailure(info);
Robert Greenwalt12c44552009-12-07 11:33:18 -08001585 } else if (state == NetworkInfo.State.DISCONNECTED) {
The Android Open Source Project28527d22009-03-03 19:31:44 -08001586 handleDisconnect(info);
Robert Greenwalt12c44552009-12-07 11:33:18 -08001587 } else if (state == NetworkInfo.State.SUSPENDED) {
The Android Open Source Project28527d22009-03-03 19:31:44 -08001588 // TODO: need to think this over.
Robert Greenwalt0659da32009-07-16 17:21:39 -07001589 // the logic here is, handle SUSPENDED the same as
1590 // DISCONNECTED. The only difference being we are
1591 // broadcasting an intent with NetworkInfo that's
1592 // suspended. This allows the applications an
1593 // opportunity to handle DISCONNECTED and SUSPENDED
1594 // differently, or not.
The Android Open Source Project28527d22009-03-03 19:31:44 -08001595 handleDisconnect(info);
Robert Greenwalt12c44552009-12-07 11:33:18 -08001596 } else if (state == NetworkInfo.State.CONNECTED) {
The Android Open Source Project28527d22009-03-03 19:31:44 -08001597 handleConnect(info);
1598 }
The Android Open Source Project28527d22009-03-03 19:31:44 -08001599 break;
The Android Open Source Project28527d22009-03-03 19:31:44 -08001600 case NetworkStateTracker.EVENT_CONFIGURATION_CHANGED:
Robert Greenwalt3afbead2010-07-23 15:46:26 -07001601 // TODO - make this handle ip/proxy/gateway/dns changes
1602 info = (NetworkInfo) msg.obj;
1603 type = info.getType();
1604 handleDnsConfigurationChange(type);
The Android Open Source Project28527d22009-03-03 19:31:44 -08001605 break;
Robert Greenwalt2034b912009-08-12 16:08:25 -07001606 case NetworkStateTracker.EVENT_RESTORE_DEFAULT_NETWORK:
Robert Greenwaltaffc3a12009-09-27 17:27:04 -07001607 FeatureUser u = (FeatureUser)msg.obj;
1608 u.expire();
Robert Greenwalt2034b912009-08-12 16:08:25 -07001609 break;
Robert Greenwalt93dc1042010-06-15 12:19:37 -07001610 case NetworkStateTracker.EVENT_CLEAR_NET_TRANSITION_WAKELOCK:
1611 String causedBy = null;
1612 synchronized (ConnectivityService.this) {
1613 if (msg.arg1 == mNetTransitionWakeLockSerialNumber &&
1614 mNetTransitionWakeLock.isHeld()) {
1615 mNetTransitionWakeLock.release();
1616 causedBy = mNetTransitionWakeLockCausedBy;
1617 }
1618 }
1619 if (causedBy != null) {
1620 Slog.d(TAG, "NetTransition Wakelock for " +
1621 causedBy + " released by timeout");
1622 }
1623 break;
The Android Open Source Project28527d22009-03-03 19:31:44 -08001624 }
1625 }
1626 }
Robert Greenwalt0c4828c2010-01-26 11:40:34 -08001627
1628 // javadoc from interface
Robert Greenwalt4283ded2010-03-02 17:25:02 -08001629 public int tether(String iface) {
Robert Greenwalt0c4828c2010-01-26 11:40:34 -08001630 enforceTetherChangePermission();
Robert Greenwalt4283ded2010-03-02 17:25:02 -08001631
1632 if (isTetheringSupported()) {
1633 return mTethering.tether(iface);
1634 } else {
1635 return ConnectivityManager.TETHER_ERROR_UNSUPPORTED;
1636 }
Robert Greenwalt0c4828c2010-01-26 11:40:34 -08001637 }
1638
1639 // javadoc from interface
Robert Greenwalt4283ded2010-03-02 17:25:02 -08001640 public int untether(String iface) {
Robert Greenwalt0c4828c2010-01-26 11:40:34 -08001641 enforceTetherChangePermission();
Robert Greenwalt4283ded2010-03-02 17:25:02 -08001642
1643 if (isTetheringSupported()) {
1644 return mTethering.untether(iface);
1645 } else {
1646 return ConnectivityManager.TETHER_ERROR_UNSUPPORTED;
1647 }
1648 }
1649
1650 // javadoc from interface
1651 public int getLastTetherError(String iface) {
1652 enforceTetherAccessPermission();
1653
1654 if (isTetheringSupported()) {
1655 return mTethering.getLastTetherError(iface);
1656 } else {
1657 return ConnectivityManager.TETHER_ERROR_UNSUPPORTED;
1658 }
Robert Greenwalt8e87f122010-02-11 18:18:40 -08001659 }
1660
1661 // TODO - proper iface API for selection by property, inspection, etc
1662 public String[] getTetherableUsbRegexs() {
1663 enforceTetherAccessPermission();
1664 if (isTetheringSupported()) {
1665 return mTethering.getTetherableUsbRegexs();
1666 } else {
1667 return new String[0];
1668 }
1669 }
1670
1671 public String[] getTetherableWifiRegexs() {
1672 enforceTetherAccessPermission();
1673 if (isTetheringSupported()) {
1674 return mTethering.getTetherableWifiRegexs();
1675 } else {
1676 return new String[0];
1677 }
Robert Greenwalt0c4828c2010-01-26 11:40:34 -08001678 }
1679
Danica Chang96567052010-08-11 14:54:43 -07001680 public String[] getTetherableBluetoothRegexs() {
1681 enforceTetherAccessPermission();
1682 if (isTetheringSupported()) {
1683 return mTethering.getTetherableBluetoothRegexs();
1684 } else {
1685 return new String[0];
1686 }
1687 }
1688
Robert Greenwalt0c4828c2010-01-26 11:40:34 -08001689 // TODO - move iface listing, queries, etc to new module
1690 // javadoc from interface
1691 public String[] getTetherableIfaces() {
Robert Greenwalt8e87f122010-02-11 18:18:40 -08001692 enforceTetherAccessPermission();
Robert Greenwalt0c4828c2010-01-26 11:40:34 -08001693 return mTethering.getTetherableIfaces();
1694 }
1695
1696 public String[] getTetheredIfaces() {
Robert Greenwalt8e87f122010-02-11 18:18:40 -08001697 enforceTetherAccessPermission();
Robert Greenwalt0c4828c2010-01-26 11:40:34 -08001698 return mTethering.getTetheredIfaces();
1699 }
Robert Greenwalt8e87f122010-02-11 18:18:40 -08001700
Robert Greenwalt4283ded2010-03-02 17:25:02 -08001701 public String[] getTetheringErroredIfaces() {
1702 enforceTetherAccessPermission();
1703 return mTethering.getErroredIfaces();
1704 }
1705
Robert Greenwalt8e87f122010-02-11 18:18:40 -08001706 // if ro.tether.denied = true we default to no tethering
1707 // gservices could set the secure setting to 1 though to enable it on a build where it
1708 // had previously been turned off.
1709 public boolean isTetheringSupported() {
1710 enforceTetherAccessPermission();
1711 int defaultVal = (SystemProperties.get("ro.tether.denied").equals("true") ? 0 : 1);
Robert Greenwaltf1b66e12010-02-25 12:29:30 -08001712 boolean tetherEnabledInSettings = (Settings.Secure.getInt(mContext.getContentResolver(),
1713 Settings.Secure.TETHER_SUPPORTED, defaultVal) != 0);
1714 return tetherEnabledInSettings && mTetheringConfigValid;
Robert Greenwalt8e87f122010-02-11 18:18:40 -08001715 }
Robert Greenwalt93dc1042010-06-15 12:19:37 -07001716
1717 // An API NetworkStateTrackers can call when they lose their network.
1718 // This will automatically be cleared after X seconds or a network becomes CONNECTED,
1719 // whichever happens first. The timer is started by the first caller and not
1720 // restarted by subsequent callers.
1721 public void requestNetworkTransitionWakelock(String forWhom) {
1722 enforceConnectivityInternalPermission();
1723 synchronized (this) {
1724 if (mNetTransitionWakeLock.isHeld()) return;
1725 mNetTransitionWakeLockSerialNumber++;
1726 mNetTransitionWakeLock.acquire();
1727 mNetTransitionWakeLockCausedBy = forWhom;
1728 }
1729 mHandler.sendMessageDelayed(mHandler.obtainMessage(
1730 NetworkStateTracker.EVENT_CLEAR_NET_TRANSITION_WAKELOCK,
1731 mNetTransitionWakeLockSerialNumber, 0),
1732 mNetTransitionWakeLockTimeout);
1733 return;
1734 }
The Android Open Source Project28527d22009-03-03 19:31:44 -08001735}