blob: bd2a0eaa6b1f10541746085b828ccf12c0d24b04 [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);
946 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 Greenwalt0659da32009-07-16 17:21:39 -0700955 intent.putExtra(ConnectivityManager.EXTRA_EXTRA_INFO,
956 info.getExtraInfo());
The Android Open Source Project28527d22009-03-03 19:31:44 -0800957 }
Robert Greenwalt2034b912009-08-12 16:08:25 -0700958
Robert Greenwalt3cc68d32010-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 Greenwaltf55ced92010-01-20 19:29:41 -0800968 }
969 // do this before we broadcast the change
Robert Greenwalt3afbead2010-07-23 15:46:26 -0700970 handleConnectivityChange(prevNetType);
Robert Greenwaltf55ced92010-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 Greenwalt3cc68d32010-01-25 17:54:29 -0800982 // returns null if no failover available
Robert Greenwaltf55ced92010-01-20 19:29:41 -0800983 private NetworkStateTracker tryFailover(int prevNetType) {
Robert Greenwalt2034b912009-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 Greenwalt2034b912009-08-12 16:08:25 -0700990 if (mActiveDefaultNetwork == prevNetType) {
991 mActiveDefaultNetwork = -1;
992 }
993
994 int newType = -1;
995 int newPriority = -1;
Robert Greenwalt72451bf2010-02-25 12:04:29 -0800996 boolean noMobileData = !getMobileDataEnabled();
Robert Greenwaltf55ced92010-01-20 19:29:41 -0800997 for (int checkType=0; checkType <= ConnectivityManager.MAX_NETWORK_TYPE; checkType++) {
Robert Greenwaltec05b3c2009-10-30 14:17:42 -0700998 if (checkType == prevNetType) continue;
999 if (mNetAttributes[checkType] == null) continue;
Robert Greenwalt72451bf2010-02-25 12:04:29 -08001000 if (mNetAttributes[checkType].mRadio == ConnectivityManager.TYPE_MOBILE &&
1001 noMobileData) {
1002 if (DBG) {
Joe Onoratoc2386bb2010-02-26 18:56:32 -08001003 Slog.d(TAG, "not failing over to mobile type " + checkType +
Robert Greenwalt72451bf2010-02-25 12:04:29 -08001004 " because Mobile Data Disabled");
1005 }
1006 continue;
1007 }
Robert Greenwalt2034b912009-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 Greenwaltec05b3c2009-10-30 14:17:42 -07001016 if (mNetAttributes[checkType].mPriority > newPriority) {
Robert Greenwalt2034b912009-08-12 16:08:25 -07001017 newType = checkType;
Robert Greenwaltec05b3c2009-10-30 14:17:42 -07001018 newPriority = mNetAttributes[newType].mPriority;
Robert Greenwalt2034b912009-08-12 16:08:25 -07001019 }
The Android Open Source Project28527d22009-03-03 19:31:44 -08001020 }
1021 }
Robert Greenwalt2034b912009-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 Greenwalta52c75a2009-08-19 20:19:33 -07001035 if (!switchTo.isConnectedOrConnecting() ||
1036 newNet.isTeardownRequested()) {
Robert Greenwalt2034b912009-08-12 16:08:25 -07001037 newNet.reconnect();
1038 }
1039 if (DBG) {
1040 if (switchTo.isConnected()) {
Joe Onoratoc2386bb2010-02-26 18:56:32 -08001041 Slog.v(TAG, "Switching to already connected " +
Robert Greenwalt2034b912009-08-12 16:08:25 -07001042 switchTo.getTypeName());
1043 } else {
Joe Onoratoc2386bb2010-02-26 18:56:32 -08001044 Slog.v(TAG, "Attempting to switch to " +
Robert Greenwalt2034b912009-08-12 16:08:25 -07001045 switchTo.getTypeName());
1046 }
1047 }
Robert Greenwalt2034b912009-08-12 16:08:25 -07001048 } else {
1049 newNet.reconnect();
Robert Greenwalt12984322010-03-09 14:55:08 -08001050 newNet = null; // not officially avail.. try anyway, but
1051 // report no failover
Robert Greenwalt2034b912009-08-12 16:08:25 -07001052 }
Robert Greenwalt2034b912009-08-12 16:08:25 -07001053 }
The Android Open Source Project28527d22009-03-03 19:31:44 -08001054 }
Robert Greenwalt2034b912009-08-12 16:08:25 -07001055
Robert Greenwaltf55ced92010-01-20 19:29:41 -08001056 return newNet;
The Android Open Source Project28527d22009-03-03 19:31:44 -08001057 }
1058
1059 private void sendConnectedBroadcast(NetworkInfo info) {
1060 Intent intent = new Intent(ConnectivityManager.CONNECTIVITY_ACTION);
1061 intent.putExtra(ConnectivityManager.EXTRA_NETWORK_INFO, info);
1062 if (info.isFailover()) {
1063 intent.putExtra(ConnectivityManager.EXTRA_IS_FAILOVER, true);
1064 info.setFailover(false);
1065 }
1066 if (info.getReason() != null) {
1067 intent.putExtra(ConnectivityManager.EXTRA_REASON, info.getReason());
1068 }
1069 if (info.getExtraInfo() != null) {
Robert Greenwalt0659da32009-07-16 17:21:39 -07001070 intent.putExtra(ConnectivityManager.EXTRA_EXTRA_INFO,
1071 info.getExtraInfo());
The Android Open Source Project28527d22009-03-03 19:31:44 -08001072 }
Mike Lockwoodfde2b762009-08-14 14:18:49 -04001073 sendStickyBroadcast(intent);
The Android Open Source Project28527d22009-03-03 19:31:44 -08001074 }
1075
1076 /**
1077 * Called when an attempt to fail over to another network has failed.
1078 * @param info the {@link NetworkInfo} for the failed network
1079 */
1080 private void handleConnectionFailure(NetworkInfo info) {
1081 mNetTrackers[info.getType()].setTeardownRequested(false);
The Android Open Source Project28527d22009-03-03 19:31:44 -08001082
Robert Greenwalt2034b912009-08-12 16:08:25 -07001083 String reason = info.getReason();
1084 String extraInfo = info.getExtraInfo();
Robert Greenwalt0659da32009-07-16 17:21:39 -07001085
Robert Greenwalt2034b912009-08-12 16:08:25 -07001086 if (DBG) {
1087 String reasonText;
1088 if (reason == null) {
1089 reasonText = ".";
1090 } else {
1091 reasonText = " (" + reason + ").";
The Android Open Source Project28527d22009-03-03 19:31:44 -08001092 }
Joe Onoratoc2386bb2010-02-26 18:56:32 -08001093 Slog.v(TAG, "Attempt to connect to " + info.getTypeName() +
Robert Greenwalt2034b912009-08-12 16:08:25 -07001094 " failed" + reasonText);
The Android Open Source Project28527d22009-03-03 19:31:44 -08001095 }
Robert Greenwalt2034b912009-08-12 16:08:25 -07001096
1097 Intent intent = new Intent(ConnectivityManager.CONNECTIVITY_ACTION);
1098 intent.putExtra(ConnectivityManager.EXTRA_NETWORK_INFO, info);
1099 if (getActiveNetworkInfo() == null) {
1100 intent.putExtra(ConnectivityManager.EXTRA_NO_CONNECTIVITY, true);
1101 }
1102 if (reason != null) {
1103 intent.putExtra(ConnectivityManager.EXTRA_REASON, reason);
1104 }
1105 if (extraInfo != null) {
1106 intent.putExtra(ConnectivityManager.EXTRA_EXTRA_INFO, extraInfo);
1107 }
1108 if (info.isFailover()) {
1109 intent.putExtra(ConnectivityManager.EXTRA_IS_FAILOVER, true);
1110 info.setFailover(false);
1111 }
Robert Greenwaltf55ced92010-01-20 19:29:41 -08001112
Robert Greenwalt3cc68d32010-01-25 17:54:29 -08001113 NetworkStateTracker newNet = null;
1114 if (mNetAttributes[info.getType()].isDefault()) {
1115 newNet = tryFailover(info.getType());
1116 if (newNet != null) {
1117 NetworkInfo switchTo = newNet.getNetworkInfo();
1118 intent.putExtra(ConnectivityManager.EXTRA_OTHER_NETWORK_INFO, switchTo);
1119 } else {
1120 intent.putExtra(ConnectivityManager.EXTRA_NO_CONNECTIVITY, true);
1121 }
Robert Greenwaltf55ced92010-01-20 19:29:41 -08001122 }
Robert Greenwalt3cc68d32010-01-25 17:54:29 -08001123
Mike Lockwoodfde2b762009-08-14 14:18:49 -04001124 sendStickyBroadcast(intent);
Robert Greenwaltf55ced92010-01-20 19:29:41 -08001125 /*
1126 * If the failover network is already connected, then immediately send
1127 * out a followup broadcast indicating successful failover
1128 */
1129 if (newNet != null && newNet.getNetworkInfo().isConnected()) {
1130 sendConnectedBroadcast(newNet.getNetworkInfo());
1131 }
Mike Lockwoodfde2b762009-08-14 14:18:49 -04001132 }
1133
1134 private void sendStickyBroadcast(Intent intent) {
1135 synchronized(this) {
Dianne Hackborna417ff82009-12-08 19:45:14 -08001136 if (!mSystemReady) {
1137 mInitialBroadcast = new Intent(intent);
Mike Lockwoodfde2b762009-08-14 14:18:49 -04001138 }
Dianne Hackborna417ff82009-12-08 19:45:14 -08001139 intent.addFlags(Intent.FLAG_RECEIVER_REGISTERED_ONLY_BEFORE_BOOT);
1140 mContext.sendStickyBroadcast(intent);
Mike Lockwoodfde2b762009-08-14 14:18:49 -04001141 }
1142 }
1143
1144 void systemReady() {
1145 synchronized(this) {
1146 mSystemReady = true;
Dianne Hackborna417ff82009-12-08 19:45:14 -08001147 if (mInitialBroadcast != null) {
1148 mContext.sendStickyBroadcast(mInitialBroadcast);
1149 mInitialBroadcast = null;
Mike Lockwoodfde2b762009-08-14 14:18:49 -04001150 }
1151 }
The Android Open Source Project28527d22009-03-03 19:31:44 -08001152 }
1153
1154 private void handleConnect(NetworkInfo info) {
Robert Greenwalt2034b912009-08-12 16:08:25 -07001155 int type = info.getType();
The Android Open Source Project28527d22009-03-03 19:31:44 -08001156
1157 // snapshot isFailover, because sendConnectedBroadcast() resets it
1158 boolean isFailover = info.isFailover();
Robert Greenwalt2034b912009-08-12 16:08:25 -07001159 NetworkStateTracker thisNet = mNetTrackers[type];
The Android Open Source Project28527d22009-03-03 19:31:44 -08001160
Robert Greenwalt2034b912009-08-12 16:08:25 -07001161 // if this is a default net and other default is running
1162 // kill the one not preferred
1163 if (mNetAttributes[type].isDefault()) {
Robert Greenwalt2034b912009-08-12 16:08:25 -07001164 if (mActiveDefaultNetwork != -1 && mActiveDefaultNetwork != type) {
1165 if ((type != mNetworkPreference &&
1166 mNetAttributes[mActiveDefaultNetwork].mPriority >
1167 mNetAttributes[type].mPriority) ||
1168 mNetworkPreference == mActiveDefaultNetwork) {
1169 // don't accept this one
Joe Onoratoc2386bb2010-02-26 18:56:32 -08001170 if (DBG) Slog.v(TAG, "Not broadcasting CONNECT_ACTION " +
Robert Greenwalt2034b912009-08-12 16:08:25 -07001171 "to torn down network " + info.getTypeName());
1172 teardown(thisNet);
1173 return;
1174 } else {
1175 // tear down the other
1176 NetworkStateTracker otherNet =
1177 mNetTrackers[mActiveDefaultNetwork];
Joe Onoratoc2386bb2010-02-26 18:56:32 -08001178 if (DBG) Slog.v(TAG, "Policy requires " +
Robert Greenwalt2034b912009-08-12 16:08:25 -07001179 otherNet.getNetworkInfo().getTypeName() +
1180 " teardown");
1181 if (!teardown(otherNet)) {
Joe Onoratoc2386bb2010-02-26 18:56:32 -08001182 Slog.e(TAG, "Network declined teardown request");
Robert Greenwalt2034b912009-08-12 16:08:25 -07001183 return;
1184 }
Robert Greenwalt93dc1042010-06-15 12:19:37 -07001185 }
1186 }
1187 synchronized (ConnectivityService.this) {
1188 // have a new default network, release the transition wakelock in a second
1189 // if it's held. The second pause is to allow apps to reconnect over the
1190 // new network
1191 if (mNetTransitionWakeLock.isHeld()) {
1192 mHandler.sendMessageDelayed(mHandler.obtainMessage(
1193 NetworkStateTracker.EVENT_CLEAR_NET_TRANSITION_WAKELOCK,
1194 mNetTransitionWakeLockSerialNumber, 0),
1195 1000);
Robert Greenwalt2034b912009-08-12 16:08:25 -07001196 }
1197 }
1198 mActiveDefaultNetwork = type;
1199 }
The Android Open Source Project28527d22009-03-03 19:31:44 -08001200 thisNet.setTeardownRequested(false);
Irfan Sheriff7f132d92010-06-09 15:39:36 -07001201 updateNetworkSettings(thisNet);
Robert Greenwalt3afbead2010-07-23 15:46:26 -07001202 handleConnectivityChange(type);
Robert Greenwalt2034b912009-08-12 16:08:25 -07001203 sendConnectedBroadcast(info);
The Android Open Source Project28527d22009-03-03 19:31:44 -08001204 }
1205
The Android Open Source Project28527d22009-03-03 19:31:44 -08001206 /**
Robert Greenwalt3afbead2010-07-23 15:46:26 -07001207 * After a change in the connectivity state of a network. We're mainly
1208 * concerned with making sure that the list of DNS servers is set up
1209 * according to which networks are connected, and ensuring that the
1210 * right routing table entries exist.
The Android Open Source Project28527d22009-03-03 19:31:44 -08001211 */
Robert Greenwalt3afbead2010-07-23 15:46:26 -07001212 private void handleConnectivityChange(int netType) {
The Android Open Source Project28527d22009-03-03 19:31:44 -08001213 /*
Robert Greenwalt2034b912009-08-12 16:08:25 -07001214 * If a non-default network is enabled, add the host routes that
Robert Greenwalt3afbead2010-07-23 15:46:26 -07001215 * will allow it's DNS servers to be accessed.
The Android Open Source Project28527d22009-03-03 19:31:44 -08001216 */
Robert Greenwalt3afbead2010-07-23 15:46:26 -07001217 handleDnsConfigurationChange(netType);
Robert Greenwalt2034b912009-08-12 16:08:25 -07001218
Robert Greenwalt3afbead2010-07-23 15:46:26 -07001219 if (mNetTrackers[netType].getNetworkInfo().isConnected()) {
1220 if (mNetAttributes[netType].isDefault()) {
1221 addDefaultRoute(mNetTrackers[netType]);
Robert Greenwalt2034b912009-08-12 16:08:25 -07001222 } else {
Robert Greenwalt3afbead2010-07-23 15:46:26 -07001223 addPrivateDnsRoutes(mNetTrackers[netType]);
1224 }
1225 } else {
1226 if (mNetAttributes[netType].isDefault()) {
1227 removeDefaultRoute(mNetTrackers[netType]);
1228 } else {
1229 removePrivateDnsRoutes(mNetTrackers[netType]);
Robert Greenwalt2034b912009-08-12 16:08:25 -07001230 }
The Android Open Source Project28527d22009-03-03 19:31:44 -08001231 }
1232 }
1233
Irfan Sheriff7f132d92010-06-09 15:39:36 -07001234 private void addPrivateDnsRoutes(NetworkStateTracker nt) {
Irfan Sheriff7f132d92010-06-09 15:39:36 -07001235 boolean privateDnsRouteSet = nt.isPrivateDnsRouteSet();
Robert Greenwalta7dfbd32010-06-15 15:43:39 -07001236 NetworkProperties p = nt.getNetworkProperties();
1237 if (p == null) return;
1238 String interfaceName = p.getInterfaceName();
Irfan Sheriff7f132d92010-06-09 15:39:36 -07001239
1240 if (DBG) {
1241 Slog.d(TAG, "addPrivateDnsRoutes for " + nt +
1242 "(" + interfaceName + ") - mPrivateDnsRouteSet = " + privateDnsRouteSet);
1243 }
Irfan Sheriff7f132d92010-06-09 15:39:36 -07001244 if (interfaceName != null && !privateDnsRouteSet) {
Robert Greenwalta7dfbd32010-06-15 15:43:39 -07001245 Collection<InetAddress> dnsList = p.getDnses();
1246 for (InetAddress dns : dnsList) {
1247 if (DBG) Slog.d(TAG, " adding " + dns);
1248 NetworkUtils.addHostRoute(interfaceName, dns);
Irfan Sheriff7f132d92010-06-09 15:39:36 -07001249 }
1250 nt.privateDnsRouteSet(true);
1251 }
1252 }
1253
1254 private void removePrivateDnsRoutes(NetworkStateTracker nt) {
1255 // TODO - we should do this explicitly but the NetUtils api doesnt
1256 // support this yet - must remove all. No worse than before
Robert Greenwalta7dfbd32010-06-15 15:43:39 -07001257 NetworkProperties p = nt.getNetworkProperties();
1258 if (p == null) return;
1259 String interfaceName = p.getInterfaceName();
Irfan Sheriff7f132d92010-06-09 15:39:36 -07001260 boolean privateDnsRouteSet = nt.isPrivateDnsRouteSet();
1261 if (interfaceName != null && privateDnsRouteSet) {
1262 if (DBG) {
1263 Slog.d(TAG, "removePrivateDnsRoutes for " + nt.getNetworkInfo().getTypeName() +
1264 " (" + interfaceName + ")");
1265 }
1266 NetworkUtils.removeHostRoutes(interfaceName);
1267 nt.privateDnsRouteSet(false);
1268 }
1269 }
1270
Irfan Sheriff7f132d92010-06-09 15:39:36 -07001271
1272 private void addDefaultRoute(NetworkStateTracker nt) {
Robert Greenwalta7dfbd32010-06-15 15:43:39 -07001273 NetworkProperties p = nt.getNetworkProperties();
1274 if (p == null) return;
1275 String interfaceName = p.getInterfaceName();
1276 InetAddress defaultGatewayAddr = p.getGateway();
Irfan Sheriff7f132d92010-06-09 15:39:36 -07001277
Robert Greenwalt3afbead2010-07-23 15:46:26 -07001278 if ((interfaceName != null) && (defaultGatewayAddr != null )) {
1279 if ((NetworkUtils.setDefaultRoute(interfaceName, defaultGatewayAddr) >= 0) && DBG) {
Robert Greenwalta7dfbd32010-06-15 15:43:39 -07001280 NetworkInfo networkInfo = nt.getNetworkInfo();
Irfan Sheriff7f132d92010-06-09 15:39:36 -07001281 Slog.d(TAG, "addDefaultRoute for " + networkInfo.getTypeName() +
1282 " (" + interfaceName + "), GatewayAddr=" + defaultGatewayAddr);
1283 }
Irfan Sheriff7f132d92010-06-09 15:39:36 -07001284 }
1285 }
1286
1287
1288 public void removeDefaultRoute(NetworkStateTracker nt) {
Robert Greenwalta7dfbd32010-06-15 15:43:39 -07001289 NetworkProperties p = nt.getNetworkProperties();
1290 if (p == null) return;
1291 String interfaceName = p.getInterfaceName();
Irfan Sheriff7f132d92010-06-09 15:39:36 -07001292
Robert Greenwalt3afbead2010-07-23 15:46:26 -07001293 if (interfaceName != null) {
1294 if ((NetworkUtils.removeDefaultRoute(interfaceName) >= 0) && DBG) {
Robert Greenwalta7dfbd32010-06-15 15:43:39 -07001295 NetworkInfo networkInfo = nt.getNetworkInfo();
Irfan Sheriff7f132d92010-06-09 15:39:36 -07001296 Slog.d(TAG, "removeDefaultRoute for " + networkInfo.getTypeName() + " (" +
1297 interfaceName + ")");
1298 }
Irfan Sheriff7f132d92010-06-09 15:39:36 -07001299 }
1300 }
1301
1302 /**
1303 * Reads the network specific TCP buffer sizes from SystemProperties
1304 * net.tcp.buffersize.[default|wifi|umts|edge|gprs] and set them for system
1305 * wide use
1306 */
1307 public void updateNetworkSettings(NetworkStateTracker nt) {
1308 String key = nt.getTcpBufferSizesPropName();
1309 String bufferSizes = SystemProperties.get(key);
1310
1311 if (bufferSizes.length() == 0) {
1312 Slog.e(TAG, key + " not found in system properties. Using defaults");
1313
1314 // Setting to default values so we won't be stuck to previous values
1315 key = "net.tcp.buffersize.default";
1316 bufferSizes = SystemProperties.get(key);
1317 }
1318
1319 // Set values in kernel
1320 if (bufferSizes.length() != 0) {
1321 if (DBG) {
1322 Slog.v(TAG, "Setting TCP values: [" + bufferSizes
1323 + "] which comes from [" + key + "]");
1324 }
1325 setBufferSize(bufferSizes);
1326 }
1327 }
1328
1329 /**
1330 * Writes TCP buffer sizes to /sys/kernel/ipv4/tcp_[r/w]mem_[min/def/max]
1331 * which maps to /proc/sys/net/ipv4/tcp_rmem and tcpwmem
1332 *
1333 * @param bufferSizes in the format of "readMin, readInitial, readMax,
1334 * writeMin, writeInitial, writeMax"
1335 */
1336 private void setBufferSize(String bufferSizes) {
1337 try {
1338 String[] values = bufferSizes.split(",");
1339
1340 if (values.length == 6) {
1341 final String prefix = "/sys/kernel/ipv4/tcp_";
1342 stringToFile(prefix + "rmem_min", values[0]);
1343 stringToFile(prefix + "rmem_def", values[1]);
1344 stringToFile(prefix + "rmem_max", values[2]);
1345 stringToFile(prefix + "wmem_min", values[3]);
1346 stringToFile(prefix + "wmem_def", values[4]);
1347 stringToFile(prefix + "wmem_max", values[5]);
1348 } else {
1349 Slog.e(TAG, "Invalid buffersize string: " + bufferSizes);
1350 }
1351 } catch (IOException e) {
1352 Slog.e(TAG, "Can't set tcp buffer sizes:" + e);
1353 }
1354 }
1355
1356 /**
1357 * Writes string to file. Basically same as "echo -n $string > $filename"
1358 *
1359 * @param filename
1360 * @param string
1361 * @throws IOException
1362 */
1363 private void stringToFile(String filename, String string) throws IOException {
1364 FileWriter out = new FileWriter(filename);
1365 try {
1366 out.write(string);
1367 } finally {
1368 out.close();
1369 }
1370 }
1371
1372
Robert Greenwalt2034b912009-08-12 16:08:25 -07001373 /**
1374 * Adjust the per-process dns entries (net.dns<x>.<pid>) based
1375 * on the highest priority active net which this process requested.
1376 * If there aren't any, clear it out
1377 */
1378 private void reassessPidDns(int myPid, boolean doBump)
1379 {
Joe Onoratoc2386bb2010-02-26 18:56:32 -08001380 if (DBG) Slog.d(TAG, "reassessPidDns for pid " + myPid);
Robert Greenwalt2034b912009-08-12 16:08:25 -07001381 for(int i : mPriorityList) {
1382 if (mNetAttributes[i].isDefault()) {
1383 continue;
1384 }
1385 NetworkStateTracker nt = mNetTrackers[i];
Robert Greenwalt0659da32009-07-16 17:21:39 -07001386 if (nt.getNetworkInfo().isConnected() &&
1387 !nt.isTeardownRequested()) {
Robert Greenwalta7dfbd32010-06-15 15:43:39 -07001388 NetworkProperties p = nt.getNetworkProperties();
1389 if (p == null) continue;
Robert Greenwalt2034b912009-08-12 16:08:25 -07001390 List pids = mNetRequestersPids[i];
1391 for (int j=0; j<pids.size(); j++) {
1392 Integer pid = (Integer)pids.get(j);
1393 if (pid.intValue() == myPid) {
Robert Greenwalta7dfbd32010-06-15 15:43:39 -07001394 Collection<InetAddress> dnses = p.getDnses();
1395 writePidDns(dnses, myPid);
Robert Greenwalt2034b912009-08-12 16:08:25 -07001396 if (doBump) {
1397 bumpDns();
1398 }
1399 return;
1400 }
1401 }
1402 }
1403 }
1404 // nothing found - delete
1405 for (int i = 1; ; i++) {
1406 String prop = "net.dns" + i + "." + myPid;
1407 if (SystemProperties.get(prop).length() == 0) {
1408 if (doBump) {
1409 bumpDns();
1410 }
1411 return;
1412 }
1413 SystemProperties.set(prop, "");
1414 }
1415 }
1416
Robert Greenwalta7dfbd32010-06-15 15:43:39 -07001417 private void writePidDns(Collection <InetAddress> dnses, int pid) {
Robert Greenwalt2034b912009-08-12 16:08:25 -07001418 int j = 1;
Robert Greenwalta7dfbd32010-06-15 15:43:39 -07001419 for (InetAddress dns : dnses) {
1420 SystemProperties.set("net.dns" + j++ + "." + pid, dns.getHostAddress());
Robert Greenwalt2034b912009-08-12 16:08:25 -07001421 }
1422 }
1423
1424 private void bumpDns() {
1425 /*
1426 * Bump the property that tells the name resolver library to reread
1427 * the DNS server list from the properties.
1428 */
1429 String propVal = SystemProperties.get("net.dnschange");
1430 int n = 0;
1431 if (propVal.length() != 0) {
1432 try {
1433 n = Integer.parseInt(propVal);
1434 } catch (NumberFormatException e) {}
1435 }
1436 SystemProperties.set("net.dnschange", "" + (n+1));
1437 }
1438
Robert Greenwalt3afbead2010-07-23 15:46:26 -07001439 private void handleDnsConfigurationChange(int netType) {
Robert Greenwalt2034b912009-08-12 16:08:25 -07001440 // add default net's dns entries
Robert Greenwalt3afbead2010-07-23 15:46:26 -07001441 NetworkStateTracker nt = mNetTrackers[netType];
1442 if (nt != null && nt.getNetworkInfo().isConnected() && !nt.isTeardownRequested()) {
1443 NetworkProperties p = nt.getNetworkProperties();
1444 if (p == null) return;
1445 Collection<InetAddress> dnses = p.getDnses();
1446 if (mNetAttributes[netType].isDefault()) {
1447 int j = 1;
1448 for (InetAddress dns : dnses) {
1449 if (DBG) {
1450 Slog.d(TAG, "adding dns " + dns + " for " +
1451 nt.getNetworkInfo().getTypeName());
Robert Greenwalt2034b912009-08-12 16:08:25 -07001452 }
Robert Greenwalt3afbead2010-07-23 15:46:26 -07001453 SystemProperties.set("net.dns" + j++, dns.getHostAddress());
1454 }
1455 for (int k=j ; k<mNumDnsEntries; k++) {
1456 if (DBG) Slog.d(TAG, "erasing net.dns" + k);
1457 SystemProperties.set("net.dns" + k, "");
1458 }
1459 mNumDnsEntries = j;
1460 } else {
1461 // set per-pid dns for attached secondary nets
1462 List pids = mNetRequestersPids[netType];
1463 for (int y=0; y< pids.size(); y++) {
1464 Integer pid = (Integer)pids.get(y);
1465 writePidDns(dnses, pid.intValue());
The Android Open Source Project28527d22009-03-03 19:31:44 -08001466 }
1467 }
Robert Greenwalt3afbead2010-07-23 15:46:26 -07001468 bumpDns();
The Android Open Source Project28527d22009-03-03 19:31:44 -08001469 }
Robert Greenwalt2034b912009-08-12 16:08:25 -07001470 }
1471
1472 private int getRestoreDefaultNetworkDelay() {
1473 String restoreDefaultNetworkDelayStr = SystemProperties.get(
1474 NETWORK_RESTORE_DELAY_PROP_NAME);
1475 if(restoreDefaultNetworkDelayStr != null &&
1476 restoreDefaultNetworkDelayStr.length() != 0) {
1477 try {
1478 return Integer.valueOf(restoreDefaultNetworkDelayStr);
1479 } catch (NumberFormatException e) {
1480 }
The Android Open Source Project28527d22009-03-03 19:31:44 -08001481 }
Robert Greenwalt2034b912009-08-12 16:08:25 -07001482 return RESTORE_DEFAULT_NETWORK_DELAY;
The Android Open Source Project28527d22009-03-03 19:31:44 -08001483 }
1484
1485 @Override
1486 protected void dump(FileDescriptor fd, PrintWriter pw, String[] args) {
Robert Greenwalt0659da32009-07-16 17:21:39 -07001487 if (mContext.checkCallingOrSelfPermission(
1488 android.Manifest.permission.DUMP)
The Android Open Source Project28527d22009-03-03 19:31:44 -08001489 != PackageManager.PERMISSION_GRANTED) {
Robert Greenwalt0659da32009-07-16 17:21:39 -07001490 pw.println("Permission Denial: can't dump ConnectivityService " +
1491 "from from pid=" + Binder.getCallingPid() + ", uid=" +
1492 Binder.getCallingUid());
The Android Open Source Project28527d22009-03-03 19:31:44 -08001493 return;
1494 }
The Android Open Source Project28527d22009-03-03 19:31:44 -08001495 pw.println();
1496 for (NetworkStateTracker nst : mNetTrackers) {
Robert Greenwalt3eeb6032009-12-21 18:24:07 -08001497 if (nst != null) {
1498 if (nst.getNetworkInfo().isConnected()) {
1499 pw.println("Active network: " + nst.getNetworkInfo().
1500 getTypeName());
1501 }
1502 pw.println(nst.getNetworkInfo());
1503 pw.println(nst);
1504 pw.println();
Robert Greenwalt2034b912009-08-12 16:08:25 -07001505 }
The Android Open Source Project28527d22009-03-03 19:31:44 -08001506 }
Robert Greenwalt3eeb6032009-12-21 18:24:07 -08001507
1508 pw.println("Network Requester Pids:");
1509 for (int net : mPriorityList) {
1510 String pidString = net + ": ";
1511 for (Object pid : mNetRequestersPids[net]) {
1512 pidString = pidString + pid.toString() + ", ";
1513 }
1514 pw.println(pidString);
1515 }
1516 pw.println();
1517
1518 pw.println("FeatureUsers:");
1519 for (Object requester : mFeatureUsers) {
1520 pw.println(requester.toString());
1521 }
1522 pw.println();
Robert Greenwalt8e87f122010-02-11 18:18:40 -08001523
Robert Greenwalt93dc1042010-06-15 12:19:37 -07001524 synchronized (this) {
1525 pw.println("NetworkTranstionWakeLock is currently " +
1526 (mNetTransitionWakeLock.isHeld() ? "" : "not ") + "held.");
1527 pw.println("It was last requested for "+mNetTransitionWakeLockCausedBy);
1528 }
1529 pw.println();
1530
Robert Greenwalt8e87f122010-02-11 18:18:40 -08001531 mTethering.dump(fd, pw, args);
The Android Open Source Project28527d22009-03-03 19:31:44 -08001532 }
1533
Robert Greenwalt2034b912009-08-12 16:08:25 -07001534 // must be stateless - things change under us.
The Android Open Source Project28527d22009-03-03 19:31:44 -08001535 private class MyHandler extends Handler {
1536 @Override
1537 public void handleMessage(Message msg) {
1538 NetworkInfo info;
1539 switch (msg.what) {
1540 case NetworkStateTracker.EVENT_STATE_CHANGED:
1541 info = (NetworkInfo) msg.obj;
Robert Greenwalt12c44552009-12-07 11:33:18 -08001542 int type = info.getType();
1543 NetworkInfo.State state = info.getState();
Robert Greenwalt24e2d2b2010-01-25 16:14:00 -08001544 // only do this optimization for wifi. It going into scan mode for location
1545 // services generates alot of noise. Meanwhile the mms apn won't send out
1546 // subsequent notifications when on default cellular because it never
1547 // disconnects.. so only do this to wifi notifications. Fixed better when the
1548 // APN notifications are standardized.
1549 if (mNetAttributes[type].mLastState == state &&
1550 mNetAttributes[type].mRadio == ConnectivityManager.TYPE_WIFI) {
Robert Greenwalt12c44552009-12-07 11:33:18 -08001551 if (DBG) {
Robert Greenwalt24e2d2b2010-01-25 16:14:00 -08001552 // TODO - remove this after we validate the dropping doesn't break
1553 // anything
Joe Onoratoc2386bb2010-02-26 18:56:32 -08001554 Slog.d(TAG, "Dropping ConnectivityChange for " +
Robert Greenwalt2adbc7f2010-01-13 09:36:31 -08001555 info.getTypeName() + ": " +
Robert Greenwalt12c44552009-12-07 11:33:18 -08001556 state + "/" + info.getDetailedState());
1557 }
1558 return;
1559 }
1560 mNetAttributes[type].mLastState = state;
1561
Joe Onoratoc2386bb2010-02-26 18:56:32 -08001562 if (DBG) Slog.d(TAG, "ConnectivityChange for " +
Robert Greenwalt0659da32009-07-16 17:21:39 -07001563 info.getTypeName() + ": " +
Robert Greenwalt12c44552009-12-07 11:33:18 -08001564 state + "/" + info.getDetailedState());
The Android Open Source Project28527d22009-03-03 19:31:44 -08001565
1566 // Connectivity state changed:
1567 // [31-13] Reserved for future use
Robert Greenwalt0659da32009-07-16 17:21:39 -07001568 // [12-9] Network subtype (for mobile network, as defined
1569 // by TelephonyManager)
1570 // [8-3] Detailed state ordinal (as defined by
1571 // NetworkInfo.DetailedState)
The Android Open Source Project28527d22009-03-03 19:31:44 -08001572 // [2-0] Network type (as defined by ConnectivityManager)
1573 int eventLogParam = (info.getType() & 0x7) |
1574 ((info.getDetailedState().ordinal() & 0x3f) << 3) |
1575 (info.getSubtype() << 9);
Doug Zongker2fc96232009-12-04 10:31:43 -08001576 EventLog.writeEvent(EventLogTags.CONNECTIVITY_STATE_CHANGED,
Robert Greenwalt0659da32009-07-16 17:21:39 -07001577 eventLogParam);
1578
1579 if (info.getDetailedState() ==
1580 NetworkInfo.DetailedState.FAILED) {
The Android Open Source Project28527d22009-03-03 19:31:44 -08001581 handleConnectionFailure(info);
Robert Greenwalt12c44552009-12-07 11:33:18 -08001582 } else if (state == NetworkInfo.State.DISCONNECTED) {
The Android Open Source Project28527d22009-03-03 19:31:44 -08001583 handleDisconnect(info);
Robert Greenwalt12c44552009-12-07 11:33:18 -08001584 } else if (state == NetworkInfo.State.SUSPENDED) {
The Android Open Source Project28527d22009-03-03 19:31:44 -08001585 // TODO: need to think this over.
Robert Greenwalt0659da32009-07-16 17:21:39 -07001586 // the logic here is, handle SUSPENDED the same as
1587 // DISCONNECTED. The only difference being we are
1588 // broadcasting an intent with NetworkInfo that's
1589 // suspended. This allows the applications an
1590 // opportunity to handle DISCONNECTED and SUSPENDED
1591 // differently, or not.
The Android Open Source Project28527d22009-03-03 19:31:44 -08001592 handleDisconnect(info);
Robert Greenwalt12c44552009-12-07 11:33:18 -08001593 } else if (state == NetworkInfo.State.CONNECTED) {
The Android Open Source Project28527d22009-03-03 19:31:44 -08001594 handleConnect(info);
1595 }
The Android Open Source Project28527d22009-03-03 19:31:44 -08001596 break;
The Android Open Source Project28527d22009-03-03 19:31:44 -08001597 case NetworkStateTracker.EVENT_CONFIGURATION_CHANGED:
Robert Greenwalt3afbead2010-07-23 15:46:26 -07001598 // TODO - make this handle ip/proxy/gateway/dns changes
1599 info = (NetworkInfo) msg.obj;
1600 type = info.getType();
1601 handleDnsConfigurationChange(type);
The Android Open Source Project28527d22009-03-03 19:31:44 -08001602 break;
Robert Greenwalt2034b912009-08-12 16:08:25 -07001603 case NetworkStateTracker.EVENT_RESTORE_DEFAULT_NETWORK:
Robert Greenwaltaffc3a12009-09-27 17:27:04 -07001604 FeatureUser u = (FeatureUser)msg.obj;
1605 u.expire();
Robert Greenwalt2034b912009-08-12 16:08:25 -07001606 break;
Robert Greenwalt93dc1042010-06-15 12:19:37 -07001607 case NetworkStateTracker.EVENT_CLEAR_NET_TRANSITION_WAKELOCK:
1608 String causedBy = null;
1609 synchronized (ConnectivityService.this) {
1610 if (msg.arg1 == mNetTransitionWakeLockSerialNumber &&
1611 mNetTransitionWakeLock.isHeld()) {
1612 mNetTransitionWakeLock.release();
1613 causedBy = mNetTransitionWakeLockCausedBy;
1614 }
1615 }
1616 if (causedBy != null) {
1617 Slog.d(TAG, "NetTransition Wakelock for " +
1618 causedBy + " released by timeout");
1619 }
1620 break;
The Android Open Source Project28527d22009-03-03 19:31:44 -08001621 }
1622 }
1623 }
Robert Greenwalt0c4828c2010-01-26 11:40:34 -08001624
1625 // javadoc from interface
Robert Greenwalt4283ded2010-03-02 17:25:02 -08001626 public int tether(String iface) {
Robert Greenwalt0c4828c2010-01-26 11:40:34 -08001627 enforceTetherChangePermission();
Robert Greenwalt4283ded2010-03-02 17:25:02 -08001628
1629 if (isTetheringSupported()) {
1630 return mTethering.tether(iface);
1631 } else {
1632 return ConnectivityManager.TETHER_ERROR_UNSUPPORTED;
1633 }
Robert Greenwalt0c4828c2010-01-26 11:40:34 -08001634 }
1635
1636 // javadoc from interface
Robert Greenwalt4283ded2010-03-02 17:25:02 -08001637 public int untether(String iface) {
Robert Greenwalt0c4828c2010-01-26 11:40:34 -08001638 enforceTetherChangePermission();
Robert Greenwalt4283ded2010-03-02 17:25:02 -08001639
1640 if (isTetheringSupported()) {
1641 return mTethering.untether(iface);
1642 } else {
1643 return ConnectivityManager.TETHER_ERROR_UNSUPPORTED;
1644 }
1645 }
1646
1647 // javadoc from interface
1648 public int getLastTetherError(String iface) {
1649 enforceTetherAccessPermission();
1650
1651 if (isTetheringSupported()) {
1652 return mTethering.getLastTetherError(iface);
1653 } else {
1654 return ConnectivityManager.TETHER_ERROR_UNSUPPORTED;
1655 }
Robert Greenwalt8e87f122010-02-11 18:18:40 -08001656 }
1657
1658 // TODO - proper iface API for selection by property, inspection, etc
1659 public String[] getTetherableUsbRegexs() {
1660 enforceTetherAccessPermission();
1661 if (isTetheringSupported()) {
1662 return mTethering.getTetherableUsbRegexs();
1663 } else {
1664 return new String[0];
1665 }
1666 }
1667
1668 public String[] getTetherableWifiRegexs() {
1669 enforceTetherAccessPermission();
1670 if (isTetheringSupported()) {
1671 return mTethering.getTetherableWifiRegexs();
1672 } else {
1673 return new String[0];
1674 }
Robert Greenwalt0c4828c2010-01-26 11:40:34 -08001675 }
1676
Danica Chang96567052010-08-11 14:54:43 -07001677 public String[] getTetherableBluetoothRegexs() {
1678 enforceTetherAccessPermission();
1679 if (isTetheringSupported()) {
1680 return mTethering.getTetherableBluetoothRegexs();
1681 } else {
1682 return new String[0];
1683 }
1684 }
1685
Robert Greenwalt0c4828c2010-01-26 11:40:34 -08001686 // TODO - move iface listing, queries, etc to new module
1687 // javadoc from interface
1688 public String[] getTetherableIfaces() {
Robert Greenwalt8e87f122010-02-11 18:18:40 -08001689 enforceTetherAccessPermission();
Robert Greenwalt0c4828c2010-01-26 11:40:34 -08001690 return mTethering.getTetherableIfaces();
1691 }
1692
1693 public String[] getTetheredIfaces() {
Robert Greenwalt8e87f122010-02-11 18:18:40 -08001694 enforceTetherAccessPermission();
Robert Greenwalt0c4828c2010-01-26 11:40:34 -08001695 return mTethering.getTetheredIfaces();
1696 }
Robert Greenwalt8e87f122010-02-11 18:18:40 -08001697
Robert Greenwalt4283ded2010-03-02 17:25:02 -08001698 public String[] getTetheringErroredIfaces() {
1699 enforceTetherAccessPermission();
1700 return mTethering.getErroredIfaces();
1701 }
1702
Robert Greenwalt8e87f122010-02-11 18:18:40 -08001703 // if ro.tether.denied = true we default to no tethering
1704 // gservices could set the secure setting to 1 though to enable it on a build where it
1705 // had previously been turned off.
1706 public boolean isTetheringSupported() {
1707 enforceTetherAccessPermission();
1708 int defaultVal = (SystemProperties.get("ro.tether.denied").equals("true") ? 0 : 1);
Robert Greenwaltf1b66e12010-02-25 12:29:30 -08001709 boolean tetherEnabledInSettings = (Settings.Secure.getInt(mContext.getContentResolver(),
1710 Settings.Secure.TETHER_SUPPORTED, defaultVal) != 0);
1711 return tetherEnabledInSettings && mTetheringConfigValid;
Robert Greenwalt8e87f122010-02-11 18:18:40 -08001712 }
Robert Greenwalt93dc1042010-06-15 12:19:37 -07001713
1714 // An API NetworkStateTrackers can call when they lose their network.
1715 // This will automatically be cleared after X seconds or a network becomes CONNECTED,
1716 // whichever happens first. The timer is started by the first caller and not
1717 // restarted by subsequent callers.
1718 public void requestNetworkTransitionWakelock(String forWhom) {
1719 enforceConnectivityInternalPermission();
1720 synchronized (this) {
1721 if (mNetTransitionWakeLock.isHeld()) return;
1722 mNetTransitionWakeLockSerialNumber++;
1723 mNetTransitionWakeLock.acquire();
1724 mNetTransitionWakeLockCausedBy = forWhom;
1725 }
1726 mHandler.sendMessageDelayed(mHandler.obtainMessage(
1727 NetworkStateTracker.EVENT_CLEAR_NET_TRANSITION_WAKELOCK,
1728 mNetTransitionWakeLockSerialNumber, 0),
1729 mNetTransitionWakeLockTimeout);
1730 return;
1731 }
The Android Open Source Project28527d22009-03-03 19:31:44 -08001732}