blob: 859353a7cb0df96a819ddc86cf8e633c15682f44 [file] [log] [blame]
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001/*
2 * Copyright (C) 2008 The Android Open Source Project
3 *
4 * Licensed under the Apache License, Version 2.0 (the "License");
5 * you may not use this file except in compliance with the License.
6 * You may obtain a copy of the License at
7 *
8 * http://www.apache.org/licenses/LICENSE-2.0
9 *
10 * Unless required by applicable law or agreed to in writing, software
11 * distributed under the License is distributed on an "AS IS" BASIS,
12 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13 * See the License for the specific language governing permissions and
14 * limitations under the License.
15 */
16
17package com.android.server;
18
19import android.app.Notification;
20import android.app.NotificationManager;
21import android.content.ContentResolver;
22import android.content.Context;
23import android.content.Intent;
24import android.content.pm.PackageManager;
25import android.net.ConnectivityManager;
26import android.net.IConnectivityManager;
27import android.net.MobileDataStateTracker;
28import android.net.NetworkInfo;
Robert Greenwalt47f69fe2010-06-15 15:43:39 -070029import android.net.NetworkProperties;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080030import android.net.NetworkStateTracker;
31import android.net.wifi.WifiStateTracker;
Irfan Sheriffd649c122010-06-09 15:39:36 -070032import android.net.NetworkUtils;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080033import android.os.Binder;
34import android.os.Handler;
Robert Greenwalt42acef32009-08-12 16:08:25 -070035import android.os.IBinder;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080036import android.os.Looper;
37import android.os.Message;
Robert Greenwalt14f2ef42010-06-15 12:19:37 -070038import android.os.PowerManager;
Robert Greenwalt42acef32009-08-12 16:08:25 -070039import android.os.RemoteException;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080040import android.os.ServiceManager;
41import android.os.SystemProperties;
42import android.provider.Settings;
Robert Greenwalt42acef32009-08-12 16:08:25 -070043import android.text.TextUtils;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080044import android.util.EventLog;
Joe Onorato8a9b2202010-02-26 18:56:32 -080045import android.util.Slog;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080046
Robert Greenwalt42acef32009-08-12 16:08:25 -070047import com.android.internal.telephony.Phone;
48
Robert Greenwaltd0e18ff2010-01-26 11:40:34 -080049import com.android.server.connectivity.Tethering;
50
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080051import java.io.FileDescriptor;
Irfan Sheriffd649c122010-06-09 15:39:36 -070052import java.io.FileWriter;
53import java.io.IOException;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080054import java.io.PrintWriter;
Robert Greenwalt47f69fe2010-06-15 15:43:39 -070055import java.net.InetAddress;
56import java.net.UnknownHostException;
Robert Greenwalt42acef32009-08-12 16:08:25 -070057import java.util.ArrayList;
Robert Greenwalt47f69fe2010-06-15 15:43:39 -070058import java.util.Collection;
Robert Greenwalt42acef32009-08-12 16:08:25 -070059import java.util.List;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080060
61/**
62 * @hide
63 */
64public class ConnectivityService extends IConnectivityManager.Stub {
65
Robert Greenwaltd8df1492009-10-06 14:12:53 -070066 private static final boolean DBG = true;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080067 private static final String TAG = "ConnectivityService";
68
Robert Greenwalt42acef32009-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 Greenwaltd0e18ff2010-01-26 11:40:34 -080075 private Tethering mTethering;
Robert Greenwaltc9d5fb72010-02-25 12:29:30 -080076 private boolean mTetheringConfigValid = false;
Robert Greenwaltd0e18ff2010-01-26 11:40:34 -080077
The Android Open Source Project9066cfe2009-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 Greenwalt42acef32009-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 Sheriffa2a1b912010-06-07 09:03:04 -070091 private WifiWatchdogService mWifiWatchdogService;
92
Robert Greenwalt42acef32009-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 Project9066cfe2009-03-03 19:31:44 -080098 private Context mContext;
99 private int mNetworkPreference;
Robert Greenwalt42acef32009-08-12 16:08:25 -0700100 private int mActiveDefaultNetwork = -1;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800101
102 private int mNumDnsEntries;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800103
104 private boolean mTestMode;
105 private static ConnectivityService sServiceInstance;
106
Robert Greenwalt42acef32009-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 Lockwood0f79b542009-08-14 14:18:49 -0400113 private boolean mSystemReady;
Dianne Hackborn1c633fc2009-12-08 19:45:14 -0800114 private Intent mInitialBroadcast;
Mike Lockwood0f79b542009-08-14 14:18:49 -0400115
Robert Greenwalt14f2ef42010-06-15 12:19:37 -0700116 private PowerManager.WakeLock mNetTransitionWakeLock;
117 private String mNetTransitionWakeLockCausedBy = "";
118 private int mNetTransitionWakeLockSerialNumber;
119 private int mNetTransitionWakeLockTimeout;
120
Robert Greenwalt511288a2009-12-07 11:33:18 -0800121 private static class NetworkAttributes {
Robert Greenwalt42acef32009-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 Greenwalt511288a2009-12-07 11:33:18 -0800129 public NetworkInfo.State mLastState;
Robert Greenwalt42acef32009-08-12 16:08:25 -0700130 public NetworkAttributes(String init) {
131 String fragments[] = init.split(",");
132 mName = fragments[0].toLowerCase();
Robert Greenwalt5154ae762009-10-30 14:17:42 -0700133 mType = Integer.parseInt(fragments[1]);
134 mRadio = Integer.parseInt(fragments[2]);
135 mPriority = Integer.parseInt(fragments[3]);
Robert Greenwalt511288a2009-12-07 11:33:18 -0800136 mLastState = NetworkInfo.State.UNKNOWN;
Robert Greenwalt42acef32009-08-12 16:08:25 -0700137 }
138 public boolean isDefault() {
139 return (mType == mRadio);
140 }
141 }
142 NetworkAttributes[] mNetAttributes;
Robert Greenwalt5154ae762009-10-30 14:17:42 -0700143 int mNetworksDefined;
Robert Greenwalt42acef32009-08-12 16:08:25 -0700144
Robert Greenwalt511288a2009-12-07 11:33:18 -0800145 private static class RadioAttributes {
Robert Greenwalt42acef32009-08-12 16:08:25 -0700146 public int mSimultaneity;
147 public int mType;
148 public RadioAttributes(String init) {
149 String fragments[] = init.split(",");
Robert Greenwalt5154ae762009-10-30 14:17:42 -0700150 mType = Integer.parseInt(fragments[0]);
151 mSimultaneity = Integer.parseInt(fragments[1]);
Robert Greenwalt42acef32009-08-12 16:08:25 -0700152 }
153 }
154 RadioAttributes[] mRadioAttributes;
155
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800156 private static class ConnectivityThread extends Thread {
157 private Context mContext;
Robert Greenwalt86e9e552009-07-16 17:21:39 -0700158
The Android Open Source Project9066cfe2009-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 Greenwalt86e9e552009-07-16 17:21:39 -0700173
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800174 public static ConnectivityService getServiceInstance(Context context) {
175 ConnectivityThread thread = new ConnectivityThread(context);
176 thread.start();
Robert Greenwalt86e9e552009-07-16 17:21:39 -0700177
The Android Open Source Project9066cfe2009-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 Onorato8a9b2202010-02-26 18:56:32 -0800184 Slog.e(TAG,
Robert Greenwalt86e9e552009-07-16 17:21:39 -0700185 "Unexpected InterruptedException while waiting"+
186 " for ConnectivityService thread");
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800187 }
188 }
189 }
Robert Greenwalt86e9e552009-07-16 17:21:39 -0700190
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800191 return sServiceInstance;
192 }
193 }
Robert Greenwalt86e9e552009-07-16 17:21:39 -0700194
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800195 public static ConnectivityService getInstance(Context context) {
196 return ConnectivityThread.getServiceInstance(context);
197 }
Robert Greenwalt86e9e552009-07-16 17:21:39 -0700198
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800199 private ConnectivityService(Context context) {
Joe Onorato8a9b2202010-02-26 18:56:32 -0800200 if (DBG) Slog.v(TAG, "ConnectivityService starting up");
Robert Greenwaltde8383c2010-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 Project9066cfe2009-03-03 19:31:44 -0800210 mContext = context;
Robert Greenwalt14f2ef42010-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 Greenwalt42acef32009-08-12 16:08:25 -0700217 mNetTrackers = new NetworkStateTracker[
218 ConnectivityManager.MAX_NETWORK_TYPE+1];
219 mHandler = new MyHandler();
Robert Greenwalt86e9e552009-07-16 17:21:39 -0700220
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800221 mNetworkPreference = getPersistedNetworkPreference();
Robert Greenwalt86e9e552009-07-16 17:21:39 -0700222
Robert Greenwalt5154ae762009-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 Greenwalt42acef32009-08-12 16:08:25 -0700226 // Load device network attributes from resources
Robert Greenwalt42acef32009-08-12 16:08:25 -0700227 String[] raStrings = context.getResources().getStringArray(
228 com.android.internal.R.array.radioAttributes);
Robert Greenwalt5154ae762009-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 Onorato8a9b2202010-02-26 18:56:32 -0800232 Slog.e(TAG, "Error in radioAttributes - ignoring attempt to define type " + r.mType);
Robert Greenwalt5154ae762009-10-30 14:17:42 -0700233 continue;
234 }
235 if (mRadioAttributes[r.mType] != null) {
Joe Onorato8a9b2202010-02-26 18:56:32 -0800236 Slog.e(TAG, "Error in radioAttributes - ignoring attempt to redefine type " +
Robert Greenwalt5154ae762009-10-30 14:17:42 -0700237 r.mType);
238 continue;
239 }
Robert Greenwalt42acef32009-08-12 16:08:25 -0700240 mRadioAttributes[r.mType] = r;
241 }
242
Robert Greenwalt5154ae762009-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 Onorato8a9b2202010-02-26 18:56:32 -0800249 Slog.e(TAG, "Error in networkAttributes - ignoring attempt to define type " +
Robert Greenwalt5154ae762009-10-30 14:17:42 -0700250 n.mType);
251 continue;
Robert Greenwalt42acef32009-08-12 16:08:25 -0700252 }
Robert Greenwalt5154ae762009-10-30 14:17:42 -0700253 if (mNetAttributes[n.mType] != null) {
Joe Onorato8a9b2202010-02-26 18:56:32 -0800254 Slog.e(TAG, "Error in networkAttributes - ignoring attempt to redefine type " +
Robert Greenwalt5154ae762009-10-30 14:17:42 -0700255 n.mType);
256 continue;
257 }
258 if (mRadioAttributes[n.mRadio] == null) {
Joe Onorato8a9b2202010-02-26 18:56:32 -0800259 Slog.e(TAG, "Error in networkAttributes - ignoring attempt to use undefined " +
Robert Greenwalt5154ae762009-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 Greenwalt42acef32009-08-12 16:08:25 -0700267 }
268 }
269
Robert Greenwalt5154ae762009-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 Greenwalt42acef32009-08-12 16:08:25 -0700295 mNetRequestersPids[i] = new ArrayList();
296 }
297
298 mFeatureUsers = new ArrayList();
299
Robert Greenwalt5154ae762009-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 Project9066cfe2009-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 Greenwaltc03fa502010-02-23 18:58:05 -0800311 boolean noMobileData = !getMobileDataEnabled();
Robert Greenwalt5154ae762009-10-30 14:17:42 -0700312 for (int netType : mPriorityList) {
313 switch (mNetAttributes[netType].mRadio) {
314 case ConnectivityManager.TYPE_WIFI:
Joe Onorato8a9b2202010-02-26 18:56:32 -0800315 if (DBG) Slog.v(TAG, "Starting Wifi Service.");
Wink Savillec7a98342010-08-13 16:11:42 -0700316 WifiStateTracker wst = new WifiStateTracker();
Irfan Sheriff0d255342010-07-28 09:35:20 -0700317 WifiService wifiService = new WifiService(context);
Robert Greenwalt5154ae762009-10-30 14:17:42 -0700318 ServiceManager.addService(Context.WIFI_SERVICE, wifiService);
Irfan Sheriff0d255342010-07-28 09:35:20 -0700319 wifiService.checkAndStartWifi();
Robert Greenwalt5154ae762009-10-30 14:17:42 -0700320 mNetTrackers[ConnectivityManager.TYPE_WIFI] = wst;
Wink Savillec7a98342010-08-13 16:11:42 -0700321 wst.startMonitoring(context, mHandler);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800322
Irfan Sheriffa2a1b912010-06-07 09:03:04 -0700323 //TODO: as part of WWS refactor, create only when needed
Irfan Sheriff0d255342010-07-28 09:35:20 -0700324 mWifiWatchdogService = new WifiWatchdogService(context);
Irfan Sheriffa2a1b912010-06-07 09:03:04 -0700325
Robert Greenwalt5154ae762009-10-30 14:17:42 -0700326 break;
327 case ConnectivityManager.TYPE_MOBILE:
Wink Savillec7a98342010-08-13 16:11:42 -0700328 mNetTrackers[netType] = new MobileDataStateTracker(netType,
329 mNetAttributes[netType].mName);
330 mNetTrackers[netType].startMonitoring(context, mHandler);
Robert Greenwaltc03fa502010-02-23 18:58:05 -0800331 if (noMobileData) {
Joe Onorato8a9b2202010-02-26 18:56:32 -0800332 if (DBG) Slog.d(TAG, "tearing down Mobile networks due to setting");
Robert Greenwaltc03fa502010-02-23 18:58:05 -0800333 mNetTrackers[netType].teardown();
334 }
Robert Greenwalt5154ae762009-10-30 14:17:42 -0700335 break;
336 default:
Joe Onorato8a9b2202010-02-26 18:56:32 -0800337 Slog.e(TAG, "Trying to create a DataStateTracker for an unknown radio type " +
Robert Greenwalt5154ae762009-10-30 14:17:42 -0700338 mNetAttributes[netType].mRadio);
339 continue;
340 }
341 }
Robert Greenwaltd0e18ff2010-01-26 11:40:34 -0800342
Robert Greenwaltdfadaea2010-03-11 15:03:08 -0800343 mTethering = new Tethering(mContext, mHandler.getLooper());
Robert Greenwaltc9d5fb72010-02-25 12:29:30 -0800344 mTetheringConfigValid = (((mNetTrackers[ConnectivityManager.TYPE_MOBILE_DUN] != null) ||
345 !mTethering.isDunRequired()) &&
346 (mTethering.getTetherableUsbRegexs().length != 0 ||
347 mTethering.getTetherableWifiRegexs().length != 0) &&
348 mTethering.getUpstreamIfaceRegexs().length != 0);
349
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800350 }
351
Robert Greenwalt5154ae762009-10-30 14:17:42 -0700352
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800353 /**
Robert Greenwalt86e9e552009-07-16 17:21:39 -0700354 * Sets the preferred network.
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800355 * @param preference the new preference
356 */
357 public synchronized void setNetworkPreference(int preference) {
358 enforceChangePermission();
Robert Greenwalt42acef32009-08-12 16:08:25 -0700359 if (ConnectivityManager.isNetworkTypeValid(preference) &&
Robert Greenwalt5154ae762009-10-30 14:17:42 -0700360 mNetAttributes[preference] != null &&
Robert Greenwalt42acef32009-08-12 16:08:25 -0700361 mNetAttributes[preference].isDefault()) {
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800362 if (mNetworkPreference != preference) {
363 persistNetworkPreference(preference);
364 mNetworkPreference = preference;
365 enforcePreference();
366 }
367 }
368 }
369
370 public int getNetworkPreference() {
371 enforceAccessPermission();
372 return mNetworkPreference;
373 }
374
375 private void persistNetworkPreference(int networkPreference) {
376 final ContentResolver cr = mContext.getContentResolver();
Robert Greenwalt86e9e552009-07-16 17:21:39 -0700377 Settings.Secure.putInt(cr, Settings.Secure.NETWORK_PREFERENCE,
378 networkPreference);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800379 }
Robert Greenwalt86e9e552009-07-16 17:21:39 -0700380
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800381 private int getPersistedNetworkPreference() {
382 final ContentResolver cr = mContext.getContentResolver();
383
384 final int networkPrefSetting = Settings.Secure
385 .getInt(cr, Settings.Secure.NETWORK_PREFERENCE, -1);
386 if (networkPrefSetting != -1) {
387 return networkPrefSetting;
388 }
389
390 return ConnectivityManager.DEFAULT_NETWORK_PREFERENCE;
391 }
Robert Greenwalt86e9e552009-07-16 17:21:39 -0700392
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800393 /**
Robert Greenwalt86e9e552009-07-16 17:21:39 -0700394 * Make the state of network connectivity conform to the preference settings
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800395 * In this method, we only tear down a non-preferred network. Establishing
396 * a connection to the preferred network is taken care of when we handle
397 * the disconnect event from the non-preferred network
398 * (see {@link #handleDisconnect(NetworkInfo)}).
399 */
400 private void enforcePreference() {
Robert Greenwalt42acef32009-08-12 16:08:25 -0700401 if (mNetTrackers[mNetworkPreference].getNetworkInfo().isConnected())
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800402 return;
403
Robert Greenwalt42acef32009-08-12 16:08:25 -0700404 if (!mNetTrackers[mNetworkPreference].isAvailable())
405 return;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800406
Robert Greenwalt42acef32009-08-12 16:08:25 -0700407 for (int t=0; t <= ConnectivityManager.MAX_RADIO_TYPE; t++) {
Robert Greenwalt5154ae762009-10-30 14:17:42 -0700408 if (t != mNetworkPreference && mNetTrackers[t] != null &&
Robert Greenwalt42acef32009-08-12 16:08:25 -0700409 mNetTrackers[t].getNetworkInfo().isConnected()) {
Robert Greenwaltec9fe462009-08-20 15:25:14 -0700410 if (DBG) {
Joe Onorato8a9b2202010-02-26 18:56:32 -0800411 Slog.d(TAG, "tearing down " +
Robert Greenwaltec9fe462009-08-20 15:25:14 -0700412 mNetTrackers[t].getNetworkInfo() +
413 " in enforcePreference");
414 }
Robert Greenwalt42acef32009-08-12 16:08:25 -0700415 teardown(mNetTrackers[t]);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800416 }
417 }
418 }
419
420 private boolean teardown(NetworkStateTracker netTracker) {
421 if (netTracker.teardown()) {
422 netTracker.setTeardownRequested(true);
423 return true;
424 } else {
425 return false;
426 }
427 }
428
429 /**
430 * Return NetworkInfo for the active (i.e., connected) network interface.
431 * It is assumed that at most one network is active at a time. If more
432 * than one is active, it is indeterminate which will be returned.
Robert Greenwalt86e9e552009-07-16 17:21:39 -0700433 * @return the info for the active network, or {@code null} if none is
434 * active
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800435 */
436 public NetworkInfo getActiveNetworkInfo() {
437 enforceAccessPermission();
Robert Greenwalt42acef32009-08-12 16:08:25 -0700438 for (int type=0; type <= ConnectivityManager.MAX_NETWORK_TYPE; type++) {
Robert Greenwalt5154ae762009-10-30 14:17:42 -0700439 if (mNetAttributes[type] == null || !mNetAttributes[type].isDefault()) {
Robert Greenwalt42acef32009-08-12 16:08:25 -0700440 continue;
441 }
442 NetworkStateTracker t = mNetTrackers[type];
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800443 NetworkInfo info = t.getNetworkInfo();
444 if (info.isConnected()) {
Joe Onorato8a9b2202010-02-26 18:56:32 -0800445 if (DBG && type != mActiveDefaultNetwork) Slog.e(TAG,
Robert Greenwalt42acef32009-08-12 16:08:25 -0700446 "connected default network is not " +
447 "mActiveDefaultNetwork!");
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800448 return info;
449 }
450 }
451 return null;
452 }
453
454 public NetworkInfo getNetworkInfo(int networkType) {
455 enforceAccessPermission();
456 if (ConnectivityManager.isNetworkTypeValid(networkType)) {
457 NetworkStateTracker t = mNetTrackers[networkType];
458 if (t != null)
459 return t.getNetworkInfo();
460 }
461 return null;
462 }
463
464 public NetworkInfo[] getAllNetworkInfo() {
465 enforceAccessPermission();
Robert Greenwalt5154ae762009-10-30 14:17:42 -0700466 NetworkInfo[] result = new NetworkInfo[mNetworksDefined];
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800467 int i = 0;
468 for (NetworkStateTracker t : mNetTrackers) {
Robert Greenwalt5154ae762009-10-30 14:17:42 -0700469 if(t != null) result[i++] = t.getNetworkInfo();
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800470 }
471 return result;
472 }
473
474 public boolean setRadios(boolean turnOn) {
475 boolean result = true;
476 enforceChangePermission();
477 for (NetworkStateTracker t : mNetTrackers) {
Robert Greenwalt5154ae762009-10-30 14:17:42 -0700478 if (t != null) result = t.setRadio(turnOn) && result;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800479 }
480 return result;
481 }
482
483 public boolean setRadio(int netType, boolean turnOn) {
484 enforceChangePermission();
485 if (!ConnectivityManager.isNetworkTypeValid(netType)) {
486 return false;
487 }
488 NetworkStateTracker tracker = mNetTrackers[netType];
489 return tracker != null && tracker.setRadio(turnOn);
490 }
491
Robert Greenwalt9c75d4a2009-09-27 17:27:04 -0700492 /**
493 * Used to notice when the calling process dies so we can self-expire
494 *
495 * Also used to know if the process has cleaned up after itself when
496 * our auto-expire timer goes off. The timer has a link to an object.
497 *
498 */
Robert Greenwalt42acef32009-08-12 16:08:25 -0700499 private class FeatureUser implements IBinder.DeathRecipient {
500 int mNetworkType;
501 String mFeature;
502 IBinder mBinder;
503 int mPid;
504 int mUid;
Robert Greenwaltb9285352009-12-21 18:24:07 -0800505 long mCreateTime;
Robert Greenwalt42acef32009-08-12 16:08:25 -0700506
507 FeatureUser(int type, String feature, IBinder binder) {
508 super();
509 mNetworkType = type;
510 mFeature = feature;
511 mBinder = binder;
512 mPid = getCallingPid();
513 mUid = getCallingUid();
Robert Greenwaltb9285352009-12-21 18:24:07 -0800514 mCreateTime = System.currentTimeMillis();
Robert Greenwalt9c75d4a2009-09-27 17:27:04 -0700515
Robert Greenwalt42acef32009-08-12 16:08:25 -0700516 try {
517 mBinder.linkToDeath(this, 0);
518 } catch (RemoteException e) {
519 binderDied();
520 }
521 }
522
523 void unlinkDeathRecipient() {
524 mBinder.unlinkToDeath(this, 0);
525 }
526
527 public void binderDied() {
Joe Onorato8a9b2202010-02-26 18:56:32 -0800528 Slog.d(TAG, "ConnectivityService FeatureUser binderDied(" +
Robert Greenwaltb9285352009-12-21 18:24:07 -0800529 mNetworkType + ", " + mFeature + ", " + mBinder + "), created " +
530 (System.currentTimeMillis() - mCreateTime) + " mSec ago");
Robert Greenwalt9c75d4a2009-09-27 17:27:04 -0700531 stopUsingNetworkFeature(this, false);
Robert Greenwalt42acef32009-08-12 16:08:25 -0700532 }
533
Robert Greenwalt9c75d4a2009-09-27 17:27:04 -0700534 public void expire() {
Joe Onorato8a9b2202010-02-26 18:56:32 -0800535 Slog.d(TAG, "ConnectivityService FeatureUser expire(" +
Robert Greenwaltb9285352009-12-21 18:24:07 -0800536 mNetworkType + ", " + mFeature + ", " + mBinder +"), created " +
537 (System.currentTimeMillis() - mCreateTime) + " mSec ago");
Robert Greenwalt9c75d4a2009-09-27 17:27:04 -0700538 stopUsingNetworkFeature(this, false);
539 }
Robert Greenwaltb9285352009-12-21 18:24:07 -0800540
541 public String toString() {
542 return "FeatureUser("+mNetworkType+","+mFeature+","+mPid+","+mUid+"), created " +
543 (System.currentTimeMillis() - mCreateTime) + " mSec ago";
544 }
Robert Greenwalt42acef32009-08-12 16:08:25 -0700545 }
546
Robert Greenwalt9c75d4a2009-09-27 17:27:04 -0700547 // javadoc from interface
Robert Greenwalt42acef32009-08-12 16:08:25 -0700548 public int startUsingNetworkFeature(int networkType, String feature,
549 IBinder binder) {
550 if (DBG) {
Joe Onorato8a9b2202010-02-26 18:56:32 -0800551 Slog.d(TAG, "startUsingNetworkFeature for net " + networkType +
Robert Greenwalt42acef32009-08-12 16:08:25 -0700552 ": " + feature);
553 }
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800554 enforceChangePermission();
Robert Greenwalt5154ae762009-10-30 14:17:42 -0700555 if (!ConnectivityManager.isNetworkTypeValid(networkType) ||
556 mNetAttributes[networkType] == null) {
Robert Greenwalt42acef32009-08-12 16:08:25 -0700557 return Phone.APN_REQUEST_FAILED;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800558 }
Robert Greenwalt42acef32009-08-12 16:08:25 -0700559
Robert Greenwalt9c75d4a2009-09-27 17:27:04 -0700560 FeatureUser f = new FeatureUser(networkType, feature, binder);
Robert Greenwalt42acef32009-08-12 16:08:25 -0700561
562 // TODO - move this into the MobileDataStateTracker
563 int usedNetworkType = networkType;
564 if(networkType == ConnectivityManager.TYPE_MOBILE) {
Robert Greenwaltc03fa502010-02-23 18:58:05 -0800565 if (!getMobileDataEnabled()) {
Joe Onorato8a9b2202010-02-26 18:56:32 -0800566 if (DBG) Slog.d(TAG, "requested special network with data disabled - rejected");
Robert Greenwaltc03fa502010-02-23 18:58:05 -0800567 return Phone.APN_TYPE_NOT_AVAILABLE;
568 }
Robert Greenwalt42acef32009-08-12 16:08:25 -0700569 if (TextUtils.equals(feature, Phone.FEATURE_ENABLE_MMS)) {
570 usedNetworkType = ConnectivityManager.TYPE_MOBILE_MMS;
571 } else if (TextUtils.equals(feature, Phone.FEATURE_ENABLE_SUPL)) {
572 usedNetworkType = ConnectivityManager.TYPE_MOBILE_SUPL;
573 } else if (TextUtils.equals(feature, Phone.FEATURE_ENABLE_DUN)) {
574 usedNetworkType = ConnectivityManager.TYPE_MOBILE_DUN;
575 } else if (TextUtils.equals(feature, Phone.FEATURE_ENABLE_HIPRI)) {
576 usedNetworkType = ConnectivityManager.TYPE_MOBILE_HIPRI;
577 }
578 }
579 NetworkStateTracker network = mNetTrackers[usedNetworkType];
580 if (network != null) {
581 if (usedNetworkType != networkType) {
582 Integer currentPid = new Integer(getCallingPid());
583
584 NetworkStateTracker radio = mNetTrackers[networkType];
585 NetworkInfo ni = network.getNetworkInfo();
586
587 if (ni.isAvailable() == false) {
Joe Onorato8a9b2202010-02-26 18:56:32 -0800588 if (DBG) Slog.d(TAG, "special network not available");
Robert Greenwalt42acef32009-08-12 16:08:25 -0700589 return Phone.APN_TYPE_NOT_AVAILABLE;
590 }
591
Robert Greenwalt9c75d4a2009-09-27 17:27:04 -0700592 synchronized(this) {
593 mFeatureUsers.add(f);
594 if (!mNetRequestersPids[usedNetworkType].contains(currentPid)) {
595 // this gets used for per-pid dns when connected
596 mNetRequestersPids[usedNetworkType].add(currentPid);
597 }
Robert Greenwalt42acef32009-08-12 16:08:25 -0700598 }
Robert Greenwalt9c75d4a2009-09-27 17:27:04 -0700599 mHandler.sendMessageDelayed(mHandler.obtainMessage(
600 NetworkStateTracker.EVENT_RESTORE_DEFAULT_NETWORK,
601 f), getRestoreDefaultNetworkDelay());
602
Robert Greenwalt42acef32009-08-12 16:08:25 -0700603
Robert Greenwalta64bf832009-08-19 20:19:33 -0700604 if ((ni.isConnectedOrConnecting() == true) &&
605 !network.isTeardownRequested()) {
Robert Greenwalt42acef32009-08-12 16:08:25 -0700606 if (ni.isConnected() == true) {
607 // add the pid-specific dns
Robert Greenwaltc76b8fa2010-07-23 15:46:26 -0700608 handleDnsConfigurationChange(networkType);
Joe Onorato8a9b2202010-02-26 18:56:32 -0800609 if (DBG) Slog.d(TAG, "special network already active");
Robert Greenwalt42acef32009-08-12 16:08:25 -0700610 return Phone.APN_ALREADY_ACTIVE;
611 }
Joe Onorato8a9b2202010-02-26 18:56:32 -0800612 if (DBG) Slog.d(TAG, "special network already connecting");
Robert Greenwalt42acef32009-08-12 16:08:25 -0700613 return Phone.APN_REQUEST_STARTED;
614 }
615
616 // check if the radio in play can make another contact
617 // assume if cannot for now
618
Joe Onorato8a9b2202010-02-26 18:56:32 -0800619 if (DBG) Slog.d(TAG, "reconnecting to special network");
Robert Greenwalt42acef32009-08-12 16:08:25 -0700620 network.reconnect();
621 return Phone.APN_REQUEST_STARTED;
622 } else {
Robert Greenwalt02648a42010-05-18 10:52:51 -0700623 return -1;
Robert Greenwalt42acef32009-08-12 16:08:25 -0700624 }
625 }
626 return Phone.APN_TYPE_NOT_AVAILABLE;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800627 }
628
Robert Greenwalt9c75d4a2009-09-27 17:27:04 -0700629 // javadoc from interface
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800630 public int stopUsingNetworkFeature(int networkType, String feature) {
Robert Greenwaltb8f16342009-10-06 17:52:40 -0700631 enforceChangePermission();
632
Robert Greenwalt9c75d4a2009-09-27 17:27:04 -0700633 int pid = getCallingPid();
634 int uid = getCallingUid();
635
636 FeatureUser u = null;
637 boolean found = false;
638
639 synchronized(this) {
640 for (int i = 0; i < mFeatureUsers.size() ; i++) {
641 u = (FeatureUser)mFeatureUsers.get(i);
642 if (uid == u.mUid && pid == u.mPid &&
643 networkType == u.mNetworkType &&
644 TextUtils.equals(feature, u.mFeature)) {
645 found = true;
646 break;
647 }
648 }
649 }
650 if (found && u != null) {
651 // stop regardless of how many other time this proc had called start
652 return stopUsingNetworkFeature(u, true);
653 } else {
654 // none found!
Joe Onorato8a9b2202010-02-26 18:56:32 -0800655 if (DBG) Slog.d(TAG, "ignoring stopUsingNetworkFeature - not a live request");
Robert Greenwalt9c75d4a2009-09-27 17:27:04 -0700656 return 1;
657 }
Robert Greenwalt42acef32009-08-12 16:08:25 -0700658 }
659
Robert Greenwalt9c75d4a2009-09-27 17:27:04 -0700660 private int stopUsingNetworkFeature(FeatureUser u, boolean ignoreDups) {
661 int networkType = u.mNetworkType;
662 String feature = u.mFeature;
663 int pid = u.mPid;
664 int uid = u.mUid;
665
666 NetworkStateTracker tracker = null;
667 boolean callTeardown = false; // used to carry our decision outside of sync block
668
Robert Greenwalt42acef32009-08-12 16:08:25 -0700669 if (DBG) {
Joe Onorato8a9b2202010-02-26 18:56:32 -0800670 Slog.d(TAG, "stopUsingNetworkFeature for net " + networkType +
Robert Greenwalt42acef32009-08-12 16:08:25 -0700671 ": " + feature);
672 }
Robert Greenwaltb8f16342009-10-06 17:52:40 -0700673
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800674 if (!ConnectivityManager.isNetworkTypeValid(networkType)) {
675 return -1;
676 }
Robert Greenwalt42acef32009-08-12 16:08:25 -0700677
Robert Greenwalt9c75d4a2009-09-27 17:27:04 -0700678 // need to link the mFeatureUsers list with the mNetRequestersPids state in this
679 // sync block
680 synchronized(this) {
681 // check if this process still has an outstanding start request
682 if (!mFeatureUsers.contains(u)) {
Robert Greenwalt78a640a2010-03-10 16:10:43 -0800683 if (DBG) Slog.d(TAG, "ignoring - this process has no outstanding requests");
Robert Greenwalt42acef32009-08-12 16:08:25 -0700684 return 1;
685 }
Robert Greenwalt9c75d4a2009-09-27 17:27:04 -0700686 u.unlinkDeathRecipient();
687 mFeatureUsers.remove(mFeatureUsers.indexOf(u));
688 // If we care about duplicate requests, check for that here.
689 //
690 // This is done to support the extension of a request - the app
691 // can request we start the network feature again and renew the
692 // auto-shutoff delay. Normal "stop" calls from the app though
693 // do not pay attention to duplicate requests - in effect the
694 // API does not refcount and a single stop will counter multiple starts.
695 if (ignoreDups == false) {
696 for (int i = 0; i < mFeatureUsers.size() ; i++) {
697 FeatureUser x = (FeatureUser)mFeatureUsers.get(i);
698 if (x.mUid == u.mUid && x.mPid == u.mPid &&
699 x.mNetworkType == u.mNetworkType &&
700 TextUtils.equals(x.mFeature, u.mFeature)) {
Joe Onorato8a9b2202010-02-26 18:56:32 -0800701 if (DBG) Slog.d(TAG, "ignoring stopUsingNetworkFeature as dup is found");
Robert Greenwalt9c75d4a2009-09-27 17:27:04 -0700702 return 1;
703 }
704 }
Robert Greenwalt42acef32009-08-12 16:08:25 -0700705 }
Robert Greenwalt9c75d4a2009-09-27 17:27:04 -0700706
707 // TODO - move to MobileDataStateTracker
708 int usedNetworkType = networkType;
709 if (networkType == ConnectivityManager.TYPE_MOBILE) {
710 if (TextUtils.equals(feature, Phone.FEATURE_ENABLE_MMS)) {
711 usedNetworkType = ConnectivityManager.TYPE_MOBILE_MMS;
712 } else if (TextUtils.equals(feature, Phone.FEATURE_ENABLE_SUPL)) {
713 usedNetworkType = ConnectivityManager.TYPE_MOBILE_SUPL;
714 } else if (TextUtils.equals(feature, Phone.FEATURE_ENABLE_DUN)) {
715 usedNetworkType = ConnectivityManager.TYPE_MOBILE_DUN;
716 } else if (TextUtils.equals(feature, Phone.FEATURE_ENABLE_HIPRI)) {
717 usedNetworkType = ConnectivityManager.TYPE_MOBILE_HIPRI;
718 }
719 }
720 tracker = mNetTrackers[usedNetworkType];
Robert Greenwalt5154ae762009-10-30 14:17:42 -0700721 if (tracker == null) {
Robert Greenwalt78a640a2010-03-10 16:10:43 -0800722 if (DBG) Slog.d(TAG, "ignoring - no known tracker for net type " + usedNetworkType);
Robert Greenwalt5154ae762009-10-30 14:17:42 -0700723 return -1;
724 }
725 if (usedNetworkType != networkType) {
Robert Greenwalt9c75d4a2009-09-27 17:27:04 -0700726 Integer currentPid = new Integer(pid);
Robert Greenwalt9c75d4a2009-09-27 17:27:04 -0700727 mNetRequestersPids[usedNetworkType].remove(currentPid);
Robert Greenwalt421c72b2009-12-17 14:54:59 -0800728 reassessPidDns(pid, true);
Robert Greenwalt9c75d4a2009-09-27 17:27:04 -0700729 if (mNetRequestersPids[usedNetworkType].size() != 0) {
Joe Onorato8a9b2202010-02-26 18:56:32 -0800730 if (DBG) Slog.d(TAG, "not tearing down special network - " +
Robert Greenwalt9c75d4a2009-09-27 17:27:04 -0700731 "others still using it");
732 return 1;
733 }
734 callTeardown = true;
735 }
736 }
Robert Greenwalt78a640a2010-03-10 16:10:43 -0800737 if (DBG) Slog.d(TAG, "Doing network teardown");
Robert Greenwalt9c75d4a2009-09-27 17:27:04 -0700738 if (callTeardown) {
739 tracker.teardown();
Robert Greenwalt42acef32009-08-12 16:08:25 -0700740 return 1;
741 } else {
Robert Greenwalt02648a42010-05-18 10:52:51 -0700742 return -1;
Robert Greenwalt42acef32009-08-12 16:08:25 -0700743 }
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800744 }
745
746 /**
747 * Ensure that a network route exists to deliver traffic to the specified
748 * host via the specified network interface.
Robert Greenwalt86e9e552009-07-16 17:21:39 -0700749 * @param networkType the type of the network over which traffic to the
750 * specified host is to be routed
751 * @param hostAddress the IP address of the host to which the route is
752 * desired
Robert Greenwalt47f69fe2010-06-15 15:43:39 -0700753 * todo - deprecate (only v4!)
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800754 * @return {@code true} on success, {@code false} on failure
755 */
756 public boolean requestRouteToHost(int networkType, int hostAddress) {
757 enforceChangePermission();
758 if (!ConnectivityManager.isNetworkTypeValid(networkType)) {
759 return false;
760 }
761 NetworkStateTracker tracker = mNetTrackers[networkType];
Robert Greenwalt8206ff32009-09-10 15:06:20 -0700762
Robert Greenwalt5154ae762009-10-30 14:17:42 -0700763 if (tracker == null || !tracker.getNetworkInfo().isConnected() ||
764 tracker.isTeardownRequested()) {
Robert Greenwalt8206ff32009-09-10 15:06:20 -0700765 if (DBG) {
Joe Onorato8a9b2202010-02-26 18:56:32 -0800766 Slog.d(TAG, "requestRouteToHost on down network (" + networkType + ") - dropped");
Robert Greenwalt8206ff32009-09-10 15:06:20 -0700767 }
768 return false;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800769 }
Robert Greenwalt47f69fe2010-06-15 15:43:39 -0700770 try {
771 InetAddress addr = InetAddress.getByAddress(NetworkUtils.v4IntToArray(hostAddress));
772 return addHostRoute(tracker, addr);
773 } catch (UnknownHostException e) {}
774 return false;
Irfan Sheriffd649c122010-06-09 15:39:36 -0700775 }
776
777 /**
778 * Ensure that a network route exists to deliver traffic to the specified
779 * host via the mobile data network.
780 * @param hostAddress the IP address of the host to which the route is desired,
781 * in network byte order.
Robert Greenwalt47f69fe2010-06-15 15:43:39 -0700782 * TODO - deprecate
Irfan Sheriffd649c122010-06-09 15:39:36 -0700783 * @return {@code true} on success, {@code false} on failure
784 */
Robert Greenwalt47f69fe2010-06-15 15:43:39 -0700785 private boolean addHostRoute(NetworkStateTracker nt, InetAddress hostAddress) {
Irfan Sheriffd649c122010-06-09 15:39:36 -0700786 if (nt.getNetworkInfo().getType() == ConnectivityManager.TYPE_WIFI) {
787 return false;
788 }
789
Robert Greenwalt47f69fe2010-06-15 15:43:39 -0700790 NetworkProperties p = nt.getNetworkProperties();
791 if (p == null) return false;
792 String interfaceName = p.getInterfaceName();
Irfan Sheriffd649c122010-06-09 15:39:36 -0700793
794 if (DBG) {
Robert Greenwalt47f69fe2010-06-15 15:43:39 -0700795 Slog.d(TAG, "Requested host route to " + hostAddress + "(" + interfaceName + ")");
Irfan Sheriffd649c122010-06-09 15:39:36 -0700796 }
Robert Greenwalt47f69fe2010-06-15 15:43:39 -0700797 if (interfaceName != null) {
Irfan Sheriffd649c122010-06-09 15:39:36 -0700798 return NetworkUtils.addHostRoute(interfaceName, hostAddress) == 0;
799 } else {
Robert Greenwalt47f69fe2010-06-15 15:43:39 -0700800 if (DBG) Slog.e(TAG, "addHostRoute failed due to null interface name");
Irfan Sheriffd649c122010-06-09 15:39:36 -0700801 return false;
802 }
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800803 }
804
805 /**
806 * @see ConnectivityManager#getBackgroundDataSetting()
807 */
808 public boolean getBackgroundDataSetting() {
809 return Settings.Secure.getInt(mContext.getContentResolver(),
810 Settings.Secure.BACKGROUND_DATA, 1) == 1;
811 }
Robert Greenwalt86e9e552009-07-16 17:21:39 -0700812
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800813 /**
814 * @see ConnectivityManager#setBackgroundDataSetting(boolean)
815 */
816 public void setBackgroundDataSetting(boolean allowBackgroundDataUsage) {
817 mContext.enforceCallingOrSelfPermission(
818 android.Manifest.permission.CHANGE_BACKGROUND_DATA_SETTING,
819 "ConnectivityService");
Robert Greenwalt86e9e552009-07-16 17:21:39 -0700820
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800821 if (getBackgroundDataSetting() == allowBackgroundDataUsage) return;
822
823 Settings.Secure.putInt(mContext.getContentResolver(),
Robert Greenwalt86e9e552009-07-16 17:21:39 -0700824 Settings.Secure.BACKGROUND_DATA,
825 allowBackgroundDataUsage ? 1 : 0);
826
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800827 Intent broadcast = new Intent(
828 ConnectivityManager.ACTION_BACKGROUND_DATA_SETTING_CHANGED);
829 mContext.sendBroadcast(broadcast);
Robert Greenwalt86e9e552009-07-16 17:21:39 -0700830 }
831
Robert Greenwaltc03fa502010-02-23 18:58:05 -0800832 /**
833 * @see ConnectivityManager#getMobileDataEnabled()
834 */
835 public boolean getMobileDataEnabled() {
836 enforceAccessPermission();
837 boolean retVal = Settings.Secure.getInt(mContext.getContentResolver(),
838 Settings.Secure.MOBILE_DATA, 1) == 1;
Joe Onorato8a9b2202010-02-26 18:56:32 -0800839 if (DBG) Slog.d(TAG, "getMobileDataEnabled returning " + retVal);
Robert Greenwaltc03fa502010-02-23 18:58:05 -0800840 return retVal;
841 }
842
843 /**
844 * @see ConnectivityManager#setMobileDataEnabled(boolean)
845 */
846 public synchronized void setMobileDataEnabled(boolean enabled) {
847 enforceChangePermission();
Joe Onorato8a9b2202010-02-26 18:56:32 -0800848 if (DBG) Slog.d(TAG, "setMobileDataEnabled(" + enabled + ")");
Robert Greenwaltc03fa502010-02-23 18:58:05 -0800849
850 if (getMobileDataEnabled() == enabled) return;
851
852 Settings.Secure.putInt(mContext.getContentResolver(),
853 Settings.Secure.MOBILE_DATA, enabled ? 1 : 0);
854
855 if (enabled) {
856 if (mNetTrackers[ConnectivityManager.TYPE_MOBILE] != null) {
Joe Onorato8a9b2202010-02-26 18:56:32 -0800857 if (DBG) Slog.d(TAG, "starting up " + mNetTrackers[ConnectivityManager.TYPE_MOBILE]);
Robert Greenwaltc03fa502010-02-23 18:58:05 -0800858 mNetTrackers[ConnectivityManager.TYPE_MOBILE].reconnect();
859 }
860 } else {
861 for (NetworkStateTracker nt : mNetTrackers) {
862 if (nt == null) continue;
863 int netType = nt.getNetworkInfo().getType();
864 if (mNetAttributes[netType].mRadio == ConnectivityManager.TYPE_MOBILE) {
Joe Onorato8a9b2202010-02-26 18:56:32 -0800865 if (DBG) Slog.d(TAG, "tearing down " + nt);
Robert Greenwaltc03fa502010-02-23 18:58:05 -0800866 nt.teardown();
867 }
868 }
869 }
870 }
871
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800872 private int getNumConnectedNetworks() {
873 int numConnectedNets = 0;
874
875 for (NetworkStateTracker nt : mNetTrackers) {
Robert Greenwalt5154ae762009-10-30 14:17:42 -0700876 if (nt != null && nt.getNetworkInfo().isConnected() &&
Robert Greenwalt86e9e552009-07-16 17:21:39 -0700877 !nt.isTeardownRequested()) {
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800878 ++numConnectedNets;
879 }
880 }
881 return numConnectedNets;
882 }
883
884 private void enforceAccessPermission() {
Robert Greenwalt86e9e552009-07-16 17:21:39 -0700885 mContext.enforceCallingOrSelfPermission(
886 android.Manifest.permission.ACCESS_NETWORK_STATE,
887 "ConnectivityService");
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800888 }
889
890 private void enforceChangePermission() {
Robert Greenwalt86e9e552009-07-16 17:21:39 -0700891 mContext.enforceCallingOrSelfPermission(
892 android.Manifest.permission.CHANGE_NETWORK_STATE,
893 "ConnectivityService");
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800894 }
895
Robert Greenwaltd0e18ff2010-01-26 11:40:34 -0800896 // TODO Make this a special check when it goes public
897 private void enforceTetherChangePermission() {
898 mContext.enforceCallingOrSelfPermission(
899 android.Manifest.permission.CHANGE_NETWORK_STATE,
900 "ConnectivityService");
901 }
902
Robert Greenwalt2a091d72010-02-11 18:18:40 -0800903 private void enforceTetherAccessPermission() {
904 mContext.enforceCallingOrSelfPermission(
905 android.Manifest.permission.ACCESS_NETWORK_STATE,
906 "ConnectivityService");
907 }
908
Robert Greenwalt14f2ef42010-06-15 12:19:37 -0700909 private void enforceConnectivityInternalPermission() {
910 mContext.enforceCallingOrSelfPermission(
911 android.Manifest.permission.CONNECTIVITY_INTERNAL,
912 "ConnectivityService");
913 }
914
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800915 /**
Robert Greenwalt86e9e552009-07-16 17:21:39 -0700916 * Handle a {@code DISCONNECTED} event. If this pertains to the non-active
917 * network, we ignore it. If it is for the active network, we send out a
918 * broadcast. But first, we check whether it might be possible to connect
919 * to a different network.
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800920 * @param info the {@code NetworkInfo} for the network
921 */
922 private void handleDisconnect(NetworkInfo info) {
923
Robert Greenwalt42acef32009-08-12 16:08:25 -0700924 int prevNetType = info.getType();
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800925
Robert Greenwalt42acef32009-08-12 16:08:25 -0700926 mNetTrackers[prevNetType].setTeardownRequested(false);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800927 /*
928 * If the disconnected network is not the active one, then don't report
929 * this as a loss of connectivity. What probably happened is that we're
930 * getting the disconnect for a network that we explicitly disabled
931 * in accordance with network preference policies.
932 */
Robert Greenwalt42acef32009-08-12 16:08:25 -0700933 if (!mNetAttributes[prevNetType].isDefault()) {
934 List pids = mNetRequestersPids[prevNetType];
935 for (int i = 0; i<pids.size(); i++) {
936 Integer pid = (Integer)pids.get(i);
937 // will remove them because the net's no longer connected
938 // need to do this now as only now do we know the pids and
939 // can properly null things that are no longer referenced.
940 reassessPidDns(pid.intValue(), false);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800941 }
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800942 }
943
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800944 Intent intent = new Intent(ConnectivityManager.CONNECTIVITY_ACTION);
Dianne Hackborn1c633fc2009-12-08 19:45:14 -0800945 intent.addFlags(Intent.FLAG_RECEIVER_REPLACE_PENDING);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800946 intent.putExtra(ConnectivityManager.EXTRA_NETWORK_INFO, info);
947 if (info.isFailover()) {
948 intent.putExtra(ConnectivityManager.EXTRA_IS_FAILOVER, true);
949 info.setFailover(false);
950 }
951 if (info.getReason() != null) {
952 intent.putExtra(ConnectivityManager.EXTRA_REASON, info.getReason());
953 }
954 if (info.getExtraInfo() != null) {
Robert Greenwalt86e9e552009-07-16 17:21:39 -0700955 intent.putExtra(ConnectivityManager.EXTRA_EXTRA_INFO,
956 info.getExtraInfo());
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800957 }
Robert Greenwalt42acef32009-08-12 16:08:25 -0700958
Robert Greenwaltcc4b4012010-01-25 17:54:29 -0800959 NetworkStateTracker newNet = null;
960 if (mNetAttributes[prevNetType].isDefault()) {
961 newNet = tryFailover(prevNetType);
962 if (newNet != null) {
963 NetworkInfo switchTo = newNet.getNetworkInfo();
964 intent.putExtra(ConnectivityManager.EXTRA_OTHER_NETWORK_INFO, switchTo);
965 } else {
966 intent.putExtra(ConnectivityManager.EXTRA_NO_CONNECTIVITY, true);
967 }
Robert Greenwaltda03c4e2010-01-20 19:29:41 -0800968 }
969 // do this before we broadcast the change
Robert Greenwaltc76b8fa2010-07-23 15:46:26 -0700970 handleConnectivityChange(prevNetType);
Robert Greenwaltda03c4e2010-01-20 19:29:41 -0800971
972 sendStickyBroadcast(intent);
973 /*
974 * If the failover network is already connected, then immediately send
975 * out a followup broadcast indicating successful failover
976 */
977 if (newNet != null && newNet.getNetworkInfo().isConnected()) {
978 sendConnectedBroadcast(newNet.getNetworkInfo());
979 }
980 }
981
Robert Greenwaltcc4b4012010-01-25 17:54:29 -0800982 // returns null if no failover available
Robert Greenwaltda03c4e2010-01-20 19:29:41 -0800983 private NetworkStateTracker tryFailover(int prevNetType) {
Robert Greenwalt42acef32009-08-12 16:08:25 -0700984 /*
985 * If this is a default network, check if other defaults are available
986 * or active
987 */
988 NetworkStateTracker newNet = null;
989 if (mNetAttributes[prevNetType].isDefault()) {
Robert Greenwalt42acef32009-08-12 16:08:25 -0700990 if (mActiveDefaultNetwork == prevNetType) {
991 mActiveDefaultNetwork = -1;
992 }
993
994 int newType = -1;
995 int newPriority = -1;
Robert Greenwalt35429592010-02-25 12:04:29 -0800996 boolean noMobileData = !getMobileDataEnabled();
Robert Greenwaltda03c4e2010-01-20 19:29:41 -0800997 for (int checkType=0; checkType <= ConnectivityManager.MAX_NETWORK_TYPE; checkType++) {
Robert Greenwalt5154ae762009-10-30 14:17:42 -0700998 if (checkType == prevNetType) continue;
999 if (mNetAttributes[checkType] == null) continue;
Robert Greenwalt35429592010-02-25 12:04:29 -08001000 if (mNetAttributes[checkType].mRadio == ConnectivityManager.TYPE_MOBILE &&
1001 noMobileData) {
1002 if (DBG) {
Joe Onorato8a9b2202010-02-26 18:56:32 -08001003 Slog.d(TAG, "not failing over to mobile type " + checkType +
Robert Greenwalt35429592010-02-25 12:04:29 -08001004 " because Mobile Data Disabled");
1005 }
1006 continue;
1007 }
Robert Greenwalt42acef32009-08-12 16:08:25 -07001008 if (mNetAttributes[checkType].isDefault()) {
1009 /* TODO - if we have multiple nets we could use
1010 * we may want to put more thought into which we choose
1011 */
1012 if (checkType == mNetworkPreference) {
1013 newType = checkType;
1014 break;
1015 }
Robert Greenwalt5154ae762009-10-30 14:17:42 -07001016 if (mNetAttributes[checkType].mPriority > newPriority) {
Robert Greenwalt42acef32009-08-12 16:08:25 -07001017 newType = checkType;
Robert Greenwalt5154ae762009-10-30 14:17:42 -07001018 newPriority = mNetAttributes[newType].mPriority;
Robert Greenwalt42acef32009-08-12 16:08:25 -07001019 }
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001020 }
1021 }
Robert Greenwalt42acef32009-08-12 16:08:25 -07001022
1023 if (newType != -1) {
1024 newNet = mNetTrackers[newType];
1025 /**
1026 * See if the other network is available to fail over to.
1027 * If is not available, we enable it anyway, so that it
1028 * will be able to connect when it does become available,
1029 * but we report a total loss of connectivity rather than
1030 * report that we are attempting to fail over.
1031 */
1032 if (newNet.isAvailable()) {
1033 NetworkInfo switchTo = newNet.getNetworkInfo();
1034 switchTo.setFailover(true);
Robert Greenwalta64bf832009-08-19 20:19:33 -07001035 if (!switchTo.isConnectedOrConnecting() ||
1036 newNet.isTeardownRequested()) {
Robert Greenwalt42acef32009-08-12 16:08:25 -07001037 newNet.reconnect();
1038 }
1039 if (DBG) {
1040 if (switchTo.isConnected()) {
Joe Onorato8a9b2202010-02-26 18:56:32 -08001041 Slog.v(TAG, "Switching to already connected " +
Robert Greenwalt42acef32009-08-12 16:08:25 -07001042 switchTo.getTypeName());
1043 } else {
Joe Onorato8a9b2202010-02-26 18:56:32 -08001044 Slog.v(TAG, "Attempting to switch to " +
Robert Greenwalt42acef32009-08-12 16:08:25 -07001045 switchTo.getTypeName());
1046 }
1047 }
Robert Greenwalt42acef32009-08-12 16:08:25 -07001048 } else {
1049 newNet.reconnect();
Robert Greenwaltf0fa39e2010-03-09 14:55:08 -08001050 newNet = null; // not officially avail.. try anyway, but
1051 // report no failover
Robert Greenwalt42acef32009-08-12 16:08:25 -07001052 }
Robert Greenwalt42acef32009-08-12 16:08:25 -07001053 }
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001054 }
Robert Greenwalt42acef32009-08-12 16:08:25 -07001055
Robert Greenwaltda03c4e2010-01-20 19:29:41 -08001056 return newNet;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001057 }
1058
1059 private void sendConnectedBroadcast(NetworkInfo info) {
1060 Intent intent = new Intent(ConnectivityManager.CONNECTIVITY_ACTION);
Dianne Hackborn1c633fc2009-12-08 19:45:14 -08001061 intent.addFlags(Intent.FLAG_RECEIVER_REPLACE_PENDING);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001062 intent.putExtra(ConnectivityManager.EXTRA_NETWORK_INFO, info);
1063 if (info.isFailover()) {
1064 intent.putExtra(ConnectivityManager.EXTRA_IS_FAILOVER, true);
1065 info.setFailover(false);
1066 }
1067 if (info.getReason() != null) {
1068 intent.putExtra(ConnectivityManager.EXTRA_REASON, info.getReason());
1069 }
1070 if (info.getExtraInfo() != null) {
Robert Greenwalt86e9e552009-07-16 17:21:39 -07001071 intent.putExtra(ConnectivityManager.EXTRA_EXTRA_INFO,
1072 info.getExtraInfo());
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001073 }
Mike Lockwood0f79b542009-08-14 14:18:49 -04001074 sendStickyBroadcast(intent);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001075 }
1076
1077 /**
1078 * Called when an attempt to fail over to another network has failed.
1079 * @param info the {@link NetworkInfo} for the failed network
1080 */
1081 private void handleConnectionFailure(NetworkInfo info) {
1082 mNetTrackers[info.getType()].setTeardownRequested(false);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001083
Robert Greenwalt42acef32009-08-12 16:08:25 -07001084 String reason = info.getReason();
1085 String extraInfo = info.getExtraInfo();
Robert Greenwalt86e9e552009-07-16 17:21:39 -07001086
Robert Greenwalt42acef32009-08-12 16:08:25 -07001087 if (DBG) {
1088 String reasonText;
1089 if (reason == null) {
1090 reasonText = ".";
1091 } else {
1092 reasonText = " (" + reason + ").";
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001093 }
Joe Onorato8a9b2202010-02-26 18:56:32 -08001094 Slog.v(TAG, "Attempt to connect to " + info.getTypeName() +
Robert Greenwalt42acef32009-08-12 16:08:25 -07001095 " failed" + reasonText);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001096 }
Robert Greenwalt42acef32009-08-12 16:08:25 -07001097
1098 Intent intent = new Intent(ConnectivityManager.CONNECTIVITY_ACTION);
Dianne Hackborn1c633fc2009-12-08 19:45:14 -08001099 intent.addFlags(Intent.FLAG_RECEIVER_REPLACE_PENDING);
Robert Greenwalt42acef32009-08-12 16:08:25 -07001100 intent.putExtra(ConnectivityManager.EXTRA_NETWORK_INFO, info);
1101 if (getActiveNetworkInfo() == null) {
1102 intent.putExtra(ConnectivityManager.EXTRA_NO_CONNECTIVITY, true);
1103 }
1104 if (reason != null) {
1105 intent.putExtra(ConnectivityManager.EXTRA_REASON, reason);
1106 }
1107 if (extraInfo != null) {
1108 intent.putExtra(ConnectivityManager.EXTRA_EXTRA_INFO, extraInfo);
1109 }
1110 if (info.isFailover()) {
1111 intent.putExtra(ConnectivityManager.EXTRA_IS_FAILOVER, true);
1112 info.setFailover(false);
1113 }
Robert Greenwaltda03c4e2010-01-20 19:29:41 -08001114
Robert Greenwaltcc4b4012010-01-25 17:54:29 -08001115 NetworkStateTracker newNet = null;
1116 if (mNetAttributes[info.getType()].isDefault()) {
1117 newNet = tryFailover(info.getType());
1118 if (newNet != null) {
1119 NetworkInfo switchTo = newNet.getNetworkInfo();
1120 intent.putExtra(ConnectivityManager.EXTRA_OTHER_NETWORK_INFO, switchTo);
1121 } else {
1122 intent.putExtra(ConnectivityManager.EXTRA_NO_CONNECTIVITY, true);
1123 }
Robert Greenwaltda03c4e2010-01-20 19:29:41 -08001124 }
Robert Greenwaltcc4b4012010-01-25 17:54:29 -08001125
Mike Lockwood0f79b542009-08-14 14:18:49 -04001126 sendStickyBroadcast(intent);
Robert Greenwaltda03c4e2010-01-20 19:29:41 -08001127 /*
1128 * If the failover network is already connected, then immediately send
1129 * out a followup broadcast indicating successful failover
1130 */
1131 if (newNet != null && newNet.getNetworkInfo().isConnected()) {
1132 sendConnectedBroadcast(newNet.getNetworkInfo());
1133 }
Mike Lockwood0f79b542009-08-14 14:18:49 -04001134 }
1135
1136 private void sendStickyBroadcast(Intent intent) {
1137 synchronized(this) {
Dianne Hackborn1c633fc2009-12-08 19:45:14 -08001138 if (!mSystemReady) {
1139 mInitialBroadcast = new Intent(intent);
Mike Lockwood0f79b542009-08-14 14:18:49 -04001140 }
Dianne Hackborn1c633fc2009-12-08 19:45:14 -08001141 intent.addFlags(Intent.FLAG_RECEIVER_REGISTERED_ONLY_BEFORE_BOOT);
1142 mContext.sendStickyBroadcast(intent);
Mike Lockwood0f79b542009-08-14 14:18:49 -04001143 }
1144 }
1145
1146 void systemReady() {
1147 synchronized(this) {
1148 mSystemReady = true;
Dianne Hackborn1c633fc2009-12-08 19:45:14 -08001149 if (mInitialBroadcast != null) {
1150 mContext.sendStickyBroadcast(mInitialBroadcast);
1151 mInitialBroadcast = null;
Mike Lockwood0f79b542009-08-14 14:18:49 -04001152 }
1153 }
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001154 }
1155
1156 private void handleConnect(NetworkInfo info) {
Robert Greenwalt42acef32009-08-12 16:08:25 -07001157 int type = info.getType();
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001158
1159 // snapshot isFailover, because sendConnectedBroadcast() resets it
1160 boolean isFailover = info.isFailover();
Robert Greenwalt42acef32009-08-12 16:08:25 -07001161 NetworkStateTracker thisNet = mNetTrackers[type];
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001162
Robert Greenwalt42acef32009-08-12 16:08:25 -07001163 // if this is a default net and other default is running
1164 // kill the one not preferred
1165 if (mNetAttributes[type].isDefault()) {
Robert Greenwalt42acef32009-08-12 16:08:25 -07001166 if (mActiveDefaultNetwork != -1 && mActiveDefaultNetwork != type) {
1167 if ((type != mNetworkPreference &&
1168 mNetAttributes[mActiveDefaultNetwork].mPriority >
1169 mNetAttributes[type].mPriority) ||
1170 mNetworkPreference == mActiveDefaultNetwork) {
1171 // don't accept this one
Joe Onorato8a9b2202010-02-26 18:56:32 -08001172 if (DBG) Slog.v(TAG, "Not broadcasting CONNECT_ACTION " +
Robert Greenwalt42acef32009-08-12 16:08:25 -07001173 "to torn down network " + info.getTypeName());
1174 teardown(thisNet);
1175 return;
1176 } else {
1177 // tear down the other
1178 NetworkStateTracker otherNet =
1179 mNetTrackers[mActiveDefaultNetwork];
Joe Onorato8a9b2202010-02-26 18:56:32 -08001180 if (DBG) Slog.v(TAG, "Policy requires " +
Robert Greenwalt42acef32009-08-12 16:08:25 -07001181 otherNet.getNetworkInfo().getTypeName() +
1182 " teardown");
1183 if (!teardown(otherNet)) {
Joe Onorato8a9b2202010-02-26 18:56:32 -08001184 Slog.e(TAG, "Network declined teardown request");
Robert Greenwalt42acef32009-08-12 16:08:25 -07001185 return;
1186 }
Robert Greenwalt14f2ef42010-06-15 12:19:37 -07001187 }
1188 }
1189 synchronized (ConnectivityService.this) {
1190 // have a new default network, release the transition wakelock in a second
1191 // if it's held. The second pause is to allow apps to reconnect over the
1192 // new network
1193 if (mNetTransitionWakeLock.isHeld()) {
1194 mHandler.sendMessageDelayed(mHandler.obtainMessage(
1195 NetworkStateTracker.EVENT_CLEAR_NET_TRANSITION_WAKELOCK,
1196 mNetTransitionWakeLockSerialNumber, 0),
1197 1000);
Robert Greenwalt42acef32009-08-12 16:08:25 -07001198 }
1199 }
1200 mActiveDefaultNetwork = type;
1201 }
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001202 thisNet.setTeardownRequested(false);
Irfan Sheriffd649c122010-06-09 15:39:36 -07001203 updateNetworkSettings(thisNet);
Robert Greenwaltc76b8fa2010-07-23 15:46:26 -07001204 handleConnectivityChange(type);
Robert Greenwalt42acef32009-08-12 16:08:25 -07001205 sendConnectedBroadcast(info);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001206 }
1207
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001208 /**
Robert Greenwaltc76b8fa2010-07-23 15:46:26 -07001209 * After a change in the connectivity state of a network. We're mainly
1210 * concerned with making sure that the list of DNS servers is set up
1211 * according to which networks are connected, and ensuring that the
1212 * right routing table entries exist.
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001213 */
Robert Greenwaltc76b8fa2010-07-23 15:46:26 -07001214 private void handleConnectivityChange(int netType) {
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001215 /*
Robert Greenwalt42acef32009-08-12 16:08:25 -07001216 * If a non-default network is enabled, add the host routes that
Robert Greenwaltc76b8fa2010-07-23 15:46:26 -07001217 * will allow it's DNS servers to be accessed.
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001218 */
Robert Greenwaltc76b8fa2010-07-23 15:46:26 -07001219 handleDnsConfigurationChange(netType);
Robert Greenwalt42acef32009-08-12 16:08:25 -07001220
Robert Greenwaltc76b8fa2010-07-23 15:46:26 -07001221 if (mNetTrackers[netType].getNetworkInfo().isConnected()) {
1222 if (mNetAttributes[netType].isDefault()) {
1223 addDefaultRoute(mNetTrackers[netType]);
Robert Greenwalt42acef32009-08-12 16:08:25 -07001224 } else {
Robert Greenwaltc76b8fa2010-07-23 15:46:26 -07001225 addPrivateDnsRoutes(mNetTrackers[netType]);
1226 }
1227 } else {
1228 if (mNetAttributes[netType].isDefault()) {
1229 removeDefaultRoute(mNetTrackers[netType]);
1230 } else {
1231 removePrivateDnsRoutes(mNetTrackers[netType]);
Robert Greenwalt42acef32009-08-12 16:08:25 -07001232 }
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001233 }
1234 }
1235
Irfan Sheriffd649c122010-06-09 15:39:36 -07001236 private void addPrivateDnsRoutes(NetworkStateTracker nt) {
Irfan Sheriffd649c122010-06-09 15:39:36 -07001237 boolean privateDnsRouteSet = nt.isPrivateDnsRouteSet();
Robert Greenwalt47f69fe2010-06-15 15:43:39 -07001238 NetworkProperties p = nt.getNetworkProperties();
1239 if (p == null) return;
1240 String interfaceName = p.getInterfaceName();
Irfan Sheriffd649c122010-06-09 15:39:36 -07001241
1242 if (DBG) {
1243 Slog.d(TAG, "addPrivateDnsRoutes for " + nt +
1244 "(" + interfaceName + ") - mPrivateDnsRouteSet = " + privateDnsRouteSet);
1245 }
Irfan Sheriffd649c122010-06-09 15:39:36 -07001246 if (interfaceName != null && !privateDnsRouteSet) {
Robert Greenwalt47f69fe2010-06-15 15:43:39 -07001247 Collection<InetAddress> dnsList = p.getDnses();
1248 for (InetAddress dns : dnsList) {
1249 if (DBG) Slog.d(TAG, " adding " + dns);
1250 NetworkUtils.addHostRoute(interfaceName, dns);
Irfan Sheriffd649c122010-06-09 15:39:36 -07001251 }
1252 nt.privateDnsRouteSet(true);
1253 }
1254 }
1255
1256 private void removePrivateDnsRoutes(NetworkStateTracker nt) {
1257 // TODO - we should do this explicitly but the NetUtils api doesnt
1258 // support this yet - must remove all. No worse than before
Robert Greenwalt47f69fe2010-06-15 15:43:39 -07001259 NetworkProperties p = nt.getNetworkProperties();
1260 if (p == null) return;
1261 String interfaceName = p.getInterfaceName();
Irfan Sheriffd649c122010-06-09 15:39:36 -07001262 boolean privateDnsRouteSet = nt.isPrivateDnsRouteSet();
1263 if (interfaceName != null && privateDnsRouteSet) {
1264 if (DBG) {
1265 Slog.d(TAG, "removePrivateDnsRoutes for " + nt.getNetworkInfo().getTypeName() +
1266 " (" + interfaceName + ")");
1267 }
1268 NetworkUtils.removeHostRoutes(interfaceName);
1269 nt.privateDnsRouteSet(false);
1270 }
1271 }
1272
Irfan Sheriffd649c122010-06-09 15:39:36 -07001273
1274 private void addDefaultRoute(NetworkStateTracker nt) {
Robert Greenwalt47f69fe2010-06-15 15:43:39 -07001275 NetworkProperties p = nt.getNetworkProperties();
1276 if (p == null) return;
1277 String interfaceName = p.getInterfaceName();
1278 InetAddress defaultGatewayAddr = p.getGateway();
Irfan Sheriffd649c122010-06-09 15:39:36 -07001279
Robert Greenwaltc76b8fa2010-07-23 15:46:26 -07001280 if ((interfaceName != null) && (defaultGatewayAddr != null )) {
1281 if ((NetworkUtils.setDefaultRoute(interfaceName, defaultGatewayAddr) >= 0) && DBG) {
Robert Greenwalt47f69fe2010-06-15 15:43:39 -07001282 NetworkInfo networkInfo = nt.getNetworkInfo();
Irfan Sheriffd649c122010-06-09 15:39:36 -07001283 Slog.d(TAG, "addDefaultRoute for " + networkInfo.getTypeName() +
1284 " (" + interfaceName + "), GatewayAddr=" + defaultGatewayAddr);
1285 }
Irfan Sheriffd649c122010-06-09 15:39:36 -07001286 }
1287 }
1288
1289
1290 public void removeDefaultRoute(NetworkStateTracker nt) {
Robert Greenwalt47f69fe2010-06-15 15:43:39 -07001291 NetworkProperties p = nt.getNetworkProperties();
1292 if (p == null) return;
1293 String interfaceName = p.getInterfaceName();
Irfan Sheriffd649c122010-06-09 15:39:36 -07001294
Robert Greenwaltc76b8fa2010-07-23 15:46:26 -07001295 if (interfaceName != null) {
1296 if ((NetworkUtils.removeDefaultRoute(interfaceName) >= 0) && DBG) {
Robert Greenwalt47f69fe2010-06-15 15:43:39 -07001297 NetworkInfo networkInfo = nt.getNetworkInfo();
Irfan Sheriffd649c122010-06-09 15:39:36 -07001298 Slog.d(TAG, "removeDefaultRoute for " + networkInfo.getTypeName() + " (" +
1299 interfaceName + ")");
1300 }
Irfan Sheriffd649c122010-06-09 15:39:36 -07001301 }
1302 }
1303
1304 /**
1305 * Reads the network specific TCP buffer sizes from SystemProperties
1306 * net.tcp.buffersize.[default|wifi|umts|edge|gprs] and set them for system
1307 * wide use
1308 */
1309 public void updateNetworkSettings(NetworkStateTracker nt) {
1310 String key = nt.getTcpBufferSizesPropName();
1311 String bufferSizes = SystemProperties.get(key);
1312
1313 if (bufferSizes.length() == 0) {
1314 Slog.e(TAG, key + " not found in system properties. Using defaults");
1315
1316 // Setting to default values so we won't be stuck to previous values
1317 key = "net.tcp.buffersize.default";
1318 bufferSizes = SystemProperties.get(key);
1319 }
1320
1321 // Set values in kernel
1322 if (bufferSizes.length() != 0) {
1323 if (DBG) {
1324 Slog.v(TAG, "Setting TCP values: [" + bufferSizes
1325 + "] which comes from [" + key + "]");
1326 }
1327 setBufferSize(bufferSizes);
1328 }
1329 }
1330
1331 /**
1332 * Writes TCP buffer sizes to /sys/kernel/ipv4/tcp_[r/w]mem_[min/def/max]
1333 * which maps to /proc/sys/net/ipv4/tcp_rmem and tcpwmem
1334 *
1335 * @param bufferSizes in the format of "readMin, readInitial, readMax,
1336 * writeMin, writeInitial, writeMax"
1337 */
1338 private void setBufferSize(String bufferSizes) {
1339 try {
1340 String[] values = bufferSizes.split(",");
1341
1342 if (values.length == 6) {
1343 final String prefix = "/sys/kernel/ipv4/tcp_";
1344 stringToFile(prefix + "rmem_min", values[0]);
1345 stringToFile(prefix + "rmem_def", values[1]);
1346 stringToFile(prefix + "rmem_max", values[2]);
1347 stringToFile(prefix + "wmem_min", values[3]);
1348 stringToFile(prefix + "wmem_def", values[4]);
1349 stringToFile(prefix + "wmem_max", values[5]);
1350 } else {
1351 Slog.e(TAG, "Invalid buffersize string: " + bufferSizes);
1352 }
1353 } catch (IOException e) {
1354 Slog.e(TAG, "Can't set tcp buffer sizes:" + e);
1355 }
1356 }
1357
1358 /**
1359 * Writes string to file. Basically same as "echo -n $string > $filename"
1360 *
1361 * @param filename
1362 * @param string
1363 * @throws IOException
1364 */
1365 private void stringToFile(String filename, String string) throws IOException {
1366 FileWriter out = new FileWriter(filename);
1367 try {
1368 out.write(string);
1369 } finally {
1370 out.close();
1371 }
1372 }
1373
1374
Robert Greenwalt42acef32009-08-12 16:08:25 -07001375 /**
1376 * Adjust the per-process dns entries (net.dns<x>.<pid>) based
1377 * on the highest priority active net which this process requested.
1378 * If there aren't any, clear it out
1379 */
1380 private void reassessPidDns(int myPid, boolean doBump)
1381 {
Joe Onorato8a9b2202010-02-26 18:56:32 -08001382 if (DBG) Slog.d(TAG, "reassessPidDns for pid " + myPid);
Robert Greenwalt42acef32009-08-12 16:08:25 -07001383 for(int i : mPriorityList) {
1384 if (mNetAttributes[i].isDefault()) {
1385 continue;
1386 }
1387 NetworkStateTracker nt = mNetTrackers[i];
Robert Greenwalt86e9e552009-07-16 17:21:39 -07001388 if (nt.getNetworkInfo().isConnected() &&
1389 !nt.isTeardownRequested()) {
Robert Greenwalt47f69fe2010-06-15 15:43:39 -07001390 NetworkProperties p = nt.getNetworkProperties();
1391 if (p == null) continue;
Robert Greenwalt42acef32009-08-12 16:08:25 -07001392 List pids = mNetRequestersPids[i];
1393 for (int j=0; j<pids.size(); j++) {
1394 Integer pid = (Integer)pids.get(j);
1395 if (pid.intValue() == myPid) {
Robert Greenwalt47f69fe2010-06-15 15:43:39 -07001396 Collection<InetAddress> dnses = p.getDnses();
1397 writePidDns(dnses, myPid);
Robert Greenwalt42acef32009-08-12 16:08:25 -07001398 if (doBump) {
1399 bumpDns();
1400 }
1401 return;
1402 }
1403 }
1404 }
1405 }
1406 // nothing found - delete
1407 for (int i = 1; ; i++) {
1408 String prop = "net.dns" + i + "." + myPid;
1409 if (SystemProperties.get(prop).length() == 0) {
1410 if (doBump) {
1411 bumpDns();
1412 }
1413 return;
1414 }
1415 SystemProperties.set(prop, "");
1416 }
1417 }
1418
Robert Greenwalt47f69fe2010-06-15 15:43:39 -07001419 private void writePidDns(Collection <InetAddress> dnses, int pid) {
Robert Greenwalt42acef32009-08-12 16:08:25 -07001420 int j = 1;
Robert Greenwalt47f69fe2010-06-15 15:43:39 -07001421 for (InetAddress dns : dnses) {
1422 SystemProperties.set("net.dns" + j++ + "." + pid, dns.getHostAddress());
Robert Greenwalt42acef32009-08-12 16:08:25 -07001423 }
1424 }
1425
1426 private void bumpDns() {
1427 /*
1428 * Bump the property that tells the name resolver library to reread
1429 * the DNS server list from the properties.
1430 */
1431 String propVal = SystemProperties.get("net.dnschange");
1432 int n = 0;
1433 if (propVal.length() != 0) {
1434 try {
1435 n = Integer.parseInt(propVal);
1436 } catch (NumberFormatException e) {}
1437 }
1438 SystemProperties.set("net.dnschange", "" + (n+1));
1439 }
1440
Robert Greenwaltc76b8fa2010-07-23 15:46:26 -07001441 private void handleDnsConfigurationChange(int netType) {
Robert Greenwalt42acef32009-08-12 16:08:25 -07001442 // add default net's dns entries
Robert Greenwaltc76b8fa2010-07-23 15:46:26 -07001443 NetworkStateTracker nt = mNetTrackers[netType];
1444 if (nt != null && nt.getNetworkInfo().isConnected() && !nt.isTeardownRequested()) {
1445 NetworkProperties p = nt.getNetworkProperties();
1446 if (p == null) return;
1447 Collection<InetAddress> dnses = p.getDnses();
1448 if (mNetAttributes[netType].isDefault()) {
1449 int j = 1;
1450 for (InetAddress dns : dnses) {
1451 if (DBG) {
1452 Slog.d(TAG, "adding dns " + dns + " for " +
1453 nt.getNetworkInfo().getTypeName());
Robert Greenwalt42acef32009-08-12 16:08:25 -07001454 }
Robert Greenwaltc76b8fa2010-07-23 15:46:26 -07001455 SystemProperties.set("net.dns" + j++, dns.getHostAddress());
1456 }
1457 for (int k=j ; k<mNumDnsEntries; k++) {
1458 if (DBG) Slog.d(TAG, "erasing net.dns" + k);
1459 SystemProperties.set("net.dns" + k, "");
1460 }
1461 mNumDnsEntries = j;
1462 } else {
1463 // set per-pid dns for attached secondary nets
1464 List pids = mNetRequestersPids[netType];
1465 for (int y=0; y< pids.size(); y++) {
1466 Integer pid = (Integer)pids.get(y);
1467 writePidDns(dnses, pid.intValue());
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001468 }
1469 }
Robert Greenwaltc76b8fa2010-07-23 15:46:26 -07001470 bumpDns();
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001471 }
Robert Greenwalt42acef32009-08-12 16:08:25 -07001472 }
1473
1474 private int getRestoreDefaultNetworkDelay() {
1475 String restoreDefaultNetworkDelayStr = SystemProperties.get(
1476 NETWORK_RESTORE_DELAY_PROP_NAME);
1477 if(restoreDefaultNetworkDelayStr != null &&
1478 restoreDefaultNetworkDelayStr.length() != 0) {
1479 try {
1480 return Integer.valueOf(restoreDefaultNetworkDelayStr);
1481 } catch (NumberFormatException e) {
1482 }
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001483 }
Robert Greenwalt42acef32009-08-12 16:08:25 -07001484 return RESTORE_DEFAULT_NETWORK_DELAY;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001485 }
1486
1487 @Override
1488 protected void dump(FileDescriptor fd, PrintWriter pw, String[] args) {
Robert Greenwalt86e9e552009-07-16 17:21:39 -07001489 if (mContext.checkCallingOrSelfPermission(
1490 android.Manifest.permission.DUMP)
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001491 != PackageManager.PERMISSION_GRANTED) {
Robert Greenwalt86e9e552009-07-16 17:21:39 -07001492 pw.println("Permission Denial: can't dump ConnectivityService " +
1493 "from from pid=" + Binder.getCallingPid() + ", uid=" +
1494 Binder.getCallingUid());
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001495 return;
1496 }
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001497 pw.println();
1498 for (NetworkStateTracker nst : mNetTrackers) {
Robert Greenwaltb9285352009-12-21 18:24:07 -08001499 if (nst != null) {
1500 if (nst.getNetworkInfo().isConnected()) {
1501 pw.println("Active network: " + nst.getNetworkInfo().
1502 getTypeName());
1503 }
1504 pw.println(nst.getNetworkInfo());
1505 pw.println(nst);
1506 pw.println();
Robert Greenwalt42acef32009-08-12 16:08:25 -07001507 }
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001508 }
Robert Greenwaltb9285352009-12-21 18:24:07 -08001509
1510 pw.println("Network Requester Pids:");
1511 for (int net : mPriorityList) {
1512 String pidString = net + ": ";
1513 for (Object pid : mNetRequestersPids[net]) {
1514 pidString = pidString + pid.toString() + ", ";
1515 }
1516 pw.println(pidString);
1517 }
1518 pw.println();
1519
1520 pw.println("FeatureUsers:");
1521 for (Object requester : mFeatureUsers) {
1522 pw.println(requester.toString());
1523 }
1524 pw.println();
Robert Greenwalt2a091d72010-02-11 18:18:40 -08001525
Robert Greenwalt14f2ef42010-06-15 12:19:37 -07001526 synchronized (this) {
1527 pw.println("NetworkTranstionWakeLock is currently " +
1528 (mNetTransitionWakeLock.isHeld() ? "" : "not ") + "held.");
1529 pw.println("It was last requested for "+mNetTransitionWakeLockCausedBy);
1530 }
1531 pw.println();
1532
Robert Greenwalt2a091d72010-02-11 18:18:40 -08001533 mTethering.dump(fd, pw, args);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001534 }
1535
Robert Greenwalt42acef32009-08-12 16:08:25 -07001536 // must be stateless - things change under us.
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001537 private class MyHandler extends Handler {
1538 @Override
1539 public void handleMessage(Message msg) {
1540 NetworkInfo info;
1541 switch (msg.what) {
1542 case NetworkStateTracker.EVENT_STATE_CHANGED:
1543 info = (NetworkInfo) msg.obj;
Robert Greenwalt511288a2009-12-07 11:33:18 -08001544 int type = info.getType();
1545 NetworkInfo.State state = info.getState();
Robert Greenwalt6e6dec22010-01-25 16:14:00 -08001546 // only do this optimization for wifi. It going into scan mode for location
1547 // services generates alot of noise. Meanwhile the mms apn won't send out
1548 // subsequent notifications when on default cellular because it never
1549 // disconnects.. so only do this to wifi notifications. Fixed better when the
1550 // APN notifications are standardized.
1551 if (mNetAttributes[type].mLastState == state &&
1552 mNetAttributes[type].mRadio == ConnectivityManager.TYPE_WIFI) {
Robert Greenwalt511288a2009-12-07 11:33:18 -08001553 if (DBG) {
Robert Greenwalt6e6dec22010-01-25 16:14:00 -08001554 // TODO - remove this after we validate the dropping doesn't break
1555 // anything
Joe Onorato8a9b2202010-02-26 18:56:32 -08001556 Slog.d(TAG, "Dropping ConnectivityChange for " +
Robert Greenwalt1193ae42010-01-13 09:36:31 -08001557 info.getTypeName() + ": " +
Robert Greenwalt511288a2009-12-07 11:33:18 -08001558 state + "/" + info.getDetailedState());
1559 }
1560 return;
1561 }
1562 mNetAttributes[type].mLastState = state;
1563
Joe Onorato8a9b2202010-02-26 18:56:32 -08001564 if (DBG) Slog.d(TAG, "ConnectivityChange for " +
Robert Greenwalt86e9e552009-07-16 17:21:39 -07001565 info.getTypeName() + ": " +
Robert Greenwalt511288a2009-12-07 11:33:18 -08001566 state + "/" + info.getDetailedState());
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001567
1568 // Connectivity state changed:
1569 // [31-13] Reserved for future use
Robert Greenwalt86e9e552009-07-16 17:21:39 -07001570 // [12-9] Network subtype (for mobile network, as defined
1571 // by TelephonyManager)
1572 // [8-3] Detailed state ordinal (as defined by
1573 // NetworkInfo.DetailedState)
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001574 // [2-0] Network type (as defined by ConnectivityManager)
1575 int eventLogParam = (info.getType() & 0x7) |
1576 ((info.getDetailedState().ordinal() & 0x3f) << 3) |
1577 (info.getSubtype() << 9);
Doug Zongkerab5c49c2009-12-04 10:31:43 -08001578 EventLog.writeEvent(EventLogTags.CONNECTIVITY_STATE_CHANGED,
Robert Greenwalt86e9e552009-07-16 17:21:39 -07001579 eventLogParam);
1580
1581 if (info.getDetailedState() ==
1582 NetworkInfo.DetailedState.FAILED) {
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001583 handleConnectionFailure(info);
Robert Greenwalt511288a2009-12-07 11:33:18 -08001584 } else if (state == NetworkInfo.State.DISCONNECTED) {
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001585 handleDisconnect(info);
Robert Greenwalt511288a2009-12-07 11:33:18 -08001586 } else if (state == NetworkInfo.State.SUSPENDED) {
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001587 // TODO: need to think this over.
Robert Greenwalt86e9e552009-07-16 17:21:39 -07001588 // the logic here is, handle SUSPENDED the same as
1589 // DISCONNECTED. The only difference being we are
1590 // broadcasting an intent with NetworkInfo that's
1591 // suspended. This allows the applications an
1592 // opportunity to handle DISCONNECTED and SUSPENDED
1593 // differently, or not.
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001594 handleDisconnect(info);
Robert Greenwalt511288a2009-12-07 11:33:18 -08001595 } else if (state == NetworkInfo.State.CONNECTED) {
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001596 handleConnect(info);
1597 }
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001598 break;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001599 case NetworkStateTracker.EVENT_CONFIGURATION_CHANGED:
Robert Greenwaltc76b8fa2010-07-23 15:46:26 -07001600 // TODO - make this handle ip/proxy/gateway/dns changes
1601 info = (NetworkInfo) msg.obj;
1602 type = info.getType();
1603 handleDnsConfigurationChange(type);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001604 break;
Robert Greenwalt42acef32009-08-12 16:08:25 -07001605 case NetworkStateTracker.EVENT_RESTORE_DEFAULT_NETWORK:
Robert Greenwalt9c75d4a2009-09-27 17:27:04 -07001606 FeatureUser u = (FeatureUser)msg.obj;
1607 u.expire();
Robert Greenwalt42acef32009-08-12 16:08:25 -07001608 break;
Robert Greenwalt14f2ef42010-06-15 12:19:37 -07001609 case NetworkStateTracker.EVENT_CLEAR_NET_TRANSITION_WAKELOCK:
1610 String causedBy = null;
1611 synchronized (ConnectivityService.this) {
1612 if (msg.arg1 == mNetTransitionWakeLockSerialNumber &&
1613 mNetTransitionWakeLock.isHeld()) {
1614 mNetTransitionWakeLock.release();
1615 causedBy = mNetTransitionWakeLockCausedBy;
1616 }
1617 }
1618 if (causedBy != null) {
1619 Slog.d(TAG, "NetTransition Wakelock for " +
1620 causedBy + " released by timeout");
1621 }
1622 break;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001623 }
1624 }
1625 }
Robert Greenwaltd0e18ff2010-01-26 11:40:34 -08001626
1627 // javadoc from interface
Robert Greenwalt5a735062010-03-02 17:25:02 -08001628 public int tether(String iface) {
Robert Greenwaltd0e18ff2010-01-26 11:40:34 -08001629 enforceTetherChangePermission();
Robert Greenwalt5a735062010-03-02 17:25:02 -08001630
1631 if (isTetheringSupported()) {
1632 return mTethering.tether(iface);
1633 } else {
1634 return ConnectivityManager.TETHER_ERROR_UNSUPPORTED;
1635 }
Robert Greenwaltd0e18ff2010-01-26 11:40:34 -08001636 }
1637
1638 // javadoc from interface
Robert Greenwalt5a735062010-03-02 17:25:02 -08001639 public int untether(String iface) {
Robert Greenwaltd0e18ff2010-01-26 11:40:34 -08001640 enforceTetherChangePermission();
Robert Greenwalt5a735062010-03-02 17:25:02 -08001641
1642 if (isTetheringSupported()) {
1643 return mTethering.untether(iface);
1644 } else {
1645 return ConnectivityManager.TETHER_ERROR_UNSUPPORTED;
1646 }
1647 }
1648
1649 // javadoc from interface
1650 public int getLastTetherError(String iface) {
1651 enforceTetherAccessPermission();
1652
1653 if (isTetheringSupported()) {
1654 return mTethering.getLastTetherError(iface);
1655 } else {
1656 return ConnectivityManager.TETHER_ERROR_UNSUPPORTED;
1657 }
Robert Greenwalt2a091d72010-02-11 18:18:40 -08001658 }
1659
1660 // TODO - proper iface API for selection by property, inspection, etc
1661 public String[] getTetherableUsbRegexs() {
1662 enforceTetherAccessPermission();
1663 if (isTetheringSupported()) {
1664 return mTethering.getTetherableUsbRegexs();
1665 } else {
1666 return new String[0];
1667 }
1668 }
1669
1670 public String[] getTetherableWifiRegexs() {
1671 enforceTetherAccessPermission();
1672 if (isTetheringSupported()) {
1673 return mTethering.getTetherableWifiRegexs();
1674 } else {
1675 return new String[0];
1676 }
Robert Greenwaltd0e18ff2010-01-26 11:40:34 -08001677 }
1678
1679 // TODO - move iface listing, queries, etc to new module
1680 // javadoc from interface
1681 public String[] getTetherableIfaces() {
Robert Greenwalt2a091d72010-02-11 18:18:40 -08001682 enforceTetherAccessPermission();
Robert Greenwaltd0e18ff2010-01-26 11:40:34 -08001683 return mTethering.getTetherableIfaces();
1684 }
1685
1686 public String[] getTetheredIfaces() {
Robert Greenwalt2a091d72010-02-11 18:18:40 -08001687 enforceTetherAccessPermission();
Robert Greenwaltd0e18ff2010-01-26 11:40:34 -08001688 return mTethering.getTetheredIfaces();
1689 }
Robert Greenwalt2a091d72010-02-11 18:18:40 -08001690
Robert Greenwalt5a735062010-03-02 17:25:02 -08001691 public String[] getTetheringErroredIfaces() {
1692 enforceTetherAccessPermission();
1693 return mTethering.getErroredIfaces();
1694 }
1695
Robert Greenwalt2a091d72010-02-11 18:18:40 -08001696 // if ro.tether.denied = true we default to no tethering
1697 // gservices could set the secure setting to 1 though to enable it on a build where it
1698 // had previously been turned off.
1699 public boolean isTetheringSupported() {
1700 enforceTetherAccessPermission();
1701 int defaultVal = (SystemProperties.get("ro.tether.denied").equals("true") ? 0 : 1);
Robert Greenwaltc9d5fb72010-02-25 12:29:30 -08001702 boolean tetherEnabledInSettings = (Settings.Secure.getInt(mContext.getContentResolver(),
1703 Settings.Secure.TETHER_SUPPORTED, defaultVal) != 0);
1704 return tetherEnabledInSettings && mTetheringConfigValid;
Robert Greenwalt2a091d72010-02-11 18:18:40 -08001705 }
Robert Greenwalt14f2ef42010-06-15 12:19:37 -07001706
1707 // An API NetworkStateTrackers can call when they lose their network.
1708 // This will automatically be cleared after X seconds or a network becomes CONNECTED,
1709 // whichever happens first. The timer is started by the first caller and not
1710 // restarted by subsequent callers.
1711 public void requestNetworkTransitionWakelock(String forWhom) {
1712 enforceConnectivityInternalPermission();
1713 synchronized (this) {
1714 if (mNetTransitionWakeLock.isHeld()) return;
1715 mNetTransitionWakeLockSerialNumber++;
1716 mNetTransitionWakeLock.acquire();
1717 mNetTransitionWakeLockCausedBy = forWhom;
1718 }
1719 mHandler.sendMessageDelayed(mHandler.obtainMessage(
1720 NetworkStateTracker.EVENT_CLEAR_NET_TRANSITION_WAKELOCK,
1721 mNetTransitionWakeLockSerialNumber, 0),
1722 mNetTransitionWakeLockTimeout);
1723 return;
1724 }
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001725}