blob: a3694fe0c9f83e46545c0ebb5fcbb4b890b79e1f [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;
The Android Open Source Project28527d22009-03-03 19:31:44 -080021import android.content.Context;
TK MUN141623d2011-02-23 18:55:55 +090022import android.content.ContentResolver;
23import android.content.ContextWrapper;
The Android Open Source Project28527d22009-03-03 19:31:44 -080024import android.content.Intent;
25import android.content.pm.PackageManager;
TK MUN141623d2011-02-23 18:55:55 +090026import android.content.res.Resources;
27import android.content.res.Resources.NotFoundException;
The Android Open Source Project28527d22009-03-03 19:31:44 -080028import android.net.ConnectivityManager;
29import android.net.IConnectivityManager;
30import android.net.MobileDataStateTracker;
31import android.net.NetworkInfo;
32import android.net.NetworkStateTracker;
Banavathu, Srinivas Naik7601f572010-08-10 20:13:53 +053033import android.net.NetworkUtils;
The Android Open Source Project28527d22009-03-03 19:31:44 -080034import android.net.wifi.WifiStateTracker;
TK MUN141623d2011-02-23 18:55:55 +090035import android.net.wimax.WimaxManagerConstants;
The Android Open Source Project28527d22009-03-03 19:31:44 -080036import android.os.Binder;
37import android.os.Handler;
Robert Greenwalt2034b912009-08-12 16:08:25 -070038import android.os.IBinder;
The Android Open Source Project28527d22009-03-03 19:31:44 -080039import android.os.Looper;
40import android.os.Message;
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;
Robert Greenwalt2034b912009-08-12 16:08:25 -070048import com.android.internal.telephony.Phone;
Robert Greenwalt0c4828c2010-01-26 11:40:34 -080049import com.android.server.connectivity.Tethering;
TK MUN141623d2011-02-23 18:55:55 +090050import dalvik.system.DexClassLoader;
The Android Open Source Project28527d22009-03-03 19:31:44 -080051import java.io.FileDescriptor;
52import java.io.PrintWriter;
TK MUN141623d2011-02-23 18:55:55 +090053import java.lang.reflect.Constructor;
54import java.lang.reflect.Method;
55import java.lang.reflect.Modifier;
56import java.lang.reflect.InvocationTargetException;
Robert Greenwalt2034b912009-08-12 16:08:25 -070057import java.util.ArrayList;
Robert Greenwalt0e80be12010-09-20 14:35:25 -070058import java.util.GregorianCalendar;
Robert Greenwalt2034b912009-08-12 16:08:25 -070059import java.util.List;
Banavathu, Srinivas Naik7601f572010-08-10 20:13:53 +053060import java.net.InetAddress;
61import java.net.UnknownHostException;
The Android Open Source Project28527d22009-03-03 19:31:44 -080062
TK MUN141623d2011-02-23 18:55:55 +090063
64
The Android Open Source Project28527d22009-03-03 19:31:44 -080065/**
66 * @hide
67 */
68public class ConnectivityService extends IConnectivityManager.Stub {
69
Dianne Hackborn8919f5a2010-09-30 18:27:22 -070070 private static final boolean DBG = false;
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
80 private Tethering mTethering;
Robert Greenwaltf1b66e12010-02-25 12:29:30 -080081 private boolean mTetheringConfigValid = false;
Robert Greenwalt0c4828c2010-01-26 11:40:34 -080082
The Android Open Source Project28527d22009-03-03 19:31:44 -080083 /**
84 * Sometimes we want to refer to the individual network state
85 * trackers separately, and sometimes we just want to treat them
86 * abstractly.
87 */
88 private NetworkStateTracker mNetTrackers[];
Robert Greenwalt2034b912009-08-12 16:08:25 -070089
90 /**
91 * A per Net list of the PID's that requested access to the net
92 * used both as a refcount and for per-PID DNS selection
93 */
94 private List mNetRequestersPids[];
95
Robert Greenwalt2034b912009-08-12 16:08:25 -070096 // priority order of the nettrackers
97 // (excluding dynamically set mNetworkPreference)
98 // TODO - move mNetworkTypePreference into this
99 private int[] mPriorityList;
100
The Android Open Source Project28527d22009-03-03 19:31:44 -0800101 private Context mContext;
102 private int mNetworkPreference;
Robert Greenwalt2034b912009-08-12 16:08:25 -0700103 private int mActiveDefaultNetwork = -1;
Robert Greenwalt986c7412010-09-08 15:24:47 -0700104 // 0 is full bad, 100 is full good
105 private int mDefaultInetCondition = 0;
106 private int mDefaultInetConditionPublished = 0;
107 private boolean mInetConditionChangeInFlight = false;
108 private int mDefaultConnectionSequence = 0;
The Android Open Source Project28527d22009-03-03 19:31:44 -0800109
110 private int mNumDnsEntries;
The Android Open Source Project28527d22009-03-03 19:31:44 -0800111
112 private boolean mTestMode;
113 private static ConnectivityService sServiceInstance;
Robert Greenwalt6a2db8a2010-09-23 10:05:56 -0700114 private static final int ENABLED = 1;
115 private static final int DISABLED = 0;
116
117 // Share the event space with NetworkStateTracker (which can't see this
118 // internal class but sends us events). If you change these, change
119 // NetworkStateTracker.java too.
120 private static final int MIN_NETWORK_STATE_TRACKER_EVENT = 1;
121 private static final int MAX_NETWORK_STATE_TRACKER_EVENT = 100;
122
123 /**
124 * used internally as a delayed event to make us switch back to the
125 * default network
126 */
127 private static final int EVENT_RESTORE_DEFAULT_NETWORK =
128 MAX_NETWORK_STATE_TRACKER_EVENT + 1;
129
130 /**
131 * used internally to change our mobile data enabled flag
132 */
133 private static final int EVENT_CHANGE_MOBILE_DATA_ENABLED =
134 MAX_NETWORK_STATE_TRACKER_EVENT + 2;
135
136 /**
137 * used internally to change our network preference setting
138 * arg1 = networkType to prefer
139 */
140 private static final int EVENT_SET_NETWORK_PREFERENCE =
141 MAX_NETWORK_STATE_TRACKER_EVENT + 3;
142
143 /**
144 * used internally to synchronize inet condition reports
145 * arg1 = networkType
146 * arg2 = condition (0 bad, 100 good)
147 */
148 private static final int EVENT_INET_CONDITION_CHANGE =
149 MAX_NETWORK_STATE_TRACKER_EVENT + 4;
150
151 /**
152 * used internally to mark the end of inet condition hold periods
153 * arg1 = networkType
154 */
155 private static final int EVENT_INET_CONDITION_HOLD_END =
156 MAX_NETWORK_STATE_TRACKER_EVENT + 5;
157
158 /**
159 * used internally to set the background data preference
160 * arg1 = TRUE for enabled, FALSE for disabled
161 */
162 private static final int EVENT_SET_BACKGROUND_DATA =
163 MAX_NETWORK_STATE_TRACKER_EVENT + 6;
164
165 /**
166 * used internally to set enable/disable cellular data
167 * arg1 = ENBALED or DISABLED
168 */
169 private static final int EVENT_SET_MOBILE_DATA =
170 MAX_NETWORK_STATE_TRACKER_EVENT + 7;
171
Robert Greenwalt2034b912009-08-12 16:08:25 -0700172 private Handler mHandler;
173
174 // list of DeathRecipients used to make sure features are turned off when
175 // a process dies
176 private List mFeatureUsers;
177
Mike Lockwoodfde2b762009-08-14 14:18:49 -0400178 private boolean mSystemReady;
Dianne Hackborna417ff82009-12-08 19:45:14 -0800179 private Intent mInitialBroadcast;
Mike Lockwoodfde2b762009-08-14 14:18:49 -0400180
Robert Greenwalt0e80be12010-09-20 14:35:25 -0700181 // used in DBG mode to track inet condition reports
182 private static final int INET_CONDITION_LOG_MAX_SIZE = 15;
183 private ArrayList mInetLog;
184
Robert Greenwalt12c44552009-12-07 11:33:18 -0800185 private static class NetworkAttributes {
Robert Greenwalt2034b912009-08-12 16:08:25 -0700186 /**
187 * Class for holding settings read from resources.
188 */
189 public String mName;
190 public int mType;
191 public int mRadio;
192 public int mPriority;
Robert Greenwalt12c44552009-12-07 11:33:18 -0800193 public NetworkInfo.State mLastState;
Robert Greenwalt2034b912009-08-12 16:08:25 -0700194 public NetworkAttributes(String init) {
195 String fragments[] = init.split(",");
196 mName = fragments[0].toLowerCase();
Robert Greenwaltec05b3c2009-10-30 14:17:42 -0700197 mType = Integer.parseInt(fragments[1]);
198 mRadio = Integer.parseInt(fragments[2]);
199 mPriority = Integer.parseInt(fragments[3]);
Robert Greenwalt12c44552009-12-07 11:33:18 -0800200 mLastState = NetworkInfo.State.UNKNOWN;
Robert Greenwalt2034b912009-08-12 16:08:25 -0700201 }
202 public boolean isDefault() {
203 return (mType == mRadio);
204 }
205 }
206 NetworkAttributes[] mNetAttributes;
Robert Greenwaltec05b3c2009-10-30 14:17:42 -0700207 int mNetworksDefined;
Robert Greenwalt2034b912009-08-12 16:08:25 -0700208
Robert Greenwalt12c44552009-12-07 11:33:18 -0800209 private static class RadioAttributes {
Robert Greenwalt2034b912009-08-12 16:08:25 -0700210 public int mSimultaneity;
211 public int mType;
212 public RadioAttributes(String init) {
213 String fragments[] = init.split(",");
Robert Greenwaltec05b3c2009-10-30 14:17:42 -0700214 mType = Integer.parseInt(fragments[0]);
215 mSimultaneity = Integer.parseInt(fragments[1]);
Robert Greenwalt2034b912009-08-12 16:08:25 -0700216 }
217 }
218 RadioAttributes[] mRadioAttributes;
219
The Android Open Source Project28527d22009-03-03 19:31:44 -0800220 private static class ConnectivityThread extends Thread {
221 private Context mContext;
Robert Greenwalt0659da32009-07-16 17:21:39 -0700222
The Android Open Source Project28527d22009-03-03 19:31:44 -0800223 private ConnectivityThread(Context context) {
224 super("ConnectivityThread");
225 mContext = context;
226 }
227
228 @Override
229 public void run() {
230 Looper.prepare();
231 synchronized (this) {
232 sServiceInstance = new ConnectivityService(mContext);
233 notifyAll();
234 }
235 Looper.loop();
236 }
Robert Greenwalt0659da32009-07-16 17:21:39 -0700237
The Android Open Source Project28527d22009-03-03 19:31:44 -0800238 public static ConnectivityService getServiceInstance(Context context) {
239 ConnectivityThread thread = new ConnectivityThread(context);
240 thread.start();
Robert Greenwalt0659da32009-07-16 17:21:39 -0700241
The Android Open Source Project28527d22009-03-03 19:31:44 -0800242 synchronized (thread) {
243 while (sServiceInstance == null) {
244 try {
245 // Wait until sServiceInstance has been initialized.
246 thread.wait();
247 } catch (InterruptedException ignore) {
Joe Onoratoc2386bb2010-02-26 18:56:32 -0800248 Slog.e(TAG,
Robert Greenwalt0659da32009-07-16 17:21:39 -0700249 "Unexpected InterruptedException while waiting"+
250 " for ConnectivityService thread");
The Android Open Source Project28527d22009-03-03 19:31:44 -0800251 }
252 }
253 }
Robert Greenwalt0659da32009-07-16 17:21:39 -0700254
The Android Open Source Project28527d22009-03-03 19:31:44 -0800255 return sServiceInstance;
256 }
257 }
Robert Greenwalt0659da32009-07-16 17:21:39 -0700258
The Android Open Source Project28527d22009-03-03 19:31:44 -0800259 public static ConnectivityService getInstance(Context context) {
260 return ConnectivityThread.getServiceInstance(context);
261 }
Robert Greenwalt0659da32009-07-16 17:21:39 -0700262
The Android Open Source Project28527d22009-03-03 19:31:44 -0800263 private ConnectivityService(Context context) {
Joe Onoratoc2386bb2010-02-26 18:56:32 -0800264 if (DBG) Slog.v(TAG, "ConnectivityService starting up");
Robert Greenwaltd48f8ee2010-01-14 17:47:58 -0800265
266 // setup our unique device name
267 String id = Settings.Secure.getString(context.getContentResolver(),
268 Settings.Secure.ANDROID_ID);
269 if (id != null && id.length() > 0) {
270 String name = new String("android_").concat(id);
271 SystemProperties.set("net.hostname", name);
272 }
273
The Android Open Source Project28527d22009-03-03 19:31:44 -0800274 mContext = context;
Robert Greenwalt2034b912009-08-12 16:08:25 -0700275 mNetTrackers = new NetworkStateTracker[
276 ConnectivityManager.MAX_NETWORK_TYPE+1];
277 mHandler = new MyHandler();
Robert Greenwalt0659da32009-07-16 17:21:39 -0700278
The Android Open Source Project28527d22009-03-03 19:31:44 -0800279 mNetworkPreference = getPersistedNetworkPreference();
Robert Greenwalt0659da32009-07-16 17:21:39 -0700280
Robert Greenwaltec05b3c2009-10-30 14:17:42 -0700281 mRadioAttributes = new RadioAttributes[ConnectivityManager.MAX_RADIO_TYPE+1];
282 mNetAttributes = new NetworkAttributes[ConnectivityManager.MAX_NETWORK_TYPE+1];
283
Robert Greenwalt2034b912009-08-12 16:08:25 -0700284 // Load device network attributes from resources
Robert Greenwalt2034b912009-08-12 16:08:25 -0700285 String[] raStrings = context.getResources().getStringArray(
286 com.android.internal.R.array.radioAttributes);
Robert Greenwaltec05b3c2009-10-30 14:17:42 -0700287 for (String raString : raStrings) {
288 RadioAttributes r = new RadioAttributes(raString);
289 if (r.mType > ConnectivityManager.MAX_RADIO_TYPE) {
Joe Onoratoc2386bb2010-02-26 18:56:32 -0800290 Slog.e(TAG, "Error in radioAttributes - ignoring attempt to define type " + r.mType);
Robert Greenwaltec05b3c2009-10-30 14:17:42 -0700291 continue;
292 }
293 if (mRadioAttributes[r.mType] != null) {
Joe Onoratoc2386bb2010-02-26 18:56:32 -0800294 Slog.e(TAG, "Error in radioAttributes - ignoring attempt to redefine type " +
Robert Greenwaltec05b3c2009-10-30 14:17:42 -0700295 r.mType);
296 continue;
297 }
Robert Greenwalt2034b912009-08-12 16:08:25 -0700298 mRadioAttributes[r.mType] = r;
299 }
300
Robert Greenwaltec05b3c2009-10-30 14:17:42 -0700301 String[] naStrings = context.getResources().getStringArray(
302 com.android.internal.R.array.networkAttributes);
303 for (String naString : naStrings) {
304 try {
305 NetworkAttributes n = new NetworkAttributes(naString);
306 if (n.mType > ConnectivityManager.MAX_NETWORK_TYPE) {
Joe Onoratoc2386bb2010-02-26 18:56:32 -0800307 Slog.e(TAG, "Error in networkAttributes - ignoring attempt to define type " +
Robert Greenwaltec05b3c2009-10-30 14:17:42 -0700308 n.mType);
309 continue;
Robert Greenwalt2034b912009-08-12 16:08:25 -0700310 }
Robert Greenwaltec05b3c2009-10-30 14:17:42 -0700311 if (mNetAttributes[n.mType] != null) {
Joe Onoratoc2386bb2010-02-26 18:56:32 -0800312 Slog.e(TAG, "Error in networkAttributes - ignoring attempt to redefine type " +
Robert Greenwaltec05b3c2009-10-30 14:17:42 -0700313 n.mType);
314 continue;
315 }
316 if (mRadioAttributes[n.mRadio] == null) {
Joe Onoratoc2386bb2010-02-26 18:56:32 -0800317 Slog.e(TAG, "Error in networkAttributes - ignoring attempt to use undefined " +
Robert Greenwaltec05b3c2009-10-30 14:17:42 -0700318 "radio " + n.mRadio + " in network type " + n.mType);
319 continue;
320 }
321 mNetAttributes[n.mType] = n;
322 mNetworksDefined++;
323 } catch(Exception e) {
324 // ignore it - leave the entry null
Robert Greenwalt2034b912009-08-12 16:08:25 -0700325 }
326 }
327
Robert Greenwaltec05b3c2009-10-30 14:17:42 -0700328 // high priority first
329 mPriorityList = new int[mNetworksDefined];
330 {
331 int insertionPoint = mNetworksDefined-1;
332 int currentLowest = 0;
333 int nextLowest = 0;
334 while (insertionPoint > -1) {
335 for (NetworkAttributes na : mNetAttributes) {
336 if (na == null) continue;
337 if (na.mPriority < currentLowest) continue;
338 if (na.mPriority > currentLowest) {
339 if (na.mPriority < nextLowest || nextLowest == 0) {
340 nextLowest = na.mPriority;
341 }
342 continue;
343 }
344 mPriorityList[insertionPoint--] = na.mType;
345 }
346 currentLowest = nextLowest;
347 nextLowest = 0;
348 }
349 }
350
351 mNetRequestersPids = new ArrayList[ConnectivityManager.MAX_NETWORK_TYPE+1];
352 for (int i : mPriorityList) {
Robert Greenwalt2034b912009-08-12 16:08:25 -0700353 mNetRequestersPids[i] = new ArrayList();
354 }
355
356 mFeatureUsers = new ArrayList();
357
Robert Greenwaltec05b3c2009-10-30 14:17:42 -0700358 mNumDnsEntries = 0;
359
360 mTestMode = SystemProperties.get("cm.test.mode").equals("true")
361 && SystemProperties.get("ro.build.type").equals("eng");
The Android Open Source Project28527d22009-03-03 19:31:44 -0800362 /*
363 * Create the network state trackers for Wi-Fi and mobile
364 * data. Maybe this could be done with a factory class,
365 * but it's not clear that it's worth it, given that
366 * the number of different network types is not going
367 * to change very often.
368 */
Robert Greenwalt1b21f6c2010-02-23 18:58:05 -0800369 boolean noMobileData = !getMobileDataEnabled();
Robert Greenwaltec05b3c2009-10-30 14:17:42 -0700370 for (int netType : mPriorityList) {
371 switch (mNetAttributes[netType].mRadio) {
372 case ConnectivityManager.TYPE_WIFI:
Joe Onoratoc2386bb2010-02-26 18:56:32 -0800373 if (DBG) Slog.v(TAG, "Starting Wifi Service.");
Robert Greenwaltec05b3c2009-10-30 14:17:42 -0700374 WifiStateTracker wst = new WifiStateTracker(context, mHandler);
375 WifiService wifiService = new WifiService(context, wst);
376 ServiceManager.addService(Context.WIFI_SERVICE, wifiService);
Irfan Sheriff324ec572010-03-11 16:37:45 -0800377 wifiService.startWifi();
Robert Greenwaltec05b3c2009-10-30 14:17:42 -0700378 mNetTrackers[ConnectivityManager.TYPE_WIFI] = wst;
379 wst.startMonitoring();
The Android Open Source Project28527d22009-03-03 19:31:44 -0800380
Robert Greenwaltec05b3c2009-10-30 14:17:42 -0700381 break;
382 case ConnectivityManager.TYPE_MOBILE:
383 mNetTrackers[netType] = new MobileDataStateTracker(context, mHandler,
384 netType, mNetAttributes[netType].mName);
385 mNetTrackers[netType].startMonitoring();
Robert Greenwalt1b21f6c2010-02-23 18:58:05 -0800386 if (noMobileData) {
Joe Onoratoc2386bb2010-02-26 18:56:32 -0800387 if (DBG) Slog.d(TAG, "tearing down Mobile networks due to setting");
Robert Greenwalt1b21f6c2010-02-23 18:58:05 -0800388 mNetTrackers[netType].teardown();
389 }
Robert Greenwaltec05b3c2009-10-30 14:17:42 -0700390 break;
TK MUN141623d2011-02-23 18:55:55 +0900391 case ConnectivityManager.TYPE_WIMAX:
392 NetworkStateTracker nst = makeWimaxStateTracker();
393 if (nst != null) {
394 nst.startMonitoring();
395 }
396 mNetTrackers[netType] = nst;
397 break;
Robert Greenwaltec05b3c2009-10-30 14:17:42 -0700398 default:
Joe Onoratoc2386bb2010-02-26 18:56:32 -0800399 Slog.e(TAG, "Trying to create a DataStateTracker for an unknown radio type " +
Robert Greenwaltec05b3c2009-10-30 14:17:42 -0700400 mNetAttributes[netType].mRadio);
401 continue;
402 }
403 }
Robert Greenwalt0c4828c2010-01-26 11:40:34 -0800404
Robert Greenwaltc0b6c602010-03-11 15:03:08 -0800405 mTethering = new Tethering(mContext, mHandler.getLooper());
Robert Greenwaltf1b66e12010-02-25 12:29:30 -0800406 mTetheringConfigValid = (((mNetTrackers[ConnectivityManager.TYPE_MOBILE_DUN] != null) ||
407 !mTethering.isDunRequired()) &&
408 (mTethering.getTetherableUsbRegexs().length != 0 ||
409 mTethering.getTetherableWifiRegexs().length != 0) &&
410 mTethering.getUpstreamIfaceRegexs().length != 0);
411
Robert Greenwalt0e80be12010-09-20 14:35:25 -0700412 if (DBG) {
413 mInetLog = new ArrayList();
414 }
The Android Open Source Project28527d22009-03-03 19:31:44 -0800415 }
416
TK MUN141623d2011-02-23 18:55:55 +0900417 private NetworkStateTracker makeWimaxStateTracker() {
418 //Initialize Wimax
419 DexClassLoader wimaxClassLoader;
420 Class wimaxStateTrackerClass = null;
421 Class wimaxServiceClass = null;
422 Class wimaxManagerClass;
423 String wimaxJarLocation;
424 String wimaxLibLocation;
425 String wimaxManagerClassName;
426 String wimaxServiceClassName;
427 String wimaxStateTrackerClassName;
428
429 NetworkStateTracker wimaxStateTracker = null;
430
431 boolean isWimaxEnabled = mContext.getResources().getBoolean(
432 com.android.internal.R.bool.config_wimaxEnabled);
433
434 if (isWimaxEnabled) {
435 try {
436 wimaxJarLocation = mContext.getResources().getString(
437 com.android.internal.R.string.config_wimaxServiceJarLocation);
438 wimaxLibLocation = mContext.getResources().getString(
439 com.android.internal.R.string.config_wimaxNativeLibLocation);
440 wimaxManagerClassName = mContext.getResources().getString(
441 com.android.internal.R.string.config_wimaxManagerClassname);
442 wimaxServiceClassName = mContext.getResources().getString(
443 com.android.internal.R.string.config_wimaxServiceClassname);
444 wimaxStateTrackerClassName = mContext.getResources().getString(
445 com.android.internal.R.string.config_wimaxStateTrackerClassname);
446
447 wimaxClassLoader = new DexClassLoader(wimaxJarLocation,
448 new ContextWrapper(mContext).getCacheDir().getAbsolutePath(),
449 wimaxLibLocation,ClassLoader.getSystemClassLoader());
450
451 try {
452 wimaxManagerClass = wimaxClassLoader.loadClass(wimaxManagerClassName);
453 wimaxStateTrackerClass = wimaxClassLoader.loadClass(wimaxStateTrackerClassName);
454 wimaxServiceClass = wimaxClassLoader.loadClass(wimaxServiceClassName);
455 } catch (ClassNotFoundException ex) {
456 ex.printStackTrace();
457 return null;
458 }
459 } catch(Resources.NotFoundException ex) {
460 Slog.e(TAG, "Wimax Resources does not exist!!! ");
461 return null;
462 }
463
464 try {
465 Slog.v(TAG, "Starting Wimax Service... ");
466
467 Constructor wmxStTrkrConst = wimaxStateTrackerClass.getConstructor
468 (new Class[] {Context.class,Handler.class});
469 wimaxStateTracker = (NetworkStateTracker)wmxStTrkrConst.newInstance(mContext,mHandler);
470
471 Constructor wmxSrvConst = wimaxServiceClass.getDeclaredConstructor
472 (new Class[] {Context.class,wimaxStateTrackerClass});
473 wmxSrvConst.setAccessible(true);
474 IBinder svcInvoker = (IBinder) wmxSrvConst.newInstance(mContext,wimaxStateTracker);
475 wmxSrvConst.setAccessible(false);
476
477 ServiceManager.addService(WimaxManagerConstants.WIMAX_SERVICE, svcInvoker);
478
479 } catch(ClassCastException ex) {
480 ex.printStackTrace();
481 return null;
482 } catch (NoSuchMethodException ex) {
483 ex.printStackTrace();
484 return null;
485 } catch (InstantiationException ex) {
486 ex.printStackTrace();
487 return null;
488 } catch(IllegalAccessException ex) {
489 ex.printStackTrace();
490 return null;
491 } catch(InvocationTargetException ex) {
492 ex.printStackTrace();
493 return null;
494 } catch(Exception ex) {
495 ex.printStackTrace();
496 return null;
497 }
498 } else {
499 Slog.e(TAG, "Wimax is not enabled or not added to the network attributes!!! ");
500 return null;
501 }
502
503 return wimaxStateTracker;
504 }
Robert Greenwaltec05b3c2009-10-30 14:17:42 -0700505
The Android Open Source Project28527d22009-03-03 19:31:44 -0800506 /**
Robert Greenwalt0659da32009-07-16 17:21:39 -0700507 * Sets the preferred network.
The Android Open Source Project28527d22009-03-03 19:31:44 -0800508 * @param preference the new preference
509 */
Robert Greenwalt6a2db8a2010-09-23 10:05:56 -0700510 public void setNetworkPreference(int preference) {
The Android Open Source Project28527d22009-03-03 19:31:44 -0800511 enforceChangePermission();
Robert Greenwalt6a2db8a2010-09-23 10:05:56 -0700512
513 mHandler.sendMessage(mHandler.obtainMessage(EVENT_SET_NETWORK_PREFERENCE, preference, 0));
The Android Open Source Project28527d22009-03-03 19:31:44 -0800514 }
515
516 public int getNetworkPreference() {
517 enforceAccessPermission();
Robert Greenwalt6a2db8a2010-09-23 10:05:56 -0700518 int preference;
519 synchronized(this) {
520 preference = mNetworkPreference;
521 }
522 return preference;
The Android Open Source Project28527d22009-03-03 19:31:44 -0800523 }
524
Robert Greenwalt6a2db8a2010-09-23 10:05:56 -0700525 private void handleSetNetworkPreference(int preference) {
526 if (ConnectivityManager.isNetworkTypeValid(preference) &&
527 mNetAttributes[preference] != null &&
528 mNetAttributes[preference].isDefault()) {
529 if (mNetworkPreference != preference) {
530 final ContentResolver cr = mContext.getContentResolver();
531 Settings.Secure.putInt(cr, Settings.Secure.NETWORK_PREFERENCE, preference);
532 synchronized(this) {
533 mNetworkPreference = preference;
534 }
535 enforcePreference();
536 }
537 }
The Android Open Source Project28527d22009-03-03 19:31:44 -0800538 }
Robert Greenwalt0659da32009-07-16 17:21:39 -0700539
The Android Open Source Project28527d22009-03-03 19:31:44 -0800540 private int getPersistedNetworkPreference() {
541 final ContentResolver cr = mContext.getContentResolver();
542
543 final int networkPrefSetting = Settings.Secure
544 .getInt(cr, Settings.Secure.NETWORK_PREFERENCE, -1);
545 if (networkPrefSetting != -1) {
546 return networkPrefSetting;
547 }
548
549 return ConnectivityManager.DEFAULT_NETWORK_PREFERENCE;
550 }
Robert Greenwalt0659da32009-07-16 17:21:39 -0700551
The Android Open Source Project28527d22009-03-03 19:31:44 -0800552 /**
Robert Greenwalt0659da32009-07-16 17:21:39 -0700553 * Make the state of network connectivity conform to the preference settings
The Android Open Source Project28527d22009-03-03 19:31:44 -0800554 * In this method, we only tear down a non-preferred network. Establishing
555 * a connection to the preferred network is taken care of when we handle
556 * the disconnect event from the non-preferred network
557 * (see {@link #handleDisconnect(NetworkInfo)}).
558 */
559 private void enforcePreference() {
Robert Greenwalt2034b912009-08-12 16:08:25 -0700560 if (mNetTrackers[mNetworkPreference].getNetworkInfo().isConnected())
The Android Open Source Project28527d22009-03-03 19:31:44 -0800561 return;
562
Robert Greenwalt2034b912009-08-12 16:08:25 -0700563 if (!mNetTrackers[mNetworkPreference].isAvailable())
564 return;
The Android Open Source Project28527d22009-03-03 19:31:44 -0800565
Robert Greenwalt2034b912009-08-12 16:08:25 -0700566 for (int t=0; t <= ConnectivityManager.MAX_RADIO_TYPE; t++) {
Robert Greenwaltec05b3c2009-10-30 14:17:42 -0700567 if (t != mNetworkPreference && mNetTrackers[t] != null &&
Robert Greenwalt2034b912009-08-12 16:08:25 -0700568 mNetTrackers[t].getNetworkInfo().isConnected()) {
Robert Greenwaltf3f045b2009-08-20 15:25:14 -0700569 if (DBG) {
Joe Onoratoc2386bb2010-02-26 18:56:32 -0800570 Slog.d(TAG, "tearing down " +
Robert Greenwaltf3f045b2009-08-20 15:25:14 -0700571 mNetTrackers[t].getNetworkInfo() +
572 " in enforcePreference");
573 }
Robert Greenwalt2034b912009-08-12 16:08:25 -0700574 teardown(mNetTrackers[t]);
The Android Open Source Project28527d22009-03-03 19:31:44 -0800575 }
576 }
577 }
578
579 private boolean teardown(NetworkStateTracker netTracker) {
580 if (netTracker.teardown()) {
581 netTracker.setTeardownRequested(true);
582 return true;
583 } else {
584 return false;
585 }
586 }
587
588 /**
589 * Return NetworkInfo for the active (i.e., connected) network interface.
590 * It is assumed that at most one network is active at a time. If more
591 * than one is active, it is indeterminate which will be returned.
Robert Greenwalt0659da32009-07-16 17:21:39 -0700592 * @return the info for the active network, or {@code null} if none is
593 * active
The Android Open Source Project28527d22009-03-03 19:31:44 -0800594 */
595 public NetworkInfo getActiveNetworkInfo() {
596 enforceAccessPermission();
Robert Greenwalt2034b912009-08-12 16:08:25 -0700597 for (int type=0; type <= ConnectivityManager.MAX_NETWORK_TYPE; type++) {
Robert Greenwaltec05b3c2009-10-30 14:17:42 -0700598 if (mNetAttributes[type] == null || !mNetAttributes[type].isDefault()) {
Robert Greenwalt2034b912009-08-12 16:08:25 -0700599 continue;
600 }
601 NetworkStateTracker t = mNetTrackers[type];
The Android Open Source Project28527d22009-03-03 19:31:44 -0800602 NetworkInfo info = t.getNetworkInfo();
603 if (info.isConnected()) {
Joe Onoratoc2386bb2010-02-26 18:56:32 -0800604 if (DBG && type != mActiveDefaultNetwork) Slog.e(TAG,
Robert Greenwalt2034b912009-08-12 16:08:25 -0700605 "connected default network is not " +
606 "mActiveDefaultNetwork!");
The Android Open Source Project28527d22009-03-03 19:31:44 -0800607 return info;
608 }
609 }
610 return null;
611 }
612
613 public NetworkInfo getNetworkInfo(int networkType) {
614 enforceAccessPermission();
615 if (ConnectivityManager.isNetworkTypeValid(networkType)) {
616 NetworkStateTracker t = mNetTrackers[networkType];
617 if (t != null)
618 return t.getNetworkInfo();
619 }
620 return null;
621 }
622
623 public NetworkInfo[] getAllNetworkInfo() {
624 enforceAccessPermission();
Robert Greenwaltec05b3c2009-10-30 14:17:42 -0700625 NetworkInfo[] result = new NetworkInfo[mNetworksDefined];
The Android Open Source Project28527d22009-03-03 19:31:44 -0800626 int i = 0;
627 for (NetworkStateTracker t : mNetTrackers) {
Robert Greenwaltec05b3c2009-10-30 14:17:42 -0700628 if(t != null) result[i++] = t.getNetworkInfo();
The Android Open Source Project28527d22009-03-03 19:31:44 -0800629 }
630 return result;
631 }
632
633 public boolean setRadios(boolean turnOn) {
634 boolean result = true;
635 enforceChangePermission();
636 for (NetworkStateTracker t : mNetTrackers) {
Robert Greenwaltec05b3c2009-10-30 14:17:42 -0700637 if (t != null) result = t.setRadio(turnOn) && result;
The Android Open Source Project28527d22009-03-03 19:31:44 -0800638 }
639 return result;
640 }
641
642 public boolean setRadio(int netType, boolean turnOn) {
643 enforceChangePermission();
644 if (!ConnectivityManager.isNetworkTypeValid(netType)) {
645 return false;
646 }
647 NetworkStateTracker tracker = mNetTrackers[netType];
648 return tracker != null && tracker.setRadio(turnOn);
649 }
650
Robert Greenwaltaffc3a12009-09-27 17:27:04 -0700651 /**
652 * Used to notice when the calling process dies so we can self-expire
653 *
654 * Also used to know if the process has cleaned up after itself when
655 * our auto-expire timer goes off. The timer has a link to an object.
656 *
657 */
Robert Greenwalt2034b912009-08-12 16:08:25 -0700658 private class FeatureUser implements IBinder.DeathRecipient {
659 int mNetworkType;
660 String mFeature;
661 IBinder mBinder;
662 int mPid;
663 int mUid;
Robert Greenwalt3eeb6032009-12-21 18:24:07 -0800664 long mCreateTime;
Robert Greenwalt2034b912009-08-12 16:08:25 -0700665
666 FeatureUser(int type, String feature, IBinder binder) {
667 super();
668 mNetworkType = type;
669 mFeature = feature;
670 mBinder = binder;
671 mPid = getCallingPid();
672 mUid = getCallingUid();
Robert Greenwalt3eeb6032009-12-21 18:24:07 -0800673 mCreateTime = System.currentTimeMillis();
Robert Greenwaltaffc3a12009-09-27 17:27:04 -0700674
Robert Greenwalt2034b912009-08-12 16:08:25 -0700675 try {
676 mBinder.linkToDeath(this, 0);
677 } catch (RemoteException e) {
678 binderDied();
679 }
680 }
681
682 void unlinkDeathRecipient() {
683 mBinder.unlinkToDeath(this, 0);
684 }
685
686 public void binderDied() {
Joe Onoratoc2386bb2010-02-26 18:56:32 -0800687 Slog.d(TAG, "ConnectivityService FeatureUser binderDied(" +
Robert Greenwalt3eeb6032009-12-21 18:24:07 -0800688 mNetworkType + ", " + mFeature + ", " + mBinder + "), created " +
689 (System.currentTimeMillis() - mCreateTime) + " mSec ago");
Robert Greenwaltaffc3a12009-09-27 17:27:04 -0700690 stopUsingNetworkFeature(this, false);
Robert Greenwalt2034b912009-08-12 16:08:25 -0700691 }
692
Robert Greenwaltaffc3a12009-09-27 17:27:04 -0700693 public void expire() {
Joe Onoratoc2386bb2010-02-26 18:56:32 -0800694 Slog.d(TAG, "ConnectivityService FeatureUser expire(" +
Robert Greenwalt3eeb6032009-12-21 18:24:07 -0800695 mNetworkType + ", " + mFeature + ", " + mBinder +"), created " +
696 (System.currentTimeMillis() - mCreateTime) + " mSec ago");
Robert Greenwaltaffc3a12009-09-27 17:27:04 -0700697 stopUsingNetworkFeature(this, false);
698 }
Robert Greenwalt3eeb6032009-12-21 18:24:07 -0800699
700 public String toString() {
701 return "FeatureUser("+mNetworkType+","+mFeature+","+mPid+","+mUid+"), created " +
702 (System.currentTimeMillis() - mCreateTime) + " mSec ago";
703 }
Robert Greenwalt2034b912009-08-12 16:08:25 -0700704 }
705
Robert Greenwaltaffc3a12009-09-27 17:27:04 -0700706 // javadoc from interface
Robert Greenwalt2034b912009-08-12 16:08:25 -0700707 public int startUsingNetworkFeature(int networkType, String feature,
708 IBinder binder) {
709 if (DBG) {
Joe Onoratoc2386bb2010-02-26 18:56:32 -0800710 Slog.d(TAG, "startUsingNetworkFeature for net " + networkType +
Robert Greenwalt2034b912009-08-12 16:08:25 -0700711 ": " + feature);
712 }
The Android Open Source Project28527d22009-03-03 19:31:44 -0800713 enforceChangePermission();
Robert Greenwaltec05b3c2009-10-30 14:17:42 -0700714 if (!ConnectivityManager.isNetworkTypeValid(networkType) ||
715 mNetAttributes[networkType] == null) {
Robert Greenwalt2034b912009-08-12 16:08:25 -0700716 return Phone.APN_REQUEST_FAILED;
The Android Open Source Project28527d22009-03-03 19:31:44 -0800717 }
Robert Greenwalt2034b912009-08-12 16:08:25 -0700718
Robert Greenwaltaffc3a12009-09-27 17:27:04 -0700719 FeatureUser f = new FeatureUser(networkType, feature, binder);
Robert Greenwalt2034b912009-08-12 16:08:25 -0700720
721 // TODO - move this into the MobileDataStateTracker
722 int usedNetworkType = networkType;
723 if(networkType == ConnectivityManager.TYPE_MOBILE) {
Robert Greenwalt1b21f6c2010-02-23 18:58:05 -0800724 if (!getMobileDataEnabled()) {
Joe Onoratoc2386bb2010-02-26 18:56:32 -0800725 if (DBG) Slog.d(TAG, "requested special network with data disabled - rejected");
Robert Greenwalt1b21f6c2010-02-23 18:58:05 -0800726 return Phone.APN_TYPE_NOT_AVAILABLE;
727 }
Robert Greenwalt2034b912009-08-12 16:08:25 -0700728 if (TextUtils.equals(feature, Phone.FEATURE_ENABLE_MMS)) {
729 usedNetworkType = ConnectivityManager.TYPE_MOBILE_MMS;
730 } else if (TextUtils.equals(feature, Phone.FEATURE_ENABLE_SUPL)) {
731 usedNetworkType = ConnectivityManager.TYPE_MOBILE_SUPL;
732 } else if (TextUtils.equals(feature, Phone.FEATURE_ENABLE_DUN)) {
733 usedNetworkType = ConnectivityManager.TYPE_MOBILE_DUN;
734 } else if (TextUtils.equals(feature, Phone.FEATURE_ENABLE_HIPRI)) {
735 usedNetworkType = ConnectivityManager.TYPE_MOBILE_HIPRI;
736 }
737 }
738 NetworkStateTracker network = mNetTrackers[usedNetworkType];
739 if (network != null) {
740 if (usedNetworkType != networkType) {
741 Integer currentPid = new Integer(getCallingPid());
742
743 NetworkStateTracker radio = mNetTrackers[networkType];
744 NetworkInfo ni = network.getNetworkInfo();
745
746 if (ni.isAvailable() == false) {
Joe Onoratoc2386bb2010-02-26 18:56:32 -0800747 if (DBG) Slog.d(TAG, "special network not available");
Robert Greenwalt2034b912009-08-12 16:08:25 -0700748 return Phone.APN_TYPE_NOT_AVAILABLE;
749 }
750
Robert Greenwaltaffc3a12009-09-27 17:27:04 -0700751 synchronized(this) {
752 mFeatureUsers.add(f);
753 if (!mNetRequestersPids[usedNetworkType].contains(currentPid)) {
754 // this gets used for per-pid dns when connected
755 mNetRequestersPids[usedNetworkType].add(currentPid);
756 }
Robert Greenwalt2034b912009-08-12 16:08:25 -0700757 }
Robert Greenwalt6a2db8a2010-09-23 10:05:56 -0700758 mHandler.sendMessageDelayed(mHandler.obtainMessage(EVENT_RESTORE_DEFAULT_NETWORK,
Robert Greenwaltaffc3a12009-09-27 17:27:04 -0700759 f), getRestoreDefaultNetworkDelay());
760
Robert Greenwalt2034b912009-08-12 16:08:25 -0700761
Robert Greenwalta52c75a2009-08-19 20:19:33 -0700762 if ((ni.isConnectedOrConnecting() == true) &&
763 !network.isTeardownRequested()) {
Robert Greenwalt2034b912009-08-12 16:08:25 -0700764 if (ni.isConnected() == true) {
765 // add the pid-specific dns
Robert Greenwalt0ef68752010-08-13 14:16:12 -0700766 handleDnsConfigurationChange(networkType);
Joe Onoratoc2386bb2010-02-26 18:56:32 -0800767 if (DBG) Slog.d(TAG, "special network already active");
Robert Greenwalt2034b912009-08-12 16:08:25 -0700768 return Phone.APN_ALREADY_ACTIVE;
769 }
Joe Onoratoc2386bb2010-02-26 18:56:32 -0800770 if (DBG) Slog.d(TAG, "special network already connecting");
Robert Greenwalt2034b912009-08-12 16:08:25 -0700771 return Phone.APN_REQUEST_STARTED;
772 }
773
774 // check if the radio in play can make another contact
775 // assume if cannot for now
776
Joe Onoratoc2386bb2010-02-26 18:56:32 -0800777 if (DBG) Slog.d(TAG, "reconnecting to special network");
Robert Greenwalt2034b912009-08-12 16:08:25 -0700778 network.reconnect();
779 return Phone.APN_REQUEST_STARTED;
780 } else {
Robert Greenwaltaffc3a12009-09-27 17:27:04 -0700781 synchronized(this) {
782 mFeatureUsers.add(f);
783 }
Robert Greenwalt6a2db8a2010-09-23 10:05:56 -0700784 mHandler.sendMessageDelayed(mHandler.obtainMessage(EVENT_RESTORE_DEFAULT_NETWORK,
Robert Greenwaltaffc3a12009-09-27 17:27:04 -0700785 f), getRestoreDefaultNetworkDelay());
786
Robert Greenwalt2034b912009-08-12 16:08:25 -0700787 return network.startUsingNetworkFeature(feature,
788 getCallingPid(), getCallingUid());
789 }
790 }
791 return Phone.APN_TYPE_NOT_AVAILABLE;
The Android Open Source Project28527d22009-03-03 19:31:44 -0800792 }
793
Robert Greenwaltaffc3a12009-09-27 17:27:04 -0700794 // javadoc from interface
The Android Open Source Project28527d22009-03-03 19:31:44 -0800795 public int stopUsingNetworkFeature(int networkType, String feature) {
Robert Greenwalt28f43012009-10-06 17:52:40 -0700796 enforceChangePermission();
797
Robert Greenwaltaffc3a12009-09-27 17:27:04 -0700798 int pid = getCallingPid();
799 int uid = getCallingUid();
800
801 FeatureUser u = null;
802 boolean found = false;
803
804 synchronized(this) {
805 for (int i = 0; i < mFeatureUsers.size() ; i++) {
806 u = (FeatureUser)mFeatureUsers.get(i);
807 if (uid == u.mUid && pid == u.mPid &&
808 networkType == u.mNetworkType &&
809 TextUtils.equals(feature, u.mFeature)) {
810 found = true;
811 break;
812 }
813 }
814 }
815 if (found && u != null) {
816 // stop regardless of how many other time this proc had called start
817 return stopUsingNetworkFeature(u, true);
818 } else {
819 // none found!
Joe Onoratoc2386bb2010-02-26 18:56:32 -0800820 if (DBG) Slog.d(TAG, "ignoring stopUsingNetworkFeature - not a live request");
Robert Greenwaltaffc3a12009-09-27 17:27:04 -0700821 return 1;
822 }
Robert Greenwalt2034b912009-08-12 16:08:25 -0700823 }
824
Robert Greenwaltaffc3a12009-09-27 17:27:04 -0700825 private int stopUsingNetworkFeature(FeatureUser u, boolean ignoreDups) {
826 int networkType = u.mNetworkType;
827 String feature = u.mFeature;
828 int pid = u.mPid;
829 int uid = u.mUid;
830
831 NetworkStateTracker tracker = null;
832 boolean callTeardown = false; // used to carry our decision outside of sync block
833
Robert Greenwalt2034b912009-08-12 16:08:25 -0700834 if (DBG) {
Joe Onoratoc2386bb2010-02-26 18:56:32 -0800835 Slog.d(TAG, "stopUsingNetworkFeature for net " + networkType +
Robert Greenwalt2034b912009-08-12 16:08:25 -0700836 ": " + feature);
837 }
Robert Greenwalt28f43012009-10-06 17:52:40 -0700838
The Android Open Source Project28527d22009-03-03 19:31:44 -0800839 if (!ConnectivityManager.isNetworkTypeValid(networkType)) {
840 return -1;
841 }
Robert Greenwalt2034b912009-08-12 16:08:25 -0700842
Robert Greenwaltaffc3a12009-09-27 17:27:04 -0700843 // need to link the mFeatureUsers list with the mNetRequestersPids state in this
844 // sync block
845 synchronized(this) {
846 // check if this process still has an outstanding start request
847 if (!mFeatureUsers.contains(u)) {
Robert Greenwalt46ceefa2010-03-10 16:10:43 -0800848 if (DBG) Slog.d(TAG, "ignoring - this process has no outstanding requests");
Robert Greenwalt2034b912009-08-12 16:08:25 -0700849 return 1;
850 }
Robert Greenwaltaffc3a12009-09-27 17:27:04 -0700851 u.unlinkDeathRecipient();
852 mFeatureUsers.remove(mFeatureUsers.indexOf(u));
853 // If we care about duplicate requests, check for that here.
854 //
855 // This is done to support the extension of a request - the app
856 // can request we start the network feature again and renew the
857 // auto-shutoff delay. Normal "stop" calls from the app though
858 // do not pay attention to duplicate requests - in effect the
859 // API does not refcount and a single stop will counter multiple starts.
860 if (ignoreDups == false) {
861 for (int i = 0; i < mFeatureUsers.size() ; i++) {
862 FeatureUser x = (FeatureUser)mFeatureUsers.get(i);
863 if (x.mUid == u.mUid && x.mPid == u.mPid &&
864 x.mNetworkType == u.mNetworkType &&
865 TextUtils.equals(x.mFeature, u.mFeature)) {
Joe Onoratoc2386bb2010-02-26 18:56:32 -0800866 if (DBG) Slog.d(TAG, "ignoring stopUsingNetworkFeature as dup is found");
Robert Greenwaltaffc3a12009-09-27 17:27:04 -0700867 return 1;
868 }
869 }
Robert Greenwalt2034b912009-08-12 16:08:25 -0700870 }
Robert Greenwaltaffc3a12009-09-27 17:27:04 -0700871
872 // TODO - move to MobileDataStateTracker
873 int usedNetworkType = networkType;
874 if (networkType == ConnectivityManager.TYPE_MOBILE) {
875 if (TextUtils.equals(feature, Phone.FEATURE_ENABLE_MMS)) {
876 usedNetworkType = ConnectivityManager.TYPE_MOBILE_MMS;
877 } else if (TextUtils.equals(feature, Phone.FEATURE_ENABLE_SUPL)) {
878 usedNetworkType = ConnectivityManager.TYPE_MOBILE_SUPL;
879 } else if (TextUtils.equals(feature, Phone.FEATURE_ENABLE_DUN)) {
880 usedNetworkType = ConnectivityManager.TYPE_MOBILE_DUN;
881 } else if (TextUtils.equals(feature, Phone.FEATURE_ENABLE_HIPRI)) {
882 usedNetworkType = ConnectivityManager.TYPE_MOBILE_HIPRI;
883 }
884 }
885 tracker = mNetTrackers[usedNetworkType];
Robert Greenwaltec05b3c2009-10-30 14:17:42 -0700886 if (tracker == null) {
Robert Greenwalt46ceefa2010-03-10 16:10:43 -0800887 if (DBG) Slog.d(TAG, "ignoring - no known tracker for net type " + usedNetworkType);
Robert Greenwaltec05b3c2009-10-30 14:17:42 -0700888 return -1;
889 }
890 if (usedNetworkType != networkType) {
Robert Greenwaltaffc3a12009-09-27 17:27:04 -0700891 Integer currentPid = new Integer(pid);
Robert Greenwaltaffc3a12009-09-27 17:27:04 -0700892 mNetRequestersPids[usedNetworkType].remove(currentPid);
Robert Greenwalt0ca68a02009-12-17 14:54:59 -0800893 reassessPidDns(pid, true);
Robert Greenwaltaffc3a12009-09-27 17:27:04 -0700894 if (mNetRequestersPids[usedNetworkType].size() != 0) {
Joe Onoratoc2386bb2010-02-26 18:56:32 -0800895 if (DBG) Slog.d(TAG, "not tearing down special network - " +
Robert Greenwaltaffc3a12009-09-27 17:27:04 -0700896 "others still using it");
897 return 1;
898 }
899 callTeardown = true;
900 }
901 }
Robert Greenwalt46ceefa2010-03-10 16:10:43 -0800902 if (DBG) Slog.d(TAG, "Doing network teardown");
Robert Greenwaltaffc3a12009-09-27 17:27:04 -0700903 if (callTeardown) {
904 tracker.teardown();
Robert Greenwalt2034b912009-08-12 16:08:25 -0700905 return 1;
906 } else {
Robert Greenwaltaffc3a12009-09-27 17:27:04 -0700907 // do it the old fashioned way
Robert Greenwalt2034b912009-08-12 16:08:25 -0700908 return tracker.stopUsingNetworkFeature(feature, pid, uid);
909 }
The Android Open Source Project28527d22009-03-03 19:31:44 -0800910 }
911
912 /**
Banavathu, Srinivas Naik7601f572010-08-10 20:13:53 +0530913 * @deprecated use requestRouteToHostAddress instead
914 *
The Android Open Source Project28527d22009-03-03 19:31:44 -0800915 * Ensure that a network route exists to deliver traffic to the specified
916 * host via the specified network interface.
Robert Greenwalt0659da32009-07-16 17:21:39 -0700917 * @param networkType the type of the network over which traffic to the
918 * specified host is to be routed
919 * @param hostAddress the IP address of the host to which the route is
920 * desired
The Android Open Source Project28527d22009-03-03 19:31:44 -0800921 * @return {@code true} on success, {@code false} on failure
922 */
923 public boolean requestRouteToHost(int networkType, int hostAddress) {
Banavathu, Srinivas Naik7601f572010-08-10 20:13:53 +0530924 InetAddress inetAddress = NetworkUtils.intToInetAddress(hostAddress);
925
926 if (inetAddress == null) {
927 return false;
928 }
929
930 return requestRouteToHostAddress(networkType, inetAddress.getAddress());
931 }
932
933 /**
934 * Ensure that a network route exists to deliver traffic to the specified
935 * host via the specified network interface.
936 * @param networkType the type of the network over which traffic to the
937 * specified host is to be routed
938 * @param hostAddress the IP address of the host to which the route is
939 * desired
940 * @return {@code true} on success, {@code false} on failure
941 */
942 public boolean requestRouteToHostAddress(int networkType, byte[] hostAddress) {
The Android Open Source Project28527d22009-03-03 19:31:44 -0800943 enforceChangePermission();
944 if (!ConnectivityManager.isNetworkTypeValid(networkType)) {
945 return false;
946 }
947 NetworkStateTracker tracker = mNetTrackers[networkType];
Robert Greenwalt4666ed02009-09-10 15:06:20 -0700948
Robert Greenwaltec05b3c2009-10-30 14:17:42 -0700949 if (tracker == null || !tracker.getNetworkInfo().isConnected() ||
950 tracker.isTeardownRequested()) {
Robert Greenwalt4666ed02009-09-10 15:06:20 -0700951 if (DBG) {
Banavathu, Srinivas Naik7601f572010-08-10 20:13:53 +0530952 Slog.d(TAG, "requestRouteToHostAddress on down network " +
953 "(" + networkType + ") - dropped");
Robert Greenwalt4666ed02009-09-10 15:06:20 -0700954 }
955 return false;
The Android Open Source Project28527d22009-03-03 19:31:44 -0800956 }
Banavathu, Srinivas Naik7601f572010-08-10 20:13:53 +0530957
958 try {
959 InetAddress inetAddress = InetAddress.getByAddress(hostAddress);
960 return tracker.requestRouteToHost(inetAddress);
961 } catch (UnknownHostException e) {
962 return false;
963 }
The Android Open Source Project28527d22009-03-03 19:31:44 -0800964 }
965
966 /**
967 * @see ConnectivityManager#getBackgroundDataSetting()
968 */
969 public boolean getBackgroundDataSetting() {
970 return Settings.Secure.getInt(mContext.getContentResolver(),
971 Settings.Secure.BACKGROUND_DATA, 1) == 1;
972 }
Robert Greenwalt0659da32009-07-16 17:21:39 -0700973
The Android Open Source Project28527d22009-03-03 19:31:44 -0800974 /**
975 * @see ConnectivityManager#setBackgroundDataSetting(boolean)
976 */
977 public void setBackgroundDataSetting(boolean allowBackgroundDataUsage) {
978 mContext.enforceCallingOrSelfPermission(
979 android.Manifest.permission.CHANGE_BACKGROUND_DATA_SETTING,
980 "ConnectivityService");
Robert Greenwalt0659da32009-07-16 17:21:39 -0700981
Robert Greenwalt6a2db8a2010-09-23 10:05:56 -0700982 mHandler.sendMessage(mHandler.obtainMessage(EVENT_SET_BACKGROUND_DATA,
983 (allowBackgroundDataUsage ? ENABLED : DISABLED), 0));
984 }
The Android Open Source Project28527d22009-03-03 19:31:44 -0800985
Robert Greenwalt6a2db8a2010-09-23 10:05:56 -0700986 private void handleSetBackgroundData(boolean enabled) {
987 if (enabled != getBackgroundDataSetting()) {
988 Settings.Secure.putInt(mContext.getContentResolver(),
989 Settings.Secure.BACKGROUND_DATA, enabled ? 1 : 0);
990 Intent broadcast = new Intent(
991 ConnectivityManager.ACTION_BACKGROUND_DATA_SETTING_CHANGED);
992 mContext.sendBroadcast(broadcast);
993 }
Robert Greenwalt0659da32009-07-16 17:21:39 -0700994 }
995
Robert Greenwalt1b21f6c2010-02-23 18:58:05 -0800996 /**
997 * @see ConnectivityManager#getMobileDataEnabled()
998 */
999 public boolean getMobileDataEnabled() {
1000 enforceAccessPermission();
1001 boolean retVal = Settings.Secure.getInt(mContext.getContentResolver(),
1002 Settings.Secure.MOBILE_DATA, 1) == 1;
Joe Onoratoc2386bb2010-02-26 18:56:32 -08001003 if (DBG) Slog.d(TAG, "getMobileDataEnabled returning " + retVal);
Robert Greenwalt1b21f6c2010-02-23 18:58:05 -08001004 return retVal;
1005 }
1006
1007 /**
1008 * @see ConnectivityManager#setMobileDataEnabled(boolean)
1009 */
Robert Greenwalt6a2db8a2010-09-23 10:05:56 -07001010 public void setMobileDataEnabled(boolean enabled) {
Robert Greenwalt1b21f6c2010-02-23 18:58:05 -08001011 enforceChangePermission();
Joe Onoratoc2386bb2010-02-26 18:56:32 -08001012 if (DBG) Slog.d(TAG, "setMobileDataEnabled(" + enabled + ")");
Robert Greenwalt1b21f6c2010-02-23 18:58:05 -08001013
Robert Greenwalt6a2db8a2010-09-23 10:05:56 -07001014 mHandler.sendMessage(mHandler.obtainMessage(EVENT_SET_MOBILE_DATA,
1015 (enabled ? ENABLED : DISABLED), 0));
1016 }
1017
1018 private void handleSetMobileData(boolean enabled) {
Robert Greenwalt1b21f6c2010-02-23 18:58:05 -08001019 if (getMobileDataEnabled() == enabled) return;
1020
1021 Settings.Secure.putInt(mContext.getContentResolver(),
1022 Settings.Secure.MOBILE_DATA, enabled ? 1 : 0);
1023
1024 if (enabled) {
1025 if (mNetTrackers[ConnectivityManager.TYPE_MOBILE] != null) {
Robert Greenwalt6a2db8a2010-09-23 10:05:56 -07001026 if (DBG) {
1027 Slog.d(TAG, "starting up " + mNetTrackers[ConnectivityManager.TYPE_MOBILE]);
1028 }
Robert Greenwalt1b21f6c2010-02-23 18:58:05 -08001029 mNetTrackers[ConnectivityManager.TYPE_MOBILE].reconnect();
1030 }
1031 } else {
1032 for (NetworkStateTracker nt : mNetTrackers) {
1033 if (nt == null) continue;
1034 int netType = nt.getNetworkInfo().getType();
1035 if (mNetAttributes[netType].mRadio == ConnectivityManager.TYPE_MOBILE) {
Joe Onoratoc2386bb2010-02-26 18:56:32 -08001036 if (DBG) Slog.d(TAG, "tearing down " + nt);
Robert Greenwalt1b21f6c2010-02-23 18:58:05 -08001037 nt.teardown();
1038 }
1039 }
1040 }
1041 }
1042
The Android Open Source Project28527d22009-03-03 19:31:44 -08001043 private int getNumConnectedNetworks() {
1044 int numConnectedNets = 0;
1045
1046 for (NetworkStateTracker nt : mNetTrackers) {
Robert Greenwaltec05b3c2009-10-30 14:17:42 -07001047 if (nt != null && nt.getNetworkInfo().isConnected() &&
Robert Greenwalt0659da32009-07-16 17:21:39 -07001048 !nt.isTeardownRequested()) {
The Android Open Source Project28527d22009-03-03 19:31:44 -08001049 ++numConnectedNets;
1050 }
1051 }
1052 return numConnectedNets;
1053 }
1054
1055 private void enforceAccessPermission() {
Robert Greenwalt0659da32009-07-16 17:21:39 -07001056 mContext.enforceCallingOrSelfPermission(
1057 android.Manifest.permission.ACCESS_NETWORK_STATE,
1058 "ConnectivityService");
The Android Open Source Project28527d22009-03-03 19:31:44 -08001059 }
1060
1061 private void enforceChangePermission() {
Robert Greenwalt0659da32009-07-16 17:21:39 -07001062 mContext.enforceCallingOrSelfPermission(
1063 android.Manifest.permission.CHANGE_NETWORK_STATE,
1064 "ConnectivityService");
The Android Open Source Project28527d22009-03-03 19:31:44 -08001065 }
1066
Robert Greenwalt0c4828c2010-01-26 11:40:34 -08001067 // TODO Make this a special check when it goes public
1068 private void enforceTetherChangePermission() {
1069 mContext.enforceCallingOrSelfPermission(
1070 android.Manifest.permission.CHANGE_NETWORK_STATE,
1071 "ConnectivityService");
1072 }
1073
Robert Greenwalt8e87f122010-02-11 18:18:40 -08001074 private void enforceTetherAccessPermission() {
1075 mContext.enforceCallingOrSelfPermission(
1076 android.Manifest.permission.ACCESS_NETWORK_STATE,
1077 "ConnectivityService");
1078 }
1079
The Android Open Source Project28527d22009-03-03 19:31:44 -08001080 /**
Robert Greenwalt0659da32009-07-16 17:21:39 -07001081 * Handle a {@code DISCONNECTED} event. If this pertains to the non-active
1082 * network, we ignore it. If it is for the active network, we send out a
1083 * broadcast. But first, we check whether it might be possible to connect
1084 * to a different network.
The Android Open Source Project28527d22009-03-03 19:31:44 -08001085 * @param info the {@code NetworkInfo} for the network
1086 */
1087 private void handleDisconnect(NetworkInfo info) {
1088
Robert Greenwalt2034b912009-08-12 16:08:25 -07001089 int prevNetType = info.getType();
The Android Open Source Project28527d22009-03-03 19:31:44 -08001090
Robert Greenwalt2034b912009-08-12 16:08:25 -07001091 mNetTrackers[prevNetType].setTeardownRequested(false);
The Android Open Source Project28527d22009-03-03 19:31:44 -08001092 /*
1093 * If the disconnected network is not the active one, then don't report
1094 * this as a loss of connectivity. What probably happened is that we're
1095 * getting the disconnect for a network that we explicitly disabled
1096 * in accordance with network preference policies.
1097 */
Robert Greenwalt2034b912009-08-12 16:08:25 -07001098 if (!mNetAttributes[prevNetType].isDefault()) {
1099 List pids = mNetRequestersPids[prevNetType];
1100 for (int i = 0; i<pids.size(); i++) {
1101 Integer pid = (Integer)pids.get(i);
1102 // will remove them because the net's no longer connected
1103 // need to do this now as only now do we know the pids and
1104 // can properly null things that are no longer referenced.
1105 reassessPidDns(pid.intValue(), false);
The Android Open Source Project28527d22009-03-03 19:31:44 -08001106 }
The Android Open Source Project28527d22009-03-03 19:31:44 -08001107 }
1108
The Android Open Source Project28527d22009-03-03 19:31:44 -08001109 Intent intent = new Intent(ConnectivityManager.CONNECTIVITY_ACTION);
1110 intent.putExtra(ConnectivityManager.EXTRA_NETWORK_INFO, info);
1111 if (info.isFailover()) {
1112 intent.putExtra(ConnectivityManager.EXTRA_IS_FAILOVER, true);
1113 info.setFailover(false);
1114 }
1115 if (info.getReason() != null) {
1116 intent.putExtra(ConnectivityManager.EXTRA_REASON, info.getReason());
1117 }
1118 if (info.getExtraInfo() != null) {
Robert Greenwalt0659da32009-07-16 17:21:39 -07001119 intent.putExtra(ConnectivityManager.EXTRA_EXTRA_INFO,
1120 info.getExtraInfo());
The Android Open Source Project28527d22009-03-03 19:31:44 -08001121 }
Robert Greenwalt2034b912009-08-12 16:08:25 -07001122
Robert Greenwalt3cc68d32010-01-25 17:54:29 -08001123 NetworkStateTracker newNet = null;
1124 if (mNetAttributes[prevNetType].isDefault()) {
1125 newNet = tryFailover(prevNetType);
1126 if (newNet != null) {
1127 NetworkInfo switchTo = newNet.getNetworkInfo();
Robert Greenwaltb2a9e492010-09-20 18:01:43 -07001128 if (!switchTo.isConnected()) {
1129 // if the other net is connected they've already reset this and perhaps even gotten
1130 // a positive report we don't want to overwrite, but if not we need to clear this now
1131 // to turn our cellular sig strength white
1132 mDefaultInetConditionPublished = 0;
Robert Greenwaltea8bca02010-12-21 11:43:28 -08001133 intent.putExtra(ConnectivityManager.EXTRA_NO_CONNECTIVITY, true);
Robert Greenwaltb2a9e492010-09-20 18:01:43 -07001134 }
Robert Greenwalt3cc68d32010-01-25 17:54:29 -08001135 intent.putExtra(ConnectivityManager.EXTRA_OTHER_NETWORK_INFO, switchTo);
1136 } else {
Robert Greenwaltb2a9e492010-09-20 18:01:43 -07001137 mDefaultInetConditionPublished = 0; // we're not connected anymore
Robert Greenwalt3cc68d32010-01-25 17:54:29 -08001138 intent.putExtra(ConnectivityManager.EXTRA_NO_CONNECTIVITY, true);
1139 }
Robert Greenwaltf55ced92010-01-20 19:29:41 -08001140 }
Robert Greenwaltb2a9e492010-09-20 18:01:43 -07001141 intent.putExtra(ConnectivityManager.EXTRA_INET_CONDITION, mDefaultInetConditionPublished);
Robert Greenwaltf55ced92010-01-20 19:29:41 -08001142 // do this before we broadcast the change
Robert Greenwalt0ef68752010-08-13 14:16:12 -07001143 handleConnectivityChange(prevNetType);
Robert Greenwaltf55ced92010-01-20 19:29:41 -08001144
1145 sendStickyBroadcast(intent);
1146 /*
1147 * If the failover network is already connected, then immediately send
1148 * out a followup broadcast indicating successful failover
1149 */
1150 if (newNet != null && newNet.getNetworkInfo().isConnected()) {
1151 sendConnectedBroadcast(newNet.getNetworkInfo());
1152 }
1153 }
1154
Robert Greenwalt3cc68d32010-01-25 17:54:29 -08001155 // returns null if no failover available
Robert Greenwaltf55ced92010-01-20 19:29:41 -08001156 private NetworkStateTracker tryFailover(int prevNetType) {
Robert Greenwalt2034b912009-08-12 16:08:25 -07001157 /*
1158 * If this is a default network, check if other defaults are available
1159 * or active
1160 */
1161 NetworkStateTracker newNet = null;
1162 if (mNetAttributes[prevNetType].isDefault()) {
Robert Greenwalt2034b912009-08-12 16:08:25 -07001163 if (mActiveDefaultNetwork == prevNetType) {
1164 mActiveDefaultNetwork = -1;
1165 }
1166
1167 int newType = -1;
1168 int newPriority = -1;
Robert Greenwalt72451bf2010-02-25 12:04:29 -08001169 boolean noMobileData = !getMobileDataEnabled();
Robert Greenwaltf55ced92010-01-20 19:29:41 -08001170 for (int checkType=0; checkType <= ConnectivityManager.MAX_NETWORK_TYPE; checkType++) {
Robert Greenwaltec05b3c2009-10-30 14:17:42 -07001171 if (checkType == prevNetType) continue;
1172 if (mNetAttributes[checkType] == null) continue;
Robert Greenwalt084e4772011-02-11 17:32:58 -08001173 if (mNetAttributes[checkType].isDefault() == false) continue;
Robert Greenwalt72451bf2010-02-25 12:04:29 -08001174 if (mNetAttributes[checkType].mRadio == ConnectivityManager.TYPE_MOBILE &&
1175 noMobileData) {
Robert Greenwalte981bc52010-10-08 16:35:52 -07001176 Slog.e(TAG, "not failing over to mobile type " + checkType +
1177 " because Mobile Data Disabled");
Robert Greenwalt72451bf2010-02-25 12:04:29 -08001178 continue;
1179 }
Robert Greenwalt084e4772011-02-11 17:32:58 -08001180 NetworkStateTracker tracker = mNetTrackers[checkType];
1181 NetworkInfo info = tracker.getNetworkInfo();
1182 if (!info.isConnectedOrConnecting() ||
1183 tracker.isTeardownRequested()) {
1184 info.setFailover(true);
1185 tracker.reconnect();
The Android Open Source Project28527d22009-03-03 19:31:44 -08001186 }
Robert Greenwalt084e4772011-02-11 17:32:58 -08001187 if (DBG) Slog.d(TAG, "Attempting to switch to " + info.getTypeName());
Robert Greenwalt2034b912009-08-12 16:08:25 -07001188
Robert Greenwalt084e4772011-02-11 17:32:58 -08001189 // figure out if this is the highest priority network
1190 // so we send an appropriate return value
1191 if (checkType == mNetworkPreference) {
1192 newType = checkType;
Robert Greenwalt2034b912009-08-12 16:08:25 -07001193 }
Robert Greenwalt084e4772011-02-11 17:32:58 -08001194 if (mNetAttributes[checkType].mPriority > newPriority &&
1195 newType != mNetworkPreference) {
1196 newType = checkType;
1197 newPriority = mNetAttributes[checkType].mPriority;
1198 }
Robert Greenwalt2034b912009-08-12 16:08:25 -07001199 }
The Android Open Source Project28527d22009-03-03 19:31:44 -08001200 }
Robert Greenwalt2034b912009-08-12 16:08:25 -07001201
Robert Greenwaltf55ced92010-01-20 19:29:41 -08001202 return newNet;
The Android Open Source Project28527d22009-03-03 19:31:44 -08001203 }
1204
1205 private void sendConnectedBroadcast(NetworkInfo info) {
Robert Greenwaltd3401f92010-09-15 17:36:33 -07001206 sendGeneralBroadcast(info, ConnectivityManager.CONNECTIVITY_ACTION);
1207 }
1208
1209 private void sendInetConditionBroadcast(NetworkInfo info) {
1210 sendGeneralBroadcast(info, ConnectivityManager.INET_CONDITION_ACTION);
1211 }
1212
1213 private void sendGeneralBroadcast(NetworkInfo info, String bcastType) {
1214 Intent intent = new Intent(bcastType);
The Android Open Source Project28527d22009-03-03 19:31:44 -08001215 intent.putExtra(ConnectivityManager.EXTRA_NETWORK_INFO, info);
1216 if (info.isFailover()) {
1217 intent.putExtra(ConnectivityManager.EXTRA_IS_FAILOVER, true);
1218 info.setFailover(false);
1219 }
1220 if (info.getReason() != null) {
1221 intent.putExtra(ConnectivityManager.EXTRA_REASON, info.getReason());
1222 }
1223 if (info.getExtraInfo() != null) {
Robert Greenwalt0659da32009-07-16 17:21:39 -07001224 intent.putExtra(ConnectivityManager.EXTRA_EXTRA_INFO,
1225 info.getExtraInfo());
The Android Open Source Project28527d22009-03-03 19:31:44 -08001226 }
Robert Greenwalt986c7412010-09-08 15:24:47 -07001227 intent.putExtra(ConnectivityManager.EXTRA_INET_CONDITION, mDefaultInetConditionPublished);
Mike Lockwoodfde2b762009-08-14 14:18:49 -04001228 sendStickyBroadcast(intent);
The Android Open Source Project28527d22009-03-03 19:31:44 -08001229 }
1230
1231 /**
1232 * Called when an attempt to fail over to another network has failed.
1233 * @param info the {@link NetworkInfo} for the failed network
1234 */
1235 private void handleConnectionFailure(NetworkInfo info) {
1236 mNetTrackers[info.getType()].setTeardownRequested(false);
The Android Open Source Project28527d22009-03-03 19:31:44 -08001237
Robert Greenwalt2034b912009-08-12 16:08:25 -07001238 String reason = info.getReason();
1239 String extraInfo = info.getExtraInfo();
Robert Greenwalt0659da32009-07-16 17:21:39 -07001240
Robert Greenwalte981bc52010-10-08 16:35:52 -07001241 String reasonText;
1242 if (reason == null) {
1243 reasonText = ".";
1244 } else {
1245 reasonText = " (" + reason + ").";
The Android Open Source Project28527d22009-03-03 19:31:44 -08001246 }
Robert Greenwalte981bc52010-10-08 16:35:52 -07001247 Slog.e(TAG, "Attempt to connect to " + info.getTypeName() + " failed" + reasonText);
Robert Greenwalt2034b912009-08-12 16:08:25 -07001248
1249 Intent intent = new Intent(ConnectivityManager.CONNECTIVITY_ACTION);
1250 intent.putExtra(ConnectivityManager.EXTRA_NETWORK_INFO, info);
1251 if (getActiveNetworkInfo() == null) {
1252 intent.putExtra(ConnectivityManager.EXTRA_NO_CONNECTIVITY, true);
1253 }
1254 if (reason != null) {
1255 intent.putExtra(ConnectivityManager.EXTRA_REASON, reason);
1256 }
1257 if (extraInfo != null) {
1258 intent.putExtra(ConnectivityManager.EXTRA_EXTRA_INFO, extraInfo);
1259 }
1260 if (info.isFailover()) {
1261 intent.putExtra(ConnectivityManager.EXTRA_IS_FAILOVER, true);
1262 info.setFailover(false);
1263 }
Robert Greenwaltf55ced92010-01-20 19:29:41 -08001264
Robert Greenwalt3cc68d32010-01-25 17:54:29 -08001265 NetworkStateTracker newNet = null;
1266 if (mNetAttributes[info.getType()].isDefault()) {
1267 newNet = tryFailover(info.getType());
1268 if (newNet != null) {
1269 NetworkInfo switchTo = newNet.getNetworkInfo();
Robert Greenwaltb2a9e492010-09-20 18:01:43 -07001270 if (!switchTo.isConnected()) {
Robert Greenwalte981bc52010-10-08 16:35:52 -07001271 // if the other net is connected they've already reset this and perhaps
1272 // even gotten a positive report we don't want to overwrite, but if not
1273 // we need to clear this now to turn our cellular sig strength white
Robert Greenwaltb2a9e492010-09-20 18:01:43 -07001274 mDefaultInetConditionPublished = 0;
1275 }
Robert Greenwalt3cc68d32010-01-25 17:54:29 -08001276 intent.putExtra(ConnectivityManager.EXTRA_OTHER_NETWORK_INFO, switchTo);
1277 } else {
Robert Greenwaltb2a9e492010-09-20 18:01:43 -07001278 mDefaultInetConditionPublished = 0;
Robert Greenwalt3cc68d32010-01-25 17:54:29 -08001279 intent.putExtra(ConnectivityManager.EXTRA_NO_CONNECTIVITY, true);
1280 }
Robert Greenwaltf55ced92010-01-20 19:29:41 -08001281 }
Robert Greenwalt3cc68d32010-01-25 17:54:29 -08001282
Robert Greenwaltb2a9e492010-09-20 18:01:43 -07001283 intent.putExtra(ConnectivityManager.EXTRA_INET_CONDITION, mDefaultInetConditionPublished);
Mike Lockwoodfde2b762009-08-14 14:18:49 -04001284 sendStickyBroadcast(intent);
Robert Greenwaltf55ced92010-01-20 19:29:41 -08001285 /*
1286 * If the failover network is already connected, then immediately send
1287 * out a followup broadcast indicating successful failover
1288 */
1289 if (newNet != null && newNet.getNetworkInfo().isConnected()) {
1290 sendConnectedBroadcast(newNet.getNetworkInfo());
1291 }
Mike Lockwoodfde2b762009-08-14 14:18:49 -04001292 }
1293
1294 private void sendStickyBroadcast(Intent intent) {
1295 synchronized(this) {
Dianne Hackborna417ff82009-12-08 19:45:14 -08001296 if (!mSystemReady) {
1297 mInitialBroadcast = new Intent(intent);
Mike Lockwoodfde2b762009-08-14 14:18:49 -04001298 }
Dianne Hackborna417ff82009-12-08 19:45:14 -08001299 intent.addFlags(Intent.FLAG_RECEIVER_REGISTERED_ONLY_BEFORE_BOOT);
1300 mContext.sendStickyBroadcast(intent);
Mike Lockwoodfde2b762009-08-14 14:18:49 -04001301 }
1302 }
1303
1304 void systemReady() {
1305 synchronized(this) {
1306 mSystemReady = true;
Dianne Hackborna417ff82009-12-08 19:45:14 -08001307 if (mInitialBroadcast != null) {
1308 mContext.sendStickyBroadcast(mInitialBroadcast);
1309 mInitialBroadcast = null;
Mike Lockwoodfde2b762009-08-14 14:18:49 -04001310 }
1311 }
The Android Open Source Project28527d22009-03-03 19:31:44 -08001312 }
1313
1314 private void handleConnect(NetworkInfo info) {
Robert Greenwalt2034b912009-08-12 16:08:25 -07001315 int type = info.getType();
The Android Open Source Project28527d22009-03-03 19:31:44 -08001316
1317 // snapshot isFailover, because sendConnectedBroadcast() resets it
1318 boolean isFailover = info.isFailover();
Robert Greenwalt2034b912009-08-12 16:08:25 -07001319 NetworkStateTracker thisNet = mNetTrackers[type];
The Android Open Source Project28527d22009-03-03 19:31:44 -08001320
Robert Greenwalt2034b912009-08-12 16:08:25 -07001321 // if this is a default net and other default is running
1322 // kill the one not preferred
1323 if (mNetAttributes[type].isDefault()) {
Robert Greenwalt2034b912009-08-12 16:08:25 -07001324 if (mActiveDefaultNetwork != -1 && mActiveDefaultNetwork != type) {
1325 if ((type != mNetworkPreference &&
1326 mNetAttributes[mActiveDefaultNetwork].mPriority >
1327 mNetAttributes[type].mPriority) ||
1328 mNetworkPreference == mActiveDefaultNetwork) {
1329 // don't accept this one
Joe Onoratoc2386bb2010-02-26 18:56:32 -08001330 if (DBG) Slog.v(TAG, "Not broadcasting CONNECT_ACTION " +
Robert Greenwalt2034b912009-08-12 16:08:25 -07001331 "to torn down network " + info.getTypeName());
1332 teardown(thisNet);
1333 return;
1334 } else {
1335 // tear down the other
1336 NetworkStateTracker otherNet =
1337 mNetTrackers[mActiveDefaultNetwork];
Joe Onoratoc2386bb2010-02-26 18:56:32 -08001338 if (DBG) Slog.v(TAG, "Policy requires " +
Robert Greenwalt2034b912009-08-12 16:08:25 -07001339 otherNet.getNetworkInfo().getTypeName() +
1340 " teardown");
1341 if (!teardown(otherNet)) {
Joe Onoratoc2386bb2010-02-26 18:56:32 -08001342 Slog.e(TAG, "Network declined teardown request");
Robert Greenwalt2034b912009-08-12 16:08:25 -07001343 return;
1344 }
1345 if (isFailover) {
1346 otherNet.releaseWakeLock();
1347 }
1348 }
1349 }
1350 mActiveDefaultNetwork = type;
Robert Greenwalt986c7412010-09-08 15:24:47 -07001351 // this will cause us to come up initially as unconnected and switching
1352 // to connected after our normal pause unless somebody reports us as reall
1353 // disconnected
1354 mDefaultInetConditionPublished = 0;
1355 mDefaultConnectionSequence++;
1356 mInetConditionChangeInFlight = false;
1357 // Don't do this - if we never sign in stay, grey
1358 //reportNetworkCondition(mActiveDefaultNetwork, 100);
Robert Greenwalt2034b912009-08-12 16:08:25 -07001359 }
The Android Open Source Project28527d22009-03-03 19:31:44 -08001360 thisNet.setTeardownRequested(false);
Robert Greenwalt2034b912009-08-12 16:08:25 -07001361 thisNet.updateNetworkSettings();
Robert Greenwalt0ef68752010-08-13 14:16:12 -07001362 handleConnectivityChange(type);
Robert Greenwalt2034b912009-08-12 16:08:25 -07001363 sendConnectedBroadcast(info);
The Android Open Source Project28527d22009-03-03 19:31:44 -08001364 }
1365
1366 private void handleScanResultsAvailable(NetworkInfo info) {
1367 int networkType = info.getType();
1368 if (networkType != ConnectivityManager.TYPE_WIFI) {
Joe Onoratoc2386bb2010-02-26 18:56:32 -08001369 if (DBG) Slog.v(TAG, "Got ScanResultsAvailable for " +
Robert Greenwalt0659da32009-07-16 17:21:39 -07001370 info.getTypeName() + " network. Don't know how to handle.");
The Android Open Source Project28527d22009-03-03 19:31:44 -08001371 }
Robert Greenwalt0659da32009-07-16 17:21:39 -07001372
The Android Open Source Project28527d22009-03-03 19:31:44 -08001373 mNetTrackers[networkType].interpretScanResultsAvailable();
1374 }
1375
Robert Greenwalt0659da32009-07-16 17:21:39 -07001376 private void handleNotificationChange(boolean visible, int id,
1377 Notification notification) {
The Android Open Source Project28527d22009-03-03 19:31:44 -08001378 NotificationManager notificationManager = (NotificationManager) mContext
1379 .getSystemService(Context.NOTIFICATION_SERVICE);
Robert Greenwalt0659da32009-07-16 17:21:39 -07001380
The Android Open Source Project28527d22009-03-03 19:31:44 -08001381 if (visible) {
1382 notificationManager.notify(id, notification);
1383 } else {
1384 notificationManager.cancel(id);
1385 }
1386 }
1387
1388 /**
Robert Greenwalt0ef68752010-08-13 14:16:12 -07001389 * After a change in the connectivity state of any network, We're mainly
1390 * concerned with making sure that the list of DNS servers is setupup
1391 * according to which networks are connected, and ensuring that the
1392 * right routing table entries exist.
The Android Open Source Project28527d22009-03-03 19:31:44 -08001393 */
Robert Greenwalt0ef68752010-08-13 14:16:12 -07001394 private void handleConnectivityChange(int netType) {
The Android Open Source Project28527d22009-03-03 19:31:44 -08001395 /*
Robert Greenwalt2034b912009-08-12 16:08:25 -07001396 * If a non-default network is enabled, add the host routes that
Robert Greenwalt0ef68752010-08-13 14:16:12 -07001397 * will allow it's DNS servers to be accessed.
The Android Open Source Project28527d22009-03-03 19:31:44 -08001398 */
Robert Greenwalt0ef68752010-08-13 14:16:12 -07001399 handleDnsConfigurationChange(netType);
Robert Greenwalt2034b912009-08-12 16:08:25 -07001400
Robert Greenwalt0ef68752010-08-13 14:16:12 -07001401 if (mNetTrackers[netType].getNetworkInfo().isConnected()) {
1402 if (mNetAttributes[netType].isDefault()) {
1403 mNetTrackers[netType].addDefaultRoute();
Robert Greenwalt2034b912009-08-12 16:08:25 -07001404 } else {
Robert Greenwalt0ef68752010-08-13 14:16:12 -07001405 mNetTrackers[netType].addPrivateDnsRoutes();
1406 }
1407 } else {
1408 if (mNetAttributes[netType].isDefault()) {
1409 mNetTrackers[netType].removeDefaultRoute();
1410 } else {
1411 mNetTrackers[netType].removePrivateDnsRoutes();
Robert Greenwalt2034b912009-08-12 16:08:25 -07001412 }
The Android Open Source Project28527d22009-03-03 19:31:44 -08001413 }
1414 }
1415
Robert Greenwalt2034b912009-08-12 16:08:25 -07001416 /**
1417 * Adjust the per-process dns entries (net.dns<x>.<pid>) based
1418 * on the highest priority active net which this process requested.
1419 * If there aren't any, clear it out
1420 */
1421 private void reassessPidDns(int myPid, boolean doBump)
1422 {
Joe Onoratoc2386bb2010-02-26 18:56:32 -08001423 if (DBG) Slog.d(TAG, "reassessPidDns for pid " + myPid);
Robert Greenwalt2034b912009-08-12 16:08:25 -07001424 for(int i : mPriorityList) {
1425 if (mNetAttributes[i].isDefault()) {
1426 continue;
1427 }
1428 NetworkStateTracker nt = mNetTrackers[i];
Robert Greenwalt0659da32009-07-16 17:21:39 -07001429 if (nt.getNetworkInfo().isConnected() &&
1430 !nt.isTeardownRequested()) {
Robert Greenwalt2034b912009-08-12 16:08:25 -07001431 List pids = mNetRequestersPids[i];
1432 for (int j=0; j<pids.size(); j++) {
1433 Integer pid = (Integer)pids.get(j);
1434 if (pid.intValue() == myPid) {
1435 String[] dnsList = nt.getNameServers();
1436 writePidDns(dnsList, myPid);
1437 if (doBump) {
1438 bumpDns();
1439 }
1440 return;
1441 }
1442 }
1443 }
1444 }
1445 // nothing found - delete
1446 for (int i = 1; ; i++) {
1447 String prop = "net.dns" + i + "." + myPid;
1448 if (SystemProperties.get(prop).length() == 0) {
1449 if (doBump) {
1450 bumpDns();
1451 }
1452 return;
1453 }
1454 SystemProperties.set(prop, "");
1455 }
1456 }
1457
1458 private void writePidDns(String[] dnsList, int pid) {
1459 int j = 1;
1460 for (String dns : dnsList) {
1461 if (dns != null && !TextUtils.equals(dns, "0.0.0.0")) {
1462 SystemProperties.set("net.dns" + j++ + "." + pid, dns);
1463 }
1464 }
1465 }
1466
1467 private void bumpDns() {
1468 /*
1469 * Bump the property that tells the name resolver library to reread
1470 * the DNS server list from the properties.
1471 */
1472 String propVal = SystemProperties.get("net.dnschange");
1473 int n = 0;
1474 if (propVal.length() != 0) {
1475 try {
1476 n = Integer.parseInt(propVal);
1477 } catch (NumberFormatException e) {}
1478 }
1479 SystemProperties.set("net.dnschange", "" + (n+1));
1480 }
1481
Robert Greenwalt0ef68752010-08-13 14:16:12 -07001482 private void handleDnsConfigurationChange(int netType) {
Robert Greenwalt2034b912009-08-12 16:08:25 -07001483 // add default net's dns entries
Robert Greenwalt0ef68752010-08-13 14:16:12 -07001484 NetworkStateTracker nt = mNetTrackers[netType];
1485 if (nt != null && nt.getNetworkInfo().isConnected() && !nt.isTeardownRequested()) {
1486 String[] dnsList = nt.getNameServers();
1487 if (mNetAttributes[netType].isDefault()) {
1488 int j = 1;
1489 for (String dns : dnsList) {
1490 if (dns != null && !TextUtils.equals(dns, "0.0.0.0")) {
1491 if (DBG) {
1492 Slog.d(TAG, "adding dns " + dns + " for " +
1493 nt.getNetworkInfo().getTypeName());
Robert Greenwalt2034b912009-08-12 16:08:25 -07001494 }
Robert Greenwalt0ef68752010-08-13 14:16:12 -07001495 SystemProperties.set("net.dns" + j++, dns);
Robert Greenwalt2034b912009-08-12 16:08:25 -07001496 }
Robert Greenwalt0ef68752010-08-13 14:16:12 -07001497 }
1498 for (int k=j ; k<mNumDnsEntries; k++) {
1499 if (DBG) Slog.d(TAG, "erasing net.dns" + k);
1500 SystemProperties.set("net.dns" + k, "");
1501 }
1502 mNumDnsEntries = j;
1503 } else {
1504 // set per-pid dns for attached secondary nets
1505 List pids = mNetRequestersPids[netType];
1506 for (int y=0; y< pids.size(); y++) {
1507 Integer pid = (Integer)pids.get(y);
1508 writePidDns(dnsList, pid.intValue());
The Android Open Source Project28527d22009-03-03 19:31:44 -08001509 }
1510 }
1511 }
Robert Greenwalt2034b912009-08-12 16:08:25 -07001512 bumpDns();
1513 }
1514
1515 private int getRestoreDefaultNetworkDelay() {
1516 String restoreDefaultNetworkDelayStr = SystemProperties.get(
1517 NETWORK_RESTORE_DELAY_PROP_NAME);
1518 if(restoreDefaultNetworkDelayStr != null &&
1519 restoreDefaultNetworkDelayStr.length() != 0) {
1520 try {
1521 return Integer.valueOf(restoreDefaultNetworkDelayStr);
1522 } catch (NumberFormatException e) {
1523 }
The Android Open Source Project28527d22009-03-03 19:31:44 -08001524 }
Robert Greenwalt2034b912009-08-12 16:08:25 -07001525 return RESTORE_DEFAULT_NETWORK_DELAY;
The Android Open Source Project28527d22009-03-03 19:31:44 -08001526 }
1527
1528 @Override
1529 protected void dump(FileDescriptor fd, PrintWriter pw, String[] args) {
Robert Greenwalt0659da32009-07-16 17:21:39 -07001530 if (mContext.checkCallingOrSelfPermission(
1531 android.Manifest.permission.DUMP)
The Android Open Source Project28527d22009-03-03 19:31:44 -08001532 != PackageManager.PERMISSION_GRANTED) {
Robert Greenwalt0659da32009-07-16 17:21:39 -07001533 pw.println("Permission Denial: can't dump ConnectivityService " +
1534 "from from pid=" + Binder.getCallingPid() + ", uid=" +
1535 Binder.getCallingUid());
The Android Open Source Project28527d22009-03-03 19:31:44 -08001536 return;
1537 }
The Android Open Source Project28527d22009-03-03 19:31:44 -08001538 pw.println();
1539 for (NetworkStateTracker nst : mNetTrackers) {
Robert Greenwalt3eeb6032009-12-21 18:24:07 -08001540 if (nst != null) {
1541 if (nst.getNetworkInfo().isConnected()) {
1542 pw.println("Active network: " + nst.getNetworkInfo().
1543 getTypeName());
1544 }
1545 pw.println(nst.getNetworkInfo());
1546 pw.println(nst);
1547 pw.println();
Robert Greenwalt2034b912009-08-12 16:08:25 -07001548 }
The Android Open Source Project28527d22009-03-03 19:31:44 -08001549 }
Robert Greenwalt3eeb6032009-12-21 18:24:07 -08001550
1551 pw.println("Network Requester Pids:");
1552 for (int net : mPriorityList) {
1553 String pidString = net + ": ";
1554 for (Object pid : mNetRequestersPids[net]) {
1555 pidString = pidString + pid.toString() + ", ";
1556 }
1557 pw.println(pidString);
1558 }
1559 pw.println();
1560
1561 pw.println("FeatureUsers:");
1562 for (Object requester : mFeatureUsers) {
1563 pw.println(requester.toString());
1564 }
1565 pw.println();
Robert Greenwalt8e87f122010-02-11 18:18:40 -08001566
1567 mTethering.dump(fd, pw, args);
Robert Greenwalt0e80be12010-09-20 14:35:25 -07001568
1569 if (mInetLog != null) {
1570 pw.println();
1571 pw.println("Inet condition reports:");
1572 for(int i = 0; i < mInetLog.size(); i++) {
1573 pw.println(mInetLog.get(i));
1574 }
1575 }
The Android Open Source Project28527d22009-03-03 19:31:44 -08001576 }
1577
Robert Greenwalt2034b912009-08-12 16:08:25 -07001578 // must be stateless - things change under us.
The Android Open Source Project28527d22009-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 Greenwalt12c44552009-12-07 11:33:18 -08001586 int type = info.getType();
1587 NetworkInfo.State state = info.getState();
Robert Greenwalt24e2d2b2010-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 Greenwalt12c44552009-12-07 11:33:18 -08001595 if (DBG) {
Robert Greenwalt24e2d2b2010-01-25 16:14:00 -08001596 // TODO - remove this after we validate the dropping doesn't break
1597 // anything
Joe Onoratoc2386bb2010-02-26 18:56:32 -08001598 Slog.d(TAG, "Dropping ConnectivityChange for " +
Robert Greenwalt2adbc7f2010-01-13 09:36:31 -08001599 info.getTypeName() + ": " +
Robert Greenwalt12c44552009-12-07 11:33:18 -08001600 state + "/" + info.getDetailedState());
1601 }
1602 return;
1603 }
1604 mNetAttributes[type].mLastState = state;
1605
Joe Onoratoc2386bb2010-02-26 18:56:32 -08001606 if (DBG) Slog.d(TAG, "ConnectivityChange for " +
Robert Greenwalt0659da32009-07-16 17:21:39 -07001607 info.getTypeName() + ": " +
Robert Greenwalt12c44552009-12-07 11:33:18 -08001608 state + "/" + info.getDetailedState());
The Android Open Source Project28527d22009-03-03 19:31:44 -08001609
1610 // Connectivity state changed:
1611 // [31-13] Reserved for future use
Robert Greenwalt0659da32009-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 Project28527d22009-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 Zongker2fc96232009-12-04 10:31:43 -08001620 EventLog.writeEvent(EventLogTags.CONNECTIVITY_STATE_CHANGED,
Robert Greenwalt0659da32009-07-16 17:21:39 -07001621 eventLogParam);
1622
1623 if (info.getDetailedState() ==
1624 NetworkInfo.DetailedState.FAILED) {
The Android Open Source Project28527d22009-03-03 19:31:44 -08001625 handleConnectionFailure(info);
Robert Greenwalt12c44552009-12-07 11:33:18 -08001626 } else if (state == NetworkInfo.State.DISCONNECTED) {
The Android Open Source Project28527d22009-03-03 19:31:44 -08001627 handleDisconnect(info);
Robert Greenwalt12c44552009-12-07 11:33:18 -08001628 } else if (state == NetworkInfo.State.SUSPENDED) {
The Android Open Source Project28527d22009-03-03 19:31:44 -08001629 // TODO: need to think this over.
Robert Greenwalt0659da32009-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 Project28527d22009-03-03 19:31:44 -08001636 handleDisconnect(info);
Robert Greenwalt12c44552009-12-07 11:33:18 -08001637 } else if (state == NetworkInfo.State.CONNECTED) {
The Android Open Source Project28527d22009-03-03 19:31:44 -08001638 handleConnect(info);
1639 }
The Android Open Source Project28527d22009-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 Greenwalt0659da32009-07-16 17:21:39 -07001646
The Android Open Source Project28527d22009-03-03 19:31:44 -08001647 case NetworkStateTracker.EVENT_NOTIFICATION_CHANGED:
Robert Greenwalt0659da32009-07-16 17:21:39 -07001648 handleNotificationChange(msg.arg1 == 1, msg.arg2,
1649 (Notification) msg.obj);
Robert Greenwalt0ef68752010-08-13 14:16:12 -07001650 break;
The Android Open Source Project28527d22009-03-03 19:31:44 -08001651
1652 case NetworkStateTracker.EVENT_CONFIGURATION_CHANGED:
Robert Greenwalt0ef68752010-08-13 14:16:12 -07001653 info = (NetworkInfo) msg.obj;
1654 type = info.getType();
1655 handleDnsConfigurationChange(type);
The Android Open Source Project28527d22009-03-03 19:31:44 -08001656 break;
1657
1658 case NetworkStateTracker.EVENT_ROAMING_CHANGED:
1659 // fill me in
1660 break;
1661
1662 case NetworkStateTracker.EVENT_NETWORK_SUBTYPE_CHANGED:
1663 // fill me in
1664 break;
Robert Greenwalt6a2db8a2010-09-23 10:05:56 -07001665 case EVENT_RESTORE_DEFAULT_NETWORK:
Robert Greenwaltaffc3a12009-09-27 17:27:04 -07001666 FeatureUser u = (FeatureUser)msg.obj;
1667 u.expire();
Robert Greenwalt2034b912009-08-12 16:08:25 -07001668 break;
Robert Greenwalt6a2db8a2010-09-23 10:05:56 -07001669 case EVENT_INET_CONDITION_CHANGE:
1670 {
1671 int netType = msg.arg1;
1672 int condition = msg.arg2;
1673 handleInetConditionChange(netType, condition);
Robert Greenwalt986c7412010-09-08 15:24:47 -07001674 break;
Robert Greenwalt6a2db8a2010-09-23 10:05:56 -07001675 }
1676 case EVENT_INET_CONDITION_HOLD_END:
1677 {
1678 int netType = msg.arg1;
1679 int sequence = msg.arg2;
1680 handleInetConditionHoldEnd(netType, sequence);
Robert Greenwalt986c7412010-09-08 15:24:47 -07001681 break;
Robert Greenwalt6a2db8a2010-09-23 10:05:56 -07001682 }
1683 case EVENT_SET_NETWORK_PREFERENCE:
1684 {
1685 int preference = msg.arg1;
1686 handleSetNetworkPreference(preference);
1687 break;
1688 }
1689 case EVENT_SET_BACKGROUND_DATA:
1690 {
1691 boolean enabled = (msg.arg1 == ENABLED);
1692 handleSetBackgroundData(enabled);
1693 break;
1694 }
1695 case EVENT_SET_MOBILE_DATA:
1696 {
1697 boolean enabled = (msg.arg1 == ENABLED);
1698 handleSetMobileData(enabled);
1699 break;
1700 }
The Android Open Source Project28527d22009-03-03 19:31:44 -08001701 }
1702 }
1703 }
Robert Greenwalt0c4828c2010-01-26 11:40:34 -08001704
1705 // javadoc from interface
Robert Greenwalt4283ded2010-03-02 17:25:02 -08001706 public int tether(String iface) {
Robert Greenwalt0c4828c2010-01-26 11:40:34 -08001707 enforceTetherChangePermission();
Robert Greenwalt4283ded2010-03-02 17:25:02 -08001708
1709 if (isTetheringSupported()) {
1710 return mTethering.tether(iface);
1711 } else {
1712 return ConnectivityManager.TETHER_ERROR_UNSUPPORTED;
1713 }
Robert Greenwalt0c4828c2010-01-26 11:40:34 -08001714 }
1715
1716 // javadoc from interface
Robert Greenwalt4283ded2010-03-02 17:25:02 -08001717 public int untether(String iface) {
Robert Greenwalt0c4828c2010-01-26 11:40:34 -08001718 enforceTetherChangePermission();
Robert Greenwalt4283ded2010-03-02 17:25:02 -08001719
1720 if (isTetheringSupported()) {
1721 return mTethering.untether(iface);
1722 } else {
1723 return ConnectivityManager.TETHER_ERROR_UNSUPPORTED;
1724 }
1725 }
1726
1727 // javadoc from interface
1728 public int getLastTetherError(String iface) {
1729 enforceTetherAccessPermission();
1730
1731 if (isTetheringSupported()) {
1732 return mTethering.getLastTetherError(iface);
1733 } else {
1734 return ConnectivityManager.TETHER_ERROR_UNSUPPORTED;
1735 }
Robert Greenwalt8e87f122010-02-11 18:18:40 -08001736 }
1737
1738 // TODO - proper iface API for selection by property, inspection, etc
1739 public String[] getTetherableUsbRegexs() {
1740 enforceTetherAccessPermission();
1741 if (isTetheringSupported()) {
1742 return mTethering.getTetherableUsbRegexs();
1743 } else {
1744 return new String[0];
1745 }
1746 }
1747
1748 public String[] getTetherableWifiRegexs() {
1749 enforceTetherAccessPermission();
1750 if (isTetheringSupported()) {
1751 return mTethering.getTetherableWifiRegexs();
1752 } else {
1753 return new String[0];
1754 }
Robert Greenwalt0c4828c2010-01-26 11:40:34 -08001755 }
1756
1757 // TODO - move iface listing, queries, etc to new module
1758 // javadoc from interface
1759 public String[] getTetherableIfaces() {
Robert Greenwalt8e87f122010-02-11 18:18:40 -08001760 enforceTetherAccessPermission();
Robert Greenwalt0c4828c2010-01-26 11:40:34 -08001761 return mTethering.getTetherableIfaces();
1762 }
1763
1764 public String[] getTetheredIfaces() {
Robert Greenwalt8e87f122010-02-11 18:18:40 -08001765 enforceTetherAccessPermission();
Robert Greenwalt0c4828c2010-01-26 11:40:34 -08001766 return mTethering.getTetheredIfaces();
1767 }
Robert Greenwalt8e87f122010-02-11 18:18:40 -08001768
Robert Greenwalt4283ded2010-03-02 17:25:02 -08001769 public String[] getTetheringErroredIfaces() {
1770 enforceTetherAccessPermission();
1771 return mTethering.getErroredIfaces();
1772 }
1773
Robert Greenwalt8e87f122010-02-11 18:18:40 -08001774 // if ro.tether.denied = true we default to no tethering
1775 // gservices could set the secure setting to 1 though to enable it on a build where it
1776 // had previously been turned off.
1777 public boolean isTetheringSupported() {
1778 enforceTetherAccessPermission();
1779 int defaultVal = (SystemProperties.get("ro.tether.denied").equals("true") ? 0 : 1);
Robert Greenwaltf1b66e12010-02-25 12:29:30 -08001780 boolean tetherEnabledInSettings = (Settings.Secure.getInt(mContext.getContentResolver(),
1781 Settings.Secure.TETHER_SUPPORTED, defaultVal) != 0);
1782 return tetherEnabledInSettings && mTetheringConfigValid;
Robert Greenwalt8e87f122010-02-11 18:18:40 -08001783 }
Robert Greenwalt986c7412010-09-08 15:24:47 -07001784
1785 // 100 percent is full good, 0 is full bad.
1786 public void reportInetCondition(int networkType, int percentage) {
1787 if (DBG) Slog.d(TAG, "reportNetworkCondition(" + networkType + ", " + percentage + ")");
1788 mContext.enforceCallingOrSelfPermission(
1789 android.Manifest.permission.STATUS_BAR,
1790 "ConnectivityService");
1791
Robert Greenwalt0e80be12010-09-20 14:35:25 -07001792 if (DBG) {
1793 int pid = getCallingPid();
1794 int uid = getCallingUid();
1795 String s = pid + "(" + uid + ") reports inet is " +
1796 (percentage > 50 ? "connected" : "disconnected") + " (" + percentage + ") on " +
1797 "network Type " + networkType + " at " + GregorianCalendar.getInstance().getTime();
1798 mInetLog.add(s);
1799 while(mInetLog.size() > INET_CONDITION_LOG_MAX_SIZE) {
1800 mInetLog.remove(0);
1801 }
1802 }
Robert Greenwalt986c7412010-09-08 15:24:47 -07001803 mHandler.sendMessage(mHandler.obtainMessage(
Robert Greenwalt6a2db8a2010-09-23 10:05:56 -07001804 EVENT_INET_CONDITION_CHANGE, networkType, percentage));
1805 }
1806
1807 private void handleInetConditionChange(int netType, int condition) {
1808 if (DBG) {
1809 Slog.d(TAG, "Inet connectivity change, net=" +
1810 netType + ", condition=" + condition +
1811 ",mActiveDefaultNetwork=" + mActiveDefaultNetwork);
1812 }
1813 if (mActiveDefaultNetwork == -1) {
1814 if (DBG) Slog.d(TAG, "no active default network - aborting");
1815 return;
1816 }
1817 if (mActiveDefaultNetwork != netType) {
1818 if (DBG) Slog.d(TAG, "given net not default - aborting");
1819 return;
1820 }
1821 mDefaultInetCondition = condition;
1822 int delay;
1823 if (mInetConditionChangeInFlight == false) {
1824 if (DBG) Slog.d(TAG, "starting a change hold");
1825 // setup a new hold to debounce this
1826 if (mDefaultInetCondition > 50) {
1827 delay = Settings.Secure.getInt(mContext.getContentResolver(),
1828 Settings.Secure.INET_CONDITION_DEBOUNCE_UP_DELAY, 500);
1829 } else {
1830 delay = Settings.Secure.getInt(mContext.getContentResolver(),
1831 Settings.Secure.INET_CONDITION_DEBOUNCE_DOWN_DELAY, 3000);
1832 }
1833 mInetConditionChangeInFlight = true;
1834 mHandler.sendMessageDelayed(mHandler.obtainMessage(EVENT_INET_CONDITION_HOLD_END,
1835 mActiveDefaultNetwork, mDefaultConnectionSequence), delay);
1836 } else {
1837 // we've set the new condition, when this hold ends that will get
1838 // picked up
1839 if (DBG) Slog.d(TAG, "currently in hold - not setting new end evt");
1840 }
1841 }
1842
1843 private void handleInetConditionHoldEnd(int netType, int sequence) {
1844 if (DBG) {
1845 Slog.d(TAG, "Inet hold end, net=" + netType +
1846 ", condition =" + mDefaultInetCondition +
1847 ", published condition =" + mDefaultInetConditionPublished);
1848 }
1849 mInetConditionChangeInFlight = false;
1850
1851 if (mActiveDefaultNetwork == -1) {
1852 if (DBG) Slog.d(TAG, "no active default network - aborting");
1853 return;
1854 }
1855 if (mDefaultConnectionSequence != sequence) {
1856 if (DBG) Slog.d(TAG, "event hold for obsolete network - aborting");
1857 return;
1858 }
1859 if (mDefaultInetConditionPublished == mDefaultInetCondition) {
1860 if (DBG) Slog.d(TAG, "no change in condition - aborting");
1861 return;
1862 }
1863 NetworkInfo networkInfo = mNetTrackers[mActiveDefaultNetwork].getNetworkInfo();
1864 if (networkInfo.isConnected() == false) {
1865 if (DBG) Slog.d(TAG, "default network not connected - aborting");
1866 return;
1867 }
1868 mDefaultInetConditionPublished = mDefaultInetCondition;
1869 sendInetConditionBroadcast(networkInfo);
1870 return;
Robert Greenwalt986c7412010-09-08 15:24:47 -07001871 }
The Android Open Source Project28527d22009-03-03 19:31:44 -08001872}