blob: 98394d07dff786018506a30deae8158d3b34c29c [file] [log] [blame]
The Android Open Source Project28527d22009-03-03 19:31:44 -08001/*
2 * Copyright (C) 2008 The Android Open Source Project
3 *
4 * Licensed under the Apache License, Version 2.0 (the "License");
5 * you may not use this file except in compliance with the License.
6 * You may obtain a copy of the License at
7 *
8 * http://www.apache.org/licenses/LICENSE-2.0
9 *
10 * Unless required by applicable law or agreed to in writing, software
11 * distributed under the License is distributed on an "AS IS" BASIS,
12 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13 * See the License for the specific language governing permissions and
14 * limitations under the License.
15 */
16
17package com.android.server;
18
19import android.app.Notification;
20import android.app.NotificationManager;
21import android.content.ContentResolver;
22import android.content.Context;
23import android.content.Intent;
24import android.content.pm.PackageManager;
25import android.net.ConnectivityManager;
26import android.net.IConnectivityManager;
27import android.net.MobileDataStateTracker;
28import android.net.NetworkInfo;
Robert Greenwalt1f1bcfe2010-08-30 10:56:47 -070029import android.net.LinkProperties;
The Android Open Source Project28527d22009-03-03 19:31:44 -080030import android.net.NetworkStateTracker;
Robert Greenwalt7fe44cb2010-08-27 09:24:29 -070031import android.net.NetworkUtils;
The Android Open Source Project28527d22009-03-03 19:31:44 -080032import android.net.wifi.WifiStateTracker;
Irfan Sheriff7f132d92010-06-09 15:39:36 -070033import android.net.NetworkUtils;
The Android Open Source Project28527d22009-03-03 19:31:44 -080034import android.os.Binder;
35import android.os.Handler;
Robert Greenwalt2034b912009-08-12 16:08:25 -070036import android.os.IBinder;
The Android Open Source Project28527d22009-03-03 19:31:44 -080037import android.os.Looper;
38import android.os.Message;
Robert Greenwalt93dc1042010-06-15 12:19:37 -070039import android.os.PowerManager;
Robert Greenwalt2034b912009-08-12 16:08:25 -070040import android.os.RemoteException;
The Android Open Source Project28527d22009-03-03 19:31:44 -080041import android.os.ServiceManager;
42import android.os.SystemProperties;
43import android.provider.Settings;
Robert Greenwalt2034b912009-08-12 16:08:25 -070044import android.text.TextUtils;
The Android Open Source Project28527d22009-03-03 19:31:44 -080045import android.util.EventLog;
Joe Onoratoc2386bb2010-02-26 18:56:32 -080046import android.util.Slog;
The Android Open Source Project28527d22009-03-03 19:31:44 -080047
Robert Greenwalt2034b912009-08-12 16:08:25 -070048import com.android.internal.telephony.Phone;
49
Robert Greenwalt0c4828c2010-01-26 11:40:34 -080050import com.android.server.connectivity.Tethering;
51
The Android Open Source Project28527d22009-03-03 19:31:44 -080052import java.io.FileDescriptor;
Irfan Sheriff7f132d92010-06-09 15:39:36 -070053import java.io.FileWriter;
54import java.io.IOException;
The Android Open Source Project28527d22009-03-03 19:31:44 -080055import java.io.PrintWriter;
Robert Greenwalta7dfbd32010-06-15 15:43:39 -070056import java.net.InetAddress;
57import java.net.UnknownHostException;
Robert Greenwalt2034b912009-08-12 16:08:25 -070058import java.util.ArrayList;
Robert Greenwalta7dfbd32010-06-15 15:43:39 -070059import java.util.Collection;
Robert Greenwalt2034b912009-08-12 16:08:25 -070060import java.util.List;
Robert Greenwalt7fe44cb2010-08-27 09:24:29 -070061import java.net.InetAddress;
62import java.net.UnknownHostException;
The Android Open Source Project28527d22009-03-03 19:31:44 -080063
64/**
65 * @hide
66 */
67public class ConnectivityService extends IConnectivityManager.Stub {
68
Robert Greenwalta25fd712009-10-06 14:12:53 -070069 private static final boolean DBG = true;
The Android Open Source Project28527d22009-03-03 19:31:44 -080070 private static final String TAG = "ConnectivityService";
71
Robert Greenwalt2034b912009-08-12 16:08:25 -070072 // how long to wait before switching back to a radio's default network
73 private static final int RESTORE_DEFAULT_NETWORK_DELAY = 1 * 60 * 1000;
74 // system property that can override the above value
75 private static final String NETWORK_RESTORE_DELAY_PROP_NAME =
76 "android.telephony.apn-restore";
77
Robert Greenwalt0c4828c2010-01-26 11:40:34 -080078 private Tethering mTethering;
Robert Greenwaltf1b66e12010-02-25 12:29:30 -080079 private boolean mTetheringConfigValid = false;
Robert Greenwalt0c4828c2010-01-26 11:40:34 -080080
The Android Open Source Project28527d22009-03-03 19:31:44 -080081 /**
82 * Sometimes we want to refer to the individual network state
83 * trackers separately, and sometimes we just want to treat them
84 * abstractly.
85 */
86 private NetworkStateTracker mNetTrackers[];
Robert Greenwalt2034b912009-08-12 16:08:25 -070087
88 /**
89 * A per Net list of the PID's that requested access to the net
90 * used both as a refcount and for per-PID DNS selection
91 */
92 private List mNetRequestersPids[];
93
Irfan Sheriff653e2a22010-06-07 09:03:04 -070094 private WifiWatchdogService mWifiWatchdogService;
95
Robert Greenwalt2034b912009-08-12 16:08:25 -070096 // priority order of the nettrackers
97 // (excluding dynamically set mNetworkPreference)
98 // TODO - move mNetworkTypePreference into this
99 private int[] mPriorityList;
100
The Android Open Source Project28527d22009-03-03 19:31:44 -0800101 private Context mContext;
102 private int mNetworkPreference;
Robert Greenwalt2034b912009-08-12 16:08:25 -0700103 private int mActiveDefaultNetwork = -1;
The Android Open Source Project28527d22009-03-03 19:31:44 -0800104
105 private int mNumDnsEntries;
The Android Open Source Project28527d22009-03-03 19:31:44 -0800106
107 private boolean mTestMode;
Wink Saville7323ea12010-09-01 14:48:29 -0700108 private static volatile ConnectivityService sServiceInstance;
The Android Open Source Project28527d22009-03-03 19:31:44 -0800109
Robert Greenwalt2034b912009-08-12 16:08:25 -0700110 private Handler mHandler;
111
112 // list of DeathRecipients used to make sure features are turned off when
113 // a process dies
114 private List mFeatureUsers;
115
Mike Lockwoodfde2b762009-08-14 14:18:49 -0400116 private boolean mSystemReady;
Dianne Hackborna417ff82009-12-08 19:45:14 -0800117 private Intent mInitialBroadcast;
Mike Lockwoodfde2b762009-08-14 14:18:49 -0400118
Robert Greenwalt93dc1042010-06-15 12:19:37 -0700119 private PowerManager.WakeLock mNetTransitionWakeLock;
120 private String mNetTransitionWakeLockCausedBy = "";
121 private int mNetTransitionWakeLockSerialNumber;
122 private int mNetTransitionWakeLockTimeout;
123
Robert Greenwalt12c44552009-12-07 11:33:18 -0800124 private static class NetworkAttributes {
Robert Greenwalt2034b912009-08-12 16:08:25 -0700125 /**
126 * Class for holding settings read from resources.
127 */
128 public String mName;
129 public int mType;
130 public int mRadio;
131 public int mPriority;
Robert Greenwalt12c44552009-12-07 11:33:18 -0800132 public NetworkInfo.State mLastState;
Robert Greenwalt2034b912009-08-12 16:08:25 -0700133 public NetworkAttributes(String init) {
134 String fragments[] = init.split(",");
135 mName = fragments[0].toLowerCase();
Robert Greenwaltec05b3c2009-10-30 14:17:42 -0700136 mType = Integer.parseInt(fragments[1]);
137 mRadio = Integer.parseInt(fragments[2]);
138 mPriority = Integer.parseInt(fragments[3]);
Robert Greenwalt12c44552009-12-07 11:33:18 -0800139 mLastState = NetworkInfo.State.UNKNOWN;
Robert Greenwalt2034b912009-08-12 16:08:25 -0700140 }
141 public boolean isDefault() {
142 return (mType == mRadio);
143 }
144 }
145 NetworkAttributes[] mNetAttributes;
Robert Greenwaltec05b3c2009-10-30 14:17:42 -0700146 int mNetworksDefined;
Robert Greenwalt2034b912009-08-12 16:08:25 -0700147
Robert Greenwalt12c44552009-12-07 11:33:18 -0800148 private static class RadioAttributes {
Robert Greenwalt2034b912009-08-12 16:08:25 -0700149 public int mSimultaneity;
150 public int mType;
151 public RadioAttributes(String init) {
152 String fragments[] = init.split(",");
Robert Greenwaltec05b3c2009-10-30 14:17:42 -0700153 mType = Integer.parseInt(fragments[0]);
154 mSimultaneity = Integer.parseInt(fragments[1]);
Robert Greenwalt2034b912009-08-12 16:08:25 -0700155 }
156 }
157 RadioAttributes[] mRadioAttributes;
158
The Android Open Source Project28527d22009-03-03 19:31:44 -0800159 private static class ConnectivityThread extends Thread {
160 private Context mContext;
Robert Greenwalt0659da32009-07-16 17:21:39 -0700161
The Android Open Source Project28527d22009-03-03 19:31:44 -0800162 private ConnectivityThread(Context context) {
163 super("ConnectivityThread");
164 mContext = context;
165 }
166
167 @Override
168 public void run() {
169 Looper.prepare();
170 synchronized (this) {
171 sServiceInstance = new ConnectivityService(mContext);
172 notifyAll();
173 }
174 Looper.loop();
175 }
Robert Greenwalt0659da32009-07-16 17:21:39 -0700176
Wink Saville7323ea12010-09-01 14:48:29 -0700177 public static synchronized ConnectivityService getServiceInstance(Context context) {
178 if (sServiceInstance == null) {
179 ConnectivityThread thread = new ConnectivityThread(context);
180 thread.start();
The Android Open Source Project28527d22009-03-03 19:31:44 -0800181 while (sServiceInstance == null) {
182 try {
183 // Wait until sServiceInstance has been initialized.
184 thread.wait();
185 } catch (InterruptedException ignore) {
Joe Onoratoc2386bb2010-02-26 18:56:32 -0800186 Slog.e(TAG,
Robert Greenwalt0659da32009-07-16 17:21:39 -0700187 "Unexpected InterruptedException while waiting"+
188 " for ConnectivityService thread");
The Android Open Source Project28527d22009-03-03 19:31:44 -0800189 }
190 }
191 }
Robert Greenwalt0659da32009-07-16 17:21:39 -0700192
The Android Open Source Project28527d22009-03-03 19:31:44 -0800193 return sServiceInstance;
194 }
195 }
Robert Greenwalt0659da32009-07-16 17:21:39 -0700196
The Android Open Source Project28527d22009-03-03 19:31:44 -0800197 public static ConnectivityService getInstance(Context context) {
198 return ConnectivityThread.getServiceInstance(context);
199 }
Robert Greenwalt0659da32009-07-16 17:21:39 -0700200
The Android Open Source Project28527d22009-03-03 19:31:44 -0800201 private ConnectivityService(Context context) {
Joe Onoratoc2386bb2010-02-26 18:56:32 -0800202 if (DBG) Slog.v(TAG, "ConnectivityService starting up");
Robert Greenwaltd48f8ee2010-01-14 17:47:58 -0800203
204 // setup our unique device name
205 String id = Settings.Secure.getString(context.getContentResolver(),
206 Settings.Secure.ANDROID_ID);
207 if (id != null && id.length() > 0) {
208 String name = new String("android_").concat(id);
209 SystemProperties.set("net.hostname", name);
210 }
211
The Android Open Source Project28527d22009-03-03 19:31:44 -0800212 mContext = context;
Robert Greenwalt93dc1042010-06-15 12:19:37 -0700213
214 PowerManager powerManager = (PowerManager)mContext.getSystemService(Context.POWER_SERVICE);
215 mNetTransitionWakeLock = powerManager.newWakeLock(PowerManager.PARTIAL_WAKE_LOCK, TAG);
216 mNetTransitionWakeLockTimeout = mContext.getResources().getInteger(
217 com.android.internal.R.integer.config_networkTransitionTimeout);
218
Robert Greenwalt2034b912009-08-12 16:08:25 -0700219 mNetTrackers = new NetworkStateTracker[
220 ConnectivityManager.MAX_NETWORK_TYPE+1];
221 mHandler = new MyHandler();
Robert Greenwalt0659da32009-07-16 17:21:39 -0700222
The Android Open Source Project28527d22009-03-03 19:31:44 -0800223 mNetworkPreference = getPersistedNetworkPreference();
Robert Greenwalt0659da32009-07-16 17:21:39 -0700224
Robert Greenwaltec05b3c2009-10-30 14:17:42 -0700225 mRadioAttributes = new RadioAttributes[ConnectivityManager.MAX_RADIO_TYPE+1];
226 mNetAttributes = new NetworkAttributes[ConnectivityManager.MAX_NETWORK_TYPE+1];
227
Robert Greenwalt2034b912009-08-12 16:08:25 -0700228 // Load device network attributes from resources
Robert Greenwalt2034b912009-08-12 16:08:25 -0700229 String[] raStrings = context.getResources().getStringArray(
230 com.android.internal.R.array.radioAttributes);
Robert Greenwaltec05b3c2009-10-30 14:17:42 -0700231 for (String raString : raStrings) {
232 RadioAttributes r = new RadioAttributes(raString);
233 if (r.mType > ConnectivityManager.MAX_RADIO_TYPE) {
Joe Onoratoc2386bb2010-02-26 18:56:32 -0800234 Slog.e(TAG, "Error in radioAttributes - ignoring attempt to define type " + r.mType);
Robert Greenwaltec05b3c2009-10-30 14:17:42 -0700235 continue;
236 }
237 if (mRadioAttributes[r.mType] != null) {
Joe Onoratoc2386bb2010-02-26 18:56:32 -0800238 Slog.e(TAG, "Error in radioAttributes - ignoring attempt to redefine type " +
Robert Greenwaltec05b3c2009-10-30 14:17:42 -0700239 r.mType);
240 continue;
241 }
Robert Greenwalt2034b912009-08-12 16:08:25 -0700242 mRadioAttributes[r.mType] = r;
243 }
244
Robert Greenwaltec05b3c2009-10-30 14:17:42 -0700245 String[] naStrings = context.getResources().getStringArray(
246 com.android.internal.R.array.networkAttributes);
247 for (String naString : naStrings) {
248 try {
249 NetworkAttributes n = new NetworkAttributes(naString);
250 if (n.mType > ConnectivityManager.MAX_NETWORK_TYPE) {
Joe Onoratoc2386bb2010-02-26 18:56:32 -0800251 Slog.e(TAG, "Error in networkAttributes - ignoring attempt to define type " +
Robert Greenwaltec05b3c2009-10-30 14:17:42 -0700252 n.mType);
253 continue;
Robert Greenwalt2034b912009-08-12 16:08:25 -0700254 }
Robert Greenwaltec05b3c2009-10-30 14:17:42 -0700255 if (mNetAttributes[n.mType] != null) {
Joe Onoratoc2386bb2010-02-26 18:56:32 -0800256 Slog.e(TAG, "Error in networkAttributes - ignoring attempt to redefine type " +
Robert Greenwaltec05b3c2009-10-30 14:17:42 -0700257 n.mType);
258 continue;
259 }
260 if (mRadioAttributes[n.mRadio] == null) {
Joe Onoratoc2386bb2010-02-26 18:56:32 -0800261 Slog.e(TAG, "Error in networkAttributes - ignoring attempt to use undefined " +
Robert Greenwaltec05b3c2009-10-30 14:17:42 -0700262 "radio " + n.mRadio + " in network type " + n.mType);
263 continue;
264 }
265 mNetAttributes[n.mType] = n;
266 mNetworksDefined++;
267 } catch(Exception e) {
268 // ignore it - leave the entry null
Robert Greenwalt2034b912009-08-12 16:08:25 -0700269 }
270 }
271
Robert Greenwaltec05b3c2009-10-30 14:17:42 -0700272 // high priority first
273 mPriorityList = new int[mNetworksDefined];
274 {
275 int insertionPoint = mNetworksDefined-1;
276 int currentLowest = 0;
277 int nextLowest = 0;
278 while (insertionPoint > -1) {
279 for (NetworkAttributes na : mNetAttributes) {
280 if (na == null) continue;
281 if (na.mPriority < currentLowest) continue;
282 if (na.mPriority > currentLowest) {
283 if (na.mPriority < nextLowest || nextLowest == 0) {
284 nextLowest = na.mPriority;
285 }
286 continue;
287 }
288 mPriorityList[insertionPoint--] = na.mType;
289 }
290 currentLowest = nextLowest;
291 nextLowest = 0;
292 }
293 }
294
295 mNetRequestersPids = new ArrayList[ConnectivityManager.MAX_NETWORK_TYPE+1];
296 for (int i : mPriorityList) {
Robert Greenwalt2034b912009-08-12 16:08:25 -0700297 mNetRequestersPids[i] = new ArrayList();
298 }
299
300 mFeatureUsers = new ArrayList();
301
Robert Greenwaltec05b3c2009-10-30 14:17:42 -0700302 mNumDnsEntries = 0;
303
304 mTestMode = SystemProperties.get("cm.test.mode").equals("true")
305 && SystemProperties.get("ro.build.type").equals("eng");
The Android Open Source Project28527d22009-03-03 19:31:44 -0800306 /*
307 * Create the network state trackers for Wi-Fi and mobile
308 * data. Maybe this could be done with a factory class,
309 * but it's not clear that it's worth it, given that
310 * the number of different network types is not going
311 * to change very often.
312 */
Robert Greenwalt1b21f6c2010-02-23 18:58:05 -0800313 boolean noMobileData = !getMobileDataEnabled();
Robert Greenwaltec05b3c2009-10-30 14:17:42 -0700314 for (int netType : mPriorityList) {
315 switch (mNetAttributes[netType].mRadio) {
316 case ConnectivityManager.TYPE_WIFI:
Joe Onoratoc2386bb2010-02-26 18:56:32 -0800317 if (DBG) Slog.v(TAG, "Starting Wifi Service.");
Wink Saville7fabfa22010-08-13 16:11:42 -0700318 WifiStateTracker wst = new WifiStateTracker();
Irfan Sheriff25be0762010-07-28 09:35:20 -0700319 WifiService wifiService = new WifiService(context);
Robert Greenwaltec05b3c2009-10-30 14:17:42 -0700320 ServiceManager.addService(Context.WIFI_SERVICE, wifiService);
Irfan Sheriff25be0762010-07-28 09:35:20 -0700321 wifiService.checkAndStartWifi();
Robert Greenwaltec05b3c2009-10-30 14:17:42 -0700322 mNetTrackers[ConnectivityManager.TYPE_WIFI] = wst;
Wink Saville7fabfa22010-08-13 16:11:42 -0700323 wst.startMonitoring(context, mHandler);
The Android Open Source Project28527d22009-03-03 19:31:44 -0800324
Irfan Sheriff653e2a22010-06-07 09:03:04 -0700325 //TODO: as part of WWS refactor, create only when needed
Irfan Sheriff25be0762010-07-28 09:35:20 -0700326 mWifiWatchdogService = new WifiWatchdogService(context);
Irfan Sheriff653e2a22010-06-07 09:03:04 -0700327
Robert Greenwaltec05b3c2009-10-30 14:17:42 -0700328 break;
329 case ConnectivityManager.TYPE_MOBILE:
Wink Saville7fabfa22010-08-13 16:11:42 -0700330 mNetTrackers[netType] = new MobileDataStateTracker(netType,
331 mNetAttributes[netType].mName);
332 mNetTrackers[netType].startMonitoring(context, mHandler);
Robert Greenwalt1b21f6c2010-02-23 18:58:05 -0800333 if (noMobileData) {
Joe Onoratoc2386bb2010-02-26 18:56:32 -0800334 if (DBG) Slog.d(TAG, "tearing down Mobile networks due to setting");
Robert Greenwalt1b21f6c2010-02-23 18:58:05 -0800335 mNetTrackers[netType].teardown();
336 }
Robert Greenwaltec05b3c2009-10-30 14:17:42 -0700337 break;
338 default:
Joe Onoratoc2386bb2010-02-26 18:56:32 -0800339 Slog.e(TAG, "Trying to create a DataStateTracker for an unknown radio type " +
Robert Greenwaltec05b3c2009-10-30 14:17:42 -0700340 mNetAttributes[netType].mRadio);
341 continue;
342 }
343 }
Robert Greenwalt0c4828c2010-01-26 11:40:34 -0800344
Robert Greenwaltc0b6c602010-03-11 15:03:08 -0800345 mTethering = new Tethering(mContext, mHandler.getLooper());
Robert Greenwaltf1b66e12010-02-25 12:29:30 -0800346 mTetheringConfigValid = (((mNetTrackers[ConnectivityManager.TYPE_MOBILE_DUN] != null) ||
347 !mTethering.isDunRequired()) &&
348 (mTethering.getTetherableUsbRegexs().length != 0 ||
Danica Chang96567052010-08-11 14:54:43 -0700349 mTethering.getTetherableWifiRegexs().length != 0 ||
350 mTethering.getTetherableBluetoothRegexs().length != 0) &&
Robert Greenwaltf1b66e12010-02-25 12:29:30 -0800351 mTethering.getUpstreamIfaceRegexs().length != 0);
352
The Android Open Source Project28527d22009-03-03 19:31:44 -0800353 }
354
Robert Greenwaltec05b3c2009-10-30 14:17:42 -0700355
The Android Open Source Project28527d22009-03-03 19:31:44 -0800356 /**
Robert Greenwalt0659da32009-07-16 17:21:39 -0700357 * Sets the preferred network.
The Android Open Source Project28527d22009-03-03 19:31:44 -0800358 * @param preference the new preference
359 */
360 public synchronized void setNetworkPreference(int preference) {
361 enforceChangePermission();
Robert Greenwalt2034b912009-08-12 16:08:25 -0700362 if (ConnectivityManager.isNetworkTypeValid(preference) &&
Robert Greenwaltec05b3c2009-10-30 14:17:42 -0700363 mNetAttributes[preference] != null &&
Robert Greenwalt2034b912009-08-12 16:08:25 -0700364 mNetAttributes[preference].isDefault()) {
The Android Open Source Project28527d22009-03-03 19:31:44 -0800365 if (mNetworkPreference != preference) {
366 persistNetworkPreference(preference);
367 mNetworkPreference = preference;
368 enforcePreference();
369 }
370 }
371 }
372
373 public int getNetworkPreference() {
374 enforceAccessPermission();
375 return mNetworkPreference;
376 }
377
378 private void persistNetworkPreference(int networkPreference) {
379 final ContentResolver cr = mContext.getContentResolver();
Robert Greenwalt0659da32009-07-16 17:21:39 -0700380 Settings.Secure.putInt(cr, Settings.Secure.NETWORK_PREFERENCE,
381 networkPreference);
The Android Open Source Project28527d22009-03-03 19:31:44 -0800382 }
Robert Greenwalt0659da32009-07-16 17:21:39 -0700383
The Android Open Source Project28527d22009-03-03 19:31:44 -0800384 private int getPersistedNetworkPreference() {
385 final ContentResolver cr = mContext.getContentResolver();
386
387 final int networkPrefSetting = Settings.Secure
388 .getInt(cr, Settings.Secure.NETWORK_PREFERENCE, -1);
389 if (networkPrefSetting != -1) {
390 return networkPrefSetting;
391 }
392
393 return ConnectivityManager.DEFAULT_NETWORK_PREFERENCE;
394 }
Robert Greenwalt0659da32009-07-16 17:21:39 -0700395
The Android Open Source Project28527d22009-03-03 19:31:44 -0800396 /**
Robert Greenwalt0659da32009-07-16 17:21:39 -0700397 * Make the state of network connectivity conform to the preference settings
The Android Open Source Project28527d22009-03-03 19:31:44 -0800398 * In this method, we only tear down a non-preferred network. Establishing
399 * a connection to the preferred network is taken care of when we handle
400 * the disconnect event from the non-preferred network
401 * (see {@link #handleDisconnect(NetworkInfo)}).
402 */
403 private void enforcePreference() {
Robert Greenwalt2034b912009-08-12 16:08:25 -0700404 if (mNetTrackers[mNetworkPreference].getNetworkInfo().isConnected())
The Android Open Source Project28527d22009-03-03 19:31:44 -0800405 return;
406
Robert Greenwalt2034b912009-08-12 16:08:25 -0700407 if (!mNetTrackers[mNetworkPreference].isAvailable())
408 return;
The Android Open Source Project28527d22009-03-03 19:31:44 -0800409
Robert Greenwalt2034b912009-08-12 16:08:25 -0700410 for (int t=0; t <= ConnectivityManager.MAX_RADIO_TYPE; t++) {
Robert Greenwaltec05b3c2009-10-30 14:17:42 -0700411 if (t != mNetworkPreference && mNetTrackers[t] != null &&
Robert Greenwalt2034b912009-08-12 16:08:25 -0700412 mNetTrackers[t].getNetworkInfo().isConnected()) {
Robert Greenwaltf3f045b2009-08-20 15:25:14 -0700413 if (DBG) {
Joe Onoratoc2386bb2010-02-26 18:56:32 -0800414 Slog.d(TAG, "tearing down " +
Robert Greenwaltf3f045b2009-08-20 15:25:14 -0700415 mNetTrackers[t].getNetworkInfo() +
416 " in enforcePreference");
417 }
Robert Greenwalt2034b912009-08-12 16:08:25 -0700418 teardown(mNetTrackers[t]);
The Android Open Source Project28527d22009-03-03 19:31:44 -0800419 }
420 }
421 }
422
423 private boolean teardown(NetworkStateTracker netTracker) {
424 if (netTracker.teardown()) {
425 netTracker.setTeardownRequested(true);
426 return true;
427 } else {
428 return false;
429 }
430 }
431
432 /**
433 * Return NetworkInfo for the active (i.e., connected) network interface.
434 * It is assumed that at most one network is active at a time. If more
435 * than one is active, it is indeterminate which will be returned.
Robert Greenwalt0659da32009-07-16 17:21:39 -0700436 * @return the info for the active network, or {@code null} if none is
437 * active
The Android Open Source Project28527d22009-03-03 19:31:44 -0800438 */
439 public NetworkInfo getActiveNetworkInfo() {
440 enforceAccessPermission();
Robert Greenwalt2034b912009-08-12 16:08:25 -0700441 for (int type=0; type <= ConnectivityManager.MAX_NETWORK_TYPE; type++) {
Robert Greenwaltec05b3c2009-10-30 14:17:42 -0700442 if (mNetAttributes[type] == null || !mNetAttributes[type].isDefault()) {
Robert Greenwalt2034b912009-08-12 16:08:25 -0700443 continue;
444 }
445 NetworkStateTracker t = mNetTrackers[type];
The Android Open Source Project28527d22009-03-03 19:31:44 -0800446 NetworkInfo info = t.getNetworkInfo();
447 if (info.isConnected()) {
Joe Onoratoc2386bb2010-02-26 18:56:32 -0800448 if (DBG && type != mActiveDefaultNetwork) Slog.e(TAG,
Robert Greenwalt2034b912009-08-12 16:08:25 -0700449 "connected default network is not " +
450 "mActiveDefaultNetwork!");
The Android Open Source Project28527d22009-03-03 19:31:44 -0800451 return info;
452 }
453 }
454 return null;
455 }
456
457 public NetworkInfo getNetworkInfo(int networkType) {
458 enforceAccessPermission();
459 if (ConnectivityManager.isNetworkTypeValid(networkType)) {
460 NetworkStateTracker t = mNetTrackers[networkType];
461 if (t != null)
462 return t.getNetworkInfo();
463 }
464 return null;
465 }
466
467 public NetworkInfo[] getAllNetworkInfo() {
468 enforceAccessPermission();
Robert Greenwaltec05b3c2009-10-30 14:17:42 -0700469 NetworkInfo[] result = new NetworkInfo[mNetworksDefined];
The Android Open Source Project28527d22009-03-03 19:31:44 -0800470 int i = 0;
471 for (NetworkStateTracker t : mNetTrackers) {
Robert Greenwaltec05b3c2009-10-30 14:17:42 -0700472 if(t != null) result[i++] = t.getNetworkInfo();
The Android Open Source Project28527d22009-03-03 19:31:44 -0800473 }
474 return result;
475 }
476
477 public boolean setRadios(boolean turnOn) {
478 boolean result = true;
479 enforceChangePermission();
480 for (NetworkStateTracker t : mNetTrackers) {
Robert Greenwaltec05b3c2009-10-30 14:17:42 -0700481 if (t != null) result = t.setRadio(turnOn) && result;
The Android Open Source Project28527d22009-03-03 19:31:44 -0800482 }
483 return result;
484 }
485
486 public boolean setRadio(int netType, boolean turnOn) {
487 enforceChangePermission();
488 if (!ConnectivityManager.isNetworkTypeValid(netType)) {
489 return false;
490 }
491 NetworkStateTracker tracker = mNetTrackers[netType];
492 return tracker != null && tracker.setRadio(turnOn);
493 }
494
Robert Greenwaltaffc3a12009-09-27 17:27:04 -0700495 /**
496 * Used to notice when the calling process dies so we can self-expire
497 *
498 * Also used to know if the process has cleaned up after itself when
499 * our auto-expire timer goes off. The timer has a link to an object.
500 *
501 */
Robert Greenwalt2034b912009-08-12 16:08:25 -0700502 private class FeatureUser implements IBinder.DeathRecipient {
503 int mNetworkType;
504 String mFeature;
505 IBinder mBinder;
506 int mPid;
507 int mUid;
Robert Greenwalt3eeb6032009-12-21 18:24:07 -0800508 long mCreateTime;
Robert Greenwalt2034b912009-08-12 16:08:25 -0700509
510 FeatureUser(int type, String feature, IBinder binder) {
511 super();
512 mNetworkType = type;
513 mFeature = feature;
514 mBinder = binder;
515 mPid = getCallingPid();
516 mUid = getCallingUid();
Robert Greenwalt3eeb6032009-12-21 18:24:07 -0800517 mCreateTime = System.currentTimeMillis();
Robert Greenwaltaffc3a12009-09-27 17:27:04 -0700518
Robert Greenwalt2034b912009-08-12 16:08:25 -0700519 try {
520 mBinder.linkToDeath(this, 0);
521 } catch (RemoteException e) {
522 binderDied();
523 }
524 }
525
526 void unlinkDeathRecipient() {
527 mBinder.unlinkToDeath(this, 0);
528 }
529
530 public void binderDied() {
Joe Onoratoc2386bb2010-02-26 18:56:32 -0800531 Slog.d(TAG, "ConnectivityService FeatureUser binderDied(" +
Robert Greenwalt3eeb6032009-12-21 18:24:07 -0800532 mNetworkType + ", " + mFeature + ", " + mBinder + "), created " +
533 (System.currentTimeMillis() - mCreateTime) + " mSec ago");
Robert Greenwaltaffc3a12009-09-27 17:27:04 -0700534 stopUsingNetworkFeature(this, false);
Robert Greenwalt2034b912009-08-12 16:08:25 -0700535 }
536
Robert Greenwaltaffc3a12009-09-27 17:27:04 -0700537 public void expire() {
Joe Onoratoc2386bb2010-02-26 18:56:32 -0800538 Slog.d(TAG, "ConnectivityService FeatureUser expire(" +
Robert Greenwalt3eeb6032009-12-21 18:24:07 -0800539 mNetworkType + ", " + mFeature + ", " + mBinder +"), created " +
540 (System.currentTimeMillis() - mCreateTime) + " mSec ago");
Robert Greenwaltaffc3a12009-09-27 17:27:04 -0700541 stopUsingNetworkFeature(this, false);
542 }
Robert Greenwalt3eeb6032009-12-21 18:24:07 -0800543
544 public String toString() {
545 return "FeatureUser("+mNetworkType+","+mFeature+","+mPid+","+mUid+"), created " +
546 (System.currentTimeMillis() - mCreateTime) + " mSec ago";
547 }
Robert Greenwalt2034b912009-08-12 16:08:25 -0700548 }
549
Robert Greenwaltaffc3a12009-09-27 17:27:04 -0700550 // javadoc from interface
Robert Greenwalt2034b912009-08-12 16:08:25 -0700551 public int startUsingNetworkFeature(int networkType, String feature,
552 IBinder binder) {
553 if (DBG) {
Joe Onoratoc2386bb2010-02-26 18:56:32 -0800554 Slog.d(TAG, "startUsingNetworkFeature for net " + networkType +
Robert Greenwalt2034b912009-08-12 16:08:25 -0700555 ": " + feature);
556 }
The Android Open Source Project28527d22009-03-03 19:31:44 -0800557 enforceChangePermission();
Robert Greenwaltec05b3c2009-10-30 14:17:42 -0700558 if (!ConnectivityManager.isNetworkTypeValid(networkType) ||
559 mNetAttributes[networkType] == null) {
Robert Greenwalt2034b912009-08-12 16:08:25 -0700560 return Phone.APN_REQUEST_FAILED;
The Android Open Source Project28527d22009-03-03 19:31:44 -0800561 }
Robert Greenwalt2034b912009-08-12 16:08:25 -0700562
Robert Greenwaltaffc3a12009-09-27 17:27:04 -0700563 FeatureUser f = new FeatureUser(networkType, feature, binder);
Robert Greenwalt2034b912009-08-12 16:08:25 -0700564
565 // TODO - move this into the MobileDataStateTracker
566 int usedNetworkType = networkType;
567 if(networkType == ConnectivityManager.TYPE_MOBILE) {
Robert Greenwalt1b21f6c2010-02-23 18:58:05 -0800568 if (!getMobileDataEnabled()) {
Joe Onoratoc2386bb2010-02-26 18:56:32 -0800569 if (DBG) Slog.d(TAG, "requested special network with data disabled - rejected");
Robert Greenwalt1b21f6c2010-02-23 18:58:05 -0800570 return Phone.APN_TYPE_NOT_AVAILABLE;
571 }
Robert Greenwalt2034b912009-08-12 16:08:25 -0700572 if (TextUtils.equals(feature, Phone.FEATURE_ENABLE_MMS)) {
573 usedNetworkType = ConnectivityManager.TYPE_MOBILE_MMS;
574 } else if (TextUtils.equals(feature, Phone.FEATURE_ENABLE_SUPL)) {
575 usedNetworkType = ConnectivityManager.TYPE_MOBILE_SUPL;
576 } else if (TextUtils.equals(feature, Phone.FEATURE_ENABLE_DUN)) {
577 usedNetworkType = ConnectivityManager.TYPE_MOBILE_DUN;
578 } else if (TextUtils.equals(feature, Phone.FEATURE_ENABLE_HIPRI)) {
579 usedNetworkType = ConnectivityManager.TYPE_MOBILE_HIPRI;
580 }
581 }
582 NetworkStateTracker network = mNetTrackers[usedNetworkType];
583 if (network != null) {
584 if (usedNetworkType != networkType) {
585 Integer currentPid = new Integer(getCallingPid());
586
587 NetworkStateTracker radio = mNetTrackers[networkType];
588 NetworkInfo ni = network.getNetworkInfo();
589
590 if (ni.isAvailable() == false) {
Joe Onoratoc2386bb2010-02-26 18:56:32 -0800591 if (DBG) Slog.d(TAG, "special network not available");
Robert Greenwalt2034b912009-08-12 16:08:25 -0700592 return Phone.APN_TYPE_NOT_AVAILABLE;
593 }
594
Robert Greenwaltaffc3a12009-09-27 17:27:04 -0700595 synchronized(this) {
596 mFeatureUsers.add(f);
597 if (!mNetRequestersPids[usedNetworkType].contains(currentPid)) {
598 // this gets used for per-pid dns when connected
599 mNetRequestersPids[usedNetworkType].add(currentPid);
600 }
Robert Greenwalt2034b912009-08-12 16:08:25 -0700601 }
Robert Greenwaltaffc3a12009-09-27 17:27:04 -0700602 mHandler.sendMessageDelayed(mHandler.obtainMessage(
603 NetworkStateTracker.EVENT_RESTORE_DEFAULT_NETWORK,
604 f), getRestoreDefaultNetworkDelay());
605
Robert Greenwalt2034b912009-08-12 16:08:25 -0700606
Robert Greenwalta52c75a2009-08-19 20:19:33 -0700607 if ((ni.isConnectedOrConnecting() == true) &&
608 !network.isTeardownRequested()) {
Robert Greenwalt2034b912009-08-12 16:08:25 -0700609 if (ni.isConnected() == true) {
610 // add the pid-specific dns
Robert Greenwalt3afbead2010-07-23 15:46:26 -0700611 handleDnsConfigurationChange(networkType);
Joe Onoratoc2386bb2010-02-26 18:56:32 -0800612 if (DBG) Slog.d(TAG, "special network already active");
Robert Greenwalt2034b912009-08-12 16:08:25 -0700613 return Phone.APN_ALREADY_ACTIVE;
614 }
Joe Onoratoc2386bb2010-02-26 18:56:32 -0800615 if (DBG) Slog.d(TAG, "special network already connecting");
Robert Greenwalt2034b912009-08-12 16:08:25 -0700616 return Phone.APN_REQUEST_STARTED;
617 }
618
619 // check if the radio in play can make another contact
620 // assume if cannot for now
621
Joe Onoratoc2386bb2010-02-26 18:56:32 -0800622 if (DBG) Slog.d(TAG, "reconnecting to special network");
Robert Greenwalt2034b912009-08-12 16:08:25 -0700623 network.reconnect();
624 return Phone.APN_REQUEST_STARTED;
625 } else {
Robert Greenwaltd391e892010-05-18 10:52:51 -0700626 return -1;
Robert Greenwalt2034b912009-08-12 16:08:25 -0700627 }
628 }
629 return Phone.APN_TYPE_NOT_AVAILABLE;
The Android Open Source Project28527d22009-03-03 19:31:44 -0800630 }
631
Robert Greenwaltaffc3a12009-09-27 17:27:04 -0700632 // javadoc from interface
The Android Open Source Project28527d22009-03-03 19:31:44 -0800633 public int stopUsingNetworkFeature(int networkType, String feature) {
Robert Greenwalt28f43012009-10-06 17:52:40 -0700634 enforceChangePermission();
635
Robert Greenwaltaffc3a12009-09-27 17:27:04 -0700636 int pid = getCallingPid();
637 int uid = getCallingUid();
638
639 FeatureUser u = null;
640 boolean found = false;
641
642 synchronized(this) {
643 for (int i = 0; i < mFeatureUsers.size() ; i++) {
644 u = (FeatureUser)mFeatureUsers.get(i);
645 if (uid == u.mUid && pid == u.mPid &&
646 networkType == u.mNetworkType &&
647 TextUtils.equals(feature, u.mFeature)) {
648 found = true;
649 break;
650 }
651 }
652 }
653 if (found && u != null) {
654 // stop regardless of how many other time this proc had called start
655 return stopUsingNetworkFeature(u, true);
656 } else {
657 // none found!
Joe Onoratoc2386bb2010-02-26 18:56:32 -0800658 if (DBG) Slog.d(TAG, "ignoring stopUsingNetworkFeature - not a live request");
Robert Greenwaltaffc3a12009-09-27 17:27:04 -0700659 return 1;
660 }
Robert Greenwalt2034b912009-08-12 16:08:25 -0700661 }
662
Robert Greenwaltaffc3a12009-09-27 17:27:04 -0700663 private int stopUsingNetworkFeature(FeatureUser u, boolean ignoreDups) {
664 int networkType = u.mNetworkType;
665 String feature = u.mFeature;
666 int pid = u.mPid;
667 int uid = u.mUid;
668
669 NetworkStateTracker tracker = null;
670 boolean callTeardown = false; // used to carry our decision outside of sync block
671
Robert Greenwalt2034b912009-08-12 16:08:25 -0700672 if (DBG) {
Joe Onoratoc2386bb2010-02-26 18:56:32 -0800673 Slog.d(TAG, "stopUsingNetworkFeature for net " + networkType +
Robert Greenwalt2034b912009-08-12 16:08:25 -0700674 ": " + feature);
675 }
Robert Greenwalt28f43012009-10-06 17:52:40 -0700676
The Android Open Source Project28527d22009-03-03 19:31:44 -0800677 if (!ConnectivityManager.isNetworkTypeValid(networkType)) {
678 return -1;
679 }
Robert Greenwalt2034b912009-08-12 16:08:25 -0700680
Robert Greenwaltaffc3a12009-09-27 17:27:04 -0700681 // need to link the mFeatureUsers list with the mNetRequestersPids state in this
682 // sync block
683 synchronized(this) {
684 // check if this process still has an outstanding start request
685 if (!mFeatureUsers.contains(u)) {
Robert Greenwalt46ceefa2010-03-10 16:10:43 -0800686 if (DBG) Slog.d(TAG, "ignoring - this process has no outstanding requests");
Robert Greenwalt2034b912009-08-12 16:08:25 -0700687 return 1;
688 }
Robert Greenwaltaffc3a12009-09-27 17:27:04 -0700689 u.unlinkDeathRecipient();
690 mFeatureUsers.remove(mFeatureUsers.indexOf(u));
691 // If we care about duplicate requests, check for that here.
692 //
693 // This is done to support the extension of a request - the app
694 // can request we start the network feature again and renew the
695 // auto-shutoff delay. Normal "stop" calls from the app though
696 // do not pay attention to duplicate requests - in effect the
697 // API does not refcount and a single stop will counter multiple starts.
698 if (ignoreDups == false) {
699 for (int i = 0; i < mFeatureUsers.size() ; i++) {
700 FeatureUser x = (FeatureUser)mFeatureUsers.get(i);
701 if (x.mUid == u.mUid && x.mPid == u.mPid &&
702 x.mNetworkType == u.mNetworkType &&
703 TextUtils.equals(x.mFeature, u.mFeature)) {
Joe Onoratoc2386bb2010-02-26 18:56:32 -0800704 if (DBG) Slog.d(TAG, "ignoring stopUsingNetworkFeature as dup is found");
Robert Greenwaltaffc3a12009-09-27 17:27:04 -0700705 return 1;
706 }
707 }
Robert Greenwalt2034b912009-08-12 16:08:25 -0700708 }
Robert Greenwaltaffc3a12009-09-27 17:27:04 -0700709
710 // TODO - move to MobileDataStateTracker
711 int usedNetworkType = networkType;
712 if (networkType == ConnectivityManager.TYPE_MOBILE) {
713 if (TextUtils.equals(feature, Phone.FEATURE_ENABLE_MMS)) {
714 usedNetworkType = ConnectivityManager.TYPE_MOBILE_MMS;
715 } else if (TextUtils.equals(feature, Phone.FEATURE_ENABLE_SUPL)) {
716 usedNetworkType = ConnectivityManager.TYPE_MOBILE_SUPL;
717 } else if (TextUtils.equals(feature, Phone.FEATURE_ENABLE_DUN)) {
718 usedNetworkType = ConnectivityManager.TYPE_MOBILE_DUN;
719 } else if (TextUtils.equals(feature, Phone.FEATURE_ENABLE_HIPRI)) {
720 usedNetworkType = ConnectivityManager.TYPE_MOBILE_HIPRI;
721 }
722 }
723 tracker = mNetTrackers[usedNetworkType];
Robert Greenwaltec05b3c2009-10-30 14:17:42 -0700724 if (tracker == null) {
Robert Greenwalt46ceefa2010-03-10 16:10:43 -0800725 if (DBG) Slog.d(TAG, "ignoring - no known tracker for net type " + usedNetworkType);
Robert Greenwaltec05b3c2009-10-30 14:17:42 -0700726 return -1;
727 }
728 if (usedNetworkType != networkType) {
Robert Greenwaltaffc3a12009-09-27 17:27:04 -0700729 Integer currentPid = new Integer(pid);
Robert Greenwaltaffc3a12009-09-27 17:27:04 -0700730 mNetRequestersPids[usedNetworkType].remove(currentPid);
Robert Greenwalt0ca68a02009-12-17 14:54:59 -0800731 reassessPidDns(pid, true);
Robert Greenwaltaffc3a12009-09-27 17:27:04 -0700732 if (mNetRequestersPids[usedNetworkType].size() != 0) {
Joe Onoratoc2386bb2010-02-26 18:56:32 -0800733 if (DBG) Slog.d(TAG, "not tearing down special network - " +
Robert Greenwaltaffc3a12009-09-27 17:27:04 -0700734 "others still using it");
735 return 1;
736 }
737 callTeardown = true;
738 }
739 }
Robert Greenwalt46ceefa2010-03-10 16:10:43 -0800740 if (DBG) Slog.d(TAG, "Doing network teardown");
Robert Greenwaltaffc3a12009-09-27 17:27:04 -0700741 if (callTeardown) {
742 tracker.teardown();
Robert Greenwalt2034b912009-08-12 16:08:25 -0700743 return 1;
744 } else {
Robert Greenwaltd391e892010-05-18 10:52:51 -0700745 return -1;
Robert Greenwalt2034b912009-08-12 16:08:25 -0700746 }
The Android Open Source Project28527d22009-03-03 19:31:44 -0800747 }
748
749 /**
Robert Greenwalt7fe44cb2010-08-27 09:24:29 -0700750 * @deprecated use requestRouteToHostAddress instead
751 *
The Android Open Source Project28527d22009-03-03 19:31:44 -0800752 * Ensure that a network route exists to deliver traffic to the specified
753 * host via the specified network interface.
Robert Greenwalt0659da32009-07-16 17:21:39 -0700754 * @param networkType the type of the network over which traffic to the
755 * specified host is to be routed
756 * @param hostAddress the IP address of the host to which the route is
757 * desired
The Android Open Source Project28527d22009-03-03 19:31:44 -0800758 * @return {@code true} on success, {@code false} on failure
759 */
760 public boolean requestRouteToHost(int networkType, int hostAddress) {
Robert Greenwalt7fe44cb2010-08-27 09:24:29 -0700761 InetAddress inetAddress = NetworkUtils.intToInetAddress(hostAddress);
762
763 if (inetAddress == null) {
764 return false;
765 }
766
767 return requestRouteToHostAddress(networkType, inetAddress.getAddress());
768 }
769
770 /**
771 * Ensure that a network route exists to deliver traffic to the specified
772 * host via the specified network interface.
773 * @param networkType the type of the network over which traffic to the
774 * specified host is to be routed
775 * @param hostAddress the IP address of the host to which the route is
776 * desired
777 * @return {@code true} on success, {@code false} on failure
778 */
779 public boolean requestRouteToHostAddress(int networkType, byte[] hostAddress) {
The Android Open Source Project28527d22009-03-03 19:31:44 -0800780 enforceChangePermission();
781 if (!ConnectivityManager.isNetworkTypeValid(networkType)) {
782 return false;
783 }
784 NetworkStateTracker tracker = mNetTrackers[networkType];
Robert Greenwalt4666ed02009-09-10 15:06:20 -0700785
Robert Greenwaltec05b3c2009-10-30 14:17:42 -0700786 if (tracker == null || !tracker.getNetworkInfo().isConnected() ||
787 tracker.isTeardownRequested()) {
Robert Greenwalt4666ed02009-09-10 15:06:20 -0700788 if (DBG) {
Robert Greenwalt7fe44cb2010-08-27 09:24:29 -0700789 Slog.d(TAG, "requestRouteToHostAddress on down network " +
790 "(" + networkType + ") - dropped");
Robert Greenwalt4666ed02009-09-10 15:06:20 -0700791 }
792 return false;
The Android Open Source Project28527d22009-03-03 19:31:44 -0800793 }
Robert Greenwalta7dfbd32010-06-15 15:43:39 -0700794 try {
Robert Greenwalt7fe44cb2010-08-27 09:24:29 -0700795 InetAddress addr = InetAddress.getByAddress(hostAddress);
Robert Greenwalta7dfbd32010-06-15 15:43:39 -0700796 return addHostRoute(tracker, addr);
797 } catch (UnknownHostException e) {}
798 return false;
Irfan Sheriff7f132d92010-06-09 15:39:36 -0700799 }
800
801 /**
802 * Ensure that a network route exists to deliver traffic to the specified
803 * host via the mobile data network.
804 * @param hostAddress the IP address of the host to which the route is desired,
805 * in network byte order.
Robert Greenwalta7dfbd32010-06-15 15:43:39 -0700806 * TODO - deprecate
Irfan Sheriff7f132d92010-06-09 15:39:36 -0700807 * @return {@code true} on success, {@code false} on failure
808 */
Robert Greenwalta7dfbd32010-06-15 15:43:39 -0700809 private boolean addHostRoute(NetworkStateTracker nt, InetAddress hostAddress) {
Irfan Sheriff7f132d92010-06-09 15:39:36 -0700810 if (nt.getNetworkInfo().getType() == ConnectivityManager.TYPE_WIFI) {
811 return false;
812 }
813
Robert Greenwalt1f1bcfe2010-08-30 10:56:47 -0700814 LinkProperties p = nt.getLinkProperties();
Robert Greenwalta7dfbd32010-06-15 15:43:39 -0700815 if (p == null) return false;
816 String interfaceName = p.getInterfaceName();
Irfan Sheriff7f132d92010-06-09 15:39:36 -0700817
818 if (DBG) {
Robert Greenwalta7dfbd32010-06-15 15:43:39 -0700819 Slog.d(TAG, "Requested host route to " + hostAddress + "(" + interfaceName + ")");
Irfan Sheriff7f132d92010-06-09 15:39:36 -0700820 }
Robert Greenwalta7dfbd32010-06-15 15:43:39 -0700821 if (interfaceName != null) {
Robert Greenwalt7fe44cb2010-08-27 09:24:29 -0700822 return NetworkUtils.addHostRoute(interfaceName, hostAddress, null);
Irfan Sheriff7f132d92010-06-09 15:39:36 -0700823 } else {
Robert Greenwalta7dfbd32010-06-15 15:43:39 -0700824 if (DBG) Slog.e(TAG, "addHostRoute failed due to null interface name");
Irfan Sheriff7f132d92010-06-09 15:39:36 -0700825 return false;
826 }
The Android Open Source Project28527d22009-03-03 19:31:44 -0800827 }
828
829 /**
830 * @see ConnectivityManager#getBackgroundDataSetting()
831 */
832 public boolean getBackgroundDataSetting() {
833 return Settings.Secure.getInt(mContext.getContentResolver(),
834 Settings.Secure.BACKGROUND_DATA, 1) == 1;
835 }
Robert Greenwalt0659da32009-07-16 17:21:39 -0700836
The Android Open Source Project28527d22009-03-03 19:31:44 -0800837 /**
838 * @see ConnectivityManager#setBackgroundDataSetting(boolean)
839 */
840 public void setBackgroundDataSetting(boolean allowBackgroundDataUsage) {
841 mContext.enforceCallingOrSelfPermission(
842 android.Manifest.permission.CHANGE_BACKGROUND_DATA_SETTING,
843 "ConnectivityService");
Robert Greenwalt0659da32009-07-16 17:21:39 -0700844
The Android Open Source Project28527d22009-03-03 19:31:44 -0800845 if (getBackgroundDataSetting() == allowBackgroundDataUsage) return;
846
847 Settings.Secure.putInt(mContext.getContentResolver(),
Robert Greenwalt0659da32009-07-16 17:21:39 -0700848 Settings.Secure.BACKGROUND_DATA,
849 allowBackgroundDataUsage ? 1 : 0);
850
The Android Open Source Project28527d22009-03-03 19:31:44 -0800851 Intent broadcast = new Intent(
852 ConnectivityManager.ACTION_BACKGROUND_DATA_SETTING_CHANGED);
853 mContext.sendBroadcast(broadcast);
Robert Greenwalt0659da32009-07-16 17:21:39 -0700854 }
855
Robert Greenwalt1b21f6c2010-02-23 18:58:05 -0800856 /**
857 * @see ConnectivityManager#getMobileDataEnabled()
858 */
859 public boolean getMobileDataEnabled() {
860 enforceAccessPermission();
861 boolean retVal = Settings.Secure.getInt(mContext.getContentResolver(),
862 Settings.Secure.MOBILE_DATA, 1) == 1;
Joe Onoratoc2386bb2010-02-26 18:56:32 -0800863 if (DBG) Slog.d(TAG, "getMobileDataEnabled returning " + retVal);
Robert Greenwalt1b21f6c2010-02-23 18:58:05 -0800864 return retVal;
865 }
866
867 /**
868 * @see ConnectivityManager#setMobileDataEnabled(boolean)
869 */
870 public synchronized void setMobileDataEnabled(boolean enabled) {
871 enforceChangePermission();
Joe Onoratoc2386bb2010-02-26 18:56:32 -0800872 if (DBG) Slog.d(TAG, "setMobileDataEnabled(" + enabled + ")");
Robert Greenwalt1b21f6c2010-02-23 18:58:05 -0800873
874 if (getMobileDataEnabled() == enabled) return;
875
876 Settings.Secure.putInt(mContext.getContentResolver(),
877 Settings.Secure.MOBILE_DATA, enabled ? 1 : 0);
878
879 if (enabled) {
880 if (mNetTrackers[ConnectivityManager.TYPE_MOBILE] != null) {
Joe Onoratoc2386bb2010-02-26 18:56:32 -0800881 if (DBG) Slog.d(TAG, "starting up " + mNetTrackers[ConnectivityManager.TYPE_MOBILE]);
Robert Greenwalt1b21f6c2010-02-23 18:58:05 -0800882 mNetTrackers[ConnectivityManager.TYPE_MOBILE].reconnect();
883 }
884 } else {
885 for (NetworkStateTracker nt : mNetTrackers) {
886 if (nt == null) continue;
887 int netType = nt.getNetworkInfo().getType();
888 if (mNetAttributes[netType].mRadio == ConnectivityManager.TYPE_MOBILE) {
Joe Onoratoc2386bb2010-02-26 18:56:32 -0800889 if (DBG) Slog.d(TAG, "tearing down " + nt);
Robert Greenwalt1b21f6c2010-02-23 18:58:05 -0800890 nt.teardown();
891 }
892 }
893 }
894 }
895
The Android Open Source Project28527d22009-03-03 19:31:44 -0800896 private int getNumConnectedNetworks() {
897 int numConnectedNets = 0;
898
899 for (NetworkStateTracker nt : mNetTrackers) {
Robert Greenwaltec05b3c2009-10-30 14:17:42 -0700900 if (nt != null && nt.getNetworkInfo().isConnected() &&
Robert Greenwalt0659da32009-07-16 17:21:39 -0700901 !nt.isTeardownRequested()) {
The Android Open Source Project28527d22009-03-03 19:31:44 -0800902 ++numConnectedNets;
903 }
904 }
905 return numConnectedNets;
906 }
907
908 private void enforceAccessPermission() {
Robert Greenwalt0659da32009-07-16 17:21:39 -0700909 mContext.enforceCallingOrSelfPermission(
910 android.Manifest.permission.ACCESS_NETWORK_STATE,
911 "ConnectivityService");
The Android Open Source Project28527d22009-03-03 19:31:44 -0800912 }
913
914 private void enforceChangePermission() {
Robert Greenwalt0659da32009-07-16 17:21:39 -0700915 mContext.enforceCallingOrSelfPermission(
916 android.Manifest.permission.CHANGE_NETWORK_STATE,
917 "ConnectivityService");
The Android Open Source Project28527d22009-03-03 19:31:44 -0800918 }
919
Robert Greenwalt0c4828c2010-01-26 11:40:34 -0800920 // TODO Make this a special check when it goes public
921 private void enforceTetherChangePermission() {
922 mContext.enforceCallingOrSelfPermission(
923 android.Manifest.permission.CHANGE_NETWORK_STATE,
924 "ConnectivityService");
925 }
926
Robert Greenwalt8e87f122010-02-11 18:18:40 -0800927 private void enforceTetherAccessPermission() {
928 mContext.enforceCallingOrSelfPermission(
929 android.Manifest.permission.ACCESS_NETWORK_STATE,
930 "ConnectivityService");
931 }
932
Robert Greenwalt93dc1042010-06-15 12:19:37 -0700933 private void enforceConnectivityInternalPermission() {
934 mContext.enforceCallingOrSelfPermission(
935 android.Manifest.permission.CONNECTIVITY_INTERNAL,
936 "ConnectivityService");
937 }
938
The Android Open Source Project28527d22009-03-03 19:31:44 -0800939 /**
Robert Greenwalt0659da32009-07-16 17:21:39 -0700940 * Handle a {@code DISCONNECTED} event. If this pertains to the non-active
941 * network, we ignore it. If it is for the active network, we send out a
942 * broadcast. But first, we check whether it might be possible to connect
943 * to a different network.
The Android Open Source Project28527d22009-03-03 19:31:44 -0800944 * @param info the {@code NetworkInfo} for the network
945 */
946 private void handleDisconnect(NetworkInfo info) {
947
Robert Greenwalt2034b912009-08-12 16:08:25 -0700948 int prevNetType = info.getType();
The Android Open Source Project28527d22009-03-03 19:31:44 -0800949
Robert Greenwalt2034b912009-08-12 16:08:25 -0700950 mNetTrackers[prevNetType].setTeardownRequested(false);
The Android Open Source Project28527d22009-03-03 19:31:44 -0800951 /*
952 * If the disconnected network is not the active one, then don't report
953 * this as a loss of connectivity. What probably happened is that we're
954 * getting the disconnect for a network that we explicitly disabled
955 * in accordance with network preference policies.
956 */
Robert Greenwalt2034b912009-08-12 16:08:25 -0700957 if (!mNetAttributes[prevNetType].isDefault()) {
958 List pids = mNetRequestersPids[prevNetType];
959 for (int i = 0; i<pids.size(); i++) {
960 Integer pid = (Integer)pids.get(i);
961 // will remove them because the net's no longer connected
962 // need to do this now as only now do we know the pids and
963 // can properly null things that are no longer referenced.
964 reassessPidDns(pid.intValue(), false);
The Android Open Source Project28527d22009-03-03 19:31:44 -0800965 }
The Android Open Source Project28527d22009-03-03 19:31:44 -0800966 }
967
The Android Open Source Project28527d22009-03-03 19:31:44 -0800968 Intent intent = new Intent(ConnectivityManager.CONNECTIVITY_ACTION);
969 intent.putExtra(ConnectivityManager.EXTRA_NETWORK_INFO, info);
970 if (info.isFailover()) {
971 intent.putExtra(ConnectivityManager.EXTRA_IS_FAILOVER, true);
972 info.setFailover(false);
973 }
974 if (info.getReason() != null) {
975 intent.putExtra(ConnectivityManager.EXTRA_REASON, info.getReason());
976 }
977 if (info.getExtraInfo() != null) {
Robert Greenwalt0659da32009-07-16 17:21:39 -0700978 intent.putExtra(ConnectivityManager.EXTRA_EXTRA_INFO,
979 info.getExtraInfo());
The Android Open Source Project28527d22009-03-03 19:31:44 -0800980 }
Robert Greenwalt2034b912009-08-12 16:08:25 -0700981
Robert Greenwalt3cc68d32010-01-25 17:54:29 -0800982 NetworkStateTracker newNet = null;
983 if (mNetAttributes[prevNetType].isDefault()) {
984 newNet = tryFailover(prevNetType);
985 if (newNet != null) {
986 NetworkInfo switchTo = newNet.getNetworkInfo();
987 intent.putExtra(ConnectivityManager.EXTRA_OTHER_NETWORK_INFO, switchTo);
988 } else {
989 intent.putExtra(ConnectivityManager.EXTRA_NO_CONNECTIVITY, true);
990 }
Robert Greenwaltf55ced92010-01-20 19:29:41 -0800991 }
992 // do this before we broadcast the change
Robert Greenwalt3afbead2010-07-23 15:46:26 -0700993 handleConnectivityChange(prevNetType);
Robert Greenwaltf55ced92010-01-20 19:29:41 -0800994
995 sendStickyBroadcast(intent);
996 /*
997 * If the failover network is already connected, then immediately send
998 * out a followup broadcast indicating successful failover
999 */
1000 if (newNet != null && newNet.getNetworkInfo().isConnected()) {
1001 sendConnectedBroadcast(newNet.getNetworkInfo());
1002 }
1003 }
1004
Robert Greenwalt3cc68d32010-01-25 17:54:29 -08001005 // returns null if no failover available
Robert Greenwaltf55ced92010-01-20 19:29:41 -08001006 private NetworkStateTracker tryFailover(int prevNetType) {
Robert Greenwalt2034b912009-08-12 16:08:25 -07001007 /*
1008 * If this is a default network, check if other defaults are available
1009 * or active
1010 */
1011 NetworkStateTracker newNet = null;
1012 if (mNetAttributes[prevNetType].isDefault()) {
Robert Greenwalt2034b912009-08-12 16:08:25 -07001013 if (mActiveDefaultNetwork == prevNetType) {
1014 mActiveDefaultNetwork = -1;
1015 }
1016
1017 int newType = -1;
1018 int newPriority = -1;
Robert Greenwalt72451bf2010-02-25 12:04:29 -08001019 boolean noMobileData = !getMobileDataEnabled();
Robert Greenwaltf55ced92010-01-20 19:29:41 -08001020 for (int checkType=0; checkType <= ConnectivityManager.MAX_NETWORK_TYPE; checkType++) {
Robert Greenwaltec05b3c2009-10-30 14:17:42 -07001021 if (checkType == prevNetType) continue;
1022 if (mNetAttributes[checkType] == null) continue;
Robert Greenwalt72451bf2010-02-25 12:04:29 -08001023 if (mNetAttributes[checkType].mRadio == ConnectivityManager.TYPE_MOBILE &&
1024 noMobileData) {
1025 if (DBG) {
Joe Onoratoc2386bb2010-02-26 18:56:32 -08001026 Slog.d(TAG, "not failing over to mobile type " + checkType +
Robert Greenwalt72451bf2010-02-25 12:04:29 -08001027 " because Mobile Data Disabled");
1028 }
1029 continue;
1030 }
Robert Greenwalt2034b912009-08-12 16:08:25 -07001031 if (mNetAttributes[checkType].isDefault()) {
1032 /* TODO - if we have multiple nets we could use
1033 * we may want to put more thought into which we choose
1034 */
1035 if (checkType == mNetworkPreference) {
1036 newType = checkType;
1037 break;
1038 }
Robert Greenwaltec05b3c2009-10-30 14:17:42 -07001039 if (mNetAttributes[checkType].mPriority > newPriority) {
Robert Greenwalt2034b912009-08-12 16:08:25 -07001040 newType = checkType;
Robert Greenwaltec05b3c2009-10-30 14:17:42 -07001041 newPriority = mNetAttributes[newType].mPriority;
Robert Greenwalt2034b912009-08-12 16:08:25 -07001042 }
The Android Open Source Project28527d22009-03-03 19:31:44 -08001043 }
1044 }
Robert Greenwalt2034b912009-08-12 16:08:25 -07001045
1046 if (newType != -1) {
1047 newNet = mNetTrackers[newType];
1048 /**
1049 * See if the other network is available to fail over to.
1050 * If is not available, we enable it anyway, so that it
1051 * will be able to connect when it does become available,
1052 * but we report a total loss of connectivity rather than
1053 * report that we are attempting to fail over.
1054 */
1055 if (newNet.isAvailable()) {
1056 NetworkInfo switchTo = newNet.getNetworkInfo();
1057 switchTo.setFailover(true);
Robert Greenwalta52c75a2009-08-19 20:19:33 -07001058 if (!switchTo.isConnectedOrConnecting() ||
1059 newNet.isTeardownRequested()) {
Robert Greenwalt2034b912009-08-12 16:08:25 -07001060 newNet.reconnect();
1061 }
1062 if (DBG) {
1063 if (switchTo.isConnected()) {
Joe Onoratoc2386bb2010-02-26 18:56:32 -08001064 Slog.v(TAG, "Switching to already connected " +
Robert Greenwalt2034b912009-08-12 16:08:25 -07001065 switchTo.getTypeName());
1066 } else {
Joe Onoratoc2386bb2010-02-26 18:56:32 -08001067 Slog.v(TAG, "Attempting to switch to " +
Robert Greenwalt2034b912009-08-12 16:08:25 -07001068 switchTo.getTypeName());
1069 }
1070 }
Robert Greenwalt2034b912009-08-12 16:08:25 -07001071 } else {
1072 newNet.reconnect();
Robert Greenwalt12984322010-03-09 14:55:08 -08001073 newNet = null; // not officially avail.. try anyway, but
1074 // report no failover
Robert Greenwalt2034b912009-08-12 16:08:25 -07001075 }
Robert Greenwalt2034b912009-08-12 16:08:25 -07001076 }
The Android Open Source Project28527d22009-03-03 19:31:44 -08001077 }
Robert Greenwalt2034b912009-08-12 16:08:25 -07001078
Robert Greenwaltf55ced92010-01-20 19:29:41 -08001079 return newNet;
The Android Open Source Project28527d22009-03-03 19:31:44 -08001080 }
1081
1082 private void sendConnectedBroadcast(NetworkInfo info) {
1083 Intent intent = new Intent(ConnectivityManager.CONNECTIVITY_ACTION);
1084 intent.putExtra(ConnectivityManager.EXTRA_NETWORK_INFO, info);
1085 if (info.isFailover()) {
1086 intent.putExtra(ConnectivityManager.EXTRA_IS_FAILOVER, true);
1087 info.setFailover(false);
1088 }
1089 if (info.getReason() != null) {
1090 intent.putExtra(ConnectivityManager.EXTRA_REASON, info.getReason());
1091 }
1092 if (info.getExtraInfo() != null) {
Robert Greenwalt0659da32009-07-16 17:21:39 -07001093 intent.putExtra(ConnectivityManager.EXTRA_EXTRA_INFO,
1094 info.getExtraInfo());
The Android Open Source Project28527d22009-03-03 19:31:44 -08001095 }
Mike Lockwoodfde2b762009-08-14 14:18:49 -04001096 sendStickyBroadcast(intent);
The Android Open Source Project28527d22009-03-03 19:31:44 -08001097 }
1098
1099 /**
1100 * Called when an attempt to fail over to another network has failed.
1101 * @param info the {@link NetworkInfo} for the failed network
1102 */
1103 private void handleConnectionFailure(NetworkInfo info) {
1104 mNetTrackers[info.getType()].setTeardownRequested(false);
The Android Open Source Project28527d22009-03-03 19:31:44 -08001105
Robert Greenwalt2034b912009-08-12 16:08:25 -07001106 String reason = info.getReason();
1107 String extraInfo = info.getExtraInfo();
Robert Greenwalt0659da32009-07-16 17:21:39 -07001108
Robert Greenwalt2034b912009-08-12 16:08:25 -07001109 if (DBG) {
1110 String reasonText;
1111 if (reason == null) {
1112 reasonText = ".";
1113 } else {
1114 reasonText = " (" + reason + ").";
The Android Open Source Project28527d22009-03-03 19:31:44 -08001115 }
Joe Onoratoc2386bb2010-02-26 18:56:32 -08001116 Slog.v(TAG, "Attempt to connect to " + info.getTypeName() +
Robert Greenwalt2034b912009-08-12 16:08:25 -07001117 " failed" + reasonText);
The Android Open Source Project28527d22009-03-03 19:31:44 -08001118 }
Robert Greenwalt2034b912009-08-12 16:08:25 -07001119
1120 Intent intent = new Intent(ConnectivityManager.CONNECTIVITY_ACTION);
1121 intent.putExtra(ConnectivityManager.EXTRA_NETWORK_INFO, info);
1122 if (getActiveNetworkInfo() == null) {
1123 intent.putExtra(ConnectivityManager.EXTRA_NO_CONNECTIVITY, true);
1124 }
1125 if (reason != null) {
1126 intent.putExtra(ConnectivityManager.EXTRA_REASON, reason);
1127 }
1128 if (extraInfo != null) {
1129 intent.putExtra(ConnectivityManager.EXTRA_EXTRA_INFO, extraInfo);
1130 }
1131 if (info.isFailover()) {
1132 intent.putExtra(ConnectivityManager.EXTRA_IS_FAILOVER, true);
1133 info.setFailover(false);
1134 }
Robert Greenwaltf55ced92010-01-20 19:29:41 -08001135
Robert Greenwalt3cc68d32010-01-25 17:54:29 -08001136 NetworkStateTracker newNet = null;
1137 if (mNetAttributes[info.getType()].isDefault()) {
1138 newNet = tryFailover(info.getType());
1139 if (newNet != null) {
1140 NetworkInfo switchTo = newNet.getNetworkInfo();
1141 intent.putExtra(ConnectivityManager.EXTRA_OTHER_NETWORK_INFO, switchTo);
1142 } else {
1143 intent.putExtra(ConnectivityManager.EXTRA_NO_CONNECTIVITY, true);
1144 }
Robert Greenwaltf55ced92010-01-20 19:29:41 -08001145 }
Robert Greenwalt3cc68d32010-01-25 17:54:29 -08001146
Mike Lockwoodfde2b762009-08-14 14:18:49 -04001147 sendStickyBroadcast(intent);
Robert Greenwaltf55ced92010-01-20 19:29:41 -08001148 /*
1149 * If the failover network is already connected, then immediately send
1150 * out a followup broadcast indicating successful failover
1151 */
1152 if (newNet != null && newNet.getNetworkInfo().isConnected()) {
1153 sendConnectedBroadcast(newNet.getNetworkInfo());
1154 }
Mike Lockwoodfde2b762009-08-14 14:18:49 -04001155 }
1156
1157 private void sendStickyBroadcast(Intent intent) {
1158 synchronized(this) {
Dianne Hackborna417ff82009-12-08 19:45:14 -08001159 if (!mSystemReady) {
1160 mInitialBroadcast = new Intent(intent);
Mike Lockwoodfde2b762009-08-14 14:18:49 -04001161 }
Dianne Hackborna417ff82009-12-08 19:45:14 -08001162 intent.addFlags(Intent.FLAG_RECEIVER_REGISTERED_ONLY_BEFORE_BOOT);
1163 mContext.sendStickyBroadcast(intent);
Mike Lockwoodfde2b762009-08-14 14:18:49 -04001164 }
1165 }
1166
1167 void systemReady() {
1168 synchronized(this) {
1169 mSystemReady = true;
Dianne Hackborna417ff82009-12-08 19:45:14 -08001170 if (mInitialBroadcast != null) {
1171 mContext.sendStickyBroadcast(mInitialBroadcast);
1172 mInitialBroadcast = null;
Mike Lockwoodfde2b762009-08-14 14:18:49 -04001173 }
1174 }
The Android Open Source Project28527d22009-03-03 19:31:44 -08001175 }
1176
1177 private void handleConnect(NetworkInfo info) {
Robert Greenwalt2034b912009-08-12 16:08:25 -07001178 int type = info.getType();
The Android Open Source Project28527d22009-03-03 19:31:44 -08001179
1180 // snapshot isFailover, because sendConnectedBroadcast() resets it
1181 boolean isFailover = info.isFailover();
Robert Greenwalt2034b912009-08-12 16:08:25 -07001182 NetworkStateTracker thisNet = mNetTrackers[type];
The Android Open Source Project28527d22009-03-03 19:31:44 -08001183
Robert Greenwalt2034b912009-08-12 16:08:25 -07001184 // if this is a default net and other default is running
1185 // kill the one not preferred
1186 if (mNetAttributes[type].isDefault()) {
Robert Greenwalt2034b912009-08-12 16:08:25 -07001187 if (mActiveDefaultNetwork != -1 && mActiveDefaultNetwork != type) {
1188 if ((type != mNetworkPreference &&
1189 mNetAttributes[mActiveDefaultNetwork].mPriority >
1190 mNetAttributes[type].mPriority) ||
1191 mNetworkPreference == mActiveDefaultNetwork) {
1192 // don't accept this one
Joe Onoratoc2386bb2010-02-26 18:56:32 -08001193 if (DBG) Slog.v(TAG, "Not broadcasting CONNECT_ACTION " +
Robert Greenwalt2034b912009-08-12 16:08:25 -07001194 "to torn down network " + info.getTypeName());
1195 teardown(thisNet);
1196 return;
1197 } else {
1198 // tear down the other
1199 NetworkStateTracker otherNet =
1200 mNetTrackers[mActiveDefaultNetwork];
Joe Onoratoc2386bb2010-02-26 18:56:32 -08001201 if (DBG) Slog.v(TAG, "Policy requires " +
Robert Greenwalt2034b912009-08-12 16:08:25 -07001202 otherNet.getNetworkInfo().getTypeName() +
1203 " teardown");
1204 if (!teardown(otherNet)) {
Joe Onoratoc2386bb2010-02-26 18:56:32 -08001205 Slog.e(TAG, "Network declined teardown request");
Robert Greenwalt2034b912009-08-12 16:08:25 -07001206 return;
1207 }
Robert Greenwalt93dc1042010-06-15 12:19:37 -07001208 }
1209 }
1210 synchronized (ConnectivityService.this) {
1211 // have a new default network, release the transition wakelock in a second
1212 // if it's held. The second pause is to allow apps to reconnect over the
1213 // new network
1214 if (mNetTransitionWakeLock.isHeld()) {
1215 mHandler.sendMessageDelayed(mHandler.obtainMessage(
1216 NetworkStateTracker.EVENT_CLEAR_NET_TRANSITION_WAKELOCK,
1217 mNetTransitionWakeLockSerialNumber, 0),
1218 1000);
Robert Greenwalt2034b912009-08-12 16:08:25 -07001219 }
1220 }
1221 mActiveDefaultNetwork = type;
1222 }
The Android Open Source Project28527d22009-03-03 19:31:44 -08001223 thisNet.setTeardownRequested(false);
Irfan Sheriff7f132d92010-06-09 15:39:36 -07001224 updateNetworkSettings(thisNet);
Robert Greenwalt3afbead2010-07-23 15:46:26 -07001225 handleConnectivityChange(type);
Robert Greenwalt2034b912009-08-12 16:08:25 -07001226 sendConnectedBroadcast(info);
The Android Open Source Project28527d22009-03-03 19:31:44 -08001227 }
1228
The Android Open Source Project28527d22009-03-03 19:31:44 -08001229 /**
Robert Greenwalt3afbead2010-07-23 15:46:26 -07001230 * After a change in the connectivity state of a network. We're mainly
1231 * concerned with making sure that the list of DNS servers is set up
1232 * according to which networks are connected, and ensuring that the
1233 * right routing table entries exist.
The Android Open Source Project28527d22009-03-03 19:31:44 -08001234 */
Robert Greenwalt3afbead2010-07-23 15:46:26 -07001235 private void handleConnectivityChange(int netType) {
The Android Open Source Project28527d22009-03-03 19:31:44 -08001236 /*
Robert Greenwalt2034b912009-08-12 16:08:25 -07001237 * If a non-default network is enabled, add the host routes that
Robert Greenwalt3afbead2010-07-23 15:46:26 -07001238 * will allow it's DNS servers to be accessed.
The Android Open Source Project28527d22009-03-03 19:31:44 -08001239 */
Robert Greenwalt3afbead2010-07-23 15:46:26 -07001240 handleDnsConfigurationChange(netType);
Robert Greenwalt2034b912009-08-12 16:08:25 -07001241
Robert Greenwalt3afbead2010-07-23 15:46:26 -07001242 if (mNetTrackers[netType].getNetworkInfo().isConnected()) {
1243 if (mNetAttributes[netType].isDefault()) {
1244 addDefaultRoute(mNetTrackers[netType]);
Robert Greenwalt2034b912009-08-12 16:08:25 -07001245 } else {
Robert Greenwalt3afbead2010-07-23 15:46:26 -07001246 addPrivateDnsRoutes(mNetTrackers[netType]);
1247 }
1248 } else {
1249 if (mNetAttributes[netType].isDefault()) {
1250 removeDefaultRoute(mNetTrackers[netType]);
1251 } else {
1252 removePrivateDnsRoutes(mNetTrackers[netType]);
Robert Greenwalt2034b912009-08-12 16:08:25 -07001253 }
The Android Open Source Project28527d22009-03-03 19:31:44 -08001254 }
1255 }
1256
Irfan Sheriff7f132d92010-06-09 15:39:36 -07001257 private void addPrivateDnsRoutes(NetworkStateTracker nt) {
Irfan Sheriff7f132d92010-06-09 15:39:36 -07001258 boolean privateDnsRouteSet = nt.isPrivateDnsRouteSet();
Robert Greenwalt1f1bcfe2010-08-30 10:56:47 -07001259 LinkProperties p = nt.getLinkProperties();
Robert Greenwalta7dfbd32010-06-15 15:43:39 -07001260 if (p == null) return;
1261 String interfaceName = p.getInterfaceName();
Irfan Sheriff7f132d92010-06-09 15:39:36 -07001262
1263 if (DBG) {
1264 Slog.d(TAG, "addPrivateDnsRoutes for " + nt +
1265 "(" + interfaceName + ") - mPrivateDnsRouteSet = " + privateDnsRouteSet);
1266 }
Irfan Sheriff7f132d92010-06-09 15:39:36 -07001267 if (interfaceName != null && !privateDnsRouteSet) {
Robert Greenwalta7dfbd32010-06-15 15:43:39 -07001268 Collection<InetAddress> dnsList = p.getDnses();
1269 for (InetAddress dns : dnsList) {
1270 if (DBG) Slog.d(TAG, " adding " + dns);
Robert Greenwalt7fe44cb2010-08-27 09:24:29 -07001271 NetworkUtils.addHostRoute(interfaceName, dns, null);
Irfan Sheriff7f132d92010-06-09 15:39:36 -07001272 }
1273 nt.privateDnsRouteSet(true);
1274 }
1275 }
1276
1277 private void removePrivateDnsRoutes(NetworkStateTracker nt) {
1278 // TODO - we should do this explicitly but the NetUtils api doesnt
1279 // support this yet - must remove all. No worse than before
Robert Greenwalt1f1bcfe2010-08-30 10:56:47 -07001280 LinkProperties p = nt.getLinkProperties();
Robert Greenwalta7dfbd32010-06-15 15:43:39 -07001281 if (p == null) return;
1282 String interfaceName = p.getInterfaceName();
Irfan Sheriff7f132d92010-06-09 15:39:36 -07001283 boolean privateDnsRouteSet = nt.isPrivateDnsRouteSet();
1284 if (interfaceName != null && privateDnsRouteSet) {
1285 if (DBG) {
1286 Slog.d(TAG, "removePrivateDnsRoutes for " + nt.getNetworkInfo().getTypeName() +
1287 " (" + interfaceName + ")");
1288 }
1289 NetworkUtils.removeHostRoutes(interfaceName);
1290 nt.privateDnsRouteSet(false);
1291 }
1292 }
1293
Irfan Sheriff7f132d92010-06-09 15:39:36 -07001294
1295 private void addDefaultRoute(NetworkStateTracker nt) {
Robert Greenwalt1f1bcfe2010-08-30 10:56:47 -07001296 LinkProperties p = nt.getLinkProperties();
Robert Greenwalta7dfbd32010-06-15 15:43:39 -07001297 if (p == null) return;
1298 String interfaceName = p.getInterfaceName();
1299 InetAddress defaultGatewayAddr = p.getGateway();
Irfan Sheriff7f132d92010-06-09 15:39:36 -07001300
Robert Greenwalt3afbead2010-07-23 15:46:26 -07001301 if ((interfaceName != null) && (defaultGatewayAddr != null )) {
Robert Greenwalt7fe44cb2010-08-27 09:24:29 -07001302 if (!NetworkUtils.addDefaultRoute(interfaceName, defaultGatewayAddr) && DBG) {
Robert Greenwalta7dfbd32010-06-15 15:43:39 -07001303 NetworkInfo networkInfo = nt.getNetworkInfo();
Irfan Sheriff7f132d92010-06-09 15:39:36 -07001304 Slog.d(TAG, "addDefaultRoute for " + networkInfo.getTypeName() +
1305 " (" + interfaceName + "), GatewayAddr=" + defaultGatewayAddr);
1306 }
Irfan Sheriff7f132d92010-06-09 15:39:36 -07001307 }
1308 }
1309
1310
1311 public void removeDefaultRoute(NetworkStateTracker nt) {
Robert Greenwalt1f1bcfe2010-08-30 10:56:47 -07001312 LinkProperties p = nt.getLinkProperties();
Robert Greenwalta7dfbd32010-06-15 15:43:39 -07001313 if (p == null) return;
1314 String interfaceName = p.getInterfaceName();
Irfan Sheriff7f132d92010-06-09 15:39:36 -07001315
Robert Greenwalt3afbead2010-07-23 15:46:26 -07001316 if (interfaceName != null) {
1317 if ((NetworkUtils.removeDefaultRoute(interfaceName) >= 0) && DBG) {
Robert Greenwalta7dfbd32010-06-15 15:43:39 -07001318 NetworkInfo networkInfo = nt.getNetworkInfo();
Irfan Sheriff7f132d92010-06-09 15:39:36 -07001319 Slog.d(TAG, "removeDefaultRoute for " + networkInfo.getTypeName() + " (" +
1320 interfaceName + ")");
1321 }
Irfan Sheriff7f132d92010-06-09 15:39:36 -07001322 }
1323 }
1324
1325 /**
1326 * Reads the network specific TCP buffer sizes from SystemProperties
1327 * net.tcp.buffersize.[default|wifi|umts|edge|gprs] and set them for system
1328 * wide use
1329 */
1330 public void updateNetworkSettings(NetworkStateTracker nt) {
1331 String key = nt.getTcpBufferSizesPropName();
1332 String bufferSizes = SystemProperties.get(key);
1333
1334 if (bufferSizes.length() == 0) {
1335 Slog.e(TAG, key + " not found in system properties. Using defaults");
1336
1337 // Setting to default values so we won't be stuck to previous values
1338 key = "net.tcp.buffersize.default";
1339 bufferSizes = SystemProperties.get(key);
1340 }
1341
1342 // Set values in kernel
1343 if (bufferSizes.length() != 0) {
1344 if (DBG) {
1345 Slog.v(TAG, "Setting TCP values: [" + bufferSizes
1346 + "] which comes from [" + key + "]");
1347 }
1348 setBufferSize(bufferSizes);
1349 }
1350 }
1351
1352 /**
1353 * Writes TCP buffer sizes to /sys/kernel/ipv4/tcp_[r/w]mem_[min/def/max]
1354 * which maps to /proc/sys/net/ipv4/tcp_rmem and tcpwmem
1355 *
1356 * @param bufferSizes in the format of "readMin, readInitial, readMax,
1357 * writeMin, writeInitial, writeMax"
1358 */
1359 private void setBufferSize(String bufferSizes) {
1360 try {
1361 String[] values = bufferSizes.split(",");
1362
1363 if (values.length == 6) {
1364 final String prefix = "/sys/kernel/ipv4/tcp_";
1365 stringToFile(prefix + "rmem_min", values[0]);
1366 stringToFile(prefix + "rmem_def", values[1]);
1367 stringToFile(prefix + "rmem_max", values[2]);
1368 stringToFile(prefix + "wmem_min", values[3]);
1369 stringToFile(prefix + "wmem_def", values[4]);
1370 stringToFile(prefix + "wmem_max", values[5]);
1371 } else {
1372 Slog.e(TAG, "Invalid buffersize string: " + bufferSizes);
1373 }
1374 } catch (IOException e) {
1375 Slog.e(TAG, "Can't set tcp buffer sizes:" + e);
1376 }
1377 }
1378
1379 /**
1380 * Writes string to file. Basically same as "echo -n $string > $filename"
1381 *
1382 * @param filename
1383 * @param string
1384 * @throws IOException
1385 */
1386 private void stringToFile(String filename, String string) throws IOException {
1387 FileWriter out = new FileWriter(filename);
1388 try {
1389 out.write(string);
1390 } finally {
1391 out.close();
1392 }
1393 }
1394
1395
Robert Greenwalt2034b912009-08-12 16:08:25 -07001396 /**
1397 * Adjust the per-process dns entries (net.dns<x>.<pid>) based
1398 * on the highest priority active net which this process requested.
1399 * If there aren't any, clear it out
1400 */
1401 private void reassessPidDns(int myPid, boolean doBump)
1402 {
Joe Onoratoc2386bb2010-02-26 18:56:32 -08001403 if (DBG) Slog.d(TAG, "reassessPidDns for pid " + myPid);
Robert Greenwalt2034b912009-08-12 16:08:25 -07001404 for(int i : mPriorityList) {
1405 if (mNetAttributes[i].isDefault()) {
1406 continue;
1407 }
1408 NetworkStateTracker nt = mNetTrackers[i];
Robert Greenwalt0659da32009-07-16 17:21:39 -07001409 if (nt.getNetworkInfo().isConnected() &&
1410 !nt.isTeardownRequested()) {
Robert Greenwalt1f1bcfe2010-08-30 10:56:47 -07001411 LinkProperties p = nt.getLinkProperties();
Robert Greenwalta7dfbd32010-06-15 15:43:39 -07001412 if (p == null) continue;
Robert Greenwalt2034b912009-08-12 16:08:25 -07001413 List pids = mNetRequestersPids[i];
1414 for (int j=0; j<pids.size(); j++) {
1415 Integer pid = (Integer)pids.get(j);
1416 if (pid.intValue() == myPid) {
Robert Greenwalta7dfbd32010-06-15 15:43:39 -07001417 Collection<InetAddress> dnses = p.getDnses();
1418 writePidDns(dnses, myPid);
Robert Greenwalt2034b912009-08-12 16:08:25 -07001419 if (doBump) {
1420 bumpDns();
1421 }
1422 return;
1423 }
1424 }
1425 }
1426 }
1427 // nothing found - delete
1428 for (int i = 1; ; i++) {
1429 String prop = "net.dns" + i + "." + myPid;
1430 if (SystemProperties.get(prop).length() == 0) {
1431 if (doBump) {
1432 bumpDns();
1433 }
1434 return;
1435 }
1436 SystemProperties.set(prop, "");
1437 }
1438 }
1439
Robert Greenwalta7dfbd32010-06-15 15:43:39 -07001440 private void writePidDns(Collection <InetAddress> dnses, int pid) {
Robert Greenwalt2034b912009-08-12 16:08:25 -07001441 int j = 1;
Robert Greenwalta7dfbd32010-06-15 15:43:39 -07001442 for (InetAddress dns : dnses) {
1443 SystemProperties.set("net.dns" + j++ + "." + pid, dns.getHostAddress());
Robert Greenwalt2034b912009-08-12 16:08:25 -07001444 }
1445 }
1446
1447 private void bumpDns() {
1448 /*
1449 * Bump the property that tells the name resolver library to reread
1450 * the DNS server list from the properties.
1451 */
1452 String propVal = SystemProperties.get("net.dnschange");
1453 int n = 0;
1454 if (propVal.length() != 0) {
1455 try {
1456 n = Integer.parseInt(propVal);
1457 } catch (NumberFormatException e) {}
1458 }
1459 SystemProperties.set("net.dnschange", "" + (n+1));
1460 }
1461
Robert Greenwalt3afbead2010-07-23 15:46:26 -07001462 private void handleDnsConfigurationChange(int netType) {
Robert Greenwalt2034b912009-08-12 16:08:25 -07001463 // add default net's dns entries
Robert Greenwalt3afbead2010-07-23 15:46:26 -07001464 NetworkStateTracker nt = mNetTrackers[netType];
1465 if (nt != null && nt.getNetworkInfo().isConnected() && !nt.isTeardownRequested()) {
Robert Greenwalt1f1bcfe2010-08-30 10:56:47 -07001466 LinkProperties p = nt.getLinkProperties();
Robert Greenwalt3afbead2010-07-23 15:46:26 -07001467 if (p == null) return;
1468 Collection<InetAddress> dnses = p.getDnses();
1469 if (mNetAttributes[netType].isDefault()) {
1470 int j = 1;
1471 for (InetAddress dns : dnses) {
1472 if (DBG) {
1473 Slog.d(TAG, "adding dns " + dns + " for " +
1474 nt.getNetworkInfo().getTypeName());
Robert Greenwalt2034b912009-08-12 16:08:25 -07001475 }
Robert Greenwalt3afbead2010-07-23 15:46:26 -07001476 SystemProperties.set("net.dns" + j++, dns.getHostAddress());
1477 }
1478 for (int k=j ; k<mNumDnsEntries; k++) {
1479 if (DBG) Slog.d(TAG, "erasing net.dns" + k);
1480 SystemProperties.set("net.dns" + k, "");
1481 }
1482 mNumDnsEntries = j;
1483 } else {
1484 // set per-pid dns for attached secondary nets
1485 List pids = mNetRequestersPids[netType];
1486 for (int y=0; y< pids.size(); y++) {
1487 Integer pid = (Integer)pids.get(y);
1488 writePidDns(dnses, pid.intValue());
The Android Open Source Project28527d22009-03-03 19:31:44 -08001489 }
1490 }
Robert Greenwalt3afbead2010-07-23 15:46:26 -07001491 bumpDns();
The Android Open Source Project28527d22009-03-03 19:31:44 -08001492 }
Robert Greenwalt2034b912009-08-12 16:08:25 -07001493 }
1494
1495 private int getRestoreDefaultNetworkDelay() {
1496 String restoreDefaultNetworkDelayStr = SystemProperties.get(
1497 NETWORK_RESTORE_DELAY_PROP_NAME);
1498 if(restoreDefaultNetworkDelayStr != null &&
1499 restoreDefaultNetworkDelayStr.length() != 0) {
1500 try {
1501 return Integer.valueOf(restoreDefaultNetworkDelayStr);
1502 } catch (NumberFormatException e) {
1503 }
The Android Open Source Project28527d22009-03-03 19:31:44 -08001504 }
Robert Greenwalt2034b912009-08-12 16:08:25 -07001505 return RESTORE_DEFAULT_NETWORK_DELAY;
The Android Open Source Project28527d22009-03-03 19:31:44 -08001506 }
1507
1508 @Override
1509 protected void dump(FileDescriptor fd, PrintWriter pw, String[] args) {
Robert Greenwalt0659da32009-07-16 17:21:39 -07001510 if (mContext.checkCallingOrSelfPermission(
1511 android.Manifest.permission.DUMP)
The Android Open Source Project28527d22009-03-03 19:31:44 -08001512 != PackageManager.PERMISSION_GRANTED) {
Robert Greenwalt0659da32009-07-16 17:21:39 -07001513 pw.println("Permission Denial: can't dump ConnectivityService " +
1514 "from from pid=" + Binder.getCallingPid() + ", uid=" +
1515 Binder.getCallingUid());
The Android Open Source Project28527d22009-03-03 19:31:44 -08001516 return;
1517 }
The Android Open Source Project28527d22009-03-03 19:31:44 -08001518 pw.println();
1519 for (NetworkStateTracker nst : mNetTrackers) {
Robert Greenwalt3eeb6032009-12-21 18:24:07 -08001520 if (nst != null) {
1521 if (nst.getNetworkInfo().isConnected()) {
1522 pw.println("Active network: " + nst.getNetworkInfo().
1523 getTypeName());
1524 }
1525 pw.println(nst.getNetworkInfo());
1526 pw.println(nst);
1527 pw.println();
Robert Greenwalt2034b912009-08-12 16:08:25 -07001528 }
The Android Open Source Project28527d22009-03-03 19:31:44 -08001529 }
Robert Greenwalt3eeb6032009-12-21 18:24:07 -08001530
1531 pw.println("Network Requester Pids:");
1532 for (int net : mPriorityList) {
1533 String pidString = net + ": ";
1534 for (Object pid : mNetRequestersPids[net]) {
1535 pidString = pidString + pid.toString() + ", ";
1536 }
1537 pw.println(pidString);
1538 }
1539 pw.println();
1540
1541 pw.println("FeatureUsers:");
1542 for (Object requester : mFeatureUsers) {
1543 pw.println(requester.toString());
1544 }
1545 pw.println();
Robert Greenwalt8e87f122010-02-11 18:18:40 -08001546
Robert Greenwalt93dc1042010-06-15 12:19:37 -07001547 synchronized (this) {
1548 pw.println("NetworkTranstionWakeLock is currently " +
1549 (mNetTransitionWakeLock.isHeld() ? "" : "not ") + "held.");
1550 pw.println("It was last requested for "+mNetTransitionWakeLockCausedBy);
1551 }
1552 pw.println();
1553
Robert Greenwalt8e87f122010-02-11 18:18:40 -08001554 mTethering.dump(fd, pw, args);
The Android Open Source Project28527d22009-03-03 19:31:44 -08001555 }
1556
Robert Greenwalt2034b912009-08-12 16:08:25 -07001557 // must be stateless - things change under us.
The Android Open Source Project28527d22009-03-03 19:31:44 -08001558 private class MyHandler extends Handler {
1559 @Override
1560 public void handleMessage(Message msg) {
1561 NetworkInfo info;
1562 switch (msg.what) {
1563 case NetworkStateTracker.EVENT_STATE_CHANGED:
1564 info = (NetworkInfo) msg.obj;
Robert Greenwalt12c44552009-12-07 11:33:18 -08001565 int type = info.getType();
1566 NetworkInfo.State state = info.getState();
Robert Greenwalt24e2d2b2010-01-25 16:14:00 -08001567 // only do this optimization for wifi. It going into scan mode for location
1568 // services generates alot of noise. Meanwhile the mms apn won't send out
1569 // subsequent notifications when on default cellular because it never
1570 // disconnects.. so only do this to wifi notifications. Fixed better when the
1571 // APN notifications are standardized.
1572 if (mNetAttributes[type].mLastState == state &&
1573 mNetAttributes[type].mRadio == ConnectivityManager.TYPE_WIFI) {
Robert Greenwalt12c44552009-12-07 11:33:18 -08001574 if (DBG) {
Robert Greenwalt24e2d2b2010-01-25 16:14:00 -08001575 // TODO - remove this after we validate the dropping doesn't break
1576 // anything
Joe Onoratoc2386bb2010-02-26 18:56:32 -08001577 Slog.d(TAG, "Dropping ConnectivityChange for " +
Robert Greenwalt2adbc7f2010-01-13 09:36:31 -08001578 info.getTypeName() + ": " +
Robert Greenwalt12c44552009-12-07 11:33:18 -08001579 state + "/" + info.getDetailedState());
1580 }
1581 return;
1582 }
1583 mNetAttributes[type].mLastState = state;
1584
Joe Onoratoc2386bb2010-02-26 18:56:32 -08001585 if (DBG) Slog.d(TAG, "ConnectivityChange for " +
Robert Greenwalt0659da32009-07-16 17:21:39 -07001586 info.getTypeName() + ": " +
Robert Greenwalt12c44552009-12-07 11:33:18 -08001587 state + "/" + info.getDetailedState());
The Android Open Source Project28527d22009-03-03 19:31:44 -08001588
1589 // Connectivity state changed:
1590 // [31-13] Reserved for future use
Robert Greenwalt0659da32009-07-16 17:21:39 -07001591 // [12-9] Network subtype (for mobile network, as defined
1592 // by TelephonyManager)
1593 // [8-3] Detailed state ordinal (as defined by
1594 // NetworkInfo.DetailedState)
The Android Open Source Project28527d22009-03-03 19:31:44 -08001595 // [2-0] Network type (as defined by ConnectivityManager)
1596 int eventLogParam = (info.getType() & 0x7) |
1597 ((info.getDetailedState().ordinal() & 0x3f) << 3) |
1598 (info.getSubtype() << 9);
Doug Zongker2fc96232009-12-04 10:31:43 -08001599 EventLog.writeEvent(EventLogTags.CONNECTIVITY_STATE_CHANGED,
Robert Greenwalt0659da32009-07-16 17:21:39 -07001600 eventLogParam);
1601
1602 if (info.getDetailedState() ==
1603 NetworkInfo.DetailedState.FAILED) {
The Android Open Source Project28527d22009-03-03 19:31:44 -08001604 handleConnectionFailure(info);
Robert Greenwalt12c44552009-12-07 11:33:18 -08001605 } else if (state == NetworkInfo.State.DISCONNECTED) {
The Android Open Source Project28527d22009-03-03 19:31:44 -08001606 handleDisconnect(info);
Robert Greenwalt12c44552009-12-07 11:33:18 -08001607 } else if (state == NetworkInfo.State.SUSPENDED) {
The Android Open Source Project28527d22009-03-03 19:31:44 -08001608 // TODO: need to think this over.
Robert Greenwalt0659da32009-07-16 17:21:39 -07001609 // the logic here is, handle SUSPENDED the same as
1610 // DISCONNECTED. The only difference being we are
1611 // broadcasting an intent with NetworkInfo that's
1612 // suspended. This allows the applications an
1613 // opportunity to handle DISCONNECTED and SUSPENDED
1614 // differently, or not.
The Android Open Source Project28527d22009-03-03 19:31:44 -08001615 handleDisconnect(info);
Robert Greenwalt12c44552009-12-07 11:33:18 -08001616 } else if (state == NetworkInfo.State.CONNECTED) {
The Android Open Source Project28527d22009-03-03 19:31:44 -08001617 handleConnect(info);
1618 }
The Android Open Source Project28527d22009-03-03 19:31:44 -08001619 break;
The Android Open Source Project28527d22009-03-03 19:31:44 -08001620 case NetworkStateTracker.EVENT_CONFIGURATION_CHANGED:
Robert Greenwalt3afbead2010-07-23 15:46:26 -07001621 // TODO - make this handle ip/proxy/gateway/dns changes
1622 info = (NetworkInfo) msg.obj;
1623 type = info.getType();
1624 handleDnsConfigurationChange(type);
The Android Open Source Project28527d22009-03-03 19:31:44 -08001625 break;
Robert Greenwalt2034b912009-08-12 16:08:25 -07001626 case NetworkStateTracker.EVENT_RESTORE_DEFAULT_NETWORK:
Robert Greenwaltaffc3a12009-09-27 17:27:04 -07001627 FeatureUser u = (FeatureUser)msg.obj;
1628 u.expire();
Robert Greenwalt2034b912009-08-12 16:08:25 -07001629 break;
Robert Greenwalt93dc1042010-06-15 12:19:37 -07001630 case NetworkStateTracker.EVENT_CLEAR_NET_TRANSITION_WAKELOCK:
1631 String causedBy = null;
1632 synchronized (ConnectivityService.this) {
1633 if (msg.arg1 == mNetTransitionWakeLockSerialNumber &&
1634 mNetTransitionWakeLock.isHeld()) {
1635 mNetTransitionWakeLock.release();
1636 causedBy = mNetTransitionWakeLockCausedBy;
1637 }
1638 }
1639 if (causedBy != null) {
1640 Slog.d(TAG, "NetTransition Wakelock for " +
1641 causedBy + " released by timeout");
1642 }
1643 break;
The Android Open Source Project28527d22009-03-03 19:31:44 -08001644 }
1645 }
1646 }
Robert Greenwalt0c4828c2010-01-26 11:40:34 -08001647
1648 // javadoc from interface
Robert Greenwalt4283ded2010-03-02 17:25:02 -08001649 public int tether(String iface) {
Robert Greenwalt0c4828c2010-01-26 11:40:34 -08001650 enforceTetherChangePermission();
Robert Greenwalt4283ded2010-03-02 17:25:02 -08001651
1652 if (isTetheringSupported()) {
1653 return mTethering.tether(iface);
1654 } else {
1655 return ConnectivityManager.TETHER_ERROR_UNSUPPORTED;
1656 }
Robert Greenwalt0c4828c2010-01-26 11:40:34 -08001657 }
1658
1659 // javadoc from interface
Robert Greenwalt4283ded2010-03-02 17:25:02 -08001660 public int untether(String iface) {
Robert Greenwalt0c4828c2010-01-26 11:40:34 -08001661 enforceTetherChangePermission();
Robert Greenwalt4283ded2010-03-02 17:25:02 -08001662
1663 if (isTetheringSupported()) {
1664 return mTethering.untether(iface);
1665 } else {
1666 return ConnectivityManager.TETHER_ERROR_UNSUPPORTED;
1667 }
1668 }
1669
1670 // javadoc from interface
1671 public int getLastTetherError(String iface) {
1672 enforceTetherAccessPermission();
1673
1674 if (isTetheringSupported()) {
1675 return mTethering.getLastTetherError(iface);
1676 } else {
1677 return ConnectivityManager.TETHER_ERROR_UNSUPPORTED;
1678 }
Robert Greenwalt8e87f122010-02-11 18:18:40 -08001679 }
1680
1681 // TODO - proper iface API for selection by property, inspection, etc
1682 public String[] getTetherableUsbRegexs() {
1683 enforceTetherAccessPermission();
1684 if (isTetheringSupported()) {
1685 return mTethering.getTetherableUsbRegexs();
1686 } else {
1687 return new String[0];
1688 }
1689 }
1690
1691 public String[] getTetherableWifiRegexs() {
1692 enforceTetherAccessPermission();
1693 if (isTetheringSupported()) {
1694 return mTethering.getTetherableWifiRegexs();
1695 } else {
1696 return new String[0];
1697 }
Robert Greenwalt0c4828c2010-01-26 11:40:34 -08001698 }
1699
Danica Chang96567052010-08-11 14:54:43 -07001700 public String[] getTetherableBluetoothRegexs() {
1701 enforceTetherAccessPermission();
1702 if (isTetheringSupported()) {
1703 return mTethering.getTetherableBluetoothRegexs();
1704 } else {
1705 return new String[0];
1706 }
1707 }
1708
Robert Greenwalt0c4828c2010-01-26 11:40:34 -08001709 // TODO - move iface listing, queries, etc to new module
1710 // javadoc from interface
1711 public String[] getTetherableIfaces() {
Robert Greenwalt8e87f122010-02-11 18:18:40 -08001712 enforceTetherAccessPermission();
Robert Greenwalt0c4828c2010-01-26 11:40:34 -08001713 return mTethering.getTetherableIfaces();
1714 }
1715
1716 public String[] getTetheredIfaces() {
Robert Greenwalt8e87f122010-02-11 18:18:40 -08001717 enforceTetherAccessPermission();
Robert Greenwalt0c4828c2010-01-26 11:40:34 -08001718 return mTethering.getTetheredIfaces();
1719 }
Robert Greenwalt8e87f122010-02-11 18:18:40 -08001720
Robert Greenwalt4283ded2010-03-02 17:25:02 -08001721 public String[] getTetheringErroredIfaces() {
1722 enforceTetherAccessPermission();
1723 return mTethering.getErroredIfaces();
1724 }
1725
Robert Greenwalt8e87f122010-02-11 18:18:40 -08001726 // if ro.tether.denied = true we default to no tethering
1727 // gservices could set the secure setting to 1 though to enable it on a build where it
1728 // had previously been turned off.
1729 public boolean isTetheringSupported() {
1730 enforceTetherAccessPermission();
1731 int defaultVal = (SystemProperties.get("ro.tether.denied").equals("true") ? 0 : 1);
Robert Greenwaltf1b66e12010-02-25 12:29:30 -08001732 boolean tetherEnabledInSettings = (Settings.Secure.getInt(mContext.getContentResolver(),
1733 Settings.Secure.TETHER_SUPPORTED, defaultVal) != 0);
1734 return tetherEnabledInSettings && mTetheringConfigValid;
Robert Greenwalt8e87f122010-02-11 18:18:40 -08001735 }
Robert Greenwalt93dc1042010-06-15 12:19:37 -07001736
1737 // An API NetworkStateTrackers can call when they lose their network.
1738 // This will automatically be cleared after X seconds or a network becomes CONNECTED,
1739 // whichever happens first. The timer is started by the first caller and not
1740 // restarted by subsequent callers.
1741 public void requestNetworkTransitionWakelock(String forWhom) {
1742 enforceConnectivityInternalPermission();
1743 synchronized (this) {
1744 if (mNetTransitionWakeLock.isHeld()) return;
1745 mNetTransitionWakeLockSerialNumber++;
1746 mNetTransitionWakeLock.acquire();
1747 mNetTransitionWakeLockCausedBy = forWhom;
1748 }
1749 mHandler.sendMessageDelayed(mHandler.obtainMessage(
1750 NetworkStateTracker.EVENT_CLEAR_NET_TRANSITION_WAKELOCK,
1751 mNetTransitionWakeLockSerialNumber, 0),
1752 mNetTransitionWakeLockTimeout);
1753 return;
1754 }
The Android Open Source Project28527d22009-03-03 19:31:44 -08001755}