blob: 9c504feac92bd6739c7323f8b5969ab68e0fac0d [file] [log] [blame]
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001/*
2 * Copyright (C) 2008 The Android Open Source Project
3 *
4 * Licensed under the Apache License, Version 2.0 (the "License");
5 * you may not use this file except in compliance with the License.
6 * You may obtain a copy of the License at
7 *
8 * http://www.apache.org/licenses/LICENSE-2.0
9 *
10 * Unless required by applicable law or agreed to in writing, software
11 * distributed under the License is distributed on an "AS IS" BASIS,
12 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13 * See the License for the specific language governing permissions and
14 * limitations under the License.
15 */
16
17package com.android.server;
18
19import android.app.Notification;
20import android.app.NotificationManager;
21import android.content.ContentResolver;
22import android.content.Context;
23import android.content.Intent;
24import android.content.pm.PackageManager;
25import android.net.ConnectivityManager;
26import android.net.IConnectivityManager;
27import android.net.MobileDataStateTracker;
28import android.net.NetworkInfo;
Robert Greenwalt47f69fe2010-06-15 15:43:39 -070029import android.net.NetworkProperties;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080030import android.net.NetworkStateTracker;
31import android.net.wifi.WifiStateTracker;
Irfan Sheriffd649c122010-06-09 15:39:36 -070032import android.net.NetworkUtils;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080033import android.os.Binder;
34import android.os.Handler;
Robert Greenwalt42acef32009-08-12 16:08:25 -070035import android.os.IBinder;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080036import android.os.Looper;
37import android.os.Message;
Robert Greenwalt14f2ef42010-06-15 12:19:37 -070038import android.os.PowerManager;
Robert Greenwalt42acef32009-08-12 16:08:25 -070039import android.os.RemoteException;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080040import android.os.ServiceManager;
41import android.os.SystemProperties;
42import android.provider.Settings;
Robert Greenwalt42acef32009-08-12 16:08:25 -070043import android.text.TextUtils;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080044import android.util.EventLog;
Joe Onorato8a9b2202010-02-26 18:56:32 -080045import android.util.Slog;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080046
Robert Greenwalt42acef32009-08-12 16:08:25 -070047import com.android.internal.telephony.Phone;
48
Robert Greenwaltd0e18ff2010-01-26 11:40:34 -080049import com.android.server.connectivity.Tethering;
50
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080051import java.io.FileDescriptor;
Irfan Sheriffd649c122010-06-09 15:39:36 -070052import java.io.FileWriter;
53import java.io.IOException;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080054import java.io.PrintWriter;
Robert Greenwalt47f69fe2010-06-15 15:43:39 -070055import java.net.InetAddress;
56import java.net.UnknownHostException;
Robert Greenwalt42acef32009-08-12 16:08:25 -070057import java.util.ArrayList;
Robert Greenwalt47f69fe2010-06-15 15:43:39 -070058import java.util.Collection;
Robert Greenwalt42acef32009-08-12 16:08:25 -070059import java.util.List;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080060
61/**
62 * @hide
63 */
64public class ConnectivityService extends IConnectivityManager.Stub {
65
Robert Greenwaltd8df1492009-10-06 14:12:53 -070066 private static final boolean DBG = true;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080067 private static final String TAG = "ConnectivityService";
68
Robert Greenwalt42acef32009-08-12 16:08:25 -070069 // how long to wait before switching back to a radio's default network
70 private static final int RESTORE_DEFAULT_NETWORK_DELAY = 1 * 60 * 1000;
71 // system property that can override the above value
72 private static final String NETWORK_RESTORE_DELAY_PROP_NAME =
73 "android.telephony.apn-restore";
74
Robert Greenwaltd0e18ff2010-01-26 11:40:34 -080075 private Tethering mTethering;
Robert Greenwaltc9d5fb72010-02-25 12:29:30 -080076 private boolean mTetheringConfigValid = false;
Robert Greenwaltd0e18ff2010-01-26 11:40:34 -080077
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080078 /**
79 * Sometimes we want to refer to the individual network state
80 * trackers separately, and sometimes we just want to treat them
81 * abstractly.
82 */
83 private NetworkStateTracker mNetTrackers[];
Robert Greenwalt42acef32009-08-12 16:08:25 -070084
85 /**
86 * A per Net list of the PID's that requested access to the net
87 * used both as a refcount and for per-PID DNS selection
88 */
89 private List mNetRequestersPids[];
90
Robert Greenwalt42acef32009-08-12 16:08:25 -070091 // priority order of the nettrackers
92 // (excluding dynamically set mNetworkPreference)
93 // TODO - move mNetworkTypePreference into this
94 private int[] mPriorityList;
95
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080096 private Context mContext;
97 private int mNetworkPreference;
Robert Greenwalt42acef32009-08-12 16:08:25 -070098 private int mActiveDefaultNetwork = -1;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080099
100 private int mNumDnsEntries;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800101
102 private boolean mTestMode;
103 private static ConnectivityService sServiceInstance;
104
Robert Greenwalt42acef32009-08-12 16:08:25 -0700105 private Handler mHandler;
106
107 // list of DeathRecipients used to make sure features are turned off when
108 // a process dies
109 private List mFeatureUsers;
110
Mike Lockwood0f79b542009-08-14 14:18:49 -0400111 private boolean mSystemReady;
Dianne Hackborn1c633fc2009-12-08 19:45:14 -0800112 private Intent mInitialBroadcast;
Mike Lockwood0f79b542009-08-14 14:18:49 -0400113
Robert Greenwalt14f2ef42010-06-15 12:19:37 -0700114 private PowerManager.WakeLock mNetTransitionWakeLock;
115 private String mNetTransitionWakeLockCausedBy = "";
116 private int mNetTransitionWakeLockSerialNumber;
117 private int mNetTransitionWakeLockTimeout;
118
Robert Greenwalt511288a2009-12-07 11:33:18 -0800119 private static class NetworkAttributes {
Robert Greenwalt42acef32009-08-12 16:08:25 -0700120 /**
121 * Class for holding settings read from resources.
122 */
123 public String mName;
124 public int mType;
125 public int mRadio;
126 public int mPriority;
Robert Greenwalt511288a2009-12-07 11:33:18 -0800127 public NetworkInfo.State mLastState;
Robert Greenwalt42acef32009-08-12 16:08:25 -0700128 public NetworkAttributes(String init) {
129 String fragments[] = init.split(",");
130 mName = fragments[0].toLowerCase();
Robert Greenwalt5154ae762009-10-30 14:17:42 -0700131 mType = Integer.parseInt(fragments[1]);
132 mRadio = Integer.parseInt(fragments[2]);
133 mPriority = Integer.parseInt(fragments[3]);
Robert Greenwalt511288a2009-12-07 11:33:18 -0800134 mLastState = NetworkInfo.State.UNKNOWN;
Robert Greenwalt42acef32009-08-12 16:08:25 -0700135 }
136 public boolean isDefault() {
137 return (mType == mRadio);
138 }
139 }
140 NetworkAttributes[] mNetAttributes;
Robert Greenwalt5154ae762009-10-30 14:17:42 -0700141 int mNetworksDefined;
Robert Greenwalt42acef32009-08-12 16:08:25 -0700142
Robert Greenwalt511288a2009-12-07 11:33:18 -0800143 private static class RadioAttributes {
Robert Greenwalt42acef32009-08-12 16:08:25 -0700144 public int mSimultaneity;
145 public int mType;
146 public RadioAttributes(String init) {
147 String fragments[] = init.split(",");
Robert Greenwalt5154ae762009-10-30 14:17:42 -0700148 mType = Integer.parseInt(fragments[0]);
149 mSimultaneity = Integer.parseInt(fragments[1]);
Robert Greenwalt42acef32009-08-12 16:08:25 -0700150 }
151 }
152 RadioAttributes[] mRadioAttributes;
153
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800154 private static class ConnectivityThread extends Thread {
155 private Context mContext;
Robert Greenwalt86e9e552009-07-16 17:21:39 -0700156
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800157 private ConnectivityThread(Context context) {
158 super("ConnectivityThread");
159 mContext = context;
160 }
161
162 @Override
163 public void run() {
164 Looper.prepare();
165 synchronized (this) {
166 sServiceInstance = new ConnectivityService(mContext);
167 notifyAll();
168 }
169 Looper.loop();
170 }
Robert Greenwalt86e9e552009-07-16 17:21:39 -0700171
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800172 public static ConnectivityService getServiceInstance(Context context) {
173 ConnectivityThread thread = new ConnectivityThread(context);
174 thread.start();
Robert Greenwalt86e9e552009-07-16 17:21:39 -0700175
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800176 synchronized (thread) {
177 while (sServiceInstance == null) {
178 try {
179 // Wait until sServiceInstance has been initialized.
180 thread.wait();
181 } catch (InterruptedException ignore) {
Joe Onorato8a9b2202010-02-26 18:56:32 -0800182 Slog.e(TAG,
Robert Greenwalt86e9e552009-07-16 17:21:39 -0700183 "Unexpected InterruptedException while waiting"+
184 " for ConnectivityService thread");
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800185 }
186 }
187 }
Robert Greenwalt86e9e552009-07-16 17:21:39 -0700188
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800189 return sServiceInstance;
190 }
191 }
Robert Greenwalt86e9e552009-07-16 17:21:39 -0700192
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800193 public static ConnectivityService getInstance(Context context) {
194 return ConnectivityThread.getServiceInstance(context);
195 }
Robert Greenwalt86e9e552009-07-16 17:21:39 -0700196
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800197 private ConnectivityService(Context context) {
Joe Onorato8a9b2202010-02-26 18:56:32 -0800198 if (DBG) Slog.v(TAG, "ConnectivityService starting up");
Robert Greenwaltde8383c2010-01-14 17:47:58 -0800199
200 // setup our unique device name
201 String id = Settings.Secure.getString(context.getContentResolver(),
202 Settings.Secure.ANDROID_ID);
203 if (id != null && id.length() > 0) {
204 String name = new String("android_").concat(id);
205 SystemProperties.set("net.hostname", name);
206 }
207
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800208 mContext = context;
Robert Greenwalt14f2ef42010-06-15 12:19:37 -0700209
210 PowerManager powerManager = (PowerManager)mContext.getSystemService(Context.POWER_SERVICE);
211 mNetTransitionWakeLock = powerManager.newWakeLock(PowerManager.PARTIAL_WAKE_LOCK, TAG);
212 mNetTransitionWakeLockTimeout = mContext.getResources().getInteger(
213 com.android.internal.R.integer.config_networkTransitionTimeout);
214
Robert Greenwalt42acef32009-08-12 16:08:25 -0700215 mNetTrackers = new NetworkStateTracker[
216 ConnectivityManager.MAX_NETWORK_TYPE+1];
217 mHandler = new MyHandler();
Robert Greenwalt86e9e552009-07-16 17:21:39 -0700218
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800219 mNetworkPreference = getPersistedNetworkPreference();
Robert Greenwalt86e9e552009-07-16 17:21:39 -0700220
Robert Greenwalt5154ae762009-10-30 14:17:42 -0700221 mRadioAttributes = new RadioAttributes[ConnectivityManager.MAX_RADIO_TYPE+1];
222 mNetAttributes = new NetworkAttributes[ConnectivityManager.MAX_NETWORK_TYPE+1];
223
Robert Greenwalt42acef32009-08-12 16:08:25 -0700224 // Load device network attributes from resources
Robert Greenwalt42acef32009-08-12 16:08:25 -0700225 String[] raStrings = context.getResources().getStringArray(
226 com.android.internal.R.array.radioAttributes);
Robert Greenwalt5154ae762009-10-30 14:17:42 -0700227 for (String raString : raStrings) {
228 RadioAttributes r = new RadioAttributes(raString);
229 if (r.mType > ConnectivityManager.MAX_RADIO_TYPE) {
Joe Onorato8a9b2202010-02-26 18:56:32 -0800230 Slog.e(TAG, "Error in radioAttributes - ignoring attempt to define type " + r.mType);
Robert Greenwalt5154ae762009-10-30 14:17:42 -0700231 continue;
232 }
233 if (mRadioAttributes[r.mType] != null) {
Joe Onorato8a9b2202010-02-26 18:56:32 -0800234 Slog.e(TAG, "Error in radioAttributes - ignoring attempt to redefine type " +
Robert Greenwalt5154ae762009-10-30 14:17:42 -0700235 r.mType);
236 continue;
237 }
Robert Greenwalt42acef32009-08-12 16:08:25 -0700238 mRadioAttributes[r.mType] = r;
239 }
240
Robert Greenwalt5154ae762009-10-30 14:17:42 -0700241 String[] naStrings = context.getResources().getStringArray(
242 com.android.internal.R.array.networkAttributes);
243 for (String naString : naStrings) {
244 try {
245 NetworkAttributes n = new NetworkAttributes(naString);
246 if (n.mType > ConnectivityManager.MAX_NETWORK_TYPE) {
Joe Onorato8a9b2202010-02-26 18:56:32 -0800247 Slog.e(TAG, "Error in networkAttributes - ignoring attempt to define type " +
Robert Greenwalt5154ae762009-10-30 14:17:42 -0700248 n.mType);
249 continue;
Robert Greenwalt42acef32009-08-12 16:08:25 -0700250 }
Robert Greenwalt5154ae762009-10-30 14:17:42 -0700251 if (mNetAttributes[n.mType] != null) {
Joe Onorato8a9b2202010-02-26 18:56:32 -0800252 Slog.e(TAG, "Error in networkAttributes - ignoring attempt to redefine type " +
Robert Greenwalt5154ae762009-10-30 14:17:42 -0700253 n.mType);
254 continue;
255 }
256 if (mRadioAttributes[n.mRadio] == null) {
Joe Onorato8a9b2202010-02-26 18:56:32 -0800257 Slog.e(TAG, "Error in networkAttributes - ignoring attempt to use undefined " +
Robert Greenwalt5154ae762009-10-30 14:17:42 -0700258 "radio " + n.mRadio + " in network type " + n.mType);
259 continue;
260 }
261 mNetAttributes[n.mType] = n;
262 mNetworksDefined++;
263 } catch(Exception e) {
264 // ignore it - leave the entry null
Robert Greenwalt42acef32009-08-12 16:08:25 -0700265 }
266 }
267
Robert Greenwalt5154ae762009-10-30 14:17:42 -0700268 // high priority first
269 mPriorityList = new int[mNetworksDefined];
270 {
271 int insertionPoint = mNetworksDefined-1;
272 int currentLowest = 0;
273 int nextLowest = 0;
274 while (insertionPoint > -1) {
275 for (NetworkAttributes na : mNetAttributes) {
276 if (na == null) continue;
277 if (na.mPriority < currentLowest) continue;
278 if (na.mPriority > currentLowest) {
279 if (na.mPriority < nextLowest || nextLowest == 0) {
280 nextLowest = na.mPriority;
281 }
282 continue;
283 }
284 mPriorityList[insertionPoint--] = na.mType;
285 }
286 currentLowest = nextLowest;
287 nextLowest = 0;
288 }
289 }
290
291 mNetRequestersPids = new ArrayList[ConnectivityManager.MAX_NETWORK_TYPE+1];
292 for (int i : mPriorityList) {
Robert Greenwalt42acef32009-08-12 16:08:25 -0700293 mNetRequestersPids[i] = new ArrayList();
294 }
295
296 mFeatureUsers = new ArrayList();
297
Robert Greenwalt5154ae762009-10-30 14:17:42 -0700298 mNumDnsEntries = 0;
299
300 mTestMode = SystemProperties.get("cm.test.mode").equals("true")
301 && SystemProperties.get("ro.build.type").equals("eng");
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800302 /*
303 * Create the network state trackers for Wi-Fi and mobile
304 * data. Maybe this could be done with a factory class,
305 * but it's not clear that it's worth it, given that
306 * the number of different network types is not going
307 * to change very often.
308 */
Robert Greenwaltc03fa502010-02-23 18:58:05 -0800309 boolean noMobileData = !getMobileDataEnabled();
Robert Greenwalt5154ae762009-10-30 14:17:42 -0700310 for (int netType : mPriorityList) {
311 switch (mNetAttributes[netType].mRadio) {
312 case ConnectivityManager.TYPE_WIFI:
Joe Onorato8a9b2202010-02-26 18:56:32 -0800313 if (DBG) Slog.v(TAG, "Starting Wifi Service.");
Robert Greenwalt5154ae762009-10-30 14:17:42 -0700314 WifiStateTracker wst = new WifiStateTracker(context, mHandler);
315 WifiService wifiService = new WifiService(context, wst);
316 ServiceManager.addService(Context.WIFI_SERVICE, wifiService);
Irfan Sheriff7b009782010-03-11 16:37:45 -0800317 wifiService.startWifi();
Robert Greenwalt5154ae762009-10-30 14:17:42 -0700318 mNetTrackers[ConnectivityManager.TYPE_WIFI] = wst;
319 wst.startMonitoring();
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800320
Robert Greenwalt5154ae762009-10-30 14:17:42 -0700321 break;
322 case ConnectivityManager.TYPE_MOBILE:
323 mNetTrackers[netType] = new MobileDataStateTracker(context, mHandler,
324 netType, mNetAttributes[netType].mName);
325 mNetTrackers[netType].startMonitoring();
Robert Greenwaltc03fa502010-02-23 18:58:05 -0800326 if (noMobileData) {
Joe Onorato8a9b2202010-02-26 18:56:32 -0800327 if (DBG) Slog.d(TAG, "tearing down Mobile networks due to setting");
Robert Greenwaltc03fa502010-02-23 18:58:05 -0800328 mNetTrackers[netType].teardown();
329 }
Robert Greenwalt5154ae762009-10-30 14:17:42 -0700330 break;
331 default:
Joe Onorato8a9b2202010-02-26 18:56:32 -0800332 Slog.e(TAG, "Trying to create a DataStateTracker for an unknown radio type " +
Robert Greenwalt5154ae762009-10-30 14:17:42 -0700333 mNetAttributes[netType].mRadio);
334 continue;
335 }
336 }
Robert Greenwaltd0e18ff2010-01-26 11:40:34 -0800337
Robert Greenwaltdfadaea2010-03-11 15:03:08 -0800338 mTethering = new Tethering(mContext, mHandler.getLooper());
Robert Greenwaltc9d5fb72010-02-25 12:29:30 -0800339 mTetheringConfigValid = (((mNetTrackers[ConnectivityManager.TYPE_MOBILE_DUN] != null) ||
340 !mTethering.isDunRequired()) &&
341 (mTethering.getTetherableUsbRegexs().length != 0 ||
342 mTethering.getTetherableWifiRegexs().length != 0) &&
343 mTethering.getUpstreamIfaceRegexs().length != 0);
344
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800345 }
346
Robert Greenwalt5154ae762009-10-30 14:17:42 -0700347
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800348 /**
Robert Greenwalt86e9e552009-07-16 17:21:39 -0700349 * Sets the preferred network.
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800350 * @param preference the new preference
351 */
352 public synchronized void setNetworkPreference(int preference) {
353 enforceChangePermission();
Robert Greenwalt42acef32009-08-12 16:08:25 -0700354 if (ConnectivityManager.isNetworkTypeValid(preference) &&
Robert Greenwalt5154ae762009-10-30 14:17:42 -0700355 mNetAttributes[preference] != null &&
Robert Greenwalt42acef32009-08-12 16:08:25 -0700356 mNetAttributes[preference].isDefault()) {
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800357 if (mNetworkPreference != preference) {
358 persistNetworkPreference(preference);
359 mNetworkPreference = preference;
360 enforcePreference();
361 }
362 }
363 }
364
365 public int getNetworkPreference() {
366 enforceAccessPermission();
367 return mNetworkPreference;
368 }
369
370 private void persistNetworkPreference(int networkPreference) {
371 final ContentResolver cr = mContext.getContentResolver();
Robert Greenwalt86e9e552009-07-16 17:21:39 -0700372 Settings.Secure.putInt(cr, Settings.Secure.NETWORK_PREFERENCE,
373 networkPreference);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800374 }
Robert Greenwalt86e9e552009-07-16 17:21:39 -0700375
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800376 private int getPersistedNetworkPreference() {
377 final ContentResolver cr = mContext.getContentResolver();
378
379 final int networkPrefSetting = Settings.Secure
380 .getInt(cr, Settings.Secure.NETWORK_PREFERENCE, -1);
381 if (networkPrefSetting != -1) {
382 return networkPrefSetting;
383 }
384
385 return ConnectivityManager.DEFAULT_NETWORK_PREFERENCE;
386 }
Robert Greenwalt86e9e552009-07-16 17:21:39 -0700387
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800388 /**
Robert Greenwalt86e9e552009-07-16 17:21:39 -0700389 * Make the state of network connectivity conform to the preference settings
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800390 * In this method, we only tear down a non-preferred network. Establishing
391 * a connection to the preferred network is taken care of when we handle
392 * the disconnect event from the non-preferred network
393 * (see {@link #handleDisconnect(NetworkInfo)}).
394 */
395 private void enforcePreference() {
Robert Greenwalt42acef32009-08-12 16:08:25 -0700396 if (mNetTrackers[mNetworkPreference].getNetworkInfo().isConnected())
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800397 return;
398
Robert Greenwalt42acef32009-08-12 16:08:25 -0700399 if (!mNetTrackers[mNetworkPreference].isAvailable())
400 return;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800401
Robert Greenwalt42acef32009-08-12 16:08:25 -0700402 for (int t=0; t <= ConnectivityManager.MAX_RADIO_TYPE; t++) {
Robert Greenwalt5154ae762009-10-30 14:17:42 -0700403 if (t != mNetworkPreference && mNetTrackers[t] != null &&
Robert Greenwalt42acef32009-08-12 16:08:25 -0700404 mNetTrackers[t].getNetworkInfo().isConnected()) {
Robert Greenwaltec9fe462009-08-20 15:25:14 -0700405 if (DBG) {
Joe Onorato8a9b2202010-02-26 18:56:32 -0800406 Slog.d(TAG, "tearing down " +
Robert Greenwaltec9fe462009-08-20 15:25:14 -0700407 mNetTrackers[t].getNetworkInfo() +
408 " in enforcePreference");
409 }
Robert Greenwalt42acef32009-08-12 16:08:25 -0700410 teardown(mNetTrackers[t]);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800411 }
412 }
413 }
414
415 private boolean teardown(NetworkStateTracker netTracker) {
416 if (netTracker.teardown()) {
417 netTracker.setTeardownRequested(true);
418 return true;
419 } else {
420 return false;
421 }
422 }
423
424 /**
425 * Return NetworkInfo for the active (i.e., connected) network interface.
426 * It is assumed that at most one network is active at a time. If more
427 * than one is active, it is indeterminate which will be returned.
Robert Greenwalt86e9e552009-07-16 17:21:39 -0700428 * @return the info for the active network, or {@code null} if none is
429 * active
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800430 */
431 public NetworkInfo getActiveNetworkInfo() {
432 enforceAccessPermission();
Robert Greenwalt42acef32009-08-12 16:08:25 -0700433 for (int type=0; type <= ConnectivityManager.MAX_NETWORK_TYPE; type++) {
Robert Greenwalt5154ae762009-10-30 14:17:42 -0700434 if (mNetAttributes[type] == null || !mNetAttributes[type].isDefault()) {
Robert Greenwalt42acef32009-08-12 16:08:25 -0700435 continue;
436 }
437 NetworkStateTracker t = mNetTrackers[type];
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800438 NetworkInfo info = t.getNetworkInfo();
439 if (info.isConnected()) {
Joe Onorato8a9b2202010-02-26 18:56:32 -0800440 if (DBG && type != mActiveDefaultNetwork) Slog.e(TAG,
Robert Greenwalt42acef32009-08-12 16:08:25 -0700441 "connected default network is not " +
442 "mActiveDefaultNetwork!");
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800443 return info;
444 }
445 }
446 return null;
447 }
448
449 public NetworkInfo getNetworkInfo(int networkType) {
450 enforceAccessPermission();
451 if (ConnectivityManager.isNetworkTypeValid(networkType)) {
452 NetworkStateTracker t = mNetTrackers[networkType];
453 if (t != null)
454 return t.getNetworkInfo();
455 }
456 return null;
457 }
458
459 public NetworkInfo[] getAllNetworkInfo() {
460 enforceAccessPermission();
Robert Greenwalt5154ae762009-10-30 14:17:42 -0700461 NetworkInfo[] result = new NetworkInfo[mNetworksDefined];
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800462 int i = 0;
463 for (NetworkStateTracker t : mNetTrackers) {
Robert Greenwalt5154ae762009-10-30 14:17:42 -0700464 if(t != null) result[i++] = t.getNetworkInfo();
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800465 }
466 return result;
467 }
468
469 public boolean setRadios(boolean turnOn) {
470 boolean result = true;
471 enforceChangePermission();
472 for (NetworkStateTracker t : mNetTrackers) {
Robert Greenwalt5154ae762009-10-30 14:17:42 -0700473 if (t != null) result = t.setRadio(turnOn) && result;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800474 }
475 return result;
476 }
477
478 public boolean setRadio(int netType, boolean turnOn) {
479 enforceChangePermission();
480 if (!ConnectivityManager.isNetworkTypeValid(netType)) {
481 return false;
482 }
483 NetworkStateTracker tracker = mNetTrackers[netType];
484 return tracker != null && tracker.setRadio(turnOn);
485 }
486
Robert Greenwalt9c75d4a2009-09-27 17:27:04 -0700487 /**
488 * Used to notice when the calling process dies so we can self-expire
489 *
490 * Also used to know if the process has cleaned up after itself when
491 * our auto-expire timer goes off. The timer has a link to an object.
492 *
493 */
Robert Greenwalt42acef32009-08-12 16:08:25 -0700494 private class FeatureUser implements IBinder.DeathRecipient {
495 int mNetworkType;
496 String mFeature;
497 IBinder mBinder;
498 int mPid;
499 int mUid;
Robert Greenwaltb9285352009-12-21 18:24:07 -0800500 long mCreateTime;
Robert Greenwalt42acef32009-08-12 16:08:25 -0700501
502 FeatureUser(int type, String feature, IBinder binder) {
503 super();
504 mNetworkType = type;
505 mFeature = feature;
506 mBinder = binder;
507 mPid = getCallingPid();
508 mUid = getCallingUid();
Robert Greenwaltb9285352009-12-21 18:24:07 -0800509 mCreateTime = System.currentTimeMillis();
Robert Greenwalt9c75d4a2009-09-27 17:27:04 -0700510
Robert Greenwalt42acef32009-08-12 16:08:25 -0700511 try {
512 mBinder.linkToDeath(this, 0);
513 } catch (RemoteException e) {
514 binderDied();
515 }
516 }
517
518 void unlinkDeathRecipient() {
519 mBinder.unlinkToDeath(this, 0);
520 }
521
522 public void binderDied() {
Joe Onorato8a9b2202010-02-26 18:56:32 -0800523 Slog.d(TAG, "ConnectivityService FeatureUser binderDied(" +
Robert Greenwaltb9285352009-12-21 18:24:07 -0800524 mNetworkType + ", " + mFeature + ", " + mBinder + "), created " +
525 (System.currentTimeMillis() - mCreateTime) + " mSec ago");
Robert Greenwalt9c75d4a2009-09-27 17:27:04 -0700526 stopUsingNetworkFeature(this, false);
Robert Greenwalt42acef32009-08-12 16:08:25 -0700527 }
528
Robert Greenwalt9c75d4a2009-09-27 17:27:04 -0700529 public void expire() {
Joe Onorato8a9b2202010-02-26 18:56:32 -0800530 Slog.d(TAG, "ConnectivityService FeatureUser expire(" +
Robert Greenwaltb9285352009-12-21 18:24:07 -0800531 mNetworkType + ", " + mFeature + ", " + mBinder +"), created " +
532 (System.currentTimeMillis() - mCreateTime) + " mSec ago");
Robert Greenwalt9c75d4a2009-09-27 17:27:04 -0700533 stopUsingNetworkFeature(this, false);
534 }
Robert Greenwaltb9285352009-12-21 18:24:07 -0800535
536 public String toString() {
537 return "FeatureUser("+mNetworkType+","+mFeature+","+mPid+","+mUid+"), created " +
538 (System.currentTimeMillis() - mCreateTime) + " mSec ago";
539 }
Robert Greenwalt42acef32009-08-12 16:08:25 -0700540 }
541
Robert Greenwalt9c75d4a2009-09-27 17:27:04 -0700542 // javadoc from interface
Robert Greenwalt42acef32009-08-12 16:08:25 -0700543 public int startUsingNetworkFeature(int networkType, String feature,
544 IBinder binder) {
545 if (DBG) {
Joe Onorato8a9b2202010-02-26 18:56:32 -0800546 Slog.d(TAG, "startUsingNetworkFeature for net " + networkType +
Robert Greenwalt42acef32009-08-12 16:08:25 -0700547 ": " + feature);
548 }
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800549 enforceChangePermission();
Robert Greenwalt5154ae762009-10-30 14:17:42 -0700550 if (!ConnectivityManager.isNetworkTypeValid(networkType) ||
551 mNetAttributes[networkType] == null) {
Robert Greenwalt42acef32009-08-12 16:08:25 -0700552 return Phone.APN_REQUEST_FAILED;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800553 }
Robert Greenwalt42acef32009-08-12 16:08:25 -0700554
Robert Greenwalt9c75d4a2009-09-27 17:27:04 -0700555 FeatureUser f = new FeatureUser(networkType, feature, binder);
Robert Greenwalt42acef32009-08-12 16:08:25 -0700556
557 // TODO - move this into the MobileDataStateTracker
558 int usedNetworkType = networkType;
559 if(networkType == ConnectivityManager.TYPE_MOBILE) {
Robert Greenwaltc03fa502010-02-23 18:58:05 -0800560 if (!getMobileDataEnabled()) {
Joe Onorato8a9b2202010-02-26 18:56:32 -0800561 if (DBG) Slog.d(TAG, "requested special network with data disabled - rejected");
Robert Greenwaltc03fa502010-02-23 18:58:05 -0800562 return Phone.APN_TYPE_NOT_AVAILABLE;
563 }
Robert Greenwalt42acef32009-08-12 16:08:25 -0700564 if (TextUtils.equals(feature, Phone.FEATURE_ENABLE_MMS)) {
565 usedNetworkType = ConnectivityManager.TYPE_MOBILE_MMS;
566 } else if (TextUtils.equals(feature, Phone.FEATURE_ENABLE_SUPL)) {
567 usedNetworkType = ConnectivityManager.TYPE_MOBILE_SUPL;
568 } else if (TextUtils.equals(feature, Phone.FEATURE_ENABLE_DUN)) {
569 usedNetworkType = ConnectivityManager.TYPE_MOBILE_DUN;
570 } else if (TextUtils.equals(feature, Phone.FEATURE_ENABLE_HIPRI)) {
571 usedNetworkType = ConnectivityManager.TYPE_MOBILE_HIPRI;
572 }
573 }
574 NetworkStateTracker network = mNetTrackers[usedNetworkType];
575 if (network != null) {
576 if (usedNetworkType != networkType) {
577 Integer currentPid = new Integer(getCallingPid());
578
579 NetworkStateTracker radio = mNetTrackers[networkType];
580 NetworkInfo ni = network.getNetworkInfo();
581
582 if (ni.isAvailable() == false) {
Joe Onorato8a9b2202010-02-26 18:56:32 -0800583 if (DBG) Slog.d(TAG, "special network not available");
Robert Greenwalt42acef32009-08-12 16:08:25 -0700584 return Phone.APN_TYPE_NOT_AVAILABLE;
585 }
586
Robert Greenwalt9c75d4a2009-09-27 17:27:04 -0700587 synchronized(this) {
588 mFeatureUsers.add(f);
589 if (!mNetRequestersPids[usedNetworkType].contains(currentPid)) {
590 // this gets used for per-pid dns when connected
591 mNetRequestersPids[usedNetworkType].add(currentPid);
592 }
Robert Greenwalt42acef32009-08-12 16:08:25 -0700593 }
Robert Greenwalt9c75d4a2009-09-27 17:27:04 -0700594 mHandler.sendMessageDelayed(mHandler.obtainMessage(
595 NetworkStateTracker.EVENT_RESTORE_DEFAULT_NETWORK,
596 f), getRestoreDefaultNetworkDelay());
597
Robert Greenwalt42acef32009-08-12 16:08:25 -0700598
Robert Greenwalta64bf832009-08-19 20:19:33 -0700599 if ((ni.isConnectedOrConnecting() == true) &&
600 !network.isTeardownRequested()) {
Robert Greenwalt42acef32009-08-12 16:08:25 -0700601 if (ni.isConnected() == true) {
602 // add the pid-specific dns
603 handleDnsConfigurationChange();
Joe Onorato8a9b2202010-02-26 18:56:32 -0800604 if (DBG) Slog.d(TAG, "special network already active");
Robert Greenwalt42acef32009-08-12 16:08:25 -0700605 return Phone.APN_ALREADY_ACTIVE;
606 }
Joe Onorato8a9b2202010-02-26 18:56:32 -0800607 if (DBG) Slog.d(TAG, "special network already connecting");
Robert Greenwalt42acef32009-08-12 16:08:25 -0700608 return Phone.APN_REQUEST_STARTED;
609 }
610
611 // check if the radio in play can make another contact
612 // assume if cannot for now
613
Joe Onorato8a9b2202010-02-26 18:56:32 -0800614 if (DBG) Slog.d(TAG, "reconnecting to special network");
Robert Greenwalt42acef32009-08-12 16:08:25 -0700615 network.reconnect();
616 return Phone.APN_REQUEST_STARTED;
617 } else {
Robert Greenwalt02648a42010-05-18 10:52:51 -0700618 return -1;
Robert Greenwalt42acef32009-08-12 16:08:25 -0700619 }
620 }
621 return Phone.APN_TYPE_NOT_AVAILABLE;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800622 }
623
Robert Greenwalt9c75d4a2009-09-27 17:27:04 -0700624 // javadoc from interface
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800625 public int stopUsingNetworkFeature(int networkType, String feature) {
Robert Greenwaltb8f16342009-10-06 17:52:40 -0700626 enforceChangePermission();
627
Robert Greenwalt9c75d4a2009-09-27 17:27:04 -0700628 int pid = getCallingPid();
629 int uid = getCallingUid();
630
631 FeatureUser u = null;
632 boolean found = false;
633
634 synchronized(this) {
635 for (int i = 0; i < mFeatureUsers.size() ; i++) {
636 u = (FeatureUser)mFeatureUsers.get(i);
637 if (uid == u.mUid && pid == u.mPid &&
638 networkType == u.mNetworkType &&
639 TextUtils.equals(feature, u.mFeature)) {
640 found = true;
641 break;
642 }
643 }
644 }
645 if (found && u != null) {
646 // stop regardless of how many other time this proc had called start
647 return stopUsingNetworkFeature(u, true);
648 } else {
649 // none found!
Joe Onorato8a9b2202010-02-26 18:56:32 -0800650 if (DBG) Slog.d(TAG, "ignoring stopUsingNetworkFeature - not a live request");
Robert Greenwalt9c75d4a2009-09-27 17:27:04 -0700651 return 1;
652 }
Robert Greenwalt42acef32009-08-12 16:08:25 -0700653 }
654
Robert Greenwalt9c75d4a2009-09-27 17:27:04 -0700655 private int stopUsingNetworkFeature(FeatureUser u, boolean ignoreDups) {
656 int networkType = u.mNetworkType;
657 String feature = u.mFeature;
658 int pid = u.mPid;
659 int uid = u.mUid;
660
661 NetworkStateTracker tracker = null;
662 boolean callTeardown = false; // used to carry our decision outside of sync block
663
Robert Greenwalt42acef32009-08-12 16:08:25 -0700664 if (DBG) {
Joe Onorato8a9b2202010-02-26 18:56:32 -0800665 Slog.d(TAG, "stopUsingNetworkFeature for net " + networkType +
Robert Greenwalt42acef32009-08-12 16:08:25 -0700666 ": " + feature);
667 }
Robert Greenwaltb8f16342009-10-06 17:52:40 -0700668
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800669 if (!ConnectivityManager.isNetworkTypeValid(networkType)) {
670 return -1;
671 }
Robert Greenwalt42acef32009-08-12 16:08:25 -0700672
Robert Greenwalt9c75d4a2009-09-27 17:27:04 -0700673 // need to link the mFeatureUsers list with the mNetRequestersPids state in this
674 // sync block
675 synchronized(this) {
676 // check if this process still has an outstanding start request
677 if (!mFeatureUsers.contains(u)) {
Robert Greenwalt78a640a2010-03-10 16:10:43 -0800678 if (DBG) Slog.d(TAG, "ignoring - this process has no outstanding requests");
Robert Greenwalt42acef32009-08-12 16:08:25 -0700679 return 1;
680 }
Robert Greenwalt9c75d4a2009-09-27 17:27:04 -0700681 u.unlinkDeathRecipient();
682 mFeatureUsers.remove(mFeatureUsers.indexOf(u));
683 // If we care about duplicate requests, check for that here.
684 //
685 // This is done to support the extension of a request - the app
686 // can request we start the network feature again and renew the
687 // auto-shutoff delay. Normal "stop" calls from the app though
688 // do not pay attention to duplicate requests - in effect the
689 // API does not refcount and a single stop will counter multiple starts.
690 if (ignoreDups == false) {
691 for (int i = 0; i < mFeatureUsers.size() ; i++) {
692 FeatureUser x = (FeatureUser)mFeatureUsers.get(i);
693 if (x.mUid == u.mUid && x.mPid == u.mPid &&
694 x.mNetworkType == u.mNetworkType &&
695 TextUtils.equals(x.mFeature, u.mFeature)) {
Joe Onorato8a9b2202010-02-26 18:56:32 -0800696 if (DBG) Slog.d(TAG, "ignoring stopUsingNetworkFeature as dup is found");
Robert Greenwalt9c75d4a2009-09-27 17:27:04 -0700697 return 1;
698 }
699 }
Robert Greenwalt42acef32009-08-12 16:08:25 -0700700 }
Robert Greenwalt9c75d4a2009-09-27 17:27:04 -0700701
702 // TODO - move to MobileDataStateTracker
703 int usedNetworkType = networkType;
704 if (networkType == ConnectivityManager.TYPE_MOBILE) {
705 if (TextUtils.equals(feature, Phone.FEATURE_ENABLE_MMS)) {
706 usedNetworkType = ConnectivityManager.TYPE_MOBILE_MMS;
707 } else if (TextUtils.equals(feature, Phone.FEATURE_ENABLE_SUPL)) {
708 usedNetworkType = ConnectivityManager.TYPE_MOBILE_SUPL;
709 } else if (TextUtils.equals(feature, Phone.FEATURE_ENABLE_DUN)) {
710 usedNetworkType = ConnectivityManager.TYPE_MOBILE_DUN;
711 } else if (TextUtils.equals(feature, Phone.FEATURE_ENABLE_HIPRI)) {
712 usedNetworkType = ConnectivityManager.TYPE_MOBILE_HIPRI;
713 }
714 }
715 tracker = mNetTrackers[usedNetworkType];
Robert Greenwalt5154ae762009-10-30 14:17:42 -0700716 if (tracker == null) {
Robert Greenwalt78a640a2010-03-10 16:10:43 -0800717 if (DBG) Slog.d(TAG, "ignoring - no known tracker for net type " + usedNetworkType);
Robert Greenwalt5154ae762009-10-30 14:17:42 -0700718 return -1;
719 }
720 if (usedNetworkType != networkType) {
Robert Greenwalt9c75d4a2009-09-27 17:27:04 -0700721 Integer currentPid = new Integer(pid);
Robert Greenwalt9c75d4a2009-09-27 17:27:04 -0700722 mNetRequestersPids[usedNetworkType].remove(currentPid);
Robert Greenwalt421c72b2009-12-17 14:54:59 -0800723 reassessPidDns(pid, true);
Robert Greenwalt9c75d4a2009-09-27 17:27:04 -0700724 if (mNetRequestersPids[usedNetworkType].size() != 0) {
Joe Onorato8a9b2202010-02-26 18:56:32 -0800725 if (DBG) Slog.d(TAG, "not tearing down special network - " +
Robert Greenwalt9c75d4a2009-09-27 17:27:04 -0700726 "others still using it");
727 return 1;
728 }
729 callTeardown = true;
730 }
731 }
Robert Greenwalt78a640a2010-03-10 16:10:43 -0800732 if (DBG) Slog.d(TAG, "Doing network teardown");
Robert Greenwalt9c75d4a2009-09-27 17:27:04 -0700733 if (callTeardown) {
734 tracker.teardown();
Robert Greenwalt42acef32009-08-12 16:08:25 -0700735 return 1;
736 } else {
Robert Greenwalt02648a42010-05-18 10:52:51 -0700737 return -1;
Robert Greenwalt42acef32009-08-12 16:08:25 -0700738 }
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800739 }
740
741 /**
742 * Ensure that a network route exists to deliver traffic to the specified
743 * host via the specified network interface.
Robert Greenwalt86e9e552009-07-16 17:21:39 -0700744 * @param networkType the type of the network over which traffic to the
745 * specified host is to be routed
746 * @param hostAddress the IP address of the host to which the route is
747 * desired
Robert Greenwalt47f69fe2010-06-15 15:43:39 -0700748 * todo - deprecate (only v4!)
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800749 * @return {@code true} on success, {@code false} on failure
750 */
751 public boolean requestRouteToHost(int networkType, int hostAddress) {
752 enforceChangePermission();
753 if (!ConnectivityManager.isNetworkTypeValid(networkType)) {
754 return false;
755 }
756 NetworkStateTracker tracker = mNetTrackers[networkType];
Robert Greenwalt8206ff32009-09-10 15:06:20 -0700757
Robert Greenwalt5154ae762009-10-30 14:17:42 -0700758 if (tracker == null || !tracker.getNetworkInfo().isConnected() ||
759 tracker.isTeardownRequested()) {
Robert Greenwalt8206ff32009-09-10 15:06:20 -0700760 if (DBG) {
Joe Onorato8a9b2202010-02-26 18:56:32 -0800761 Slog.d(TAG, "requestRouteToHost on down network (" + networkType + ") - dropped");
Robert Greenwalt8206ff32009-09-10 15:06:20 -0700762 }
763 return false;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800764 }
Robert Greenwalt47f69fe2010-06-15 15:43:39 -0700765 try {
766 InetAddress addr = InetAddress.getByAddress(NetworkUtils.v4IntToArray(hostAddress));
767 return addHostRoute(tracker, addr);
768 } catch (UnknownHostException e) {}
769 return false;
Irfan Sheriffd649c122010-06-09 15:39:36 -0700770 }
771
772 /**
773 * Ensure that a network route exists to deliver traffic to the specified
774 * host via the mobile data network.
775 * @param hostAddress the IP address of the host to which the route is desired,
776 * in network byte order.
Robert Greenwalt47f69fe2010-06-15 15:43:39 -0700777 * TODO - deprecate
Irfan Sheriffd649c122010-06-09 15:39:36 -0700778 * @return {@code true} on success, {@code false} on failure
779 */
Robert Greenwalt47f69fe2010-06-15 15:43:39 -0700780 private boolean addHostRoute(NetworkStateTracker nt, InetAddress hostAddress) {
Irfan Sheriffd649c122010-06-09 15:39:36 -0700781 if (nt.getNetworkInfo().getType() == ConnectivityManager.TYPE_WIFI) {
782 return false;
783 }
784
Robert Greenwalt47f69fe2010-06-15 15:43:39 -0700785 NetworkProperties p = nt.getNetworkProperties();
786 if (p == null) return false;
787 String interfaceName = p.getInterfaceName();
Irfan Sheriffd649c122010-06-09 15:39:36 -0700788
789 if (DBG) {
Robert Greenwalt47f69fe2010-06-15 15:43:39 -0700790 Slog.d(TAG, "Requested host route to " + hostAddress + "(" + interfaceName + ")");
Irfan Sheriffd649c122010-06-09 15:39:36 -0700791 }
Robert Greenwalt47f69fe2010-06-15 15:43:39 -0700792 if (interfaceName != null) {
Irfan Sheriffd649c122010-06-09 15:39:36 -0700793 return NetworkUtils.addHostRoute(interfaceName, hostAddress) == 0;
794 } else {
Robert Greenwalt47f69fe2010-06-15 15:43:39 -0700795 if (DBG) Slog.e(TAG, "addHostRoute failed due to null interface name");
Irfan Sheriffd649c122010-06-09 15:39:36 -0700796 return false;
797 }
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800798 }
799
800 /**
801 * @see ConnectivityManager#getBackgroundDataSetting()
802 */
803 public boolean getBackgroundDataSetting() {
804 return Settings.Secure.getInt(mContext.getContentResolver(),
805 Settings.Secure.BACKGROUND_DATA, 1) == 1;
806 }
Robert Greenwalt86e9e552009-07-16 17:21:39 -0700807
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800808 /**
809 * @see ConnectivityManager#setBackgroundDataSetting(boolean)
810 */
811 public void setBackgroundDataSetting(boolean allowBackgroundDataUsage) {
812 mContext.enforceCallingOrSelfPermission(
813 android.Manifest.permission.CHANGE_BACKGROUND_DATA_SETTING,
814 "ConnectivityService");
Robert Greenwalt86e9e552009-07-16 17:21:39 -0700815
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800816 if (getBackgroundDataSetting() == allowBackgroundDataUsage) return;
817
818 Settings.Secure.putInt(mContext.getContentResolver(),
Robert Greenwalt86e9e552009-07-16 17:21:39 -0700819 Settings.Secure.BACKGROUND_DATA,
820 allowBackgroundDataUsage ? 1 : 0);
821
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800822 Intent broadcast = new Intent(
823 ConnectivityManager.ACTION_BACKGROUND_DATA_SETTING_CHANGED);
824 mContext.sendBroadcast(broadcast);
Robert Greenwalt86e9e552009-07-16 17:21:39 -0700825 }
826
Robert Greenwaltc03fa502010-02-23 18:58:05 -0800827 /**
828 * @see ConnectivityManager#getMobileDataEnabled()
829 */
830 public boolean getMobileDataEnabled() {
831 enforceAccessPermission();
832 boolean retVal = Settings.Secure.getInt(mContext.getContentResolver(),
833 Settings.Secure.MOBILE_DATA, 1) == 1;
Joe Onorato8a9b2202010-02-26 18:56:32 -0800834 if (DBG) Slog.d(TAG, "getMobileDataEnabled returning " + retVal);
Robert Greenwaltc03fa502010-02-23 18:58:05 -0800835 return retVal;
836 }
837
838 /**
839 * @see ConnectivityManager#setMobileDataEnabled(boolean)
840 */
841 public synchronized void setMobileDataEnabled(boolean enabled) {
842 enforceChangePermission();
Joe Onorato8a9b2202010-02-26 18:56:32 -0800843 if (DBG) Slog.d(TAG, "setMobileDataEnabled(" + enabled + ")");
Robert Greenwaltc03fa502010-02-23 18:58:05 -0800844
845 if (getMobileDataEnabled() == enabled) return;
846
847 Settings.Secure.putInt(mContext.getContentResolver(),
848 Settings.Secure.MOBILE_DATA, enabled ? 1 : 0);
849
850 if (enabled) {
851 if (mNetTrackers[ConnectivityManager.TYPE_MOBILE] != null) {
Joe Onorato8a9b2202010-02-26 18:56:32 -0800852 if (DBG) Slog.d(TAG, "starting up " + mNetTrackers[ConnectivityManager.TYPE_MOBILE]);
Robert Greenwaltc03fa502010-02-23 18:58:05 -0800853 mNetTrackers[ConnectivityManager.TYPE_MOBILE].reconnect();
854 }
855 } else {
856 for (NetworkStateTracker nt : mNetTrackers) {
857 if (nt == null) continue;
858 int netType = nt.getNetworkInfo().getType();
859 if (mNetAttributes[netType].mRadio == ConnectivityManager.TYPE_MOBILE) {
Joe Onorato8a9b2202010-02-26 18:56:32 -0800860 if (DBG) Slog.d(TAG, "tearing down " + nt);
Robert Greenwaltc03fa502010-02-23 18:58:05 -0800861 nt.teardown();
862 }
863 }
864 }
865 }
866
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800867 private int getNumConnectedNetworks() {
868 int numConnectedNets = 0;
869
870 for (NetworkStateTracker nt : mNetTrackers) {
Robert Greenwalt5154ae762009-10-30 14:17:42 -0700871 if (nt != null && nt.getNetworkInfo().isConnected() &&
Robert Greenwalt86e9e552009-07-16 17:21:39 -0700872 !nt.isTeardownRequested()) {
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800873 ++numConnectedNets;
874 }
875 }
876 return numConnectedNets;
877 }
878
879 private void enforceAccessPermission() {
Robert Greenwalt86e9e552009-07-16 17:21:39 -0700880 mContext.enforceCallingOrSelfPermission(
881 android.Manifest.permission.ACCESS_NETWORK_STATE,
882 "ConnectivityService");
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800883 }
884
885 private void enforceChangePermission() {
Robert Greenwalt86e9e552009-07-16 17:21:39 -0700886 mContext.enforceCallingOrSelfPermission(
887 android.Manifest.permission.CHANGE_NETWORK_STATE,
888 "ConnectivityService");
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800889 }
890
Robert Greenwaltd0e18ff2010-01-26 11:40:34 -0800891 // TODO Make this a special check when it goes public
892 private void enforceTetherChangePermission() {
893 mContext.enforceCallingOrSelfPermission(
894 android.Manifest.permission.CHANGE_NETWORK_STATE,
895 "ConnectivityService");
896 }
897
Robert Greenwalt2a091d72010-02-11 18:18:40 -0800898 private void enforceTetherAccessPermission() {
899 mContext.enforceCallingOrSelfPermission(
900 android.Manifest.permission.ACCESS_NETWORK_STATE,
901 "ConnectivityService");
902 }
903
Robert Greenwalt14f2ef42010-06-15 12:19:37 -0700904 private void enforceConnectivityInternalPermission() {
905 mContext.enforceCallingOrSelfPermission(
906 android.Manifest.permission.CONNECTIVITY_INTERNAL,
907 "ConnectivityService");
908 }
909
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800910 /**
Robert Greenwalt86e9e552009-07-16 17:21:39 -0700911 * Handle a {@code DISCONNECTED} event. If this pertains to the non-active
912 * network, we ignore it. If it is for the active network, we send out a
913 * broadcast. But first, we check whether it might be possible to connect
914 * to a different network.
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800915 * @param info the {@code NetworkInfo} for the network
916 */
917 private void handleDisconnect(NetworkInfo info) {
918
Robert Greenwalt42acef32009-08-12 16:08:25 -0700919 int prevNetType = info.getType();
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800920
Robert Greenwalt42acef32009-08-12 16:08:25 -0700921 mNetTrackers[prevNetType].setTeardownRequested(false);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800922 /*
923 * If the disconnected network is not the active one, then don't report
924 * this as a loss of connectivity. What probably happened is that we're
925 * getting the disconnect for a network that we explicitly disabled
926 * in accordance with network preference policies.
927 */
Robert Greenwalt42acef32009-08-12 16:08:25 -0700928 if (!mNetAttributes[prevNetType].isDefault()) {
929 List pids = mNetRequestersPids[prevNetType];
930 for (int i = 0; i<pids.size(); i++) {
931 Integer pid = (Integer)pids.get(i);
932 // will remove them because the net's no longer connected
933 // need to do this now as only now do we know the pids and
934 // can properly null things that are no longer referenced.
935 reassessPidDns(pid.intValue(), false);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800936 }
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800937 }
938
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800939 Intent intent = new Intent(ConnectivityManager.CONNECTIVITY_ACTION);
Dianne Hackborn1c633fc2009-12-08 19:45:14 -0800940 intent.addFlags(Intent.FLAG_RECEIVER_REPLACE_PENDING);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800941 intent.putExtra(ConnectivityManager.EXTRA_NETWORK_INFO, info);
942 if (info.isFailover()) {
943 intent.putExtra(ConnectivityManager.EXTRA_IS_FAILOVER, true);
944 info.setFailover(false);
945 }
946 if (info.getReason() != null) {
947 intent.putExtra(ConnectivityManager.EXTRA_REASON, info.getReason());
948 }
949 if (info.getExtraInfo() != null) {
Robert Greenwalt86e9e552009-07-16 17:21:39 -0700950 intent.putExtra(ConnectivityManager.EXTRA_EXTRA_INFO,
951 info.getExtraInfo());
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800952 }
Robert Greenwalt42acef32009-08-12 16:08:25 -0700953
Robert Greenwaltcc4b4012010-01-25 17:54:29 -0800954 NetworkStateTracker newNet = null;
955 if (mNetAttributes[prevNetType].isDefault()) {
956 newNet = tryFailover(prevNetType);
957 if (newNet != null) {
958 NetworkInfo switchTo = newNet.getNetworkInfo();
959 intent.putExtra(ConnectivityManager.EXTRA_OTHER_NETWORK_INFO, switchTo);
960 } else {
961 intent.putExtra(ConnectivityManager.EXTRA_NO_CONNECTIVITY, true);
962 }
Robert Greenwaltda03c4e2010-01-20 19:29:41 -0800963 }
964 // do this before we broadcast the change
965 handleConnectivityChange();
966
967 sendStickyBroadcast(intent);
968 /*
969 * If the failover network is already connected, then immediately send
970 * out a followup broadcast indicating successful failover
971 */
972 if (newNet != null && newNet.getNetworkInfo().isConnected()) {
973 sendConnectedBroadcast(newNet.getNetworkInfo());
974 }
975 }
976
Robert Greenwaltcc4b4012010-01-25 17:54:29 -0800977 // returns null if no failover available
Robert Greenwaltda03c4e2010-01-20 19:29:41 -0800978 private NetworkStateTracker tryFailover(int prevNetType) {
Robert Greenwalt42acef32009-08-12 16:08:25 -0700979 /*
980 * If this is a default network, check if other defaults are available
981 * or active
982 */
983 NetworkStateTracker newNet = null;
984 if (mNetAttributes[prevNetType].isDefault()) {
Robert Greenwalt42acef32009-08-12 16:08:25 -0700985 if (mActiveDefaultNetwork == prevNetType) {
986 mActiveDefaultNetwork = -1;
987 }
988
989 int newType = -1;
990 int newPriority = -1;
Robert Greenwalt35429592010-02-25 12:04:29 -0800991 boolean noMobileData = !getMobileDataEnabled();
Robert Greenwaltda03c4e2010-01-20 19:29:41 -0800992 for (int checkType=0; checkType <= ConnectivityManager.MAX_NETWORK_TYPE; checkType++) {
Robert Greenwalt5154ae762009-10-30 14:17:42 -0700993 if (checkType == prevNetType) continue;
994 if (mNetAttributes[checkType] == null) continue;
Robert Greenwalt35429592010-02-25 12:04:29 -0800995 if (mNetAttributes[checkType].mRadio == ConnectivityManager.TYPE_MOBILE &&
996 noMobileData) {
997 if (DBG) {
Joe Onorato8a9b2202010-02-26 18:56:32 -0800998 Slog.d(TAG, "not failing over to mobile type " + checkType +
Robert Greenwalt35429592010-02-25 12:04:29 -0800999 " because Mobile Data Disabled");
1000 }
1001 continue;
1002 }
Robert Greenwalt42acef32009-08-12 16:08:25 -07001003 if (mNetAttributes[checkType].isDefault()) {
1004 /* TODO - if we have multiple nets we could use
1005 * we may want to put more thought into which we choose
1006 */
1007 if (checkType == mNetworkPreference) {
1008 newType = checkType;
1009 break;
1010 }
Robert Greenwalt5154ae762009-10-30 14:17:42 -07001011 if (mNetAttributes[checkType].mPriority > newPriority) {
Robert Greenwalt42acef32009-08-12 16:08:25 -07001012 newType = checkType;
Robert Greenwalt5154ae762009-10-30 14:17:42 -07001013 newPriority = mNetAttributes[newType].mPriority;
Robert Greenwalt42acef32009-08-12 16:08:25 -07001014 }
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001015 }
1016 }
Robert Greenwalt42acef32009-08-12 16:08:25 -07001017
1018 if (newType != -1) {
1019 newNet = mNetTrackers[newType];
1020 /**
1021 * See if the other network is available to fail over to.
1022 * If is not available, we enable it anyway, so that it
1023 * will be able to connect when it does become available,
1024 * but we report a total loss of connectivity rather than
1025 * report that we are attempting to fail over.
1026 */
1027 if (newNet.isAvailable()) {
1028 NetworkInfo switchTo = newNet.getNetworkInfo();
1029 switchTo.setFailover(true);
Robert Greenwalta64bf832009-08-19 20:19:33 -07001030 if (!switchTo.isConnectedOrConnecting() ||
1031 newNet.isTeardownRequested()) {
Robert Greenwalt42acef32009-08-12 16:08:25 -07001032 newNet.reconnect();
1033 }
1034 if (DBG) {
1035 if (switchTo.isConnected()) {
Joe Onorato8a9b2202010-02-26 18:56:32 -08001036 Slog.v(TAG, "Switching to already connected " +
Robert Greenwalt42acef32009-08-12 16:08:25 -07001037 switchTo.getTypeName());
1038 } else {
Joe Onorato8a9b2202010-02-26 18:56:32 -08001039 Slog.v(TAG, "Attempting to switch to " +
Robert Greenwalt42acef32009-08-12 16:08:25 -07001040 switchTo.getTypeName());
1041 }
1042 }
Robert Greenwalt42acef32009-08-12 16:08:25 -07001043 } else {
1044 newNet.reconnect();
Robert Greenwaltf0fa39e2010-03-09 14:55:08 -08001045 newNet = null; // not officially avail.. try anyway, but
1046 // report no failover
Robert Greenwalt42acef32009-08-12 16:08:25 -07001047 }
Robert Greenwalt42acef32009-08-12 16:08:25 -07001048 }
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001049 }
Robert Greenwalt42acef32009-08-12 16:08:25 -07001050
Robert Greenwaltda03c4e2010-01-20 19:29:41 -08001051 return newNet;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001052 }
1053
1054 private void sendConnectedBroadcast(NetworkInfo info) {
1055 Intent intent = new Intent(ConnectivityManager.CONNECTIVITY_ACTION);
Dianne Hackborn1c633fc2009-12-08 19:45:14 -08001056 intent.addFlags(Intent.FLAG_RECEIVER_REPLACE_PENDING);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001057 intent.putExtra(ConnectivityManager.EXTRA_NETWORK_INFO, info);
1058 if (info.isFailover()) {
1059 intent.putExtra(ConnectivityManager.EXTRA_IS_FAILOVER, true);
1060 info.setFailover(false);
1061 }
1062 if (info.getReason() != null) {
1063 intent.putExtra(ConnectivityManager.EXTRA_REASON, info.getReason());
1064 }
1065 if (info.getExtraInfo() != null) {
Robert Greenwalt86e9e552009-07-16 17:21:39 -07001066 intent.putExtra(ConnectivityManager.EXTRA_EXTRA_INFO,
1067 info.getExtraInfo());
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001068 }
Mike Lockwood0f79b542009-08-14 14:18:49 -04001069 sendStickyBroadcast(intent);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001070 }
1071
1072 /**
1073 * Called when an attempt to fail over to another network has failed.
1074 * @param info the {@link NetworkInfo} for the failed network
1075 */
1076 private void handleConnectionFailure(NetworkInfo info) {
1077 mNetTrackers[info.getType()].setTeardownRequested(false);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001078
Robert Greenwalt42acef32009-08-12 16:08:25 -07001079 String reason = info.getReason();
1080 String extraInfo = info.getExtraInfo();
Robert Greenwalt86e9e552009-07-16 17:21:39 -07001081
Robert Greenwalt42acef32009-08-12 16:08:25 -07001082 if (DBG) {
1083 String reasonText;
1084 if (reason == null) {
1085 reasonText = ".";
1086 } else {
1087 reasonText = " (" + reason + ").";
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001088 }
Joe Onorato8a9b2202010-02-26 18:56:32 -08001089 Slog.v(TAG, "Attempt to connect to " + info.getTypeName() +
Robert Greenwalt42acef32009-08-12 16:08:25 -07001090 " failed" + reasonText);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001091 }
Robert Greenwalt42acef32009-08-12 16:08:25 -07001092
1093 Intent intent = new Intent(ConnectivityManager.CONNECTIVITY_ACTION);
Dianne Hackborn1c633fc2009-12-08 19:45:14 -08001094 intent.addFlags(Intent.FLAG_RECEIVER_REPLACE_PENDING);
Robert Greenwalt42acef32009-08-12 16:08:25 -07001095 intent.putExtra(ConnectivityManager.EXTRA_NETWORK_INFO, info);
1096 if (getActiveNetworkInfo() == null) {
1097 intent.putExtra(ConnectivityManager.EXTRA_NO_CONNECTIVITY, true);
1098 }
1099 if (reason != null) {
1100 intent.putExtra(ConnectivityManager.EXTRA_REASON, reason);
1101 }
1102 if (extraInfo != null) {
1103 intent.putExtra(ConnectivityManager.EXTRA_EXTRA_INFO, extraInfo);
1104 }
1105 if (info.isFailover()) {
1106 intent.putExtra(ConnectivityManager.EXTRA_IS_FAILOVER, true);
1107 info.setFailover(false);
1108 }
Robert Greenwaltda03c4e2010-01-20 19:29:41 -08001109
Robert Greenwaltcc4b4012010-01-25 17:54:29 -08001110 NetworkStateTracker newNet = null;
1111 if (mNetAttributes[info.getType()].isDefault()) {
1112 newNet = tryFailover(info.getType());
1113 if (newNet != null) {
1114 NetworkInfo switchTo = newNet.getNetworkInfo();
1115 intent.putExtra(ConnectivityManager.EXTRA_OTHER_NETWORK_INFO, switchTo);
1116 } else {
1117 intent.putExtra(ConnectivityManager.EXTRA_NO_CONNECTIVITY, true);
1118 }
Robert Greenwaltda03c4e2010-01-20 19:29:41 -08001119 }
Robert Greenwaltcc4b4012010-01-25 17:54:29 -08001120
Robert Greenwaltda03c4e2010-01-20 19:29:41 -08001121 // do this before we broadcast the change
1122 handleConnectivityChange();
1123
Mike Lockwood0f79b542009-08-14 14:18:49 -04001124 sendStickyBroadcast(intent);
Robert Greenwaltda03c4e2010-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 Lockwood0f79b542009-08-14 14:18:49 -04001132 }
1133
1134 private void sendStickyBroadcast(Intent intent) {
1135 synchronized(this) {
Dianne Hackborn1c633fc2009-12-08 19:45:14 -08001136 if (!mSystemReady) {
1137 mInitialBroadcast = new Intent(intent);
Mike Lockwood0f79b542009-08-14 14:18:49 -04001138 }
Dianne Hackborn1c633fc2009-12-08 19:45:14 -08001139 intent.addFlags(Intent.FLAG_RECEIVER_REGISTERED_ONLY_BEFORE_BOOT);
1140 mContext.sendStickyBroadcast(intent);
Mike Lockwood0f79b542009-08-14 14:18:49 -04001141 }
1142 }
1143
1144 void systemReady() {
1145 synchronized(this) {
1146 mSystemReady = true;
Dianne Hackborn1c633fc2009-12-08 19:45:14 -08001147 if (mInitialBroadcast != null) {
1148 mContext.sendStickyBroadcast(mInitialBroadcast);
1149 mInitialBroadcast = null;
Mike Lockwood0f79b542009-08-14 14:18:49 -04001150 }
1151 }
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001152 }
1153
1154 private void handleConnect(NetworkInfo info) {
Robert Greenwalt42acef32009-08-12 16:08:25 -07001155 int type = info.getType();
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001156
1157 // snapshot isFailover, because sendConnectedBroadcast() resets it
1158 boolean isFailover = info.isFailover();
Robert Greenwalt42acef32009-08-12 16:08:25 -07001159 NetworkStateTracker thisNet = mNetTrackers[type];
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001160
Robert Greenwalt42acef32009-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 Greenwalt42acef32009-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 Onorato8a9b2202010-02-26 18:56:32 -08001170 if (DBG) Slog.v(TAG, "Not broadcasting CONNECT_ACTION " +
Robert Greenwalt42acef32009-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 Onorato8a9b2202010-02-26 18:56:32 -08001178 if (DBG) Slog.v(TAG, "Policy requires " +
Robert Greenwalt42acef32009-08-12 16:08:25 -07001179 otherNet.getNetworkInfo().getTypeName() +
1180 " teardown");
1181 if (!teardown(otherNet)) {
Joe Onorato8a9b2202010-02-26 18:56:32 -08001182 Slog.e(TAG, "Network declined teardown request");
Robert Greenwalt42acef32009-08-12 16:08:25 -07001183 return;
1184 }
Robert Greenwalt14f2ef42010-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 Greenwalt42acef32009-08-12 16:08:25 -07001196 }
1197 }
1198 mActiveDefaultNetwork = type;
1199 }
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001200 thisNet.setTeardownRequested(false);
Irfan Sheriffd649c122010-06-09 15:39:36 -07001201 updateNetworkSettings(thisNet);
Robert Greenwalt42acef32009-08-12 16:08:25 -07001202 handleConnectivityChange();
1203 sendConnectedBroadcast(info);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001204 }
1205
1206 private void handleScanResultsAvailable(NetworkInfo info) {
1207 int networkType = info.getType();
1208 if (networkType != ConnectivityManager.TYPE_WIFI) {
Joe Onorato8a9b2202010-02-26 18:56:32 -08001209 if (DBG) Slog.v(TAG, "Got ScanResultsAvailable for " +
Robert Greenwalt86e9e552009-07-16 17:21:39 -07001210 info.getTypeName() + " network. Don't know how to handle.");
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001211 }
Robert Greenwalt86e9e552009-07-16 17:21:39 -07001212
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001213 mNetTrackers[networkType].interpretScanResultsAvailable();
1214 }
1215
Robert Greenwalt86e9e552009-07-16 17:21:39 -07001216 private void handleNotificationChange(boolean visible, int id,
1217 Notification notification) {
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001218 NotificationManager notificationManager = (NotificationManager) mContext
1219 .getSystemService(Context.NOTIFICATION_SERVICE);
Robert Greenwalt86e9e552009-07-16 17:21:39 -07001220
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001221 if (visible) {
1222 notificationManager.notify(id, notification);
1223 } else {
1224 notificationManager.cancel(id);
1225 }
1226 }
1227
1228 /**
1229 * After any kind of change in the connectivity state of any network,
1230 * make sure that anything that depends on the connectivity state of
1231 * more than one network is set up correctly. We're mainly concerned
1232 * with making sure that the list of DNS servers is set up according
1233 * to which networks are connected, and ensuring that the right routing
1234 * table entries exist.
1235 */
1236 private void handleConnectivityChange() {
1237 /*
Robert Greenwalt42acef32009-08-12 16:08:25 -07001238 * If a non-default network is enabled, add the host routes that
Robert Greenwalt1ef95f92009-09-30 21:01:30 -07001239 * will allow it's DNS servers to be accessed. Only
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001240 * If both mobile and wifi are enabled, add the host routes that
1241 * will allow MMS traffic to pass on the mobile network. But
1242 * remove the default route for the mobile network, so that there
1243 * will be only one default route, to ensure that all traffic
1244 * except MMS will travel via Wi-Fi.
1245 */
Robert Greenwalt42acef32009-08-12 16:08:25 -07001246 handleDnsConfigurationChange();
1247
1248 for (int netType : mPriorityList) {
1249 if (mNetTrackers[netType].getNetworkInfo().isConnected()) {
1250 if (mNetAttributes[netType].isDefault()) {
Irfan Sheriffd649c122010-06-09 15:39:36 -07001251 addDefaultRoute(mNetTrackers[netType]);
Robert Greenwalt42acef32009-08-12 16:08:25 -07001252 } else {
Irfan Sheriffd649c122010-06-09 15:39:36 -07001253 addPrivateDnsRoutes(mNetTrackers[netType]);
Robert Greenwalt42acef32009-08-12 16:08:25 -07001254 }
1255 } else {
1256 if (mNetAttributes[netType].isDefault()) {
Irfan Sheriffd649c122010-06-09 15:39:36 -07001257 removeDefaultRoute(mNetTrackers[netType]);
Robert Greenwalt42acef32009-08-12 16:08:25 -07001258 } else {
Irfan Sheriffd649c122010-06-09 15:39:36 -07001259 removePrivateDnsRoutes(mNetTrackers[netType]);
Robert Greenwalt42acef32009-08-12 16:08:25 -07001260 }
1261 }
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001262 }
1263 }
1264
Irfan Sheriffd649c122010-06-09 15:39:36 -07001265 private void addPrivateDnsRoutes(NetworkStateTracker nt) {
Irfan Sheriffd649c122010-06-09 15:39:36 -07001266 boolean privateDnsRouteSet = nt.isPrivateDnsRouteSet();
Robert Greenwalt47f69fe2010-06-15 15:43:39 -07001267 NetworkProperties p = nt.getNetworkProperties();
1268 if (p == null) return;
1269 String interfaceName = p.getInterfaceName();
Irfan Sheriffd649c122010-06-09 15:39:36 -07001270
1271 if (DBG) {
1272 Slog.d(TAG, "addPrivateDnsRoutes for " + nt +
1273 "(" + interfaceName + ") - mPrivateDnsRouteSet = " + privateDnsRouteSet);
1274 }
Irfan Sheriffd649c122010-06-09 15:39:36 -07001275 if (interfaceName != null && !privateDnsRouteSet) {
Robert Greenwalt47f69fe2010-06-15 15:43:39 -07001276 Collection<InetAddress> dnsList = p.getDnses();
1277 for (InetAddress dns : dnsList) {
1278 if (DBG) Slog.d(TAG, " adding " + dns);
1279 NetworkUtils.addHostRoute(interfaceName, dns);
Irfan Sheriffd649c122010-06-09 15:39:36 -07001280 }
1281 nt.privateDnsRouteSet(true);
1282 }
1283 }
1284
1285 private void removePrivateDnsRoutes(NetworkStateTracker nt) {
1286 // TODO - we should do this explicitly but the NetUtils api doesnt
1287 // support this yet - must remove all. No worse than before
Robert Greenwalt47f69fe2010-06-15 15:43:39 -07001288 NetworkProperties p = nt.getNetworkProperties();
1289 if (p == null) return;
1290 String interfaceName = p.getInterfaceName();
Irfan Sheriffd649c122010-06-09 15:39:36 -07001291 boolean privateDnsRouteSet = nt.isPrivateDnsRouteSet();
1292 if (interfaceName != null && privateDnsRouteSet) {
1293 if (DBG) {
1294 Slog.d(TAG, "removePrivateDnsRoutes for " + nt.getNetworkInfo().getTypeName() +
1295 " (" + interfaceName + ")");
1296 }
1297 NetworkUtils.removeHostRoutes(interfaceName);
1298 nt.privateDnsRouteSet(false);
1299 }
1300 }
1301
Irfan Sheriffd649c122010-06-09 15:39:36 -07001302
1303 private void addDefaultRoute(NetworkStateTracker nt) {
Robert Greenwalt47f69fe2010-06-15 15:43:39 -07001304 NetworkProperties p = nt.getNetworkProperties();
1305 if (p == null) return;
1306 String interfaceName = p.getInterfaceName();
1307 InetAddress defaultGatewayAddr = p.getGateway();
Irfan Sheriffd649c122010-06-09 15:39:36 -07001308 boolean defaultRouteSet = nt.isDefaultRouteSet();
Irfan Sheriffd649c122010-06-09 15:39:36 -07001309
Robert Greenwalt47f69fe2010-06-15 15:43:39 -07001310 if ((interfaceName != null) && (defaultGatewayAddr != null ) &&
1311 (defaultRouteSet == false)) {
1312 boolean error = (NetworkUtils.setDefaultRoute(interfaceName, defaultGatewayAddr) < 0);
1313
1314 if (DBG && !error) {
1315 NetworkInfo networkInfo = nt.getNetworkInfo();
Irfan Sheriffd649c122010-06-09 15:39:36 -07001316 Slog.d(TAG, "addDefaultRoute for " + networkInfo.getTypeName() +
1317 " (" + interfaceName + "), GatewayAddr=" + defaultGatewayAddr);
1318 }
Robert Greenwalt47f69fe2010-06-15 15:43:39 -07001319 nt.defaultRouteSet(!error);
Irfan Sheriffd649c122010-06-09 15:39:36 -07001320 }
1321 }
1322
1323
1324 public void removeDefaultRoute(NetworkStateTracker nt) {
Robert Greenwalt47f69fe2010-06-15 15:43:39 -07001325 NetworkProperties p = nt.getNetworkProperties();
1326 if (p == null) return;
1327 String interfaceName = p.getInterfaceName();
Irfan Sheriffd649c122010-06-09 15:39:36 -07001328 boolean defaultRouteSet = nt.isDefaultRouteSet();
Irfan Sheriffd649c122010-06-09 15:39:36 -07001329
1330 if (interfaceName != null && defaultRouteSet == true) {
Robert Greenwalt47f69fe2010-06-15 15:43:39 -07001331 boolean error = (NetworkUtils.removeDefaultRoute(interfaceName) < 0);
1332 if (DBG && !error) {
1333 NetworkInfo networkInfo = nt.getNetworkInfo();
Irfan Sheriffd649c122010-06-09 15:39:36 -07001334 Slog.d(TAG, "removeDefaultRoute for " + networkInfo.getTypeName() + " (" +
1335 interfaceName + ")");
1336 }
Robert Greenwalt47f69fe2010-06-15 15:43:39 -07001337 nt.defaultRouteSet(error);
Irfan Sheriffd649c122010-06-09 15:39:36 -07001338 }
1339 }
1340
1341 /**
1342 * Reads the network specific TCP buffer sizes from SystemProperties
1343 * net.tcp.buffersize.[default|wifi|umts|edge|gprs] and set them for system
1344 * wide use
1345 */
1346 public void updateNetworkSettings(NetworkStateTracker nt) {
1347 String key = nt.getTcpBufferSizesPropName();
1348 String bufferSizes = SystemProperties.get(key);
1349
1350 if (bufferSizes.length() == 0) {
1351 Slog.e(TAG, key + " not found in system properties. Using defaults");
1352
1353 // Setting to default values so we won't be stuck to previous values
1354 key = "net.tcp.buffersize.default";
1355 bufferSizes = SystemProperties.get(key);
1356 }
1357
1358 // Set values in kernel
1359 if (bufferSizes.length() != 0) {
1360 if (DBG) {
1361 Slog.v(TAG, "Setting TCP values: [" + bufferSizes
1362 + "] which comes from [" + key + "]");
1363 }
1364 setBufferSize(bufferSizes);
1365 }
1366 }
1367
1368 /**
1369 * Writes TCP buffer sizes to /sys/kernel/ipv4/tcp_[r/w]mem_[min/def/max]
1370 * which maps to /proc/sys/net/ipv4/tcp_rmem and tcpwmem
1371 *
1372 * @param bufferSizes in the format of "readMin, readInitial, readMax,
1373 * writeMin, writeInitial, writeMax"
1374 */
1375 private void setBufferSize(String bufferSizes) {
1376 try {
1377 String[] values = bufferSizes.split(",");
1378
1379 if (values.length == 6) {
1380 final String prefix = "/sys/kernel/ipv4/tcp_";
1381 stringToFile(prefix + "rmem_min", values[0]);
1382 stringToFile(prefix + "rmem_def", values[1]);
1383 stringToFile(prefix + "rmem_max", values[2]);
1384 stringToFile(prefix + "wmem_min", values[3]);
1385 stringToFile(prefix + "wmem_def", values[4]);
1386 stringToFile(prefix + "wmem_max", values[5]);
1387 } else {
1388 Slog.e(TAG, "Invalid buffersize string: " + bufferSizes);
1389 }
1390 } catch (IOException e) {
1391 Slog.e(TAG, "Can't set tcp buffer sizes:" + e);
1392 }
1393 }
1394
1395 /**
1396 * Writes string to file. Basically same as "echo -n $string > $filename"
1397 *
1398 * @param filename
1399 * @param string
1400 * @throws IOException
1401 */
1402 private void stringToFile(String filename, String string) throws IOException {
1403 FileWriter out = new FileWriter(filename);
1404 try {
1405 out.write(string);
1406 } finally {
1407 out.close();
1408 }
1409 }
1410
1411
Robert Greenwalt42acef32009-08-12 16:08:25 -07001412 /**
1413 * Adjust the per-process dns entries (net.dns<x>.<pid>) based
1414 * on the highest priority active net which this process requested.
1415 * If there aren't any, clear it out
1416 */
1417 private void reassessPidDns(int myPid, boolean doBump)
1418 {
Joe Onorato8a9b2202010-02-26 18:56:32 -08001419 if (DBG) Slog.d(TAG, "reassessPidDns for pid " + myPid);
Robert Greenwalt42acef32009-08-12 16:08:25 -07001420 for(int i : mPriorityList) {
1421 if (mNetAttributes[i].isDefault()) {
1422 continue;
1423 }
1424 NetworkStateTracker nt = mNetTrackers[i];
Robert Greenwalt86e9e552009-07-16 17:21:39 -07001425 if (nt.getNetworkInfo().isConnected() &&
1426 !nt.isTeardownRequested()) {
Robert Greenwalt47f69fe2010-06-15 15:43:39 -07001427 NetworkProperties p = nt.getNetworkProperties();
1428 if (p == null) continue;
Robert Greenwalt42acef32009-08-12 16:08:25 -07001429 List pids = mNetRequestersPids[i];
1430 for (int j=0; j<pids.size(); j++) {
1431 Integer pid = (Integer)pids.get(j);
1432 if (pid.intValue() == myPid) {
Robert Greenwalt47f69fe2010-06-15 15:43:39 -07001433 Collection<InetAddress> dnses = p.getDnses();
1434 writePidDns(dnses, myPid);
Robert Greenwalt42acef32009-08-12 16:08:25 -07001435 if (doBump) {
1436 bumpDns();
1437 }
1438 return;
1439 }
1440 }
1441 }
1442 }
1443 // nothing found - delete
1444 for (int i = 1; ; i++) {
1445 String prop = "net.dns" + i + "." + myPid;
1446 if (SystemProperties.get(prop).length() == 0) {
1447 if (doBump) {
1448 bumpDns();
1449 }
1450 return;
1451 }
1452 SystemProperties.set(prop, "");
1453 }
1454 }
1455
Robert Greenwalt47f69fe2010-06-15 15:43:39 -07001456 private void writePidDns(Collection <InetAddress> dnses, int pid) {
Robert Greenwalt42acef32009-08-12 16:08:25 -07001457 int j = 1;
Robert Greenwalt47f69fe2010-06-15 15:43:39 -07001458 for (InetAddress dns : dnses) {
1459 SystemProperties.set("net.dns" + j++ + "." + pid, dns.getHostAddress());
Robert Greenwalt42acef32009-08-12 16:08:25 -07001460 }
1461 }
1462
1463 private void bumpDns() {
1464 /*
1465 * Bump the property that tells the name resolver library to reread
1466 * the DNS server list from the properties.
1467 */
1468 String propVal = SystemProperties.get("net.dnschange");
1469 int n = 0;
1470 if (propVal.length() != 0) {
1471 try {
1472 n = Integer.parseInt(propVal);
1473 } catch (NumberFormatException e) {}
1474 }
1475 SystemProperties.set("net.dnschange", "" + (n+1));
1476 }
1477
1478 private void handleDnsConfigurationChange() {
Robert Greenwalt42acef32009-08-12 16:08:25 -07001479 // add default net's dns entries
1480 for (int x = mPriorityList.length-1; x>= 0; x--) {
1481 int netType = mPriorityList[x];
1482 NetworkStateTracker nt = mNetTrackers[netType];
Robert Greenwalt42acef32009-08-12 16:08:25 -07001483 if (nt != null && nt.getNetworkInfo().isConnected() &&
1484 !nt.isTeardownRequested()) {
Robert Greenwalt47f69fe2010-06-15 15:43:39 -07001485 NetworkProperties p = nt.getNetworkProperties();
1486 if (p == null) continue;
1487 Collection<InetAddress> dnses = p.getDnses();
Robert Greenwalt42acef32009-08-12 16:08:25 -07001488 if (mNetAttributes[netType].isDefault()) {
1489 int j = 1;
Robert Greenwalt47f69fe2010-06-15 15:43:39 -07001490 for (InetAddress dns : dnses) {
1491 if (DBG) {
1492 Slog.d(TAG, "adding dns " + dns + " for " +
1493 nt.getNetworkInfo().getTypeName());
Robert Greenwalt42acef32009-08-12 16:08:25 -07001494 }
Robert Greenwalt47f69fe2010-06-15 15:43:39 -07001495 SystemProperties.set("net.dns" + j++, dns.getHostAddress());
Robert Greenwalt42acef32009-08-12 16:08:25 -07001496 }
1497 for (int k=j ; k<mNumDnsEntries; k++) {
Joe Onorato8a9b2202010-02-26 18:56:32 -08001498 if (DBG) Slog.d(TAG, "erasing net.dns" + k);
Robert Greenwaltb06324a2009-08-25 14:00:10 -07001499 SystemProperties.set("net.dns" + k, "");
Robert Greenwalt42acef32009-08-12 16:08:25 -07001500 }
1501 mNumDnsEntries = j;
1502 } else {
1503 // set per-pid dns for attached secondary nets
1504 List pids = mNetRequestersPids[netType];
1505 for (int y=0; y< pids.size(); y++) {
1506 Integer pid = (Integer)pids.get(y);
Robert Greenwalt47f69fe2010-06-15 15:43:39 -07001507 writePidDns(dnses, pid.intValue());
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001508 }
1509 }
1510 }
1511 }
Robert Greenwalt42acef32009-08-12 16:08:25 -07001512
1513 bumpDns();
1514 }
1515
1516 private int getRestoreDefaultNetworkDelay() {
1517 String restoreDefaultNetworkDelayStr = SystemProperties.get(
1518 NETWORK_RESTORE_DELAY_PROP_NAME);
1519 if(restoreDefaultNetworkDelayStr != null &&
1520 restoreDefaultNetworkDelayStr.length() != 0) {
1521 try {
1522 return Integer.valueOf(restoreDefaultNetworkDelayStr);
1523 } catch (NumberFormatException e) {
1524 }
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001525 }
Robert Greenwalt42acef32009-08-12 16:08:25 -07001526 return RESTORE_DEFAULT_NETWORK_DELAY;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001527 }
1528
1529 @Override
1530 protected void dump(FileDescriptor fd, PrintWriter pw, String[] args) {
Robert Greenwalt86e9e552009-07-16 17:21:39 -07001531 if (mContext.checkCallingOrSelfPermission(
1532 android.Manifest.permission.DUMP)
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001533 != PackageManager.PERMISSION_GRANTED) {
Robert Greenwalt86e9e552009-07-16 17:21:39 -07001534 pw.println("Permission Denial: can't dump ConnectivityService " +
1535 "from from pid=" + Binder.getCallingPid() + ", uid=" +
1536 Binder.getCallingUid());
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001537 return;
1538 }
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001539 pw.println();
1540 for (NetworkStateTracker nst : mNetTrackers) {
Robert Greenwaltb9285352009-12-21 18:24:07 -08001541 if (nst != null) {
1542 if (nst.getNetworkInfo().isConnected()) {
1543 pw.println("Active network: " + nst.getNetworkInfo().
1544 getTypeName());
1545 }
1546 pw.println(nst.getNetworkInfo());
1547 pw.println(nst);
1548 pw.println();
Robert Greenwalt42acef32009-08-12 16:08:25 -07001549 }
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001550 }
Robert Greenwaltb9285352009-12-21 18:24:07 -08001551
1552 pw.println("Network Requester Pids:");
1553 for (int net : mPriorityList) {
1554 String pidString = net + ": ";
1555 for (Object pid : mNetRequestersPids[net]) {
1556 pidString = pidString + pid.toString() + ", ";
1557 }
1558 pw.println(pidString);
1559 }
1560 pw.println();
1561
1562 pw.println("FeatureUsers:");
1563 for (Object requester : mFeatureUsers) {
1564 pw.println(requester.toString());
1565 }
1566 pw.println();
Robert Greenwalt2a091d72010-02-11 18:18:40 -08001567
Robert Greenwalt14f2ef42010-06-15 12:19:37 -07001568 synchronized (this) {
1569 pw.println("NetworkTranstionWakeLock is currently " +
1570 (mNetTransitionWakeLock.isHeld() ? "" : "not ") + "held.");
1571 pw.println("It was last requested for "+mNetTransitionWakeLockCausedBy);
1572 }
1573 pw.println();
1574
Robert Greenwalt2a091d72010-02-11 18:18:40 -08001575 mTethering.dump(fd, pw, args);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001576 }
1577
Robert Greenwalt42acef32009-08-12 16:08:25 -07001578 // must be stateless - things change under us.
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001579 private class MyHandler extends Handler {
1580 @Override
1581 public void handleMessage(Message msg) {
1582 NetworkInfo info;
1583 switch (msg.what) {
1584 case NetworkStateTracker.EVENT_STATE_CHANGED:
1585 info = (NetworkInfo) msg.obj;
Robert Greenwalt511288a2009-12-07 11:33:18 -08001586 int type = info.getType();
1587 NetworkInfo.State state = info.getState();
Robert Greenwalt6e6dec22010-01-25 16:14:00 -08001588 // only do this optimization for wifi. It going into scan mode for location
1589 // services generates alot of noise. Meanwhile the mms apn won't send out
1590 // subsequent notifications when on default cellular because it never
1591 // disconnects.. so only do this to wifi notifications. Fixed better when the
1592 // APN notifications are standardized.
1593 if (mNetAttributes[type].mLastState == state &&
1594 mNetAttributes[type].mRadio == ConnectivityManager.TYPE_WIFI) {
Robert Greenwalt511288a2009-12-07 11:33:18 -08001595 if (DBG) {
Robert Greenwalt6e6dec22010-01-25 16:14:00 -08001596 // TODO - remove this after we validate the dropping doesn't break
1597 // anything
Joe Onorato8a9b2202010-02-26 18:56:32 -08001598 Slog.d(TAG, "Dropping ConnectivityChange for " +
Robert Greenwalt1193ae42010-01-13 09:36:31 -08001599 info.getTypeName() + ": " +
Robert Greenwalt511288a2009-12-07 11:33:18 -08001600 state + "/" + info.getDetailedState());
1601 }
1602 return;
1603 }
1604 mNetAttributes[type].mLastState = state;
1605
Joe Onorato8a9b2202010-02-26 18:56:32 -08001606 if (DBG) Slog.d(TAG, "ConnectivityChange for " +
Robert Greenwalt86e9e552009-07-16 17:21:39 -07001607 info.getTypeName() + ": " +
Robert Greenwalt511288a2009-12-07 11:33:18 -08001608 state + "/" + info.getDetailedState());
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001609
1610 // Connectivity state changed:
1611 // [31-13] Reserved for future use
Robert Greenwalt86e9e552009-07-16 17:21:39 -07001612 // [12-9] Network subtype (for mobile network, as defined
1613 // by TelephonyManager)
1614 // [8-3] Detailed state ordinal (as defined by
1615 // NetworkInfo.DetailedState)
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001616 // [2-0] Network type (as defined by ConnectivityManager)
1617 int eventLogParam = (info.getType() & 0x7) |
1618 ((info.getDetailedState().ordinal() & 0x3f) << 3) |
1619 (info.getSubtype() << 9);
Doug Zongkerab5c49c2009-12-04 10:31:43 -08001620 EventLog.writeEvent(EventLogTags.CONNECTIVITY_STATE_CHANGED,
Robert Greenwalt86e9e552009-07-16 17:21:39 -07001621 eventLogParam);
1622
1623 if (info.getDetailedState() ==
1624 NetworkInfo.DetailedState.FAILED) {
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001625 handleConnectionFailure(info);
Robert Greenwalt511288a2009-12-07 11:33:18 -08001626 } else if (state == NetworkInfo.State.DISCONNECTED) {
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001627 handleDisconnect(info);
Robert Greenwalt511288a2009-12-07 11:33:18 -08001628 } else if (state == NetworkInfo.State.SUSPENDED) {
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001629 // TODO: need to think this over.
Robert Greenwalt86e9e552009-07-16 17:21:39 -07001630 // the logic here is, handle SUSPENDED the same as
1631 // DISCONNECTED. The only difference being we are
1632 // broadcasting an intent with NetworkInfo that's
1633 // suspended. This allows the applications an
1634 // opportunity to handle DISCONNECTED and SUSPENDED
1635 // differently, or not.
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001636 handleDisconnect(info);
Robert Greenwalt511288a2009-12-07 11:33:18 -08001637 } else if (state == NetworkInfo.State.CONNECTED) {
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001638 handleConnect(info);
1639 }
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001640 break;
1641
1642 case NetworkStateTracker.EVENT_SCAN_RESULTS_AVAILABLE:
1643 info = (NetworkInfo) msg.obj;
1644 handleScanResultsAvailable(info);
1645 break;
Robert Greenwalt86e9e552009-07-16 17:21:39 -07001646
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001647 case NetworkStateTracker.EVENT_NOTIFICATION_CHANGED:
Robert Greenwalt86e9e552009-07-16 17:21:39 -07001648 handleNotificationChange(msg.arg1 == 1, msg.arg2,
1649 (Notification) msg.obj);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001650
1651 case NetworkStateTracker.EVENT_CONFIGURATION_CHANGED:
Robert Greenwalt42acef32009-08-12 16:08:25 -07001652 handleDnsConfigurationChange();
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001653 break;
1654
1655 case NetworkStateTracker.EVENT_ROAMING_CHANGED:
1656 // fill me in
1657 break;
1658
1659 case NetworkStateTracker.EVENT_NETWORK_SUBTYPE_CHANGED:
1660 // fill me in
1661 break;
Robert Greenwalt42acef32009-08-12 16:08:25 -07001662 case NetworkStateTracker.EVENT_RESTORE_DEFAULT_NETWORK:
Robert Greenwalt9c75d4a2009-09-27 17:27:04 -07001663 FeatureUser u = (FeatureUser)msg.obj;
1664 u.expire();
Robert Greenwalt42acef32009-08-12 16:08:25 -07001665 break;
Robert Greenwalt14f2ef42010-06-15 12:19:37 -07001666 case NetworkStateTracker.EVENT_CLEAR_NET_TRANSITION_WAKELOCK:
1667 String causedBy = null;
1668 synchronized (ConnectivityService.this) {
1669 if (msg.arg1 == mNetTransitionWakeLockSerialNumber &&
1670 mNetTransitionWakeLock.isHeld()) {
1671 mNetTransitionWakeLock.release();
1672 causedBy = mNetTransitionWakeLockCausedBy;
1673 }
1674 }
1675 if (causedBy != null) {
1676 Slog.d(TAG, "NetTransition Wakelock for " +
1677 causedBy + " released by timeout");
1678 }
1679 break;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001680 }
1681 }
1682 }
Robert Greenwaltd0e18ff2010-01-26 11:40:34 -08001683
1684 // javadoc from interface
Robert Greenwalt5a735062010-03-02 17:25:02 -08001685 public int tether(String iface) {
Robert Greenwaltd0e18ff2010-01-26 11:40:34 -08001686 enforceTetherChangePermission();
Robert Greenwalt5a735062010-03-02 17:25:02 -08001687
1688 if (isTetheringSupported()) {
1689 return mTethering.tether(iface);
1690 } else {
1691 return ConnectivityManager.TETHER_ERROR_UNSUPPORTED;
1692 }
Robert Greenwaltd0e18ff2010-01-26 11:40:34 -08001693 }
1694
1695 // javadoc from interface
Robert Greenwalt5a735062010-03-02 17:25:02 -08001696 public int untether(String iface) {
Robert Greenwaltd0e18ff2010-01-26 11:40:34 -08001697 enforceTetherChangePermission();
Robert Greenwalt5a735062010-03-02 17:25:02 -08001698
1699 if (isTetheringSupported()) {
1700 return mTethering.untether(iface);
1701 } else {
1702 return ConnectivityManager.TETHER_ERROR_UNSUPPORTED;
1703 }
1704 }
1705
1706 // javadoc from interface
1707 public int getLastTetherError(String iface) {
1708 enforceTetherAccessPermission();
1709
1710 if (isTetheringSupported()) {
1711 return mTethering.getLastTetherError(iface);
1712 } else {
1713 return ConnectivityManager.TETHER_ERROR_UNSUPPORTED;
1714 }
Robert Greenwalt2a091d72010-02-11 18:18:40 -08001715 }
1716
1717 // TODO - proper iface API for selection by property, inspection, etc
1718 public String[] getTetherableUsbRegexs() {
1719 enforceTetherAccessPermission();
1720 if (isTetheringSupported()) {
1721 return mTethering.getTetherableUsbRegexs();
1722 } else {
1723 return new String[0];
1724 }
1725 }
1726
1727 public String[] getTetherableWifiRegexs() {
1728 enforceTetherAccessPermission();
1729 if (isTetheringSupported()) {
1730 return mTethering.getTetherableWifiRegexs();
1731 } else {
1732 return new String[0];
1733 }
Robert Greenwaltd0e18ff2010-01-26 11:40:34 -08001734 }
1735
1736 // TODO - move iface listing, queries, etc to new module
1737 // javadoc from interface
1738 public String[] getTetherableIfaces() {
Robert Greenwalt2a091d72010-02-11 18:18:40 -08001739 enforceTetherAccessPermission();
Robert Greenwaltd0e18ff2010-01-26 11:40:34 -08001740 return mTethering.getTetherableIfaces();
1741 }
1742
1743 public String[] getTetheredIfaces() {
Robert Greenwalt2a091d72010-02-11 18:18:40 -08001744 enforceTetherAccessPermission();
Robert Greenwaltd0e18ff2010-01-26 11:40:34 -08001745 return mTethering.getTetheredIfaces();
1746 }
Robert Greenwalt2a091d72010-02-11 18:18:40 -08001747
Robert Greenwalt5a735062010-03-02 17:25:02 -08001748 public String[] getTetheringErroredIfaces() {
1749 enforceTetherAccessPermission();
1750 return mTethering.getErroredIfaces();
1751 }
1752
Robert Greenwalt2a091d72010-02-11 18:18:40 -08001753 // if ro.tether.denied = true we default to no tethering
1754 // gservices could set the secure setting to 1 though to enable it on a build where it
1755 // had previously been turned off.
1756 public boolean isTetheringSupported() {
1757 enforceTetherAccessPermission();
1758 int defaultVal = (SystemProperties.get("ro.tether.denied").equals("true") ? 0 : 1);
Robert Greenwaltc9d5fb72010-02-25 12:29:30 -08001759 boolean tetherEnabledInSettings = (Settings.Secure.getInt(mContext.getContentResolver(),
1760 Settings.Secure.TETHER_SUPPORTED, defaultVal) != 0);
1761 return tetherEnabledInSettings && mTetheringConfigValid;
Robert Greenwalt2a091d72010-02-11 18:18:40 -08001762 }
Robert Greenwalt14f2ef42010-06-15 12:19:37 -07001763
1764 // An API NetworkStateTrackers can call when they lose their network.
1765 // This will automatically be cleared after X seconds or a network becomes CONNECTED,
1766 // whichever happens first. The timer is started by the first caller and not
1767 // restarted by subsequent callers.
1768 public void requestNetworkTransitionWakelock(String forWhom) {
1769 enforceConnectivityInternalPermission();
1770 synchronized (this) {
1771 if (mNetTransitionWakeLock.isHeld()) return;
1772 mNetTransitionWakeLockSerialNumber++;
1773 mNetTransitionWakeLock.acquire();
1774 mNetTransitionWakeLockCausedBy = forWhom;
1775 }
1776 mHandler.sendMessageDelayed(mHandler.obtainMessage(
1777 NetworkStateTracker.EVENT_CLEAR_NET_TRANSITION_WAKELOCK,
1778 mNetTransitionWakeLockSerialNumber, 0),
1779 mNetTransitionWakeLockTimeout);
1780 return;
1781 }
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001782}