blob: 07855d960a4d902dade64c84e233393f138d840f [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 Sharkey1a303952011-06-16 13:04:20 -070019import static android.Manifest.permission.MANAGE_NETWORK_POLICY;
Jeff Sharkeyc006f1a2011-05-19 17:12:49 -070020import static android.net.ConnectivityManager.isNetworkTypeValid;
21import static android.net.NetworkPolicyManager.RULE_ALLOW_ALL;
22import static android.net.NetworkPolicyManager.RULE_REJECT_PAID;
23
Jaikumar 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;
Jeff Sharkeyd2a45872011-05-28 20:56:34 -070041import android.net.NetworkState;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080042import android.net.NetworkStateTracker;
Robert Greenwalt585ac0f2010-08-27 09:24:29 -070043import android.net.NetworkUtils;
Robert Greenwalt434203a2010-10-11 16:00:27 -070044import android.net.Proxy;
45import android.net.ProxyProperties;
Robert Greenwaltaa70f102011-04-28 14:28:50 -070046import android.net.RouteInfo;
Hung-ying Tyan6b818de2011-01-19 16:48:38 +080047import android.net.vpn.VpnManager;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080048import android.net.wifi.WifiStateTracker;
49import android.os.Binder;
Mike Lockwoodda8bb742011-05-28 13:24:04 -040050import android.os.FileUtils;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080051import android.os.Handler;
Wink Savillebb08caf2010-09-02 19:23:52 -070052import android.os.HandlerThread;
Robert Greenwalt42acef32009-08-12 16:08:25 -070053import android.os.IBinder;
Chia-chi Yehc9338302011-05-11 16:35:13 -070054import android.os.INetworkManagementService;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080055import android.os.Looper;
56import android.os.Message;
Chia-chi Yehff3bdca2011-05-23 17:26:46 -070057import android.os.ParcelFileDescriptor;
Robert Greenwalt14f2ef42010-06-15 12:19:37 -070058import android.os.PowerManager;
Robert Greenwalt42acef32009-08-12 16:08:25 -070059import android.os.RemoteException;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080060import android.os.ServiceManager;
61import android.os.SystemProperties;
62import android.provider.Settings;
Robert Greenwalt42acef32009-08-12 16:08:25 -070063import android.text.TextUtils;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080064import android.util.EventLog;
Joe Onorato8a9b2202010-02-26 18:56:32 -080065import android.util.Slog;
Jeff Sharkeyc006f1a2011-05-19 17:12:49 -070066import android.util.SparseIntArray;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080067
Chia-chi Yeh04ba25c2011-06-15 17:07:27 -070068import com.android.internal.net.VpnConfig;
Robert Greenwalt42acef32009-08-12 16:08:25 -070069import com.android.internal.telephony.Phone;
Robert Greenwaltd0e18ff2010-01-26 11:40:34 -080070import com.android.server.connectivity.Tethering;
Chia-chi Yehff3bdca2011-05-23 17:26:46 -070071import com.android.server.connectivity.Vpn;
72
Jeff Sharkeyd2a45872011-05-28 20:56:34 -070073import com.google.android.collect.Lists;
Robert Greenwaltd0e18ff2010-01-26 11:40:34 -080074
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080075import java.io.FileDescriptor;
Irfan Sheriffd649c122010-06-09 15:39:36 -070076import java.io.IOException;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080077import java.io.PrintWriter;
Robert Greenwalt47f69fe2010-06-15 15:43:39 -070078import java.net.InetAddress;
79import java.net.UnknownHostException;
Robert Greenwalt42acef32009-08-12 16:08:25 -070080import java.util.ArrayList;
Robert Greenwalt47f69fe2010-06-15 15:43:39 -070081import java.util.Collection;
Robert Greenwalt4e8dfef2010-09-20 14:35:25 -070082import java.util.GregorianCalendar;
Robert Greenwalt42acef32009-08-12 16:08:25 -070083import java.util.List;
Jeff Sharkeyc006f1a2011-05-19 17:12:49 -070084import java.util.concurrent.atomic.AtomicBoolean;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080085
86/**
87 * @hide
88 */
89public class ConnectivityService extends IConnectivityManager.Stub {
90
Robert Greenwaltba175a52010-10-05 19:12:26 -070091 private static final boolean DBG = true;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080092 private static final String TAG = "ConnectivityService";
93
Jeff Sharkeyc006f1a2011-05-19 17:12:49 -070094 private static final boolean LOGD_RULES = false;
95
Robert Greenwalt42acef32009-08-12 16:08:25 -070096 // how long to wait before switching back to a radio's default network
97 private static final int RESTORE_DEFAULT_NETWORK_DELAY = 1 * 60 * 1000;
98 // system property that can override the above value
99 private static final String NETWORK_RESTORE_DELAY_PROP_NAME =
100 "android.telephony.apn-restore";
101
Robert Greenwaltf43396c2011-05-06 17:10:53 -0700102 // used in recursive route setting to add gateways for the host for which
103 // a host route was requested.
104 private static final int MAX_HOSTROUTE_CYCLE_COUNT = 10;
105
Robert Greenwaltd0e18ff2010-01-26 11:40:34 -0800106 private Tethering mTethering;
Robert Greenwaltc9d5fb72010-02-25 12:29:30 -0800107 private boolean mTetheringConfigValid = false;
Robert Greenwaltd0e18ff2010-01-26 11:40:34 -0800108
Chia-chi Yehff3bdca2011-05-23 17:26:46 -0700109 private Vpn mVpn;
110
Jeff Sharkeyc006f1a2011-05-19 17:12:49 -0700111 /** Currently active network rules by UID. */
112 private SparseIntArray mUidRules = new SparseIntArray();
113
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800114 /**
115 * Sometimes we want to refer to the individual network state
116 * trackers separately, and sometimes we just want to treat them
117 * abstractly.
118 */
119 private NetworkStateTracker mNetTrackers[];
Robert Greenwalt42acef32009-08-12 16:08:25 -0700120
121 /**
122 * A per Net list of the PID's that requested access to the net
123 * used both as a refcount and for per-PID DNS selection
124 */
125 private List mNetRequestersPids[];
126
Irfan Sheriffa2a1b912010-06-07 09:03:04 -0700127 private WifiWatchdogService mWifiWatchdogService;
128
Robert Greenwalt42acef32009-08-12 16:08:25 -0700129 // priority order of the nettrackers
130 // (excluding dynamically set mNetworkPreference)
131 // TODO - move mNetworkTypePreference into this
132 private int[] mPriorityList;
133
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800134 private Context mContext;
135 private int mNetworkPreference;
Robert Greenwalt42acef32009-08-12 16:08:25 -0700136 private int mActiveDefaultNetwork = -1;
Robert Greenwaltd7085fc2010-09-08 15:24:47 -0700137 // 0 is full bad, 100 is full good
138 private int mDefaultInetCondition = 0;
139 private int mDefaultInetConditionPublished = 0;
140 private boolean mInetConditionChangeInFlight = false;
141 private int mDefaultConnectionSequence = 0;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800142
143 private int mNumDnsEntries;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800144
145 private boolean mTestMode;
Joe Onorato00092872010-09-01 21:18:22 -0700146 private static ConnectivityService sServiceInstance;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800147
Robert Greenwaltd825ea42010-12-29 16:15:02 -0800148 private AtomicBoolean mBackgroundDataEnabled = new AtomicBoolean(true);
149
Robert Greenwalt59b1a4e2011-05-10 15:05:02 -0700150 private INetworkManagementService mNetd;
Jeff Sharkeyc006f1a2011-05-19 17:12:49 -0700151 private INetworkPolicyManager mPolicyManager;
Robert Greenwalt59b1a4e2011-05-10 15:05:02 -0700152
Robert Greenwalt8dcc28b2010-09-23 10:05:56 -0700153 private static final int ENABLED = 1;
154 private static final int DISABLED = 0;
155
156 // Share the event space with NetworkStateTracker (which can't see this
157 // internal class but sends us events). If you change these, change
158 // NetworkStateTracker.java too.
159 private static final int MIN_NETWORK_STATE_TRACKER_EVENT = 1;
160 private static final int MAX_NETWORK_STATE_TRACKER_EVENT = 100;
161
162 /**
163 * used internally as a delayed event to make us switch back to the
164 * default network
165 */
166 private static final int EVENT_RESTORE_DEFAULT_NETWORK =
167 MAX_NETWORK_STATE_TRACKER_EVENT + 1;
168
169 /**
170 * used internally to change our mobile data enabled flag
171 */
172 private static final int EVENT_CHANGE_MOBILE_DATA_ENABLED =
173 MAX_NETWORK_STATE_TRACKER_EVENT + 2;
174
175 /**
176 * used internally to change our network preference setting
177 * arg1 = networkType to prefer
178 */
179 private static final int EVENT_SET_NETWORK_PREFERENCE =
180 MAX_NETWORK_STATE_TRACKER_EVENT + 3;
181
182 /**
183 * used internally to synchronize inet condition reports
184 * arg1 = networkType
185 * arg2 = condition (0 bad, 100 good)
186 */
187 private static final int EVENT_INET_CONDITION_CHANGE =
188 MAX_NETWORK_STATE_TRACKER_EVENT + 4;
189
190 /**
191 * used internally to mark the end of inet condition hold periods
192 * arg1 = networkType
193 */
194 private static final int EVENT_INET_CONDITION_HOLD_END =
195 MAX_NETWORK_STATE_TRACKER_EVENT + 5;
196
197 /**
198 * used internally to set the background data preference
199 * arg1 = TRUE for enabled, FALSE for disabled
200 */
201 private static final int EVENT_SET_BACKGROUND_DATA =
202 MAX_NETWORK_STATE_TRACKER_EVENT + 6;
203
204 /**
205 * used internally to set enable/disable cellular data
206 * arg1 = ENBALED or DISABLED
207 */
208 private static final int EVENT_SET_MOBILE_DATA =
209 MAX_NETWORK_STATE_TRACKER_EVENT + 7;
210
Robert Greenwaltf3331232010-09-24 14:32:21 -0700211 /**
212 * used internally to clear a wakelock when transitioning
213 * from one net to another
214 */
215 private static final int EVENT_CLEAR_NET_TRANSITION_WAKELOCK =
216 MAX_NETWORK_STATE_TRACKER_EVENT + 8;
217
Robert Greenwalt434203a2010-10-11 16:00:27 -0700218 /**
219 * used internally to reload global proxy settings
220 */
221 private static final int EVENT_APPLY_GLOBAL_HTTP_PROXY =
222 MAX_NETWORK_STATE_TRACKER_EVENT + 9;
223
Robert Greenwaltd55a6b42011-03-25 13:09:25 -0700224 /**
225 * used internally to set external dependency met/unmet
226 * arg1 = ENABLED (met) or DISABLED (unmet)
227 * arg2 = NetworkType
228 */
229 private static final int EVENT_SET_DEPENDENCY_MET =
230 MAX_NETWORK_STATE_TRACKER_EVENT + 10;
231
Robert Greenwalt42acef32009-08-12 16:08:25 -0700232 private Handler mHandler;
233
234 // list of DeathRecipients used to make sure features are turned off when
235 // a process dies
236 private List mFeatureUsers;
237
Mike Lockwood0f79b542009-08-14 14:18:49 -0400238 private boolean mSystemReady;
Dianne Hackborn1c633fc2009-12-08 19:45:14 -0800239 private Intent mInitialBroadcast;
Mike Lockwood0f79b542009-08-14 14:18:49 -0400240
Robert Greenwalt14f2ef42010-06-15 12:19:37 -0700241 private PowerManager.WakeLock mNetTransitionWakeLock;
242 private String mNetTransitionWakeLockCausedBy = "";
243 private int mNetTransitionWakeLockSerialNumber;
244 private int mNetTransitionWakeLockTimeout;
245
Robert Greenwalte90aa5e2010-09-01 11:34:05 -0700246 private InetAddress mDefaultDns;
247
Robert Greenwalt4e8dfef2010-09-20 14:35:25 -0700248 // used in DBG mode to track inet condition reports
249 private static final int INET_CONDITION_LOG_MAX_SIZE = 15;
250 private ArrayList mInetLog;
251
Robert Greenwalt434203a2010-10-11 16:00:27 -0700252 // track the current default http proxy - tell the world if we get a new one (real change)
253 private ProxyProperties mDefaultProxy = null;
254 // track the global proxy.
255 private ProxyProperties mGlobalProxy = null;
256 private final Object mGlobalProxyLock = new Object();
257
258 private SettingsObserver mSettingsObserver;
259
Robert Greenwaltd55a6b42011-03-25 13:09:25 -0700260 NetworkConfig[] mNetConfigs;
Robert Greenwalt5154ae762009-10-30 14:17:42 -0700261 int mNetworksDefined;
Robert Greenwalt42acef32009-08-12 16:08:25 -0700262
Robert Greenwalt511288a2009-12-07 11:33:18 -0800263 private static class RadioAttributes {
Robert Greenwalt42acef32009-08-12 16:08:25 -0700264 public int mSimultaneity;
265 public int mType;
266 public RadioAttributes(String init) {
267 String fragments[] = init.split(",");
Robert Greenwalt5154ae762009-10-30 14:17:42 -0700268 mType = Integer.parseInt(fragments[0]);
269 mSimultaneity = Integer.parseInt(fragments[1]);
Robert Greenwalt42acef32009-08-12 16:08:25 -0700270 }
271 }
272 RadioAttributes[] mRadioAttributes;
273
Jeff Sharkeyc006f1a2011-05-19 17:12:49 -0700274 public ConnectivityService(
275 Context context, INetworkManagementService netd, INetworkPolicyManager policyManager) {
Wink Savilleed9c02b2010-12-03 12:01:38 -0800276 if (DBG) log("ConnectivityService starting up");
Robert Greenwaltde8383c2010-01-14 17:47:58 -0800277
Wink Savillebb08caf2010-09-02 19:23:52 -0700278 HandlerThread handlerThread = new HandlerThread("ConnectivityServiceThread");
279 handlerThread.start();
280 mHandler = new MyHandler(handlerThread.getLooper());
281
Robert Greenwaltd825ea42010-12-29 16:15:02 -0800282 mBackgroundDataEnabled.set(Settings.Secure.getInt(context.getContentResolver(),
283 Settings.Secure.BACKGROUND_DATA, 1) == 1);
284
Robert Greenwaltde8383c2010-01-14 17:47:58 -0800285 // setup our unique device name
Robert Greenwalt733c6292010-12-06 09:30:17 -0800286 if (TextUtils.isEmpty(SystemProperties.get("net.hostname"))) {
287 String id = Settings.Secure.getString(context.getContentResolver(),
288 Settings.Secure.ANDROID_ID);
289 if (id != null && id.length() > 0) {
290 String name = new String("android_").concat(id);
291 SystemProperties.set("net.hostname", name);
292 }
Robert Greenwaltde8383c2010-01-14 17:47:58 -0800293 }
294
Robert Greenwalte90aa5e2010-09-01 11:34:05 -0700295 // read our default dns server ip
296 String dns = Settings.Secure.getString(context.getContentResolver(),
297 Settings.Secure.DEFAULT_DNS_SERVER);
298 if (dns == null || dns.length() == 0) {
299 dns = context.getResources().getString(
300 com.android.internal.R.string.config_default_dns_server);
301 }
302 try {
Robert Greenwalte5903732011-02-22 16:00:42 -0800303 mDefaultDns = NetworkUtils.numericToInetAddress(dns);
304 } catch (IllegalArgumentException e) {
Wink Savilleed9c02b2010-12-03 12:01:38 -0800305 loge("Error setting defaultDns using " + dns);
Robert Greenwalte90aa5e2010-09-01 11:34:05 -0700306 }
307
Jeff Sharkeyc006f1a2011-05-19 17:12:49 -0700308 mContext = checkNotNull(context, "missing Context");
309 mNetd = checkNotNull(netd, "missing INetworkManagementService");
310 mPolicyManager = checkNotNull(policyManager, "missing INetworkPolicyManager");
Robert Greenwalt14f2ef42010-06-15 12:19:37 -0700311
Jeff Sharkeyc006f1a2011-05-19 17:12:49 -0700312 try {
313 mPolicyManager.registerListener(mPolicyListener);
314 } catch (RemoteException e) {
315 // ouch, no rules updates means some processes may never get network
316 Slog.e(TAG, "unable to register INetworkPolicyListener", e);
317 }
318
319 final PowerManager powerManager = (PowerManager) context.getSystemService(
320 Context.POWER_SERVICE);
Robert Greenwalt14f2ef42010-06-15 12:19:37 -0700321 mNetTransitionWakeLock = powerManager.newWakeLock(PowerManager.PARTIAL_WAKE_LOCK, TAG);
322 mNetTransitionWakeLockTimeout = mContext.getResources().getInteger(
323 com.android.internal.R.integer.config_networkTransitionTimeout);
324
Robert Greenwalt42acef32009-08-12 16:08:25 -0700325 mNetTrackers = new NetworkStateTracker[
326 ConnectivityManager.MAX_NETWORK_TYPE+1];
Robert Greenwalt86e9e552009-07-16 17:21:39 -0700327
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800328 mNetworkPreference = getPersistedNetworkPreference();
Robert Greenwalt86e9e552009-07-16 17:21:39 -0700329
Robert Greenwalt5154ae762009-10-30 14:17:42 -0700330 mRadioAttributes = new RadioAttributes[ConnectivityManager.MAX_RADIO_TYPE+1];
Robert Greenwaltd55a6b42011-03-25 13:09:25 -0700331 mNetConfigs = new NetworkConfig[ConnectivityManager.MAX_NETWORK_TYPE+1];
Robert Greenwalt5154ae762009-10-30 14:17:42 -0700332
Robert Greenwalt42acef32009-08-12 16:08:25 -0700333 // Load device network attributes from resources
Robert Greenwalt42acef32009-08-12 16:08:25 -0700334 String[] raStrings = context.getResources().getStringArray(
335 com.android.internal.R.array.radioAttributes);
Robert Greenwalt5154ae762009-10-30 14:17:42 -0700336 for (String raString : raStrings) {
337 RadioAttributes r = new RadioAttributes(raString);
338 if (r.mType > ConnectivityManager.MAX_RADIO_TYPE) {
Wink Savilleed9c02b2010-12-03 12:01:38 -0800339 loge("Error in radioAttributes - ignoring attempt to define type " + r.mType);
Robert Greenwalt5154ae762009-10-30 14:17:42 -0700340 continue;
341 }
342 if (mRadioAttributes[r.mType] != null) {
Wink Savilleed9c02b2010-12-03 12:01:38 -0800343 loge("Error in radioAttributes - ignoring attempt to redefine type " +
Robert Greenwalt5154ae762009-10-30 14:17:42 -0700344 r.mType);
345 continue;
346 }
Robert Greenwalt42acef32009-08-12 16:08:25 -0700347 mRadioAttributes[r.mType] = r;
348 }
349
Robert Greenwalt5154ae762009-10-30 14:17:42 -0700350 String[] naStrings = context.getResources().getStringArray(
351 com.android.internal.R.array.networkAttributes);
352 for (String naString : naStrings) {
353 try {
Robert Greenwaltd55a6b42011-03-25 13:09:25 -0700354 NetworkConfig n = new NetworkConfig(naString);
Wink Saville975c8482011-04-07 14:23:45 -0700355 if (n.type > ConnectivityManager.MAX_NETWORK_TYPE) {
Wink Savilleed9c02b2010-12-03 12:01:38 -0800356 loge("Error in networkAttributes - ignoring attempt to define type " +
Wink Saville975c8482011-04-07 14:23:45 -0700357 n.type);
Robert Greenwalt5154ae762009-10-30 14:17:42 -0700358 continue;
Robert Greenwalt42acef32009-08-12 16:08:25 -0700359 }
Wink Saville975c8482011-04-07 14:23:45 -0700360 if (mNetConfigs[n.type] != null) {
Wink Savilleed9c02b2010-12-03 12:01:38 -0800361 loge("Error in networkAttributes - ignoring attempt to redefine type " +
Wink Saville975c8482011-04-07 14:23:45 -0700362 n.type);
Robert Greenwalt5154ae762009-10-30 14:17:42 -0700363 continue;
364 }
Wink Saville975c8482011-04-07 14:23:45 -0700365 if (mRadioAttributes[n.radio] == null) {
Wink Savilleed9c02b2010-12-03 12:01:38 -0800366 loge("Error in networkAttributes - ignoring attempt to use undefined " +
Wink Saville975c8482011-04-07 14:23:45 -0700367 "radio " + n.radio + " in network type " + n.type);
Robert Greenwalt5154ae762009-10-30 14:17:42 -0700368 continue;
369 }
Wink Saville975c8482011-04-07 14:23:45 -0700370 mNetConfigs[n.type] = n;
Robert Greenwalt5154ae762009-10-30 14:17:42 -0700371 mNetworksDefined++;
372 } catch(Exception e) {
373 // ignore it - leave the entry null
Robert Greenwalt42acef32009-08-12 16:08:25 -0700374 }
375 }
376
Robert Greenwalt5154ae762009-10-30 14:17:42 -0700377 // high priority first
378 mPriorityList = new int[mNetworksDefined];
379 {
380 int insertionPoint = mNetworksDefined-1;
381 int currentLowest = 0;
382 int nextLowest = 0;
383 while (insertionPoint > -1) {
Robert Greenwaltd55a6b42011-03-25 13:09:25 -0700384 for (NetworkConfig na : mNetConfigs) {
Robert Greenwalt5154ae762009-10-30 14:17:42 -0700385 if (na == null) continue;
Wink Saville975c8482011-04-07 14:23:45 -0700386 if (na.priority < currentLowest) continue;
387 if (na.priority > currentLowest) {
388 if (na.priority < nextLowest || nextLowest == 0) {
389 nextLowest = na.priority;
Robert Greenwalt5154ae762009-10-30 14:17:42 -0700390 }
391 continue;
392 }
Wink Saville975c8482011-04-07 14:23:45 -0700393 mPriorityList[insertionPoint--] = na.type;
Robert Greenwalt5154ae762009-10-30 14:17:42 -0700394 }
395 currentLowest = nextLowest;
396 nextLowest = 0;
397 }
398 }
399
400 mNetRequestersPids = new ArrayList[ConnectivityManager.MAX_NETWORK_TYPE+1];
401 for (int i : mPriorityList) {
Robert Greenwalt42acef32009-08-12 16:08:25 -0700402 mNetRequestersPids[i] = new ArrayList();
403 }
404
405 mFeatureUsers = new ArrayList();
406
Robert Greenwalt5154ae762009-10-30 14:17:42 -0700407 mNumDnsEntries = 0;
408
409 mTestMode = SystemProperties.get("cm.test.mode").equals("true")
410 && SystemProperties.get("ro.build.type").equals("eng");
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800411 /*
412 * Create the network state trackers for Wi-Fi and mobile
413 * data. Maybe this could be done with a factory class,
414 * but it's not clear that it's worth it, given that
415 * the number of different network types is not going
416 * to change very often.
417 */
Robert Greenwalt5154ae762009-10-30 14:17:42 -0700418 for (int netType : mPriorityList) {
Wink Saville975c8482011-04-07 14:23:45 -0700419 switch (mNetConfigs[netType].radio) {
Robert Greenwalt5154ae762009-10-30 14:17:42 -0700420 case ConnectivityManager.TYPE_WIFI:
Wink Savilleed9c02b2010-12-03 12:01:38 -0800421 if (DBG) log("Starting Wifi Service.");
Wink Savillec7a98342010-08-13 16:11:42 -0700422 WifiStateTracker wst = new WifiStateTracker();
Irfan Sheriff0d255342010-07-28 09:35:20 -0700423 WifiService wifiService = new WifiService(context);
Robert Greenwalt5154ae762009-10-30 14:17:42 -0700424 ServiceManager.addService(Context.WIFI_SERVICE, wifiService);
Irfan Sheriff0d255342010-07-28 09:35:20 -0700425 wifiService.checkAndStartWifi();
Robert Greenwalt5154ae762009-10-30 14:17:42 -0700426 mNetTrackers[ConnectivityManager.TYPE_WIFI] = wst;
Wink Savillec7a98342010-08-13 16:11:42 -0700427 wst.startMonitoring(context, mHandler);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800428
Irfan Sheriffa2a1b912010-06-07 09:03:04 -0700429 //TODO: as part of WWS refactor, create only when needed
Irfan Sheriff0d255342010-07-28 09:35:20 -0700430 mWifiWatchdogService = new WifiWatchdogService(context);
Irfan Sheriffa2a1b912010-06-07 09:03:04 -0700431
Robert Greenwalt5154ae762009-10-30 14:17:42 -0700432 break;
433 case ConnectivityManager.TYPE_MOBILE:
Wink Savillec7a98342010-08-13 16:11:42 -0700434 mNetTrackers[netType] = new MobileDataStateTracker(netType,
Wink Saville975c8482011-04-07 14:23:45 -0700435 mNetConfigs[netType].name);
Wink Savillec7a98342010-08-13 16:11:42 -0700436 mNetTrackers[netType].startMonitoring(context, mHandler);
Robert Greenwalt5154ae762009-10-30 14:17:42 -0700437 break;
Robert Greenwaltda3d5e62010-12-06 13:56:24 -0800438 case ConnectivityManager.TYPE_DUMMY:
439 mNetTrackers[netType] = new DummyDataStateTracker(netType,
Wink Saville975c8482011-04-07 14:23:45 -0700440 mNetConfigs[netType].name);
Robert Greenwaltda3d5e62010-12-06 13:56:24 -0800441 mNetTrackers[netType].startMonitoring(context, mHandler);
442 break;
Jaikumar Ganesh15c74392010-12-21 22:31:44 -0800443 case ConnectivityManager.TYPE_BLUETOOTH:
444 mNetTrackers[netType] = BluetoothTetheringDataTracker.getInstance();
445 mNetTrackers[netType].startMonitoring(context, mHandler);
446 break;
Benoit Goby19970692010-12-22 14:29:40 -0800447 case ConnectivityManager.TYPE_ETHERNET:
448 mNetTrackers[netType] = EthernetDataTracker.getInstance();
449 mNetTrackers[netType].startMonitoring(context, mHandler);
450 break;
Robert Greenwalt5154ae762009-10-30 14:17:42 -0700451 default:
Wink Savilleed9c02b2010-12-03 12:01:38 -0800452 loge("Trying to create a DataStateTracker for an unknown radio type " +
Wink Saville975c8482011-04-07 14:23:45 -0700453 mNetConfigs[netType].radio);
Robert Greenwalt5154ae762009-10-30 14:17:42 -0700454 continue;
455 }
456 }
Robert Greenwaltd0e18ff2010-01-26 11:40:34 -0800457
Chia-chi Yehc9338302011-05-11 16:35:13 -0700458 IBinder b = ServiceManager.getService(Context.NETWORKMANAGEMENT_SERVICE);
459 INetworkManagementService nmService = INetworkManagementService.Stub.asInterface(b);
460
461 mTethering = new Tethering(mContext, nmService, mHandler.getLooper());
Robert Greenwaltc9d5fb72010-02-25 12:29:30 -0800462 mTetheringConfigValid = (((mNetTrackers[ConnectivityManager.TYPE_MOBILE_DUN] != null) ||
463 !mTethering.isDunRequired()) &&
464 (mTethering.getTetherableUsbRegexs().length != 0 ||
Danica Chang6fdd0c62010-08-11 14:54:43 -0700465 mTethering.getTetherableWifiRegexs().length != 0 ||
466 mTethering.getTetherableBluetoothRegexs().length != 0) &&
Robert Greenwaltc9d5fb72010-02-25 12:29:30 -0800467 mTethering.getUpstreamIfaceRegexs().length != 0);
468
Chia-chi Yehff3bdca2011-05-23 17:26:46 -0700469 mVpn = new Vpn(mContext, new VpnCallback());
470
Chia-chi Yeh008ff392011-05-23 15:08:29 -0700471 try {
472 nmService.registerObserver(mTethering);
Chia-chi Yehff3bdca2011-05-23 17:26:46 -0700473 nmService.registerObserver(mVpn);
Chia-chi Yeh008ff392011-05-23 15:08:29 -0700474 } catch (RemoteException e) {
475 loge("Error registering observer :" + e);
476 }
477
Robert Greenwalt4e8dfef2010-09-20 14:35:25 -0700478 if (DBG) {
479 mInetLog = new ArrayList();
480 }
Robert Greenwalt434203a2010-10-11 16:00:27 -0700481
482 mSettingsObserver = new SettingsObserver(mHandler, EVENT_APPLY_GLOBAL_HTTP_PROXY);
483 mSettingsObserver.observe(mContext);
Robert Greenwaltb7090d62010-12-02 11:31:00 -0800484
485 loadGlobalProxy();
Hung-ying Tyan6b818de2011-01-19 16:48:38 +0800486
487 VpnManager.startVpnService(context);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800488 }
489
Robert Greenwalt5154ae762009-10-30 14:17:42 -0700490
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800491 /**
Robert Greenwalt86e9e552009-07-16 17:21:39 -0700492 * Sets the preferred network.
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800493 * @param preference the new preference
494 */
Robert Greenwalt8dcc28b2010-09-23 10:05:56 -0700495 public void setNetworkPreference(int preference) {
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800496 enforceChangePermission();
Robert Greenwalt8dcc28b2010-09-23 10:05:56 -0700497
498 mHandler.sendMessage(mHandler.obtainMessage(EVENT_SET_NETWORK_PREFERENCE, preference, 0));
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800499 }
500
501 public int getNetworkPreference() {
502 enforceAccessPermission();
Robert Greenwalt8dcc28b2010-09-23 10:05:56 -0700503 int preference;
504 synchronized(this) {
505 preference = mNetworkPreference;
506 }
507 return preference;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800508 }
509
Robert Greenwalt8dcc28b2010-09-23 10:05:56 -0700510 private void handleSetNetworkPreference(int preference) {
511 if (ConnectivityManager.isNetworkTypeValid(preference) &&
Robert Greenwaltd55a6b42011-03-25 13:09:25 -0700512 mNetConfigs[preference] != null &&
513 mNetConfigs[preference].isDefault()) {
Robert Greenwalt8dcc28b2010-09-23 10:05:56 -0700514 if (mNetworkPreference != preference) {
515 final ContentResolver cr = mContext.getContentResolver();
516 Settings.Secure.putInt(cr, Settings.Secure.NETWORK_PREFERENCE, preference);
517 synchronized(this) {
518 mNetworkPreference = preference;
519 }
520 enforcePreference();
521 }
522 }
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800523 }
Robert Greenwalt86e9e552009-07-16 17:21:39 -0700524
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800525 private int getPersistedNetworkPreference() {
526 final ContentResolver cr = mContext.getContentResolver();
527
528 final int networkPrefSetting = Settings.Secure
529 .getInt(cr, Settings.Secure.NETWORK_PREFERENCE, -1);
530 if (networkPrefSetting != -1) {
531 return networkPrefSetting;
532 }
533
534 return ConnectivityManager.DEFAULT_NETWORK_PREFERENCE;
535 }
Robert Greenwalt86e9e552009-07-16 17:21:39 -0700536
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800537 /**
Robert Greenwalt86e9e552009-07-16 17:21:39 -0700538 * Make the state of network connectivity conform to the preference settings
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800539 * In this method, we only tear down a non-preferred network. Establishing
540 * a connection to the preferred network is taken care of when we handle
541 * the disconnect event from the non-preferred network
542 * (see {@link #handleDisconnect(NetworkInfo)}).
543 */
544 private void enforcePreference() {
Robert Greenwalt42acef32009-08-12 16:08:25 -0700545 if (mNetTrackers[mNetworkPreference].getNetworkInfo().isConnected())
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800546 return;
547
Robert Greenwalt42acef32009-08-12 16:08:25 -0700548 if (!mNetTrackers[mNetworkPreference].isAvailable())
549 return;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800550
Robert Greenwalt42acef32009-08-12 16:08:25 -0700551 for (int t=0; t <= ConnectivityManager.MAX_RADIO_TYPE; t++) {
Robert Greenwalt5154ae762009-10-30 14:17:42 -0700552 if (t != mNetworkPreference && mNetTrackers[t] != null &&
Robert Greenwalt42acef32009-08-12 16:08:25 -0700553 mNetTrackers[t].getNetworkInfo().isConnected()) {
Robert Greenwaltec9fe462009-08-20 15:25:14 -0700554 if (DBG) {
Wink Savilleed9c02b2010-12-03 12:01:38 -0800555 log("tearing down " + mNetTrackers[t].getNetworkInfo() +
Robert Greenwaltec9fe462009-08-20 15:25:14 -0700556 " in enforcePreference");
557 }
Robert Greenwalt42acef32009-08-12 16:08:25 -0700558 teardown(mNetTrackers[t]);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800559 }
560 }
561 }
562
563 private boolean teardown(NetworkStateTracker netTracker) {
564 if (netTracker.teardown()) {
565 netTracker.setTeardownRequested(true);
566 return true;
567 } else {
568 return false;
569 }
570 }
571
572 /**
Jeff Sharkeyc006f1a2011-05-19 17:12:49 -0700573 * Check if UID is blocked from using the given {@link NetworkInfo}.
574 */
575 private boolean isNetworkBlocked(NetworkInfo info, int uid) {
576 synchronized (mUidRules) {
Jeff Sharkeyd2a45872011-05-28 20:56:34 -0700577 // TODO: expand definition of "paid" network to cover tethered or
578 // paid hotspot use cases.
579 final boolean networkIsPaid = info.getType() != ConnectivityManager.TYPE_WIFI;
580 final int uidRules = mUidRules.get(uid, RULE_ALLOW_ALL);
581
582 if (networkIsPaid && (uidRules & RULE_REJECT_PAID) != 0) {
583 return true;
584 }
585
586 // no restrictive rules; network is visible
587 return false;
Jeff Sharkeyc006f1a2011-05-19 17:12:49 -0700588 }
589 }
590
591 /**
Jeff Sharkeyd2a45872011-05-28 20:56:34 -0700592 * Return a filtered version of the given {@link NetworkInfo}, potentially
593 * marked {@link DetailedState#BLOCKED} based on
594 * {@link #isNetworkBlocked(NetworkInfo, int)}.
Jeff Sharkeyc006f1a2011-05-19 17:12:49 -0700595 */
Jeff Sharkeyd2a45872011-05-28 20:56:34 -0700596 private NetworkInfo filterNetworkInfo(NetworkInfo info, int uid) {
597 if (isNetworkBlocked(info, uid)) {
598 // network is blocked; clone and override state
599 info = new NetworkInfo(info);
600 info.setDetailedState(DetailedState.BLOCKED, null, null);
Jeff Sharkeyc006f1a2011-05-19 17:12:49 -0700601 }
Jeff Sharkeyd2a45872011-05-28 20:56:34 -0700602 return info;
Jeff Sharkeyc006f1a2011-05-19 17:12:49 -0700603 }
604
605 /**
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800606 * Return NetworkInfo for the active (i.e., connected) network interface.
607 * It is assumed that at most one network is active at a time. If more
608 * than one is active, it is indeterminate which will be returned.
Robert Greenwalt86e9e552009-07-16 17:21:39 -0700609 * @return the info for the active network, or {@code null} if none is
610 * active
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800611 */
Jeff Sharkeyc006f1a2011-05-19 17:12:49 -0700612 @Override
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800613 public NetworkInfo getActiveNetworkInfo() {
Jeff Sharkeyc006f1a2011-05-19 17:12:49 -0700614 enforceAccessPermission();
615 final int uid = Binder.getCallingUid();
616 return getNetworkInfo(mActiveDefaultNetwork, uid);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800617 }
618
Jeff Sharkeyc006f1a2011-05-19 17:12:49 -0700619 @Override
620 public NetworkInfo getActiveNetworkInfoForUid(int uid) {
621 enforceConnectivityInternalPermission();
622 return getNetworkInfo(mActiveDefaultNetwork, uid);
623 }
624
625 @Override
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800626 public NetworkInfo getNetworkInfo(int networkType) {
627 enforceAccessPermission();
Jeff Sharkeyc006f1a2011-05-19 17:12:49 -0700628 final int uid = Binder.getCallingUid();
629 return getNetworkInfo(networkType, uid);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800630 }
631
Jeff Sharkeyc006f1a2011-05-19 17:12:49 -0700632 private NetworkInfo getNetworkInfo(int networkType, int uid) {
633 NetworkInfo info = null;
634 if (isNetworkTypeValid(networkType)) {
635 final NetworkStateTracker tracker = mNetTrackers[networkType];
636 if (tracker != null) {
Jeff Sharkeyd2a45872011-05-28 20:56:34 -0700637 info = filterNetworkInfo(tracker.getNetworkInfo(), uid);
Jeff Sharkeyc006f1a2011-05-19 17:12:49 -0700638 }
639 }
640 return info;
641 }
642
643 @Override
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800644 public NetworkInfo[] getAllNetworkInfo() {
645 enforceAccessPermission();
Jeff Sharkeyc006f1a2011-05-19 17:12:49 -0700646 final int uid = Binder.getCallingUid();
Jeff Sharkeyd2a45872011-05-28 20:56:34 -0700647 final ArrayList<NetworkInfo> result = Lists.newArrayList();
Jeff Sharkeyc006f1a2011-05-19 17:12:49 -0700648 synchronized (mUidRules) {
649 for (NetworkStateTracker tracker : mNetTrackers) {
650 if (tracker != null) {
Jeff Sharkeyd2a45872011-05-28 20:56:34 -0700651 result.add(filterNetworkInfo(tracker.getNetworkInfo(), uid));
Jeff Sharkeyc006f1a2011-05-19 17:12:49 -0700652 }
653 }
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800654 }
Jeff Sharkeyd2a45872011-05-28 20:56:34 -0700655 return result.toArray(new NetworkInfo[result.size()]);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800656 }
657
Robert Greenwaltd192dad2010-09-14 09:18:02 -0700658 /**
659 * Return LinkProperties for the active (i.e., connected) default
660 * network interface. It is assumed that at most one default network
661 * is active at a time. If more than one is active, it is indeterminate
662 * which will be returned.
663 * @return the ip properties for the active network, or {@code null} if
664 * none is active
665 */
Jeff Sharkeyc006f1a2011-05-19 17:12:49 -0700666 @Override
Robert Greenwaltd192dad2010-09-14 09:18:02 -0700667 public LinkProperties getActiveLinkProperties() {
Robert Greenwalt59911582011-05-20 12:23:41 -0700668 return getLinkProperties(mActiveDefaultNetwork);
Robert Greenwaltd192dad2010-09-14 09:18:02 -0700669 }
670
Jeff Sharkeyc006f1a2011-05-19 17:12:49 -0700671 @Override
Robert Greenwaltd192dad2010-09-14 09:18:02 -0700672 public LinkProperties getLinkProperties(int networkType) {
673 enforceAccessPermission();
Jeff Sharkeyc006f1a2011-05-19 17:12:49 -0700674 if (isNetworkTypeValid(networkType)) {
675 final NetworkStateTracker tracker = mNetTrackers[networkType];
676 if (tracker != null) {
677 return tracker.getLinkProperties();
678 }
Robert Greenwaltd192dad2010-09-14 09:18:02 -0700679 }
680 return null;
681 }
682
Jeff Sharkeyd2a45872011-05-28 20:56:34 -0700683 @Override
684 public NetworkState[] getAllNetworkState() {
685 enforceAccessPermission();
686 final int uid = Binder.getCallingUid();
687 final ArrayList<NetworkState> result = Lists.newArrayList();
688 synchronized (mUidRules) {
689 for (NetworkStateTracker tracker : mNetTrackers) {
690 if (tracker != null) {
691 final NetworkInfo info = filterNetworkInfo(tracker.getNetworkInfo(), uid);
692 result.add(new NetworkState(
693 info, tracker.getLinkProperties(), tracker.getLinkCapabilities()));
694 }
695 }
696 }
697 return result.toArray(new NetworkState[result.size()]);
698 }
699
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800700 public boolean setRadios(boolean turnOn) {
701 boolean result = true;
702 enforceChangePermission();
703 for (NetworkStateTracker t : mNetTrackers) {
Robert Greenwalt5154ae762009-10-30 14:17:42 -0700704 if (t != null) result = t.setRadio(turnOn) && result;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800705 }
706 return result;
707 }
708
709 public boolean setRadio(int netType, boolean turnOn) {
710 enforceChangePermission();
711 if (!ConnectivityManager.isNetworkTypeValid(netType)) {
712 return false;
713 }
714 NetworkStateTracker tracker = mNetTrackers[netType];
715 return tracker != null && tracker.setRadio(turnOn);
716 }
717
Robert Greenwalt9c75d4a2009-09-27 17:27:04 -0700718 /**
719 * Used to notice when the calling process dies so we can self-expire
720 *
721 * Also used to know if the process has cleaned up after itself when
722 * our auto-expire timer goes off. The timer has a link to an object.
723 *
724 */
Robert Greenwalt42acef32009-08-12 16:08:25 -0700725 private class FeatureUser implements IBinder.DeathRecipient {
726 int mNetworkType;
727 String mFeature;
728 IBinder mBinder;
729 int mPid;
730 int mUid;
Robert Greenwaltb9285352009-12-21 18:24:07 -0800731 long mCreateTime;
Robert Greenwalt42acef32009-08-12 16:08:25 -0700732
733 FeatureUser(int type, String feature, IBinder binder) {
734 super();
735 mNetworkType = type;
736 mFeature = feature;
737 mBinder = binder;
738 mPid = getCallingPid();
739 mUid = getCallingUid();
Robert Greenwaltb9285352009-12-21 18:24:07 -0800740 mCreateTime = System.currentTimeMillis();
Robert Greenwalt9c75d4a2009-09-27 17:27:04 -0700741
Robert Greenwalt42acef32009-08-12 16:08:25 -0700742 try {
743 mBinder.linkToDeath(this, 0);
744 } catch (RemoteException e) {
745 binderDied();
746 }
747 }
748
749 void unlinkDeathRecipient() {
750 mBinder.unlinkToDeath(this, 0);
751 }
752
753 public void binderDied() {
Wink Savilleed9c02b2010-12-03 12:01:38 -0800754 log("ConnectivityService FeatureUser binderDied(" +
Robert Greenwaltb9285352009-12-21 18:24:07 -0800755 mNetworkType + ", " + mFeature + ", " + mBinder + "), created " +
756 (System.currentTimeMillis() - mCreateTime) + " mSec ago");
Robert Greenwalt9c75d4a2009-09-27 17:27:04 -0700757 stopUsingNetworkFeature(this, false);
Robert Greenwalt42acef32009-08-12 16:08:25 -0700758 }
759
Robert Greenwalt9c75d4a2009-09-27 17:27:04 -0700760 public void expire() {
Wink Savilleed9c02b2010-12-03 12:01:38 -0800761 log("ConnectivityService FeatureUser expire(" +
Robert Greenwaltb9285352009-12-21 18:24:07 -0800762 mNetworkType + ", " + mFeature + ", " + mBinder +"), created " +
763 (System.currentTimeMillis() - mCreateTime) + " mSec ago");
Robert Greenwalt9c75d4a2009-09-27 17:27:04 -0700764 stopUsingNetworkFeature(this, false);
765 }
Robert Greenwaltb9285352009-12-21 18:24:07 -0800766
767 public String toString() {
768 return "FeatureUser("+mNetworkType+","+mFeature+","+mPid+","+mUid+"), created " +
769 (System.currentTimeMillis() - mCreateTime) + " mSec ago";
770 }
Robert Greenwalt42acef32009-08-12 16:08:25 -0700771 }
772
Robert Greenwalt9c75d4a2009-09-27 17:27:04 -0700773 // javadoc from interface
Robert Greenwalt42acef32009-08-12 16:08:25 -0700774 public int startUsingNetworkFeature(int networkType, String feature,
775 IBinder binder) {
776 if (DBG) {
Wink Savilleed9c02b2010-12-03 12:01:38 -0800777 log("startUsingNetworkFeature for net " + networkType + ": " + feature);
Robert Greenwalt42acef32009-08-12 16:08:25 -0700778 }
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800779 enforceChangePermission();
Robert Greenwalt5154ae762009-10-30 14:17:42 -0700780 if (!ConnectivityManager.isNetworkTypeValid(networkType) ||
Robert Greenwaltd55a6b42011-03-25 13:09:25 -0700781 mNetConfigs[networkType] == null) {
Robert Greenwalt42acef32009-08-12 16:08:25 -0700782 return Phone.APN_REQUEST_FAILED;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800783 }
Robert Greenwalt42acef32009-08-12 16:08:25 -0700784
Robert Greenwalt9c75d4a2009-09-27 17:27:04 -0700785 FeatureUser f = new FeatureUser(networkType, feature, binder);
Robert Greenwalt42acef32009-08-12 16:08:25 -0700786
787 // TODO - move this into the MobileDataStateTracker
788 int usedNetworkType = networkType;
789 if(networkType == ConnectivityManager.TYPE_MOBILE) {
Wink Saville2b8bcfe2011-02-24 17:58:51 -0800790 usedNetworkType = convertFeatureToNetworkType(feature);
791 if (usedNetworkType < 0) {
792 Slog.e(TAG, "Can't match any netTracker!");
793 usedNetworkType = networkType;
Robert Greenwalt42acef32009-08-12 16:08:25 -0700794 }
795 }
796 NetworkStateTracker network = mNetTrackers[usedNetworkType];
797 if (network != null) {
Robert Greenwalt0be1e982010-12-15 13:26:33 -0800798 Integer currentPid = new Integer(getCallingPid());
Robert Greenwalt42acef32009-08-12 16:08:25 -0700799 if (usedNetworkType != networkType) {
Robert Greenwalt42acef32009-08-12 16:08:25 -0700800 NetworkStateTracker radio = mNetTrackers[networkType];
801 NetworkInfo ni = network.getNetworkInfo();
802
803 if (ni.isAvailable() == false) {
Wink Savilleed9c02b2010-12-03 12:01:38 -0800804 if (DBG) log("special network not available");
Robert Greenwalte32e8122010-12-29 14:35:21 -0800805 if (!TextUtils.equals(feature,Phone.FEATURE_ENABLE_DUN_ALWAYS)) {
806 return Phone.APN_TYPE_NOT_AVAILABLE;
807 } else {
808 // else make the attempt anyway - probably giving REQUEST_STARTED below
809 }
Robert Greenwalt42acef32009-08-12 16:08:25 -0700810 }
811
Robert Greenwalt9c75d4a2009-09-27 17:27:04 -0700812 synchronized(this) {
813 mFeatureUsers.add(f);
814 if (!mNetRequestersPids[usedNetworkType].contains(currentPid)) {
815 // this gets used for per-pid dns when connected
816 mNetRequestersPids[usedNetworkType].add(currentPid);
817 }
Robert Greenwalt42acef32009-08-12 16:08:25 -0700818 }
Robert Greenwalt9c75d4a2009-09-27 17:27:04 -0700819
Robert Greenwaltf2102f72011-05-03 19:02:44 -0700820 int restoreTimer = getRestoreDefaultNetworkDelay(usedNetworkType);
821
822 if (restoreTimer >= 0) {
823 mHandler.sendMessageDelayed(
824 mHandler.obtainMessage(EVENT_RESTORE_DEFAULT_NETWORK, f), restoreTimer);
825 }
Robert Greenwalt42acef32009-08-12 16:08:25 -0700826
Robert Greenwalta64bf832009-08-19 20:19:33 -0700827 if ((ni.isConnectedOrConnecting() == true) &&
828 !network.isTeardownRequested()) {
Robert Greenwalt42acef32009-08-12 16:08:25 -0700829 if (ni.isConnected() == true) {
830 // add the pid-specific dns
Robert Greenwaltc76b8fa2010-07-23 15:46:26 -0700831 handleDnsConfigurationChange(networkType);
Wink Savilleed9c02b2010-12-03 12:01:38 -0800832 if (DBG) log("special network already active");
Robert Greenwalt42acef32009-08-12 16:08:25 -0700833 return Phone.APN_ALREADY_ACTIVE;
834 }
Wink Savilleed9c02b2010-12-03 12:01:38 -0800835 if (DBG) log("special network already connecting");
Robert Greenwalt42acef32009-08-12 16:08:25 -0700836 return Phone.APN_REQUEST_STARTED;
837 }
838
839 // check if the radio in play can make another contact
840 // assume if cannot for now
841
Wink Savilleed9c02b2010-12-03 12:01:38 -0800842 if (DBG) log("reconnecting to special network");
Robert Greenwalt42acef32009-08-12 16:08:25 -0700843 network.reconnect();
844 return Phone.APN_REQUEST_STARTED;
845 } else {
Robert Greenwalt0be1e982010-12-15 13:26:33 -0800846 // need to remember this unsupported request so we respond appropriately on stop
847 synchronized(this) {
848 mFeatureUsers.add(f);
849 if (!mNetRequestersPids[usedNetworkType].contains(currentPid)) {
850 // this gets used for per-pid dns when connected
851 mNetRequestersPids[usedNetworkType].add(currentPid);
852 }
853 }
Robert Greenwalt02648a42010-05-18 10:52:51 -0700854 return -1;
Robert Greenwalt42acef32009-08-12 16:08:25 -0700855 }
856 }
857 return Phone.APN_TYPE_NOT_AVAILABLE;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800858 }
859
Robert Greenwalt9c75d4a2009-09-27 17:27:04 -0700860 // javadoc from interface
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800861 public int stopUsingNetworkFeature(int networkType, String feature) {
Robert Greenwaltb8f16342009-10-06 17:52:40 -0700862 enforceChangePermission();
863
Robert Greenwalt9c75d4a2009-09-27 17:27:04 -0700864 int pid = getCallingPid();
865 int uid = getCallingUid();
866
867 FeatureUser u = null;
868 boolean found = false;
869
870 synchronized(this) {
871 for (int i = 0; i < mFeatureUsers.size() ; i++) {
872 u = (FeatureUser)mFeatureUsers.get(i);
873 if (uid == u.mUid && pid == u.mPid &&
874 networkType == u.mNetworkType &&
875 TextUtils.equals(feature, u.mFeature)) {
876 found = true;
877 break;
878 }
879 }
880 }
881 if (found && u != null) {
882 // stop regardless of how many other time this proc had called start
883 return stopUsingNetworkFeature(u, true);
884 } else {
885 // none found!
Wink Savilleed9c02b2010-12-03 12:01:38 -0800886 if (DBG) log("ignoring stopUsingNetworkFeature - not a live request");
Robert Greenwalt9c75d4a2009-09-27 17:27:04 -0700887 return 1;
888 }
Robert Greenwalt42acef32009-08-12 16:08:25 -0700889 }
890
Robert Greenwalt9c75d4a2009-09-27 17:27:04 -0700891 private int stopUsingNetworkFeature(FeatureUser u, boolean ignoreDups) {
892 int networkType = u.mNetworkType;
893 String feature = u.mFeature;
894 int pid = u.mPid;
895 int uid = u.mUid;
896
897 NetworkStateTracker tracker = null;
898 boolean callTeardown = false; // used to carry our decision outside of sync block
899
Robert Greenwalt42acef32009-08-12 16:08:25 -0700900 if (DBG) {
Wink Savilleed9c02b2010-12-03 12:01:38 -0800901 log("stopUsingNetworkFeature for net " + networkType +
Robert Greenwalt42acef32009-08-12 16:08:25 -0700902 ": " + feature);
903 }
Robert Greenwaltb8f16342009-10-06 17:52:40 -0700904
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800905 if (!ConnectivityManager.isNetworkTypeValid(networkType)) {
906 return -1;
907 }
Robert Greenwalt42acef32009-08-12 16:08:25 -0700908
Robert Greenwalt9c75d4a2009-09-27 17:27:04 -0700909 // need to link the mFeatureUsers list with the mNetRequestersPids state in this
910 // sync block
911 synchronized(this) {
912 // check if this process still has an outstanding start request
913 if (!mFeatureUsers.contains(u)) {
Wink Savilleed9c02b2010-12-03 12:01:38 -0800914 if (DBG) log("ignoring - this process has no outstanding requests");
Robert Greenwalt42acef32009-08-12 16:08:25 -0700915 return 1;
916 }
Robert Greenwalt9c75d4a2009-09-27 17:27:04 -0700917 u.unlinkDeathRecipient();
918 mFeatureUsers.remove(mFeatureUsers.indexOf(u));
919 // If we care about duplicate requests, check for that here.
920 //
921 // This is done to support the extension of a request - the app
922 // can request we start the network feature again and renew the
923 // auto-shutoff delay. Normal "stop" calls from the app though
924 // do not pay attention to duplicate requests - in effect the
925 // API does not refcount and a single stop will counter multiple starts.
926 if (ignoreDups == false) {
927 for (int i = 0; i < mFeatureUsers.size() ; i++) {
928 FeatureUser x = (FeatureUser)mFeatureUsers.get(i);
929 if (x.mUid == u.mUid && x.mPid == u.mPid &&
930 x.mNetworkType == u.mNetworkType &&
931 TextUtils.equals(x.mFeature, u.mFeature)) {
Wink Savilleed9c02b2010-12-03 12:01:38 -0800932 if (DBG) log("ignoring stopUsingNetworkFeature as dup is found");
Robert Greenwalt9c75d4a2009-09-27 17:27:04 -0700933 return 1;
934 }
935 }
Robert Greenwalt42acef32009-08-12 16:08:25 -0700936 }
Robert Greenwalt9c75d4a2009-09-27 17:27:04 -0700937
938 // TODO - move to MobileDataStateTracker
939 int usedNetworkType = networkType;
940 if (networkType == ConnectivityManager.TYPE_MOBILE) {
Wink Saville2b8bcfe2011-02-24 17:58:51 -0800941 usedNetworkType = convertFeatureToNetworkType(feature);
942 if (usedNetworkType < 0) {
943 usedNetworkType = networkType;
Robert Greenwalt9c75d4a2009-09-27 17:27:04 -0700944 }
945 }
946 tracker = mNetTrackers[usedNetworkType];
Robert Greenwalt5154ae762009-10-30 14:17:42 -0700947 if (tracker == null) {
Wink Savilleed9c02b2010-12-03 12:01:38 -0800948 if (DBG) log("ignoring - no known tracker for net type " + usedNetworkType);
Robert Greenwalt5154ae762009-10-30 14:17:42 -0700949 return -1;
950 }
951 if (usedNetworkType != networkType) {
Robert Greenwalt9c75d4a2009-09-27 17:27:04 -0700952 Integer currentPid = new Integer(pid);
Robert Greenwalt9c75d4a2009-09-27 17:27:04 -0700953 mNetRequestersPids[usedNetworkType].remove(currentPid);
Robert Greenwalt421c72b2009-12-17 14:54:59 -0800954 reassessPidDns(pid, true);
Robert Greenwalt9c75d4a2009-09-27 17:27:04 -0700955 if (mNetRequestersPids[usedNetworkType].size() != 0) {
Wink Savilleed9c02b2010-12-03 12:01:38 -0800956 if (DBG) log("not tearing down special network - " +
Robert Greenwalt9c75d4a2009-09-27 17:27:04 -0700957 "others still using it");
958 return 1;
959 }
960 callTeardown = true;
Robert Greenwalt19b9ab42011-01-10 11:58:31 -0800961 } else {
962 if (DBG) log("not a known feature - dropping");
Robert Greenwalt9c75d4a2009-09-27 17:27:04 -0700963 }
964 }
Wink Savilleed9c02b2010-12-03 12:01:38 -0800965 if (DBG) log("Doing network teardown");
Robert Greenwalt9c75d4a2009-09-27 17:27:04 -0700966 if (callTeardown) {
967 tracker.teardown();
Robert Greenwalt42acef32009-08-12 16:08:25 -0700968 return 1;
969 } else {
Robert Greenwalt02648a42010-05-18 10:52:51 -0700970 return -1;
Robert Greenwalt42acef32009-08-12 16:08:25 -0700971 }
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800972 }
973
974 /**
Robert Greenwalt585ac0f2010-08-27 09:24:29 -0700975 * @deprecated use requestRouteToHostAddress instead
976 *
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800977 * Ensure that a network route exists to deliver traffic to the specified
978 * host via the specified network interface.
Robert Greenwalt86e9e552009-07-16 17:21:39 -0700979 * @param networkType the type of the network over which traffic to the
980 * specified host is to be routed
981 * @param hostAddress the IP address of the host to which the route is
982 * desired
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800983 * @return {@code true} on success, {@code false} on failure
984 */
985 public boolean requestRouteToHost(int networkType, int hostAddress) {
Robert Greenwalt585ac0f2010-08-27 09:24:29 -0700986 InetAddress inetAddress = NetworkUtils.intToInetAddress(hostAddress);
987
988 if (inetAddress == null) {
989 return false;
990 }
991
992 return requestRouteToHostAddress(networkType, inetAddress.getAddress());
993 }
994
995 /**
996 * Ensure that a network route exists to deliver traffic to the specified
997 * host via the specified network interface.
998 * @param networkType the type of the network over which traffic to the
999 * specified host is to be routed
1000 * @param hostAddress the IP address of the host to which the route is
1001 * desired
1002 * @return {@code true} on success, {@code false} on failure
1003 */
1004 public boolean requestRouteToHostAddress(int networkType, byte[] hostAddress) {
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001005 enforceChangePermission();
1006 if (!ConnectivityManager.isNetworkTypeValid(networkType)) {
1007 return false;
1008 }
1009 NetworkStateTracker tracker = mNetTrackers[networkType];
Robert Greenwalt8206ff32009-09-10 15:06:20 -07001010
Robert Greenwalt5154ae762009-10-30 14:17:42 -07001011 if (tracker == null || !tracker.getNetworkInfo().isConnected() ||
1012 tracker.isTeardownRequested()) {
Robert Greenwalt8206ff32009-09-10 15:06:20 -07001013 if (DBG) {
Wink Savilleed9c02b2010-12-03 12:01:38 -08001014 log("requestRouteToHostAddress on down network " +
Robert Greenwalt585ac0f2010-08-27 09:24:29 -07001015 "(" + networkType + ") - dropped");
Robert Greenwalt8206ff32009-09-10 15:06:20 -07001016 }
1017 return false;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001018 }
Robert Greenwalt47f69fe2010-06-15 15:43:39 -07001019 try {
Robert Greenwalt585ac0f2010-08-27 09:24:29 -07001020 InetAddress addr = InetAddress.getByAddress(hostAddress);
Robert Greenwaltf43396c2011-05-06 17:10:53 -07001021 return addHostRoute(tracker, addr, 0);
Robert Greenwalt47f69fe2010-06-15 15:43:39 -07001022 } catch (UnknownHostException e) {}
1023 return false;
Irfan Sheriffd649c122010-06-09 15:39:36 -07001024 }
1025
1026 /**
1027 * Ensure that a network route exists to deliver traffic to the specified
1028 * host via the mobile data network.
1029 * @param hostAddress the IP address of the host to which the route is desired,
1030 * in network byte order.
Robert Greenwalt47f69fe2010-06-15 15:43:39 -07001031 * TODO - deprecate
Irfan Sheriffd649c122010-06-09 15:39:36 -07001032 * @return {@code true} on success, {@code false} on failure
1033 */
Robert Greenwaltf43396c2011-05-06 17:10:53 -07001034 private boolean addHostRoute(NetworkStateTracker nt, InetAddress hostAddress, int cycleCount) {
Robert Greenwaltf43396c2011-05-06 17:10:53 -07001035 LinkProperties lp = nt.getLinkProperties();
1036 if ((lp == null) || (hostAddress == null)) return false;
Irfan Sheriffd649c122010-06-09 15:39:36 -07001037
Robert Greenwaltf43396c2011-05-06 17:10:53 -07001038 String interfaceName = lp.getInterfaceName();
Irfan Sheriffd649c122010-06-09 15:39:36 -07001039 if (DBG) {
Robert Greenwaltf43396c2011-05-06 17:10:53 -07001040 log("Requested host route to " + hostAddress + "(" + interfaceName + "), cycleCount=" +
1041 cycleCount);
Irfan Sheriffd649c122010-06-09 15:39:36 -07001042 }
Robert Greenwaltf43396c2011-05-06 17:10:53 -07001043 if (interfaceName == null) {
Wink Savilleed9c02b2010-12-03 12:01:38 -08001044 if (DBG) loge("addHostRoute failed due to null interface name");
Irfan Sheriffd649c122010-06-09 15:39:36 -07001045 return false;
1046 }
Robert Greenwaltf43396c2011-05-06 17:10:53 -07001047
1048 RouteInfo bestRoute = RouteInfo.selectBestRoute(lp.getRoutes(), hostAddress);
Robert Greenwalt59b1a4e2011-05-10 15:05:02 -07001049 InetAddress gatewayAddress = null;
Robert Greenwaltf43396c2011-05-06 17:10:53 -07001050 if (bestRoute != null) {
Robert Greenwalt59b1a4e2011-05-10 15:05:02 -07001051 gatewayAddress = bestRoute.getGateway();
Robert Greenwaltf43396c2011-05-06 17:10:53 -07001052 // if the best route is ourself, don't relf-reference, just add the host route
Robert Greenwalt59b1a4e2011-05-10 15:05:02 -07001053 if (hostAddress.equals(gatewayAddress)) gatewayAddress = null;
Robert Greenwaltf43396c2011-05-06 17:10:53 -07001054 }
Robert Greenwalt59b1a4e2011-05-10 15:05:02 -07001055 if (gatewayAddress != null) {
Robert Greenwaltf43396c2011-05-06 17:10:53 -07001056 if (cycleCount > MAX_HOSTROUTE_CYCLE_COUNT) {
1057 loge("Error adding hostroute - too much recursion");
1058 return false;
1059 }
Robert Greenwalt59b1a4e2011-05-10 15:05:02 -07001060 if (!addHostRoute(nt, gatewayAddress, cycleCount+1)) return false;
Robert Greenwaltf43396c2011-05-06 17:10:53 -07001061 }
Robert Greenwalt59b1a4e2011-05-10 15:05:02 -07001062
1063 RouteInfo route = RouteInfo.makeHostRoute(hostAddress, gatewayAddress);
1064
1065 try {
1066 mNetd.addRoute(interfaceName, route);
1067 return true;
1068 } catch (Exception ex) {
1069 return false;
1070 }
Robert Greenwaltf43396c2011-05-06 17:10:53 -07001071 }
1072
1073 // TODO support the removal of single host routes. Keep a ref count of them so we
1074 // aren't over-zealous
1075 private boolean removeHostRoute(NetworkStateTracker nt, InetAddress hostAddress) {
1076 return false;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001077 }
1078
1079 /**
1080 * @see ConnectivityManager#getBackgroundDataSetting()
1081 */
1082 public boolean getBackgroundDataSetting() {
Robert Greenwaltd825ea42010-12-29 16:15:02 -08001083 return mBackgroundDataEnabled.get();
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001084 }
Robert Greenwalt86e9e552009-07-16 17:21:39 -07001085
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001086 /**
1087 * @see ConnectivityManager#setBackgroundDataSetting(boolean)
1088 */
1089 public void setBackgroundDataSetting(boolean allowBackgroundDataUsage) {
1090 mContext.enforceCallingOrSelfPermission(
1091 android.Manifest.permission.CHANGE_BACKGROUND_DATA_SETTING,
1092 "ConnectivityService");
Robert Greenwalt86e9e552009-07-16 17:21:39 -07001093
Robert Greenwaltd825ea42010-12-29 16:15:02 -08001094 mBackgroundDataEnabled.set(allowBackgroundDataUsage);
1095
Robert Greenwalt8dcc28b2010-09-23 10:05:56 -07001096 mHandler.sendMessage(mHandler.obtainMessage(EVENT_SET_BACKGROUND_DATA,
1097 (allowBackgroundDataUsage ? ENABLED : DISABLED), 0));
1098 }
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001099
Robert Greenwalt8dcc28b2010-09-23 10:05:56 -07001100 private void handleSetBackgroundData(boolean enabled) {
Robert Greenwaltdb4afae2011-02-25 13:44:09 -08001101 Settings.Secure.putInt(mContext.getContentResolver(),
1102 Settings.Secure.BACKGROUND_DATA, enabled ? 1 : 0);
1103 Intent broadcast = new Intent(
1104 ConnectivityManager.ACTION_BACKGROUND_DATA_SETTING_CHANGED);
1105 mContext.sendBroadcast(broadcast);
Robert Greenwalt86e9e552009-07-16 17:21:39 -07001106 }
1107
Robert Greenwaltc03fa502010-02-23 18:58:05 -08001108 /**
1109 * @see ConnectivityManager#getMobileDataEnabled()
1110 */
1111 public boolean getMobileDataEnabled() {
Wink Savillee7982682010-12-07 10:31:02 -08001112 // TODO: This detail should probably be in DataConnectionTracker's
1113 // which is where we store the value and maybe make this
1114 // asynchronous.
Robert Greenwaltc03fa502010-02-23 18:58:05 -08001115 enforceAccessPermission();
1116 boolean retVal = Settings.Secure.getInt(mContext.getContentResolver(),
1117 Settings.Secure.MOBILE_DATA, 1) == 1;
Wink Savilleed9c02b2010-12-03 12:01:38 -08001118 if (DBG) log("getMobileDataEnabled returning " + retVal);
Robert Greenwaltc03fa502010-02-23 18:58:05 -08001119 return retVal;
1120 }
1121
Robert Greenwaltd55a6b42011-03-25 13:09:25 -07001122 public void setDataDependency(int networkType, boolean met) {
1123 enforceChangePermission();
1124 if (DBG) {
1125 log("setDataDependency(" + networkType + ", " + met + ")");
1126 }
1127 mHandler.sendMessage(mHandler.obtainMessage(EVENT_SET_DEPENDENCY_MET,
1128 (met ? ENABLED : DISABLED), networkType));
1129 }
1130
1131 private void handleSetDependencyMet(int networkType, boolean met) {
1132 if (mNetTrackers[networkType] != null) {
1133 if (DBG) {
1134 log("handleSetDependencyMet(" + networkType + ", " + met + ")");
1135 }
1136 mNetTrackers[networkType].setDependencyMet(met);
1137 }
1138 }
1139
Jeff Sharkeyc006f1a2011-05-19 17:12:49 -07001140 private INetworkPolicyListener mPolicyListener = new INetworkPolicyListener.Stub() {
1141 @Override
1142 public void onRulesChanged(int uid, int uidRules) {
1143 // only someone like NPMS should only be calling us
Jeff Sharkey1a303952011-06-16 13:04:20 -07001144 mContext.enforceCallingOrSelfPermission(MANAGE_NETWORK_POLICY, TAG);
Jeff Sharkeyc006f1a2011-05-19 17:12:49 -07001145
1146 if (LOGD_RULES) {
1147 Slog.d(TAG, "onRulesChanged(uid=" + uid + ", uidRules=" + uidRules + ")");
1148 }
1149
1150 synchronized (mUidRules) {
1151 // skip update when we've already applied rules
1152 final int oldRules = mUidRules.get(uid, RULE_ALLOW_ALL);
1153 if (oldRules == uidRules) return;
1154
1155 mUidRules.put(uid, uidRules);
1156 }
1157
1158 // TODO: dispatch into NMS to push rules towards kernel module
1159 // TODO: notify UID when it has requested targeted updates
1160 }
1161 };
1162
Robert Greenwaltc03fa502010-02-23 18:58:05 -08001163 /**
1164 * @see ConnectivityManager#setMobileDataEnabled(boolean)
1165 */
Robert Greenwalt8dcc28b2010-09-23 10:05:56 -07001166 public void setMobileDataEnabled(boolean enabled) {
Robert Greenwaltc03fa502010-02-23 18:58:05 -08001167 enforceChangePermission();
Wink Savilleed9c02b2010-12-03 12:01:38 -08001168 if (DBG) log("setMobileDataEnabled(" + enabled + ")");
Robert Greenwaltc03fa502010-02-23 18:58:05 -08001169
Robert Greenwalt8dcc28b2010-09-23 10:05:56 -07001170 mHandler.sendMessage(mHandler.obtainMessage(EVENT_SET_MOBILE_DATA,
Robert Greenwaltd55a6b42011-03-25 13:09:25 -07001171 (enabled ? ENABLED : DISABLED), 0));
Robert Greenwalt8dcc28b2010-09-23 10:05:56 -07001172 }
1173
1174 private void handleSetMobileData(boolean enabled) {
Wink Savillee7982682010-12-07 10:31:02 -08001175 if (mNetTrackers[ConnectivityManager.TYPE_MOBILE] != null) {
1176 if (DBG) {
1177 Slog.d(TAG, mNetTrackers[ConnectivityManager.TYPE_MOBILE].toString() + enabled);
Robert Greenwaltc03fa502010-02-23 18:58:05 -08001178 }
Wink Savillee7982682010-12-07 10:31:02 -08001179 mNetTrackers[ConnectivityManager.TYPE_MOBILE].setDataEnable(enabled);
Robert Greenwaltc03fa502010-02-23 18:58:05 -08001180 }
1181 }
1182
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001183 private void enforceAccessPermission() {
Robert Greenwalt86e9e552009-07-16 17:21:39 -07001184 mContext.enforceCallingOrSelfPermission(
1185 android.Manifest.permission.ACCESS_NETWORK_STATE,
1186 "ConnectivityService");
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001187 }
1188
1189 private void enforceChangePermission() {
Robert Greenwalt86e9e552009-07-16 17:21:39 -07001190 mContext.enforceCallingOrSelfPermission(
1191 android.Manifest.permission.CHANGE_NETWORK_STATE,
1192 "ConnectivityService");
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001193 }
1194
Robert Greenwaltd0e18ff2010-01-26 11:40:34 -08001195 // TODO Make this a special check when it goes public
1196 private void enforceTetherChangePermission() {
1197 mContext.enforceCallingOrSelfPermission(
1198 android.Manifest.permission.CHANGE_NETWORK_STATE,
1199 "ConnectivityService");
1200 }
1201
Robert Greenwalt2a091d72010-02-11 18:18:40 -08001202 private void enforceTetherAccessPermission() {
1203 mContext.enforceCallingOrSelfPermission(
1204 android.Manifest.permission.ACCESS_NETWORK_STATE,
1205 "ConnectivityService");
1206 }
1207
Robert Greenwalt14f2ef42010-06-15 12:19:37 -07001208 private void enforceConnectivityInternalPermission() {
1209 mContext.enforceCallingOrSelfPermission(
1210 android.Manifest.permission.CONNECTIVITY_INTERNAL,
1211 "ConnectivityService");
1212 }
1213
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001214 /**
Robert Greenwalt86e9e552009-07-16 17:21:39 -07001215 * Handle a {@code DISCONNECTED} event. If this pertains to the non-active
1216 * network, we ignore it. If it is for the active network, we send out a
1217 * broadcast. But first, we check whether it might be possible to connect
1218 * to a different network.
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001219 * @param info the {@code NetworkInfo} for the network
1220 */
1221 private void handleDisconnect(NetworkInfo info) {
1222
Robert Greenwalt42acef32009-08-12 16:08:25 -07001223 int prevNetType = info.getType();
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001224
Robert Greenwalt42acef32009-08-12 16:08:25 -07001225 mNetTrackers[prevNetType].setTeardownRequested(false);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001226 /*
1227 * If the disconnected network is not the active one, then don't report
1228 * this as a loss of connectivity. What probably happened is that we're
1229 * getting the disconnect for a network that we explicitly disabled
1230 * in accordance with network preference policies.
1231 */
Robert Greenwaltd55a6b42011-03-25 13:09:25 -07001232 if (!mNetConfigs[prevNetType].isDefault()) {
Robert Greenwalt42acef32009-08-12 16:08:25 -07001233 List pids = mNetRequestersPids[prevNetType];
1234 for (int i = 0; i<pids.size(); i++) {
1235 Integer pid = (Integer)pids.get(i);
1236 // will remove them because the net's no longer connected
1237 // need to do this now as only now do we know the pids and
1238 // can properly null things that are no longer referenced.
1239 reassessPidDns(pid.intValue(), false);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001240 }
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001241 }
1242
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001243 Intent intent = new Intent(ConnectivityManager.CONNECTIVITY_ACTION);
1244 intent.putExtra(ConnectivityManager.EXTRA_NETWORK_INFO, info);
1245 if (info.isFailover()) {
1246 intent.putExtra(ConnectivityManager.EXTRA_IS_FAILOVER, true);
1247 info.setFailover(false);
1248 }
1249 if (info.getReason() != null) {
1250 intent.putExtra(ConnectivityManager.EXTRA_REASON, info.getReason());
1251 }
1252 if (info.getExtraInfo() != null) {
Robert Greenwalt86e9e552009-07-16 17:21:39 -07001253 intent.putExtra(ConnectivityManager.EXTRA_EXTRA_INFO,
1254 info.getExtraInfo());
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001255 }
Robert Greenwalt42acef32009-08-12 16:08:25 -07001256
Robert Greenwaltd55a6b42011-03-25 13:09:25 -07001257 if (mNetConfigs[prevNetType].isDefault()) {
Robert Greenwaltf21ef7d2011-01-11 13:56:33 -08001258 tryFailover(prevNetType);
1259 if (mActiveDefaultNetwork != -1) {
1260 NetworkInfo switchTo = mNetTrackers[mActiveDefaultNetwork].getNetworkInfo();
Robert Greenwaltcc4b4012010-01-25 17:54:29 -08001261 intent.putExtra(ConnectivityManager.EXTRA_OTHER_NETWORK_INFO, switchTo);
1262 } else {
Robert Greenwalt029be812010-09-20 18:01:43 -07001263 mDefaultInetConditionPublished = 0; // we're not connected anymore
Robert Greenwaltcc4b4012010-01-25 17:54:29 -08001264 intent.putExtra(ConnectivityManager.EXTRA_NO_CONNECTIVITY, true);
1265 }
Robert Greenwaltda03c4e2010-01-20 19:29:41 -08001266 }
Robert Greenwalt029be812010-09-20 18:01:43 -07001267 intent.putExtra(ConnectivityManager.EXTRA_INET_CONDITION, mDefaultInetConditionPublished);
Robert Greenwaltec896c62011-06-15 12:22:07 -07001268
1269 // Reset interface if no other connections are using the same interface
1270 boolean doReset = true;
1271 LinkProperties linkProperties = mNetTrackers[prevNetType].getLinkProperties();
1272 if (linkProperties != null) {
1273 String oldIface = linkProperties.getInterfaceName();
1274 if (TextUtils.isEmpty(oldIface) == false) {
1275 for (NetworkStateTracker networkStateTracker : mNetTrackers) {
1276 if (networkStateTracker == null) continue;
1277 NetworkInfo networkInfo = networkStateTracker.getNetworkInfo();
1278 if (networkInfo.isConnected() && networkInfo.getType() != prevNetType) {
1279 LinkProperties l = networkStateTracker.getLinkProperties();
1280 if (l == null) continue;
1281 if (oldIface.equals(l.getInterfaceName())) {
1282 doReset = false;
1283 break;
1284 }
1285 }
1286 }
1287 }
1288 }
1289
Robert Greenwaltda03c4e2010-01-20 19:29:41 -08001290 // do this before we broadcast the change
Robert Greenwaltec896c62011-06-15 12:22:07 -07001291 handleConnectivityChange(prevNetType, doReset);
Robert Greenwaltda03c4e2010-01-20 19:29:41 -08001292
1293 sendStickyBroadcast(intent);
1294 /*
1295 * If the failover network is already connected, then immediately send
1296 * out a followup broadcast indicating successful failover
1297 */
Robert Greenwaltf21ef7d2011-01-11 13:56:33 -08001298 if (mActiveDefaultNetwork != -1) {
1299 sendConnectedBroadcast(mNetTrackers[mActiveDefaultNetwork].getNetworkInfo());
Robert Greenwaltda03c4e2010-01-20 19:29:41 -08001300 }
1301 }
1302
Robert Greenwaltf21ef7d2011-01-11 13:56:33 -08001303 private void tryFailover(int prevNetType) {
Robert Greenwalt42acef32009-08-12 16:08:25 -07001304 /*
Robert Greenwaltbff90182011-01-06 15:41:07 -08001305 * If this is a default network, check if other defaults are available.
1306 * Try to reconnect on all available and let them hash it out when
1307 * more than one connects.
Robert Greenwalt42acef32009-08-12 16:08:25 -07001308 */
Robert Greenwaltd55a6b42011-03-25 13:09:25 -07001309 if (mNetConfigs[prevNetType].isDefault()) {
Robert Greenwalt42acef32009-08-12 16:08:25 -07001310 if (mActiveDefaultNetwork == prevNetType) {
1311 mActiveDefaultNetwork = -1;
1312 }
1313
Robert Greenwaltf21ef7d2011-01-11 13:56:33 -08001314 // don't signal a reconnect for anything lower or equal priority than our
1315 // current connected default
1316 // TODO - don't filter by priority now - nice optimization but risky
1317// int currentPriority = -1;
1318// if (mActiveDefaultNetwork != -1) {
Robert Greenwaltd55a6b42011-03-25 13:09:25 -07001319// currentPriority = mNetConfigs[mActiveDefaultNetwork].mPriority;
Robert Greenwaltf21ef7d2011-01-11 13:56:33 -08001320// }
Robert Greenwaltda03c4e2010-01-20 19:29:41 -08001321 for (int checkType=0; checkType <= ConnectivityManager.MAX_NETWORK_TYPE; checkType++) {
Robert Greenwalt5154ae762009-10-30 14:17:42 -07001322 if (checkType == prevNetType) continue;
Robert Greenwaltd55a6b42011-03-25 13:09:25 -07001323 if (mNetConfigs[checkType] == null) continue;
1324 if (!mNetConfigs[checkType].isDefault()) continue;
Wink Saville9f7a0b22011-01-26 15:43:49 -08001325
1326// Enabling the isAvailable() optimization caused mobile to not get
1327// selected if it was in the middle of error handling. Specifically
1328// a moble connection that took 30 seconds to complete the DEACTIVATE_DATA_CALL
1329// would not be available and we wouldn't get connected to anything.
1330// So removing the isAvailable() optimization below for now. TODO: This
1331// optimization should work and we need to investigate why it doesn't work.
1332// This could be related to how DEACTIVATE_DATA_CALL is reporting its
1333// complete before it is really complete.
1334// if (!mNetTrackers[checkType].isAvailable()) continue;
1335
Robert Greenwaltd55a6b42011-03-25 13:09:25 -07001336// if (currentPriority >= mNetConfigs[checkType].mPriority) continue;
Robert Greenwalt42acef32009-08-12 16:08:25 -07001337
Robert Greenwaltf21ef7d2011-01-11 13:56:33 -08001338 NetworkStateTracker checkTracker = mNetTrackers[checkType];
1339 NetworkInfo checkInfo = checkTracker.getNetworkInfo();
1340 if (!checkInfo.isConnectedOrConnecting() || checkTracker.isTeardownRequested()) {
1341 checkInfo.setFailover(true);
1342 checkTracker.reconnect();
Robert Greenwalt42acef32009-08-12 16:08:25 -07001343 }
Robert Greenwaltf21ef7d2011-01-11 13:56:33 -08001344 if (DBG) log("Attempting to switch to " + checkInfo.getTypeName());
Robert Greenwalt42acef32009-08-12 16:08:25 -07001345 }
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001346 }
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001347 }
1348
1349 private void sendConnectedBroadcast(NetworkInfo info) {
Robert Greenwalt1e9aac22010-09-15 17:36:33 -07001350 sendGeneralBroadcast(info, ConnectivityManager.CONNECTIVITY_ACTION);
1351 }
1352
1353 private void sendInetConditionBroadcast(NetworkInfo info) {
1354 sendGeneralBroadcast(info, ConnectivityManager.INET_CONDITION_ACTION);
1355 }
1356
1357 private void sendGeneralBroadcast(NetworkInfo info, String bcastType) {
1358 Intent intent = new Intent(bcastType);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001359 intent.putExtra(ConnectivityManager.EXTRA_NETWORK_INFO, info);
1360 if (info.isFailover()) {
1361 intent.putExtra(ConnectivityManager.EXTRA_IS_FAILOVER, true);
1362 info.setFailover(false);
1363 }
1364 if (info.getReason() != null) {
1365 intent.putExtra(ConnectivityManager.EXTRA_REASON, info.getReason());
1366 }
1367 if (info.getExtraInfo() != null) {
Robert Greenwalt86e9e552009-07-16 17:21:39 -07001368 intent.putExtra(ConnectivityManager.EXTRA_EXTRA_INFO,
1369 info.getExtraInfo());
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001370 }
Robert Greenwaltd7085fc2010-09-08 15:24:47 -07001371 intent.putExtra(ConnectivityManager.EXTRA_INET_CONDITION, mDefaultInetConditionPublished);
Mike Lockwood0f79b542009-08-14 14:18:49 -04001372 sendStickyBroadcast(intent);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001373 }
1374
1375 /**
1376 * Called when an attempt to fail over to another network has failed.
1377 * @param info the {@link NetworkInfo} for the failed network
1378 */
1379 private void handleConnectionFailure(NetworkInfo info) {
1380 mNetTrackers[info.getType()].setTeardownRequested(false);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001381
Robert Greenwalt42acef32009-08-12 16:08:25 -07001382 String reason = info.getReason();
1383 String extraInfo = info.getExtraInfo();
Robert Greenwalt86e9e552009-07-16 17:21:39 -07001384
Robert Greenwalt572172b2010-10-08 16:35:52 -07001385 String reasonText;
1386 if (reason == null) {
1387 reasonText = ".";
1388 } else {
1389 reasonText = " (" + reason + ").";
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001390 }
Wink Savilleed9c02b2010-12-03 12:01:38 -08001391 loge("Attempt to connect to " + info.getTypeName() + " failed" + reasonText);
Robert Greenwalt42acef32009-08-12 16:08:25 -07001392
1393 Intent intent = new Intent(ConnectivityManager.CONNECTIVITY_ACTION);
1394 intent.putExtra(ConnectivityManager.EXTRA_NETWORK_INFO, info);
1395 if (getActiveNetworkInfo() == null) {
1396 intent.putExtra(ConnectivityManager.EXTRA_NO_CONNECTIVITY, true);
1397 }
1398 if (reason != null) {
1399 intent.putExtra(ConnectivityManager.EXTRA_REASON, reason);
1400 }
1401 if (extraInfo != null) {
1402 intent.putExtra(ConnectivityManager.EXTRA_EXTRA_INFO, extraInfo);
1403 }
1404 if (info.isFailover()) {
1405 intent.putExtra(ConnectivityManager.EXTRA_IS_FAILOVER, true);
1406 info.setFailover(false);
1407 }
Robert Greenwaltda03c4e2010-01-20 19:29:41 -08001408
Robert Greenwaltd55a6b42011-03-25 13:09:25 -07001409 if (mNetConfigs[info.getType()].isDefault()) {
Robert Greenwaltf21ef7d2011-01-11 13:56:33 -08001410 tryFailover(info.getType());
1411 if (mActiveDefaultNetwork != -1) {
1412 NetworkInfo switchTo = mNetTrackers[mActiveDefaultNetwork].getNetworkInfo();
Robert Greenwaltcc4b4012010-01-25 17:54:29 -08001413 intent.putExtra(ConnectivityManager.EXTRA_OTHER_NETWORK_INFO, switchTo);
1414 } else {
Robert Greenwalt029be812010-09-20 18:01:43 -07001415 mDefaultInetConditionPublished = 0;
Robert Greenwaltcc4b4012010-01-25 17:54:29 -08001416 intent.putExtra(ConnectivityManager.EXTRA_NO_CONNECTIVITY, true);
1417 }
Robert Greenwaltda03c4e2010-01-20 19:29:41 -08001418 }
Robert Greenwaltcc4b4012010-01-25 17:54:29 -08001419
Robert Greenwalt029be812010-09-20 18:01:43 -07001420 intent.putExtra(ConnectivityManager.EXTRA_INET_CONDITION, mDefaultInetConditionPublished);
Mike Lockwood0f79b542009-08-14 14:18:49 -04001421 sendStickyBroadcast(intent);
Robert Greenwaltda03c4e2010-01-20 19:29:41 -08001422 /*
1423 * If the failover network is already connected, then immediately send
1424 * out a followup broadcast indicating successful failover
1425 */
Robert Greenwaltf21ef7d2011-01-11 13:56:33 -08001426 if (mActiveDefaultNetwork != -1) {
1427 sendConnectedBroadcast(mNetTrackers[mActiveDefaultNetwork].getNetworkInfo());
Robert Greenwaltda03c4e2010-01-20 19:29:41 -08001428 }
Mike Lockwood0f79b542009-08-14 14:18:49 -04001429 }
1430
1431 private void sendStickyBroadcast(Intent intent) {
1432 synchronized(this) {
Dianne Hackborn1c633fc2009-12-08 19:45:14 -08001433 if (!mSystemReady) {
1434 mInitialBroadcast = new Intent(intent);
Mike Lockwood0f79b542009-08-14 14:18:49 -04001435 }
Dianne Hackborn1c633fc2009-12-08 19:45:14 -08001436 intent.addFlags(Intent.FLAG_RECEIVER_REGISTERED_ONLY_BEFORE_BOOT);
1437 mContext.sendStickyBroadcast(intent);
Mike Lockwood0f79b542009-08-14 14:18:49 -04001438 }
1439 }
1440
1441 void systemReady() {
1442 synchronized(this) {
1443 mSystemReady = true;
Dianne Hackborn1c633fc2009-12-08 19:45:14 -08001444 if (mInitialBroadcast != null) {
1445 mContext.sendStickyBroadcast(mInitialBroadcast);
1446 mInitialBroadcast = null;
Mike Lockwood0f79b542009-08-14 14:18:49 -04001447 }
1448 }
Robert Greenwalt434203a2010-10-11 16:00:27 -07001449 // load the global proxy at startup
1450 mHandler.sendMessage(mHandler.obtainMessage(EVENT_APPLY_GLOBAL_HTTP_PROXY));
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001451 }
1452
1453 private void handleConnect(NetworkInfo info) {
Robert Greenwalt42acef32009-08-12 16:08:25 -07001454 int type = info.getType();
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001455
1456 // snapshot isFailover, because sendConnectedBroadcast() resets it
1457 boolean isFailover = info.isFailover();
Robert Greenwalt42acef32009-08-12 16:08:25 -07001458 NetworkStateTracker thisNet = mNetTrackers[type];
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001459
Robert Greenwalt42acef32009-08-12 16:08:25 -07001460 // if this is a default net and other default is running
1461 // kill the one not preferred
Robert Greenwaltd55a6b42011-03-25 13:09:25 -07001462 if (mNetConfigs[type].isDefault()) {
Robert Greenwalt42acef32009-08-12 16:08:25 -07001463 if (mActiveDefaultNetwork != -1 && mActiveDefaultNetwork != type) {
1464 if ((type != mNetworkPreference &&
Wink Saville975c8482011-04-07 14:23:45 -07001465 mNetConfigs[mActiveDefaultNetwork].priority >
1466 mNetConfigs[type].priority) ||
Robert Greenwalt42acef32009-08-12 16:08:25 -07001467 mNetworkPreference == mActiveDefaultNetwork) {
1468 // don't accept this one
Wink Savilleed9c02b2010-12-03 12:01:38 -08001469 if (DBG) {
1470 log("Not broadcasting CONNECT_ACTION " +
Robert Greenwalt42acef32009-08-12 16:08:25 -07001471 "to torn down network " + info.getTypeName());
Wink Savilleed9c02b2010-12-03 12:01:38 -08001472 }
Robert Greenwalt42acef32009-08-12 16:08:25 -07001473 teardown(thisNet);
1474 return;
1475 } else {
1476 // tear down the other
1477 NetworkStateTracker otherNet =
1478 mNetTrackers[mActiveDefaultNetwork];
Wink Savilleed9c02b2010-12-03 12:01:38 -08001479 if (DBG) {
1480 log("Policy requires " + otherNet.getNetworkInfo().getTypeName() +
Robert Greenwalt42acef32009-08-12 16:08:25 -07001481 " teardown");
Wink Savilleed9c02b2010-12-03 12:01:38 -08001482 }
Robert Greenwalt42acef32009-08-12 16:08:25 -07001483 if (!teardown(otherNet)) {
Wink Savilleed9c02b2010-12-03 12:01:38 -08001484 loge("Network declined teardown request");
Robert Greenwalt27725e82011-03-29 11:36:28 -07001485 teardown(thisNet);
Robert Greenwalt42acef32009-08-12 16:08:25 -07001486 return;
1487 }
Robert Greenwalt14f2ef42010-06-15 12:19:37 -07001488 }
1489 }
1490 synchronized (ConnectivityService.this) {
1491 // have a new default network, release the transition wakelock in a second
1492 // if it's held. The second pause is to allow apps to reconnect over the
1493 // new network
1494 if (mNetTransitionWakeLock.isHeld()) {
1495 mHandler.sendMessageDelayed(mHandler.obtainMessage(
Robert Greenwaltf3331232010-09-24 14:32:21 -07001496 EVENT_CLEAR_NET_TRANSITION_WAKELOCK,
Robert Greenwalt14f2ef42010-06-15 12:19:37 -07001497 mNetTransitionWakeLockSerialNumber, 0),
1498 1000);
Robert Greenwalt42acef32009-08-12 16:08:25 -07001499 }
1500 }
1501 mActiveDefaultNetwork = type;
Robert Greenwaltd7085fc2010-09-08 15:24:47 -07001502 // this will cause us to come up initially as unconnected and switching
1503 // to connected after our normal pause unless somebody reports us as reall
1504 // disconnected
1505 mDefaultInetConditionPublished = 0;
1506 mDefaultConnectionSequence++;
1507 mInetConditionChangeInFlight = false;
1508 // Don't do this - if we never sign in stay, grey
1509 //reportNetworkCondition(mActiveDefaultNetwork, 100);
Robert Greenwalt42acef32009-08-12 16:08:25 -07001510 }
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001511 thisNet.setTeardownRequested(false);
Irfan Sheriffd649c122010-06-09 15:39:36 -07001512 updateNetworkSettings(thisNet);
Robert Greenwaltec896c62011-06-15 12:22:07 -07001513 handleConnectivityChange(type, false);
Robert Greenwalt42acef32009-08-12 16:08:25 -07001514 sendConnectedBroadcast(info);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001515 }
1516
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001517 /**
Robert Greenwaltc76b8fa2010-07-23 15:46:26 -07001518 * After a change in the connectivity state of a network. We're mainly
1519 * concerned with making sure that the list of DNS servers is set up
1520 * according to which networks are connected, and ensuring that the
1521 * right routing table entries exist.
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001522 */
Robert Greenwaltec896c62011-06-15 12:22:07 -07001523 private void handleConnectivityChange(int netType, boolean doReset) {
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001524 /*
Robert Greenwalt42acef32009-08-12 16:08:25 -07001525 * If a non-default network is enabled, add the host routes that
Robert Greenwaltc76b8fa2010-07-23 15:46:26 -07001526 * will allow it's DNS servers to be accessed.
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001527 */
Robert Greenwaltc76b8fa2010-07-23 15:46:26 -07001528 handleDnsConfigurationChange(netType);
Robert Greenwalt42acef32009-08-12 16:08:25 -07001529
Robert Greenwaltc76b8fa2010-07-23 15:46:26 -07001530 if (mNetTrackers[netType].getNetworkInfo().isConnected()) {
Robert Greenwaltd55a6b42011-03-25 13:09:25 -07001531 if (mNetConfigs[netType].isDefault()) {
Robert Greenwalt434203a2010-10-11 16:00:27 -07001532 handleApplyDefaultProxy(netType);
Robert Greenwaltc76b8fa2010-07-23 15:46:26 -07001533 addDefaultRoute(mNetTrackers[netType]);
Robert Greenwalt42acef32009-08-12 16:08:25 -07001534 } else {
Robert Greenwalt13ec4062011-04-01 10:51:22 -07001535 // many radios add a default route even when we don't want one.
1536 // remove the default route unless we need it for our active network
1537 if (mActiveDefaultNetwork != -1) {
1538 LinkProperties defaultLinkProperties =
1539 mNetTrackers[mActiveDefaultNetwork].getLinkProperties();
1540 LinkProperties newLinkProperties =
1541 mNetTrackers[netType].getLinkProperties();
1542 String defaultIface = defaultLinkProperties.getInterfaceName();
1543 if (defaultIface != null &&
1544 !defaultIface.equals(newLinkProperties.getInterfaceName())) {
1545 removeDefaultRoute(mNetTrackers[netType]);
1546 }
1547 }
Michael Jurka8fad7ee2011-03-30 19:54:08 -07001548 addPrivateDnsRoutes(mNetTrackers[netType]);
Robert Greenwaltc76b8fa2010-07-23 15:46:26 -07001549 }
Kazuhiro Ondo01758e82011-04-30 20:10:57 -05001550
1551 /** Notify TetheringService if interface name has been changed. */
1552 if (TextUtils.equals(mNetTrackers[netType].getNetworkInfo().getReason(),
1553 Phone.REASON_LINK_PROPERTIES_CHANGED)) {
1554 handleTetherIfaceChange(netType);
1555 }
Robert Greenwaltc76b8fa2010-07-23 15:46:26 -07001556 } else {
Robert Greenwaltd55a6b42011-03-25 13:09:25 -07001557 if (mNetConfigs[netType].isDefault()) {
Robert Greenwaltc76b8fa2010-07-23 15:46:26 -07001558 removeDefaultRoute(mNetTrackers[netType]);
1559 } else {
1560 removePrivateDnsRoutes(mNetTrackers[netType]);
Robert Greenwalt42acef32009-08-12 16:08:25 -07001561 }
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001562 }
Robert Greenwaltec896c62011-06-15 12:22:07 -07001563
1564 if (doReset) {
1565 LinkProperties linkProperties = mNetTrackers[netType].getLinkProperties();
1566 if (linkProperties != null) {
1567 String iface = linkProperties.getInterfaceName();
1568 if (TextUtils.isEmpty(iface) == false) {
1569 if (DBG) log("resetConnections(" + iface + ")");
1570 NetworkUtils.resetConnections(iface);
1571 }
1572 }
1573 }
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001574 }
1575
Irfan Sheriffd649c122010-06-09 15:39:36 -07001576 private void addPrivateDnsRoutes(NetworkStateTracker nt) {
Irfan Sheriffd649c122010-06-09 15:39:36 -07001577 boolean privateDnsRouteSet = nt.isPrivateDnsRouteSet();
Robert Greenwalt37e65eb2010-08-30 10:56:47 -07001578 LinkProperties p = nt.getLinkProperties();
Robert Greenwalt47f69fe2010-06-15 15:43:39 -07001579 if (p == null) return;
1580 String interfaceName = p.getInterfaceName();
Irfan Sheriffd649c122010-06-09 15:39:36 -07001581
1582 if (DBG) {
Wink Savilleed9c02b2010-12-03 12:01:38 -08001583 log("addPrivateDnsRoutes for " + nt +
Irfan Sheriffd649c122010-06-09 15:39:36 -07001584 "(" + interfaceName + ") - mPrivateDnsRouteSet = " + privateDnsRouteSet);
1585 }
Irfan Sheriffd649c122010-06-09 15:39:36 -07001586 if (interfaceName != null && !privateDnsRouteSet) {
Robert Greenwalt47f69fe2010-06-15 15:43:39 -07001587 Collection<InetAddress> dnsList = p.getDnses();
1588 for (InetAddress dns : dnsList) {
Robert Greenwaltf43396c2011-05-06 17:10:53 -07001589 addHostRoute(nt, dns, 0);
Irfan Sheriffd649c122010-06-09 15:39:36 -07001590 }
1591 nt.privateDnsRouteSet(true);
1592 }
1593 }
1594
1595 private void removePrivateDnsRoutes(NetworkStateTracker nt) {
Robert Greenwalt37e65eb2010-08-30 10:56:47 -07001596 LinkProperties p = nt.getLinkProperties();
Robert Greenwalt47f69fe2010-06-15 15:43:39 -07001597 if (p == null) return;
1598 String interfaceName = p.getInterfaceName();
Irfan Sheriffd649c122010-06-09 15:39:36 -07001599 boolean privateDnsRouteSet = nt.isPrivateDnsRouteSet();
1600 if (interfaceName != null && privateDnsRouteSet) {
1601 if (DBG) {
Wink Savilleed9c02b2010-12-03 12:01:38 -08001602 log("removePrivateDnsRoutes for " + nt.getNetworkInfo().getTypeName() +
Irfan Sheriffd649c122010-06-09 15:39:36 -07001603 " (" + interfaceName + ")");
1604 }
Robert Greenwalt59b1a4e2011-05-10 15:05:02 -07001605
1606 Collection<InetAddress> dnsList = p.getDnses();
1607 for (InetAddress dns : dnsList) {
1608 if (DBG) log(" removing " + dns);
1609 RouteInfo route = RouteInfo.makeHostRoute(dns);
1610 try {
1611 mNetd.removeRoute(interfaceName, route);
1612 } catch (Exception ex) {
1613 loge("error (" + ex + ") removing dns route " + route);
1614 }
1615 }
Irfan Sheriffd649c122010-06-09 15:39:36 -07001616 nt.privateDnsRouteSet(false);
1617 }
1618 }
1619
Irfan Sheriffd649c122010-06-09 15:39:36 -07001620
1621 private void addDefaultRoute(NetworkStateTracker nt) {
Robert Greenwalt37e65eb2010-08-30 10:56:47 -07001622 LinkProperties p = nt.getLinkProperties();
Robert Greenwalt47f69fe2010-06-15 15:43:39 -07001623 if (p == null) return;
1624 String interfaceName = p.getInterfaceName();
Robert Greenwalt992564e2011-02-09 13:56:06 -08001625 if (TextUtils.isEmpty(interfaceName)) return;
Irfan Sheriffd649c122010-06-09 15:39:36 -07001626
Robert Greenwalt59b1a4e2011-05-10 15:05:02 -07001627 for (RouteInfo route : p.getRoutes()) {
Robert Greenwaltaa70f102011-04-28 14:28:50 -07001628 //TODO - handle non-default routes
1629 if (route.isDefaultRoute()) {
Robert Greenwalt59b1a4e2011-05-10 15:05:02 -07001630 if (DBG) log("adding default route " + route);
Robert Greenwaltaa70f102011-04-28 14:28:50 -07001631 InetAddress gateway = route.getGateway();
Robert Greenwalt59b1a4e2011-05-10 15:05:02 -07001632 if (addHostRoute(nt, gateway, 0)) {
1633 try {
1634 mNetd.addRoute(interfaceName, route);
1635 } catch (Exception e) {
1636 loge("error adding default route " + route);
1637 continue;
1638 }
Robert Greenwaltaa70f102011-04-28 14:28:50 -07001639 if (DBG) {
1640 NetworkInfo networkInfo = nt.getNetworkInfo();
1641 log("addDefaultRoute for " + networkInfo.getTypeName() +
1642 " (" + interfaceName + "), GatewayAddr=" +
1643 gateway.getHostAddress());
1644 }
Robert Greenwalt59b1a4e2011-05-10 15:05:02 -07001645 } else {
1646 loge("error adding host route for default route " + route);
Robert Greenwaltedcb4f92011-03-22 18:47:42 -07001647 }
Irfan Sheriffd649c122010-06-09 15:39:36 -07001648 }
Irfan Sheriffd649c122010-06-09 15:39:36 -07001649 }
1650 }
1651
1652
1653 public void removeDefaultRoute(NetworkStateTracker nt) {
Robert Greenwalt37e65eb2010-08-30 10:56:47 -07001654 LinkProperties p = nt.getLinkProperties();
Robert Greenwalt47f69fe2010-06-15 15:43:39 -07001655 if (p == null) return;
1656 String interfaceName = p.getInterfaceName();
Irfan Sheriffd649c122010-06-09 15:39:36 -07001657
Robert Greenwalt59b1a4e2011-05-10 15:05:02 -07001658 if (interfaceName == null) return;
1659
1660 for (RouteInfo route : p.getRoutes()) {
1661 //TODO - handle non-default routes
1662 if (route.isDefaultRoute()) {
1663 try {
1664 mNetd.removeRoute(interfaceName, route);
1665 } catch (Exception ex) {
1666 loge("error (" + ex + ") removing default route " + route);
1667 continue;
1668 }
Robert Greenwaltedcb4f92011-03-22 18:47:42 -07001669 if (DBG) {
1670 NetworkInfo networkInfo = nt.getNetworkInfo();
1671 log("removeDefaultRoute for " + networkInfo.getTypeName() + " (" +
1672 interfaceName + ")");
1673 }
Irfan Sheriffd649c122010-06-09 15:39:36 -07001674 }
Irfan Sheriffd649c122010-06-09 15:39:36 -07001675 }
1676 }
1677
1678 /**
1679 * Reads the network specific TCP buffer sizes from SystemProperties
1680 * net.tcp.buffersize.[default|wifi|umts|edge|gprs] and set them for system
1681 * wide use
1682 */
1683 public void updateNetworkSettings(NetworkStateTracker nt) {
1684 String key = nt.getTcpBufferSizesPropName();
1685 String bufferSizes = SystemProperties.get(key);
1686
1687 if (bufferSizes.length() == 0) {
Wink Savilleed9c02b2010-12-03 12:01:38 -08001688 loge(key + " not found in system properties. Using defaults");
Irfan Sheriffd649c122010-06-09 15:39:36 -07001689
1690 // Setting to default values so we won't be stuck to previous values
1691 key = "net.tcp.buffersize.default";
1692 bufferSizes = SystemProperties.get(key);
1693 }
1694
1695 // Set values in kernel
1696 if (bufferSizes.length() != 0) {
1697 if (DBG) {
Wink Savilleed9c02b2010-12-03 12:01:38 -08001698 log("Setting TCP values: [" + bufferSizes
Irfan Sheriffd649c122010-06-09 15:39:36 -07001699 + "] which comes from [" + key + "]");
1700 }
1701 setBufferSize(bufferSizes);
1702 }
1703 }
1704
1705 /**
1706 * Writes TCP buffer sizes to /sys/kernel/ipv4/tcp_[r/w]mem_[min/def/max]
1707 * which maps to /proc/sys/net/ipv4/tcp_rmem and tcpwmem
1708 *
1709 * @param bufferSizes in the format of "readMin, readInitial, readMax,
1710 * writeMin, writeInitial, writeMax"
1711 */
1712 private void setBufferSize(String bufferSizes) {
1713 try {
1714 String[] values = bufferSizes.split(",");
1715
1716 if (values.length == 6) {
1717 final String prefix = "/sys/kernel/ipv4/tcp_";
Mike Lockwoodda8bb742011-05-28 13:24:04 -04001718 FileUtils.stringToFile(prefix + "rmem_min", values[0]);
1719 FileUtils.stringToFile(prefix + "rmem_def", values[1]);
1720 FileUtils.stringToFile(prefix + "rmem_max", values[2]);
1721 FileUtils.stringToFile(prefix + "wmem_min", values[3]);
1722 FileUtils.stringToFile(prefix + "wmem_def", values[4]);
1723 FileUtils.stringToFile(prefix + "wmem_max", values[5]);
Irfan Sheriffd649c122010-06-09 15:39:36 -07001724 } else {
Wink Savilleed9c02b2010-12-03 12:01:38 -08001725 loge("Invalid buffersize string: " + bufferSizes);
Irfan Sheriffd649c122010-06-09 15:39:36 -07001726 }
1727 } catch (IOException e) {
Wink Savilleed9c02b2010-12-03 12:01:38 -08001728 loge("Can't set tcp buffer sizes:" + e);
Irfan Sheriffd649c122010-06-09 15:39:36 -07001729 }
1730 }
1731
Robert Greenwalt42acef32009-08-12 16:08:25 -07001732 /**
1733 * Adjust the per-process dns entries (net.dns<x>.<pid>) based
1734 * on the highest priority active net which this process requested.
1735 * If there aren't any, clear it out
1736 */
1737 private void reassessPidDns(int myPid, boolean doBump)
1738 {
Wink Savilleed9c02b2010-12-03 12:01:38 -08001739 if (DBG) log("reassessPidDns for pid " + myPid);
Robert Greenwalt42acef32009-08-12 16:08:25 -07001740 for(int i : mPriorityList) {
Robert Greenwaltd55a6b42011-03-25 13:09:25 -07001741 if (mNetConfigs[i].isDefault()) {
Robert Greenwalt42acef32009-08-12 16:08:25 -07001742 continue;
1743 }
1744 NetworkStateTracker nt = mNetTrackers[i];
Robert Greenwalt86e9e552009-07-16 17:21:39 -07001745 if (nt.getNetworkInfo().isConnected() &&
1746 !nt.isTeardownRequested()) {
Robert Greenwalt37e65eb2010-08-30 10:56:47 -07001747 LinkProperties p = nt.getLinkProperties();
Robert Greenwalt47f69fe2010-06-15 15:43:39 -07001748 if (p == null) continue;
Robert Greenwalt42acef32009-08-12 16:08:25 -07001749 List pids = mNetRequestersPids[i];
1750 for (int j=0; j<pids.size(); j++) {
1751 Integer pid = (Integer)pids.get(j);
1752 if (pid.intValue() == myPid) {
Robert Greenwalt47f69fe2010-06-15 15:43:39 -07001753 Collection<InetAddress> dnses = p.getDnses();
1754 writePidDns(dnses, myPid);
Robert Greenwalt42acef32009-08-12 16:08:25 -07001755 if (doBump) {
1756 bumpDns();
1757 }
1758 return;
1759 }
1760 }
1761 }
1762 }
1763 // nothing found - delete
1764 for (int i = 1; ; i++) {
1765 String prop = "net.dns" + i + "." + myPid;
1766 if (SystemProperties.get(prop).length() == 0) {
1767 if (doBump) {
1768 bumpDns();
1769 }
1770 return;
1771 }
1772 SystemProperties.set(prop, "");
1773 }
1774 }
1775
Robert Greenwalt10398722010-12-17 15:20:36 -08001776 // return true if results in a change
1777 private boolean writePidDns(Collection <InetAddress> dnses, int pid) {
Robert Greenwalt42acef32009-08-12 16:08:25 -07001778 int j = 1;
Robert Greenwalt10398722010-12-17 15:20:36 -08001779 boolean changed = false;
Robert Greenwalt47f69fe2010-06-15 15:43:39 -07001780 for (InetAddress dns : dnses) {
Robert Greenwalt10398722010-12-17 15:20:36 -08001781 String dnsString = dns.getHostAddress();
1782 if (changed || !dnsString.equals(SystemProperties.get("net.dns" + j + "." + pid))) {
1783 changed = true;
1784 SystemProperties.set("net.dns" + j++ + "." + pid, dns.getHostAddress());
1785 }
Robert Greenwalt42acef32009-08-12 16:08:25 -07001786 }
Robert Greenwalt10398722010-12-17 15:20:36 -08001787 return changed;
Robert Greenwalt42acef32009-08-12 16:08:25 -07001788 }
1789
1790 private void bumpDns() {
1791 /*
1792 * Bump the property that tells the name resolver library to reread
1793 * the DNS server list from the properties.
1794 */
1795 String propVal = SystemProperties.get("net.dnschange");
1796 int n = 0;
1797 if (propVal.length() != 0) {
1798 try {
1799 n = Integer.parseInt(propVal);
1800 } catch (NumberFormatException e) {}
1801 }
1802 SystemProperties.set("net.dnschange", "" + (n+1));
Robert Greenwalt03595d02010-11-02 14:08:23 -07001803 /*
1804 * Tell the VMs to toss their DNS caches
1805 */
1806 Intent intent = new Intent(Intent.ACTION_CLEAR_DNS_CACHE);
1807 intent.addFlags(Intent.FLAG_RECEIVER_REPLACE_PENDING);
Stan Chesnutt3d1db862011-01-05 17:14:03 -08001808 /*
1809 * Connectivity events can happen before boot has completed ...
1810 */
1811 intent.addFlags(Intent.FLAG_RECEIVER_REGISTERED_ONLY_BEFORE_BOOT);
Robert Greenwalt03595d02010-11-02 14:08:23 -07001812 mContext.sendBroadcast(intent);
Robert Greenwalt42acef32009-08-12 16:08:25 -07001813 }
1814
Robert Greenwaltc76b8fa2010-07-23 15:46:26 -07001815 private void handleDnsConfigurationChange(int netType) {
Robert Greenwalt42acef32009-08-12 16:08:25 -07001816 // add default net's dns entries
Robert Greenwaltc76b8fa2010-07-23 15:46:26 -07001817 NetworkStateTracker nt = mNetTrackers[netType];
1818 if (nt != null && nt.getNetworkInfo().isConnected() && !nt.isTeardownRequested()) {
Robert Greenwalt37e65eb2010-08-30 10:56:47 -07001819 LinkProperties p = nt.getLinkProperties();
Robert Greenwaltc76b8fa2010-07-23 15:46:26 -07001820 if (p == null) return;
1821 Collection<InetAddress> dnses = p.getDnses();
Robert Greenwalt10398722010-12-17 15:20:36 -08001822 boolean changed = false;
Robert Greenwaltd55a6b42011-03-25 13:09:25 -07001823 if (mNetConfigs[netType].isDefault()) {
Robert Greenwaltc76b8fa2010-07-23 15:46:26 -07001824 int j = 1;
Robert Greenwalte90aa5e2010-09-01 11:34:05 -07001825 if (dnses.size() == 0 && mDefaultDns != null) {
Robert Greenwalt10398722010-12-17 15:20:36 -08001826 String dnsString = mDefaultDns.getHostAddress();
1827 if (!dnsString.equals(SystemProperties.get("net.dns1"))) {
1828 if (DBG) {
1829 log("no dns provided - using " + dnsString);
1830 }
1831 changed = true;
1832 SystemProperties.set("net.dns1", dnsString);
Robert Greenwalt42acef32009-08-12 16:08:25 -07001833 }
Robert Greenwalte90aa5e2010-09-01 11:34:05 -07001834 j++;
1835 } else {
1836 for (InetAddress dns : dnses) {
Robert Greenwalt10398722010-12-17 15:20:36 -08001837 String dnsString = dns.getHostAddress();
1838 if (!changed && dnsString.equals(SystemProperties.get("net.dns" + j))) {
1839 j++;
1840 continue;
1841 }
Robert Greenwalte90aa5e2010-09-01 11:34:05 -07001842 if (DBG) {
Wink Savilleed9c02b2010-12-03 12:01:38 -08001843 log("adding dns " + dns + " for " +
Robert Greenwalte90aa5e2010-09-01 11:34:05 -07001844 nt.getNetworkInfo().getTypeName());
1845 }
Robert Greenwalt10398722010-12-17 15:20:36 -08001846 changed = true;
1847 SystemProperties.set("net.dns" + j++, dnsString);
Robert Greenwalte90aa5e2010-09-01 11:34:05 -07001848 }
Robert Greenwaltc76b8fa2010-07-23 15:46:26 -07001849 }
1850 for (int k=j ; k<mNumDnsEntries; k++) {
Robert Greenwalt10398722010-12-17 15:20:36 -08001851 if (changed || !TextUtils.isEmpty(SystemProperties.get("net.dns" + k))) {
1852 if (DBG) log("erasing net.dns" + k);
1853 changed = true;
1854 SystemProperties.set("net.dns" + k, "");
1855 }
Robert Greenwaltc76b8fa2010-07-23 15:46:26 -07001856 }
1857 mNumDnsEntries = j;
1858 } else {
1859 // set per-pid dns for attached secondary nets
1860 List pids = mNetRequestersPids[netType];
1861 for (int y=0; y< pids.size(); y++) {
1862 Integer pid = (Integer)pids.get(y);
Robert Greenwalt10398722010-12-17 15:20:36 -08001863 changed = writePidDns(dnses, pid.intValue());
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001864 }
1865 }
Robert Greenwalt10398722010-12-17 15:20:36 -08001866 if (changed) bumpDns();
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001867 }
Robert Greenwalt42acef32009-08-12 16:08:25 -07001868 }
1869
Robert Greenwaltf2102f72011-05-03 19:02:44 -07001870 private int getRestoreDefaultNetworkDelay(int networkType) {
Robert Greenwalt42acef32009-08-12 16:08:25 -07001871 String restoreDefaultNetworkDelayStr = SystemProperties.get(
1872 NETWORK_RESTORE_DELAY_PROP_NAME);
1873 if(restoreDefaultNetworkDelayStr != null &&
1874 restoreDefaultNetworkDelayStr.length() != 0) {
1875 try {
1876 return Integer.valueOf(restoreDefaultNetworkDelayStr);
1877 } catch (NumberFormatException e) {
1878 }
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001879 }
Robert Greenwaltf2102f72011-05-03 19:02:44 -07001880 // if the system property isn't set, use the value for the apn type
1881 int ret = RESTORE_DEFAULT_NETWORK_DELAY;
1882
1883 if ((networkType <= ConnectivityManager.MAX_NETWORK_TYPE) &&
1884 (mNetConfigs[networkType] != null)) {
1885 ret = mNetConfigs[networkType].restoreTime;
1886 }
1887 return ret;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001888 }
1889
1890 @Override
1891 protected void dump(FileDescriptor fd, PrintWriter pw, String[] args) {
Robert Greenwalt86e9e552009-07-16 17:21:39 -07001892 if (mContext.checkCallingOrSelfPermission(
1893 android.Manifest.permission.DUMP)
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001894 != PackageManager.PERMISSION_GRANTED) {
Robert Greenwalt86e9e552009-07-16 17:21:39 -07001895 pw.println("Permission Denial: can't dump ConnectivityService " +
1896 "from from pid=" + Binder.getCallingPid() + ", uid=" +
1897 Binder.getCallingUid());
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001898 return;
1899 }
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001900 pw.println();
1901 for (NetworkStateTracker nst : mNetTrackers) {
Robert Greenwaltb9285352009-12-21 18:24:07 -08001902 if (nst != null) {
1903 if (nst.getNetworkInfo().isConnected()) {
1904 pw.println("Active network: " + nst.getNetworkInfo().
1905 getTypeName());
1906 }
1907 pw.println(nst.getNetworkInfo());
1908 pw.println(nst);
1909 pw.println();
Robert Greenwalt42acef32009-08-12 16:08:25 -07001910 }
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001911 }
Robert Greenwaltb9285352009-12-21 18:24:07 -08001912
1913 pw.println("Network Requester Pids:");
1914 for (int net : mPriorityList) {
1915 String pidString = net + ": ";
1916 for (Object pid : mNetRequestersPids[net]) {
1917 pidString = pidString + pid.toString() + ", ";
1918 }
1919 pw.println(pidString);
1920 }
1921 pw.println();
1922
1923 pw.println("FeatureUsers:");
1924 for (Object requester : mFeatureUsers) {
1925 pw.println(requester.toString());
1926 }
1927 pw.println();
Robert Greenwalt2a091d72010-02-11 18:18:40 -08001928
Robert Greenwalt14f2ef42010-06-15 12:19:37 -07001929 synchronized (this) {
1930 pw.println("NetworkTranstionWakeLock is currently " +
1931 (mNetTransitionWakeLock.isHeld() ? "" : "not ") + "held.");
1932 pw.println("It was last requested for "+mNetTransitionWakeLockCausedBy);
1933 }
1934 pw.println();
1935
Robert Greenwalt2a091d72010-02-11 18:18:40 -08001936 mTethering.dump(fd, pw, args);
Robert Greenwalt4e8dfef2010-09-20 14:35:25 -07001937
1938 if (mInetLog != null) {
1939 pw.println();
1940 pw.println("Inet condition reports:");
1941 for(int i = 0; i < mInetLog.size(); i++) {
1942 pw.println(mInetLog.get(i));
1943 }
1944 }
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001945 }
1946
Robert Greenwalt42acef32009-08-12 16:08:25 -07001947 // must be stateless - things change under us.
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001948 private class MyHandler extends Handler {
Wink Savillebb08caf2010-09-02 19:23:52 -07001949 public MyHandler(Looper looper) {
1950 super(looper);
1951 }
1952
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001953 @Override
1954 public void handleMessage(Message msg) {
1955 NetworkInfo info;
1956 switch (msg.what) {
1957 case NetworkStateTracker.EVENT_STATE_CHANGED:
1958 info = (NetworkInfo) msg.obj;
Robert Greenwalt511288a2009-12-07 11:33:18 -08001959 int type = info.getType();
1960 NetworkInfo.State state = info.getState();
Robert Greenwalt511288a2009-12-07 11:33:18 -08001961
Wink Savilleed9c02b2010-12-03 12:01:38 -08001962 if (DBG) log("ConnectivityChange for " +
Robert Greenwalt86e9e552009-07-16 17:21:39 -07001963 info.getTypeName() + ": " +
Robert Greenwalt511288a2009-12-07 11:33:18 -08001964 state + "/" + info.getDetailedState());
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001965
1966 // Connectivity state changed:
1967 // [31-13] Reserved for future use
Robert Greenwalt86e9e552009-07-16 17:21:39 -07001968 // [12-9] Network subtype (for mobile network, as defined
1969 // by TelephonyManager)
1970 // [8-3] Detailed state ordinal (as defined by
1971 // NetworkInfo.DetailedState)
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001972 // [2-0] Network type (as defined by ConnectivityManager)
1973 int eventLogParam = (info.getType() & 0x7) |
1974 ((info.getDetailedState().ordinal() & 0x3f) << 3) |
1975 (info.getSubtype() << 9);
Doug Zongkerab5c49c2009-12-04 10:31:43 -08001976 EventLog.writeEvent(EventLogTags.CONNECTIVITY_STATE_CHANGED,
Robert Greenwalt86e9e552009-07-16 17:21:39 -07001977 eventLogParam);
1978
1979 if (info.getDetailedState() ==
1980 NetworkInfo.DetailedState.FAILED) {
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001981 handleConnectionFailure(info);
Robert Greenwalt511288a2009-12-07 11:33:18 -08001982 } else if (state == NetworkInfo.State.DISCONNECTED) {
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001983 handleDisconnect(info);
Robert Greenwalt511288a2009-12-07 11:33:18 -08001984 } else if (state == NetworkInfo.State.SUSPENDED) {
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001985 // TODO: need to think this over.
Robert Greenwalt86e9e552009-07-16 17:21:39 -07001986 // the logic here is, handle SUSPENDED the same as
1987 // DISCONNECTED. The only difference being we are
1988 // broadcasting an intent with NetworkInfo that's
1989 // suspended. This allows the applications an
1990 // opportunity to handle DISCONNECTED and SUSPENDED
1991 // differently, or not.
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001992 handleDisconnect(info);
Robert Greenwalt511288a2009-12-07 11:33:18 -08001993 } else if (state == NetworkInfo.State.CONNECTED) {
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001994 handleConnect(info);
1995 }
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001996 break;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001997 case NetworkStateTracker.EVENT_CONFIGURATION_CHANGED:
Robert Greenwaltc76b8fa2010-07-23 15:46:26 -07001998 info = (NetworkInfo) msg.obj;
Robert Greenwaltec896c62011-06-15 12:22:07 -07001999 handleConnectivityChange(info.getType(), true);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002000 break;
Robert Greenwaltf3331232010-09-24 14:32:21 -07002001 case EVENT_CLEAR_NET_TRANSITION_WAKELOCK:
Robert Greenwalt14f2ef42010-06-15 12:19:37 -07002002 String causedBy = null;
2003 synchronized (ConnectivityService.this) {
2004 if (msg.arg1 == mNetTransitionWakeLockSerialNumber &&
2005 mNetTransitionWakeLock.isHeld()) {
2006 mNetTransitionWakeLock.release();
2007 causedBy = mNetTransitionWakeLockCausedBy;
2008 }
2009 }
2010 if (causedBy != null) {
Wink Savilleed9c02b2010-12-03 12:01:38 -08002011 log("NetTransition Wakelock for " + causedBy + " released by timeout");
Robert Greenwalt14f2ef42010-06-15 12:19:37 -07002012 }
Robert Greenwalt057d5e92010-09-09 14:05:10 -07002013 break;
Robert Greenwalt8dcc28b2010-09-23 10:05:56 -07002014 case EVENT_RESTORE_DEFAULT_NETWORK:
Robert Greenwalt9c75d4a2009-09-27 17:27:04 -07002015 FeatureUser u = (FeatureUser)msg.obj;
2016 u.expire();
Robert Greenwaltd7085fc2010-09-08 15:24:47 -07002017 break;
Robert Greenwalt8dcc28b2010-09-23 10:05:56 -07002018 case EVENT_INET_CONDITION_CHANGE:
2019 {
2020 int netType = msg.arg1;
2021 int condition = msg.arg2;
2022 handleInetConditionChange(netType, condition);
Robert Greenwalt14f2ef42010-06-15 12:19:37 -07002023 break;
Robert Greenwalt8dcc28b2010-09-23 10:05:56 -07002024 }
2025 case EVENT_INET_CONDITION_HOLD_END:
2026 {
2027 int netType = msg.arg1;
2028 int sequence = msg.arg2;
2029 handleInetConditionHoldEnd(netType, sequence);
Robert Greenwaltd7085fc2010-09-08 15:24:47 -07002030 break;
Robert Greenwalt8dcc28b2010-09-23 10:05:56 -07002031 }
2032 case EVENT_SET_NETWORK_PREFERENCE:
2033 {
2034 int preference = msg.arg1;
2035 handleSetNetworkPreference(preference);
2036 break;
2037 }
2038 case EVENT_SET_BACKGROUND_DATA:
2039 {
2040 boolean enabled = (msg.arg1 == ENABLED);
2041 handleSetBackgroundData(enabled);
2042 break;
2043 }
2044 case EVENT_SET_MOBILE_DATA:
2045 {
2046 boolean enabled = (msg.arg1 == ENABLED);
2047 handleSetMobileData(enabled);
2048 break;
2049 }
Robert Greenwalt434203a2010-10-11 16:00:27 -07002050 case EVENT_APPLY_GLOBAL_HTTP_PROXY:
2051 {
2052 handleDeprecatedGlobalHttpProxy();
Robert Greenwaltd55a6b42011-03-25 13:09:25 -07002053 break;
2054 }
2055 case EVENT_SET_DEPENDENCY_MET:
2056 {
2057 boolean met = (msg.arg1 == ENABLED);
2058 handleSetDependencyMet(msg.arg2, met);
2059 break;
Robert Greenwalt434203a2010-10-11 16:00:27 -07002060 }
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002061 }
2062 }
2063 }
Robert Greenwaltd0e18ff2010-01-26 11:40:34 -08002064
2065 // javadoc from interface
Robert Greenwalt5a735062010-03-02 17:25:02 -08002066 public int tether(String iface) {
Robert Greenwaltd0e18ff2010-01-26 11:40:34 -08002067 enforceTetherChangePermission();
Robert Greenwalt5a735062010-03-02 17:25:02 -08002068
2069 if (isTetheringSupported()) {
2070 return mTethering.tether(iface);
2071 } else {
2072 return ConnectivityManager.TETHER_ERROR_UNSUPPORTED;
2073 }
Robert Greenwaltd0e18ff2010-01-26 11:40:34 -08002074 }
2075
2076 // javadoc from interface
Robert Greenwalt5a735062010-03-02 17:25:02 -08002077 public int untether(String iface) {
Robert Greenwaltd0e18ff2010-01-26 11:40:34 -08002078 enforceTetherChangePermission();
Robert Greenwalt5a735062010-03-02 17:25:02 -08002079
2080 if (isTetheringSupported()) {
2081 return mTethering.untether(iface);
2082 } else {
2083 return ConnectivityManager.TETHER_ERROR_UNSUPPORTED;
2084 }
2085 }
2086
2087 // javadoc from interface
2088 public int getLastTetherError(String iface) {
2089 enforceTetherAccessPermission();
2090
2091 if (isTetheringSupported()) {
2092 return mTethering.getLastTetherError(iface);
2093 } else {
2094 return ConnectivityManager.TETHER_ERROR_UNSUPPORTED;
2095 }
Robert Greenwalt2a091d72010-02-11 18:18:40 -08002096 }
2097
2098 // TODO - proper iface API for selection by property, inspection, etc
2099 public String[] getTetherableUsbRegexs() {
2100 enforceTetherAccessPermission();
2101 if (isTetheringSupported()) {
2102 return mTethering.getTetherableUsbRegexs();
2103 } else {
2104 return new String[0];
2105 }
2106 }
2107
2108 public String[] getTetherableWifiRegexs() {
2109 enforceTetherAccessPermission();
2110 if (isTetheringSupported()) {
2111 return mTethering.getTetherableWifiRegexs();
2112 } else {
2113 return new String[0];
2114 }
Robert Greenwaltd0e18ff2010-01-26 11:40:34 -08002115 }
2116
Danica Chang6fdd0c62010-08-11 14:54:43 -07002117 public String[] getTetherableBluetoothRegexs() {
2118 enforceTetherAccessPermission();
2119 if (isTetheringSupported()) {
2120 return mTethering.getTetherableBluetoothRegexs();
2121 } else {
2122 return new String[0];
2123 }
2124 }
2125
Robert Greenwaltd0e18ff2010-01-26 11:40:34 -08002126 // TODO - move iface listing, queries, etc to new module
2127 // javadoc from interface
2128 public String[] getTetherableIfaces() {
Robert Greenwalt2a091d72010-02-11 18:18:40 -08002129 enforceTetherAccessPermission();
Robert Greenwaltd0e18ff2010-01-26 11:40:34 -08002130 return mTethering.getTetherableIfaces();
2131 }
2132
2133 public String[] getTetheredIfaces() {
Robert Greenwalt2a091d72010-02-11 18:18:40 -08002134 enforceTetherAccessPermission();
Robert Greenwaltd0e18ff2010-01-26 11:40:34 -08002135 return mTethering.getTetheredIfaces();
2136 }
Robert Greenwalt2a091d72010-02-11 18:18:40 -08002137
Robert Greenwalt5a735062010-03-02 17:25:02 -08002138 public String[] getTetheringErroredIfaces() {
2139 enforceTetherAccessPermission();
2140 return mTethering.getErroredIfaces();
2141 }
2142
Robert Greenwalt2a091d72010-02-11 18:18:40 -08002143 // if ro.tether.denied = true we default to no tethering
2144 // gservices could set the secure setting to 1 though to enable it on a build where it
2145 // had previously been turned off.
2146 public boolean isTetheringSupported() {
2147 enforceTetherAccessPermission();
2148 int defaultVal = (SystemProperties.get("ro.tether.denied").equals("true") ? 0 : 1);
Robert Greenwaltc9d5fb72010-02-25 12:29:30 -08002149 boolean tetherEnabledInSettings = (Settings.Secure.getInt(mContext.getContentResolver(),
2150 Settings.Secure.TETHER_SUPPORTED, defaultVal) != 0);
2151 return tetherEnabledInSettings && mTetheringConfigValid;
Robert Greenwalt2a091d72010-02-11 18:18:40 -08002152 }
Robert Greenwalt14f2ef42010-06-15 12:19:37 -07002153
2154 // An API NetworkStateTrackers can call when they lose their network.
2155 // This will automatically be cleared after X seconds or a network becomes CONNECTED,
2156 // whichever happens first. The timer is started by the first caller and not
2157 // restarted by subsequent callers.
2158 public void requestNetworkTransitionWakelock(String forWhom) {
2159 enforceConnectivityInternalPermission();
2160 synchronized (this) {
2161 if (mNetTransitionWakeLock.isHeld()) return;
2162 mNetTransitionWakeLockSerialNumber++;
2163 mNetTransitionWakeLock.acquire();
2164 mNetTransitionWakeLockCausedBy = forWhom;
2165 }
2166 mHandler.sendMessageDelayed(mHandler.obtainMessage(
Robert Greenwaltf3331232010-09-24 14:32:21 -07002167 EVENT_CLEAR_NET_TRANSITION_WAKELOCK,
Robert Greenwalt14f2ef42010-06-15 12:19:37 -07002168 mNetTransitionWakeLockSerialNumber, 0),
2169 mNetTransitionWakeLockTimeout);
2170 return;
2171 }
Robert Greenwaltca4306c2010-09-09 13:15:32 -07002172
Robert Greenwaltd7085fc2010-09-08 15:24:47 -07002173 // 100 percent is full good, 0 is full bad.
2174 public void reportInetCondition(int networkType, int percentage) {
Wink Savilleed9c02b2010-12-03 12:01:38 -08002175 if (DBG) log("reportNetworkCondition(" + networkType + ", " + percentage + ")");
Robert Greenwaltd7085fc2010-09-08 15:24:47 -07002176 mContext.enforceCallingOrSelfPermission(
2177 android.Manifest.permission.STATUS_BAR,
2178 "ConnectivityService");
2179
Robert Greenwalt4e8dfef2010-09-20 14:35:25 -07002180 if (DBG) {
2181 int pid = getCallingPid();
2182 int uid = getCallingUid();
2183 String s = pid + "(" + uid + ") reports inet is " +
2184 (percentage > 50 ? "connected" : "disconnected") + " (" + percentage + ") on " +
2185 "network Type " + networkType + " at " + GregorianCalendar.getInstance().getTime();
2186 mInetLog.add(s);
2187 while(mInetLog.size() > INET_CONDITION_LOG_MAX_SIZE) {
2188 mInetLog.remove(0);
2189 }
2190 }
Robert Greenwaltd7085fc2010-09-08 15:24:47 -07002191 mHandler.sendMessage(mHandler.obtainMessage(
Robert Greenwalt8dcc28b2010-09-23 10:05:56 -07002192 EVENT_INET_CONDITION_CHANGE, networkType, percentage));
2193 }
2194
2195 private void handleInetConditionChange(int netType, int condition) {
2196 if (DBG) {
Wink Savilleed9c02b2010-12-03 12:01:38 -08002197 log("Inet connectivity change, net=" +
Robert Greenwalt8dcc28b2010-09-23 10:05:56 -07002198 netType + ", condition=" + condition +
2199 ",mActiveDefaultNetwork=" + mActiveDefaultNetwork);
2200 }
2201 if (mActiveDefaultNetwork == -1) {
Wink Savilleed9c02b2010-12-03 12:01:38 -08002202 if (DBG) log("no active default network - aborting");
Robert Greenwalt8dcc28b2010-09-23 10:05:56 -07002203 return;
2204 }
2205 if (mActiveDefaultNetwork != netType) {
Wink Savilleed9c02b2010-12-03 12:01:38 -08002206 if (DBG) log("given net not default - aborting");
Robert Greenwalt8dcc28b2010-09-23 10:05:56 -07002207 return;
2208 }
2209 mDefaultInetCondition = condition;
2210 int delay;
2211 if (mInetConditionChangeInFlight == false) {
Wink Savilleed9c02b2010-12-03 12:01:38 -08002212 if (DBG) log("starting a change hold");
Robert Greenwalt8dcc28b2010-09-23 10:05:56 -07002213 // setup a new hold to debounce this
2214 if (mDefaultInetCondition > 50) {
2215 delay = Settings.Secure.getInt(mContext.getContentResolver(),
2216 Settings.Secure.INET_CONDITION_DEBOUNCE_UP_DELAY, 500);
2217 } else {
2218 delay = Settings.Secure.getInt(mContext.getContentResolver(),
2219 Settings.Secure.INET_CONDITION_DEBOUNCE_DOWN_DELAY, 3000);
2220 }
2221 mInetConditionChangeInFlight = true;
2222 mHandler.sendMessageDelayed(mHandler.obtainMessage(EVENT_INET_CONDITION_HOLD_END,
2223 mActiveDefaultNetwork, mDefaultConnectionSequence), delay);
2224 } else {
2225 // we've set the new condition, when this hold ends that will get
2226 // picked up
Wink Savilleed9c02b2010-12-03 12:01:38 -08002227 if (DBG) log("currently in hold - not setting new end evt");
Robert Greenwalt8dcc28b2010-09-23 10:05:56 -07002228 }
2229 }
2230
2231 private void handleInetConditionHoldEnd(int netType, int sequence) {
2232 if (DBG) {
Wink Savilleed9c02b2010-12-03 12:01:38 -08002233 log("Inet hold end, net=" + netType +
Robert Greenwalt8dcc28b2010-09-23 10:05:56 -07002234 ", condition =" + mDefaultInetCondition +
2235 ", published condition =" + mDefaultInetConditionPublished);
2236 }
2237 mInetConditionChangeInFlight = false;
2238
2239 if (mActiveDefaultNetwork == -1) {
Wink Savilleed9c02b2010-12-03 12:01:38 -08002240 if (DBG) log("no active default network - aborting");
Robert Greenwalt8dcc28b2010-09-23 10:05:56 -07002241 return;
2242 }
2243 if (mDefaultConnectionSequence != sequence) {
Wink Savilleed9c02b2010-12-03 12:01:38 -08002244 if (DBG) log("event hold for obsolete network - aborting");
Robert Greenwalt8dcc28b2010-09-23 10:05:56 -07002245 return;
2246 }
2247 if (mDefaultInetConditionPublished == mDefaultInetCondition) {
Wink Savilleed9c02b2010-12-03 12:01:38 -08002248 if (DBG) log("no change in condition - aborting");
Robert Greenwalt8dcc28b2010-09-23 10:05:56 -07002249 return;
2250 }
2251 NetworkInfo networkInfo = mNetTrackers[mActiveDefaultNetwork].getNetworkInfo();
2252 if (networkInfo.isConnected() == false) {
Wink Savilleed9c02b2010-12-03 12:01:38 -08002253 if (DBG) log("default network not connected - aborting");
Robert Greenwalt8dcc28b2010-09-23 10:05:56 -07002254 return;
2255 }
2256 mDefaultInetConditionPublished = mDefaultInetCondition;
2257 sendInetConditionBroadcast(networkInfo);
2258 return;
Robert Greenwaltd7085fc2010-09-08 15:24:47 -07002259 }
Robert Greenwalt434203a2010-10-11 16:00:27 -07002260
2261 public synchronized ProxyProperties getProxy() {
2262 if (mGlobalProxy != null) return mGlobalProxy;
2263 if (mDefaultProxy != null) return mDefaultProxy;
2264 return null;
2265 }
2266
2267 public void setGlobalProxy(ProxyProperties proxyProperties) {
2268 enforceChangePermission();
2269 synchronized (mGlobalProxyLock) {
2270 if (proxyProperties == mGlobalProxy) return;
2271 if (proxyProperties != null && proxyProperties.equals(mGlobalProxy)) return;
2272 if (mGlobalProxy != null && mGlobalProxy.equals(proxyProperties)) return;
2273
2274 String host = "";
2275 int port = 0;
2276 String exclList = "";
2277 if (proxyProperties != null && !TextUtils.isEmpty(proxyProperties.getHost())) {
2278 mGlobalProxy = new ProxyProperties(proxyProperties);
2279 host = mGlobalProxy.getHost();
2280 port = mGlobalProxy.getPort();
2281 exclList = mGlobalProxy.getExclusionList();
2282 } else {
2283 mGlobalProxy = null;
2284 }
2285 ContentResolver res = mContext.getContentResolver();
2286 Settings.Secure.putString(res, Settings.Secure.GLOBAL_HTTP_PROXY_HOST, host);
2287 Settings.Secure.putInt(res, Settings.Secure.GLOBAL_HTTP_PROXY_PORT, port);
Robert Greenwaltb7090d62010-12-02 11:31:00 -08002288 Settings.Secure.putString(res, Settings.Secure.GLOBAL_HTTP_PROXY_EXCLUSION_LIST,
Robert Greenwalt434203a2010-10-11 16:00:27 -07002289 exclList);
2290 }
2291
2292 if (mGlobalProxy == null) {
2293 proxyProperties = mDefaultProxy;
2294 }
2295 sendProxyBroadcast(proxyProperties);
2296 }
2297
Robert Greenwaltb7090d62010-12-02 11:31:00 -08002298 private void loadGlobalProxy() {
2299 ContentResolver res = mContext.getContentResolver();
2300 String host = Settings.Secure.getString(res, Settings.Secure.GLOBAL_HTTP_PROXY_HOST);
2301 int port = Settings.Secure.getInt(res, Settings.Secure.GLOBAL_HTTP_PROXY_PORT, 0);
2302 String exclList = Settings.Secure.getString(res,
2303 Settings.Secure.GLOBAL_HTTP_PROXY_EXCLUSION_LIST);
2304 if (!TextUtils.isEmpty(host)) {
2305 ProxyProperties proxyProperties = new ProxyProperties(host, port, exclList);
2306 synchronized (mGlobalProxyLock) {
2307 mGlobalProxy = proxyProperties;
2308 }
2309 }
2310 }
2311
Robert Greenwalt434203a2010-10-11 16:00:27 -07002312 public ProxyProperties getGlobalProxy() {
2313 synchronized (mGlobalProxyLock) {
2314 return mGlobalProxy;
2315 }
2316 }
2317
2318 private void handleApplyDefaultProxy(int type) {
2319 // check if new default - push it out to all VM if so
2320 ProxyProperties proxy = mNetTrackers[type].getLinkProperties().getHttpProxy();
2321 synchronized (this) {
2322 if (mDefaultProxy != null && mDefaultProxy.equals(proxy)) return;
2323 if (mDefaultProxy == proxy) return;
2324 if (!TextUtils.isEmpty(proxy.getHost())) {
2325 mDefaultProxy = proxy;
2326 } else {
2327 mDefaultProxy = null;
2328 }
2329 }
Wink Savilleed9c02b2010-12-03 12:01:38 -08002330 if (DBG) log("changing default proxy to " + proxy);
Robert Greenwalt434203a2010-10-11 16:00:27 -07002331 if ((proxy == null && mGlobalProxy == null) || proxy.equals(mGlobalProxy)) return;
2332 if (mGlobalProxy != null) return;
2333 sendProxyBroadcast(proxy);
2334 }
2335
2336 private void handleDeprecatedGlobalHttpProxy() {
2337 String proxy = Settings.Secure.getString(mContext.getContentResolver(),
2338 Settings.Secure.HTTP_PROXY);
2339 if (!TextUtils.isEmpty(proxy)) {
2340 String data[] = proxy.split(":");
2341 String proxyHost = data[0];
2342 int proxyPort = 8080;
2343 if (data.length > 1) {
2344 try {
2345 proxyPort = Integer.parseInt(data[1]);
2346 } catch (NumberFormatException e) {
2347 return;
2348 }
2349 }
2350 ProxyProperties p = new ProxyProperties(data[0], proxyPort, "");
2351 setGlobalProxy(p);
2352 }
2353 }
2354
2355 private void sendProxyBroadcast(ProxyProperties proxy) {
Robert Greenwalt55985be2010-12-23 15:51:10 -08002356 if (proxy == null) proxy = new ProxyProperties("", 0, "");
Wink Savilleed9c02b2010-12-03 12:01:38 -08002357 log("sending Proxy Broadcast for " + proxy);
Robert Greenwalt434203a2010-10-11 16:00:27 -07002358 Intent intent = new Intent(Proxy.PROXY_CHANGE_ACTION);
Stan Chesnuttb35d67a2011-01-06 11:00:19 -08002359 intent.addFlags(Intent.FLAG_RECEIVER_REPLACE_PENDING |
2360 Intent.FLAG_RECEIVER_REGISTERED_ONLY_BEFORE_BOOT);
Robert Greenwalt434203a2010-10-11 16:00:27 -07002361 intent.putExtra(Proxy.EXTRA_PROXY_INFO, proxy);
Robert Greenwalta2e13392010-12-06 11:29:17 -08002362 mContext.sendStickyBroadcast(intent);
Robert Greenwalt434203a2010-10-11 16:00:27 -07002363 }
2364
2365 private static class SettingsObserver extends ContentObserver {
2366 private int mWhat;
2367 private Handler mHandler;
2368 SettingsObserver(Handler handler, int what) {
2369 super(handler);
2370 mHandler = handler;
2371 mWhat = what;
2372 }
2373
2374 void observe(Context context) {
2375 ContentResolver resolver = context.getContentResolver();
2376 resolver.registerContentObserver(Settings.Secure.getUriFor(
2377 Settings.Secure.HTTP_PROXY), false, this);
2378 }
2379
2380 @Override
2381 public void onChange(boolean selfChange) {
2382 mHandler.obtainMessage(mWhat).sendToTarget();
2383 }
2384 }
Wink Savilleed9c02b2010-12-03 12:01:38 -08002385
Kazuhiro Ondo01758e82011-04-30 20:10:57 -05002386 private void handleTetherIfaceChange(int type) {
2387 String iface = mNetTrackers[type].getLinkProperties().getInterfaceName();
2388
2389 if (isTetheringSupported()) {
2390 mTethering.handleTetherIfaceChange(iface);
2391 }
2392 }
2393
Wink Savilleed9c02b2010-12-03 12:01:38 -08002394 private void log(String s) {
2395 Slog.d(TAG, s);
2396 }
2397
2398 private void loge(String s) {
2399 Slog.e(TAG, s);
2400 }
Chia-chi Yehff3bdca2011-05-23 17:26:46 -07002401
Wink Saville2b8bcfe2011-02-24 17:58:51 -08002402 int convertFeatureToNetworkType(String feature){
2403 int networkType = -1;
2404 if (TextUtils.equals(feature, Phone.FEATURE_ENABLE_MMS)) {
2405 networkType = ConnectivityManager.TYPE_MOBILE_MMS;
2406 } else if (TextUtils.equals(feature, Phone.FEATURE_ENABLE_SUPL)) {
2407 networkType = ConnectivityManager.TYPE_MOBILE_SUPL;
2408 } else if (TextUtils.equals(feature, Phone.FEATURE_ENABLE_DUN) ||
2409 TextUtils.equals(feature, Phone.FEATURE_ENABLE_DUN_ALWAYS)) {
2410 networkType = ConnectivityManager.TYPE_MOBILE_DUN;
2411 } else if (TextUtils.equals(feature, Phone.FEATURE_ENABLE_HIPRI)) {
2412 networkType = ConnectivityManager.TYPE_MOBILE_HIPRI;
2413 } else if (TextUtils.equals(feature, Phone.FEATURE_ENABLE_FOTA)) {
2414 networkType = ConnectivityManager.TYPE_MOBILE_FOTA;
2415 } else if (TextUtils.equals(feature, Phone.FEATURE_ENABLE_IMS)) {
2416 networkType = ConnectivityManager.TYPE_MOBILE_IMS;
2417 } else if (TextUtils.equals(feature, Phone.FEATURE_ENABLE_CBS)) {
2418 networkType = ConnectivityManager.TYPE_MOBILE_CBS;
2419 }
2420 return networkType;
2421 }
Jeff Sharkeyc006f1a2011-05-19 17:12:49 -07002422
2423 private static <T> T checkNotNull(T value, String message) {
2424 if (value == null) {
2425 throw new NullPointerException(message);
2426 }
2427 return value;
2428 }
Chia-chi Yehff3bdca2011-05-23 17:26:46 -07002429
Chia-chi Yeh04ba25c2011-06-15 17:07:27 -07002430 /**
2431 * Protect a socket from VPN routing rules. This method is used by
2432 * VpnBuilder and not available in ConnectivityManager. Permission
2433 * checks are done in Vpn class.
2434 * @hide
2435 */
Chia-chi Yehff3bdca2011-05-23 17:26:46 -07002436 @Override
2437 public void protectVpn(ParcelFileDescriptor socket) {
2438 mVpn.protect(socket, getDefaultInterface());
2439 }
2440
Chia-chi Yeh04ba25c2011-06-15 17:07:27 -07002441 /**
2442 * Prepare for a VPN application. This method is used by VpnDialogs
2443 * and not available in ConnectivityManager. Permission checks are
2444 * done in Vpn class.
2445 * @hide
2446 */
Chia-chi Yehff3bdca2011-05-23 17:26:46 -07002447 @Override
2448 public String prepareVpn(String packageName) {
2449 return mVpn.prepare(packageName);
2450 }
2451
Chia-chi Yeh04ba25c2011-06-15 17:07:27 -07002452 /**
2453 * Configure a TUN interface and return its file descriptor. Parameters
2454 * are encoded and opaque to this class. This method is used by VpnBuilder
2455 * and not available in ConnectivityManager. Permission checks are done
2456 * in Vpn class.
2457 * @hide
2458 */
Chia-chi Yehff3bdca2011-05-23 17:26:46 -07002459 @Override
Chia-chi Yeh04ba25c2011-06-15 17:07:27 -07002460 public ParcelFileDescriptor establishVpn(VpnConfig config) {
Chia-chi Yehff3bdca2011-05-23 17:26:46 -07002461 return mVpn.establish(config);
2462 }
2463
2464 private String getDefaultInterface() {
2465 if (ConnectivityManager.isNetworkTypeValid(mActiveDefaultNetwork)) {
2466 NetworkStateTracker tracker = mNetTrackers[mActiveDefaultNetwork];
2467 if (tracker != null) {
2468 LinkProperties properties = tracker.getLinkProperties();
2469 if (properties != null) {
2470 return properties.getInterfaceName();
2471 }
2472 }
2473 }
2474 throw new IllegalStateException("No default interface");
2475 }
2476
2477 /**
2478 * Callback for VPN subsystem. Currently VPN is not adapted to the service
2479 * through NetworkStateTracker since it works differently. For example, it
2480 * needs to override DNS servers but never takes the default routes. It
2481 * relies on another data network, and it could keep existing connections
2482 * alive after reconnecting, switching between networks, or even resuming
2483 * from deep sleep. Calls from applications should be done synchronously
2484 * to avoid race conditions. As these are all hidden APIs, refactoring can
2485 * be done whenever a better abstraction is developed.
2486 */
2487 public class VpnCallback {
2488
2489 private VpnCallback() {
2490 }
2491
2492 public synchronized void override(String[] dnsServers) {
2493 // TODO: override DNS servers and http proxy.
2494 }
2495
2496 public synchronized void restore() {
2497 // TODO: restore VPN changes.
2498 }
2499 }
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002500}