blob: c6f4c205b00c291705ff1caf419e2d2c768d87cd [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 Sharkey21062e72011-05-28 20:56:34 -070019import static android.Manifest.permission.READ_PHONE_STATE;
Jeff Sharkey921ebf22011-05-19 17:12:49 -070020import static android.Manifest.permission.UPDATE_DEVICE_STATS;
21import static android.net.ConnectivityManager.isNetworkTypeValid;
22import static android.net.NetworkPolicyManager.RULE_ALLOW_ALL;
23import static android.net.NetworkPolicyManager.RULE_REJECT_PAID;
24
Jaikumar Ganesh0db51a02010-12-21 22:31:44 -080025import android.bluetooth.BluetoothTetheringDataTracker;
The Android Open Source Project28527d22009-03-03 19:31:44 -080026import android.content.ContentResolver;
27import android.content.Context;
28import android.content.Intent;
29import android.content.pm.PackageManager;
Robert Greenwaltc3c5f862010-10-11 16:00:27 -070030import android.database.ContentObserver;
The Android Open Source Project28527d22009-03-03 19:31:44 -080031import android.net.ConnectivityManager;
Robert Greenwalteb123ac2010-12-06 13:56:24 -080032import android.net.DummyDataStateTracker;
Benoit Goby211b5692010-12-22 14:29:40 -080033import android.net.EthernetDataTracker;
The Android Open Source Project28527d22009-03-03 19:31:44 -080034import android.net.IConnectivityManager;
Jeff Sharkey921ebf22011-05-19 17:12:49 -070035import android.net.INetworkPolicyListener;
36import android.net.INetworkPolicyManager;
Jaikumar Ganesh0db51a02010-12-21 22:31:44 -080037import android.net.LinkProperties;
The Android Open Source Project28527d22009-03-03 19:31:44 -080038import android.net.MobileDataStateTracker;
Robert Greenwalt34848c02011-03-25 13:09:25 -070039import android.net.NetworkConfig;
The Android Open Source Project28527d22009-03-03 19:31:44 -080040import android.net.NetworkInfo;
Jeff Sharkey921ebf22011-05-19 17:12:49 -070041import android.net.NetworkInfo.DetailedState;
Jeff Sharkey21062e72011-05-28 20:56:34 -070042import android.net.NetworkState;
The Android Open Source Project28527d22009-03-03 19:31:44 -080043import android.net.NetworkStateTracker;
Robert Greenwalt7fe44cb2010-08-27 09:24:29 -070044import android.net.NetworkUtils;
Robert Greenwaltc3c5f862010-10-11 16:00:27 -070045import android.net.Proxy;
46import android.net.ProxyProperties;
Robert Greenwalt5a901292011-04-28 14:28:50 -070047import android.net.RouteInfo;
Hung-ying Tyan4e723422011-01-19 16:48:38 +080048import android.net.vpn.VpnManager;
The Android Open Source Project28527d22009-03-03 19:31:44 -080049import android.net.wifi.WifiStateTracker;
50import android.os.Binder;
Chia-chi Yeh9a4ad7d2011-05-23 17:26:46 -070051import android.os.Bundle;
Mike Lockwood0d5916c2011-05-28 13:24:04 -040052import android.os.FileUtils;
The Android Open Source Project28527d22009-03-03 19:31:44 -080053import android.os.Handler;
Wink Saville775aad62010-09-02 19:23:52 -070054import android.os.HandlerThread;
Robert Greenwalt2034b912009-08-12 16:08:25 -070055import android.os.IBinder;
Chia-chi Yeh4df51322011-05-11 16:35:13 -070056import android.os.INetworkManagementService;
The Android Open Source Project28527d22009-03-03 19:31:44 -080057import android.os.Looper;
58import android.os.Message;
Chia-chi Yeh9a4ad7d2011-05-23 17:26:46 -070059import android.os.ParcelFileDescriptor;
Robert Greenwalt93dc1042010-06-15 12:19:37 -070060import android.os.PowerManager;
Robert Greenwalt2034b912009-08-12 16:08:25 -070061import android.os.RemoteException;
The Android Open Source Project28527d22009-03-03 19:31:44 -080062import android.os.ServiceManager;
63import android.os.SystemProperties;
64import android.provider.Settings;
Robert Greenwalt2034b912009-08-12 16:08:25 -070065import android.text.TextUtils;
The Android Open Source Project28527d22009-03-03 19:31:44 -080066import android.util.EventLog;
Joe Onoratoc2386bb2010-02-26 18:56:32 -080067import android.util.Slog;
Jeff Sharkey921ebf22011-05-19 17:12:49 -070068import android.util.SparseIntArray;
The Android Open Source Project28527d22009-03-03 19:31:44 -080069
Robert Greenwalt2034b912009-08-12 16:08:25 -070070import com.android.internal.telephony.Phone;
Robert Greenwalt0c4828c2010-01-26 11:40:34 -080071import com.android.server.connectivity.Tethering;
Chia-chi Yeh9a4ad7d2011-05-23 17:26:46 -070072import com.android.server.connectivity.Vpn;
73
Jeff Sharkey21062e72011-05-28 20:56:34 -070074import com.google.android.collect.Lists;
Robert Greenwalt0c4828c2010-01-26 11:40:34 -080075
The Android Open Source Project28527d22009-03-03 19:31:44 -080076import java.io.FileDescriptor;
Irfan Sheriff7f132d92010-06-09 15:39:36 -070077import java.io.IOException;
The Android Open Source Project28527d22009-03-03 19:31:44 -080078import java.io.PrintWriter;
Robert Greenwalta7dfbd32010-06-15 15:43:39 -070079import java.net.InetAddress;
80import java.net.UnknownHostException;
Robert Greenwalt2034b912009-08-12 16:08:25 -070081import java.util.ArrayList;
Robert Greenwalta7dfbd32010-06-15 15:43:39 -070082import java.util.Collection;
Robert Greenwalt0e80be12010-09-20 14:35:25 -070083import java.util.GregorianCalendar;
Robert Greenwalt2034b912009-08-12 16:08:25 -070084import java.util.List;
Jeff Sharkey921ebf22011-05-19 17:12:49 -070085import java.util.concurrent.atomic.AtomicBoolean;
The Android Open Source Project28527d22009-03-03 19:31:44 -080086
87/**
88 * @hide
89 */
90public class ConnectivityService extends IConnectivityManager.Stub {
91
Robert Greenwalt063dc7d2010-10-05 19:12:26 -070092 private static final boolean DBG = true;
The Android Open Source Project28527d22009-03-03 19:31:44 -080093 private static final String TAG = "ConnectivityService";
94
Jeff Sharkey921ebf22011-05-19 17:12:49 -070095 private static final boolean LOGD_RULES = false;
96
Robert Greenwalt2034b912009-08-12 16:08:25 -070097 // how long to wait before switching back to a radio's default network
98 private static final int RESTORE_DEFAULT_NETWORK_DELAY = 1 * 60 * 1000;
99 // system property that can override the above value
100 private static final String NETWORK_RESTORE_DELAY_PROP_NAME =
101 "android.telephony.apn-restore";
102
Robert Greenwaltbd492212011-05-06 17:10:53 -0700103 // used in recursive route setting to add gateways for the host for which
104 // a host route was requested.
105 private static final int MAX_HOSTROUTE_CYCLE_COUNT = 10;
106
Robert Greenwalt0c4828c2010-01-26 11:40:34 -0800107 private Tethering mTethering;
Robert Greenwaltf1b66e12010-02-25 12:29:30 -0800108 private boolean mTetheringConfigValid = false;
Robert Greenwalt0c4828c2010-01-26 11:40:34 -0800109
Chia-chi Yeh9a4ad7d2011-05-23 17:26:46 -0700110 private Vpn mVpn;
111
Jeff Sharkey921ebf22011-05-19 17:12:49 -0700112 /** Currently active network rules by UID. */
113 private SparseIntArray mUidRules = new SparseIntArray();
114
The Android Open Source Project28527d22009-03-03 19:31:44 -0800115 /**
116 * Sometimes we want to refer to the individual network state
117 * trackers separately, and sometimes we just want to treat them
118 * abstractly.
119 */
120 private NetworkStateTracker mNetTrackers[];
Robert Greenwalt2034b912009-08-12 16:08:25 -0700121
122 /**
123 * A per Net list of the PID's that requested access to the net
124 * used both as a refcount and for per-PID DNS selection
125 */
126 private List mNetRequestersPids[];
127
Irfan Sheriff653e2a22010-06-07 09:03:04 -0700128 private WifiWatchdogService mWifiWatchdogService;
129
Robert Greenwalt2034b912009-08-12 16:08:25 -0700130 // priority order of the nettrackers
131 // (excluding dynamically set mNetworkPreference)
132 // TODO - move mNetworkTypePreference into this
133 private int[] mPriorityList;
134
The Android Open Source Project28527d22009-03-03 19:31:44 -0800135 private Context mContext;
136 private int mNetworkPreference;
Robert Greenwalt2034b912009-08-12 16:08:25 -0700137 private int mActiveDefaultNetwork = -1;
Robert Greenwalt986c7412010-09-08 15:24:47 -0700138 // 0 is full bad, 100 is full good
139 private int mDefaultInetCondition = 0;
140 private int mDefaultInetConditionPublished = 0;
141 private boolean mInetConditionChangeInFlight = false;
142 private int mDefaultConnectionSequence = 0;
The Android Open Source Project28527d22009-03-03 19:31:44 -0800143
144 private int mNumDnsEntries;
The Android Open Source Project28527d22009-03-03 19:31:44 -0800145
146 private boolean mTestMode;
Joe Onorato56023ad2010-09-01 21:18:22 -0700147 private static ConnectivityService sServiceInstance;
The Android Open Source Project28527d22009-03-03 19:31:44 -0800148
Robert Greenwaltd62c7002010-12-29 16:15:02 -0800149 private AtomicBoolean mBackgroundDataEnabled = new AtomicBoolean(true);
150
Robert Greenwalt355205c2011-05-10 15:05:02 -0700151 private INetworkManagementService mNetd;
Jeff Sharkey921ebf22011-05-19 17:12:49 -0700152 private INetworkPolicyManager mPolicyManager;
Robert Greenwalt355205c2011-05-10 15:05:02 -0700153
Robert Greenwalt6a2db8a2010-09-23 10:05:56 -0700154 private static final int ENABLED = 1;
155 private static final int DISABLED = 0;
156
157 // Share the event space with NetworkStateTracker (which can't see this
158 // internal class but sends us events). If you change these, change
159 // NetworkStateTracker.java too.
160 private static final int MIN_NETWORK_STATE_TRACKER_EVENT = 1;
161 private static final int MAX_NETWORK_STATE_TRACKER_EVENT = 100;
162
163 /**
164 * used internally as a delayed event to make us switch back to the
165 * default network
166 */
167 private static final int EVENT_RESTORE_DEFAULT_NETWORK =
168 MAX_NETWORK_STATE_TRACKER_EVENT + 1;
169
170 /**
171 * used internally to change our mobile data enabled flag
172 */
173 private static final int EVENT_CHANGE_MOBILE_DATA_ENABLED =
174 MAX_NETWORK_STATE_TRACKER_EVENT + 2;
175
176 /**
177 * used internally to change our network preference setting
178 * arg1 = networkType to prefer
179 */
180 private static final int EVENT_SET_NETWORK_PREFERENCE =
181 MAX_NETWORK_STATE_TRACKER_EVENT + 3;
182
183 /**
184 * used internally to synchronize inet condition reports
185 * arg1 = networkType
186 * arg2 = condition (0 bad, 100 good)
187 */
188 private static final int EVENT_INET_CONDITION_CHANGE =
189 MAX_NETWORK_STATE_TRACKER_EVENT + 4;
190
191 /**
192 * used internally to mark the end of inet condition hold periods
193 * arg1 = networkType
194 */
195 private static final int EVENT_INET_CONDITION_HOLD_END =
196 MAX_NETWORK_STATE_TRACKER_EVENT + 5;
197
198 /**
199 * used internally to set the background data preference
200 * arg1 = TRUE for enabled, FALSE for disabled
201 */
202 private static final int EVENT_SET_BACKGROUND_DATA =
203 MAX_NETWORK_STATE_TRACKER_EVENT + 6;
204
205 /**
206 * used internally to set enable/disable cellular data
207 * arg1 = ENBALED or DISABLED
208 */
209 private static final int EVENT_SET_MOBILE_DATA =
210 MAX_NETWORK_STATE_TRACKER_EVENT + 7;
211
Robert Greenwaltccb36f92010-09-24 14:32:21 -0700212 /**
213 * used internally to clear a wakelock when transitioning
214 * from one net to another
215 */
216 private static final int EVENT_CLEAR_NET_TRANSITION_WAKELOCK =
217 MAX_NETWORK_STATE_TRACKER_EVENT + 8;
218
Robert Greenwaltc3c5f862010-10-11 16:00:27 -0700219 /**
220 * used internally to reload global proxy settings
221 */
222 private static final int EVENT_APPLY_GLOBAL_HTTP_PROXY =
223 MAX_NETWORK_STATE_TRACKER_EVENT + 9;
224
Robert Greenwalt34848c02011-03-25 13:09:25 -0700225 /**
226 * used internally to set external dependency met/unmet
227 * arg1 = ENABLED (met) or DISABLED (unmet)
228 * arg2 = NetworkType
229 */
230 private static final int EVENT_SET_DEPENDENCY_MET =
231 MAX_NETWORK_STATE_TRACKER_EVENT + 10;
232
Robert Greenwalt2034b912009-08-12 16:08:25 -0700233 private Handler mHandler;
234
235 // list of DeathRecipients used to make sure features are turned off when
236 // a process dies
237 private List mFeatureUsers;
238
Mike Lockwoodfde2b762009-08-14 14:18:49 -0400239 private boolean mSystemReady;
Dianne Hackborna417ff82009-12-08 19:45:14 -0800240 private Intent mInitialBroadcast;
Mike Lockwoodfde2b762009-08-14 14:18:49 -0400241
Robert Greenwalt93dc1042010-06-15 12:19:37 -0700242 private PowerManager.WakeLock mNetTransitionWakeLock;
243 private String mNetTransitionWakeLockCausedBy = "";
244 private int mNetTransitionWakeLockSerialNumber;
245 private int mNetTransitionWakeLockTimeout;
246
Robert Greenwalt94daa182010-09-01 11:34:05 -0700247 private InetAddress mDefaultDns;
248
Robert Greenwalt0e80be12010-09-20 14:35:25 -0700249 // used in DBG mode to track inet condition reports
250 private static final int INET_CONDITION_LOG_MAX_SIZE = 15;
251 private ArrayList mInetLog;
252
Robert Greenwaltc3c5f862010-10-11 16:00:27 -0700253 // track the current default http proxy - tell the world if we get a new one (real change)
254 private ProxyProperties mDefaultProxy = null;
255 // track the global proxy.
256 private ProxyProperties mGlobalProxy = null;
257 private final Object mGlobalProxyLock = new Object();
258
259 private SettingsObserver mSettingsObserver;
260
Robert Greenwalt34848c02011-03-25 13:09:25 -0700261 NetworkConfig[] mNetConfigs;
Robert Greenwaltec05b3c2009-10-30 14:17:42 -0700262 int mNetworksDefined;
Robert Greenwalt2034b912009-08-12 16:08:25 -0700263
Robert Greenwalt12c44552009-12-07 11:33:18 -0800264 private static class RadioAttributes {
Robert Greenwalt2034b912009-08-12 16:08:25 -0700265 public int mSimultaneity;
266 public int mType;
267 public RadioAttributes(String init) {
268 String fragments[] = init.split(",");
Robert Greenwaltec05b3c2009-10-30 14:17:42 -0700269 mType = Integer.parseInt(fragments[0]);
270 mSimultaneity = Integer.parseInt(fragments[1]);
Robert Greenwalt2034b912009-08-12 16:08:25 -0700271 }
272 }
273 RadioAttributes[] mRadioAttributes;
274
Jeff Sharkey921ebf22011-05-19 17:12:49 -0700275 public ConnectivityService(
276 Context context, INetworkManagementService netd, INetworkPolicyManager policyManager) {
Wink Savillee70c6f52010-12-03 12:01:38 -0800277 if (DBG) log("ConnectivityService starting up");
Robert Greenwaltd48f8ee2010-01-14 17:47:58 -0800278
Wink Saville775aad62010-09-02 19:23:52 -0700279 HandlerThread handlerThread = new HandlerThread("ConnectivityServiceThread");
280 handlerThread.start();
281 mHandler = new MyHandler(handlerThread.getLooper());
282
Robert Greenwaltd62c7002010-12-29 16:15:02 -0800283 mBackgroundDataEnabled.set(Settings.Secure.getInt(context.getContentResolver(),
284 Settings.Secure.BACKGROUND_DATA, 1) == 1);
285
Robert Greenwaltd48f8ee2010-01-14 17:47:58 -0800286 // setup our unique device name
Robert Greenwalt82cde132010-12-06 09:30:17 -0800287 if (TextUtils.isEmpty(SystemProperties.get("net.hostname"))) {
288 String id = Settings.Secure.getString(context.getContentResolver(),
289 Settings.Secure.ANDROID_ID);
290 if (id != null && id.length() > 0) {
291 String name = new String("android_").concat(id);
292 SystemProperties.set("net.hostname", name);
293 }
Robert Greenwaltd48f8ee2010-01-14 17:47:58 -0800294 }
295
Robert Greenwalt94daa182010-09-01 11:34:05 -0700296 // read our default dns server ip
297 String dns = Settings.Secure.getString(context.getContentResolver(),
298 Settings.Secure.DEFAULT_DNS_SERVER);
299 if (dns == null || dns.length() == 0) {
300 dns = context.getResources().getString(
301 com.android.internal.R.string.config_default_dns_server);
302 }
303 try {
Robert Greenwalt35e34d12011-02-22 16:00:42 -0800304 mDefaultDns = NetworkUtils.numericToInetAddress(dns);
305 } catch (IllegalArgumentException e) {
Wink Savillee70c6f52010-12-03 12:01:38 -0800306 loge("Error setting defaultDns using " + dns);
Robert Greenwalt94daa182010-09-01 11:34:05 -0700307 }
308
Jeff Sharkey921ebf22011-05-19 17:12:49 -0700309 mContext = checkNotNull(context, "missing Context");
310 mNetd = checkNotNull(netd, "missing INetworkManagementService");
311 mPolicyManager = checkNotNull(policyManager, "missing INetworkPolicyManager");
Robert Greenwalt93dc1042010-06-15 12:19:37 -0700312
Jeff Sharkey921ebf22011-05-19 17:12:49 -0700313 try {
314 mPolicyManager.registerListener(mPolicyListener);
315 } catch (RemoteException e) {
316 // ouch, no rules updates means some processes may never get network
317 Slog.e(TAG, "unable to register INetworkPolicyListener", e);
318 }
319
320 final PowerManager powerManager = (PowerManager) context.getSystemService(
321 Context.POWER_SERVICE);
Robert Greenwalt93dc1042010-06-15 12:19:37 -0700322 mNetTransitionWakeLock = powerManager.newWakeLock(PowerManager.PARTIAL_WAKE_LOCK, TAG);
323 mNetTransitionWakeLockTimeout = mContext.getResources().getInteger(
324 com.android.internal.R.integer.config_networkTransitionTimeout);
325
Robert Greenwalt2034b912009-08-12 16:08:25 -0700326 mNetTrackers = new NetworkStateTracker[
327 ConnectivityManager.MAX_NETWORK_TYPE+1];
Robert Greenwalt0659da32009-07-16 17:21:39 -0700328
The Android Open Source Project28527d22009-03-03 19:31:44 -0800329 mNetworkPreference = getPersistedNetworkPreference();
Robert Greenwalt0659da32009-07-16 17:21:39 -0700330
Robert Greenwaltec05b3c2009-10-30 14:17:42 -0700331 mRadioAttributes = new RadioAttributes[ConnectivityManager.MAX_RADIO_TYPE+1];
Robert Greenwalt34848c02011-03-25 13:09:25 -0700332 mNetConfigs = new NetworkConfig[ConnectivityManager.MAX_NETWORK_TYPE+1];
Robert Greenwaltec05b3c2009-10-30 14:17:42 -0700333
Robert Greenwalt2034b912009-08-12 16:08:25 -0700334 // Load device network attributes from resources
Robert Greenwalt2034b912009-08-12 16:08:25 -0700335 String[] raStrings = context.getResources().getStringArray(
336 com.android.internal.R.array.radioAttributes);
Robert Greenwaltec05b3c2009-10-30 14:17:42 -0700337 for (String raString : raStrings) {
338 RadioAttributes r = new RadioAttributes(raString);
339 if (r.mType > ConnectivityManager.MAX_RADIO_TYPE) {
Wink Savillee70c6f52010-12-03 12:01:38 -0800340 loge("Error in radioAttributes - ignoring attempt to define type " + r.mType);
Robert Greenwaltec05b3c2009-10-30 14:17:42 -0700341 continue;
342 }
343 if (mRadioAttributes[r.mType] != null) {
Wink Savillee70c6f52010-12-03 12:01:38 -0800344 loge("Error in radioAttributes - ignoring attempt to redefine type " +
Robert Greenwaltec05b3c2009-10-30 14:17:42 -0700345 r.mType);
346 continue;
347 }
Robert Greenwalt2034b912009-08-12 16:08:25 -0700348 mRadioAttributes[r.mType] = r;
349 }
350
Robert Greenwaltec05b3c2009-10-30 14:17:42 -0700351 String[] naStrings = context.getResources().getStringArray(
352 com.android.internal.R.array.networkAttributes);
353 for (String naString : naStrings) {
354 try {
Robert Greenwalt34848c02011-03-25 13:09:25 -0700355 NetworkConfig n = new NetworkConfig(naString);
Wink Savillef2a62832011-04-07 14:23:45 -0700356 if (n.type > ConnectivityManager.MAX_NETWORK_TYPE) {
Wink Savillee70c6f52010-12-03 12:01:38 -0800357 loge("Error in networkAttributes - ignoring attempt to define type " +
Wink Savillef2a62832011-04-07 14:23:45 -0700358 n.type);
Robert Greenwaltec05b3c2009-10-30 14:17:42 -0700359 continue;
Robert Greenwalt2034b912009-08-12 16:08:25 -0700360 }
Wink Savillef2a62832011-04-07 14:23:45 -0700361 if (mNetConfigs[n.type] != null) {
Wink Savillee70c6f52010-12-03 12:01:38 -0800362 loge("Error in networkAttributes - ignoring attempt to redefine type " +
Wink Savillef2a62832011-04-07 14:23:45 -0700363 n.type);
Robert Greenwaltec05b3c2009-10-30 14:17:42 -0700364 continue;
365 }
Wink Savillef2a62832011-04-07 14:23:45 -0700366 if (mRadioAttributes[n.radio] == null) {
Wink Savillee70c6f52010-12-03 12:01:38 -0800367 loge("Error in networkAttributes - ignoring attempt to use undefined " +
Wink Savillef2a62832011-04-07 14:23:45 -0700368 "radio " + n.radio + " in network type " + n.type);
Robert Greenwaltec05b3c2009-10-30 14:17:42 -0700369 continue;
370 }
Wink Savillef2a62832011-04-07 14:23:45 -0700371 mNetConfigs[n.type] = n;
Robert Greenwaltec05b3c2009-10-30 14:17:42 -0700372 mNetworksDefined++;
373 } catch(Exception e) {
374 // ignore it - leave the entry null
Robert Greenwalt2034b912009-08-12 16:08:25 -0700375 }
376 }
377
Robert Greenwaltec05b3c2009-10-30 14:17:42 -0700378 // high priority first
379 mPriorityList = new int[mNetworksDefined];
380 {
381 int insertionPoint = mNetworksDefined-1;
382 int currentLowest = 0;
383 int nextLowest = 0;
384 while (insertionPoint > -1) {
Robert Greenwalt34848c02011-03-25 13:09:25 -0700385 for (NetworkConfig na : mNetConfigs) {
Robert Greenwaltec05b3c2009-10-30 14:17:42 -0700386 if (na == null) continue;
Wink Savillef2a62832011-04-07 14:23:45 -0700387 if (na.priority < currentLowest) continue;
388 if (na.priority > currentLowest) {
389 if (na.priority < nextLowest || nextLowest == 0) {
390 nextLowest = na.priority;
Robert Greenwaltec05b3c2009-10-30 14:17:42 -0700391 }
392 continue;
393 }
Wink Savillef2a62832011-04-07 14:23:45 -0700394 mPriorityList[insertionPoint--] = na.type;
Robert Greenwaltec05b3c2009-10-30 14:17:42 -0700395 }
396 currentLowest = nextLowest;
397 nextLowest = 0;
398 }
399 }
400
401 mNetRequestersPids = new ArrayList[ConnectivityManager.MAX_NETWORK_TYPE+1];
402 for (int i : mPriorityList) {
Robert Greenwalt2034b912009-08-12 16:08:25 -0700403 mNetRequestersPids[i] = new ArrayList();
404 }
405
406 mFeatureUsers = new ArrayList();
407
Robert Greenwaltec05b3c2009-10-30 14:17:42 -0700408 mNumDnsEntries = 0;
409
410 mTestMode = SystemProperties.get("cm.test.mode").equals("true")
411 && SystemProperties.get("ro.build.type").equals("eng");
The Android Open Source Project28527d22009-03-03 19:31:44 -0800412 /*
413 * Create the network state trackers for Wi-Fi and mobile
414 * data. Maybe this could be done with a factory class,
415 * but it's not clear that it's worth it, given that
416 * the number of different network types is not going
417 * to change very often.
418 */
Robert Greenwaltec05b3c2009-10-30 14:17:42 -0700419 for (int netType : mPriorityList) {
Wink Savillef2a62832011-04-07 14:23:45 -0700420 switch (mNetConfigs[netType].radio) {
Robert Greenwaltec05b3c2009-10-30 14:17:42 -0700421 case ConnectivityManager.TYPE_WIFI:
Wink Savillee70c6f52010-12-03 12:01:38 -0800422 if (DBG) log("Starting Wifi Service.");
Wink Saville7fabfa22010-08-13 16:11:42 -0700423 WifiStateTracker wst = new WifiStateTracker();
Irfan Sheriff25be0762010-07-28 09:35:20 -0700424 WifiService wifiService = new WifiService(context);
Robert Greenwaltec05b3c2009-10-30 14:17:42 -0700425 ServiceManager.addService(Context.WIFI_SERVICE, wifiService);
Irfan Sheriff25be0762010-07-28 09:35:20 -0700426 wifiService.checkAndStartWifi();
Robert Greenwaltec05b3c2009-10-30 14:17:42 -0700427 mNetTrackers[ConnectivityManager.TYPE_WIFI] = wst;
Wink Saville7fabfa22010-08-13 16:11:42 -0700428 wst.startMonitoring(context, mHandler);
The Android Open Source Project28527d22009-03-03 19:31:44 -0800429
Irfan Sheriff653e2a22010-06-07 09:03:04 -0700430 //TODO: as part of WWS refactor, create only when needed
Irfan Sheriff25be0762010-07-28 09:35:20 -0700431 mWifiWatchdogService = new WifiWatchdogService(context);
Irfan Sheriff653e2a22010-06-07 09:03:04 -0700432
Robert Greenwaltec05b3c2009-10-30 14:17:42 -0700433 break;
434 case ConnectivityManager.TYPE_MOBILE:
Wink Saville7fabfa22010-08-13 16:11:42 -0700435 mNetTrackers[netType] = new MobileDataStateTracker(netType,
Wink Savillef2a62832011-04-07 14:23:45 -0700436 mNetConfigs[netType].name);
Wink Saville7fabfa22010-08-13 16:11:42 -0700437 mNetTrackers[netType].startMonitoring(context, mHandler);
Robert Greenwaltec05b3c2009-10-30 14:17:42 -0700438 break;
Robert Greenwalteb123ac2010-12-06 13:56:24 -0800439 case ConnectivityManager.TYPE_DUMMY:
440 mNetTrackers[netType] = new DummyDataStateTracker(netType,
Wink Savillef2a62832011-04-07 14:23:45 -0700441 mNetConfigs[netType].name);
Robert Greenwalteb123ac2010-12-06 13:56:24 -0800442 mNetTrackers[netType].startMonitoring(context, mHandler);
443 break;
Jaikumar Ganesh0db51a02010-12-21 22:31:44 -0800444 case ConnectivityManager.TYPE_BLUETOOTH:
445 mNetTrackers[netType] = BluetoothTetheringDataTracker.getInstance();
446 mNetTrackers[netType].startMonitoring(context, mHandler);
447 break;
Benoit Goby211b5692010-12-22 14:29:40 -0800448 case ConnectivityManager.TYPE_ETHERNET:
449 mNetTrackers[netType] = EthernetDataTracker.getInstance();
450 mNetTrackers[netType].startMonitoring(context, mHandler);
451 break;
Robert Greenwaltec05b3c2009-10-30 14:17:42 -0700452 default:
Wink Savillee70c6f52010-12-03 12:01:38 -0800453 loge("Trying to create a DataStateTracker for an unknown radio type " +
Wink Savillef2a62832011-04-07 14:23:45 -0700454 mNetConfigs[netType].radio);
Robert Greenwaltec05b3c2009-10-30 14:17:42 -0700455 continue;
456 }
457 }
Robert Greenwalt0c4828c2010-01-26 11:40:34 -0800458
Chia-chi Yeh4df51322011-05-11 16:35:13 -0700459 IBinder b = ServiceManager.getService(Context.NETWORKMANAGEMENT_SERVICE);
460 INetworkManagementService nmService = INetworkManagementService.Stub.asInterface(b);
461
462 mTethering = new Tethering(mContext, nmService, mHandler.getLooper());
Robert Greenwaltf1b66e12010-02-25 12:29:30 -0800463 mTetheringConfigValid = (((mNetTrackers[ConnectivityManager.TYPE_MOBILE_DUN] != null) ||
464 !mTethering.isDunRequired()) &&
465 (mTethering.getTetherableUsbRegexs().length != 0 ||
Danica Chang96567052010-08-11 14:54:43 -0700466 mTethering.getTetherableWifiRegexs().length != 0 ||
467 mTethering.getTetherableBluetoothRegexs().length != 0) &&
Robert Greenwaltf1b66e12010-02-25 12:29:30 -0800468 mTethering.getUpstreamIfaceRegexs().length != 0);
469
Chia-chi Yeh9a4ad7d2011-05-23 17:26:46 -0700470 mVpn = new Vpn(mContext, new VpnCallback());
471
Chia-chi Yehf3204aa2011-05-23 15:08:29 -0700472 try {
473 nmService.registerObserver(mTethering);
Chia-chi Yeh9a4ad7d2011-05-23 17:26:46 -0700474 nmService.registerObserver(mVpn);
Chia-chi Yehf3204aa2011-05-23 15:08:29 -0700475 } catch (RemoteException e) {
476 loge("Error registering observer :" + e);
477 }
478
Robert Greenwalt0e80be12010-09-20 14:35:25 -0700479 if (DBG) {
480 mInetLog = new ArrayList();
481 }
Robert Greenwaltc3c5f862010-10-11 16:00:27 -0700482
483 mSettingsObserver = new SettingsObserver(mHandler, EVENT_APPLY_GLOBAL_HTTP_PROXY);
484 mSettingsObserver.observe(mContext);
Robert Greenwalt6f7c6092010-12-02 11:31:00 -0800485
486 loadGlobalProxy();
Hung-ying Tyan4e723422011-01-19 16:48:38 +0800487
488 VpnManager.startVpnService(context);
The Android Open Source Project28527d22009-03-03 19:31:44 -0800489 }
490
Robert Greenwaltec05b3c2009-10-30 14:17:42 -0700491
The Android Open Source Project28527d22009-03-03 19:31:44 -0800492 /**
Robert Greenwalt0659da32009-07-16 17:21:39 -0700493 * Sets the preferred network.
The Android Open Source Project28527d22009-03-03 19:31:44 -0800494 * @param preference the new preference
495 */
Robert Greenwalt6a2db8a2010-09-23 10:05:56 -0700496 public void setNetworkPreference(int preference) {
The Android Open Source Project28527d22009-03-03 19:31:44 -0800497 enforceChangePermission();
Robert Greenwalt6a2db8a2010-09-23 10:05:56 -0700498
499 mHandler.sendMessage(mHandler.obtainMessage(EVENT_SET_NETWORK_PREFERENCE, preference, 0));
The Android Open Source Project28527d22009-03-03 19:31:44 -0800500 }
501
502 public int getNetworkPreference() {
503 enforceAccessPermission();
Robert Greenwalt6a2db8a2010-09-23 10:05:56 -0700504 int preference;
505 synchronized(this) {
506 preference = mNetworkPreference;
507 }
508 return preference;
The Android Open Source Project28527d22009-03-03 19:31:44 -0800509 }
510
Robert Greenwalt6a2db8a2010-09-23 10:05:56 -0700511 private void handleSetNetworkPreference(int preference) {
512 if (ConnectivityManager.isNetworkTypeValid(preference) &&
Robert Greenwalt34848c02011-03-25 13:09:25 -0700513 mNetConfigs[preference] != null &&
514 mNetConfigs[preference].isDefault()) {
Robert Greenwalt6a2db8a2010-09-23 10:05:56 -0700515 if (mNetworkPreference != preference) {
516 final ContentResolver cr = mContext.getContentResolver();
517 Settings.Secure.putInt(cr, Settings.Secure.NETWORK_PREFERENCE, preference);
518 synchronized(this) {
519 mNetworkPreference = preference;
520 }
521 enforcePreference();
522 }
523 }
The Android Open Source Project28527d22009-03-03 19:31:44 -0800524 }
Robert Greenwalt0659da32009-07-16 17:21:39 -0700525
The Android Open Source Project28527d22009-03-03 19:31:44 -0800526 private int getPersistedNetworkPreference() {
527 final ContentResolver cr = mContext.getContentResolver();
528
529 final int networkPrefSetting = Settings.Secure
530 .getInt(cr, Settings.Secure.NETWORK_PREFERENCE, -1);
531 if (networkPrefSetting != -1) {
532 return networkPrefSetting;
533 }
534
535 return ConnectivityManager.DEFAULT_NETWORK_PREFERENCE;
536 }
Robert Greenwalt0659da32009-07-16 17:21:39 -0700537
The Android Open Source Project28527d22009-03-03 19:31:44 -0800538 /**
Robert Greenwalt0659da32009-07-16 17:21:39 -0700539 * Make the state of network connectivity conform to the preference settings
The Android Open Source Project28527d22009-03-03 19:31:44 -0800540 * In this method, we only tear down a non-preferred network. Establishing
541 * a connection to the preferred network is taken care of when we handle
542 * the disconnect event from the non-preferred network
543 * (see {@link #handleDisconnect(NetworkInfo)}).
544 */
545 private void enforcePreference() {
Robert Greenwalt2034b912009-08-12 16:08:25 -0700546 if (mNetTrackers[mNetworkPreference].getNetworkInfo().isConnected())
The Android Open Source Project28527d22009-03-03 19:31:44 -0800547 return;
548
Robert Greenwalt2034b912009-08-12 16:08:25 -0700549 if (!mNetTrackers[mNetworkPreference].isAvailable())
550 return;
The Android Open Source Project28527d22009-03-03 19:31:44 -0800551
Robert Greenwalt2034b912009-08-12 16:08:25 -0700552 for (int t=0; t <= ConnectivityManager.MAX_RADIO_TYPE; t++) {
Robert Greenwaltec05b3c2009-10-30 14:17:42 -0700553 if (t != mNetworkPreference && mNetTrackers[t] != null &&
Robert Greenwalt2034b912009-08-12 16:08:25 -0700554 mNetTrackers[t].getNetworkInfo().isConnected()) {
Robert Greenwaltf3f045b2009-08-20 15:25:14 -0700555 if (DBG) {
Wink Savillee70c6f52010-12-03 12:01:38 -0800556 log("tearing down " + mNetTrackers[t].getNetworkInfo() +
Robert Greenwaltf3f045b2009-08-20 15:25:14 -0700557 " in enforcePreference");
558 }
Robert Greenwalt2034b912009-08-12 16:08:25 -0700559 teardown(mNetTrackers[t]);
The Android Open Source Project28527d22009-03-03 19:31:44 -0800560 }
561 }
562 }
563
564 private boolean teardown(NetworkStateTracker netTracker) {
565 if (netTracker.teardown()) {
566 netTracker.setTeardownRequested(true);
567 return true;
568 } else {
569 return false;
570 }
571 }
572
573 /**
Jeff Sharkey921ebf22011-05-19 17:12:49 -0700574 * Check if UID is blocked from using the given {@link NetworkInfo}.
575 */
576 private boolean isNetworkBlocked(NetworkInfo info, int uid) {
577 synchronized (mUidRules) {
Jeff Sharkey21062e72011-05-28 20:56:34 -0700578 // TODO: expand definition of "paid" network to cover tethered or
579 // paid hotspot use cases.
580 final boolean networkIsPaid = info.getType() != ConnectivityManager.TYPE_WIFI;
581 final int uidRules = mUidRules.get(uid, RULE_ALLOW_ALL);
582
583 if (networkIsPaid && (uidRules & RULE_REJECT_PAID) != 0) {
584 return true;
585 }
586
587 // no restrictive rules; network is visible
588 return false;
Jeff Sharkey921ebf22011-05-19 17:12:49 -0700589 }
590 }
591
592 /**
Jeff Sharkey21062e72011-05-28 20:56:34 -0700593 * Return a filtered version of the given {@link NetworkInfo}, potentially
594 * marked {@link DetailedState#BLOCKED} based on
595 * {@link #isNetworkBlocked(NetworkInfo, int)}.
Jeff Sharkey921ebf22011-05-19 17:12:49 -0700596 */
Jeff Sharkey21062e72011-05-28 20:56:34 -0700597 private NetworkInfo filterNetworkInfo(NetworkInfo info, int uid) {
598 if (isNetworkBlocked(info, uid)) {
599 // network is blocked; clone and override state
600 info = new NetworkInfo(info);
601 info.setDetailedState(DetailedState.BLOCKED, null, null);
Jeff Sharkey921ebf22011-05-19 17:12:49 -0700602 }
Jeff Sharkey21062e72011-05-28 20:56:34 -0700603 return info;
Jeff Sharkey921ebf22011-05-19 17:12:49 -0700604 }
605
606 /**
The Android Open Source Project28527d22009-03-03 19:31:44 -0800607 * Return NetworkInfo for the active (i.e., connected) network interface.
608 * It is assumed that at most one network is active at a time. If more
609 * than one is active, it is indeterminate which will be returned.
Robert Greenwalt0659da32009-07-16 17:21:39 -0700610 * @return the info for the active network, or {@code null} if none is
611 * active
The Android Open Source Project28527d22009-03-03 19:31:44 -0800612 */
Jeff Sharkey921ebf22011-05-19 17:12:49 -0700613 @Override
The Android Open Source Project28527d22009-03-03 19:31:44 -0800614 public NetworkInfo getActiveNetworkInfo() {
Jeff Sharkey921ebf22011-05-19 17:12:49 -0700615 enforceAccessPermission();
616 final int uid = Binder.getCallingUid();
617 return getNetworkInfo(mActiveDefaultNetwork, uid);
The Android Open Source Project28527d22009-03-03 19:31:44 -0800618 }
619
Jeff Sharkey921ebf22011-05-19 17:12:49 -0700620 @Override
621 public NetworkInfo getActiveNetworkInfoForUid(int uid) {
622 enforceConnectivityInternalPermission();
623 return getNetworkInfo(mActiveDefaultNetwork, uid);
624 }
625
626 @Override
The Android Open Source Project28527d22009-03-03 19:31:44 -0800627 public NetworkInfo getNetworkInfo(int networkType) {
628 enforceAccessPermission();
Jeff Sharkey921ebf22011-05-19 17:12:49 -0700629 final int uid = Binder.getCallingUid();
630 return getNetworkInfo(networkType, uid);
The Android Open Source Project28527d22009-03-03 19:31:44 -0800631 }
632
Jeff Sharkey921ebf22011-05-19 17:12:49 -0700633 private NetworkInfo getNetworkInfo(int networkType, int uid) {
634 NetworkInfo info = null;
635 if (isNetworkTypeValid(networkType)) {
636 final NetworkStateTracker tracker = mNetTrackers[networkType];
637 if (tracker != null) {
Jeff Sharkey21062e72011-05-28 20:56:34 -0700638 info = filterNetworkInfo(tracker.getNetworkInfo(), uid);
Jeff Sharkey921ebf22011-05-19 17:12:49 -0700639 }
640 }
641 return info;
642 }
643
644 @Override
The Android Open Source Project28527d22009-03-03 19:31:44 -0800645 public NetworkInfo[] getAllNetworkInfo() {
646 enforceAccessPermission();
Jeff Sharkey921ebf22011-05-19 17:12:49 -0700647 final int uid = Binder.getCallingUid();
Jeff Sharkey21062e72011-05-28 20:56:34 -0700648 final ArrayList<NetworkInfo> result = Lists.newArrayList();
Jeff Sharkey921ebf22011-05-19 17:12:49 -0700649 synchronized (mUidRules) {
650 for (NetworkStateTracker tracker : mNetTrackers) {
651 if (tracker != null) {
Jeff Sharkey21062e72011-05-28 20:56:34 -0700652 result.add(filterNetworkInfo(tracker.getNetworkInfo(), uid));
Jeff Sharkey921ebf22011-05-19 17:12:49 -0700653 }
654 }
The Android Open Source Project28527d22009-03-03 19:31:44 -0800655 }
Jeff Sharkey21062e72011-05-28 20:56:34 -0700656 return result.toArray(new NetworkInfo[result.size()]);
The Android Open Source Project28527d22009-03-03 19:31:44 -0800657 }
658
Robert Greenwalt9f0ee4f2010-09-14 09:18:02 -0700659 /**
660 * Return LinkProperties for the active (i.e., connected) default
661 * network interface. It is assumed that at most one default network
662 * is active at a time. If more than one is active, it is indeterminate
663 * which will be returned.
664 * @return the ip properties for the active network, or {@code null} if
665 * none is active
666 */
Jeff Sharkey921ebf22011-05-19 17:12:49 -0700667 @Override
Robert Greenwalt9f0ee4f2010-09-14 09:18:02 -0700668 public LinkProperties getActiveLinkProperties() {
Robert Greenwalte1544bb2011-05-20 12:23:41 -0700669 return getLinkProperties(mActiveDefaultNetwork);
Robert Greenwalt9f0ee4f2010-09-14 09:18:02 -0700670 }
671
Jeff Sharkey921ebf22011-05-19 17:12:49 -0700672 @Override
Robert Greenwalt9f0ee4f2010-09-14 09:18:02 -0700673 public LinkProperties getLinkProperties(int networkType) {
674 enforceAccessPermission();
Jeff Sharkey921ebf22011-05-19 17:12:49 -0700675 if (isNetworkTypeValid(networkType)) {
676 final NetworkStateTracker tracker = mNetTrackers[networkType];
677 if (tracker != null) {
678 return tracker.getLinkProperties();
679 }
Robert Greenwalt9f0ee4f2010-09-14 09:18:02 -0700680 }
681 return null;
682 }
683
Jeff Sharkey21062e72011-05-28 20:56:34 -0700684 @Override
685 public NetworkState[] getAllNetworkState() {
686 enforceAccessPermission();
687 final int uid = Binder.getCallingUid();
688 final ArrayList<NetworkState> result = Lists.newArrayList();
689 synchronized (mUidRules) {
690 for (NetworkStateTracker tracker : mNetTrackers) {
691 if (tracker != null) {
692 final NetworkInfo info = filterNetworkInfo(tracker.getNetworkInfo(), uid);
693 result.add(new NetworkState(
694 info, tracker.getLinkProperties(), tracker.getLinkCapabilities()));
695 }
696 }
697 }
698 return result.toArray(new NetworkState[result.size()]);
699 }
700
The Android Open Source Project28527d22009-03-03 19:31:44 -0800701 public boolean setRadios(boolean turnOn) {
702 boolean result = true;
703 enforceChangePermission();
704 for (NetworkStateTracker t : mNetTrackers) {
Robert Greenwaltec05b3c2009-10-30 14:17:42 -0700705 if (t != null) result = t.setRadio(turnOn) && result;
The Android Open Source Project28527d22009-03-03 19:31:44 -0800706 }
707 return result;
708 }
709
710 public boolean setRadio(int netType, boolean turnOn) {
711 enforceChangePermission();
712 if (!ConnectivityManager.isNetworkTypeValid(netType)) {
713 return false;
714 }
715 NetworkStateTracker tracker = mNetTrackers[netType];
716 return tracker != null && tracker.setRadio(turnOn);
717 }
718
Robert Greenwaltaffc3a12009-09-27 17:27:04 -0700719 /**
720 * Used to notice when the calling process dies so we can self-expire
721 *
722 * Also used to know if the process has cleaned up after itself when
723 * our auto-expire timer goes off. The timer has a link to an object.
724 *
725 */
Robert Greenwalt2034b912009-08-12 16:08:25 -0700726 private class FeatureUser implements IBinder.DeathRecipient {
727 int mNetworkType;
728 String mFeature;
729 IBinder mBinder;
730 int mPid;
731 int mUid;
Robert Greenwalt3eeb6032009-12-21 18:24:07 -0800732 long mCreateTime;
Robert Greenwalt2034b912009-08-12 16:08:25 -0700733
734 FeatureUser(int type, String feature, IBinder binder) {
735 super();
736 mNetworkType = type;
737 mFeature = feature;
738 mBinder = binder;
739 mPid = getCallingPid();
740 mUid = getCallingUid();
Robert Greenwalt3eeb6032009-12-21 18:24:07 -0800741 mCreateTime = System.currentTimeMillis();
Robert Greenwaltaffc3a12009-09-27 17:27:04 -0700742
Robert Greenwalt2034b912009-08-12 16:08:25 -0700743 try {
744 mBinder.linkToDeath(this, 0);
745 } catch (RemoteException e) {
746 binderDied();
747 }
748 }
749
750 void unlinkDeathRecipient() {
751 mBinder.unlinkToDeath(this, 0);
752 }
753
754 public void binderDied() {
Wink Savillee70c6f52010-12-03 12:01:38 -0800755 log("ConnectivityService FeatureUser binderDied(" +
Robert Greenwalt3eeb6032009-12-21 18:24:07 -0800756 mNetworkType + ", " + mFeature + ", " + mBinder + "), created " +
757 (System.currentTimeMillis() - mCreateTime) + " mSec ago");
Robert Greenwaltaffc3a12009-09-27 17:27:04 -0700758 stopUsingNetworkFeature(this, false);
Robert Greenwalt2034b912009-08-12 16:08:25 -0700759 }
760
Robert Greenwaltaffc3a12009-09-27 17:27:04 -0700761 public void expire() {
Wink Savillee70c6f52010-12-03 12:01:38 -0800762 log("ConnectivityService FeatureUser expire(" +
Robert Greenwalt3eeb6032009-12-21 18:24:07 -0800763 mNetworkType + ", " + mFeature + ", " + mBinder +"), created " +
764 (System.currentTimeMillis() - mCreateTime) + " mSec ago");
Robert Greenwaltaffc3a12009-09-27 17:27:04 -0700765 stopUsingNetworkFeature(this, false);
766 }
Robert Greenwalt3eeb6032009-12-21 18:24:07 -0800767
768 public String toString() {
769 return "FeatureUser("+mNetworkType+","+mFeature+","+mPid+","+mUid+"), created " +
770 (System.currentTimeMillis() - mCreateTime) + " mSec ago";
771 }
Robert Greenwalt2034b912009-08-12 16:08:25 -0700772 }
773
Robert Greenwaltaffc3a12009-09-27 17:27:04 -0700774 // javadoc from interface
Robert Greenwalt2034b912009-08-12 16:08:25 -0700775 public int startUsingNetworkFeature(int networkType, String feature,
776 IBinder binder) {
777 if (DBG) {
Wink Savillee70c6f52010-12-03 12:01:38 -0800778 log("startUsingNetworkFeature for net " + networkType + ": " + feature);
Robert Greenwalt2034b912009-08-12 16:08:25 -0700779 }
The Android Open Source Project28527d22009-03-03 19:31:44 -0800780 enforceChangePermission();
Robert Greenwaltec05b3c2009-10-30 14:17:42 -0700781 if (!ConnectivityManager.isNetworkTypeValid(networkType) ||
Robert Greenwalt34848c02011-03-25 13:09:25 -0700782 mNetConfigs[networkType] == null) {
Robert Greenwalt2034b912009-08-12 16:08:25 -0700783 return Phone.APN_REQUEST_FAILED;
The Android Open Source Project28527d22009-03-03 19:31:44 -0800784 }
Robert Greenwalt2034b912009-08-12 16:08:25 -0700785
Robert Greenwaltaffc3a12009-09-27 17:27:04 -0700786 FeatureUser f = new FeatureUser(networkType, feature, binder);
Robert Greenwalt2034b912009-08-12 16:08:25 -0700787
788 // TODO - move this into the MobileDataStateTracker
789 int usedNetworkType = networkType;
790 if(networkType == ConnectivityManager.TYPE_MOBILE) {
Wink Savillef6b76692011-02-24 17:58:51 -0800791 usedNetworkType = convertFeatureToNetworkType(feature);
792 if (usedNetworkType < 0) {
793 Slog.e(TAG, "Can't match any netTracker!");
794 usedNetworkType = networkType;
Robert Greenwalt2034b912009-08-12 16:08:25 -0700795 }
796 }
797 NetworkStateTracker network = mNetTrackers[usedNetworkType];
798 if (network != null) {
Robert Greenwalt5364d752010-12-15 13:26:33 -0800799 Integer currentPid = new Integer(getCallingPid());
Robert Greenwalt2034b912009-08-12 16:08:25 -0700800 if (usedNetworkType != networkType) {
Robert Greenwalt2034b912009-08-12 16:08:25 -0700801 NetworkStateTracker radio = mNetTrackers[networkType];
802 NetworkInfo ni = network.getNetworkInfo();
803
804 if (ni.isAvailable() == false) {
Wink Savillee70c6f52010-12-03 12:01:38 -0800805 if (DBG) log("special network not available");
Robert Greenwalt2cc87442010-12-29 14:35:21 -0800806 if (!TextUtils.equals(feature,Phone.FEATURE_ENABLE_DUN_ALWAYS)) {
807 return Phone.APN_TYPE_NOT_AVAILABLE;
808 } else {
809 // else make the attempt anyway - probably giving REQUEST_STARTED below
810 }
Robert Greenwalt2034b912009-08-12 16:08:25 -0700811 }
812
Robert Greenwaltaffc3a12009-09-27 17:27:04 -0700813 synchronized(this) {
814 mFeatureUsers.add(f);
815 if (!mNetRequestersPids[usedNetworkType].contains(currentPid)) {
816 // this gets used for per-pid dns when connected
817 mNetRequestersPids[usedNetworkType].add(currentPid);
818 }
Robert Greenwalt2034b912009-08-12 16:08:25 -0700819 }
Robert Greenwaltaffc3a12009-09-27 17:27:04 -0700820
Robert Greenwalt20f819c2011-05-03 19:02:44 -0700821 int restoreTimer = getRestoreDefaultNetworkDelay(usedNetworkType);
822
823 if (restoreTimer >= 0) {
824 mHandler.sendMessageDelayed(
825 mHandler.obtainMessage(EVENT_RESTORE_DEFAULT_NETWORK, f), restoreTimer);
826 }
Robert Greenwalt2034b912009-08-12 16:08:25 -0700827
Robert Greenwalta52c75a2009-08-19 20:19:33 -0700828 if ((ni.isConnectedOrConnecting() == true) &&
829 !network.isTeardownRequested()) {
Robert Greenwalt2034b912009-08-12 16:08:25 -0700830 if (ni.isConnected() == true) {
831 // add the pid-specific dns
Robert Greenwalt3afbead2010-07-23 15:46:26 -0700832 handleDnsConfigurationChange(networkType);
Wink Savillee70c6f52010-12-03 12:01:38 -0800833 if (DBG) log("special network already active");
Robert Greenwalt2034b912009-08-12 16:08:25 -0700834 return Phone.APN_ALREADY_ACTIVE;
835 }
Wink Savillee70c6f52010-12-03 12:01:38 -0800836 if (DBG) log("special network already connecting");
Robert Greenwalt2034b912009-08-12 16:08:25 -0700837 return Phone.APN_REQUEST_STARTED;
838 }
839
840 // check if the radio in play can make another contact
841 // assume if cannot for now
842
Wink Savillee70c6f52010-12-03 12:01:38 -0800843 if (DBG) log("reconnecting to special network");
Robert Greenwalt2034b912009-08-12 16:08:25 -0700844 network.reconnect();
845 return Phone.APN_REQUEST_STARTED;
846 } else {
Robert Greenwalt5364d752010-12-15 13:26:33 -0800847 // need to remember this unsupported request so we respond appropriately on stop
848 synchronized(this) {
849 mFeatureUsers.add(f);
850 if (!mNetRequestersPids[usedNetworkType].contains(currentPid)) {
851 // this gets used for per-pid dns when connected
852 mNetRequestersPids[usedNetworkType].add(currentPid);
853 }
854 }
Robert Greenwaltd391e892010-05-18 10:52:51 -0700855 return -1;
Robert Greenwalt2034b912009-08-12 16:08:25 -0700856 }
857 }
858 return Phone.APN_TYPE_NOT_AVAILABLE;
The Android Open Source Project28527d22009-03-03 19:31:44 -0800859 }
860
Robert Greenwaltaffc3a12009-09-27 17:27:04 -0700861 // javadoc from interface
The Android Open Source Project28527d22009-03-03 19:31:44 -0800862 public int stopUsingNetworkFeature(int networkType, String feature) {
Robert Greenwalt28f43012009-10-06 17:52:40 -0700863 enforceChangePermission();
864
Robert Greenwaltaffc3a12009-09-27 17:27:04 -0700865 int pid = getCallingPid();
866 int uid = getCallingUid();
867
868 FeatureUser u = null;
869 boolean found = false;
870
871 synchronized(this) {
872 for (int i = 0; i < mFeatureUsers.size() ; i++) {
873 u = (FeatureUser)mFeatureUsers.get(i);
874 if (uid == u.mUid && pid == u.mPid &&
875 networkType == u.mNetworkType &&
876 TextUtils.equals(feature, u.mFeature)) {
877 found = true;
878 break;
879 }
880 }
881 }
882 if (found && u != null) {
883 // stop regardless of how many other time this proc had called start
884 return stopUsingNetworkFeature(u, true);
885 } else {
886 // none found!
Wink Savillee70c6f52010-12-03 12:01:38 -0800887 if (DBG) log("ignoring stopUsingNetworkFeature - not a live request");
Robert Greenwaltaffc3a12009-09-27 17:27:04 -0700888 return 1;
889 }
Robert Greenwalt2034b912009-08-12 16:08:25 -0700890 }
891
Robert Greenwaltaffc3a12009-09-27 17:27:04 -0700892 private int stopUsingNetworkFeature(FeatureUser u, boolean ignoreDups) {
893 int networkType = u.mNetworkType;
894 String feature = u.mFeature;
895 int pid = u.mPid;
896 int uid = u.mUid;
897
898 NetworkStateTracker tracker = null;
899 boolean callTeardown = false; // used to carry our decision outside of sync block
900
Robert Greenwalt2034b912009-08-12 16:08:25 -0700901 if (DBG) {
Wink Savillee70c6f52010-12-03 12:01:38 -0800902 log("stopUsingNetworkFeature for net " + networkType +
Robert Greenwalt2034b912009-08-12 16:08:25 -0700903 ": " + feature);
904 }
Robert Greenwalt28f43012009-10-06 17:52:40 -0700905
The Android Open Source Project28527d22009-03-03 19:31:44 -0800906 if (!ConnectivityManager.isNetworkTypeValid(networkType)) {
907 return -1;
908 }
Robert Greenwalt2034b912009-08-12 16:08:25 -0700909
Robert Greenwaltaffc3a12009-09-27 17:27:04 -0700910 // need to link the mFeatureUsers list with the mNetRequestersPids state in this
911 // sync block
912 synchronized(this) {
913 // check if this process still has an outstanding start request
914 if (!mFeatureUsers.contains(u)) {
Wink Savillee70c6f52010-12-03 12:01:38 -0800915 if (DBG) log("ignoring - this process has no outstanding requests");
Robert Greenwalt2034b912009-08-12 16:08:25 -0700916 return 1;
917 }
Robert Greenwaltaffc3a12009-09-27 17:27:04 -0700918 u.unlinkDeathRecipient();
919 mFeatureUsers.remove(mFeatureUsers.indexOf(u));
920 // If we care about duplicate requests, check for that here.
921 //
922 // This is done to support the extension of a request - the app
923 // can request we start the network feature again and renew the
924 // auto-shutoff delay. Normal "stop" calls from the app though
925 // do not pay attention to duplicate requests - in effect the
926 // API does not refcount and a single stop will counter multiple starts.
927 if (ignoreDups == false) {
928 for (int i = 0; i < mFeatureUsers.size() ; i++) {
929 FeatureUser x = (FeatureUser)mFeatureUsers.get(i);
930 if (x.mUid == u.mUid && x.mPid == u.mPid &&
931 x.mNetworkType == u.mNetworkType &&
932 TextUtils.equals(x.mFeature, u.mFeature)) {
Wink Savillee70c6f52010-12-03 12:01:38 -0800933 if (DBG) log("ignoring stopUsingNetworkFeature as dup is found");
Robert Greenwaltaffc3a12009-09-27 17:27:04 -0700934 return 1;
935 }
936 }
Robert Greenwalt2034b912009-08-12 16:08:25 -0700937 }
Robert Greenwaltaffc3a12009-09-27 17:27:04 -0700938
939 // TODO - move to MobileDataStateTracker
940 int usedNetworkType = networkType;
941 if (networkType == ConnectivityManager.TYPE_MOBILE) {
Wink Savillef6b76692011-02-24 17:58:51 -0800942 usedNetworkType = convertFeatureToNetworkType(feature);
943 if (usedNetworkType < 0) {
944 usedNetworkType = networkType;
Robert Greenwaltaffc3a12009-09-27 17:27:04 -0700945 }
946 }
947 tracker = mNetTrackers[usedNetworkType];
Robert Greenwaltec05b3c2009-10-30 14:17:42 -0700948 if (tracker == null) {
Wink Savillee70c6f52010-12-03 12:01:38 -0800949 if (DBG) log("ignoring - no known tracker for net type " + usedNetworkType);
Robert Greenwaltec05b3c2009-10-30 14:17:42 -0700950 return -1;
951 }
952 if (usedNetworkType != networkType) {
Robert Greenwaltaffc3a12009-09-27 17:27:04 -0700953 Integer currentPid = new Integer(pid);
Robert Greenwaltaffc3a12009-09-27 17:27:04 -0700954 mNetRequestersPids[usedNetworkType].remove(currentPid);
Robert Greenwalt0ca68a02009-12-17 14:54:59 -0800955 reassessPidDns(pid, true);
Robert Greenwaltaffc3a12009-09-27 17:27:04 -0700956 if (mNetRequestersPids[usedNetworkType].size() != 0) {
Wink Savillee70c6f52010-12-03 12:01:38 -0800957 if (DBG) log("not tearing down special network - " +
Robert Greenwaltaffc3a12009-09-27 17:27:04 -0700958 "others still using it");
959 return 1;
960 }
961 callTeardown = true;
Robert Greenwalt9f3be4c2011-01-10 11:58:31 -0800962 } else {
963 if (DBG) log("not a known feature - dropping");
Robert Greenwaltaffc3a12009-09-27 17:27:04 -0700964 }
965 }
Wink Savillee70c6f52010-12-03 12:01:38 -0800966 if (DBG) log("Doing network teardown");
Robert Greenwaltaffc3a12009-09-27 17:27:04 -0700967 if (callTeardown) {
968 tracker.teardown();
Robert Greenwalt2034b912009-08-12 16:08:25 -0700969 return 1;
970 } else {
Robert Greenwaltd391e892010-05-18 10:52:51 -0700971 return -1;
Robert Greenwalt2034b912009-08-12 16:08:25 -0700972 }
The Android Open Source Project28527d22009-03-03 19:31:44 -0800973 }
974
975 /**
Robert Greenwalt7fe44cb2010-08-27 09:24:29 -0700976 * @deprecated use requestRouteToHostAddress instead
977 *
The Android Open Source Project28527d22009-03-03 19:31:44 -0800978 * Ensure that a network route exists to deliver traffic to the specified
979 * host via the specified network interface.
Robert Greenwalt0659da32009-07-16 17:21:39 -0700980 * @param networkType the type of the network over which traffic to the
981 * specified host is to be routed
982 * @param hostAddress the IP address of the host to which the route is
983 * desired
The Android Open Source Project28527d22009-03-03 19:31:44 -0800984 * @return {@code true} on success, {@code false} on failure
985 */
986 public boolean requestRouteToHost(int networkType, int hostAddress) {
Robert Greenwalt7fe44cb2010-08-27 09:24:29 -0700987 InetAddress inetAddress = NetworkUtils.intToInetAddress(hostAddress);
988
989 if (inetAddress == null) {
990 return false;
991 }
992
993 return requestRouteToHostAddress(networkType, inetAddress.getAddress());
994 }
995
996 /**
997 * Ensure that a network route exists to deliver traffic to the specified
998 * host via the specified network interface.
999 * @param networkType the type of the network over which traffic to the
1000 * specified host is to be routed
1001 * @param hostAddress the IP address of the host to which the route is
1002 * desired
1003 * @return {@code true} on success, {@code false} on failure
1004 */
1005 public boolean requestRouteToHostAddress(int networkType, byte[] hostAddress) {
The Android Open Source Project28527d22009-03-03 19:31:44 -08001006 enforceChangePermission();
1007 if (!ConnectivityManager.isNetworkTypeValid(networkType)) {
1008 return false;
1009 }
1010 NetworkStateTracker tracker = mNetTrackers[networkType];
Robert Greenwalt4666ed02009-09-10 15:06:20 -07001011
Robert Greenwaltec05b3c2009-10-30 14:17:42 -07001012 if (tracker == null || !tracker.getNetworkInfo().isConnected() ||
1013 tracker.isTeardownRequested()) {
Robert Greenwalt4666ed02009-09-10 15:06:20 -07001014 if (DBG) {
Wink Savillee70c6f52010-12-03 12:01:38 -08001015 log("requestRouteToHostAddress on down network " +
Robert Greenwalt7fe44cb2010-08-27 09:24:29 -07001016 "(" + networkType + ") - dropped");
Robert Greenwalt4666ed02009-09-10 15:06:20 -07001017 }
1018 return false;
The Android Open Source Project28527d22009-03-03 19:31:44 -08001019 }
Robert Greenwalta7dfbd32010-06-15 15:43:39 -07001020 try {
Robert Greenwalt7fe44cb2010-08-27 09:24:29 -07001021 InetAddress addr = InetAddress.getByAddress(hostAddress);
Robert Greenwaltbd492212011-05-06 17:10:53 -07001022 return addHostRoute(tracker, addr, 0);
Robert Greenwalta7dfbd32010-06-15 15:43:39 -07001023 } catch (UnknownHostException e) {}
1024 return false;
Irfan Sheriff7f132d92010-06-09 15:39:36 -07001025 }
1026
1027 /**
1028 * Ensure that a network route exists to deliver traffic to the specified
1029 * host via the mobile data network.
1030 * @param hostAddress the IP address of the host to which the route is desired,
1031 * in network byte order.
Robert Greenwalta7dfbd32010-06-15 15:43:39 -07001032 * TODO - deprecate
Irfan Sheriff7f132d92010-06-09 15:39:36 -07001033 * @return {@code true} on success, {@code false} on failure
1034 */
Robert Greenwaltbd492212011-05-06 17:10:53 -07001035 private boolean addHostRoute(NetworkStateTracker nt, InetAddress hostAddress, int cycleCount) {
Robert Greenwaltbd492212011-05-06 17:10:53 -07001036 LinkProperties lp = nt.getLinkProperties();
1037 if ((lp == null) || (hostAddress == null)) return false;
Irfan Sheriff7f132d92010-06-09 15:39:36 -07001038
Robert Greenwaltbd492212011-05-06 17:10:53 -07001039 String interfaceName = lp.getInterfaceName();
Irfan Sheriff7f132d92010-06-09 15:39:36 -07001040 if (DBG) {
Robert Greenwaltbd492212011-05-06 17:10:53 -07001041 log("Requested host route to " + hostAddress + "(" + interfaceName + "), cycleCount=" +
1042 cycleCount);
Irfan Sheriff7f132d92010-06-09 15:39:36 -07001043 }
Robert Greenwaltbd492212011-05-06 17:10:53 -07001044 if (interfaceName == null) {
Wink Savillee70c6f52010-12-03 12:01:38 -08001045 if (DBG) loge("addHostRoute failed due to null interface name");
Irfan Sheriff7f132d92010-06-09 15:39:36 -07001046 return false;
1047 }
Robert Greenwaltbd492212011-05-06 17:10:53 -07001048
1049 RouteInfo bestRoute = RouteInfo.selectBestRoute(lp.getRoutes(), hostAddress);
Robert Greenwalt355205c2011-05-10 15:05:02 -07001050 InetAddress gatewayAddress = null;
Robert Greenwaltbd492212011-05-06 17:10:53 -07001051 if (bestRoute != null) {
Robert Greenwalt355205c2011-05-10 15:05:02 -07001052 gatewayAddress = bestRoute.getGateway();
Robert Greenwaltbd492212011-05-06 17:10:53 -07001053 // if the best route is ourself, don't relf-reference, just add the host route
Robert Greenwalt355205c2011-05-10 15:05:02 -07001054 if (hostAddress.equals(gatewayAddress)) gatewayAddress = null;
Robert Greenwaltbd492212011-05-06 17:10:53 -07001055 }
Robert Greenwalt355205c2011-05-10 15:05:02 -07001056 if (gatewayAddress != null) {
Robert Greenwaltbd492212011-05-06 17:10:53 -07001057 if (cycleCount > MAX_HOSTROUTE_CYCLE_COUNT) {
1058 loge("Error adding hostroute - too much recursion");
1059 return false;
1060 }
Robert Greenwalt355205c2011-05-10 15:05:02 -07001061 if (!addHostRoute(nt, gatewayAddress, cycleCount+1)) return false;
Robert Greenwaltbd492212011-05-06 17:10:53 -07001062 }
Robert Greenwalt355205c2011-05-10 15:05:02 -07001063
1064 RouteInfo route = RouteInfo.makeHostRoute(hostAddress, gatewayAddress);
1065
1066 try {
1067 mNetd.addRoute(interfaceName, route);
1068 return true;
1069 } catch (Exception ex) {
1070 return false;
1071 }
Robert Greenwaltbd492212011-05-06 17:10:53 -07001072 }
1073
1074 // TODO support the removal of single host routes. Keep a ref count of them so we
1075 // aren't over-zealous
1076 private boolean removeHostRoute(NetworkStateTracker nt, InetAddress hostAddress) {
1077 return false;
The Android Open Source Project28527d22009-03-03 19:31:44 -08001078 }
1079
1080 /**
1081 * @see ConnectivityManager#getBackgroundDataSetting()
1082 */
1083 public boolean getBackgroundDataSetting() {
Robert Greenwaltd62c7002010-12-29 16:15:02 -08001084 return mBackgroundDataEnabled.get();
The Android Open Source Project28527d22009-03-03 19:31:44 -08001085 }
Robert Greenwalt0659da32009-07-16 17:21:39 -07001086
The Android Open Source Project28527d22009-03-03 19:31:44 -08001087 /**
1088 * @see ConnectivityManager#setBackgroundDataSetting(boolean)
1089 */
1090 public void setBackgroundDataSetting(boolean allowBackgroundDataUsage) {
1091 mContext.enforceCallingOrSelfPermission(
1092 android.Manifest.permission.CHANGE_BACKGROUND_DATA_SETTING,
1093 "ConnectivityService");
Robert Greenwalt0659da32009-07-16 17:21:39 -07001094
Robert Greenwaltd62c7002010-12-29 16:15:02 -08001095 mBackgroundDataEnabled.set(allowBackgroundDataUsage);
1096
Robert Greenwalt6a2db8a2010-09-23 10:05:56 -07001097 mHandler.sendMessage(mHandler.obtainMessage(EVENT_SET_BACKGROUND_DATA,
1098 (allowBackgroundDataUsage ? ENABLED : DISABLED), 0));
1099 }
The Android Open Source Project28527d22009-03-03 19:31:44 -08001100
Robert Greenwalt6a2db8a2010-09-23 10:05:56 -07001101 private void handleSetBackgroundData(boolean enabled) {
Robert Greenwalt0ffdef12011-02-25 13:44:09 -08001102 Settings.Secure.putInt(mContext.getContentResolver(),
1103 Settings.Secure.BACKGROUND_DATA, enabled ? 1 : 0);
1104 Intent broadcast = new Intent(
1105 ConnectivityManager.ACTION_BACKGROUND_DATA_SETTING_CHANGED);
1106 mContext.sendBroadcast(broadcast);
Robert Greenwalt0659da32009-07-16 17:21:39 -07001107 }
1108
Robert Greenwalt1b21f6c2010-02-23 18:58:05 -08001109 /**
1110 * @see ConnectivityManager#getMobileDataEnabled()
1111 */
1112 public boolean getMobileDataEnabled() {
Wink Savilleb9024c62010-12-07 10:31:02 -08001113 // TODO: This detail should probably be in DataConnectionTracker's
1114 // which is where we store the value and maybe make this
1115 // asynchronous.
Robert Greenwalt1b21f6c2010-02-23 18:58:05 -08001116 enforceAccessPermission();
1117 boolean retVal = Settings.Secure.getInt(mContext.getContentResolver(),
1118 Settings.Secure.MOBILE_DATA, 1) == 1;
Wink Savillee70c6f52010-12-03 12:01:38 -08001119 if (DBG) log("getMobileDataEnabled returning " + retVal);
Robert Greenwalt1b21f6c2010-02-23 18:58:05 -08001120 return retVal;
1121 }
1122
Robert Greenwalt34848c02011-03-25 13:09:25 -07001123 public void setDataDependency(int networkType, boolean met) {
1124 enforceChangePermission();
1125 if (DBG) {
1126 log("setDataDependency(" + networkType + ", " + met + ")");
1127 }
1128 mHandler.sendMessage(mHandler.obtainMessage(EVENT_SET_DEPENDENCY_MET,
1129 (met ? ENABLED : DISABLED), networkType));
1130 }
1131
1132 private void handleSetDependencyMet(int networkType, boolean met) {
1133 if (mNetTrackers[networkType] != null) {
1134 if (DBG) {
1135 log("handleSetDependencyMet(" + networkType + ", " + met + ")");
1136 }
1137 mNetTrackers[networkType].setDependencyMet(met);
1138 }
1139 }
1140
Jeff Sharkey921ebf22011-05-19 17:12:49 -07001141 private INetworkPolicyListener mPolicyListener = new INetworkPolicyListener.Stub() {
1142 @Override
1143 public void onRulesChanged(int uid, int uidRules) {
1144 // only someone like NPMS should only be calling us
1145 // TODO: create permission for modifying data policy
1146 mContext.enforceCallingOrSelfPermission(UPDATE_DEVICE_STATS, TAG);
1147
1148 if (LOGD_RULES) {
1149 Slog.d(TAG, "onRulesChanged(uid=" + uid + ", uidRules=" + uidRules + ")");
1150 }
1151
1152 synchronized (mUidRules) {
1153 // skip update when we've already applied rules
1154 final int oldRules = mUidRules.get(uid, RULE_ALLOW_ALL);
1155 if (oldRules == uidRules) return;
1156
1157 mUidRules.put(uid, uidRules);
1158 }
1159
1160 // TODO: dispatch into NMS to push rules towards kernel module
1161 // TODO: notify UID when it has requested targeted updates
1162 }
1163 };
1164
Robert Greenwalt1b21f6c2010-02-23 18:58:05 -08001165 /**
1166 * @see ConnectivityManager#setMobileDataEnabled(boolean)
1167 */
Robert Greenwalt6a2db8a2010-09-23 10:05:56 -07001168 public void setMobileDataEnabled(boolean enabled) {
Robert Greenwalt1b21f6c2010-02-23 18:58:05 -08001169 enforceChangePermission();
Wink Savillee70c6f52010-12-03 12:01:38 -08001170 if (DBG) log("setMobileDataEnabled(" + enabled + ")");
Robert Greenwalt1b21f6c2010-02-23 18:58:05 -08001171
Robert Greenwalt6a2db8a2010-09-23 10:05:56 -07001172 mHandler.sendMessage(mHandler.obtainMessage(EVENT_SET_MOBILE_DATA,
Robert Greenwalt34848c02011-03-25 13:09:25 -07001173 (enabled ? ENABLED : DISABLED), 0));
Robert Greenwalt6a2db8a2010-09-23 10:05:56 -07001174 }
1175
1176 private void handleSetMobileData(boolean enabled) {
Wink Savilleb9024c62010-12-07 10:31:02 -08001177 if (mNetTrackers[ConnectivityManager.TYPE_MOBILE] != null) {
1178 if (DBG) {
1179 Slog.d(TAG, mNetTrackers[ConnectivityManager.TYPE_MOBILE].toString() + enabled);
Robert Greenwalt1b21f6c2010-02-23 18:58:05 -08001180 }
Wink Savilleb9024c62010-12-07 10:31:02 -08001181 mNetTrackers[ConnectivityManager.TYPE_MOBILE].setDataEnable(enabled);
Robert Greenwalt1b21f6c2010-02-23 18:58:05 -08001182 }
1183 }
1184
The Android Open Source Project28527d22009-03-03 19:31:44 -08001185 private void enforceAccessPermission() {
Robert Greenwalt0659da32009-07-16 17:21:39 -07001186 mContext.enforceCallingOrSelfPermission(
1187 android.Manifest.permission.ACCESS_NETWORK_STATE,
1188 "ConnectivityService");
The Android Open Source Project28527d22009-03-03 19:31:44 -08001189 }
1190
1191 private void enforceChangePermission() {
Robert Greenwalt0659da32009-07-16 17:21:39 -07001192 mContext.enforceCallingOrSelfPermission(
1193 android.Manifest.permission.CHANGE_NETWORK_STATE,
1194 "ConnectivityService");
The Android Open Source Project28527d22009-03-03 19:31:44 -08001195 }
1196
Robert Greenwalt0c4828c2010-01-26 11:40:34 -08001197 // TODO Make this a special check when it goes public
1198 private void enforceTetherChangePermission() {
1199 mContext.enforceCallingOrSelfPermission(
1200 android.Manifest.permission.CHANGE_NETWORK_STATE,
1201 "ConnectivityService");
1202 }
1203
Robert Greenwalt8e87f122010-02-11 18:18:40 -08001204 private void enforceTetherAccessPermission() {
1205 mContext.enforceCallingOrSelfPermission(
1206 android.Manifest.permission.ACCESS_NETWORK_STATE,
1207 "ConnectivityService");
1208 }
1209
Robert Greenwalt93dc1042010-06-15 12:19:37 -07001210 private void enforceConnectivityInternalPermission() {
1211 mContext.enforceCallingOrSelfPermission(
1212 android.Manifest.permission.CONNECTIVITY_INTERNAL,
1213 "ConnectivityService");
1214 }
1215
The Android Open Source Project28527d22009-03-03 19:31:44 -08001216 /**
Robert Greenwalt0659da32009-07-16 17:21:39 -07001217 * Handle a {@code DISCONNECTED} event. If this pertains to the non-active
1218 * network, we ignore it. If it is for the active network, we send out a
1219 * broadcast. But first, we check whether it might be possible to connect
1220 * to a different network.
The Android Open Source Project28527d22009-03-03 19:31:44 -08001221 * @param info the {@code NetworkInfo} for the network
1222 */
1223 private void handleDisconnect(NetworkInfo info) {
1224
Robert Greenwalt2034b912009-08-12 16:08:25 -07001225 int prevNetType = info.getType();
The Android Open Source Project28527d22009-03-03 19:31:44 -08001226
Robert Greenwalt2034b912009-08-12 16:08:25 -07001227 mNetTrackers[prevNetType].setTeardownRequested(false);
The Android Open Source Project28527d22009-03-03 19:31:44 -08001228 /*
1229 * If the disconnected network is not the active one, then don't report
1230 * this as a loss of connectivity. What probably happened is that we're
1231 * getting the disconnect for a network that we explicitly disabled
1232 * in accordance with network preference policies.
1233 */
Robert Greenwalt34848c02011-03-25 13:09:25 -07001234 if (!mNetConfigs[prevNetType].isDefault()) {
Robert Greenwalt2034b912009-08-12 16:08:25 -07001235 List pids = mNetRequestersPids[prevNetType];
1236 for (int i = 0; i<pids.size(); i++) {
1237 Integer pid = (Integer)pids.get(i);
1238 // will remove them because the net's no longer connected
1239 // need to do this now as only now do we know the pids and
1240 // can properly null things that are no longer referenced.
1241 reassessPidDns(pid.intValue(), false);
The Android Open Source Project28527d22009-03-03 19:31:44 -08001242 }
The Android Open Source Project28527d22009-03-03 19:31:44 -08001243 }
1244
The Android Open Source Project28527d22009-03-03 19:31:44 -08001245 Intent intent = new Intent(ConnectivityManager.CONNECTIVITY_ACTION);
1246 intent.putExtra(ConnectivityManager.EXTRA_NETWORK_INFO, info);
1247 if (info.isFailover()) {
1248 intent.putExtra(ConnectivityManager.EXTRA_IS_FAILOVER, true);
1249 info.setFailover(false);
1250 }
1251 if (info.getReason() != null) {
1252 intent.putExtra(ConnectivityManager.EXTRA_REASON, info.getReason());
1253 }
1254 if (info.getExtraInfo() != null) {
Robert Greenwalt0659da32009-07-16 17:21:39 -07001255 intent.putExtra(ConnectivityManager.EXTRA_EXTRA_INFO,
1256 info.getExtraInfo());
The Android Open Source Project28527d22009-03-03 19:31:44 -08001257 }
Robert Greenwalt2034b912009-08-12 16:08:25 -07001258
Robert Greenwalt34848c02011-03-25 13:09:25 -07001259 if (mNetConfigs[prevNetType].isDefault()) {
Robert Greenwalt4b7c55e2011-01-11 13:56:33 -08001260 tryFailover(prevNetType);
1261 if (mActiveDefaultNetwork != -1) {
1262 NetworkInfo switchTo = mNetTrackers[mActiveDefaultNetwork].getNetworkInfo();
Robert Greenwalt3cc68d32010-01-25 17:54:29 -08001263 intent.putExtra(ConnectivityManager.EXTRA_OTHER_NETWORK_INFO, switchTo);
1264 } else {
Robert Greenwaltb2a9e492010-09-20 18:01:43 -07001265 mDefaultInetConditionPublished = 0; // we're not connected anymore
Robert Greenwalt3cc68d32010-01-25 17:54:29 -08001266 intent.putExtra(ConnectivityManager.EXTRA_NO_CONNECTIVITY, true);
1267 }
Robert Greenwaltf55ced92010-01-20 19:29:41 -08001268 }
Robert Greenwaltb2a9e492010-09-20 18:01:43 -07001269 intent.putExtra(ConnectivityManager.EXTRA_INET_CONDITION, mDefaultInetConditionPublished);
Robert Greenwaltf55ced92010-01-20 19:29:41 -08001270 // do this before we broadcast the change
Robert Greenwalt3afbead2010-07-23 15:46:26 -07001271 handleConnectivityChange(prevNetType);
Robert Greenwaltf55ced92010-01-20 19:29:41 -08001272
1273 sendStickyBroadcast(intent);
1274 /*
1275 * If the failover network is already connected, then immediately send
1276 * out a followup broadcast indicating successful failover
1277 */
Robert Greenwalt4b7c55e2011-01-11 13:56:33 -08001278 if (mActiveDefaultNetwork != -1) {
1279 sendConnectedBroadcast(mNetTrackers[mActiveDefaultNetwork].getNetworkInfo());
Robert Greenwaltf55ced92010-01-20 19:29:41 -08001280 }
1281 }
1282
Robert Greenwalt4b7c55e2011-01-11 13:56:33 -08001283 private void tryFailover(int prevNetType) {
Robert Greenwalt2034b912009-08-12 16:08:25 -07001284 /*
Robert Greenwalt92564852011-01-06 15:41:07 -08001285 * If this is a default network, check if other defaults are available.
1286 * Try to reconnect on all available and let them hash it out when
1287 * more than one connects.
Robert Greenwalt2034b912009-08-12 16:08:25 -07001288 */
Robert Greenwalt34848c02011-03-25 13:09:25 -07001289 if (mNetConfigs[prevNetType].isDefault()) {
Robert Greenwalt2034b912009-08-12 16:08:25 -07001290 if (mActiveDefaultNetwork == prevNetType) {
1291 mActiveDefaultNetwork = -1;
1292 }
1293
Robert Greenwalt4b7c55e2011-01-11 13:56:33 -08001294 // don't signal a reconnect for anything lower or equal priority than our
1295 // current connected default
1296 // TODO - don't filter by priority now - nice optimization but risky
1297// int currentPriority = -1;
1298// if (mActiveDefaultNetwork != -1) {
Robert Greenwalt34848c02011-03-25 13:09:25 -07001299// currentPriority = mNetConfigs[mActiveDefaultNetwork].mPriority;
Robert Greenwalt4b7c55e2011-01-11 13:56:33 -08001300// }
Robert Greenwaltf55ced92010-01-20 19:29:41 -08001301 for (int checkType=0; checkType <= ConnectivityManager.MAX_NETWORK_TYPE; checkType++) {
Robert Greenwaltec05b3c2009-10-30 14:17:42 -07001302 if (checkType == prevNetType) continue;
Robert Greenwalt34848c02011-03-25 13:09:25 -07001303 if (mNetConfigs[checkType] == null) continue;
1304 if (!mNetConfigs[checkType].isDefault()) continue;
Wink Saville72a95b92011-01-26 15:43:49 -08001305
1306// Enabling the isAvailable() optimization caused mobile to not get
1307// selected if it was in the middle of error handling. Specifically
1308// a moble connection that took 30 seconds to complete the DEACTIVATE_DATA_CALL
1309// would not be available and we wouldn't get connected to anything.
1310// So removing the isAvailable() optimization below for now. TODO: This
1311// optimization should work and we need to investigate why it doesn't work.
1312// This could be related to how DEACTIVATE_DATA_CALL is reporting its
1313// complete before it is really complete.
1314// if (!mNetTrackers[checkType].isAvailable()) continue;
1315
Robert Greenwalt34848c02011-03-25 13:09:25 -07001316// if (currentPriority >= mNetConfigs[checkType].mPriority) continue;
Robert Greenwalt2034b912009-08-12 16:08:25 -07001317
Robert Greenwalt4b7c55e2011-01-11 13:56:33 -08001318 NetworkStateTracker checkTracker = mNetTrackers[checkType];
1319 NetworkInfo checkInfo = checkTracker.getNetworkInfo();
1320 if (!checkInfo.isConnectedOrConnecting() || checkTracker.isTeardownRequested()) {
1321 checkInfo.setFailover(true);
1322 checkTracker.reconnect();
Robert Greenwalt2034b912009-08-12 16:08:25 -07001323 }
Robert Greenwalt4b7c55e2011-01-11 13:56:33 -08001324 if (DBG) log("Attempting to switch to " + checkInfo.getTypeName());
Robert Greenwalt2034b912009-08-12 16:08:25 -07001325 }
The Android Open Source Project28527d22009-03-03 19:31:44 -08001326 }
The Android Open Source Project28527d22009-03-03 19:31:44 -08001327 }
1328
1329 private void sendConnectedBroadcast(NetworkInfo info) {
Robert Greenwaltd3401f92010-09-15 17:36:33 -07001330 sendGeneralBroadcast(info, ConnectivityManager.CONNECTIVITY_ACTION);
1331 }
1332
1333 private void sendInetConditionBroadcast(NetworkInfo info) {
1334 sendGeneralBroadcast(info, ConnectivityManager.INET_CONDITION_ACTION);
1335 }
1336
1337 private void sendGeneralBroadcast(NetworkInfo info, String bcastType) {
1338 Intent intent = new Intent(bcastType);
The Android Open Source Project28527d22009-03-03 19:31:44 -08001339 intent.putExtra(ConnectivityManager.EXTRA_NETWORK_INFO, info);
1340 if (info.isFailover()) {
1341 intent.putExtra(ConnectivityManager.EXTRA_IS_FAILOVER, true);
1342 info.setFailover(false);
1343 }
1344 if (info.getReason() != null) {
1345 intent.putExtra(ConnectivityManager.EXTRA_REASON, info.getReason());
1346 }
1347 if (info.getExtraInfo() != null) {
Robert Greenwalt0659da32009-07-16 17:21:39 -07001348 intent.putExtra(ConnectivityManager.EXTRA_EXTRA_INFO,
1349 info.getExtraInfo());
The Android Open Source Project28527d22009-03-03 19:31:44 -08001350 }
Robert Greenwalt986c7412010-09-08 15:24:47 -07001351 intent.putExtra(ConnectivityManager.EXTRA_INET_CONDITION, mDefaultInetConditionPublished);
Mike Lockwoodfde2b762009-08-14 14:18:49 -04001352 sendStickyBroadcast(intent);
The Android Open Source Project28527d22009-03-03 19:31:44 -08001353 }
1354
1355 /**
1356 * Called when an attempt to fail over to another network has failed.
1357 * @param info the {@link NetworkInfo} for the failed network
1358 */
1359 private void handleConnectionFailure(NetworkInfo info) {
1360 mNetTrackers[info.getType()].setTeardownRequested(false);
The Android Open Source Project28527d22009-03-03 19:31:44 -08001361
Robert Greenwalt2034b912009-08-12 16:08:25 -07001362 String reason = info.getReason();
1363 String extraInfo = info.getExtraInfo();
Robert Greenwalt0659da32009-07-16 17:21:39 -07001364
Robert Greenwalte981bc52010-10-08 16:35:52 -07001365 String reasonText;
1366 if (reason == null) {
1367 reasonText = ".";
1368 } else {
1369 reasonText = " (" + reason + ").";
The Android Open Source Project28527d22009-03-03 19:31:44 -08001370 }
Wink Savillee70c6f52010-12-03 12:01:38 -08001371 loge("Attempt to connect to " + info.getTypeName() + " failed" + reasonText);
Robert Greenwalt2034b912009-08-12 16:08:25 -07001372
1373 Intent intent = new Intent(ConnectivityManager.CONNECTIVITY_ACTION);
1374 intent.putExtra(ConnectivityManager.EXTRA_NETWORK_INFO, info);
1375 if (getActiveNetworkInfo() == null) {
1376 intent.putExtra(ConnectivityManager.EXTRA_NO_CONNECTIVITY, true);
1377 }
1378 if (reason != null) {
1379 intent.putExtra(ConnectivityManager.EXTRA_REASON, reason);
1380 }
1381 if (extraInfo != null) {
1382 intent.putExtra(ConnectivityManager.EXTRA_EXTRA_INFO, extraInfo);
1383 }
1384 if (info.isFailover()) {
1385 intent.putExtra(ConnectivityManager.EXTRA_IS_FAILOVER, true);
1386 info.setFailover(false);
1387 }
Robert Greenwaltf55ced92010-01-20 19:29:41 -08001388
Robert Greenwalt34848c02011-03-25 13:09:25 -07001389 if (mNetConfigs[info.getType()].isDefault()) {
Robert Greenwalt4b7c55e2011-01-11 13:56:33 -08001390 tryFailover(info.getType());
1391 if (mActiveDefaultNetwork != -1) {
1392 NetworkInfo switchTo = mNetTrackers[mActiveDefaultNetwork].getNetworkInfo();
Robert Greenwalt3cc68d32010-01-25 17:54:29 -08001393 intent.putExtra(ConnectivityManager.EXTRA_OTHER_NETWORK_INFO, switchTo);
1394 } else {
Robert Greenwaltb2a9e492010-09-20 18:01:43 -07001395 mDefaultInetConditionPublished = 0;
Robert Greenwalt3cc68d32010-01-25 17:54:29 -08001396 intent.putExtra(ConnectivityManager.EXTRA_NO_CONNECTIVITY, true);
1397 }
Robert Greenwaltf55ced92010-01-20 19:29:41 -08001398 }
Robert Greenwalt3cc68d32010-01-25 17:54:29 -08001399
Robert Greenwaltb2a9e492010-09-20 18:01:43 -07001400 intent.putExtra(ConnectivityManager.EXTRA_INET_CONDITION, mDefaultInetConditionPublished);
Mike Lockwoodfde2b762009-08-14 14:18:49 -04001401 sendStickyBroadcast(intent);
Robert Greenwaltf55ced92010-01-20 19:29:41 -08001402 /*
1403 * If the failover network is already connected, then immediately send
1404 * out a followup broadcast indicating successful failover
1405 */
Robert Greenwalt4b7c55e2011-01-11 13:56:33 -08001406 if (mActiveDefaultNetwork != -1) {
1407 sendConnectedBroadcast(mNetTrackers[mActiveDefaultNetwork].getNetworkInfo());
Robert Greenwaltf55ced92010-01-20 19:29:41 -08001408 }
Mike Lockwoodfde2b762009-08-14 14:18:49 -04001409 }
1410
1411 private void sendStickyBroadcast(Intent intent) {
1412 synchronized(this) {
Dianne Hackborna417ff82009-12-08 19:45:14 -08001413 if (!mSystemReady) {
1414 mInitialBroadcast = new Intent(intent);
Mike Lockwoodfde2b762009-08-14 14:18:49 -04001415 }
Dianne Hackborna417ff82009-12-08 19:45:14 -08001416 intent.addFlags(Intent.FLAG_RECEIVER_REGISTERED_ONLY_BEFORE_BOOT);
1417 mContext.sendStickyBroadcast(intent);
Mike Lockwoodfde2b762009-08-14 14:18:49 -04001418 }
1419 }
1420
1421 void systemReady() {
1422 synchronized(this) {
1423 mSystemReady = true;
Dianne Hackborna417ff82009-12-08 19:45:14 -08001424 if (mInitialBroadcast != null) {
1425 mContext.sendStickyBroadcast(mInitialBroadcast);
1426 mInitialBroadcast = null;
Mike Lockwoodfde2b762009-08-14 14:18:49 -04001427 }
1428 }
Robert Greenwaltc3c5f862010-10-11 16:00:27 -07001429 // load the global proxy at startup
1430 mHandler.sendMessage(mHandler.obtainMessage(EVENT_APPLY_GLOBAL_HTTP_PROXY));
The Android Open Source Project28527d22009-03-03 19:31:44 -08001431 }
1432
1433 private void handleConnect(NetworkInfo info) {
Robert Greenwalt2034b912009-08-12 16:08:25 -07001434 int type = info.getType();
The Android Open Source Project28527d22009-03-03 19:31:44 -08001435
1436 // snapshot isFailover, because sendConnectedBroadcast() resets it
1437 boolean isFailover = info.isFailover();
Robert Greenwalt2034b912009-08-12 16:08:25 -07001438 NetworkStateTracker thisNet = mNetTrackers[type];
The Android Open Source Project28527d22009-03-03 19:31:44 -08001439
Robert Greenwalt2034b912009-08-12 16:08:25 -07001440 // if this is a default net and other default is running
1441 // kill the one not preferred
Robert Greenwalt34848c02011-03-25 13:09:25 -07001442 if (mNetConfigs[type].isDefault()) {
Robert Greenwalt2034b912009-08-12 16:08:25 -07001443 if (mActiveDefaultNetwork != -1 && mActiveDefaultNetwork != type) {
1444 if ((type != mNetworkPreference &&
Wink Savillef2a62832011-04-07 14:23:45 -07001445 mNetConfigs[mActiveDefaultNetwork].priority >
1446 mNetConfigs[type].priority) ||
Robert Greenwalt2034b912009-08-12 16:08:25 -07001447 mNetworkPreference == mActiveDefaultNetwork) {
1448 // don't accept this one
Wink Savillee70c6f52010-12-03 12:01:38 -08001449 if (DBG) {
1450 log("Not broadcasting CONNECT_ACTION " +
Robert Greenwalt2034b912009-08-12 16:08:25 -07001451 "to torn down network " + info.getTypeName());
Wink Savillee70c6f52010-12-03 12:01:38 -08001452 }
Robert Greenwalt2034b912009-08-12 16:08:25 -07001453 teardown(thisNet);
1454 return;
1455 } else {
1456 // tear down the other
1457 NetworkStateTracker otherNet =
1458 mNetTrackers[mActiveDefaultNetwork];
Wink Savillee70c6f52010-12-03 12:01:38 -08001459 if (DBG) {
1460 log("Policy requires " + otherNet.getNetworkInfo().getTypeName() +
Robert Greenwalt2034b912009-08-12 16:08:25 -07001461 " teardown");
Wink Savillee70c6f52010-12-03 12:01:38 -08001462 }
Robert Greenwalt2034b912009-08-12 16:08:25 -07001463 if (!teardown(otherNet)) {
Wink Savillee70c6f52010-12-03 12:01:38 -08001464 loge("Network declined teardown request");
Robert Greenwalt99910172011-03-29 11:36:28 -07001465 teardown(thisNet);
Robert Greenwalt2034b912009-08-12 16:08:25 -07001466 return;
1467 }
Robert Greenwalt93dc1042010-06-15 12:19:37 -07001468 }
1469 }
1470 synchronized (ConnectivityService.this) {
1471 // have a new default network, release the transition wakelock in a second
1472 // if it's held. The second pause is to allow apps to reconnect over the
1473 // new network
1474 if (mNetTransitionWakeLock.isHeld()) {
1475 mHandler.sendMessageDelayed(mHandler.obtainMessage(
Robert Greenwaltccb36f92010-09-24 14:32:21 -07001476 EVENT_CLEAR_NET_TRANSITION_WAKELOCK,
Robert Greenwalt93dc1042010-06-15 12:19:37 -07001477 mNetTransitionWakeLockSerialNumber, 0),
1478 1000);
Robert Greenwalt2034b912009-08-12 16:08:25 -07001479 }
1480 }
1481 mActiveDefaultNetwork = type;
Robert Greenwalt986c7412010-09-08 15:24:47 -07001482 // this will cause us to come up initially as unconnected and switching
1483 // to connected after our normal pause unless somebody reports us as reall
1484 // disconnected
1485 mDefaultInetConditionPublished = 0;
1486 mDefaultConnectionSequence++;
1487 mInetConditionChangeInFlight = false;
1488 // Don't do this - if we never sign in stay, grey
1489 //reportNetworkCondition(mActiveDefaultNetwork, 100);
Robert Greenwalt2034b912009-08-12 16:08:25 -07001490 }
The Android Open Source Project28527d22009-03-03 19:31:44 -08001491 thisNet.setTeardownRequested(false);
Irfan Sheriff7f132d92010-06-09 15:39:36 -07001492 updateNetworkSettings(thisNet);
Robert Greenwalt3afbead2010-07-23 15:46:26 -07001493 handleConnectivityChange(type);
Robert Greenwalt2034b912009-08-12 16:08:25 -07001494 sendConnectedBroadcast(info);
The Android Open Source Project28527d22009-03-03 19:31:44 -08001495 }
1496
The Android Open Source Project28527d22009-03-03 19:31:44 -08001497 /**
Robert Greenwalt3afbead2010-07-23 15:46:26 -07001498 * After a change in the connectivity state of a network. We're mainly
1499 * concerned with making sure that the list of DNS servers is set up
1500 * according to which networks are connected, and ensuring that the
1501 * right routing table entries exist.
The Android Open Source Project28527d22009-03-03 19:31:44 -08001502 */
Robert Greenwalt3afbead2010-07-23 15:46:26 -07001503 private void handleConnectivityChange(int netType) {
The Android Open Source Project28527d22009-03-03 19:31:44 -08001504 /*
Robert Greenwalt2034b912009-08-12 16:08:25 -07001505 * If a non-default network is enabled, add the host routes that
Robert Greenwalt3afbead2010-07-23 15:46:26 -07001506 * will allow it's DNS servers to be accessed.
The Android Open Source Project28527d22009-03-03 19:31:44 -08001507 */
Robert Greenwalt3afbead2010-07-23 15:46:26 -07001508 handleDnsConfigurationChange(netType);
Robert Greenwalt2034b912009-08-12 16:08:25 -07001509
Robert Greenwalt3afbead2010-07-23 15:46:26 -07001510 if (mNetTrackers[netType].getNetworkInfo().isConnected()) {
Robert Greenwalt34848c02011-03-25 13:09:25 -07001511 if (mNetConfigs[netType].isDefault()) {
Robert Greenwaltc3c5f862010-10-11 16:00:27 -07001512 handleApplyDefaultProxy(netType);
Robert Greenwalt3afbead2010-07-23 15:46:26 -07001513 addDefaultRoute(mNetTrackers[netType]);
Robert Greenwalt2034b912009-08-12 16:08:25 -07001514 } else {
Robert Greenwalt1e2f2862011-04-01 10:51:22 -07001515 // many radios add a default route even when we don't want one.
1516 // remove the default route unless we need it for our active network
1517 if (mActiveDefaultNetwork != -1) {
1518 LinkProperties defaultLinkProperties =
1519 mNetTrackers[mActiveDefaultNetwork].getLinkProperties();
1520 LinkProperties newLinkProperties =
1521 mNetTrackers[netType].getLinkProperties();
1522 String defaultIface = defaultLinkProperties.getInterfaceName();
1523 if (defaultIface != null &&
1524 !defaultIface.equals(newLinkProperties.getInterfaceName())) {
1525 removeDefaultRoute(mNetTrackers[netType]);
1526 }
1527 }
Michael Jurka837e3642011-03-30 19:54:08 -07001528 addPrivateDnsRoutes(mNetTrackers[netType]);
Robert Greenwalt3afbead2010-07-23 15:46:26 -07001529 }
Kazuhiro Ondo3a340412011-04-30 20:10:57 -05001530
1531 /** Notify TetheringService if interface name has been changed. */
1532 if (TextUtils.equals(mNetTrackers[netType].getNetworkInfo().getReason(),
1533 Phone.REASON_LINK_PROPERTIES_CHANGED)) {
1534 handleTetherIfaceChange(netType);
1535 }
Robert Greenwalt3afbead2010-07-23 15:46:26 -07001536 } else {
Robert Greenwalt34848c02011-03-25 13:09:25 -07001537 if (mNetConfigs[netType].isDefault()) {
Robert Greenwalt3afbead2010-07-23 15:46:26 -07001538 removeDefaultRoute(mNetTrackers[netType]);
1539 } else {
1540 removePrivateDnsRoutes(mNetTrackers[netType]);
Robert Greenwalt2034b912009-08-12 16:08:25 -07001541 }
The Android Open Source Project28527d22009-03-03 19:31:44 -08001542 }
1543 }
1544
Irfan Sheriff7f132d92010-06-09 15:39:36 -07001545 private void addPrivateDnsRoutes(NetworkStateTracker nt) {
Irfan Sheriff7f132d92010-06-09 15:39:36 -07001546 boolean privateDnsRouteSet = nt.isPrivateDnsRouteSet();
Robert Greenwalt1f1bcfe2010-08-30 10:56:47 -07001547 LinkProperties p = nt.getLinkProperties();
Robert Greenwalta7dfbd32010-06-15 15:43:39 -07001548 if (p == null) return;
1549 String interfaceName = p.getInterfaceName();
Irfan Sheriff7f132d92010-06-09 15:39:36 -07001550
1551 if (DBG) {
Wink Savillee70c6f52010-12-03 12:01:38 -08001552 log("addPrivateDnsRoutes for " + nt +
Irfan Sheriff7f132d92010-06-09 15:39:36 -07001553 "(" + interfaceName + ") - mPrivateDnsRouteSet = " + privateDnsRouteSet);
1554 }
Irfan Sheriff7f132d92010-06-09 15:39:36 -07001555 if (interfaceName != null && !privateDnsRouteSet) {
Robert Greenwalta7dfbd32010-06-15 15:43:39 -07001556 Collection<InetAddress> dnsList = p.getDnses();
1557 for (InetAddress dns : dnsList) {
Robert Greenwaltbd492212011-05-06 17:10:53 -07001558 addHostRoute(nt, dns, 0);
Irfan Sheriff7f132d92010-06-09 15:39:36 -07001559 }
1560 nt.privateDnsRouteSet(true);
1561 }
1562 }
1563
1564 private void removePrivateDnsRoutes(NetworkStateTracker nt) {
Robert Greenwalt1f1bcfe2010-08-30 10:56:47 -07001565 LinkProperties p = nt.getLinkProperties();
Robert Greenwalta7dfbd32010-06-15 15:43:39 -07001566 if (p == null) return;
1567 String interfaceName = p.getInterfaceName();
Irfan Sheriff7f132d92010-06-09 15:39:36 -07001568 boolean privateDnsRouteSet = nt.isPrivateDnsRouteSet();
1569 if (interfaceName != null && privateDnsRouteSet) {
1570 if (DBG) {
Wink Savillee70c6f52010-12-03 12:01:38 -08001571 log("removePrivateDnsRoutes for " + nt.getNetworkInfo().getTypeName() +
Irfan Sheriff7f132d92010-06-09 15:39:36 -07001572 " (" + interfaceName + ")");
1573 }
Robert Greenwalt355205c2011-05-10 15:05:02 -07001574
1575 Collection<InetAddress> dnsList = p.getDnses();
1576 for (InetAddress dns : dnsList) {
1577 if (DBG) log(" removing " + dns);
1578 RouteInfo route = RouteInfo.makeHostRoute(dns);
1579 try {
1580 mNetd.removeRoute(interfaceName, route);
1581 } catch (Exception ex) {
1582 loge("error (" + ex + ") removing dns route " + route);
1583 }
1584 }
Irfan Sheriff7f132d92010-06-09 15:39:36 -07001585 nt.privateDnsRouteSet(false);
1586 }
1587 }
1588
Irfan Sheriff7f132d92010-06-09 15:39:36 -07001589
1590 private void addDefaultRoute(NetworkStateTracker nt) {
Robert Greenwalt1f1bcfe2010-08-30 10:56:47 -07001591 LinkProperties p = nt.getLinkProperties();
Robert Greenwalta7dfbd32010-06-15 15:43:39 -07001592 if (p == null) return;
1593 String interfaceName = p.getInterfaceName();
Robert Greenwalt5c733972011-02-09 13:56:06 -08001594 if (TextUtils.isEmpty(interfaceName)) return;
Irfan Sheriff7f132d92010-06-09 15:39:36 -07001595
Robert Greenwalt355205c2011-05-10 15:05:02 -07001596 for (RouteInfo route : p.getRoutes()) {
Robert Greenwalt5a901292011-04-28 14:28:50 -07001597 //TODO - handle non-default routes
1598 if (route.isDefaultRoute()) {
Robert Greenwalt355205c2011-05-10 15:05:02 -07001599 if (DBG) log("adding default route " + route);
Robert Greenwalt5a901292011-04-28 14:28:50 -07001600 InetAddress gateway = route.getGateway();
Robert Greenwalt355205c2011-05-10 15:05:02 -07001601 if (addHostRoute(nt, gateway, 0)) {
1602 try {
1603 mNetd.addRoute(interfaceName, route);
1604 } catch (Exception e) {
1605 loge("error adding default route " + route);
1606 continue;
1607 }
Robert Greenwalt5a901292011-04-28 14:28:50 -07001608 if (DBG) {
1609 NetworkInfo networkInfo = nt.getNetworkInfo();
1610 log("addDefaultRoute for " + networkInfo.getTypeName() +
1611 " (" + interfaceName + "), GatewayAddr=" +
1612 gateway.getHostAddress());
1613 }
Robert Greenwalt355205c2011-05-10 15:05:02 -07001614 } else {
1615 loge("error adding host route for default route " + route);
Robert Greenwalt03d53da2011-03-22 18:47:42 -07001616 }
Irfan Sheriff7f132d92010-06-09 15:39:36 -07001617 }
Irfan Sheriff7f132d92010-06-09 15:39:36 -07001618 }
1619 }
1620
1621
1622 public void removeDefaultRoute(NetworkStateTracker nt) {
Robert Greenwalt1f1bcfe2010-08-30 10:56:47 -07001623 LinkProperties p = nt.getLinkProperties();
Robert Greenwalta7dfbd32010-06-15 15:43:39 -07001624 if (p == null) return;
1625 String interfaceName = p.getInterfaceName();
Irfan Sheriff7f132d92010-06-09 15:39:36 -07001626
Robert Greenwalt355205c2011-05-10 15:05:02 -07001627 if (interfaceName == null) return;
1628
1629 for (RouteInfo route : p.getRoutes()) {
1630 //TODO - handle non-default routes
1631 if (route.isDefaultRoute()) {
1632 try {
1633 mNetd.removeRoute(interfaceName, route);
1634 } catch (Exception ex) {
1635 loge("error (" + ex + ") removing default route " + route);
1636 continue;
1637 }
Robert Greenwalt03d53da2011-03-22 18:47:42 -07001638 if (DBG) {
1639 NetworkInfo networkInfo = nt.getNetworkInfo();
1640 log("removeDefaultRoute for " + networkInfo.getTypeName() + " (" +
1641 interfaceName + ")");
1642 }
Irfan Sheriff7f132d92010-06-09 15:39:36 -07001643 }
Irfan Sheriff7f132d92010-06-09 15:39:36 -07001644 }
1645 }
1646
1647 /**
1648 * Reads the network specific TCP buffer sizes from SystemProperties
1649 * net.tcp.buffersize.[default|wifi|umts|edge|gprs] and set them for system
1650 * wide use
1651 */
1652 public void updateNetworkSettings(NetworkStateTracker nt) {
1653 String key = nt.getTcpBufferSizesPropName();
1654 String bufferSizes = SystemProperties.get(key);
1655
1656 if (bufferSizes.length() == 0) {
Wink Savillee70c6f52010-12-03 12:01:38 -08001657 loge(key + " not found in system properties. Using defaults");
Irfan Sheriff7f132d92010-06-09 15:39:36 -07001658
1659 // Setting to default values so we won't be stuck to previous values
1660 key = "net.tcp.buffersize.default";
1661 bufferSizes = SystemProperties.get(key);
1662 }
1663
1664 // Set values in kernel
1665 if (bufferSizes.length() != 0) {
1666 if (DBG) {
Wink Savillee70c6f52010-12-03 12:01:38 -08001667 log("Setting TCP values: [" + bufferSizes
Irfan Sheriff7f132d92010-06-09 15:39:36 -07001668 + "] which comes from [" + key + "]");
1669 }
1670 setBufferSize(bufferSizes);
1671 }
1672 }
1673
1674 /**
1675 * Writes TCP buffer sizes to /sys/kernel/ipv4/tcp_[r/w]mem_[min/def/max]
1676 * which maps to /proc/sys/net/ipv4/tcp_rmem and tcpwmem
1677 *
1678 * @param bufferSizes in the format of "readMin, readInitial, readMax,
1679 * writeMin, writeInitial, writeMax"
1680 */
1681 private void setBufferSize(String bufferSizes) {
1682 try {
1683 String[] values = bufferSizes.split(",");
1684
1685 if (values.length == 6) {
1686 final String prefix = "/sys/kernel/ipv4/tcp_";
Mike Lockwood0d5916c2011-05-28 13:24:04 -04001687 FileUtils.stringToFile(prefix + "rmem_min", values[0]);
1688 FileUtils.stringToFile(prefix + "rmem_def", values[1]);
1689 FileUtils.stringToFile(prefix + "rmem_max", values[2]);
1690 FileUtils.stringToFile(prefix + "wmem_min", values[3]);
1691 FileUtils.stringToFile(prefix + "wmem_def", values[4]);
1692 FileUtils.stringToFile(prefix + "wmem_max", values[5]);
Irfan Sheriff7f132d92010-06-09 15:39:36 -07001693 } else {
Wink Savillee70c6f52010-12-03 12:01:38 -08001694 loge("Invalid buffersize string: " + bufferSizes);
Irfan Sheriff7f132d92010-06-09 15:39:36 -07001695 }
1696 } catch (IOException e) {
Wink Savillee70c6f52010-12-03 12:01:38 -08001697 loge("Can't set tcp buffer sizes:" + e);
Irfan Sheriff7f132d92010-06-09 15:39:36 -07001698 }
1699 }
1700
Robert Greenwalt2034b912009-08-12 16:08:25 -07001701 /**
1702 * Adjust the per-process dns entries (net.dns<x>.<pid>) based
1703 * on the highest priority active net which this process requested.
1704 * If there aren't any, clear it out
1705 */
1706 private void reassessPidDns(int myPid, boolean doBump)
1707 {
Wink Savillee70c6f52010-12-03 12:01:38 -08001708 if (DBG) log("reassessPidDns for pid " + myPid);
Robert Greenwalt2034b912009-08-12 16:08:25 -07001709 for(int i : mPriorityList) {
Robert Greenwalt34848c02011-03-25 13:09:25 -07001710 if (mNetConfigs[i].isDefault()) {
Robert Greenwalt2034b912009-08-12 16:08:25 -07001711 continue;
1712 }
1713 NetworkStateTracker nt = mNetTrackers[i];
Robert Greenwalt0659da32009-07-16 17:21:39 -07001714 if (nt.getNetworkInfo().isConnected() &&
1715 !nt.isTeardownRequested()) {
Robert Greenwalt1f1bcfe2010-08-30 10:56:47 -07001716 LinkProperties p = nt.getLinkProperties();
Robert Greenwalta7dfbd32010-06-15 15:43:39 -07001717 if (p == null) continue;
Robert Greenwalt2034b912009-08-12 16:08:25 -07001718 List pids = mNetRequestersPids[i];
1719 for (int j=0; j<pids.size(); j++) {
1720 Integer pid = (Integer)pids.get(j);
1721 if (pid.intValue() == myPid) {
Robert Greenwalta7dfbd32010-06-15 15:43:39 -07001722 Collection<InetAddress> dnses = p.getDnses();
1723 writePidDns(dnses, myPid);
Robert Greenwalt2034b912009-08-12 16:08:25 -07001724 if (doBump) {
1725 bumpDns();
1726 }
1727 return;
1728 }
1729 }
1730 }
1731 }
1732 // nothing found - delete
1733 for (int i = 1; ; i++) {
1734 String prop = "net.dns" + i + "." + myPid;
1735 if (SystemProperties.get(prop).length() == 0) {
1736 if (doBump) {
1737 bumpDns();
1738 }
1739 return;
1740 }
1741 SystemProperties.set(prop, "");
1742 }
1743 }
1744
Robert Greenwalt8ca88762010-12-17 15:20:36 -08001745 // return true if results in a change
1746 private boolean writePidDns(Collection <InetAddress> dnses, int pid) {
Robert Greenwalt2034b912009-08-12 16:08:25 -07001747 int j = 1;
Robert Greenwalt8ca88762010-12-17 15:20:36 -08001748 boolean changed = false;
Robert Greenwalta7dfbd32010-06-15 15:43:39 -07001749 for (InetAddress dns : dnses) {
Robert Greenwalt8ca88762010-12-17 15:20:36 -08001750 String dnsString = dns.getHostAddress();
1751 if (changed || !dnsString.equals(SystemProperties.get("net.dns" + j + "." + pid))) {
1752 changed = true;
1753 SystemProperties.set("net.dns" + j++ + "." + pid, dns.getHostAddress());
1754 }
Robert Greenwalt2034b912009-08-12 16:08:25 -07001755 }
Robert Greenwalt8ca88762010-12-17 15:20:36 -08001756 return changed;
Robert Greenwalt2034b912009-08-12 16:08:25 -07001757 }
1758
1759 private void bumpDns() {
1760 /*
1761 * Bump the property that tells the name resolver library to reread
1762 * the DNS server list from the properties.
1763 */
1764 String propVal = SystemProperties.get("net.dnschange");
1765 int n = 0;
1766 if (propVal.length() != 0) {
1767 try {
1768 n = Integer.parseInt(propVal);
1769 } catch (NumberFormatException e) {}
1770 }
1771 SystemProperties.set("net.dnschange", "" + (n+1));
Robert Greenwalt051642b2010-11-02 14:08:23 -07001772 /*
1773 * Tell the VMs to toss their DNS caches
1774 */
1775 Intent intent = new Intent(Intent.ACTION_CLEAR_DNS_CACHE);
1776 intent.addFlags(Intent.FLAG_RECEIVER_REPLACE_PENDING);
Stan Chesnuttf444f502011-01-05 17:14:03 -08001777 /*
1778 * Connectivity events can happen before boot has completed ...
1779 */
1780 intent.addFlags(Intent.FLAG_RECEIVER_REGISTERED_ONLY_BEFORE_BOOT);
Robert Greenwalt051642b2010-11-02 14:08:23 -07001781 mContext.sendBroadcast(intent);
Robert Greenwalt2034b912009-08-12 16:08:25 -07001782 }
1783
Robert Greenwalt3afbead2010-07-23 15:46:26 -07001784 private void handleDnsConfigurationChange(int netType) {
Robert Greenwalt2034b912009-08-12 16:08:25 -07001785 // add default net's dns entries
Robert Greenwalt3afbead2010-07-23 15:46:26 -07001786 NetworkStateTracker nt = mNetTrackers[netType];
1787 if (nt != null && nt.getNetworkInfo().isConnected() && !nt.isTeardownRequested()) {
Robert Greenwalt1f1bcfe2010-08-30 10:56:47 -07001788 LinkProperties p = nt.getLinkProperties();
Robert Greenwalt3afbead2010-07-23 15:46:26 -07001789 if (p == null) return;
1790 Collection<InetAddress> dnses = p.getDnses();
Robert Greenwalt8ca88762010-12-17 15:20:36 -08001791 boolean changed = false;
Robert Greenwalt34848c02011-03-25 13:09:25 -07001792 if (mNetConfigs[netType].isDefault()) {
Robert Greenwalt3afbead2010-07-23 15:46:26 -07001793 int j = 1;
Robert Greenwalt94daa182010-09-01 11:34:05 -07001794 if (dnses.size() == 0 && mDefaultDns != null) {
Robert Greenwalt8ca88762010-12-17 15:20:36 -08001795 String dnsString = mDefaultDns.getHostAddress();
1796 if (!dnsString.equals(SystemProperties.get("net.dns1"))) {
1797 if (DBG) {
1798 log("no dns provided - using " + dnsString);
1799 }
1800 changed = true;
1801 SystemProperties.set("net.dns1", dnsString);
Robert Greenwalt2034b912009-08-12 16:08:25 -07001802 }
Robert Greenwalt94daa182010-09-01 11:34:05 -07001803 j++;
1804 } else {
1805 for (InetAddress dns : dnses) {
Robert Greenwalt8ca88762010-12-17 15:20:36 -08001806 String dnsString = dns.getHostAddress();
1807 if (!changed && dnsString.equals(SystemProperties.get("net.dns" + j))) {
1808 j++;
1809 continue;
1810 }
Robert Greenwalt94daa182010-09-01 11:34:05 -07001811 if (DBG) {
Wink Savillee70c6f52010-12-03 12:01:38 -08001812 log("adding dns " + dns + " for " +
Robert Greenwalt94daa182010-09-01 11:34:05 -07001813 nt.getNetworkInfo().getTypeName());
1814 }
Robert Greenwalt8ca88762010-12-17 15:20:36 -08001815 changed = true;
1816 SystemProperties.set("net.dns" + j++, dnsString);
Robert Greenwalt94daa182010-09-01 11:34:05 -07001817 }
Robert Greenwalt3afbead2010-07-23 15:46:26 -07001818 }
1819 for (int k=j ; k<mNumDnsEntries; k++) {
Robert Greenwalt8ca88762010-12-17 15:20:36 -08001820 if (changed || !TextUtils.isEmpty(SystemProperties.get("net.dns" + k))) {
1821 if (DBG) log("erasing net.dns" + k);
1822 changed = true;
1823 SystemProperties.set("net.dns" + k, "");
1824 }
Robert Greenwalt3afbead2010-07-23 15:46:26 -07001825 }
1826 mNumDnsEntries = j;
1827 } else {
1828 // set per-pid dns for attached secondary nets
1829 List pids = mNetRequestersPids[netType];
1830 for (int y=0; y< pids.size(); y++) {
1831 Integer pid = (Integer)pids.get(y);
Robert Greenwalt8ca88762010-12-17 15:20:36 -08001832 changed = writePidDns(dnses, pid.intValue());
The Android Open Source Project28527d22009-03-03 19:31:44 -08001833 }
1834 }
Robert Greenwalt8ca88762010-12-17 15:20:36 -08001835 if (changed) bumpDns();
The Android Open Source Project28527d22009-03-03 19:31:44 -08001836 }
Robert Greenwalt2034b912009-08-12 16:08:25 -07001837 }
1838
Robert Greenwalt20f819c2011-05-03 19:02:44 -07001839 private int getRestoreDefaultNetworkDelay(int networkType) {
Robert Greenwalt2034b912009-08-12 16:08:25 -07001840 String restoreDefaultNetworkDelayStr = SystemProperties.get(
1841 NETWORK_RESTORE_DELAY_PROP_NAME);
1842 if(restoreDefaultNetworkDelayStr != null &&
1843 restoreDefaultNetworkDelayStr.length() != 0) {
1844 try {
1845 return Integer.valueOf(restoreDefaultNetworkDelayStr);
1846 } catch (NumberFormatException e) {
1847 }
The Android Open Source Project28527d22009-03-03 19:31:44 -08001848 }
Robert Greenwalt20f819c2011-05-03 19:02:44 -07001849 // if the system property isn't set, use the value for the apn type
1850 int ret = RESTORE_DEFAULT_NETWORK_DELAY;
1851
1852 if ((networkType <= ConnectivityManager.MAX_NETWORK_TYPE) &&
1853 (mNetConfigs[networkType] != null)) {
1854 ret = mNetConfigs[networkType].restoreTime;
1855 }
1856 return ret;
The Android Open Source Project28527d22009-03-03 19:31:44 -08001857 }
1858
1859 @Override
1860 protected void dump(FileDescriptor fd, PrintWriter pw, String[] args) {
Robert Greenwalt0659da32009-07-16 17:21:39 -07001861 if (mContext.checkCallingOrSelfPermission(
1862 android.Manifest.permission.DUMP)
The Android Open Source Project28527d22009-03-03 19:31:44 -08001863 != PackageManager.PERMISSION_GRANTED) {
Robert Greenwalt0659da32009-07-16 17:21:39 -07001864 pw.println("Permission Denial: can't dump ConnectivityService " +
1865 "from from pid=" + Binder.getCallingPid() + ", uid=" +
1866 Binder.getCallingUid());
The Android Open Source Project28527d22009-03-03 19:31:44 -08001867 return;
1868 }
The Android Open Source Project28527d22009-03-03 19:31:44 -08001869 pw.println();
1870 for (NetworkStateTracker nst : mNetTrackers) {
Robert Greenwalt3eeb6032009-12-21 18:24:07 -08001871 if (nst != null) {
1872 if (nst.getNetworkInfo().isConnected()) {
1873 pw.println("Active network: " + nst.getNetworkInfo().
1874 getTypeName());
1875 }
1876 pw.println(nst.getNetworkInfo());
1877 pw.println(nst);
1878 pw.println();
Robert Greenwalt2034b912009-08-12 16:08:25 -07001879 }
The Android Open Source Project28527d22009-03-03 19:31:44 -08001880 }
Robert Greenwalt3eeb6032009-12-21 18:24:07 -08001881
1882 pw.println("Network Requester Pids:");
1883 for (int net : mPriorityList) {
1884 String pidString = net + ": ";
1885 for (Object pid : mNetRequestersPids[net]) {
1886 pidString = pidString + pid.toString() + ", ";
1887 }
1888 pw.println(pidString);
1889 }
1890 pw.println();
1891
1892 pw.println("FeatureUsers:");
1893 for (Object requester : mFeatureUsers) {
1894 pw.println(requester.toString());
1895 }
1896 pw.println();
Robert Greenwalt8e87f122010-02-11 18:18:40 -08001897
Robert Greenwalt93dc1042010-06-15 12:19:37 -07001898 synchronized (this) {
1899 pw.println("NetworkTranstionWakeLock is currently " +
1900 (mNetTransitionWakeLock.isHeld() ? "" : "not ") + "held.");
1901 pw.println("It was last requested for "+mNetTransitionWakeLockCausedBy);
1902 }
1903 pw.println();
1904
Robert Greenwalt8e87f122010-02-11 18:18:40 -08001905 mTethering.dump(fd, pw, args);
Robert Greenwalt0e80be12010-09-20 14:35:25 -07001906
1907 if (mInetLog != null) {
1908 pw.println();
1909 pw.println("Inet condition reports:");
1910 for(int i = 0; i < mInetLog.size(); i++) {
1911 pw.println(mInetLog.get(i));
1912 }
1913 }
The Android Open Source Project28527d22009-03-03 19:31:44 -08001914 }
1915
Robert Greenwalt2034b912009-08-12 16:08:25 -07001916 // must be stateless - things change under us.
The Android Open Source Project28527d22009-03-03 19:31:44 -08001917 private class MyHandler extends Handler {
Wink Saville775aad62010-09-02 19:23:52 -07001918 public MyHandler(Looper looper) {
1919 super(looper);
1920 }
1921
The Android Open Source Project28527d22009-03-03 19:31:44 -08001922 @Override
1923 public void handleMessage(Message msg) {
1924 NetworkInfo info;
1925 switch (msg.what) {
1926 case NetworkStateTracker.EVENT_STATE_CHANGED:
1927 info = (NetworkInfo) msg.obj;
Robert Greenwalt12c44552009-12-07 11:33:18 -08001928 int type = info.getType();
1929 NetworkInfo.State state = info.getState();
Robert Greenwalt12c44552009-12-07 11:33:18 -08001930
Wink Savillee70c6f52010-12-03 12:01:38 -08001931 if (DBG) log("ConnectivityChange for " +
Robert Greenwalt0659da32009-07-16 17:21:39 -07001932 info.getTypeName() + ": " +
Robert Greenwalt12c44552009-12-07 11:33:18 -08001933 state + "/" + info.getDetailedState());
The Android Open Source Project28527d22009-03-03 19:31:44 -08001934
1935 // Connectivity state changed:
1936 // [31-13] Reserved for future use
Robert Greenwalt0659da32009-07-16 17:21:39 -07001937 // [12-9] Network subtype (for mobile network, as defined
1938 // by TelephonyManager)
1939 // [8-3] Detailed state ordinal (as defined by
1940 // NetworkInfo.DetailedState)
The Android Open Source Project28527d22009-03-03 19:31:44 -08001941 // [2-0] Network type (as defined by ConnectivityManager)
1942 int eventLogParam = (info.getType() & 0x7) |
1943 ((info.getDetailedState().ordinal() & 0x3f) << 3) |
1944 (info.getSubtype() << 9);
Doug Zongker2fc96232009-12-04 10:31:43 -08001945 EventLog.writeEvent(EventLogTags.CONNECTIVITY_STATE_CHANGED,
Robert Greenwalt0659da32009-07-16 17:21:39 -07001946 eventLogParam);
1947
1948 if (info.getDetailedState() ==
1949 NetworkInfo.DetailedState.FAILED) {
The Android Open Source Project28527d22009-03-03 19:31:44 -08001950 handleConnectionFailure(info);
Robert Greenwalt12c44552009-12-07 11:33:18 -08001951 } else if (state == NetworkInfo.State.DISCONNECTED) {
The Android Open Source Project28527d22009-03-03 19:31:44 -08001952 handleDisconnect(info);
Robert Greenwalt12c44552009-12-07 11:33:18 -08001953 } else if (state == NetworkInfo.State.SUSPENDED) {
The Android Open Source Project28527d22009-03-03 19:31:44 -08001954 // TODO: need to think this over.
Robert Greenwalt0659da32009-07-16 17:21:39 -07001955 // the logic here is, handle SUSPENDED the same as
1956 // DISCONNECTED. The only difference being we are
1957 // broadcasting an intent with NetworkInfo that's
1958 // suspended. This allows the applications an
1959 // opportunity to handle DISCONNECTED and SUSPENDED
1960 // differently, or not.
The Android Open Source Project28527d22009-03-03 19:31:44 -08001961 handleDisconnect(info);
Robert Greenwalt12c44552009-12-07 11:33:18 -08001962 } else if (state == NetworkInfo.State.CONNECTED) {
The Android Open Source Project28527d22009-03-03 19:31:44 -08001963 handleConnect(info);
1964 }
The Android Open Source Project28527d22009-03-03 19:31:44 -08001965 break;
The Android Open Source Project28527d22009-03-03 19:31:44 -08001966 case NetworkStateTracker.EVENT_CONFIGURATION_CHANGED:
Robert Greenwalt3afbead2010-07-23 15:46:26 -07001967 info = (NetworkInfo) msg.obj;
Robert Greenwalt34848c02011-03-25 13:09:25 -07001968 handleConnectivityChange(info.getType());
The Android Open Source Project28527d22009-03-03 19:31:44 -08001969 break;
Robert Greenwaltccb36f92010-09-24 14:32:21 -07001970 case EVENT_CLEAR_NET_TRANSITION_WAKELOCK:
Robert Greenwalt93dc1042010-06-15 12:19:37 -07001971 String causedBy = null;
1972 synchronized (ConnectivityService.this) {
1973 if (msg.arg1 == mNetTransitionWakeLockSerialNumber &&
1974 mNetTransitionWakeLock.isHeld()) {
1975 mNetTransitionWakeLock.release();
1976 causedBy = mNetTransitionWakeLockCausedBy;
1977 }
1978 }
1979 if (causedBy != null) {
Wink Savillee70c6f52010-12-03 12:01:38 -08001980 log("NetTransition Wakelock for " + causedBy + " released by timeout");
Robert Greenwalt93dc1042010-06-15 12:19:37 -07001981 }
Robert Greenwaltcf1a56c2010-09-09 14:05:10 -07001982 break;
Robert Greenwalt6a2db8a2010-09-23 10:05:56 -07001983 case EVENT_RESTORE_DEFAULT_NETWORK:
Robert Greenwaltaffc3a12009-09-27 17:27:04 -07001984 FeatureUser u = (FeatureUser)msg.obj;
1985 u.expire();
Robert Greenwalt986c7412010-09-08 15:24:47 -07001986 break;
Robert Greenwalt6a2db8a2010-09-23 10:05:56 -07001987 case EVENT_INET_CONDITION_CHANGE:
1988 {
1989 int netType = msg.arg1;
1990 int condition = msg.arg2;
1991 handleInetConditionChange(netType, condition);
Robert Greenwalt93dc1042010-06-15 12:19:37 -07001992 break;
Robert Greenwalt6a2db8a2010-09-23 10:05:56 -07001993 }
1994 case EVENT_INET_CONDITION_HOLD_END:
1995 {
1996 int netType = msg.arg1;
1997 int sequence = msg.arg2;
1998 handleInetConditionHoldEnd(netType, sequence);
Robert Greenwalt986c7412010-09-08 15:24:47 -07001999 break;
Robert Greenwalt6a2db8a2010-09-23 10:05:56 -07002000 }
2001 case EVENT_SET_NETWORK_PREFERENCE:
2002 {
2003 int preference = msg.arg1;
2004 handleSetNetworkPreference(preference);
2005 break;
2006 }
2007 case EVENT_SET_BACKGROUND_DATA:
2008 {
2009 boolean enabled = (msg.arg1 == ENABLED);
2010 handleSetBackgroundData(enabled);
2011 break;
2012 }
2013 case EVENT_SET_MOBILE_DATA:
2014 {
2015 boolean enabled = (msg.arg1 == ENABLED);
2016 handleSetMobileData(enabled);
2017 break;
2018 }
Robert Greenwaltc3c5f862010-10-11 16:00:27 -07002019 case EVENT_APPLY_GLOBAL_HTTP_PROXY:
2020 {
2021 handleDeprecatedGlobalHttpProxy();
Robert Greenwalt34848c02011-03-25 13:09:25 -07002022 break;
2023 }
2024 case EVENT_SET_DEPENDENCY_MET:
2025 {
2026 boolean met = (msg.arg1 == ENABLED);
2027 handleSetDependencyMet(msg.arg2, met);
2028 break;
Robert Greenwaltc3c5f862010-10-11 16:00:27 -07002029 }
The Android Open Source Project28527d22009-03-03 19:31:44 -08002030 }
2031 }
2032 }
Robert Greenwalt0c4828c2010-01-26 11:40:34 -08002033
2034 // javadoc from interface
Robert Greenwalt4283ded2010-03-02 17:25:02 -08002035 public int tether(String iface) {
Robert Greenwalt0c4828c2010-01-26 11:40:34 -08002036 enforceTetherChangePermission();
Robert Greenwalt4283ded2010-03-02 17:25:02 -08002037
2038 if (isTetheringSupported()) {
2039 return mTethering.tether(iface);
2040 } else {
2041 return ConnectivityManager.TETHER_ERROR_UNSUPPORTED;
2042 }
Robert Greenwalt0c4828c2010-01-26 11:40:34 -08002043 }
2044
2045 // javadoc from interface
Robert Greenwalt4283ded2010-03-02 17:25:02 -08002046 public int untether(String iface) {
Robert Greenwalt0c4828c2010-01-26 11:40:34 -08002047 enforceTetherChangePermission();
Robert Greenwalt4283ded2010-03-02 17:25:02 -08002048
2049 if (isTetheringSupported()) {
2050 return mTethering.untether(iface);
2051 } else {
2052 return ConnectivityManager.TETHER_ERROR_UNSUPPORTED;
2053 }
2054 }
2055
2056 // javadoc from interface
2057 public int getLastTetherError(String iface) {
2058 enforceTetherAccessPermission();
2059
2060 if (isTetheringSupported()) {
2061 return mTethering.getLastTetherError(iface);
2062 } else {
2063 return ConnectivityManager.TETHER_ERROR_UNSUPPORTED;
2064 }
Robert Greenwalt8e87f122010-02-11 18:18:40 -08002065 }
2066
2067 // TODO - proper iface API for selection by property, inspection, etc
2068 public String[] getTetherableUsbRegexs() {
2069 enforceTetherAccessPermission();
2070 if (isTetheringSupported()) {
2071 return mTethering.getTetherableUsbRegexs();
2072 } else {
2073 return new String[0];
2074 }
2075 }
2076
2077 public String[] getTetherableWifiRegexs() {
2078 enforceTetherAccessPermission();
2079 if (isTetheringSupported()) {
2080 return mTethering.getTetherableWifiRegexs();
2081 } else {
2082 return new String[0];
2083 }
Robert Greenwalt0c4828c2010-01-26 11:40:34 -08002084 }
2085
Danica Chang96567052010-08-11 14:54:43 -07002086 public String[] getTetherableBluetoothRegexs() {
2087 enforceTetherAccessPermission();
2088 if (isTetheringSupported()) {
2089 return mTethering.getTetherableBluetoothRegexs();
2090 } else {
2091 return new String[0];
2092 }
2093 }
2094
Robert Greenwalt0c4828c2010-01-26 11:40:34 -08002095 // TODO - move iface listing, queries, etc to new module
2096 // javadoc from interface
2097 public String[] getTetherableIfaces() {
Robert Greenwalt8e87f122010-02-11 18:18:40 -08002098 enforceTetherAccessPermission();
Robert Greenwalt0c4828c2010-01-26 11:40:34 -08002099 return mTethering.getTetherableIfaces();
2100 }
2101
2102 public String[] getTetheredIfaces() {
Robert Greenwalt8e87f122010-02-11 18:18:40 -08002103 enforceTetherAccessPermission();
Robert Greenwalt0c4828c2010-01-26 11:40:34 -08002104 return mTethering.getTetheredIfaces();
2105 }
Robert Greenwalt8e87f122010-02-11 18:18:40 -08002106
Robert Greenwalt4283ded2010-03-02 17:25:02 -08002107 public String[] getTetheringErroredIfaces() {
2108 enforceTetherAccessPermission();
2109 return mTethering.getErroredIfaces();
2110 }
2111
Robert Greenwalt8e87f122010-02-11 18:18:40 -08002112 // if ro.tether.denied = true we default to no tethering
2113 // gservices could set the secure setting to 1 though to enable it on a build where it
2114 // had previously been turned off.
2115 public boolean isTetheringSupported() {
2116 enforceTetherAccessPermission();
2117 int defaultVal = (SystemProperties.get("ro.tether.denied").equals("true") ? 0 : 1);
Robert Greenwaltf1b66e12010-02-25 12:29:30 -08002118 boolean tetherEnabledInSettings = (Settings.Secure.getInt(mContext.getContentResolver(),
2119 Settings.Secure.TETHER_SUPPORTED, defaultVal) != 0);
2120 return tetherEnabledInSettings && mTetheringConfigValid;
Robert Greenwalt8e87f122010-02-11 18:18:40 -08002121 }
Robert Greenwalt93dc1042010-06-15 12:19:37 -07002122
2123 // An API NetworkStateTrackers can call when they lose their network.
2124 // This will automatically be cleared after X seconds or a network becomes CONNECTED,
2125 // whichever happens first. The timer is started by the first caller and not
2126 // restarted by subsequent callers.
2127 public void requestNetworkTransitionWakelock(String forWhom) {
2128 enforceConnectivityInternalPermission();
2129 synchronized (this) {
2130 if (mNetTransitionWakeLock.isHeld()) return;
2131 mNetTransitionWakeLockSerialNumber++;
2132 mNetTransitionWakeLock.acquire();
2133 mNetTransitionWakeLockCausedBy = forWhom;
2134 }
2135 mHandler.sendMessageDelayed(mHandler.obtainMessage(
Robert Greenwaltccb36f92010-09-24 14:32:21 -07002136 EVENT_CLEAR_NET_TRANSITION_WAKELOCK,
Robert Greenwalt93dc1042010-06-15 12:19:37 -07002137 mNetTransitionWakeLockSerialNumber, 0),
2138 mNetTransitionWakeLockTimeout);
2139 return;
2140 }
Robert Greenwalt24118e82010-09-09 13:15:32 -07002141
Robert Greenwalt986c7412010-09-08 15:24:47 -07002142 // 100 percent is full good, 0 is full bad.
2143 public void reportInetCondition(int networkType, int percentage) {
Wink Savillee70c6f52010-12-03 12:01:38 -08002144 if (DBG) log("reportNetworkCondition(" + networkType + ", " + percentage + ")");
Robert Greenwalt986c7412010-09-08 15:24:47 -07002145 mContext.enforceCallingOrSelfPermission(
2146 android.Manifest.permission.STATUS_BAR,
2147 "ConnectivityService");
2148
Robert Greenwalt0e80be12010-09-20 14:35:25 -07002149 if (DBG) {
2150 int pid = getCallingPid();
2151 int uid = getCallingUid();
2152 String s = pid + "(" + uid + ") reports inet is " +
2153 (percentage > 50 ? "connected" : "disconnected") + " (" + percentage + ") on " +
2154 "network Type " + networkType + " at " + GregorianCalendar.getInstance().getTime();
2155 mInetLog.add(s);
2156 while(mInetLog.size() > INET_CONDITION_LOG_MAX_SIZE) {
2157 mInetLog.remove(0);
2158 }
2159 }
Robert Greenwalt986c7412010-09-08 15:24:47 -07002160 mHandler.sendMessage(mHandler.obtainMessage(
Robert Greenwalt6a2db8a2010-09-23 10:05:56 -07002161 EVENT_INET_CONDITION_CHANGE, networkType, percentage));
2162 }
2163
2164 private void handleInetConditionChange(int netType, int condition) {
2165 if (DBG) {
Wink Savillee70c6f52010-12-03 12:01:38 -08002166 log("Inet connectivity change, net=" +
Robert Greenwalt6a2db8a2010-09-23 10:05:56 -07002167 netType + ", condition=" + condition +
2168 ",mActiveDefaultNetwork=" + mActiveDefaultNetwork);
2169 }
2170 if (mActiveDefaultNetwork == -1) {
Wink Savillee70c6f52010-12-03 12:01:38 -08002171 if (DBG) log("no active default network - aborting");
Robert Greenwalt6a2db8a2010-09-23 10:05:56 -07002172 return;
2173 }
2174 if (mActiveDefaultNetwork != netType) {
Wink Savillee70c6f52010-12-03 12:01:38 -08002175 if (DBG) log("given net not default - aborting");
Robert Greenwalt6a2db8a2010-09-23 10:05:56 -07002176 return;
2177 }
2178 mDefaultInetCondition = condition;
2179 int delay;
2180 if (mInetConditionChangeInFlight == false) {
Wink Savillee70c6f52010-12-03 12:01:38 -08002181 if (DBG) log("starting a change hold");
Robert Greenwalt6a2db8a2010-09-23 10:05:56 -07002182 // setup a new hold to debounce this
2183 if (mDefaultInetCondition > 50) {
2184 delay = Settings.Secure.getInt(mContext.getContentResolver(),
2185 Settings.Secure.INET_CONDITION_DEBOUNCE_UP_DELAY, 500);
2186 } else {
2187 delay = Settings.Secure.getInt(mContext.getContentResolver(),
2188 Settings.Secure.INET_CONDITION_DEBOUNCE_DOWN_DELAY, 3000);
2189 }
2190 mInetConditionChangeInFlight = true;
2191 mHandler.sendMessageDelayed(mHandler.obtainMessage(EVENT_INET_CONDITION_HOLD_END,
2192 mActiveDefaultNetwork, mDefaultConnectionSequence), delay);
2193 } else {
2194 // we've set the new condition, when this hold ends that will get
2195 // picked up
Wink Savillee70c6f52010-12-03 12:01:38 -08002196 if (DBG) log("currently in hold - not setting new end evt");
Robert Greenwalt6a2db8a2010-09-23 10:05:56 -07002197 }
2198 }
2199
2200 private void handleInetConditionHoldEnd(int netType, int sequence) {
2201 if (DBG) {
Wink Savillee70c6f52010-12-03 12:01:38 -08002202 log("Inet hold end, net=" + netType +
Robert Greenwalt6a2db8a2010-09-23 10:05:56 -07002203 ", condition =" + mDefaultInetCondition +
2204 ", published condition =" + mDefaultInetConditionPublished);
2205 }
2206 mInetConditionChangeInFlight = false;
2207
2208 if (mActiveDefaultNetwork == -1) {
Wink Savillee70c6f52010-12-03 12:01:38 -08002209 if (DBG) log("no active default network - aborting");
Robert Greenwalt6a2db8a2010-09-23 10:05:56 -07002210 return;
2211 }
2212 if (mDefaultConnectionSequence != sequence) {
Wink Savillee70c6f52010-12-03 12:01:38 -08002213 if (DBG) log("event hold for obsolete network - aborting");
Robert Greenwalt6a2db8a2010-09-23 10:05:56 -07002214 return;
2215 }
2216 if (mDefaultInetConditionPublished == mDefaultInetCondition) {
Wink Savillee70c6f52010-12-03 12:01:38 -08002217 if (DBG) log("no change in condition - aborting");
Robert Greenwalt6a2db8a2010-09-23 10:05:56 -07002218 return;
2219 }
2220 NetworkInfo networkInfo = mNetTrackers[mActiveDefaultNetwork].getNetworkInfo();
2221 if (networkInfo.isConnected() == false) {
Wink Savillee70c6f52010-12-03 12:01:38 -08002222 if (DBG) log("default network not connected - aborting");
Robert Greenwalt6a2db8a2010-09-23 10:05:56 -07002223 return;
2224 }
2225 mDefaultInetConditionPublished = mDefaultInetCondition;
2226 sendInetConditionBroadcast(networkInfo);
2227 return;
Robert Greenwalt986c7412010-09-08 15:24:47 -07002228 }
Robert Greenwaltc3c5f862010-10-11 16:00:27 -07002229
2230 public synchronized ProxyProperties getProxy() {
2231 if (mGlobalProxy != null) return mGlobalProxy;
2232 if (mDefaultProxy != null) return mDefaultProxy;
2233 return null;
2234 }
2235
2236 public void setGlobalProxy(ProxyProperties proxyProperties) {
2237 enforceChangePermission();
2238 synchronized (mGlobalProxyLock) {
2239 if (proxyProperties == mGlobalProxy) return;
2240 if (proxyProperties != null && proxyProperties.equals(mGlobalProxy)) return;
2241 if (mGlobalProxy != null && mGlobalProxy.equals(proxyProperties)) return;
2242
2243 String host = "";
2244 int port = 0;
2245 String exclList = "";
2246 if (proxyProperties != null && !TextUtils.isEmpty(proxyProperties.getHost())) {
2247 mGlobalProxy = new ProxyProperties(proxyProperties);
2248 host = mGlobalProxy.getHost();
2249 port = mGlobalProxy.getPort();
2250 exclList = mGlobalProxy.getExclusionList();
2251 } else {
2252 mGlobalProxy = null;
2253 }
2254 ContentResolver res = mContext.getContentResolver();
2255 Settings.Secure.putString(res, Settings.Secure.GLOBAL_HTTP_PROXY_HOST, host);
2256 Settings.Secure.putInt(res, Settings.Secure.GLOBAL_HTTP_PROXY_PORT, port);
Robert Greenwalt6f7c6092010-12-02 11:31:00 -08002257 Settings.Secure.putString(res, Settings.Secure.GLOBAL_HTTP_PROXY_EXCLUSION_LIST,
Robert Greenwaltc3c5f862010-10-11 16:00:27 -07002258 exclList);
2259 }
2260
2261 if (mGlobalProxy == null) {
2262 proxyProperties = mDefaultProxy;
2263 }
2264 sendProxyBroadcast(proxyProperties);
2265 }
2266
Robert Greenwalt6f7c6092010-12-02 11:31:00 -08002267 private void loadGlobalProxy() {
2268 ContentResolver res = mContext.getContentResolver();
2269 String host = Settings.Secure.getString(res, Settings.Secure.GLOBAL_HTTP_PROXY_HOST);
2270 int port = Settings.Secure.getInt(res, Settings.Secure.GLOBAL_HTTP_PROXY_PORT, 0);
2271 String exclList = Settings.Secure.getString(res,
2272 Settings.Secure.GLOBAL_HTTP_PROXY_EXCLUSION_LIST);
2273 if (!TextUtils.isEmpty(host)) {
2274 ProxyProperties proxyProperties = new ProxyProperties(host, port, exclList);
2275 synchronized (mGlobalProxyLock) {
2276 mGlobalProxy = proxyProperties;
2277 }
2278 }
2279 }
2280
Robert Greenwaltc3c5f862010-10-11 16:00:27 -07002281 public ProxyProperties getGlobalProxy() {
2282 synchronized (mGlobalProxyLock) {
2283 return mGlobalProxy;
2284 }
2285 }
2286
2287 private void handleApplyDefaultProxy(int type) {
2288 // check if new default - push it out to all VM if so
2289 ProxyProperties proxy = mNetTrackers[type].getLinkProperties().getHttpProxy();
2290 synchronized (this) {
2291 if (mDefaultProxy != null && mDefaultProxy.equals(proxy)) return;
2292 if (mDefaultProxy == proxy) return;
2293 if (!TextUtils.isEmpty(proxy.getHost())) {
2294 mDefaultProxy = proxy;
2295 } else {
2296 mDefaultProxy = null;
2297 }
2298 }
Wink Savillee70c6f52010-12-03 12:01:38 -08002299 if (DBG) log("changing default proxy to " + proxy);
Robert Greenwaltc3c5f862010-10-11 16:00:27 -07002300 if ((proxy == null && mGlobalProxy == null) || proxy.equals(mGlobalProxy)) return;
2301 if (mGlobalProxy != null) return;
2302 sendProxyBroadcast(proxy);
2303 }
2304
2305 private void handleDeprecatedGlobalHttpProxy() {
2306 String proxy = Settings.Secure.getString(mContext.getContentResolver(),
2307 Settings.Secure.HTTP_PROXY);
2308 if (!TextUtils.isEmpty(proxy)) {
2309 String data[] = proxy.split(":");
2310 String proxyHost = data[0];
2311 int proxyPort = 8080;
2312 if (data.length > 1) {
2313 try {
2314 proxyPort = Integer.parseInt(data[1]);
2315 } catch (NumberFormatException e) {
2316 return;
2317 }
2318 }
2319 ProxyProperties p = new ProxyProperties(data[0], proxyPort, "");
2320 setGlobalProxy(p);
2321 }
2322 }
2323
2324 private void sendProxyBroadcast(ProxyProperties proxy) {
Robert Greenwalt611291c2010-12-23 15:51:10 -08002325 if (proxy == null) proxy = new ProxyProperties("", 0, "");
Wink Savillee70c6f52010-12-03 12:01:38 -08002326 log("sending Proxy Broadcast for " + proxy);
Robert Greenwaltc3c5f862010-10-11 16:00:27 -07002327 Intent intent = new Intent(Proxy.PROXY_CHANGE_ACTION);
Stan Chesnutt1f2a2ac2011-01-06 11:00:19 -08002328 intent.addFlags(Intent.FLAG_RECEIVER_REPLACE_PENDING |
2329 Intent.FLAG_RECEIVER_REGISTERED_ONLY_BEFORE_BOOT);
Robert Greenwaltc3c5f862010-10-11 16:00:27 -07002330 intent.putExtra(Proxy.EXTRA_PROXY_INFO, proxy);
Robert Greenwaltd93dc8f2010-12-06 11:29:17 -08002331 mContext.sendStickyBroadcast(intent);
Robert Greenwaltc3c5f862010-10-11 16:00:27 -07002332 }
2333
2334 private static class SettingsObserver extends ContentObserver {
2335 private int mWhat;
2336 private Handler mHandler;
2337 SettingsObserver(Handler handler, int what) {
2338 super(handler);
2339 mHandler = handler;
2340 mWhat = what;
2341 }
2342
2343 void observe(Context context) {
2344 ContentResolver resolver = context.getContentResolver();
2345 resolver.registerContentObserver(Settings.Secure.getUriFor(
2346 Settings.Secure.HTTP_PROXY), false, this);
2347 }
2348
2349 @Override
2350 public void onChange(boolean selfChange) {
2351 mHandler.obtainMessage(mWhat).sendToTarget();
2352 }
2353 }
Wink Savillee70c6f52010-12-03 12:01:38 -08002354
Kazuhiro Ondo3a340412011-04-30 20:10:57 -05002355 private void handleTetherIfaceChange(int type) {
2356 String iface = mNetTrackers[type].getLinkProperties().getInterfaceName();
2357
2358 if (isTetheringSupported()) {
2359 mTethering.handleTetherIfaceChange(iface);
2360 }
2361 }
2362
Wink Savillee70c6f52010-12-03 12:01:38 -08002363 private void log(String s) {
2364 Slog.d(TAG, s);
2365 }
2366
2367 private void loge(String s) {
2368 Slog.e(TAG, s);
2369 }
Chia-chi Yeh9a4ad7d2011-05-23 17:26:46 -07002370
Wink Savillef6b76692011-02-24 17:58:51 -08002371 int convertFeatureToNetworkType(String feature){
2372 int networkType = -1;
2373 if (TextUtils.equals(feature, Phone.FEATURE_ENABLE_MMS)) {
2374 networkType = ConnectivityManager.TYPE_MOBILE_MMS;
2375 } else if (TextUtils.equals(feature, Phone.FEATURE_ENABLE_SUPL)) {
2376 networkType = ConnectivityManager.TYPE_MOBILE_SUPL;
2377 } else if (TextUtils.equals(feature, Phone.FEATURE_ENABLE_DUN) ||
2378 TextUtils.equals(feature, Phone.FEATURE_ENABLE_DUN_ALWAYS)) {
2379 networkType = ConnectivityManager.TYPE_MOBILE_DUN;
2380 } else if (TextUtils.equals(feature, Phone.FEATURE_ENABLE_HIPRI)) {
2381 networkType = ConnectivityManager.TYPE_MOBILE_HIPRI;
2382 } else if (TextUtils.equals(feature, Phone.FEATURE_ENABLE_FOTA)) {
2383 networkType = ConnectivityManager.TYPE_MOBILE_FOTA;
2384 } else if (TextUtils.equals(feature, Phone.FEATURE_ENABLE_IMS)) {
2385 networkType = ConnectivityManager.TYPE_MOBILE_IMS;
2386 } else if (TextUtils.equals(feature, Phone.FEATURE_ENABLE_CBS)) {
2387 networkType = ConnectivityManager.TYPE_MOBILE_CBS;
2388 }
2389 return networkType;
2390 }
Jeff Sharkey921ebf22011-05-19 17:12:49 -07002391
2392 private static <T> T checkNotNull(T value, String message) {
2393 if (value == null) {
2394 throw new NullPointerException(message);
2395 }
2396 return value;
2397 }
Chia-chi Yeh9a4ad7d2011-05-23 17:26:46 -07002398
2399 // @see ConnectivityManager#protectVpn(ParcelFileDescriptor)
2400 // Permission checks are done in Vpn class.
2401 @Override
2402 public void protectVpn(ParcelFileDescriptor socket) {
2403 mVpn.protect(socket, getDefaultInterface());
2404 }
2405
2406 // @see ConnectivityManager#prepareVpn(String)
2407 // Permission checks are done in Vpn class.
2408 @Override
2409 public String prepareVpn(String packageName) {
2410 return mVpn.prepare(packageName);
2411 }
2412
2413 // @see ConnectivityManager#establishVpn(Bundle)
2414 // Permission checks are done in Vpn class.
2415 @Override
2416 public ParcelFileDescriptor establishVpn(Bundle config) {
2417 return mVpn.establish(config);
2418 }
2419
2420 private String getDefaultInterface() {
2421 if (ConnectivityManager.isNetworkTypeValid(mActiveDefaultNetwork)) {
2422 NetworkStateTracker tracker = mNetTrackers[mActiveDefaultNetwork];
2423 if (tracker != null) {
2424 LinkProperties properties = tracker.getLinkProperties();
2425 if (properties != null) {
2426 return properties.getInterfaceName();
2427 }
2428 }
2429 }
2430 throw new IllegalStateException("No default interface");
2431 }
2432
2433 /**
2434 * Callback for VPN subsystem. Currently VPN is not adapted to the service
2435 * through NetworkStateTracker since it works differently. For example, it
2436 * needs to override DNS servers but never takes the default routes. It
2437 * relies on another data network, and it could keep existing connections
2438 * alive after reconnecting, switching between networks, or even resuming
2439 * from deep sleep. Calls from applications should be done synchronously
2440 * to avoid race conditions. As these are all hidden APIs, refactoring can
2441 * be done whenever a better abstraction is developed.
2442 */
2443 public class VpnCallback {
2444
2445 private VpnCallback() {
2446 }
2447
2448 public synchronized void override(String[] dnsServers) {
2449 // TODO: override DNS servers and http proxy.
2450 }
2451
2452 public synchronized void restore() {
2453 // TODO: restore VPN changes.
2454 }
2455 }
The Android Open Source Project28527d22009-03-03 19:31:44 -08002456}