blob: 7812927428074b0168c563a325b7e4d81d9e4660 [file] [log] [blame]
The Android Open Source Project28527d22009-03-03 19:31:44 -08001/*
2 * Copyright (C) 2008 The Android Open Source Project
3 *
4 * Licensed under the Apache License, Version 2.0 (the "License");
5 * you may not use this file except in compliance with the License.
6 * You may obtain a copy of the License at
7 *
8 * http://www.apache.org/licenses/LICENSE-2.0
9 *
10 * Unless required by applicable law or agreed to in writing, software
11 * distributed under the License is distributed on an "AS IS" BASIS,
12 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13 * See the License for the specific language governing permissions and
14 * limitations under the License.
15 */
16
17package com.android.server;
18
Jeff Sharkey4434b0b2011-06-16 13:04:20 -070019import static android.Manifest.permission.MANAGE_NETWORK_POLICY;
Jeff Sharkey921ebf22011-05-19 17:12:49 -070020import static android.net.ConnectivityManager.isNetworkTypeValid;
21import static android.net.NetworkPolicyManager.RULE_ALLOW_ALL;
Jeff Sharkeya47d7a12011-06-16 15:07:48 -070022import static android.net.NetworkPolicyManager.RULE_REJECT_METERED;
Jeff Sharkey921ebf22011-05-19 17:12:49 -070023
Jaikumar Ganesh0db51a02010-12-21 22:31:44 -080024import android.bluetooth.BluetoothTetheringDataTracker;
The Android Open Source Project28527d22009-03-03 19:31:44 -080025import android.content.ContentResolver;
26import android.content.Context;
27import android.content.Intent;
28import android.content.pm.PackageManager;
Robert Greenwaltc3c5f862010-10-11 16:00:27 -070029import android.database.ContentObserver;
The Android Open Source Project28527d22009-03-03 19:31:44 -080030import android.net.ConnectivityManager;
Robert Greenwalteb123ac2010-12-06 13:56:24 -080031import android.net.DummyDataStateTracker;
Benoit Goby211b5692010-12-22 14:29:40 -080032import android.net.EthernetDataTracker;
The Android Open Source Project28527d22009-03-03 19:31:44 -080033import android.net.IConnectivityManager;
Jeff Sharkey921ebf22011-05-19 17:12:49 -070034import android.net.INetworkPolicyListener;
35import android.net.INetworkPolicyManager;
Wink Savilledc5d1ba2011-07-14 12:23:28 -070036import android.net.LinkAddress;
Jaikumar Ganesh0db51a02010-12-21 22:31:44 -080037import android.net.LinkProperties;
Robert Greenwalte8d2a4a2011-07-14 14:28:05 -070038import android.net.LinkProperties.CompareResult;
The Android Open Source Project28527d22009-03-03 19:31:44 -080039import android.net.MobileDataStateTracker;
Robert Greenwalt34848c02011-03-25 13:09:25 -070040import android.net.NetworkConfig;
The Android Open Source Project28527d22009-03-03 19:31:44 -080041import android.net.NetworkInfo;
Jeff Sharkey921ebf22011-05-19 17:12:49 -070042import android.net.NetworkInfo.DetailedState;
Jeff Sharkey21062e72011-05-28 20:56:34 -070043import android.net.NetworkState;
The Android Open Source Project28527d22009-03-03 19:31:44 -080044import android.net.NetworkStateTracker;
Robert Greenwalt7fe44cb2010-08-27 09:24:29 -070045import android.net.NetworkUtils;
Robert Greenwaltc3c5f862010-10-11 16:00:27 -070046import android.net.Proxy;
47import android.net.ProxyProperties;
Robert Greenwalt5a901292011-04-28 14:28:50 -070048import android.net.RouteInfo;
The Android Open Source Project28527d22009-03-03 19:31:44 -080049import android.net.wifi.WifiStateTracker;
50import android.os.Binder;
Mike Lockwood0d5916c2011-05-28 13:24:04 -040051import android.os.FileUtils;
The Android Open Source Project28527d22009-03-03 19:31:44 -080052import android.os.Handler;
Wink Saville775aad62010-09-02 19:23:52 -070053import android.os.HandlerThread;
Robert Greenwalt2034b912009-08-12 16:08:25 -070054import android.os.IBinder;
Chia-chi Yeh4df51322011-05-11 16:35:13 -070055import android.os.INetworkManagementService;
The Android Open Source Project28527d22009-03-03 19:31:44 -080056import android.os.Looper;
57import android.os.Message;
Chia-chi Yeh9a4ad7d2011-05-23 17:26:46 -070058import android.os.ParcelFileDescriptor;
Robert Greenwalt93dc1042010-06-15 12:19:37 -070059import android.os.PowerManager;
Robert Greenwalt2034b912009-08-12 16:08:25 -070060import android.os.RemoteException;
The Android Open Source Project28527d22009-03-03 19:31:44 -080061import android.os.ServiceManager;
62import android.os.SystemProperties;
63import android.provider.Settings;
Robert Greenwalt2034b912009-08-12 16:08:25 -070064import android.text.TextUtils;
The Android Open Source Project28527d22009-03-03 19:31:44 -080065import android.util.EventLog;
Joe Onoratoc2386bb2010-02-26 18:56:32 -080066import android.util.Slog;
Jeff Sharkey921ebf22011-05-19 17:12:49 -070067import android.util.SparseIntArray;
The Android Open Source Project28527d22009-03-03 19:31:44 -080068
Chia-chi Yehbded3eb2011-07-04 03:23:12 -070069import com.android.internal.net.LegacyVpnInfo;
Chia-chi Yeh75cacd52011-06-15 17:07:27 -070070import com.android.internal.net.VpnConfig;
Robert Greenwalt2034b912009-08-12 16:08:25 -070071import com.android.internal.telephony.Phone;
Robert Greenwalt0c4828c2010-01-26 11:40:34 -080072import com.android.server.connectivity.Tethering;
Chia-chi Yeh9a4ad7d2011-05-23 17:26:46 -070073import com.android.server.connectivity.Vpn;
74
Jeff Sharkey21062e72011-05-28 20:56:34 -070075import com.google.android.collect.Lists;
Jeff Sharkeya47d7a12011-06-16 15:07:48 -070076import com.google.android.collect.Sets;
Robert Greenwalt0c4828c2010-01-26 11:40:34 -080077
The Android Open Source Project28527d22009-03-03 19:31:44 -080078import java.io.FileDescriptor;
Irfan Sheriff7f132d92010-06-09 15:39:36 -070079import java.io.IOException;
The Android Open Source Project28527d22009-03-03 19:31:44 -080080import java.io.PrintWriter;
Wink Savilledc5d1ba2011-07-14 12:23:28 -070081import java.net.Inet4Address;
Wink Saville051a6642011-07-13 13:44:13 -070082import java.net.Inet6Address;
Robert Greenwalta7dfbd32010-06-15 15:43:39 -070083import java.net.InetAddress;
84import java.net.UnknownHostException;
Robert Greenwalt2034b912009-08-12 16:08:25 -070085import java.util.ArrayList;
Jeff Sharkeya47d7a12011-06-16 15:07:48 -070086import java.util.Arrays;
Robert Greenwalta7dfbd32010-06-15 15:43:39 -070087import java.util.Collection;
Robert Greenwalt0e80be12010-09-20 14:35:25 -070088import java.util.GregorianCalendar;
Jeff Sharkeya47d7a12011-06-16 15:07:48 -070089import java.util.HashSet;
Robert Greenwalt2034b912009-08-12 16:08:25 -070090import java.util.List;
Jeff Sharkey921ebf22011-05-19 17:12:49 -070091import java.util.concurrent.atomic.AtomicBoolean;
The Android Open Source Project28527d22009-03-03 19:31:44 -080092
93/**
94 * @hide
95 */
96public class ConnectivityService extends IConnectivityManager.Stub {
97
Robert Greenwalt063dc7d2010-10-05 19:12:26 -070098 private static final boolean DBG = true;
Wink Saville051a6642011-07-13 13:44:13 -070099 private static final boolean VDBG = true;
The Android Open Source Project28527d22009-03-03 19:31:44 -0800100 private static final String TAG = "ConnectivityService";
101
Jeff Sharkey921ebf22011-05-19 17:12:49 -0700102 private static final boolean LOGD_RULES = false;
103
Robert Greenwalt2034b912009-08-12 16:08:25 -0700104 // how long to wait before switching back to a radio's default network
105 private static final int RESTORE_DEFAULT_NETWORK_DELAY = 1 * 60 * 1000;
106 // system property that can override the above value
107 private static final String NETWORK_RESTORE_DELAY_PROP_NAME =
108 "android.telephony.apn-restore";
109
Robert Greenwaltbd492212011-05-06 17:10:53 -0700110 // used in recursive route setting to add gateways for the host for which
111 // a host route was requested.
112 private static final int MAX_HOSTROUTE_CYCLE_COUNT = 10;
113
Robert Greenwalt0c4828c2010-01-26 11:40:34 -0800114 private Tethering mTethering;
Robert Greenwaltf1b66e12010-02-25 12:29:30 -0800115 private boolean mTetheringConfigValid = false;
Robert Greenwalt0c4828c2010-01-26 11:40:34 -0800116
Chia-chi Yeh9a4ad7d2011-05-23 17:26:46 -0700117 private Vpn mVpn;
118
Jeff Sharkeya47d7a12011-06-16 15:07:48 -0700119 /** Lock around {@link #mUidRules} and {@link #mMeteredIfaces}. */
120 private Object mRulesLock = new Object();
Jeff Sharkey921ebf22011-05-19 17:12:49 -0700121 /** Currently active network rules by UID. */
122 private SparseIntArray mUidRules = new SparseIntArray();
Jeff Sharkeya47d7a12011-06-16 15:07:48 -0700123 /** Set of ifaces that are costly. */
124 private HashSet<String> mMeteredIfaces = Sets.newHashSet();
Jeff Sharkey921ebf22011-05-19 17:12:49 -0700125
The Android Open Source Project28527d22009-03-03 19:31:44 -0800126 /**
127 * Sometimes we want to refer to the individual network state
128 * trackers separately, and sometimes we just want to treat them
129 * abstractly.
130 */
131 private NetworkStateTracker mNetTrackers[];
Robert Greenwalt2034b912009-08-12 16:08:25 -0700132
133 /**
Wink Saville051a6642011-07-13 13:44:13 -0700134 * The link properties that define the current links
135 */
136 private LinkProperties mCurrentLinkProperties[];
137
138 /**
Robert Greenwalt2034b912009-08-12 16:08:25 -0700139 * A per Net list of the PID's that requested access to the net
140 * used both as a refcount and for per-PID DNS selection
141 */
142 private List mNetRequestersPids[];
143
Robert Greenwalt2034b912009-08-12 16:08:25 -0700144 // priority order of the nettrackers
145 // (excluding dynamically set mNetworkPreference)
146 // TODO - move mNetworkTypePreference into this
147 private int[] mPriorityList;
148
The Android Open Source Project28527d22009-03-03 19:31:44 -0800149 private Context mContext;
150 private int mNetworkPreference;
Robert Greenwalt2034b912009-08-12 16:08:25 -0700151 private int mActiveDefaultNetwork = -1;
Robert Greenwalt986c7412010-09-08 15:24:47 -0700152 // 0 is full bad, 100 is full good
153 private int mDefaultInetCondition = 0;
154 private int mDefaultInetConditionPublished = 0;
155 private boolean mInetConditionChangeInFlight = false;
156 private int mDefaultConnectionSequence = 0;
The Android Open Source Project28527d22009-03-03 19:31:44 -0800157
158 private int mNumDnsEntries;
The Android Open Source Project28527d22009-03-03 19:31:44 -0800159
160 private boolean mTestMode;
Joe Onorato56023ad2010-09-01 21:18:22 -0700161 private static ConnectivityService sServiceInstance;
The Android Open Source Project28527d22009-03-03 19:31:44 -0800162
Robert Greenwaltd62c7002010-12-29 16:15:02 -0800163 private AtomicBoolean mBackgroundDataEnabled = new AtomicBoolean(true);
164
Robert Greenwalt355205c2011-05-10 15:05:02 -0700165 private INetworkManagementService mNetd;
Jeff Sharkey921ebf22011-05-19 17:12:49 -0700166 private INetworkPolicyManager mPolicyManager;
Robert Greenwalt355205c2011-05-10 15:05:02 -0700167
Robert Greenwalt6a2db8a2010-09-23 10:05:56 -0700168 private static final int ENABLED = 1;
169 private static final int DISABLED = 0;
170
171 // Share the event space with NetworkStateTracker (which can't see this
172 // internal class but sends us events). If you change these, change
173 // NetworkStateTracker.java too.
174 private static final int MIN_NETWORK_STATE_TRACKER_EVENT = 1;
175 private static final int MAX_NETWORK_STATE_TRACKER_EVENT = 100;
176
177 /**
178 * used internally as a delayed event to make us switch back to the
179 * default network
180 */
181 private static final int EVENT_RESTORE_DEFAULT_NETWORK =
182 MAX_NETWORK_STATE_TRACKER_EVENT + 1;
183
184 /**
185 * used internally to change our mobile data enabled flag
186 */
187 private static final int EVENT_CHANGE_MOBILE_DATA_ENABLED =
188 MAX_NETWORK_STATE_TRACKER_EVENT + 2;
189
190 /**
191 * used internally to change our network preference setting
192 * arg1 = networkType to prefer
193 */
194 private static final int EVENT_SET_NETWORK_PREFERENCE =
195 MAX_NETWORK_STATE_TRACKER_EVENT + 3;
196
197 /**
198 * used internally to synchronize inet condition reports
199 * arg1 = networkType
200 * arg2 = condition (0 bad, 100 good)
201 */
202 private static final int EVENT_INET_CONDITION_CHANGE =
203 MAX_NETWORK_STATE_TRACKER_EVENT + 4;
204
205 /**
206 * used internally to mark the end of inet condition hold periods
207 * arg1 = networkType
208 */
209 private static final int EVENT_INET_CONDITION_HOLD_END =
210 MAX_NETWORK_STATE_TRACKER_EVENT + 5;
211
212 /**
213 * used internally to set the background data preference
214 * arg1 = TRUE for enabled, FALSE for disabled
215 */
216 private static final int EVENT_SET_BACKGROUND_DATA =
217 MAX_NETWORK_STATE_TRACKER_EVENT + 6;
218
219 /**
220 * used internally to set enable/disable cellular data
221 * arg1 = ENBALED or DISABLED
222 */
223 private static final int EVENT_SET_MOBILE_DATA =
224 MAX_NETWORK_STATE_TRACKER_EVENT + 7;
225
Robert Greenwaltccb36f92010-09-24 14:32:21 -0700226 /**
227 * used internally to clear a wakelock when transitioning
228 * from one net to another
229 */
230 private static final int EVENT_CLEAR_NET_TRANSITION_WAKELOCK =
231 MAX_NETWORK_STATE_TRACKER_EVENT + 8;
232
Robert Greenwaltc3c5f862010-10-11 16:00:27 -0700233 /**
234 * used internally to reload global proxy settings
235 */
236 private static final int EVENT_APPLY_GLOBAL_HTTP_PROXY =
237 MAX_NETWORK_STATE_TRACKER_EVENT + 9;
238
Robert Greenwalt34848c02011-03-25 13:09:25 -0700239 /**
240 * used internally to set external dependency met/unmet
241 * arg1 = ENABLED (met) or DISABLED (unmet)
242 * arg2 = NetworkType
243 */
244 private static final int EVENT_SET_DEPENDENCY_MET =
245 MAX_NETWORK_STATE_TRACKER_EVENT + 10;
246
Robert Greenwalt2034b912009-08-12 16:08:25 -0700247 private Handler mHandler;
248
249 // list of DeathRecipients used to make sure features are turned off when
250 // a process dies
251 private List mFeatureUsers;
252
Mike Lockwoodfde2b762009-08-14 14:18:49 -0400253 private boolean mSystemReady;
Dianne Hackborna417ff82009-12-08 19:45:14 -0800254 private Intent mInitialBroadcast;
Mike Lockwoodfde2b762009-08-14 14:18:49 -0400255
Robert Greenwalt93dc1042010-06-15 12:19:37 -0700256 private PowerManager.WakeLock mNetTransitionWakeLock;
257 private String mNetTransitionWakeLockCausedBy = "";
258 private int mNetTransitionWakeLockSerialNumber;
259 private int mNetTransitionWakeLockTimeout;
260
Robert Greenwalt94daa182010-09-01 11:34:05 -0700261 private InetAddress mDefaultDns;
262
Robert Greenwalte8d2a4a2011-07-14 14:28:05 -0700263 // this collection is used to refcount the added routes - if there are none left
264 // it's time to remove the route from the route table
265 private Collection<RouteInfo> mAddedRoutes = new ArrayList<RouteInfo>();
266
Robert Greenwalt0e80be12010-09-20 14:35:25 -0700267 // used in DBG mode to track inet condition reports
268 private static final int INET_CONDITION_LOG_MAX_SIZE = 15;
269 private ArrayList mInetLog;
270
Robert Greenwaltc3c5f862010-10-11 16:00:27 -0700271 // track the current default http proxy - tell the world if we get a new one (real change)
272 private ProxyProperties mDefaultProxy = null;
273 // track the global proxy.
274 private ProxyProperties mGlobalProxy = null;
275 private final Object mGlobalProxyLock = new Object();
276
277 private SettingsObserver mSettingsObserver;
278
Robert Greenwalt34848c02011-03-25 13:09:25 -0700279 NetworkConfig[] mNetConfigs;
Robert Greenwaltec05b3c2009-10-30 14:17:42 -0700280 int mNetworksDefined;
Robert Greenwalt2034b912009-08-12 16:08:25 -0700281
Robert Greenwalt12c44552009-12-07 11:33:18 -0800282 private static class RadioAttributes {
Robert Greenwalt2034b912009-08-12 16:08:25 -0700283 public int mSimultaneity;
284 public int mType;
285 public RadioAttributes(String init) {
286 String fragments[] = init.split(",");
Robert Greenwaltec05b3c2009-10-30 14:17:42 -0700287 mType = Integer.parseInt(fragments[0]);
288 mSimultaneity = Integer.parseInt(fragments[1]);
Robert Greenwalt2034b912009-08-12 16:08:25 -0700289 }
290 }
291 RadioAttributes[] mRadioAttributes;
292
Robert Greenwalt6cac0742011-06-21 17:26:14 -0700293 // the set of network types that can only be enabled by system/sig apps
294 List mProtectedNetworks;
295
Jeff Sharkey921ebf22011-05-19 17:12:49 -0700296 public ConnectivityService(
297 Context context, INetworkManagementService netd, INetworkPolicyManager policyManager) {
Wink Savillee70c6f52010-12-03 12:01:38 -0800298 if (DBG) log("ConnectivityService starting up");
Robert Greenwaltd48f8ee2010-01-14 17:47:58 -0800299
Wink Saville775aad62010-09-02 19:23:52 -0700300 HandlerThread handlerThread = new HandlerThread("ConnectivityServiceThread");
301 handlerThread.start();
302 mHandler = new MyHandler(handlerThread.getLooper());
303
Robert Greenwaltd62c7002010-12-29 16:15:02 -0800304 mBackgroundDataEnabled.set(Settings.Secure.getInt(context.getContentResolver(),
305 Settings.Secure.BACKGROUND_DATA, 1) == 1);
306
Robert Greenwaltd48f8ee2010-01-14 17:47:58 -0800307 // setup our unique device name
Robert Greenwalt82cde132010-12-06 09:30:17 -0800308 if (TextUtils.isEmpty(SystemProperties.get("net.hostname"))) {
309 String id = Settings.Secure.getString(context.getContentResolver(),
310 Settings.Secure.ANDROID_ID);
311 if (id != null && id.length() > 0) {
312 String name = new String("android_").concat(id);
313 SystemProperties.set("net.hostname", name);
314 }
Robert Greenwaltd48f8ee2010-01-14 17:47:58 -0800315 }
316
Robert Greenwalt94daa182010-09-01 11:34:05 -0700317 // read our default dns server ip
318 String dns = Settings.Secure.getString(context.getContentResolver(),
319 Settings.Secure.DEFAULT_DNS_SERVER);
320 if (dns == null || dns.length() == 0) {
321 dns = context.getResources().getString(
322 com.android.internal.R.string.config_default_dns_server);
323 }
324 try {
Robert Greenwalt35e34d12011-02-22 16:00:42 -0800325 mDefaultDns = NetworkUtils.numericToInetAddress(dns);
326 } catch (IllegalArgumentException e) {
Wink Savillee70c6f52010-12-03 12:01:38 -0800327 loge("Error setting defaultDns using " + dns);
Robert Greenwalt94daa182010-09-01 11:34:05 -0700328 }
329
Jeff Sharkey921ebf22011-05-19 17:12:49 -0700330 mContext = checkNotNull(context, "missing Context");
331 mNetd = checkNotNull(netd, "missing INetworkManagementService");
332 mPolicyManager = checkNotNull(policyManager, "missing INetworkPolicyManager");
Robert Greenwalt93dc1042010-06-15 12:19:37 -0700333
Jeff Sharkey921ebf22011-05-19 17:12:49 -0700334 try {
335 mPolicyManager.registerListener(mPolicyListener);
336 } catch (RemoteException e) {
337 // ouch, no rules updates means some processes may never get network
338 Slog.e(TAG, "unable to register INetworkPolicyListener", e);
339 }
340
341 final PowerManager powerManager = (PowerManager) context.getSystemService(
342 Context.POWER_SERVICE);
Robert Greenwalt93dc1042010-06-15 12:19:37 -0700343 mNetTransitionWakeLock = powerManager.newWakeLock(PowerManager.PARTIAL_WAKE_LOCK, TAG);
344 mNetTransitionWakeLockTimeout = mContext.getResources().getInteger(
345 com.android.internal.R.integer.config_networkTransitionTimeout);
346
Robert Greenwalt2034b912009-08-12 16:08:25 -0700347 mNetTrackers = new NetworkStateTracker[
348 ConnectivityManager.MAX_NETWORK_TYPE+1];
Wink Saville051a6642011-07-13 13:44:13 -0700349 mCurrentLinkProperties = new LinkProperties[ConnectivityManager.MAX_NETWORK_TYPE+1];
Robert Greenwalt0659da32009-07-16 17:21:39 -0700350
The Android Open Source Project28527d22009-03-03 19:31:44 -0800351 mNetworkPreference = getPersistedNetworkPreference();
Robert Greenwalt0659da32009-07-16 17:21:39 -0700352
Robert Greenwaltec05b3c2009-10-30 14:17:42 -0700353 mRadioAttributes = new RadioAttributes[ConnectivityManager.MAX_RADIO_TYPE+1];
Robert Greenwalt34848c02011-03-25 13:09:25 -0700354 mNetConfigs = new NetworkConfig[ConnectivityManager.MAX_NETWORK_TYPE+1];
Robert Greenwaltec05b3c2009-10-30 14:17:42 -0700355
Robert Greenwalt2034b912009-08-12 16:08:25 -0700356 // Load device network attributes from resources
Robert Greenwalt2034b912009-08-12 16:08:25 -0700357 String[] raStrings = context.getResources().getStringArray(
358 com.android.internal.R.array.radioAttributes);
Robert Greenwaltec05b3c2009-10-30 14:17:42 -0700359 for (String raString : raStrings) {
360 RadioAttributes r = new RadioAttributes(raString);
361 if (r.mType > ConnectivityManager.MAX_RADIO_TYPE) {
Wink Savillee70c6f52010-12-03 12:01:38 -0800362 loge("Error in radioAttributes - ignoring attempt to define type " + r.mType);
Robert Greenwaltec05b3c2009-10-30 14:17:42 -0700363 continue;
364 }
365 if (mRadioAttributes[r.mType] != null) {
Wink Savillee70c6f52010-12-03 12:01:38 -0800366 loge("Error in radioAttributes - ignoring attempt to redefine type " +
Robert Greenwaltec05b3c2009-10-30 14:17:42 -0700367 r.mType);
368 continue;
369 }
Robert Greenwalt2034b912009-08-12 16:08:25 -0700370 mRadioAttributes[r.mType] = r;
371 }
372
Robert Greenwaltec05b3c2009-10-30 14:17:42 -0700373 String[] naStrings = context.getResources().getStringArray(
374 com.android.internal.R.array.networkAttributes);
375 for (String naString : naStrings) {
376 try {
Robert Greenwalt34848c02011-03-25 13:09:25 -0700377 NetworkConfig n = new NetworkConfig(naString);
Wink Savillef2a62832011-04-07 14:23:45 -0700378 if (n.type > ConnectivityManager.MAX_NETWORK_TYPE) {
Wink Savillee70c6f52010-12-03 12:01:38 -0800379 loge("Error in networkAttributes - ignoring attempt to define type " +
Wink Savillef2a62832011-04-07 14:23:45 -0700380 n.type);
Robert Greenwaltec05b3c2009-10-30 14:17:42 -0700381 continue;
Robert Greenwalt2034b912009-08-12 16:08:25 -0700382 }
Wink Savillef2a62832011-04-07 14:23:45 -0700383 if (mNetConfigs[n.type] != null) {
Wink Savillee70c6f52010-12-03 12:01:38 -0800384 loge("Error in networkAttributes - ignoring attempt to redefine type " +
Wink Savillef2a62832011-04-07 14:23:45 -0700385 n.type);
Robert Greenwaltec05b3c2009-10-30 14:17:42 -0700386 continue;
387 }
Wink Savillef2a62832011-04-07 14:23:45 -0700388 if (mRadioAttributes[n.radio] == null) {
Wink Savillee70c6f52010-12-03 12:01:38 -0800389 loge("Error in networkAttributes - ignoring attempt to use undefined " +
Wink Savillef2a62832011-04-07 14:23:45 -0700390 "radio " + n.radio + " in network type " + n.type);
Robert Greenwaltec05b3c2009-10-30 14:17:42 -0700391 continue;
392 }
Wink Savillef2a62832011-04-07 14:23:45 -0700393 mNetConfigs[n.type] = n;
Robert Greenwaltec05b3c2009-10-30 14:17:42 -0700394 mNetworksDefined++;
395 } catch(Exception e) {
396 // ignore it - leave the entry null
Robert Greenwalt2034b912009-08-12 16:08:25 -0700397 }
398 }
399
Robert Greenwalt6cac0742011-06-21 17:26:14 -0700400 mProtectedNetworks = new ArrayList<Integer>();
401 int[] protectedNetworks = context.getResources().getIntArray(
402 com.android.internal.R.array.config_protectedNetworks);
403 for (int p : protectedNetworks) {
404 if ((mNetConfigs[p] != null) && (mProtectedNetworks.contains(p) == false)) {
405 mProtectedNetworks.add(p);
406 } else {
407 if (DBG) loge("Ignoring protectedNetwork " + p);
408 }
409 }
410
Robert Greenwaltec05b3c2009-10-30 14:17:42 -0700411 // high priority first
412 mPriorityList = new int[mNetworksDefined];
413 {
414 int insertionPoint = mNetworksDefined-1;
415 int currentLowest = 0;
416 int nextLowest = 0;
417 while (insertionPoint > -1) {
Robert Greenwalt34848c02011-03-25 13:09:25 -0700418 for (NetworkConfig na : mNetConfigs) {
Robert Greenwaltec05b3c2009-10-30 14:17:42 -0700419 if (na == null) continue;
Wink Savillef2a62832011-04-07 14:23:45 -0700420 if (na.priority < currentLowest) continue;
421 if (na.priority > currentLowest) {
422 if (na.priority < nextLowest || nextLowest == 0) {
423 nextLowest = na.priority;
Robert Greenwaltec05b3c2009-10-30 14:17:42 -0700424 }
425 continue;
426 }
Wink Savillef2a62832011-04-07 14:23:45 -0700427 mPriorityList[insertionPoint--] = na.type;
Robert Greenwaltec05b3c2009-10-30 14:17:42 -0700428 }
429 currentLowest = nextLowest;
430 nextLowest = 0;
431 }
432 }
433
434 mNetRequestersPids = new ArrayList[ConnectivityManager.MAX_NETWORK_TYPE+1];
435 for (int i : mPriorityList) {
Robert Greenwalt2034b912009-08-12 16:08:25 -0700436 mNetRequestersPids[i] = new ArrayList();
437 }
438
439 mFeatureUsers = new ArrayList();
440
Robert Greenwaltec05b3c2009-10-30 14:17:42 -0700441 mNumDnsEntries = 0;
442
443 mTestMode = SystemProperties.get("cm.test.mode").equals("true")
444 && SystemProperties.get("ro.build.type").equals("eng");
The Android Open Source Project28527d22009-03-03 19:31:44 -0800445 /*
446 * Create the network state trackers for Wi-Fi and mobile
447 * data. Maybe this could be done with a factory class,
448 * but it's not clear that it's worth it, given that
449 * the number of different network types is not going
450 * to change very often.
451 */
Robert Greenwaltec05b3c2009-10-30 14:17:42 -0700452 for (int netType : mPriorityList) {
Wink Savillef2a62832011-04-07 14:23:45 -0700453 switch (mNetConfigs[netType].radio) {
Robert Greenwaltec05b3c2009-10-30 14:17:42 -0700454 case ConnectivityManager.TYPE_WIFI:
Wink Savillee70c6f52010-12-03 12:01:38 -0800455 if (DBG) log("Starting Wifi Service.");
Wink Saville7fabfa22010-08-13 16:11:42 -0700456 WifiStateTracker wst = new WifiStateTracker();
Irfan Sheriff25be0762010-07-28 09:35:20 -0700457 WifiService wifiService = new WifiService(context);
Robert Greenwaltec05b3c2009-10-30 14:17:42 -0700458 ServiceManager.addService(Context.WIFI_SERVICE, wifiService);
Irfan Sheriff25be0762010-07-28 09:35:20 -0700459 wifiService.checkAndStartWifi();
Robert Greenwaltec05b3c2009-10-30 14:17:42 -0700460 mNetTrackers[ConnectivityManager.TYPE_WIFI] = wst;
Wink Saville7fabfa22010-08-13 16:11:42 -0700461 wst.startMonitoring(context, mHandler);
Robert Greenwaltec05b3c2009-10-30 14:17:42 -0700462 break;
463 case ConnectivityManager.TYPE_MOBILE:
Wink Saville7fabfa22010-08-13 16:11:42 -0700464 mNetTrackers[netType] = new MobileDataStateTracker(netType,
Wink Savillef2a62832011-04-07 14:23:45 -0700465 mNetConfigs[netType].name);
Wink Saville7fabfa22010-08-13 16:11:42 -0700466 mNetTrackers[netType].startMonitoring(context, mHandler);
Robert Greenwaltec05b3c2009-10-30 14:17:42 -0700467 break;
Robert Greenwalteb123ac2010-12-06 13:56:24 -0800468 case ConnectivityManager.TYPE_DUMMY:
469 mNetTrackers[netType] = new DummyDataStateTracker(netType,
Wink Savillef2a62832011-04-07 14:23:45 -0700470 mNetConfigs[netType].name);
Robert Greenwalteb123ac2010-12-06 13:56:24 -0800471 mNetTrackers[netType].startMonitoring(context, mHandler);
472 break;
Jaikumar Ganesh0db51a02010-12-21 22:31:44 -0800473 case ConnectivityManager.TYPE_BLUETOOTH:
474 mNetTrackers[netType] = BluetoothTetheringDataTracker.getInstance();
475 mNetTrackers[netType].startMonitoring(context, mHandler);
476 break;
Benoit Goby211b5692010-12-22 14:29:40 -0800477 case ConnectivityManager.TYPE_ETHERNET:
478 mNetTrackers[netType] = EthernetDataTracker.getInstance();
479 mNetTrackers[netType].startMonitoring(context, mHandler);
480 break;
Robert Greenwaltec05b3c2009-10-30 14:17:42 -0700481 default:
Wink Savillee70c6f52010-12-03 12:01:38 -0800482 loge("Trying to create a DataStateTracker for an unknown radio type " +
Wink Savillef2a62832011-04-07 14:23:45 -0700483 mNetConfigs[netType].radio);
Robert Greenwaltec05b3c2009-10-30 14:17:42 -0700484 continue;
485 }
Robert Greenwalte8d2a4a2011-07-14 14:28:05 -0700486 mCurrentLinkProperties[netType] = null;
Robert Greenwalt497ff5e72011-07-15 11:16:45 -0700487 if (mNetConfigs[netType].isDefault()) mNetTrackers[netType].reconnect();
Robert Greenwaltec05b3c2009-10-30 14:17:42 -0700488 }
Robert Greenwalt0c4828c2010-01-26 11:40:34 -0800489
Chia-chi Yeh4df51322011-05-11 16:35:13 -0700490 IBinder b = ServiceManager.getService(Context.NETWORKMANAGEMENT_SERVICE);
491 INetworkManagementService nmService = INetworkManagementService.Stub.asInterface(b);
492
493 mTethering = new Tethering(mContext, nmService, mHandler.getLooper());
Robert Greenwalt33cdcdf2011-06-02 17:30:47 -0700494 mTetheringConfigValid = ((mTethering.getTetherableUsbRegexs().length != 0 ||
Danica Chang96567052010-08-11 14:54:43 -0700495 mTethering.getTetherableWifiRegexs().length != 0 ||
496 mTethering.getTetherableBluetoothRegexs().length != 0) &&
Robert Greenwalt33cdcdf2011-06-02 17:30:47 -0700497 mTethering.getUpstreamIfaceTypes().length != 0);
Robert Greenwaltf1b66e12010-02-25 12:29:30 -0800498
Chia-chi Yeh9a4ad7d2011-05-23 17:26:46 -0700499 mVpn = new Vpn(mContext, new VpnCallback());
500
Chia-chi Yehf3204aa2011-05-23 15:08:29 -0700501 try {
502 nmService.registerObserver(mTethering);
Chia-chi Yeh9a4ad7d2011-05-23 17:26:46 -0700503 nmService.registerObserver(mVpn);
Chia-chi Yehf3204aa2011-05-23 15:08:29 -0700504 } catch (RemoteException e) {
505 loge("Error registering observer :" + e);
506 }
507
Robert Greenwalt0e80be12010-09-20 14:35:25 -0700508 if (DBG) {
509 mInetLog = new ArrayList();
510 }
Robert Greenwaltc3c5f862010-10-11 16:00:27 -0700511
512 mSettingsObserver = new SettingsObserver(mHandler, EVENT_APPLY_GLOBAL_HTTP_PROXY);
513 mSettingsObserver.observe(mContext);
Robert Greenwalt6f7c6092010-12-02 11:31:00 -0800514
515 loadGlobalProxy();
The Android Open Source Project28527d22009-03-03 19:31:44 -0800516 }
517
518 /**
Robert Greenwalt0659da32009-07-16 17:21:39 -0700519 * Sets the preferred network.
The Android Open Source Project28527d22009-03-03 19:31:44 -0800520 * @param preference the new preference
521 */
Robert Greenwalt6a2db8a2010-09-23 10:05:56 -0700522 public void setNetworkPreference(int preference) {
The Android Open Source Project28527d22009-03-03 19:31:44 -0800523 enforceChangePermission();
Robert Greenwalt6a2db8a2010-09-23 10:05:56 -0700524
525 mHandler.sendMessage(mHandler.obtainMessage(EVENT_SET_NETWORK_PREFERENCE, preference, 0));
The Android Open Source Project28527d22009-03-03 19:31:44 -0800526 }
527
528 public int getNetworkPreference() {
529 enforceAccessPermission();
Robert Greenwalt6a2db8a2010-09-23 10:05:56 -0700530 int preference;
531 synchronized(this) {
532 preference = mNetworkPreference;
533 }
534 return preference;
The Android Open Source Project28527d22009-03-03 19:31:44 -0800535 }
536
Robert Greenwalt6a2db8a2010-09-23 10:05:56 -0700537 private void handleSetNetworkPreference(int preference) {
538 if (ConnectivityManager.isNetworkTypeValid(preference) &&
Robert Greenwalt34848c02011-03-25 13:09:25 -0700539 mNetConfigs[preference] != null &&
540 mNetConfigs[preference].isDefault()) {
Robert Greenwalt6a2db8a2010-09-23 10:05:56 -0700541 if (mNetworkPreference != preference) {
542 final ContentResolver cr = mContext.getContentResolver();
543 Settings.Secure.putInt(cr, Settings.Secure.NETWORK_PREFERENCE, preference);
544 synchronized(this) {
545 mNetworkPreference = preference;
546 }
547 enforcePreference();
548 }
549 }
The Android Open Source Project28527d22009-03-03 19:31:44 -0800550 }
Robert Greenwalt0659da32009-07-16 17:21:39 -0700551
The Android Open Source Project28527d22009-03-03 19:31:44 -0800552 private int getPersistedNetworkPreference() {
553 final ContentResolver cr = mContext.getContentResolver();
554
555 final int networkPrefSetting = Settings.Secure
556 .getInt(cr, Settings.Secure.NETWORK_PREFERENCE, -1);
557 if (networkPrefSetting != -1) {
558 return networkPrefSetting;
559 }
560
561 return ConnectivityManager.DEFAULT_NETWORK_PREFERENCE;
562 }
Robert Greenwalt0659da32009-07-16 17:21:39 -0700563
The Android Open Source Project28527d22009-03-03 19:31:44 -0800564 /**
Robert Greenwalt0659da32009-07-16 17:21:39 -0700565 * Make the state of network connectivity conform to the preference settings
The Android Open Source Project28527d22009-03-03 19:31:44 -0800566 * In this method, we only tear down a non-preferred network. Establishing
567 * a connection to the preferred network is taken care of when we handle
568 * the disconnect event from the non-preferred network
569 * (see {@link #handleDisconnect(NetworkInfo)}).
570 */
571 private void enforcePreference() {
Robert Greenwalt2034b912009-08-12 16:08:25 -0700572 if (mNetTrackers[mNetworkPreference].getNetworkInfo().isConnected())
The Android Open Source Project28527d22009-03-03 19:31:44 -0800573 return;
574
Robert Greenwalt2034b912009-08-12 16:08:25 -0700575 if (!mNetTrackers[mNetworkPreference].isAvailable())
576 return;
The Android Open Source Project28527d22009-03-03 19:31:44 -0800577
Robert Greenwalt2034b912009-08-12 16:08:25 -0700578 for (int t=0; t <= ConnectivityManager.MAX_RADIO_TYPE; t++) {
Robert Greenwaltec05b3c2009-10-30 14:17:42 -0700579 if (t != mNetworkPreference && mNetTrackers[t] != null &&
Robert Greenwalt2034b912009-08-12 16:08:25 -0700580 mNetTrackers[t].getNetworkInfo().isConnected()) {
Robert Greenwaltf3f045b2009-08-20 15:25:14 -0700581 if (DBG) {
Wink Savillee70c6f52010-12-03 12:01:38 -0800582 log("tearing down " + mNetTrackers[t].getNetworkInfo() +
Robert Greenwaltf3f045b2009-08-20 15:25:14 -0700583 " in enforcePreference");
584 }
Robert Greenwalt2034b912009-08-12 16:08:25 -0700585 teardown(mNetTrackers[t]);
The Android Open Source Project28527d22009-03-03 19:31:44 -0800586 }
587 }
588 }
589
590 private boolean teardown(NetworkStateTracker netTracker) {
591 if (netTracker.teardown()) {
592 netTracker.setTeardownRequested(true);
593 return true;
594 } else {
595 return false;
596 }
597 }
598
599 /**
Jeff Sharkeya47d7a12011-06-16 15:07:48 -0700600 * Check if UID should be blocked from using the network represented by the
601 * given {@link NetworkStateTracker}.
Jeff Sharkey921ebf22011-05-19 17:12:49 -0700602 */
Jeff Sharkeya47d7a12011-06-16 15:07:48 -0700603 private boolean isNetworkBlocked(NetworkStateTracker tracker, int uid) {
604 final String iface = tracker.getLinkProperties().getInterfaceName();
Jeff Sharkey21062e72011-05-28 20:56:34 -0700605
Jeff Sharkeya47d7a12011-06-16 15:07:48 -0700606 final boolean networkCostly;
607 final int uidRules;
608 synchronized (mRulesLock) {
609 networkCostly = mMeteredIfaces.contains(iface);
610 uidRules = mUidRules.get(uid, RULE_ALLOW_ALL);
Jeff Sharkey921ebf22011-05-19 17:12:49 -0700611 }
Jeff Sharkeya47d7a12011-06-16 15:07:48 -0700612
613 if (networkCostly && (uidRules & RULE_REJECT_METERED) != 0) {
614 return true;
615 }
616
617 // no restrictive rules; network is visible
618 return false;
Jeff Sharkey921ebf22011-05-19 17:12:49 -0700619 }
620
621 /**
Jeff Sharkeya47d7a12011-06-16 15:07:48 -0700622 * Return a filtered {@link NetworkInfo}, potentially marked
623 * {@link DetailedState#BLOCKED} based on
624 * {@link #isNetworkBlocked(NetworkStateTracker, int)}.
Jeff Sharkey921ebf22011-05-19 17:12:49 -0700625 */
Jeff Sharkeya47d7a12011-06-16 15:07:48 -0700626 private NetworkInfo getFilteredNetworkInfo(NetworkStateTracker tracker, int uid) {
627 NetworkInfo info = tracker.getNetworkInfo();
628 if (isNetworkBlocked(tracker, uid)) {
Jeff Sharkey21062e72011-05-28 20:56:34 -0700629 // network is blocked; clone and override state
630 info = new NetworkInfo(info);
631 info.setDetailedState(DetailedState.BLOCKED, null, null);
Jeff Sharkey921ebf22011-05-19 17:12:49 -0700632 }
Jeff Sharkey21062e72011-05-28 20:56:34 -0700633 return info;
Jeff Sharkey921ebf22011-05-19 17:12:49 -0700634 }
635
636 /**
The Android Open Source Project28527d22009-03-03 19:31:44 -0800637 * Return NetworkInfo for the active (i.e., connected) network interface.
638 * It is assumed that at most one network is active at a time. If more
639 * than one is active, it is indeterminate which will be returned.
Robert Greenwalt0659da32009-07-16 17:21:39 -0700640 * @return the info for the active network, or {@code null} if none is
641 * active
The Android Open Source Project28527d22009-03-03 19:31:44 -0800642 */
Jeff Sharkey921ebf22011-05-19 17:12:49 -0700643 @Override
The Android Open Source Project28527d22009-03-03 19:31:44 -0800644 public NetworkInfo getActiveNetworkInfo() {
Jeff Sharkey921ebf22011-05-19 17:12:49 -0700645 enforceAccessPermission();
646 final int uid = Binder.getCallingUid();
647 return getNetworkInfo(mActiveDefaultNetwork, uid);
The Android Open Source Project28527d22009-03-03 19:31:44 -0800648 }
649
Jeff Sharkey921ebf22011-05-19 17:12:49 -0700650 @Override
651 public NetworkInfo getActiveNetworkInfoForUid(int uid) {
652 enforceConnectivityInternalPermission();
653 return getNetworkInfo(mActiveDefaultNetwork, uid);
654 }
655
656 @Override
The Android Open Source Project28527d22009-03-03 19:31:44 -0800657 public NetworkInfo getNetworkInfo(int networkType) {
658 enforceAccessPermission();
Jeff Sharkey921ebf22011-05-19 17:12:49 -0700659 final int uid = Binder.getCallingUid();
660 return getNetworkInfo(networkType, uid);
The Android Open Source Project28527d22009-03-03 19:31:44 -0800661 }
662
Jeff Sharkey921ebf22011-05-19 17:12:49 -0700663 private NetworkInfo getNetworkInfo(int networkType, int uid) {
664 NetworkInfo info = null;
665 if (isNetworkTypeValid(networkType)) {
666 final NetworkStateTracker tracker = mNetTrackers[networkType];
667 if (tracker != null) {
Jeff Sharkeya47d7a12011-06-16 15:07:48 -0700668 info = getFilteredNetworkInfo(tracker, uid);
Jeff Sharkey921ebf22011-05-19 17:12:49 -0700669 }
670 }
671 return info;
672 }
673
674 @Override
The Android Open Source Project28527d22009-03-03 19:31:44 -0800675 public NetworkInfo[] getAllNetworkInfo() {
676 enforceAccessPermission();
Jeff Sharkey921ebf22011-05-19 17:12:49 -0700677 final int uid = Binder.getCallingUid();
Jeff Sharkey21062e72011-05-28 20:56:34 -0700678 final ArrayList<NetworkInfo> result = Lists.newArrayList();
Jeff Sharkeya47d7a12011-06-16 15:07:48 -0700679 synchronized (mRulesLock) {
Jeff Sharkey921ebf22011-05-19 17:12:49 -0700680 for (NetworkStateTracker tracker : mNetTrackers) {
681 if (tracker != null) {
Jeff Sharkeya47d7a12011-06-16 15:07:48 -0700682 result.add(getFilteredNetworkInfo(tracker, uid));
Jeff Sharkey921ebf22011-05-19 17:12:49 -0700683 }
684 }
The Android Open Source Project28527d22009-03-03 19:31:44 -0800685 }
Jeff Sharkey21062e72011-05-28 20:56:34 -0700686 return result.toArray(new NetworkInfo[result.size()]);
The Android Open Source Project28527d22009-03-03 19:31:44 -0800687 }
688
Robert Greenwalt9f0ee4f2010-09-14 09:18:02 -0700689 /**
690 * Return LinkProperties for the active (i.e., connected) default
691 * network interface. It is assumed that at most one default network
692 * is active at a time. If more than one is active, it is indeterminate
693 * which will be returned.
694 * @return the ip properties for the active network, or {@code null} if
695 * none is active
696 */
Jeff Sharkey921ebf22011-05-19 17:12:49 -0700697 @Override
Robert Greenwalt9f0ee4f2010-09-14 09:18:02 -0700698 public LinkProperties getActiveLinkProperties() {
Robert Greenwalte1544bb2011-05-20 12:23:41 -0700699 return getLinkProperties(mActiveDefaultNetwork);
Robert Greenwalt9f0ee4f2010-09-14 09:18:02 -0700700 }
701
Jeff Sharkey921ebf22011-05-19 17:12:49 -0700702 @Override
Robert Greenwalt9f0ee4f2010-09-14 09:18:02 -0700703 public LinkProperties getLinkProperties(int networkType) {
704 enforceAccessPermission();
Jeff Sharkey921ebf22011-05-19 17:12:49 -0700705 if (isNetworkTypeValid(networkType)) {
706 final NetworkStateTracker tracker = mNetTrackers[networkType];
707 if (tracker != null) {
708 return tracker.getLinkProperties();
709 }
Robert Greenwalt9f0ee4f2010-09-14 09:18:02 -0700710 }
711 return null;
712 }
713
Jeff Sharkey21062e72011-05-28 20:56:34 -0700714 @Override
715 public NetworkState[] getAllNetworkState() {
716 enforceAccessPermission();
717 final int uid = Binder.getCallingUid();
718 final ArrayList<NetworkState> result = Lists.newArrayList();
Jeff Sharkeya47d7a12011-06-16 15:07:48 -0700719 synchronized (mRulesLock) {
Jeff Sharkey21062e72011-05-28 20:56:34 -0700720 for (NetworkStateTracker tracker : mNetTrackers) {
721 if (tracker != null) {
Jeff Sharkeya47d7a12011-06-16 15:07:48 -0700722 final NetworkInfo info = getFilteredNetworkInfo(tracker, uid);
Jeff Sharkey21062e72011-05-28 20:56:34 -0700723 result.add(new NetworkState(
724 info, tracker.getLinkProperties(), tracker.getLinkCapabilities()));
725 }
726 }
727 }
728 return result.toArray(new NetworkState[result.size()]);
729 }
730
The Android Open Source Project28527d22009-03-03 19:31:44 -0800731 public boolean setRadios(boolean turnOn) {
732 boolean result = true;
733 enforceChangePermission();
734 for (NetworkStateTracker t : mNetTrackers) {
Robert Greenwaltec05b3c2009-10-30 14:17:42 -0700735 if (t != null) result = t.setRadio(turnOn) && result;
The Android Open Source Project28527d22009-03-03 19:31:44 -0800736 }
737 return result;
738 }
739
740 public boolean setRadio(int netType, boolean turnOn) {
741 enforceChangePermission();
742 if (!ConnectivityManager.isNetworkTypeValid(netType)) {
743 return false;
744 }
745 NetworkStateTracker tracker = mNetTrackers[netType];
746 return tracker != null && tracker.setRadio(turnOn);
747 }
748
Robert Greenwaltaffc3a12009-09-27 17:27:04 -0700749 /**
750 * Used to notice when the calling process dies so we can self-expire
751 *
752 * Also used to know if the process has cleaned up after itself when
753 * our auto-expire timer goes off. The timer has a link to an object.
754 *
755 */
Robert Greenwalt2034b912009-08-12 16:08:25 -0700756 private class FeatureUser implements IBinder.DeathRecipient {
757 int mNetworkType;
758 String mFeature;
759 IBinder mBinder;
760 int mPid;
761 int mUid;
Robert Greenwalt3eeb6032009-12-21 18:24:07 -0800762 long mCreateTime;
Robert Greenwalt2034b912009-08-12 16:08:25 -0700763
764 FeatureUser(int type, String feature, IBinder binder) {
765 super();
766 mNetworkType = type;
767 mFeature = feature;
768 mBinder = binder;
769 mPid = getCallingPid();
770 mUid = getCallingUid();
Robert Greenwalt3eeb6032009-12-21 18:24:07 -0800771 mCreateTime = System.currentTimeMillis();
Robert Greenwaltaffc3a12009-09-27 17:27:04 -0700772
Robert Greenwalt2034b912009-08-12 16:08:25 -0700773 try {
774 mBinder.linkToDeath(this, 0);
775 } catch (RemoteException e) {
776 binderDied();
777 }
778 }
779
780 void unlinkDeathRecipient() {
781 mBinder.unlinkToDeath(this, 0);
782 }
783
784 public void binderDied() {
Wink Savillee70c6f52010-12-03 12:01:38 -0800785 log("ConnectivityService FeatureUser binderDied(" +
Robert Greenwalt3eeb6032009-12-21 18:24:07 -0800786 mNetworkType + ", " + mFeature + ", " + mBinder + "), created " +
787 (System.currentTimeMillis() - mCreateTime) + " mSec ago");
Robert Greenwaltaffc3a12009-09-27 17:27:04 -0700788 stopUsingNetworkFeature(this, false);
Robert Greenwalt2034b912009-08-12 16:08:25 -0700789 }
790
Robert Greenwaltaffc3a12009-09-27 17:27:04 -0700791 public void expire() {
Wink Savillee70c6f52010-12-03 12:01:38 -0800792 log("ConnectivityService FeatureUser expire(" +
Robert Greenwalt3eeb6032009-12-21 18:24:07 -0800793 mNetworkType + ", " + mFeature + ", " + mBinder +"), created " +
794 (System.currentTimeMillis() - mCreateTime) + " mSec ago");
Robert Greenwaltaffc3a12009-09-27 17:27:04 -0700795 stopUsingNetworkFeature(this, false);
796 }
Robert Greenwalt3eeb6032009-12-21 18:24:07 -0800797
798 public String toString() {
799 return "FeatureUser("+mNetworkType+","+mFeature+","+mPid+","+mUid+"), created " +
800 (System.currentTimeMillis() - mCreateTime) + " mSec ago";
801 }
Robert Greenwalt2034b912009-08-12 16:08:25 -0700802 }
803
Robert Greenwaltaffc3a12009-09-27 17:27:04 -0700804 // javadoc from interface
Robert Greenwalt2034b912009-08-12 16:08:25 -0700805 public int startUsingNetworkFeature(int networkType, String feature,
806 IBinder binder) {
807 if (DBG) {
Wink Savillee70c6f52010-12-03 12:01:38 -0800808 log("startUsingNetworkFeature for net " + networkType + ": " + feature);
Robert Greenwalt2034b912009-08-12 16:08:25 -0700809 }
The Android Open Source Project28527d22009-03-03 19:31:44 -0800810 enforceChangePermission();
Robert Greenwaltec05b3c2009-10-30 14:17:42 -0700811 if (!ConnectivityManager.isNetworkTypeValid(networkType) ||
Robert Greenwalt34848c02011-03-25 13:09:25 -0700812 mNetConfigs[networkType] == null) {
Robert Greenwalt2034b912009-08-12 16:08:25 -0700813 return Phone.APN_REQUEST_FAILED;
The Android Open Source Project28527d22009-03-03 19:31:44 -0800814 }
Robert Greenwalt2034b912009-08-12 16:08:25 -0700815
Robert Greenwaltaffc3a12009-09-27 17:27:04 -0700816 FeatureUser f = new FeatureUser(networkType, feature, binder);
Robert Greenwalt2034b912009-08-12 16:08:25 -0700817
818 // TODO - move this into the MobileDataStateTracker
819 int usedNetworkType = networkType;
820 if(networkType == ConnectivityManager.TYPE_MOBILE) {
Wink Savillef6b76692011-02-24 17:58:51 -0800821 usedNetworkType = convertFeatureToNetworkType(feature);
822 if (usedNetworkType < 0) {
823 Slog.e(TAG, "Can't match any netTracker!");
824 usedNetworkType = networkType;
Robert Greenwalt2034b912009-08-12 16:08:25 -0700825 }
826 }
Robert Greenwalt6cac0742011-06-21 17:26:14 -0700827
828 if (mProtectedNetworks.contains(usedNetworkType)) {
829 enforceConnectivityInternalPermission();
830 }
831
Robert Greenwalt2034b912009-08-12 16:08:25 -0700832 NetworkStateTracker network = mNetTrackers[usedNetworkType];
833 if (network != null) {
Robert Greenwalt5364d752010-12-15 13:26:33 -0800834 Integer currentPid = new Integer(getCallingPid());
Robert Greenwalt2034b912009-08-12 16:08:25 -0700835 if (usedNetworkType != networkType) {
Robert Greenwalt2034b912009-08-12 16:08:25 -0700836 NetworkStateTracker radio = mNetTrackers[networkType];
837 NetworkInfo ni = network.getNetworkInfo();
838
839 if (ni.isAvailable() == false) {
Wink Savillee70c6f52010-12-03 12:01:38 -0800840 if (DBG) log("special network not available");
Robert Greenwalt2cc87442010-12-29 14:35:21 -0800841 if (!TextUtils.equals(feature,Phone.FEATURE_ENABLE_DUN_ALWAYS)) {
842 return Phone.APN_TYPE_NOT_AVAILABLE;
843 } else {
844 // else make the attempt anyway - probably giving REQUEST_STARTED below
845 }
Robert Greenwalt2034b912009-08-12 16:08:25 -0700846 }
847
Robert Greenwaltaffc3a12009-09-27 17:27:04 -0700848 synchronized(this) {
849 mFeatureUsers.add(f);
850 if (!mNetRequestersPids[usedNetworkType].contains(currentPid)) {
851 // this gets used for per-pid dns when connected
852 mNetRequestersPids[usedNetworkType].add(currentPid);
853 }
Robert Greenwalt2034b912009-08-12 16:08:25 -0700854 }
Robert Greenwaltaffc3a12009-09-27 17:27:04 -0700855
Robert Greenwalt20f819c2011-05-03 19:02:44 -0700856 int restoreTimer = getRestoreDefaultNetworkDelay(usedNetworkType);
857
858 if (restoreTimer >= 0) {
859 mHandler.sendMessageDelayed(
860 mHandler.obtainMessage(EVENT_RESTORE_DEFAULT_NETWORK, f), restoreTimer);
861 }
Robert Greenwalt2034b912009-08-12 16:08:25 -0700862
Robert Greenwalta52c75a2009-08-19 20:19:33 -0700863 if ((ni.isConnectedOrConnecting() == true) &&
864 !network.isTeardownRequested()) {
Robert Greenwalt2034b912009-08-12 16:08:25 -0700865 if (ni.isConnected() == true) {
866 // add the pid-specific dns
Robert Greenwalt3afbead2010-07-23 15:46:26 -0700867 handleDnsConfigurationChange(networkType);
Wink Savillee70c6f52010-12-03 12:01:38 -0800868 if (DBG) log("special network already active");
Robert Greenwalt2034b912009-08-12 16:08:25 -0700869 return Phone.APN_ALREADY_ACTIVE;
870 }
Wink Savillee70c6f52010-12-03 12:01:38 -0800871 if (DBG) log("special network already connecting");
Robert Greenwalt2034b912009-08-12 16:08:25 -0700872 return Phone.APN_REQUEST_STARTED;
873 }
874
875 // check if the radio in play can make another contact
876 // assume if cannot for now
877
Wink Savillee70c6f52010-12-03 12:01:38 -0800878 if (DBG) log("reconnecting to special network");
Robert Greenwalt2034b912009-08-12 16:08:25 -0700879 network.reconnect();
880 return Phone.APN_REQUEST_STARTED;
881 } else {
Robert Greenwalt5364d752010-12-15 13:26:33 -0800882 // need to remember this unsupported request so we respond appropriately on stop
883 synchronized(this) {
884 mFeatureUsers.add(f);
885 if (!mNetRequestersPids[usedNetworkType].contains(currentPid)) {
886 // this gets used for per-pid dns when connected
887 mNetRequestersPids[usedNetworkType].add(currentPid);
888 }
889 }
Robert Greenwaltd391e892010-05-18 10:52:51 -0700890 return -1;
Robert Greenwalt2034b912009-08-12 16:08:25 -0700891 }
892 }
893 return Phone.APN_TYPE_NOT_AVAILABLE;
The Android Open Source Project28527d22009-03-03 19:31:44 -0800894 }
895
Robert Greenwaltaffc3a12009-09-27 17:27:04 -0700896 // javadoc from interface
The Android Open Source Project28527d22009-03-03 19:31:44 -0800897 public int stopUsingNetworkFeature(int networkType, String feature) {
Robert Greenwalt28f43012009-10-06 17:52:40 -0700898 enforceChangePermission();
899
Robert Greenwaltaffc3a12009-09-27 17:27:04 -0700900 int pid = getCallingPid();
901 int uid = getCallingUid();
902
903 FeatureUser u = null;
904 boolean found = false;
905
906 synchronized(this) {
907 for (int i = 0; i < mFeatureUsers.size() ; i++) {
908 u = (FeatureUser)mFeatureUsers.get(i);
909 if (uid == u.mUid && pid == u.mPid &&
910 networkType == u.mNetworkType &&
911 TextUtils.equals(feature, u.mFeature)) {
912 found = true;
913 break;
914 }
915 }
916 }
917 if (found && u != null) {
918 // stop regardless of how many other time this proc had called start
919 return stopUsingNetworkFeature(u, true);
920 } else {
921 // none found!
Wink Savillee70c6f52010-12-03 12:01:38 -0800922 if (DBG) log("ignoring stopUsingNetworkFeature - not a live request");
Robert Greenwaltaffc3a12009-09-27 17:27:04 -0700923 return 1;
924 }
Robert Greenwalt2034b912009-08-12 16:08:25 -0700925 }
926
Robert Greenwaltaffc3a12009-09-27 17:27:04 -0700927 private int stopUsingNetworkFeature(FeatureUser u, boolean ignoreDups) {
928 int networkType = u.mNetworkType;
929 String feature = u.mFeature;
930 int pid = u.mPid;
931 int uid = u.mUid;
932
933 NetworkStateTracker tracker = null;
934 boolean callTeardown = false; // used to carry our decision outside of sync block
935
Robert Greenwalt2034b912009-08-12 16:08:25 -0700936 if (DBG) {
Wink Savillee70c6f52010-12-03 12:01:38 -0800937 log("stopUsingNetworkFeature for net " + networkType +
Robert Greenwalt2034b912009-08-12 16:08:25 -0700938 ": " + feature);
939 }
Robert Greenwalt28f43012009-10-06 17:52:40 -0700940
The Android Open Source Project28527d22009-03-03 19:31:44 -0800941 if (!ConnectivityManager.isNetworkTypeValid(networkType)) {
942 return -1;
943 }
Robert Greenwalt2034b912009-08-12 16:08:25 -0700944
Robert Greenwaltaffc3a12009-09-27 17:27:04 -0700945 // need to link the mFeatureUsers list with the mNetRequestersPids state in this
946 // sync block
947 synchronized(this) {
948 // check if this process still has an outstanding start request
949 if (!mFeatureUsers.contains(u)) {
Wink Savillee70c6f52010-12-03 12:01:38 -0800950 if (DBG) log("ignoring - this process has no outstanding requests");
Robert Greenwalt2034b912009-08-12 16:08:25 -0700951 return 1;
952 }
Robert Greenwaltaffc3a12009-09-27 17:27:04 -0700953 u.unlinkDeathRecipient();
954 mFeatureUsers.remove(mFeatureUsers.indexOf(u));
955 // If we care about duplicate requests, check for that here.
956 //
957 // This is done to support the extension of a request - the app
958 // can request we start the network feature again and renew the
959 // auto-shutoff delay. Normal "stop" calls from the app though
960 // do not pay attention to duplicate requests - in effect the
961 // API does not refcount and a single stop will counter multiple starts.
962 if (ignoreDups == false) {
963 for (int i = 0; i < mFeatureUsers.size() ; i++) {
964 FeatureUser x = (FeatureUser)mFeatureUsers.get(i);
965 if (x.mUid == u.mUid && x.mPid == u.mPid &&
966 x.mNetworkType == u.mNetworkType &&
967 TextUtils.equals(x.mFeature, u.mFeature)) {
Wink Savillee70c6f52010-12-03 12:01:38 -0800968 if (DBG) log("ignoring stopUsingNetworkFeature as dup is found");
Robert Greenwaltaffc3a12009-09-27 17:27:04 -0700969 return 1;
970 }
971 }
Robert Greenwalt2034b912009-08-12 16:08:25 -0700972 }
Robert Greenwaltaffc3a12009-09-27 17:27:04 -0700973
974 // TODO - move to MobileDataStateTracker
975 int usedNetworkType = networkType;
976 if (networkType == ConnectivityManager.TYPE_MOBILE) {
Wink Savillef6b76692011-02-24 17:58:51 -0800977 usedNetworkType = convertFeatureToNetworkType(feature);
978 if (usedNetworkType < 0) {
979 usedNetworkType = networkType;
Robert Greenwaltaffc3a12009-09-27 17:27:04 -0700980 }
981 }
982 tracker = mNetTrackers[usedNetworkType];
Robert Greenwaltec05b3c2009-10-30 14:17:42 -0700983 if (tracker == null) {
Wink Savillee70c6f52010-12-03 12:01:38 -0800984 if (DBG) log("ignoring - no known tracker for net type " + usedNetworkType);
Robert Greenwaltec05b3c2009-10-30 14:17:42 -0700985 return -1;
986 }
987 if (usedNetworkType != networkType) {
Robert Greenwaltaffc3a12009-09-27 17:27:04 -0700988 Integer currentPid = new Integer(pid);
Robert Greenwaltaffc3a12009-09-27 17:27:04 -0700989 mNetRequestersPids[usedNetworkType].remove(currentPid);
Robert Greenwalt0ca68a02009-12-17 14:54:59 -0800990 reassessPidDns(pid, true);
Robert Greenwaltaffc3a12009-09-27 17:27:04 -0700991 if (mNetRequestersPids[usedNetworkType].size() != 0) {
Wink Savillee70c6f52010-12-03 12:01:38 -0800992 if (DBG) log("not tearing down special network - " +
Robert Greenwaltaffc3a12009-09-27 17:27:04 -0700993 "others still using it");
994 return 1;
995 }
996 callTeardown = true;
Robert Greenwalt9f3be4c2011-01-10 11:58:31 -0800997 } else {
998 if (DBG) log("not a known feature - dropping");
Robert Greenwaltaffc3a12009-09-27 17:27:04 -0700999 }
1000 }
Wink Savillee70c6f52010-12-03 12:01:38 -08001001 if (DBG) log("Doing network teardown");
Robert Greenwaltaffc3a12009-09-27 17:27:04 -07001002 if (callTeardown) {
1003 tracker.teardown();
Robert Greenwalt2034b912009-08-12 16:08:25 -07001004 return 1;
1005 } else {
Robert Greenwaltd391e892010-05-18 10:52:51 -07001006 return -1;
Robert Greenwalt2034b912009-08-12 16:08:25 -07001007 }
The Android Open Source Project28527d22009-03-03 19:31:44 -08001008 }
1009
1010 /**
Robert Greenwalt7fe44cb2010-08-27 09:24:29 -07001011 * @deprecated use requestRouteToHostAddress instead
1012 *
The Android Open Source Project28527d22009-03-03 19:31:44 -08001013 * Ensure that a network route exists to deliver traffic to the specified
1014 * host via the specified network interface.
Robert Greenwalt0659da32009-07-16 17:21:39 -07001015 * @param networkType the type of the network over which traffic to the
1016 * specified host is to be routed
1017 * @param hostAddress the IP address of the host to which the route is
1018 * desired
The Android Open Source Project28527d22009-03-03 19:31:44 -08001019 * @return {@code true} on success, {@code false} on failure
1020 */
1021 public boolean requestRouteToHost(int networkType, int hostAddress) {
Robert Greenwalt7fe44cb2010-08-27 09:24:29 -07001022 InetAddress inetAddress = NetworkUtils.intToInetAddress(hostAddress);
1023
1024 if (inetAddress == null) {
1025 return false;
1026 }
1027
1028 return requestRouteToHostAddress(networkType, inetAddress.getAddress());
1029 }
1030
1031 /**
1032 * Ensure that a network route exists to deliver traffic to the specified
1033 * host via the specified network interface.
1034 * @param networkType the type of the network over which traffic to the
1035 * specified host is to be routed
1036 * @param hostAddress the IP address of the host to which the route is
1037 * desired
1038 * @return {@code true} on success, {@code false} on failure
1039 */
1040 public boolean requestRouteToHostAddress(int networkType, byte[] hostAddress) {
The Android Open Source Project28527d22009-03-03 19:31:44 -08001041 enforceChangePermission();
Robert Greenwalt6cac0742011-06-21 17:26:14 -07001042 if (mProtectedNetworks.contains(networkType)) {
1043 enforceConnectivityInternalPermission();
1044 }
1045
The Android Open Source Project28527d22009-03-03 19:31:44 -08001046 if (!ConnectivityManager.isNetworkTypeValid(networkType)) {
1047 return false;
1048 }
1049 NetworkStateTracker tracker = mNetTrackers[networkType];
Robert Greenwalt4666ed02009-09-10 15:06:20 -07001050
Robert Greenwaltec05b3c2009-10-30 14:17:42 -07001051 if (tracker == null || !tracker.getNetworkInfo().isConnected() ||
1052 tracker.isTeardownRequested()) {
Robert Greenwalt4666ed02009-09-10 15:06:20 -07001053 if (DBG) {
Wink Savillee70c6f52010-12-03 12:01:38 -08001054 log("requestRouteToHostAddress on down network " +
Robert Greenwalt7fe44cb2010-08-27 09:24:29 -07001055 "(" + networkType + ") - dropped");
Robert Greenwalt4666ed02009-09-10 15:06:20 -07001056 }
1057 return false;
The Android Open Source Project28527d22009-03-03 19:31:44 -08001058 }
Robert Greenwalta7dfbd32010-06-15 15:43:39 -07001059 try {
Robert Greenwalt7fe44cb2010-08-27 09:24:29 -07001060 InetAddress addr = InetAddress.getByAddress(hostAddress);
Robert Greenwalte8d2a4a2011-07-14 14:28:05 -07001061 LinkProperties lp = tracker.getLinkProperties();
1062 return addRoute(lp, RouteInfo.makeHostRoute(addr));
Robert Greenwalta7dfbd32010-06-15 15:43:39 -07001063 } catch (UnknownHostException e) {}
1064 return false;
Irfan Sheriff7f132d92010-06-09 15:39:36 -07001065 }
1066
Robert Greenwalte8d2a4a2011-07-14 14:28:05 -07001067 private boolean addRoute(LinkProperties p, RouteInfo r) {
1068 return modifyRoute(p.getInterfaceName(), p, r, 0, true);
Robert Greenwaltbd492212011-05-06 17:10:53 -07001069 }
1070
Robert Greenwalte8d2a4a2011-07-14 14:28:05 -07001071 private boolean removeRoute(LinkProperties p, RouteInfo r) {
1072 return modifyRoute(p.getInterfaceName(), p, r, 0, false);
1073 }
1074
1075 private boolean modifyRoute(String ifaceName, LinkProperties lp, RouteInfo r, int cycleCount,
1076 boolean doAdd) {
1077 if ((ifaceName == null) || (lp == null) || (r == null)) return false;
1078
1079 if (cycleCount > MAX_HOSTROUTE_CYCLE_COUNT) {
1080 loge("Error adding route - too much recursion");
1081 return false;
1082 }
1083
1084 if (r.isHostRoute() == false) {
1085 RouteInfo bestRoute = RouteInfo.selectBestRoute(lp.getRoutes(), r.getGateway());
1086 if (bestRoute != null) {
1087 if (bestRoute.getGateway().equals(r.getGateway()) == false) {
1088 bestRoute = RouteInfo.makeHostRoute(r.getGateway(), bestRoute.getGateway());
1089 } else {
1090 bestRoute = RouteInfo.makeHostRoute(r.getGateway());
1091 }
1092 if (!modifyRoute(ifaceName, lp, bestRoute, cycleCount+1, doAdd)) return false;
1093 }
1094 }
1095 if (doAdd) {
1096 if (DBG) log("Adding " + r + " for interface " + ifaceName);
1097 mAddedRoutes.add(r);
1098 try {
1099 mNetd.addRoute(ifaceName, r);
1100 } catch (Exception e) {
1101 // never crash - catch them all
1102 loge("Exception trying to add a route: " + e);
1103 return false;
1104 }
1105 } else {
1106 // if we remove this one and there are no more like it, then refcount==0 and
1107 // we can remove it from the table
1108 mAddedRoutes.remove(r);
1109 if (mAddedRoutes.contains(r) == false) {
1110 if (DBG) log("Removing " + r + " for interface " + ifaceName);
1111 try {
1112 mNetd.removeRoute(ifaceName, r);
1113 } catch (Exception e) {
1114 // never crash - catch them all
1115 loge("Exception trying to remove a route: " + e);
1116 return false;
1117 }
1118 } else {
1119 if (DBG) log("not removing " + r + " as it's still in use");
1120 }
1121 }
1122 return true;
The Android Open Source Project28527d22009-03-03 19:31:44 -08001123 }
1124
1125 /**
1126 * @see ConnectivityManager#getBackgroundDataSetting()
1127 */
1128 public boolean getBackgroundDataSetting() {
Robert Greenwaltd62c7002010-12-29 16:15:02 -08001129 return mBackgroundDataEnabled.get();
The Android Open Source Project28527d22009-03-03 19:31:44 -08001130 }
Robert Greenwalt0659da32009-07-16 17:21:39 -07001131
The Android Open Source Project28527d22009-03-03 19:31:44 -08001132 /**
1133 * @see ConnectivityManager#setBackgroundDataSetting(boolean)
1134 */
1135 public void setBackgroundDataSetting(boolean allowBackgroundDataUsage) {
1136 mContext.enforceCallingOrSelfPermission(
1137 android.Manifest.permission.CHANGE_BACKGROUND_DATA_SETTING,
1138 "ConnectivityService");
Robert Greenwalt0659da32009-07-16 17:21:39 -07001139
Robert Greenwaltd62c7002010-12-29 16:15:02 -08001140 mBackgroundDataEnabled.set(allowBackgroundDataUsage);
1141
Robert Greenwalt6a2db8a2010-09-23 10:05:56 -07001142 mHandler.sendMessage(mHandler.obtainMessage(EVENT_SET_BACKGROUND_DATA,
1143 (allowBackgroundDataUsage ? ENABLED : DISABLED), 0));
1144 }
The Android Open Source Project28527d22009-03-03 19:31:44 -08001145
Robert Greenwalt6a2db8a2010-09-23 10:05:56 -07001146 private void handleSetBackgroundData(boolean enabled) {
Robert Greenwalt0ffdef12011-02-25 13:44:09 -08001147 Settings.Secure.putInt(mContext.getContentResolver(),
1148 Settings.Secure.BACKGROUND_DATA, enabled ? 1 : 0);
1149 Intent broadcast = new Intent(
1150 ConnectivityManager.ACTION_BACKGROUND_DATA_SETTING_CHANGED);
1151 mContext.sendBroadcast(broadcast);
Robert Greenwalt0659da32009-07-16 17:21:39 -07001152 }
1153
Robert Greenwalt1b21f6c2010-02-23 18:58:05 -08001154 /**
1155 * @see ConnectivityManager#getMobileDataEnabled()
1156 */
1157 public boolean getMobileDataEnabled() {
Wink Savilleb9024c62010-12-07 10:31:02 -08001158 // TODO: This detail should probably be in DataConnectionTracker's
1159 // which is where we store the value and maybe make this
1160 // asynchronous.
Robert Greenwalt1b21f6c2010-02-23 18:58:05 -08001161 enforceAccessPermission();
1162 boolean retVal = Settings.Secure.getInt(mContext.getContentResolver(),
1163 Settings.Secure.MOBILE_DATA, 1) == 1;
Wink Savillee70c6f52010-12-03 12:01:38 -08001164 if (DBG) log("getMobileDataEnabled returning " + retVal);
Robert Greenwalt1b21f6c2010-02-23 18:58:05 -08001165 return retVal;
1166 }
1167
Robert Greenwalt34848c02011-03-25 13:09:25 -07001168 public void setDataDependency(int networkType, boolean met) {
Robert Greenwalt6cac0742011-06-21 17:26:14 -07001169 enforceConnectivityInternalPermission();
1170
Robert Greenwalt34848c02011-03-25 13:09:25 -07001171 if (DBG) {
1172 log("setDataDependency(" + networkType + ", " + met + ")");
1173 }
1174 mHandler.sendMessage(mHandler.obtainMessage(EVENT_SET_DEPENDENCY_MET,
1175 (met ? ENABLED : DISABLED), networkType));
1176 }
1177
1178 private void handleSetDependencyMet(int networkType, boolean met) {
1179 if (mNetTrackers[networkType] != null) {
1180 if (DBG) {
1181 log("handleSetDependencyMet(" + networkType + ", " + met + ")");
1182 }
1183 mNetTrackers[networkType].setDependencyMet(met);
1184 }
1185 }
1186
Jeff Sharkey921ebf22011-05-19 17:12:49 -07001187 private INetworkPolicyListener mPolicyListener = new INetworkPolicyListener.Stub() {
1188 @Override
Jeff Sharkeya47d7a12011-06-16 15:07:48 -07001189 public void onUidRulesChanged(int uid, int uidRules) {
Jeff Sharkey921ebf22011-05-19 17:12:49 -07001190 // only someone like NPMS should only be calling us
Jeff Sharkey4434b0b2011-06-16 13:04:20 -07001191 mContext.enforceCallingOrSelfPermission(MANAGE_NETWORK_POLICY, TAG);
Jeff Sharkey921ebf22011-05-19 17:12:49 -07001192
1193 if (LOGD_RULES) {
Jeff Sharkeya47d7a12011-06-16 15:07:48 -07001194 Slog.d(TAG, "onUidRulesChanged(uid=" + uid + ", uidRules=" + uidRules + ")");
Jeff Sharkey921ebf22011-05-19 17:12:49 -07001195 }
1196
Jeff Sharkeya47d7a12011-06-16 15:07:48 -07001197 synchronized (mRulesLock) {
Jeff Sharkey921ebf22011-05-19 17:12:49 -07001198 // skip update when we've already applied rules
1199 final int oldRules = mUidRules.get(uid, RULE_ALLOW_ALL);
1200 if (oldRules == uidRules) return;
1201
1202 mUidRules.put(uid, uidRules);
1203 }
1204
1205 // TODO: dispatch into NMS to push rules towards kernel module
1206 // TODO: notify UID when it has requested targeted updates
1207 }
Jeff Sharkeya47d7a12011-06-16 15:07:48 -07001208
1209 @Override
1210 public void onMeteredIfacesChanged(String[] meteredIfaces) {
1211 // only someone like NPMS should only be calling us
1212 mContext.enforceCallingOrSelfPermission(MANAGE_NETWORK_POLICY, TAG);
1213
1214 if (LOGD_RULES) {
1215 Slog.d(TAG,
1216 "onMeteredIfacesChanged(ifaces=" + Arrays.toString(meteredIfaces) + ")");
1217 }
1218
1219 synchronized (mRulesLock) {
1220 mMeteredIfaces.clear();
1221 for (String iface : meteredIfaces) {
1222 mMeteredIfaces.add(iface);
1223 }
1224 }
1225 }
Jeff Sharkey921ebf22011-05-19 17:12:49 -07001226 };
1227
Robert Greenwalt1b21f6c2010-02-23 18:58:05 -08001228 /**
1229 * @see ConnectivityManager#setMobileDataEnabled(boolean)
1230 */
Robert Greenwalt6a2db8a2010-09-23 10:05:56 -07001231 public void setMobileDataEnabled(boolean enabled) {
Robert Greenwalt1b21f6c2010-02-23 18:58:05 -08001232 enforceChangePermission();
Wink Savillee70c6f52010-12-03 12:01:38 -08001233 if (DBG) log("setMobileDataEnabled(" + enabled + ")");
Robert Greenwalt1b21f6c2010-02-23 18:58:05 -08001234
Robert Greenwalt6a2db8a2010-09-23 10:05:56 -07001235 mHandler.sendMessage(mHandler.obtainMessage(EVENT_SET_MOBILE_DATA,
Robert Greenwalt34848c02011-03-25 13:09:25 -07001236 (enabled ? ENABLED : DISABLED), 0));
Robert Greenwalt6a2db8a2010-09-23 10:05:56 -07001237 }
1238
1239 private void handleSetMobileData(boolean enabled) {
Wink Savilleb9024c62010-12-07 10:31:02 -08001240 if (mNetTrackers[ConnectivityManager.TYPE_MOBILE] != null) {
1241 if (DBG) {
1242 Slog.d(TAG, mNetTrackers[ConnectivityManager.TYPE_MOBILE].toString() + enabled);
Robert Greenwalt1b21f6c2010-02-23 18:58:05 -08001243 }
Wink Savilleb9024c62010-12-07 10:31:02 -08001244 mNetTrackers[ConnectivityManager.TYPE_MOBILE].setDataEnable(enabled);
Robert Greenwalt1b21f6c2010-02-23 18:58:05 -08001245 }
1246 }
1247
The Android Open Source Project28527d22009-03-03 19:31:44 -08001248 private void enforceAccessPermission() {
Robert Greenwalt0659da32009-07-16 17:21:39 -07001249 mContext.enforceCallingOrSelfPermission(
1250 android.Manifest.permission.ACCESS_NETWORK_STATE,
1251 "ConnectivityService");
The Android Open Source Project28527d22009-03-03 19:31:44 -08001252 }
1253
1254 private void enforceChangePermission() {
Robert Greenwalt0659da32009-07-16 17:21:39 -07001255 mContext.enforceCallingOrSelfPermission(
1256 android.Manifest.permission.CHANGE_NETWORK_STATE,
1257 "ConnectivityService");
The Android Open Source Project28527d22009-03-03 19:31:44 -08001258 }
1259
Robert Greenwalt0c4828c2010-01-26 11:40:34 -08001260 // TODO Make this a special check when it goes public
1261 private void enforceTetherChangePermission() {
1262 mContext.enforceCallingOrSelfPermission(
1263 android.Manifest.permission.CHANGE_NETWORK_STATE,
1264 "ConnectivityService");
1265 }
1266
Robert Greenwalt8e87f122010-02-11 18:18:40 -08001267 private void enforceTetherAccessPermission() {
1268 mContext.enforceCallingOrSelfPermission(
1269 android.Manifest.permission.ACCESS_NETWORK_STATE,
1270 "ConnectivityService");
1271 }
1272
Robert Greenwalt93dc1042010-06-15 12:19:37 -07001273 private void enforceConnectivityInternalPermission() {
1274 mContext.enforceCallingOrSelfPermission(
1275 android.Manifest.permission.CONNECTIVITY_INTERNAL,
1276 "ConnectivityService");
1277 }
1278
The Android Open Source Project28527d22009-03-03 19:31:44 -08001279 /**
Robert Greenwalt0659da32009-07-16 17:21:39 -07001280 * Handle a {@code DISCONNECTED} event. If this pertains to the non-active
1281 * network, we ignore it. If it is for the active network, we send out a
1282 * broadcast. But first, we check whether it might be possible to connect
1283 * to a different network.
The Android Open Source Project28527d22009-03-03 19:31:44 -08001284 * @param info the {@code NetworkInfo} for the network
1285 */
1286 private void handleDisconnect(NetworkInfo info) {
1287
Robert Greenwalt2034b912009-08-12 16:08:25 -07001288 int prevNetType = info.getType();
The Android Open Source Project28527d22009-03-03 19:31:44 -08001289
Robert Greenwalt2034b912009-08-12 16:08:25 -07001290 mNetTrackers[prevNetType].setTeardownRequested(false);
The Android Open Source Project28527d22009-03-03 19:31:44 -08001291 /*
1292 * If the disconnected network is not the active one, then don't report
1293 * this as a loss of connectivity. What probably happened is that we're
1294 * getting the disconnect for a network that we explicitly disabled
1295 * in accordance with network preference policies.
1296 */
Robert Greenwalt34848c02011-03-25 13:09:25 -07001297 if (!mNetConfigs[prevNetType].isDefault()) {
Robert Greenwalt2034b912009-08-12 16:08:25 -07001298 List pids = mNetRequestersPids[prevNetType];
1299 for (int i = 0; i<pids.size(); i++) {
1300 Integer pid = (Integer)pids.get(i);
1301 // will remove them because the net's no longer connected
1302 // need to do this now as only now do we know the pids and
1303 // can properly null things that are no longer referenced.
1304 reassessPidDns(pid.intValue(), false);
The Android Open Source Project28527d22009-03-03 19:31:44 -08001305 }
The Android Open Source Project28527d22009-03-03 19:31:44 -08001306 }
1307
The Android Open Source Project28527d22009-03-03 19:31:44 -08001308 Intent intent = new Intent(ConnectivityManager.CONNECTIVITY_ACTION);
1309 intent.putExtra(ConnectivityManager.EXTRA_NETWORK_INFO, info);
1310 if (info.isFailover()) {
1311 intent.putExtra(ConnectivityManager.EXTRA_IS_FAILOVER, true);
1312 info.setFailover(false);
1313 }
1314 if (info.getReason() != null) {
1315 intent.putExtra(ConnectivityManager.EXTRA_REASON, info.getReason());
1316 }
1317 if (info.getExtraInfo() != null) {
Robert Greenwalt0659da32009-07-16 17:21:39 -07001318 intent.putExtra(ConnectivityManager.EXTRA_EXTRA_INFO,
1319 info.getExtraInfo());
The Android Open Source Project28527d22009-03-03 19:31:44 -08001320 }
Robert Greenwalt2034b912009-08-12 16:08:25 -07001321
Robert Greenwalt34848c02011-03-25 13:09:25 -07001322 if (mNetConfigs[prevNetType].isDefault()) {
Robert Greenwalt4b7c55e2011-01-11 13:56:33 -08001323 tryFailover(prevNetType);
1324 if (mActiveDefaultNetwork != -1) {
1325 NetworkInfo switchTo = mNetTrackers[mActiveDefaultNetwork].getNetworkInfo();
Robert Greenwalt3cc68d32010-01-25 17:54:29 -08001326 intent.putExtra(ConnectivityManager.EXTRA_OTHER_NETWORK_INFO, switchTo);
1327 } else {
Robert Greenwaltb2a9e492010-09-20 18:01:43 -07001328 mDefaultInetConditionPublished = 0; // we're not connected anymore
Robert Greenwalt3cc68d32010-01-25 17:54:29 -08001329 intent.putExtra(ConnectivityManager.EXTRA_NO_CONNECTIVITY, true);
1330 }
Robert Greenwaltf55ced92010-01-20 19:29:41 -08001331 }
Robert Greenwaltb2a9e492010-09-20 18:01:43 -07001332 intent.putExtra(ConnectivityManager.EXTRA_INET_CONDITION, mDefaultInetConditionPublished);
Robert Greenwalt36ea8692011-06-15 12:22:07 -07001333
1334 // Reset interface if no other connections are using the same interface
1335 boolean doReset = true;
1336 LinkProperties linkProperties = mNetTrackers[prevNetType].getLinkProperties();
1337 if (linkProperties != null) {
1338 String oldIface = linkProperties.getInterfaceName();
1339 if (TextUtils.isEmpty(oldIface) == false) {
1340 for (NetworkStateTracker networkStateTracker : mNetTrackers) {
1341 if (networkStateTracker == null) continue;
1342 NetworkInfo networkInfo = networkStateTracker.getNetworkInfo();
1343 if (networkInfo.isConnected() && networkInfo.getType() != prevNetType) {
1344 LinkProperties l = networkStateTracker.getLinkProperties();
1345 if (l == null) continue;
1346 if (oldIface.equals(l.getInterfaceName())) {
1347 doReset = false;
1348 break;
1349 }
1350 }
1351 }
1352 }
1353 }
1354
Robert Greenwaltf55ced92010-01-20 19:29:41 -08001355 // do this before we broadcast the change
Robert Greenwalt36ea8692011-06-15 12:22:07 -07001356 handleConnectivityChange(prevNetType, doReset);
Robert Greenwaltf55ced92010-01-20 19:29:41 -08001357
1358 sendStickyBroadcast(intent);
1359 /*
1360 * If the failover network is already connected, then immediately send
1361 * out a followup broadcast indicating successful failover
1362 */
Robert Greenwalt4b7c55e2011-01-11 13:56:33 -08001363 if (mActiveDefaultNetwork != -1) {
1364 sendConnectedBroadcast(mNetTrackers[mActiveDefaultNetwork].getNetworkInfo());
Robert Greenwaltf55ced92010-01-20 19:29:41 -08001365 }
1366 }
1367
Robert Greenwalt4b7c55e2011-01-11 13:56:33 -08001368 private void tryFailover(int prevNetType) {
Robert Greenwalt2034b912009-08-12 16:08:25 -07001369 /*
Robert Greenwalt92564852011-01-06 15:41:07 -08001370 * If this is a default network, check if other defaults are available.
1371 * Try to reconnect on all available and let them hash it out when
1372 * more than one connects.
Robert Greenwalt2034b912009-08-12 16:08:25 -07001373 */
Robert Greenwalt34848c02011-03-25 13:09:25 -07001374 if (mNetConfigs[prevNetType].isDefault()) {
Robert Greenwalt2034b912009-08-12 16:08:25 -07001375 if (mActiveDefaultNetwork == prevNetType) {
1376 mActiveDefaultNetwork = -1;
1377 }
1378
Robert Greenwalt4b7c55e2011-01-11 13:56:33 -08001379 // don't signal a reconnect for anything lower or equal priority than our
1380 // current connected default
1381 // TODO - don't filter by priority now - nice optimization but risky
1382// int currentPriority = -1;
1383// if (mActiveDefaultNetwork != -1) {
Robert Greenwalt34848c02011-03-25 13:09:25 -07001384// currentPriority = mNetConfigs[mActiveDefaultNetwork].mPriority;
Robert Greenwalt4b7c55e2011-01-11 13:56:33 -08001385// }
Robert Greenwaltf55ced92010-01-20 19:29:41 -08001386 for (int checkType=0; checkType <= ConnectivityManager.MAX_NETWORK_TYPE; checkType++) {
Robert Greenwaltec05b3c2009-10-30 14:17:42 -07001387 if (checkType == prevNetType) continue;
Robert Greenwalt34848c02011-03-25 13:09:25 -07001388 if (mNetConfigs[checkType] == null) continue;
1389 if (!mNetConfigs[checkType].isDefault()) continue;
Wink Saville72a95b92011-01-26 15:43:49 -08001390
1391// Enabling the isAvailable() optimization caused mobile to not get
1392// selected if it was in the middle of error handling. Specifically
1393// a moble connection that took 30 seconds to complete the DEACTIVATE_DATA_CALL
1394// would not be available and we wouldn't get connected to anything.
1395// So removing the isAvailable() optimization below for now. TODO: This
1396// optimization should work and we need to investigate why it doesn't work.
1397// This could be related to how DEACTIVATE_DATA_CALL is reporting its
1398// complete before it is really complete.
1399// if (!mNetTrackers[checkType].isAvailable()) continue;
1400
Robert Greenwalt34848c02011-03-25 13:09:25 -07001401// if (currentPriority >= mNetConfigs[checkType].mPriority) continue;
Robert Greenwalt2034b912009-08-12 16:08:25 -07001402
Robert Greenwalt4b7c55e2011-01-11 13:56:33 -08001403 NetworkStateTracker checkTracker = mNetTrackers[checkType];
1404 NetworkInfo checkInfo = checkTracker.getNetworkInfo();
1405 if (!checkInfo.isConnectedOrConnecting() || checkTracker.isTeardownRequested()) {
1406 checkInfo.setFailover(true);
1407 checkTracker.reconnect();
Robert Greenwalt2034b912009-08-12 16:08:25 -07001408 }
Robert Greenwalt4b7c55e2011-01-11 13:56:33 -08001409 if (DBG) log("Attempting to switch to " + checkInfo.getTypeName());
Robert Greenwalt2034b912009-08-12 16:08:25 -07001410 }
The Android Open Source Project28527d22009-03-03 19:31:44 -08001411 }
The Android Open Source Project28527d22009-03-03 19:31:44 -08001412 }
1413
1414 private void sendConnectedBroadcast(NetworkInfo info) {
Robert Greenwaltd3401f92010-09-15 17:36:33 -07001415 sendGeneralBroadcast(info, ConnectivityManager.CONNECTIVITY_ACTION);
1416 }
1417
1418 private void sendInetConditionBroadcast(NetworkInfo info) {
1419 sendGeneralBroadcast(info, ConnectivityManager.INET_CONDITION_ACTION);
1420 }
1421
1422 private void sendGeneralBroadcast(NetworkInfo info, String bcastType) {
1423 Intent intent = new Intent(bcastType);
The Android Open Source Project28527d22009-03-03 19:31:44 -08001424 intent.putExtra(ConnectivityManager.EXTRA_NETWORK_INFO, info);
1425 if (info.isFailover()) {
1426 intent.putExtra(ConnectivityManager.EXTRA_IS_FAILOVER, true);
1427 info.setFailover(false);
1428 }
1429 if (info.getReason() != null) {
1430 intent.putExtra(ConnectivityManager.EXTRA_REASON, info.getReason());
1431 }
1432 if (info.getExtraInfo() != null) {
Robert Greenwalt0659da32009-07-16 17:21:39 -07001433 intent.putExtra(ConnectivityManager.EXTRA_EXTRA_INFO,
1434 info.getExtraInfo());
The Android Open Source Project28527d22009-03-03 19:31:44 -08001435 }
Robert Greenwalt986c7412010-09-08 15:24:47 -07001436 intent.putExtra(ConnectivityManager.EXTRA_INET_CONDITION, mDefaultInetConditionPublished);
Mike Lockwoodfde2b762009-08-14 14:18:49 -04001437 sendStickyBroadcast(intent);
The Android Open Source Project28527d22009-03-03 19:31:44 -08001438 }
1439
1440 /**
1441 * Called when an attempt to fail over to another network has failed.
1442 * @param info the {@link NetworkInfo} for the failed network
1443 */
1444 private void handleConnectionFailure(NetworkInfo info) {
1445 mNetTrackers[info.getType()].setTeardownRequested(false);
The Android Open Source Project28527d22009-03-03 19:31:44 -08001446
Robert Greenwalt2034b912009-08-12 16:08:25 -07001447 String reason = info.getReason();
1448 String extraInfo = info.getExtraInfo();
Robert Greenwalt0659da32009-07-16 17:21:39 -07001449
Robert Greenwalte981bc52010-10-08 16:35:52 -07001450 String reasonText;
1451 if (reason == null) {
1452 reasonText = ".";
1453 } else {
1454 reasonText = " (" + reason + ").";
The Android Open Source Project28527d22009-03-03 19:31:44 -08001455 }
Wink Savillee70c6f52010-12-03 12:01:38 -08001456 loge("Attempt to connect to " + info.getTypeName() + " failed" + reasonText);
Robert Greenwalt2034b912009-08-12 16:08:25 -07001457
1458 Intent intent = new Intent(ConnectivityManager.CONNECTIVITY_ACTION);
1459 intent.putExtra(ConnectivityManager.EXTRA_NETWORK_INFO, info);
1460 if (getActiveNetworkInfo() == null) {
1461 intent.putExtra(ConnectivityManager.EXTRA_NO_CONNECTIVITY, true);
1462 }
1463 if (reason != null) {
1464 intent.putExtra(ConnectivityManager.EXTRA_REASON, reason);
1465 }
1466 if (extraInfo != null) {
1467 intent.putExtra(ConnectivityManager.EXTRA_EXTRA_INFO, extraInfo);
1468 }
1469 if (info.isFailover()) {
1470 intent.putExtra(ConnectivityManager.EXTRA_IS_FAILOVER, true);
1471 info.setFailover(false);
1472 }
Robert Greenwaltf55ced92010-01-20 19:29:41 -08001473
Robert Greenwalt34848c02011-03-25 13:09:25 -07001474 if (mNetConfigs[info.getType()].isDefault()) {
Robert Greenwalt4b7c55e2011-01-11 13:56:33 -08001475 tryFailover(info.getType());
1476 if (mActiveDefaultNetwork != -1) {
1477 NetworkInfo switchTo = mNetTrackers[mActiveDefaultNetwork].getNetworkInfo();
Robert Greenwalt3cc68d32010-01-25 17:54:29 -08001478 intent.putExtra(ConnectivityManager.EXTRA_OTHER_NETWORK_INFO, switchTo);
1479 } else {
Robert Greenwaltb2a9e492010-09-20 18:01:43 -07001480 mDefaultInetConditionPublished = 0;
Robert Greenwalt3cc68d32010-01-25 17:54:29 -08001481 intent.putExtra(ConnectivityManager.EXTRA_NO_CONNECTIVITY, true);
1482 }
Robert Greenwaltf55ced92010-01-20 19:29:41 -08001483 }
Robert Greenwalt3cc68d32010-01-25 17:54:29 -08001484
Robert Greenwaltb2a9e492010-09-20 18:01:43 -07001485 intent.putExtra(ConnectivityManager.EXTRA_INET_CONDITION, mDefaultInetConditionPublished);
Mike Lockwoodfde2b762009-08-14 14:18:49 -04001486 sendStickyBroadcast(intent);
Robert Greenwaltf55ced92010-01-20 19:29:41 -08001487 /*
1488 * If the failover network is already connected, then immediately send
1489 * out a followup broadcast indicating successful failover
1490 */
Robert Greenwalt4b7c55e2011-01-11 13:56:33 -08001491 if (mActiveDefaultNetwork != -1) {
1492 sendConnectedBroadcast(mNetTrackers[mActiveDefaultNetwork].getNetworkInfo());
Robert Greenwaltf55ced92010-01-20 19:29:41 -08001493 }
Mike Lockwoodfde2b762009-08-14 14:18:49 -04001494 }
1495
1496 private void sendStickyBroadcast(Intent intent) {
1497 synchronized(this) {
Dianne Hackborna417ff82009-12-08 19:45:14 -08001498 if (!mSystemReady) {
1499 mInitialBroadcast = new Intent(intent);
Mike Lockwoodfde2b762009-08-14 14:18:49 -04001500 }
Dianne Hackborna417ff82009-12-08 19:45:14 -08001501 intent.addFlags(Intent.FLAG_RECEIVER_REGISTERED_ONLY_BEFORE_BOOT);
1502 mContext.sendStickyBroadcast(intent);
Mike Lockwoodfde2b762009-08-14 14:18:49 -04001503 }
1504 }
1505
1506 void systemReady() {
1507 synchronized(this) {
1508 mSystemReady = true;
Dianne Hackborna417ff82009-12-08 19:45:14 -08001509 if (mInitialBroadcast != null) {
1510 mContext.sendStickyBroadcast(mInitialBroadcast);
1511 mInitialBroadcast = null;
Mike Lockwoodfde2b762009-08-14 14:18:49 -04001512 }
1513 }
Robert Greenwaltc3c5f862010-10-11 16:00:27 -07001514 // load the global proxy at startup
1515 mHandler.sendMessage(mHandler.obtainMessage(EVENT_APPLY_GLOBAL_HTTP_PROXY));
The Android Open Source Project28527d22009-03-03 19:31:44 -08001516 }
1517
1518 private void handleConnect(NetworkInfo info) {
Robert Greenwalt2034b912009-08-12 16:08:25 -07001519 int type = info.getType();
The Android Open Source Project28527d22009-03-03 19:31:44 -08001520
1521 // snapshot isFailover, because sendConnectedBroadcast() resets it
1522 boolean isFailover = info.isFailover();
Robert Greenwalt2034b912009-08-12 16:08:25 -07001523 NetworkStateTracker thisNet = mNetTrackers[type];
The Android Open Source Project28527d22009-03-03 19:31:44 -08001524
Robert Greenwalt2034b912009-08-12 16:08:25 -07001525 // if this is a default net and other default is running
1526 // kill the one not preferred
Robert Greenwalt34848c02011-03-25 13:09:25 -07001527 if (mNetConfigs[type].isDefault()) {
Robert Greenwalt2034b912009-08-12 16:08:25 -07001528 if (mActiveDefaultNetwork != -1 && mActiveDefaultNetwork != type) {
1529 if ((type != mNetworkPreference &&
Wink Savillef2a62832011-04-07 14:23:45 -07001530 mNetConfigs[mActiveDefaultNetwork].priority >
1531 mNetConfigs[type].priority) ||
Robert Greenwalt2034b912009-08-12 16:08:25 -07001532 mNetworkPreference == mActiveDefaultNetwork) {
1533 // don't accept this one
Wink Savillee70c6f52010-12-03 12:01:38 -08001534 if (DBG) {
1535 log("Not broadcasting CONNECT_ACTION " +
Robert Greenwalt2034b912009-08-12 16:08:25 -07001536 "to torn down network " + info.getTypeName());
Wink Savillee70c6f52010-12-03 12:01:38 -08001537 }
Robert Greenwalt2034b912009-08-12 16:08:25 -07001538 teardown(thisNet);
1539 return;
1540 } else {
1541 // tear down the other
1542 NetworkStateTracker otherNet =
1543 mNetTrackers[mActiveDefaultNetwork];
Wink Savillee70c6f52010-12-03 12:01:38 -08001544 if (DBG) {
1545 log("Policy requires " + otherNet.getNetworkInfo().getTypeName() +
Robert Greenwalt2034b912009-08-12 16:08:25 -07001546 " teardown");
Wink Savillee70c6f52010-12-03 12:01:38 -08001547 }
Robert Greenwalt2034b912009-08-12 16:08:25 -07001548 if (!teardown(otherNet)) {
Wink Savillee70c6f52010-12-03 12:01:38 -08001549 loge("Network declined teardown request");
Robert Greenwalt99910172011-03-29 11:36:28 -07001550 teardown(thisNet);
Robert Greenwalt2034b912009-08-12 16:08:25 -07001551 return;
1552 }
Robert Greenwalt93dc1042010-06-15 12:19:37 -07001553 }
1554 }
1555 synchronized (ConnectivityService.this) {
1556 // have a new default network, release the transition wakelock in a second
1557 // if it's held. The second pause is to allow apps to reconnect over the
1558 // new network
1559 if (mNetTransitionWakeLock.isHeld()) {
1560 mHandler.sendMessageDelayed(mHandler.obtainMessage(
Robert Greenwaltccb36f92010-09-24 14:32:21 -07001561 EVENT_CLEAR_NET_TRANSITION_WAKELOCK,
Robert Greenwalt93dc1042010-06-15 12:19:37 -07001562 mNetTransitionWakeLockSerialNumber, 0),
1563 1000);
Robert Greenwalt2034b912009-08-12 16:08:25 -07001564 }
1565 }
1566 mActiveDefaultNetwork = type;
Robert Greenwalt986c7412010-09-08 15:24:47 -07001567 // this will cause us to come up initially as unconnected and switching
1568 // to connected after our normal pause unless somebody reports us as reall
1569 // disconnected
1570 mDefaultInetConditionPublished = 0;
1571 mDefaultConnectionSequence++;
1572 mInetConditionChangeInFlight = false;
1573 // Don't do this - if we never sign in stay, grey
1574 //reportNetworkCondition(mActiveDefaultNetwork, 100);
Robert Greenwalt2034b912009-08-12 16:08:25 -07001575 }
The Android Open Source Project28527d22009-03-03 19:31:44 -08001576 thisNet.setTeardownRequested(false);
Irfan Sheriff7f132d92010-06-09 15:39:36 -07001577 updateNetworkSettings(thisNet);
Robert Greenwalt36ea8692011-06-15 12:22:07 -07001578 handleConnectivityChange(type, false);
Robert Greenwalt2034b912009-08-12 16:08:25 -07001579 sendConnectedBroadcast(info);
The Android Open Source Project28527d22009-03-03 19:31:44 -08001580 }
1581
The Android Open Source Project28527d22009-03-03 19:31:44 -08001582 /**
Robert Greenwalt3afbead2010-07-23 15:46:26 -07001583 * After a change in the connectivity state of a network. We're mainly
1584 * concerned with making sure that the list of DNS servers is set up
1585 * according to which networks are connected, and ensuring that the
1586 * right routing table entries exist.
The Android Open Source Project28527d22009-03-03 19:31:44 -08001587 */
Robert Greenwalt36ea8692011-06-15 12:22:07 -07001588 private void handleConnectivityChange(int netType, boolean doReset) {
Wink Saville051a6642011-07-13 13:44:13 -07001589 int resetMask = doReset ? NetworkUtils.RESET_ALL_ADDRESSES : 0;
1590
The Android Open Source Project28527d22009-03-03 19:31:44 -08001591 /*
Robert Greenwalt2034b912009-08-12 16:08:25 -07001592 * If a non-default network is enabled, add the host routes that
Robert Greenwalt3afbead2010-07-23 15:46:26 -07001593 * will allow it's DNS servers to be accessed.
The Android Open Source Project28527d22009-03-03 19:31:44 -08001594 */
Robert Greenwalt3afbead2010-07-23 15:46:26 -07001595 handleDnsConfigurationChange(netType);
Robert Greenwalt2034b912009-08-12 16:08:25 -07001596
Robert Greenwalte8d2a4a2011-07-14 14:28:05 -07001597 LinkProperties curLp = mCurrentLinkProperties[netType];
1598 LinkProperties newLp = null;
1599
Robert Greenwalt3afbead2010-07-23 15:46:26 -07001600 if (mNetTrackers[netType].getNetworkInfo().isConnected()) {
Robert Greenwalte8d2a4a2011-07-14 14:28:05 -07001601 newLp = mNetTrackers[netType].getLinkProperties();
Wink Saville051a6642011-07-13 13:44:13 -07001602 if (VDBG) {
1603 log("handleConnectivityChange: changed linkProperty[" + netType + "]:" +
1604 " doReset=" + doReset + " resetMask=" + resetMask +
1605 "\n curLp=" + curLp +
1606 "\n newLp=" + newLp);
1607 }
1608
Robert Greenwalte8d2a4a2011-07-14 14:28:05 -07001609 if (curLp != null) {
1610 if (curLp.isIdenticalInterfaceName(newLp)) {
1611 CompareResult<LinkAddress> car = curLp.compareAddresses(newLp);
1612 if ((car.removed.size() != 0) || (car.added.size() != 0)) {
1613 for (LinkAddress linkAddr : car.removed) {
1614 if (linkAddr.getAddress() instanceof Inet4Address) {
1615 resetMask |= NetworkUtils.RESET_IPV4_ADDRESSES;
1616 }
1617 if (linkAddr.getAddress() instanceof Inet6Address) {
1618 resetMask |= NetworkUtils.RESET_IPV6_ADDRESSES;
1619 }
Wink Saville051a6642011-07-13 13:44:13 -07001620 }
Robert Greenwalte8d2a4a2011-07-14 14:28:05 -07001621 if (DBG) {
1622 log("handleConnectivityChange: addresses changed" +
1623 " linkProperty[" + netType + "]:" + " resetMask=" + resetMask +
1624 "\n car=" + car);
Wink Saville051a6642011-07-13 13:44:13 -07001625 }
Robert Greenwalte8d2a4a2011-07-14 14:28:05 -07001626 } else {
1627 if (DBG) {
1628 log("handleConnectivityChange: address are the same reset per doReset" +
1629 " linkProperty[" + netType + "]:" +
1630 " resetMask=" + resetMask);
1631 }
Wink Saville051a6642011-07-13 13:44:13 -07001632 }
1633 } else {
Robert Greenwalte8d2a4a2011-07-14 14:28:05 -07001634 resetMask = NetworkUtils.RESET_ALL_ADDRESSES;
1635 log("handleConnectivityChange: interface not not equivalent reset both" +
1636 " linkProperty[" + netType + "]:" +
1637 " resetMask=" + resetMask);
Wink Saville051a6642011-07-13 13:44:13 -07001638 }
Wink Saville051a6642011-07-13 13:44:13 -07001639 }
Robert Greenwalt34848c02011-03-25 13:09:25 -07001640 if (mNetConfigs[netType].isDefault()) {
Robert Greenwaltc3c5f862010-10-11 16:00:27 -07001641 handleApplyDefaultProxy(netType);
Robert Greenwalt3afbead2010-07-23 15:46:26 -07001642 }
1643 } else {
Robert Greenwalte8d2a4a2011-07-14 14:28:05 -07001644 if (VDBG) {
1645 log("handleConnectivityChange: changed linkProperty[" + netType + "]:" +
1646 " doReset=" + doReset + " resetMask=" + resetMask +
1647 "\n curLp=" + curLp +
1648 "\n newLp= null");
Robert Greenwalt2034b912009-08-12 16:08:25 -07001649 }
The Android Open Source Project28527d22009-03-03 19:31:44 -08001650 }
Robert Greenwalte8d2a4a2011-07-14 14:28:05 -07001651 mCurrentLinkProperties[netType] = newLp;
1652 updateRoutes(newLp, curLp, mNetConfigs[netType].isDefault());
Robert Greenwalt36ea8692011-06-15 12:22:07 -07001653
Wink Saville051a6642011-07-13 13:44:13 -07001654 if (doReset || resetMask != 0) {
Robert Greenwalt36ea8692011-06-15 12:22:07 -07001655 LinkProperties linkProperties = mNetTrackers[netType].getLinkProperties();
1656 if (linkProperties != null) {
1657 String iface = linkProperties.getInterfaceName();
1658 if (TextUtils.isEmpty(iface) == false) {
Wink Saville051a6642011-07-13 13:44:13 -07001659 if (DBG) log("resetConnections(" + iface + ", " + resetMask + ")");
1660 NetworkUtils.resetConnections(iface, resetMask);
Robert Greenwalt36ea8692011-06-15 12:22:07 -07001661 }
1662 }
1663 }
Kazuhiro Ondo07680062011-06-22 21:10:34 -05001664
1665 // TODO: Temporary notifying upstread change to Tethering.
1666 // @see bug/4455071
1667 /** Notify TetheringService if interface name has been changed. */
1668 if (TextUtils.equals(mNetTrackers[netType].getNetworkInfo().getReason(),
1669 Phone.REASON_LINK_PROPERTIES_CHANGED)) {
1670 if (isTetheringSupported()) {
1671 mTethering.handleTetherIfaceChange();
1672 }
1673 }
The Android Open Source Project28527d22009-03-03 19:31:44 -08001674 }
1675
Robert Greenwalte8d2a4a2011-07-14 14:28:05 -07001676 /**
1677 * Add and remove routes using the old properties (null if not previously connected),
1678 * new properties (null if becoming disconnected). May even be double null, which
1679 * is a noop.
1680 * Uses isLinkDefault to determine if default routes should be set or conversely if
1681 * host routes should be set to the dns servers
1682 */
1683 private void updateRoutes(LinkProperties newLp, LinkProperties curLp, boolean isLinkDefault) {
1684 Collection<RouteInfo> routesToAdd = null;
1685 CompareResult<InetAddress> dnsDiff = null;
Irfan Sheriff7f132d92010-06-09 15:39:36 -07001686
Robert Greenwalte8d2a4a2011-07-14 14:28:05 -07001687 if (curLp != null) {
1688 // check for the delta between the current set and the new
1689 CompareResult<RouteInfo> routeDiff = curLp.compareRoutes(newLp);
1690 dnsDiff = curLp.compareDnses(newLp);
Irfan Sheriff7f132d92010-06-09 15:39:36 -07001691
Robert Greenwalte8d2a4a2011-07-14 14:28:05 -07001692 for (RouteInfo r : routeDiff.removed) {
1693 if (isLinkDefault || ! r.isDefaultRoute()) {
1694 removeRoute(curLp, r);
Robert Greenwalt355205c2011-05-10 15:05:02 -07001695 }
1696 }
Robert Greenwalte8d2a4a2011-07-14 14:28:05 -07001697 routesToAdd = routeDiff.added;
Irfan Sheriff7f132d92010-06-09 15:39:36 -07001698 }
Irfan Sheriff7f132d92010-06-09 15:39:36 -07001699
Robert Greenwalte8d2a4a2011-07-14 14:28:05 -07001700 if (newLp != null) {
1701 // if we didn't get a diff from cur -> new, then just use the new
1702 if (routesToAdd == null) {
1703 routesToAdd = newLp.getRoutes();
1704 }
Irfan Sheriff7f132d92010-06-09 15:39:36 -07001705
Robert Greenwalte8d2a4a2011-07-14 14:28:05 -07001706 for (RouteInfo r : routesToAdd) {
1707 if (isLinkDefault || ! r.isDefaultRoute()) {
1708 addRoute(newLp, r);
1709 }
1710 }
1711 }
Irfan Sheriff7f132d92010-06-09 15:39:36 -07001712
Robert Greenwalte8d2a4a2011-07-14 14:28:05 -07001713 if (!isLinkDefault) {
1714 // handle DNS routes
1715 Collection<InetAddress> dnsToAdd = null;
1716 if (dnsDiff != null) {
1717 dnsToAdd = dnsDiff.added;
1718 for (InetAddress dnsAddress : dnsDiff.removed) {
1719 removeRoute(curLp, RouteInfo.makeHostRoute(dnsAddress));
1720 }
1721 }
1722 if (newLp != null) {
1723 if (dnsToAdd == null) {
1724 dnsToAdd = newLp.getDnses();
1725 }
1726 for(InetAddress dnsAddress : dnsToAdd) {
1727 addRoute(newLp, RouteInfo.makeHostRoute(dnsAddress));
Robert Greenwalt03d53da2011-03-22 18:47:42 -07001728 }
Irfan Sheriff7f132d92010-06-09 15:39:36 -07001729 }
Irfan Sheriff7f132d92010-06-09 15:39:36 -07001730 }
1731 }
1732
1733
Irfan Sheriff7f132d92010-06-09 15:39:36 -07001734 /**
1735 * Reads the network specific TCP buffer sizes from SystemProperties
1736 * net.tcp.buffersize.[default|wifi|umts|edge|gprs] and set them for system
1737 * wide use
1738 */
1739 public void updateNetworkSettings(NetworkStateTracker nt) {
1740 String key = nt.getTcpBufferSizesPropName();
1741 String bufferSizes = SystemProperties.get(key);
1742
1743 if (bufferSizes.length() == 0) {
Wink Savillee70c6f52010-12-03 12:01:38 -08001744 loge(key + " not found in system properties. Using defaults");
Irfan Sheriff7f132d92010-06-09 15:39:36 -07001745
1746 // Setting to default values so we won't be stuck to previous values
1747 key = "net.tcp.buffersize.default";
1748 bufferSizes = SystemProperties.get(key);
1749 }
1750
1751 // Set values in kernel
1752 if (bufferSizes.length() != 0) {
1753 if (DBG) {
Wink Savillee70c6f52010-12-03 12:01:38 -08001754 log("Setting TCP values: [" + bufferSizes
Irfan Sheriff7f132d92010-06-09 15:39:36 -07001755 + "] which comes from [" + key + "]");
1756 }
1757 setBufferSize(bufferSizes);
1758 }
1759 }
1760
1761 /**
1762 * Writes TCP buffer sizes to /sys/kernel/ipv4/tcp_[r/w]mem_[min/def/max]
1763 * which maps to /proc/sys/net/ipv4/tcp_rmem and tcpwmem
1764 *
1765 * @param bufferSizes in the format of "readMin, readInitial, readMax,
1766 * writeMin, writeInitial, writeMax"
1767 */
1768 private void setBufferSize(String bufferSizes) {
1769 try {
1770 String[] values = bufferSizes.split(",");
1771
1772 if (values.length == 6) {
1773 final String prefix = "/sys/kernel/ipv4/tcp_";
Mike Lockwood0d5916c2011-05-28 13:24:04 -04001774 FileUtils.stringToFile(prefix + "rmem_min", values[0]);
1775 FileUtils.stringToFile(prefix + "rmem_def", values[1]);
1776 FileUtils.stringToFile(prefix + "rmem_max", values[2]);
1777 FileUtils.stringToFile(prefix + "wmem_min", values[3]);
1778 FileUtils.stringToFile(prefix + "wmem_def", values[4]);
1779 FileUtils.stringToFile(prefix + "wmem_max", values[5]);
Irfan Sheriff7f132d92010-06-09 15:39:36 -07001780 } else {
Wink Savillee70c6f52010-12-03 12:01:38 -08001781 loge("Invalid buffersize string: " + bufferSizes);
Irfan Sheriff7f132d92010-06-09 15:39:36 -07001782 }
1783 } catch (IOException e) {
Wink Savillee70c6f52010-12-03 12:01:38 -08001784 loge("Can't set tcp buffer sizes:" + e);
Irfan Sheriff7f132d92010-06-09 15:39:36 -07001785 }
1786 }
1787
Robert Greenwalt2034b912009-08-12 16:08:25 -07001788 /**
1789 * Adjust the per-process dns entries (net.dns<x>.<pid>) based
1790 * on the highest priority active net which this process requested.
1791 * If there aren't any, clear it out
1792 */
1793 private void reassessPidDns(int myPid, boolean doBump)
1794 {
Wink Savillee70c6f52010-12-03 12:01:38 -08001795 if (DBG) log("reassessPidDns for pid " + myPid);
Robert Greenwalt2034b912009-08-12 16:08:25 -07001796 for(int i : mPriorityList) {
Robert Greenwalt34848c02011-03-25 13:09:25 -07001797 if (mNetConfigs[i].isDefault()) {
Robert Greenwalt2034b912009-08-12 16:08:25 -07001798 continue;
1799 }
1800 NetworkStateTracker nt = mNetTrackers[i];
Robert Greenwalt0659da32009-07-16 17:21:39 -07001801 if (nt.getNetworkInfo().isConnected() &&
1802 !nt.isTeardownRequested()) {
Robert Greenwalt1f1bcfe2010-08-30 10:56:47 -07001803 LinkProperties p = nt.getLinkProperties();
Robert Greenwalta7dfbd32010-06-15 15:43:39 -07001804 if (p == null) continue;
Robert Greenwalt2034b912009-08-12 16:08:25 -07001805 List pids = mNetRequestersPids[i];
1806 for (int j=0; j<pids.size(); j++) {
1807 Integer pid = (Integer)pids.get(j);
1808 if (pid.intValue() == myPid) {
Robert Greenwalta7dfbd32010-06-15 15:43:39 -07001809 Collection<InetAddress> dnses = p.getDnses();
1810 writePidDns(dnses, myPid);
Robert Greenwalt2034b912009-08-12 16:08:25 -07001811 if (doBump) {
1812 bumpDns();
1813 }
1814 return;
1815 }
1816 }
1817 }
1818 }
1819 // nothing found - delete
1820 for (int i = 1; ; i++) {
1821 String prop = "net.dns" + i + "." + myPid;
1822 if (SystemProperties.get(prop).length() == 0) {
1823 if (doBump) {
1824 bumpDns();
1825 }
1826 return;
1827 }
1828 SystemProperties.set(prop, "");
1829 }
1830 }
1831
Robert Greenwalt8ca88762010-12-17 15:20:36 -08001832 // return true if results in a change
1833 private boolean writePidDns(Collection <InetAddress> dnses, int pid) {
Robert Greenwalt2034b912009-08-12 16:08:25 -07001834 int j = 1;
Robert Greenwalt8ca88762010-12-17 15:20:36 -08001835 boolean changed = false;
Robert Greenwalta7dfbd32010-06-15 15:43:39 -07001836 for (InetAddress dns : dnses) {
Robert Greenwalt8ca88762010-12-17 15:20:36 -08001837 String dnsString = dns.getHostAddress();
1838 if (changed || !dnsString.equals(SystemProperties.get("net.dns" + j + "." + pid))) {
1839 changed = true;
1840 SystemProperties.set("net.dns" + j++ + "." + pid, dns.getHostAddress());
1841 }
Robert Greenwalt2034b912009-08-12 16:08:25 -07001842 }
Robert Greenwalt8ca88762010-12-17 15:20:36 -08001843 return changed;
Robert Greenwalt2034b912009-08-12 16:08:25 -07001844 }
1845
1846 private void bumpDns() {
1847 /*
1848 * Bump the property that tells the name resolver library to reread
1849 * the DNS server list from the properties.
1850 */
1851 String propVal = SystemProperties.get("net.dnschange");
1852 int n = 0;
1853 if (propVal.length() != 0) {
1854 try {
1855 n = Integer.parseInt(propVal);
1856 } catch (NumberFormatException e) {}
1857 }
1858 SystemProperties.set("net.dnschange", "" + (n+1));
Robert Greenwalt051642b2010-11-02 14:08:23 -07001859 /*
1860 * Tell the VMs to toss their DNS caches
1861 */
1862 Intent intent = new Intent(Intent.ACTION_CLEAR_DNS_CACHE);
1863 intent.addFlags(Intent.FLAG_RECEIVER_REPLACE_PENDING);
Stan Chesnuttf444f502011-01-05 17:14:03 -08001864 /*
1865 * Connectivity events can happen before boot has completed ...
1866 */
1867 intent.addFlags(Intent.FLAG_RECEIVER_REGISTERED_ONLY_BEFORE_BOOT);
Robert Greenwalt051642b2010-11-02 14:08:23 -07001868 mContext.sendBroadcast(intent);
Robert Greenwalt2034b912009-08-12 16:08:25 -07001869 }
1870
Robert Greenwalt3afbead2010-07-23 15:46:26 -07001871 private void handleDnsConfigurationChange(int netType) {
Robert Greenwalt2034b912009-08-12 16:08:25 -07001872 // add default net's dns entries
Robert Greenwalt3afbead2010-07-23 15:46:26 -07001873 NetworkStateTracker nt = mNetTrackers[netType];
1874 if (nt != null && nt.getNetworkInfo().isConnected() && !nt.isTeardownRequested()) {
Robert Greenwalt1f1bcfe2010-08-30 10:56:47 -07001875 LinkProperties p = nt.getLinkProperties();
Robert Greenwalt3afbead2010-07-23 15:46:26 -07001876 if (p == null) return;
1877 Collection<InetAddress> dnses = p.getDnses();
Robert Greenwalt8ca88762010-12-17 15:20:36 -08001878 boolean changed = false;
Robert Greenwalt34848c02011-03-25 13:09:25 -07001879 if (mNetConfigs[netType].isDefault()) {
Robert Greenwalt3afbead2010-07-23 15:46:26 -07001880 int j = 1;
Robert Greenwalt94daa182010-09-01 11:34:05 -07001881 if (dnses.size() == 0 && mDefaultDns != null) {
Robert Greenwalt8ca88762010-12-17 15:20:36 -08001882 String dnsString = mDefaultDns.getHostAddress();
1883 if (!dnsString.equals(SystemProperties.get("net.dns1"))) {
1884 if (DBG) {
1885 log("no dns provided - using " + dnsString);
1886 }
1887 changed = true;
1888 SystemProperties.set("net.dns1", dnsString);
Robert Greenwalt2034b912009-08-12 16:08:25 -07001889 }
Robert Greenwalt94daa182010-09-01 11:34:05 -07001890 j++;
1891 } else {
1892 for (InetAddress dns : dnses) {
Robert Greenwalt8ca88762010-12-17 15:20:36 -08001893 String dnsString = dns.getHostAddress();
1894 if (!changed && dnsString.equals(SystemProperties.get("net.dns" + j))) {
1895 j++;
1896 continue;
1897 }
Robert Greenwalt94daa182010-09-01 11:34:05 -07001898 if (DBG) {
Wink Savillee70c6f52010-12-03 12:01:38 -08001899 log("adding dns " + dns + " for " +
Robert Greenwalt94daa182010-09-01 11:34:05 -07001900 nt.getNetworkInfo().getTypeName());
1901 }
Robert Greenwalt8ca88762010-12-17 15:20:36 -08001902 changed = true;
1903 SystemProperties.set("net.dns" + j++, dnsString);
Robert Greenwalt94daa182010-09-01 11:34:05 -07001904 }
Robert Greenwalt3afbead2010-07-23 15:46:26 -07001905 }
1906 for (int k=j ; k<mNumDnsEntries; k++) {
Robert Greenwalt8ca88762010-12-17 15:20:36 -08001907 if (changed || !TextUtils.isEmpty(SystemProperties.get("net.dns" + k))) {
1908 if (DBG) log("erasing net.dns" + k);
1909 changed = true;
1910 SystemProperties.set("net.dns" + k, "");
1911 }
Robert Greenwalt3afbead2010-07-23 15:46:26 -07001912 }
1913 mNumDnsEntries = j;
1914 } else {
1915 // set per-pid dns for attached secondary nets
1916 List pids = mNetRequestersPids[netType];
1917 for (int y=0; y< pids.size(); y++) {
1918 Integer pid = (Integer)pids.get(y);
Robert Greenwalt8ca88762010-12-17 15:20:36 -08001919 changed = writePidDns(dnses, pid.intValue());
The Android Open Source Project28527d22009-03-03 19:31:44 -08001920 }
1921 }
Robert Greenwalt8ca88762010-12-17 15:20:36 -08001922 if (changed) bumpDns();
The Android Open Source Project28527d22009-03-03 19:31:44 -08001923 }
Robert Greenwalt2034b912009-08-12 16:08:25 -07001924 }
1925
Robert Greenwalt20f819c2011-05-03 19:02:44 -07001926 private int getRestoreDefaultNetworkDelay(int networkType) {
Robert Greenwalt2034b912009-08-12 16:08:25 -07001927 String restoreDefaultNetworkDelayStr = SystemProperties.get(
1928 NETWORK_RESTORE_DELAY_PROP_NAME);
1929 if(restoreDefaultNetworkDelayStr != null &&
1930 restoreDefaultNetworkDelayStr.length() != 0) {
1931 try {
1932 return Integer.valueOf(restoreDefaultNetworkDelayStr);
1933 } catch (NumberFormatException e) {
1934 }
The Android Open Source Project28527d22009-03-03 19:31:44 -08001935 }
Robert Greenwalt20f819c2011-05-03 19:02:44 -07001936 // if the system property isn't set, use the value for the apn type
1937 int ret = RESTORE_DEFAULT_NETWORK_DELAY;
1938
1939 if ((networkType <= ConnectivityManager.MAX_NETWORK_TYPE) &&
1940 (mNetConfigs[networkType] != null)) {
1941 ret = mNetConfigs[networkType].restoreTime;
1942 }
1943 return ret;
The Android Open Source Project28527d22009-03-03 19:31:44 -08001944 }
1945
1946 @Override
1947 protected void dump(FileDescriptor fd, PrintWriter pw, String[] args) {
Robert Greenwalt0659da32009-07-16 17:21:39 -07001948 if (mContext.checkCallingOrSelfPermission(
1949 android.Manifest.permission.DUMP)
The Android Open Source Project28527d22009-03-03 19:31:44 -08001950 != PackageManager.PERMISSION_GRANTED) {
Robert Greenwalt0659da32009-07-16 17:21:39 -07001951 pw.println("Permission Denial: can't dump ConnectivityService " +
1952 "from from pid=" + Binder.getCallingPid() + ", uid=" +
1953 Binder.getCallingUid());
The Android Open Source Project28527d22009-03-03 19:31:44 -08001954 return;
1955 }
The Android Open Source Project28527d22009-03-03 19:31:44 -08001956 pw.println();
1957 for (NetworkStateTracker nst : mNetTrackers) {
Robert Greenwalt3eeb6032009-12-21 18:24:07 -08001958 if (nst != null) {
1959 if (nst.getNetworkInfo().isConnected()) {
1960 pw.println("Active network: " + nst.getNetworkInfo().
1961 getTypeName());
1962 }
1963 pw.println(nst.getNetworkInfo());
1964 pw.println(nst);
1965 pw.println();
Robert Greenwalt2034b912009-08-12 16:08:25 -07001966 }
The Android Open Source Project28527d22009-03-03 19:31:44 -08001967 }
Robert Greenwalt3eeb6032009-12-21 18:24:07 -08001968
1969 pw.println("Network Requester Pids:");
1970 for (int net : mPriorityList) {
1971 String pidString = net + ": ";
1972 for (Object pid : mNetRequestersPids[net]) {
1973 pidString = pidString + pid.toString() + ", ";
1974 }
1975 pw.println(pidString);
1976 }
1977 pw.println();
1978
1979 pw.println("FeatureUsers:");
1980 for (Object requester : mFeatureUsers) {
1981 pw.println(requester.toString());
1982 }
1983 pw.println();
Robert Greenwalt8e87f122010-02-11 18:18:40 -08001984
Robert Greenwalt93dc1042010-06-15 12:19:37 -07001985 synchronized (this) {
1986 pw.println("NetworkTranstionWakeLock is currently " +
1987 (mNetTransitionWakeLock.isHeld() ? "" : "not ") + "held.");
1988 pw.println("It was last requested for "+mNetTransitionWakeLockCausedBy);
1989 }
1990 pw.println();
1991
Robert Greenwalt8e87f122010-02-11 18:18:40 -08001992 mTethering.dump(fd, pw, args);
Robert Greenwalt0e80be12010-09-20 14:35:25 -07001993
1994 if (mInetLog != null) {
1995 pw.println();
1996 pw.println("Inet condition reports:");
1997 for(int i = 0; i < mInetLog.size(); i++) {
1998 pw.println(mInetLog.get(i));
1999 }
2000 }
The Android Open Source Project28527d22009-03-03 19:31:44 -08002001 }
2002
Robert Greenwalt2034b912009-08-12 16:08:25 -07002003 // must be stateless - things change under us.
The Android Open Source Project28527d22009-03-03 19:31:44 -08002004 private class MyHandler extends Handler {
Wink Saville775aad62010-09-02 19:23:52 -07002005 public MyHandler(Looper looper) {
2006 super(looper);
2007 }
2008
The Android Open Source Project28527d22009-03-03 19:31:44 -08002009 @Override
2010 public void handleMessage(Message msg) {
2011 NetworkInfo info;
2012 switch (msg.what) {
2013 case NetworkStateTracker.EVENT_STATE_CHANGED:
2014 info = (NetworkInfo) msg.obj;
Robert Greenwalt12c44552009-12-07 11:33:18 -08002015 int type = info.getType();
2016 NetworkInfo.State state = info.getState();
Robert Greenwalt12c44552009-12-07 11:33:18 -08002017
Wink Savillee70c6f52010-12-03 12:01:38 -08002018 if (DBG) log("ConnectivityChange for " +
Robert Greenwalt0659da32009-07-16 17:21:39 -07002019 info.getTypeName() + ": " +
Robert Greenwalt12c44552009-12-07 11:33:18 -08002020 state + "/" + info.getDetailedState());
The Android Open Source Project28527d22009-03-03 19:31:44 -08002021
2022 // Connectivity state changed:
2023 // [31-13] Reserved for future use
Robert Greenwalt0659da32009-07-16 17:21:39 -07002024 // [12-9] Network subtype (for mobile network, as defined
2025 // by TelephonyManager)
2026 // [8-3] Detailed state ordinal (as defined by
2027 // NetworkInfo.DetailedState)
The Android Open Source Project28527d22009-03-03 19:31:44 -08002028 // [2-0] Network type (as defined by ConnectivityManager)
2029 int eventLogParam = (info.getType() & 0x7) |
2030 ((info.getDetailedState().ordinal() & 0x3f) << 3) |
2031 (info.getSubtype() << 9);
Doug Zongker2fc96232009-12-04 10:31:43 -08002032 EventLog.writeEvent(EventLogTags.CONNECTIVITY_STATE_CHANGED,
Robert Greenwalt0659da32009-07-16 17:21:39 -07002033 eventLogParam);
2034
2035 if (info.getDetailedState() ==
2036 NetworkInfo.DetailedState.FAILED) {
The Android Open Source Project28527d22009-03-03 19:31:44 -08002037 handleConnectionFailure(info);
Robert Greenwalt12c44552009-12-07 11:33:18 -08002038 } else if (state == NetworkInfo.State.DISCONNECTED) {
The Android Open Source Project28527d22009-03-03 19:31:44 -08002039 handleDisconnect(info);
Robert Greenwalt12c44552009-12-07 11:33:18 -08002040 } else if (state == NetworkInfo.State.SUSPENDED) {
The Android Open Source Project28527d22009-03-03 19:31:44 -08002041 // TODO: need to think this over.
Robert Greenwalt0659da32009-07-16 17:21:39 -07002042 // the logic here is, handle SUSPENDED the same as
2043 // DISCONNECTED. The only difference being we are
2044 // broadcasting an intent with NetworkInfo that's
2045 // suspended. This allows the applications an
2046 // opportunity to handle DISCONNECTED and SUSPENDED
2047 // differently, or not.
The Android Open Source Project28527d22009-03-03 19:31:44 -08002048 handleDisconnect(info);
Robert Greenwalt12c44552009-12-07 11:33:18 -08002049 } else if (state == NetworkInfo.State.CONNECTED) {
The Android Open Source Project28527d22009-03-03 19:31:44 -08002050 handleConnect(info);
2051 }
The Android Open Source Project28527d22009-03-03 19:31:44 -08002052 break;
The Android Open Source Project28527d22009-03-03 19:31:44 -08002053 case NetworkStateTracker.EVENT_CONFIGURATION_CHANGED:
Robert Greenwalt3afbead2010-07-23 15:46:26 -07002054 info = (NetworkInfo) msg.obj;
Kazuhiro Ondo07680062011-06-22 21:10:34 -05002055 // TODO: Temporary allowing network configuration
2056 // change not resetting sockets.
2057 // @see bug/4455071
2058 handleConnectivityChange(info.getType(), false);
The Android Open Source Project28527d22009-03-03 19:31:44 -08002059 break;
Robert Greenwaltccb36f92010-09-24 14:32:21 -07002060 case EVENT_CLEAR_NET_TRANSITION_WAKELOCK:
Robert Greenwalt93dc1042010-06-15 12:19:37 -07002061 String causedBy = null;
2062 synchronized (ConnectivityService.this) {
2063 if (msg.arg1 == mNetTransitionWakeLockSerialNumber &&
2064 mNetTransitionWakeLock.isHeld()) {
2065 mNetTransitionWakeLock.release();
2066 causedBy = mNetTransitionWakeLockCausedBy;
2067 }
2068 }
2069 if (causedBy != null) {
Wink Savillee70c6f52010-12-03 12:01:38 -08002070 log("NetTransition Wakelock for " + causedBy + " released by timeout");
Robert Greenwalt93dc1042010-06-15 12:19:37 -07002071 }
Robert Greenwaltcf1a56c2010-09-09 14:05:10 -07002072 break;
Robert Greenwalt6a2db8a2010-09-23 10:05:56 -07002073 case EVENT_RESTORE_DEFAULT_NETWORK:
Robert Greenwaltaffc3a12009-09-27 17:27:04 -07002074 FeatureUser u = (FeatureUser)msg.obj;
2075 u.expire();
Robert Greenwalt986c7412010-09-08 15:24:47 -07002076 break;
Robert Greenwalt6a2db8a2010-09-23 10:05:56 -07002077 case EVENT_INET_CONDITION_CHANGE:
2078 {
2079 int netType = msg.arg1;
2080 int condition = msg.arg2;
2081 handleInetConditionChange(netType, condition);
Robert Greenwalt93dc1042010-06-15 12:19:37 -07002082 break;
Robert Greenwalt6a2db8a2010-09-23 10:05:56 -07002083 }
2084 case EVENT_INET_CONDITION_HOLD_END:
2085 {
2086 int netType = msg.arg1;
2087 int sequence = msg.arg2;
2088 handleInetConditionHoldEnd(netType, sequence);
Robert Greenwalt986c7412010-09-08 15:24:47 -07002089 break;
Robert Greenwalt6a2db8a2010-09-23 10:05:56 -07002090 }
2091 case EVENT_SET_NETWORK_PREFERENCE:
2092 {
2093 int preference = msg.arg1;
2094 handleSetNetworkPreference(preference);
2095 break;
2096 }
2097 case EVENT_SET_BACKGROUND_DATA:
2098 {
2099 boolean enabled = (msg.arg1 == ENABLED);
2100 handleSetBackgroundData(enabled);
2101 break;
2102 }
2103 case EVENT_SET_MOBILE_DATA:
2104 {
2105 boolean enabled = (msg.arg1 == ENABLED);
2106 handleSetMobileData(enabled);
2107 break;
2108 }
Robert Greenwaltc3c5f862010-10-11 16:00:27 -07002109 case EVENT_APPLY_GLOBAL_HTTP_PROXY:
2110 {
2111 handleDeprecatedGlobalHttpProxy();
Robert Greenwalt34848c02011-03-25 13:09:25 -07002112 break;
2113 }
2114 case EVENT_SET_DEPENDENCY_MET:
2115 {
2116 boolean met = (msg.arg1 == ENABLED);
2117 handleSetDependencyMet(msg.arg2, met);
2118 break;
Robert Greenwaltc3c5f862010-10-11 16:00:27 -07002119 }
The Android Open Source Project28527d22009-03-03 19:31:44 -08002120 }
2121 }
2122 }
Robert Greenwalt0c4828c2010-01-26 11:40:34 -08002123
2124 // javadoc from interface
Robert Greenwalt4283ded2010-03-02 17:25:02 -08002125 public int tether(String iface) {
Robert Greenwalt0c4828c2010-01-26 11:40:34 -08002126 enforceTetherChangePermission();
Robert Greenwalt4283ded2010-03-02 17:25:02 -08002127
2128 if (isTetheringSupported()) {
2129 return mTethering.tether(iface);
2130 } else {
2131 return ConnectivityManager.TETHER_ERROR_UNSUPPORTED;
2132 }
Robert Greenwalt0c4828c2010-01-26 11:40:34 -08002133 }
2134
2135 // javadoc from interface
Robert Greenwalt4283ded2010-03-02 17:25:02 -08002136 public int untether(String iface) {
Robert Greenwalt0c4828c2010-01-26 11:40:34 -08002137 enforceTetherChangePermission();
Robert Greenwalt4283ded2010-03-02 17:25:02 -08002138
2139 if (isTetheringSupported()) {
2140 return mTethering.untether(iface);
2141 } else {
2142 return ConnectivityManager.TETHER_ERROR_UNSUPPORTED;
2143 }
2144 }
2145
2146 // javadoc from interface
2147 public int getLastTetherError(String iface) {
2148 enforceTetherAccessPermission();
2149
2150 if (isTetheringSupported()) {
2151 return mTethering.getLastTetherError(iface);
2152 } else {
2153 return ConnectivityManager.TETHER_ERROR_UNSUPPORTED;
2154 }
Robert Greenwalt8e87f122010-02-11 18:18:40 -08002155 }
2156
2157 // TODO - proper iface API for selection by property, inspection, etc
2158 public String[] getTetherableUsbRegexs() {
2159 enforceTetherAccessPermission();
2160 if (isTetheringSupported()) {
2161 return mTethering.getTetherableUsbRegexs();
2162 } else {
2163 return new String[0];
2164 }
2165 }
2166
2167 public String[] getTetherableWifiRegexs() {
2168 enforceTetherAccessPermission();
2169 if (isTetheringSupported()) {
2170 return mTethering.getTetherableWifiRegexs();
2171 } else {
2172 return new String[0];
2173 }
Robert Greenwalt0c4828c2010-01-26 11:40:34 -08002174 }
2175
Danica Chang96567052010-08-11 14:54:43 -07002176 public String[] getTetherableBluetoothRegexs() {
2177 enforceTetherAccessPermission();
2178 if (isTetheringSupported()) {
2179 return mTethering.getTetherableBluetoothRegexs();
2180 } else {
2181 return new String[0];
2182 }
2183 }
2184
Robert Greenwalt0c4828c2010-01-26 11:40:34 -08002185 // TODO - move iface listing, queries, etc to new module
2186 // javadoc from interface
2187 public String[] getTetherableIfaces() {
Robert Greenwalt8e87f122010-02-11 18:18:40 -08002188 enforceTetherAccessPermission();
Robert Greenwalt0c4828c2010-01-26 11:40:34 -08002189 return mTethering.getTetherableIfaces();
2190 }
2191
2192 public String[] getTetheredIfaces() {
Robert Greenwalt8e87f122010-02-11 18:18:40 -08002193 enforceTetherAccessPermission();
Robert Greenwalt0c4828c2010-01-26 11:40:34 -08002194 return mTethering.getTetheredIfaces();
2195 }
Robert Greenwalt8e87f122010-02-11 18:18:40 -08002196
Robert Greenwalt4283ded2010-03-02 17:25:02 -08002197 public String[] getTetheringErroredIfaces() {
2198 enforceTetherAccessPermission();
2199 return mTethering.getErroredIfaces();
2200 }
2201
Robert Greenwalt8e87f122010-02-11 18:18:40 -08002202 // if ro.tether.denied = true we default to no tethering
2203 // gservices could set the secure setting to 1 though to enable it on a build where it
2204 // had previously been turned off.
2205 public boolean isTetheringSupported() {
2206 enforceTetherAccessPermission();
2207 int defaultVal = (SystemProperties.get("ro.tether.denied").equals("true") ? 0 : 1);
Robert Greenwaltf1b66e12010-02-25 12:29:30 -08002208 boolean tetherEnabledInSettings = (Settings.Secure.getInt(mContext.getContentResolver(),
2209 Settings.Secure.TETHER_SUPPORTED, defaultVal) != 0);
2210 return tetherEnabledInSettings && mTetheringConfigValid;
Robert Greenwalt8e87f122010-02-11 18:18:40 -08002211 }
Robert Greenwalt93dc1042010-06-15 12:19:37 -07002212
2213 // An API NetworkStateTrackers can call when they lose their network.
2214 // This will automatically be cleared after X seconds or a network becomes CONNECTED,
2215 // whichever happens first. The timer is started by the first caller and not
2216 // restarted by subsequent callers.
2217 public void requestNetworkTransitionWakelock(String forWhom) {
2218 enforceConnectivityInternalPermission();
2219 synchronized (this) {
2220 if (mNetTransitionWakeLock.isHeld()) return;
2221 mNetTransitionWakeLockSerialNumber++;
2222 mNetTransitionWakeLock.acquire();
2223 mNetTransitionWakeLockCausedBy = forWhom;
2224 }
2225 mHandler.sendMessageDelayed(mHandler.obtainMessage(
Robert Greenwaltccb36f92010-09-24 14:32:21 -07002226 EVENT_CLEAR_NET_TRANSITION_WAKELOCK,
Robert Greenwalt93dc1042010-06-15 12:19:37 -07002227 mNetTransitionWakeLockSerialNumber, 0),
2228 mNetTransitionWakeLockTimeout);
2229 return;
2230 }
Robert Greenwalt24118e82010-09-09 13:15:32 -07002231
Robert Greenwalt986c7412010-09-08 15:24:47 -07002232 // 100 percent is full good, 0 is full bad.
2233 public void reportInetCondition(int networkType, int percentage) {
Wink Savillee70c6f52010-12-03 12:01:38 -08002234 if (DBG) log("reportNetworkCondition(" + networkType + ", " + percentage + ")");
Robert Greenwalt986c7412010-09-08 15:24:47 -07002235 mContext.enforceCallingOrSelfPermission(
2236 android.Manifest.permission.STATUS_BAR,
2237 "ConnectivityService");
2238
Robert Greenwalt0e80be12010-09-20 14:35:25 -07002239 if (DBG) {
2240 int pid = getCallingPid();
2241 int uid = getCallingUid();
2242 String s = pid + "(" + uid + ") reports inet is " +
2243 (percentage > 50 ? "connected" : "disconnected") + " (" + percentage + ") on " +
2244 "network Type " + networkType + " at " + GregorianCalendar.getInstance().getTime();
2245 mInetLog.add(s);
2246 while(mInetLog.size() > INET_CONDITION_LOG_MAX_SIZE) {
2247 mInetLog.remove(0);
2248 }
2249 }
Robert Greenwalt986c7412010-09-08 15:24:47 -07002250 mHandler.sendMessage(mHandler.obtainMessage(
Robert Greenwalt6a2db8a2010-09-23 10:05:56 -07002251 EVENT_INET_CONDITION_CHANGE, networkType, percentage));
2252 }
2253
2254 private void handleInetConditionChange(int netType, int condition) {
2255 if (DBG) {
Wink Savillee70c6f52010-12-03 12:01:38 -08002256 log("Inet connectivity change, net=" +
Robert Greenwalt6a2db8a2010-09-23 10:05:56 -07002257 netType + ", condition=" + condition +
2258 ",mActiveDefaultNetwork=" + mActiveDefaultNetwork);
2259 }
2260 if (mActiveDefaultNetwork == -1) {
Wink Savillee70c6f52010-12-03 12:01:38 -08002261 if (DBG) log("no active default network - aborting");
Robert Greenwalt6a2db8a2010-09-23 10:05:56 -07002262 return;
2263 }
2264 if (mActiveDefaultNetwork != netType) {
Wink Savillee70c6f52010-12-03 12:01:38 -08002265 if (DBG) log("given net not default - aborting");
Robert Greenwalt6a2db8a2010-09-23 10:05:56 -07002266 return;
2267 }
2268 mDefaultInetCondition = condition;
2269 int delay;
2270 if (mInetConditionChangeInFlight == false) {
Wink Savillee70c6f52010-12-03 12:01:38 -08002271 if (DBG) log("starting a change hold");
Robert Greenwalt6a2db8a2010-09-23 10:05:56 -07002272 // setup a new hold to debounce this
2273 if (mDefaultInetCondition > 50) {
2274 delay = Settings.Secure.getInt(mContext.getContentResolver(),
2275 Settings.Secure.INET_CONDITION_DEBOUNCE_UP_DELAY, 500);
2276 } else {
2277 delay = Settings.Secure.getInt(mContext.getContentResolver(),
2278 Settings.Secure.INET_CONDITION_DEBOUNCE_DOWN_DELAY, 3000);
2279 }
2280 mInetConditionChangeInFlight = true;
2281 mHandler.sendMessageDelayed(mHandler.obtainMessage(EVENT_INET_CONDITION_HOLD_END,
2282 mActiveDefaultNetwork, mDefaultConnectionSequence), delay);
2283 } else {
2284 // we've set the new condition, when this hold ends that will get
2285 // picked up
Wink Savillee70c6f52010-12-03 12:01:38 -08002286 if (DBG) log("currently in hold - not setting new end evt");
Robert Greenwalt6a2db8a2010-09-23 10:05:56 -07002287 }
2288 }
2289
2290 private void handleInetConditionHoldEnd(int netType, int sequence) {
2291 if (DBG) {
Wink Savillee70c6f52010-12-03 12:01:38 -08002292 log("Inet hold end, net=" + netType +
Robert Greenwalt6a2db8a2010-09-23 10:05:56 -07002293 ", condition =" + mDefaultInetCondition +
2294 ", published condition =" + mDefaultInetConditionPublished);
2295 }
2296 mInetConditionChangeInFlight = false;
2297
2298 if (mActiveDefaultNetwork == -1) {
Wink Savillee70c6f52010-12-03 12:01:38 -08002299 if (DBG) log("no active default network - aborting");
Robert Greenwalt6a2db8a2010-09-23 10:05:56 -07002300 return;
2301 }
2302 if (mDefaultConnectionSequence != sequence) {
Wink Savillee70c6f52010-12-03 12:01:38 -08002303 if (DBG) log("event hold for obsolete network - aborting");
Robert Greenwalt6a2db8a2010-09-23 10:05:56 -07002304 return;
2305 }
2306 if (mDefaultInetConditionPublished == mDefaultInetCondition) {
Wink Savillee70c6f52010-12-03 12:01:38 -08002307 if (DBG) log("no change in condition - aborting");
Robert Greenwalt6a2db8a2010-09-23 10:05:56 -07002308 return;
2309 }
2310 NetworkInfo networkInfo = mNetTrackers[mActiveDefaultNetwork].getNetworkInfo();
2311 if (networkInfo.isConnected() == false) {
Wink Savillee70c6f52010-12-03 12:01:38 -08002312 if (DBG) log("default network not connected - aborting");
Robert Greenwalt6a2db8a2010-09-23 10:05:56 -07002313 return;
2314 }
2315 mDefaultInetConditionPublished = mDefaultInetCondition;
2316 sendInetConditionBroadcast(networkInfo);
2317 return;
Robert Greenwalt986c7412010-09-08 15:24:47 -07002318 }
Robert Greenwaltc3c5f862010-10-11 16:00:27 -07002319
2320 public synchronized ProxyProperties getProxy() {
2321 if (mGlobalProxy != null) return mGlobalProxy;
2322 if (mDefaultProxy != null) return mDefaultProxy;
2323 return null;
2324 }
2325
2326 public void setGlobalProxy(ProxyProperties proxyProperties) {
2327 enforceChangePermission();
2328 synchronized (mGlobalProxyLock) {
2329 if (proxyProperties == mGlobalProxy) return;
2330 if (proxyProperties != null && proxyProperties.equals(mGlobalProxy)) return;
2331 if (mGlobalProxy != null && mGlobalProxy.equals(proxyProperties)) return;
2332
2333 String host = "";
2334 int port = 0;
2335 String exclList = "";
2336 if (proxyProperties != null && !TextUtils.isEmpty(proxyProperties.getHost())) {
2337 mGlobalProxy = new ProxyProperties(proxyProperties);
2338 host = mGlobalProxy.getHost();
2339 port = mGlobalProxy.getPort();
2340 exclList = mGlobalProxy.getExclusionList();
2341 } else {
2342 mGlobalProxy = null;
2343 }
2344 ContentResolver res = mContext.getContentResolver();
2345 Settings.Secure.putString(res, Settings.Secure.GLOBAL_HTTP_PROXY_HOST, host);
2346 Settings.Secure.putInt(res, Settings.Secure.GLOBAL_HTTP_PROXY_PORT, port);
Robert Greenwalt6f7c6092010-12-02 11:31:00 -08002347 Settings.Secure.putString(res, Settings.Secure.GLOBAL_HTTP_PROXY_EXCLUSION_LIST,
Robert Greenwaltc3c5f862010-10-11 16:00:27 -07002348 exclList);
2349 }
2350
2351 if (mGlobalProxy == null) {
2352 proxyProperties = mDefaultProxy;
2353 }
2354 sendProxyBroadcast(proxyProperties);
2355 }
2356
Robert Greenwalt6f7c6092010-12-02 11:31:00 -08002357 private void loadGlobalProxy() {
2358 ContentResolver res = mContext.getContentResolver();
2359 String host = Settings.Secure.getString(res, Settings.Secure.GLOBAL_HTTP_PROXY_HOST);
2360 int port = Settings.Secure.getInt(res, Settings.Secure.GLOBAL_HTTP_PROXY_PORT, 0);
2361 String exclList = Settings.Secure.getString(res,
2362 Settings.Secure.GLOBAL_HTTP_PROXY_EXCLUSION_LIST);
2363 if (!TextUtils.isEmpty(host)) {
2364 ProxyProperties proxyProperties = new ProxyProperties(host, port, exclList);
2365 synchronized (mGlobalProxyLock) {
2366 mGlobalProxy = proxyProperties;
2367 }
2368 }
2369 }
2370
Robert Greenwaltc3c5f862010-10-11 16:00:27 -07002371 public ProxyProperties getGlobalProxy() {
2372 synchronized (mGlobalProxyLock) {
2373 return mGlobalProxy;
2374 }
2375 }
2376
2377 private void handleApplyDefaultProxy(int type) {
2378 // check if new default - push it out to all VM if so
2379 ProxyProperties proxy = mNetTrackers[type].getLinkProperties().getHttpProxy();
2380 synchronized (this) {
2381 if (mDefaultProxy != null && mDefaultProxy.equals(proxy)) return;
2382 if (mDefaultProxy == proxy) return;
2383 if (!TextUtils.isEmpty(proxy.getHost())) {
2384 mDefaultProxy = proxy;
2385 } else {
2386 mDefaultProxy = null;
2387 }
2388 }
Wink Savillee70c6f52010-12-03 12:01:38 -08002389 if (DBG) log("changing default proxy to " + proxy);
Robert Greenwaltc3c5f862010-10-11 16:00:27 -07002390 if ((proxy == null && mGlobalProxy == null) || proxy.equals(mGlobalProxy)) return;
2391 if (mGlobalProxy != null) return;
2392 sendProxyBroadcast(proxy);
2393 }
2394
2395 private void handleDeprecatedGlobalHttpProxy() {
2396 String proxy = Settings.Secure.getString(mContext.getContentResolver(),
2397 Settings.Secure.HTTP_PROXY);
2398 if (!TextUtils.isEmpty(proxy)) {
2399 String data[] = proxy.split(":");
2400 String proxyHost = data[0];
2401 int proxyPort = 8080;
2402 if (data.length > 1) {
2403 try {
2404 proxyPort = Integer.parseInt(data[1]);
2405 } catch (NumberFormatException e) {
2406 return;
2407 }
2408 }
2409 ProxyProperties p = new ProxyProperties(data[0], proxyPort, "");
2410 setGlobalProxy(p);
2411 }
2412 }
2413
2414 private void sendProxyBroadcast(ProxyProperties proxy) {
Robert Greenwalt611291c2010-12-23 15:51:10 -08002415 if (proxy == null) proxy = new ProxyProperties("", 0, "");
Wink Savillee70c6f52010-12-03 12:01:38 -08002416 log("sending Proxy Broadcast for " + proxy);
Robert Greenwaltc3c5f862010-10-11 16:00:27 -07002417 Intent intent = new Intent(Proxy.PROXY_CHANGE_ACTION);
Stan Chesnutt1f2a2ac2011-01-06 11:00:19 -08002418 intent.addFlags(Intent.FLAG_RECEIVER_REPLACE_PENDING |
2419 Intent.FLAG_RECEIVER_REGISTERED_ONLY_BEFORE_BOOT);
Robert Greenwaltc3c5f862010-10-11 16:00:27 -07002420 intent.putExtra(Proxy.EXTRA_PROXY_INFO, proxy);
Robert Greenwaltd93dc8f2010-12-06 11:29:17 -08002421 mContext.sendStickyBroadcast(intent);
Robert Greenwaltc3c5f862010-10-11 16:00:27 -07002422 }
2423
2424 private static class SettingsObserver extends ContentObserver {
2425 private int mWhat;
2426 private Handler mHandler;
2427 SettingsObserver(Handler handler, int what) {
2428 super(handler);
2429 mHandler = handler;
2430 mWhat = what;
2431 }
2432
2433 void observe(Context context) {
2434 ContentResolver resolver = context.getContentResolver();
2435 resolver.registerContentObserver(Settings.Secure.getUriFor(
2436 Settings.Secure.HTTP_PROXY), false, this);
2437 }
2438
2439 @Override
2440 public void onChange(boolean selfChange) {
2441 mHandler.obtainMessage(mWhat).sendToTarget();
2442 }
2443 }
Wink Savillee70c6f52010-12-03 12:01:38 -08002444
2445 private void log(String s) {
2446 Slog.d(TAG, s);
2447 }
2448
2449 private void loge(String s) {
2450 Slog.e(TAG, s);
2451 }
Chia-chi Yeh9a4ad7d2011-05-23 17:26:46 -07002452
Wink Savillef6b76692011-02-24 17:58:51 -08002453 int convertFeatureToNetworkType(String feature){
2454 int networkType = -1;
2455 if (TextUtils.equals(feature, Phone.FEATURE_ENABLE_MMS)) {
2456 networkType = ConnectivityManager.TYPE_MOBILE_MMS;
2457 } else if (TextUtils.equals(feature, Phone.FEATURE_ENABLE_SUPL)) {
2458 networkType = ConnectivityManager.TYPE_MOBILE_SUPL;
2459 } else if (TextUtils.equals(feature, Phone.FEATURE_ENABLE_DUN) ||
2460 TextUtils.equals(feature, Phone.FEATURE_ENABLE_DUN_ALWAYS)) {
2461 networkType = ConnectivityManager.TYPE_MOBILE_DUN;
2462 } else if (TextUtils.equals(feature, Phone.FEATURE_ENABLE_HIPRI)) {
2463 networkType = ConnectivityManager.TYPE_MOBILE_HIPRI;
2464 } else if (TextUtils.equals(feature, Phone.FEATURE_ENABLE_FOTA)) {
2465 networkType = ConnectivityManager.TYPE_MOBILE_FOTA;
2466 } else if (TextUtils.equals(feature, Phone.FEATURE_ENABLE_IMS)) {
2467 networkType = ConnectivityManager.TYPE_MOBILE_IMS;
2468 } else if (TextUtils.equals(feature, Phone.FEATURE_ENABLE_CBS)) {
2469 networkType = ConnectivityManager.TYPE_MOBILE_CBS;
2470 }
2471 return networkType;
2472 }
Jeff Sharkey921ebf22011-05-19 17:12:49 -07002473
2474 private static <T> T checkNotNull(T value, String message) {
2475 if (value == null) {
2476 throw new NullPointerException(message);
2477 }
2478 return value;
2479 }
Chia-chi Yeh9a4ad7d2011-05-23 17:26:46 -07002480
Chia-chi Yeh75cacd52011-06-15 17:07:27 -07002481 /**
2482 * Protect a socket from VPN routing rules. This method is used by
Chia-chi Yehbded3eb2011-07-04 03:23:12 -07002483 * VpnBuilder and not available in ConnectivityManager. Permissions
2484 * are checked in Vpn class.
Chia-chi Yeh75cacd52011-06-15 17:07:27 -07002485 * @hide
2486 */
Chia-chi Yeh9a4ad7d2011-05-23 17:26:46 -07002487 @Override
Chia-chi Yeh9e4ff6e2011-07-14 16:19:19 -07002488 public boolean protectVpn(ParcelFileDescriptor socket) {
2489 try {
2490 int type = mActiveDefaultNetwork;
2491 if (ConnectivityManager.isNetworkTypeValid(type)) {
2492 mVpn.protect(socket, mNetTrackers[type].getLinkProperties().getInterfaceName());
2493 return true;
2494 }
2495 } catch (Exception e) {
2496 // ignore
2497 } finally {
2498 try {
2499 socket.close();
2500 } catch (Exception e) {
2501 // ignore
2502 }
2503 }
2504 return false;
Chia-chi Yeh9a4ad7d2011-05-23 17:26:46 -07002505 }
2506
Chia-chi Yeh75cacd52011-06-15 17:07:27 -07002507 /**
2508 * Prepare for a VPN application. This method is used by VpnDialogs
Chia-chi Yehbded3eb2011-07-04 03:23:12 -07002509 * and not available in ConnectivityManager. Permissions are checked
2510 * in Vpn class.
Chia-chi Yeh75cacd52011-06-15 17:07:27 -07002511 * @hide
2512 */
Chia-chi Yeh9a4ad7d2011-05-23 17:26:46 -07002513 @Override
Chia-chi Yeh3e2e1da2011-07-03 16:52:38 -07002514 public boolean prepareVpn(String oldPackage, String newPackage) {
2515 return mVpn.prepare(oldPackage, newPackage);
Chia-chi Yeh9a4ad7d2011-05-23 17:26:46 -07002516 }
2517
Chia-chi Yeh75cacd52011-06-15 17:07:27 -07002518 /**
2519 * Configure a TUN interface and return its file descriptor. Parameters
2520 * are encoded and opaque to this class. This method is used by VpnBuilder
Chia-chi Yehbded3eb2011-07-04 03:23:12 -07002521 * and not available in ConnectivityManager. Permissions are checked in
2522 * Vpn class.
Chia-chi Yeh75cacd52011-06-15 17:07:27 -07002523 * @hide
2524 */
Chia-chi Yeh9a4ad7d2011-05-23 17:26:46 -07002525 @Override
Chia-chi Yeh75cacd52011-06-15 17:07:27 -07002526 public ParcelFileDescriptor establishVpn(VpnConfig config) {
Chia-chi Yeh9a4ad7d2011-05-23 17:26:46 -07002527 return mVpn.establish(config);
2528 }
2529
Chia-chi Yeh7e026b32011-07-02 17:15:00 -07002530 /**
Chia-chi Yehbded3eb2011-07-04 03:23:12 -07002531 * Start legacy VPN and return an intent to VpnDialogs. This method is
2532 * used by VpnSettings and not available in ConnectivityManager.
2533 * Permissions are checked in Vpn class.
Chia-chi Yeh7e026b32011-07-02 17:15:00 -07002534 * @hide
2535 */
2536 @Override
Chia-chi Yehbded3eb2011-07-04 03:23:12 -07002537 public void startLegacyVpn(VpnConfig config, String[] racoon, String[] mtpd) {
2538 mVpn.startLegacyVpn(config, racoon, mtpd);
2539 }
2540
2541 /**
2542 * Return the information of the ongoing legacy VPN. This method is used
2543 * by VpnSettings and not available in ConnectivityManager. Permissions
2544 * are checked in Vpn class.
2545 * @hide
2546 */
2547 @Override
2548 public LegacyVpnInfo getLegacyVpnInfo() {
2549 return mVpn.getLegacyVpnInfo();
Chia-chi Yeh7e026b32011-07-02 17:15:00 -07002550 }
2551
Chia-chi Yeh9a4ad7d2011-05-23 17:26:46 -07002552 /**
2553 * Callback for VPN subsystem. Currently VPN is not adapted to the service
2554 * through NetworkStateTracker since it works differently. For example, it
2555 * needs to override DNS servers but never takes the default routes. It
2556 * relies on another data network, and it could keep existing connections
2557 * alive after reconnecting, switching between networks, or even resuming
2558 * from deep sleep. Calls from applications should be done synchronously
2559 * to avoid race conditions. As these are all hidden APIs, refactoring can
2560 * be done whenever a better abstraction is developed.
2561 */
2562 public class VpnCallback {
2563
2564 private VpnCallback() {
2565 }
2566
Chia-chi Yehd050d1c2011-07-01 01:09:42 -07002567 public synchronized void override(List<String> dnsServers, List<String> searchDomains) {
Chia-chi Yeh9a4ad7d2011-05-23 17:26:46 -07002568 // TODO: override DNS servers and http proxy.
2569 }
2570
2571 public synchronized void restore() {
2572 // TODO: restore VPN changes.
2573 }
2574 }
The Android Open Source Project28527d22009-03-03 19:31:44 -08002575}