blob: e3d4c45cf62ad26ee2869e993cbcde3e701b2ed5 [file] [log] [blame]
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001/*
2 * Copyright (C) 2008 The Android Open Source Project
3 *
4 * Licensed under the Apache License, Version 2.0 (the "License");
5 * you may not use this file except in compliance with the License.
6 * You may obtain a copy of the License at
7 *
8 * http://www.apache.org/licenses/LICENSE-2.0
9 *
10 * Unless required by applicable law or agreed to in writing, software
11 * distributed under the License is distributed on an "AS IS" BASIS,
12 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13 * See the License for the specific language governing permissions and
14 * limitations under the License.
15 */
16
17package com.android.server;
18
Jeff Sharkeyd2a45872011-05-28 20:56:34 -070019import static android.Manifest.permission.READ_PHONE_STATE;
Jeff Sharkeyc006f1a2011-05-19 17:12:49 -070020import static android.Manifest.permission.UPDATE_DEVICE_STATS;
21import static android.net.ConnectivityManager.isNetworkTypeValid;
22import static android.net.NetworkPolicyManager.RULE_ALLOW_ALL;
23import static android.net.NetworkPolicyManager.RULE_REJECT_PAID;
24
Jaikumar Ganesh15c74392010-12-21 22:31:44 -080025import android.bluetooth.BluetoothTetheringDataTracker;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080026import android.content.ContentResolver;
27import android.content.Context;
28import android.content.Intent;
29import android.content.pm.PackageManager;
Robert Greenwalt434203a2010-10-11 16:00:27 -070030import android.database.ContentObserver;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080031import android.net.ConnectivityManager;
Robert Greenwaltda3d5e62010-12-06 13:56:24 -080032import android.net.DummyDataStateTracker;
Benoit Goby19970692010-12-22 14:29:40 -080033import android.net.EthernetDataTracker;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080034import android.net.IConnectivityManager;
Jeff Sharkeyc006f1a2011-05-19 17:12:49 -070035import android.net.INetworkPolicyListener;
36import android.net.INetworkPolicyManager;
Jaikumar Ganesh15c74392010-12-21 22:31:44 -080037import android.net.LinkProperties;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080038import android.net.MobileDataStateTracker;
Robert Greenwaltd55a6b42011-03-25 13:09:25 -070039import android.net.NetworkConfig;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080040import android.net.NetworkInfo;
Jeff Sharkeyc006f1a2011-05-19 17:12:49 -070041import android.net.NetworkInfo.DetailedState;
Jeff Sharkeyd2a45872011-05-28 20:56:34 -070042import android.net.NetworkState;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080043import android.net.NetworkStateTracker;
Robert Greenwalt585ac0f2010-08-27 09:24:29 -070044import android.net.NetworkUtils;
Robert Greenwalt434203a2010-10-11 16:00:27 -070045import android.net.Proxy;
46import android.net.ProxyProperties;
Robert Greenwaltaa70f102011-04-28 14:28:50 -070047import android.net.RouteInfo;
Hung-ying Tyan6b818de2011-01-19 16:48:38 +080048import android.net.vpn.VpnManager;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080049import android.net.wifi.WifiStateTracker;
50import android.os.Binder;
Mike Lockwoodda8bb742011-05-28 13:24:04 -040051import android.os.FileUtils;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080052import android.os.Handler;
Wink Savillebb08caf2010-09-02 19:23:52 -070053import android.os.HandlerThread;
Robert Greenwalt42acef32009-08-12 16:08:25 -070054import android.os.IBinder;
Chia-chi Yehc9338302011-05-11 16:35:13 -070055import android.os.INetworkManagementService;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080056import android.os.Looper;
57import android.os.Message;
Robert Greenwalt14f2ef42010-06-15 12:19:37 -070058import android.os.PowerManager;
Robert Greenwalt42acef32009-08-12 16:08:25 -070059import android.os.RemoteException;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080060import android.os.ServiceManager;
61import android.os.SystemProperties;
62import android.provider.Settings;
Robert Greenwalt42acef32009-08-12 16:08:25 -070063import android.text.TextUtils;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080064import android.util.EventLog;
Joe Onorato8a9b2202010-02-26 18:56:32 -080065import android.util.Slog;
Jeff Sharkeyc006f1a2011-05-19 17:12:49 -070066import android.util.SparseIntArray;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080067
Robert Greenwalt42acef32009-08-12 16:08:25 -070068import com.android.internal.telephony.Phone;
Robert Greenwaltd0e18ff2010-01-26 11:40:34 -080069import com.android.server.connectivity.Tethering;
Jeff Sharkeyd2a45872011-05-28 20:56:34 -070070import com.google.android.collect.Lists;
Robert Greenwaltd0e18ff2010-01-26 11:40:34 -080071
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080072import java.io.FileDescriptor;
Irfan Sheriffd649c122010-06-09 15:39:36 -070073import java.io.IOException;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080074import java.io.PrintWriter;
Robert Greenwalt47f69fe2010-06-15 15:43:39 -070075import java.net.InetAddress;
76import java.net.UnknownHostException;
Robert Greenwalt42acef32009-08-12 16:08:25 -070077import java.util.ArrayList;
Robert Greenwalt47f69fe2010-06-15 15:43:39 -070078import java.util.Collection;
Robert Greenwalt4e8dfef2010-09-20 14:35:25 -070079import java.util.GregorianCalendar;
Robert Greenwalt42acef32009-08-12 16:08:25 -070080import java.util.List;
Jeff Sharkeyc006f1a2011-05-19 17:12:49 -070081import java.util.concurrent.atomic.AtomicBoolean;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080082
83/**
84 * @hide
85 */
86public class ConnectivityService extends IConnectivityManager.Stub {
87
Robert Greenwaltba175a52010-10-05 19:12:26 -070088 private static final boolean DBG = true;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080089 private static final String TAG = "ConnectivityService";
90
Jeff Sharkeyc006f1a2011-05-19 17:12:49 -070091 private static final boolean LOGD_RULES = false;
92
Robert Greenwalt42acef32009-08-12 16:08:25 -070093 // how long to wait before switching back to a radio's default network
94 private static final int RESTORE_DEFAULT_NETWORK_DELAY = 1 * 60 * 1000;
95 // system property that can override the above value
96 private static final String NETWORK_RESTORE_DELAY_PROP_NAME =
97 "android.telephony.apn-restore";
98
Robert Greenwaltf43396c2011-05-06 17:10:53 -070099 // used in recursive route setting to add gateways for the host for which
100 // a host route was requested.
101 private static final int MAX_HOSTROUTE_CYCLE_COUNT = 10;
102
Robert Greenwaltd0e18ff2010-01-26 11:40:34 -0800103 private Tethering mTethering;
Robert Greenwaltc9d5fb72010-02-25 12:29:30 -0800104 private boolean mTetheringConfigValid = false;
Robert Greenwaltd0e18ff2010-01-26 11:40:34 -0800105
Jeff Sharkeyc006f1a2011-05-19 17:12:49 -0700106 /** Currently active network rules by UID. */
107 private SparseIntArray mUidRules = new SparseIntArray();
108
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800109 /**
110 * Sometimes we want to refer to the individual network state
111 * trackers separately, and sometimes we just want to treat them
112 * abstractly.
113 */
114 private NetworkStateTracker mNetTrackers[];
Robert Greenwalt42acef32009-08-12 16:08:25 -0700115
116 /**
117 * A per Net list of the PID's that requested access to the net
118 * used both as a refcount and for per-PID DNS selection
119 */
120 private List mNetRequestersPids[];
121
Irfan Sheriffa2a1b912010-06-07 09:03:04 -0700122 private WifiWatchdogService mWifiWatchdogService;
123
Robert Greenwalt42acef32009-08-12 16:08:25 -0700124 // priority order of the nettrackers
125 // (excluding dynamically set mNetworkPreference)
126 // TODO - move mNetworkTypePreference into this
127 private int[] mPriorityList;
128
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800129 private Context mContext;
130 private int mNetworkPreference;
Robert Greenwalt42acef32009-08-12 16:08:25 -0700131 private int mActiveDefaultNetwork = -1;
Robert Greenwaltd7085fc2010-09-08 15:24:47 -0700132 // 0 is full bad, 100 is full good
133 private int mDefaultInetCondition = 0;
134 private int mDefaultInetConditionPublished = 0;
135 private boolean mInetConditionChangeInFlight = false;
136 private int mDefaultConnectionSequence = 0;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800137
138 private int mNumDnsEntries;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800139
140 private boolean mTestMode;
Joe Onorato00092872010-09-01 21:18:22 -0700141 private static ConnectivityService sServiceInstance;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800142
Robert Greenwaltd825ea42010-12-29 16:15:02 -0800143 private AtomicBoolean mBackgroundDataEnabled = new AtomicBoolean(true);
144
Robert Greenwalt59b1a4e2011-05-10 15:05:02 -0700145 private INetworkManagementService mNetd;
Jeff Sharkeyc006f1a2011-05-19 17:12:49 -0700146 private INetworkPolicyManager mPolicyManager;
Robert Greenwalt59b1a4e2011-05-10 15:05:02 -0700147
Robert Greenwalt8dcc28b2010-09-23 10:05:56 -0700148 private static final int ENABLED = 1;
149 private static final int DISABLED = 0;
150
151 // Share the event space with NetworkStateTracker (which can't see this
152 // internal class but sends us events). If you change these, change
153 // NetworkStateTracker.java too.
154 private static final int MIN_NETWORK_STATE_TRACKER_EVENT = 1;
155 private static final int MAX_NETWORK_STATE_TRACKER_EVENT = 100;
156
157 /**
158 * used internally as a delayed event to make us switch back to the
159 * default network
160 */
161 private static final int EVENT_RESTORE_DEFAULT_NETWORK =
162 MAX_NETWORK_STATE_TRACKER_EVENT + 1;
163
164 /**
165 * used internally to change our mobile data enabled flag
166 */
167 private static final int EVENT_CHANGE_MOBILE_DATA_ENABLED =
168 MAX_NETWORK_STATE_TRACKER_EVENT + 2;
169
170 /**
171 * used internally to change our network preference setting
172 * arg1 = networkType to prefer
173 */
174 private static final int EVENT_SET_NETWORK_PREFERENCE =
175 MAX_NETWORK_STATE_TRACKER_EVENT + 3;
176
177 /**
178 * used internally to synchronize inet condition reports
179 * arg1 = networkType
180 * arg2 = condition (0 bad, 100 good)
181 */
182 private static final int EVENT_INET_CONDITION_CHANGE =
183 MAX_NETWORK_STATE_TRACKER_EVENT + 4;
184
185 /**
186 * used internally to mark the end of inet condition hold periods
187 * arg1 = networkType
188 */
189 private static final int EVENT_INET_CONDITION_HOLD_END =
190 MAX_NETWORK_STATE_TRACKER_EVENT + 5;
191
192 /**
193 * used internally to set the background data preference
194 * arg1 = TRUE for enabled, FALSE for disabled
195 */
196 private static final int EVENT_SET_BACKGROUND_DATA =
197 MAX_NETWORK_STATE_TRACKER_EVENT + 6;
198
199 /**
200 * used internally to set enable/disable cellular data
201 * arg1 = ENBALED or DISABLED
202 */
203 private static final int EVENT_SET_MOBILE_DATA =
204 MAX_NETWORK_STATE_TRACKER_EVENT + 7;
205
Robert Greenwaltf3331232010-09-24 14:32:21 -0700206 /**
207 * used internally to clear a wakelock when transitioning
208 * from one net to another
209 */
210 private static final int EVENT_CLEAR_NET_TRANSITION_WAKELOCK =
211 MAX_NETWORK_STATE_TRACKER_EVENT + 8;
212
Robert Greenwalt434203a2010-10-11 16:00:27 -0700213 /**
214 * used internally to reload global proxy settings
215 */
216 private static final int EVENT_APPLY_GLOBAL_HTTP_PROXY =
217 MAX_NETWORK_STATE_TRACKER_EVENT + 9;
218
Robert Greenwaltd55a6b42011-03-25 13:09:25 -0700219 /**
220 * used internally to set external dependency met/unmet
221 * arg1 = ENABLED (met) or DISABLED (unmet)
222 * arg2 = NetworkType
223 */
224 private static final int EVENT_SET_DEPENDENCY_MET =
225 MAX_NETWORK_STATE_TRACKER_EVENT + 10;
226
Robert Greenwalt42acef32009-08-12 16:08:25 -0700227 private Handler mHandler;
228
229 // list of DeathRecipients used to make sure features are turned off when
230 // a process dies
231 private List mFeatureUsers;
232
Mike Lockwood0f79b542009-08-14 14:18:49 -0400233 private boolean mSystemReady;
Dianne Hackborn1c633fc2009-12-08 19:45:14 -0800234 private Intent mInitialBroadcast;
Mike Lockwood0f79b542009-08-14 14:18:49 -0400235
Robert Greenwalt14f2ef42010-06-15 12:19:37 -0700236 private PowerManager.WakeLock mNetTransitionWakeLock;
237 private String mNetTransitionWakeLockCausedBy = "";
238 private int mNetTransitionWakeLockSerialNumber;
239 private int mNetTransitionWakeLockTimeout;
240
Robert Greenwalte90aa5e2010-09-01 11:34:05 -0700241 private InetAddress mDefaultDns;
242
Robert Greenwalt4e8dfef2010-09-20 14:35:25 -0700243 // used in DBG mode to track inet condition reports
244 private static final int INET_CONDITION_LOG_MAX_SIZE = 15;
245 private ArrayList mInetLog;
246
Robert Greenwalt434203a2010-10-11 16:00:27 -0700247 // track the current default http proxy - tell the world if we get a new one (real change)
248 private ProxyProperties mDefaultProxy = null;
249 // track the global proxy.
250 private ProxyProperties mGlobalProxy = null;
251 private final Object mGlobalProxyLock = new Object();
252
253 private SettingsObserver mSettingsObserver;
254
Robert Greenwaltd55a6b42011-03-25 13:09:25 -0700255 NetworkConfig[] mNetConfigs;
Robert Greenwalt5154ae762009-10-30 14:17:42 -0700256 int mNetworksDefined;
Robert Greenwalt42acef32009-08-12 16:08:25 -0700257
Robert Greenwalt511288a2009-12-07 11:33:18 -0800258 private static class RadioAttributes {
Robert Greenwalt42acef32009-08-12 16:08:25 -0700259 public int mSimultaneity;
260 public int mType;
261 public RadioAttributes(String init) {
262 String fragments[] = init.split(",");
Robert Greenwalt5154ae762009-10-30 14:17:42 -0700263 mType = Integer.parseInt(fragments[0]);
264 mSimultaneity = Integer.parseInt(fragments[1]);
Robert Greenwalt42acef32009-08-12 16:08:25 -0700265 }
266 }
267 RadioAttributes[] mRadioAttributes;
268
Jeff Sharkeyc006f1a2011-05-19 17:12:49 -0700269 public ConnectivityService(
270 Context context, INetworkManagementService netd, INetworkPolicyManager policyManager) {
Wink Savilleed9c02b2010-12-03 12:01:38 -0800271 if (DBG) log("ConnectivityService starting up");
Robert Greenwaltde8383c2010-01-14 17:47:58 -0800272
Wink Savillebb08caf2010-09-02 19:23:52 -0700273 HandlerThread handlerThread = new HandlerThread("ConnectivityServiceThread");
274 handlerThread.start();
275 mHandler = new MyHandler(handlerThread.getLooper());
276
Robert Greenwaltd825ea42010-12-29 16:15:02 -0800277 mBackgroundDataEnabled.set(Settings.Secure.getInt(context.getContentResolver(),
278 Settings.Secure.BACKGROUND_DATA, 1) == 1);
279
Robert Greenwaltde8383c2010-01-14 17:47:58 -0800280 // setup our unique device name
Robert Greenwalt733c6292010-12-06 09:30:17 -0800281 if (TextUtils.isEmpty(SystemProperties.get("net.hostname"))) {
282 String id = Settings.Secure.getString(context.getContentResolver(),
283 Settings.Secure.ANDROID_ID);
284 if (id != null && id.length() > 0) {
285 String name = new String("android_").concat(id);
286 SystemProperties.set("net.hostname", name);
287 }
Robert Greenwaltde8383c2010-01-14 17:47:58 -0800288 }
289
Robert Greenwalte90aa5e2010-09-01 11:34:05 -0700290 // read our default dns server ip
291 String dns = Settings.Secure.getString(context.getContentResolver(),
292 Settings.Secure.DEFAULT_DNS_SERVER);
293 if (dns == null || dns.length() == 0) {
294 dns = context.getResources().getString(
295 com.android.internal.R.string.config_default_dns_server);
296 }
297 try {
Robert Greenwalte5903732011-02-22 16:00:42 -0800298 mDefaultDns = NetworkUtils.numericToInetAddress(dns);
299 } catch (IllegalArgumentException e) {
Wink Savilleed9c02b2010-12-03 12:01:38 -0800300 loge("Error setting defaultDns using " + dns);
Robert Greenwalte90aa5e2010-09-01 11:34:05 -0700301 }
302
Jeff Sharkeyc006f1a2011-05-19 17:12:49 -0700303 mContext = checkNotNull(context, "missing Context");
304 mNetd = checkNotNull(netd, "missing INetworkManagementService");
305 mPolicyManager = checkNotNull(policyManager, "missing INetworkPolicyManager");
Robert Greenwalt14f2ef42010-06-15 12:19:37 -0700306
Jeff Sharkeyc006f1a2011-05-19 17:12:49 -0700307 try {
308 mPolicyManager.registerListener(mPolicyListener);
309 } catch (RemoteException e) {
310 // ouch, no rules updates means some processes may never get network
311 Slog.e(TAG, "unable to register INetworkPolicyListener", e);
312 }
313
314 final PowerManager powerManager = (PowerManager) context.getSystemService(
315 Context.POWER_SERVICE);
Robert Greenwalt14f2ef42010-06-15 12:19:37 -0700316 mNetTransitionWakeLock = powerManager.newWakeLock(PowerManager.PARTIAL_WAKE_LOCK, TAG);
317 mNetTransitionWakeLockTimeout = mContext.getResources().getInteger(
318 com.android.internal.R.integer.config_networkTransitionTimeout);
319
Robert Greenwalt42acef32009-08-12 16:08:25 -0700320 mNetTrackers = new NetworkStateTracker[
321 ConnectivityManager.MAX_NETWORK_TYPE+1];
Robert Greenwalt86e9e552009-07-16 17:21:39 -0700322
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800323 mNetworkPreference = getPersistedNetworkPreference();
Robert Greenwalt86e9e552009-07-16 17:21:39 -0700324
Robert Greenwalt5154ae762009-10-30 14:17:42 -0700325 mRadioAttributes = new RadioAttributes[ConnectivityManager.MAX_RADIO_TYPE+1];
Robert Greenwaltd55a6b42011-03-25 13:09:25 -0700326 mNetConfigs = new NetworkConfig[ConnectivityManager.MAX_NETWORK_TYPE+1];
Robert Greenwalt5154ae762009-10-30 14:17:42 -0700327
Robert Greenwalt42acef32009-08-12 16:08:25 -0700328 // Load device network attributes from resources
Robert Greenwalt42acef32009-08-12 16:08:25 -0700329 String[] raStrings = context.getResources().getStringArray(
330 com.android.internal.R.array.radioAttributes);
Robert Greenwalt5154ae762009-10-30 14:17:42 -0700331 for (String raString : raStrings) {
332 RadioAttributes r = new RadioAttributes(raString);
333 if (r.mType > ConnectivityManager.MAX_RADIO_TYPE) {
Wink Savilleed9c02b2010-12-03 12:01:38 -0800334 loge("Error in radioAttributes - ignoring attempt to define type " + r.mType);
Robert Greenwalt5154ae762009-10-30 14:17:42 -0700335 continue;
336 }
337 if (mRadioAttributes[r.mType] != null) {
Wink Savilleed9c02b2010-12-03 12:01:38 -0800338 loge("Error in radioAttributes - ignoring attempt to redefine type " +
Robert Greenwalt5154ae762009-10-30 14:17:42 -0700339 r.mType);
340 continue;
341 }
Robert Greenwalt42acef32009-08-12 16:08:25 -0700342 mRadioAttributes[r.mType] = r;
343 }
344
Robert Greenwalt5154ae762009-10-30 14:17:42 -0700345 String[] naStrings = context.getResources().getStringArray(
346 com.android.internal.R.array.networkAttributes);
347 for (String naString : naStrings) {
348 try {
Robert Greenwaltd55a6b42011-03-25 13:09:25 -0700349 NetworkConfig n = new NetworkConfig(naString);
Wink Saville975c8482011-04-07 14:23:45 -0700350 if (n.type > ConnectivityManager.MAX_NETWORK_TYPE) {
Wink Savilleed9c02b2010-12-03 12:01:38 -0800351 loge("Error in networkAttributes - ignoring attempt to define type " +
Wink Saville975c8482011-04-07 14:23:45 -0700352 n.type);
Robert Greenwalt5154ae762009-10-30 14:17:42 -0700353 continue;
Robert Greenwalt42acef32009-08-12 16:08:25 -0700354 }
Wink Saville975c8482011-04-07 14:23:45 -0700355 if (mNetConfigs[n.type] != null) {
Wink Savilleed9c02b2010-12-03 12:01:38 -0800356 loge("Error in networkAttributes - ignoring attempt to redefine type " +
Wink Saville975c8482011-04-07 14:23:45 -0700357 n.type);
Robert Greenwalt5154ae762009-10-30 14:17:42 -0700358 continue;
359 }
Wink Saville975c8482011-04-07 14:23:45 -0700360 if (mRadioAttributes[n.radio] == null) {
Wink Savilleed9c02b2010-12-03 12:01:38 -0800361 loge("Error in networkAttributes - ignoring attempt to use undefined " +
Wink Saville975c8482011-04-07 14:23:45 -0700362 "radio " + n.radio + " in network type " + n.type);
Robert Greenwalt5154ae762009-10-30 14:17:42 -0700363 continue;
364 }
Wink Saville975c8482011-04-07 14:23:45 -0700365 mNetConfigs[n.type] = n;
Robert Greenwalt5154ae762009-10-30 14:17:42 -0700366 mNetworksDefined++;
367 } catch(Exception e) {
368 // ignore it - leave the entry null
Robert Greenwalt42acef32009-08-12 16:08:25 -0700369 }
370 }
371
Robert Greenwalt5154ae762009-10-30 14:17:42 -0700372 // high priority first
373 mPriorityList = new int[mNetworksDefined];
374 {
375 int insertionPoint = mNetworksDefined-1;
376 int currentLowest = 0;
377 int nextLowest = 0;
378 while (insertionPoint > -1) {
Robert Greenwaltd55a6b42011-03-25 13:09:25 -0700379 for (NetworkConfig na : mNetConfigs) {
Robert Greenwalt5154ae762009-10-30 14:17:42 -0700380 if (na == null) continue;
Wink Saville975c8482011-04-07 14:23:45 -0700381 if (na.priority < currentLowest) continue;
382 if (na.priority > currentLowest) {
383 if (na.priority < nextLowest || nextLowest == 0) {
384 nextLowest = na.priority;
Robert Greenwalt5154ae762009-10-30 14:17:42 -0700385 }
386 continue;
387 }
Wink Saville975c8482011-04-07 14:23:45 -0700388 mPriorityList[insertionPoint--] = na.type;
Robert Greenwalt5154ae762009-10-30 14:17:42 -0700389 }
390 currentLowest = nextLowest;
391 nextLowest = 0;
392 }
393 }
394
395 mNetRequestersPids = new ArrayList[ConnectivityManager.MAX_NETWORK_TYPE+1];
396 for (int i : mPriorityList) {
Robert Greenwalt42acef32009-08-12 16:08:25 -0700397 mNetRequestersPids[i] = new ArrayList();
398 }
399
400 mFeatureUsers = new ArrayList();
401
Robert Greenwalt5154ae762009-10-30 14:17:42 -0700402 mNumDnsEntries = 0;
403
404 mTestMode = SystemProperties.get("cm.test.mode").equals("true")
405 && SystemProperties.get("ro.build.type").equals("eng");
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800406 /*
407 * Create the network state trackers for Wi-Fi and mobile
408 * data. Maybe this could be done with a factory class,
409 * but it's not clear that it's worth it, given that
410 * the number of different network types is not going
411 * to change very often.
412 */
Robert Greenwalt5154ae762009-10-30 14:17:42 -0700413 for (int netType : mPriorityList) {
Wink Saville975c8482011-04-07 14:23:45 -0700414 switch (mNetConfigs[netType].radio) {
Robert Greenwalt5154ae762009-10-30 14:17:42 -0700415 case ConnectivityManager.TYPE_WIFI:
Wink Savilleed9c02b2010-12-03 12:01:38 -0800416 if (DBG) log("Starting Wifi Service.");
Wink Savillec7a98342010-08-13 16:11:42 -0700417 WifiStateTracker wst = new WifiStateTracker();
Irfan Sheriff0d255342010-07-28 09:35:20 -0700418 WifiService wifiService = new WifiService(context);
Robert Greenwalt5154ae762009-10-30 14:17:42 -0700419 ServiceManager.addService(Context.WIFI_SERVICE, wifiService);
Irfan Sheriff0d255342010-07-28 09:35:20 -0700420 wifiService.checkAndStartWifi();
Robert Greenwalt5154ae762009-10-30 14:17:42 -0700421 mNetTrackers[ConnectivityManager.TYPE_WIFI] = wst;
Wink Savillec7a98342010-08-13 16:11:42 -0700422 wst.startMonitoring(context, mHandler);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800423
Irfan Sheriffa2a1b912010-06-07 09:03:04 -0700424 //TODO: as part of WWS refactor, create only when needed
Irfan Sheriff0d255342010-07-28 09:35:20 -0700425 mWifiWatchdogService = new WifiWatchdogService(context);
Irfan Sheriffa2a1b912010-06-07 09:03:04 -0700426
Robert Greenwalt5154ae762009-10-30 14:17:42 -0700427 break;
428 case ConnectivityManager.TYPE_MOBILE:
Wink Savillec7a98342010-08-13 16:11:42 -0700429 mNetTrackers[netType] = new MobileDataStateTracker(netType,
Wink Saville975c8482011-04-07 14:23:45 -0700430 mNetConfigs[netType].name);
Wink Savillec7a98342010-08-13 16:11:42 -0700431 mNetTrackers[netType].startMonitoring(context, mHandler);
Robert Greenwalt5154ae762009-10-30 14:17:42 -0700432 break;
Robert Greenwaltda3d5e62010-12-06 13:56:24 -0800433 case ConnectivityManager.TYPE_DUMMY:
434 mNetTrackers[netType] = new DummyDataStateTracker(netType,
Wink Saville975c8482011-04-07 14:23:45 -0700435 mNetConfigs[netType].name);
Robert Greenwaltda3d5e62010-12-06 13:56:24 -0800436 mNetTrackers[netType].startMonitoring(context, mHandler);
437 break;
Jaikumar Ganesh15c74392010-12-21 22:31:44 -0800438 case ConnectivityManager.TYPE_BLUETOOTH:
439 mNetTrackers[netType] = BluetoothTetheringDataTracker.getInstance();
440 mNetTrackers[netType].startMonitoring(context, mHandler);
441 break;
Benoit Goby19970692010-12-22 14:29:40 -0800442 case ConnectivityManager.TYPE_ETHERNET:
443 mNetTrackers[netType] = EthernetDataTracker.getInstance();
444 mNetTrackers[netType].startMonitoring(context, mHandler);
445 break;
Robert Greenwalt5154ae762009-10-30 14:17:42 -0700446 default:
Wink Savilleed9c02b2010-12-03 12:01:38 -0800447 loge("Trying to create a DataStateTracker for an unknown radio type " +
Wink Saville975c8482011-04-07 14:23:45 -0700448 mNetConfigs[netType].radio);
Robert Greenwalt5154ae762009-10-30 14:17:42 -0700449 continue;
450 }
451 }
Robert Greenwaltd0e18ff2010-01-26 11:40:34 -0800452
Chia-chi Yehc9338302011-05-11 16:35:13 -0700453 IBinder b = ServiceManager.getService(Context.NETWORKMANAGEMENT_SERVICE);
454 INetworkManagementService nmService = INetworkManagementService.Stub.asInterface(b);
455
456 mTethering = new Tethering(mContext, nmService, mHandler.getLooper());
Robert Greenwaltc9d5fb72010-02-25 12:29:30 -0800457 mTetheringConfigValid = (((mNetTrackers[ConnectivityManager.TYPE_MOBILE_DUN] != null) ||
458 !mTethering.isDunRequired()) &&
459 (mTethering.getTetherableUsbRegexs().length != 0 ||
Danica Chang6fdd0c62010-08-11 14:54:43 -0700460 mTethering.getTetherableWifiRegexs().length != 0 ||
461 mTethering.getTetherableBluetoothRegexs().length != 0) &&
Robert Greenwaltc9d5fb72010-02-25 12:29:30 -0800462 mTethering.getUpstreamIfaceRegexs().length != 0);
463
Chia-chi Yeh008ff392011-05-23 15:08:29 -0700464 try {
465 nmService.registerObserver(mTethering);
466 } catch (RemoteException e) {
467 loge("Error registering observer :" + e);
468 }
469
Robert Greenwalt4e8dfef2010-09-20 14:35:25 -0700470 if (DBG) {
471 mInetLog = new ArrayList();
472 }
Robert Greenwalt434203a2010-10-11 16:00:27 -0700473
474 mSettingsObserver = new SettingsObserver(mHandler, EVENT_APPLY_GLOBAL_HTTP_PROXY);
475 mSettingsObserver.observe(mContext);
Robert Greenwaltb7090d62010-12-02 11:31:00 -0800476
477 loadGlobalProxy();
Hung-ying Tyan6b818de2011-01-19 16:48:38 +0800478
479 VpnManager.startVpnService(context);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800480 }
481
Robert Greenwalt5154ae762009-10-30 14:17:42 -0700482
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800483 /**
Robert Greenwalt86e9e552009-07-16 17:21:39 -0700484 * Sets the preferred network.
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800485 * @param preference the new preference
486 */
Robert Greenwalt8dcc28b2010-09-23 10:05:56 -0700487 public void setNetworkPreference(int preference) {
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800488 enforceChangePermission();
Robert Greenwalt8dcc28b2010-09-23 10:05:56 -0700489
490 mHandler.sendMessage(mHandler.obtainMessage(EVENT_SET_NETWORK_PREFERENCE, preference, 0));
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800491 }
492
493 public int getNetworkPreference() {
494 enforceAccessPermission();
Robert Greenwalt8dcc28b2010-09-23 10:05:56 -0700495 int preference;
496 synchronized(this) {
497 preference = mNetworkPreference;
498 }
499 return preference;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800500 }
501
Robert Greenwalt8dcc28b2010-09-23 10:05:56 -0700502 private void handleSetNetworkPreference(int preference) {
503 if (ConnectivityManager.isNetworkTypeValid(preference) &&
Robert Greenwaltd55a6b42011-03-25 13:09:25 -0700504 mNetConfigs[preference] != null &&
505 mNetConfigs[preference].isDefault()) {
Robert Greenwalt8dcc28b2010-09-23 10:05:56 -0700506 if (mNetworkPreference != preference) {
507 final ContentResolver cr = mContext.getContentResolver();
508 Settings.Secure.putInt(cr, Settings.Secure.NETWORK_PREFERENCE, preference);
509 synchronized(this) {
510 mNetworkPreference = preference;
511 }
512 enforcePreference();
513 }
514 }
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800515 }
Robert Greenwalt86e9e552009-07-16 17:21:39 -0700516
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800517 private int getPersistedNetworkPreference() {
518 final ContentResolver cr = mContext.getContentResolver();
519
520 final int networkPrefSetting = Settings.Secure
521 .getInt(cr, Settings.Secure.NETWORK_PREFERENCE, -1);
522 if (networkPrefSetting != -1) {
523 return networkPrefSetting;
524 }
525
526 return ConnectivityManager.DEFAULT_NETWORK_PREFERENCE;
527 }
Robert Greenwalt86e9e552009-07-16 17:21:39 -0700528
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800529 /**
Robert Greenwalt86e9e552009-07-16 17:21:39 -0700530 * Make the state of network connectivity conform to the preference settings
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800531 * In this method, we only tear down a non-preferred network. Establishing
532 * a connection to the preferred network is taken care of when we handle
533 * the disconnect event from the non-preferred network
534 * (see {@link #handleDisconnect(NetworkInfo)}).
535 */
536 private void enforcePreference() {
Robert Greenwalt42acef32009-08-12 16:08:25 -0700537 if (mNetTrackers[mNetworkPreference].getNetworkInfo().isConnected())
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800538 return;
539
Robert Greenwalt42acef32009-08-12 16:08:25 -0700540 if (!mNetTrackers[mNetworkPreference].isAvailable())
541 return;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800542
Robert Greenwalt42acef32009-08-12 16:08:25 -0700543 for (int t=0; t <= ConnectivityManager.MAX_RADIO_TYPE; t++) {
Robert Greenwalt5154ae762009-10-30 14:17:42 -0700544 if (t != mNetworkPreference && mNetTrackers[t] != null &&
Robert Greenwalt42acef32009-08-12 16:08:25 -0700545 mNetTrackers[t].getNetworkInfo().isConnected()) {
Robert Greenwaltec9fe462009-08-20 15:25:14 -0700546 if (DBG) {
Wink Savilleed9c02b2010-12-03 12:01:38 -0800547 log("tearing down " + mNetTrackers[t].getNetworkInfo() +
Robert Greenwaltec9fe462009-08-20 15:25:14 -0700548 " in enforcePreference");
549 }
Robert Greenwalt42acef32009-08-12 16:08:25 -0700550 teardown(mNetTrackers[t]);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800551 }
552 }
553 }
554
555 private boolean teardown(NetworkStateTracker netTracker) {
556 if (netTracker.teardown()) {
557 netTracker.setTeardownRequested(true);
558 return true;
559 } else {
560 return false;
561 }
562 }
563
564 /**
Jeff Sharkeyc006f1a2011-05-19 17:12:49 -0700565 * Check if UID is blocked from using the given {@link NetworkInfo}.
566 */
567 private boolean isNetworkBlocked(NetworkInfo info, int uid) {
568 synchronized (mUidRules) {
Jeff Sharkeyd2a45872011-05-28 20:56:34 -0700569 // TODO: expand definition of "paid" network to cover tethered or
570 // paid hotspot use cases.
571 final boolean networkIsPaid = info.getType() != ConnectivityManager.TYPE_WIFI;
572 final int uidRules = mUidRules.get(uid, RULE_ALLOW_ALL);
573
574 if (networkIsPaid && (uidRules & RULE_REJECT_PAID) != 0) {
575 return true;
576 }
577
578 // no restrictive rules; network is visible
579 return false;
Jeff Sharkeyc006f1a2011-05-19 17:12:49 -0700580 }
581 }
582
583 /**
Jeff Sharkeyd2a45872011-05-28 20:56:34 -0700584 * Return a filtered version of the given {@link NetworkInfo}, potentially
585 * marked {@link DetailedState#BLOCKED} based on
586 * {@link #isNetworkBlocked(NetworkInfo, int)}.
Jeff Sharkeyc006f1a2011-05-19 17:12:49 -0700587 */
Jeff Sharkeyd2a45872011-05-28 20:56:34 -0700588 private NetworkInfo filterNetworkInfo(NetworkInfo info, int uid) {
589 if (isNetworkBlocked(info, uid)) {
590 // network is blocked; clone and override state
591 info = new NetworkInfo(info);
592 info.setDetailedState(DetailedState.BLOCKED, null, null);
Jeff Sharkeyc006f1a2011-05-19 17:12:49 -0700593 }
Jeff Sharkeyd2a45872011-05-28 20:56:34 -0700594 return info;
Jeff Sharkeyc006f1a2011-05-19 17:12:49 -0700595 }
596
597 /**
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800598 * Return NetworkInfo for the active (i.e., connected) network interface.
599 * It is assumed that at most one network is active at a time. If more
600 * than one is active, it is indeterminate which will be returned.
Robert Greenwalt86e9e552009-07-16 17:21:39 -0700601 * @return the info for the active network, or {@code null} if none is
602 * active
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800603 */
Jeff Sharkeyc006f1a2011-05-19 17:12:49 -0700604 @Override
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800605 public NetworkInfo getActiveNetworkInfo() {
Jeff Sharkeyc006f1a2011-05-19 17:12:49 -0700606 enforceAccessPermission();
607 final int uid = Binder.getCallingUid();
608 return getNetworkInfo(mActiveDefaultNetwork, uid);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800609 }
610
Jeff Sharkeyc006f1a2011-05-19 17:12:49 -0700611 @Override
612 public NetworkInfo getActiveNetworkInfoForUid(int uid) {
613 enforceConnectivityInternalPermission();
614 return getNetworkInfo(mActiveDefaultNetwork, uid);
615 }
616
617 @Override
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800618 public NetworkInfo getNetworkInfo(int networkType) {
619 enforceAccessPermission();
Jeff Sharkeyc006f1a2011-05-19 17:12:49 -0700620 final int uid = Binder.getCallingUid();
621 return getNetworkInfo(networkType, uid);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800622 }
623
Jeff Sharkeyc006f1a2011-05-19 17:12:49 -0700624 private NetworkInfo getNetworkInfo(int networkType, int uid) {
625 NetworkInfo info = null;
626 if (isNetworkTypeValid(networkType)) {
627 final NetworkStateTracker tracker = mNetTrackers[networkType];
628 if (tracker != null) {
Jeff Sharkeyd2a45872011-05-28 20:56:34 -0700629 info = filterNetworkInfo(tracker.getNetworkInfo(), uid);
Jeff Sharkeyc006f1a2011-05-19 17:12:49 -0700630 }
631 }
632 return info;
633 }
634
635 @Override
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800636 public NetworkInfo[] getAllNetworkInfo() {
637 enforceAccessPermission();
Jeff Sharkeyc006f1a2011-05-19 17:12:49 -0700638 final int uid = Binder.getCallingUid();
Jeff Sharkeyd2a45872011-05-28 20:56:34 -0700639 final ArrayList<NetworkInfo> result = Lists.newArrayList();
Jeff Sharkeyc006f1a2011-05-19 17:12:49 -0700640 synchronized (mUidRules) {
641 for (NetworkStateTracker tracker : mNetTrackers) {
642 if (tracker != null) {
Jeff Sharkeyd2a45872011-05-28 20:56:34 -0700643 result.add(filterNetworkInfo(tracker.getNetworkInfo(), uid));
Jeff Sharkeyc006f1a2011-05-19 17:12:49 -0700644 }
645 }
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800646 }
Jeff Sharkeyd2a45872011-05-28 20:56:34 -0700647 return result.toArray(new NetworkInfo[result.size()]);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800648 }
649
Robert Greenwaltd192dad2010-09-14 09:18:02 -0700650 /**
651 * Return LinkProperties for the active (i.e., connected) default
652 * network interface. It is assumed that at most one default network
653 * is active at a time. If more than one is active, it is indeterminate
654 * which will be returned.
655 * @return the ip properties for the active network, or {@code null} if
656 * none is active
657 */
Jeff Sharkeyc006f1a2011-05-19 17:12:49 -0700658 @Override
Robert Greenwaltd192dad2010-09-14 09:18:02 -0700659 public LinkProperties getActiveLinkProperties() {
Robert Greenwalt59911582011-05-20 12:23:41 -0700660 return getLinkProperties(mActiveDefaultNetwork);
Robert Greenwaltd192dad2010-09-14 09:18:02 -0700661 }
662
Jeff Sharkeyc006f1a2011-05-19 17:12:49 -0700663 @Override
Robert Greenwaltd192dad2010-09-14 09:18:02 -0700664 public LinkProperties getLinkProperties(int networkType) {
665 enforceAccessPermission();
Jeff Sharkeyc006f1a2011-05-19 17:12:49 -0700666 if (isNetworkTypeValid(networkType)) {
667 final NetworkStateTracker tracker = mNetTrackers[networkType];
668 if (tracker != null) {
669 return tracker.getLinkProperties();
670 }
Robert Greenwaltd192dad2010-09-14 09:18:02 -0700671 }
672 return null;
673 }
674
Jeff Sharkeyd2a45872011-05-28 20:56:34 -0700675 @Override
676 public NetworkState[] getAllNetworkState() {
677 enforceAccessPermission();
678 final int uid = Binder.getCallingUid();
679 final ArrayList<NetworkState> result = Lists.newArrayList();
680 synchronized (mUidRules) {
681 for (NetworkStateTracker tracker : mNetTrackers) {
682 if (tracker != null) {
683 final NetworkInfo info = filterNetworkInfo(tracker.getNetworkInfo(), uid);
684 result.add(new NetworkState(
685 info, tracker.getLinkProperties(), tracker.getLinkCapabilities()));
686 }
687 }
688 }
689 return result.toArray(new NetworkState[result.size()]);
690 }
691
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800692 public boolean setRadios(boolean turnOn) {
693 boolean result = true;
694 enforceChangePermission();
695 for (NetworkStateTracker t : mNetTrackers) {
Robert Greenwalt5154ae762009-10-30 14:17:42 -0700696 if (t != null) result = t.setRadio(turnOn) && result;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800697 }
698 return result;
699 }
700
701 public boolean setRadio(int netType, boolean turnOn) {
702 enforceChangePermission();
703 if (!ConnectivityManager.isNetworkTypeValid(netType)) {
704 return false;
705 }
706 NetworkStateTracker tracker = mNetTrackers[netType];
707 return tracker != null && tracker.setRadio(turnOn);
708 }
709
Robert Greenwalt9c75d4a2009-09-27 17:27:04 -0700710 /**
711 * Used to notice when the calling process dies so we can self-expire
712 *
713 * Also used to know if the process has cleaned up after itself when
714 * our auto-expire timer goes off. The timer has a link to an object.
715 *
716 */
Robert Greenwalt42acef32009-08-12 16:08:25 -0700717 private class FeatureUser implements IBinder.DeathRecipient {
718 int mNetworkType;
719 String mFeature;
720 IBinder mBinder;
721 int mPid;
722 int mUid;
Robert Greenwaltb9285352009-12-21 18:24:07 -0800723 long mCreateTime;
Robert Greenwalt42acef32009-08-12 16:08:25 -0700724
725 FeatureUser(int type, String feature, IBinder binder) {
726 super();
727 mNetworkType = type;
728 mFeature = feature;
729 mBinder = binder;
730 mPid = getCallingPid();
731 mUid = getCallingUid();
Robert Greenwaltb9285352009-12-21 18:24:07 -0800732 mCreateTime = System.currentTimeMillis();
Robert Greenwalt9c75d4a2009-09-27 17:27:04 -0700733
Robert Greenwalt42acef32009-08-12 16:08:25 -0700734 try {
735 mBinder.linkToDeath(this, 0);
736 } catch (RemoteException e) {
737 binderDied();
738 }
739 }
740
741 void unlinkDeathRecipient() {
742 mBinder.unlinkToDeath(this, 0);
743 }
744
745 public void binderDied() {
Wink Savilleed9c02b2010-12-03 12:01:38 -0800746 log("ConnectivityService FeatureUser binderDied(" +
Robert Greenwaltb9285352009-12-21 18:24:07 -0800747 mNetworkType + ", " + mFeature + ", " + mBinder + "), created " +
748 (System.currentTimeMillis() - mCreateTime) + " mSec ago");
Robert Greenwalt9c75d4a2009-09-27 17:27:04 -0700749 stopUsingNetworkFeature(this, false);
Robert Greenwalt42acef32009-08-12 16:08:25 -0700750 }
751
Robert Greenwalt9c75d4a2009-09-27 17:27:04 -0700752 public void expire() {
Wink Savilleed9c02b2010-12-03 12:01:38 -0800753 log("ConnectivityService FeatureUser expire(" +
Robert Greenwaltb9285352009-12-21 18:24:07 -0800754 mNetworkType + ", " + mFeature + ", " + mBinder +"), created " +
755 (System.currentTimeMillis() - mCreateTime) + " mSec ago");
Robert Greenwalt9c75d4a2009-09-27 17:27:04 -0700756 stopUsingNetworkFeature(this, false);
757 }
Robert Greenwaltb9285352009-12-21 18:24:07 -0800758
759 public String toString() {
760 return "FeatureUser("+mNetworkType+","+mFeature+","+mPid+","+mUid+"), created " +
761 (System.currentTimeMillis() - mCreateTime) + " mSec ago";
762 }
Robert Greenwalt42acef32009-08-12 16:08:25 -0700763 }
764
Robert Greenwalt9c75d4a2009-09-27 17:27:04 -0700765 // javadoc from interface
Robert Greenwalt42acef32009-08-12 16:08:25 -0700766 public int startUsingNetworkFeature(int networkType, String feature,
767 IBinder binder) {
768 if (DBG) {
Wink Savilleed9c02b2010-12-03 12:01:38 -0800769 log("startUsingNetworkFeature for net " + networkType + ": " + feature);
Robert Greenwalt42acef32009-08-12 16:08:25 -0700770 }
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800771 enforceChangePermission();
Robert Greenwalt5154ae762009-10-30 14:17:42 -0700772 if (!ConnectivityManager.isNetworkTypeValid(networkType) ||
Robert Greenwaltd55a6b42011-03-25 13:09:25 -0700773 mNetConfigs[networkType] == null) {
Robert Greenwalt42acef32009-08-12 16:08:25 -0700774 return Phone.APN_REQUEST_FAILED;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800775 }
Robert Greenwalt42acef32009-08-12 16:08:25 -0700776
Robert Greenwalt9c75d4a2009-09-27 17:27:04 -0700777 FeatureUser f = new FeatureUser(networkType, feature, binder);
Robert Greenwalt42acef32009-08-12 16:08:25 -0700778
779 // TODO - move this into the MobileDataStateTracker
780 int usedNetworkType = networkType;
781 if(networkType == ConnectivityManager.TYPE_MOBILE) {
Wink Saville2b8bcfe2011-02-24 17:58:51 -0800782 usedNetworkType = convertFeatureToNetworkType(feature);
783 if (usedNetworkType < 0) {
784 Slog.e(TAG, "Can't match any netTracker!");
785 usedNetworkType = networkType;
Robert Greenwalt42acef32009-08-12 16:08:25 -0700786 }
787 }
788 NetworkStateTracker network = mNetTrackers[usedNetworkType];
789 if (network != null) {
Robert Greenwalt0be1e982010-12-15 13:26:33 -0800790 Integer currentPid = new Integer(getCallingPid());
Robert Greenwalt42acef32009-08-12 16:08:25 -0700791 if (usedNetworkType != networkType) {
Robert Greenwalt42acef32009-08-12 16:08:25 -0700792 NetworkStateTracker radio = mNetTrackers[networkType];
793 NetworkInfo ni = network.getNetworkInfo();
794
795 if (ni.isAvailable() == false) {
Wink Savilleed9c02b2010-12-03 12:01:38 -0800796 if (DBG) log("special network not available");
Robert Greenwalte32e8122010-12-29 14:35:21 -0800797 if (!TextUtils.equals(feature,Phone.FEATURE_ENABLE_DUN_ALWAYS)) {
798 return Phone.APN_TYPE_NOT_AVAILABLE;
799 } else {
800 // else make the attempt anyway - probably giving REQUEST_STARTED below
801 }
Robert Greenwalt42acef32009-08-12 16:08:25 -0700802 }
803
Robert Greenwalt9c75d4a2009-09-27 17:27:04 -0700804 synchronized(this) {
805 mFeatureUsers.add(f);
806 if (!mNetRequestersPids[usedNetworkType].contains(currentPid)) {
807 // this gets used for per-pid dns when connected
808 mNetRequestersPids[usedNetworkType].add(currentPid);
809 }
Robert Greenwalt42acef32009-08-12 16:08:25 -0700810 }
Robert Greenwalt9c75d4a2009-09-27 17:27:04 -0700811
Robert Greenwaltf2102f72011-05-03 19:02:44 -0700812 int restoreTimer = getRestoreDefaultNetworkDelay(usedNetworkType);
813
814 if (restoreTimer >= 0) {
815 mHandler.sendMessageDelayed(
816 mHandler.obtainMessage(EVENT_RESTORE_DEFAULT_NETWORK, f), restoreTimer);
817 }
Robert Greenwalt42acef32009-08-12 16:08:25 -0700818
Robert Greenwalta64bf832009-08-19 20:19:33 -0700819 if ((ni.isConnectedOrConnecting() == true) &&
820 !network.isTeardownRequested()) {
Robert Greenwalt42acef32009-08-12 16:08:25 -0700821 if (ni.isConnected() == true) {
822 // add the pid-specific dns
Robert Greenwaltc76b8fa2010-07-23 15:46:26 -0700823 handleDnsConfigurationChange(networkType);
Wink Savilleed9c02b2010-12-03 12:01:38 -0800824 if (DBG) log("special network already active");
Robert Greenwalt42acef32009-08-12 16:08:25 -0700825 return Phone.APN_ALREADY_ACTIVE;
826 }
Wink Savilleed9c02b2010-12-03 12:01:38 -0800827 if (DBG) log("special network already connecting");
Robert Greenwalt42acef32009-08-12 16:08:25 -0700828 return Phone.APN_REQUEST_STARTED;
829 }
830
831 // check if the radio in play can make another contact
832 // assume if cannot for now
833
Wink Savilleed9c02b2010-12-03 12:01:38 -0800834 if (DBG) log("reconnecting to special network");
Robert Greenwalt42acef32009-08-12 16:08:25 -0700835 network.reconnect();
836 return Phone.APN_REQUEST_STARTED;
837 } else {
Robert Greenwalt0be1e982010-12-15 13:26:33 -0800838 // need to remember this unsupported request so we respond appropriately on stop
839 synchronized(this) {
840 mFeatureUsers.add(f);
841 if (!mNetRequestersPids[usedNetworkType].contains(currentPid)) {
842 // this gets used for per-pid dns when connected
843 mNetRequestersPids[usedNetworkType].add(currentPid);
844 }
845 }
Robert Greenwalt02648a42010-05-18 10:52:51 -0700846 return -1;
Robert Greenwalt42acef32009-08-12 16:08:25 -0700847 }
848 }
849 return Phone.APN_TYPE_NOT_AVAILABLE;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800850 }
851
Robert Greenwalt9c75d4a2009-09-27 17:27:04 -0700852 // javadoc from interface
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800853 public int stopUsingNetworkFeature(int networkType, String feature) {
Robert Greenwaltb8f16342009-10-06 17:52:40 -0700854 enforceChangePermission();
855
Robert Greenwalt9c75d4a2009-09-27 17:27:04 -0700856 int pid = getCallingPid();
857 int uid = getCallingUid();
858
859 FeatureUser u = null;
860 boolean found = false;
861
862 synchronized(this) {
863 for (int i = 0; i < mFeatureUsers.size() ; i++) {
864 u = (FeatureUser)mFeatureUsers.get(i);
865 if (uid == u.mUid && pid == u.mPid &&
866 networkType == u.mNetworkType &&
867 TextUtils.equals(feature, u.mFeature)) {
868 found = true;
869 break;
870 }
871 }
872 }
873 if (found && u != null) {
874 // stop regardless of how many other time this proc had called start
875 return stopUsingNetworkFeature(u, true);
876 } else {
877 // none found!
Wink Savilleed9c02b2010-12-03 12:01:38 -0800878 if (DBG) log("ignoring stopUsingNetworkFeature - not a live request");
Robert Greenwalt9c75d4a2009-09-27 17:27:04 -0700879 return 1;
880 }
Robert Greenwalt42acef32009-08-12 16:08:25 -0700881 }
882
Robert Greenwalt9c75d4a2009-09-27 17:27:04 -0700883 private int stopUsingNetworkFeature(FeatureUser u, boolean ignoreDups) {
884 int networkType = u.mNetworkType;
885 String feature = u.mFeature;
886 int pid = u.mPid;
887 int uid = u.mUid;
888
889 NetworkStateTracker tracker = null;
890 boolean callTeardown = false; // used to carry our decision outside of sync block
891
Robert Greenwalt42acef32009-08-12 16:08:25 -0700892 if (DBG) {
Wink Savilleed9c02b2010-12-03 12:01:38 -0800893 log("stopUsingNetworkFeature for net " + networkType +
Robert Greenwalt42acef32009-08-12 16:08:25 -0700894 ": " + feature);
895 }
Robert Greenwaltb8f16342009-10-06 17:52:40 -0700896
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800897 if (!ConnectivityManager.isNetworkTypeValid(networkType)) {
898 return -1;
899 }
Robert Greenwalt42acef32009-08-12 16:08:25 -0700900
Robert Greenwalt9c75d4a2009-09-27 17:27:04 -0700901 // need to link the mFeatureUsers list with the mNetRequestersPids state in this
902 // sync block
903 synchronized(this) {
904 // check if this process still has an outstanding start request
905 if (!mFeatureUsers.contains(u)) {
Wink Savilleed9c02b2010-12-03 12:01:38 -0800906 if (DBG) log("ignoring - this process has no outstanding requests");
Robert Greenwalt42acef32009-08-12 16:08:25 -0700907 return 1;
908 }
Robert Greenwalt9c75d4a2009-09-27 17:27:04 -0700909 u.unlinkDeathRecipient();
910 mFeatureUsers.remove(mFeatureUsers.indexOf(u));
911 // If we care about duplicate requests, check for that here.
912 //
913 // This is done to support the extension of a request - the app
914 // can request we start the network feature again and renew the
915 // auto-shutoff delay. Normal "stop" calls from the app though
916 // do not pay attention to duplicate requests - in effect the
917 // API does not refcount and a single stop will counter multiple starts.
918 if (ignoreDups == false) {
919 for (int i = 0; i < mFeatureUsers.size() ; i++) {
920 FeatureUser x = (FeatureUser)mFeatureUsers.get(i);
921 if (x.mUid == u.mUid && x.mPid == u.mPid &&
922 x.mNetworkType == u.mNetworkType &&
923 TextUtils.equals(x.mFeature, u.mFeature)) {
Wink Savilleed9c02b2010-12-03 12:01:38 -0800924 if (DBG) log("ignoring stopUsingNetworkFeature as dup is found");
Robert Greenwalt9c75d4a2009-09-27 17:27:04 -0700925 return 1;
926 }
927 }
Robert Greenwalt42acef32009-08-12 16:08:25 -0700928 }
Robert Greenwalt9c75d4a2009-09-27 17:27:04 -0700929
930 // TODO - move to MobileDataStateTracker
931 int usedNetworkType = networkType;
932 if (networkType == ConnectivityManager.TYPE_MOBILE) {
Wink Saville2b8bcfe2011-02-24 17:58:51 -0800933 usedNetworkType = convertFeatureToNetworkType(feature);
934 if (usedNetworkType < 0) {
935 usedNetworkType = networkType;
Robert Greenwalt9c75d4a2009-09-27 17:27:04 -0700936 }
937 }
938 tracker = mNetTrackers[usedNetworkType];
Robert Greenwalt5154ae762009-10-30 14:17:42 -0700939 if (tracker == null) {
Wink Savilleed9c02b2010-12-03 12:01:38 -0800940 if (DBG) log("ignoring - no known tracker for net type " + usedNetworkType);
Robert Greenwalt5154ae762009-10-30 14:17:42 -0700941 return -1;
942 }
943 if (usedNetworkType != networkType) {
Robert Greenwalt9c75d4a2009-09-27 17:27:04 -0700944 Integer currentPid = new Integer(pid);
Robert Greenwalt9c75d4a2009-09-27 17:27:04 -0700945 mNetRequestersPids[usedNetworkType].remove(currentPid);
Robert Greenwalt421c72b2009-12-17 14:54:59 -0800946 reassessPidDns(pid, true);
Robert Greenwalt9c75d4a2009-09-27 17:27:04 -0700947 if (mNetRequestersPids[usedNetworkType].size() != 0) {
Wink Savilleed9c02b2010-12-03 12:01:38 -0800948 if (DBG) log("not tearing down special network - " +
Robert Greenwalt9c75d4a2009-09-27 17:27:04 -0700949 "others still using it");
950 return 1;
951 }
952 callTeardown = true;
Robert Greenwalt19b9ab42011-01-10 11:58:31 -0800953 } else {
954 if (DBG) log("not a known feature - dropping");
Robert Greenwalt9c75d4a2009-09-27 17:27:04 -0700955 }
956 }
Wink Savilleed9c02b2010-12-03 12:01:38 -0800957 if (DBG) log("Doing network teardown");
Robert Greenwalt9c75d4a2009-09-27 17:27:04 -0700958 if (callTeardown) {
959 tracker.teardown();
Robert Greenwalt42acef32009-08-12 16:08:25 -0700960 return 1;
961 } else {
Robert Greenwalt02648a42010-05-18 10:52:51 -0700962 return -1;
Robert Greenwalt42acef32009-08-12 16:08:25 -0700963 }
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800964 }
965
966 /**
Robert Greenwalt585ac0f2010-08-27 09:24:29 -0700967 * @deprecated use requestRouteToHostAddress instead
968 *
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800969 * Ensure that a network route exists to deliver traffic to the specified
970 * host via the specified network interface.
Robert Greenwalt86e9e552009-07-16 17:21:39 -0700971 * @param networkType the type of the network over which traffic to the
972 * specified host is to be routed
973 * @param hostAddress the IP address of the host to which the route is
974 * desired
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800975 * @return {@code true} on success, {@code false} on failure
976 */
977 public boolean requestRouteToHost(int networkType, int hostAddress) {
Robert Greenwalt585ac0f2010-08-27 09:24:29 -0700978 InetAddress inetAddress = NetworkUtils.intToInetAddress(hostAddress);
979
980 if (inetAddress == null) {
981 return false;
982 }
983
984 return requestRouteToHostAddress(networkType, inetAddress.getAddress());
985 }
986
987 /**
988 * Ensure that a network route exists to deliver traffic to the specified
989 * host via the specified network interface.
990 * @param networkType the type of the network over which traffic to the
991 * specified host is to be routed
992 * @param hostAddress the IP address of the host to which the route is
993 * desired
994 * @return {@code true} on success, {@code false} on failure
995 */
996 public boolean requestRouteToHostAddress(int networkType, byte[] hostAddress) {
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800997 enforceChangePermission();
998 if (!ConnectivityManager.isNetworkTypeValid(networkType)) {
999 return false;
1000 }
1001 NetworkStateTracker tracker = mNetTrackers[networkType];
Robert Greenwalt8206ff32009-09-10 15:06:20 -07001002
Robert Greenwalt5154ae762009-10-30 14:17:42 -07001003 if (tracker == null || !tracker.getNetworkInfo().isConnected() ||
1004 tracker.isTeardownRequested()) {
Robert Greenwalt8206ff32009-09-10 15:06:20 -07001005 if (DBG) {
Wink Savilleed9c02b2010-12-03 12:01:38 -08001006 log("requestRouteToHostAddress on down network " +
Robert Greenwalt585ac0f2010-08-27 09:24:29 -07001007 "(" + networkType + ") - dropped");
Robert Greenwalt8206ff32009-09-10 15:06:20 -07001008 }
1009 return false;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001010 }
Robert Greenwalt47f69fe2010-06-15 15:43:39 -07001011 try {
Robert Greenwalt585ac0f2010-08-27 09:24:29 -07001012 InetAddress addr = InetAddress.getByAddress(hostAddress);
Robert Greenwaltf43396c2011-05-06 17:10:53 -07001013 return addHostRoute(tracker, addr, 0);
Robert Greenwalt47f69fe2010-06-15 15:43:39 -07001014 } catch (UnknownHostException e) {}
1015 return false;
Irfan Sheriffd649c122010-06-09 15:39:36 -07001016 }
1017
1018 /**
1019 * Ensure that a network route exists to deliver traffic to the specified
1020 * host via the mobile data network.
1021 * @param hostAddress the IP address of the host to which the route is desired,
1022 * in network byte order.
Robert Greenwalt47f69fe2010-06-15 15:43:39 -07001023 * TODO - deprecate
Irfan Sheriffd649c122010-06-09 15:39:36 -07001024 * @return {@code true} on success, {@code false} on failure
1025 */
Robert Greenwaltf43396c2011-05-06 17:10:53 -07001026 private boolean addHostRoute(NetworkStateTracker nt, InetAddress hostAddress, int cycleCount) {
Robert Greenwaltf43396c2011-05-06 17:10:53 -07001027 LinkProperties lp = nt.getLinkProperties();
1028 if ((lp == null) || (hostAddress == null)) return false;
Irfan Sheriffd649c122010-06-09 15:39:36 -07001029
Robert Greenwaltf43396c2011-05-06 17:10:53 -07001030 String interfaceName = lp.getInterfaceName();
Irfan Sheriffd649c122010-06-09 15:39:36 -07001031 if (DBG) {
Robert Greenwaltf43396c2011-05-06 17:10:53 -07001032 log("Requested host route to " + hostAddress + "(" + interfaceName + "), cycleCount=" +
1033 cycleCount);
Irfan Sheriffd649c122010-06-09 15:39:36 -07001034 }
Robert Greenwaltf43396c2011-05-06 17:10:53 -07001035 if (interfaceName == null) {
Wink Savilleed9c02b2010-12-03 12:01:38 -08001036 if (DBG) loge("addHostRoute failed due to null interface name");
Irfan Sheriffd649c122010-06-09 15:39:36 -07001037 return false;
1038 }
Robert Greenwaltf43396c2011-05-06 17:10:53 -07001039
1040 RouteInfo bestRoute = RouteInfo.selectBestRoute(lp.getRoutes(), hostAddress);
Robert Greenwalt59b1a4e2011-05-10 15:05:02 -07001041 InetAddress gatewayAddress = null;
Robert Greenwaltf43396c2011-05-06 17:10:53 -07001042 if (bestRoute != null) {
Robert Greenwalt59b1a4e2011-05-10 15:05:02 -07001043 gatewayAddress = bestRoute.getGateway();
Robert Greenwaltf43396c2011-05-06 17:10:53 -07001044 // if the best route is ourself, don't relf-reference, just add the host route
Robert Greenwalt59b1a4e2011-05-10 15:05:02 -07001045 if (hostAddress.equals(gatewayAddress)) gatewayAddress = null;
Robert Greenwaltf43396c2011-05-06 17:10:53 -07001046 }
Robert Greenwalt59b1a4e2011-05-10 15:05:02 -07001047 if (gatewayAddress != null) {
Robert Greenwaltf43396c2011-05-06 17:10:53 -07001048 if (cycleCount > MAX_HOSTROUTE_CYCLE_COUNT) {
1049 loge("Error adding hostroute - too much recursion");
1050 return false;
1051 }
Robert Greenwalt59b1a4e2011-05-10 15:05:02 -07001052 if (!addHostRoute(nt, gatewayAddress, cycleCount+1)) return false;
Robert Greenwaltf43396c2011-05-06 17:10:53 -07001053 }
Robert Greenwalt59b1a4e2011-05-10 15:05:02 -07001054
1055 RouteInfo route = RouteInfo.makeHostRoute(hostAddress, gatewayAddress);
1056
1057 try {
1058 mNetd.addRoute(interfaceName, route);
1059 return true;
1060 } catch (Exception ex) {
1061 return false;
1062 }
Robert Greenwaltf43396c2011-05-06 17:10:53 -07001063 }
1064
1065 // TODO support the removal of single host routes. Keep a ref count of them so we
1066 // aren't over-zealous
1067 private boolean removeHostRoute(NetworkStateTracker nt, InetAddress hostAddress) {
1068 return false;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001069 }
1070
1071 /**
1072 * @see ConnectivityManager#getBackgroundDataSetting()
1073 */
1074 public boolean getBackgroundDataSetting() {
Robert Greenwaltd825ea42010-12-29 16:15:02 -08001075 return mBackgroundDataEnabled.get();
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001076 }
Robert Greenwalt86e9e552009-07-16 17:21:39 -07001077
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001078 /**
1079 * @see ConnectivityManager#setBackgroundDataSetting(boolean)
1080 */
1081 public void setBackgroundDataSetting(boolean allowBackgroundDataUsage) {
1082 mContext.enforceCallingOrSelfPermission(
1083 android.Manifest.permission.CHANGE_BACKGROUND_DATA_SETTING,
1084 "ConnectivityService");
Robert Greenwalt86e9e552009-07-16 17:21:39 -07001085
Robert Greenwaltd825ea42010-12-29 16:15:02 -08001086 mBackgroundDataEnabled.set(allowBackgroundDataUsage);
1087
Robert Greenwalt8dcc28b2010-09-23 10:05:56 -07001088 mHandler.sendMessage(mHandler.obtainMessage(EVENT_SET_BACKGROUND_DATA,
1089 (allowBackgroundDataUsage ? ENABLED : DISABLED), 0));
1090 }
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001091
Robert Greenwalt8dcc28b2010-09-23 10:05:56 -07001092 private void handleSetBackgroundData(boolean enabled) {
Robert Greenwaltdb4afae2011-02-25 13:44:09 -08001093 Settings.Secure.putInt(mContext.getContentResolver(),
1094 Settings.Secure.BACKGROUND_DATA, enabled ? 1 : 0);
1095 Intent broadcast = new Intent(
1096 ConnectivityManager.ACTION_BACKGROUND_DATA_SETTING_CHANGED);
1097 mContext.sendBroadcast(broadcast);
Robert Greenwalt86e9e552009-07-16 17:21:39 -07001098 }
1099
Robert Greenwaltc03fa502010-02-23 18:58:05 -08001100 /**
1101 * @see ConnectivityManager#getMobileDataEnabled()
1102 */
1103 public boolean getMobileDataEnabled() {
Wink Savillee7982682010-12-07 10:31:02 -08001104 // TODO: This detail should probably be in DataConnectionTracker's
1105 // which is where we store the value and maybe make this
1106 // asynchronous.
Robert Greenwaltc03fa502010-02-23 18:58:05 -08001107 enforceAccessPermission();
1108 boolean retVal = Settings.Secure.getInt(mContext.getContentResolver(),
1109 Settings.Secure.MOBILE_DATA, 1) == 1;
Wink Savilleed9c02b2010-12-03 12:01:38 -08001110 if (DBG) log("getMobileDataEnabled returning " + retVal);
Robert Greenwaltc03fa502010-02-23 18:58:05 -08001111 return retVal;
1112 }
1113
Robert Greenwaltd55a6b42011-03-25 13:09:25 -07001114 public void setDataDependency(int networkType, boolean met) {
1115 enforceChangePermission();
1116 if (DBG) {
1117 log("setDataDependency(" + networkType + ", " + met + ")");
1118 }
1119 mHandler.sendMessage(mHandler.obtainMessage(EVENT_SET_DEPENDENCY_MET,
1120 (met ? ENABLED : DISABLED), networkType));
1121 }
1122
1123 private void handleSetDependencyMet(int networkType, boolean met) {
1124 if (mNetTrackers[networkType] != null) {
1125 if (DBG) {
1126 log("handleSetDependencyMet(" + networkType + ", " + met + ")");
1127 }
1128 mNetTrackers[networkType].setDependencyMet(met);
1129 }
1130 }
1131
Jeff Sharkeyc006f1a2011-05-19 17:12:49 -07001132 private INetworkPolicyListener mPolicyListener = new INetworkPolicyListener.Stub() {
1133 @Override
1134 public void onRulesChanged(int uid, int uidRules) {
1135 // only someone like NPMS should only be calling us
1136 // TODO: create permission for modifying data policy
1137 mContext.enforceCallingOrSelfPermission(UPDATE_DEVICE_STATS, TAG);
1138
1139 if (LOGD_RULES) {
1140 Slog.d(TAG, "onRulesChanged(uid=" + uid + ", uidRules=" + uidRules + ")");
1141 }
1142
1143 synchronized (mUidRules) {
1144 // skip update when we've already applied rules
1145 final int oldRules = mUidRules.get(uid, RULE_ALLOW_ALL);
1146 if (oldRules == uidRules) return;
1147
1148 mUidRules.put(uid, uidRules);
1149 }
1150
1151 // TODO: dispatch into NMS to push rules towards kernel module
1152 // TODO: notify UID when it has requested targeted updates
1153 }
1154 };
1155
Robert Greenwaltc03fa502010-02-23 18:58:05 -08001156 /**
1157 * @see ConnectivityManager#setMobileDataEnabled(boolean)
1158 */
Robert Greenwalt8dcc28b2010-09-23 10:05:56 -07001159 public void setMobileDataEnabled(boolean enabled) {
Robert Greenwaltc03fa502010-02-23 18:58:05 -08001160 enforceChangePermission();
Wink Savilleed9c02b2010-12-03 12:01:38 -08001161 if (DBG) log("setMobileDataEnabled(" + enabled + ")");
Robert Greenwaltc03fa502010-02-23 18:58:05 -08001162
Robert Greenwalt8dcc28b2010-09-23 10:05:56 -07001163 mHandler.sendMessage(mHandler.obtainMessage(EVENT_SET_MOBILE_DATA,
Robert Greenwaltd55a6b42011-03-25 13:09:25 -07001164 (enabled ? ENABLED : DISABLED), 0));
Robert Greenwalt8dcc28b2010-09-23 10:05:56 -07001165 }
1166
1167 private void handleSetMobileData(boolean enabled) {
Wink Savillee7982682010-12-07 10:31:02 -08001168 if (mNetTrackers[ConnectivityManager.TYPE_MOBILE] != null) {
1169 if (DBG) {
1170 Slog.d(TAG, mNetTrackers[ConnectivityManager.TYPE_MOBILE].toString() + enabled);
Robert Greenwaltc03fa502010-02-23 18:58:05 -08001171 }
Wink Savillee7982682010-12-07 10:31:02 -08001172 mNetTrackers[ConnectivityManager.TYPE_MOBILE].setDataEnable(enabled);
Robert Greenwaltc03fa502010-02-23 18:58:05 -08001173 }
1174 }
1175
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001176 private void enforceAccessPermission() {
Robert Greenwalt86e9e552009-07-16 17:21:39 -07001177 mContext.enforceCallingOrSelfPermission(
1178 android.Manifest.permission.ACCESS_NETWORK_STATE,
1179 "ConnectivityService");
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001180 }
1181
1182 private void enforceChangePermission() {
Robert Greenwalt86e9e552009-07-16 17:21:39 -07001183 mContext.enforceCallingOrSelfPermission(
1184 android.Manifest.permission.CHANGE_NETWORK_STATE,
1185 "ConnectivityService");
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001186 }
1187
Robert Greenwaltd0e18ff2010-01-26 11:40:34 -08001188 // TODO Make this a special check when it goes public
1189 private void enforceTetherChangePermission() {
1190 mContext.enforceCallingOrSelfPermission(
1191 android.Manifest.permission.CHANGE_NETWORK_STATE,
1192 "ConnectivityService");
1193 }
1194
Robert Greenwalt2a091d72010-02-11 18:18:40 -08001195 private void enforceTetherAccessPermission() {
1196 mContext.enforceCallingOrSelfPermission(
1197 android.Manifest.permission.ACCESS_NETWORK_STATE,
1198 "ConnectivityService");
1199 }
1200
Robert Greenwalt14f2ef42010-06-15 12:19:37 -07001201 private void enforceConnectivityInternalPermission() {
1202 mContext.enforceCallingOrSelfPermission(
1203 android.Manifest.permission.CONNECTIVITY_INTERNAL,
1204 "ConnectivityService");
1205 }
1206
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001207 /**
Robert Greenwalt86e9e552009-07-16 17:21:39 -07001208 * Handle a {@code DISCONNECTED} event. If this pertains to the non-active
1209 * network, we ignore it. If it is for the active network, we send out a
1210 * broadcast. But first, we check whether it might be possible to connect
1211 * to a different network.
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001212 * @param info the {@code NetworkInfo} for the network
1213 */
1214 private void handleDisconnect(NetworkInfo info) {
1215
Robert Greenwalt42acef32009-08-12 16:08:25 -07001216 int prevNetType = info.getType();
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001217
Robert Greenwalt42acef32009-08-12 16:08:25 -07001218 mNetTrackers[prevNetType].setTeardownRequested(false);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001219 /*
1220 * If the disconnected network is not the active one, then don't report
1221 * this as a loss of connectivity. What probably happened is that we're
1222 * getting the disconnect for a network that we explicitly disabled
1223 * in accordance with network preference policies.
1224 */
Robert Greenwaltd55a6b42011-03-25 13:09:25 -07001225 if (!mNetConfigs[prevNetType].isDefault()) {
Robert Greenwalt42acef32009-08-12 16:08:25 -07001226 List pids = mNetRequestersPids[prevNetType];
1227 for (int i = 0; i<pids.size(); i++) {
1228 Integer pid = (Integer)pids.get(i);
1229 // will remove them because the net's no longer connected
1230 // need to do this now as only now do we know the pids and
1231 // can properly null things that are no longer referenced.
1232 reassessPidDns(pid.intValue(), false);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001233 }
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001234 }
1235
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001236 Intent intent = new Intent(ConnectivityManager.CONNECTIVITY_ACTION);
1237 intent.putExtra(ConnectivityManager.EXTRA_NETWORK_INFO, info);
1238 if (info.isFailover()) {
1239 intent.putExtra(ConnectivityManager.EXTRA_IS_FAILOVER, true);
1240 info.setFailover(false);
1241 }
1242 if (info.getReason() != null) {
1243 intent.putExtra(ConnectivityManager.EXTRA_REASON, info.getReason());
1244 }
1245 if (info.getExtraInfo() != null) {
Robert Greenwalt86e9e552009-07-16 17:21:39 -07001246 intent.putExtra(ConnectivityManager.EXTRA_EXTRA_INFO,
1247 info.getExtraInfo());
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001248 }
Robert Greenwalt42acef32009-08-12 16:08:25 -07001249
Robert Greenwaltd55a6b42011-03-25 13:09:25 -07001250 if (mNetConfigs[prevNetType].isDefault()) {
Robert Greenwaltf21ef7d2011-01-11 13:56:33 -08001251 tryFailover(prevNetType);
1252 if (mActiveDefaultNetwork != -1) {
1253 NetworkInfo switchTo = mNetTrackers[mActiveDefaultNetwork].getNetworkInfo();
Robert Greenwaltcc4b4012010-01-25 17:54:29 -08001254 intent.putExtra(ConnectivityManager.EXTRA_OTHER_NETWORK_INFO, switchTo);
1255 } else {
Robert Greenwalt029be812010-09-20 18:01:43 -07001256 mDefaultInetConditionPublished = 0; // we're not connected anymore
Robert Greenwaltcc4b4012010-01-25 17:54:29 -08001257 intent.putExtra(ConnectivityManager.EXTRA_NO_CONNECTIVITY, true);
1258 }
Robert Greenwaltda03c4e2010-01-20 19:29:41 -08001259 }
Robert Greenwalt029be812010-09-20 18:01:43 -07001260 intent.putExtra(ConnectivityManager.EXTRA_INET_CONDITION, mDefaultInetConditionPublished);
Robert Greenwaltda03c4e2010-01-20 19:29:41 -08001261 // do this before we broadcast the change
Robert Greenwaltc76b8fa2010-07-23 15:46:26 -07001262 handleConnectivityChange(prevNetType);
Robert Greenwaltda03c4e2010-01-20 19:29:41 -08001263
1264 sendStickyBroadcast(intent);
1265 /*
1266 * If the failover network is already connected, then immediately send
1267 * out a followup broadcast indicating successful failover
1268 */
Robert Greenwaltf21ef7d2011-01-11 13:56:33 -08001269 if (mActiveDefaultNetwork != -1) {
1270 sendConnectedBroadcast(mNetTrackers[mActiveDefaultNetwork].getNetworkInfo());
Robert Greenwaltda03c4e2010-01-20 19:29:41 -08001271 }
1272 }
1273
Robert Greenwaltf21ef7d2011-01-11 13:56:33 -08001274 private void tryFailover(int prevNetType) {
Robert Greenwalt42acef32009-08-12 16:08:25 -07001275 /*
Robert Greenwaltbff90182011-01-06 15:41:07 -08001276 * If this is a default network, check if other defaults are available.
1277 * Try to reconnect on all available and let them hash it out when
1278 * more than one connects.
Robert Greenwalt42acef32009-08-12 16:08:25 -07001279 */
Robert Greenwaltd55a6b42011-03-25 13:09:25 -07001280 if (mNetConfigs[prevNetType].isDefault()) {
Robert Greenwalt42acef32009-08-12 16:08:25 -07001281 if (mActiveDefaultNetwork == prevNetType) {
1282 mActiveDefaultNetwork = -1;
1283 }
1284
Robert Greenwaltf21ef7d2011-01-11 13:56:33 -08001285 // don't signal a reconnect for anything lower or equal priority than our
1286 // current connected default
1287 // TODO - don't filter by priority now - nice optimization but risky
1288// int currentPriority = -1;
1289// if (mActiveDefaultNetwork != -1) {
Robert Greenwaltd55a6b42011-03-25 13:09:25 -07001290// currentPriority = mNetConfigs[mActiveDefaultNetwork].mPriority;
Robert Greenwaltf21ef7d2011-01-11 13:56:33 -08001291// }
Robert Greenwaltda03c4e2010-01-20 19:29:41 -08001292 for (int checkType=0; checkType <= ConnectivityManager.MAX_NETWORK_TYPE; checkType++) {
Robert Greenwalt5154ae762009-10-30 14:17:42 -07001293 if (checkType == prevNetType) continue;
Robert Greenwaltd55a6b42011-03-25 13:09:25 -07001294 if (mNetConfigs[checkType] == null) continue;
1295 if (!mNetConfigs[checkType].isDefault()) continue;
Wink Saville9f7a0b22011-01-26 15:43:49 -08001296
1297// Enabling the isAvailable() optimization caused mobile to not get
1298// selected if it was in the middle of error handling. Specifically
1299// a moble connection that took 30 seconds to complete the DEACTIVATE_DATA_CALL
1300// would not be available and we wouldn't get connected to anything.
1301// So removing the isAvailable() optimization below for now. TODO: This
1302// optimization should work and we need to investigate why it doesn't work.
1303// This could be related to how DEACTIVATE_DATA_CALL is reporting its
1304// complete before it is really complete.
1305// if (!mNetTrackers[checkType].isAvailable()) continue;
1306
Robert Greenwaltd55a6b42011-03-25 13:09:25 -07001307// if (currentPriority >= mNetConfigs[checkType].mPriority) continue;
Robert Greenwalt42acef32009-08-12 16:08:25 -07001308
Robert Greenwaltf21ef7d2011-01-11 13:56:33 -08001309 NetworkStateTracker checkTracker = mNetTrackers[checkType];
1310 NetworkInfo checkInfo = checkTracker.getNetworkInfo();
1311 if (!checkInfo.isConnectedOrConnecting() || checkTracker.isTeardownRequested()) {
1312 checkInfo.setFailover(true);
1313 checkTracker.reconnect();
Robert Greenwalt42acef32009-08-12 16:08:25 -07001314 }
Robert Greenwaltf21ef7d2011-01-11 13:56:33 -08001315 if (DBG) log("Attempting to switch to " + checkInfo.getTypeName());
Robert Greenwalt42acef32009-08-12 16:08:25 -07001316 }
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001317 }
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001318 }
1319
1320 private void sendConnectedBroadcast(NetworkInfo info) {
Robert Greenwalt1e9aac22010-09-15 17:36:33 -07001321 sendGeneralBroadcast(info, ConnectivityManager.CONNECTIVITY_ACTION);
1322 }
1323
1324 private void sendInetConditionBroadcast(NetworkInfo info) {
1325 sendGeneralBroadcast(info, ConnectivityManager.INET_CONDITION_ACTION);
1326 }
1327
1328 private void sendGeneralBroadcast(NetworkInfo info, String bcastType) {
1329 Intent intent = new Intent(bcastType);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001330 intent.putExtra(ConnectivityManager.EXTRA_NETWORK_INFO, info);
1331 if (info.isFailover()) {
1332 intent.putExtra(ConnectivityManager.EXTRA_IS_FAILOVER, true);
1333 info.setFailover(false);
1334 }
1335 if (info.getReason() != null) {
1336 intent.putExtra(ConnectivityManager.EXTRA_REASON, info.getReason());
1337 }
1338 if (info.getExtraInfo() != null) {
Robert Greenwalt86e9e552009-07-16 17:21:39 -07001339 intent.putExtra(ConnectivityManager.EXTRA_EXTRA_INFO,
1340 info.getExtraInfo());
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001341 }
Robert Greenwaltd7085fc2010-09-08 15:24:47 -07001342 intent.putExtra(ConnectivityManager.EXTRA_INET_CONDITION, mDefaultInetConditionPublished);
Mike Lockwood0f79b542009-08-14 14:18:49 -04001343 sendStickyBroadcast(intent);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001344 }
1345
1346 /**
1347 * Called when an attempt to fail over to another network has failed.
1348 * @param info the {@link NetworkInfo} for the failed network
1349 */
1350 private void handleConnectionFailure(NetworkInfo info) {
1351 mNetTrackers[info.getType()].setTeardownRequested(false);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001352
Robert Greenwalt42acef32009-08-12 16:08:25 -07001353 String reason = info.getReason();
1354 String extraInfo = info.getExtraInfo();
Robert Greenwalt86e9e552009-07-16 17:21:39 -07001355
Robert Greenwalt572172b2010-10-08 16:35:52 -07001356 String reasonText;
1357 if (reason == null) {
1358 reasonText = ".";
1359 } else {
1360 reasonText = " (" + reason + ").";
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001361 }
Wink Savilleed9c02b2010-12-03 12:01:38 -08001362 loge("Attempt to connect to " + info.getTypeName() + " failed" + reasonText);
Robert Greenwalt42acef32009-08-12 16:08:25 -07001363
1364 Intent intent = new Intent(ConnectivityManager.CONNECTIVITY_ACTION);
1365 intent.putExtra(ConnectivityManager.EXTRA_NETWORK_INFO, info);
1366 if (getActiveNetworkInfo() == null) {
1367 intent.putExtra(ConnectivityManager.EXTRA_NO_CONNECTIVITY, true);
1368 }
1369 if (reason != null) {
1370 intent.putExtra(ConnectivityManager.EXTRA_REASON, reason);
1371 }
1372 if (extraInfo != null) {
1373 intent.putExtra(ConnectivityManager.EXTRA_EXTRA_INFO, extraInfo);
1374 }
1375 if (info.isFailover()) {
1376 intent.putExtra(ConnectivityManager.EXTRA_IS_FAILOVER, true);
1377 info.setFailover(false);
1378 }
Robert Greenwaltda03c4e2010-01-20 19:29:41 -08001379
Robert Greenwaltd55a6b42011-03-25 13:09:25 -07001380 if (mNetConfigs[info.getType()].isDefault()) {
Robert Greenwaltf21ef7d2011-01-11 13:56:33 -08001381 tryFailover(info.getType());
1382 if (mActiveDefaultNetwork != -1) {
1383 NetworkInfo switchTo = mNetTrackers[mActiveDefaultNetwork].getNetworkInfo();
Robert Greenwaltcc4b4012010-01-25 17:54:29 -08001384 intent.putExtra(ConnectivityManager.EXTRA_OTHER_NETWORK_INFO, switchTo);
1385 } else {
Robert Greenwalt029be812010-09-20 18:01:43 -07001386 mDefaultInetConditionPublished = 0;
Robert Greenwaltcc4b4012010-01-25 17:54:29 -08001387 intent.putExtra(ConnectivityManager.EXTRA_NO_CONNECTIVITY, true);
1388 }
Robert Greenwaltda03c4e2010-01-20 19:29:41 -08001389 }
Robert Greenwaltcc4b4012010-01-25 17:54:29 -08001390
Robert Greenwalt029be812010-09-20 18:01:43 -07001391 intent.putExtra(ConnectivityManager.EXTRA_INET_CONDITION, mDefaultInetConditionPublished);
Mike Lockwood0f79b542009-08-14 14:18:49 -04001392 sendStickyBroadcast(intent);
Robert Greenwaltda03c4e2010-01-20 19:29:41 -08001393 /*
1394 * If the failover network is already connected, then immediately send
1395 * out a followup broadcast indicating successful failover
1396 */
Robert Greenwaltf21ef7d2011-01-11 13:56:33 -08001397 if (mActiveDefaultNetwork != -1) {
1398 sendConnectedBroadcast(mNetTrackers[mActiveDefaultNetwork].getNetworkInfo());
Robert Greenwaltda03c4e2010-01-20 19:29:41 -08001399 }
Mike Lockwood0f79b542009-08-14 14:18:49 -04001400 }
1401
1402 private void sendStickyBroadcast(Intent intent) {
1403 synchronized(this) {
Dianne Hackborn1c633fc2009-12-08 19:45:14 -08001404 if (!mSystemReady) {
1405 mInitialBroadcast = new Intent(intent);
Mike Lockwood0f79b542009-08-14 14:18:49 -04001406 }
Dianne Hackborn1c633fc2009-12-08 19:45:14 -08001407 intent.addFlags(Intent.FLAG_RECEIVER_REGISTERED_ONLY_BEFORE_BOOT);
1408 mContext.sendStickyBroadcast(intent);
Mike Lockwood0f79b542009-08-14 14:18:49 -04001409 }
1410 }
1411
1412 void systemReady() {
1413 synchronized(this) {
1414 mSystemReady = true;
Dianne Hackborn1c633fc2009-12-08 19:45:14 -08001415 if (mInitialBroadcast != null) {
1416 mContext.sendStickyBroadcast(mInitialBroadcast);
1417 mInitialBroadcast = null;
Mike Lockwood0f79b542009-08-14 14:18:49 -04001418 }
1419 }
Robert Greenwalt434203a2010-10-11 16:00:27 -07001420 // load the global proxy at startup
1421 mHandler.sendMessage(mHandler.obtainMessage(EVENT_APPLY_GLOBAL_HTTP_PROXY));
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001422 }
1423
1424 private void handleConnect(NetworkInfo info) {
Robert Greenwalt42acef32009-08-12 16:08:25 -07001425 int type = info.getType();
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001426
1427 // snapshot isFailover, because sendConnectedBroadcast() resets it
1428 boolean isFailover = info.isFailover();
Robert Greenwalt42acef32009-08-12 16:08:25 -07001429 NetworkStateTracker thisNet = mNetTrackers[type];
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001430
Robert Greenwalt42acef32009-08-12 16:08:25 -07001431 // if this is a default net and other default is running
1432 // kill the one not preferred
Robert Greenwaltd55a6b42011-03-25 13:09:25 -07001433 if (mNetConfigs[type].isDefault()) {
Robert Greenwalt42acef32009-08-12 16:08:25 -07001434 if (mActiveDefaultNetwork != -1 && mActiveDefaultNetwork != type) {
1435 if ((type != mNetworkPreference &&
Wink Saville975c8482011-04-07 14:23:45 -07001436 mNetConfigs[mActiveDefaultNetwork].priority >
1437 mNetConfigs[type].priority) ||
Robert Greenwalt42acef32009-08-12 16:08:25 -07001438 mNetworkPreference == mActiveDefaultNetwork) {
1439 // don't accept this one
Wink Savilleed9c02b2010-12-03 12:01:38 -08001440 if (DBG) {
1441 log("Not broadcasting CONNECT_ACTION " +
Robert Greenwalt42acef32009-08-12 16:08:25 -07001442 "to torn down network " + info.getTypeName());
Wink Savilleed9c02b2010-12-03 12:01:38 -08001443 }
Robert Greenwalt42acef32009-08-12 16:08:25 -07001444 teardown(thisNet);
1445 return;
1446 } else {
1447 // tear down the other
1448 NetworkStateTracker otherNet =
1449 mNetTrackers[mActiveDefaultNetwork];
Wink Savilleed9c02b2010-12-03 12:01:38 -08001450 if (DBG) {
1451 log("Policy requires " + otherNet.getNetworkInfo().getTypeName() +
Robert Greenwalt42acef32009-08-12 16:08:25 -07001452 " teardown");
Wink Savilleed9c02b2010-12-03 12:01:38 -08001453 }
Robert Greenwalt42acef32009-08-12 16:08:25 -07001454 if (!teardown(otherNet)) {
Wink Savilleed9c02b2010-12-03 12:01:38 -08001455 loge("Network declined teardown request");
Robert Greenwalt27725e82011-03-29 11:36:28 -07001456 teardown(thisNet);
Robert Greenwalt42acef32009-08-12 16:08:25 -07001457 return;
1458 }
Robert Greenwalt14f2ef42010-06-15 12:19:37 -07001459 }
1460 }
1461 synchronized (ConnectivityService.this) {
1462 // have a new default network, release the transition wakelock in a second
1463 // if it's held. The second pause is to allow apps to reconnect over the
1464 // new network
1465 if (mNetTransitionWakeLock.isHeld()) {
1466 mHandler.sendMessageDelayed(mHandler.obtainMessage(
Robert Greenwaltf3331232010-09-24 14:32:21 -07001467 EVENT_CLEAR_NET_TRANSITION_WAKELOCK,
Robert Greenwalt14f2ef42010-06-15 12:19:37 -07001468 mNetTransitionWakeLockSerialNumber, 0),
1469 1000);
Robert Greenwalt42acef32009-08-12 16:08:25 -07001470 }
1471 }
1472 mActiveDefaultNetwork = type;
Robert Greenwaltd7085fc2010-09-08 15:24:47 -07001473 // this will cause us to come up initially as unconnected and switching
1474 // to connected after our normal pause unless somebody reports us as reall
1475 // disconnected
1476 mDefaultInetConditionPublished = 0;
1477 mDefaultConnectionSequence++;
1478 mInetConditionChangeInFlight = false;
1479 // Don't do this - if we never sign in stay, grey
1480 //reportNetworkCondition(mActiveDefaultNetwork, 100);
Robert Greenwalt42acef32009-08-12 16:08:25 -07001481 }
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001482 thisNet.setTeardownRequested(false);
Irfan Sheriffd649c122010-06-09 15:39:36 -07001483 updateNetworkSettings(thisNet);
Robert Greenwaltc76b8fa2010-07-23 15:46:26 -07001484 handleConnectivityChange(type);
Robert Greenwalt42acef32009-08-12 16:08:25 -07001485 sendConnectedBroadcast(info);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001486 }
1487
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001488 /**
Robert Greenwaltc76b8fa2010-07-23 15:46:26 -07001489 * After a change in the connectivity state of a network. We're mainly
1490 * concerned with making sure that the list of DNS servers is set up
1491 * according to which networks are connected, and ensuring that the
1492 * right routing table entries exist.
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001493 */
Robert Greenwaltc76b8fa2010-07-23 15:46:26 -07001494 private void handleConnectivityChange(int netType) {
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001495 /*
Robert Greenwalt42acef32009-08-12 16:08:25 -07001496 * If a non-default network is enabled, add the host routes that
Robert Greenwaltc76b8fa2010-07-23 15:46:26 -07001497 * will allow it's DNS servers to be accessed.
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001498 */
Robert Greenwaltc76b8fa2010-07-23 15:46:26 -07001499 handleDnsConfigurationChange(netType);
Robert Greenwalt42acef32009-08-12 16:08:25 -07001500
Robert Greenwaltc76b8fa2010-07-23 15:46:26 -07001501 if (mNetTrackers[netType].getNetworkInfo().isConnected()) {
Robert Greenwaltd55a6b42011-03-25 13:09:25 -07001502 if (mNetConfigs[netType].isDefault()) {
Robert Greenwalt434203a2010-10-11 16:00:27 -07001503 handleApplyDefaultProxy(netType);
Robert Greenwaltc76b8fa2010-07-23 15:46:26 -07001504 addDefaultRoute(mNetTrackers[netType]);
Robert Greenwalt42acef32009-08-12 16:08:25 -07001505 } else {
Robert Greenwalt13ec4062011-04-01 10:51:22 -07001506 // many radios add a default route even when we don't want one.
1507 // remove the default route unless we need it for our active network
1508 if (mActiveDefaultNetwork != -1) {
1509 LinkProperties defaultLinkProperties =
1510 mNetTrackers[mActiveDefaultNetwork].getLinkProperties();
1511 LinkProperties newLinkProperties =
1512 mNetTrackers[netType].getLinkProperties();
1513 String defaultIface = defaultLinkProperties.getInterfaceName();
1514 if (defaultIface != null &&
1515 !defaultIface.equals(newLinkProperties.getInterfaceName())) {
1516 removeDefaultRoute(mNetTrackers[netType]);
1517 }
1518 }
Michael Jurka8fad7ee2011-03-30 19:54:08 -07001519 addPrivateDnsRoutes(mNetTrackers[netType]);
Robert Greenwaltc76b8fa2010-07-23 15:46:26 -07001520 }
Kazuhiro Ondo01758e82011-04-30 20:10:57 -05001521
1522 /** Notify TetheringService if interface name has been changed. */
1523 if (TextUtils.equals(mNetTrackers[netType].getNetworkInfo().getReason(),
1524 Phone.REASON_LINK_PROPERTIES_CHANGED)) {
1525 handleTetherIfaceChange(netType);
1526 }
Robert Greenwaltc76b8fa2010-07-23 15:46:26 -07001527 } else {
Robert Greenwaltd55a6b42011-03-25 13:09:25 -07001528 if (mNetConfigs[netType].isDefault()) {
Robert Greenwaltc76b8fa2010-07-23 15:46:26 -07001529 removeDefaultRoute(mNetTrackers[netType]);
1530 } else {
1531 removePrivateDnsRoutes(mNetTrackers[netType]);
Robert Greenwalt42acef32009-08-12 16:08:25 -07001532 }
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001533 }
1534 }
1535
Irfan Sheriffd649c122010-06-09 15:39:36 -07001536 private void addPrivateDnsRoutes(NetworkStateTracker nt) {
Irfan Sheriffd649c122010-06-09 15:39:36 -07001537 boolean privateDnsRouteSet = nt.isPrivateDnsRouteSet();
Robert Greenwalt37e65eb2010-08-30 10:56:47 -07001538 LinkProperties p = nt.getLinkProperties();
Robert Greenwalt47f69fe2010-06-15 15:43:39 -07001539 if (p == null) return;
1540 String interfaceName = p.getInterfaceName();
Irfan Sheriffd649c122010-06-09 15:39:36 -07001541
1542 if (DBG) {
Wink Savilleed9c02b2010-12-03 12:01:38 -08001543 log("addPrivateDnsRoutes for " + nt +
Irfan Sheriffd649c122010-06-09 15:39:36 -07001544 "(" + interfaceName + ") - mPrivateDnsRouteSet = " + privateDnsRouteSet);
1545 }
Irfan Sheriffd649c122010-06-09 15:39:36 -07001546 if (interfaceName != null && !privateDnsRouteSet) {
Robert Greenwalt47f69fe2010-06-15 15:43:39 -07001547 Collection<InetAddress> dnsList = p.getDnses();
1548 for (InetAddress dns : dnsList) {
Robert Greenwaltf43396c2011-05-06 17:10:53 -07001549 addHostRoute(nt, dns, 0);
Irfan Sheriffd649c122010-06-09 15:39:36 -07001550 }
1551 nt.privateDnsRouteSet(true);
1552 }
1553 }
1554
1555 private void removePrivateDnsRoutes(NetworkStateTracker nt) {
Robert Greenwalt37e65eb2010-08-30 10:56:47 -07001556 LinkProperties p = nt.getLinkProperties();
Robert Greenwalt47f69fe2010-06-15 15:43:39 -07001557 if (p == null) return;
1558 String interfaceName = p.getInterfaceName();
Irfan Sheriffd649c122010-06-09 15:39:36 -07001559 boolean privateDnsRouteSet = nt.isPrivateDnsRouteSet();
1560 if (interfaceName != null && privateDnsRouteSet) {
1561 if (DBG) {
Wink Savilleed9c02b2010-12-03 12:01:38 -08001562 log("removePrivateDnsRoutes for " + nt.getNetworkInfo().getTypeName() +
Irfan Sheriffd649c122010-06-09 15:39:36 -07001563 " (" + interfaceName + ")");
1564 }
Robert Greenwalt59b1a4e2011-05-10 15:05:02 -07001565
1566 Collection<InetAddress> dnsList = p.getDnses();
1567 for (InetAddress dns : dnsList) {
1568 if (DBG) log(" removing " + dns);
1569 RouteInfo route = RouteInfo.makeHostRoute(dns);
1570 try {
1571 mNetd.removeRoute(interfaceName, route);
1572 } catch (Exception ex) {
1573 loge("error (" + ex + ") removing dns route " + route);
1574 }
1575 }
Irfan Sheriffd649c122010-06-09 15:39:36 -07001576 nt.privateDnsRouteSet(false);
1577 }
1578 }
1579
Irfan Sheriffd649c122010-06-09 15:39:36 -07001580
1581 private void addDefaultRoute(NetworkStateTracker nt) {
Robert Greenwalt37e65eb2010-08-30 10:56:47 -07001582 LinkProperties p = nt.getLinkProperties();
Robert Greenwalt47f69fe2010-06-15 15:43:39 -07001583 if (p == null) return;
1584 String interfaceName = p.getInterfaceName();
Robert Greenwalt992564e2011-02-09 13:56:06 -08001585 if (TextUtils.isEmpty(interfaceName)) return;
Irfan Sheriffd649c122010-06-09 15:39:36 -07001586
Robert Greenwalt59b1a4e2011-05-10 15:05:02 -07001587 for (RouteInfo route : p.getRoutes()) {
Robert Greenwaltaa70f102011-04-28 14:28:50 -07001588 //TODO - handle non-default routes
1589 if (route.isDefaultRoute()) {
Robert Greenwalt59b1a4e2011-05-10 15:05:02 -07001590 if (DBG) log("adding default route " + route);
Robert Greenwaltaa70f102011-04-28 14:28:50 -07001591 InetAddress gateway = route.getGateway();
Robert Greenwalt59b1a4e2011-05-10 15:05:02 -07001592 if (addHostRoute(nt, gateway, 0)) {
1593 try {
1594 mNetd.addRoute(interfaceName, route);
1595 } catch (Exception e) {
1596 loge("error adding default route " + route);
1597 continue;
1598 }
Robert Greenwaltaa70f102011-04-28 14:28:50 -07001599 if (DBG) {
1600 NetworkInfo networkInfo = nt.getNetworkInfo();
1601 log("addDefaultRoute for " + networkInfo.getTypeName() +
1602 " (" + interfaceName + "), GatewayAddr=" +
1603 gateway.getHostAddress());
1604 }
Robert Greenwalt59b1a4e2011-05-10 15:05:02 -07001605 } else {
1606 loge("error adding host route for default route " + route);
Robert Greenwaltedcb4f92011-03-22 18:47:42 -07001607 }
Irfan Sheriffd649c122010-06-09 15:39:36 -07001608 }
Irfan Sheriffd649c122010-06-09 15:39:36 -07001609 }
1610 }
1611
1612
1613 public void removeDefaultRoute(NetworkStateTracker nt) {
Robert Greenwalt37e65eb2010-08-30 10:56:47 -07001614 LinkProperties p = nt.getLinkProperties();
Robert Greenwalt47f69fe2010-06-15 15:43:39 -07001615 if (p == null) return;
1616 String interfaceName = p.getInterfaceName();
Irfan Sheriffd649c122010-06-09 15:39:36 -07001617
Robert Greenwalt59b1a4e2011-05-10 15:05:02 -07001618 if (interfaceName == null) return;
1619
1620 for (RouteInfo route : p.getRoutes()) {
1621 //TODO - handle non-default routes
1622 if (route.isDefaultRoute()) {
1623 try {
1624 mNetd.removeRoute(interfaceName, route);
1625 } catch (Exception ex) {
1626 loge("error (" + ex + ") removing default route " + route);
1627 continue;
1628 }
Robert Greenwaltedcb4f92011-03-22 18:47:42 -07001629 if (DBG) {
1630 NetworkInfo networkInfo = nt.getNetworkInfo();
1631 log("removeDefaultRoute for " + networkInfo.getTypeName() + " (" +
1632 interfaceName + ")");
1633 }
Irfan Sheriffd649c122010-06-09 15:39:36 -07001634 }
Irfan Sheriffd649c122010-06-09 15:39:36 -07001635 }
1636 }
1637
1638 /**
1639 * Reads the network specific TCP buffer sizes from SystemProperties
1640 * net.tcp.buffersize.[default|wifi|umts|edge|gprs] and set them for system
1641 * wide use
1642 */
1643 public void updateNetworkSettings(NetworkStateTracker nt) {
1644 String key = nt.getTcpBufferSizesPropName();
1645 String bufferSizes = SystemProperties.get(key);
1646
1647 if (bufferSizes.length() == 0) {
Wink Savilleed9c02b2010-12-03 12:01:38 -08001648 loge(key + " not found in system properties. Using defaults");
Irfan Sheriffd649c122010-06-09 15:39:36 -07001649
1650 // Setting to default values so we won't be stuck to previous values
1651 key = "net.tcp.buffersize.default";
1652 bufferSizes = SystemProperties.get(key);
1653 }
1654
1655 // Set values in kernel
1656 if (bufferSizes.length() != 0) {
1657 if (DBG) {
Wink Savilleed9c02b2010-12-03 12:01:38 -08001658 log("Setting TCP values: [" + bufferSizes
Irfan Sheriffd649c122010-06-09 15:39:36 -07001659 + "] which comes from [" + key + "]");
1660 }
1661 setBufferSize(bufferSizes);
1662 }
1663 }
1664
1665 /**
1666 * Writes TCP buffer sizes to /sys/kernel/ipv4/tcp_[r/w]mem_[min/def/max]
1667 * which maps to /proc/sys/net/ipv4/tcp_rmem and tcpwmem
1668 *
1669 * @param bufferSizes in the format of "readMin, readInitial, readMax,
1670 * writeMin, writeInitial, writeMax"
1671 */
1672 private void setBufferSize(String bufferSizes) {
1673 try {
1674 String[] values = bufferSizes.split(",");
1675
1676 if (values.length == 6) {
1677 final String prefix = "/sys/kernel/ipv4/tcp_";
Mike Lockwoodda8bb742011-05-28 13:24:04 -04001678 FileUtils.stringToFile(prefix + "rmem_min", values[0]);
1679 FileUtils.stringToFile(prefix + "rmem_def", values[1]);
1680 FileUtils.stringToFile(prefix + "rmem_max", values[2]);
1681 FileUtils.stringToFile(prefix + "wmem_min", values[3]);
1682 FileUtils.stringToFile(prefix + "wmem_def", values[4]);
1683 FileUtils.stringToFile(prefix + "wmem_max", values[5]);
Irfan Sheriffd649c122010-06-09 15:39:36 -07001684 } else {
Wink Savilleed9c02b2010-12-03 12:01:38 -08001685 loge("Invalid buffersize string: " + bufferSizes);
Irfan Sheriffd649c122010-06-09 15:39:36 -07001686 }
1687 } catch (IOException e) {
Wink Savilleed9c02b2010-12-03 12:01:38 -08001688 loge("Can't set tcp buffer sizes:" + e);
Irfan Sheriffd649c122010-06-09 15:39:36 -07001689 }
1690 }
1691
Robert Greenwalt42acef32009-08-12 16:08:25 -07001692 /**
1693 * Adjust the per-process dns entries (net.dns<x>.<pid>) based
1694 * on the highest priority active net which this process requested.
1695 * If there aren't any, clear it out
1696 */
1697 private void reassessPidDns(int myPid, boolean doBump)
1698 {
Wink Savilleed9c02b2010-12-03 12:01:38 -08001699 if (DBG) log("reassessPidDns for pid " + myPid);
Robert Greenwalt42acef32009-08-12 16:08:25 -07001700 for(int i : mPriorityList) {
Robert Greenwaltd55a6b42011-03-25 13:09:25 -07001701 if (mNetConfigs[i].isDefault()) {
Robert Greenwalt42acef32009-08-12 16:08:25 -07001702 continue;
1703 }
1704 NetworkStateTracker nt = mNetTrackers[i];
Robert Greenwalt86e9e552009-07-16 17:21:39 -07001705 if (nt.getNetworkInfo().isConnected() &&
1706 !nt.isTeardownRequested()) {
Robert Greenwalt37e65eb2010-08-30 10:56:47 -07001707 LinkProperties p = nt.getLinkProperties();
Robert Greenwalt47f69fe2010-06-15 15:43:39 -07001708 if (p == null) continue;
Robert Greenwalt42acef32009-08-12 16:08:25 -07001709 List pids = mNetRequestersPids[i];
1710 for (int j=0; j<pids.size(); j++) {
1711 Integer pid = (Integer)pids.get(j);
1712 if (pid.intValue() == myPid) {
Robert Greenwalt47f69fe2010-06-15 15:43:39 -07001713 Collection<InetAddress> dnses = p.getDnses();
1714 writePidDns(dnses, myPid);
Robert Greenwalt42acef32009-08-12 16:08:25 -07001715 if (doBump) {
1716 bumpDns();
1717 }
1718 return;
1719 }
1720 }
1721 }
1722 }
1723 // nothing found - delete
1724 for (int i = 1; ; i++) {
1725 String prop = "net.dns" + i + "." + myPid;
1726 if (SystemProperties.get(prop).length() == 0) {
1727 if (doBump) {
1728 bumpDns();
1729 }
1730 return;
1731 }
1732 SystemProperties.set(prop, "");
1733 }
1734 }
1735
Robert Greenwalt10398722010-12-17 15:20:36 -08001736 // return true if results in a change
1737 private boolean writePidDns(Collection <InetAddress> dnses, int pid) {
Robert Greenwalt42acef32009-08-12 16:08:25 -07001738 int j = 1;
Robert Greenwalt10398722010-12-17 15:20:36 -08001739 boolean changed = false;
Robert Greenwalt47f69fe2010-06-15 15:43:39 -07001740 for (InetAddress dns : dnses) {
Robert Greenwalt10398722010-12-17 15:20:36 -08001741 String dnsString = dns.getHostAddress();
1742 if (changed || !dnsString.equals(SystemProperties.get("net.dns" + j + "." + pid))) {
1743 changed = true;
1744 SystemProperties.set("net.dns" + j++ + "." + pid, dns.getHostAddress());
1745 }
Robert Greenwalt42acef32009-08-12 16:08:25 -07001746 }
Robert Greenwalt10398722010-12-17 15:20:36 -08001747 return changed;
Robert Greenwalt42acef32009-08-12 16:08:25 -07001748 }
1749
1750 private void bumpDns() {
1751 /*
1752 * Bump the property that tells the name resolver library to reread
1753 * the DNS server list from the properties.
1754 */
1755 String propVal = SystemProperties.get("net.dnschange");
1756 int n = 0;
1757 if (propVal.length() != 0) {
1758 try {
1759 n = Integer.parseInt(propVal);
1760 } catch (NumberFormatException e) {}
1761 }
1762 SystemProperties.set("net.dnschange", "" + (n+1));
Robert Greenwalt03595d02010-11-02 14:08:23 -07001763 /*
1764 * Tell the VMs to toss their DNS caches
1765 */
1766 Intent intent = new Intent(Intent.ACTION_CLEAR_DNS_CACHE);
1767 intent.addFlags(Intent.FLAG_RECEIVER_REPLACE_PENDING);
Stan Chesnutt3d1db862011-01-05 17:14:03 -08001768 /*
1769 * Connectivity events can happen before boot has completed ...
1770 */
1771 intent.addFlags(Intent.FLAG_RECEIVER_REGISTERED_ONLY_BEFORE_BOOT);
Robert Greenwalt03595d02010-11-02 14:08:23 -07001772 mContext.sendBroadcast(intent);
Robert Greenwalt42acef32009-08-12 16:08:25 -07001773 }
1774
Robert Greenwaltc76b8fa2010-07-23 15:46:26 -07001775 private void handleDnsConfigurationChange(int netType) {
Robert Greenwalt42acef32009-08-12 16:08:25 -07001776 // add default net's dns entries
Robert Greenwaltc76b8fa2010-07-23 15:46:26 -07001777 NetworkStateTracker nt = mNetTrackers[netType];
1778 if (nt != null && nt.getNetworkInfo().isConnected() && !nt.isTeardownRequested()) {
Robert Greenwalt37e65eb2010-08-30 10:56:47 -07001779 LinkProperties p = nt.getLinkProperties();
Robert Greenwaltc76b8fa2010-07-23 15:46:26 -07001780 if (p == null) return;
1781 Collection<InetAddress> dnses = p.getDnses();
Robert Greenwalt10398722010-12-17 15:20:36 -08001782 boolean changed = false;
Robert Greenwaltd55a6b42011-03-25 13:09:25 -07001783 if (mNetConfigs[netType].isDefault()) {
Robert Greenwaltc76b8fa2010-07-23 15:46:26 -07001784 int j = 1;
Robert Greenwalte90aa5e2010-09-01 11:34:05 -07001785 if (dnses.size() == 0 && mDefaultDns != null) {
Robert Greenwalt10398722010-12-17 15:20:36 -08001786 String dnsString = mDefaultDns.getHostAddress();
1787 if (!dnsString.equals(SystemProperties.get("net.dns1"))) {
1788 if (DBG) {
1789 log("no dns provided - using " + dnsString);
1790 }
1791 changed = true;
1792 SystemProperties.set("net.dns1", dnsString);
Robert Greenwalt42acef32009-08-12 16:08:25 -07001793 }
Robert Greenwalte90aa5e2010-09-01 11:34:05 -07001794 j++;
1795 } else {
1796 for (InetAddress dns : dnses) {
Robert Greenwalt10398722010-12-17 15:20:36 -08001797 String dnsString = dns.getHostAddress();
1798 if (!changed && dnsString.equals(SystemProperties.get("net.dns" + j))) {
1799 j++;
1800 continue;
1801 }
Robert Greenwalte90aa5e2010-09-01 11:34:05 -07001802 if (DBG) {
Wink Savilleed9c02b2010-12-03 12:01:38 -08001803 log("adding dns " + dns + " for " +
Robert Greenwalte90aa5e2010-09-01 11:34:05 -07001804 nt.getNetworkInfo().getTypeName());
1805 }
Robert Greenwalt10398722010-12-17 15:20:36 -08001806 changed = true;
1807 SystemProperties.set("net.dns" + j++, dnsString);
Robert Greenwalte90aa5e2010-09-01 11:34:05 -07001808 }
Robert Greenwaltc76b8fa2010-07-23 15:46:26 -07001809 }
1810 for (int k=j ; k<mNumDnsEntries; k++) {
Robert Greenwalt10398722010-12-17 15:20:36 -08001811 if (changed || !TextUtils.isEmpty(SystemProperties.get("net.dns" + k))) {
1812 if (DBG) log("erasing net.dns" + k);
1813 changed = true;
1814 SystemProperties.set("net.dns" + k, "");
1815 }
Robert Greenwaltc76b8fa2010-07-23 15:46:26 -07001816 }
1817 mNumDnsEntries = j;
1818 } else {
1819 // set per-pid dns for attached secondary nets
1820 List pids = mNetRequestersPids[netType];
1821 for (int y=0; y< pids.size(); y++) {
1822 Integer pid = (Integer)pids.get(y);
Robert Greenwalt10398722010-12-17 15:20:36 -08001823 changed = writePidDns(dnses, pid.intValue());
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001824 }
1825 }
Robert Greenwalt10398722010-12-17 15:20:36 -08001826 if (changed) bumpDns();
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001827 }
Robert Greenwalt42acef32009-08-12 16:08:25 -07001828 }
1829
Robert Greenwaltf2102f72011-05-03 19:02:44 -07001830 private int getRestoreDefaultNetworkDelay(int networkType) {
Robert Greenwalt42acef32009-08-12 16:08:25 -07001831 String restoreDefaultNetworkDelayStr = SystemProperties.get(
1832 NETWORK_RESTORE_DELAY_PROP_NAME);
1833 if(restoreDefaultNetworkDelayStr != null &&
1834 restoreDefaultNetworkDelayStr.length() != 0) {
1835 try {
1836 return Integer.valueOf(restoreDefaultNetworkDelayStr);
1837 } catch (NumberFormatException e) {
1838 }
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001839 }
Robert Greenwaltf2102f72011-05-03 19:02:44 -07001840 // if the system property isn't set, use the value for the apn type
1841 int ret = RESTORE_DEFAULT_NETWORK_DELAY;
1842
1843 if ((networkType <= ConnectivityManager.MAX_NETWORK_TYPE) &&
1844 (mNetConfigs[networkType] != null)) {
1845 ret = mNetConfigs[networkType].restoreTime;
1846 }
1847 return ret;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001848 }
1849
1850 @Override
1851 protected void dump(FileDescriptor fd, PrintWriter pw, String[] args) {
Robert Greenwalt86e9e552009-07-16 17:21:39 -07001852 if (mContext.checkCallingOrSelfPermission(
1853 android.Manifest.permission.DUMP)
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001854 != PackageManager.PERMISSION_GRANTED) {
Robert Greenwalt86e9e552009-07-16 17:21:39 -07001855 pw.println("Permission Denial: can't dump ConnectivityService " +
1856 "from from pid=" + Binder.getCallingPid() + ", uid=" +
1857 Binder.getCallingUid());
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001858 return;
1859 }
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001860 pw.println();
1861 for (NetworkStateTracker nst : mNetTrackers) {
Robert Greenwaltb9285352009-12-21 18:24:07 -08001862 if (nst != null) {
1863 if (nst.getNetworkInfo().isConnected()) {
1864 pw.println("Active network: " + nst.getNetworkInfo().
1865 getTypeName());
1866 }
1867 pw.println(nst.getNetworkInfo());
1868 pw.println(nst);
1869 pw.println();
Robert Greenwalt42acef32009-08-12 16:08:25 -07001870 }
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001871 }
Robert Greenwaltb9285352009-12-21 18:24:07 -08001872
1873 pw.println("Network Requester Pids:");
1874 for (int net : mPriorityList) {
1875 String pidString = net + ": ";
1876 for (Object pid : mNetRequestersPids[net]) {
1877 pidString = pidString + pid.toString() + ", ";
1878 }
1879 pw.println(pidString);
1880 }
1881 pw.println();
1882
1883 pw.println("FeatureUsers:");
1884 for (Object requester : mFeatureUsers) {
1885 pw.println(requester.toString());
1886 }
1887 pw.println();
Robert Greenwalt2a091d72010-02-11 18:18:40 -08001888
Robert Greenwalt14f2ef42010-06-15 12:19:37 -07001889 synchronized (this) {
1890 pw.println("NetworkTranstionWakeLock is currently " +
1891 (mNetTransitionWakeLock.isHeld() ? "" : "not ") + "held.");
1892 pw.println("It was last requested for "+mNetTransitionWakeLockCausedBy);
1893 }
1894 pw.println();
1895
Robert Greenwalt2a091d72010-02-11 18:18:40 -08001896 mTethering.dump(fd, pw, args);
Robert Greenwalt4e8dfef2010-09-20 14:35:25 -07001897
1898 if (mInetLog != null) {
1899 pw.println();
1900 pw.println("Inet condition reports:");
1901 for(int i = 0; i < mInetLog.size(); i++) {
1902 pw.println(mInetLog.get(i));
1903 }
1904 }
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001905 }
1906
Robert Greenwalt42acef32009-08-12 16:08:25 -07001907 // must be stateless - things change under us.
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001908 private class MyHandler extends Handler {
Wink Savillebb08caf2010-09-02 19:23:52 -07001909 public MyHandler(Looper looper) {
1910 super(looper);
1911 }
1912
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001913 @Override
1914 public void handleMessage(Message msg) {
1915 NetworkInfo info;
1916 switch (msg.what) {
1917 case NetworkStateTracker.EVENT_STATE_CHANGED:
1918 info = (NetworkInfo) msg.obj;
Robert Greenwalt511288a2009-12-07 11:33:18 -08001919 int type = info.getType();
1920 NetworkInfo.State state = info.getState();
Robert Greenwalt511288a2009-12-07 11:33:18 -08001921
Wink Savilleed9c02b2010-12-03 12:01:38 -08001922 if (DBG) log("ConnectivityChange for " +
Robert Greenwalt86e9e552009-07-16 17:21:39 -07001923 info.getTypeName() + ": " +
Robert Greenwalt511288a2009-12-07 11:33:18 -08001924 state + "/" + info.getDetailedState());
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001925
1926 // Connectivity state changed:
1927 // [31-13] Reserved for future use
Robert Greenwalt86e9e552009-07-16 17:21:39 -07001928 // [12-9] Network subtype (for mobile network, as defined
1929 // by TelephonyManager)
1930 // [8-3] Detailed state ordinal (as defined by
1931 // NetworkInfo.DetailedState)
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001932 // [2-0] Network type (as defined by ConnectivityManager)
1933 int eventLogParam = (info.getType() & 0x7) |
1934 ((info.getDetailedState().ordinal() & 0x3f) << 3) |
1935 (info.getSubtype() << 9);
Doug Zongkerab5c49c2009-12-04 10:31:43 -08001936 EventLog.writeEvent(EventLogTags.CONNECTIVITY_STATE_CHANGED,
Robert Greenwalt86e9e552009-07-16 17:21:39 -07001937 eventLogParam);
1938
1939 if (info.getDetailedState() ==
1940 NetworkInfo.DetailedState.FAILED) {
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001941 handleConnectionFailure(info);
Robert Greenwalt511288a2009-12-07 11:33:18 -08001942 } else if (state == NetworkInfo.State.DISCONNECTED) {
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001943 handleDisconnect(info);
Robert Greenwalt511288a2009-12-07 11:33:18 -08001944 } else if (state == NetworkInfo.State.SUSPENDED) {
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001945 // TODO: need to think this over.
Robert Greenwalt86e9e552009-07-16 17:21:39 -07001946 // the logic here is, handle SUSPENDED the same as
1947 // DISCONNECTED. The only difference being we are
1948 // broadcasting an intent with NetworkInfo that's
1949 // suspended. This allows the applications an
1950 // opportunity to handle DISCONNECTED and SUSPENDED
1951 // differently, or not.
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001952 handleDisconnect(info);
Robert Greenwalt511288a2009-12-07 11:33:18 -08001953 } else if (state == NetworkInfo.State.CONNECTED) {
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001954 handleConnect(info);
1955 }
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001956 break;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001957 case NetworkStateTracker.EVENT_CONFIGURATION_CHANGED:
Robert Greenwaltc76b8fa2010-07-23 15:46:26 -07001958 info = (NetworkInfo) msg.obj;
Robert Greenwaltd55a6b42011-03-25 13:09:25 -07001959 handleConnectivityChange(info.getType());
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001960 break;
Robert Greenwaltf3331232010-09-24 14:32:21 -07001961 case EVENT_CLEAR_NET_TRANSITION_WAKELOCK:
Robert Greenwalt14f2ef42010-06-15 12:19:37 -07001962 String causedBy = null;
1963 synchronized (ConnectivityService.this) {
1964 if (msg.arg1 == mNetTransitionWakeLockSerialNumber &&
1965 mNetTransitionWakeLock.isHeld()) {
1966 mNetTransitionWakeLock.release();
1967 causedBy = mNetTransitionWakeLockCausedBy;
1968 }
1969 }
1970 if (causedBy != null) {
Wink Savilleed9c02b2010-12-03 12:01:38 -08001971 log("NetTransition Wakelock for " + causedBy + " released by timeout");
Robert Greenwalt14f2ef42010-06-15 12:19:37 -07001972 }
Robert Greenwalt057d5e92010-09-09 14:05:10 -07001973 break;
Robert Greenwalt8dcc28b2010-09-23 10:05:56 -07001974 case EVENT_RESTORE_DEFAULT_NETWORK:
Robert Greenwalt9c75d4a2009-09-27 17:27:04 -07001975 FeatureUser u = (FeatureUser)msg.obj;
1976 u.expire();
Robert Greenwaltd7085fc2010-09-08 15:24:47 -07001977 break;
Robert Greenwalt8dcc28b2010-09-23 10:05:56 -07001978 case EVENT_INET_CONDITION_CHANGE:
1979 {
1980 int netType = msg.arg1;
1981 int condition = msg.arg2;
1982 handleInetConditionChange(netType, condition);
Robert Greenwalt14f2ef42010-06-15 12:19:37 -07001983 break;
Robert Greenwalt8dcc28b2010-09-23 10:05:56 -07001984 }
1985 case EVENT_INET_CONDITION_HOLD_END:
1986 {
1987 int netType = msg.arg1;
1988 int sequence = msg.arg2;
1989 handleInetConditionHoldEnd(netType, sequence);
Robert Greenwaltd7085fc2010-09-08 15:24:47 -07001990 break;
Robert Greenwalt8dcc28b2010-09-23 10:05:56 -07001991 }
1992 case EVENT_SET_NETWORK_PREFERENCE:
1993 {
1994 int preference = msg.arg1;
1995 handleSetNetworkPreference(preference);
1996 break;
1997 }
1998 case EVENT_SET_BACKGROUND_DATA:
1999 {
2000 boolean enabled = (msg.arg1 == ENABLED);
2001 handleSetBackgroundData(enabled);
2002 break;
2003 }
2004 case EVENT_SET_MOBILE_DATA:
2005 {
2006 boolean enabled = (msg.arg1 == ENABLED);
2007 handleSetMobileData(enabled);
2008 break;
2009 }
Robert Greenwalt434203a2010-10-11 16:00:27 -07002010 case EVENT_APPLY_GLOBAL_HTTP_PROXY:
2011 {
2012 handleDeprecatedGlobalHttpProxy();
Robert Greenwaltd55a6b42011-03-25 13:09:25 -07002013 break;
2014 }
2015 case EVENT_SET_DEPENDENCY_MET:
2016 {
2017 boolean met = (msg.arg1 == ENABLED);
2018 handleSetDependencyMet(msg.arg2, met);
2019 break;
Robert Greenwalt434203a2010-10-11 16:00:27 -07002020 }
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002021 }
2022 }
2023 }
Robert Greenwaltd0e18ff2010-01-26 11:40:34 -08002024
2025 // javadoc from interface
Robert Greenwalt5a735062010-03-02 17:25:02 -08002026 public int tether(String iface) {
Robert Greenwaltd0e18ff2010-01-26 11:40:34 -08002027 enforceTetherChangePermission();
Robert Greenwalt5a735062010-03-02 17:25:02 -08002028
2029 if (isTetheringSupported()) {
2030 return mTethering.tether(iface);
2031 } else {
2032 return ConnectivityManager.TETHER_ERROR_UNSUPPORTED;
2033 }
Robert Greenwaltd0e18ff2010-01-26 11:40:34 -08002034 }
2035
2036 // javadoc from interface
Robert Greenwalt5a735062010-03-02 17:25:02 -08002037 public int untether(String iface) {
Robert Greenwaltd0e18ff2010-01-26 11:40:34 -08002038 enforceTetherChangePermission();
Robert Greenwalt5a735062010-03-02 17:25:02 -08002039
2040 if (isTetheringSupported()) {
2041 return mTethering.untether(iface);
2042 } else {
2043 return ConnectivityManager.TETHER_ERROR_UNSUPPORTED;
2044 }
2045 }
2046
2047 // javadoc from interface
2048 public int getLastTetherError(String iface) {
2049 enforceTetherAccessPermission();
2050
2051 if (isTetheringSupported()) {
2052 return mTethering.getLastTetherError(iface);
2053 } else {
2054 return ConnectivityManager.TETHER_ERROR_UNSUPPORTED;
2055 }
Robert Greenwalt2a091d72010-02-11 18:18:40 -08002056 }
2057
2058 // TODO - proper iface API for selection by property, inspection, etc
2059 public String[] getTetherableUsbRegexs() {
2060 enforceTetherAccessPermission();
2061 if (isTetheringSupported()) {
2062 return mTethering.getTetherableUsbRegexs();
2063 } else {
2064 return new String[0];
2065 }
2066 }
2067
2068 public String[] getTetherableWifiRegexs() {
2069 enforceTetherAccessPermission();
2070 if (isTetheringSupported()) {
2071 return mTethering.getTetherableWifiRegexs();
2072 } else {
2073 return new String[0];
2074 }
Robert Greenwaltd0e18ff2010-01-26 11:40:34 -08002075 }
2076
Danica Chang6fdd0c62010-08-11 14:54:43 -07002077 public String[] getTetherableBluetoothRegexs() {
2078 enforceTetherAccessPermission();
2079 if (isTetheringSupported()) {
2080 return mTethering.getTetherableBluetoothRegexs();
2081 } else {
2082 return new String[0];
2083 }
2084 }
2085
Robert Greenwaltd0e18ff2010-01-26 11:40:34 -08002086 // TODO - move iface listing, queries, etc to new module
2087 // javadoc from interface
2088 public String[] getTetherableIfaces() {
Robert Greenwalt2a091d72010-02-11 18:18:40 -08002089 enforceTetherAccessPermission();
Robert Greenwaltd0e18ff2010-01-26 11:40:34 -08002090 return mTethering.getTetherableIfaces();
2091 }
2092
2093 public String[] getTetheredIfaces() {
Robert Greenwalt2a091d72010-02-11 18:18:40 -08002094 enforceTetherAccessPermission();
Robert Greenwaltd0e18ff2010-01-26 11:40:34 -08002095 return mTethering.getTetheredIfaces();
2096 }
Robert Greenwalt2a091d72010-02-11 18:18:40 -08002097
Robert Greenwalt5a735062010-03-02 17:25:02 -08002098 public String[] getTetheringErroredIfaces() {
2099 enforceTetherAccessPermission();
2100 return mTethering.getErroredIfaces();
2101 }
2102
Robert Greenwalt2a091d72010-02-11 18:18:40 -08002103 // if ro.tether.denied = true we default to no tethering
2104 // gservices could set the secure setting to 1 though to enable it on a build where it
2105 // had previously been turned off.
2106 public boolean isTetheringSupported() {
2107 enforceTetherAccessPermission();
2108 int defaultVal = (SystemProperties.get("ro.tether.denied").equals("true") ? 0 : 1);
Robert Greenwaltc9d5fb72010-02-25 12:29:30 -08002109 boolean tetherEnabledInSettings = (Settings.Secure.getInt(mContext.getContentResolver(),
2110 Settings.Secure.TETHER_SUPPORTED, defaultVal) != 0);
2111 return tetherEnabledInSettings && mTetheringConfigValid;
Robert Greenwalt2a091d72010-02-11 18:18:40 -08002112 }
Robert Greenwalt14f2ef42010-06-15 12:19:37 -07002113
2114 // An API NetworkStateTrackers can call when they lose their network.
2115 // This will automatically be cleared after X seconds or a network becomes CONNECTED,
2116 // whichever happens first. The timer is started by the first caller and not
2117 // restarted by subsequent callers.
2118 public void requestNetworkTransitionWakelock(String forWhom) {
2119 enforceConnectivityInternalPermission();
2120 synchronized (this) {
2121 if (mNetTransitionWakeLock.isHeld()) return;
2122 mNetTransitionWakeLockSerialNumber++;
2123 mNetTransitionWakeLock.acquire();
2124 mNetTransitionWakeLockCausedBy = forWhom;
2125 }
2126 mHandler.sendMessageDelayed(mHandler.obtainMessage(
Robert Greenwaltf3331232010-09-24 14:32:21 -07002127 EVENT_CLEAR_NET_TRANSITION_WAKELOCK,
Robert Greenwalt14f2ef42010-06-15 12:19:37 -07002128 mNetTransitionWakeLockSerialNumber, 0),
2129 mNetTransitionWakeLockTimeout);
2130 return;
2131 }
Robert Greenwaltca4306c2010-09-09 13:15:32 -07002132
Robert Greenwaltd7085fc2010-09-08 15:24:47 -07002133 // 100 percent is full good, 0 is full bad.
2134 public void reportInetCondition(int networkType, int percentage) {
Wink Savilleed9c02b2010-12-03 12:01:38 -08002135 if (DBG) log("reportNetworkCondition(" + networkType + ", " + percentage + ")");
Robert Greenwaltd7085fc2010-09-08 15:24:47 -07002136 mContext.enforceCallingOrSelfPermission(
2137 android.Manifest.permission.STATUS_BAR,
2138 "ConnectivityService");
2139
Robert Greenwalt4e8dfef2010-09-20 14:35:25 -07002140 if (DBG) {
2141 int pid = getCallingPid();
2142 int uid = getCallingUid();
2143 String s = pid + "(" + uid + ") reports inet is " +
2144 (percentage > 50 ? "connected" : "disconnected") + " (" + percentage + ") on " +
2145 "network Type " + networkType + " at " + GregorianCalendar.getInstance().getTime();
2146 mInetLog.add(s);
2147 while(mInetLog.size() > INET_CONDITION_LOG_MAX_SIZE) {
2148 mInetLog.remove(0);
2149 }
2150 }
Robert Greenwaltd7085fc2010-09-08 15:24:47 -07002151 mHandler.sendMessage(mHandler.obtainMessage(
Robert Greenwalt8dcc28b2010-09-23 10:05:56 -07002152 EVENT_INET_CONDITION_CHANGE, networkType, percentage));
2153 }
2154
2155 private void handleInetConditionChange(int netType, int condition) {
2156 if (DBG) {
Wink Savilleed9c02b2010-12-03 12:01:38 -08002157 log("Inet connectivity change, net=" +
Robert Greenwalt8dcc28b2010-09-23 10:05:56 -07002158 netType + ", condition=" + condition +
2159 ",mActiveDefaultNetwork=" + mActiveDefaultNetwork);
2160 }
2161 if (mActiveDefaultNetwork == -1) {
Wink Savilleed9c02b2010-12-03 12:01:38 -08002162 if (DBG) log("no active default network - aborting");
Robert Greenwalt8dcc28b2010-09-23 10:05:56 -07002163 return;
2164 }
2165 if (mActiveDefaultNetwork != netType) {
Wink Savilleed9c02b2010-12-03 12:01:38 -08002166 if (DBG) log("given net not default - aborting");
Robert Greenwalt8dcc28b2010-09-23 10:05:56 -07002167 return;
2168 }
2169 mDefaultInetCondition = condition;
2170 int delay;
2171 if (mInetConditionChangeInFlight == false) {
Wink Savilleed9c02b2010-12-03 12:01:38 -08002172 if (DBG) log("starting a change hold");
Robert Greenwalt8dcc28b2010-09-23 10:05:56 -07002173 // setup a new hold to debounce this
2174 if (mDefaultInetCondition > 50) {
2175 delay = Settings.Secure.getInt(mContext.getContentResolver(),
2176 Settings.Secure.INET_CONDITION_DEBOUNCE_UP_DELAY, 500);
2177 } else {
2178 delay = Settings.Secure.getInt(mContext.getContentResolver(),
2179 Settings.Secure.INET_CONDITION_DEBOUNCE_DOWN_DELAY, 3000);
2180 }
2181 mInetConditionChangeInFlight = true;
2182 mHandler.sendMessageDelayed(mHandler.obtainMessage(EVENT_INET_CONDITION_HOLD_END,
2183 mActiveDefaultNetwork, mDefaultConnectionSequence), delay);
2184 } else {
2185 // we've set the new condition, when this hold ends that will get
2186 // picked up
Wink Savilleed9c02b2010-12-03 12:01:38 -08002187 if (DBG) log("currently in hold - not setting new end evt");
Robert Greenwalt8dcc28b2010-09-23 10:05:56 -07002188 }
2189 }
2190
2191 private void handleInetConditionHoldEnd(int netType, int sequence) {
2192 if (DBG) {
Wink Savilleed9c02b2010-12-03 12:01:38 -08002193 log("Inet hold end, net=" + netType +
Robert Greenwalt8dcc28b2010-09-23 10:05:56 -07002194 ", condition =" + mDefaultInetCondition +
2195 ", published condition =" + mDefaultInetConditionPublished);
2196 }
2197 mInetConditionChangeInFlight = false;
2198
2199 if (mActiveDefaultNetwork == -1) {
Wink Savilleed9c02b2010-12-03 12:01:38 -08002200 if (DBG) log("no active default network - aborting");
Robert Greenwalt8dcc28b2010-09-23 10:05:56 -07002201 return;
2202 }
2203 if (mDefaultConnectionSequence != sequence) {
Wink Savilleed9c02b2010-12-03 12:01:38 -08002204 if (DBG) log("event hold for obsolete network - aborting");
Robert Greenwalt8dcc28b2010-09-23 10:05:56 -07002205 return;
2206 }
2207 if (mDefaultInetConditionPublished == mDefaultInetCondition) {
Wink Savilleed9c02b2010-12-03 12:01:38 -08002208 if (DBG) log("no change in condition - aborting");
Robert Greenwalt8dcc28b2010-09-23 10:05:56 -07002209 return;
2210 }
2211 NetworkInfo networkInfo = mNetTrackers[mActiveDefaultNetwork].getNetworkInfo();
2212 if (networkInfo.isConnected() == false) {
Wink Savilleed9c02b2010-12-03 12:01:38 -08002213 if (DBG) log("default network not connected - aborting");
Robert Greenwalt8dcc28b2010-09-23 10:05:56 -07002214 return;
2215 }
2216 mDefaultInetConditionPublished = mDefaultInetCondition;
2217 sendInetConditionBroadcast(networkInfo);
2218 return;
Robert Greenwaltd7085fc2010-09-08 15:24:47 -07002219 }
Robert Greenwalt434203a2010-10-11 16:00:27 -07002220
2221 public synchronized ProxyProperties getProxy() {
2222 if (mGlobalProxy != null) return mGlobalProxy;
2223 if (mDefaultProxy != null) return mDefaultProxy;
2224 return null;
2225 }
2226
2227 public void setGlobalProxy(ProxyProperties proxyProperties) {
2228 enforceChangePermission();
2229 synchronized (mGlobalProxyLock) {
2230 if (proxyProperties == mGlobalProxy) return;
2231 if (proxyProperties != null && proxyProperties.equals(mGlobalProxy)) return;
2232 if (mGlobalProxy != null && mGlobalProxy.equals(proxyProperties)) return;
2233
2234 String host = "";
2235 int port = 0;
2236 String exclList = "";
2237 if (proxyProperties != null && !TextUtils.isEmpty(proxyProperties.getHost())) {
2238 mGlobalProxy = new ProxyProperties(proxyProperties);
2239 host = mGlobalProxy.getHost();
2240 port = mGlobalProxy.getPort();
2241 exclList = mGlobalProxy.getExclusionList();
2242 } else {
2243 mGlobalProxy = null;
2244 }
2245 ContentResolver res = mContext.getContentResolver();
2246 Settings.Secure.putString(res, Settings.Secure.GLOBAL_HTTP_PROXY_HOST, host);
2247 Settings.Secure.putInt(res, Settings.Secure.GLOBAL_HTTP_PROXY_PORT, port);
Robert Greenwaltb7090d62010-12-02 11:31:00 -08002248 Settings.Secure.putString(res, Settings.Secure.GLOBAL_HTTP_PROXY_EXCLUSION_LIST,
Robert Greenwalt434203a2010-10-11 16:00:27 -07002249 exclList);
2250 }
2251
2252 if (mGlobalProxy == null) {
2253 proxyProperties = mDefaultProxy;
2254 }
2255 sendProxyBroadcast(proxyProperties);
2256 }
2257
Robert Greenwaltb7090d62010-12-02 11:31:00 -08002258 private void loadGlobalProxy() {
2259 ContentResolver res = mContext.getContentResolver();
2260 String host = Settings.Secure.getString(res, Settings.Secure.GLOBAL_HTTP_PROXY_HOST);
2261 int port = Settings.Secure.getInt(res, Settings.Secure.GLOBAL_HTTP_PROXY_PORT, 0);
2262 String exclList = Settings.Secure.getString(res,
2263 Settings.Secure.GLOBAL_HTTP_PROXY_EXCLUSION_LIST);
2264 if (!TextUtils.isEmpty(host)) {
2265 ProxyProperties proxyProperties = new ProxyProperties(host, port, exclList);
2266 synchronized (mGlobalProxyLock) {
2267 mGlobalProxy = proxyProperties;
2268 }
2269 }
2270 }
2271
Robert Greenwalt434203a2010-10-11 16:00:27 -07002272 public ProxyProperties getGlobalProxy() {
2273 synchronized (mGlobalProxyLock) {
2274 return mGlobalProxy;
2275 }
2276 }
2277
2278 private void handleApplyDefaultProxy(int type) {
2279 // check if new default - push it out to all VM if so
2280 ProxyProperties proxy = mNetTrackers[type].getLinkProperties().getHttpProxy();
2281 synchronized (this) {
2282 if (mDefaultProxy != null && mDefaultProxy.equals(proxy)) return;
2283 if (mDefaultProxy == proxy) return;
2284 if (!TextUtils.isEmpty(proxy.getHost())) {
2285 mDefaultProxy = proxy;
2286 } else {
2287 mDefaultProxy = null;
2288 }
2289 }
Wink Savilleed9c02b2010-12-03 12:01:38 -08002290 if (DBG) log("changing default proxy to " + proxy);
Robert Greenwalt434203a2010-10-11 16:00:27 -07002291 if ((proxy == null && mGlobalProxy == null) || proxy.equals(mGlobalProxy)) return;
2292 if (mGlobalProxy != null) return;
2293 sendProxyBroadcast(proxy);
2294 }
2295
2296 private void handleDeprecatedGlobalHttpProxy() {
2297 String proxy = Settings.Secure.getString(mContext.getContentResolver(),
2298 Settings.Secure.HTTP_PROXY);
2299 if (!TextUtils.isEmpty(proxy)) {
2300 String data[] = proxy.split(":");
2301 String proxyHost = data[0];
2302 int proxyPort = 8080;
2303 if (data.length > 1) {
2304 try {
2305 proxyPort = Integer.parseInt(data[1]);
2306 } catch (NumberFormatException e) {
2307 return;
2308 }
2309 }
2310 ProxyProperties p = new ProxyProperties(data[0], proxyPort, "");
2311 setGlobalProxy(p);
2312 }
2313 }
2314
2315 private void sendProxyBroadcast(ProxyProperties proxy) {
Robert Greenwalt55985be2010-12-23 15:51:10 -08002316 if (proxy == null) proxy = new ProxyProperties("", 0, "");
Wink Savilleed9c02b2010-12-03 12:01:38 -08002317 log("sending Proxy Broadcast for " + proxy);
Robert Greenwalt434203a2010-10-11 16:00:27 -07002318 Intent intent = new Intent(Proxy.PROXY_CHANGE_ACTION);
Stan Chesnuttb35d67a2011-01-06 11:00:19 -08002319 intent.addFlags(Intent.FLAG_RECEIVER_REPLACE_PENDING |
2320 Intent.FLAG_RECEIVER_REGISTERED_ONLY_BEFORE_BOOT);
Robert Greenwalt434203a2010-10-11 16:00:27 -07002321 intent.putExtra(Proxy.EXTRA_PROXY_INFO, proxy);
Robert Greenwalta2e13392010-12-06 11:29:17 -08002322 mContext.sendStickyBroadcast(intent);
Robert Greenwalt434203a2010-10-11 16:00:27 -07002323 }
2324
2325 private static class SettingsObserver extends ContentObserver {
2326 private int mWhat;
2327 private Handler mHandler;
2328 SettingsObserver(Handler handler, int what) {
2329 super(handler);
2330 mHandler = handler;
2331 mWhat = what;
2332 }
2333
2334 void observe(Context context) {
2335 ContentResolver resolver = context.getContentResolver();
2336 resolver.registerContentObserver(Settings.Secure.getUriFor(
2337 Settings.Secure.HTTP_PROXY), false, this);
2338 }
2339
2340 @Override
2341 public void onChange(boolean selfChange) {
2342 mHandler.obtainMessage(mWhat).sendToTarget();
2343 }
2344 }
Wink Savilleed9c02b2010-12-03 12:01:38 -08002345
Kazuhiro Ondo01758e82011-04-30 20:10:57 -05002346 private void handleTetherIfaceChange(int type) {
2347 String iface = mNetTrackers[type].getLinkProperties().getInterfaceName();
2348
2349 if (isTetheringSupported()) {
2350 mTethering.handleTetherIfaceChange(iface);
2351 }
2352 }
2353
Wink Savilleed9c02b2010-12-03 12:01:38 -08002354 private void log(String s) {
2355 Slog.d(TAG, s);
2356 }
2357
2358 private void loge(String s) {
2359 Slog.e(TAG, s);
2360 }
Wink Saville2b8bcfe2011-02-24 17:58:51 -08002361 int convertFeatureToNetworkType(String feature){
2362 int networkType = -1;
2363 if (TextUtils.equals(feature, Phone.FEATURE_ENABLE_MMS)) {
2364 networkType = ConnectivityManager.TYPE_MOBILE_MMS;
2365 } else if (TextUtils.equals(feature, Phone.FEATURE_ENABLE_SUPL)) {
2366 networkType = ConnectivityManager.TYPE_MOBILE_SUPL;
2367 } else if (TextUtils.equals(feature, Phone.FEATURE_ENABLE_DUN) ||
2368 TextUtils.equals(feature, Phone.FEATURE_ENABLE_DUN_ALWAYS)) {
2369 networkType = ConnectivityManager.TYPE_MOBILE_DUN;
2370 } else if (TextUtils.equals(feature, Phone.FEATURE_ENABLE_HIPRI)) {
2371 networkType = ConnectivityManager.TYPE_MOBILE_HIPRI;
2372 } else if (TextUtils.equals(feature, Phone.FEATURE_ENABLE_FOTA)) {
2373 networkType = ConnectivityManager.TYPE_MOBILE_FOTA;
2374 } else if (TextUtils.equals(feature, Phone.FEATURE_ENABLE_IMS)) {
2375 networkType = ConnectivityManager.TYPE_MOBILE_IMS;
2376 } else if (TextUtils.equals(feature, Phone.FEATURE_ENABLE_CBS)) {
2377 networkType = ConnectivityManager.TYPE_MOBILE_CBS;
2378 }
2379 return networkType;
2380 }
Jeff Sharkeyc006f1a2011-05-19 17:12:49 -07002381
2382 private static <T> T checkNotNull(T value, String message) {
2383 if (value == null) {
2384 throw new NullPointerException(message);
2385 }
2386 return value;
2387 }
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002388}