blob: 96a239d5cf0c2b70cfac4ba1bb88fa94b5cbfc47 [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;
Wink Saville775aad62010-09-02 19:23:52 -070036import android.os.HandlerThread;
Robert Greenwalt2034b912009-08-12 16:08:25 -070037import android.os.IBinder;
The Android Open Source Project28527d22009-03-03 19:31:44 -080038import android.os.Looper;
39import android.os.Message;
Robert Greenwalt93dc1042010-06-15 12:19:37 -070040import android.os.PowerManager;
Robert Greenwalt2034b912009-08-12 16:08:25 -070041import android.os.RemoteException;
The Android Open Source Project28527d22009-03-03 19:31:44 -080042import android.os.ServiceManager;
43import android.os.SystemProperties;
44import android.provider.Settings;
Robert Greenwalt2034b912009-08-12 16:08:25 -070045import android.text.TextUtils;
The Android Open Source Project28527d22009-03-03 19:31:44 -080046import android.util.EventLog;
Joe Onoratoc2386bb2010-02-26 18:56:32 -080047import android.util.Slog;
The Android Open Source Project28527d22009-03-03 19:31:44 -080048
Robert Greenwalt2034b912009-08-12 16:08:25 -070049import com.android.internal.telephony.Phone;
50
Robert Greenwalt0c4828c2010-01-26 11:40:34 -080051import com.android.server.connectivity.Tethering;
52
The Android Open Source Project28527d22009-03-03 19:31:44 -080053import java.io.FileDescriptor;
Irfan Sheriff7f132d92010-06-09 15:39:36 -070054import java.io.FileWriter;
55import java.io.IOException;
The Android Open Source Project28527d22009-03-03 19:31:44 -080056import java.io.PrintWriter;
Robert Greenwalta7dfbd32010-06-15 15:43:39 -070057import java.net.InetAddress;
58import java.net.UnknownHostException;
Robert Greenwalt2034b912009-08-12 16:08:25 -070059import java.util.ArrayList;
Robert Greenwalta7dfbd32010-06-15 15:43:39 -070060import java.util.Collection;
Robert Greenwalt2034b912009-08-12 16:08:25 -070061import java.util.List;
Robert Greenwalt7fe44cb2010-08-27 09:24:29 -070062import java.net.InetAddress;
63import java.net.UnknownHostException;
The Android Open Source Project28527d22009-03-03 19:31:44 -080064
65/**
66 * @hide
67 */
68public class ConnectivityService extends IConnectivityManager.Stub {
69
Robert Greenwalta25fd712009-10-06 14:12:53 -070070 private static final boolean DBG = true;
The Android Open Source Project28527d22009-03-03 19:31:44 -080071 private static final String TAG = "ConnectivityService";
72
Robert Greenwalt2034b912009-08-12 16:08:25 -070073 // how long to wait before switching back to a radio's default network
74 private static final int RESTORE_DEFAULT_NETWORK_DELAY = 1 * 60 * 1000;
75 // system property that can override the above value
76 private static final String NETWORK_RESTORE_DELAY_PROP_NAME =
77 "android.telephony.apn-restore";
78
Robert Greenwalt0c4828c2010-01-26 11:40:34 -080079 private Tethering mTethering;
Robert Greenwaltf1b66e12010-02-25 12:29:30 -080080 private boolean mTetheringConfigValid = false;
Robert Greenwalt0c4828c2010-01-26 11:40:34 -080081
The Android Open Source Project28527d22009-03-03 19:31:44 -080082 /**
83 * Sometimes we want to refer to the individual network state
84 * trackers separately, and sometimes we just want to treat them
85 * abstractly.
86 */
87 private NetworkStateTracker mNetTrackers[];
Robert Greenwalt2034b912009-08-12 16:08:25 -070088
89 /**
90 * A per Net list of the PID's that requested access to the net
91 * used both as a refcount and for per-PID DNS selection
92 */
93 private List mNetRequestersPids[];
94
Irfan Sheriff653e2a22010-06-07 09:03:04 -070095 private WifiWatchdogService mWifiWatchdogService;
96
Robert Greenwalt2034b912009-08-12 16:08:25 -070097 // priority order of the nettrackers
98 // (excluding dynamically set mNetworkPreference)
99 // TODO - move mNetworkTypePreference into this
100 private int[] mPriorityList;
101
The Android Open Source Project28527d22009-03-03 19:31:44 -0800102 private Context mContext;
103 private int mNetworkPreference;
Robert Greenwalt2034b912009-08-12 16:08:25 -0700104 private int mActiveDefaultNetwork = -1;
Robert Greenwalt986c7412010-09-08 15:24:47 -0700105 // 0 is full bad, 100 is full good
106 private int mDefaultInetCondition = 0;
107 private int mDefaultInetConditionPublished = 0;
108 private boolean mInetConditionChangeInFlight = false;
109 private int mDefaultConnectionSequence = 0;
The Android Open Source Project28527d22009-03-03 19:31:44 -0800110
111 private int mNumDnsEntries;
The Android Open Source Project28527d22009-03-03 19:31:44 -0800112
113 private boolean mTestMode;
Joe Onorato56023ad2010-09-01 21:18:22 -0700114 private static ConnectivityService sServiceInstance;
The Android Open Source Project28527d22009-03-03 19:31:44 -0800115
Robert Greenwalt2034b912009-08-12 16:08:25 -0700116 private Handler mHandler;
117
118 // list of DeathRecipients used to make sure features are turned off when
119 // a process dies
120 private List mFeatureUsers;
121
Mike Lockwoodfde2b762009-08-14 14:18:49 -0400122 private boolean mSystemReady;
Dianne Hackborna417ff82009-12-08 19:45:14 -0800123 private Intent mInitialBroadcast;
Mike Lockwoodfde2b762009-08-14 14:18:49 -0400124
Robert Greenwalt93dc1042010-06-15 12:19:37 -0700125 private PowerManager.WakeLock mNetTransitionWakeLock;
126 private String mNetTransitionWakeLockCausedBy = "";
127 private int mNetTransitionWakeLockSerialNumber;
128 private int mNetTransitionWakeLockTimeout;
129
Robert Greenwalt94daa182010-09-01 11:34:05 -0700130 private InetAddress mDefaultDns;
131
Robert Greenwalt12c44552009-12-07 11:33:18 -0800132 private static class NetworkAttributes {
Robert Greenwalt2034b912009-08-12 16:08:25 -0700133 /**
134 * Class for holding settings read from resources.
135 */
136 public String mName;
137 public int mType;
138 public int mRadio;
139 public int mPriority;
Robert Greenwalt12c44552009-12-07 11:33:18 -0800140 public NetworkInfo.State mLastState;
Robert Greenwalt2034b912009-08-12 16:08:25 -0700141 public NetworkAttributes(String init) {
142 String fragments[] = init.split(",");
143 mName = fragments[0].toLowerCase();
Robert Greenwaltec05b3c2009-10-30 14:17:42 -0700144 mType = Integer.parseInt(fragments[1]);
145 mRadio = Integer.parseInt(fragments[2]);
146 mPriority = Integer.parseInt(fragments[3]);
Robert Greenwalt12c44552009-12-07 11:33:18 -0800147 mLastState = NetworkInfo.State.UNKNOWN;
Robert Greenwalt2034b912009-08-12 16:08:25 -0700148 }
149 public boolean isDefault() {
150 return (mType == mRadio);
151 }
152 }
153 NetworkAttributes[] mNetAttributes;
Robert Greenwaltec05b3c2009-10-30 14:17:42 -0700154 int mNetworksDefined;
Robert Greenwalt2034b912009-08-12 16:08:25 -0700155
Robert Greenwalt12c44552009-12-07 11:33:18 -0800156 private static class RadioAttributes {
Robert Greenwalt2034b912009-08-12 16:08:25 -0700157 public int mSimultaneity;
158 public int mType;
159 public RadioAttributes(String init) {
160 String fragments[] = init.split(",");
Robert Greenwaltec05b3c2009-10-30 14:17:42 -0700161 mType = Integer.parseInt(fragments[0]);
162 mSimultaneity = Integer.parseInt(fragments[1]);
Robert Greenwalt2034b912009-08-12 16:08:25 -0700163 }
164 }
165 RadioAttributes[] mRadioAttributes;
166
Wink Saville775aad62010-09-02 19:23:52 -0700167 public static synchronized ConnectivityService getInstance(Context context) {
168 if (sServiceInstance == null) {
169 sServiceInstance = new ConnectivityService(context);
The Android Open Source Project28527d22009-03-03 19:31:44 -0800170 }
Wink Saville775aad62010-09-02 19:23:52 -0700171 return sServiceInstance;
The Android Open Source Project28527d22009-03-03 19:31:44 -0800172 }
Robert Greenwalt0659da32009-07-16 17:21:39 -0700173
The Android Open Source Project28527d22009-03-03 19:31:44 -0800174 private ConnectivityService(Context context) {
Joe Onoratoc2386bb2010-02-26 18:56:32 -0800175 if (DBG) Slog.v(TAG, "ConnectivityService starting up");
Robert Greenwaltd48f8ee2010-01-14 17:47:58 -0800176
Wink Saville775aad62010-09-02 19:23:52 -0700177 HandlerThread handlerThread = new HandlerThread("ConnectivityServiceThread");
178 handlerThread.start();
179 mHandler = new MyHandler(handlerThread.getLooper());
180
Robert Greenwaltd48f8ee2010-01-14 17:47:58 -0800181 // setup our unique device name
182 String id = Settings.Secure.getString(context.getContentResolver(),
183 Settings.Secure.ANDROID_ID);
184 if (id != null && id.length() > 0) {
185 String name = new String("android_").concat(id);
186 SystemProperties.set("net.hostname", name);
187 }
188
Robert Greenwalt94daa182010-09-01 11:34:05 -0700189 // read our default dns server ip
190 String dns = Settings.Secure.getString(context.getContentResolver(),
191 Settings.Secure.DEFAULT_DNS_SERVER);
192 if (dns == null || dns.length() == 0) {
193 dns = context.getResources().getString(
194 com.android.internal.R.string.config_default_dns_server);
195 }
196 try {
197 mDefaultDns = InetAddress.getByName(dns);
198 } catch (UnknownHostException e) {
199 Slog.e(TAG, "Error setting defaultDns using " + dns);
200 }
201
The Android Open Source Project28527d22009-03-03 19:31:44 -0800202 mContext = context;
Robert Greenwalt93dc1042010-06-15 12:19:37 -0700203
204 PowerManager powerManager = (PowerManager)mContext.getSystemService(Context.POWER_SERVICE);
205 mNetTransitionWakeLock = powerManager.newWakeLock(PowerManager.PARTIAL_WAKE_LOCK, TAG);
206 mNetTransitionWakeLockTimeout = mContext.getResources().getInteger(
207 com.android.internal.R.integer.config_networkTransitionTimeout);
208
Robert Greenwalt2034b912009-08-12 16:08:25 -0700209 mNetTrackers = new NetworkStateTracker[
210 ConnectivityManager.MAX_NETWORK_TYPE+1];
Robert Greenwalt0659da32009-07-16 17:21:39 -0700211
The Android Open Source Project28527d22009-03-03 19:31:44 -0800212 mNetworkPreference = getPersistedNetworkPreference();
Robert Greenwalt0659da32009-07-16 17:21:39 -0700213
Robert Greenwaltec05b3c2009-10-30 14:17:42 -0700214 mRadioAttributes = new RadioAttributes[ConnectivityManager.MAX_RADIO_TYPE+1];
215 mNetAttributes = new NetworkAttributes[ConnectivityManager.MAX_NETWORK_TYPE+1];
216
Robert Greenwalt2034b912009-08-12 16:08:25 -0700217 // Load device network attributes from resources
Robert Greenwalt2034b912009-08-12 16:08:25 -0700218 String[] raStrings = context.getResources().getStringArray(
219 com.android.internal.R.array.radioAttributes);
Robert Greenwaltec05b3c2009-10-30 14:17:42 -0700220 for (String raString : raStrings) {
221 RadioAttributes r = new RadioAttributes(raString);
222 if (r.mType > ConnectivityManager.MAX_RADIO_TYPE) {
Joe Onoratoc2386bb2010-02-26 18:56:32 -0800223 Slog.e(TAG, "Error in radioAttributes - ignoring attempt to define type " + r.mType);
Robert Greenwaltec05b3c2009-10-30 14:17:42 -0700224 continue;
225 }
226 if (mRadioAttributes[r.mType] != null) {
Joe Onoratoc2386bb2010-02-26 18:56:32 -0800227 Slog.e(TAG, "Error in radioAttributes - ignoring attempt to redefine type " +
Robert Greenwaltec05b3c2009-10-30 14:17:42 -0700228 r.mType);
229 continue;
230 }
Robert Greenwalt2034b912009-08-12 16:08:25 -0700231 mRadioAttributes[r.mType] = r;
232 }
233
Robert Greenwaltec05b3c2009-10-30 14:17:42 -0700234 String[] naStrings = context.getResources().getStringArray(
235 com.android.internal.R.array.networkAttributes);
236 for (String naString : naStrings) {
237 try {
238 NetworkAttributes n = new NetworkAttributes(naString);
239 if (n.mType > ConnectivityManager.MAX_NETWORK_TYPE) {
Joe Onoratoc2386bb2010-02-26 18:56:32 -0800240 Slog.e(TAG, "Error in networkAttributes - ignoring attempt to define type " +
Robert Greenwaltec05b3c2009-10-30 14:17:42 -0700241 n.mType);
242 continue;
Robert Greenwalt2034b912009-08-12 16:08:25 -0700243 }
Robert Greenwaltec05b3c2009-10-30 14:17:42 -0700244 if (mNetAttributes[n.mType] != null) {
Joe Onoratoc2386bb2010-02-26 18:56:32 -0800245 Slog.e(TAG, "Error in networkAttributes - ignoring attempt to redefine type " +
Robert Greenwaltec05b3c2009-10-30 14:17:42 -0700246 n.mType);
247 continue;
248 }
249 if (mRadioAttributes[n.mRadio] == null) {
Joe Onoratoc2386bb2010-02-26 18:56:32 -0800250 Slog.e(TAG, "Error in networkAttributes - ignoring attempt to use undefined " +
Robert Greenwaltec05b3c2009-10-30 14:17:42 -0700251 "radio " + n.mRadio + " in network type " + n.mType);
252 continue;
253 }
254 mNetAttributes[n.mType] = n;
255 mNetworksDefined++;
256 } catch(Exception e) {
257 // ignore it - leave the entry null
Robert Greenwalt2034b912009-08-12 16:08:25 -0700258 }
259 }
260
Robert Greenwaltec05b3c2009-10-30 14:17:42 -0700261 // high priority first
262 mPriorityList = new int[mNetworksDefined];
263 {
264 int insertionPoint = mNetworksDefined-1;
265 int currentLowest = 0;
266 int nextLowest = 0;
267 while (insertionPoint > -1) {
268 for (NetworkAttributes na : mNetAttributes) {
269 if (na == null) continue;
270 if (na.mPriority < currentLowest) continue;
271 if (na.mPriority > currentLowest) {
272 if (na.mPriority < nextLowest || nextLowest == 0) {
273 nextLowest = na.mPriority;
274 }
275 continue;
276 }
277 mPriorityList[insertionPoint--] = na.mType;
278 }
279 currentLowest = nextLowest;
280 nextLowest = 0;
281 }
282 }
283
284 mNetRequestersPids = new ArrayList[ConnectivityManager.MAX_NETWORK_TYPE+1];
285 for (int i : mPriorityList) {
Robert Greenwalt2034b912009-08-12 16:08:25 -0700286 mNetRequestersPids[i] = new ArrayList();
287 }
288
289 mFeatureUsers = new ArrayList();
290
Robert Greenwaltec05b3c2009-10-30 14:17:42 -0700291 mNumDnsEntries = 0;
292
293 mTestMode = SystemProperties.get("cm.test.mode").equals("true")
294 && SystemProperties.get("ro.build.type").equals("eng");
The Android Open Source Project28527d22009-03-03 19:31:44 -0800295 /*
296 * Create the network state trackers for Wi-Fi and mobile
297 * data. Maybe this could be done with a factory class,
298 * but it's not clear that it's worth it, given that
299 * the number of different network types is not going
300 * to change very often.
301 */
Robert Greenwalt1b21f6c2010-02-23 18:58:05 -0800302 boolean noMobileData = !getMobileDataEnabled();
Robert Greenwaltec05b3c2009-10-30 14:17:42 -0700303 for (int netType : mPriorityList) {
304 switch (mNetAttributes[netType].mRadio) {
305 case ConnectivityManager.TYPE_WIFI:
Joe Onoratoc2386bb2010-02-26 18:56:32 -0800306 if (DBG) Slog.v(TAG, "Starting Wifi Service.");
Wink Saville7fabfa22010-08-13 16:11:42 -0700307 WifiStateTracker wst = new WifiStateTracker();
Irfan Sheriff25be0762010-07-28 09:35:20 -0700308 WifiService wifiService = new WifiService(context);
Robert Greenwaltec05b3c2009-10-30 14:17:42 -0700309 ServiceManager.addService(Context.WIFI_SERVICE, wifiService);
Irfan Sheriff25be0762010-07-28 09:35:20 -0700310 wifiService.checkAndStartWifi();
Robert Greenwaltec05b3c2009-10-30 14:17:42 -0700311 mNetTrackers[ConnectivityManager.TYPE_WIFI] = wst;
Wink Saville7fabfa22010-08-13 16:11:42 -0700312 wst.startMonitoring(context, mHandler);
The Android Open Source Project28527d22009-03-03 19:31:44 -0800313
Irfan Sheriff653e2a22010-06-07 09:03:04 -0700314 //TODO: as part of WWS refactor, create only when needed
Irfan Sheriff25be0762010-07-28 09:35:20 -0700315 mWifiWatchdogService = new WifiWatchdogService(context);
Irfan Sheriff653e2a22010-06-07 09:03:04 -0700316
Robert Greenwaltec05b3c2009-10-30 14:17:42 -0700317 break;
318 case ConnectivityManager.TYPE_MOBILE:
Wink Saville7fabfa22010-08-13 16:11:42 -0700319 mNetTrackers[netType] = new MobileDataStateTracker(netType,
320 mNetAttributes[netType].mName);
321 mNetTrackers[netType].startMonitoring(context, mHandler);
Robert Greenwalt1b21f6c2010-02-23 18:58:05 -0800322 if (noMobileData) {
Joe Onoratoc2386bb2010-02-26 18:56:32 -0800323 if (DBG) Slog.d(TAG, "tearing down Mobile networks due to setting");
Robert Greenwalt1b21f6c2010-02-23 18:58:05 -0800324 mNetTrackers[netType].teardown();
325 }
Robert Greenwaltec05b3c2009-10-30 14:17:42 -0700326 break;
327 default:
Joe Onoratoc2386bb2010-02-26 18:56:32 -0800328 Slog.e(TAG, "Trying to create a DataStateTracker for an unknown radio type " +
Robert Greenwaltec05b3c2009-10-30 14:17:42 -0700329 mNetAttributes[netType].mRadio);
330 continue;
331 }
332 }
Robert Greenwalt0c4828c2010-01-26 11:40:34 -0800333
Robert Greenwaltc0b6c602010-03-11 15:03:08 -0800334 mTethering = new Tethering(mContext, mHandler.getLooper());
Robert Greenwaltf1b66e12010-02-25 12:29:30 -0800335 mTetheringConfigValid = (((mNetTrackers[ConnectivityManager.TYPE_MOBILE_DUN] != null) ||
336 !mTethering.isDunRequired()) &&
337 (mTethering.getTetherableUsbRegexs().length != 0 ||
Danica Chang96567052010-08-11 14:54:43 -0700338 mTethering.getTetherableWifiRegexs().length != 0 ||
339 mTethering.getTetherableBluetoothRegexs().length != 0) &&
Robert Greenwaltf1b66e12010-02-25 12:29:30 -0800340 mTethering.getUpstreamIfaceRegexs().length != 0);
341
The Android Open Source Project28527d22009-03-03 19:31:44 -0800342 }
343
Robert Greenwaltec05b3c2009-10-30 14:17:42 -0700344
The Android Open Source Project28527d22009-03-03 19:31:44 -0800345 /**
Robert Greenwalt0659da32009-07-16 17:21:39 -0700346 * Sets the preferred network.
The Android Open Source Project28527d22009-03-03 19:31:44 -0800347 * @param preference the new preference
348 */
349 public synchronized void setNetworkPreference(int preference) {
350 enforceChangePermission();
Robert Greenwalt2034b912009-08-12 16:08:25 -0700351 if (ConnectivityManager.isNetworkTypeValid(preference) &&
Robert Greenwaltec05b3c2009-10-30 14:17:42 -0700352 mNetAttributes[preference] != null &&
Robert Greenwalt2034b912009-08-12 16:08:25 -0700353 mNetAttributes[preference].isDefault()) {
The Android Open Source Project28527d22009-03-03 19:31:44 -0800354 if (mNetworkPreference != preference) {
355 persistNetworkPreference(preference);
356 mNetworkPreference = preference;
357 enforcePreference();
358 }
359 }
360 }
361
362 public int getNetworkPreference() {
363 enforceAccessPermission();
364 return mNetworkPreference;
365 }
366
367 private void persistNetworkPreference(int networkPreference) {
368 final ContentResolver cr = mContext.getContentResolver();
Robert Greenwalt0659da32009-07-16 17:21:39 -0700369 Settings.Secure.putInt(cr, Settings.Secure.NETWORK_PREFERENCE,
370 networkPreference);
The Android Open Source Project28527d22009-03-03 19:31:44 -0800371 }
Robert Greenwalt0659da32009-07-16 17:21:39 -0700372
The Android Open Source Project28527d22009-03-03 19:31:44 -0800373 private int getPersistedNetworkPreference() {
374 final ContentResolver cr = mContext.getContentResolver();
375
376 final int networkPrefSetting = Settings.Secure
377 .getInt(cr, Settings.Secure.NETWORK_PREFERENCE, -1);
378 if (networkPrefSetting != -1) {
379 return networkPrefSetting;
380 }
381
382 return ConnectivityManager.DEFAULT_NETWORK_PREFERENCE;
383 }
Robert Greenwalt0659da32009-07-16 17:21:39 -0700384
The Android Open Source Project28527d22009-03-03 19:31:44 -0800385 /**
Robert Greenwalt0659da32009-07-16 17:21:39 -0700386 * Make the state of network connectivity conform to the preference settings
The Android Open Source Project28527d22009-03-03 19:31:44 -0800387 * In this method, we only tear down a non-preferred network. Establishing
388 * a connection to the preferred network is taken care of when we handle
389 * the disconnect event from the non-preferred network
390 * (see {@link #handleDisconnect(NetworkInfo)}).
391 */
392 private void enforcePreference() {
Robert Greenwalt2034b912009-08-12 16:08:25 -0700393 if (mNetTrackers[mNetworkPreference].getNetworkInfo().isConnected())
The Android Open Source Project28527d22009-03-03 19:31:44 -0800394 return;
395
Robert Greenwalt2034b912009-08-12 16:08:25 -0700396 if (!mNetTrackers[mNetworkPreference].isAvailable())
397 return;
The Android Open Source Project28527d22009-03-03 19:31:44 -0800398
Robert Greenwalt2034b912009-08-12 16:08:25 -0700399 for (int t=0; t <= ConnectivityManager.MAX_RADIO_TYPE; t++) {
Robert Greenwaltec05b3c2009-10-30 14:17:42 -0700400 if (t != mNetworkPreference && mNetTrackers[t] != null &&
Robert Greenwalt2034b912009-08-12 16:08:25 -0700401 mNetTrackers[t].getNetworkInfo().isConnected()) {
Robert Greenwaltf3f045b2009-08-20 15:25:14 -0700402 if (DBG) {
Joe Onoratoc2386bb2010-02-26 18:56:32 -0800403 Slog.d(TAG, "tearing down " +
Robert Greenwaltf3f045b2009-08-20 15:25:14 -0700404 mNetTrackers[t].getNetworkInfo() +
405 " in enforcePreference");
406 }
Robert Greenwalt2034b912009-08-12 16:08:25 -0700407 teardown(mNetTrackers[t]);
The Android Open Source Project28527d22009-03-03 19:31:44 -0800408 }
409 }
410 }
411
412 private boolean teardown(NetworkStateTracker netTracker) {
413 if (netTracker.teardown()) {
414 netTracker.setTeardownRequested(true);
415 return true;
416 } else {
417 return false;
418 }
419 }
420
421 /**
422 * Return NetworkInfo for the active (i.e., connected) network interface.
423 * It is assumed that at most one network is active at a time. If more
424 * than one is active, it is indeterminate which will be returned.
Robert Greenwalt0659da32009-07-16 17:21:39 -0700425 * @return the info for the active network, or {@code null} if none is
426 * active
The Android Open Source Project28527d22009-03-03 19:31:44 -0800427 */
428 public NetworkInfo getActiveNetworkInfo() {
429 enforceAccessPermission();
Robert Greenwalt2034b912009-08-12 16:08:25 -0700430 for (int type=0; type <= ConnectivityManager.MAX_NETWORK_TYPE; type++) {
Robert Greenwaltec05b3c2009-10-30 14:17:42 -0700431 if (mNetAttributes[type] == null || !mNetAttributes[type].isDefault()) {
Robert Greenwalt2034b912009-08-12 16:08:25 -0700432 continue;
433 }
434 NetworkStateTracker t = mNetTrackers[type];
The Android Open Source Project28527d22009-03-03 19:31:44 -0800435 NetworkInfo info = t.getNetworkInfo();
436 if (info.isConnected()) {
Joe Onoratoc2386bb2010-02-26 18:56:32 -0800437 if (DBG && type != mActiveDefaultNetwork) Slog.e(TAG,
Robert Greenwalt2034b912009-08-12 16:08:25 -0700438 "connected default network is not " +
439 "mActiveDefaultNetwork!");
The Android Open Source Project28527d22009-03-03 19:31:44 -0800440 return info;
441 }
442 }
443 return null;
444 }
445
446 public NetworkInfo getNetworkInfo(int networkType) {
447 enforceAccessPermission();
448 if (ConnectivityManager.isNetworkTypeValid(networkType)) {
449 NetworkStateTracker t = mNetTrackers[networkType];
450 if (t != null)
451 return t.getNetworkInfo();
452 }
453 return null;
454 }
455
456 public NetworkInfo[] getAllNetworkInfo() {
457 enforceAccessPermission();
Robert Greenwaltec05b3c2009-10-30 14:17:42 -0700458 NetworkInfo[] result = new NetworkInfo[mNetworksDefined];
The Android Open Source Project28527d22009-03-03 19:31:44 -0800459 int i = 0;
460 for (NetworkStateTracker t : mNetTrackers) {
Robert Greenwaltec05b3c2009-10-30 14:17:42 -0700461 if(t != null) result[i++] = t.getNetworkInfo();
The Android Open Source Project28527d22009-03-03 19:31:44 -0800462 }
463 return result;
464 }
465
466 public boolean setRadios(boolean turnOn) {
467 boolean result = true;
468 enforceChangePermission();
469 for (NetworkStateTracker t : mNetTrackers) {
Robert Greenwaltec05b3c2009-10-30 14:17:42 -0700470 if (t != null) result = t.setRadio(turnOn) && result;
The Android Open Source Project28527d22009-03-03 19:31:44 -0800471 }
472 return result;
473 }
474
475 public boolean setRadio(int netType, boolean turnOn) {
476 enforceChangePermission();
477 if (!ConnectivityManager.isNetworkTypeValid(netType)) {
478 return false;
479 }
480 NetworkStateTracker tracker = mNetTrackers[netType];
481 return tracker != null && tracker.setRadio(turnOn);
482 }
483
Robert Greenwaltaffc3a12009-09-27 17:27:04 -0700484 /**
485 * Used to notice when the calling process dies so we can self-expire
486 *
487 * Also used to know if the process has cleaned up after itself when
488 * our auto-expire timer goes off. The timer has a link to an object.
489 *
490 */
Robert Greenwalt2034b912009-08-12 16:08:25 -0700491 private class FeatureUser implements IBinder.DeathRecipient {
492 int mNetworkType;
493 String mFeature;
494 IBinder mBinder;
495 int mPid;
496 int mUid;
Robert Greenwalt3eeb6032009-12-21 18:24:07 -0800497 long mCreateTime;
Robert Greenwalt2034b912009-08-12 16:08:25 -0700498
499 FeatureUser(int type, String feature, IBinder binder) {
500 super();
501 mNetworkType = type;
502 mFeature = feature;
503 mBinder = binder;
504 mPid = getCallingPid();
505 mUid = getCallingUid();
Robert Greenwalt3eeb6032009-12-21 18:24:07 -0800506 mCreateTime = System.currentTimeMillis();
Robert Greenwaltaffc3a12009-09-27 17:27:04 -0700507
Robert Greenwalt2034b912009-08-12 16:08:25 -0700508 try {
509 mBinder.linkToDeath(this, 0);
510 } catch (RemoteException e) {
511 binderDied();
512 }
513 }
514
515 void unlinkDeathRecipient() {
516 mBinder.unlinkToDeath(this, 0);
517 }
518
519 public void binderDied() {
Joe Onoratoc2386bb2010-02-26 18:56:32 -0800520 Slog.d(TAG, "ConnectivityService FeatureUser binderDied(" +
Robert Greenwalt3eeb6032009-12-21 18:24:07 -0800521 mNetworkType + ", " + mFeature + ", " + mBinder + "), created " +
522 (System.currentTimeMillis() - mCreateTime) + " mSec ago");
Robert Greenwaltaffc3a12009-09-27 17:27:04 -0700523 stopUsingNetworkFeature(this, false);
Robert Greenwalt2034b912009-08-12 16:08:25 -0700524 }
525
Robert Greenwaltaffc3a12009-09-27 17:27:04 -0700526 public void expire() {
Joe Onoratoc2386bb2010-02-26 18:56:32 -0800527 Slog.d(TAG, "ConnectivityService FeatureUser expire(" +
Robert Greenwalt3eeb6032009-12-21 18:24:07 -0800528 mNetworkType + ", " + mFeature + ", " + mBinder +"), created " +
529 (System.currentTimeMillis() - mCreateTime) + " mSec ago");
Robert Greenwaltaffc3a12009-09-27 17:27:04 -0700530 stopUsingNetworkFeature(this, false);
531 }
Robert Greenwalt3eeb6032009-12-21 18:24:07 -0800532
533 public String toString() {
534 return "FeatureUser("+mNetworkType+","+mFeature+","+mPid+","+mUid+"), created " +
535 (System.currentTimeMillis() - mCreateTime) + " mSec ago";
536 }
Robert Greenwalt2034b912009-08-12 16:08:25 -0700537 }
538
Robert Greenwaltaffc3a12009-09-27 17:27:04 -0700539 // javadoc from interface
Robert Greenwalt2034b912009-08-12 16:08:25 -0700540 public int startUsingNetworkFeature(int networkType, String feature,
541 IBinder binder) {
542 if (DBG) {
Joe Onoratoc2386bb2010-02-26 18:56:32 -0800543 Slog.d(TAG, "startUsingNetworkFeature for net " + networkType +
Robert Greenwalt2034b912009-08-12 16:08:25 -0700544 ": " + feature);
545 }
The Android Open Source Project28527d22009-03-03 19:31:44 -0800546 enforceChangePermission();
Robert Greenwaltec05b3c2009-10-30 14:17:42 -0700547 if (!ConnectivityManager.isNetworkTypeValid(networkType) ||
548 mNetAttributes[networkType] == null) {
Robert Greenwalt2034b912009-08-12 16:08:25 -0700549 return Phone.APN_REQUEST_FAILED;
The Android Open Source Project28527d22009-03-03 19:31:44 -0800550 }
Robert Greenwalt2034b912009-08-12 16:08:25 -0700551
Robert Greenwaltaffc3a12009-09-27 17:27:04 -0700552 FeatureUser f = new FeatureUser(networkType, feature, binder);
Robert Greenwalt2034b912009-08-12 16:08:25 -0700553
554 // TODO - move this into the MobileDataStateTracker
555 int usedNetworkType = networkType;
556 if(networkType == ConnectivityManager.TYPE_MOBILE) {
Robert Greenwalt1b21f6c2010-02-23 18:58:05 -0800557 if (!getMobileDataEnabled()) {
Joe Onoratoc2386bb2010-02-26 18:56:32 -0800558 if (DBG) Slog.d(TAG, "requested special network with data disabled - rejected");
Robert Greenwalt1b21f6c2010-02-23 18:58:05 -0800559 return Phone.APN_TYPE_NOT_AVAILABLE;
560 }
Robert Greenwalt2034b912009-08-12 16:08:25 -0700561 if (TextUtils.equals(feature, Phone.FEATURE_ENABLE_MMS)) {
562 usedNetworkType = ConnectivityManager.TYPE_MOBILE_MMS;
563 } else if (TextUtils.equals(feature, Phone.FEATURE_ENABLE_SUPL)) {
564 usedNetworkType = ConnectivityManager.TYPE_MOBILE_SUPL;
565 } else if (TextUtils.equals(feature, Phone.FEATURE_ENABLE_DUN)) {
566 usedNetworkType = ConnectivityManager.TYPE_MOBILE_DUN;
567 } else if (TextUtils.equals(feature, Phone.FEATURE_ENABLE_HIPRI)) {
568 usedNetworkType = ConnectivityManager.TYPE_MOBILE_HIPRI;
569 }
570 }
571 NetworkStateTracker network = mNetTrackers[usedNetworkType];
572 if (network != null) {
573 if (usedNetworkType != networkType) {
574 Integer currentPid = new Integer(getCallingPid());
575
576 NetworkStateTracker radio = mNetTrackers[networkType];
577 NetworkInfo ni = network.getNetworkInfo();
578
579 if (ni.isAvailable() == false) {
Joe Onoratoc2386bb2010-02-26 18:56:32 -0800580 if (DBG) Slog.d(TAG, "special network not available");
Robert Greenwalt2034b912009-08-12 16:08:25 -0700581 return Phone.APN_TYPE_NOT_AVAILABLE;
582 }
583
Robert Greenwaltaffc3a12009-09-27 17:27:04 -0700584 synchronized(this) {
585 mFeatureUsers.add(f);
586 if (!mNetRequestersPids[usedNetworkType].contains(currentPid)) {
587 // this gets used for per-pid dns when connected
588 mNetRequestersPids[usedNetworkType].add(currentPid);
589 }
Robert Greenwalt2034b912009-08-12 16:08:25 -0700590 }
Robert Greenwaltaffc3a12009-09-27 17:27:04 -0700591 mHandler.sendMessageDelayed(mHandler.obtainMessage(
592 NetworkStateTracker.EVENT_RESTORE_DEFAULT_NETWORK,
593 f), getRestoreDefaultNetworkDelay());
594
Robert Greenwalt2034b912009-08-12 16:08:25 -0700595
Robert Greenwalta52c75a2009-08-19 20:19:33 -0700596 if ((ni.isConnectedOrConnecting() == true) &&
597 !network.isTeardownRequested()) {
Robert Greenwalt2034b912009-08-12 16:08:25 -0700598 if (ni.isConnected() == true) {
599 // add the pid-specific dns
Robert Greenwalt3afbead2010-07-23 15:46:26 -0700600 handleDnsConfigurationChange(networkType);
Joe Onoratoc2386bb2010-02-26 18:56:32 -0800601 if (DBG) Slog.d(TAG, "special network already active");
Robert Greenwalt2034b912009-08-12 16:08:25 -0700602 return Phone.APN_ALREADY_ACTIVE;
603 }
Joe Onoratoc2386bb2010-02-26 18:56:32 -0800604 if (DBG) Slog.d(TAG, "special network already connecting");
Robert Greenwalt2034b912009-08-12 16:08:25 -0700605 return Phone.APN_REQUEST_STARTED;
606 }
607
608 // check if the radio in play can make another contact
609 // assume if cannot for now
610
Joe Onoratoc2386bb2010-02-26 18:56:32 -0800611 if (DBG) Slog.d(TAG, "reconnecting to special network");
Robert Greenwalt2034b912009-08-12 16:08:25 -0700612 network.reconnect();
613 return Phone.APN_REQUEST_STARTED;
614 } else {
Robert Greenwaltd391e892010-05-18 10:52:51 -0700615 return -1;
Robert Greenwalt2034b912009-08-12 16:08:25 -0700616 }
617 }
618 return Phone.APN_TYPE_NOT_AVAILABLE;
The Android Open Source Project28527d22009-03-03 19:31:44 -0800619 }
620
Robert Greenwaltaffc3a12009-09-27 17:27:04 -0700621 // javadoc from interface
The Android Open Source Project28527d22009-03-03 19:31:44 -0800622 public int stopUsingNetworkFeature(int networkType, String feature) {
Robert Greenwalt28f43012009-10-06 17:52:40 -0700623 enforceChangePermission();
624
Robert Greenwaltaffc3a12009-09-27 17:27:04 -0700625 int pid = getCallingPid();
626 int uid = getCallingUid();
627
628 FeatureUser u = null;
629 boolean found = false;
630
631 synchronized(this) {
632 for (int i = 0; i < mFeatureUsers.size() ; i++) {
633 u = (FeatureUser)mFeatureUsers.get(i);
634 if (uid == u.mUid && pid == u.mPid &&
635 networkType == u.mNetworkType &&
636 TextUtils.equals(feature, u.mFeature)) {
637 found = true;
638 break;
639 }
640 }
641 }
642 if (found && u != null) {
643 // stop regardless of how many other time this proc had called start
644 return stopUsingNetworkFeature(u, true);
645 } else {
646 // none found!
Joe Onoratoc2386bb2010-02-26 18:56:32 -0800647 if (DBG) Slog.d(TAG, "ignoring stopUsingNetworkFeature - not a live request");
Robert Greenwaltaffc3a12009-09-27 17:27:04 -0700648 return 1;
649 }
Robert Greenwalt2034b912009-08-12 16:08:25 -0700650 }
651
Robert Greenwaltaffc3a12009-09-27 17:27:04 -0700652 private int stopUsingNetworkFeature(FeatureUser u, boolean ignoreDups) {
653 int networkType = u.mNetworkType;
654 String feature = u.mFeature;
655 int pid = u.mPid;
656 int uid = u.mUid;
657
658 NetworkStateTracker tracker = null;
659 boolean callTeardown = false; // used to carry our decision outside of sync block
660
Robert Greenwalt2034b912009-08-12 16:08:25 -0700661 if (DBG) {
Joe Onoratoc2386bb2010-02-26 18:56:32 -0800662 Slog.d(TAG, "stopUsingNetworkFeature for net " + networkType +
Robert Greenwalt2034b912009-08-12 16:08:25 -0700663 ": " + feature);
664 }
Robert Greenwalt28f43012009-10-06 17:52:40 -0700665
The Android Open Source Project28527d22009-03-03 19:31:44 -0800666 if (!ConnectivityManager.isNetworkTypeValid(networkType)) {
667 return -1;
668 }
Robert Greenwalt2034b912009-08-12 16:08:25 -0700669
Robert Greenwaltaffc3a12009-09-27 17:27:04 -0700670 // need to link the mFeatureUsers list with the mNetRequestersPids state in this
671 // sync block
672 synchronized(this) {
673 // check if this process still has an outstanding start request
674 if (!mFeatureUsers.contains(u)) {
Robert Greenwalt46ceefa2010-03-10 16:10:43 -0800675 if (DBG) Slog.d(TAG, "ignoring - this process has no outstanding requests");
Robert Greenwalt2034b912009-08-12 16:08:25 -0700676 return 1;
677 }
Robert Greenwaltaffc3a12009-09-27 17:27:04 -0700678 u.unlinkDeathRecipient();
679 mFeatureUsers.remove(mFeatureUsers.indexOf(u));
680 // If we care about duplicate requests, check for that here.
681 //
682 // This is done to support the extension of a request - the app
683 // can request we start the network feature again and renew the
684 // auto-shutoff delay. Normal "stop" calls from the app though
685 // do not pay attention to duplicate requests - in effect the
686 // API does not refcount and a single stop will counter multiple starts.
687 if (ignoreDups == false) {
688 for (int i = 0; i < mFeatureUsers.size() ; i++) {
689 FeatureUser x = (FeatureUser)mFeatureUsers.get(i);
690 if (x.mUid == u.mUid && x.mPid == u.mPid &&
691 x.mNetworkType == u.mNetworkType &&
692 TextUtils.equals(x.mFeature, u.mFeature)) {
Joe Onoratoc2386bb2010-02-26 18:56:32 -0800693 if (DBG) Slog.d(TAG, "ignoring stopUsingNetworkFeature as dup is found");
Robert Greenwaltaffc3a12009-09-27 17:27:04 -0700694 return 1;
695 }
696 }
Robert Greenwalt2034b912009-08-12 16:08:25 -0700697 }
Robert Greenwaltaffc3a12009-09-27 17:27:04 -0700698
699 // TODO - move to MobileDataStateTracker
700 int usedNetworkType = networkType;
701 if (networkType == ConnectivityManager.TYPE_MOBILE) {
702 if (TextUtils.equals(feature, Phone.FEATURE_ENABLE_MMS)) {
703 usedNetworkType = ConnectivityManager.TYPE_MOBILE_MMS;
704 } else if (TextUtils.equals(feature, Phone.FEATURE_ENABLE_SUPL)) {
705 usedNetworkType = ConnectivityManager.TYPE_MOBILE_SUPL;
706 } else if (TextUtils.equals(feature, Phone.FEATURE_ENABLE_DUN)) {
707 usedNetworkType = ConnectivityManager.TYPE_MOBILE_DUN;
708 } else if (TextUtils.equals(feature, Phone.FEATURE_ENABLE_HIPRI)) {
709 usedNetworkType = ConnectivityManager.TYPE_MOBILE_HIPRI;
710 }
711 }
712 tracker = mNetTrackers[usedNetworkType];
Robert Greenwaltec05b3c2009-10-30 14:17:42 -0700713 if (tracker == null) {
Robert Greenwalt46ceefa2010-03-10 16:10:43 -0800714 if (DBG) Slog.d(TAG, "ignoring - no known tracker for net type " + usedNetworkType);
Robert Greenwaltec05b3c2009-10-30 14:17:42 -0700715 return -1;
716 }
717 if (usedNetworkType != networkType) {
Robert Greenwaltaffc3a12009-09-27 17:27:04 -0700718 Integer currentPid = new Integer(pid);
Robert Greenwaltaffc3a12009-09-27 17:27:04 -0700719 mNetRequestersPids[usedNetworkType].remove(currentPid);
Robert Greenwalt0ca68a02009-12-17 14:54:59 -0800720 reassessPidDns(pid, true);
Robert Greenwaltaffc3a12009-09-27 17:27:04 -0700721 if (mNetRequestersPids[usedNetworkType].size() != 0) {
Joe Onoratoc2386bb2010-02-26 18:56:32 -0800722 if (DBG) Slog.d(TAG, "not tearing down special network - " +
Robert Greenwaltaffc3a12009-09-27 17:27:04 -0700723 "others still using it");
724 return 1;
725 }
726 callTeardown = true;
727 }
728 }
Robert Greenwalt46ceefa2010-03-10 16:10:43 -0800729 if (DBG) Slog.d(TAG, "Doing network teardown");
Robert Greenwaltaffc3a12009-09-27 17:27:04 -0700730 if (callTeardown) {
731 tracker.teardown();
Robert Greenwalt2034b912009-08-12 16:08:25 -0700732 return 1;
733 } else {
Robert Greenwaltd391e892010-05-18 10:52:51 -0700734 return -1;
Robert Greenwalt2034b912009-08-12 16:08:25 -0700735 }
The Android Open Source Project28527d22009-03-03 19:31:44 -0800736 }
737
738 /**
Robert Greenwalt7fe44cb2010-08-27 09:24:29 -0700739 * @deprecated use requestRouteToHostAddress instead
740 *
The Android Open Source Project28527d22009-03-03 19:31:44 -0800741 * Ensure that a network route exists to deliver traffic to the specified
742 * host via the specified network interface.
Robert Greenwalt0659da32009-07-16 17:21:39 -0700743 * @param networkType the type of the network over which traffic to the
744 * specified host is to be routed
745 * @param hostAddress the IP address of the host to which the route is
746 * desired
The Android Open Source Project28527d22009-03-03 19:31:44 -0800747 * @return {@code true} on success, {@code false} on failure
748 */
749 public boolean requestRouteToHost(int networkType, int hostAddress) {
Robert Greenwalt7fe44cb2010-08-27 09:24:29 -0700750 InetAddress inetAddress = NetworkUtils.intToInetAddress(hostAddress);
751
752 if (inetAddress == null) {
753 return false;
754 }
755
756 return requestRouteToHostAddress(networkType, inetAddress.getAddress());
757 }
758
759 /**
760 * Ensure that a network route exists to deliver traffic to the specified
761 * host via the specified network interface.
762 * @param networkType the type of the network over which traffic to the
763 * specified host is to be routed
764 * @param hostAddress the IP address of the host to which the route is
765 * desired
766 * @return {@code true} on success, {@code false} on failure
767 */
768 public boolean requestRouteToHostAddress(int networkType, byte[] hostAddress) {
The Android Open Source Project28527d22009-03-03 19:31:44 -0800769 enforceChangePermission();
770 if (!ConnectivityManager.isNetworkTypeValid(networkType)) {
771 return false;
772 }
773 NetworkStateTracker tracker = mNetTrackers[networkType];
Robert Greenwalt4666ed02009-09-10 15:06:20 -0700774
Robert Greenwaltec05b3c2009-10-30 14:17:42 -0700775 if (tracker == null || !tracker.getNetworkInfo().isConnected() ||
776 tracker.isTeardownRequested()) {
Robert Greenwalt4666ed02009-09-10 15:06:20 -0700777 if (DBG) {
Robert Greenwalt7fe44cb2010-08-27 09:24:29 -0700778 Slog.d(TAG, "requestRouteToHostAddress on down network " +
779 "(" + networkType + ") - dropped");
Robert Greenwalt4666ed02009-09-10 15:06:20 -0700780 }
781 return false;
The Android Open Source Project28527d22009-03-03 19:31:44 -0800782 }
Robert Greenwalta7dfbd32010-06-15 15:43:39 -0700783 try {
Robert Greenwalt7fe44cb2010-08-27 09:24:29 -0700784 InetAddress addr = InetAddress.getByAddress(hostAddress);
Robert Greenwalta7dfbd32010-06-15 15:43:39 -0700785 return addHostRoute(tracker, addr);
786 } catch (UnknownHostException e) {}
787 return false;
Irfan Sheriff7f132d92010-06-09 15:39:36 -0700788 }
789
790 /**
791 * Ensure that a network route exists to deliver traffic to the specified
792 * host via the mobile data network.
793 * @param hostAddress the IP address of the host to which the route is desired,
794 * in network byte order.
Robert Greenwalta7dfbd32010-06-15 15:43:39 -0700795 * TODO - deprecate
Irfan Sheriff7f132d92010-06-09 15:39:36 -0700796 * @return {@code true} on success, {@code false} on failure
797 */
Robert Greenwalta7dfbd32010-06-15 15:43:39 -0700798 private boolean addHostRoute(NetworkStateTracker nt, InetAddress hostAddress) {
Irfan Sheriff7f132d92010-06-09 15:39:36 -0700799 if (nt.getNetworkInfo().getType() == ConnectivityManager.TYPE_WIFI) {
800 return false;
801 }
802
Robert Greenwalt1f1bcfe2010-08-30 10:56:47 -0700803 LinkProperties p = nt.getLinkProperties();
Robert Greenwalta7dfbd32010-06-15 15:43:39 -0700804 if (p == null) return false;
805 String interfaceName = p.getInterfaceName();
Irfan Sheriff7f132d92010-06-09 15:39:36 -0700806
807 if (DBG) {
Robert Greenwalta7dfbd32010-06-15 15:43:39 -0700808 Slog.d(TAG, "Requested host route to " + hostAddress + "(" + interfaceName + ")");
Irfan Sheriff7f132d92010-06-09 15:39:36 -0700809 }
Robert Greenwalta7dfbd32010-06-15 15:43:39 -0700810 if (interfaceName != null) {
Robert Greenwalt7fe44cb2010-08-27 09:24:29 -0700811 return NetworkUtils.addHostRoute(interfaceName, hostAddress, null);
Irfan Sheriff7f132d92010-06-09 15:39:36 -0700812 } else {
Robert Greenwalta7dfbd32010-06-15 15:43:39 -0700813 if (DBG) Slog.e(TAG, "addHostRoute failed due to null interface name");
Irfan Sheriff7f132d92010-06-09 15:39:36 -0700814 return false;
815 }
The Android Open Source Project28527d22009-03-03 19:31:44 -0800816 }
817
818 /**
819 * @see ConnectivityManager#getBackgroundDataSetting()
820 */
821 public boolean getBackgroundDataSetting() {
822 return Settings.Secure.getInt(mContext.getContentResolver(),
823 Settings.Secure.BACKGROUND_DATA, 1) == 1;
824 }
Robert Greenwalt0659da32009-07-16 17:21:39 -0700825
The Android Open Source Project28527d22009-03-03 19:31:44 -0800826 /**
827 * @see ConnectivityManager#setBackgroundDataSetting(boolean)
828 */
829 public void setBackgroundDataSetting(boolean allowBackgroundDataUsage) {
830 mContext.enforceCallingOrSelfPermission(
831 android.Manifest.permission.CHANGE_BACKGROUND_DATA_SETTING,
832 "ConnectivityService");
Robert Greenwalt0659da32009-07-16 17:21:39 -0700833
The Android Open Source Project28527d22009-03-03 19:31:44 -0800834 if (getBackgroundDataSetting() == allowBackgroundDataUsage) return;
835
836 Settings.Secure.putInt(mContext.getContentResolver(),
Robert Greenwalt0659da32009-07-16 17:21:39 -0700837 Settings.Secure.BACKGROUND_DATA,
838 allowBackgroundDataUsage ? 1 : 0);
839
The Android Open Source Project28527d22009-03-03 19:31:44 -0800840 Intent broadcast = new Intent(
841 ConnectivityManager.ACTION_BACKGROUND_DATA_SETTING_CHANGED);
842 mContext.sendBroadcast(broadcast);
Robert Greenwalt0659da32009-07-16 17:21:39 -0700843 }
844
Robert Greenwalt1b21f6c2010-02-23 18:58:05 -0800845 /**
846 * @see ConnectivityManager#getMobileDataEnabled()
847 */
848 public boolean getMobileDataEnabled() {
849 enforceAccessPermission();
850 boolean retVal = Settings.Secure.getInt(mContext.getContentResolver(),
851 Settings.Secure.MOBILE_DATA, 1) == 1;
Joe Onoratoc2386bb2010-02-26 18:56:32 -0800852 if (DBG) Slog.d(TAG, "getMobileDataEnabled returning " + retVal);
Robert Greenwalt1b21f6c2010-02-23 18:58:05 -0800853 return retVal;
854 }
855
856 /**
857 * @see ConnectivityManager#setMobileDataEnabled(boolean)
858 */
859 public synchronized void setMobileDataEnabled(boolean enabled) {
860 enforceChangePermission();
Joe Onoratoc2386bb2010-02-26 18:56:32 -0800861 if (DBG) Slog.d(TAG, "setMobileDataEnabled(" + enabled + ")");
Robert Greenwalt1b21f6c2010-02-23 18:58:05 -0800862
863 if (getMobileDataEnabled() == enabled) return;
864
865 Settings.Secure.putInt(mContext.getContentResolver(),
866 Settings.Secure.MOBILE_DATA, enabled ? 1 : 0);
867
868 if (enabled) {
869 if (mNetTrackers[ConnectivityManager.TYPE_MOBILE] != null) {
Joe Onoratoc2386bb2010-02-26 18:56:32 -0800870 if (DBG) Slog.d(TAG, "starting up " + mNetTrackers[ConnectivityManager.TYPE_MOBILE]);
Robert Greenwalt1b21f6c2010-02-23 18:58:05 -0800871 mNetTrackers[ConnectivityManager.TYPE_MOBILE].reconnect();
872 }
873 } else {
874 for (NetworkStateTracker nt : mNetTrackers) {
875 if (nt == null) continue;
876 int netType = nt.getNetworkInfo().getType();
877 if (mNetAttributes[netType].mRadio == ConnectivityManager.TYPE_MOBILE) {
Joe Onoratoc2386bb2010-02-26 18:56:32 -0800878 if (DBG) Slog.d(TAG, "tearing down " + nt);
Robert Greenwalt1b21f6c2010-02-23 18:58:05 -0800879 nt.teardown();
880 }
881 }
882 }
883 }
884
The Android Open Source Project28527d22009-03-03 19:31:44 -0800885 private int getNumConnectedNetworks() {
886 int numConnectedNets = 0;
887
888 for (NetworkStateTracker nt : mNetTrackers) {
Robert Greenwaltec05b3c2009-10-30 14:17:42 -0700889 if (nt != null && nt.getNetworkInfo().isConnected() &&
Robert Greenwalt0659da32009-07-16 17:21:39 -0700890 !nt.isTeardownRequested()) {
The Android Open Source Project28527d22009-03-03 19:31:44 -0800891 ++numConnectedNets;
892 }
893 }
894 return numConnectedNets;
895 }
896
897 private void enforceAccessPermission() {
Robert Greenwalt0659da32009-07-16 17:21:39 -0700898 mContext.enforceCallingOrSelfPermission(
899 android.Manifest.permission.ACCESS_NETWORK_STATE,
900 "ConnectivityService");
The Android Open Source Project28527d22009-03-03 19:31:44 -0800901 }
902
903 private void enforceChangePermission() {
Robert Greenwalt0659da32009-07-16 17:21:39 -0700904 mContext.enforceCallingOrSelfPermission(
905 android.Manifest.permission.CHANGE_NETWORK_STATE,
906 "ConnectivityService");
The Android Open Source Project28527d22009-03-03 19:31:44 -0800907 }
908
Robert Greenwalt0c4828c2010-01-26 11:40:34 -0800909 // TODO Make this a special check when it goes public
910 private void enforceTetherChangePermission() {
911 mContext.enforceCallingOrSelfPermission(
912 android.Manifest.permission.CHANGE_NETWORK_STATE,
913 "ConnectivityService");
914 }
915
Robert Greenwalt8e87f122010-02-11 18:18:40 -0800916 private void enforceTetherAccessPermission() {
917 mContext.enforceCallingOrSelfPermission(
918 android.Manifest.permission.ACCESS_NETWORK_STATE,
919 "ConnectivityService");
920 }
921
Robert Greenwalt93dc1042010-06-15 12:19:37 -0700922 private void enforceConnectivityInternalPermission() {
923 mContext.enforceCallingOrSelfPermission(
924 android.Manifest.permission.CONNECTIVITY_INTERNAL,
925 "ConnectivityService");
926 }
927
The Android Open Source Project28527d22009-03-03 19:31:44 -0800928 /**
Robert Greenwalt0659da32009-07-16 17:21:39 -0700929 * Handle a {@code DISCONNECTED} event. If this pertains to the non-active
930 * network, we ignore it. If it is for the active network, we send out a
931 * broadcast. But first, we check whether it might be possible to connect
932 * to a different network.
The Android Open Source Project28527d22009-03-03 19:31:44 -0800933 * @param info the {@code NetworkInfo} for the network
934 */
935 private void handleDisconnect(NetworkInfo info) {
936
Robert Greenwalt2034b912009-08-12 16:08:25 -0700937 int prevNetType = info.getType();
The Android Open Source Project28527d22009-03-03 19:31:44 -0800938
Robert Greenwalt2034b912009-08-12 16:08:25 -0700939 mNetTrackers[prevNetType].setTeardownRequested(false);
The Android Open Source Project28527d22009-03-03 19:31:44 -0800940 /*
941 * If the disconnected network is not the active one, then don't report
942 * this as a loss of connectivity. What probably happened is that we're
943 * getting the disconnect for a network that we explicitly disabled
944 * in accordance with network preference policies.
945 */
Robert Greenwalt2034b912009-08-12 16:08:25 -0700946 if (!mNetAttributes[prevNetType].isDefault()) {
947 List pids = mNetRequestersPids[prevNetType];
948 for (int i = 0; i<pids.size(); i++) {
949 Integer pid = (Integer)pids.get(i);
950 // will remove them because the net's no longer connected
951 // need to do this now as only now do we know the pids and
952 // can properly null things that are no longer referenced.
953 reassessPidDns(pid.intValue(), false);
The Android Open Source Project28527d22009-03-03 19:31:44 -0800954 }
The Android Open Source Project28527d22009-03-03 19:31:44 -0800955 }
956
The Android Open Source Project28527d22009-03-03 19:31:44 -0800957 Intent intent = new Intent(ConnectivityManager.CONNECTIVITY_ACTION);
958 intent.putExtra(ConnectivityManager.EXTRA_NETWORK_INFO, info);
959 if (info.isFailover()) {
960 intent.putExtra(ConnectivityManager.EXTRA_IS_FAILOVER, true);
961 info.setFailover(false);
962 }
963 if (info.getReason() != null) {
964 intent.putExtra(ConnectivityManager.EXTRA_REASON, info.getReason());
965 }
966 if (info.getExtraInfo() != null) {
Robert Greenwalt0659da32009-07-16 17:21:39 -0700967 intent.putExtra(ConnectivityManager.EXTRA_EXTRA_INFO,
968 info.getExtraInfo());
The Android Open Source Project28527d22009-03-03 19:31:44 -0800969 }
Robert Greenwalt2034b912009-08-12 16:08:25 -0700970
Robert Greenwalt3cc68d32010-01-25 17:54:29 -0800971 NetworkStateTracker newNet = null;
972 if (mNetAttributes[prevNetType].isDefault()) {
973 newNet = tryFailover(prevNetType);
974 if (newNet != null) {
975 NetworkInfo switchTo = newNet.getNetworkInfo();
976 intent.putExtra(ConnectivityManager.EXTRA_OTHER_NETWORK_INFO, switchTo);
977 } else {
978 intent.putExtra(ConnectivityManager.EXTRA_NO_CONNECTIVITY, true);
979 }
Robert Greenwaltf55ced92010-01-20 19:29:41 -0800980 }
981 // do this before we broadcast the change
Robert Greenwalt3afbead2010-07-23 15:46:26 -0700982 handleConnectivityChange(prevNetType);
Robert Greenwaltf55ced92010-01-20 19:29:41 -0800983
984 sendStickyBroadcast(intent);
985 /*
986 * If the failover network is already connected, then immediately send
987 * out a followup broadcast indicating successful failover
988 */
989 if (newNet != null && newNet.getNetworkInfo().isConnected()) {
990 sendConnectedBroadcast(newNet.getNetworkInfo());
991 }
992 }
993
Robert Greenwalt3cc68d32010-01-25 17:54:29 -0800994 // returns null if no failover available
Robert Greenwaltf55ced92010-01-20 19:29:41 -0800995 private NetworkStateTracker tryFailover(int prevNetType) {
Robert Greenwalt2034b912009-08-12 16:08:25 -0700996 /*
997 * If this is a default network, check if other defaults are available
998 * or active
999 */
1000 NetworkStateTracker newNet = null;
1001 if (mNetAttributes[prevNetType].isDefault()) {
Robert Greenwalt2034b912009-08-12 16:08:25 -07001002 if (mActiveDefaultNetwork == prevNetType) {
1003 mActiveDefaultNetwork = -1;
1004 }
1005
1006 int newType = -1;
1007 int newPriority = -1;
Robert Greenwalt72451bf2010-02-25 12:04:29 -08001008 boolean noMobileData = !getMobileDataEnabled();
Robert Greenwaltf55ced92010-01-20 19:29:41 -08001009 for (int checkType=0; checkType <= ConnectivityManager.MAX_NETWORK_TYPE; checkType++) {
Robert Greenwaltec05b3c2009-10-30 14:17:42 -07001010 if (checkType == prevNetType) continue;
1011 if (mNetAttributes[checkType] == null) continue;
Robert Greenwalt72451bf2010-02-25 12:04:29 -08001012 if (mNetAttributes[checkType].mRadio == ConnectivityManager.TYPE_MOBILE &&
1013 noMobileData) {
1014 if (DBG) {
Joe Onoratoc2386bb2010-02-26 18:56:32 -08001015 Slog.d(TAG, "not failing over to mobile type " + checkType +
Robert Greenwalt72451bf2010-02-25 12:04:29 -08001016 " because Mobile Data Disabled");
1017 }
1018 continue;
1019 }
Robert Greenwalt2034b912009-08-12 16:08:25 -07001020 if (mNetAttributes[checkType].isDefault()) {
1021 /* TODO - if we have multiple nets we could use
1022 * we may want to put more thought into which we choose
1023 */
1024 if (checkType == mNetworkPreference) {
1025 newType = checkType;
1026 break;
1027 }
Robert Greenwaltec05b3c2009-10-30 14:17:42 -07001028 if (mNetAttributes[checkType].mPriority > newPriority) {
Robert Greenwalt2034b912009-08-12 16:08:25 -07001029 newType = checkType;
Robert Greenwaltec05b3c2009-10-30 14:17:42 -07001030 newPriority = mNetAttributes[newType].mPriority;
Robert Greenwalt2034b912009-08-12 16:08:25 -07001031 }
The Android Open Source Project28527d22009-03-03 19:31:44 -08001032 }
1033 }
Robert Greenwalt2034b912009-08-12 16:08:25 -07001034
1035 if (newType != -1) {
1036 newNet = mNetTrackers[newType];
1037 /**
1038 * See if the other network is available to fail over to.
1039 * If is not available, we enable it anyway, so that it
1040 * will be able to connect when it does become available,
1041 * but we report a total loss of connectivity rather than
1042 * report that we are attempting to fail over.
1043 */
1044 if (newNet.isAvailable()) {
1045 NetworkInfo switchTo = newNet.getNetworkInfo();
1046 switchTo.setFailover(true);
Robert Greenwalta52c75a2009-08-19 20:19:33 -07001047 if (!switchTo.isConnectedOrConnecting() ||
1048 newNet.isTeardownRequested()) {
Robert Greenwalt2034b912009-08-12 16:08:25 -07001049 newNet.reconnect();
1050 }
1051 if (DBG) {
1052 if (switchTo.isConnected()) {
Joe Onoratoc2386bb2010-02-26 18:56:32 -08001053 Slog.v(TAG, "Switching to already connected " +
Robert Greenwalt2034b912009-08-12 16:08:25 -07001054 switchTo.getTypeName());
1055 } else {
Joe Onoratoc2386bb2010-02-26 18:56:32 -08001056 Slog.v(TAG, "Attempting to switch to " +
Robert Greenwalt2034b912009-08-12 16:08:25 -07001057 switchTo.getTypeName());
1058 }
1059 }
Robert Greenwalt2034b912009-08-12 16:08:25 -07001060 } else {
1061 newNet.reconnect();
Robert Greenwalt12984322010-03-09 14:55:08 -08001062 newNet = null; // not officially avail.. try anyway, but
1063 // report no failover
Robert Greenwalt2034b912009-08-12 16:08:25 -07001064 }
Robert Greenwalt2034b912009-08-12 16:08:25 -07001065 }
The Android Open Source Project28527d22009-03-03 19:31:44 -08001066 }
Robert Greenwalt2034b912009-08-12 16:08:25 -07001067
Robert Greenwaltf55ced92010-01-20 19:29:41 -08001068 return newNet;
The Android Open Source Project28527d22009-03-03 19:31:44 -08001069 }
1070
1071 private void sendConnectedBroadcast(NetworkInfo info) {
1072 Intent intent = new Intent(ConnectivityManager.CONNECTIVITY_ACTION);
1073 intent.putExtra(ConnectivityManager.EXTRA_NETWORK_INFO, info);
1074 if (info.isFailover()) {
1075 intent.putExtra(ConnectivityManager.EXTRA_IS_FAILOVER, true);
1076 info.setFailover(false);
1077 }
1078 if (info.getReason() != null) {
1079 intent.putExtra(ConnectivityManager.EXTRA_REASON, info.getReason());
1080 }
1081 if (info.getExtraInfo() != null) {
Robert Greenwalt0659da32009-07-16 17:21:39 -07001082 intent.putExtra(ConnectivityManager.EXTRA_EXTRA_INFO,
1083 info.getExtraInfo());
The Android Open Source Project28527d22009-03-03 19:31:44 -08001084 }
Robert Greenwalt986c7412010-09-08 15:24:47 -07001085 intent.putExtra(ConnectivityManager.EXTRA_INET_CONDITION, mDefaultInetConditionPublished);
Mike Lockwoodfde2b762009-08-14 14:18:49 -04001086 sendStickyBroadcast(intent);
The Android Open Source Project28527d22009-03-03 19:31:44 -08001087 }
1088
1089 /**
1090 * Called when an attempt to fail over to another network has failed.
1091 * @param info the {@link NetworkInfo} for the failed network
1092 */
1093 private void handleConnectionFailure(NetworkInfo info) {
1094 mNetTrackers[info.getType()].setTeardownRequested(false);
The Android Open Source Project28527d22009-03-03 19:31:44 -08001095
Robert Greenwalt2034b912009-08-12 16:08:25 -07001096 String reason = info.getReason();
1097 String extraInfo = info.getExtraInfo();
Robert Greenwalt0659da32009-07-16 17:21:39 -07001098
Robert Greenwalt2034b912009-08-12 16:08:25 -07001099 if (DBG) {
1100 String reasonText;
1101 if (reason == null) {
1102 reasonText = ".";
1103 } else {
1104 reasonText = " (" + reason + ").";
The Android Open Source Project28527d22009-03-03 19:31:44 -08001105 }
Joe Onoratoc2386bb2010-02-26 18:56:32 -08001106 Slog.v(TAG, "Attempt to connect to " + info.getTypeName() +
Robert Greenwalt2034b912009-08-12 16:08:25 -07001107 " failed" + reasonText);
The Android Open Source Project28527d22009-03-03 19:31:44 -08001108 }
Robert Greenwalt2034b912009-08-12 16:08:25 -07001109
1110 Intent intent = new Intent(ConnectivityManager.CONNECTIVITY_ACTION);
1111 intent.putExtra(ConnectivityManager.EXTRA_NETWORK_INFO, info);
1112 if (getActiveNetworkInfo() == null) {
1113 intent.putExtra(ConnectivityManager.EXTRA_NO_CONNECTIVITY, true);
1114 }
1115 if (reason != null) {
1116 intent.putExtra(ConnectivityManager.EXTRA_REASON, reason);
1117 }
1118 if (extraInfo != null) {
1119 intent.putExtra(ConnectivityManager.EXTRA_EXTRA_INFO, extraInfo);
1120 }
1121 if (info.isFailover()) {
1122 intent.putExtra(ConnectivityManager.EXTRA_IS_FAILOVER, true);
1123 info.setFailover(false);
1124 }
Robert Greenwaltf55ced92010-01-20 19:29:41 -08001125
Robert Greenwalt3cc68d32010-01-25 17:54:29 -08001126 NetworkStateTracker newNet = null;
1127 if (mNetAttributes[info.getType()].isDefault()) {
1128 newNet = tryFailover(info.getType());
1129 if (newNet != null) {
1130 NetworkInfo switchTo = newNet.getNetworkInfo();
1131 intent.putExtra(ConnectivityManager.EXTRA_OTHER_NETWORK_INFO, switchTo);
1132 } else {
1133 intent.putExtra(ConnectivityManager.EXTRA_NO_CONNECTIVITY, true);
1134 }
Robert Greenwaltf55ced92010-01-20 19:29:41 -08001135 }
Robert Greenwalt3cc68d32010-01-25 17:54:29 -08001136
Mike Lockwoodfde2b762009-08-14 14:18:49 -04001137 sendStickyBroadcast(intent);
Robert Greenwaltf55ced92010-01-20 19:29:41 -08001138 /*
1139 * If the failover network is already connected, then immediately send
1140 * out a followup broadcast indicating successful failover
1141 */
1142 if (newNet != null && newNet.getNetworkInfo().isConnected()) {
1143 sendConnectedBroadcast(newNet.getNetworkInfo());
1144 }
Mike Lockwoodfde2b762009-08-14 14:18:49 -04001145 }
1146
1147 private void sendStickyBroadcast(Intent intent) {
1148 synchronized(this) {
Dianne Hackborna417ff82009-12-08 19:45:14 -08001149 if (!mSystemReady) {
1150 mInitialBroadcast = new Intent(intent);
Mike Lockwoodfde2b762009-08-14 14:18:49 -04001151 }
Dianne Hackborna417ff82009-12-08 19:45:14 -08001152 intent.addFlags(Intent.FLAG_RECEIVER_REGISTERED_ONLY_BEFORE_BOOT);
1153 mContext.sendStickyBroadcast(intent);
Mike Lockwoodfde2b762009-08-14 14:18:49 -04001154 }
1155 }
1156
1157 void systemReady() {
1158 synchronized(this) {
1159 mSystemReady = true;
Dianne Hackborna417ff82009-12-08 19:45:14 -08001160 if (mInitialBroadcast != null) {
1161 mContext.sendStickyBroadcast(mInitialBroadcast);
1162 mInitialBroadcast = null;
Mike Lockwoodfde2b762009-08-14 14:18:49 -04001163 }
1164 }
The Android Open Source Project28527d22009-03-03 19:31:44 -08001165 }
1166
1167 private void handleConnect(NetworkInfo info) {
Robert Greenwalt2034b912009-08-12 16:08:25 -07001168 int type = info.getType();
The Android Open Source Project28527d22009-03-03 19:31:44 -08001169
1170 // snapshot isFailover, because sendConnectedBroadcast() resets it
1171 boolean isFailover = info.isFailover();
Robert Greenwalt2034b912009-08-12 16:08:25 -07001172 NetworkStateTracker thisNet = mNetTrackers[type];
The Android Open Source Project28527d22009-03-03 19:31:44 -08001173
Robert Greenwalt2034b912009-08-12 16:08:25 -07001174 // if this is a default net and other default is running
1175 // kill the one not preferred
1176 if (mNetAttributes[type].isDefault()) {
Robert Greenwalt2034b912009-08-12 16:08:25 -07001177 if (mActiveDefaultNetwork != -1 && mActiveDefaultNetwork != type) {
1178 if ((type != mNetworkPreference &&
1179 mNetAttributes[mActiveDefaultNetwork].mPriority >
1180 mNetAttributes[type].mPriority) ||
1181 mNetworkPreference == mActiveDefaultNetwork) {
1182 // don't accept this one
Joe Onoratoc2386bb2010-02-26 18:56:32 -08001183 if (DBG) Slog.v(TAG, "Not broadcasting CONNECT_ACTION " +
Robert Greenwalt2034b912009-08-12 16:08:25 -07001184 "to torn down network " + info.getTypeName());
1185 teardown(thisNet);
1186 return;
1187 } else {
1188 // tear down the other
1189 NetworkStateTracker otherNet =
1190 mNetTrackers[mActiveDefaultNetwork];
Joe Onoratoc2386bb2010-02-26 18:56:32 -08001191 if (DBG) Slog.v(TAG, "Policy requires " +
Robert Greenwalt2034b912009-08-12 16:08:25 -07001192 otherNet.getNetworkInfo().getTypeName() +
1193 " teardown");
1194 if (!teardown(otherNet)) {
Joe Onoratoc2386bb2010-02-26 18:56:32 -08001195 Slog.e(TAG, "Network declined teardown request");
Robert Greenwalt2034b912009-08-12 16:08:25 -07001196 return;
1197 }
Robert Greenwalt93dc1042010-06-15 12:19:37 -07001198 }
1199 }
1200 synchronized (ConnectivityService.this) {
1201 // have a new default network, release the transition wakelock in a second
1202 // if it's held. The second pause is to allow apps to reconnect over the
1203 // new network
1204 if (mNetTransitionWakeLock.isHeld()) {
1205 mHandler.sendMessageDelayed(mHandler.obtainMessage(
1206 NetworkStateTracker.EVENT_CLEAR_NET_TRANSITION_WAKELOCK,
1207 mNetTransitionWakeLockSerialNumber, 0),
1208 1000);
Robert Greenwalt2034b912009-08-12 16:08:25 -07001209 }
1210 }
1211 mActiveDefaultNetwork = type;
Robert Greenwalt986c7412010-09-08 15:24:47 -07001212 // this will cause us to come up initially as unconnected and switching
1213 // to connected after our normal pause unless somebody reports us as reall
1214 // disconnected
1215 mDefaultInetConditionPublished = 0;
1216 mDefaultConnectionSequence++;
1217 mInetConditionChangeInFlight = false;
1218 // Don't do this - if we never sign in stay, grey
1219 //reportNetworkCondition(mActiveDefaultNetwork, 100);
Robert Greenwalt2034b912009-08-12 16:08:25 -07001220 }
The Android Open Source Project28527d22009-03-03 19:31:44 -08001221 thisNet.setTeardownRequested(false);
Irfan Sheriff7f132d92010-06-09 15:39:36 -07001222 updateNetworkSettings(thisNet);
Robert Greenwalt3afbead2010-07-23 15:46:26 -07001223 handleConnectivityChange(type);
Robert Greenwalt2034b912009-08-12 16:08:25 -07001224 sendConnectedBroadcast(info);
The Android Open Source Project28527d22009-03-03 19:31:44 -08001225 }
1226
The Android Open Source Project28527d22009-03-03 19:31:44 -08001227 /**
Robert Greenwalt3afbead2010-07-23 15:46:26 -07001228 * After a change in the connectivity state of a network. We're mainly
1229 * concerned with making sure that the list of DNS servers is set up
1230 * according to which networks are connected, and ensuring that the
1231 * right routing table entries exist.
The Android Open Source Project28527d22009-03-03 19:31:44 -08001232 */
Robert Greenwalt3afbead2010-07-23 15:46:26 -07001233 private void handleConnectivityChange(int netType) {
The Android Open Source Project28527d22009-03-03 19:31:44 -08001234 /*
Robert Greenwalt2034b912009-08-12 16:08:25 -07001235 * If a non-default network is enabled, add the host routes that
Robert Greenwalt3afbead2010-07-23 15:46:26 -07001236 * will allow it's DNS servers to be accessed.
The Android Open Source Project28527d22009-03-03 19:31:44 -08001237 */
Robert Greenwalt3afbead2010-07-23 15:46:26 -07001238 handleDnsConfigurationChange(netType);
Robert Greenwalt2034b912009-08-12 16:08:25 -07001239
Robert Greenwalt3afbead2010-07-23 15:46:26 -07001240 if (mNetTrackers[netType].getNetworkInfo().isConnected()) {
1241 if (mNetAttributes[netType].isDefault()) {
1242 addDefaultRoute(mNetTrackers[netType]);
Robert Greenwalt2034b912009-08-12 16:08:25 -07001243 } else {
Robert Greenwalt3afbead2010-07-23 15:46:26 -07001244 addPrivateDnsRoutes(mNetTrackers[netType]);
1245 }
1246 } else {
1247 if (mNetAttributes[netType].isDefault()) {
1248 removeDefaultRoute(mNetTrackers[netType]);
1249 } else {
1250 removePrivateDnsRoutes(mNetTrackers[netType]);
Robert Greenwalt2034b912009-08-12 16:08:25 -07001251 }
The Android Open Source Project28527d22009-03-03 19:31:44 -08001252 }
1253 }
1254
Irfan Sheriff7f132d92010-06-09 15:39:36 -07001255 private void addPrivateDnsRoutes(NetworkStateTracker nt) {
Irfan Sheriff7f132d92010-06-09 15:39:36 -07001256 boolean privateDnsRouteSet = nt.isPrivateDnsRouteSet();
Robert Greenwalt1f1bcfe2010-08-30 10:56:47 -07001257 LinkProperties p = nt.getLinkProperties();
Robert Greenwalta7dfbd32010-06-15 15:43:39 -07001258 if (p == null) return;
1259 String interfaceName = p.getInterfaceName();
Irfan Sheriff7f132d92010-06-09 15:39:36 -07001260
1261 if (DBG) {
1262 Slog.d(TAG, "addPrivateDnsRoutes for " + nt +
1263 "(" + interfaceName + ") - mPrivateDnsRouteSet = " + privateDnsRouteSet);
1264 }
Irfan Sheriff7f132d92010-06-09 15:39:36 -07001265 if (interfaceName != null && !privateDnsRouteSet) {
Robert Greenwalta7dfbd32010-06-15 15:43:39 -07001266 Collection<InetAddress> dnsList = p.getDnses();
1267 for (InetAddress dns : dnsList) {
1268 if (DBG) Slog.d(TAG, " adding " + dns);
Robert Greenwalt7fe44cb2010-08-27 09:24:29 -07001269 NetworkUtils.addHostRoute(interfaceName, dns, null);
Irfan Sheriff7f132d92010-06-09 15:39:36 -07001270 }
1271 nt.privateDnsRouteSet(true);
1272 }
1273 }
1274
1275 private void removePrivateDnsRoutes(NetworkStateTracker nt) {
1276 // TODO - we should do this explicitly but the NetUtils api doesnt
1277 // support this yet - must remove all. No worse than before
Robert Greenwalt1f1bcfe2010-08-30 10:56:47 -07001278 LinkProperties p = nt.getLinkProperties();
Robert Greenwalta7dfbd32010-06-15 15:43:39 -07001279 if (p == null) return;
1280 String interfaceName = p.getInterfaceName();
Irfan Sheriff7f132d92010-06-09 15:39:36 -07001281 boolean privateDnsRouteSet = nt.isPrivateDnsRouteSet();
1282 if (interfaceName != null && privateDnsRouteSet) {
1283 if (DBG) {
1284 Slog.d(TAG, "removePrivateDnsRoutes for " + nt.getNetworkInfo().getTypeName() +
1285 " (" + interfaceName + ")");
1286 }
1287 NetworkUtils.removeHostRoutes(interfaceName);
1288 nt.privateDnsRouteSet(false);
1289 }
1290 }
1291
Irfan Sheriff7f132d92010-06-09 15:39:36 -07001292
1293 private void addDefaultRoute(NetworkStateTracker nt) {
Robert Greenwalt1f1bcfe2010-08-30 10:56:47 -07001294 LinkProperties p = nt.getLinkProperties();
Robert Greenwalta7dfbd32010-06-15 15:43:39 -07001295 if (p == null) return;
1296 String interfaceName = p.getInterfaceName();
1297 InetAddress defaultGatewayAddr = p.getGateway();
Irfan Sheriff7f132d92010-06-09 15:39:36 -07001298
Robert Greenwalt3afbead2010-07-23 15:46:26 -07001299 if ((interfaceName != null) && (defaultGatewayAddr != null )) {
Robert Greenwalt7fe44cb2010-08-27 09:24:29 -07001300 if (!NetworkUtils.addDefaultRoute(interfaceName, defaultGatewayAddr) && DBG) {
Robert Greenwalta7dfbd32010-06-15 15:43:39 -07001301 NetworkInfo networkInfo = nt.getNetworkInfo();
Irfan Sheriff7f132d92010-06-09 15:39:36 -07001302 Slog.d(TAG, "addDefaultRoute for " + networkInfo.getTypeName() +
1303 " (" + interfaceName + "), GatewayAddr=" + defaultGatewayAddr);
1304 }
Irfan Sheriff7f132d92010-06-09 15:39:36 -07001305 }
1306 }
1307
1308
1309 public void removeDefaultRoute(NetworkStateTracker nt) {
Robert Greenwalt1f1bcfe2010-08-30 10:56:47 -07001310 LinkProperties p = nt.getLinkProperties();
Robert Greenwalta7dfbd32010-06-15 15:43:39 -07001311 if (p == null) return;
1312 String interfaceName = p.getInterfaceName();
Irfan Sheriff7f132d92010-06-09 15:39:36 -07001313
Robert Greenwalt3afbead2010-07-23 15:46:26 -07001314 if (interfaceName != null) {
1315 if ((NetworkUtils.removeDefaultRoute(interfaceName) >= 0) && DBG) {
Robert Greenwalta7dfbd32010-06-15 15:43:39 -07001316 NetworkInfo networkInfo = nt.getNetworkInfo();
Irfan Sheriff7f132d92010-06-09 15:39:36 -07001317 Slog.d(TAG, "removeDefaultRoute for " + networkInfo.getTypeName() + " (" +
1318 interfaceName + ")");
1319 }
Irfan Sheriff7f132d92010-06-09 15:39:36 -07001320 }
1321 }
1322
1323 /**
1324 * Reads the network specific TCP buffer sizes from SystemProperties
1325 * net.tcp.buffersize.[default|wifi|umts|edge|gprs] and set them for system
1326 * wide use
1327 */
1328 public void updateNetworkSettings(NetworkStateTracker nt) {
1329 String key = nt.getTcpBufferSizesPropName();
1330 String bufferSizes = SystemProperties.get(key);
1331
1332 if (bufferSizes.length() == 0) {
1333 Slog.e(TAG, key + " not found in system properties. Using defaults");
1334
1335 // Setting to default values so we won't be stuck to previous values
1336 key = "net.tcp.buffersize.default";
1337 bufferSizes = SystemProperties.get(key);
1338 }
1339
1340 // Set values in kernel
1341 if (bufferSizes.length() != 0) {
1342 if (DBG) {
1343 Slog.v(TAG, "Setting TCP values: [" + bufferSizes
1344 + "] which comes from [" + key + "]");
1345 }
1346 setBufferSize(bufferSizes);
1347 }
1348 }
1349
1350 /**
1351 * Writes TCP buffer sizes to /sys/kernel/ipv4/tcp_[r/w]mem_[min/def/max]
1352 * which maps to /proc/sys/net/ipv4/tcp_rmem and tcpwmem
1353 *
1354 * @param bufferSizes in the format of "readMin, readInitial, readMax,
1355 * writeMin, writeInitial, writeMax"
1356 */
1357 private void setBufferSize(String bufferSizes) {
1358 try {
1359 String[] values = bufferSizes.split(",");
1360
1361 if (values.length == 6) {
1362 final String prefix = "/sys/kernel/ipv4/tcp_";
1363 stringToFile(prefix + "rmem_min", values[0]);
1364 stringToFile(prefix + "rmem_def", values[1]);
1365 stringToFile(prefix + "rmem_max", values[2]);
1366 stringToFile(prefix + "wmem_min", values[3]);
1367 stringToFile(prefix + "wmem_def", values[4]);
1368 stringToFile(prefix + "wmem_max", values[5]);
1369 } else {
1370 Slog.e(TAG, "Invalid buffersize string: " + bufferSizes);
1371 }
1372 } catch (IOException e) {
1373 Slog.e(TAG, "Can't set tcp buffer sizes:" + e);
1374 }
1375 }
1376
1377 /**
1378 * Writes string to file. Basically same as "echo -n $string > $filename"
1379 *
1380 * @param filename
1381 * @param string
1382 * @throws IOException
1383 */
1384 private void stringToFile(String filename, String string) throws IOException {
1385 FileWriter out = new FileWriter(filename);
1386 try {
1387 out.write(string);
1388 } finally {
1389 out.close();
1390 }
1391 }
1392
1393
Robert Greenwalt2034b912009-08-12 16:08:25 -07001394 /**
1395 * Adjust the per-process dns entries (net.dns<x>.<pid>) based
1396 * on the highest priority active net which this process requested.
1397 * If there aren't any, clear it out
1398 */
1399 private void reassessPidDns(int myPid, boolean doBump)
1400 {
Joe Onoratoc2386bb2010-02-26 18:56:32 -08001401 if (DBG) Slog.d(TAG, "reassessPidDns for pid " + myPid);
Robert Greenwalt2034b912009-08-12 16:08:25 -07001402 for(int i : mPriorityList) {
1403 if (mNetAttributes[i].isDefault()) {
1404 continue;
1405 }
1406 NetworkStateTracker nt = mNetTrackers[i];
Robert Greenwalt0659da32009-07-16 17:21:39 -07001407 if (nt.getNetworkInfo().isConnected() &&
1408 !nt.isTeardownRequested()) {
Robert Greenwalt1f1bcfe2010-08-30 10:56:47 -07001409 LinkProperties p = nt.getLinkProperties();
Robert Greenwalta7dfbd32010-06-15 15:43:39 -07001410 if (p == null) continue;
Robert Greenwalt2034b912009-08-12 16:08:25 -07001411 List pids = mNetRequestersPids[i];
1412 for (int j=0; j<pids.size(); j++) {
1413 Integer pid = (Integer)pids.get(j);
1414 if (pid.intValue() == myPid) {
Robert Greenwalta7dfbd32010-06-15 15:43:39 -07001415 Collection<InetAddress> dnses = p.getDnses();
1416 writePidDns(dnses, myPid);
Robert Greenwalt2034b912009-08-12 16:08:25 -07001417 if (doBump) {
1418 bumpDns();
1419 }
1420 return;
1421 }
1422 }
1423 }
1424 }
1425 // nothing found - delete
1426 for (int i = 1; ; i++) {
1427 String prop = "net.dns" + i + "." + myPid;
1428 if (SystemProperties.get(prop).length() == 0) {
1429 if (doBump) {
1430 bumpDns();
1431 }
1432 return;
1433 }
1434 SystemProperties.set(prop, "");
1435 }
1436 }
1437
Robert Greenwalta7dfbd32010-06-15 15:43:39 -07001438 private void writePidDns(Collection <InetAddress> dnses, int pid) {
Robert Greenwalt2034b912009-08-12 16:08:25 -07001439 int j = 1;
Robert Greenwalta7dfbd32010-06-15 15:43:39 -07001440 for (InetAddress dns : dnses) {
1441 SystemProperties.set("net.dns" + j++ + "." + pid, dns.getHostAddress());
Robert Greenwalt2034b912009-08-12 16:08:25 -07001442 }
1443 }
1444
1445 private void bumpDns() {
1446 /*
1447 * Bump the property that tells the name resolver library to reread
1448 * the DNS server list from the properties.
1449 */
1450 String propVal = SystemProperties.get("net.dnschange");
1451 int n = 0;
1452 if (propVal.length() != 0) {
1453 try {
1454 n = Integer.parseInt(propVal);
1455 } catch (NumberFormatException e) {}
1456 }
1457 SystemProperties.set("net.dnschange", "" + (n+1));
1458 }
1459
Robert Greenwalt3afbead2010-07-23 15:46:26 -07001460 private void handleDnsConfigurationChange(int netType) {
Robert Greenwalt2034b912009-08-12 16:08:25 -07001461 // add default net's dns entries
Robert Greenwalt3afbead2010-07-23 15:46:26 -07001462 NetworkStateTracker nt = mNetTrackers[netType];
1463 if (nt != null && nt.getNetworkInfo().isConnected() && !nt.isTeardownRequested()) {
Robert Greenwalt1f1bcfe2010-08-30 10:56:47 -07001464 LinkProperties p = nt.getLinkProperties();
Robert Greenwalt3afbead2010-07-23 15:46:26 -07001465 if (p == null) return;
1466 Collection<InetAddress> dnses = p.getDnses();
1467 if (mNetAttributes[netType].isDefault()) {
1468 int j = 1;
Robert Greenwalt94daa182010-09-01 11:34:05 -07001469 if (dnses.size() == 0 && mDefaultDns != null) {
Robert Greenwalt3afbead2010-07-23 15:46:26 -07001470 if (DBG) {
Robert Greenwalt94daa182010-09-01 11:34:05 -07001471 Slog.d(TAG, "no dns provided - using " + mDefaultDns.getHostAddress());
Robert Greenwalt2034b912009-08-12 16:08:25 -07001472 }
Robert Greenwalt94daa182010-09-01 11:34:05 -07001473 SystemProperties.set("net.dns1", mDefaultDns.getHostAddress());
1474 j++;
1475 } else {
1476 for (InetAddress dns : dnses) {
1477 if (DBG) {
1478 Slog.d(TAG, "adding dns " + dns + " for " +
1479 nt.getNetworkInfo().getTypeName());
1480 }
1481 SystemProperties.set("net.dns" + j++, dns.getHostAddress());
1482 }
Robert Greenwalt3afbead2010-07-23 15:46:26 -07001483 }
1484 for (int k=j ; k<mNumDnsEntries; k++) {
1485 if (DBG) Slog.d(TAG, "erasing net.dns" + k);
1486 SystemProperties.set("net.dns" + k, "");
1487 }
1488 mNumDnsEntries = j;
1489 } else {
1490 // set per-pid dns for attached secondary nets
1491 List pids = mNetRequestersPids[netType];
1492 for (int y=0; y< pids.size(); y++) {
1493 Integer pid = (Integer)pids.get(y);
1494 writePidDns(dnses, pid.intValue());
The Android Open Source Project28527d22009-03-03 19:31:44 -08001495 }
1496 }
Robert Greenwalt3afbead2010-07-23 15:46:26 -07001497 bumpDns();
The Android Open Source Project28527d22009-03-03 19:31:44 -08001498 }
Robert Greenwalt2034b912009-08-12 16:08:25 -07001499 }
1500
1501 private int getRestoreDefaultNetworkDelay() {
1502 String restoreDefaultNetworkDelayStr = SystemProperties.get(
1503 NETWORK_RESTORE_DELAY_PROP_NAME);
1504 if(restoreDefaultNetworkDelayStr != null &&
1505 restoreDefaultNetworkDelayStr.length() != 0) {
1506 try {
1507 return Integer.valueOf(restoreDefaultNetworkDelayStr);
1508 } catch (NumberFormatException e) {
1509 }
The Android Open Source Project28527d22009-03-03 19:31:44 -08001510 }
Robert Greenwalt2034b912009-08-12 16:08:25 -07001511 return RESTORE_DEFAULT_NETWORK_DELAY;
The Android Open Source Project28527d22009-03-03 19:31:44 -08001512 }
1513
1514 @Override
1515 protected void dump(FileDescriptor fd, PrintWriter pw, String[] args) {
Robert Greenwalt0659da32009-07-16 17:21:39 -07001516 if (mContext.checkCallingOrSelfPermission(
1517 android.Manifest.permission.DUMP)
The Android Open Source Project28527d22009-03-03 19:31:44 -08001518 != PackageManager.PERMISSION_GRANTED) {
Robert Greenwalt0659da32009-07-16 17:21:39 -07001519 pw.println("Permission Denial: can't dump ConnectivityService " +
1520 "from from pid=" + Binder.getCallingPid() + ", uid=" +
1521 Binder.getCallingUid());
The Android Open Source Project28527d22009-03-03 19:31:44 -08001522 return;
1523 }
The Android Open Source Project28527d22009-03-03 19:31:44 -08001524 pw.println();
1525 for (NetworkStateTracker nst : mNetTrackers) {
Robert Greenwalt3eeb6032009-12-21 18:24:07 -08001526 if (nst != null) {
1527 if (nst.getNetworkInfo().isConnected()) {
1528 pw.println("Active network: " + nst.getNetworkInfo().
1529 getTypeName());
1530 }
1531 pw.println(nst.getNetworkInfo());
1532 pw.println(nst);
1533 pw.println();
Robert Greenwalt2034b912009-08-12 16:08:25 -07001534 }
The Android Open Source Project28527d22009-03-03 19:31:44 -08001535 }
Robert Greenwalt3eeb6032009-12-21 18:24:07 -08001536
1537 pw.println("Network Requester Pids:");
1538 for (int net : mPriorityList) {
1539 String pidString = net + ": ";
1540 for (Object pid : mNetRequestersPids[net]) {
1541 pidString = pidString + pid.toString() + ", ";
1542 }
1543 pw.println(pidString);
1544 }
1545 pw.println();
1546
1547 pw.println("FeatureUsers:");
1548 for (Object requester : mFeatureUsers) {
1549 pw.println(requester.toString());
1550 }
1551 pw.println();
Robert Greenwalt8e87f122010-02-11 18:18:40 -08001552
Robert Greenwalt93dc1042010-06-15 12:19:37 -07001553 synchronized (this) {
1554 pw.println("NetworkTranstionWakeLock is currently " +
1555 (mNetTransitionWakeLock.isHeld() ? "" : "not ") + "held.");
1556 pw.println("It was last requested for "+mNetTransitionWakeLockCausedBy);
1557 }
1558 pw.println();
1559
Robert Greenwalt8e87f122010-02-11 18:18:40 -08001560 mTethering.dump(fd, pw, args);
The Android Open Source Project28527d22009-03-03 19:31:44 -08001561 }
1562
Robert Greenwalt2034b912009-08-12 16:08:25 -07001563 // must be stateless - things change under us.
The Android Open Source Project28527d22009-03-03 19:31:44 -08001564 private class MyHandler extends Handler {
Wink Saville775aad62010-09-02 19:23:52 -07001565 public MyHandler(Looper looper) {
1566 super(looper);
1567 }
1568
The Android Open Source Project28527d22009-03-03 19:31:44 -08001569 @Override
1570 public void handleMessage(Message msg) {
1571 NetworkInfo info;
1572 switch (msg.what) {
1573 case NetworkStateTracker.EVENT_STATE_CHANGED:
1574 info = (NetworkInfo) msg.obj;
Robert Greenwalt12c44552009-12-07 11:33:18 -08001575 int type = info.getType();
1576 NetworkInfo.State state = info.getState();
Robert Greenwalt24e2d2b2010-01-25 16:14:00 -08001577 // only do this optimization for wifi. It going into scan mode for location
1578 // services generates alot of noise. Meanwhile the mms apn won't send out
1579 // subsequent notifications when on default cellular because it never
1580 // disconnects.. so only do this to wifi notifications. Fixed better when the
1581 // APN notifications are standardized.
1582 if (mNetAttributes[type].mLastState == state &&
1583 mNetAttributes[type].mRadio == ConnectivityManager.TYPE_WIFI) {
Robert Greenwalt12c44552009-12-07 11:33:18 -08001584 if (DBG) {
Robert Greenwalt24e2d2b2010-01-25 16:14:00 -08001585 // TODO - remove this after we validate the dropping doesn't break
1586 // anything
Joe Onoratoc2386bb2010-02-26 18:56:32 -08001587 Slog.d(TAG, "Dropping ConnectivityChange for " +
Robert Greenwalt2adbc7f2010-01-13 09:36:31 -08001588 info.getTypeName() + ": " +
Robert Greenwalt12c44552009-12-07 11:33:18 -08001589 state + "/" + info.getDetailedState());
1590 }
1591 return;
1592 }
1593 mNetAttributes[type].mLastState = state;
1594
Joe Onoratoc2386bb2010-02-26 18:56:32 -08001595 if (DBG) Slog.d(TAG, "ConnectivityChange for " +
Robert Greenwalt0659da32009-07-16 17:21:39 -07001596 info.getTypeName() + ": " +
Robert Greenwalt12c44552009-12-07 11:33:18 -08001597 state + "/" + info.getDetailedState());
The Android Open Source Project28527d22009-03-03 19:31:44 -08001598
1599 // Connectivity state changed:
1600 // [31-13] Reserved for future use
Robert Greenwalt0659da32009-07-16 17:21:39 -07001601 // [12-9] Network subtype (for mobile network, as defined
1602 // by TelephonyManager)
1603 // [8-3] Detailed state ordinal (as defined by
1604 // NetworkInfo.DetailedState)
The Android Open Source Project28527d22009-03-03 19:31:44 -08001605 // [2-0] Network type (as defined by ConnectivityManager)
1606 int eventLogParam = (info.getType() & 0x7) |
1607 ((info.getDetailedState().ordinal() & 0x3f) << 3) |
1608 (info.getSubtype() << 9);
Doug Zongker2fc96232009-12-04 10:31:43 -08001609 EventLog.writeEvent(EventLogTags.CONNECTIVITY_STATE_CHANGED,
Robert Greenwalt0659da32009-07-16 17:21:39 -07001610 eventLogParam);
1611
1612 if (info.getDetailedState() ==
1613 NetworkInfo.DetailedState.FAILED) {
The Android Open Source Project28527d22009-03-03 19:31:44 -08001614 handleConnectionFailure(info);
Robert Greenwalt12c44552009-12-07 11:33:18 -08001615 } else if (state == NetworkInfo.State.DISCONNECTED) {
The Android Open Source Project28527d22009-03-03 19:31:44 -08001616 handleDisconnect(info);
Robert Greenwalt12c44552009-12-07 11:33:18 -08001617 } else if (state == NetworkInfo.State.SUSPENDED) {
The Android Open Source Project28527d22009-03-03 19:31:44 -08001618 // TODO: need to think this over.
Robert Greenwalt0659da32009-07-16 17:21:39 -07001619 // the logic here is, handle SUSPENDED the same as
1620 // DISCONNECTED. The only difference being we are
1621 // broadcasting an intent with NetworkInfo that's
1622 // suspended. This allows the applications an
1623 // opportunity to handle DISCONNECTED and SUSPENDED
1624 // differently, or not.
The Android Open Source Project28527d22009-03-03 19:31:44 -08001625 handleDisconnect(info);
Robert Greenwalt12c44552009-12-07 11:33:18 -08001626 } else if (state == NetworkInfo.State.CONNECTED) {
The Android Open Source Project28527d22009-03-03 19:31:44 -08001627 handleConnect(info);
1628 }
The Android Open Source Project28527d22009-03-03 19:31:44 -08001629 break;
The Android Open Source Project28527d22009-03-03 19:31:44 -08001630 case NetworkStateTracker.EVENT_CONFIGURATION_CHANGED:
Robert Greenwalt3afbead2010-07-23 15:46:26 -07001631 // TODO - make this handle ip/proxy/gateway/dns changes
1632 info = (NetworkInfo) msg.obj;
1633 type = info.getType();
1634 handleDnsConfigurationChange(type);
The Android Open Source Project28527d22009-03-03 19:31:44 -08001635 break;
Robert Greenwalt2034b912009-08-12 16:08:25 -07001636 case NetworkStateTracker.EVENT_RESTORE_DEFAULT_NETWORK:
Robert Greenwaltaffc3a12009-09-27 17:27:04 -07001637 FeatureUser u = (FeatureUser)msg.obj;
1638 u.expire();
Robert Greenwalt2034b912009-08-12 16:08:25 -07001639 break;
Robert Greenwalt93dc1042010-06-15 12:19:37 -07001640 case NetworkStateTracker.EVENT_CLEAR_NET_TRANSITION_WAKELOCK:
1641 String causedBy = null;
1642 synchronized (ConnectivityService.this) {
1643 if (msg.arg1 == mNetTransitionWakeLockSerialNumber &&
1644 mNetTransitionWakeLock.isHeld()) {
1645 mNetTransitionWakeLock.release();
1646 causedBy = mNetTransitionWakeLockCausedBy;
1647 }
1648 }
1649 if (causedBy != null) {
1650 Slog.d(TAG, "NetTransition Wakelock for " +
1651 causedBy + " released by timeout");
1652 }
Robert Greenwaltcf1a56c2010-09-09 14:05:10 -07001653 break;
Robert Greenwalt986c7412010-09-08 15:24:47 -07001654 case NetworkStateTracker.EVENT_INET_CONDITION_CHANGE:
1655 if (DBG) {
1656 Slog.d(TAG, "Inet connectivity change, net=" +
1657 msg.arg1 + ", condition=" + msg.arg2 +
1658 ",mActiveDefaultNetwork=" + mActiveDefaultNetwork);
1659 }
1660 if (mActiveDefaultNetwork == -1) {
1661 if (DBG) Slog.d(TAG, "no active default network - aborting");
1662 break;
1663 }
1664 if (mActiveDefaultNetwork != msg.arg1) {
1665 if (DBG) Slog.d(TAG, "given net not default - aborting");
1666 break;
1667 }
1668 mDefaultInetCondition = msg.arg2;
1669 int delay;
1670 if (mInetConditionChangeInFlight == false) {
1671 if (DBG) Slog.d(TAG, "starting a change hold");
1672 // setup a new hold to debounce this
1673 if (mDefaultInetCondition > 50) {
1674 delay = Settings.Secure.getInt(mContext.getContentResolver(),
1675 Settings.Secure.INET_CONDITION_DEBOUNCE_UP_DELAY, 500);
1676 } else {
1677 delay = Settings.Secure.getInt(mContext.getContentResolver(),
1678 Settings.Secure.INET_CONDITION_DEBOUNCE_DOWN_DELAY, 3000);
1679 }
1680 mInetConditionChangeInFlight = true;
1681 sendMessageDelayed(obtainMessage(
1682 NetworkStateTracker.EVENT_INET_CONDITION_HOLD_END,
1683 mActiveDefaultNetwork, mDefaultConnectionSequence), delay);
1684 } else {
1685 // we've set the new condition, when this hold ends that will get
1686 // picked up
1687 if (DBG) Slog.d(TAG, "currently in hold - not setting new end evt");
1688 }
1689 break;
1690 case NetworkStateTracker.EVENT_INET_CONDITION_HOLD_END:
1691 if (DBG) {
1692 Slog.d(TAG, "Inet hold end, net=" + msg.arg1 +
1693 ", condition =" + mDefaultInetCondition +
1694 ", published condition =" + mDefaultInetConditionPublished);
1695 }
1696 mInetConditionChangeInFlight = false;
1697
1698 if (mActiveDefaultNetwork == -1) {
1699 if (DBG) Slog.d(TAG, "no active default network - aborting");
1700 break;
1701 }
1702 if (mDefaultConnectionSequence != msg.arg2) {
1703 if (DBG) Slog.d(TAG, "event hold for obsolete network - aborting");
1704 break;
1705 }
1706 if (mDefaultInetConditionPublished == mDefaultInetCondition) {
1707 if (DBG) Slog.d(TAG, "no change in condition - aborting");
1708 break;
1709 }
1710 NetworkInfo networkInfo = mNetTrackers[mActiveDefaultNetwork].getNetworkInfo();
1711 if (networkInfo.isConnected() == false) {
1712 if (DBG) Slog.d(TAG, "default network not connected - aborting");
1713 break;
1714 }
1715 mDefaultInetConditionPublished = mDefaultInetCondition;
1716 sendConnectedBroadcast(networkInfo);
Robert Greenwalt93dc1042010-06-15 12:19:37 -07001717 break;
The Android Open Source Project28527d22009-03-03 19:31:44 -08001718 }
1719 }
1720 }
Robert Greenwalt0c4828c2010-01-26 11:40:34 -08001721
1722 // javadoc from interface
Robert Greenwalt4283ded2010-03-02 17:25:02 -08001723 public int tether(String iface) {
Robert Greenwalt0c4828c2010-01-26 11:40:34 -08001724 enforceTetherChangePermission();
Robert Greenwalt4283ded2010-03-02 17:25:02 -08001725
1726 if (isTetheringSupported()) {
1727 return mTethering.tether(iface);
1728 } else {
1729 return ConnectivityManager.TETHER_ERROR_UNSUPPORTED;
1730 }
Robert Greenwalt0c4828c2010-01-26 11:40:34 -08001731 }
1732
1733 // javadoc from interface
Robert Greenwalt4283ded2010-03-02 17:25:02 -08001734 public int untether(String iface) {
Robert Greenwalt0c4828c2010-01-26 11:40:34 -08001735 enforceTetherChangePermission();
Robert Greenwalt4283ded2010-03-02 17:25:02 -08001736
1737 if (isTetheringSupported()) {
1738 return mTethering.untether(iface);
1739 } else {
1740 return ConnectivityManager.TETHER_ERROR_UNSUPPORTED;
1741 }
1742 }
1743
1744 // javadoc from interface
1745 public int getLastTetherError(String iface) {
1746 enforceTetherAccessPermission();
1747
1748 if (isTetheringSupported()) {
1749 return mTethering.getLastTetherError(iface);
1750 } else {
1751 return ConnectivityManager.TETHER_ERROR_UNSUPPORTED;
1752 }
Robert Greenwalt8e87f122010-02-11 18:18:40 -08001753 }
1754
1755 // TODO - proper iface API for selection by property, inspection, etc
1756 public String[] getTetherableUsbRegexs() {
1757 enforceTetherAccessPermission();
1758 if (isTetheringSupported()) {
1759 return mTethering.getTetherableUsbRegexs();
1760 } else {
1761 return new String[0];
1762 }
1763 }
1764
1765 public String[] getTetherableWifiRegexs() {
1766 enforceTetherAccessPermission();
1767 if (isTetheringSupported()) {
1768 return mTethering.getTetherableWifiRegexs();
1769 } else {
1770 return new String[0];
1771 }
Robert Greenwalt0c4828c2010-01-26 11:40:34 -08001772 }
1773
Danica Chang96567052010-08-11 14:54:43 -07001774 public String[] getTetherableBluetoothRegexs() {
1775 enforceTetherAccessPermission();
1776 if (isTetheringSupported()) {
1777 return mTethering.getTetherableBluetoothRegexs();
1778 } else {
1779 return new String[0];
1780 }
1781 }
1782
Robert Greenwalt0c4828c2010-01-26 11:40:34 -08001783 // TODO - move iface listing, queries, etc to new module
1784 // javadoc from interface
1785 public String[] getTetherableIfaces() {
Robert Greenwalt8e87f122010-02-11 18:18:40 -08001786 enforceTetherAccessPermission();
Robert Greenwalt0c4828c2010-01-26 11:40:34 -08001787 return mTethering.getTetherableIfaces();
1788 }
1789
1790 public String[] getTetheredIfaces() {
Robert Greenwalt8e87f122010-02-11 18:18:40 -08001791 enforceTetherAccessPermission();
Robert Greenwalt0c4828c2010-01-26 11:40:34 -08001792 return mTethering.getTetheredIfaces();
1793 }
Robert Greenwalt8e87f122010-02-11 18:18:40 -08001794
Robert Greenwalt4283ded2010-03-02 17:25:02 -08001795 public String[] getTetheringErroredIfaces() {
1796 enforceTetherAccessPermission();
1797 return mTethering.getErroredIfaces();
1798 }
1799
Robert Greenwalt8e87f122010-02-11 18:18:40 -08001800 // if ro.tether.denied = true we default to no tethering
1801 // gservices could set the secure setting to 1 though to enable it on a build where it
1802 // had previously been turned off.
1803 public boolean isTetheringSupported() {
1804 enforceTetherAccessPermission();
1805 int defaultVal = (SystemProperties.get("ro.tether.denied").equals("true") ? 0 : 1);
Robert Greenwaltf1b66e12010-02-25 12:29:30 -08001806 boolean tetherEnabledInSettings = (Settings.Secure.getInt(mContext.getContentResolver(),
1807 Settings.Secure.TETHER_SUPPORTED, defaultVal) != 0);
1808 return tetherEnabledInSettings && mTetheringConfigValid;
Robert Greenwalt8e87f122010-02-11 18:18:40 -08001809 }
Robert Greenwalt93dc1042010-06-15 12:19:37 -07001810
1811 // An API NetworkStateTrackers can call when they lose their network.
1812 // This will automatically be cleared after X seconds or a network becomes CONNECTED,
1813 // whichever happens first. The timer is started by the first caller and not
1814 // restarted by subsequent callers.
1815 public void requestNetworkTransitionWakelock(String forWhom) {
1816 enforceConnectivityInternalPermission();
1817 synchronized (this) {
1818 if (mNetTransitionWakeLock.isHeld()) return;
1819 mNetTransitionWakeLockSerialNumber++;
1820 mNetTransitionWakeLock.acquire();
1821 mNetTransitionWakeLockCausedBy = forWhom;
1822 }
1823 mHandler.sendMessageDelayed(mHandler.obtainMessage(
1824 NetworkStateTracker.EVENT_CLEAR_NET_TRANSITION_WAKELOCK,
1825 mNetTransitionWakeLockSerialNumber, 0),
1826 mNetTransitionWakeLockTimeout);
1827 return;
1828 }
Robert Greenwalt24118e82010-09-09 13:15:32 -07001829
Robert Greenwalt986c7412010-09-08 15:24:47 -07001830 // 100 percent is full good, 0 is full bad.
1831 public void reportInetCondition(int networkType, int percentage) {
1832 if (DBG) Slog.d(TAG, "reportNetworkCondition(" + networkType + ", " + percentage + ")");
1833 mContext.enforceCallingOrSelfPermission(
1834 android.Manifest.permission.STATUS_BAR,
1835 "ConnectivityService");
1836
1837 mHandler.sendMessage(mHandler.obtainMessage(
1838 NetworkStateTracker.EVENT_INET_CONDITION_CHANGE, networkType, percentage));
1839 }
The Android Open Source Project28527d22009-03-03 19:31:44 -08001840}