blob: b559fb900d13b7c71d15c43b6733bafa81165b93 [file] [log] [blame]
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001/*
2 * Copyright (C) 2008 The Android Open Source Project
3 *
4 * Licensed under the Apache License, Version 2.0 (the "License");
5 * you may not use this file except in compliance with the License.
6 * You may obtain a copy of the License at
7 *
8 * http://www.apache.org/licenses/LICENSE-2.0
9 *
10 * Unless required by applicable law or agreed to in writing, software
11 * distributed under the License is distributed on an "AS IS" BASIS,
12 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13 * See the License for the specific language governing permissions and
14 * limitations under the License.
15 */
16
17package com.android.server;
18
Jeff Sharkeyc006f1a2011-05-19 17:12:49 -070019import static android.Manifest.permission.UPDATE_DEVICE_STATS;
20import static android.net.ConnectivityManager.isNetworkTypeValid;
21import static android.net.NetworkPolicyManager.RULE_ALLOW_ALL;
22import static android.net.NetworkPolicyManager.RULE_REJECT_PAID;
23
Jaikumar Ganesh15c74392010-12-21 22:31:44 -080024import android.bluetooth.BluetoothTetheringDataTracker;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080025import android.content.ContentResolver;
26import android.content.Context;
27import android.content.Intent;
28import android.content.pm.PackageManager;
Robert Greenwalt434203a2010-10-11 16:00:27 -070029import android.database.ContentObserver;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080030import android.net.ConnectivityManager;
Robert Greenwaltda3d5e62010-12-06 13:56:24 -080031import android.net.DummyDataStateTracker;
Benoit Goby19970692010-12-22 14:29:40 -080032import android.net.EthernetDataTracker;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080033import android.net.IConnectivityManager;
Jeff Sharkeyc006f1a2011-05-19 17:12:49 -070034import android.net.INetworkPolicyListener;
35import android.net.INetworkPolicyManager;
Jaikumar Ganesh15c74392010-12-21 22:31:44 -080036import android.net.LinkProperties;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080037import android.net.MobileDataStateTracker;
Robert Greenwaltd55a6b42011-03-25 13:09:25 -070038import android.net.NetworkConfig;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080039import android.net.NetworkInfo;
Jeff Sharkeyc006f1a2011-05-19 17:12:49 -070040import android.net.NetworkInfo.DetailedState;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080041import android.net.NetworkStateTracker;
Robert Greenwalt585ac0f2010-08-27 09:24:29 -070042import android.net.NetworkUtils;
Robert Greenwalt434203a2010-10-11 16:00:27 -070043import android.net.Proxy;
44import android.net.ProxyProperties;
Robert Greenwaltaa70f102011-04-28 14:28:50 -070045import android.net.RouteInfo;
Hung-ying Tyan6b818de2011-01-19 16:48:38 +080046import android.net.vpn.VpnManager;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080047import android.net.wifi.WifiStateTracker;
48import android.os.Binder;
Mike Lockwoodda8bb742011-05-28 13:24:04 -040049import android.os.FileUtils;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080050import android.os.Handler;
Wink Savillebb08caf2010-09-02 19:23:52 -070051import android.os.HandlerThread;
Robert Greenwalt42acef32009-08-12 16:08:25 -070052import android.os.IBinder;
Chia-chi Yehc9338302011-05-11 16:35:13 -070053import android.os.INetworkManagementService;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080054import android.os.Looper;
55import android.os.Message;
Robert Greenwalt14f2ef42010-06-15 12:19:37 -070056import android.os.PowerManager;
Robert Greenwalt42acef32009-08-12 16:08:25 -070057import android.os.RemoteException;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080058import android.os.ServiceManager;
59import android.os.SystemProperties;
60import android.provider.Settings;
Robert Greenwalt42acef32009-08-12 16:08:25 -070061import android.text.TextUtils;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080062import android.util.EventLog;
Joe Onorato8a9b2202010-02-26 18:56:32 -080063import android.util.Slog;
Jeff Sharkeyc006f1a2011-05-19 17:12:49 -070064import android.util.SparseIntArray;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080065
Robert Greenwalt42acef32009-08-12 16:08:25 -070066import com.android.internal.telephony.Phone;
Robert Greenwaltd0e18ff2010-01-26 11:40:34 -080067import com.android.server.connectivity.Tethering;
68
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080069import java.io.FileDescriptor;
Irfan Sheriffd649c122010-06-09 15:39:36 -070070import java.io.IOException;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080071import java.io.PrintWriter;
Robert Greenwalt47f69fe2010-06-15 15:43:39 -070072import java.net.InetAddress;
73import java.net.UnknownHostException;
Robert Greenwalt42acef32009-08-12 16:08:25 -070074import java.util.ArrayList;
Robert Greenwalt47f69fe2010-06-15 15:43:39 -070075import java.util.Collection;
Robert Greenwalt4e8dfef2010-09-20 14:35:25 -070076import java.util.GregorianCalendar;
Robert Greenwalt42acef32009-08-12 16:08:25 -070077import java.util.List;
Jeff Sharkeyc006f1a2011-05-19 17:12:49 -070078import java.util.concurrent.atomic.AtomicBoolean;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080079
80/**
81 * @hide
82 */
83public class ConnectivityService extends IConnectivityManager.Stub {
84
Robert Greenwaltba175a52010-10-05 19:12:26 -070085 private static final boolean DBG = true;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080086 private static final String TAG = "ConnectivityService";
87
Jeff Sharkeyc006f1a2011-05-19 17:12:49 -070088 private static final boolean LOGD_RULES = false;
89
Robert Greenwalt42acef32009-08-12 16:08:25 -070090 // how long to wait before switching back to a radio's default network
91 private static final int RESTORE_DEFAULT_NETWORK_DELAY = 1 * 60 * 1000;
92 // system property that can override the above value
93 private static final String NETWORK_RESTORE_DELAY_PROP_NAME =
94 "android.telephony.apn-restore";
95
Robert Greenwaltf43396c2011-05-06 17:10:53 -070096 // used in recursive route setting to add gateways for the host for which
97 // a host route was requested.
98 private static final int MAX_HOSTROUTE_CYCLE_COUNT = 10;
99
Robert Greenwaltd0e18ff2010-01-26 11:40:34 -0800100 private Tethering mTethering;
Robert Greenwaltc9d5fb72010-02-25 12:29:30 -0800101 private boolean mTetheringConfigValid = false;
Robert Greenwaltd0e18ff2010-01-26 11:40:34 -0800102
Jeff Sharkeyc006f1a2011-05-19 17:12:49 -0700103 /** Currently active network rules by UID. */
104 private SparseIntArray mUidRules = new SparseIntArray();
105
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800106 /**
107 * Sometimes we want to refer to the individual network state
108 * trackers separately, and sometimes we just want to treat them
109 * abstractly.
110 */
111 private NetworkStateTracker mNetTrackers[];
Robert Greenwalt42acef32009-08-12 16:08:25 -0700112
113 /**
114 * A per Net list of the PID's that requested access to the net
115 * used both as a refcount and for per-PID DNS selection
116 */
117 private List mNetRequestersPids[];
118
Irfan Sheriffa2a1b912010-06-07 09:03:04 -0700119 private WifiWatchdogService mWifiWatchdogService;
120
Robert Greenwalt42acef32009-08-12 16:08:25 -0700121 // priority order of the nettrackers
122 // (excluding dynamically set mNetworkPreference)
123 // TODO - move mNetworkTypePreference into this
124 private int[] mPriorityList;
125
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800126 private Context mContext;
127 private int mNetworkPreference;
Robert Greenwalt42acef32009-08-12 16:08:25 -0700128 private int mActiveDefaultNetwork = -1;
Robert Greenwaltd7085fc2010-09-08 15:24:47 -0700129 // 0 is full bad, 100 is full good
130 private int mDefaultInetCondition = 0;
131 private int mDefaultInetConditionPublished = 0;
132 private boolean mInetConditionChangeInFlight = false;
133 private int mDefaultConnectionSequence = 0;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800134
135 private int mNumDnsEntries;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800136
137 private boolean mTestMode;
Joe Onorato00092872010-09-01 21:18:22 -0700138 private static ConnectivityService sServiceInstance;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800139
Robert Greenwaltd825ea42010-12-29 16:15:02 -0800140 private AtomicBoolean mBackgroundDataEnabled = new AtomicBoolean(true);
141
Robert Greenwalt59b1a4e2011-05-10 15:05:02 -0700142 private INetworkManagementService mNetd;
Jeff Sharkeyc006f1a2011-05-19 17:12:49 -0700143 private INetworkPolicyManager mPolicyManager;
Robert Greenwalt59b1a4e2011-05-10 15:05:02 -0700144
Robert Greenwalt8dcc28b2010-09-23 10:05:56 -0700145 private static final int ENABLED = 1;
146 private static final int DISABLED = 0;
147
148 // Share the event space with NetworkStateTracker (which can't see this
149 // internal class but sends us events). If you change these, change
150 // NetworkStateTracker.java too.
151 private static final int MIN_NETWORK_STATE_TRACKER_EVENT = 1;
152 private static final int MAX_NETWORK_STATE_TRACKER_EVENT = 100;
153
154 /**
155 * used internally as a delayed event to make us switch back to the
156 * default network
157 */
158 private static final int EVENT_RESTORE_DEFAULT_NETWORK =
159 MAX_NETWORK_STATE_TRACKER_EVENT + 1;
160
161 /**
162 * used internally to change our mobile data enabled flag
163 */
164 private static final int EVENT_CHANGE_MOBILE_DATA_ENABLED =
165 MAX_NETWORK_STATE_TRACKER_EVENT + 2;
166
167 /**
168 * used internally to change our network preference setting
169 * arg1 = networkType to prefer
170 */
171 private static final int EVENT_SET_NETWORK_PREFERENCE =
172 MAX_NETWORK_STATE_TRACKER_EVENT + 3;
173
174 /**
175 * used internally to synchronize inet condition reports
176 * arg1 = networkType
177 * arg2 = condition (0 bad, 100 good)
178 */
179 private static final int EVENT_INET_CONDITION_CHANGE =
180 MAX_NETWORK_STATE_TRACKER_EVENT + 4;
181
182 /**
183 * used internally to mark the end of inet condition hold periods
184 * arg1 = networkType
185 */
186 private static final int EVENT_INET_CONDITION_HOLD_END =
187 MAX_NETWORK_STATE_TRACKER_EVENT + 5;
188
189 /**
190 * used internally to set the background data preference
191 * arg1 = TRUE for enabled, FALSE for disabled
192 */
193 private static final int EVENT_SET_BACKGROUND_DATA =
194 MAX_NETWORK_STATE_TRACKER_EVENT + 6;
195
196 /**
197 * used internally to set enable/disable cellular data
198 * arg1 = ENBALED or DISABLED
199 */
200 private static final int EVENT_SET_MOBILE_DATA =
201 MAX_NETWORK_STATE_TRACKER_EVENT + 7;
202
Robert Greenwaltf3331232010-09-24 14:32:21 -0700203 /**
204 * used internally to clear a wakelock when transitioning
205 * from one net to another
206 */
207 private static final int EVENT_CLEAR_NET_TRANSITION_WAKELOCK =
208 MAX_NETWORK_STATE_TRACKER_EVENT + 8;
209
Robert Greenwalt434203a2010-10-11 16:00:27 -0700210 /**
211 * used internally to reload global proxy settings
212 */
213 private static final int EVENT_APPLY_GLOBAL_HTTP_PROXY =
214 MAX_NETWORK_STATE_TRACKER_EVENT + 9;
215
Robert Greenwaltd55a6b42011-03-25 13:09:25 -0700216 /**
217 * used internally to set external dependency met/unmet
218 * arg1 = ENABLED (met) or DISABLED (unmet)
219 * arg2 = NetworkType
220 */
221 private static final int EVENT_SET_DEPENDENCY_MET =
222 MAX_NETWORK_STATE_TRACKER_EVENT + 10;
223
Robert Greenwalt42acef32009-08-12 16:08:25 -0700224 private Handler mHandler;
225
226 // list of DeathRecipients used to make sure features are turned off when
227 // a process dies
228 private List mFeatureUsers;
229
Mike Lockwood0f79b542009-08-14 14:18:49 -0400230 private boolean mSystemReady;
Dianne Hackborn1c633fc2009-12-08 19:45:14 -0800231 private Intent mInitialBroadcast;
Mike Lockwood0f79b542009-08-14 14:18:49 -0400232
Robert Greenwalt14f2ef42010-06-15 12:19:37 -0700233 private PowerManager.WakeLock mNetTransitionWakeLock;
234 private String mNetTransitionWakeLockCausedBy = "";
235 private int mNetTransitionWakeLockSerialNumber;
236 private int mNetTransitionWakeLockTimeout;
237
Robert Greenwalte90aa5e2010-09-01 11:34:05 -0700238 private InetAddress mDefaultDns;
239
Robert Greenwalt4e8dfef2010-09-20 14:35:25 -0700240 // used in DBG mode to track inet condition reports
241 private static final int INET_CONDITION_LOG_MAX_SIZE = 15;
242 private ArrayList mInetLog;
243
Robert Greenwalt434203a2010-10-11 16:00:27 -0700244 // track the current default http proxy - tell the world if we get a new one (real change)
245 private ProxyProperties mDefaultProxy = null;
246 // track the global proxy.
247 private ProxyProperties mGlobalProxy = null;
248 private final Object mGlobalProxyLock = new Object();
249
250 private SettingsObserver mSettingsObserver;
251
Robert Greenwaltd55a6b42011-03-25 13:09:25 -0700252 NetworkConfig[] mNetConfigs;
Robert Greenwalt5154ae762009-10-30 14:17:42 -0700253 int mNetworksDefined;
Robert Greenwalt42acef32009-08-12 16:08:25 -0700254
Robert Greenwalt511288a2009-12-07 11:33:18 -0800255 private static class RadioAttributes {
Robert Greenwalt42acef32009-08-12 16:08:25 -0700256 public int mSimultaneity;
257 public int mType;
258 public RadioAttributes(String init) {
259 String fragments[] = init.split(",");
Robert Greenwalt5154ae762009-10-30 14:17:42 -0700260 mType = Integer.parseInt(fragments[0]);
261 mSimultaneity = Integer.parseInt(fragments[1]);
Robert Greenwalt42acef32009-08-12 16:08:25 -0700262 }
263 }
264 RadioAttributes[] mRadioAttributes;
265
Jeff Sharkeyc006f1a2011-05-19 17:12:49 -0700266 public ConnectivityService(
267 Context context, INetworkManagementService netd, INetworkPolicyManager policyManager) {
Wink Savilleed9c02b2010-12-03 12:01:38 -0800268 if (DBG) log("ConnectivityService starting up");
Robert Greenwaltde8383c2010-01-14 17:47:58 -0800269
Wink Savillebb08caf2010-09-02 19:23:52 -0700270 HandlerThread handlerThread = new HandlerThread("ConnectivityServiceThread");
271 handlerThread.start();
272 mHandler = new MyHandler(handlerThread.getLooper());
273
Robert Greenwaltd825ea42010-12-29 16:15:02 -0800274 mBackgroundDataEnabled.set(Settings.Secure.getInt(context.getContentResolver(),
275 Settings.Secure.BACKGROUND_DATA, 1) == 1);
276
Robert Greenwaltde8383c2010-01-14 17:47:58 -0800277 // setup our unique device name
Robert Greenwalt733c6292010-12-06 09:30:17 -0800278 if (TextUtils.isEmpty(SystemProperties.get("net.hostname"))) {
279 String id = Settings.Secure.getString(context.getContentResolver(),
280 Settings.Secure.ANDROID_ID);
281 if (id != null && id.length() > 0) {
282 String name = new String("android_").concat(id);
283 SystemProperties.set("net.hostname", name);
284 }
Robert Greenwaltde8383c2010-01-14 17:47:58 -0800285 }
286
Robert Greenwalte90aa5e2010-09-01 11:34:05 -0700287 // read our default dns server ip
288 String dns = Settings.Secure.getString(context.getContentResolver(),
289 Settings.Secure.DEFAULT_DNS_SERVER);
290 if (dns == null || dns.length() == 0) {
291 dns = context.getResources().getString(
292 com.android.internal.R.string.config_default_dns_server);
293 }
294 try {
Robert Greenwalte5903732011-02-22 16:00:42 -0800295 mDefaultDns = NetworkUtils.numericToInetAddress(dns);
296 } catch (IllegalArgumentException e) {
Wink Savilleed9c02b2010-12-03 12:01:38 -0800297 loge("Error setting defaultDns using " + dns);
Robert Greenwalte90aa5e2010-09-01 11:34:05 -0700298 }
299
Jeff Sharkeyc006f1a2011-05-19 17:12:49 -0700300 mContext = checkNotNull(context, "missing Context");
301 mNetd = checkNotNull(netd, "missing INetworkManagementService");
302 mPolicyManager = checkNotNull(policyManager, "missing INetworkPolicyManager");
Robert Greenwalt14f2ef42010-06-15 12:19:37 -0700303
Jeff Sharkeyc006f1a2011-05-19 17:12:49 -0700304 try {
305 mPolicyManager.registerListener(mPolicyListener);
306 } catch (RemoteException e) {
307 // ouch, no rules updates means some processes may never get network
308 Slog.e(TAG, "unable to register INetworkPolicyListener", e);
309 }
310
311 final PowerManager powerManager = (PowerManager) context.getSystemService(
312 Context.POWER_SERVICE);
Robert Greenwalt14f2ef42010-06-15 12:19:37 -0700313 mNetTransitionWakeLock = powerManager.newWakeLock(PowerManager.PARTIAL_WAKE_LOCK, TAG);
314 mNetTransitionWakeLockTimeout = mContext.getResources().getInteger(
315 com.android.internal.R.integer.config_networkTransitionTimeout);
316
Robert Greenwalt42acef32009-08-12 16:08:25 -0700317 mNetTrackers = new NetworkStateTracker[
318 ConnectivityManager.MAX_NETWORK_TYPE+1];
Robert Greenwalt86e9e552009-07-16 17:21:39 -0700319
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800320 mNetworkPreference = getPersistedNetworkPreference();
Robert Greenwalt86e9e552009-07-16 17:21:39 -0700321
Robert Greenwalt5154ae762009-10-30 14:17:42 -0700322 mRadioAttributes = new RadioAttributes[ConnectivityManager.MAX_RADIO_TYPE+1];
Robert Greenwaltd55a6b42011-03-25 13:09:25 -0700323 mNetConfigs = new NetworkConfig[ConnectivityManager.MAX_NETWORK_TYPE+1];
Robert Greenwalt5154ae762009-10-30 14:17:42 -0700324
Robert Greenwalt42acef32009-08-12 16:08:25 -0700325 // Load device network attributes from resources
Robert Greenwalt42acef32009-08-12 16:08:25 -0700326 String[] raStrings = context.getResources().getStringArray(
327 com.android.internal.R.array.radioAttributes);
Robert Greenwalt5154ae762009-10-30 14:17:42 -0700328 for (String raString : raStrings) {
329 RadioAttributes r = new RadioAttributes(raString);
330 if (r.mType > ConnectivityManager.MAX_RADIO_TYPE) {
Wink Savilleed9c02b2010-12-03 12:01:38 -0800331 loge("Error in radioAttributes - ignoring attempt to define type " + r.mType);
Robert Greenwalt5154ae762009-10-30 14:17:42 -0700332 continue;
333 }
334 if (mRadioAttributes[r.mType] != null) {
Wink Savilleed9c02b2010-12-03 12:01:38 -0800335 loge("Error in radioAttributes - ignoring attempt to redefine type " +
Robert Greenwalt5154ae762009-10-30 14:17:42 -0700336 r.mType);
337 continue;
338 }
Robert Greenwalt42acef32009-08-12 16:08:25 -0700339 mRadioAttributes[r.mType] = r;
340 }
341
Robert Greenwalt5154ae762009-10-30 14:17:42 -0700342 String[] naStrings = context.getResources().getStringArray(
343 com.android.internal.R.array.networkAttributes);
344 for (String naString : naStrings) {
345 try {
Robert Greenwaltd55a6b42011-03-25 13:09:25 -0700346 NetworkConfig n = new NetworkConfig(naString);
Wink Saville975c8482011-04-07 14:23:45 -0700347 if (n.type > ConnectivityManager.MAX_NETWORK_TYPE) {
Wink Savilleed9c02b2010-12-03 12:01:38 -0800348 loge("Error in networkAttributes - ignoring attempt to define type " +
Wink Saville975c8482011-04-07 14:23:45 -0700349 n.type);
Robert Greenwalt5154ae762009-10-30 14:17:42 -0700350 continue;
Robert Greenwalt42acef32009-08-12 16:08:25 -0700351 }
Wink Saville975c8482011-04-07 14:23:45 -0700352 if (mNetConfigs[n.type] != null) {
Wink Savilleed9c02b2010-12-03 12:01:38 -0800353 loge("Error in networkAttributes - ignoring attempt to redefine type " +
Wink Saville975c8482011-04-07 14:23:45 -0700354 n.type);
Robert Greenwalt5154ae762009-10-30 14:17:42 -0700355 continue;
356 }
Wink Saville975c8482011-04-07 14:23:45 -0700357 if (mRadioAttributes[n.radio] == null) {
Wink Savilleed9c02b2010-12-03 12:01:38 -0800358 loge("Error in networkAttributes - ignoring attempt to use undefined " +
Wink Saville975c8482011-04-07 14:23:45 -0700359 "radio " + n.radio + " in network type " + n.type);
Robert Greenwalt5154ae762009-10-30 14:17:42 -0700360 continue;
361 }
Wink Saville975c8482011-04-07 14:23:45 -0700362 mNetConfigs[n.type] = n;
Robert Greenwalt5154ae762009-10-30 14:17:42 -0700363 mNetworksDefined++;
364 } catch(Exception e) {
365 // ignore it - leave the entry null
Robert Greenwalt42acef32009-08-12 16:08:25 -0700366 }
367 }
368
Robert Greenwalt5154ae762009-10-30 14:17:42 -0700369 // high priority first
370 mPriorityList = new int[mNetworksDefined];
371 {
372 int insertionPoint = mNetworksDefined-1;
373 int currentLowest = 0;
374 int nextLowest = 0;
375 while (insertionPoint > -1) {
Robert Greenwaltd55a6b42011-03-25 13:09:25 -0700376 for (NetworkConfig na : mNetConfigs) {
Robert Greenwalt5154ae762009-10-30 14:17:42 -0700377 if (na == null) continue;
Wink Saville975c8482011-04-07 14:23:45 -0700378 if (na.priority < currentLowest) continue;
379 if (na.priority > currentLowest) {
380 if (na.priority < nextLowest || nextLowest == 0) {
381 nextLowest = na.priority;
Robert Greenwalt5154ae762009-10-30 14:17:42 -0700382 }
383 continue;
384 }
Wink Saville975c8482011-04-07 14:23:45 -0700385 mPriorityList[insertionPoint--] = na.type;
Robert Greenwalt5154ae762009-10-30 14:17:42 -0700386 }
387 currentLowest = nextLowest;
388 nextLowest = 0;
389 }
390 }
391
392 mNetRequestersPids = new ArrayList[ConnectivityManager.MAX_NETWORK_TYPE+1];
393 for (int i : mPriorityList) {
Robert Greenwalt42acef32009-08-12 16:08:25 -0700394 mNetRequestersPids[i] = new ArrayList();
395 }
396
397 mFeatureUsers = new ArrayList();
398
Robert Greenwalt5154ae762009-10-30 14:17:42 -0700399 mNumDnsEntries = 0;
400
401 mTestMode = SystemProperties.get("cm.test.mode").equals("true")
402 && SystemProperties.get("ro.build.type").equals("eng");
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800403 /*
404 * Create the network state trackers for Wi-Fi and mobile
405 * data. Maybe this could be done with a factory class,
406 * but it's not clear that it's worth it, given that
407 * the number of different network types is not going
408 * to change very often.
409 */
Robert Greenwalt5154ae762009-10-30 14:17:42 -0700410 for (int netType : mPriorityList) {
Wink Saville975c8482011-04-07 14:23:45 -0700411 switch (mNetConfigs[netType].radio) {
Robert Greenwalt5154ae762009-10-30 14:17:42 -0700412 case ConnectivityManager.TYPE_WIFI:
Wink Savilleed9c02b2010-12-03 12:01:38 -0800413 if (DBG) log("Starting Wifi Service.");
Wink Savillec7a98342010-08-13 16:11:42 -0700414 WifiStateTracker wst = new WifiStateTracker();
Irfan Sheriff0d255342010-07-28 09:35:20 -0700415 WifiService wifiService = new WifiService(context);
Robert Greenwalt5154ae762009-10-30 14:17:42 -0700416 ServiceManager.addService(Context.WIFI_SERVICE, wifiService);
Irfan Sheriff0d255342010-07-28 09:35:20 -0700417 wifiService.checkAndStartWifi();
Robert Greenwalt5154ae762009-10-30 14:17:42 -0700418 mNetTrackers[ConnectivityManager.TYPE_WIFI] = wst;
Wink Savillec7a98342010-08-13 16:11:42 -0700419 wst.startMonitoring(context, mHandler);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800420
Irfan Sheriffa2a1b912010-06-07 09:03:04 -0700421 //TODO: as part of WWS refactor, create only when needed
Irfan Sheriff0d255342010-07-28 09:35:20 -0700422 mWifiWatchdogService = new WifiWatchdogService(context);
Irfan Sheriffa2a1b912010-06-07 09:03:04 -0700423
Robert Greenwalt5154ae762009-10-30 14:17:42 -0700424 break;
425 case ConnectivityManager.TYPE_MOBILE:
Wink Savillec7a98342010-08-13 16:11:42 -0700426 mNetTrackers[netType] = new MobileDataStateTracker(netType,
Wink Saville975c8482011-04-07 14:23:45 -0700427 mNetConfigs[netType].name);
Wink Savillec7a98342010-08-13 16:11:42 -0700428 mNetTrackers[netType].startMonitoring(context, mHandler);
Robert Greenwalt5154ae762009-10-30 14:17:42 -0700429 break;
Robert Greenwaltda3d5e62010-12-06 13:56:24 -0800430 case ConnectivityManager.TYPE_DUMMY:
431 mNetTrackers[netType] = new DummyDataStateTracker(netType,
Wink Saville975c8482011-04-07 14:23:45 -0700432 mNetConfigs[netType].name);
Robert Greenwaltda3d5e62010-12-06 13:56:24 -0800433 mNetTrackers[netType].startMonitoring(context, mHandler);
434 break;
Jaikumar Ganesh15c74392010-12-21 22:31:44 -0800435 case ConnectivityManager.TYPE_BLUETOOTH:
436 mNetTrackers[netType] = BluetoothTetheringDataTracker.getInstance();
437 mNetTrackers[netType].startMonitoring(context, mHandler);
438 break;
Benoit Goby19970692010-12-22 14:29:40 -0800439 case ConnectivityManager.TYPE_ETHERNET:
440 mNetTrackers[netType] = EthernetDataTracker.getInstance();
441 mNetTrackers[netType].startMonitoring(context, mHandler);
442 break;
Robert Greenwalt5154ae762009-10-30 14:17:42 -0700443 default:
Wink Savilleed9c02b2010-12-03 12:01:38 -0800444 loge("Trying to create a DataStateTracker for an unknown radio type " +
Wink Saville975c8482011-04-07 14:23:45 -0700445 mNetConfigs[netType].radio);
Robert Greenwalt5154ae762009-10-30 14:17:42 -0700446 continue;
447 }
448 }
Robert Greenwaltd0e18ff2010-01-26 11:40:34 -0800449
Chia-chi Yehc9338302011-05-11 16:35:13 -0700450 IBinder b = ServiceManager.getService(Context.NETWORKMANAGEMENT_SERVICE);
451 INetworkManagementService nmService = INetworkManagementService.Stub.asInterface(b);
452
453 mTethering = new Tethering(mContext, nmService, mHandler.getLooper());
Robert Greenwaltc9d5fb72010-02-25 12:29:30 -0800454 mTetheringConfigValid = (((mNetTrackers[ConnectivityManager.TYPE_MOBILE_DUN] != null) ||
455 !mTethering.isDunRequired()) &&
456 (mTethering.getTetherableUsbRegexs().length != 0 ||
Danica Chang6fdd0c62010-08-11 14:54:43 -0700457 mTethering.getTetherableWifiRegexs().length != 0 ||
458 mTethering.getTetherableBluetoothRegexs().length != 0) &&
Robert Greenwaltc9d5fb72010-02-25 12:29:30 -0800459 mTethering.getUpstreamIfaceRegexs().length != 0);
460
Chia-chi Yeh008ff392011-05-23 15:08:29 -0700461 try {
462 nmService.registerObserver(mTethering);
463 } catch (RemoteException e) {
464 loge("Error registering observer :" + e);
465 }
466
Robert Greenwalt4e8dfef2010-09-20 14:35:25 -0700467 if (DBG) {
468 mInetLog = new ArrayList();
469 }
Robert Greenwalt434203a2010-10-11 16:00:27 -0700470
471 mSettingsObserver = new SettingsObserver(mHandler, EVENT_APPLY_GLOBAL_HTTP_PROXY);
472 mSettingsObserver.observe(mContext);
Robert Greenwaltb7090d62010-12-02 11:31:00 -0800473
474 loadGlobalProxy();
Hung-ying Tyan6b818de2011-01-19 16:48:38 +0800475
476 VpnManager.startVpnService(context);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800477 }
478
Robert Greenwalt5154ae762009-10-30 14:17:42 -0700479
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800480 /**
Robert Greenwalt86e9e552009-07-16 17:21:39 -0700481 * Sets the preferred network.
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800482 * @param preference the new preference
483 */
Robert Greenwalt8dcc28b2010-09-23 10:05:56 -0700484 public void setNetworkPreference(int preference) {
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800485 enforceChangePermission();
Robert Greenwalt8dcc28b2010-09-23 10:05:56 -0700486
487 mHandler.sendMessage(mHandler.obtainMessage(EVENT_SET_NETWORK_PREFERENCE, preference, 0));
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800488 }
489
490 public int getNetworkPreference() {
491 enforceAccessPermission();
Robert Greenwalt8dcc28b2010-09-23 10:05:56 -0700492 int preference;
493 synchronized(this) {
494 preference = mNetworkPreference;
495 }
496 return preference;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800497 }
498
Robert Greenwalt8dcc28b2010-09-23 10:05:56 -0700499 private void handleSetNetworkPreference(int preference) {
500 if (ConnectivityManager.isNetworkTypeValid(preference) &&
Robert Greenwaltd55a6b42011-03-25 13:09:25 -0700501 mNetConfigs[preference] != null &&
502 mNetConfigs[preference].isDefault()) {
Robert Greenwalt8dcc28b2010-09-23 10:05:56 -0700503 if (mNetworkPreference != preference) {
504 final ContentResolver cr = mContext.getContentResolver();
505 Settings.Secure.putInt(cr, Settings.Secure.NETWORK_PREFERENCE, preference);
506 synchronized(this) {
507 mNetworkPreference = preference;
508 }
509 enforcePreference();
510 }
511 }
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800512 }
Robert Greenwalt86e9e552009-07-16 17:21:39 -0700513
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800514 private int getPersistedNetworkPreference() {
515 final ContentResolver cr = mContext.getContentResolver();
516
517 final int networkPrefSetting = Settings.Secure
518 .getInt(cr, Settings.Secure.NETWORK_PREFERENCE, -1);
519 if (networkPrefSetting != -1) {
520 return networkPrefSetting;
521 }
522
523 return ConnectivityManager.DEFAULT_NETWORK_PREFERENCE;
524 }
Robert Greenwalt86e9e552009-07-16 17:21:39 -0700525
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800526 /**
Robert Greenwalt86e9e552009-07-16 17:21:39 -0700527 * Make the state of network connectivity conform to the preference settings
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800528 * In this method, we only tear down a non-preferred network. Establishing
529 * a connection to the preferred network is taken care of when we handle
530 * the disconnect event from the non-preferred network
531 * (see {@link #handleDisconnect(NetworkInfo)}).
532 */
533 private void enforcePreference() {
Robert Greenwalt42acef32009-08-12 16:08:25 -0700534 if (mNetTrackers[mNetworkPreference].getNetworkInfo().isConnected())
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800535 return;
536
Robert Greenwalt42acef32009-08-12 16:08:25 -0700537 if (!mNetTrackers[mNetworkPreference].isAvailable())
538 return;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800539
Robert Greenwalt42acef32009-08-12 16:08:25 -0700540 for (int t=0; t <= ConnectivityManager.MAX_RADIO_TYPE; t++) {
Robert Greenwalt5154ae762009-10-30 14:17:42 -0700541 if (t != mNetworkPreference && mNetTrackers[t] != null &&
Robert Greenwalt42acef32009-08-12 16:08:25 -0700542 mNetTrackers[t].getNetworkInfo().isConnected()) {
Robert Greenwaltec9fe462009-08-20 15:25:14 -0700543 if (DBG) {
Wink Savilleed9c02b2010-12-03 12:01:38 -0800544 log("tearing down " + mNetTrackers[t].getNetworkInfo() +
Robert Greenwaltec9fe462009-08-20 15:25:14 -0700545 " in enforcePreference");
546 }
Robert Greenwalt42acef32009-08-12 16:08:25 -0700547 teardown(mNetTrackers[t]);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800548 }
549 }
550 }
551
552 private boolean teardown(NetworkStateTracker netTracker) {
553 if (netTracker.teardown()) {
554 netTracker.setTeardownRequested(true);
555 return true;
556 } else {
557 return false;
558 }
559 }
560
561 /**
Jeff Sharkeyc006f1a2011-05-19 17:12:49 -0700562 * Check if UID is blocked from using the given {@link NetworkInfo}.
563 */
564 private boolean isNetworkBlocked(NetworkInfo info, int uid) {
565 synchronized (mUidRules) {
566 return isNetworkBlockedLocked(info, uid);
567 }
568 }
569
570 /**
571 * Check if UID is blocked from using the given {@link NetworkInfo}.
572 */
573 private boolean isNetworkBlockedLocked(NetworkInfo info, int uid) {
574 // TODO: expand definition of "paid" network to cover tethered or paid
575 // hotspot use cases.
576 final boolean networkIsPaid = info.getType() != ConnectivityManager.TYPE_WIFI;
577 final int uidRules = mUidRules.get(uid, RULE_ALLOW_ALL);
578
579 if (networkIsPaid && (uidRules & RULE_REJECT_PAID) != 0) {
580 return true;
581 }
582
583 // no restrictive rules; network is visible
584 return false;
585 }
586
587 /**
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800588 * Return NetworkInfo for the active (i.e., connected) network interface.
589 * It is assumed that at most one network is active at a time. If more
590 * than one is active, it is indeterminate which will be returned.
Robert Greenwalt86e9e552009-07-16 17:21:39 -0700591 * @return the info for the active network, or {@code null} if none is
592 * active
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800593 */
Jeff Sharkeyc006f1a2011-05-19 17:12:49 -0700594 @Override
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800595 public NetworkInfo getActiveNetworkInfo() {
Jeff Sharkeyc006f1a2011-05-19 17:12:49 -0700596 enforceAccessPermission();
597 final int uid = Binder.getCallingUid();
598 return getNetworkInfo(mActiveDefaultNetwork, uid);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800599 }
600
Jeff Sharkeyc006f1a2011-05-19 17:12:49 -0700601 @Override
602 public NetworkInfo getActiveNetworkInfoForUid(int uid) {
603 enforceConnectivityInternalPermission();
604 return getNetworkInfo(mActiveDefaultNetwork, uid);
605 }
606
607 @Override
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800608 public NetworkInfo getNetworkInfo(int networkType) {
609 enforceAccessPermission();
Jeff Sharkeyc006f1a2011-05-19 17:12:49 -0700610 final int uid = Binder.getCallingUid();
611 return getNetworkInfo(networkType, uid);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800612 }
613
Jeff Sharkeyc006f1a2011-05-19 17:12:49 -0700614 private NetworkInfo getNetworkInfo(int networkType, int uid) {
615 NetworkInfo info = null;
616 if (isNetworkTypeValid(networkType)) {
617 final NetworkStateTracker tracker = mNetTrackers[networkType];
618 if (tracker != null) {
619 info = tracker.getNetworkInfo();
620 if (isNetworkBlocked(info, uid)) {
621 // network is blocked; clone and override state
622 info = new NetworkInfo(info);
623 info.setDetailedState(DetailedState.BLOCKED, null, null);
624 }
625 }
626 }
627 return info;
628 }
629
630 @Override
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800631 public NetworkInfo[] getAllNetworkInfo() {
632 enforceAccessPermission();
Jeff Sharkeyc006f1a2011-05-19 17:12:49 -0700633 final int uid = Binder.getCallingUid();
634 final NetworkInfo[] result = new NetworkInfo[mNetworksDefined];
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800635 int i = 0;
Jeff Sharkeyc006f1a2011-05-19 17:12:49 -0700636 synchronized (mUidRules) {
637 for (NetworkStateTracker tracker : mNetTrackers) {
638 if (tracker != null) {
639 NetworkInfo info = tracker.getNetworkInfo();
640 if (isNetworkBlockedLocked(info, uid)) {
641 // network is blocked; clone and override state
642 info = new NetworkInfo(info);
643 info.setDetailedState(DetailedState.BLOCKED, null, null);
644 }
645 result[i++] = info;
646 }
647 }
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800648 }
649 return result;
650 }
651
Robert Greenwaltd192dad2010-09-14 09:18:02 -0700652 /**
653 * Return LinkProperties for the active (i.e., connected) default
654 * network interface. It is assumed that at most one default network
655 * is active at a time. If more than one is active, it is indeterminate
656 * which will be returned.
657 * @return the ip properties for the active network, or {@code null} if
658 * none is active
659 */
Jeff Sharkeyc006f1a2011-05-19 17:12:49 -0700660 @Override
Robert Greenwaltd192dad2010-09-14 09:18:02 -0700661 public LinkProperties getActiveLinkProperties() {
Robert Greenwalt59911582011-05-20 12:23:41 -0700662 return getLinkProperties(mActiveDefaultNetwork);
Robert Greenwaltd192dad2010-09-14 09:18:02 -0700663 }
664
Jeff Sharkeyc006f1a2011-05-19 17:12:49 -0700665 @Override
Robert Greenwaltd192dad2010-09-14 09:18:02 -0700666 public LinkProperties getLinkProperties(int networkType) {
667 enforceAccessPermission();
Jeff Sharkeyc006f1a2011-05-19 17:12:49 -0700668 if (isNetworkTypeValid(networkType)) {
669 final NetworkStateTracker tracker = mNetTrackers[networkType];
670 if (tracker != null) {
671 return tracker.getLinkProperties();
672 }
Robert Greenwaltd192dad2010-09-14 09:18:02 -0700673 }
674 return null;
675 }
676
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800677 public boolean setRadios(boolean turnOn) {
678 boolean result = true;
679 enforceChangePermission();
680 for (NetworkStateTracker t : mNetTrackers) {
Robert Greenwalt5154ae762009-10-30 14:17:42 -0700681 if (t != null) result = t.setRadio(turnOn) && result;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800682 }
683 return result;
684 }
685
686 public boolean setRadio(int netType, boolean turnOn) {
687 enforceChangePermission();
688 if (!ConnectivityManager.isNetworkTypeValid(netType)) {
689 return false;
690 }
691 NetworkStateTracker tracker = mNetTrackers[netType];
692 return tracker != null && tracker.setRadio(turnOn);
693 }
694
Robert Greenwalt9c75d4a2009-09-27 17:27:04 -0700695 /**
696 * Used to notice when the calling process dies so we can self-expire
697 *
698 * Also used to know if the process has cleaned up after itself when
699 * our auto-expire timer goes off. The timer has a link to an object.
700 *
701 */
Robert Greenwalt42acef32009-08-12 16:08:25 -0700702 private class FeatureUser implements IBinder.DeathRecipient {
703 int mNetworkType;
704 String mFeature;
705 IBinder mBinder;
706 int mPid;
707 int mUid;
Robert Greenwaltb9285352009-12-21 18:24:07 -0800708 long mCreateTime;
Robert Greenwalt42acef32009-08-12 16:08:25 -0700709
710 FeatureUser(int type, String feature, IBinder binder) {
711 super();
712 mNetworkType = type;
713 mFeature = feature;
714 mBinder = binder;
715 mPid = getCallingPid();
716 mUid = getCallingUid();
Robert Greenwaltb9285352009-12-21 18:24:07 -0800717 mCreateTime = System.currentTimeMillis();
Robert Greenwalt9c75d4a2009-09-27 17:27:04 -0700718
Robert Greenwalt42acef32009-08-12 16:08:25 -0700719 try {
720 mBinder.linkToDeath(this, 0);
721 } catch (RemoteException e) {
722 binderDied();
723 }
724 }
725
726 void unlinkDeathRecipient() {
727 mBinder.unlinkToDeath(this, 0);
728 }
729
730 public void binderDied() {
Wink Savilleed9c02b2010-12-03 12:01:38 -0800731 log("ConnectivityService FeatureUser binderDied(" +
Robert Greenwaltb9285352009-12-21 18:24:07 -0800732 mNetworkType + ", " + mFeature + ", " + mBinder + "), created " +
733 (System.currentTimeMillis() - mCreateTime) + " mSec ago");
Robert Greenwalt9c75d4a2009-09-27 17:27:04 -0700734 stopUsingNetworkFeature(this, false);
Robert Greenwalt42acef32009-08-12 16:08:25 -0700735 }
736
Robert Greenwalt9c75d4a2009-09-27 17:27:04 -0700737 public void expire() {
Wink Savilleed9c02b2010-12-03 12:01:38 -0800738 log("ConnectivityService FeatureUser expire(" +
Robert Greenwaltb9285352009-12-21 18:24:07 -0800739 mNetworkType + ", " + mFeature + ", " + mBinder +"), created " +
740 (System.currentTimeMillis() - mCreateTime) + " mSec ago");
Robert Greenwalt9c75d4a2009-09-27 17:27:04 -0700741 stopUsingNetworkFeature(this, false);
742 }
Robert Greenwaltb9285352009-12-21 18:24:07 -0800743
744 public String toString() {
745 return "FeatureUser("+mNetworkType+","+mFeature+","+mPid+","+mUid+"), created " +
746 (System.currentTimeMillis() - mCreateTime) + " mSec ago";
747 }
Robert Greenwalt42acef32009-08-12 16:08:25 -0700748 }
749
Robert Greenwalt9c75d4a2009-09-27 17:27:04 -0700750 // javadoc from interface
Robert Greenwalt42acef32009-08-12 16:08:25 -0700751 public int startUsingNetworkFeature(int networkType, String feature,
752 IBinder binder) {
753 if (DBG) {
Wink Savilleed9c02b2010-12-03 12:01:38 -0800754 log("startUsingNetworkFeature for net " + networkType + ": " + feature);
Robert Greenwalt42acef32009-08-12 16:08:25 -0700755 }
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800756 enforceChangePermission();
Robert Greenwalt5154ae762009-10-30 14:17:42 -0700757 if (!ConnectivityManager.isNetworkTypeValid(networkType) ||
Robert Greenwaltd55a6b42011-03-25 13:09:25 -0700758 mNetConfigs[networkType] == null) {
Robert Greenwalt42acef32009-08-12 16:08:25 -0700759 return Phone.APN_REQUEST_FAILED;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800760 }
Robert Greenwalt42acef32009-08-12 16:08:25 -0700761
Robert Greenwalt9c75d4a2009-09-27 17:27:04 -0700762 FeatureUser f = new FeatureUser(networkType, feature, binder);
Robert Greenwalt42acef32009-08-12 16:08:25 -0700763
764 // TODO - move this into the MobileDataStateTracker
765 int usedNetworkType = networkType;
766 if(networkType == ConnectivityManager.TYPE_MOBILE) {
Wink Saville2b8bcfe2011-02-24 17:58:51 -0800767 usedNetworkType = convertFeatureToNetworkType(feature);
768 if (usedNetworkType < 0) {
769 Slog.e(TAG, "Can't match any netTracker!");
770 usedNetworkType = networkType;
Robert Greenwalt42acef32009-08-12 16:08:25 -0700771 }
772 }
773 NetworkStateTracker network = mNetTrackers[usedNetworkType];
774 if (network != null) {
Robert Greenwalt0be1e982010-12-15 13:26:33 -0800775 Integer currentPid = new Integer(getCallingPid());
Robert Greenwalt42acef32009-08-12 16:08:25 -0700776 if (usedNetworkType != networkType) {
Robert Greenwalt42acef32009-08-12 16:08:25 -0700777 NetworkStateTracker radio = mNetTrackers[networkType];
778 NetworkInfo ni = network.getNetworkInfo();
779
780 if (ni.isAvailable() == false) {
Wink Savilleed9c02b2010-12-03 12:01:38 -0800781 if (DBG) log("special network not available");
Robert Greenwalte32e8122010-12-29 14:35:21 -0800782 if (!TextUtils.equals(feature,Phone.FEATURE_ENABLE_DUN_ALWAYS)) {
783 return Phone.APN_TYPE_NOT_AVAILABLE;
784 } else {
785 // else make the attempt anyway - probably giving REQUEST_STARTED below
786 }
Robert Greenwalt42acef32009-08-12 16:08:25 -0700787 }
788
Robert Greenwalt9c75d4a2009-09-27 17:27:04 -0700789 synchronized(this) {
790 mFeatureUsers.add(f);
791 if (!mNetRequestersPids[usedNetworkType].contains(currentPid)) {
792 // this gets used for per-pid dns when connected
793 mNetRequestersPids[usedNetworkType].add(currentPid);
794 }
Robert Greenwalt42acef32009-08-12 16:08:25 -0700795 }
Robert Greenwalt9c75d4a2009-09-27 17:27:04 -0700796
Robert Greenwaltf2102f72011-05-03 19:02:44 -0700797 int restoreTimer = getRestoreDefaultNetworkDelay(usedNetworkType);
798
799 if (restoreTimer >= 0) {
800 mHandler.sendMessageDelayed(
801 mHandler.obtainMessage(EVENT_RESTORE_DEFAULT_NETWORK, f), restoreTimer);
802 }
Robert Greenwalt42acef32009-08-12 16:08:25 -0700803
Robert Greenwalta64bf832009-08-19 20:19:33 -0700804 if ((ni.isConnectedOrConnecting() == true) &&
805 !network.isTeardownRequested()) {
Robert Greenwalt42acef32009-08-12 16:08:25 -0700806 if (ni.isConnected() == true) {
807 // add the pid-specific dns
Robert Greenwaltc76b8fa2010-07-23 15:46:26 -0700808 handleDnsConfigurationChange(networkType);
Wink Savilleed9c02b2010-12-03 12:01:38 -0800809 if (DBG) log("special network already active");
Robert Greenwalt42acef32009-08-12 16:08:25 -0700810 return Phone.APN_ALREADY_ACTIVE;
811 }
Wink Savilleed9c02b2010-12-03 12:01:38 -0800812 if (DBG) log("special network already connecting");
Robert Greenwalt42acef32009-08-12 16:08:25 -0700813 return Phone.APN_REQUEST_STARTED;
814 }
815
816 // check if the radio in play can make another contact
817 // assume if cannot for now
818
Wink Savilleed9c02b2010-12-03 12:01:38 -0800819 if (DBG) log("reconnecting to special network");
Robert Greenwalt42acef32009-08-12 16:08:25 -0700820 network.reconnect();
821 return Phone.APN_REQUEST_STARTED;
822 } else {
Robert Greenwalt0be1e982010-12-15 13:26:33 -0800823 // need to remember this unsupported request so we respond appropriately on stop
824 synchronized(this) {
825 mFeatureUsers.add(f);
826 if (!mNetRequestersPids[usedNetworkType].contains(currentPid)) {
827 // this gets used for per-pid dns when connected
828 mNetRequestersPids[usedNetworkType].add(currentPid);
829 }
830 }
Robert Greenwalt02648a42010-05-18 10:52:51 -0700831 return -1;
Robert Greenwalt42acef32009-08-12 16:08:25 -0700832 }
833 }
834 return Phone.APN_TYPE_NOT_AVAILABLE;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800835 }
836
Robert Greenwalt9c75d4a2009-09-27 17:27:04 -0700837 // javadoc from interface
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800838 public int stopUsingNetworkFeature(int networkType, String feature) {
Robert Greenwaltb8f16342009-10-06 17:52:40 -0700839 enforceChangePermission();
840
Robert Greenwalt9c75d4a2009-09-27 17:27:04 -0700841 int pid = getCallingPid();
842 int uid = getCallingUid();
843
844 FeatureUser u = null;
845 boolean found = false;
846
847 synchronized(this) {
848 for (int i = 0; i < mFeatureUsers.size() ; i++) {
849 u = (FeatureUser)mFeatureUsers.get(i);
850 if (uid == u.mUid && pid == u.mPid &&
851 networkType == u.mNetworkType &&
852 TextUtils.equals(feature, u.mFeature)) {
853 found = true;
854 break;
855 }
856 }
857 }
858 if (found && u != null) {
859 // stop regardless of how many other time this proc had called start
860 return stopUsingNetworkFeature(u, true);
861 } else {
862 // none found!
Wink Savilleed9c02b2010-12-03 12:01:38 -0800863 if (DBG) log("ignoring stopUsingNetworkFeature - not a live request");
Robert Greenwalt9c75d4a2009-09-27 17:27:04 -0700864 return 1;
865 }
Robert Greenwalt42acef32009-08-12 16:08:25 -0700866 }
867
Robert Greenwalt9c75d4a2009-09-27 17:27:04 -0700868 private int stopUsingNetworkFeature(FeatureUser u, boolean ignoreDups) {
869 int networkType = u.mNetworkType;
870 String feature = u.mFeature;
871 int pid = u.mPid;
872 int uid = u.mUid;
873
874 NetworkStateTracker tracker = null;
875 boolean callTeardown = false; // used to carry our decision outside of sync block
876
Robert Greenwalt42acef32009-08-12 16:08:25 -0700877 if (DBG) {
Wink Savilleed9c02b2010-12-03 12:01:38 -0800878 log("stopUsingNetworkFeature for net " + networkType +
Robert Greenwalt42acef32009-08-12 16:08:25 -0700879 ": " + feature);
880 }
Robert Greenwaltb8f16342009-10-06 17:52:40 -0700881
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800882 if (!ConnectivityManager.isNetworkTypeValid(networkType)) {
883 return -1;
884 }
Robert Greenwalt42acef32009-08-12 16:08:25 -0700885
Robert Greenwalt9c75d4a2009-09-27 17:27:04 -0700886 // need to link the mFeatureUsers list with the mNetRequestersPids state in this
887 // sync block
888 synchronized(this) {
889 // check if this process still has an outstanding start request
890 if (!mFeatureUsers.contains(u)) {
Wink Savilleed9c02b2010-12-03 12:01:38 -0800891 if (DBG) log("ignoring - this process has no outstanding requests");
Robert Greenwalt42acef32009-08-12 16:08:25 -0700892 return 1;
893 }
Robert Greenwalt9c75d4a2009-09-27 17:27:04 -0700894 u.unlinkDeathRecipient();
895 mFeatureUsers.remove(mFeatureUsers.indexOf(u));
896 // If we care about duplicate requests, check for that here.
897 //
898 // This is done to support the extension of a request - the app
899 // can request we start the network feature again and renew the
900 // auto-shutoff delay. Normal "stop" calls from the app though
901 // do not pay attention to duplicate requests - in effect the
902 // API does not refcount and a single stop will counter multiple starts.
903 if (ignoreDups == false) {
904 for (int i = 0; i < mFeatureUsers.size() ; i++) {
905 FeatureUser x = (FeatureUser)mFeatureUsers.get(i);
906 if (x.mUid == u.mUid && x.mPid == u.mPid &&
907 x.mNetworkType == u.mNetworkType &&
908 TextUtils.equals(x.mFeature, u.mFeature)) {
Wink Savilleed9c02b2010-12-03 12:01:38 -0800909 if (DBG) log("ignoring stopUsingNetworkFeature as dup is found");
Robert Greenwalt9c75d4a2009-09-27 17:27:04 -0700910 return 1;
911 }
912 }
Robert Greenwalt42acef32009-08-12 16:08:25 -0700913 }
Robert Greenwalt9c75d4a2009-09-27 17:27:04 -0700914
915 // TODO - move to MobileDataStateTracker
916 int usedNetworkType = networkType;
917 if (networkType == ConnectivityManager.TYPE_MOBILE) {
Wink Saville2b8bcfe2011-02-24 17:58:51 -0800918 usedNetworkType = convertFeatureToNetworkType(feature);
919 if (usedNetworkType < 0) {
920 usedNetworkType = networkType;
Robert Greenwalt9c75d4a2009-09-27 17:27:04 -0700921 }
922 }
923 tracker = mNetTrackers[usedNetworkType];
Robert Greenwalt5154ae762009-10-30 14:17:42 -0700924 if (tracker == null) {
Wink Savilleed9c02b2010-12-03 12:01:38 -0800925 if (DBG) log("ignoring - no known tracker for net type " + usedNetworkType);
Robert Greenwalt5154ae762009-10-30 14:17:42 -0700926 return -1;
927 }
928 if (usedNetworkType != networkType) {
Robert Greenwalt9c75d4a2009-09-27 17:27:04 -0700929 Integer currentPid = new Integer(pid);
Robert Greenwalt9c75d4a2009-09-27 17:27:04 -0700930 mNetRequestersPids[usedNetworkType].remove(currentPid);
Robert Greenwalt421c72b2009-12-17 14:54:59 -0800931 reassessPidDns(pid, true);
Robert Greenwalt9c75d4a2009-09-27 17:27:04 -0700932 if (mNetRequestersPids[usedNetworkType].size() != 0) {
Wink Savilleed9c02b2010-12-03 12:01:38 -0800933 if (DBG) log("not tearing down special network - " +
Robert Greenwalt9c75d4a2009-09-27 17:27:04 -0700934 "others still using it");
935 return 1;
936 }
937 callTeardown = true;
Robert Greenwalt19b9ab42011-01-10 11:58:31 -0800938 } else {
939 if (DBG) log("not a known feature - dropping");
Robert Greenwalt9c75d4a2009-09-27 17:27:04 -0700940 }
941 }
Wink Savilleed9c02b2010-12-03 12:01:38 -0800942 if (DBG) log("Doing network teardown");
Robert Greenwalt9c75d4a2009-09-27 17:27:04 -0700943 if (callTeardown) {
944 tracker.teardown();
Robert Greenwalt42acef32009-08-12 16:08:25 -0700945 return 1;
946 } else {
Robert Greenwalt02648a42010-05-18 10:52:51 -0700947 return -1;
Robert Greenwalt42acef32009-08-12 16:08:25 -0700948 }
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800949 }
950
951 /**
Robert Greenwalt585ac0f2010-08-27 09:24:29 -0700952 * @deprecated use requestRouteToHostAddress instead
953 *
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800954 * Ensure that a network route exists to deliver traffic to the specified
955 * host via the specified network interface.
Robert Greenwalt86e9e552009-07-16 17:21:39 -0700956 * @param networkType the type of the network over which traffic to the
957 * specified host is to be routed
958 * @param hostAddress the IP address of the host to which the route is
959 * desired
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800960 * @return {@code true} on success, {@code false} on failure
961 */
962 public boolean requestRouteToHost(int networkType, int hostAddress) {
Robert Greenwalt585ac0f2010-08-27 09:24:29 -0700963 InetAddress inetAddress = NetworkUtils.intToInetAddress(hostAddress);
964
965 if (inetAddress == null) {
966 return false;
967 }
968
969 return requestRouteToHostAddress(networkType, inetAddress.getAddress());
970 }
971
972 /**
973 * Ensure that a network route exists to deliver traffic to the specified
974 * host via the specified network interface.
975 * @param networkType the type of the network over which traffic to the
976 * specified host is to be routed
977 * @param hostAddress the IP address of the host to which the route is
978 * desired
979 * @return {@code true} on success, {@code false} on failure
980 */
981 public boolean requestRouteToHostAddress(int networkType, byte[] hostAddress) {
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800982 enforceChangePermission();
983 if (!ConnectivityManager.isNetworkTypeValid(networkType)) {
984 return false;
985 }
986 NetworkStateTracker tracker = mNetTrackers[networkType];
Robert Greenwalt8206ff32009-09-10 15:06:20 -0700987
Robert Greenwalt5154ae762009-10-30 14:17:42 -0700988 if (tracker == null || !tracker.getNetworkInfo().isConnected() ||
989 tracker.isTeardownRequested()) {
Robert Greenwalt8206ff32009-09-10 15:06:20 -0700990 if (DBG) {
Wink Savilleed9c02b2010-12-03 12:01:38 -0800991 log("requestRouteToHostAddress on down network " +
Robert Greenwalt585ac0f2010-08-27 09:24:29 -0700992 "(" + networkType + ") - dropped");
Robert Greenwalt8206ff32009-09-10 15:06:20 -0700993 }
994 return false;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800995 }
Robert Greenwalt47f69fe2010-06-15 15:43:39 -0700996 try {
Robert Greenwalt585ac0f2010-08-27 09:24:29 -0700997 InetAddress addr = InetAddress.getByAddress(hostAddress);
Robert Greenwaltf43396c2011-05-06 17:10:53 -0700998 return addHostRoute(tracker, addr, 0);
Robert Greenwalt47f69fe2010-06-15 15:43:39 -0700999 } catch (UnknownHostException e) {}
1000 return false;
Irfan Sheriffd649c122010-06-09 15:39:36 -07001001 }
1002
1003 /**
1004 * Ensure that a network route exists to deliver traffic to the specified
1005 * host via the mobile data network.
1006 * @param hostAddress the IP address of the host to which the route is desired,
1007 * in network byte order.
Robert Greenwalt47f69fe2010-06-15 15:43:39 -07001008 * TODO - deprecate
Irfan Sheriffd649c122010-06-09 15:39:36 -07001009 * @return {@code true} on success, {@code false} on failure
1010 */
Robert Greenwaltf43396c2011-05-06 17:10:53 -07001011 private boolean addHostRoute(NetworkStateTracker nt, InetAddress hostAddress, int cycleCount) {
Robert Greenwaltf43396c2011-05-06 17:10:53 -07001012 LinkProperties lp = nt.getLinkProperties();
1013 if ((lp == null) || (hostAddress == null)) return false;
Irfan Sheriffd649c122010-06-09 15:39:36 -07001014
Robert Greenwaltf43396c2011-05-06 17:10:53 -07001015 String interfaceName = lp.getInterfaceName();
Irfan Sheriffd649c122010-06-09 15:39:36 -07001016 if (DBG) {
Robert Greenwaltf43396c2011-05-06 17:10:53 -07001017 log("Requested host route to " + hostAddress + "(" + interfaceName + "), cycleCount=" +
1018 cycleCount);
Irfan Sheriffd649c122010-06-09 15:39:36 -07001019 }
Robert Greenwaltf43396c2011-05-06 17:10:53 -07001020 if (interfaceName == null) {
Wink Savilleed9c02b2010-12-03 12:01:38 -08001021 if (DBG) loge("addHostRoute failed due to null interface name");
Irfan Sheriffd649c122010-06-09 15:39:36 -07001022 return false;
1023 }
Robert Greenwaltf43396c2011-05-06 17:10:53 -07001024
1025 RouteInfo bestRoute = RouteInfo.selectBestRoute(lp.getRoutes(), hostAddress);
Robert Greenwalt59b1a4e2011-05-10 15:05:02 -07001026 InetAddress gatewayAddress = null;
Robert Greenwaltf43396c2011-05-06 17:10:53 -07001027 if (bestRoute != null) {
Robert Greenwalt59b1a4e2011-05-10 15:05:02 -07001028 gatewayAddress = bestRoute.getGateway();
Robert Greenwaltf43396c2011-05-06 17:10:53 -07001029 // if the best route is ourself, don't relf-reference, just add the host route
Robert Greenwalt59b1a4e2011-05-10 15:05:02 -07001030 if (hostAddress.equals(gatewayAddress)) gatewayAddress = null;
Robert Greenwaltf43396c2011-05-06 17:10:53 -07001031 }
Robert Greenwalt59b1a4e2011-05-10 15:05:02 -07001032 if (gatewayAddress != null) {
Robert Greenwaltf43396c2011-05-06 17:10:53 -07001033 if (cycleCount > MAX_HOSTROUTE_CYCLE_COUNT) {
1034 loge("Error adding hostroute - too much recursion");
1035 return false;
1036 }
Robert Greenwalt59b1a4e2011-05-10 15:05:02 -07001037 if (!addHostRoute(nt, gatewayAddress, cycleCount+1)) return false;
Robert Greenwaltf43396c2011-05-06 17:10:53 -07001038 }
Robert Greenwalt59b1a4e2011-05-10 15:05:02 -07001039
1040 RouteInfo route = RouteInfo.makeHostRoute(hostAddress, gatewayAddress);
1041
1042 try {
1043 mNetd.addRoute(interfaceName, route);
1044 return true;
1045 } catch (Exception ex) {
1046 return false;
1047 }
Robert Greenwaltf43396c2011-05-06 17:10:53 -07001048 }
1049
1050 // TODO support the removal of single host routes. Keep a ref count of them so we
1051 // aren't over-zealous
1052 private boolean removeHostRoute(NetworkStateTracker nt, InetAddress hostAddress) {
1053 return false;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001054 }
1055
1056 /**
1057 * @see ConnectivityManager#getBackgroundDataSetting()
1058 */
1059 public boolean getBackgroundDataSetting() {
Robert Greenwaltd825ea42010-12-29 16:15:02 -08001060 return mBackgroundDataEnabled.get();
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001061 }
Robert Greenwalt86e9e552009-07-16 17:21:39 -07001062
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001063 /**
1064 * @see ConnectivityManager#setBackgroundDataSetting(boolean)
1065 */
1066 public void setBackgroundDataSetting(boolean allowBackgroundDataUsage) {
1067 mContext.enforceCallingOrSelfPermission(
1068 android.Manifest.permission.CHANGE_BACKGROUND_DATA_SETTING,
1069 "ConnectivityService");
Robert Greenwalt86e9e552009-07-16 17:21:39 -07001070
Robert Greenwaltd825ea42010-12-29 16:15:02 -08001071 mBackgroundDataEnabled.set(allowBackgroundDataUsage);
1072
Robert Greenwalt8dcc28b2010-09-23 10:05:56 -07001073 mHandler.sendMessage(mHandler.obtainMessage(EVENT_SET_BACKGROUND_DATA,
1074 (allowBackgroundDataUsage ? ENABLED : DISABLED), 0));
1075 }
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001076
Robert Greenwalt8dcc28b2010-09-23 10:05:56 -07001077 private void handleSetBackgroundData(boolean enabled) {
Robert Greenwaltdb4afae2011-02-25 13:44:09 -08001078 Settings.Secure.putInt(mContext.getContentResolver(),
1079 Settings.Secure.BACKGROUND_DATA, enabled ? 1 : 0);
1080 Intent broadcast = new Intent(
1081 ConnectivityManager.ACTION_BACKGROUND_DATA_SETTING_CHANGED);
1082 mContext.sendBroadcast(broadcast);
Robert Greenwalt86e9e552009-07-16 17:21:39 -07001083 }
1084
Robert Greenwaltc03fa502010-02-23 18:58:05 -08001085 /**
1086 * @see ConnectivityManager#getMobileDataEnabled()
1087 */
1088 public boolean getMobileDataEnabled() {
Wink Savillee7982682010-12-07 10:31:02 -08001089 // TODO: This detail should probably be in DataConnectionTracker's
1090 // which is where we store the value and maybe make this
1091 // asynchronous.
Robert Greenwaltc03fa502010-02-23 18:58:05 -08001092 enforceAccessPermission();
1093 boolean retVal = Settings.Secure.getInt(mContext.getContentResolver(),
1094 Settings.Secure.MOBILE_DATA, 1) == 1;
Wink Savilleed9c02b2010-12-03 12:01:38 -08001095 if (DBG) log("getMobileDataEnabled returning " + retVal);
Robert Greenwaltc03fa502010-02-23 18:58:05 -08001096 return retVal;
1097 }
1098
Robert Greenwaltd55a6b42011-03-25 13:09:25 -07001099 public void setDataDependency(int networkType, boolean met) {
1100 enforceChangePermission();
1101 if (DBG) {
1102 log("setDataDependency(" + networkType + ", " + met + ")");
1103 }
1104 mHandler.sendMessage(mHandler.obtainMessage(EVENT_SET_DEPENDENCY_MET,
1105 (met ? ENABLED : DISABLED), networkType));
1106 }
1107
1108 private void handleSetDependencyMet(int networkType, boolean met) {
1109 if (mNetTrackers[networkType] != null) {
1110 if (DBG) {
1111 log("handleSetDependencyMet(" + networkType + ", " + met + ")");
1112 }
1113 mNetTrackers[networkType].setDependencyMet(met);
1114 }
1115 }
1116
Jeff Sharkeyc006f1a2011-05-19 17:12:49 -07001117 private INetworkPolicyListener mPolicyListener = new INetworkPolicyListener.Stub() {
1118 @Override
1119 public void onRulesChanged(int uid, int uidRules) {
1120 // only someone like NPMS should only be calling us
1121 // TODO: create permission for modifying data policy
1122 mContext.enforceCallingOrSelfPermission(UPDATE_DEVICE_STATS, TAG);
1123
1124 if (LOGD_RULES) {
1125 Slog.d(TAG, "onRulesChanged(uid=" + uid + ", uidRules=" + uidRules + ")");
1126 }
1127
1128 synchronized (mUidRules) {
1129 // skip update when we've already applied rules
1130 final int oldRules = mUidRules.get(uid, RULE_ALLOW_ALL);
1131 if (oldRules == uidRules) return;
1132
1133 mUidRules.put(uid, uidRules);
1134 }
1135
1136 // TODO: dispatch into NMS to push rules towards kernel module
1137 // TODO: notify UID when it has requested targeted updates
1138 }
1139 };
1140
Robert Greenwaltc03fa502010-02-23 18:58:05 -08001141 /**
1142 * @see ConnectivityManager#setMobileDataEnabled(boolean)
1143 */
Robert Greenwalt8dcc28b2010-09-23 10:05:56 -07001144 public void setMobileDataEnabled(boolean enabled) {
Robert Greenwaltc03fa502010-02-23 18:58:05 -08001145 enforceChangePermission();
Wink Savilleed9c02b2010-12-03 12:01:38 -08001146 if (DBG) log("setMobileDataEnabled(" + enabled + ")");
Robert Greenwaltc03fa502010-02-23 18:58:05 -08001147
Robert Greenwalt8dcc28b2010-09-23 10:05:56 -07001148 mHandler.sendMessage(mHandler.obtainMessage(EVENT_SET_MOBILE_DATA,
Robert Greenwaltd55a6b42011-03-25 13:09:25 -07001149 (enabled ? ENABLED : DISABLED), 0));
Robert Greenwalt8dcc28b2010-09-23 10:05:56 -07001150 }
1151
1152 private void handleSetMobileData(boolean enabled) {
Wink Savillee7982682010-12-07 10:31:02 -08001153 if (mNetTrackers[ConnectivityManager.TYPE_MOBILE] != null) {
1154 if (DBG) {
1155 Slog.d(TAG, mNetTrackers[ConnectivityManager.TYPE_MOBILE].toString() + enabled);
Robert Greenwaltc03fa502010-02-23 18:58:05 -08001156 }
Wink Savillee7982682010-12-07 10:31:02 -08001157 mNetTrackers[ConnectivityManager.TYPE_MOBILE].setDataEnable(enabled);
Robert Greenwaltc03fa502010-02-23 18:58:05 -08001158 }
1159 }
1160
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001161 private void enforceAccessPermission() {
Robert Greenwalt86e9e552009-07-16 17:21:39 -07001162 mContext.enforceCallingOrSelfPermission(
1163 android.Manifest.permission.ACCESS_NETWORK_STATE,
1164 "ConnectivityService");
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001165 }
1166
1167 private void enforceChangePermission() {
Robert Greenwalt86e9e552009-07-16 17:21:39 -07001168 mContext.enforceCallingOrSelfPermission(
1169 android.Manifest.permission.CHANGE_NETWORK_STATE,
1170 "ConnectivityService");
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001171 }
1172
Robert Greenwaltd0e18ff2010-01-26 11:40:34 -08001173 // TODO Make this a special check when it goes public
1174 private void enforceTetherChangePermission() {
1175 mContext.enforceCallingOrSelfPermission(
1176 android.Manifest.permission.CHANGE_NETWORK_STATE,
1177 "ConnectivityService");
1178 }
1179
Robert Greenwalt2a091d72010-02-11 18:18:40 -08001180 private void enforceTetherAccessPermission() {
1181 mContext.enforceCallingOrSelfPermission(
1182 android.Manifest.permission.ACCESS_NETWORK_STATE,
1183 "ConnectivityService");
1184 }
1185
Robert Greenwalt14f2ef42010-06-15 12:19:37 -07001186 private void enforceConnectivityInternalPermission() {
1187 mContext.enforceCallingOrSelfPermission(
1188 android.Manifest.permission.CONNECTIVITY_INTERNAL,
1189 "ConnectivityService");
1190 }
1191
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001192 /**
Robert Greenwalt86e9e552009-07-16 17:21:39 -07001193 * Handle a {@code DISCONNECTED} event. If this pertains to the non-active
1194 * network, we ignore it. If it is for the active network, we send out a
1195 * broadcast. But first, we check whether it might be possible to connect
1196 * to a different network.
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001197 * @param info the {@code NetworkInfo} for the network
1198 */
1199 private void handleDisconnect(NetworkInfo info) {
1200
Robert Greenwalt42acef32009-08-12 16:08:25 -07001201 int prevNetType = info.getType();
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001202
Robert Greenwalt42acef32009-08-12 16:08:25 -07001203 mNetTrackers[prevNetType].setTeardownRequested(false);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001204 /*
1205 * If the disconnected network is not the active one, then don't report
1206 * this as a loss of connectivity. What probably happened is that we're
1207 * getting the disconnect for a network that we explicitly disabled
1208 * in accordance with network preference policies.
1209 */
Robert Greenwaltd55a6b42011-03-25 13:09:25 -07001210 if (!mNetConfigs[prevNetType].isDefault()) {
Robert Greenwalt42acef32009-08-12 16:08:25 -07001211 List pids = mNetRequestersPids[prevNetType];
1212 for (int i = 0; i<pids.size(); i++) {
1213 Integer pid = (Integer)pids.get(i);
1214 // will remove them because the net's no longer connected
1215 // need to do this now as only now do we know the pids and
1216 // can properly null things that are no longer referenced.
1217 reassessPidDns(pid.intValue(), false);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001218 }
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001219 }
1220
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001221 Intent intent = new Intent(ConnectivityManager.CONNECTIVITY_ACTION);
1222 intent.putExtra(ConnectivityManager.EXTRA_NETWORK_INFO, info);
1223 if (info.isFailover()) {
1224 intent.putExtra(ConnectivityManager.EXTRA_IS_FAILOVER, true);
1225 info.setFailover(false);
1226 }
1227 if (info.getReason() != null) {
1228 intent.putExtra(ConnectivityManager.EXTRA_REASON, info.getReason());
1229 }
1230 if (info.getExtraInfo() != null) {
Robert Greenwalt86e9e552009-07-16 17:21:39 -07001231 intent.putExtra(ConnectivityManager.EXTRA_EXTRA_INFO,
1232 info.getExtraInfo());
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001233 }
Robert Greenwalt42acef32009-08-12 16:08:25 -07001234
Robert Greenwaltd55a6b42011-03-25 13:09:25 -07001235 if (mNetConfigs[prevNetType].isDefault()) {
Robert Greenwaltf21ef7d2011-01-11 13:56:33 -08001236 tryFailover(prevNetType);
1237 if (mActiveDefaultNetwork != -1) {
1238 NetworkInfo switchTo = mNetTrackers[mActiveDefaultNetwork].getNetworkInfo();
Robert Greenwaltcc4b4012010-01-25 17:54:29 -08001239 intent.putExtra(ConnectivityManager.EXTRA_OTHER_NETWORK_INFO, switchTo);
1240 } else {
Robert Greenwalt029be812010-09-20 18:01:43 -07001241 mDefaultInetConditionPublished = 0; // we're not connected anymore
Robert Greenwaltcc4b4012010-01-25 17:54:29 -08001242 intent.putExtra(ConnectivityManager.EXTRA_NO_CONNECTIVITY, true);
1243 }
Robert Greenwaltda03c4e2010-01-20 19:29:41 -08001244 }
Robert Greenwalt029be812010-09-20 18:01:43 -07001245 intent.putExtra(ConnectivityManager.EXTRA_INET_CONDITION, mDefaultInetConditionPublished);
Robert Greenwaltda03c4e2010-01-20 19:29:41 -08001246 // do this before we broadcast the change
Robert Greenwaltc76b8fa2010-07-23 15:46:26 -07001247 handleConnectivityChange(prevNetType);
Robert Greenwaltda03c4e2010-01-20 19:29:41 -08001248
1249 sendStickyBroadcast(intent);
1250 /*
1251 * If the failover network is already connected, then immediately send
1252 * out a followup broadcast indicating successful failover
1253 */
Robert Greenwaltf21ef7d2011-01-11 13:56:33 -08001254 if (mActiveDefaultNetwork != -1) {
1255 sendConnectedBroadcast(mNetTrackers[mActiveDefaultNetwork].getNetworkInfo());
Robert Greenwaltda03c4e2010-01-20 19:29:41 -08001256 }
1257 }
1258
Robert Greenwaltf21ef7d2011-01-11 13:56:33 -08001259 private void tryFailover(int prevNetType) {
Robert Greenwalt42acef32009-08-12 16:08:25 -07001260 /*
Robert Greenwaltbff90182011-01-06 15:41:07 -08001261 * If this is a default network, check if other defaults are available.
1262 * Try to reconnect on all available and let them hash it out when
1263 * more than one connects.
Robert Greenwalt42acef32009-08-12 16:08:25 -07001264 */
Robert Greenwaltd55a6b42011-03-25 13:09:25 -07001265 if (mNetConfigs[prevNetType].isDefault()) {
Robert Greenwalt42acef32009-08-12 16:08:25 -07001266 if (mActiveDefaultNetwork == prevNetType) {
1267 mActiveDefaultNetwork = -1;
1268 }
1269
Robert Greenwaltf21ef7d2011-01-11 13:56:33 -08001270 // don't signal a reconnect for anything lower or equal priority than our
1271 // current connected default
1272 // TODO - don't filter by priority now - nice optimization but risky
1273// int currentPriority = -1;
1274// if (mActiveDefaultNetwork != -1) {
Robert Greenwaltd55a6b42011-03-25 13:09:25 -07001275// currentPriority = mNetConfigs[mActiveDefaultNetwork].mPriority;
Robert Greenwaltf21ef7d2011-01-11 13:56:33 -08001276// }
Robert Greenwaltda03c4e2010-01-20 19:29:41 -08001277 for (int checkType=0; checkType <= ConnectivityManager.MAX_NETWORK_TYPE; checkType++) {
Robert Greenwalt5154ae762009-10-30 14:17:42 -07001278 if (checkType == prevNetType) continue;
Robert Greenwaltd55a6b42011-03-25 13:09:25 -07001279 if (mNetConfigs[checkType] == null) continue;
1280 if (!mNetConfigs[checkType].isDefault()) continue;
Wink Saville9f7a0b22011-01-26 15:43:49 -08001281
1282// Enabling the isAvailable() optimization caused mobile to not get
1283// selected if it was in the middle of error handling. Specifically
1284// a moble connection that took 30 seconds to complete the DEACTIVATE_DATA_CALL
1285// would not be available and we wouldn't get connected to anything.
1286// So removing the isAvailable() optimization below for now. TODO: This
1287// optimization should work and we need to investigate why it doesn't work.
1288// This could be related to how DEACTIVATE_DATA_CALL is reporting its
1289// complete before it is really complete.
1290// if (!mNetTrackers[checkType].isAvailable()) continue;
1291
Robert Greenwaltd55a6b42011-03-25 13:09:25 -07001292// if (currentPriority >= mNetConfigs[checkType].mPriority) continue;
Robert Greenwalt42acef32009-08-12 16:08:25 -07001293
Robert Greenwaltf21ef7d2011-01-11 13:56:33 -08001294 NetworkStateTracker checkTracker = mNetTrackers[checkType];
1295 NetworkInfo checkInfo = checkTracker.getNetworkInfo();
1296 if (!checkInfo.isConnectedOrConnecting() || checkTracker.isTeardownRequested()) {
1297 checkInfo.setFailover(true);
1298 checkTracker.reconnect();
Robert Greenwalt42acef32009-08-12 16:08:25 -07001299 }
Robert Greenwaltf21ef7d2011-01-11 13:56:33 -08001300 if (DBG) log("Attempting to switch to " + checkInfo.getTypeName());
Robert Greenwalt42acef32009-08-12 16:08:25 -07001301 }
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001302 }
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001303 }
1304
1305 private void sendConnectedBroadcast(NetworkInfo info) {
Robert Greenwalt1e9aac22010-09-15 17:36:33 -07001306 sendGeneralBroadcast(info, ConnectivityManager.CONNECTIVITY_ACTION);
1307 }
1308
1309 private void sendInetConditionBroadcast(NetworkInfo info) {
1310 sendGeneralBroadcast(info, ConnectivityManager.INET_CONDITION_ACTION);
1311 }
1312
1313 private void sendGeneralBroadcast(NetworkInfo info, String bcastType) {
1314 Intent intent = new Intent(bcastType);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001315 intent.putExtra(ConnectivityManager.EXTRA_NETWORK_INFO, info);
1316 if (info.isFailover()) {
1317 intent.putExtra(ConnectivityManager.EXTRA_IS_FAILOVER, true);
1318 info.setFailover(false);
1319 }
1320 if (info.getReason() != null) {
1321 intent.putExtra(ConnectivityManager.EXTRA_REASON, info.getReason());
1322 }
1323 if (info.getExtraInfo() != null) {
Robert Greenwalt86e9e552009-07-16 17:21:39 -07001324 intent.putExtra(ConnectivityManager.EXTRA_EXTRA_INFO,
1325 info.getExtraInfo());
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001326 }
Robert Greenwaltd7085fc2010-09-08 15:24:47 -07001327 intent.putExtra(ConnectivityManager.EXTRA_INET_CONDITION, mDefaultInetConditionPublished);
Mike Lockwood0f79b542009-08-14 14:18:49 -04001328 sendStickyBroadcast(intent);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001329 }
1330
1331 /**
1332 * Called when an attempt to fail over to another network has failed.
1333 * @param info the {@link NetworkInfo} for the failed network
1334 */
1335 private void handleConnectionFailure(NetworkInfo info) {
1336 mNetTrackers[info.getType()].setTeardownRequested(false);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001337
Robert Greenwalt42acef32009-08-12 16:08:25 -07001338 String reason = info.getReason();
1339 String extraInfo = info.getExtraInfo();
Robert Greenwalt86e9e552009-07-16 17:21:39 -07001340
Robert Greenwalt572172b2010-10-08 16:35:52 -07001341 String reasonText;
1342 if (reason == null) {
1343 reasonText = ".";
1344 } else {
1345 reasonText = " (" + reason + ").";
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001346 }
Wink Savilleed9c02b2010-12-03 12:01:38 -08001347 loge("Attempt to connect to " + info.getTypeName() + " failed" + reasonText);
Robert Greenwalt42acef32009-08-12 16:08:25 -07001348
1349 Intent intent = new Intent(ConnectivityManager.CONNECTIVITY_ACTION);
1350 intent.putExtra(ConnectivityManager.EXTRA_NETWORK_INFO, info);
1351 if (getActiveNetworkInfo() == null) {
1352 intent.putExtra(ConnectivityManager.EXTRA_NO_CONNECTIVITY, true);
1353 }
1354 if (reason != null) {
1355 intent.putExtra(ConnectivityManager.EXTRA_REASON, reason);
1356 }
1357 if (extraInfo != null) {
1358 intent.putExtra(ConnectivityManager.EXTRA_EXTRA_INFO, extraInfo);
1359 }
1360 if (info.isFailover()) {
1361 intent.putExtra(ConnectivityManager.EXTRA_IS_FAILOVER, true);
1362 info.setFailover(false);
1363 }
Robert Greenwaltda03c4e2010-01-20 19:29:41 -08001364
Robert Greenwaltd55a6b42011-03-25 13:09:25 -07001365 if (mNetConfigs[info.getType()].isDefault()) {
Robert Greenwaltf21ef7d2011-01-11 13:56:33 -08001366 tryFailover(info.getType());
1367 if (mActiveDefaultNetwork != -1) {
1368 NetworkInfo switchTo = mNetTrackers[mActiveDefaultNetwork].getNetworkInfo();
Robert Greenwaltcc4b4012010-01-25 17:54:29 -08001369 intent.putExtra(ConnectivityManager.EXTRA_OTHER_NETWORK_INFO, switchTo);
1370 } else {
Robert Greenwalt029be812010-09-20 18:01:43 -07001371 mDefaultInetConditionPublished = 0;
Robert Greenwaltcc4b4012010-01-25 17:54:29 -08001372 intent.putExtra(ConnectivityManager.EXTRA_NO_CONNECTIVITY, true);
1373 }
Robert Greenwaltda03c4e2010-01-20 19:29:41 -08001374 }
Robert Greenwaltcc4b4012010-01-25 17:54:29 -08001375
Robert Greenwalt029be812010-09-20 18:01:43 -07001376 intent.putExtra(ConnectivityManager.EXTRA_INET_CONDITION, mDefaultInetConditionPublished);
Mike Lockwood0f79b542009-08-14 14:18:49 -04001377 sendStickyBroadcast(intent);
Robert Greenwaltda03c4e2010-01-20 19:29:41 -08001378 /*
1379 * If the failover network is already connected, then immediately send
1380 * out a followup broadcast indicating successful failover
1381 */
Robert Greenwaltf21ef7d2011-01-11 13:56:33 -08001382 if (mActiveDefaultNetwork != -1) {
1383 sendConnectedBroadcast(mNetTrackers[mActiveDefaultNetwork].getNetworkInfo());
Robert Greenwaltda03c4e2010-01-20 19:29:41 -08001384 }
Mike Lockwood0f79b542009-08-14 14:18:49 -04001385 }
1386
1387 private void sendStickyBroadcast(Intent intent) {
1388 synchronized(this) {
Dianne Hackborn1c633fc2009-12-08 19:45:14 -08001389 if (!mSystemReady) {
1390 mInitialBroadcast = new Intent(intent);
Mike Lockwood0f79b542009-08-14 14:18:49 -04001391 }
Dianne Hackborn1c633fc2009-12-08 19:45:14 -08001392 intent.addFlags(Intent.FLAG_RECEIVER_REGISTERED_ONLY_BEFORE_BOOT);
1393 mContext.sendStickyBroadcast(intent);
Mike Lockwood0f79b542009-08-14 14:18:49 -04001394 }
1395 }
1396
1397 void systemReady() {
1398 synchronized(this) {
1399 mSystemReady = true;
Dianne Hackborn1c633fc2009-12-08 19:45:14 -08001400 if (mInitialBroadcast != null) {
1401 mContext.sendStickyBroadcast(mInitialBroadcast);
1402 mInitialBroadcast = null;
Mike Lockwood0f79b542009-08-14 14:18:49 -04001403 }
1404 }
Robert Greenwalt434203a2010-10-11 16:00:27 -07001405 // load the global proxy at startup
1406 mHandler.sendMessage(mHandler.obtainMessage(EVENT_APPLY_GLOBAL_HTTP_PROXY));
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001407 }
1408
1409 private void handleConnect(NetworkInfo info) {
Robert Greenwalt42acef32009-08-12 16:08:25 -07001410 int type = info.getType();
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001411
1412 // snapshot isFailover, because sendConnectedBroadcast() resets it
1413 boolean isFailover = info.isFailover();
Robert Greenwalt42acef32009-08-12 16:08:25 -07001414 NetworkStateTracker thisNet = mNetTrackers[type];
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001415
Robert Greenwalt42acef32009-08-12 16:08:25 -07001416 // if this is a default net and other default is running
1417 // kill the one not preferred
Robert Greenwaltd55a6b42011-03-25 13:09:25 -07001418 if (mNetConfigs[type].isDefault()) {
Robert Greenwalt42acef32009-08-12 16:08:25 -07001419 if (mActiveDefaultNetwork != -1 && mActiveDefaultNetwork != type) {
1420 if ((type != mNetworkPreference &&
Wink Saville975c8482011-04-07 14:23:45 -07001421 mNetConfigs[mActiveDefaultNetwork].priority >
1422 mNetConfigs[type].priority) ||
Robert Greenwalt42acef32009-08-12 16:08:25 -07001423 mNetworkPreference == mActiveDefaultNetwork) {
1424 // don't accept this one
Wink Savilleed9c02b2010-12-03 12:01:38 -08001425 if (DBG) {
1426 log("Not broadcasting CONNECT_ACTION " +
Robert Greenwalt42acef32009-08-12 16:08:25 -07001427 "to torn down network " + info.getTypeName());
Wink Savilleed9c02b2010-12-03 12:01:38 -08001428 }
Robert Greenwalt42acef32009-08-12 16:08:25 -07001429 teardown(thisNet);
1430 return;
1431 } else {
1432 // tear down the other
1433 NetworkStateTracker otherNet =
1434 mNetTrackers[mActiveDefaultNetwork];
Wink Savilleed9c02b2010-12-03 12:01:38 -08001435 if (DBG) {
1436 log("Policy requires " + otherNet.getNetworkInfo().getTypeName() +
Robert Greenwalt42acef32009-08-12 16:08:25 -07001437 " teardown");
Wink Savilleed9c02b2010-12-03 12:01:38 -08001438 }
Robert Greenwalt42acef32009-08-12 16:08:25 -07001439 if (!teardown(otherNet)) {
Wink Savilleed9c02b2010-12-03 12:01:38 -08001440 loge("Network declined teardown request");
Robert Greenwalt27725e82011-03-29 11:36:28 -07001441 teardown(thisNet);
Robert Greenwalt42acef32009-08-12 16:08:25 -07001442 return;
1443 }
Robert Greenwalt14f2ef42010-06-15 12:19:37 -07001444 }
1445 }
1446 synchronized (ConnectivityService.this) {
1447 // have a new default network, release the transition wakelock in a second
1448 // if it's held. The second pause is to allow apps to reconnect over the
1449 // new network
1450 if (mNetTransitionWakeLock.isHeld()) {
1451 mHandler.sendMessageDelayed(mHandler.obtainMessage(
Robert Greenwaltf3331232010-09-24 14:32:21 -07001452 EVENT_CLEAR_NET_TRANSITION_WAKELOCK,
Robert Greenwalt14f2ef42010-06-15 12:19:37 -07001453 mNetTransitionWakeLockSerialNumber, 0),
1454 1000);
Robert Greenwalt42acef32009-08-12 16:08:25 -07001455 }
1456 }
1457 mActiveDefaultNetwork = type;
Robert Greenwaltd7085fc2010-09-08 15:24:47 -07001458 // this will cause us to come up initially as unconnected and switching
1459 // to connected after our normal pause unless somebody reports us as reall
1460 // disconnected
1461 mDefaultInetConditionPublished = 0;
1462 mDefaultConnectionSequence++;
1463 mInetConditionChangeInFlight = false;
1464 // Don't do this - if we never sign in stay, grey
1465 //reportNetworkCondition(mActiveDefaultNetwork, 100);
Robert Greenwalt42acef32009-08-12 16:08:25 -07001466 }
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001467 thisNet.setTeardownRequested(false);
Irfan Sheriffd649c122010-06-09 15:39:36 -07001468 updateNetworkSettings(thisNet);
Robert Greenwaltc76b8fa2010-07-23 15:46:26 -07001469 handleConnectivityChange(type);
Robert Greenwalt42acef32009-08-12 16:08:25 -07001470 sendConnectedBroadcast(info);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001471 }
1472
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001473 /**
Robert Greenwaltc76b8fa2010-07-23 15:46:26 -07001474 * After a change in the connectivity state of a network. We're mainly
1475 * concerned with making sure that the list of DNS servers is set up
1476 * according to which networks are connected, and ensuring that the
1477 * right routing table entries exist.
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001478 */
Robert Greenwaltc76b8fa2010-07-23 15:46:26 -07001479 private void handleConnectivityChange(int netType) {
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001480 /*
Robert Greenwalt42acef32009-08-12 16:08:25 -07001481 * If a non-default network is enabled, add the host routes that
Robert Greenwaltc76b8fa2010-07-23 15:46:26 -07001482 * will allow it's DNS servers to be accessed.
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001483 */
Robert Greenwaltc76b8fa2010-07-23 15:46:26 -07001484 handleDnsConfigurationChange(netType);
Robert Greenwalt42acef32009-08-12 16:08:25 -07001485
Robert Greenwaltc76b8fa2010-07-23 15:46:26 -07001486 if (mNetTrackers[netType].getNetworkInfo().isConnected()) {
Robert Greenwaltd55a6b42011-03-25 13:09:25 -07001487 if (mNetConfigs[netType].isDefault()) {
Robert Greenwalt434203a2010-10-11 16:00:27 -07001488 handleApplyDefaultProxy(netType);
Robert Greenwaltc76b8fa2010-07-23 15:46:26 -07001489 addDefaultRoute(mNetTrackers[netType]);
Robert Greenwalt42acef32009-08-12 16:08:25 -07001490 } else {
Robert Greenwalt13ec4062011-04-01 10:51:22 -07001491 // many radios add a default route even when we don't want one.
1492 // remove the default route unless we need it for our active network
1493 if (mActiveDefaultNetwork != -1) {
1494 LinkProperties defaultLinkProperties =
1495 mNetTrackers[mActiveDefaultNetwork].getLinkProperties();
1496 LinkProperties newLinkProperties =
1497 mNetTrackers[netType].getLinkProperties();
1498 String defaultIface = defaultLinkProperties.getInterfaceName();
1499 if (defaultIface != null &&
1500 !defaultIface.equals(newLinkProperties.getInterfaceName())) {
1501 removeDefaultRoute(mNetTrackers[netType]);
1502 }
1503 }
Michael Jurka8fad7ee2011-03-30 19:54:08 -07001504 addPrivateDnsRoutes(mNetTrackers[netType]);
Robert Greenwaltc76b8fa2010-07-23 15:46:26 -07001505 }
Kazuhiro Ondo01758e82011-04-30 20:10:57 -05001506
1507 /** Notify TetheringService if interface name has been changed. */
1508 if (TextUtils.equals(mNetTrackers[netType].getNetworkInfo().getReason(),
1509 Phone.REASON_LINK_PROPERTIES_CHANGED)) {
1510 handleTetherIfaceChange(netType);
1511 }
Robert Greenwaltc76b8fa2010-07-23 15:46:26 -07001512 } else {
Robert Greenwaltd55a6b42011-03-25 13:09:25 -07001513 if (mNetConfigs[netType].isDefault()) {
Robert Greenwaltc76b8fa2010-07-23 15:46:26 -07001514 removeDefaultRoute(mNetTrackers[netType]);
1515 } else {
1516 removePrivateDnsRoutes(mNetTrackers[netType]);
Robert Greenwalt42acef32009-08-12 16:08:25 -07001517 }
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001518 }
1519 }
1520
Irfan Sheriffd649c122010-06-09 15:39:36 -07001521 private void addPrivateDnsRoutes(NetworkStateTracker nt) {
Irfan Sheriffd649c122010-06-09 15:39:36 -07001522 boolean privateDnsRouteSet = nt.isPrivateDnsRouteSet();
Robert Greenwalt37e65eb2010-08-30 10:56:47 -07001523 LinkProperties p = nt.getLinkProperties();
Robert Greenwalt47f69fe2010-06-15 15:43:39 -07001524 if (p == null) return;
1525 String interfaceName = p.getInterfaceName();
Irfan Sheriffd649c122010-06-09 15:39:36 -07001526
1527 if (DBG) {
Wink Savilleed9c02b2010-12-03 12:01:38 -08001528 log("addPrivateDnsRoutes for " + nt +
Irfan Sheriffd649c122010-06-09 15:39:36 -07001529 "(" + interfaceName + ") - mPrivateDnsRouteSet = " + privateDnsRouteSet);
1530 }
Irfan Sheriffd649c122010-06-09 15:39:36 -07001531 if (interfaceName != null && !privateDnsRouteSet) {
Robert Greenwalt47f69fe2010-06-15 15:43:39 -07001532 Collection<InetAddress> dnsList = p.getDnses();
1533 for (InetAddress dns : dnsList) {
Robert Greenwaltf43396c2011-05-06 17:10:53 -07001534 addHostRoute(nt, dns, 0);
Irfan Sheriffd649c122010-06-09 15:39:36 -07001535 }
1536 nt.privateDnsRouteSet(true);
1537 }
1538 }
1539
1540 private void removePrivateDnsRoutes(NetworkStateTracker nt) {
Robert Greenwalt37e65eb2010-08-30 10:56:47 -07001541 LinkProperties p = nt.getLinkProperties();
Robert Greenwalt47f69fe2010-06-15 15:43:39 -07001542 if (p == null) return;
1543 String interfaceName = p.getInterfaceName();
Irfan Sheriffd649c122010-06-09 15:39:36 -07001544 boolean privateDnsRouteSet = nt.isPrivateDnsRouteSet();
1545 if (interfaceName != null && privateDnsRouteSet) {
1546 if (DBG) {
Wink Savilleed9c02b2010-12-03 12:01:38 -08001547 log("removePrivateDnsRoutes for " + nt.getNetworkInfo().getTypeName() +
Irfan Sheriffd649c122010-06-09 15:39:36 -07001548 " (" + interfaceName + ")");
1549 }
Robert Greenwalt59b1a4e2011-05-10 15:05:02 -07001550
1551 Collection<InetAddress> dnsList = p.getDnses();
1552 for (InetAddress dns : dnsList) {
1553 if (DBG) log(" removing " + dns);
1554 RouteInfo route = RouteInfo.makeHostRoute(dns);
1555 try {
1556 mNetd.removeRoute(interfaceName, route);
1557 } catch (Exception ex) {
1558 loge("error (" + ex + ") removing dns route " + route);
1559 }
1560 }
Irfan Sheriffd649c122010-06-09 15:39:36 -07001561 nt.privateDnsRouteSet(false);
1562 }
1563 }
1564
Irfan Sheriffd649c122010-06-09 15:39:36 -07001565
1566 private void addDefaultRoute(NetworkStateTracker nt) {
Robert Greenwalt37e65eb2010-08-30 10:56:47 -07001567 LinkProperties p = nt.getLinkProperties();
Robert Greenwalt47f69fe2010-06-15 15:43:39 -07001568 if (p == null) return;
1569 String interfaceName = p.getInterfaceName();
Robert Greenwalt992564e2011-02-09 13:56:06 -08001570 if (TextUtils.isEmpty(interfaceName)) return;
Irfan Sheriffd649c122010-06-09 15:39:36 -07001571
Robert Greenwalt59b1a4e2011-05-10 15:05:02 -07001572 for (RouteInfo route : p.getRoutes()) {
Robert Greenwaltaa70f102011-04-28 14:28:50 -07001573 //TODO - handle non-default routes
1574 if (route.isDefaultRoute()) {
Robert Greenwalt59b1a4e2011-05-10 15:05:02 -07001575 if (DBG) log("adding default route " + route);
Robert Greenwaltaa70f102011-04-28 14:28:50 -07001576 InetAddress gateway = route.getGateway();
Robert Greenwalt59b1a4e2011-05-10 15:05:02 -07001577 if (addHostRoute(nt, gateway, 0)) {
1578 try {
1579 mNetd.addRoute(interfaceName, route);
1580 } catch (Exception e) {
1581 loge("error adding default route " + route);
1582 continue;
1583 }
Robert Greenwaltaa70f102011-04-28 14:28:50 -07001584 if (DBG) {
1585 NetworkInfo networkInfo = nt.getNetworkInfo();
1586 log("addDefaultRoute for " + networkInfo.getTypeName() +
1587 " (" + interfaceName + "), GatewayAddr=" +
1588 gateway.getHostAddress());
1589 }
Robert Greenwalt59b1a4e2011-05-10 15:05:02 -07001590 } else {
1591 loge("error adding host route for default route " + route);
Robert Greenwaltedcb4f92011-03-22 18:47:42 -07001592 }
Irfan Sheriffd649c122010-06-09 15:39:36 -07001593 }
Irfan Sheriffd649c122010-06-09 15:39:36 -07001594 }
1595 }
1596
1597
1598 public void removeDefaultRoute(NetworkStateTracker nt) {
Robert Greenwalt37e65eb2010-08-30 10:56:47 -07001599 LinkProperties p = nt.getLinkProperties();
Robert Greenwalt47f69fe2010-06-15 15:43:39 -07001600 if (p == null) return;
1601 String interfaceName = p.getInterfaceName();
Irfan Sheriffd649c122010-06-09 15:39:36 -07001602
Robert Greenwalt59b1a4e2011-05-10 15:05:02 -07001603 if (interfaceName == null) return;
1604
1605 for (RouteInfo route : p.getRoutes()) {
1606 //TODO - handle non-default routes
1607 if (route.isDefaultRoute()) {
1608 try {
1609 mNetd.removeRoute(interfaceName, route);
1610 } catch (Exception ex) {
1611 loge("error (" + ex + ") removing default route " + route);
1612 continue;
1613 }
Robert Greenwaltedcb4f92011-03-22 18:47:42 -07001614 if (DBG) {
1615 NetworkInfo networkInfo = nt.getNetworkInfo();
1616 log("removeDefaultRoute for " + networkInfo.getTypeName() + " (" +
1617 interfaceName + ")");
1618 }
Irfan Sheriffd649c122010-06-09 15:39:36 -07001619 }
Irfan Sheriffd649c122010-06-09 15:39:36 -07001620 }
1621 }
1622
1623 /**
1624 * Reads the network specific TCP buffer sizes from SystemProperties
1625 * net.tcp.buffersize.[default|wifi|umts|edge|gprs] and set them for system
1626 * wide use
1627 */
1628 public void updateNetworkSettings(NetworkStateTracker nt) {
1629 String key = nt.getTcpBufferSizesPropName();
1630 String bufferSizes = SystemProperties.get(key);
1631
1632 if (bufferSizes.length() == 0) {
Wink Savilleed9c02b2010-12-03 12:01:38 -08001633 loge(key + " not found in system properties. Using defaults");
Irfan Sheriffd649c122010-06-09 15:39:36 -07001634
1635 // Setting to default values so we won't be stuck to previous values
1636 key = "net.tcp.buffersize.default";
1637 bufferSizes = SystemProperties.get(key);
1638 }
1639
1640 // Set values in kernel
1641 if (bufferSizes.length() != 0) {
1642 if (DBG) {
Wink Savilleed9c02b2010-12-03 12:01:38 -08001643 log("Setting TCP values: [" + bufferSizes
Irfan Sheriffd649c122010-06-09 15:39:36 -07001644 + "] which comes from [" + key + "]");
1645 }
1646 setBufferSize(bufferSizes);
1647 }
1648 }
1649
1650 /**
1651 * Writes TCP buffer sizes to /sys/kernel/ipv4/tcp_[r/w]mem_[min/def/max]
1652 * which maps to /proc/sys/net/ipv4/tcp_rmem and tcpwmem
1653 *
1654 * @param bufferSizes in the format of "readMin, readInitial, readMax,
1655 * writeMin, writeInitial, writeMax"
1656 */
1657 private void setBufferSize(String bufferSizes) {
1658 try {
1659 String[] values = bufferSizes.split(",");
1660
1661 if (values.length == 6) {
1662 final String prefix = "/sys/kernel/ipv4/tcp_";
Mike Lockwoodda8bb742011-05-28 13:24:04 -04001663 FileUtils.stringToFile(prefix + "rmem_min", values[0]);
1664 FileUtils.stringToFile(prefix + "rmem_def", values[1]);
1665 FileUtils.stringToFile(prefix + "rmem_max", values[2]);
1666 FileUtils.stringToFile(prefix + "wmem_min", values[3]);
1667 FileUtils.stringToFile(prefix + "wmem_def", values[4]);
1668 FileUtils.stringToFile(prefix + "wmem_max", values[5]);
Irfan Sheriffd649c122010-06-09 15:39:36 -07001669 } else {
Wink Savilleed9c02b2010-12-03 12:01:38 -08001670 loge("Invalid buffersize string: " + bufferSizes);
Irfan Sheriffd649c122010-06-09 15:39:36 -07001671 }
1672 } catch (IOException e) {
Wink Savilleed9c02b2010-12-03 12:01:38 -08001673 loge("Can't set tcp buffer sizes:" + e);
Irfan Sheriffd649c122010-06-09 15:39:36 -07001674 }
1675 }
1676
Robert Greenwalt42acef32009-08-12 16:08:25 -07001677 /**
1678 * Adjust the per-process dns entries (net.dns<x>.<pid>) based
1679 * on the highest priority active net which this process requested.
1680 * If there aren't any, clear it out
1681 */
1682 private void reassessPidDns(int myPid, boolean doBump)
1683 {
Wink Savilleed9c02b2010-12-03 12:01:38 -08001684 if (DBG) log("reassessPidDns for pid " + myPid);
Robert Greenwalt42acef32009-08-12 16:08:25 -07001685 for(int i : mPriorityList) {
Robert Greenwaltd55a6b42011-03-25 13:09:25 -07001686 if (mNetConfigs[i].isDefault()) {
Robert Greenwalt42acef32009-08-12 16:08:25 -07001687 continue;
1688 }
1689 NetworkStateTracker nt = mNetTrackers[i];
Robert Greenwalt86e9e552009-07-16 17:21:39 -07001690 if (nt.getNetworkInfo().isConnected() &&
1691 !nt.isTeardownRequested()) {
Robert Greenwalt37e65eb2010-08-30 10:56:47 -07001692 LinkProperties p = nt.getLinkProperties();
Robert Greenwalt47f69fe2010-06-15 15:43:39 -07001693 if (p == null) continue;
Robert Greenwalt42acef32009-08-12 16:08:25 -07001694 List pids = mNetRequestersPids[i];
1695 for (int j=0; j<pids.size(); j++) {
1696 Integer pid = (Integer)pids.get(j);
1697 if (pid.intValue() == myPid) {
Robert Greenwalt47f69fe2010-06-15 15:43:39 -07001698 Collection<InetAddress> dnses = p.getDnses();
1699 writePidDns(dnses, myPid);
Robert Greenwalt42acef32009-08-12 16:08:25 -07001700 if (doBump) {
1701 bumpDns();
1702 }
1703 return;
1704 }
1705 }
1706 }
1707 }
1708 // nothing found - delete
1709 for (int i = 1; ; i++) {
1710 String prop = "net.dns" + i + "." + myPid;
1711 if (SystemProperties.get(prop).length() == 0) {
1712 if (doBump) {
1713 bumpDns();
1714 }
1715 return;
1716 }
1717 SystemProperties.set(prop, "");
1718 }
1719 }
1720
Robert Greenwalt10398722010-12-17 15:20:36 -08001721 // return true if results in a change
1722 private boolean writePidDns(Collection <InetAddress> dnses, int pid) {
Robert Greenwalt42acef32009-08-12 16:08:25 -07001723 int j = 1;
Robert Greenwalt10398722010-12-17 15:20:36 -08001724 boolean changed = false;
Robert Greenwalt47f69fe2010-06-15 15:43:39 -07001725 for (InetAddress dns : dnses) {
Robert Greenwalt10398722010-12-17 15:20:36 -08001726 String dnsString = dns.getHostAddress();
1727 if (changed || !dnsString.equals(SystemProperties.get("net.dns" + j + "." + pid))) {
1728 changed = true;
1729 SystemProperties.set("net.dns" + j++ + "." + pid, dns.getHostAddress());
1730 }
Robert Greenwalt42acef32009-08-12 16:08:25 -07001731 }
Robert Greenwalt10398722010-12-17 15:20:36 -08001732 return changed;
Robert Greenwalt42acef32009-08-12 16:08:25 -07001733 }
1734
1735 private void bumpDns() {
1736 /*
1737 * Bump the property that tells the name resolver library to reread
1738 * the DNS server list from the properties.
1739 */
1740 String propVal = SystemProperties.get("net.dnschange");
1741 int n = 0;
1742 if (propVal.length() != 0) {
1743 try {
1744 n = Integer.parseInt(propVal);
1745 } catch (NumberFormatException e) {}
1746 }
1747 SystemProperties.set("net.dnschange", "" + (n+1));
Robert Greenwalt03595d02010-11-02 14:08:23 -07001748 /*
1749 * Tell the VMs to toss their DNS caches
1750 */
1751 Intent intent = new Intent(Intent.ACTION_CLEAR_DNS_CACHE);
1752 intent.addFlags(Intent.FLAG_RECEIVER_REPLACE_PENDING);
Stan Chesnutt3d1db862011-01-05 17:14:03 -08001753 /*
1754 * Connectivity events can happen before boot has completed ...
1755 */
1756 intent.addFlags(Intent.FLAG_RECEIVER_REGISTERED_ONLY_BEFORE_BOOT);
Robert Greenwalt03595d02010-11-02 14:08:23 -07001757 mContext.sendBroadcast(intent);
Robert Greenwalt42acef32009-08-12 16:08:25 -07001758 }
1759
Robert Greenwaltc76b8fa2010-07-23 15:46:26 -07001760 private void handleDnsConfigurationChange(int netType) {
Robert Greenwalt42acef32009-08-12 16:08:25 -07001761 // add default net's dns entries
Robert Greenwaltc76b8fa2010-07-23 15:46:26 -07001762 NetworkStateTracker nt = mNetTrackers[netType];
1763 if (nt != null && nt.getNetworkInfo().isConnected() && !nt.isTeardownRequested()) {
Robert Greenwalt37e65eb2010-08-30 10:56:47 -07001764 LinkProperties p = nt.getLinkProperties();
Robert Greenwaltc76b8fa2010-07-23 15:46:26 -07001765 if (p == null) return;
1766 Collection<InetAddress> dnses = p.getDnses();
Robert Greenwalt10398722010-12-17 15:20:36 -08001767 boolean changed = false;
Robert Greenwaltd55a6b42011-03-25 13:09:25 -07001768 if (mNetConfigs[netType].isDefault()) {
Robert Greenwaltc76b8fa2010-07-23 15:46:26 -07001769 int j = 1;
Robert Greenwalte90aa5e2010-09-01 11:34:05 -07001770 if (dnses.size() == 0 && mDefaultDns != null) {
Robert Greenwalt10398722010-12-17 15:20:36 -08001771 String dnsString = mDefaultDns.getHostAddress();
1772 if (!dnsString.equals(SystemProperties.get("net.dns1"))) {
1773 if (DBG) {
1774 log("no dns provided - using " + dnsString);
1775 }
1776 changed = true;
1777 SystemProperties.set("net.dns1", dnsString);
Robert Greenwalt42acef32009-08-12 16:08:25 -07001778 }
Robert Greenwalte90aa5e2010-09-01 11:34:05 -07001779 j++;
1780 } else {
1781 for (InetAddress dns : dnses) {
Robert Greenwalt10398722010-12-17 15:20:36 -08001782 String dnsString = dns.getHostAddress();
1783 if (!changed && dnsString.equals(SystemProperties.get("net.dns" + j))) {
1784 j++;
1785 continue;
1786 }
Robert Greenwalte90aa5e2010-09-01 11:34:05 -07001787 if (DBG) {
Wink Savilleed9c02b2010-12-03 12:01:38 -08001788 log("adding dns " + dns + " for " +
Robert Greenwalte90aa5e2010-09-01 11:34:05 -07001789 nt.getNetworkInfo().getTypeName());
1790 }
Robert Greenwalt10398722010-12-17 15:20:36 -08001791 changed = true;
1792 SystemProperties.set("net.dns" + j++, dnsString);
Robert Greenwalte90aa5e2010-09-01 11:34:05 -07001793 }
Robert Greenwaltc76b8fa2010-07-23 15:46:26 -07001794 }
1795 for (int k=j ; k<mNumDnsEntries; k++) {
Robert Greenwalt10398722010-12-17 15:20:36 -08001796 if (changed || !TextUtils.isEmpty(SystemProperties.get("net.dns" + k))) {
1797 if (DBG) log("erasing net.dns" + k);
1798 changed = true;
1799 SystemProperties.set("net.dns" + k, "");
1800 }
Robert Greenwaltc76b8fa2010-07-23 15:46:26 -07001801 }
1802 mNumDnsEntries = j;
1803 } else {
1804 // set per-pid dns for attached secondary nets
1805 List pids = mNetRequestersPids[netType];
1806 for (int y=0; y< pids.size(); y++) {
1807 Integer pid = (Integer)pids.get(y);
Robert Greenwalt10398722010-12-17 15:20:36 -08001808 changed = writePidDns(dnses, pid.intValue());
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001809 }
1810 }
Robert Greenwalt10398722010-12-17 15:20:36 -08001811 if (changed) bumpDns();
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001812 }
Robert Greenwalt42acef32009-08-12 16:08:25 -07001813 }
1814
Robert Greenwaltf2102f72011-05-03 19:02:44 -07001815 private int getRestoreDefaultNetworkDelay(int networkType) {
Robert Greenwalt42acef32009-08-12 16:08:25 -07001816 String restoreDefaultNetworkDelayStr = SystemProperties.get(
1817 NETWORK_RESTORE_DELAY_PROP_NAME);
1818 if(restoreDefaultNetworkDelayStr != null &&
1819 restoreDefaultNetworkDelayStr.length() != 0) {
1820 try {
1821 return Integer.valueOf(restoreDefaultNetworkDelayStr);
1822 } catch (NumberFormatException e) {
1823 }
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001824 }
Robert Greenwaltf2102f72011-05-03 19:02:44 -07001825 // if the system property isn't set, use the value for the apn type
1826 int ret = RESTORE_DEFAULT_NETWORK_DELAY;
1827
1828 if ((networkType <= ConnectivityManager.MAX_NETWORK_TYPE) &&
1829 (mNetConfigs[networkType] != null)) {
1830 ret = mNetConfigs[networkType].restoreTime;
1831 }
1832 return ret;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001833 }
1834
1835 @Override
1836 protected void dump(FileDescriptor fd, PrintWriter pw, String[] args) {
Robert Greenwalt86e9e552009-07-16 17:21:39 -07001837 if (mContext.checkCallingOrSelfPermission(
1838 android.Manifest.permission.DUMP)
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001839 != PackageManager.PERMISSION_GRANTED) {
Robert Greenwalt86e9e552009-07-16 17:21:39 -07001840 pw.println("Permission Denial: can't dump ConnectivityService " +
1841 "from from pid=" + Binder.getCallingPid() + ", uid=" +
1842 Binder.getCallingUid());
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001843 return;
1844 }
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001845 pw.println();
1846 for (NetworkStateTracker nst : mNetTrackers) {
Robert Greenwaltb9285352009-12-21 18:24:07 -08001847 if (nst != null) {
1848 if (nst.getNetworkInfo().isConnected()) {
1849 pw.println("Active network: " + nst.getNetworkInfo().
1850 getTypeName());
1851 }
1852 pw.println(nst.getNetworkInfo());
1853 pw.println(nst);
1854 pw.println();
Robert Greenwalt42acef32009-08-12 16:08:25 -07001855 }
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001856 }
Robert Greenwaltb9285352009-12-21 18:24:07 -08001857
1858 pw.println("Network Requester Pids:");
1859 for (int net : mPriorityList) {
1860 String pidString = net + ": ";
1861 for (Object pid : mNetRequestersPids[net]) {
1862 pidString = pidString + pid.toString() + ", ";
1863 }
1864 pw.println(pidString);
1865 }
1866 pw.println();
1867
1868 pw.println("FeatureUsers:");
1869 for (Object requester : mFeatureUsers) {
1870 pw.println(requester.toString());
1871 }
1872 pw.println();
Robert Greenwalt2a091d72010-02-11 18:18:40 -08001873
Robert Greenwalt14f2ef42010-06-15 12:19:37 -07001874 synchronized (this) {
1875 pw.println("NetworkTranstionWakeLock is currently " +
1876 (mNetTransitionWakeLock.isHeld() ? "" : "not ") + "held.");
1877 pw.println("It was last requested for "+mNetTransitionWakeLockCausedBy);
1878 }
1879 pw.println();
1880
Robert Greenwalt2a091d72010-02-11 18:18:40 -08001881 mTethering.dump(fd, pw, args);
Robert Greenwalt4e8dfef2010-09-20 14:35:25 -07001882
1883 if (mInetLog != null) {
1884 pw.println();
1885 pw.println("Inet condition reports:");
1886 for(int i = 0; i < mInetLog.size(); i++) {
1887 pw.println(mInetLog.get(i));
1888 }
1889 }
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001890 }
1891
Robert Greenwalt42acef32009-08-12 16:08:25 -07001892 // must be stateless - things change under us.
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001893 private class MyHandler extends Handler {
Wink Savillebb08caf2010-09-02 19:23:52 -07001894 public MyHandler(Looper looper) {
1895 super(looper);
1896 }
1897
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001898 @Override
1899 public void handleMessage(Message msg) {
1900 NetworkInfo info;
1901 switch (msg.what) {
1902 case NetworkStateTracker.EVENT_STATE_CHANGED:
1903 info = (NetworkInfo) msg.obj;
Robert Greenwalt511288a2009-12-07 11:33:18 -08001904 int type = info.getType();
1905 NetworkInfo.State state = info.getState();
Robert Greenwalt511288a2009-12-07 11:33:18 -08001906
Wink Savilleed9c02b2010-12-03 12:01:38 -08001907 if (DBG) log("ConnectivityChange for " +
Robert Greenwalt86e9e552009-07-16 17:21:39 -07001908 info.getTypeName() + ": " +
Robert Greenwalt511288a2009-12-07 11:33:18 -08001909 state + "/" + info.getDetailedState());
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001910
1911 // Connectivity state changed:
1912 // [31-13] Reserved for future use
Robert Greenwalt86e9e552009-07-16 17:21:39 -07001913 // [12-9] Network subtype (for mobile network, as defined
1914 // by TelephonyManager)
1915 // [8-3] Detailed state ordinal (as defined by
1916 // NetworkInfo.DetailedState)
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001917 // [2-0] Network type (as defined by ConnectivityManager)
1918 int eventLogParam = (info.getType() & 0x7) |
1919 ((info.getDetailedState().ordinal() & 0x3f) << 3) |
1920 (info.getSubtype() << 9);
Doug Zongkerab5c49c2009-12-04 10:31:43 -08001921 EventLog.writeEvent(EventLogTags.CONNECTIVITY_STATE_CHANGED,
Robert Greenwalt86e9e552009-07-16 17:21:39 -07001922 eventLogParam);
1923
1924 if (info.getDetailedState() ==
1925 NetworkInfo.DetailedState.FAILED) {
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001926 handleConnectionFailure(info);
Robert Greenwalt511288a2009-12-07 11:33:18 -08001927 } else if (state == NetworkInfo.State.DISCONNECTED) {
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001928 handleDisconnect(info);
Robert Greenwalt511288a2009-12-07 11:33:18 -08001929 } else if (state == NetworkInfo.State.SUSPENDED) {
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001930 // TODO: need to think this over.
Robert Greenwalt86e9e552009-07-16 17:21:39 -07001931 // the logic here is, handle SUSPENDED the same as
1932 // DISCONNECTED. The only difference being we are
1933 // broadcasting an intent with NetworkInfo that's
1934 // suspended. This allows the applications an
1935 // opportunity to handle DISCONNECTED and SUSPENDED
1936 // differently, or not.
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001937 handleDisconnect(info);
Robert Greenwalt511288a2009-12-07 11:33:18 -08001938 } else if (state == NetworkInfo.State.CONNECTED) {
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001939 handleConnect(info);
1940 }
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001941 break;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001942 case NetworkStateTracker.EVENT_CONFIGURATION_CHANGED:
Robert Greenwaltc76b8fa2010-07-23 15:46:26 -07001943 info = (NetworkInfo) msg.obj;
Robert Greenwaltd55a6b42011-03-25 13:09:25 -07001944 handleConnectivityChange(info.getType());
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001945 break;
Robert Greenwaltf3331232010-09-24 14:32:21 -07001946 case EVENT_CLEAR_NET_TRANSITION_WAKELOCK:
Robert Greenwalt14f2ef42010-06-15 12:19:37 -07001947 String causedBy = null;
1948 synchronized (ConnectivityService.this) {
1949 if (msg.arg1 == mNetTransitionWakeLockSerialNumber &&
1950 mNetTransitionWakeLock.isHeld()) {
1951 mNetTransitionWakeLock.release();
1952 causedBy = mNetTransitionWakeLockCausedBy;
1953 }
1954 }
1955 if (causedBy != null) {
Wink Savilleed9c02b2010-12-03 12:01:38 -08001956 log("NetTransition Wakelock for " + causedBy + " released by timeout");
Robert Greenwalt14f2ef42010-06-15 12:19:37 -07001957 }
Robert Greenwalt057d5e92010-09-09 14:05:10 -07001958 break;
Robert Greenwalt8dcc28b2010-09-23 10:05:56 -07001959 case EVENT_RESTORE_DEFAULT_NETWORK:
Robert Greenwalt9c75d4a2009-09-27 17:27:04 -07001960 FeatureUser u = (FeatureUser)msg.obj;
1961 u.expire();
Robert Greenwaltd7085fc2010-09-08 15:24:47 -07001962 break;
Robert Greenwalt8dcc28b2010-09-23 10:05:56 -07001963 case EVENT_INET_CONDITION_CHANGE:
1964 {
1965 int netType = msg.arg1;
1966 int condition = msg.arg2;
1967 handleInetConditionChange(netType, condition);
Robert Greenwalt14f2ef42010-06-15 12:19:37 -07001968 break;
Robert Greenwalt8dcc28b2010-09-23 10:05:56 -07001969 }
1970 case EVENT_INET_CONDITION_HOLD_END:
1971 {
1972 int netType = msg.arg1;
1973 int sequence = msg.arg2;
1974 handleInetConditionHoldEnd(netType, sequence);
Robert Greenwaltd7085fc2010-09-08 15:24:47 -07001975 break;
Robert Greenwalt8dcc28b2010-09-23 10:05:56 -07001976 }
1977 case EVENT_SET_NETWORK_PREFERENCE:
1978 {
1979 int preference = msg.arg1;
1980 handleSetNetworkPreference(preference);
1981 break;
1982 }
1983 case EVENT_SET_BACKGROUND_DATA:
1984 {
1985 boolean enabled = (msg.arg1 == ENABLED);
1986 handleSetBackgroundData(enabled);
1987 break;
1988 }
1989 case EVENT_SET_MOBILE_DATA:
1990 {
1991 boolean enabled = (msg.arg1 == ENABLED);
1992 handleSetMobileData(enabled);
1993 break;
1994 }
Robert Greenwalt434203a2010-10-11 16:00:27 -07001995 case EVENT_APPLY_GLOBAL_HTTP_PROXY:
1996 {
1997 handleDeprecatedGlobalHttpProxy();
Robert Greenwaltd55a6b42011-03-25 13:09:25 -07001998 break;
1999 }
2000 case EVENT_SET_DEPENDENCY_MET:
2001 {
2002 boolean met = (msg.arg1 == ENABLED);
2003 handleSetDependencyMet(msg.arg2, met);
2004 break;
Robert Greenwalt434203a2010-10-11 16:00:27 -07002005 }
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002006 }
2007 }
2008 }
Robert Greenwaltd0e18ff2010-01-26 11:40:34 -08002009
2010 // javadoc from interface
Robert Greenwalt5a735062010-03-02 17:25:02 -08002011 public int tether(String iface) {
Robert Greenwaltd0e18ff2010-01-26 11:40:34 -08002012 enforceTetherChangePermission();
Robert Greenwalt5a735062010-03-02 17:25:02 -08002013
2014 if (isTetheringSupported()) {
2015 return mTethering.tether(iface);
2016 } else {
2017 return ConnectivityManager.TETHER_ERROR_UNSUPPORTED;
2018 }
Robert Greenwaltd0e18ff2010-01-26 11:40:34 -08002019 }
2020
2021 // javadoc from interface
Robert Greenwalt5a735062010-03-02 17:25:02 -08002022 public int untether(String iface) {
Robert Greenwaltd0e18ff2010-01-26 11:40:34 -08002023 enforceTetherChangePermission();
Robert Greenwalt5a735062010-03-02 17:25:02 -08002024
2025 if (isTetheringSupported()) {
2026 return mTethering.untether(iface);
2027 } else {
2028 return ConnectivityManager.TETHER_ERROR_UNSUPPORTED;
2029 }
2030 }
2031
2032 // javadoc from interface
2033 public int getLastTetherError(String iface) {
2034 enforceTetherAccessPermission();
2035
2036 if (isTetheringSupported()) {
2037 return mTethering.getLastTetherError(iface);
2038 } else {
2039 return ConnectivityManager.TETHER_ERROR_UNSUPPORTED;
2040 }
Robert Greenwalt2a091d72010-02-11 18:18:40 -08002041 }
2042
2043 // TODO - proper iface API for selection by property, inspection, etc
2044 public String[] getTetherableUsbRegexs() {
2045 enforceTetherAccessPermission();
2046 if (isTetheringSupported()) {
2047 return mTethering.getTetherableUsbRegexs();
2048 } else {
2049 return new String[0];
2050 }
2051 }
2052
2053 public String[] getTetherableWifiRegexs() {
2054 enforceTetherAccessPermission();
2055 if (isTetheringSupported()) {
2056 return mTethering.getTetherableWifiRegexs();
2057 } else {
2058 return new String[0];
2059 }
Robert Greenwaltd0e18ff2010-01-26 11:40:34 -08002060 }
2061
Danica Chang6fdd0c62010-08-11 14:54:43 -07002062 public String[] getTetherableBluetoothRegexs() {
2063 enforceTetherAccessPermission();
2064 if (isTetheringSupported()) {
2065 return mTethering.getTetherableBluetoothRegexs();
2066 } else {
2067 return new String[0];
2068 }
2069 }
2070
Robert Greenwaltd0e18ff2010-01-26 11:40:34 -08002071 // TODO - move iface listing, queries, etc to new module
2072 // javadoc from interface
2073 public String[] getTetherableIfaces() {
Robert Greenwalt2a091d72010-02-11 18:18:40 -08002074 enforceTetherAccessPermission();
Robert Greenwaltd0e18ff2010-01-26 11:40:34 -08002075 return mTethering.getTetherableIfaces();
2076 }
2077
2078 public String[] getTetheredIfaces() {
Robert Greenwalt2a091d72010-02-11 18:18:40 -08002079 enforceTetherAccessPermission();
Robert Greenwaltd0e18ff2010-01-26 11:40:34 -08002080 return mTethering.getTetheredIfaces();
2081 }
Robert Greenwalt2a091d72010-02-11 18:18:40 -08002082
Robert Greenwalt5a735062010-03-02 17:25:02 -08002083 public String[] getTetheringErroredIfaces() {
2084 enforceTetherAccessPermission();
2085 return mTethering.getErroredIfaces();
2086 }
2087
Robert Greenwalt2a091d72010-02-11 18:18:40 -08002088 // if ro.tether.denied = true we default to no tethering
2089 // gservices could set the secure setting to 1 though to enable it on a build where it
2090 // had previously been turned off.
2091 public boolean isTetheringSupported() {
2092 enforceTetherAccessPermission();
2093 int defaultVal = (SystemProperties.get("ro.tether.denied").equals("true") ? 0 : 1);
Robert Greenwaltc9d5fb72010-02-25 12:29:30 -08002094 boolean tetherEnabledInSettings = (Settings.Secure.getInt(mContext.getContentResolver(),
2095 Settings.Secure.TETHER_SUPPORTED, defaultVal) != 0);
2096 return tetherEnabledInSettings && mTetheringConfigValid;
Robert Greenwalt2a091d72010-02-11 18:18:40 -08002097 }
Robert Greenwalt14f2ef42010-06-15 12:19:37 -07002098
2099 // An API NetworkStateTrackers can call when they lose their network.
2100 // This will automatically be cleared after X seconds or a network becomes CONNECTED,
2101 // whichever happens first. The timer is started by the first caller and not
2102 // restarted by subsequent callers.
2103 public void requestNetworkTransitionWakelock(String forWhom) {
2104 enforceConnectivityInternalPermission();
2105 synchronized (this) {
2106 if (mNetTransitionWakeLock.isHeld()) return;
2107 mNetTransitionWakeLockSerialNumber++;
2108 mNetTransitionWakeLock.acquire();
2109 mNetTransitionWakeLockCausedBy = forWhom;
2110 }
2111 mHandler.sendMessageDelayed(mHandler.obtainMessage(
Robert Greenwaltf3331232010-09-24 14:32:21 -07002112 EVENT_CLEAR_NET_TRANSITION_WAKELOCK,
Robert Greenwalt14f2ef42010-06-15 12:19:37 -07002113 mNetTransitionWakeLockSerialNumber, 0),
2114 mNetTransitionWakeLockTimeout);
2115 return;
2116 }
Robert Greenwaltca4306c2010-09-09 13:15:32 -07002117
Robert Greenwaltd7085fc2010-09-08 15:24:47 -07002118 // 100 percent is full good, 0 is full bad.
2119 public void reportInetCondition(int networkType, int percentage) {
Wink Savilleed9c02b2010-12-03 12:01:38 -08002120 if (DBG) log("reportNetworkCondition(" + networkType + ", " + percentage + ")");
Robert Greenwaltd7085fc2010-09-08 15:24:47 -07002121 mContext.enforceCallingOrSelfPermission(
2122 android.Manifest.permission.STATUS_BAR,
2123 "ConnectivityService");
2124
Robert Greenwalt4e8dfef2010-09-20 14:35:25 -07002125 if (DBG) {
2126 int pid = getCallingPid();
2127 int uid = getCallingUid();
2128 String s = pid + "(" + uid + ") reports inet is " +
2129 (percentage > 50 ? "connected" : "disconnected") + " (" + percentage + ") on " +
2130 "network Type " + networkType + " at " + GregorianCalendar.getInstance().getTime();
2131 mInetLog.add(s);
2132 while(mInetLog.size() > INET_CONDITION_LOG_MAX_SIZE) {
2133 mInetLog.remove(0);
2134 }
2135 }
Robert Greenwaltd7085fc2010-09-08 15:24:47 -07002136 mHandler.sendMessage(mHandler.obtainMessage(
Robert Greenwalt8dcc28b2010-09-23 10:05:56 -07002137 EVENT_INET_CONDITION_CHANGE, networkType, percentage));
2138 }
2139
2140 private void handleInetConditionChange(int netType, int condition) {
2141 if (DBG) {
Wink Savilleed9c02b2010-12-03 12:01:38 -08002142 log("Inet connectivity change, net=" +
Robert Greenwalt8dcc28b2010-09-23 10:05:56 -07002143 netType + ", condition=" + condition +
2144 ",mActiveDefaultNetwork=" + mActiveDefaultNetwork);
2145 }
2146 if (mActiveDefaultNetwork == -1) {
Wink Savilleed9c02b2010-12-03 12:01:38 -08002147 if (DBG) log("no active default network - aborting");
Robert Greenwalt8dcc28b2010-09-23 10:05:56 -07002148 return;
2149 }
2150 if (mActiveDefaultNetwork != netType) {
Wink Savilleed9c02b2010-12-03 12:01:38 -08002151 if (DBG) log("given net not default - aborting");
Robert Greenwalt8dcc28b2010-09-23 10:05:56 -07002152 return;
2153 }
2154 mDefaultInetCondition = condition;
2155 int delay;
2156 if (mInetConditionChangeInFlight == false) {
Wink Savilleed9c02b2010-12-03 12:01:38 -08002157 if (DBG) log("starting a change hold");
Robert Greenwalt8dcc28b2010-09-23 10:05:56 -07002158 // setup a new hold to debounce this
2159 if (mDefaultInetCondition > 50) {
2160 delay = Settings.Secure.getInt(mContext.getContentResolver(),
2161 Settings.Secure.INET_CONDITION_DEBOUNCE_UP_DELAY, 500);
2162 } else {
2163 delay = Settings.Secure.getInt(mContext.getContentResolver(),
2164 Settings.Secure.INET_CONDITION_DEBOUNCE_DOWN_DELAY, 3000);
2165 }
2166 mInetConditionChangeInFlight = true;
2167 mHandler.sendMessageDelayed(mHandler.obtainMessage(EVENT_INET_CONDITION_HOLD_END,
2168 mActiveDefaultNetwork, mDefaultConnectionSequence), delay);
2169 } else {
2170 // we've set the new condition, when this hold ends that will get
2171 // picked up
Wink Savilleed9c02b2010-12-03 12:01:38 -08002172 if (DBG) log("currently in hold - not setting new end evt");
Robert Greenwalt8dcc28b2010-09-23 10:05:56 -07002173 }
2174 }
2175
2176 private void handleInetConditionHoldEnd(int netType, int sequence) {
2177 if (DBG) {
Wink Savilleed9c02b2010-12-03 12:01:38 -08002178 log("Inet hold end, net=" + netType +
Robert Greenwalt8dcc28b2010-09-23 10:05:56 -07002179 ", condition =" + mDefaultInetCondition +
2180 ", published condition =" + mDefaultInetConditionPublished);
2181 }
2182 mInetConditionChangeInFlight = false;
2183
2184 if (mActiveDefaultNetwork == -1) {
Wink Savilleed9c02b2010-12-03 12:01:38 -08002185 if (DBG) log("no active default network - aborting");
Robert Greenwalt8dcc28b2010-09-23 10:05:56 -07002186 return;
2187 }
2188 if (mDefaultConnectionSequence != sequence) {
Wink Savilleed9c02b2010-12-03 12:01:38 -08002189 if (DBG) log("event hold for obsolete network - aborting");
Robert Greenwalt8dcc28b2010-09-23 10:05:56 -07002190 return;
2191 }
2192 if (mDefaultInetConditionPublished == mDefaultInetCondition) {
Wink Savilleed9c02b2010-12-03 12:01:38 -08002193 if (DBG) log("no change in condition - aborting");
Robert Greenwalt8dcc28b2010-09-23 10:05:56 -07002194 return;
2195 }
2196 NetworkInfo networkInfo = mNetTrackers[mActiveDefaultNetwork].getNetworkInfo();
2197 if (networkInfo.isConnected() == false) {
Wink Savilleed9c02b2010-12-03 12:01:38 -08002198 if (DBG) log("default network not connected - aborting");
Robert Greenwalt8dcc28b2010-09-23 10:05:56 -07002199 return;
2200 }
2201 mDefaultInetConditionPublished = mDefaultInetCondition;
2202 sendInetConditionBroadcast(networkInfo);
2203 return;
Robert Greenwaltd7085fc2010-09-08 15:24:47 -07002204 }
Robert Greenwalt434203a2010-10-11 16:00:27 -07002205
2206 public synchronized ProxyProperties getProxy() {
2207 if (mGlobalProxy != null) return mGlobalProxy;
2208 if (mDefaultProxy != null) return mDefaultProxy;
2209 return null;
2210 }
2211
2212 public void setGlobalProxy(ProxyProperties proxyProperties) {
2213 enforceChangePermission();
2214 synchronized (mGlobalProxyLock) {
2215 if (proxyProperties == mGlobalProxy) return;
2216 if (proxyProperties != null && proxyProperties.equals(mGlobalProxy)) return;
2217 if (mGlobalProxy != null && mGlobalProxy.equals(proxyProperties)) return;
2218
2219 String host = "";
2220 int port = 0;
2221 String exclList = "";
2222 if (proxyProperties != null && !TextUtils.isEmpty(proxyProperties.getHost())) {
2223 mGlobalProxy = new ProxyProperties(proxyProperties);
2224 host = mGlobalProxy.getHost();
2225 port = mGlobalProxy.getPort();
2226 exclList = mGlobalProxy.getExclusionList();
2227 } else {
2228 mGlobalProxy = null;
2229 }
2230 ContentResolver res = mContext.getContentResolver();
2231 Settings.Secure.putString(res, Settings.Secure.GLOBAL_HTTP_PROXY_HOST, host);
2232 Settings.Secure.putInt(res, Settings.Secure.GLOBAL_HTTP_PROXY_PORT, port);
Robert Greenwaltb7090d62010-12-02 11:31:00 -08002233 Settings.Secure.putString(res, Settings.Secure.GLOBAL_HTTP_PROXY_EXCLUSION_LIST,
Robert Greenwalt434203a2010-10-11 16:00:27 -07002234 exclList);
2235 }
2236
2237 if (mGlobalProxy == null) {
2238 proxyProperties = mDefaultProxy;
2239 }
2240 sendProxyBroadcast(proxyProperties);
2241 }
2242
Robert Greenwaltb7090d62010-12-02 11:31:00 -08002243 private void loadGlobalProxy() {
2244 ContentResolver res = mContext.getContentResolver();
2245 String host = Settings.Secure.getString(res, Settings.Secure.GLOBAL_HTTP_PROXY_HOST);
2246 int port = Settings.Secure.getInt(res, Settings.Secure.GLOBAL_HTTP_PROXY_PORT, 0);
2247 String exclList = Settings.Secure.getString(res,
2248 Settings.Secure.GLOBAL_HTTP_PROXY_EXCLUSION_LIST);
2249 if (!TextUtils.isEmpty(host)) {
2250 ProxyProperties proxyProperties = new ProxyProperties(host, port, exclList);
2251 synchronized (mGlobalProxyLock) {
2252 mGlobalProxy = proxyProperties;
2253 }
2254 }
2255 }
2256
Robert Greenwalt434203a2010-10-11 16:00:27 -07002257 public ProxyProperties getGlobalProxy() {
2258 synchronized (mGlobalProxyLock) {
2259 return mGlobalProxy;
2260 }
2261 }
2262
2263 private void handleApplyDefaultProxy(int type) {
2264 // check if new default - push it out to all VM if so
2265 ProxyProperties proxy = mNetTrackers[type].getLinkProperties().getHttpProxy();
2266 synchronized (this) {
2267 if (mDefaultProxy != null && mDefaultProxy.equals(proxy)) return;
2268 if (mDefaultProxy == proxy) return;
2269 if (!TextUtils.isEmpty(proxy.getHost())) {
2270 mDefaultProxy = proxy;
2271 } else {
2272 mDefaultProxy = null;
2273 }
2274 }
Wink Savilleed9c02b2010-12-03 12:01:38 -08002275 if (DBG) log("changing default proxy to " + proxy);
Robert Greenwalt434203a2010-10-11 16:00:27 -07002276 if ((proxy == null && mGlobalProxy == null) || proxy.equals(mGlobalProxy)) return;
2277 if (mGlobalProxy != null) return;
2278 sendProxyBroadcast(proxy);
2279 }
2280
2281 private void handleDeprecatedGlobalHttpProxy() {
2282 String proxy = Settings.Secure.getString(mContext.getContentResolver(),
2283 Settings.Secure.HTTP_PROXY);
2284 if (!TextUtils.isEmpty(proxy)) {
2285 String data[] = proxy.split(":");
2286 String proxyHost = data[0];
2287 int proxyPort = 8080;
2288 if (data.length > 1) {
2289 try {
2290 proxyPort = Integer.parseInt(data[1]);
2291 } catch (NumberFormatException e) {
2292 return;
2293 }
2294 }
2295 ProxyProperties p = new ProxyProperties(data[0], proxyPort, "");
2296 setGlobalProxy(p);
2297 }
2298 }
2299
2300 private void sendProxyBroadcast(ProxyProperties proxy) {
Robert Greenwalt55985be2010-12-23 15:51:10 -08002301 if (proxy == null) proxy = new ProxyProperties("", 0, "");
Wink Savilleed9c02b2010-12-03 12:01:38 -08002302 log("sending Proxy Broadcast for " + proxy);
Robert Greenwalt434203a2010-10-11 16:00:27 -07002303 Intent intent = new Intent(Proxy.PROXY_CHANGE_ACTION);
Stan Chesnuttb35d67a2011-01-06 11:00:19 -08002304 intent.addFlags(Intent.FLAG_RECEIVER_REPLACE_PENDING |
2305 Intent.FLAG_RECEIVER_REGISTERED_ONLY_BEFORE_BOOT);
Robert Greenwalt434203a2010-10-11 16:00:27 -07002306 intent.putExtra(Proxy.EXTRA_PROXY_INFO, proxy);
Robert Greenwalta2e13392010-12-06 11:29:17 -08002307 mContext.sendStickyBroadcast(intent);
Robert Greenwalt434203a2010-10-11 16:00:27 -07002308 }
2309
2310 private static class SettingsObserver extends ContentObserver {
2311 private int mWhat;
2312 private Handler mHandler;
2313 SettingsObserver(Handler handler, int what) {
2314 super(handler);
2315 mHandler = handler;
2316 mWhat = what;
2317 }
2318
2319 void observe(Context context) {
2320 ContentResolver resolver = context.getContentResolver();
2321 resolver.registerContentObserver(Settings.Secure.getUriFor(
2322 Settings.Secure.HTTP_PROXY), false, this);
2323 }
2324
2325 @Override
2326 public void onChange(boolean selfChange) {
2327 mHandler.obtainMessage(mWhat).sendToTarget();
2328 }
2329 }
Wink Savilleed9c02b2010-12-03 12:01:38 -08002330
Kazuhiro Ondo01758e82011-04-30 20:10:57 -05002331 private void handleTetherIfaceChange(int type) {
2332 String iface = mNetTrackers[type].getLinkProperties().getInterfaceName();
2333
2334 if (isTetheringSupported()) {
2335 mTethering.handleTetherIfaceChange(iface);
2336 }
2337 }
2338
Wink Savilleed9c02b2010-12-03 12:01:38 -08002339 private void log(String s) {
2340 Slog.d(TAG, s);
2341 }
2342
2343 private void loge(String s) {
2344 Slog.e(TAG, s);
2345 }
Wink Saville2b8bcfe2011-02-24 17:58:51 -08002346 int convertFeatureToNetworkType(String feature){
2347 int networkType = -1;
2348 if (TextUtils.equals(feature, Phone.FEATURE_ENABLE_MMS)) {
2349 networkType = ConnectivityManager.TYPE_MOBILE_MMS;
2350 } else if (TextUtils.equals(feature, Phone.FEATURE_ENABLE_SUPL)) {
2351 networkType = ConnectivityManager.TYPE_MOBILE_SUPL;
2352 } else if (TextUtils.equals(feature, Phone.FEATURE_ENABLE_DUN) ||
2353 TextUtils.equals(feature, Phone.FEATURE_ENABLE_DUN_ALWAYS)) {
2354 networkType = ConnectivityManager.TYPE_MOBILE_DUN;
2355 } else if (TextUtils.equals(feature, Phone.FEATURE_ENABLE_HIPRI)) {
2356 networkType = ConnectivityManager.TYPE_MOBILE_HIPRI;
2357 } else if (TextUtils.equals(feature, Phone.FEATURE_ENABLE_FOTA)) {
2358 networkType = ConnectivityManager.TYPE_MOBILE_FOTA;
2359 } else if (TextUtils.equals(feature, Phone.FEATURE_ENABLE_IMS)) {
2360 networkType = ConnectivityManager.TYPE_MOBILE_IMS;
2361 } else if (TextUtils.equals(feature, Phone.FEATURE_ENABLE_CBS)) {
2362 networkType = ConnectivityManager.TYPE_MOBILE_CBS;
2363 }
2364 return networkType;
2365 }
Jeff Sharkeyc006f1a2011-05-19 17:12:49 -07002366
2367 private static <T> T checkNotNull(T value, String message) {
2368 if (value == null) {
2369 throw new NullPointerException(message);
2370 }
2371 return value;
2372 }
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002373}