blob: c39b24fc1bdf53a6942db86c15e6d1f8c4c704fb [file] [log] [blame]
The Android Open Source Project28527d22009-03-03 19:31:44 -08001/*
2 * Copyright (C) 2008 The Android Open Source Project
3 *
4 * Licensed under the Apache License, Version 2.0 (the "License");
5 * you may not use this file except in compliance with the License.
6 * You may obtain a copy of the License at
7 *
8 * http://www.apache.org/licenses/LICENSE-2.0
9 *
10 * Unless required by applicable law or agreed to in writing, software
11 * distributed under the License is distributed on an "AS IS" BASIS,
12 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13 * See the License for the specific language governing permissions and
14 * limitations under the License.
15 */
16
17package com.android.server;
18
Jeff Sharkey4434b0b2011-06-16 13:04:20 -070019import static android.Manifest.permission.MANAGE_NETWORK_POLICY;
Jeff Sharkey921ebf22011-05-19 17:12:49 -070020import static android.net.ConnectivityManager.isNetworkTypeValid;
21import static android.net.NetworkPolicyManager.RULE_ALLOW_ALL;
22import static android.net.NetworkPolicyManager.RULE_REJECT_PAID;
23
Jaikumar Ganesh0db51a02010-12-21 22:31:44 -080024import android.bluetooth.BluetoothTetheringDataTracker;
The Android Open Source Project28527d22009-03-03 19:31:44 -080025import android.content.ContentResolver;
26import android.content.Context;
27import android.content.Intent;
28import android.content.pm.PackageManager;
Robert Greenwaltc3c5f862010-10-11 16:00:27 -070029import android.database.ContentObserver;
The Android Open Source Project28527d22009-03-03 19:31:44 -080030import android.net.ConnectivityManager;
Robert Greenwalteb123ac2010-12-06 13:56:24 -080031import android.net.DummyDataStateTracker;
Benoit Goby211b5692010-12-22 14:29:40 -080032import android.net.EthernetDataTracker;
The Android Open Source Project28527d22009-03-03 19:31:44 -080033import android.net.IConnectivityManager;
Jeff Sharkey921ebf22011-05-19 17:12:49 -070034import android.net.INetworkPolicyListener;
35import android.net.INetworkPolicyManager;
Jaikumar Ganesh0db51a02010-12-21 22:31:44 -080036import android.net.LinkProperties;
The Android Open Source Project28527d22009-03-03 19:31:44 -080037import android.net.MobileDataStateTracker;
Robert Greenwalt34848c02011-03-25 13:09:25 -070038import android.net.NetworkConfig;
The Android Open Source Project28527d22009-03-03 19:31:44 -080039import android.net.NetworkInfo;
Jeff Sharkey921ebf22011-05-19 17:12:49 -070040import android.net.NetworkInfo.DetailedState;
Jeff Sharkey21062e72011-05-28 20:56:34 -070041import android.net.NetworkState;
The Android Open Source Project28527d22009-03-03 19:31:44 -080042import android.net.NetworkStateTracker;
Robert Greenwalt7fe44cb2010-08-27 09:24:29 -070043import android.net.NetworkUtils;
Robert Greenwaltc3c5f862010-10-11 16:00:27 -070044import android.net.Proxy;
45import android.net.ProxyProperties;
Robert Greenwalt5a901292011-04-28 14:28:50 -070046import android.net.RouteInfo;
Hung-ying Tyan4e723422011-01-19 16:48:38 +080047import android.net.vpn.VpnManager;
The Android Open Source Project28527d22009-03-03 19:31:44 -080048import android.net.wifi.WifiStateTracker;
49import android.os.Binder;
Chia-chi Yeh9a4ad7d2011-05-23 17:26:46 -070050import android.os.Bundle;
Mike Lockwood0d5916c2011-05-28 13:24:04 -040051import android.os.FileUtils;
The Android Open Source Project28527d22009-03-03 19:31:44 -080052import android.os.Handler;
Wink Saville775aad62010-09-02 19:23:52 -070053import android.os.HandlerThread;
Robert Greenwalt2034b912009-08-12 16:08:25 -070054import android.os.IBinder;
Chia-chi Yeh4df51322011-05-11 16:35:13 -070055import android.os.INetworkManagementService;
The Android Open Source Project28527d22009-03-03 19:31:44 -080056import android.os.Looper;
57import android.os.Message;
Chia-chi Yeh9a4ad7d2011-05-23 17:26:46 -070058import android.os.ParcelFileDescriptor;
Robert Greenwalt93dc1042010-06-15 12:19:37 -070059import android.os.PowerManager;
Robert Greenwalt2034b912009-08-12 16:08:25 -070060import android.os.RemoteException;
The Android Open Source Project28527d22009-03-03 19:31:44 -080061import android.os.ServiceManager;
62import android.os.SystemProperties;
63import android.provider.Settings;
Robert Greenwalt2034b912009-08-12 16:08:25 -070064import android.text.TextUtils;
The Android Open Source Project28527d22009-03-03 19:31:44 -080065import android.util.EventLog;
Joe Onoratoc2386bb2010-02-26 18:56:32 -080066import android.util.Slog;
Jeff Sharkey921ebf22011-05-19 17:12:49 -070067import android.util.SparseIntArray;
The Android Open Source Project28527d22009-03-03 19:31:44 -080068
Robert Greenwalt2034b912009-08-12 16:08:25 -070069import com.android.internal.telephony.Phone;
Robert Greenwalt0c4828c2010-01-26 11:40:34 -080070import com.android.server.connectivity.Tethering;
Chia-chi Yeh9a4ad7d2011-05-23 17:26:46 -070071import com.android.server.connectivity.Vpn;
72
Jeff Sharkey21062e72011-05-28 20:56:34 -070073import com.google.android.collect.Lists;
Robert Greenwalt0c4828c2010-01-26 11:40:34 -080074
The Android Open Source Project28527d22009-03-03 19:31:44 -080075import java.io.FileDescriptor;
Irfan Sheriff7f132d92010-06-09 15:39:36 -070076import java.io.IOException;
The Android Open Source Project28527d22009-03-03 19:31:44 -080077import java.io.PrintWriter;
Robert Greenwalta7dfbd32010-06-15 15:43:39 -070078import java.net.InetAddress;
79import java.net.UnknownHostException;
Robert Greenwalt2034b912009-08-12 16:08:25 -070080import java.util.ArrayList;
Robert Greenwalta7dfbd32010-06-15 15:43:39 -070081import java.util.Collection;
Robert Greenwalt0e80be12010-09-20 14:35:25 -070082import java.util.GregorianCalendar;
Robert Greenwalt2034b912009-08-12 16:08:25 -070083import java.util.List;
Jeff Sharkey921ebf22011-05-19 17:12:49 -070084import java.util.concurrent.atomic.AtomicBoolean;
The Android Open Source Project28527d22009-03-03 19:31:44 -080085
86/**
87 * @hide
88 */
89public class ConnectivityService extends IConnectivityManager.Stub {
90
Robert Greenwalt063dc7d2010-10-05 19:12:26 -070091 private static final boolean DBG = true;
The Android Open Source Project28527d22009-03-03 19:31:44 -080092 private static final String TAG = "ConnectivityService";
93
Jeff Sharkey921ebf22011-05-19 17:12:49 -070094 private static final boolean LOGD_RULES = false;
95
Robert Greenwalt2034b912009-08-12 16:08:25 -070096 // how long to wait before switching back to a radio's default network
97 private static final int RESTORE_DEFAULT_NETWORK_DELAY = 1 * 60 * 1000;
98 // system property that can override the above value
99 private static final String NETWORK_RESTORE_DELAY_PROP_NAME =
100 "android.telephony.apn-restore";
101
Robert Greenwaltbd492212011-05-06 17:10:53 -0700102 // used in recursive route setting to add gateways for the host for which
103 // a host route was requested.
104 private static final int MAX_HOSTROUTE_CYCLE_COUNT = 10;
105
Robert Greenwalt0c4828c2010-01-26 11:40:34 -0800106 private Tethering mTethering;
Robert Greenwaltf1b66e12010-02-25 12:29:30 -0800107 private boolean mTetheringConfigValid = false;
Robert Greenwalt0c4828c2010-01-26 11:40:34 -0800108
Chia-chi Yeh9a4ad7d2011-05-23 17:26:46 -0700109 private Vpn mVpn;
110
Jeff Sharkey921ebf22011-05-19 17:12:49 -0700111 /** Currently active network rules by UID. */
112 private SparseIntArray mUidRules = new SparseIntArray();
113
The Android Open Source Project28527d22009-03-03 19:31:44 -0800114 /**
115 * Sometimes we want to refer to the individual network state
116 * trackers separately, and sometimes we just want to treat them
117 * abstractly.
118 */
119 private NetworkStateTracker mNetTrackers[];
Robert Greenwalt2034b912009-08-12 16:08:25 -0700120
121 /**
122 * A per Net list of the PID's that requested access to the net
123 * used both as a refcount and for per-PID DNS selection
124 */
125 private List mNetRequestersPids[];
126
Irfan Sheriff653e2a22010-06-07 09:03:04 -0700127 private WifiWatchdogService mWifiWatchdogService;
128
Robert Greenwalt2034b912009-08-12 16:08:25 -0700129 // priority order of the nettrackers
130 // (excluding dynamically set mNetworkPreference)
131 // TODO - move mNetworkTypePreference into this
132 private int[] mPriorityList;
133
The Android Open Source Project28527d22009-03-03 19:31:44 -0800134 private Context mContext;
135 private int mNetworkPreference;
Robert Greenwalt2034b912009-08-12 16:08:25 -0700136 private int mActiveDefaultNetwork = -1;
Robert Greenwalt986c7412010-09-08 15:24:47 -0700137 // 0 is full bad, 100 is full good
138 private int mDefaultInetCondition = 0;
139 private int mDefaultInetConditionPublished = 0;
140 private boolean mInetConditionChangeInFlight = false;
141 private int mDefaultConnectionSequence = 0;
The Android Open Source Project28527d22009-03-03 19:31:44 -0800142
143 private int mNumDnsEntries;
The Android Open Source Project28527d22009-03-03 19:31:44 -0800144
145 private boolean mTestMode;
Joe Onorato56023ad2010-09-01 21:18:22 -0700146 private static ConnectivityService sServiceInstance;
The Android Open Source Project28527d22009-03-03 19:31:44 -0800147
Robert Greenwaltd62c7002010-12-29 16:15:02 -0800148 private AtomicBoolean mBackgroundDataEnabled = new AtomicBoolean(true);
149
Robert Greenwalt355205c2011-05-10 15:05:02 -0700150 private INetworkManagementService mNetd;
Jeff Sharkey921ebf22011-05-19 17:12:49 -0700151 private INetworkPolicyManager mPolicyManager;
Robert Greenwalt355205c2011-05-10 15:05:02 -0700152
Robert Greenwalt6a2db8a2010-09-23 10:05:56 -0700153 private static final int ENABLED = 1;
154 private static final int DISABLED = 0;
155
156 // Share the event space with NetworkStateTracker (which can't see this
157 // internal class but sends us events). If you change these, change
158 // NetworkStateTracker.java too.
159 private static final int MIN_NETWORK_STATE_TRACKER_EVENT = 1;
160 private static final int MAX_NETWORK_STATE_TRACKER_EVENT = 100;
161
162 /**
163 * used internally as a delayed event to make us switch back to the
164 * default network
165 */
166 private static final int EVENT_RESTORE_DEFAULT_NETWORK =
167 MAX_NETWORK_STATE_TRACKER_EVENT + 1;
168
169 /**
170 * used internally to change our mobile data enabled flag
171 */
172 private static final int EVENT_CHANGE_MOBILE_DATA_ENABLED =
173 MAX_NETWORK_STATE_TRACKER_EVENT + 2;
174
175 /**
176 * used internally to change our network preference setting
177 * arg1 = networkType to prefer
178 */
179 private static final int EVENT_SET_NETWORK_PREFERENCE =
180 MAX_NETWORK_STATE_TRACKER_EVENT + 3;
181
182 /**
183 * used internally to synchronize inet condition reports
184 * arg1 = networkType
185 * arg2 = condition (0 bad, 100 good)
186 */
187 private static final int EVENT_INET_CONDITION_CHANGE =
188 MAX_NETWORK_STATE_TRACKER_EVENT + 4;
189
190 /**
191 * used internally to mark the end of inet condition hold periods
192 * arg1 = networkType
193 */
194 private static final int EVENT_INET_CONDITION_HOLD_END =
195 MAX_NETWORK_STATE_TRACKER_EVENT + 5;
196
197 /**
198 * used internally to set the background data preference
199 * arg1 = TRUE for enabled, FALSE for disabled
200 */
201 private static final int EVENT_SET_BACKGROUND_DATA =
202 MAX_NETWORK_STATE_TRACKER_EVENT + 6;
203
204 /**
205 * used internally to set enable/disable cellular data
206 * arg1 = ENBALED or DISABLED
207 */
208 private static final int EVENT_SET_MOBILE_DATA =
209 MAX_NETWORK_STATE_TRACKER_EVENT + 7;
210
Robert Greenwaltccb36f92010-09-24 14:32:21 -0700211 /**
212 * used internally to clear a wakelock when transitioning
213 * from one net to another
214 */
215 private static final int EVENT_CLEAR_NET_TRANSITION_WAKELOCK =
216 MAX_NETWORK_STATE_TRACKER_EVENT + 8;
217
Robert Greenwaltc3c5f862010-10-11 16:00:27 -0700218 /**
219 * used internally to reload global proxy settings
220 */
221 private static final int EVENT_APPLY_GLOBAL_HTTP_PROXY =
222 MAX_NETWORK_STATE_TRACKER_EVENT + 9;
223
Robert Greenwalt34848c02011-03-25 13:09:25 -0700224 /**
225 * used internally to set external dependency met/unmet
226 * arg1 = ENABLED (met) or DISABLED (unmet)
227 * arg2 = NetworkType
228 */
229 private static final int EVENT_SET_DEPENDENCY_MET =
230 MAX_NETWORK_STATE_TRACKER_EVENT + 10;
231
Robert Greenwalt2034b912009-08-12 16:08:25 -0700232 private Handler mHandler;
233
234 // list of DeathRecipients used to make sure features are turned off when
235 // a process dies
236 private List mFeatureUsers;
237
Mike Lockwoodfde2b762009-08-14 14:18:49 -0400238 private boolean mSystemReady;
Dianne Hackborna417ff82009-12-08 19:45:14 -0800239 private Intent mInitialBroadcast;
Mike Lockwoodfde2b762009-08-14 14:18:49 -0400240
Robert Greenwalt93dc1042010-06-15 12:19:37 -0700241 private PowerManager.WakeLock mNetTransitionWakeLock;
242 private String mNetTransitionWakeLockCausedBy = "";
243 private int mNetTransitionWakeLockSerialNumber;
244 private int mNetTransitionWakeLockTimeout;
245
Robert Greenwalt94daa182010-09-01 11:34:05 -0700246 private InetAddress mDefaultDns;
247
Robert Greenwalt0e80be12010-09-20 14:35:25 -0700248 // used in DBG mode to track inet condition reports
249 private static final int INET_CONDITION_LOG_MAX_SIZE = 15;
250 private ArrayList mInetLog;
251
Robert Greenwaltc3c5f862010-10-11 16:00:27 -0700252 // track the current default http proxy - tell the world if we get a new one (real change)
253 private ProxyProperties mDefaultProxy = null;
254 // track the global proxy.
255 private ProxyProperties mGlobalProxy = null;
256 private final Object mGlobalProxyLock = new Object();
257
258 private SettingsObserver mSettingsObserver;
259
Robert Greenwalt34848c02011-03-25 13:09:25 -0700260 NetworkConfig[] mNetConfigs;
Robert Greenwaltec05b3c2009-10-30 14:17:42 -0700261 int mNetworksDefined;
Robert Greenwalt2034b912009-08-12 16:08:25 -0700262
Robert Greenwalt12c44552009-12-07 11:33:18 -0800263 private static class RadioAttributes {
Robert Greenwalt2034b912009-08-12 16:08:25 -0700264 public int mSimultaneity;
265 public int mType;
266 public RadioAttributes(String init) {
267 String fragments[] = init.split(",");
Robert Greenwaltec05b3c2009-10-30 14:17:42 -0700268 mType = Integer.parseInt(fragments[0]);
269 mSimultaneity = Integer.parseInt(fragments[1]);
Robert Greenwalt2034b912009-08-12 16:08:25 -0700270 }
271 }
272 RadioAttributes[] mRadioAttributes;
273
Jeff Sharkey921ebf22011-05-19 17:12:49 -0700274 public ConnectivityService(
275 Context context, INetworkManagementService netd, INetworkPolicyManager policyManager) {
Wink Savillee70c6f52010-12-03 12:01:38 -0800276 if (DBG) log("ConnectivityService starting up");
Robert Greenwaltd48f8ee2010-01-14 17:47:58 -0800277
Wink Saville775aad62010-09-02 19:23:52 -0700278 HandlerThread handlerThread = new HandlerThread("ConnectivityServiceThread");
279 handlerThread.start();
280 mHandler = new MyHandler(handlerThread.getLooper());
281
Robert Greenwaltd62c7002010-12-29 16:15:02 -0800282 mBackgroundDataEnabled.set(Settings.Secure.getInt(context.getContentResolver(),
283 Settings.Secure.BACKGROUND_DATA, 1) == 1);
284
Robert Greenwaltd48f8ee2010-01-14 17:47:58 -0800285 // setup our unique device name
Robert Greenwalt82cde132010-12-06 09:30:17 -0800286 if (TextUtils.isEmpty(SystemProperties.get("net.hostname"))) {
287 String id = Settings.Secure.getString(context.getContentResolver(),
288 Settings.Secure.ANDROID_ID);
289 if (id != null && id.length() > 0) {
290 String name = new String("android_").concat(id);
291 SystemProperties.set("net.hostname", name);
292 }
Robert Greenwaltd48f8ee2010-01-14 17:47:58 -0800293 }
294
Robert Greenwalt94daa182010-09-01 11:34:05 -0700295 // read our default dns server ip
296 String dns = Settings.Secure.getString(context.getContentResolver(),
297 Settings.Secure.DEFAULT_DNS_SERVER);
298 if (dns == null || dns.length() == 0) {
299 dns = context.getResources().getString(
300 com.android.internal.R.string.config_default_dns_server);
301 }
302 try {
Robert Greenwalt35e34d12011-02-22 16:00:42 -0800303 mDefaultDns = NetworkUtils.numericToInetAddress(dns);
304 } catch (IllegalArgumentException e) {
Wink Savillee70c6f52010-12-03 12:01:38 -0800305 loge("Error setting defaultDns using " + dns);
Robert Greenwalt94daa182010-09-01 11:34:05 -0700306 }
307
Jeff Sharkey921ebf22011-05-19 17:12:49 -0700308 mContext = checkNotNull(context, "missing Context");
309 mNetd = checkNotNull(netd, "missing INetworkManagementService");
310 mPolicyManager = checkNotNull(policyManager, "missing INetworkPolicyManager");
Robert Greenwalt93dc1042010-06-15 12:19:37 -0700311
Jeff Sharkey921ebf22011-05-19 17:12:49 -0700312 try {
313 mPolicyManager.registerListener(mPolicyListener);
314 } catch (RemoteException e) {
315 // ouch, no rules updates means some processes may never get network
316 Slog.e(TAG, "unable to register INetworkPolicyListener", e);
317 }
318
319 final PowerManager powerManager = (PowerManager) context.getSystemService(
320 Context.POWER_SERVICE);
Robert Greenwalt93dc1042010-06-15 12:19:37 -0700321 mNetTransitionWakeLock = powerManager.newWakeLock(PowerManager.PARTIAL_WAKE_LOCK, TAG);
322 mNetTransitionWakeLockTimeout = mContext.getResources().getInteger(
323 com.android.internal.R.integer.config_networkTransitionTimeout);
324
Robert Greenwalt2034b912009-08-12 16:08:25 -0700325 mNetTrackers = new NetworkStateTracker[
326 ConnectivityManager.MAX_NETWORK_TYPE+1];
Robert Greenwalt0659da32009-07-16 17:21:39 -0700327
The Android Open Source Project28527d22009-03-03 19:31:44 -0800328 mNetworkPreference = getPersistedNetworkPreference();
Robert Greenwalt0659da32009-07-16 17:21:39 -0700329
Robert Greenwaltec05b3c2009-10-30 14:17:42 -0700330 mRadioAttributes = new RadioAttributes[ConnectivityManager.MAX_RADIO_TYPE+1];
Robert Greenwalt34848c02011-03-25 13:09:25 -0700331 mNetConfigs = new NetworkConfig[ConnectivityManager.MAX_NETWORK_TYPE+1];
Robert Greenwaltec05b3c2009-10-30 14:17:42 -0700332
Robert Greenwalt2034b912009-08-12 16:08:25 -0700333 // Load device network attributes from resources
Robert Greenwalt2034b912009-08-12 16:08:25 -0700334 String[] raStrings = context.getResources().getStringArray(
335 com.android.internal.R.array.radioAttributes);
Robert Greenwaltec05b3c2009-10-30 14:17:42 -0700336 for (String raString : raStrings) {
337 RadioAttributes r = new RadioAttributes(raString);
338 if (r.mType > ConnectivityManager.MAX_RADIO_TYPE) {
Wink Savillee70c6f52010-12-03 12:01:38 -0800339 loge("Error in radioAttributes - ignoring attempt to define type " + r.mType);
Robert Greenwaltec05b3c2009-10-30 14:17:42 -0700340 continue;
341 }
342 if (mRadioAttributes[r.mType] != null) {
Wink Savillee70c6f52010-12-03 12:01:38 -0800343 loge("Error in radioAttributes - ignoring attempt to redefine type " +
Robert Greenwaltec05b3c2009-10-30 14:17:42 -0700344 r.mType);
345 continue;
346 }
Robert Greenwalt2034b912009-08-12 16:08:25 -0700347 mRadioAttributes[r.mType] = r;
348 }
349
Robert Greenwaltec05b3c2009-10-30 14:17:42 -0700350 String[] naStrings = context.getResources().getStringArray(
351 com.android.internal.R.array.networkAttributes);
352 for (String naString : naStrings) {
353 try {
Robert Greenwalt34848c02011-03-25 13:09:25 -0700354 NetworkConfig n = new NetworkConfig(naString);
Wink Savillef2a62832011-04-07 14:23:45 -0700355 if (n.type > ConnectivityManager.MAX_NETWORK_TYPE) {
Wink Savillee70c6f52010-12-03 12:01:38 -0800356 loge("Error in networkAttributes - ignoring attempt to define type " +
Wink Savillef2a62832011-04-07 14:23:45 -0700357 n.type);
Robert Greenwaltec05b3c2009-10-30 14:17:42 -0700358 continue;
Robert Greenwalt2034b912009-08-12 16:08:25 -0700359 }
Wink Savillef2a62832011-04-07 14:23:45 -0700360 if (mNetConfigs[n.type] != null) {
Wink Savillee70c6f52010-12-03 12:01:38 -0800361 loge("Error in networkAttributes - ignoring attempt to redefine type " +
Wink Savillef2a62832011-04-07 14:23:45 -0700362 n.type);
Robert Greenwaltec05b3c2009-10-30 14:17:42 -0700363 continue;
364 }
Wink Savillef2a62832011-04-07 14:23:45 -0700365 if (mRadioAttributes[n.radio] == null) {
Wink Savillee70c6f52010-12-03 12:01:38 -0800366 loge("Error in networkAttributes - ignoring attempt to use undefined " +
Wink Savillef2a62832011-04-07 14:23:45 -0700367 "radio " + n.radio + " in network type " + n.type);
Robert Greenwaltec05b3c2009-10-30 14:17:42 -0700368 continue;
369 }
Wink Savillef2a62832011-04-07 14:23:45 -0700370 mNetConfigs[n.type] = n;
Robert Greenwaltec05b3c2009-10-30 14:17:42 -0700371 mNetworksDefined++;
372 } catch(Exception e) {
373 // ignore it - leave the entry null
Robert Greenwalt2034b912009-08-12 16:08:25 -0700374 }
375 }
376
Robert Greenwaltec05b3c2009-10-30 14:17:42 -0700377 // high priority first
378 mPriorityList = new int[mNetworksDefined];
379 {
380 int insertionPoint = mNetworksDefined-1;
381 int currentLowest = 0;
382 int nextLowest = 0;
383 while (insertionPoint > -1) {
Robert Greenwalt34848c02011-03-25 13:09:25 -0700384 for (NetworkConfig na : mNetConfigs) {
Robert Greenwaltec05b3c2009-10-30 14:17:42 -0700385 if (na == null) continue;
Wink Savillef2a62832011-04-07 14:23:45 -0700386 if (na.priority < currentLowest) continue;
387 if (na.priority > currentLowest) {
388 if (na.priority < nextLowest || nextLowest == 0) {
389 nextLowest = na.priority;
Robert Greenwaltec05b3c2009-10-30 14:17:42 -0700390 }
391 continue;
392 }
Wink Savillef2a62832011-04-07 14:23:45 -0700393 mPriorityList[insertionPoint--] = na.type;
Robert Greenwaltec05b3c2009-10-30 14:17:42 -0700394 }
395 currentLowest = nextLowest;
396 nextLowest = 0;
397 }
398 }
399
400 mNetRequestersPids = new ArrayList[ConnectivityManager.MAX_NETWORK_TYPE+1];
401 for (int i : mPriorityList) {
Robert Greenwalt2034b912009-08-12 16:08:25 -0700402 mNetRequestersPids[i] = new ArrayList();
403 }
404
405 mFeatureUsers = new ArrayList();
406
Robert Greenwaltec05b3c2009-10-30 14:17:42 -0700407 mNumDnsEntries = 0;
408
409 mTestMode = SystemProperties.get("cm.test.mode").equals("true")
410 && SystemProperties.get("ro.build.type").equals("eng");
The Android Open Source Project28527d22009-03-03 19:31:44 -0800411 /*
412 * Create the network state trackers for Wi-Fi and mobile
413 * data. Maybe this could be done with a factory class,
414 * but it's not clear that it's worth it, given that
415 * the number of different network types is not going
416 * to change very often.
417 */
Robert Greenwaltec05b3c2009-10-30 14:17:42 -0700418 for (int netType : mPriorityList) {
Wink Savillef2a62832011-04-07 14:23:45 -0700419 switch (mNetConfigs[netType].radio) {
Robert Greenwaltec05b3c2009-10-30 14:17:42 -0700420 case ConnectivityManager.TYPE_WIFI:
Wink Savillee70c6f52010-12-03 12:01:38 -0800421 if (DBG) log("Starting Wifi Service.");
Wink Saville7fabfa22010-08-13 16:11:42 -0700422 WifiStateTracker wst = new WifiStateTracker();
Irfan Sheriff25be0762010-07-28 09:35:20 -0700423 WifiService wifiService = new WifiService(context);
Robert Greenwaltec05b3c2009-10-30 14:17:42 -0700424 ServiceManager.addService(Context.WIFI_SERVICE, wifiService);
Irfan Sheriff25be0762010-07-28 09:35:20 -0700425 wifiService.checkAndStartWifi();
Robert Greenwaltec05b3c2009-10-30 14:17:42 -0700426 mNetTrackers[ConnectivityManager.TYPE_WIFI] = wst;
Wink Saville7fabfa22010-08-13 16:11:42 -0700427 wst.startMonitoring(context, mHandler);
The Android Open Source Project28527d22009-03-03 19:31:44 -0800428
Irfan Sheriff653e2a22010-06-07 09:03:04 -0700429 //TODO: as part of WWS refactor, create only when needed
Irfan Sheriff25be0762010-07-28 09:35:20 -0700430 mWifiWatchdogService = new WifiWatchdogService(context);
Irfan Sheriff653e2a22010-06-07 09:03:04 -0700431
Robert Greenwaltec05b3c2009-10-30 14:17:42 -0700432 break;
433 case ConnectivityManager.TYPE_MOBILE:
Wink Saville7fabfa22010-08-13 16:11:42 -0700434 mNetTrackers[netType] = new MobileDataStateTracker(netType,
Wink Savillef2a62832011-04-07 14:23:45 -0700435 mNetConfigs[netType].name);
Wink Saville7fabfa22010-08-13 16:11:42 -0700436 mNetTrackers[netType].startMonitoring(context, mHandler);
Robert Greenwaltec05b3c2009-10-30 14:17:42 -0700437 break;
Robert Greenwalteb123ac2010-12-06 13:56:24 -0800438 case ConnectivityManager.TYPE_DUMMY:
439 mNetTrackers[netType] = new DummyDataStateTracker(netType,
Wink Savillef2a62832011-04-07 14:23:45 -0700440 mNetConfigs[netType].name);
Robert Greenwalteb123ac2010-12-06 13:56:24 -0800441 mNetTrackers[netType].startMonitoring(context, mHandler);
442 break;
Jaikumar Ganesh0db51a02010-12-21 22:31:44 -0800443 case ConnectivityManager.TYPE_BLUETOOTH:
444 mNetTrackers[netType] = BluetoothTetheringDataTracker.getInstance();
445 mNetTrackers[netType].startMonitoring(context, mHandler);
446 break;
Benoit Goby211b5692010-12-22 14:29:40 -0800447 case ConnectivityManager.TYPE_ETHERNET:
448 mNetTrackers[netType] = EthernetDataTracker.getInstance();
449 mNetTrackers[netType].startMonitoring(context, mHandler);
450 break;
Robert Greenwaltec05b3c2009-10-30 14:17:42 -0700451 default:
Wink Savillee70c6f52010-12-03 12:01:38 -0800452 loge("Trying to create a DataStateTracker for an unknown radio type " +
Wink Savillef2a62832011-04-07 14:23:45 -0700453 mNetConfigs[netType].radio);
Robert Greenwaltec05b3c2009-10-30 14:17:42 -0700454 continue;
455 }
456 }
Robert Greenwalt0c4828c2010-01-26 11:40:34 -0800457
Chia-chi Yeh4df51322011-05-11 16:35:13 -0700458 IBinder b = ServiceManager.getService(Context.NETWORKMANAGEMENT_SERVICE);
459 INetworkManagementService nmService = INetworkManagementService.Stub.asInterface(b);
460
461 mTethering = new Tethering(mContext, nmService, mHandler.getLooper());
Robert Greenwaltf1b66e12010-02-25 12:29:30 -0800462 mTetheringConfigValid = (((mNetTrackers[ConnectivityManager.TYPE_MOBILE_DUN] != null) ||
463 !mTethering.isDunRequired()) &&
464 (mTethering.getTetherableUsbRegexs().length != 0 ||
Danica Chang96567052010-08-11 14:54:43 -0700465 mTethering.getTetherableWifiRegexs().length != 0 ||
466 mTethering.getTetherableBluetoothRegexs().length != 0) &&
Robert Greenwaltf1b66e12010-02-25 12:29:30 -0800467 mTethering.getUpstreamIfaceRegexs().length != 0);
468
Chia-chi Yeh9a4ad7d2011-05-23 17:26:46 -0700469 mVpn = new Vpn(mContext, new VpnCallback());
470
Chia-chi Yehf3204aa2011-05-23 15:08:29 -0700471 try {
472 nmService.registerObserver(mTethering);
Chia-chi Yeh9a4ad7d2011-05-23 17:26:46 -0700473 nmService.registerObserver(mVpn);
Chia-chi Yehf3204aa2011-05-23 15:08:29 -0700474 } catch (RemoteException e) {
475 loge("Error registering observer :" + e);
476 }
477
Robert Greenwalt0e80be12010-09-20 14:35:25 -0700478 if (DBG) {
479 mInetLog = new ArrayList();
480 }
Robert Greenwaltc3c5f862010-10-11 16:00:27 -0700481
482 mSettingsObserver = new SettingsObserver(mHandler, EVENT_APPLY_GLOBAL_HTTP_PROXY);
483 mSettingsObserver.observe(mContext);
Robert Greenwalt6f7c6092010-12-02 11:31:00 -0800484
485 loadGlobalProxy();
Hung-ying Tyan4e723422011-01-19 16:48:38 +0800486
487 VpnManager.startVpnService(context);
The Android Open Source Project28527d22009-03-03 19:31:44 -0800488 }
489
Robert Greenwaltec05b3c2009-10-30 14:17:42 -0700490
The Android Open Source Project28527d22009-03-03 19:31:44 -0800491 /**
Robert Greenwalt0659da32009-07-16 17:21:39 -0700492 * Sets the preferred network.
The Android Open Source Project28527d22009-03-03 19:31:44 -0800493 * @param preference the new preference
494 */
Robert Greenwalt6a2db8a2010-09-23 10:05:56 -0700495 public void setNetworkPreference(int preference) {
The Android Open Source Project28527d22009-03-03 19:31:44 -0800496 enforceChangePermission();
Robert Greenwalt6a2db8a2010-09-23 10:05:56 -0700497
498 mHandler.sendMessage(mHandler.obtainMessage(EVENT_SET_NETWORK_PREFERENCE, preference, 0));
The Android Open Source Project28527d22009-03-03 19:31:44 -0800499 }
500
501 public int getNetworkPreference() {
502 enforceAccessPermission();
Robert Greenwalt6a2db8a2010-09-23 10:05:56 -0700503 int preference;
504 synchronized(this) {
505 preference = mNetworkPreference;
506 }
507 return preference;
The Android Open Source Project28527d22009-03-03 19:31:44 -0800508 }
509
Robert Greenwalt6a2db8a2010-09-23 10:05:56 -0700510 private void handleSetNetworkPreference(int preference) {
511 if (ConnectivityManager.isNetworkTypeValid(preference) &&
Robert Greenwalt34848c02011-03-25 13:09:25 -0700512 mNetConfigs[preference] != null &&
513 mNetConfigs[preference].isDefault()) {
Robert Greenwalt6a2db8a2010-09-23 10:05:56 -0700514 if (mNetworkPreference != preference) {
515 final ContentResolver cr = mContext.getContentResolver();
516 Settings.Secure.putInt(cr, Settings.Secure.NETWORK_PREFERENCE, preference);
517 synchronized(this) {
518 mNetworkPreference = preference;
519 }
520 enforcePreference();
521 }
522 }
The Android Open Source Project28527d22009-03-03 19:31:44 -0800523 }
Robert Greenwalt0659da32009-07-16 17:21:39 -0700524
The Android Open Source Project28527d22009-03-03 19:31:44 -0800525 private int getPersistedNetworkPreference() {
526 final ContentResolver cr = mContext.getContentResolver();
527
528 final int networkPrefSetting = Settings.Secure
529 .getInt(cr, Settings.Secure.NETWORK_PREFERENCE, -1);
530 if (networkPrefSetting != -1) {
531 return networkPrefSetting;
532 }
533
534 return ConnectivityManager.DEFAULT_NETWORK_PREFERENCE;
535 }
Robert Greenwalt0659da32009-07-16 17:21:39 -0700536
The Android Open Source Project28527d22009-03-03 19:31:44 -0800537 /**
Robert Greenwalt0659da32009-07-16 17:21:39 -0700538 * Make the state of network connectivity conform to the preference settings
The Android Open Source Project28527d22009-03-03 19:31:44 -0800539 * In this method, we only tear down a non-preferred network. Establishing
540 * a connection to the preferred network is taken care of when we handle
541 * the disconnect event from the non-preferred network
542 * (see {@link #handleDisconnect(NetworkInfo)}).
543 */
544 private void enforcePreference() {
Robert Greenwalt2034b912009-08-12 16:08:25 -0700545 if (mNetTrackers[mNetworkPreference].getNetworkInfo().isConnected())
The Android Open Source Project28527d22009-03-03 19:31:44 -0800546 return;
547
Robert Greenwalt2034b912009-08-12 16:08:25 -0700548 if (!mNetTrackers[mNetworkPreference].isAvailable())
549 return;
The Android Open Source Project28527d22009-03-03 19:31:44 -0800550
Robert Greenwalt2034b912009-08-12 16:08:25 -0700551 for (int t=0; t <= ConnectivityManager.MAX_RADIO_TYPE; t++) {
Robert Greenwaltec05b3c2009-10-30 14:17:42 -0700552 if (t != mNetworkPreference && mNetTrackers[t] != null &&
Robert Greenwalt2034b912009-08-12 16:08:25 -0700553 mNetTrackers[t].getNetworkInfo().isConnected()) {
Robert Greenwaltf3f045b2009-08-20 15:25:14 -0700554 if (DBG) {
Wink Savillee70c6f52010-12-03 12:01:38 -0800555 log("tearing down " + mNetTrackers[t].getNetworkInfo() +
Robert Greenwaltf3f045b2009-08-20 15:25:14 -0700556 " in enforcePreference");
557 }
Robert Greenwalt2034b912009-08-12 16:08:25 -0700558 teardown(mNetTrackers[t]);
The Android Open Source Project28527d22009-03-03 19:31:44 -0800559 }
560 }
561 }
562
563 private boolean teardown(NetworkStateTracker netTracker) {
564 if (netTracker.teardown()) {
565 netTracker.setTeardownRequested(true);
566 return true;
567 } else {
568 return false;
569 }
570 }
571
572 /**
Jeff Sharkey921ebf22011-05-19 17:12:49 -0700573 * Check if UID is blocked from using the given {@link NetworkInfo}.
574 */
575 private boolean isNetworkBlocked(NetworkInfo info, int uid) {
576 synchronized (mUidRules) {
Jeff Sharkey21062e72011-05-28 20:56:34 -0700577 // TODO: expand definition of "paid" network to cover tethered or
578 // paid hotspot use cases.
579 final boolean networkIsPaid = info.getType() != ConnectivityManager.TYPE_WIFI;
580 final int uidRules = mUidRules.get(uid, RULE_ALLOW_ALL);
581
582 if (networkIsPaid && (uidRules & RULE_REJECT_PAID) != 0) {
583 return true;
584 }
585
586 // no restrictive rules; network is visible
587 return false;
Jeff Sharkey921ebf22011-05-19 17:12:49 -0700588 }
589 }
590
591 /**
Jeff Sharkey21062e72011-05-28 20:56:34 -0700592 * Return a filtered version of the given {@link NetworkInfo}, potentially
593 * marked {@link DetailedState#BLOCKED} based on
594 * {@link #isNetworkBlocked(NetworkInfo, int)}.
Jeff Sharkey921ebf22011-05-19 17:12:49 -0700595 */
Jeff Sharkey21062e72011-05-28 20:56:34 -0700596 private NetworkInfo filterNetworkInfo(NetworkInfo info, int uid) {
597 if (isNetworkBlocked(info, uid)) {
598 // network is blocked; clone and override state
599 info = new NetworkInfo(info);
600 info.setDetailedState(DetailedState.BLOCKED, null, null);
Jeff Sharkey921ebf22011-05-19 17:12:49 -0700601 }
Jeff Sharkey21062e72011-05-28 20:56:34 -0700602 return info;
Jeff Sharkey921ebf22011-05-19 17:12:49 -0700603 }
604
605 /**
The Android Open Source Project28527d22009-03-03 19:31:44 -0800606 * Return NetworkInfo for the active (i.e., connected) network interface.
607 * It is assumed that at most one network is active at a time. If more
608 * than one is active, it is indeterminate which will be returned.
Robert Greenwalt0659da32009-07-16 17:21:39 -0700609 * @return the info for the active network, or {@code null} if none is
610 * active
The Android Open Source Project28527d22009-03-03 19:31:44 -0800611 */
Jeff Sharkey921ebf22011-05-19 17:12:49 -0700612 @Override
The Android Open Source Project28527d22009-03-03 19:31:44 -0800613 public NetworkInfo getActiveNetworkInfo() {
Jeff Sharkey921ebf22011-05-19 17:12:49 -0700614 enforceAccessPermission();
615 final int uid = Binder.getCallingUid();
616 return getNetworkInfo(mActiveDefaultNetwork, uid);
The Android Open Source Project28527d22009-03-03 19:31:44 -0800617 }
618
Jeff Sharkey921ebf22011-05-19 17:12:49 -0700619 @Override
620 public NetworkInfo getActiveNetworkInfoForUid(int uid) {
621 enforceConnectivityInternalPermission();
622 return getNetworkInfo(mActiveDefaultNetwork, uid);
623 }
624
625 @Override
The Android Open Source Project28527d22009-03-03 19:31:44 -0800626 public NetworkInfo getNetworkInfo(int networkType) {
627 enforceAccessPermission();
Jeff Sharkey921ebf22011-05-19 17:12:49 -0700628 final int uid = Binder.getCallingUid();
629 return getNetworkInfo(networkType, uid);
The Android Open Source Project28527d22009-03-03 19:31:44 -0800630 }
631
Jeff Sharkey921ebf22011-05-19 17:12:49 -0700632 private NetworkInfo getNetworkInfo(int networkType, int uid) {
633 NetworkInfo info = null;
634 if (isNetworkTypeValid(networkType)) {
635 final NetworkStateTracker tracker = mNetTrackers[networkType];
636 if (tracker != null) {
Jeff Sharkey21062e72011-05-28 20:56:34 -0700637 info = filterNetworkInfo(tracker.getNetworkInfo(), uid);
Jeff Sharkey921ebf22011-05-19 17:12:49 -0700638 }
639 }
640 return info;
641 }
642
643 @Override
The Android Open Source Project28527d22009-03-03 19:31:44 -0800644 public NetworkInfo[] getAllNetworkInfo() {
645 enforceAccessPermission();
Jeff Sharkey921ebf22011-05-19 17:12:49 -0700646 final int uid = Binder.getCallingUid();
Jeff Sharkey21062e72011-05-28 20:56:34 -0700647 final ArrayList<NetworkInfo> result = Lists.newArrayList();
Jeff Sharkey921ebf22011-05-19 17:12:49 -0700648 synchronized (mUidRules) {
649 for (NetworkStateTracker tracker : mNetTrackers) {
650 if (tracker != null) {
Jeff Sharkey21062e72011-05-28 20:56:34 -0700651 result.add(filterNetworkInfo(tracker.getNetworkInfo(), uid));
Jeff Sharkey921ebf22011-05-19 17:12:49 -0700652 }
653 }
The Android Open Source Project28527d22009-03-03 19:31:44 -0800654 }
Jeff Sharkey21062e72011-05-28 20:56:34 -0700655 return result.toArray(new NetworkInfo[result.size()]);
The Android Open Source Project28527d22009-03-03 19:31:44 -0800656 }
657
Robert Greenwalt9f0ee4f2010-09-14 09:18:02 -0700658 /**
659 * Return LinkProperties for the active (i.e., connected) default
660 * network interface. It is assumed that at most one default network
661 * is active at a time. If more than one is active, it is indeterminate
662 * which will be returned.
663 * @return the ip properties for the active network, or {@code null} if
664 * none is active
665 */
Jeff Sharkey921ebf22011-05-19 17:12:49 -0700666 @Override
Robert Greenwalt9f0ee4f2010-09-14 09:18:02 -0700667 public LinkProperties getActiveLinkProperties() {
Robert Greenwalte1544bb2011-05-20 12:23:41 -0700668 return getLinkProperties(mActiveDefaultNetwork);
Robert Greenwalt9f0ee4f2010-09-14 09:18:02 -0700669 }
670
Jeff Sharkey921ebf22011-05-19 17:12:49 -0700671 @Override
Robert Greenwalt9f0ee4f2010-09-14 09:18:02 -0700672 public LinkProperties getLinkProperties(int networkType) {
673 enforceAccessPermission();
Jeff Sharkey921ebf22011-05-19 17:12:49 -0700674 if (isNetworkTypeValid(networkType)) {
675 final NetworkStateTracker tracker = mNetTrackers[networkType];
676 if (tracker != null) {
677 return tracker.getLinkProperties();
678 }
Robert Greenwalt9f0ee4f2010-09-14 09:18:02 -0700679 }
680 return null;
681 }
682
Jeff Sharkey21062e72011-05-28 20:56:34 -0700683 @Override
684 public NetworkState[] getAllNetworkState() {
685 enforceAccessPermission();
686 final int uid = Binder.getCallingUid();
687 final ArrayList<NetworkState> result = Lists.newArrayList();
688 synchronized (mUidRules) {
689 for (NetworkStateTracker tracker : mNetTrackers) {
690 if (tracker != null) {
691 final NetworkInfo info = filterNetworkInfo(tracker.getNetworkInfo(), uid);
692 result.add(new NetworkState(
693 info, tracker.getLinkProperties(), tracker.getLinkCapabilities()));
694 }
695 }
696 }
697 return result.toArray(new NetworkState[result.size()]);
698 }
699
The Android Open Source Project28527d22009-03-03 19:31:44 -0800700 public boolean setRadios(boolean turnOn) {
701 boolean result = true;
702 enforceChangePermission();
703 for (NetworkStateTracker t : mNetTrackers) {
Robert Greenwaltec05b3c2009-10-30 14:17:42 -0700704 if (t != null) result = t.setRadio(turnOn) && result;
The Android Open Source Project28527d22009-03-03 19:31:44 -0800705 }
706 return result;
707 }
708
709 public boolean setRadio(int netType, boolean turnOn) {
710 enforceChangePermission();
711 if (!ConnectivityManager.isNetworkTypeValid(netType)) {
712 return false;
713 }
714 NetworkStateTracker tracker = mNetTrackers[netType];
715 return tracker != null && tracker.setRadio(turnOn);
716 }
717
Robert Greenwaltaffc3a12009-09-27 17:27:04 -0700718 /**
719 * Used to notice when the calling process dies so we can self-expire
720 *
721 * Also used to know if the process has cleaned up after itself when
722 * our auto-expire timer goes off. The timer has a link to an object.
723 *
724 */
Robert Greenwalt2034b912009-08-12 16:08:25 -0700725 private class FeatureUser implements IBinder.DeathRecipient {
726 int mNetworkType;
727 String mFeature;
728 IBinder mBinder;
729 int mPid;
730 int mUid;
Robert Greenwalt3eeb6032009-12-21 18:24:07 -0800731 long mCreateTime;
Robert Greenwalt2034b912009-08-12 16:08:25 -0700732
733 FeatureUser(int type, String feature, IBinder binder) {
734 super();
735 mNetworkType = type;
736 mFeature = feature;
737 mBinder = binder;
738 mPid = getCallingPid();
739 mUid = getCallingUid();
Robert Greenwalt3eeb6032009-12-21 18:24:07 -0800740 mCreateTime = System.currentTimeMillis();
Robert Greenwaltaffc3a12009-09-27 17:27:04 -0700741
Robert Greenwalt2034b912009-08-12 16:08:25 -0700742 try {
743 mBinder.linkToDeath(this, 0);
744 } catch (RemoteException e) {
745 binderDied();
746 }
747 }
748
749 void unlinkDeathRecipient() {
750 mBinder.unlinkToDeath(this, 0);
751 }
752
753 public void binderDied() {
Wink Savillee70c6f52010-12-03 12:01:38 -0800754 log("ConnectivityService FeatureUser binderDied(" +
Robert Greenwalt3eeb6032009-12-21 18:24:07 -0800755 mNetworkType + ", " + mFeature + ", " + mBinder + "), created " +
756 (System.currentTimeMillis() - mCreateTime) + " mSec ago");
Robert Greenwaltaffc3a12009-09-27 17:27:04 -0700757 stopUsingNetworkFeature(this, false);
Robert Greenwalt2034b912009-08-12 16:08:25 -0700758 }
759
Robert Greenwaltaffc3a12009-09-27 17:27:04 -0700760 public void expire() {
Wink Savillee70c6f52010-12-03 12:01:38 -0800761 log("ConnectivityService FeatureUser expire(" +
Robert Greenwalt3eeb6032009-12-21 18:24:07 -0800762 mNetworkType + ", " + mFeature + ", " + mBinder +"), created " +
763 (System.currentTimeMillis() - mCreateTime) + " mSec ago");
Robert Greenwaltaffc3a12009-09-27 17:27:04 -0700764 stopUsingNetworkFeature(this, false);
765 }
Robert Greenwalt3eeb6032009-12-21 18:24:07 -0800766
767 public String toString() {
768 return "FeatureUser("+mNetworkType+","+mFeature+","+mPid+","+mUid+"), created " +
769 (System.currentTimeMillis() - mCreateTime) + " mSec ago";
770 }
Robert Greenwalt2034b912009-08-12 16:08:25 -0700771 }
772
Robert Greenwaltaffc3a12009-09-27 17:27:04 -0700773 // javadoc from interface
Robert Greenwalt2034b912009-08-12 16:08:25 -0700774 public int startUsingNetworkFeature(int networkType, String feature,
775 IBinder binder) {
776 if (DBG) {
Wink Savillee70c6f52010-12-03 12:01:38 -0800777 log("startUsingNetworkFeature for net " + networkType + ": " + feature);
Robert Greenwalt2034b912009-08-12 16:08:25 -0700778 }
The Android Open Source Project28527d22009-03-03 19:31:44 -0800779 enforceChangePermission();
Robert Greenwaltec05b3c2009-10-30 14:17:42 -0700780 if (!ConnectivityManager.isNetworkTypeValid(networkType) ||
Robert Greenwalt34848c02011-03-25 13:09:25 -0700781 mNetConfigs[networkType] == null) {
Robert Greenwalt2034b912009-08-12 16:08:25 -0700782 return Phone.APN_REQUEST_FAILED;
The Android Open Source Project28527d22009-03-03 19:31:44 -0800783 }
Robert Greenwalt2034b912009-08-12 16:08:25 -0700784
Robert Greenwaltaffc3a12009-09-27 17:27:04 -0700785 FeatureUser f = new FeatureUser(networkType, feature, binder);
Robert Greenwalt2034b912009-08-12 16:08:25 -0700786
787 // TODO - move this into the MobileDataStateTracker
788 int usedNetworkType = networkType;
789 if(networkType == ConnectivityManager.TYPE_MOBILE) {
Wink Savillef6b76692011-02-24 17:58:51 -0800790 usedNetworkType = convertFeatureToNetworkType(feature);
791 if (usedNetworkType < 0) {
792 Slog.e(TAG, "Can't match any netTracker!");
793 usedNetworkType = networkType;
Robert Greenwalt2034b912009-08-12 16:08:25 -0700794 }
795 }
796 NetworkStateTracker network = mNetTrackers[usedNetworkType];
797 if (network != null) {
Robert Greenwalt5364d752010-12-15 13:26:33 -0800798 Integer currentPid = new Integer(getCallingPid());
Robert Greenwalt2034b912009-08-12 16:08:25 -0700799 if (usedNetworkType != networkType) {
Robert Greenwalt2034b912009-08-12 16:08:25 -0700800 NetworkStateTracker radio = mNetTrackers[networkType];
801 NetworkInfo ni = network.getNetworkInfo();
802
803 if (ni.isAvailable() == false) {
Wink Savillee70c6f52010-12-03 12:01:38 -0800804 if (DBG) log("special network not available");
Robert Greenwalt2cc87442010-12-29 14:35:21 -0800805 if (!TextUtils.equals(feature,Phone.FEATURE_ENABLE_DUN_ALWAYS)) {
806 return Phone.APN_TYPE_NOT_AVAILABLE;
807 } else {
808 // else make the attempt anyway - probably giving REQUEST_STARTED below
809 }
Robert Greenwalt2034b912009-08-12 16:08:25 -0700810 }
811
Robert Greenwaltaffc3a12009-09-27 17:27:04 -0700812 synchronized(this) {
813 mFeatureUsers.add(f);
814 if (!mNetRequestersPids[usedNetworkType].contains(currentPid)) {
815 // this gets used for per-pid dns when connected
816 mNetRequestersPids[usedNetworkType].add(currentPid);
817 }
Robert Greenwalt2034b912009-08-12 16:08:25 -0700818 }
Robert Greenwaltaffc3a12009-09-27 17:27:04 -0700819
Robert Greenwalt20f819c2011-05-03 19:02:44 -0700820 int restoreTimer = getRestoreDefaultNetworkDelay(usedNetworkType);
821
822 if (restoreTimer >= 0) {
823 mHandler.sendMessageDelayed(
824 mHandler.obtainMessage(EVENT_RESTORE_DEFAULT_NETWORK, f), restoreTimer);
825 }
Robert Greenwalt2034b912009-08-12 16:08:25 -0700826
Robert Greenwalta52c75a2009-08-19 20:19:33 -0700827 if ((ni.isConnectedOrConnecting() == true) &&
828 !network.isTeardownRequested()) {
Robert Greenwalt2034b912009-08-12 16:08:25 -0700829 if (ni.isConnected() == true) {
830 // add the pid-specific dns
Robert Greenwalt3afbead2010-07-23 15:46:26 -0700831 handleDnsConfigurationChange(networkType);
Wink Savillee70c6f52010-12-03 12:01:38 -0800832 if (DBG) log("special network already active");
Robert Greenwalt2034b912009-08-12 16:08:25 -0700833 return Phone.APN_ALREADY_ACTIVE;
834 }
Wink Savillee70c6f52010-12-03 12:01:38 -0800835 if (DBG) log("special network already connecting");
Robert Greenwalt2034b912009-08-12 16:08:25 -0700836 return Phone.APN_REQUEST_STARTED;
837 }
838
839 // check if the radio in play can make another contact
840 // assume if cannot for now
841
Wink Savillee70c6f52010-12-03 12:01:38 -0800842 if (DBG) log("reconnecting to special network");
Robert Greenwalt2034b912009-08-12 16:08:25 -0700843 network.reconnect();
844 return Phone.APN_REQUEST_STARTED;
845 } else {
Robert Greenwalt5364d752010-12-15 13:26:33 -0800846 // need to remember this unsupported request so we respond appropriately on stop
847 synchronized(this) {
848 mFeatureUsers.add(f);
849 if (!mNetRequestersPids[usedNetworkType].contains(currentPid)) {
850 // this gets used for per-pid dns when connected
851 mNetRequestersPids[usedNetworkType].add(currentPid);
852 }
853 }
Robert Greenwaltd391e892010-05-18 10:52:51 -0700854 return -1;
Robert Greenwalt2034b912009-08-12 16:08:25 -0700855 }
856 }
857 return Phone.APN_TYPE_NOT_AVAILABLE;
The Android Open Source Project28527d22009-03-03 19:31:44 -0800858 }
859
Robert Greenwaltaffc3a12009-09-27 17:27:04 -0700860 // javadoc from interface
The Android Open Source Project28527d22009-03-03 19:31:44 -0800861 public int stopUsingNetworkFeature(int networkType, String feature) {
Robert Greenwalt28f43012009-10-06 17:52:40 -0700862 enforceChangePermission();
863
Robert Greenwaltaffc3a12009-09-27 17:27:04 -0700864 int pid = getCallingPid();
865 int uid = getCallingUid();
866
867 FeatureUser u = null;
868 boolean found = false;
869
870 synchronized(this) {
871 for (int i = 0; i < mFeatureUsers.size() ; i++) {
872 u = (FeatureUser)mFeatureUsers.get(i);
873 if (uid == u.mUid && pid == u.mPid &&
874 networkType == u.mNetworkType &&
875 TextUtils.equals(feature, u.mFeature)) {
876 found = true;
877 break;
878 }
879 }
880 }
881 if (found && u != null) {
882 // stop regardless of how many other time this proc had called start
883 return stopUsingNetworkFeature(u, true);
884 } else {
885 // none found!
Wink Savillee70c6f52010-12-03 12:01:38 -0800886 if (DBG) log("ignoring stopUsingNetworkFeature - not a live request");
Robert Greenwaltaffc3a12009-09-27 17:27:04 -0700887 return 1;
888 }
Robert Greenwalt2034b912009-08-12 16:08:25 -0700889 }
890
Robert Greenwaltaffc3a12009-09-27 17:27:04 -0700891 private int stopUsingNetworkFeature(FeatureUser u, boolean ignoreDups) {
892 int networkType = u.mNetworkType;
893 String feature = u.mFeature;
894 int pid = u.mPid;
895 int uid = u.mUid;
896
897 NetworkStateTracker tracker = null;
898 boolean callTeardown = false; // used to carry our decision outside of sync block
899
Robert Greenwalt2034b912009-08-12 16:08:25 -0700900 if (DBG) {
Wink Savillee70c6f52010-12-03 12:01:38 -0800901 log("stopUsingNetworkFeature for net " + networkType +
Robert Greenwalt2034b912009-08-12 16:08:25 -0700902 ": " + feature);
903 }
Robert Greenwalt28f43012009-10-06 17:52:40 -0700904
The Android Open Source Project28527d22009-03-03 19:31:44 -0800905 if (!ConnectivityManager.isNetworkTypeValid(networkType)) {
906 return -1;
907 }
Robert Greenwalt2034b912009-08-12 16:08:25 -0700908
Robert Greenwaltaffc3a12009-09-27 17:27:04 -0700909 // need to link the mFeatureUsers list with the mNetRequestersPids state in this
910 // sync block
911 synchronized(this) {
912 // check if this process still has an outstanding start request
913 if (!mFeatureUsers.contains(u)) {
Wink Savillee70c6f52010-12-03 12:01:38 -0800914 if (DBG) log("ignoring - this process has no outstanding requests");
Robert Greenwalt2034b912009-08-12 16:08:25 -0700915 return 1;
916 }
Robert Greenwaltaffc3a12009-09-27 17:27:04 -0700917 u.unlinkDeathRecipient();
918 mFeatureUsers.remove(mFeatureUsers.indexOf(u));
919 // If we care about duplicate requests, check for that here.
920 //
921 // This is done to support the extension of a request - the app
922 // can request we start the network feature again and renew the
923 // auto-shutoff delay. Normal "stop" calls from the app though
924 // do not pay attention to duplicate requests - in effect the
925 // API does not refcount and a single stop will counter multiple starts.
926 if (ignoreDups == false) {
927 for (int i = 0; i < mFeatureUsers.size() ; i++) {
928 FeatureUser x = (FeatureUser)mFeatureUsers.get(i);
929 if (x.mUid == u.mUid && x.mPid == u.mPid &&
930 x.mNetworkType == u.mNetworkType &&
931 TextUtils.equals(x.mFeature, u.mFeature)) {
Wink Savillee70c6f52010-12-03 12:01:38 -0800932 if (DBG) log("ignoring stopUsingNetworkFeature as dup is found");
Robert Greenwaltaffc3a12009-09-27 17:27:04 -0700933 return 1;
934 }
935 }
Robert Greenwalt2034b912009-08-12 16:08:25 -0700936 }
Robert Greenwaltaffc3a12009-09-27 17:27:04 -0700937
938 // TODO - move to MobileDataStateTracker
939 int usedNetworkType = networkType;
940 if (networkType == ConnectivityManager.TYPE_MOBILE) {
Wink Savillef6b76692011-02-24 17:58:51 -0800941 usedNetworkType = convertFeatureToNetworkType(feature);
942 if (usedNetworkType < 0) {
943 usedNetworkType = networkType;
Robert Greenwaltaffc3a12009-09-27 17:27:04 -0700944 }
945 }
946 tracker = mNetTrackers[usedNetworkType];
Robert Greenwaltec05b3c2009-10-30 14:17:42 -0700947 if (tracker == null) {
Wink Savillee70c6f52010-12-03 12:01:38 -0800948 if (DBG) log("ignoring - no known tracker for net type " + usedNetworkType);
Robert Greenwaltec05b3c2009-10-30 14:17:42 -0700949 return -1;
950 }
951 if (usedNetworkType != networkType) {
Robert Greenwaltaffc3a12009-09-27 17:27:04 -0700952 Integer currentPid = new Integer(pid);
Robert Greenwaltaffc3a12009-09-27 17:27:04 -0700953 mNetRequestersPids[usedNetworkType].remove(currentPid);
Robert Greenwalt0ca68a02009-12-17 14:54:59 -0800954 reassessPidDns(pid, true);
Robert Greenwaltaffc3a12009-09-27 17:27:04 -0700955 if (mNetRequestersPids[usedNetworkType].size() != 0) {
Wink Savillee70c6f52010-12-03 12:01:38 -0800956 if (DBG) log("not tearing down special network - " +
Robert Greenwaltaffc3a12009-09-27 17:27:04 -0700957 "others still using it");
958 return 1;
959 }
960 callTeardown = true;
Robert Greenwalt9f3be4c2011-01-10 11:58:31 -0800961 } else {
962 if (DBG) log("not a known feature - dropping");
Robert Greenwaltaffc3a12009-09-27 17:27:04 -0700963 }
964 }
Wink Savillee70c6f52010-12-03 12:01:38 -0800965 if (DBG) log("Doing network teardown");
Robert Greenwaltaffc3a12009-09-27 17:27:04 -0700966 if (callTeardown) {
967 tracker.teardown();
Robert Greenwalt2034b912009-08-12 16:08:25 -0700968 return 1;
969 } else {
Robert Greenwaltd391e892010-05-18 10:52:51 -0700970 return -1;
Robert Greenwalt2034b912009-08-12 16:08:25 -0700971 }
The Android Open Source Project28527d22009-03-03 19:31:44 -0800972 }
973
974 /**
Robert Greenwalt7fe44cb2010-08-27 09:24:29 -0700975 * @deprecated use requestRouteToHostAddress instead
976 *
The Android Open Source Project28527d22009-03-03 19:31:44 -0800977 * Ensure that a network route exists to deliver traffic to the specified
978 * host via the specified network interface.
Robert Greenwalt0659da32009-07-16 17:21:39 -0700979 * @param networkType the type of the network over which traffic to the
980 * specified host is to be routed
981 * @param hostAddress the IP address of the host to which the route is
982 * desired
The Android Open Source Project28527d22009-03-03 19:31:44 -0800983 * @return {@code true} on success, {@code false} on failure
984 */
985 public boolean requestRouteToHost(int networkType, int hostAddress) {
Robert Greenwalt7fe44cb2010-08-27 09:24:29 -0700986 InetAddress inetAddress = NetworkUtils.intToInetAddress(hostAddress);
987
988 if (inetAddress == null) {
989 return false;
990 }
991
992 return requestRouteToHostAddress(networkType, inetAddress.getAddress());
993 }
994
995 /**
996 * Ensure that a network route exists to deliver traffic to the specified
997 * host via the specified network interface.
998 * @param networkType the type of the network over which traffic to the
999 * specified host is to be routed
1000 * @param hostAddress the IP address of the host to which the route is
1001 * desired
1002 * @return {@code true} on success, {@code false} on failure
1003 */
1004 public boolean requestRouteToHostAddress(int networkType, byte[] hostAddress) {
The Android Open Source Project28527d22009-03-03 19:31:44 -08001005 enforceChangePermission();
1006 if (!ConnectivityManager.isNetworkTypeValid(networkType)) {
1007 return false;
1008 }
1009 NetworkStateTracker tracker = mNetTrackers[networkType];
Robert Greenwalt4666ed02009-09-10 15:06:20 -07001010
Robert Greenwaltec05b3c2009-10-30 14:17:42 -07001011 if (tracker == null || !tracker.getNetworkInfo().isConnected() ||
1012 tracker.isTeardownRequested()) {
Robert Greenwalt4666ed02009-09-10 15:06:20 -07001013 if (DBG) {
Wink Savillee70c6f52010-12-03 12:01:38 -08001014 log("requestRouteToHostAddress on down network " +
Robert Greenwalt7fe44cb2010-08-27 09:24:29 -07001015 "(" + networkType + ") - dropped");
Robert Greenwalt4666ed02009-09-10 15:06:20 -07001016 }
1017 return false;
The Android Open Source Project28527d22009-03-03 19:31:44 -08001018 }
Robert Greenwalta7dfbd32010-06-15 15:43:39 -07001019 try {
Robert Greenwalt7fe44cb2010-08-27 09:24:29 -07001020 InetAddress addr = InetAddress.getByAddress(hostAddress);
Robert Greenwaltbd492212011-05-06 17:10:53 -07001021 return addHostRoute(tracker, addr, 0);
Robert Greenwalta7dfbd32010-06-15 15:43:39 -07001022 } catch (UnknownHostException e) {}
1023 return false;
Irfan Sheriff7f132d92010-06-09 15:39:36 -07001024 }
1025
1026 /**
1027 * Ensure that a network route exists to deliver traffic to the specified
1028 * host via the mobile data network.
1029 * @param hostAddress the IP address of the host to which the route is desired,
1030 * in network byte order.
Robert Greenwalta7dfbd32010-06-15 15:43:39 -07001031 * TODO - deprecate
Irfan Sheriff7f132d92010-06-09 15:39:36 -07001032 * @return {@code true} on success, {@code false} on failure
1033 */
Robert Greenwaltbd492212011-05-06 17:10:53 -07001034 private boolean addHostRoute(NetworkStateTracker nt, InetAddress hostAddress, int cycleCount) {
Robert Greenwaltbd492212011-05-06 17:10:53 -07001035 LinkProperties lp = nt.getLinkProperties();
1036 if ((lp == null) || (hostAddress == null)) return false;
Irfan Sheriff7f132d92010-06-09 15:39:36 -07001037
Robert Greenwaltbd492212011-05-06 17:10:53 -07001038 String interfaceName = lp.getInterfaceName();
Irfan Sheriff7f132d92010-06-09 15:39:36 -07001039 if (DBG) {
Robert Greenwaltbd492212011-05-06 17:10:53 -07001040 log("Requested host route to " + hostAddress + "(" + interfaceName + "), cycleCount=" +
1041 cycleCount);
Irfan Sheriff7f132d92010-06-09 15:39:36 -07001042 }
Robert Greenwaltbd492212011-05-06 17:10:53 -07001043 if (interfaceName == null) {
Wink Savillee70c6f52010-12-03 12:01:38 -08001044 if (DBG) loge("addHostRoute failed due to null interface name");
Irfan Sheriff7f132d92010-06-09 15:39:36 -07001045 return false;
1046 }
Robert Greenwaltbd492212011-05-06 17:10:53 -07001047
1048 RouteInfo bestRoute = RouteInfo.selectBestRoute(lp.getRoutes(), hostAddress);
Robert Greenwalt355205c2011-05-10 15:05:02 -07001049 InetAddress gatewayAddress = null;
Robert Greenwaltbd492212011-05-06 17:10:53 -07001050 if (bestRoute != null) {
Robert Greenwalt355205c2011-05-10 15:05:02 -07001051 gatewayAddress = bestRoute.getGateway();
Robert Greenwaltbd492212011-05-06 17:10:53 -07001052 // if the best route is ourself, don't relf-reference, just add the host route
Robert Greenwalt355205c2011-05-10 15:05:02 -07001053 if (hostAddress.equals(gatewayAddress)) gatewayAddress = null;
Robert Greenwaltbd492212011-05-06 17:10:53 -07001054 }
Robert Greenwalt355205c2011-05-10 15:05:02 -07001055 if (gatewayAddress != null) {
Robert Greenwaltbd492212011-05-06 17:10:53 -07001056 if (cycleCount > MAX_HOSTROUTE_CYCLE_COUNT) {
1057 loge("Error adding hostroute - too much recursion");
1058 return false;
1059 }
Robert Greenwalt355205c2011-05-10 15:05:02 -07001060 if (!addHostRoute(nt, gatewayAddress, cycleCount+1)) return false;
Robert Greenwaltbd492212011-05-06 17:10:53 -07001061 }
Robert Greenwalt355205c2011-05-10 15:05:02 -07001062
1063 RouteInfo route = RouteInfo.makeHostRoute(hostAddress, gatewayAddress);
1064
1065 try {
1066 mNetd.addRoute(interfaceName, route);
1067 return true;
1068 } catch (Exception ex) {
1069 return false;
1070 }
Robert Greenwaltbd492212011-05-06 17:10:53 -07001071 }
1072
1073 // TODO support the removal of single host routes. Keep a ref count of them so we
1074 // aren't over-zealous
1075 private boolean removeHostRoute(NetworkStateTracker nt, InetAddress hostAddress) {
1076 return false;
The Android Open Source Project28527d22009-03-03 19:31:44 -08001077 }
1078
1079 /**
1080 * @see ConnectivityManager#getBackgroundDataSetting()
1081 */
1082 public boolean getBackgroundDataSetting() {
Robert Greenwaltd62c7002010-12-29 16:15:02 -08001083 return mBackgroundDataEnabled.get();
The Android Open Source Project28527d22009-03-03 19:31:44 -08001084 }
Robert Greenwalt0659da32009-07-16 17:21:39 -07001085
The Android Open Source Project28527d22009-03-03 19:31:44 -08001086 /**
1087 * @see ConnectivityManager#setBackgroundDataSetting(boolean)
1088 */
1089 public void setBackgroundDataSetting(boolean allowBackgroundDataUsage) {
1090 mContext.enforceCallingOrSelfPermission(
1091 android.Manifest.permission.CHANGE_BACKGROUND_DATA_SETTING,
1092 "ConnectivityService");
Robert Greenwalt0659da32009-07-16 17:21:39 -07001093
Robert Greenwaltd62c7002010-12-29 16:15:02 -08001094 mBackgroundDataEnabled.set(allowBackgroundDataUsage);
1095
Robert Greenwalt6a2db8a2010-09-23 10:05:56 -07001096 mHandler.sendMessage(mHandler.obtainMessage(EVENT_SET_BACKGROUND_DATA,
1097 (allowBackgroundDataUsage ? ENABLED : DISABLED), 0));
1098 }
The Android Open Source Project28527d22009-03-03 19:31:44 -08001099
Robert Greenwalt6a2db8a2010-09-23 10:05:56 -07001100 private void handleSetBackgroundData(boolean enabled) {
Robert Greenwalt0ffdef12011-02-25 13:44:09 -08001101 Settings.Secure.putInt(mContext.getContentResolver(),
1102 Settings.Secure.BACKGROUND_DATA, enabled ? 1 : 0);
1103 Intent broadcast = new Intent(
1104 ConnectivityManager.ACTION_BACKGROUND_DATA_SETTING_CHANGED);
1105 mContext.sendBroadcast(broadcast);
Robert Greenwalt0659da32009-07-16 17:21:39 -07001106 }
1107
Robert Greenwalt1b21f6c2010-02-23 18:58:05 -08001108 /**
1109 * @see ConnectivityManager#getMobileDataEnabled()
1110 */
1111 public boolean getMobileDataEnabled() {
Wink Savilleb9024c62010-12-07 10:31:02 -08001112 // TODO: This detail should probably be in DataConnectionTracker's
1113 // which is where we store the value and maybe make this
1114 // asynchronous.
Robert Greenwalt1b21f6c2010-02-23 18:58:05 -08001115 enforceAccessPermission();
1116 boolean retVal = Settings.Secure.getInt(mContext.getContentResolver(),
1117 Settings.Secure.MOBILE_DATA, 1) == 1;
Wink Savillee70c6f52010-12-03 12:01:38 -08001118 if (DBG) log("getMobileDataEnabled returning " + retVal);
Robert Greenwalt1b21f6c2010-02-23 18:58:05 -08001119 return retVal;
1120 }
1121
Robert Greenwalt34848c02011-03-25 13:09:25 -07001122 public void setDataDependency(int networkType, boolean met) {
1123 enforceChangePermission();
1124 if (DBG) {
1125 log("setDataDependency(" + networkType + ", " + met + ")");
1126 }
1127 mHandler.sendMessage(mHandler.obtainMessage(EVENT_SET_DEPENDENCY_MET,
1128 (met ? ENABLED : DISABLED), networkType));
1129 }
1130
1131 private void handleSetDependencyMet(int networkType, boolean met) {
1132 if (mNetTrackers[networkType] != null) {
1133 if (DBG) {
1134 log("handleSetDependencyMet(" + networkType + ", " + met + ")");
1135 }
1136 mNetTrackers[networkType].setDependencyMet(met);
1137 }
1138 }
1139
Jeff Sharkey921ebf22011-05-19 17:12:49 -07001140 private INetworkPolicyListener mPolicyListener = new INetworkPolicyListener.Stub() {
1141 @Override
1142 public void onRulesChanged(int uid, int uidRules) {
1143 // only someone like NPMS should only be calling us
Jeff Sharkey4434b0b2011-06-16 13:04:20 -07001144 mContext.enforceCallingOrSelfPermission(MANAGE_NETWORK_POLICY, TAG);
Jeff Sharkey921ebf22011-05-19 17:12:49 -07001145
1146 if (LOGD_RULES) {
1147 Slog.d(TAG, "onRulesChanged(uid=" + uid + ", uidRules=" + uidRules + ")");
1148 }
1149
1150 synchronized (mUidRules) {
1151 // skip update when we've already applied rules
1152 final int oldRules = mUidRules.get(uid, RULE_ALLOW_ALL);
1153 if (oldRules == uidRules) return;
1154
1155 mUidRules.put(uid, uidRules);
1156 }
1157
1158 // TODO: dispatch into NMS to push rules towards kernel module
1159 // TODO: notify UID when it has requested targeted updates
1160 }
1161 };
1162
Robert Greenwalt1b21f6c2010-02-23 18:58:05 -08001163 /**
1164 * @see ConnectivityManager#setMobileDataEnabled(boolean)
1165 */
Robert Greenwalt6a2db8a2010-09-23 10:05:56 -07001166 public void setMobileDataEnabled(boolean enabled) {
Robert Greenwalt1b21f6c2010-02-23 18:58:05 -08001167 enforceChangePermission();
Wink Savillee70c6f52010-12-03 12:01:38 -08001168 if (DBG) log("setMobileDataEnabled(" + enabled + ")");
Robert Greenwalt1b21f6c2010-02-23 18:58:05 -08001169
Robert Greenwalt6a2db8a2010-09-23 10:05:56 -07001170 mHandler.sendMessage(mHandler.obtainMessage(EVENT_SET_MOBILE_DATA,
Robert Greenwalt34848c02011-03-25 13:09:25 -07001171 (enabled ? ENABLED : DISABLED), 0));
Robert Greenwalt6a2db8a2010-09-23 10:05:56 -07001172 }
1173
1174 private void handleSetMobileData(boolean enabled) {
Wink Savilleb9024c62010-12-07 10:31:02 -08001175 if (mNetTrackers[ConnectivityManager.TYPE_MOBILE] != null) {
1176 if (DBG) {
1177 Slog.d(TAG, mNetTrackers[ConnectivityManager.TYPE_MOBILE].toString() + enabled);
Robert Greenwalt1b21f6c2010-02-23 18:58:05 -08001178 }
Wink Savilleb9024c62010-12-07 10:31:02 -08001179 mNetTrackers[ConnectivityManager.TYPE_MOBILE].setDataEnable(enabled);
Robert Greenwalt1b21f6c2010-02-23 18:58:05 -08001180 }
1181 }
1182
The Android Open Source Project28527d22009-03-03 19:31:44 -08001183 private void enforceAccessPermission() {
Robert Greenwalt0659da32009-07-16 17:21:39 -07001184 mContext.enforceCallingOrSelfPermission(
1185 android.Manifest.permission.ACCESS_NETWORK_STATE,
1186 "ConnectivityService");
The Android Open Source Project28527d22009-03-03 19:31:44 -08001187 }
1188
1189 private void enforceChangePermission() {
Robert Greenwalt0659da32009-07-16 17:21:39 -07001190 mContext.enforceCallingOrSelfPermission(
1191 android.Manifest.permission.CHANGE_NETWORK_STATE,
1192 "ConnectivityService");
The Android Open Source Project28527d22009-03-03 19:31:44 -08001193 }
1194
Robert Greenwalt0c4828c2010-01-26 11:40:34 -08001195 // TODO Make this a special check when it goes public
1196 private void enforceTetherChangePermission() {
1197 mContext.enforceCallingOrSelfPermission(
1198 android.Manifest.permission.CHANGE_NETWORK_STATE,
1199 "ConnectivityService");
1200 }
1201
Robert Greenwalt8e87f122010-02-11 18:18:40 -08001202 private void enforceTetherAccessPermission() {
1203 mContext.enforceCallingOrSelfPermission(
1204 android.Manifest.permission.ACCESS_NETWORK_STATE,
1205 "ConnectivityService");
1206 }
1207
Robert Greenwalt93dc1042010-06-15 12:19:37 -07001208 private void enforceConnectivityInternalPermission() {
1209 mContext.enforceCallingOrSelfPermission(
1210 android.Manifest.permission.CONNECTIVITY_INTERNAL,
1211 "ConnectivityService");
1212 }
1213
The Android Open Source Project28527d22009-03-03 19:31:44 -08001214 /**
Robert Greenwalt0659da32009-07-16 17:21:39 -07001215 * Handle a {@code DISCONNECTED} event. If this pertains to the non-active
1216 * network, we ignore it. If it is for the active network, we send out a
1217 * broadcast. But first, we check whether it might be possible to connect
1218 * to a different network.
The Android Open Source Project28527d22009-03-03 19:31:44 -08001219 * @param info the {@code NetworkInfo} for the network
1220 */
1221 private void handleDisconnect(NetworkInfo info) {
1222
Robert Greenwalt2034b912009-08-12 16:08:25 -07001223 int prevNetType = info.getType();
The Android Open Source Project28527d22009-03-03 19:31:44 -08001224
Robert Greenwalt2034b912009-08-12 16:08:25 -07001225 mNetTrackers[prevNetType].setTeardownRequested(false);
The Android Open Source Project28527d22009-03-03 19:31:44 -08001226 /*
1227 * If the disconnected network is not the active one, then don't report
1228 * this as a loss of connectivity. What probably happened is that we're
1229 * getting the disconnect for a network that we explicitly disabled
1230 * in accordance with network preference policies.
1231 */
Robert Greenwalt34848c02011-03-25 13:09:25 -07001232 if (!mNetConfigs[prevNetType].isDefault()) {
Robert Greenwalt2034b912009-08-12 16:08:25 -07001233 List pids = mNetRequestersPids[prevNetType];
1234 for (int i = 0; i<pids.size(); i++) {
1235 Integer pid = (Integer)pids.get(i);
1236 // will remove them because the net's no longer connected
1237 // need to do this now as only now do we know the pids and
1238 // can properly null things that are no longer referenced.
1239 reassessPidDns(pid.intValue(), false);
The Android Open Source Project28527d22009-03-03 19:31:44 -08001240 }
The Android Open Source Project28527d22009-03-03 19:31:44 -08001241 }
1242
The Android Open Source Project28527d22009-03-03 19:31:44 -08001243 Intent intent = new Intent(ConnectivityManager.CONNECTIVITY_ACTION);
1244 intent.putExtra(ConnectivityManager.EXTRA_NETWORK_INFO, info);
1245 if (info.isFailover()) {
1246 intent.putExtra(ConnectivityManager.EXTRA_IS_FAILOVER, true);
1247 info.setFailover(false);
1248 }
1249 if (info.getReason() != null) {
1250 intent.putExtra(ConnectivityManager.EXTRA_REASON, info.getReason());
1251 }
1252 if (info.getExtraInfo() != null) {
Robert Greenwalt0659da32009-07-16 17:21:39 -07001253 intent.putExtra(ConnectivityManager.EXTRA_EXTRA_INFO,
1254 info.getExtraInfo());
The Android Open Source Project28527d22009-03-03 19:31:44 -08001255 }
Robert Greenwalt2034b912009-08-12 16:08:25 -07001256
Robert Greenwalt34848c02011-03-25 13:09:25 -07001257 if (mNetConfigs[prevNetType].isDefault()) {
Robert Greenwalt4b7c55e2011-01-11 13:56:33 -08001258 tryFailover(prevNetType);
1259 if (mActiveDefaultNetwork != -1) {
1260 NetworkInfo switchTo = mNetTrackers[mActiveDefaultNetwork].getNetworkInfo();
Robert Greenwalt3cc68d32010-01-25 17:54:29 -08001261 intent.putExtra(ConnectivityManager.EXTRA_OTHER_NETWORK_INFO, switchTo);
1262 } else {
Robert Greenwaltb2a9e492010-09-20 18:01:43 -07001263 mDefaultInetConditionPublished = 0; // we're not connected anymore
Robert Greenwalt3cc68d32010-01-25 17:54:29 -08001264 intent.putExtra(ConnectivityManager.EXTRA_NO_CONNECTIVITY, true);
1265 }
Robert Greenwaltf55ced92010-01-20 19:29:41 -08001266 }
Robert Greenwaltb2a9e492010-09-20 18:01:43 -07001267 intent.putExtra(ConnectivityManager.EXTRA_INET_CONDITION, mDefaultInetConditionPublished);
Robert Greenwaltf55ced92010-01-20 19:29:41 -08001268 // do this before we broadcast the change
Robert Greenwalt3afbead2010-07-23 15:46:26 -07001269 handleConnectivityChange(prevNetType);
Robert Greenwaltf55ced92010-01-20 19:29:41 -08001270
1271 sendStickyBroadcast(intent);
1272 /*
1273 * If the failover network is already connected, then immediately send
1274 * out a followup broadcast indicating successful failover
1275 */
Robert Greenwalt4b7c55e2011-01-11 13:56:33 -08001276 if (mActiveDefaultNetwork != -1) {
1277 sendConnectedBroadcast(mNetTrackers[mActiveDefaultNetwork].getNetworkInfo());
Robert Greenwaltf55ced92010-01-20 19:29:41 -08001278 }
1279 }
1280
Robert Greenwalt4b7c55e2011-01-11 13:56:33 -08001281 private void tryFailover(int prevNetType) {
Robert Greenwalt2034b912009-08-12 16:08:25 -07001282 /*
Robert Greenwalt92564852011-01-06 15:41:07 -08001283 * If this is a default network, check if other defaults are available.
1284 * Try to reconnect on all available and let them hash it out when
1285 * more than one connects.
Robert Greenwalt2034b912009-08-12 16:08:25 -07001286 */
Robert Greenwalt34848c02011-03-25 13:09:25 -07001287 if (mNetConfigs[prevNetType].isDefault()) {
Robert Greenwalt2034b912009-08-12 16:08:25 -07001288 if (mActiveDefaultNetwork == prevNetType) {
1289 mActiveDefaultNetwork = -1;
1290 }
1291
Robert Greenwalt4b7c55e2011-01-11 13:56:33 -08001292 // don't signal a reconnect for anything lower or equal priority than our
1293 // current connected default
1294 // TODO - don't filter by priority now - nice optimization but risky
1295// int currentPriority = -1;
1296// if (mActiveDefaultNetwork != -1) {
Robert Greenwalt34848c02011-03-25 13:09:25 -07001297// currentPriority = mNetConfigs[mActiveDefaultNetwork].mPriority;
Robert Greenwalt4b7c55e2011-01-11 13:56:33 -08001298// }
Robert Greenwaltf55ced92010-01-20 19:29:41 -08001299 for (int checkType=0; checkType <= ConnectivityManager.MAX_NETWORK_TYPE; checkType++) {
Robert Greenwaltec05b3c2009-10-30 14:17:42 -07001300 if (checkType == prevNetType) continue;
Robert Greenwalt34848c02011-03-25 13:09:25 -07001301 if (mNetConfigs[checkType] == null) continue;
1302 if (!mNetConfigs[checkType].isDefault()) continue;
Wink Saville72a95b92011-01-26 15:43:49 -08001303
1304// Enabling the isAvailable() optimization caused mobile to not get
1305// selected if it was in the middle of error handling. Specifically
1306// a moble connection that took 30 seconds to complete the DEACTIVATE_DATA_CALL
1307// would not be available and we wouldn't get connected to anything.
1308// So removing the isAvailable() optimization below for now. TODO: This
1309// optimization should work and we need to investigate why it doesn't work.
1310// This could be related to how DEACTIVATE_DATA_CALL is reporting its
1311// complete before it is really complete.
1312// if (!mNetTrackers[checkType].isAvailable()) continue;
1313
Robert Greenwalt34848c02011-03-25 13:09:25 -07001314// if (currentPriority >= mNetConfigs[checkType].mPriority) continue;
Robert Greenwalt2034b912009-08-12 16:08:25 -07001315
Robert Greenwalt4b7c55e2011-01-11 13:56:33 -08001316 NetworkStateTracker checkTracker = mNetTrackers[checkType];
1317 NetworkInfo checkInfo = checkTracker.getNetworkInfo();
1318 if (!checkInfo.isConnectedOrConnecting() || checkTracker.isTeardownRequested()) {
1319 checkInfo.setFailover(true);
1320 checkTracker.reconnect();
Robert Greenwalt2034b912009-08-12 16:08:25 -07001321 }
Robert Greenwalt4b7c55e2011-01-11 13:56:33 -08001322 if (DBG) log("Attempting to switch to " + checkInfo.getTypeName());
Robert Greenwalt2034b912009-08-12 16:08:25 -07001323 }
The Android Open Source Project28527d22009-03-03 19:31:44 -08001324 }
The Android Open Source Project28527d22009-03-03 19:31:44 -08001325 }
1326
1327 private void sendConnectedBroadcast(NetworkInfo info) {
Robert Greenwaltd3401f92010-09-15 17:36:33 -07001328 sendGeneralBroadcast(info, ConnectivityManager.CONNECTIVITY_ACTION);
1329 }
1330
1331 private void sendInetConditionBroadcast(NetworkInfo info) {
1332 sendGeneralBroadcast(info, ConnectivityManager.INET_CONDITION_ACTION);
1333 }
1334
1335 private void sendGeneralBroadcast(NetworkInfo info, String bcastType) {
1336 Intent intent = new Intent(bcastType);
The Android Open Source Project28527d22009-03-03 19:31:44 -08001337 intent.putExtra(ConnectivityManager.EXTRA_NETWORK_INFO, info);
1338 if (info.isFailover()) {
1339 intent.putExtra(ConnectivityManager.EXTRA_IS_FAILOVER, true);
1340 info.setFailover(false);
1341 }
1342 if (info.getReason() != null) {
1343 intent.putExtra(ConnectivityManager.EXTRA_REASON, info.getReason());
1344 }
1345 if (info.getExtraInfo() != null) {
Robert Greenwalt0659da32009-07-16 17:21:39 -07001346 intent.putExtra(ConnectivityManager.EXTRA_EXTRA_INFO,
1347 info.getExtraInfo());
The Android Open Source Project28527d22009-03-03 19:31:44 -08001348 }
Robert Greenwalt986c7412010-09-08 15:24:47 -07001349 intent.putExtra(ConnectivityManager.EXTRA_INET_CONDITION, mDefaultInetConditionPublished);
Mike Lockwoodfde2b762009-08-14 14:18:49 -04001350 sendStickyBroadcast(intent);
The Android Open Source Project28527d22009-03-03 19:31:44 -08001351 }
1352
1353 /**
1354 * Called when an attempt to fail over to another network has failed.
1355 * @param info the {@link NetworkInfo} for the failed network
1356 */
1357 private void handleConnectionFailure(NetworkInfo info) {
1358 mNetTrackers[info.getType()].setTeardownRequested(false);
The Android Open Source Project28527d22009-03-03 19:31:44 -08001359
Robert Greenwalt2034b912009-08-12 16:08:25 -07001360 String reason = info.getReason();
1361 String extraInfo = info.getExtraInfo();
Robert Greenwalt0659da32009-07-16 17:21:39 -07001362
Robert Greenwalte981bc52010-10-08 16:35:52 -07001363 String reasonText;
1364 if (reason == null) {
1365 reasonText = ".";
1366 } else {
1367 reasonText = " (" + reason + ").";
The Android Open Source Project28527d22009-03-03 19:31:44 -08001368 }
Wink Savillee70c6f52010-12-03 12:01:38 -08001369 loge("Attempt to connect to " + info.getTypeName() + " failed" + reasonText);
Robert Greenwalt2034b912009-08-12 16:08:25 -07001370
1371 Intent intent = new Intent(ConnectivityManager.CONNECTIVITY_ACTION);
1372 intent.putExtra(ConnectivityManager.EXTRA_NETWORK_INFO, info);
1373 if (getActiveNetworkInfo() == null) {
1374 intent.putExtra(ConnectivityManager.EXTRA_NO_CONNECTIVITY, true);
1375 }
1376 if (reason != null) {
1377 intent.putExtra(ConnectivityManager.EXTRA_REASON, reason);
1378 }
1379 if (extraInfo != null) {
1380 intent.putExtra(ConnectivityManager.EXTRA_EXTRA_INFO, extraInfo);
1381 }
1382 if (info.isFailover()) {
1383 intent.putExtra(ConnectivityManager.EXTRA_IS_FAILOVER, true);
1384 info.setFailover(false);
1385 }
Robert Greenwaltf55ced92010-01-20 19:29:41 -08001386
Robert Greenwalt34848c02011-03-25 13:09:25 -07001387 if (mNetConfigs[info.getType()].isDefault()) {
Robert Greenwalt4b7c55e2011-01-11 13:56:33 -08001388 tryFailover(info.getType());
1389 if (mActiveDefaultNetwork != -1) {
1390 NetworkInfo switchTo = mNetTrackers[mActiveDefaultNetwork].getNetworkInfo();
Robert Greenwalt3cc68d32010-01-25 17:54:29 -08001391 intent.putExtra(ConnectivityManager.EXTRA_OTHER_NETWORK_INFO, switchTo);
1392 } else {
Robert Greenwaltb2a9e492010-09-20 18:01:43 -07001393 mDefaultInetConditionPublished = 0;
Robert Greenwalt3cc68d32010-01-25 17:54:29 -08001394 intent.putExtra(ConnectivityManager.EXTRA_NO_CONNECTIVITY, true);
1395 }
Robert Greenwaltf55ced92010-01-20 19:29:41 -08001396 }
Robert Greenwalt3cc68d32010-01-25 17:54:29 -08001397
Robert Greenwaltb2a9e492010-09-20 18:01:43 -07001398 intent.putExtra(ConnectivityManager.EXTRA_INET_CONDITION, mDefaultInetConditionPublished);
Mike Lockwoodfde2b762009-08-14 14:18:49 -04001399 sendStickyBroadcast(intent);
Robert Greenwaltf55ced92010-01-20 19:29:41 -08001400 /*
1401 * If the failover network is already connected, then immediately send
1402 * out a followup broadcast indicating successful failover
1403 */
Robert Greenwalt4b7c55e2011-01-11 13:56:33 -08001404 if (mActiveDefaultNetwork != -1) {
1405 sendConnectedBroadcast(mNetTrackers[mActiveDefaultNetwork].getNetworkInfo());
Robert Greenwaltf55ced92010-01-20 19:29:41 -08001406 }
Mike Lockwoodfde2b762009-08-14 14:18:49 -04001407 }
1408
1409 private void sendStickyBroadcast(Intent intent) {
1410 synchronized(this) {
Dianne Hackborna417ff82009-12-08 19:45:14 -08001411 if (!mSystemReady) {
1412 mInitialBroadcast = new Intent(intent);
Mike Lockwoodfde2b762009-08-14 14:18:49 -04001413 }
Dianne Hackborna417ff82009-12-08 19:45:14 -08001414 intent.addFlags(Intent.FLAG_RECEIVER_REGISTERED_ONLY_BEFORE_BOOT);
1415 mContext.sendStickyBroadcast(intent);
Mike Lockwoodfde2b762009-08-14 14:18:49 -04001416 }
1417 }
1418
1419 void systemReady() {
1420 synchronized(this) {
1421 mSystemReady = true;
Dianne Hackborna417ff82009-12-08 19:45:14 -08001422 if (mInitialBroadcast != null) {
1423 mContext.sendStickyBroadcast(mInitialBroadcast);
1424 mInitialBroadcast = null;
Mike Lockwoodfde2b762009-08-14 14:18:49 -04001425 }
1426 }
Robert Greenwaltc3c5f862010-10-11 16:00:27 -07001427 // load the global proxy at startup
1428 mHandler.sendMessage(mHandler.obtainMessage(EVENT_APPLY_GLOBAL_HTTP_PROXY));
The Android Open Source Project28527d22009-03-03 19:31:44 -08001429 }
1430
1431 private void handleConnect(NetworkInfo info) {
Robert Greenwalt2034b912009-08-12 16:08:25 -07001432 int type = info.getType();
The Android Open Source Project28527d22009-03-03 19:31:44 -08001433
1434 // snapshot isFailover, because sendConnectedBroadcast() resets it
1435 boolean isFailover = info.isFailover();
Robert Greenwalt2034b912009-08-12 16:08:25 -07001436 NetworkStateTracker thisNet = mNetTrackers[type];
The Android Open Source Project28527d22009-03-03 19:31:44 -08001437
Robert Greenwalt2034b912009-08-12 16:08:25 -07001438 // if this is a default net and other default is running
1439 // kill the one not preferred
Robert Greenwalt34848c02011-03-25 13:09:25 -07001440 if (mNetConfigs[type].isDefault()) {
Robert Greenwalt2034b912009-08-12 16:08:25 -07001441 if (mActiveDefaultNetwork != -1 && mActiveDefaultNetwork != type) {
1442 if ((type != mNetworkPreference &&
Wink Savillef2a62832011-04-07 14:23:45 -07001443 mNetConfigs[mActiveDefaultNetwork].priority >
1444 mNetConfigs[type].priority) ||
Robert Greenwalt2034b912009-08-12 16:08:25 -07001445 mNetworkPreference == mActiveDefaultNetwork) {
1446 // don't accept this one
Wink Savillee70c6f52010-12-03 12:01:38 -08001447 if (DBG) {
1448 log("Not broadcasting CONNECT_ACTION " +
Robert Greenwalt2034b912009-08-12 16:08:25 -07001449 "to torn down network " + info.getTypeName());
Wink Savillee70c6f52010-12-03 12:01:38 -08001450 }
Robert Greenwalt2034b912009-08-12 16:08:25 -07001451 teardown(thisNet);
1452 return;
1453 } else {
1454 // tear down the other
1455 NetworkStateTracker otherNet =
1456 mNetTrackers[mActiveDefaultNetwork];
Wink Savillee70c6f52010-12-03 12:01:38 -08001457 if (DBG) {
1458 log("Policy requires " + otherNet.getNetworkInfo().getTypeName() +
Robert Greenwalt2034b912009-08-12 16:08:25 -07001459 " teardown");
Wink Savillee70c6f52010-12-03 12:01:38 -08001460 }
Robert Greenwalt2034b912009-08-12 16:08:25 -07001461 if (!teardown(otherNet)) {
Wink Savillee70c6f52010-12-03 12:01:38 -08001462 loge("Network declined teardown request");
Robert Greenwalt99910172011-03-29 11:36:28 -07001463 teardown(thisNet);
Robert Greenwalt2034b912009-08-12 16:08:25 -07001464 return;
1465 }
Robert Greenwalt93dc1042010-06-15 12:19:37 -07001466 }
1467 }
1468 synchronized (ConnectivityService.this) {
1469 // have a new default network, release the transition wakelock in a second
1470 // if it's held. The second pause is to allow apps to reconnect over the
1471 // new network
1472 if (mNetTransitionWakeLock.isHeld()) {
1473 mHandler.sendMessageDelayed(mHandler.obtainMessage(
Robert Greenwaltccb36f92010-09-24 14:32:21 -07001474 EVENT_CLEAR_NET_TRANSITION_WAKELOCK,
Robert Greenwalt93dc1042010-06-15 12:19:37 -07001475 mNetTransitionWakeLockSerialNumber, 0),
1476 1000);
Robert Greenwalt2034b912009-08-12 16:08:25 -07001477 }
1478 }
1479 mActiveDefaultNetwork = type;
Robert Greenwalt986c7412010-09-08 15:24:47 -07001480 // this will cause us to come up initially as unconnected and switching
1481 // to connected after our normal pause unless somebody reports us as reall
1482 // disconnected
1483 mDefaultInetConditionPublished = 0;
1484 mDefaultConnectionSequence++;
1485 mInetConditionChangeInFlight = false;
1486 // Don't do this - if we never sign in stay, grey
1487 //reportNetworkCondition(mActiveDefaultNetwork, 100);
Robert Greenwalt2034b912009-08-12 16:08:25 -07001488 }
The Android Open Source Project28527d22009-03-03 19:31:44 -08001489 thisNet.setTeardownRequested(false);
Irfan Sheriff7f132d92010-06-09 15:39:36 -07001490 updateNetworkSettings(thisNet);
Robert Greenwalt3afbead2010-07-23 15:46:26 -07001491 handleConnectivityChange(type);
Robert Greenwalt2034b912009-08-12 16:08:25 -07001492 sendConnectedBroadcast(info);
The Android Open Source Project28527d22009-03-03 19:31:44 -08001493 }
1494
The Android Open Source Project28527d22009-03-03 19:31:44 -08001495 /**
Robert Greenwalt3afbead2010-07-23 15:46:26 -07001496 * After a change in the connectivity state of a network. We're mainly
1497 * concerned with making sure that the list of DNS servers is set up
1498 * according to which networks are connected, and ensuring that the
1499 * right routing table entries exist.
The Android Open Source Project28527d22009-03-03 19:31:44 -08001500 */
Robert Greenwalt3afbead2010-07-23 15:46:26 -07001501 private void handleConnectivityChange(int netType) {
The Android Open Source Project28527d22009-03-03 19:31:44 -08001502 /*
Robert Greenwalt2034b912009-08-12 16:08:25 -07001503 * If a non-default network is enabled, add the host routes that
Robert Greenwalt3afbead2010-07-23 15:46:26 -07001504 * will allow it's DNS servers to be accessed.
The Android Open Source Project28527d22009-03-03 19:31:44 -08001505 */
Robert Greenwalt3afbead2010-07-23 15:46:26 -07001506 handleDnsConfigurationChange(netType);
Robert Greenwalt2034b912009-08-12 16:08:25 -07001507
Robert Greenwalt3afbead2010-07-23 15:46:26 -07001508 if (mNetTrackers[netType].getNetworkInfo().isConnected()) {
Robert Greenwalt34848c02011-03-25 13:09:25 -07001509 if (mNetConfigs[netType].isDefault()) {
Robert Greenwaltc3c5f862010-10-11 16:00:27 -07001510 handleApplyDefaultProxy(netType);
Robert Greenwalt3afbead2010-07-23 15:46:26 -07001511 addDefaultRoute(mNetTrackers[netType]);
Robert Greenwalt2034b912009-08-12 16:08:25 -07001512 } else {
Robert Greenwalt1e2f2862011-04-01 10:51:22 -07001513 // many radios add a default route even when we don't want one.
1514 // remove the default route unless we need it for our active network
1515 if (mActiveDefaultNetwork != -1) {
1516 LinkProperties defaultLinkProperties =
1517 mNetTrackers[mActiveDefaultNetwork].getLinkProperties();
1518 LinkProperties newLinkProperties =
1519 mNetTrackers[netType].getLinkProperties();
1520 String defaultIface = defaultLinkProperties.getInterfaceName();
1521 if (defaultIface != null &&
1522 !defaultIface.equals(newLinkProperties.getInterfaceName())) {
1523 removeDefaultRoute(mNetTrackers[netType]);
1524 }
1525 }
Michael Jurka837e3642011-03-30 19:54:08 -07001526 addPrivateDnsRoutes(mNetTrackers[netType]);
Robert Greenwalt3afbead2010-07-23 15:46:26 -07001527 }
Kazuhiro Ondo3a340412011-04-30 20:10:57 -05001528
1529 /** Notify TetheringService if interface name has been changed. */
1530 if (TextUtils.equals(mNetTrackers[netType].getNetworkInfo().getReason(),
1531 Phone.REASON_LINK_PROPERTIES_CHANGED)) {
1532 handleTetherIfaceChange(netType);
1533 }
Robert Greenwalt3afbead2010-07-23 15:46:26 -07001534 } else {
Robert Greenwalt34848c02011-03-25 13:09:25 -07001535 if (mNetConfigs[netType].isDefault()) {
Robert Greenwalt3afbead2010-07-23 15:46:26 -07001536 removeDefaultRoute(mNetTrackers[netType]);
1537 } else {
1538 removePrivateDnsRoutes(mNetTrackers[netType]);
Robert Greenwalt2034b912009-08-12 16:08:25 -07001539 }
The Android Open Source Project28527d22009-03-03 19:31:44 -08001540 }
1541 }
1542
Irfan Sheriff7f132d92010-06-09 15:39:36 -07001543 private void addPrivateDnsRoutes(NetworkStateTracker nt) {
Irfan Sheriff7f132d92010-06-09 15:39:36 -07001544 boolean privateDnsRouteSet = nt.isPrivateDnsRouteSet();
Robert Greenwalt1f1bcfe2010-08-30 10:56:47 -07001545 LinkProperties p = nt.getLinkProperties();
Robert Greenwalta7dfbd32010-06-15 15:43:39 -07001546 if (p == null) return;
1547 String interfaceName = p.getInterfaceName();
Irfan Sheriff7f132d92010-06-09 15:39:36 -07001548
1549 if (DBG) {
Wink Savillee70c6f52010-12-03 12:01:38 -08001550 log("addPrivateDnsRoutes for " + nt +
Irfan Sheriff7f132d92010-06-09 15:39:36 -07001551 "(" + interfaceName + ") - mPrivateDnsRouteSet = " + privateDnsRouteSet);
1552 }
Irfan Sheriff7f132d92010-06-09 15:39:36 -07001553 if (interfaceName != null && !privateDnsRouteSet) {
Robert Greenwalta7dfbd32010-06-15 15:43:39 -07001554 Collection<InetAddress> dnsList = p.getDnses();
1555 for (InetAddress dns : dnsList) {
Robert Greenwaltbd492212011-05-06 17:10:53 -07001556 addHostRoute(nt, dns, 0);
Irfan Sheriff7f132d92010-06-09 15:39:36 -07001557 }
1558 nt.privateDnsRouteSet(true);
1559 }
1560 }
1561
1562 private void removePrivateDnsRoutes(NetworkStateTracker nt) {
Robert Greenwalt1f1bcfe2010-08-30 10:56:47 -07001563 LinkProperties p = nt.getLinkProperties();
Robert Greenwalta7dfbd32010-06-15 15:43:39 -07001564 if (p == null) return;
1565 String interfaceName = p.getInterfaceName();
Irfan Sheriff7f132d92010-06-09 15:39:36 -07001566 boolean privateDnsRouteSet = nt.isPrivateDnsRouteSet();
1567 if (interfaceName != null && privateDnsRouteSet) {
1568 if (DBG) {
Wink Savillee70c6f52010-12-03 12:01:38 -08001569 log("removePrivateDnsRoutes for " + nt.getNetworkInfo().getTypeName() +
Irfan Sheriff7f132d92010-06-09 15:39:36 -07001570 " (" + interfaceName + ")");
1571 }
Robert Greenwalt355205c2011-05-10 15:05:02 -07001572
1573 Collection<InetAddress> dnsList = p.getDnses();
1574 for (InetAddress dns : dnsList) {
1575 if (DBG) log(" removing " + dns);
1576 RouteInfo route = RouteInfo.makeHostRoute(dns);
1577 try {
1578 mNetd.removeRoute(interfaceName, route);
1579 } catch (Exception ex) {
1580 loge("error (" + ex + ") removing dns route " + route);
1581 }
1582 }
Irfan Sheriff7f132d92010-06-09 15:39:36 -07001583 nt.privateDnsRouteSet(false);
1584 }
1585 }
1586
Irfan Sheriff7f132d92010-06-09 15:39:36 -07001587
1588 private void addDefaultRoute(NetworkStateTracker nt) {
Robert Greenwalt1f1bcfe2010-08-30 10:56:47 -07001589 LinkProperties p = nt.getLinkProperties();
Robert Greenwalta7dfbd32010-06-15 15:43:39 -07001590 if (p == null) return;
1591 String interfaceName = p.getInterfaceName();
Robert Greenwalt5c733972011-02-09 13:56:06 -08001592 if (TextUtils.isEmpty(interfaceName)) return;
Irfan Sheriff7f132d92010-06-09 15:39:36 -07001593
Robert Greenwalt355205c2011-05-10 15:05:02 -07001594 for (RouteInfo route : p.getRoutes()) {
Robert Greenwalt5a901292011-04-28 14:28:50 -07001595 //TODO - handle non-default routes
1596 if (route.isDefaultRoute()) {
Robert Greenwalt355205c2011-05-10 15:05:02 -07001597 if (DBG) log("adding default route " + route);
Robert Greenwalt5a901292011-04-28 14:28:50 -07001598 InetAddress gateway = route.getGateway();
Robert Greenwalt355205c2011-05-10 15:05:02 -07001599 if (addHostRoute(nt, gateway, 0)) {
1600 try {
1601 mNetd.addRoute(interfaceName, route);
1602 } catch (Exception e) {
1603 loge("error adding default route " + route);
1604 continue;
1605 }
Robert Greenwalt5a901292011-04-28 14:28:50 -07001606 if (DBG) {
1607 NetworkInfo networkInfo = nt.getNetworkInfo();
1608 log("addDefaultRoute for " + networkInfo.getTypeName() +
1609 " (" + interfaceName + "), GatewayAddr=" +
1610 gateway.getHostAddress());
1611 }
Robert Greenwalt355205c2011-05-10 15:05:02 -07001612 } else {
1613 loge("error adding host route for default route " + route);
Robert Greenwalt03d53da2011-03-22 18:47:42 -07001614 }
Irfan Sheriff7f132d92010-06-09 15:39:36 -07001615 }
Irfan Sheriff7f132d92010-06-09 15:39:36 -07001616 }
1617 }
1618
1619
1620 public void removeDefaultRoute(NetworkStateTracker nt) {
Robert Greenwalt1f1bcfe2010-08-30 10:56:47 -07001621 LinkProperties p = nt.getLinkProperties();
Robert Greenwalta7dfbd32010-06-15 15:43:39 -07001622 if (p == null) return;
1623 String interfaceName = p.getInterfaceName();
Irfan Sheriff7f132d92010-06-09 15:39:36 -07001624
Robert Greenwalt355205c2011-05-10 15:05:02 -07001625 if (interfaceName == null) return;
1626
1627 for (RouteInfo route : p.getRoutes()) {
1628 //TODO - handle non-default routes
1629 if (route.isDefaultRoute()) {
1630 try {
1631 mNetd.removeRoute(interfaceName, route);
1632 } catch (Exception ex) {
1633 loge("error (" + ex + ") removing default route " + route);
1634 continue;
1635 }
Robert Greenwalt03d53da2011-03-22 18:47:42 -07001636 if (DBG) {
1637 NetworkInfo networkInfo = nt.getNetworkInfo();
1638 log("removeDefaultRoute for " + networkInfo.getTypeName() + " (" +
1639 interfaceName + ")");
1640 }
Irfan Sheriff7f132d92010-06-09 15:39:36 -07001641 }
Irfan Sheriff7f132d92010-06-09 15:39:36 -07001642 }
1643 }
1644
1645 /**
1646 * Reads the network specific TCP buffer sizes from SystemProperties
1647 * net.tcp.buffersize.[default|wifi|umts|edge|gprs] and set them for system
1648 * wide use
1649 */
1650 public void updateNetworkSettings(NetworkStateTracker nt) {
1651 String key = nt.getTcpBufferSizesPropName();
1652 String bufferSizes = SystemProperties.get(key);
1653
1654 if (bufferSizes.length() == 0) {
Wink Savillee70c6f52010-12-03 12:01:38 -08001655 loge(key + " not found in system properties. Using defaults");
Irfan Sheriff7f132d92010-06-09 15:39:36 -07001656
1657 // Setting to default values so we won't be stuck to previous values
1658 key = "net.tcp.buffersize.default";
1659 bufferSizes = SystemProperties.get(key);
1660 }
1661
1662 // Set values in kernel
1663 if (bufferSizes.length() != 0) {
1664 if (DBG) {
Wink Savillee70c6f52010-12-03 12:01:38 -08001665 log("Setting TCP values: [" + bufferSizes
Irfan Sheriff7f132d92010-06-09 15:39:36 -07001666 + "] which comes from [" + key + "]");
1667 }
1668 setBufferSize(bufferSizes);
1669 }
1670 }
1671
1672 /**
1673 * Writes TCP buffer sizes to /sys/kernel/ipv4/tcp_[r/w]mem_[min/def/max]
1674 * which maps to /proc/sys/net/ipv4/tcp_rmem and tcpwmem
1675 *
1676 * @param bufferSizes in the format of "readMin, readInitial, readMax,
1677 * writeMin, writeInitial, writeMax"
1678 */
1679 private void setBufferSize(String bufferSizes) {
1680 try {
1681 String[] values = bufferSizes.split(",");
1682
1683 if (values.length == 6) {
1684 final String prefix = "/sys/kernel/ipv4/tcp_";
Mike Lockwood0d5916c2011-05-28 13:24:04 -04001685 FileUtils.stringToFile(prefix + "rmem_min", values[0]);
1686 FileUtils.stringToFile(prefix + "rmem_def", values[1]);
1687 FileUtils.stringToFile(prefix + "rmem_max", values[2]);
1688 FileUtils.stringToFile(prefix + "wmem_min", values[3]);
1689 FileUtils.stringToFile(prefix + "wmem_def", values[4]);
1690 FileUtils.stringToFile(prefix + "wmem_max", values[5]);
Irfan Sheriff7f132d92010-06-09 15:39:36 -07001691 } else {
Wink Savillee70c6f52010-12-03 12:01:38 -08001692 loge("Invalid buffersize string: " + bufferSizes);
Irfan Sheriff7f132d92010-06-09 15:39:36 -07001693 }
1694 } catch (IOException e) {
Wink Savillee70c6f52010-12-03 12:01:38 -08001695 loge("Can't set tcp buffer sizes:" + e);
Irfan Sheriff7f132d92010-06-09 15:39:36 -07001696 }
1697 }
1698
Robert Greenwalt2034b912009-08-12 16:08:25 -07001699 /**
1700 * Adjust the per-process dns entries (net.dns<x>.<pid>) based
1701 * on the highest priority active net which this process requested.
1702 * If there aren't any, clear it out
1703 */
1704 private void reassessPidDns(int myPid, boolean doBump)
1705 {
Wink Savillee70c6f52010-12-03 12:01:38 -08001706 if (DBG) log("reassessPidDns for pid " + myPid);
Robert Greenwalt2034b912009-08-12 16:08:25 -07001707 for(int i : mPriorityList) {
Robert Greenwalt34848c02011-03-25 13:09:25 -07001708 if (mNetConfigs[i].isDefault()) {
Robert Greenwalt2034b912009-08-12 16:08:25 -07001709 continue;
1710 }
1711 NetworkStateTracker nt = mNetTrackers[i];
Robert Greenwalt0659da32009-07-16 17:21:39 -07001712 if (nt.getNetworkInfo().isConnected() &&
1713 !nt.isTeardownRequested()) {
Robert Greenwalt1f1bcfe2010-08-30 10:56:47 -07001714 LinkProperties p = nt.getLinkProperties();
Robert Greenwalta7dfbd32010-06-15 15:43:39 -07001715 if (p == null) continue;
Robert Greenwalt2034b912009-08-12 16:08:25 -07001716 List pids = mNetRequestersPids[i];
1717 for (int j=0; j<pids.size(); j++) {
1718 Integer pid = (Integer)pids.get(j);
1719 if (pid.intValue() == myPid) {
Robert Greenwalta7dfbd32010-06-15 15:43:39 -07001720 Collection<InetAddress> dnses = p.getDnses();
1721 writePidDns(dnses, myPid);
Robert Greenwalt2034b912009-08-12 16:08:25 -07001722 if (doBump) {
1723 bumpDns();
1724 }
1725 return;
1726 }
1727 }
1728 }
1729 }
1730 // nothing found - delete
1731 for (int i = 1; ; i++) {
1732 String prop = "net.dns" + i + "." + myPid;
1733 if (SystemProperties.get(prop).length() == 0) {
1734 if (doBump) {
1735 bumpDns();
1736 }
1737 return;
1738 }
1739 SystemProperties.set(prop, "");
1740 }
1741 }
1742
Robert Greenwalt8ca88762010-12-17 15:20:36 -08001743 // return true if results in a change
1744 private boolean writePidDns(Collection <InetAddress> dnses, int pid) {
Robert Greenwalt2034b912009-08-12 16:08:25 -07001745 int j = 1;
Robert Greenwalt8ca88762010-12-17 15:20:36 -08001746 boolean changed = false;
Robert Greenwalta7dfbd32010-06-15 15:43:39 -07001747 for (InetAddress dns : dnses) {
Robert Greenwalt8ca88762010-12-17 15:20:36 -08001748 String dnsString = dns.getHostAddress();
1749 if (changed || !dnsString.equals(SystemProperties.get("net.dns" + j + "." + pid))) {
1750 changed = true;
1751 SystemProperties.set("net.dns" + j++ + "." + pid, dns.getHostAddress());
1752 }
Robert Greenwalt2034b912009-08-12 16:08:25 -07001753 }
Robert Greenwalt8ca88762010-12-17 15:20:36 -08001754 return changed;
Robert Greenwalt2034b912009-08-12 16:08:25 -07001755 }
1756
1757 private void bumpDns() {
1758 /*
1759 * Bump the property that tells the name resolver library to reread
1760 * the DNS server list from the properties.
1761 */
1762 String propVal = SystemProperties.get("net.dnschange");
1763 int n = 0;
1764 if (propVal.length() != 0) {
1765 try {
1766 n = Integer.parseInt(propVal);
1767 } catch (NumberFormatException e) {}
1768 }
1769 SystemProperties.set("net.dnschange", "" + (n+1));
Robert Greenwalt051642b2010-11-02 14:08:23 -07001770 /*
1771 * Tell the VMs to toss their DNS caches
1772 */
1773 Intent intent = new Intent(Intent.ACTION_CLEAR_DNS_CACHE);
1774 intent.addFlags(Intent.FLAG_RECEIVER_REPLACE_PENDING);
Stan Chesnuttf444f502011-01-05 17:14:03 -08001775 /*
1776 * Connectivity events can happen before boot has completed ...
1777 */
1778 intent.addFlags(Intent.FLAG_RECEIVER_REGISTERED_ONLY_BEFORE_BOOT);
Robert Greenwalt051642b2010-11-02 14:08:23 -07001779 mContext.sendBroadcast(intent);
Robert Greenwalt2034b912009-08-12 16:08:25 -07001780 }
1781
Robert Greenwalt3afbead2010-07-23 15:46:26 -07001782 private void handleDnsConfigurationChange(int netType) {
Robert Greenwalt2034b912009-08-12 16:08:25 -07001783 // add default net's dns entries
Robert Greenwalt3afbead2010-07-23 15:46:26 -07001784 NetworkStateTracker nt = mNetTrackers[netType];
1785 if (nt != null && nt.getNetworkInfo().isConnected() && !nt.isTeardownRequested()) {
Robert Greenwalt1f1bcfe2010-08-30 10:56:47 -07001786 LinkProperties p = nt.getLinkProperties();
Robert Greenwalt3afbead2010-07-23 15:46:26 -07001787 if (p == null) return;
1788 Collection<InetAddress> dnses = p.getDnses();
Robert Greenwalt8ca88762010-12-17 15:20:36 -08001789 boolean changed = false;
Robert Greenwalt34848c02011-03-25 13:09:25 -07001790 if (mNetConfigs[netType].isDefault()) {
Robert Greenwalt3afbead2010-07-23 15:46:26 -07001791 int j = 1;
Robert Greenwalt94daa182010-09-01 11:34:05 -07001792 if (dnses.size() == 0 && mDefaultDns != null) {
Robert Greenwalt8ca88762010-12-17 15:20:36 -08001793 String dnsString = mDefaultDns.getHostAddress();
1794 if (!dnsString.equals(SystemProperties.get("net.dns1"))) {
1795 if (DBG) {
1796 log("no dns provided - using " + dnsString);
1797 }
1798 changed = true;
1799 SystemProperties.set("net.dns1", dnsString);
Robert Greenwalt2034b912009-08-12 16:08:25 -07001800 }
Robert Greenwalt94daa182010-09-01 11:34:05 -07001801 j++;
1802 } else {
1803 for (InetAddress dns : dnses) {
Robert Greenwalt8ca88762010-12-17 15:20:36 -08001804 String dnsString = dns.getHostAddress();
1805 if (!changed && dnsString.equals(SystemProperties.get("net.dns" + j))) {
1806 j++;
1807 continue;
1808 }
Robert Greenwalt94daa182010-09-01 11:34:05 -07001809 if (DBG) {
Wink Savillee70c6f52010-12-03 12:01:38 -08001810 log("adding dns " + dns + " for " +
Robert Greenwalt94daa182010-09-01 11:34:05 -07001811 nt.getNetworkInfo().getTypeName());
1812 }
Robert Greenwalt8ca88762010-12-17 15:20:36 -08001813 changed = true;
1814 SystemProperties.set("net.dns" + j++, dnsString);
Robert Greenwalt94daa182010-09-01 11:34:05 -07001815 }
Robert Greenwalt3afbead2010-07-23 15:46:26 -07001816 }
1817 for (int k=j ; k<mNumDnsEntries; k++) {
Robert Greenwalt8ca88762010-12-17 15:20:36 -08001818 if (changed || !TextUtils.isEmpty(SystemProperties.get("net.dns" + k))) {
1819 if (DBG) log("erasing net.dns" + k);
1820 changed = true;
1821 SystemProperties.set("net.dns" + k, "");
1822 }
Robert Greenwalt3afbead2010-07-23 15:46:26 -07001823 }
1824 mNumDnsEntries = j;
1825 } else {
1826 // set per-pid dns for attached secondary nets
1827 List pids = mNetRequestersPids[netType];
1828 for (int y=0; y< pids.size(); y++) {
1829 Integer pid = (Integer)pids.get(y);
Robert Greenwalt8ca88762010-12-17 15:20:36 -08001830 changed = writePidDns(dnses, pid.intValue());
The Android Open Source Project28527d22009-03-03 19:31:44 -08001831 }
1832 }
Robert Greenwalt8ca88762010-12-17 15:20:36 -08001833 if (changed) bumpDns();
The Android Open Source Project28527d22009-03-03 19:31:44 -08001834 }
Robert Greenwalt2034b912009-08-12 16:08:25 -07001835 }
1836
Robert Greenwalt20f819c2011-05-03 19:02:44 -07001837 private int getRestoreDefaultNetworkDelay(int networkType) {
Robert Greenwalt2034b912009-08-12 16:08:25 -07001838 String restoreDefaultNetworkDelayStr = SystemProperties.get(
1839 NETWORK_RESTORE_DELAY_PROP_NAME);
1840 if(restoreDefaultNetworkDelayStr != null &&
1841 restoreDefaultNetworkDelayStr.length() != 0) {
1842 try {
1843 return Integer.valueOf(restoreDefaultNetworkDelayStr);
1844 } catch (NumberFormatException e) {
1845 }
The Android Open Source Project28527d22009-03-03 19:31:44 -08001846 }
Robert Greenwalt20f819c2011-05-03 19:02:44 -07001847 // if the system property isn't set, use the value for the apn type
1848 int ret = RESTORE_DEFAULT_NETWORK_DELAY;
1849
1850 if ((networkType <= ConnectivityManager.MAX_NETWORK_TYPE) &&
1851 (mNetConfigs[networkType] != null)) {
1852 ret = mNetConfigs[networkType].restoreTime;
1853 }
1854 return ret;
The Android Open Source Project28527d22009-03-03 19:31:44 -08001855 }
1856
1857 @Override
1858 protected void dump(FileDescriptor fd, PrintWriter pw, String[] args) {
Robert Greenwalt0659da32009-07-16 17:21:39 -07001859 if (mContext.checkCallingOrSelfPermission(
1860 android.Manifest.permission.DUMP)
The Android Open Source Project28527d22009-03-03 19:31:44 -08001861 != PackageManager.PERMISSION_GRANTED) {
Robert Greenwalt0659da32009-07-16 17:21:39 -07001862 pw.println("Permission Denial: can't dump ConnectivityService " +
1863 "from from pid=" + Binder.getCallingPid() + ", uid=" +
1864 Binder.getCallingUid());
The Android Open Source Project28527d22009-03-03 19:31:44 -08001865 return;
1866 }
The Android Open Source Project28527d22009-03-03 19:31:44 -08001867 pw.println();
1868 for (NetworkStateTracker nst : mNetTrackers) {
Robert Greenwalt3eeb6032009-12-21 18:24:07 -08001869 if (nst != null) {
1870 if (nst.getNetworkInfo().isConnected()) {
1871 pw.println("Active network: " + nst.getNetworkInfo().
1872 getTypeName());
1873 }
1874 pw.println(nst.getNetworkInfo());
1875 pw.println(nst);
1876 pw.println();
Robert Greenwalt2034b912009-08-12 16:08:25 -07001877 }
The Android Open Source Project28527d22009-03-03 19:31:44 -08001878 }
Robert Greenwalt3eeb6032009-12-21 18:24:07 -08001879
1880 pw.println("Network Requester Pids:");
1881 for (int net : mPriorityList) {
1882 String pidString = net + ": ";
1883 for (Object pid : mNetRequestersPids[net]) {
1884 pidString = pidString + pid.toString() + ", ";
1885 }
1886 pw.println(pidString);
1887 }
1888 pw.println();
1889
1890 pw.println("FeatureUsers:");
1891 for (Object requester : mFeatureUsers) {
1892 pw.println(requester.toString());
1893 }
1894 pw.println();
Robert Greenwalt8e87f122010-02-11 18:18:40 -08001895
Robert Greenwalt93dc1042010-06-15 12:19:37 -07001896 synchronized (this) {
1897 pw.println("NetworkTranstionWakeLock is currently " +
1898 (mNetTransitionWakeLock.isHeld() ? "" : "not ") + "held.");
1899 pw.println("It was last requested for "+mNetTransitionWakeLockCausedBy);
1900 }
1901 pw.println();
1902
Robert Greenwalt8e87f122010-02-11 18:18:40 -08001903 mTethering.dump(fd, pw, args);
Robert Greenwalt0e80be12010-09-20 14:35:25 -07001904
1905 if (mInetLog != null) {
1906 pw.println();
1907 pw.println("Inet condition reports:");
1908 for(int i = 0; i < mInetLog.size(); i++) {
1909 pw.println(mInetLog.get(i));
1910 }
1911 }
The Android Open Source Project28527d22009-03-03 19:31:44 -08001912 }
1913
Robert Greenwalt2034b912009-08-12 16:08:25 -07001914 // must be stateless - things change under us.
The Android Open Source Project28527d22009-03-03 19:31:44 -08001915 private class MyHandler extends Handler {
Wink Saville775aad62010-09-02 19:23:52 -07001916 public MyHandler(Looper looper) {
1917 super(looper);
1918 }
1919
The Android Open Source Project28527d22009-03-03 19:31:44 -08001920 @Override
1921 public void handleMessage(Message msg) {
1922 NetworkInfo info;
1923 switch (msg.what) {
1924 case NetworkStateTracker.EVENT_STATE_CHANGED:
1925 info = (NetworkInfo) msg.obj;
Robert Greenwalt12c44552009-12-07 11:33:18 -08001926 int type = info.getType();
1927 NetworkInfo.State state = info.getState();
Robert Greenwalt12c44552009-12-07 11:33:18 -08001928
Wink Savillee70c6f52010-12-03 12:01:38 -08001929 if (DBG) log("ConnectivityChange for " +
Robert Greenwalt0659da32009-07-16 17:21:39 -07001930 info.getTypeName() + ": " +
Robert Greenwalt12c44552009-12-07 11:33:18 -08001931 state + "/" + info.getDetailedState());
The Android Open Source Project28527d22009-03-03 19:31:44 -08001932
1933 // Connectivity state changed:
1934 // [31-13] Reserved for future use
Robert Greenwalt0659da32009-07-16 17:21:39 -07001935 // [12-9] Network subtype (for mobile network, as defined
1936 // by TelephonyManager)
1937 // [8-3] Detailed state ordinal (as defined by
1938 // NetworkInfo.DetailedState)
The Android Open Source Project28527d22009-03-03 19:31:44 -08001939 // [2-0] Network type (as defined by ConnectivityManager)
1940 int eventLogParam = (info.getType() & 0x7) |
1941 ((info.getDetailedState().ordinal() & 0x3f) << 3) |
1942 (info.getSubtype() << 9);
Doug Zongker2fc96232009-12-04 10:31:43 -08001943 EventLog.writeEvent(EventLogTags.CONNECTIVITY_STATE_CHANGED,
Robert Greenwalt0659da32009-07-16 17:21:39 -07001944 eventLogParam);
1945
1946 if (info.getDetailedState() ==
1947 NetworkInfo.DetailedState.FAILED) {
The Android Open Source Project28527d22009-03-03 19:31:44 -08001948 handleConnectionFailure(info);
Robert Greenwalt12c44552009-12-07 11:33:18 -08001949 } else if (state == NetworkInfo.State.DISCONNECTED) {
The Android Open Source Project28527d22009-03-03 19:31:44 -08001950 handleDisconnect(info);
Robert Greenwalt12c44552009-12-07 11:33:18 -08001951 } else if (state == NetworkInfo.State.SUSPENDED) {
The Android Open Source Project28527d22009-03-03 19:31:44 -08001952 // TODO: need to think this over.
Robert Greenwalt0659da32009-07-16 17:21:39 -07001953 // the logic here is, handle SUSPENDED the same as
1954 // DISCONNECTED. The only difference being we are
1955 // broadcasting an intent with NetworkInfo that's
1956 // suspended. This allows the applications an
1957 // opportunity to handle DISCONNECTED and SUSPENDED
1958 // differently, or not.
The Android Open Source Project28527d22009-03-03 19:31:44 -08001959 handleDisconnect(info);
Robert Greenwalt12c44552009-12-07 11:33:18 -08001960 } else if (state == NetworkInfo.State.CONNECTED) {
The Android Open Source Project28527d22009-03-03 19:31:44 -08001961 handleConnect(info);
1962 }
The Android Open Source Project28527d22009-03-03 19:31:44 -08001963 break;
The Android Open Source Project28527d22009-03-03 19:31:44 -08001964 case NetworkStateTracker.EVENT_CONFIGURATION_CHANGED:
Robert Greenwalt3afbead2010-07-23 15:46:26 -07001965 info = (NetworkInfo) msg.obj;
Robert Greenwalt34848c02011-03-25 13:09:25 -07001966 handleConnectivityChange(info.getType());
The Android Open Source Project28527d22009-03-03 19:31:44 -08001967 break;
Robert Greenwaltccb36f92010-09-24 14:32:21 -07001968 case EVENT_CLEAR_NET_TRANSITION_WAKELOCK:
Robert Greenwalt93dc1042010-06-15 12:19:37 -07001969 String causedBy = null;
1970 synchronized (ConnectivityService.this) {
1971 if (msg.arg1 == mNetTransitionWakeLockSerialNumber &&
1972 mNetTransitionWakeLock.isHeld()) {
1973 mNetTransitionWakeLock.release();
1974 causedBy = mNetTransitionWakeLockCausedBy;
1975 }
1976 }
1977 if (causedBy != null) {
Wink Savillee70c6f52010-12-03 12:01:38 -08001978 log("NetTransition Wakelock for " + causedBy + " released by timeout");
Robert Greenwalt93dc1042010-06-15 12:19:37 -07001979 }
Robert Greenwaltcf1a56c2010-09-09 14:05:10 -07001980 break;
Robert Greenwalt6a2db8a2010-09-23 10:05:56 -07001981 case EVENT_RESTORE_DEFAULT_NETWORK:
Robert Greenwaltaffc3a12009-09-27 17:27:04 -07001982 FeatureUser u = (FeatureUser)msg.obj;
1983 u.expire();
Robert Greenwalt986c7412010-09-08 15:24:47 -07001984 break;
Robert Greenwalt6a2db8a2010-09-23 10:05:56 -07001985 case EVENT_INET_CONDITION_CHANGE:
1986 {
1987 int netType = msg.arg1;
1988 int condition = msg.arg2;
1989 handleInetConditionChange(netType, condition);
Robert Greenwalt93dc1042010-06-15 12:19:37 -07001990 break;
Robert Greenwalt6a2db8a2010-09-23 10:05:56 -07001991 }
1992 case EVENT_INET_CONDITION_HOLD_END:
1993 {
1994 int netType = msg.arg1;
1995 int sequence = msg.arg2;
1996 handleInetConditionHoldEnd(netType, sequence);
Robert Greenwalt986c7412010-09-08 15:24:47 -07001997 break;
Robert Greenwalt6a2db8a2010-09-23 10:05:56 -07001998 }
1999 case EVENT_SET_NETWORK_PREFERENCE:
2000 {
2001 int preference = msg.arg1;
2002 handleSetNetworkPreference(preference);
2003 break;
2004 }
2005 case EVENT_SET_BACKGROUND_DATA:
2006 {
2007 boolean enabled = (msg.arg1 == ENABLED);
2008 handleSetBackgroundData(enabled);
2009 break;
2010 }
2011 case EVENT_SET_MOBILE_DATA:
2012 {
2013 boolean enabled = (msg.arg1 == ENABLED);
2014 handleSetMobileData(enabled);
2015 break;
2016 }
Robert Greenwaltc3c5f862010-10-11 16:00:27 -07002017 case EVENT_APPLY_GLOBAL_HTTP_PROXY:
2018 {
2019 handleDeprecatedGlobalHttpProxy();
Robert Greenwalt34848c02011-03-25 13:09:25 -07002020 break;
2021 }
2022 case EVENT_SET_DEPENDENCY_MET:
2023 {
2024 boolean met = (msg.arg1 == ENABLED);
2025 handleSetDependencyMet(msg.arg2, met);
2026 break;
Robert Greenwaltc3c5f862010-10-11 16:00:27 -07002027 }
The Android Open Source Project28527d22009-03-03 19:31:44 -08002028 }
2029 }
2030 }
Robert Greenwalt0c4828c2010-01-26 11:40:34 -08002031
2032 // javadoc from interface
Robert Greenwalt4283ded2010-03-02 17:25:02 -08002033 public int tether(String iface) {
Robert Greenwalt0c4828c2010-01-26 11:40:34 -08002034 enforceTetherChangePermission();
Robert Greenwalt4283ded2010-03-02 17:25:02 -08002035
2036 if (isTetheringSupported()) {
2037 return mTethering.tether(iface);
2038 } else {
2039 return ConnectivityManager.TETHER_ERROR_UNSUPPORTED;
2040 }
Robert Greenwalt0c4828c2010-01-26 11:40:34 -08002041 }
2042
2043 // javadoc from interface
Robert Greenwalt4283ded2010-03-02 17:25:02 -08002044 public int untether(String iface) {
Robert Greenwalt0c4828c2010-01-26 11:40:34 -08002045 enforceTetherChangePermission();
Robert Greenwalt4283ded2010-03-02 17:25:02 -08002046
2047 if (isTetheringSupported()) {
2048 return mTethering.untether(iface);
2049 } else {
2050 return ConnectivityManager.TETHER_ERROR_UNSUPPORTED;
2051 }
2052 }
2053
2054 // javadoc from interface
2055 public int getLastTetherError(String iface) {
2056 enforceTetherAccessPermission();
2057
2058 if (isTetheringSupported()) {
2059 return mTethering.getLastTetherError(iface);
2060 } else {
2061 return ConnectivityManager.TETHER_ERROR_UNSUPPORTED;
2062 }
Robert Greenwalt8e87f122010-02-11 18:18:40 -08002063 }
2064
2065 // TODO - proper iface API for selection by property, inspection, etc
2066 public String[] getTetherableUsbRegexs() {
2067 enforceTetherAccessPermission();
2068 if (isTetheringSupported()) {
2069 return mTethering.getTetherableUsbRegexs();
2070 } else {
2071 return new String[0];
2072 }
2073 }
2074
2075 public String[] getTetherableWifiRegexs() {
2076 enforceTetherAccessPermission();
2077 if (isTetheringSupported()) {
2078 return mTethering.getTetherableWifiRegexs();
2079 } else {
2080 return new String[0];
2081 }
Robert Greenwalt0c4828c2010-01-26 11:40:34 -08002082 }
2083
Danica Chang96567052010-08-11 14:54:43 -07002084 public String[] getTetherableBluetoothRegexs() {
2085 enforceTetherAccessPermission();
2086 if (isTetheringSupported()) {
2087 return mTethering.getTetherableBluetoothRegexs();
2088 } else {
2089 return new String[0];
2090 }
2091 }
2092
Robert Greenwalt0c4828c2010-01-26 11:40:34 -08002093 // TODO - move iface listing, queries, etc to new module
2094 // javadoc from interface
2095 public String[] getTetherableIfaces() {
Robert Greenwalt8e87f122010-02-11 18:18:40 -08002096 enforceTetherAccessPermission();
Robert Greenwalt0c4828c2010-01-26 11:40:34 -08002097 return mTethering.getTetherableIfaces();
2098 }
2099
2100 public String[] getTetheredIfaces() {
Robert Greenwalt8e87f122010-02-11 18:18:40 -08002101 enforceTetherAccessPermission();
Robert Greenwalt0c4828c2010-01-26 11:40:34 -08002102 return mTethering.getTetheredIfaces();
2103 }
Robert Greenwalt8e87f122010-02-11 18:18:40 -08002104
Robert Greenwalt4283ded2010-03-02 17:25:02 -08002105 public String[] getTetheringErroredIfaces() {
2106 enforceTetherAccessPermission();
2107 return mTethering.getErroredIfaces();
2108 }
2109
Robert Greenwalt8e87f122010-02-11 18:18:40 -08002110 // if ro.tether.denied = true we default to no tethering
2111 // gservices could set the secure setting to 1 though to enable it on a build where it
2112 // had previously been turned off.
2113 public boolean isTetheringSupported() {
2114 enforceTetherAccessPermission();
2115 int defaultVal = (SystemProperties.get("ro.tether.denied").equals("true") ? 0 : 1);
Robert Greenwaltf1b66e12010-02-25 12:29:30 -08002116 boolean tetherEnabledInSettings = (Settings.Secure.getInt(mContext.getContentResolver(),
2117 Settings.Secure.TETHER_SUPPORTED, defaultVal) != 0);
2118 return tetherEnabledInSettings && mTetheringConfigValid;
Robert Greenwalt8e87f122010-02-11 18:18:40 -08002119 }
Robert Greenwalt93dc1042010-06-15 12:19:37 -07002120
2121 // An API NetworkStateTrackers can call when they lose their network.
2122 // This will automatically be cleared after X seconds or a network becomes CONNECTED,
2123 // whichever happens first. The timer is started by the first caller and not
2124 // restarted by subsequent callers.
2125 public void requestNetworkTransitionWakelock(String forWhom) {
2126 enforceConnectivityInternalPermission();
2127 synchronized (this) {
2128 if (mNetTransitionWakeLock.isHeld()) return;
2129 mNetTransitionWakeLockSerialNumber++;
2130 mNetTransitionWakeLock.acquire();
2131 mNetTransitionWakeLockCausedBy = forWhom;
2132 }
2133 mHandler.sendMessageDelayed(mHandler.obtainMessage(
Robert Greenwaltccb36f92010-09-24 14:32:21 -07002134 EVENT_CLEAR_NET_TRANSITION_WAKELOCK,
Robert Greenwalt93dc1042010-06-15 12:19:37 -07002135 mNetTransitionWakeLockSerialNumber, 0),
2136 mNetTransitionWakeLockTimeout);
2137 return;
2138 }
Robert Greenwalt24118e82010-09-09 13:15:32 -07002139
Robert Greenwalt986c7412010-09-08 15:24:47 -07002140 // 100 percent is full good, 0 is full bad.
2141 public void reportInetCondition(int networkType, int percentage) {
Wink Savillee70c6f52010-12-03 12:01:38 -08002142 if (DBG) log("reportNetworkCondition(" + networkType + ", " + percentage + ")");
Robert Greenwalt986c7412010-09-08 15:24:47 -07002143 mContext.enforceCallingOrSelfPermission(
2144 android.Manifest.permission.STATUS_BAR,
2145 "ConnectivityService");
2146
Robert Greenwalt0e80be12010-09-20 14:35:25 -07002147 if (DBG) {
2148 int pid = getCallingPid();
2149 int uid = getCallingUid();
2150 String s = pid + "(" + uid + ") reports inet is " +
2151 (percentage > 50 ? "connected" : "disconnected") + " (" + percentage + ") on " +
2152 "network Type " + networkType + " at " + GregorianCalendar.getInstance().getTime();
2153 mInetLog.add(s);
2154 while(mInetLog.size() > INET_CONDITION_LOG_MAX_SIZE) {
2155 mInetLog.remove(0);
2156 }
2157 }
Robert Greenwalt986c7412010-09-08 15:24:47 -07002158 mHandler.sendMessage(mHandler.obtainMessage(
Robert Greenwalt6a2db8a2010-09-23 10:05:56 -07002159 EVENT_INET_CONDITION_CHANGE, networkType, percentage));
2160 }
2161
2162 private void handleInetConditionChange(int netType, int condition) {
2163 if (DBG) {
Wink Savillee70c6f52010-12-03 12:01:38 -08002164 log("Inet connectivity change, net=" +
Robert Greenwalt6a2db8a2010-09-23 10:05:56 -07002165 netType + ", condition=" + condition +
2166 ",mActiveDefaultNetwork=" + mActiveDefaultNetwork);
2167 }
2168 if (mActiveDefaultNetwork == -1) {
Wink Savillee70c6f52010-12-03 12:01:38 -08002169 if (DBG) log("no active default network - aborting");
Robert Greenwalt6a2db8a2010-09-23 10:05:56 -07002170 return;
2171 }
2172 if (mActiveDefaultNetwork != netType) {
Wink Savillee70c6f52010-12-03 12:01:38 -08002173 if (DBG) log("given net not default - aborting");
Robert Greenwalt6a2db8a2010-09-23 10:05:56 -07002174 return;
2175 }
2176 mDefaultInetCondition = condition;
2177 int delay;
2178 if (mInetConditionChangeInFlight == false) {
Wink Savillee70c6f52010-12-03 12:01:38 -08002179 if (DBG) log("starting a change hold");
Robert Greenwalt6a2db8a2010-09-23 10:05:56 -07002180 // setup a new hold to debounce this
2181 if (mDefaultInetCondition > 50) {
2182 delay = Settings.Secure.getInt(mContext.getContentResolver(),
2183 Settings.Secure.INET_CONDITION_DEBOUNCE_UP_DELAY, 500);
2184 } else {
2185 delay = Settings.Secure.getInt(mContext.getContentResolver(),
2186 Settings.Secure.INET_CONDITION_DEBOUNCE_DOWN_DELAY, 3000);
2187 }
2188 mInetConditionChangeInFlight = true;
2189 mHandler.sendMessageDelayed(mHandler.obtainMessage(EVENT_INET_CONDITION_HOLD_END,
2190 mActiveDefaultNetwork, mDefaultConnectionSequence), delay);
2191 } else {
2192 // we've set the new condition, when this hold ends that will get
2193 // picked up
Wink Savillee70c6f52010-12-03 12:01:38 -08002194 if (DBG) log("currently in hold - not setting new end evt");
Robert Greenwalt6a2db8a2010-09-23 10:05:56 -07002195 }
2196 }
2197
2198 private void handleInetConditionHoldEnd(int netType, int sequence) {
2199 if (DBG) {
Wink Savillee70c6f52010-12-03 12:01:38 -08002200 log("Inet hold end, net=" + netType +
Robert Greenwalt6a2db8a2010-09-23 10:05:56 -07002201 ", condition =" + mDefaultInetCondition +
2202 ", published condition =" + mDefaultInetConditionPublished);
2203 }
2204 mInetConditionChangeInFlight = false;
2205
2206 if (mActiveDefaultNetwork == -1) {
Wink Savillee70c6f52010-12-03 12:01:38 -08002207 if (DBG) log("no active default network - aborting");
Robert Greenwalt6a2db8a2010-09-23 10:05:56 -07002208 return;
2209 }
2210 if (mDefaultConnectionSequence != sequence) {
Wink Savillee70c6f52010-12-03 12:01:38 -08002211 if (DBG) log("event hold for obsolete network - aborting");
Robert Greenwalt6a2db8a2010-09-23 10:05:56 -07002212 return;
2213 }
2214 if (mDefaultInetConditionPublished == mDefaultInetCondition) {
Wink Savillee70c6f52010-12-03 12:01:38 -08002215 if (DBG) log("no change in condition - aborting");
Robert Greenwalt6a2db8a2010-09-23 10:05:56 -07002216 return;
2217 }
2218 NetworkInfo networkInfo = mNetTrackers[mActiveDefaultNetwork].getNetworkInfo();
2219 if (networkInfo.isConnected() == false) {
Wink Savillee70c6f52010-12-03 12:01:38 -08002220 if (DBG) log("default network not connected - aborting");
Robert Greenwalt6a2db8a2010-09-23 10:05:56 -07002221 return;
2222 }
2223 mDefaultInetConditionPublished = mDefaultInetCondition;
2224 sendInetConditionBroadcast(networkInfo);
2225 return;
Robert Greenwalt986c7412010-09-08 15:24:47 -07002226 }
Robert Greenwaltc3c5f862010-10-11 16:00:27 -07002227
2228 public synchronized ProxyProperties getProxy() {
2229 if (mGlobalProxy != null) return mGlobalProxy;
2230 if (mDefaultProxy != null) return mDefaultProxy;
2231 return null;
2232 }
2233
2234 public void setGlobalProxy(ProxyProperties proxyProperties) {
2235 enforceChangePermission();
2236 synchronized (mGlobalProxyLock) {
2237 if (proxyProperties == mGlobalProxy) return;
2238 if (proxyProperties != null && proxyProperties.equals(mGlobalProxy)) return;
2239 if (mGlobalProxy != null && mGlobalProxy.equals(proxyProperties)) return;
2240
2241 String host = "";
2242 int port = 0;
2243 String exclList = "";
2244 if (proxyProperties != null && !TextUtils.isEmpty(proxyProperties.getHost())) {
2245 mGlobalProxy = new ProxyProperties(proxyProperties);
2246 host = mGlobalProxy.getHost();
2247 port = mGlobalProxy.getPort();
2248 exclList = mGlobalProxy.getExclusionList();
2249 } else {
2250 mGlobalProxy = null;
2251 }
2252 ContentResolver res = mContext.getContentResolver();
2253 Settings.Secure.putString(res, Settings.Secure.GLOBAL_HTTP_PROXY_HOST, host);
2254 Settings.Secure.putInt(res, Settings.Secure.GLOBAL_HTTP_PROXY_PORT, port);
Robert Greenwalt6f7c6092010-12-02 11:31:00 -08002255 Settings.Secure.putString(res, Settings.Secure.GLOBAL_HTTP_PROXY_EXCLUSION_LIST,
Robert Greenwaltc3c5f862010-10-11 16:00:27 -07002256 exclList);
2257 }
2258
2259 if (mGlobalProxy == null) {
2260 proxyProperties = mDefaultProxy;
2261 }
2262 sendProxyBroadcast(proxyProperties);
2263 }
2264
Robert Greenwalt6f7c6092010-12-02 11:31:00 -08002265 private void loadGlobalProxy() {
2266 ContentResolver res = mContext.getContentResolver();
2267 String host = Settings.Secure.getString(res, Settings.Secure.GLOBAL_HTTP_PROXY_HOST);
2268 int port = Settings.Secure.getInt(res, Settings.Secure.GLOBAL_HTTP_PROXY_PORT, 0);
2269 String exclList = Settings.Secure.getString(res,
2270 Settings.Secure.GLOBAL_HTTP_PROXY_EXCLUSION_LIST);
2271 if (!TextUtils.isEmpty(host)) {
2272 ProxyProperties proxyProperties = new ProxyProperties(host, port, exclList);
2273 synchronized (mGlobalProxyLock) {
2274 mGlobalProxy = proxyProperties;
2275 }
2276 }
2277 }
2278
Robert Greenwaltc3c5f862010-10-11 16:00:27 -07002279 public ProxyProperties getGlobalProxy() {
2280 synchronized (mGlobalProxyLock) {
2281 return mGlobalProxy;
2282 }
2283 }
2284
2285 private void handleApplyDefaultProxy(int type) {
2286 // check if new default - push it out to all VM if so
2287 ProxyProperties proxy = mNetTrackers[type].getLinkProperties().getHttpProxy();
2288 synchronized (this) {
2289 if (mDefaultProxy != null && mDefaultProxy.equals(proxy)) return;
2290 if (mDefaultProxy == proxy) return;
2291 if (!TextUtils.isEmpty(proxy.getHost())) {
2292 mDefaultProxy = proxy;
2293 } else {
2294 mDefaultProxy = null;
2295 }
2296 }
Wink Savillee70c6f52010-12-03 12:01:38 -08002297 if (DBG) log("changing default proxy to " + proxy);
Robert Greenwaltc3c5f862010-10-11 16:00:27 -07002298 if ((proxy == null && mGlobalProxy == null) || proxy.equals(mGlobalProxy)) return;
2299 if (mGlobalProxy != null) return;
2300 sendProxyBroadcast(proxy);
2301 }
2302
2303 private void handleDeprecatedGlobalHttpProxy() {
2304 String proxy = Settings.Secure.getString(mContext.getContentResolver(),
2305 Settings.Secure.HTTP_PROXY);
2306 if (!TextUtils.isEmpty(proxy)) {
2307 String data[] = proxy.split(":");
2308 String proxyHost = data[0];
2309 int proxyPort = 8080;
2310 if (data.length > 1) {
2311 try {
2312 proxyPort = Integer.parseInt(data[1]);
2313 } catch (NumberFormatException e) {
2314 return;
2315 }
2316 }
2317 ProxyProperties p = new ProxyProperties(data[0], proxyPort, "");
2318 setGlobalProxy(p);
2319 }
2320 }
2321
2322 private void sendProxyBroadcast(ProxyProperties proxy) {
Robert Greenwalt611291c2010-12-23 15:51:10 -08002323 if (proxy == null) proxy = new ProxyProperties("", 0, "");
Wink Savillee70c6f52010-12-03 12:01:38 -08002324 log("sending Proxy Broadcast for " + proxy);
Robert Greenwaltc3c5f862010-10-11 16:00:27 -07002325 Intent intent = new Intent(Proxy.PROXY_CHANGE_ACTION);
Stan Chesnutt1f2a2ac2011-01-06 11:00:19 -08002326 intent.addFlags(Intent.FLAG_RECEIVER_REPLACE_PENDING |
2327 Intent.FLAG_RECEIVER_REGISTERED_ONLY_BEFORE_BOOT);
Robert Greenwaltc3c5f862010-10-11 16:00:27 -07002328 intent.putExtra(Proxy.EXTRA_PROXY_INFO, proxy);
Robert Greenwaltd93dc8f2010-12-06 11:29:17 -08002329 mContext.sendStickyBroadcast(intent);
Robert Greenwaltc3c5f862010-10-11 16:00:27 -07002330 }
2331
2332 private static class SettingsObserver extends ContentObserver {
2333 private int mWhat;
2334 private Handler mHandler;
2335 SettingsObserver(Handler handler, int what) {
2336 super(handler);
2337 mHandler = handler;
2338 mWhat = what;
2339 }
2340
2341 void observe(Context context) {
2342 ContentResolver resolver = context.getContentResolver();
2343 resolver.registerContentObserver(Settings.Secure.getUriFor(
2344 Settings.Secure.HTTP_PROXY), false, this);
2345 }
2346
2347 @Override
2348 public void onChange(boolean selfChange) {
2349 mHandler.obtainMessage(mWhat).sendToTarget();
2350 }
2351 }
Wink Savillee70c6f52010-12-03 12:01:38 -08002352
Kazuhiro Ondo3a340412011-04-30 20:10:57 -05002353 private void handleTetherIfaceChange(int type) {
2354 String iface = mNetTrackers[type].getLinkProperties().getInterfaceName();
2355
2356 if (isTetheringSupported()) {
2357 mTethering.handleTetherIfaceChange(iface);
2358 }
2359 }
2360
Wink Savillee70c6f52010-12-03 12:01:38 -08002361 private void log(String s) {
2362 Slog.d(TAG, s);
2363 }
2364
2365 private void loge(String s) {
2366 Slog.e(TAG, s);
2367 }
Chia-chi Yeh9a4ad7d2011-05-23 17:26:46 -07002368
Wink Savillef6b76692011-02-24 17:58:51 -08002369 int convertFeatureToNetworkType(String feature){
2370 int networkType = -1;
2371 if (TextUtils.equals(feature, Phone.FEATURE_ENABLE_MMS)) {
2372 networkType = ConnectivityManager.TYPE_MOBILE_MMS;
2373 } else if (TextUtils.equals(feature, Phone.FEATURE_ENABLE_SUPL)) {
2374 networkType = ConnectivityManager.TYPE_MOBILE_SUPL;
2375 } else if (TextUtils.equals(feature, Phone.FEATURE_ENABLE_DUN) ||
2376 TextUtils.equals(feature, Phone.FEATURE_ENABLE_DUN_ALWAYS)) {
2377 networkType = ConnectivityManager.TYPE_MOBILE_DUN;
2378 } else if (TextUtils.equals(feature, Phone.FEATURE_ENABLE_HIPRI)) {
2379 networkType = ConnectivityManager.TYPE_MOBILE_HIPRI;
2380 } else if (TextUtils.equals(feature, Phone.FEATURE_ENABLE_FOTA)) {
2381 networkType = ConnectivityManager.TYPE_MOBILE_FOTA;
2382 } else if (TextUtils.equals(feature, Phone.FEATURE_ENABLE_IMS)) {
2383 networkType = ConnectivityManager.TYPE_MOBILE_IMS;
2384 } else if (TextUtils.equals(feature, Phone.FEATURE_ENABLE_CBS)) {
2385 networkType = ConnectivityManager.TYPE_MOBILE_CBS;
2386 }
2387 return networkType;
2388 }
Jeff Sharkey921ebf22011-05-19 17:12:49 -07002389
2390 private static <T> T checkNotNull(T value, String message) {
2391 if (value == null) {
2392 throw new NullPointerException(message);
2393 }
2394 return value;
2395 }
Chia-chi Yeh9a4ad7d2011-05-23 17:26:46 -07002396
2397 // @see ConnectivityManager#protectVpn(ParcelFileDescriptor)
2398 // Permission checks are done in Vpn class.
2399 @Override
2400 public void protectVpn(ParcelFileDescriptor socket) {
2401 mVpn.protect(socket, getDefaultInterface());
2402 }
2403
2404 // @see ConnectivityManager#prepareVpn(String)
2405 // Permission checks are done in Vpn class.
2406 @Override
2407 public String prepareVpn(String packageName) {
2408 return mVpn.prepare(packageName);
2409 }
2410
2411 // @see ConnectivityManager#establishVpn(Bundle)
2412 // Permission checks are done in Vpn class.
2413 @Override
2414 public ParcelFileDescriptor establishVpn(Bundle config) {
2415 return mVpn.establish(config);
2416 }
2417
2418 private String getDefaultInterface() {
2419 if (ConnectivityManager.isNetworkTypeValid(mActiveDefaultNetwork)) {
2420 NetworkStateTracker tracker = mNetTrackers[mActiveDefaultNetwork];
2421 if (tracker != null) {
2422 LinkProperties properties = tracker.getLinkProperties();
2423 if (properties != null) {
2424 return properties.getInterfaceName();
2425 }
2426 }
2427 }
2428 throw new IllegalStateException("No default interface");
2429 }
2430
2431 /**
2432 * Callback for VPN subsystem. Currently VPN is not adapted to the service
2433 * through NetworkStateTracker since it works differently. For example, it
2434 * needs to override DNS servers but never takes the default routes. It
2435 * relies on another data network, and it could keep existing connections
2436 * alive after reconnecting, switching between networks, or even resuming
2437 * from deep sleep. Calls from applications should be done synchronously
2438 * to avoid race conditions. As these are all hidden APIs, refactoring can
2439 * be done whenever a better abstraction is developed.
2440 */
2441 public class VpnCallback {
2442
2443 private VpnCallback() {
2444 }
2445
2446 public synchronized void override(String[] dnsServers) {
2447 // TODO: override DNS servers and http proxy.
2448 }
2449
2450 public synchronized void restore() {
2451 // TODO: restore VPN changes.
2452 }
2453 }
The Android Open Source Project28527d22009-03-03 19:31:44 -08002454}