blob: ad63424493872873144102e8461bdf4128125920 [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;
Haoyu Baib5da5752012-06-20 14:29:57 -070020import static android.Manifest.permission.RECEIVE_DATA_ACTIVITY_CHANGE;
Jeff Sharkey971cd162011-08-29 16:02:57 -070021import static android.net.ConnectivityManager.CONNECTIVITY_ACTION;
22import static android.net.ConnectivityManager.CONNECTIVITY_ACTION_IMMEDIATE;
Jeff Sharkey921ebf22011-05-19 17:12:49 -070023import static android.net.ConnectivityManager.isNetworkTypeValid;
24import static android.net.NetworkPolicyManager.RULE_ALLOW_ALL;
Jeff Sharkeya47d7a12011-06-16 15:07:48 -070025import static android.net.NetworkPolicyManager.RULE_REJECT_METERED;
Jeff Sharkey921ebf22011-05-19 17:12:49 -070026
Jaikumar Ganesh0db51a02010-12-21 22:31:44 -080027import android.bluetooth.BluetoothTetheringDataTracker;
The Android Open Source Project28527d22009-03-03 19:31:44 -080028import android.content.ContentResolver;
29import android.content.Context;
tk.mun093f55c2011-10-13 22:51:57 +090030import android.content.ContextWrapper;
The Android Open Source Project28527d22009-03-03 19:31:44 -080031import android.content.Intent;
32import android.content.pm.PackageManager;
tk.mun093f55c2011-10-13 22:51:57 +090033import android.content.res.Resources;
Robert Greenwaltc3c5f862010-10-11 16:00:27 -070034import android.database.ContentObserver;
The Android Open Source Project28527d22009-03-03 19:31:44 -080035import android.net.ConnectivityManager;
Robert Greenwalteb123ac2010-12-06 13:56:24 -080036import android.net.DummyDataStateTracker;
Benoit Goby211b5692010-12-22 14:29:40 -080037import android.net.EthernetDataTracker;
The Android Open Source Project28527d22009-03-03 19:31:44 -080038import android.net.IConnectivityManager;
Haoyu Baib5da5752012-06-20 14:29:57 -070039import android.net.INetworkManagementEventObserver;
Jeff Sharkey921ebf22011-05-19 17:12:49 -070040import android.net.INetworkPolicyListener;
41import android.net.INetworkPolicyManager;
Jeff Sharkeyb6188a12011-09-22 14:59:51 -070042import android.net.INetworkStatsService;
Wink Savilledc5d1ba2011-07-14 12:23:28 -070043import android.net.LinkAddress;
Jaikumar Ganesh0db51a02010-12-21 22:31:44 -080044import android.net.LinkProperties;
Robert Greenwalte8d2a4a2011-07-14 14:28:05 -070045import android.net.LinkProperties.CompareResult;
The Android Open Source Project28527d22009-03-03 19:31:44 -080046import android.net.MobileDataStateTracker;
Robert Greenwalt34848c02011-03-25 13:09:25 -070047import android.net.NetworkConfig;
The Android Open Source Project28527d22009-03-03 19:31:44 -080048import android.net.NetworkInfo;
Jeff Sharkey921ebf22011-05-19 17:12:49 -070049import android.net.NetworkInfo.DetailedState;
Jeff Sharkey66fa9682011-08-02 17:22:34 -070050import android.net.NetworkQuotaInfo;
Jeff Sharkey21062e72011-05-28 20:56:34 -070051import android.net.NetworkState;
The Android Open Source Project28527d22009-03-03 19:31:44 -080052import android.net.NetworkStateTracker;
Robert Greenwalt7fe44cb2010-08-27 09:24:29 -070053import android.net.NetworkUtils;
Robert Greenwaltc3c5f862010-10-11 16:00:27 -070054import android.net.Proxy;
55import android.net.ProxyProperties;
Robert Greenwalt5a901292011-04-28 14:28:50 -070056import android.net.RouteInfo;
The Android Open Source Project28527d22009-03-03 19:31:44 -080057import android.net.wifi.WifiStateTracker;
tk.mun093f55c2011-10-13 22:51:57 +090058import android.net.wimax.WimaxManagerConstants;
The Android Open Source Project28527d22009-03-03 19:31:44 -080059import android.os.Binder;
Mike Lockwood0d5916c2011-05-28 13:24:04 -040060import android.os.FileUtils;
The Android Open Source Project28527d22009-03-03 19:31:44 -080061import android.os.Handler;
Wink Saville775aad62010-09-02 19:23:52 -070062import android.os.HandlerThread;
Robert Greenwalt2034b912009-08-12 16:08:25 -070063import android.os.IBinder;
Chia-chi Yeh4df51322011-05-11 16:35:13 -070064import android.os.INetworkManagementService;
The Android Open Source Project28527d22009-03-03 19:31:44 -080065import android.os.Looper;
66import android.os.Message;
Chia-chi Yeh9a4ad7d2011-05-23 17:26:46 -070067import android.os.ParcelFileDescriptor;
Robert Greenwalt93dc1042010-06-15 12:19:37 -070068import android.os.PowerManager;
Robert Greenwalt2034b912009-08-12 16:08:25 -070069import android.os.RemoteException;
The Android Open Source Project28527d22009-03-03 19:31:44 -080070import android.os.ServiceManager;
Robert Greenwalt1e6991e2012-05-22 16:07:46 -070071import android.os.SystemClock;
The Android Open Source Project28527d22009-03-03 19:31:44 -080072import android.os.SystemProperties;
73import android.provider.Settings;
Robert Greenwalt2034b912009-08-12 16:08:25 -070074import android.text.TextUtils;
The Android Open Source Project28527d22009-03-03 19:31:44 -080075import android.util.EventLog;
Joe Onoratoc2386bb2010-02-26 18:56:32 -080076import android.util.Slog;
Jeff Sharkey921ebf22011-05-19 17:12:49 -070077import android.util.SparseIntArray;
The Android Open Source Project28527d22009-03-03 19:31:44 -080078
Chia-chi Yehbded3eb2011-07-04 03:23:12 -070079import com.android.internal.net.LegacyVpnInfo;
Chia-chi Yeh75cacd52011-06-15 17:07:27 -070080import com.android.internal.net.VpnConfig;
Robert Greenwalt2034b912009-08-12 16:08:25 -070081import com.android.internal.telephony.Phone;
Wink Saville64e3f782012-07-10 12:37:54 -070082import com.android.internal.telephony.PhoneConstants;
Jeff Sharkeyaac2c502011-10-04 16:54:49 -070083import com.android.server.am.BatteryStatsService;
Robert Greenwalt0c4828c2010-01-26 11:40:34 -080084import com.android.server.connectivity.Tethering;
Chia-chi Yeh9a4ad7d2011-05-23 17:26:46 -070085import com.android.server.connectivity.Vpn;
Jeff Sharkey60e6c1a2012-08-05 14:29:23 -070086import com.android.server.net.BaseNetworkObserver;
Jeff Sharkey21062e72011-05-28 20:56:34 -070087import com.google.android.collect.Lists;
Jeff Sharkeya47d7a12011-06-16 15:07:48 -070088import com.google.android.collect.Sets;
tk.mun093f55c2011-10-13 22:51:57 +090089import dalvik.system.DexClassLoader;
The Android Open Source Project28527d22009-03-03 19:31:44 -080090import java.io.FileDescriptor;
Irfan Sheriff7f132d92010-06-09 15:39:36 -070091import java.io.IOException;
The Android Open Source Project28527d22009-03-03 19:31:44 -080092import java.io.PrintWriter;
tk.mun093f55c2011-10-13 22:51:57 +090093import java.lang.reflect.Constructor;
94import java.lang.reflect.Method;
95import java.lang.reflect.Modifier;
96import java.lang.reflect.InvocationTargetException;
Wink Savilledc5d1ba2011-07-14 12:23:28 -070097import java.net.Inet4Address;
Wink Saville051a6642011-07-13 13:44:13 -070098import java.net.Inet6Address;
Robert Greenwalta7dfbd32010-06-15 15:43:39 -070099import java.net.InetAddress;
100import java.net.UnknownHostException;
Robert Greenwalt2034b912009-08-12 16:08:25 -0700101import java.util.ArrayList;
Jeff Sharkeya47d7a12011-06-16 15:07:48 -0700102import java.util.Arrays;
Robert Greenwalta7dfbd32010-06-15 15:43:39 -0700103import java.util.Collection;
Robert Greenwalt0e80be12010-09-20 14:35:25 -0700104import java.util.GregorianCalendar;
Jeff Sharkeya47d7a12011-06-16 15:07:48 -0700105import java.util.HashSet;
Robert Greenwalt2034b912009-08-12 16:08:25 -0700106import java.util.List;
The Android Open Source Project28527d22009-03-03 19:31:44 -0800107
108/**
109 * @hide
110 */
111public class ConnectivityService extends IConnectivityManager.Stub {
112
Robert Greenwalt063dc7d2010-10-05 19:12:26 -0700113 private static final boolean DBG = true;
Wink Savillea7d56572011-09-21 11:05:43 -0700114 private static final boolean VDBG = false;
The Android Open Source Project28527d22009-03-03 19:31:44 -0800115 private static final String TAG = "ConnectivityService";
116
Jeff Sharkey921ebf22011-05-19 17:12:49 -0700117 private static final boolean LOGD_RULES = false;
118
Robert Greenwalt2034b912009-08-12 16:08:25 -0700119 // how long to wait before switching back to a radio's default network
120 private static final int RESTORE_DEFAULT_NETWORK_DELAY = 1 * 60 * 1000;
121 // system property that can override the above value
122 private static final String NETWORK_RESTORE_DELAY_PROP_NAME =
123 "android.telephony.apn-restore";
124
Robert Greenwaltbd492212011-05-06 17:10:53 -0700125 // used in recursive route setting to add gateways for the host for which
126 // a host route was requested.
127 private static final int MAX_HOSTROUTE_CYCLE_COUNT = 10;
128
Robert Greenwalt0c4828c2010-01-26 11:40:34 -0800129 private Tethering mTethering;
Robert Greenwaltf1b66e12010-02-25 12:29:30 -0800130 private boolean mTetheringConfigValid = false;
Robert Greenwalt0c4828c2010-01-26 11:40:34 -0800131
Chia-chi Yeh9a4ad7d2011-05-23 17:26:46 -0700132 private Vpn mVpn;
133
Jeff Sharkeya47d7a12011-06-16 15:07:48 -0700134 /** Lock around {@link #mUidRules} and {@link #mMeteredIfaces}. */
135 private Object mRulesLock = new Object();
Jeff Sharkey921ebf22011-05-19 17:12:49 -0700136 /** Currently active network rules by UID. */
137 private SparseIntArray mUidRules = new SparseIntArray();
Jeff Sharkeya47d7a12011-06-16 15:07:48 -0700138 /** Set of ifaces that are costly. */
139 private HashSet<String> mMeteredIfaces = Sets.newHashSet();
Jeff Sharkey921ebf22011-05-19 17:12:49 -0700140
The Android Open Source Project28527d22009-03-03 19:31:44 -0800141 /**
142 * Sometimes we want to refer to the individual network state
143 * trackers separately, and sometimes we just want to treat them
144 * abstractly.
145 */
146 private NetworkStateTracker mNetTrackers[];
Robert Greenwalt2034b912009-08-12 16:08:25 -0700147
148 /**
Wink Saville051a6642011-07-13 13:44:13 -0700149 * The link properties that define the current links
150 */
151 private LinkProperties mCurrentLinkProperties[];
152
153 /**
Robert Greenwalt2034b912009-08-12 16:08:25 -0700154 * A per Net list of the PID's that requested access to the net
155 * used both as a refcount and for per-PID DNS selection
156 */
157 private List mNetRequestersPids[];
158
Robert Greenwalt2034b912009-08-12 16:08:25 -0700159 // priority order of the nettrackers
160 // (excluding dynamically set mNetworkPreference)
161 // TODO - move mNetworkTypePreference into this
162 private int[] mPriorityList;
163
The Android Open Source Project28527d22009-03-03 19:31:44 -0800164 private Context mContext;
165 private int mNetworkPreference;
Robert Greenwalt2034b912009-08-12 16:08:25 -0700166 private int mActiveDefaultNetwork = -1;
Robert Greenwalt986c7412010-09-08 15:24:47 -0700167 // 0 is full bad, 100 is full good
168 private int mDefaultInetCondition = 0;
169 private int mDefaultInetConditionPublished = 0;
170 private boolean mInetConditionChangeInFlight = false;
171 private int mDefaultConnectionSequence = 0;
The Android Open Source Project28527d22009-03-03 19:31:44 -0800172
Chia-chi Yehcc844502011-07-14 18:01:57 -0700173 private Object mDnsLock = new Object();
The Android Open Source Project28527d22009-03-03 19:31:44 -0800174 private int mNumDnsEntries;
Chia-chi Yehcc844502011-07-14 18:01:57 -0700175 private boolean mDnsOverridden = false;
The Android Open Source Project28527d22009-03-03 19:31:44 -0800176
177 private boolean mTestMode;
Joe Onorato56023ad2010-09-01 21:18:22 -0700178 private static ConnectivityService sServiceInstance;
The Android Open Source Project28527d22009-03-03 19:31:44 -0800179
Robert Greenwalt355205c2011-05-10 15:05:02 -0700180 private INetworkManagementService mNetd;
Jeff Sharkey921ebf22011-05-19 17:12:49 -0700181 private INetworkPolicyManager mPolicyManager;
Robert Greenwalt355205c2011-05-10 15:05:02 -0700182
Robert Greenwalt6a2db8a2010-09-23 10:05:56 -0700183 private static final int ENABLED = 1;
184 private static final int DISABLED = 0;
185
Robert Greenwalt49c75d32011-11-02 14:37:19 -0700186 private static final boolean ADD = true;
187 private static final boolean REMOVE = false;
188
189 private static final boolean TO_DEFAULT_TABLE = true;
190 private static final boolean TO_SECONDARY_TABLE = false;
191
Robert Greenwalt6a2db8a2010-09-23 10:05:56 -0700192 /**
193 * used internally as a delayed event to make us switch back to the
194 * default network
195 */
Jeff Sharkeya1ef1be2012-07-23 13:19:46 -0700196 private static final int EVENT_RESTORE_DEFAULT_NETWORK = 1;
Robert Greenwalt6a2db8a2010-09-23 10:05:56 -0700197
198 /**
199 * used internally to change our mobile data enabled flag
200 */
Jeff Sharkeya1ef1be2012-07-23 13:19:46 -0700201 private static final int EVENT_CHANGE_MOBILE_DATA_ENABLED = 2;
Robert Greenwalt6a2db8a2010-09-23 10:05:56 -0700202
203 /**
204 * used internally to change our network preference setting
205 * arg1 = networkType to prefer
206 */
Jeff Sharkeya1ef1be2012-07-23 13:19:46 -0700207 private static final int EVENT_SET_NETWORK_PREFERENCE = 3;
Robert Greenwalt6a2db8a2010-09-23 10:05:56 -0700208
209 /**
210 * used internally to synchronize inet condition reports
211 * arg1 = networkType
212 * arg2 = condition (0 bad, 100 good)
213 */
Jeff Sharkeya1ef1be2012-07-23 13:19:46 -0700214 private static final int EVENT_INET_CONDITION_CHANGE = 4;
Robert Greenwalt6a2db8a2010-09-23 10:05:56 -0700215
216 /**
217 * used internally to mark the end of inet condition hold periods
218 * arg1 = networkType
219 */
Jeff Sharkeya1ef1be2012-07-23 13:19:46 -0700220 private static final int EVENT_INET_CONDITION_HOLD_END = 5;
Robert Greenwalt6a2db8a2010-09-23 10:05:56 -0700221
222 /**
Robert Greenwalt6a2db8a2010-09-23 10:05:56 -0700223 * used internally to set enable/disable cellular data
224 * arg1 = ENBALED or DISABLED
225 */
Jeff Sharkeya1ef1be2012-07-23 13:19:46 -0700226 private static final int EVENT_SET_MOBILE_DATA = 7;
Robert Greenwalt6a2db8a2010-09-23 10:05:56 -0700227
Robert Greenwaltccb36f92010-09-24 14:32:21 -0700228 /**
229 * used internally to clear a wakelock when transitioning
230 * from one net to another
231 */
Jeff Sharkeya1ef1be2012-07-23 13:19:46 -0700232 private static final int EVENT_CLEAR_NET_TRANSITION_WAKELOCK = 8;
Robert Greenwaltccb36f92010-09-24 14:32:21 -0700233
Robert Greenwaltc3c5f862010-10-11 16:00:27 -0700234 /**
235 * used internally to reload global proxy settings
236 */
Jeff Sharkeya1ef1be2012-07-23 13:19:46 -0700237 private static final int EVENT_APPLY_GLOBAL_HTTP_PROXY = 9;
Robert Greenwaltc3c5f862010-10-11 16:00:27 -0700238
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 */
Jeff Sharkeya1ef1be2012-07-23 13:19:46 -0700244 private static final int EVENT_SET_DEPENDENCY_MET = 10;
Robert Greenwalt34848c02011-03-25 13:09:25 -0700245
Chia-chi Yehcc844502011-07-14 18:01:57 -0700246 /**
247 * used internally to restore DNS properties back to the
248 * default network
249 */
Jeff Sharkeya1ef1be2012-07-23 13:19:46 -0700250 private static final int EVENT_RESTORE_DNS = 11;
Chia-chi Yehcc844502011-07-14 18:01:57 -0700251
Wink Saville7e4333c2011-08-05 11:40:22 -0700252 /**
Wink Saville4f0de1e2011-08-04 15:01:58 -0700253 * used internally to send a sticky broadcast delayed.
254 */
Jeff Sharkeya1ef1be2012-07-23 13:19:46 -0700255 private static final int EVENT_SEND_STICKY_BROADCAST_INTENT = 12;
Wink Saville4f0de1e2011-08-04 15:01:58 -0700256
Jeff Sharkey805662d2011-08-19 02:24:24 -0700257 /**
258 * Used internally to
259 * {@link NetworkStateTracker#setPolicyDataEnable(boolean)}.
260 */
Jeff Sharkeya1ef1be2012-07-23 13:19:46 -0700261 private static final int EVENT_SET_POLICY_DATA_ENABLE = 13;
Jeff Sharkey805662d2011-08-19 02:24:24 -0700262
Jeff Sharkeya1ef1be2012-07-23 13:19:46 -0700263 /** Handler used for internal events. */
264 private InternalHandler mHandler;
265 /** Handler used for incoming {@link NetworkStateTracker} events. */
266 private NetworkStateTrackerHandler mTrackerHandler;
Robert Greenwalt2034b912009-08-12 16:08:25 -0700267
268 // list of DeathRecipients used to make sure features are turned off when
269 // a process dies
Kazuhiro Ondocae18f12011-07-19 11:23:37 -0500270 private List<FeatureUser> mFeatureUsers;
Robert Greenwalt2034b912009-08-12 16:08:25 -0700271
Mike Lockwoodfde2b762009-08-14 14:18:49 -0400272 private boolean mSystemReady;
Dianne Hackborna417ff82009-12-08 19:45:14 -0800273 private Intent mInitialBroadcast;
Mike Lockwoodfde2b762009-08-14 14:18:49 -0400274
Robert Greenwalt93dc1042010-06-15 12:19:37 -0700275 private PowerManager.WakeLock mNetTransitionWakeLock;
276 private String mNetTransitionWakeLockCausedBy = "";
277 private int mNetTransitionWakeLockSerialNumber;
278 private int mNetTransitionWakeLockTimeout;
279
Robert Greenwalt94daa182010-09-01 11:34:05 -0700280 private InetAddress mDefaultDns;
281
Robert Greenwalte8d2a4a2011-07-14 14:28:05 -0700282 // this collection is used to refcount the added routes - if there are none left
283 // it's time to remove the route from the route table
284 private Collection<RouteInfo> mAddedRoutes = new ArrayList<RouteInfo>();
285
Robert Greenwalt0e80be12010-09-20 14:35:25 -0700286 // used in DBG mode to track inet condition reports
287 private static final int INET_CONDITION_LOG_MAX_SIZE = 15;
288 private ArrayList mInetLog;
289
Robert Greenwaltc3c5f862010-10-11 16:00:27 -0700290 // track the current default http proxy - tell the world if we get a new one (real change)
291 private ProxyProperties mDefaultProxy = null;
Chia-chi Yeh763a11c2011-10-03 15:34:04 -0700292 private Object mDefaultProxyLock = new Object();
293 private boolean mDefaultProxyDisabled = false;
294
Robert Greenwaltc3c5f862010-10-11 16:00:27 -0700295 // track the global proxy.
296 private ProxyProperties mGlobalProxy = null;
297 private final Object mGlobalProxyLock = new Object();
298
299 private SettingsObserver mSettingsObserver;
300
Robert Greenwalt34848c02011-03-25 13:09:25 -0700301 NetworkConfig[] mNetConfigs;
Robert Greenwaltec05b3c2009-10-30 14:17:42 -0700302 int mNetworksDefined;
Robert Greenwalt2034b912009-08-12 16:08:25 -0700303
Robert Greenwalt12c44552009-12-07 11:33:18 -0800304 private static class RadioAttributes {
Robert Greenwalt2034b912009-08-12 16:08:25 -0700305 public int mSimultaneity;
306 public int mType;
307 public RadioAttributes(String init) {
308 String fragments[] = init.split(",");
Robert Greenwaltec05b3c2009-10-30 14:17:42 -0700309 mType = Integer.parseInt(fragments[0]);
310 mSimultaneity = Integer.parseInt(fragments[1]);
Robert Greenwalt2034b912009-08-12 16:08:25 -0700311 }
312 }
313 RadioAttributes[] mRadioAttributes;
314
Robert Greenwalt6cac0742011-06-21 17:26:14 -0700315 // the set of network types that can only be enabled by system/sig apps
316 List mProtectedNetworks;
317
Jeff Sharkeyb6188a12011-09-22 14:59:51 -0700318 public ConnectivityService(Context context, INetworkManagementService netd,
319 INetworkStatsService statsService, INetworkPolicyManager policyManager) {
Wink Savillee70c6f52010-12-03 12:01:38 -0800320 if (DBG) log("ConnectivityService starting up");
Robert Greenwaltd48f8ee2010-01-14 17:47:58 -0800321
Wink Saville775aad62010-09-02 19:23:52 -0700322 HandlerThread handlerThread = new HandlerThread("ConnectivityServiceThread");
323 handlerThread.start();
Jeff Sharkeya1ef1be2012-07-23 13:19:46 -0700324 mHandler = new InternalHandler(handlerThread.getLooper());
325 mTrackerHandler = new NetworkStateTrackerHandler(handlerThread.getLooper());
Wink Saville775aad62010-09-02 19:23:52 -0700326
Robert Greenwaltd48f8ee2010-01-14 17:47:58 -0800327 // setup our unique device name
Robert Greenwalt82cde132010-12-06 09:30:17 -0800328 if (TextUtils.isEmpty(SystemProperties.get("net.hostname"))) {
329 String id = Settings.Secure.getString(context.getContentResolver(),
330 Settings.Secure.ANDROID_ID);
331 if (id != null && id.length() > 0) {
Irfan Sheriff4aa0b2e2011-09-20 15:17:07 -0700332 String name = new String("android-").concat(id);
Robert Greenwalt82cde132010-12-06 09:30:17 -0800333 SystemProperties.set("net.hostname", name);
334 }
Robert Greenwaltd48f8ee2010-01-14 17:47:58 -0800335 }
336
Robert Greenwalt94daa182010-09-01 11:34:05 -0700337 // read our default dns server ip
338 String dns = Settings.Secure.getString(context.getContentResolver(),
339 Settings.Secure.DEFAULT_DNS_SERVER);
340 if (dns == null || dns.length() == 0) {
341 dns = context.getResources().getString(
342 com.android.internal.R.string.config_default_dns_server);
343 }
344 try {
Robert Greenwalt35e34d12011-02-22 16:00:42 -0800345 mDefaultDns = NetworkUtils.numericToInetAddress(dns);
346 } catch (IllegalArgumentException e) {
Wink Savillee70c6f52010-12-03 12:01:38 -0800347 loge("Error setting defaultDns using " + dns);
Robert Greenwalt94daa182010-09-01 11:34:05 -0700348 }
349
Jeff Sharkey921ebf22011-05-19 17:12:49 -0700350 mContext = checkNotNull(context, "missing Context");
351 mNetd = checkNotNull(netd, "missing INetworkManagementService");
352 mPolicyManager = checkNotNull(policyManager, "missing INetworkPolicyManager");
Robert Greenwalt93dc1042010-06-15 12:19:37 -0700353
Jeff Sharkey921ebf22011-05-19 17:12:49 -0700354 try {
355 mPolicyManager.registerListener(mPolicyListener);
356 } catch (RemoteException e) {
357 // ouch, no rules updates means some processes may never get network
Robert Greenwalt78f28112011-08-02 17:18:41 -0700358 loge("unable to register INetworkPolicyListener" + e.toString());
Jeff Sharkey921ebf22011-05-19 17:12:49 -0700359 }
360
361 final PowerManager powerManager = (PowerManager) context.getSystemService(
362 Context.POWER_SERVICE);
Robert Greenwalt93dc1042010-06-15 12:19:37 -0700363 mNetTransitionWakeLock = powerManager.newWakeLock(PowerManager.PARTIAL_WAKE_LOCK, TAG);
364 mNetTransitionWakeLockTimeout = mContext.getResources().getInteger(
365 com.android.internal.R.integer.config_networkTransitionTimeout);
366
Robert Greenwalt2034b912009-08-12 16:08:25 -0700367 mNetTrackers = new NetworkStateTracker[
368 ConnectivityManager.MAX_NETWORK_TYPE+1];
Wink Saville051a6642011-07-13 13:44:13 -0700369 mCurrentLinkProperties = new LinkProperties[ConnectivityManager.MAX_NETWORK_TYPE+1];
Robert Greenwalt0659da32009-07-16 17:21:39 -0700370
The Android Open Source Project28527d22009-03-03 19:31:44 -0800371 mNetworkPreference = getPersistedNetworkPreference();
Robert Greenwalt0659da32009-07-16 17:21:39 -0700372
Robert Greenwaltec05b3c2009-10-30 14:17:42 -0700373 mRadioAttributes = new RadioAttributes[ConnectivityManager.MAX_RADIO_TYPE+1];
Robert Greenwalt34848c02011-03-25 13:09:25 -0700374 mNetConfigs = new NetworkConfig[ConnectivityManager.MAX_NETWORK_TYPE+1];
Robert Greenwaltec05b3c2009-10-30 14:17:42 -0700375
Robert Greenwalt2034b912009-08-12 16:08:25 -0700376 // Load device network attributes from resources
Robert Greenwalt2034b912009-08-12 16:08:25 -0700377 String[] raStrings = context.getResources().getStringArray(
378 com.android.internal.R.array.radioAttributes);
Robert Greenwaltec05b3c2009-10-30 14:17:42 -0700379 for (String raString : raStrings) {
380 RadioAttributes r = new RadioAttributes(raString);
381 if (r.mType > ConnectivityManager.MAX_RADIO_TYPE) {
Wink Savillee70c6f52010-12-03 12:01:38 -0800382 loge("Error in radioAttributes - ignoring attempt to define type " + r.mType);
Robert Greenwaltec05b3c2009-10-30 14:17:42 -0700383 continue;
384 }
385 if (mRadioAttributes[r.mType] != null) {
Wink Savillee70c6f52010-12-03 12:01:38 -0800386 loge("Error in radioAttributes - ignoring attempt to redefine type " +
Robert Greenwaltec05b3c2009-10-30 14:17:42 -0700387 r.mType);
388 continue;
389 }
Robert Greenwalt2034b912009-08-12 16:08:25 -0700390 mRadioAttributes[r.mType] = r;
391 }
392
Robert Greenwaltec05b3c2009-10-30 14:17:42 -0700393 String[] naStrings = context.getResources().getStringArray(
394 com.android.internal.R.array.networkAttributes);
395 for (String naString : naStrings) {
396 try {
Robert Greenwalt34848c02011-03-25 13:09:25 -0700397 NetworkConfig n = new NetworkConfig(naString);
Wink Savillef2a62832011-04-07 14:23:45 -0700398 if (n.type > ConnectivityManager.MAX_NETWORK_TYPE) {
Wink Savillee70c6f52010-12-03 12:01:38 -0800399 loge("Error in networkAttributes - ignoring attempt to define type " +
Wink Savillef2a62832011-04-07 14:23:45 -0700400 n.type);
Robert Greenwaltec05b3c2009-10-30 14:17:42 -0700401 continue;
Robert Greenwalt2034b912009-08-12 16:08:25 -0700402 }
Wink Savillef2a62832011-04-07 14:23:45 -0700403 if (mNetConfigs[n.type] != null) {
Wink Savillee70c6f52010-12-03 12:01:38 -0800404 loge("Error in networkAttributes - ignoring attempt to redefine type " +
Wink Savillef2a62832011-04-07 14:23:45 -0700405 n.type);
Robert Greenwaltec05b3c2009-10-30 14:17:42 -0700406 continue;
407 }
Wink Savillef2a62832011-04-07 14:23:45 -0700408 if (mRadioAttributes[n.radio] == null) {
Wink Savillee70c6f52010-12-03 12:01:38 -0800409 loge("Error in networkAttributes - ignoring attempt to use undefined " +
Wink Savillef2a62832011-04-07 14:23:45 -0700410 "radio " + n.radio + " in network type " + n.type);
Robert Greenwaltec05b3c2009-10-30 14:17:42 -0700411 continue;
412 }
Wink Savillef2a62832011-04-07 14:23:45 -0700413 mNetConfigs[n.type] = n;
Robert Greenwaltec05b3c2009-10-30 14:17:42 -0700414 mNetworksDefined++;
415 } catch(Exception e) {
416 // ignore it - leave the entry null
Robert Greenwalt2034b912009-08-12 16:08:25 -0700417 }
418 }
419
Robert Greenwalt6cac0742011-06-21 17:26:14 -0700420 mProtectedNetworks = new ArrayList<Integer>();
421 int[] protectedNetworks = context.getResources().getIntArray(
422 com.android.internal.R.array.config_protectedNetworks);
423 for (int p : protectedNetworks) {
424 if ((mNetConfigs[p] != null) && (mProtectedNetworks.contains(p) == false)) {
425 mProtectedNetworks.add(p);
426 } else {
427 if (DBG) loge("Ignoring protectedNetwork " + p);
428 }
429 }
430
Robert Greenwaltec05b3c2009-10-30 14:17:42 -0700431 // high priority first
432 mPriorityList = new int[mNetworksDefined];
433 {
434 int insertionPoint = mNetworksDefined-1;
435 int currentLowest = 0;
436 int nextLowest = 0;
437 while (insertionPoint > -1) {
Robert Greenwalt34848c02011-03-25 13:09:25 -0700438 for (NetworkConfig na : mNetConfigs) {
Robert Greenwaltec05b3c2009-10-30 14:17:42 -0700439 if (na == null) continue;
Wink Savillef2a62832011-04-07 14:23:45 -0700440 if (na.priority < currentLowest) continue;
441 if (na.priority > currentLowest) {
442 if (na.priority < nextLowest || nextLowest == 0) {
443 nextLowest = na.priority;
Robert Greenwaltec05b3c2009-10-30 14:17:42 -0700444 }
445 continue;
446 }
Wink Savillef2a62832011-04-07 14:23:45 -0700447 mPriorityList[insertionPoint--] = na.type;
Robert Greenwaltec05b3c2009-10-30 14:17:42 -0700448 }
449 currentLowest = nextLowest;
450 nextLowest = 0;
451 }
452 }
453
454 mNetRequestersPids = new ArrayList[ConnectivityManager.MAX_NETWORK_TYPE+1];
455 for (int i : mPriorityList) {
Robert Greenwalt2034b912009-08-12 16:08:25 -0700456 mNetRequestersPids[i] = new ArrayList();
457 }
458
Kazuhiro Ondocae18f12011-07-19 11:23:37 -0500459 mFeatureUsers = new ArrayList<FeatureUser>();
Robert Greenwalt2034b912009-08-12 16:08:25 -0700460
Robert Greenwaltec05b3c2009-10-30 14:17:42 -0700461 mNumDnsEntries = 0;
462
463 mTestMode = SystemProperties.get("cm.test.mode").equals("true")
464 && SystemProperties.get("ro.build.type").equals("eng");
The Android Open Source Project28527d22009-03-03 19:31:44 -0800465 /*
466 * Create the network state trackers for Wi-Fi and mobile
467 * data. Maybe this could be done with a factory class,
468 * but it's not clear that it's worth it, given that
469 * the number of different network types is not going
470 * to change very often.
471 */
Robert Greenwaltec05b3c2009-10-30 14:17:42 -0700472 for (int netType : mPriorityList) {
Wink Savillef2a62832011-04-07 14:23:45 -0700473 switch (mNetConfigs[netType].radio) {
Robert Greenwaltec05b3c2009-10-30 14:17:42 -0700474 case ConnectivityManager.TYPE_WIFI:
Jeff Sharkeya1ef1be2012-07-23 13:19:46 -0700475 mNetTrackers[netType] = new WifiStateTracker(
476 netType, mNetConfigs[netType].name);
477 mNetTrackers[netType].startMonitoring(context, mTrackerHandler);
478 break;
Robert Greenwaltec05b3c2009-10-30 14:17:42 -0700479 case ConnectivityManager.TYPE_MOBILE:
Wink Saville7fabfa22010-08-13 16:11:42 -0700480 mNetTrackers[netType] = new MobileDataStateTracker(netType,
Wink Savillef2a62832011-04-07 14:23:45 -0700481 mNetConfigs[netType].name);
Jeff Sharkeya1ef1be2012-07-23 13:19:46 -0700482 mNetTrackers[netType].startMonitoring(context, mTrackerHandler);
Robert Greenwaltec05b3c2009-10-30 14:17:42 -0700483 break;
Robert Greenwalteb123ac2010-12-06 13:56:24 -0800484 case ConnectivityManager.TYPE_DUMMY:
485 mNetTrackers[netType] = new DummyDataStateTracker(netType,
Wink Savillef2a62832011-04-07 14:23:45 -0700486 mNetConfigs[netType].name);
Jeff Sharkeya1ef1be2012-07-23 13:19:46 -0700487 mNetTrackers[netType].startMonitoring(context, mTrackerHandler);
Robert Greenwalteb123ac2010-12-06 13:56:24 -0800488 break;
Jaikumar Ganesh0db51a02010-12-21 22:31:44 -0800489 case ConnectivityManager.TYPE_BLUETOOTH:
490 mNetTrackers[netType] = BluetoothTetheringDataTracker.getInstance();
Jeff Sharkeya1ef1be2012-07-23 13:19:46 -0700491 mNetTrackers[netType].startMonitoring(context, mTrackerHandler);
Jaikumar Ganesh0db51a02010-12-21 22:31:44 -0800492 break;
tk.mun093f55c2011-10-13 22:51:57 +0900493 case ConnectivityManager.TYPE_WIMAX:
494 mNetTrackers[netType] = makeWimaxStateTracker();
Robert Greenwalte72c4ac2011-11-08 10:12:25 -0800495 if (mNetTrackers[netType]!= null) {
Jeff Sharkeya1ef1be2012-07-23 13:19:46 -0700496 mNetTrackers[netType].startMonitoring(context, mTrackerHandler);
tk.mun093f55c2011-10-13 22:51:57 +0900497 }
498 break;
Benoit Goby211b5692010-12-22 14:29:40 -0800499 case ConnectivityManager.TYPE_ETHERNET:
500 mNetTrackers[netType] = EthernetDataTracker.getInstance();
Jeff Sharkeya1ef1be2012-07-23 13:19:46 -0700501 mNetTrackers[netType].startMonitoring(context, mTrackerHandler);
Benoit Goby211b5692010-12-22 14:29:40 -0800502 break;
Robert Greenwaltec05b3c2009-10-30 14:17:42 -0700503 default:
Wink Savillee70c6f52010-12-03 12:01:38 -0800504 loge("Trying to create a DataStateTracker for an unknown radio type " +
Wink Savillef2a62832011-04-07 14:23:45 -0700505 mNetConfigs[netType].radio);
Robert Greenwaltec05b3c2009-10-30 14:17:42 -0700506 continue;
507 }
Robert Greenwalte8d2a4a2011-07-14 14:28:05 -0700508 mCurrentLinkProperties[netType] = null;
Robert Greenwaltcafd8582011-11-10 16:55:20 -0800509 if (mNetTrackers[netType] != null && mNetConfigs[netType].isDefault()) {
510 mNetTrackers[netType].reconnect();
511 }
Robert Greenwaltec05b3c2009-10-30 14:17:42 -0700512 }
Robert Greenwalt0c4828c2010-01-26 11:40:34 -0800513
Chia-chi Yeh4df51322011-05-11 16:35:13 -0700514 IBinder b = ServiceManager.getService(Context.NETWORKMANAGEMENT_SERVICE);
515 INetworkManagementService nmService = INetworkManagementService.Stub.asInterface(b);
516
Robert Greenwalt49c75d32011-11-02 14:37:19 -0700517 mTethering = new Tethering(mContext, nmService, statsService, this, mHandler.getLooper());
Robert Greenwalt33cdcdf2011-06-02 17:30:47 -0700518 mTetheringConfigValid = ((mTethering.getTetherableUsbRegexs().length != 0 ||
Danica Chang96567052010-08-11 14:54:43 -0700519 mTethering.getTetherableWifiRegexs().length != 0 ||
520 mTethering.getTetherableBluetoothRegexs().length != 0) &&
Robert Greenwalt33cdcdf2011-06-02 17:30:47 -0700521 mTethering.getUpstreamIfaceTypes().length != 0);
Robert Greenwaltf1b66e12010-02-25 12:29:30 -0800522
Chia-chi Yeh9a4ad7d2011-05-23 17:26:46 -0700523 mVpn = new Vpn(mContext, new VpnCallback());
524
Chia-chi Yehf3204aa2011-05-23 15:08:29 -0700525 try {
526 nmService.registerObserver(mTethering);
Chia-chi Yeh9a4ad7d2011-05-23 17:26:46 -0700527 nmService.registerObserver(mVpn);
Jeff Sharkey60e6c1a2012-08-05 14:29:23 -0700528 nmService.registerObserver(mDataActivityObserver);
Chia-chi Yehf3204aa2011-05-23 15:08:29 -0700529 } catch (RemoteException e) {
530 loge("Error registering observer :" + e);
531 }
532
Robert Greenwalt0e80be12010-09-20 14:35:25 -0700533 if (DBG) {
534 mInetLog = new ArrayList();
535 }
Robert Greenwaltc3c5f862010-10-11 16:00:27 -0700536
537 mSettingsObserver = new SettingsObserver(mHandler, EVENT_APPLY_GLOBAL_HTTP_PROXY);
538 mSettingsObserver.observe(mContext);
Robert Greenwalt6f7c6092010-12-02 11:31:00 -0800539
540 loadGlobalProxy();
The Android Open Source Project28527d22009-03-03 19:31:44 -0800541 }
Jeff Sharkeya1ef1be2012-07-23 13:19:46 -0700542
543 private NetworkStateTracker makeWimaxStateTracker() {
544 // Initialize Wimax
tk.mun093f55c2011-10-13 22:51:57 +0900545 DexClassLoader wimaxClassLoader;
546 Class wimaxStateTrackerClass = null;
547 Class wimaxServiceClass = null;
548 Class wimaxManagerClass;
549 String wimaxJarLocation;
550 String wimaxLibLocation;
551 String wimaxManagerClassName;
552 String wimaxServiceClassName;
553 String wimaxStateTrackerClassName;
The Android Open Source Project28527d22009-03-03 19:31:44 -0800554
tk.mun093f55c2011-10-13 22:51:57 +0900555 NetworkStateTracker wimaxStateTracker = null;
556
557 boolean isWimaxEnabled = mContext.getResources().getBoolean(
558 com.android.internal.R.bool.config_wimaxEnabled);
559
560 if (isWimaxEnabled) {
561 try {
562 wimaxJarLocation = mContext.getResources().getString(
563 com.android.internal.R.string.config_wimaxServiceJarLocation);
564 wimaxLibLocation = mContext.getResources().getString(
565 com.android.internal.R.string.config_wimaxNativeLibLocation);
566 wimaxManagerClassName = mContext.getResources().getString(
567 com.android.internal.R.string.config_wimaxManagerClassname);
568 wimaxServiceClassName = mContext.getResources().getString(
569 com.android.internal.R.string.config_wimaxServiceClassname);
570 wimaxStateTrackerClassName = mContext.getResources().getString(
571 com.android.internal.R.string.config_wimaxStateTrackerClassname);
572
573 log("wimaxJarLocation: " + wimaxJarLocation);
574 wimaxClassLoader = new DexClassLoader(wimaxJarLocation,
575 new ContextWrapper(mContext).getCacheDir().getAbsolutePath(),
576 wimaxLibLocation, ClassLoader.getSystemClassLoader());
577
578 try {
579 wimaxManagerClass = wimaxClassLoader.loadClass(wimaxManagerClassName);
580 wimaxStateTrackerClass = wimaxClassLoader.loadClass(wimaxStateTrackerClassName);
581 wimaxServiceClass = wimaxClassLoader.loadClass(wimaxServiceClassName);
582 } catch (ClassNotFoundException ex) {
583 loge("Exception finding Wimax classes: " + ex.toString());
584 return null;
585 }
586 } catch(Resources.NotFoundException ex) {
587 loge("Wimax Resources does not exist!!! ");
588 return null;
589 }
590
591 try {
592 log("Starting Wimax Service... ");
593
594 Constructor wmxStTrkrConst = wimaxStateTrackerClass.getConstructor
595 (new Class[] {Context.class, Handler.class});
596 wimaxStateTracker = (NetworkStateTracker)wmxStTrkrConst.newInstance(mContext,
Jeff Sharkeya1ef1be2012-07-23 13:19:46 -0700597 mTrackerHandler);
tk.mun093f55c2011-10-13 22:51:57 +0900598
599 Constructor wmxSrvConst = wimaxServiceClass.getDeclaredConstructor
600 (new Class[] {Context.class, wimaxStateTrackerClass});
601 wmxSrvConst.setAccessible(true);
602 IBinder svcInvoker = (IBinder)wmxSrvConst.newInstance(mContext, wimaxStateTracker);
603 wmxSrvConst.setAccessible(false);
604
605 ServiceManager.addService(WimaxManagerConstants.WIMAX_SERVICE, svcInvoker);
606
607 } catch(Exception ex) {
608 loge("Exception creating Wimax classes: " + ex.toString());
609 return null;
610 }
611 } else {
612 loge("Wimax is not enabled or not added to the network attributes!!! ");
613 return null;
614 }
615
616 return wimaxStateTracker;
617 }
Jeff Sharkeya1ef1be2012-07-23 13:19:46 -0700618
The Android Open Source Project28527d22009-03-03 19:31:44 -0800619 /**
Robert Greenwalt0659da32009-07-16 17:21:39 -0700620 * Sets the preferred network.
The Android Open Source Project28527d22009-03-03 19:31:44 -0800621 * @param preference the new preference
622 */
Robert Greenwalt6a2db8a2010-09-23 10:05:56 -0700623 public void setNetworkPreference(int preference) {
The Android Open Source Project28527d22009-03-03 19:31:44 -0800624 enforceChangePermission();
Robert Greenwalt6a2db8a2010-09-23 10:05:56 -0700625
Jeff Sharkeya1ef1be2012-07-23 13:19:46 -0700626 mHandler.sendMessage(
627 mHandler.obtainMessage(EVENT_SET_NETWORK_PREFERENCE, preference, 0));
The Android Open Source Project28527d22009-03-03 19:31:44 -0800628 }
629
630 public int getNetworkPreference() {
631 enforceAccessPermission();
Robert Greenwalt6a2db8a2010-09-23 10:05:56 -0700632 int preference;
633 synchronized(this) {
634 preference = mNetworkPreference;
635 }
636 return preference;
The Android Open Source Project28527d22009-03-03 19:31:44 -0800637 }
638
Robert Greenwalt6a2db8a2010-09-23 10:05:56 -0700639 private void handleSetNetworkPreference(int preference) {
640 if (ConnectivityManager.isNetworkTypeValid(preference) &&
Robert Greenwalt34848c02011-03-25 13:09:25 -0700641 mNetConfigs[preference] != null &&
642 mNetConfigs[preference].isDefault()) {
Robert Greenwalt6a2db8a2010-09-23 10:05:56 -0700643 if (mNetworkPreference != preference) {
644 final ContentResolver cr = mContext.getContentResolver();
645 Settings.Secure.putInt(cr, Settings.Secure.NETWORK_PREFERENCE, preference);
646 synchronized(this) {
647 mNetworkPreference = preference;
648 }
649 enforcePreference();
650 }
651 }
The Android Open Source Project28527d22009-03-03 19:31:44 -0800652 }
Robert Greenwalt0659da32009-07-16 17:21:39 -0700653
Wink Saville4f0de1e2011-08-04 15:01:58 -0700654 private int getConnectivityChangeDelay() {
655 final ContentResolver cr = mContext.getContentResolver();
656
657 /** Check system properties for the default value then use secure settings value, if any. */
658 int defaultDelay = SystemProperties.getInt(
659 "conn." + Settings.Secure.CONNECTIVITY_CHANGE_DELAY,
660 Settings.Secure.CONNECTIVITY_CHANGE_DELAY_DEFAULT);
661 return Settings.Secure.getInt(cr, Settings.Secure.CONNECTIVITY_CHANGE_DELAY,
662 defaultDelay);
663 }
664
The Android Open Source Project28527d22009-03-03 19:31:44 -0800665 private int getPersistedNetworkPreference() {
666 final ContentResolver cr = mContext.getContentResolver();
667
668 final int networkPrefSetting = Settings.Secure
669 .getInt(cr, Settings.Secure.NETWORK_PREFERENCE, -1);
670 if (networkPrefSetting != -1) {
671 return networkPrefSetting;
672 }
673
674 return ConnectivityManager.DEFAULT_NETWORK_PREFERENCE;
675 }
Robert Greenwalt0659da32009-07-16 17:21:39 -0700676
The Android Open Source Project28527d22009-03-03 19:31:44 -0800677 /**
Robert Greenwalt0659da32009-07-16 17:21:39 -0700678 * Make the state of network connectivity conform to the preference settings
The Android Open Source Project28527d22009-03-03 19:31:44 -0800679 * In this method, we only tear down a non-preferred network. Establishing
680 * a connection to the preferred network is taken care of when we handle
681 * the disconnect event from the non-preferred network
682 * (see {@link #handleDisconnect(NetworkInfo)}).
683 */
684 private void enforcePreference() {
Robert Greenwalt2034b912009-08-12 16:08:25 -0700685 if (mNetTrackers[mNetworkPreference].getNetworkInfo().isConnected())
The Android Open Source Project28527d22009-03-03 19:31:44 -0800686 return;
687
Robert Greenwalt2034b912009-08-12 16:08:25 -0700688 if (!mNetTrackers[mNetworkPreference].isAvailable())
689 return;
The Android Open Source Project28527d22009-03-03 19:31:44 -0800690
Robert Greenwalt2034b912009-08-12 16:08:25 -0700691 for (int t=0; t <= ConnectivityManager.MAX_RADIO_TYPE; t++) {
Robert Greenwaltec05b3c2009-10-30 14:17:42 -0700692 if (t != mNetworkPreference && mNetTrackers[t] != null &&
Robert Greenwalt2034b912009-08-12 16:08:25 -0700693 mNetTrackers[t].getNetworkInfo().isConnected()) {
Robert Greenwaltf3f045b2009-08-20 15:25:14 -0700694 if (DBG) {
Wink Savillee70c6f52010-12-03 12:01:38 -0800695 log("tearing down " + mNetTrackers[t].getNetworkInfo() +
Robert Greenwaltf3f045b2009-08-20 15:25:14 -0700696 " in enforcePreference");
697 }
Robert Greenwalt2034b912009-08-12 16:08:25 -0700698 teardown(mNetTrackers[t]);
The Android Open Source Project28527d22009-03-03 19:31:44 -0800699 }
700 }
701 }
702
703 private boolean teardown(NetworkStateTracker netTracker) {
704 if (netTracker.teardown()) {
705 netTracker.setTeardownRequested(true);
706 return true;
707 } else {
708 return false;
709 }
710 }
711
712 /**
Jeff Sharkeya47d7a12011-06-16 15:07:48 -0700713 * Check if UID should be blocked from using the network represented by the
714 * given {@link NetworkStateTracker}.
Jeff Sharkey921ebf22011-05-19 17:12:49 -0700715 */
Jeff Sharkeya47d7a12011-06-16 15:07:48 -0700716 private boolean isNetworkBlocked(NetworkStateTracker tracker, int uid) {
717 final String iface = tracker.getLinkProperties().getInterfaceName();
Jeff Sharkey21062e72011-05-28 20:56:34 -0700718
Jeff Sharkeya47d7a12011-06-16 15:07:48 -0700719 final boolean networkCostly;
720 final int uidRules;
721 synchronized (mRulesLock) {
722 networkCostly = mMeteredIfaces.contains(iface);
723 uidRules = mUidRules.get(uid, RULE_ALLOW_ALL);
Jeff Sharkey921ebf22011-05-19 17:12:49 -0700724 }
Jeff Sharkeya47d7a12011-06-16 15:07:48 -0700725
726 if (networkCostly && (uidRules & RULE_REJECT_METERED) != 0) {
727 return true;
728 }
729
730 // no restrictive rules; network is visible
731 return false;
Jeff Sharkey921ebf22011-05-19 17:12:49 -0700732 }
733
734 /**
Jeff Sharkeya47d7a12011-06-16 15:07:48 -0700735 * Return a filtered {@link NetworkInfo}, potentially marked
736 * {@link DetailedState#BLOCKED} based on
737 * {@link #isNetworkBlocked(NetworkStateTracker, int)}.
Jeff Sharkey921ebf22011-05-19 17:12:49 -0700738 */
Jeff Sharkeya47d7a12011-06-16 15:07:48 -0700739 private NetworkInfo getFilteredNetworkInfo(NetworkStateTracker tracker, int uid) {
740 NetworkInfo info = tracker.getNetworkInfo();
741 if (isNetworkBlocked(tracker, uid)) {
Jeff Sharkey21062e72011-05-28 20:56:34 -0700742 // network is blocked; clone and override state
743 info = new NetworkInfo(info);
744 info.setDetailedState(DetailedState.BLOCKED, null, null);
Jeff Sharkey921ebf22011-05-19 17:12:49 -0700745 }
Jeff Sharkey21062e72011-05-28 20:56:34 -0700746 return info;
Jeff Sharkey921ebf22011-05-19 17:12:49 -0700747 }
748
749 /**
The Android Open Source Project28527d22009-03-03 19:31:44 -0800750 * Return NetworkInfo for the active (i.e., connected) network interface.
751 * It is assumed that at most one network is active at a time. If more
752 * than one is active, it is indeterminate which will be returned.
Robert Greenwalt0659da32009-07-16 17:21:39 -0700753 * @return the info for the active network, or {@code null} if none is
754 * active
The Android Open Source Project28527d22009-03-03 19:31:44 -0800755 */
Jeff Sharkey921ebf22011-05-19 17:12:49 -0700756 @Override
The Android Open Source Project28527d22009-03-03 19:31:44 -0800757 public NetworkInfo getActiveNetworkInfo() {
Jeff Sharkey921ebf22011-05-19 17:12:49 -0700758 enforceAccessPermission();
759 final int uid = Binder.getCallingUid();
760 return getNetworkInfo(mActiveDefaultNetwork, uid);
The Android Open Source Project28527d22009-03-03 19:31:44 -0800761 }
762
Jeff Sharkey921ebf22011-05-19 17:12:49 -0700763 @Override
764 public NetworkInfo getActiveNetworkInfoForUid(int uid) {
765 enforceConnectivityInternalPermission();
766 return getNetworkInfo(mActiveDefaultNetwork, uid);
767 }
768
769 @Override
The Android Open Source Project28527d22009-03-03 19:31:44 -0800770 public NetworkInfo getNetworkInfo(int networkType) {
771 enforceAccessPermission();
Jeff Sharkey921ebf22011-05-19 17:12:49 -0700772 final int uid = Binder.getCallingUid();
773 return getNetworkInfo(networkType, uid);
The Android Open Source Project28527d22009-03-03 19:31:44 -0800774 }
775
Jeff Sharkey921ebf22011-05-19 17:12:49 -0700776 private NetworkInfo getNetworkInfo(int networkType, int uid) {
777 NetworkInfo info = null;
778 if (isNetworkTypeValid(networkType)) {
779 final NetworkStateTracker tracker = mNetTrackers[networkType];
780 if (tracker != null) {
Jeff Sharkeya47d7a12011-06-16 15:07:48 -0700781 info = getFilteredNetworkInfo(tracker, uid);
Jeff Sharkey921ebf22011-05-19 17:12:49 -0700782 }
783 }
784 return info;
785 }
786
787 @Override
The Android Open Source Project28527d22009-03-03 19:31:44 -0800788 public NetworkInfo[] getAllNetworkInfo() {
789 enforceAccessPermission();
Jeff Sharkey921ebf22011-05-19 17:12:49 -0700790 final int uid = Binder.getCallingUid();
Jeff Sharkey21062e72011-05-28 20:56:34 -0700791 final ArrayList<NetworkInfo> result = Lists.newArrayList();
Jeff Sharkeya47d7a12011-06-16 15:07:48 -0700792 synchronized (mRulesLock) {
Jeff Sharkey921ebf22011-05-19 17:12:49 -0700793 for (NetworkStateTracker tracker : mNetTrackers) {
794 if (tracker != null) {
Jeff Sharkeya47d7a12011-06-16 15:07:48 -0700795 result.add(getFilteredNetworkInfo(tracker, uid));
Jeff Sharkey921ebf22011-05-19 17:12:49 -0700796 }
797 }
The Android Open Source Project28527d22009-03-03 19:31:44 -0800798 }
Jeff Sharkey21062e72011-05-28 20:56:34 -0700799 return result.toArray(new NetworkInfo[result.size()]);
The Android Open Source Project28527d22009-03-03 19:31:44 -0800800 }
801
Robert Greenwalt0114f6e2011-08-31 11:46:42 -0700802 @Override
803 public boolean isNetworkSupported(int networkType) {
804 enforceAccessPermission();
805 return (isNetworkTypeValid(networkType) && (mNetTrackers[networkType] != null));
806 }
807
Robert Greenwalt9f0ee4f2010-09-14 09:18:02 -0700808 /**
809 * Return LinkProperties for the active (i.e., connected) default
810 * network interface. It is assumed that at most one default network
811 * is active at a time. If more than one is active, it is indeterminate
812 * which will be returned.
813 * @return the ip properties for the active network, or {@code null} if
814 * none is active
815 */
Jeff Sharkey921ebf22011-05-19 17:12:49 -0700816 @Override
Robert Greenwalt9f0ee4f2010-09-14 09:18:02 -0700817 public LinkProperties getActiveLinkProperties() {
Robert Greenwalte1544bb2011-05-20 12:23:41 -0700818 return getLinkProperties(mActiveDefaultNetwork);
Robert Greenwalt9f0ee4f2010-09-14 09:18:02 -0700819 }
820
Jeff Sharkey921ebf22011-05-19 17:12:49 -0700821 @Override
Robert Greenwalt9f0ee4f2010-09-14 09:18:02 -0700822 public LinkProperties getLinkProperties(int networkType) {
823 enforceAccessPermission();
Jeff Sharkey921ebf22011-05-19 17:12:49 -0700824 if (isNetworkTypeValid(networkType)) {
825 final NetworkStateTracker tracker = mNetTrackers[networkType];
826 if (tracker != null) {
827 return tracker.getLinkProperties();
828 }
Robert Greenwalt9f0ee4f2010-09-14 09:18:02 -0700829 }
830 return null;
831 }
832
Jeff Sharkey21062e72011-05-28 20:56:34 -0700833 @Override
834 public NetworkState[] getAllNetworkState() {
835 enforceAccessPermission();
836 final int uid = Binder.getCallingUid();
837 final ArrayList<NetworkState> result = Lists.newArrayList();
Jeff Sharkeya47d7a12011-06-16 15:07:48 -0700838 synchronized (mRulesLock) {
Jeff Sharkey21062e72011-05-28 20:56:34 -0700839 for (NetworkStateTracker tracker : mNetTrackers) {
840 if (tracker != null) {
Jeff Sharkeya47d7a12011-06-16 15:07:48 -0700841 final NetworkInfo info = getFilteredNetworkInfo(tracker, uid);
Jeff Sharkey21062e72011-05-28 20:56:34 -0700842 result.add(new NetworkState(
843 info, tracker.getLinkProperties(), tracker.getLinkCapabilities()));
844 }
845 }
846 }
847 return result.toArray(new NetworkState[result.size()]);
848 }
849
Jeff Sharkey66fa9682011-08-02 17:22:34 -0700850 private NetworkState getNetworkStateUnchecked(int networkType) {
851 if (isNetworkTypeValid(networkType)) {
852 final NetworkStateTracker tracker = mNetTrackers[networkType];
853 if (tracker != null) {
854 return new NetworkState(tracker.getNetworkInfo(), tracker.getLinkProperties(),
855 tracker.getLinkCapabilities());
856 }
857 }
858 return null;
859 }
860
861 @Override
862 public NetworkQuotaInfo getActiveNetworkQuotaInfo() {
863 enforceAccessPermission();
Jeff Sharkey3022ee12012-04-17 12:23:40 -0700864
865 final long token = Binder.clearCallingIdentity();
866 try {
867 final NetworkState state = getNetworkStateUnchecked(mActiveDefaultNetwork);
868 if (state != null) {
869 try {
870 return mPolicyManager.getNetworkQuotaInfo(state);
871 } catch (RemoteException e) {
872 }
Jeff Sharkey66fa9682011-08-02 17:22:34 -0700873 }
Jeff Sharkey3022ee12012-04-17 12:23:40 -0700874 return null;
875 } finally {
876 Binder.restoreCallingIdentity(token);
Jeff Sharkey66fa9682011-08-02 17:22:34 -0700877 }
Jeff Sharkey66fa9682011-08-02 17:22:34 -0700878 }
879
Jeff Sharkeyd00b1302012-04-12 18:34:54 -0700880 @Override
881 public boolean isActiveNetworkMetered() {
882 enforceAccessPermission();
Jeff Sharkey3022ee12012-04-17 12:23:40 -0700883 final long token = Binder.clearCallingIdentity();
884 try {
Jeff Sharkeyedf85d42012-04-30 15:47:05 -0700885 return isNetworkMeteredUnchecked(mActiveDefaultNetwork);
Jeff Sharkey3022ee12012-04-17 12:23:40 -0700886 } finally {
887 Binder.restoreCallingIdentity(token);
Jeff Sharkeyd00b1302012-04-12 18:34:54 -0700888 }
Jeff Sharkeyd00b1302012-04-12 18:34:54 -0700889 }
890
Jeff Sharkeyedf85d42012-04-30 15:47:05 -0700891 private boolean isNetworkMeteredUnchecked(int networkType) {
892 final NetworkState state = getNetworkStateUnchecked(networkType);
893 if (state != null) {
894 try {
895 return mPolicyManager.isNetworkMetered(state);
896 } catch (RemoteException e) {
897 }
898 }
899 return false;
900 }
901
The Android Open Source Project28527d22009-03-03 19:31:44 -0800902 public boolean setRadios(boolean turnOn) {
903 boolean result = true;
904 enforceChangePermission();
905 for (NetworkStateTracker t : mNetTrackers) {
Robert Greenwaltec05b3c2009-10-30 14:17:42 -0700906 if (t != null) result = t.setRadio(turnOn) && result;
The Android Open Source Project28527d22009-03-03 19:31:44 -0800907 }
908 return result;
909 }
910
911 public boolean setRadio(int netType, boolean turnOn) {
912 enforceChangePermission();
913 if (!ConnectivityManager.isNetworkTypeValid(netType)) {
914 return false;
915 }
916 NetworkStateTracker tracker = mNetTrackers[netType];
917 return tracker != null && tracker.setRadio(turnOn);
918 }
919
Jeff Sharkey60e6c1a2012-08-05 14:29:23 -0700920 private INetworkManagementEventObserver mDataActivityObserver = new BaseNetworkObserver() {
921 @Override
Haoyu Baib5da5752012-06-20 14:29:57 -0700922 public void interfaceClassDataActivityChanged(String label, boolean active) {
923 int deviceType = Integer.parseInt(label);
924 sendDataActivityBroadcast(deviceType, active);
925 }
Jeff Sharkey60e6c1a2012-08-05 14:29:23 -0700926 };
Haoyu Baib5da5752012-06-20 14:29:57 -0700927
Robert Greenwaltaffc3a12009-09-27 17:27:04 -0700928 /**
929 * Used to notice when the calling process dies so we can self-expire
930 *
931 * Also used to know if the process has cleaned up after itself when
932 * our auto-expire timer goes off. The timer has a link to an object.
933 *
934 */
Robert Greenwalt2034b912009-08-12 16:08:25 -0700935 private class FeatureUser implements IBinder.DeathRecipient {
936 int mNetworkType;
937 String mFeature;
938 IBinder mBinder;
939 int mPid;
940 int mUid;
Robert Greenwalt3eeb6032009-12-21 18:24:07 -0800941 long mCreateTime;
Robert Greenwalt2034b912009-08-12 16:08:25 -0700942
943 FeatureUser(int type, String feature, IBinder binder) {
944 super();
945 mNetworkType = type;
946 mFeature = feature;
947 mBinder = binder;
948 mPid = getCallingPid();
949 mUid = getCallingUid();
Robert Greenwalt3eeb6032009-12-21 18:24:07 -0800950 mCreateTime = System.currentTimeMillis();
Robert Greenwaltaffc3a12009-09-27 17:27:04 -0700951
Robert Greenwalt2034b912009-08-12 16:08:25 -0700952 try {
953 mBinder.linkToDeath(this, 0);
954 } catch (RemoteException e) {
955 binderDied();
956 }
957 }
958
959 void unlinkDeathRecipient() {
960 mBinder.unlinkToDeath(this, 0);
961 }
962
963 public void binderDied() {
Wink Savillee70c6f52010-12-03 12:01:38 -0800964 log("ConnectivityService FeatureUser binderDied(" +
Robert Greenwalt3eeb6032009-12-21 18:24:07 -0800965 mNetworkType + ", " + mFeature + ", " + mBinder + "), created " +
966 (System.currentTimeMillis() - mCreateTime) + " mSec ago");
Robert Greenwaltaffc3a12009-09-27 17:27:04 -0700967 stopUsingNetworkFeature(this, false);
Robert Greenwalt2034b912009-08-12 16:08:25 -0700968 }
969
Robert Greenwaltaffc3a12009-09-27 17:27:04 -0700970 public void expire() {
Robert Greenwalt78f28112011-08-02 17:18:41 -0700971 if (VDBG) {
972 log("ConnectivityService FeatureUser expire(" +
973 mNetworkType + ", " + mFeature + ", " + mBinder +"), created " +
974 (System.currentTimeMillis() - mCreateTime) + " mSec ago");
975 }
Robert Greenwaltaffc3a12009-09-27 17:27:04 -0700976 stopUsingNetworkFeature(this, false);
977 }
Robert Greenwalt3eeb6032009-12-21 18:24:07 -0800978
Kazuhiro Ondocae18f12011-07-19 11:23:37 -0500979 public boolean isSameUser(FeatureUser u) {
980 if (u == null) return false;
981
982 return isSameUser(u.mPid, u.mUid, u.mNetworkType, u.mFeature);
983 }
984
985 public boolean isSameUser(int pid, int uid, int networkType, String feature) {
986 if ((mPid == pid) && (mUid == uid) && (mNetworkType == networkType) &&
987 TextUtils.equals(mFeature, feature)) {
988 return true;
989 }
990 return false;
991 }
992
Robert Greenwalt3eeb6032009-12-21 18:24:07 -0800993 public String toString() {
994 return "FeatureUser("+mNetworkType+","+mFeature+","+mPid+","+mUid+"), created " +
995 (System.currentTimeMillis() - mCreateTime) + " mSec ago";
996 }
Robert Greenwalt2034b912009-08-12 16:08:25 -0700997 }
998
Robert Greenwaltaffc3a12009-09-27 17:27:04 -0700999 // javadoc from interface
Robert Greenwalt2034b912009-08-12 16:08:25 -07001000 public int startUsingNetworkFeature(int networkType, String feature,
1001 IBinder binder) {
Robert Greenwalt1e6991e2012-05-22 16:07:46 -07001002 long startTime = 0;
1003 if (DBG) {
1004 startTime = SystemClock.elapsedRealtime();
1005 }
Wink Savillea7d56572011-09-21 11:05:43 -07001006 if (VDBG) {
Jeff Sharkeyedf85d42012-04-30 15:47:05 -07001007 log("startUsingNetworkFeature for net " + networkType + ": " + feature + ", uid="
1008 + Binder.getCallingUid());
Robert Greenwalt2034b912009-08-12 16:08:25 -07001009 }
The Android Open Source Project28527d22009-03-03 19:31:44 -08001010 enforceChangePermission();
Robert Greenwalt1e6991e2012-05-22 16:07:46 -07001011 try {
1012 if (!ConnectivityManager.isNetworkTypeValid(networkType) ||
1013 mNetConfigs[networkType] == null) {
Wink Saville64e3f782012-07-10 12:37:54 -07001014 return PhoneConstants.APN_REQUEST_FAILED;
Robert Greenwalt1e6991e2012-05-22 16:07:46 -07001015 }
Robert Greenwalt2034b912009-08-12 16:08:25 -07001016
Robert Greenwalt1e6991e2012-05-22 16:07:46 -07001017 FeatureUser f = new FeatureUser(networkType, feature, binder);
Robert Greenwalt2034b912009-08-12 16:08:25 -07001018
Robert Greenwalt1e6991e2012-05-22 16:07:46 -07001019 // TODO - move this into individual networktrackers
1020 int usedNetworkType = convertFeatureToNetworkType(networkType, feature);
Robert Greenwalt6cac0742011-06-21 17:26:14 -07001021
Robert Greenwalt1e6991e2012-05-22 16:07:46 -07001022 if (mProtectedNetworks.contains(usedNetworkType)) {
1023 enforceConnectivityInternalPermission();
1024 }
Robert Greenwalt6cac0742011-06-21 17:26:14 -07001025
Robert Greenwalt1e6991e2012-05-22 16:07:46 -07001026 // if UID is restricted, don't allow them to bring up metered APNs
1027 final boolean networkMetered = isNetworkMeteredUnchecked(usedNetworkType);
1028 final int uidRules;
1029 synchronized (mRulesLock) {
1030 uidRules = mUidRules.get(Binder.getCallingUid(), RULE_ALLOW_ALL);
1031 }
1032 if (networkMetered && (uidRules & RULE_REJECT_METERED) != 0) {
Wink Saville64e3f782012-07-10 12:37:54 -07001033 return PhoneConstants.APN_REQUEST_FAILED;
Robert Greenwalt1e6991e2012-05-22 16:07:46 -07001034 }
Jeff Sharkeyedf85d42012-04-30 15:47:05 -07001035
Robert Greenwalt1e6991e2012-05-22 16:07:46 -07001036 NetworkStateTracker network = mNetTrackers[usedNetworkType];
1037 if (network != null) {
1038 Integer currentPid = new Integer(getCallingPid());
1039 if (usedNetworkType != networkType) {
1040 NetworkInfo ni = network.getNetworkInfo();
Robert Greenwalt2034b912009-08-12 16:08:25 -07001041
Robert Greenwalt1e6991e2012-05-22 16:07:46 -07001042 if (ni.isAvailable() == false) {
1043 if (!TextUtils.equals(feature,Phone.FEATURE_ENABLE_DUN_ALWAYS)) {
1044 if (DBG) log("special network not available ni=" + ni.getTypeName());
Wink Saville64e3f782012-07-10 12:37:54 -07001045 return PhoneConstants.APN_TYPE_NOT_AVAILABLE;
Robert Greenwalt1e6991e2012-05-22 16:07:46 -07001046 } else {
1047 // else make the attempt anyway - probably giving REQUEST_STARTED below
1048 if (DBG) {
1049 log("special network not available, but try anyway ni=" +
1050 ni.getTypeName());
Kazuhiro Ondocae18f12011-07-19 11:23:37 -05001051 }
1052 }
1053 }
1054
Robert Greenwalt1e6991e2012-05-22 16:07:46 -07001055 int restoreTimer = getRestoreDefaultNetworkDelay(usedNetworkType);
Robert Greenwaltaffc3a12009-09-27 17:27:04 -07001056
Robert Greenwalt1e6991e2012-05-22 16:07:46 -07001057 synchronized(this) {
1058 boolean addToList = true;
1059 if (restoreTimer < 0) {
1060 // In case there is no timer is specified for the feature,
1061 // make sure we don't add duplicate entry with the same request.
1062 for (FeatureUser u : mFeatureUsers) {
1063 if (u.isSameUser(f)) {
1064 // Duplicate user is found. Do not add.
1065 addToList = false;
1066 break;
1067 }
1068 }
Robert Greenwalt7fa3bdb2011-12-13 15:26:02 -08001069 }
Robert Greenwalt1e6991e2012-05-22 16:07:46 -07001070
1071 if (addToList) mFeatureUsers.add(f);
1072 if (!mNetRequestersPids[usedNetworkType].contains(currentPid)) {
1073 // this gets used for per-pid dns when connected
1074 mNetRequestersPids[usedNetworkType].add(currentPid);
1075 }
Robert Greenwalt2034b912009-08-12 16:08:25 -07001076 }
Robert Greenwalt1e6991e2012-05-22 16:07:46 -07001077
1078 if (restoreTimer >= 0) {
1079 mHandler.sendMessageDelayed(mHandler.obtainMessage(
1080 EVENT_RESTORE_DEFAULT_NETWORK, f), restoreTimer);
1081 }
1082
1083 if ((ni.isConnectedOrConnecting() == true) &&
1084 !network.isTeardownRequested()) {
1085 if (ni.isConnected() == true) {
1086 final long token = Binder.clearCallingIdentity();
1087 try {
1088 // add the pid-specific dns
1089 handleDnsConfigurationChange(usedNetworkType);
1090 if (VDBG) log("special network already active");
1091 } finally {
1092 Binder.restoreCallingIdentity(token);
1093 }
Wink Saville64e3f782012-07-10 12:37:54 -07001094 return PhoneConstants.APN_ALREADY_ACTIVE;
Robert Greenwalt1e6991e2012-05-22 16:07:46 -07001095 }
1096 if (VDBG) log("special network already connecting");
Wink Saville64e3f782012-07-10 12:37:54 -07001097 return PhoneConstants.APN_REQUEST_STARTED;
Robert Greenwalt1e6991e2012-05-22 16:07:46 -07001098 }
1099
1100 // check if the radio in play can make another contact
1101 // assume if cannot for now
1102
1103 if (DBG) {
1104 log("startUsingNetworkFeature reconnecting to " + networkType + ": " +
1105 feature);
1106 }
1107 network.reconnect();
Wink Saville64e3f782012-07-10 12:37:54 -07001108 return PhoneConstants.APN_REQUEST_STARTED;
Robert Greenwalt1e6991e2012-05-22 16:07:46 -07001109 } else {
1110 // need to remember this unsupported request so we respond appropriately on stop
1111 synchronized(this) {
1112 mFeatureUsers.add(f);
1113 if (!mNetRequestersPids[usedNetworkType].contains(currentPid)) {
1114 // this gets used for per-pid dns when connected
1115 mNetRequestersPids[usedNetworkType].add(currentPid);
1116 }
Robert Greenwalt5364d752010-12-15 13:26:33 -08001117 }
Robert Greenwalt1e6991e2012-05-22 16:07:46 -07001118 return -1;
Robert Greenwalt5364d752010-12-15 13:26:33 -08001119 }
Robert Greenwalt2034b912009-08-12 16:08:25 -07001120 }
Wink Saville64e3f782012-07-10 12:37:54 -07001121 return PhoneConstants.APN_TYPE_NOT_AVAILABLE;
Robert Greenwalt1e6991e2012-05-22 16:07:46 -07001122 } finally {
1123 if (DBG) {
1124 final long execTime = SystemClock.elapsedRealtime() - startTime;
1125 if (execTime > 250) {
1126 loge("startUsingNetworkFeature took too long: " + execTime + "ms");
1127 } else {
1128 if (VDBG) log("startUsingNetworkFeature took " + execTime + "ms");
1129 }
1130 }
1131 }
The Android Open Source Project28527d22009-03-03 19:31:44 -08001132 }
1133
Robert Greenwaltaffc3a12009-09-27 17:27:04 -07001134 // javadoc from interface
The Android Open Source Project28527d22009-03-03 19:31:44 -08001135 public int stopUsingNetworkFeature(int networkType, String feature) {
Robert Greenwalt28f43012009-10-06 17:52:40 -07001136 enforceChangePermission();
1137
Robert Greenwaltaffc3a12009-09-27 17:27:04 -07001138 int pid = getCallingPid();
1139 int uid = getCallingUid();
1140
1141 FeatureUser u = null;
1142 boolean found = false;
1143
1144 synchronized(this) {
Kazuhiro Ondocae18f12011-07-19 11:23:37 -05001145 for (FeatureUser x : mFeatureUsers) {
1146 if (x.isSameUser(pid, uid, networkType, feature)) {
1147 u = x;
Robert Greenwaltaffc3a12009-09-27 17:27:04 -07001148 found = true;
1149 break;
1150 }
1151 }
1152 }
1153 if (found && u != null) {
1154 // stop regardless of how many other time this proc had called start
1155 return stopUsingNetworkFeature(u, true);
1156 } else {
1157 // none found!
Wink Savillea7d56572011-09-21 11:05:43 -07001158 if (VDBG) log("stopUsingNetworkFeature - not a live request, ignoring");
Robert Greenwaltaffc3a12009-09-27 17:27:04 -07001159 return 1;
1160 }
Robert Greenwalt2034b912009-08-12 16:08:25 -07001161 }
1162
Robert Greenwaltaffc3a12009-09-27 17:27:04 -07001163 private int stopUsingNetworkFeature(FeatureUser u, boolean ignoreDups) {
1164 int networkType = u.mNetworkType;
1165 String feature = u.mFeature;
1166 int pid = u.mPid;
1167 int uid = u.mUid;
1168
1169 NetworkStateTracker tracker = null;
1170 boolean callTeardown = false; // used to carry our decision outside of sync block
1171
Wink Savillea7d56572011-09-21 11:05:43 -07001172 if (VDBG) {
1173 log("stopUsingNetworkFeature: net " + networkType + ": " + feature);
Robert Greenwalt2034b912009-08-12 16:08:25 -07001174 }
Robert Greenwalt28f43012009-10-06 17:52:40 -07001175
The Android Open Source Project28527d22009-03-03 19:31:44 -08001176 if (!ConnectivityManager.isNetworkTypeValid(networkType)) {
Wink Savillea7d56572011-09-21 11:05:43 -07001177 if (DBG) {
1178 log("stopUsingNetworkFeature: net " + networkType + ": " + feature +
1179 ", net is invalid");
1180 }
The Android Open Source Project28527d22009-03-03 19:31:44 -08001181 return -1;
1182 }
Robert Greenwalt2034b912009-08-12 16:08:25 -07001183
Robert Greenwaltaffc3a12009-09-27 17:27:04 -07001184 // need to link the mFeatureUsers list with the mNetRequestersPids state in this
1185 // sync block
1186 synchronized(this) {
1187 // check if this process still has an outstanding start request
1188 if (!mFeatureUsers.contains(u)) {
Wink Savillea7d56572011-09-21 11:05:43 -07001189 if (VDBG) {
1190 log("stopUsingNetworkFeature: this process has no outstanding requests" +
1191 ", ignoring");
1192 }
Robert Greenwalt2034b912009-08-12 16:08:25 -07001193 return 1;
1194 }
Robert Greenwaltaffc3a12009-09-27 17:27:04 -07001195 u.unlinkDeathRecipient();
1196 mFeatureUsers.remove(mFeatureUsers.indexOf(u));
1197 // If we care about duplicate requests, check for that here.
1198 //
1199 // This is done to support the extension of a request - the app
1200 // can request we start the network feature again and renew the
1201 // auto-shutoff delay. Normal "stop" calls from the app though
1202 // do not pay attention to duplicate requests - in effect the
1203 // API does not refcount and a single stop will counter multiple starts.
1204 if (ignoreDups == false) {
Kazuhiro Ondocae18f12011-07-19 11:23:37 -05001205 for (FeatureUser x : mFeatureUsers) {
1206 if (x.isSameUser(u)) {
Wink Savillea7d56572011-09-21 11:05:43 -07001207 if (VDBG) log("stopUsingNetworkFeature: dup is found, ignoring");
Robert Greenwaltaffc3a12009-09-27 17:27:04 -07001208 return 1;
1209 }
1210 }
Robert Greenwalt2034b912009-08-12 16:08:25 -07001211 }
Robert Greenwaltaffc3a12009-09-27 17:27:04 -07001212
repo syncf5de5572011-07-29 23:55:49 -07001213 // TODO - move to individual network trackers
1214 int usedNetworkType = convertFeatureToNetworkType(networkType, feature);
1215
Robert Greenwaltaffc3a12009-09-27 17:27:04 -07001216 tracker = mNetTrackers[usedNetworkType];
Robert Greenwaltec05b3c2009-10-30 14:17:42 -07001217 if (tracker == null) {
Wink Savillea7d56572011-09-21 11:05:43 -07001218 if (DBG) {
1219 log("stopUsingNetworkFeature: net " + networkType + ": " + feature +
1220 " no known tracker for used net type " + usedNetworkType);
1221 }
Robert Greenwaltec05b3c2009-10-30 14:17:42 -07001222 return -1;
1223 }
1224 if (usedNetworkType != networkType) {
Robert Greenwaltaffc3a12009-09-27 17:27:04 -07001225 Integer currentPid = new Integer(pid);
Robert Greenwaltaffc3a12009-09-27 17:27:04 -07001226 mNetRequestersPids[usedNetworkType].remove(currentPid);
Robert Greenwalt0ca68a02009-12-17 14:54:59 -08001227 reassessPidDns(pid, true);
Robert Greenwaltaffc3a12009-09-27 17:27:04 -07001228 if (mNetRequestersPids[usedNetworkType].size() != 0) {
Wink Savillea7d56572011-09-21 11:05:43 -07001229 if (VDBG) {
1230 log("stopUsingNetworkFeature: net " + networkType + ": " + feature +
1231 " others still using it");
1232 }
Robert Greenwaltaffc3a12009-09-27 17:27:04 -07001233 return 1;
1234 }
1235 callTeardown = true;
Robert Greenwalt9f3be4c2011-01-10 11:58:31 -08001236 } else {
Wink Savillea7d56572011-09-21 11:05:43 -07001237 if (DBG) {
1238 log("stopUsingNetworkFeature: net " + networkType + ": " + feature +
1239 " not a known feature - dropping");
1240 }
Robert Greenwaltaffc3a12009-09-27 17:27:04 -07001241 }
1242 }
Wink Savillea7d56572011-09-21 11:05:43 -07001243
Robert Greenwaltaffc3a12009-09-27 17:27:04 -07001244 if (callTeardown) {
Wink Savillea7d56572011-09-21 11:05:43 -07001245 if (DBG) {
1246 log("stopUsingNetworkFeature: teardown net " + networkType + ": " + feature);
1247 }
Robert Greenwaltaffc3a12009-09-27 17:27:04 -07001248 tracker.teardown();
Robert Greenwalt2034b912009-08-12 16:08:25 -07001249 return 1;
1250 } else {
Robert Greenwaltd391e892010-05-18 10:52:51 -07001251 return -1;
Robert Greenwalt2034b912009-08-12 16:08:25 -07001252 }
The Android Open Source Project28527d22009-03-03 19:31:44 -08001253 }
1254
1255 /**
Robert Greenwalt7fe44cb2010-08-27 09:24:29 -07001256 * @deprecated use requestRouteToHostAddress instead
1257 *
The Android Open Source Project28527d22009-03-03 19:31:44 -08001258 * Ensure that a network route exists to deliver traffic to the specified
1259 * host via the specified network interface.
Robert Greenwalt0659da32009-07-16 17:21:39 -07001260 * @param networkType the type of the network over which traffic to the
1261 * specified host is to be routed
1262 * @param hostAddress the IP address of the host to which the route is
1263 * desired
The Android Open Source Project28527d22009-03-03 19:31:44 -08001264 * @return {@code true} on success, {@code false} on failure
1265 */
1266 public boolean requestRouteToHost(int networkType, int hostAddress) {
Robert Greenwalt7fe44cb2010-08-27 09:24:29 -07001267 InetAddress inetAddress = NetworkUtils.intToInetAddress(hostAddress);
1268
1269 if (inetAddress == null) {
1270 return false;
1271 }
1272
1273 return requestRouteToHostAddress(networkType, inetAddress.getAddress());
1274 }
1275
1276 /**
1277 * Ensure that a network route exists to deliver traffic to the specified
1278 * host via the specified network interface.
1279 * @param networkType the type of the network over which traffic to the
1280 * specified host is to be routed
1281 * @param hostAddress the IP address of the host to which the route is
1282 * desired
1283 * @return {@code true} on success, {@code false} on failure
1284 */
1285 public boolean requestRouteToHostAddress(int networkType, byte[] hostAddress) {
The Android Open Source Project28527d22009-03-03 19:31:44 -08001286 enforceChangePermission();
Robert Greenwalt6cac0742011-06-21 17:26:14 -07001287 if (mProtectedNetworks.contains(networkType)) {
1288 enforceConnectivityInternalPermission();
1289 }
1290
The Android Open Source Project28527d22009-03-03 19:31:44 -08001291 if (!ConnectivityManager.isNetworkTypeValid(networkType)) {
Robert Greenwalt7fa3bdb2011-12-13 15:26:02 -08001292 if (DBG) log("requestRouteToHostAddress on invalid network: " + networkType);
The Android Open Source Project28527d22009-03-03 19:31:44 -08001293 return false;
1294 }
1295 NetworkStateTracker tracker = mNetTrackers[networkType];
Robert Greenwalt4666ed02009-09-10 15:06:20 -07001296
Robert Greenwaltec05b3c2009-10-30 14:17:42 -07001297 if (tracker == null || !tracker.getNetworkInfo().isConnected() ||
1298 tracker.isTeardownRequested()) {
Robert Greenwalt78f28112011-08-02 17:18:41 -07001299 if (VDBG) {
Wink Savillee70c6f52010-12-03 12:01:38 -08001300 log("requestRouteToHostAddress on down network " +
Robert Greenwalt7fe44cb2010-08-27 09:24:29 -07001301 "(" + networkType + ") - dropped");
Robert Greenwalt4666ed02009-09-10 15:06:20 -07001302 }
1303 return false;
The Android Open Source Project28527d22009-03-03 19:31:44 -08001304 }
Robert Greenwalt7fa3bdb2011-12-13 15:26:02 -08001305 final long token = Binder.clearCallingIdentity();
Robert Greenwalta7dfbd32010-06-15 15:43:39 -07001306 try {
Robert Greenwalt7fe44cb2010-08-27 09:24:29 -07001307 InetAddress addr = InetAddress.getByAddress(hostAddress);
Robert Greenwalte8d2a4a2011-07-14 14:28:05 -07001308 LinkProperties lp = tracker.getLinkProperties();
Robert Greenwalt98107422011-07-22 11:55:33 -07001309 return addRouteToAddress(lp, addr);
Robert Greenwalt7fa3bdb2011-12-13 15:26:02 -08001310 } catch (UnknownHostException e) {
1311 if (DBG) log("requestRouteToHostAddress got " + e.toString());
1312 } finally {
1313 Binder.restoreCallingIdentity(token);
1314 }
Robert Greenwalta7dfbd32010-06-15 15:43:39 -07001315 return false;
Irfan Sheriff7f132d92010-06-09 15:39:36 -07001316 }
1317
Robert Greenwalt49c75d32011-11-02 14:37:19 -07001318 private boolean addRoute(LinkProperties p, RouteInfo r, boolean toDefaultTable) {
1319 return modifyRoute(p.getInterfaceName(), p, r, 0, ADD, toDefaultTable);
Robert Greenwaltbd492212011-05-06 17:10:53 -07001320 }
1321
Robert Greenwalt49c75d32011-11-02 14:37:19 -07001322 private boolean removeRoute(LinkProperties p, RouteInfo r, boolean toDefaultTable) {
1323 return modifyRoute(p.getInterfaceName(), p, r, 0, REMOVE, toDefaultTable);
Robert Greenwalte8d2a4a2011-07-14 14:28:05 -07001324 }
1325
Robert Greenwalt98107422011-07-22 11:55:33 -07001326 private boolean addRouteToAddress(LinkProperties lp, InetAddress addr) {
Robert Greenwalt49c75d32011-11-02 14:37:19 -07001327 return modifyRouteToAddress(lp, addr, ADD, TO_DEFAULT_TABLE);
Robert Greenwalt98107422011-07-22 11:55:33 -07001328 }
1329
1330 private boolean removeRouteToAddress(LinkProperties lp, InetAddress addr) {
Robert Greenwalt49c75d32011-11-02 14:37:19 -07001331 return modifyRouteToAddress(lp, addr, REMOVE, TO_DEFAULT_TABLE);
Robert Greenwalt98107422011-07-22 11:55:33 -07001332 }
1333
Robert Greenwalt49c75d32011-11-02 14:37:19 -07001334 private boolean modifyRouteToAddress(LinkProperties lp, InetAddress addr, boolean doAdd,
1335 boolean toDefaultTable) {
Robert Greenwalt98107422011-07-22 11:55:33 -07001336 RouteInfo bestRoute = RouteInfo.selectBestRoute(lp.getRoutes(), addr);
1337 if (bestRoute == null) {
1338 bestRoute = RouteInfo.makeHostRoute(addr);
1339 } else {
1340 if (bestRoute.getGateway().equals(addr)) {
1341 // if there is no better route, add the implied hostroute for our gateway
1342 bestRoute = RouteInfo.makeHostRoute(addr);
1343 } else {
1344 // if we will connect to this through another route, add a direct route
1345 // to it's gateway
1346 bestRoute = RouteInfo.makeHostRoute(addr, bestRoute.getGateway());
1347 }
1348 }
Robert Greenwalt49c75d32011-11-02 14:37:19 -07001349 return modifyRoute(lp.getInterfaceName(), lp, bestRoute, 0, doAdd, toDefaultTable);
Robert Greenwalt98107422011-07-22 11:55:33 -07001350 }
1351
Robert Greenwalte8d2a4a2011-07-14 14:28:05 -07001352 private boolean modifyRoute(String ifaceName, LinkProperties lp, RouteInfo r, int cycleCount,
Robert Greenwalt49c75d32011-11-02 14:37:19 -07001353 boolean doAdd, boolean toDefaultTable) {
Robert Greenwalt7fa3bdb2011-12-13 15:26:02 -08001354 if ((ifaceName == null) || (lp == null) || (r == null)) {
1355 if (DBG) log("modifyRoute got unexpected null: " + ifaceName + ", " + lp + ", " + r);
1356 return false;
1357 }
Robert Greenwalte8d2a4a2011-07-14 14:28:05 -07001358
1359 if (cycleCount > MAX_HOSTROUTE_CYCLE_COUNT) {
Robert Greenwalt49c75d32011-11-02 14:37:19 -07001360 loge("Error modifying route - too much recursion");
Robert Greenwalte8d2a4a2011-07-14 14:28:05 -07001361 return false;
1362 }
1363
1364 if (r.isHostRoute() == false) {
1365 RouteInfo bestRoute = RouteInfo.selectBestRoute(lp.getRoutes(), r.getGateway());
1366 if (bestRoute != null) {
Robert Greenwalt57c83bb2011-07-15 09:45:08 -07001367 if (bestRoute.getGateway().equals(r.getGateway())) {
1368 // if there is no better route, add the implied hostroute for our gateway
Robert Greenwalte8d2a4a2011-07-14 14:28:05 -07001369 bestRoute = RouteInfo.makeHostRoute(r.getGateway());
Robert Greenwalt57c83bb2011-07-15 09:45:08 -07001370 } else {
1371 // if we will connect to our gateway through another route, add a direct
1372 // route to it's gateway
1373 bestRoute = RouteInfo.makeHostRoute(r.getGateway(), bestRoute.getGateway());
Robert Greenwalte8d2a4a2011-07-14 14:28:05 -07001374 }
Robert Greenwalt49c75d32011-11-02 14:37:19 -07001375 modifyRoute(ifaceName, lp, bestRoute, cycleCount+1, doAdd, toDefaultTable);
Robert Greenwalte8d2a4a2011-07-14 14:28:05 -07001376 }
1377 }
1378 if (doAdd) {
Robert Greenwalt78f28112011-08-02 17:18:41 -07001379 if (VDBG) log("Adding " + r + " for interface " + ifaceName);
Robert Greenwalte8d2a4a2011-07-14 14:28:05 -07001380 try {
Robert Greenwalt49c75d32011-11-02 14:37:19 -07001381 if (toDefaultTable) {
1382 mAddedRoutes.add(r); // only track default table - only one apps can effect
1383 mNetd.addRoute(ifaceName, r);
1384 } else {
1385 mNetd.addSecondaryRoute(ifaceName, r);
1386 }
Robert Greenwalte8d2a4a2011-07-14 14:28:05 -07001387 } catch (Exception e) {
1388 // never crash - catch them all
Robert Greenwalt7fa3bdb2011-12-13 15:26:02 -08001389 if (DBG) loge("Exception trying to add a route: " + e);
Robert Greenwalte8d2a4a2011-07-14 14:28:05 -07001390 return false;
1391 }
1392 } else {
1393 // if we remove this one and there are no more like it, then refcount==0 and
1394 // we can remove it from the table
Robert Greenwalt49c75d32011-11-02 14:37:19 -07001395 if (toDefaultTable) {
1396 mAddedRoutes.remove(r);
1397 if (mAddedRoutes.contains(r) == false) {
1398 if (VDBG) log("Removing " + r + " for interface " + ifaceName);
1399 try {
1400 mNetd.removeRoute(ifaceName, r);
1401 } catch (Exception e) {
1402 // never crash - catch them all
Robert Greenwalt436db352012-04-23 18:00:37 -07001403 if (VDBG) loge("Exception trying to remove a route: " + e);
Robert Greenwalt49c75d32011-11-02 14:37:19 -07001404 return false;
1405 }
1406 } else {
1407 if (VDBG) log("not removing " + r + " as it's still in use");
1408 }
1409 } else {
Robert Greenwalt78f28112011-08-02 17:18:41 -07001410 if (VDBG) log("Removing " + r + " for interface " + ifaceName);
Robert Greenwalte8d2a4a2011-07-14 14:28:05 -07001411 try {
Robert Greenwalt49c75d32011-11-02 14:37:19 -07001412 mNetd.removeSecondaryRoute(ifaceName, r);
Robert Greenwalte8d2a4a2011-07-14 14:28:05 -07001413 } catch (Exception e) {
1414 // never crash - catch them all
Robert Greenwalt436db352012-04-23 18:00:37 -07001415 if (VDBG) loge("Exception trying to remove a route: " + e);
Robert Greenwalte8d2a4a2011-07-14 14:28:05 -07001416 return false;
1417 }
Robert Greenwalte8d2a4a2011-07-14 14:28:05 -07001418 }
1419 }
1420 return true;
The Android Open Source Project28527d22009-03-03 19:31:44 -08001421 }
1422
1423 /**
Robert Greenwalt1b21f6c2010-02-23 18:58:05 -08001424 * @see ConnectivityManager#getMobileDataEnabled()
1425 */
1426 public boolean getMobileDataEnabled() {
Wink Savilleb9024c62010-12-07 10:31:02 -08001427 // TODO: This detail should probably be in DataConnectionTracker's
1428 // which is where we store the value and maybe make this
1429 // asynchronous.
Robert Greenwalt1b21f6c2010-02-23 18:58:05 -08001430 enforceAccessPermission();
1431 boolean retVal = Settings.Secure.getInt(mContext.getContentResolver(),
1432 Settings.Secure.MOBILE_DATA, 1) == 1;
Robert Greenwalt78f28112011-08-02 17:18:41 -07001433 if (VDBG) log("getMobileDataEnabled returning " + retVal);
Robert Greenwalt1b21f6c2010-02-23 18:58:05 -08001434 return retVal;
1435 }
1436
Robert Greenwalt34848c02011-03-25 13:09:25 -07001437 public void setDataDependency(int networkType, boolean met) {
Robert Greenwalt6cac0742011-06-21 17:26:14 -07001438 enforceConnectivityInternalPermission();
1439
Robert Greenwalt34848c02011-03-25 13:09:25 -07001440 mHandler.sendMessage(mHandler.obtainMessage(EVENT_SET_DEPENDENCY_MET,
1441 (met ? ENABLED : DISABLED), networkType));
1442 }
1443
1444 private void handleSetDependencyMet(int networkType, boolean met) {
1445 if (mNetTrackers[networkType] != null) {
1446 if (DBG) {
1447 log("handleSetDependencyMet(" + networkType + ", " + met + ")");
1448 }
1449 mNetTrackers[networkType].setDependencyMet(met);
1450 }
1451 }
1452
Jeff Sharkey921ebf22011-05-19 17:12:49 -07001453 private INetworkPolicyListener mPolicyListener = new INetworkPolicyListener.Stub() {
1454 @Override
Jeff Sharkeya47d7a12011-06-16 15:07:48 -07001455 public void onUidRulesChanged(int uid, int uidRules) {
Jeff Sharkeyb1b6ccd2012-02-07 12:05:43 -08001456 // caller is NPMS, since we only register with them
Jeff Sharkey921ebf22011-05-19 17:12:49 -07001457 if (LOGD_RULES) {
Robert Greenwalt78f28112011-08-02 17:18:41 -07001458 log("onUidRulesChanged(uid=" + uid + ", uidRules=" + uidRules + ")");
Jeff Sharkey921ebf22011-05-19 17:12:49 -07001459 }
1460
Jeff Sharkeya47d7a12011-06-16 15:07:48 -07001461 synchronized (mRulesLock) {
Jeff Sharkey921ebf22011-05-19 17:12:49 -07001462 // skip update when we've already applied rules
1463 final int oldRules = mUidRules.get(uid, RULE_ALLOW_ALL);
1464 if (oldRules == uidRules) return;
1465
1466 mUidRules.put(uid, uidRules);
1467 }
1468
Jeff Sharkey921ebf22011-05-19 17:12:49 -07001469 // TODO: notify UID when it has requested targeted updates
1470 }
Jeff Sharkeya47d7a12011-06-16 15:07:48 -07001471
1472 @Override
1473 public void onMeteredIfacesChanged(String[] meteredIfaces) {
Jeff Sharkeyb1b6ccd2012-02-07 12:05:43 -08001474 // caller is NPMS, since we only register with them
Jeff Sharkeya47d7a12011-06-16 15:07:48 -07001475 if (LOGD_RULES) {
Robert Greenwalt78f28112011-08-02 17:18:41 -07001476 log("onMeteredIfacesChanged(ifaces=" + Arrays.toString(meteredIfaces) + ")");
Jeff Sharkeya47d7a12011-06-16 15:07:48 -07001477 }
1478
1479 synchronized (mRulesLock) {
1480 mMeteredIfaces.clear();
1481 for (String iface : meteredIfaces) {
1482 mMeteredIfaces.add(iface);
1483 }
1484 }
1485 }
Jeff Sharkeyb1b6ccd2012-02-07 12:05:43 -08001486
1487 @Override
1488 public void onRestrictBackgroundChanged(boolean restrictBackground) {
1489 // caller is NPMS, since we only register with them
1490 if (LOGD_RULES) {
1491 log("onRestrictBackgroundChanged(restrictBackground=" + restrictBackground + ")");
1492 }
1493
1494 // kick off connectivity change broadcast for active network, since
1495 // global background policy change is radical.
1496 final int networkType = mActiveDefaultNetwork;
1497 if (isNetworkTypeValid(networkType)) {
1498 final NetworkStateTracker tracker = mNetTrackers[networkType];
1499 if (tracker != null) {
1500 final NetworkInfo info = tracker.getNetworkInfo();
1501 if (info != null && info.isConnected()) {
1502 sendConnectedBroadcast(info);
1503 }
1504 }
1505 }
1506 }
Jeff Sharkey921ebf22011-05-19 17:12:49 -07001507 };
1508
Robert Greenwalt1b21f6c2010-02-23 18:58:05 -08001509 /**
1510 * @see ConnectivityManager#setMobileDataEnabled(boolean)
1511 */
Robert Greenwalt6a2db8a2010-09-23 10:05:56 -07001512 public void setMobileDataEnabled(boolean enabled) {
Robert Greenwalt1b21f6c2010-02-23 18:58:05 -08001513 enforceChangePermission();
Wink Savillee70c6f52010-12-03 12:01:38 -08001514 if (DBG) log("setMobileDataEnabled(" + enabled + ")");
Robert Greenwalt1b21f6c2010-02-23 18:58:05 -08001515
Robert Greenwalt6a2db8a2010-09-23 10:05:56 -07001516 mHandler.sendMessage(mHandler.obtainMessage(EVENT_SET_MOBILE_DATA,
Robert Greenwalt34848c02011-03-25 13:09:25 -07001517 (enabled ? ENABLED : DISABLED), 0));
Robert Greenwalt6a2db8a2010-09-23 10:05:56 -07001518 }
1519
1520 private void handleSetMobileData(boolean enabled) {
Wink Savilleb9024c62010-12-07 10:31:02 -08001521 if (mNetTrackers[ConnectivityManager.TYPE_MOBILE] != null) {
Robert Greenwalt78f28112011-08-02 17:18:41 -07001522 if (VDBG) {
1523 log(mNetTrackers[ConnectivityManager.TYPE_MOBILE].toString() + enabled);
Robert Greenwalt1b21f6c2010-02-23 18:58:05 -08001524 }
Jeff Sharkey805662d2011-08-19 02:24:24 -07001525 mNetTrackers[ConnectivityManager.TYPE_MOBILE].setUserDataEnable(enabled);
1526 }
tk.mun5eee1042012-01-06 10:43:52 +09001527 if (mNetTrackers[ConnectivityManager.TYPE_WIMAX] != null) {
1528 if (VDBG) {
1529 log(mNetTrackers[ConnectivityManager.TYPE_WIMAX].toString() + enabled);
1530 }
1531 mNetTrackers[ConnectivityManager.TYPE_WIMAX].setUserDataEnable(enabled);
1532 }
Jeff Sharkey805662d2011-08-19 02:24:24 -07001533 }
1534
1535 @Override
1536 public void setPolicyDataEnable(int networkType, boolean enabled) {
1537 // only someone like NPMS should only be calling us
1538 mContext.enforceCallingOrSelfPermission(MANAGE_NETWORK_POLICY, TAG);
1539
1540 mHandler.sendMessage(mHandler.obtainMessage(
1541 EVENT_SET_POLICY_DATA_ENABLE, networkType, (enabled ? ENABLED : DISABLED)));
1542 }
1543
1544 private void handleSetPolicyDataEnable(int networkType, boolean enabled) {
1545 if (isNetworkTypeValid(networkType)) {
1546 final NetworkStateTracker tracker = mNetTrackers[networkType];
1547 if (tracker != null) {
1548 tracker.setPolicyDataEnable(enabled);
1549 }
Robert Greenwalt1b21f6c2010-02-23 18:58:05 -08001550 }
1551 }
1552
The Android Open Source Project28527d22009-03-03 19:31:44 -08001553 private void enforceAccessPermission() {
Robert Greenwalt0659da32009-07-16 17:21:39 -07001554 mContext.enforceCallingOrSelfPermission(
1555 android.Manifest.permission.ACCESS_NETWORK_STATE,
1556 "ConnectivityService");
The Android Open Source Project28527d22009-03-03 19:31:44 -08001557 }
1558
1559 private void enforceChangePermission() {
Robert Greenwalt0659da32009-07-16 17:21:39 -07001560 mContext.enforceCallingOrSelfPermission(
1561 android.Manifest.permission.CHANGE_NETWORK_STATE,
1562 "ConnectivityService");
The Android Open Source Project28527d22009-03-03 19:31:44 -08001563 }
1564
Robert Greenwalt0c4828c2010-01-26 11:40:34 -08001565 // TODO Make this a special check when it goes public
1566 private void enforceTetherChangePermission() {
1567 mContext.enforceCallingOrSelfPermission(
1568 android.Manifest.permission.CHANGE_NETWORK_STATE,
1569 "ConnectivityService");
1570 }
1571
Robert Greenwalt8e87f122010-02-11 18:18:40 -08001572 private void enforceTetherAccessPermission() {
1573 mContext.enforceCallingOrSelfPermission(
1574 android.Manifest.permission.ACCESS_NETWORK_STATE,
1575 "ConnectivityService");
1576 }
1577
Robert Greenwalt93dc1042010-06-15 12:19:37 -07001578 private void enforceConnectivityInternalPermission() {
1579 mContext.enforceCallingOrSelfPermission(
1580 android.Manifest.permission.CONNECTIVITY_INTERNAL,
1581 "ConnectivityService");
1582 }
1583
The Android Open Source Project28527d22009-03-03 19:31:44 -08001584 /**
Robert Greenwalt0659da32009-07-16 17:21:39 -07001585 * Handle a {@code DISCONNECTED} event. If this pertains to the non-active
1586 * network, we ignore it. If it is for the active network, we send out a
1587 * broadcast. But first, we check whether it might be possible to connect
1588 * to a different network.
The Android Open Source Project28527d22009-03-03 19:31:44 -08001589 * @param info the {@code NetworkInfo} for the network
1590 */
1591 private void handleDisconnect(NetworkInfo info) {
1592
Robert Greenwalt2034b912009-08-12 16:08:25 -07001593 int prevNetType = info.getType();
The Android Open Source Project28527d22009-03-03 19:31:44 -08001594
Robert Greenwalt2034b912009-08-12 16:08:25 -07001595 mNetTrackers[prevNetType].setTeardownRequested(false);
The Android Open Source Project28527d22009-03-03 19:31:44 -08001596 /*
1597 * If the disconnected network is not the active one, then don't report
1598 * this as a loss of connectivity. What probably happened is that we're
1599 * getting the disconnect for a network that we explicitly disabled
1600 * in accordance with network preference policies.
1601 */
Robert Greenwalt34848c02011-03-25 13:09:25 -07001602 if (!mNetConfigs[prevNetType].isDefault()) {
Robert Greenwalt2034b912009-08-12 16:08:25 -07001603 List pids = mNetRequestersPids[prevNetType];
1604 for (int i = 0; i<pids.size(); i++) {
1605 Integer pid = (Integer)pids.get(i);
1606 // will remove them because the net's no longer connected
1607 // need to do this now as only now do we know the pids and
1608 // can properly null things that are no longer referenced.
1609 reassessPidDns(pid.intValue(), false);
The Android Open Source Project28527d22009-03-03 19:31:44 -08001610 }
The Android Open Source Project28527d22009-03-03 19:31:44 -08001611 }
1612
The Android Open Source Project28527d22009-03-03 19:31:44 -08001613 Intent intent = new Intent(ConnectivityManager.CONNECTIVITY_ACTION);
1614 intent.putExtra(ConnectivityManager.EXTRA_NETWORK_INFO, info);
1615 if (info.isFailover()) {
1616 intent.putExtra(ConnectivityManager.EXTRA_IS_FAILOVER, true);
1617 info.setFailover(false);
1618 }
1619 if (info.getReason() != null) {
1620 intent.putExtra(ConnectivityManager.EXTRA_REASON, info.getReason());
1621 }
1622 if (info.getExtraInfo() != null) {
Robert Greenwalt0659da32009-07-16 17:21:39 -07001623 intent.putExtra(ConnectivityManager.EXTRA_EXTRA_INFO,
1624 info.getExtraInfo());
The Android Open Source Project28527d22009-03-03 19:31:44 -08001625 }
Robert Greenwalt2034b912009-08-12 16:08:25 -07001626
Robert Greenwalt34848c02011-03-25 13:09:25 -07001627 if (mNetConfigs[prevNetType].isDefault()) {
Robert Greenwalt4b7c55e2011-01-11 13:56:33 -08001628 tryFailover(prevNetType);
1629 if (mActiveDefaultNetwork != -1) {
1630 NetworkInfo switchTo = mNetTrackers[mActiveDefaultNetwork].getNetworkInfo();
Robert Greenwalt3cc68d32010-01-25 17:54:29 -08001631 intent.putExtra(ConnectivityManager.EXTRA_OTHER_NETWORK_INFO, switchTo);
1632 } else {
Robert Greenwaltb2a9e492010-09-20 18:01:43 -07001633 mDefaultInetConditionPublished = 0; // we're not connected anymore
Robert Greenwalt3cc68d32010-01-25 17:54:29 -08001634 intent.putExtra(ConnectivityManager.EXTRA_NO_CONNECTIVITY, true);
1635 }
Robert Greenwaltf55ced92010-01-20 19:29:41 -08001636 }
Robert Greenwaltb2a9e492010-09-20 18:01:43 -07001637 intent.putExtra(ConnectivityManager.EXTRA_INET_CONDITION, mDefaultInetConditionPublished);
Robert Greenwalt36ea8692011-06-15 12:22:07 -07001638
1639 // Reset interface if no other connections are using the same interface
1640 boolean doReset = true;
1641 LinkProperties linkProperties = mNetTrackers[prevNetType].getLinkProperties();
1642 if (linkProperties != null) {
1643 String oldIface = linkProperties.getInterfaceName();
1644 if (TextUtils.isEmpty(oldIface) == false) {
1645 for (NetworkStateTracker networkStateTracker : mNetTrackers) {
1646 if (networkStateTracker == null) continue;
1647 NetworkInfo networkInfo = networkStateTracker.getNetworkInfo();
1648 if (networkInfo.isConnected() && networkInfo.getType() != prevNetType) {
1649 LinkProperties l = networkStateTracker.getLinkProperties();
1650 if (l == null) continue;
1651 if (oldIface.equals(l.getInterfaceName())) {
1652 doReset = false;
1653 break;
1654 }
1655 }
1656 }
1657 }
1658 }
1659
Robert Greenwaltf55ced92010-01-20 19:29:41 -08001660 // do this before we broadcast the change
Robert Greenwalt36ea8692011-06-15 12:22:07 -07001661 handleConnectivityChange(prevNetType, doReset);
Robert Greenwaltf55ced92010-01-20 19:29:41 -08001662
Jeff Sharkey971cd162011-08-29 16:02:57 -07001663 final Intent immediateIntent = new Intent(intent);
1664 immediateIntent.setAction(CONNECTIVITY_ACTION_IMMEDIATE);
1665 sendStickyBroadcast(immediateIntent);
Wink Saville4f0de1e2011-08-04 15:01:58 -07001666 sendStickyBroadcastDelayed(intent, getConnectivityChangeDelay());
Robert Greenwaltf55ced92010-01-20 19:29:41 -08001667 /*
1668 * If the failover network is already connected, then immediately send
1669 * out a followup broadcast indicating successful failover
1670 */
Robert Greenwalt4b7c55e2011-01-11 13:56:33 -08001671 if (mActiveDefaultNetwork != -1) {
Wink Saville4f0de1e2011-08-04 15:01:58 -07001672 sendConnectedBroadcastDelayed(mNetTrackers[mActiveDefaultNetwork].getNetworkInfo(),
1673 getConnectivityChangeDelay());
Robert Greenwaltf55ced92010-01-20 19:29:41 -08001674 }
1675 }
1676
Robert Greenwalt4b7c55e2011-01-11 13:56:33 -08001677 private void tryFailover(int prevNetType) {
Robert Greenwalt2034b912009-08-12 16:08:25 -07001678 /*
Robert Greenwalt92564852011-01-06 15:41:07 -08001679 * If this is a default network, check if other defaults are available.
1680 * Try to reconnect on all available and let them hash it out when
1681 * more than one connects.
Robert Greenwalt2034b912009-08-12 16:08:25 -07001682 */
Robert Greenwalt34848c02011-03-25 13:09:25 -07001683 if (mNetConfigs[prevNetType].isDefault()) {
Robert Greenwalt2034b912009-08-12 16:08:25 -07001684 if (mActiveDefaultNetwork == prevNetType) {
1685 mActiveDefaultNetwork = -1;
1686 }
1687
Robert Greenwalt4b7c55e2011-01-11 13:56:33 -08001688 // don't signal a reconnect for anything lower or equal priority than our
1689 // current connected default
1690 // TODO - don't filter by priority now - nice optimization but risky
1691// int currentPriority = -1;
1692// if (mActiveDefaultNetwork != -1) {
Robert Greenwalt34848c02011-03-25 13:09:25 -07001693// currentPriority = mNetConfigs[mActiveDefaultNetwork].mPriority;
Robert Greenwalt4b7c55e2011-01-11 13:56:33 -08001694// }
Robert Greenwaltf55ced92010-01-20 19:29:41 -08001695 for (int checkType=0; checkType <= ConnectivityManager.MAX_NETWORK_TYPE; checkType++) {
Robert Greenwaltec05b3c2009-10-30 14:17:42 -07001696 if (checkType == prevNetType) continue;
Robert Greenwalt34848c02011-03-25 13:09:25 -07001697 if (mNetConfigs[checkType] == null) continue;
1698 if (!mNetConfigs[checkType].isDefault()) continue;
Robert Greenwaltbc026d02011-11-11 09:56:03 -08001699 if (mNetTrackers[checkType] == null) continue;
Wink Saville72a95b92011-01-26 15:43:49 -08001700
1701// Enabling the isAvailable() optimization caused mobile to not get
1702// selected if it was in the middle of error handling. Specifically
1703// a moble connection that took 30 seconds to complete the DEACTIVATE_DATA_CALL
1704// would not be available and we wouldn't get connected to anything.
1705// So removing the isAvailable() optimization below for now. TODO: This
1706// optimization should work and we need to investigate why it doesn't work.
1707// This could be related to how DEACTIVATE_DATA_CALL is reporting its
1708// complete before it is really complete.
1709// if (!mNetTrackers[checkType].isAvailable()) continue;
1710
Robert Greenwalt34848c02011-03-25 13:09:25 -07001711// if (currentPriority >= mNetConfigs[checkType].mPriority) continue;
Robert Greenwalt2034b912009-08-12 16:08:25 -07001712
Robert Greenwalt4b7c55e2011-01-11 13:56:33 -08001713 NetworkStateTracker checkTracker = mNetTrackers[checkType];
1714 NetworkInfo checkInfo = checkTracker.getNetworkInfo();
1715 if (!checkInfo.isConnectedOrConnecting() || checkTracker.isTeardownRequested()) {
1716 checkInfo.setFailover(true);
1717 checkTracker.reconnect();
Robert Greenwalt2034b912009-08-12 16:08:25 -07001718 }
Robert Greenwalt4b7c55e2011-01-11 13:56:33 -08001719 if (DBG) log("Attempting to switch to " + checkInfo.getTypeName());
Robert Greenwalt2034b912009-08-12 16:08:25 -07001720 }
The Android Open Source Project28527d22009-03-03 19:31:44 -08001721 }
The Android Open Source Project28527d22009-03-03 19:31:44 -08001722 }
1723
1724 private void sendConnectedBroadcast(NetworkInfo info) {
Jeff Sharkey971cd162011-08-29 16:02:57 -07001725 sendGeneralBroadcast(info, CONNECTIVITY_ACTION_IMMEDIATE);
1726 sendGeneralBroadcast(info, CONNECTIVITY_ACTION);
Robert Greenwaltd3401f92010-09-15 17:36:33 -07001727 }
1728
Wink Saville4f0de1e2011-08-04 15:01:58 -07001729 private void sendConnectedBroadcastDelayed(NetworkInfo info, int delayMs) {
Jeff Sharkey971cd162011-08-29 16:02:57 -07001730 sendGeneralBroadcast(info, CONNECTIVITY_ACTION_IMMEDIATE);
1731 sendGeneralBroadcastDelayed(info, CONNECTIVITY_ACTION, delayMs);
Wink Saville4f0de1e2011-08-04 15:01:58 -07001732 }
1733
Robert Greenwaltd3401f92010-09-15 17:36:33 -07001734 private void sendInetConditionBroadcast(NetworkInfo info) {
1735 sendGeneralBroadcast(info, ConnectivityManager.INET_CONDITION_ACTION);
1736 }
1737
Wink Saville4f0de1e2011-08-04 15:01:58 -07001738 private Intent makeGeneralIntent(NetworkInfo info, String bcastType) {
Robert Greenwaltd3401f92010-09-15 17:36:33 -07001739 Intent intent = new Intent(bcastType);
The Android Open Source Project28527d22009-03-03 19:31:44 -08001740 intent.putExtra(ConnectivityManager.EXTRA_NETWORK_INFO, info);
1741 if (info.isFailover()) {
1742 intent.putExtra(ConnectivityManager.EXTRA_IS_FAILOVER, true);
1743 info.setFailover(false);
1744 }
1745 if (info.getReason() != null) {
1746 intent.putExtra(ConnectivityManager.EXTRA_REASON, info.getReason());
1747 }
1748 if (info.getExtraInfo() != null) {
Robert Greenwalt0659da32009-07-16 17:21:39 -07001749 intent.putExtra(ConnectivityManager.EXTRA_EXTRA_INFO,
1750 info.getExtraInfo());
The Android Open Source Project28527d22009-03-03 19:31:44 -08001751 }
Robert Greenwalt986c7412010-09-08 15:24:47 -07001752 intent.putExtra(ConnectivityManager.EXTRA_INET_CONDITION, mDefaultInetConditionPublished);
Wink Saville4f0de1e2011-08-04 15:01:58 -07001753 return intent;
1754 }
1755
1756 private void sendGeneralBroadcast(NetworkInfo info, String bcastType) {
1757 sendStickyBroadcast(makeGeneralIntent(info, bcastType));
1758 }
1759
1760 private void sendGeneralBroadcastDelayed(NetworkInfo info, String bcastType, int delayMs) {
1761 sendStickyBroadcastDelayed(makeGeneralIntent(info, bcastType), delayMs);
The Android Open Source Project28527d22009-03-03 19:31:44 -08001762 }
1763
Haoyu Baib5da5752012-06-20 14:29:57 -07001764 private void sendDataActivityBroadcast(int deviceType, boolean active) {
1765 Intent intent = new Intent(ConnectivityManager.ACTION_DATA_ACTIVITY_CHANGE);
1766 intent.putExtra(ConnectivityManager.EXTRA_DEVICE_TYPE, deviceType);
1767 intent.putExtra(ConnectivityManager.EXTRA_IS_ACTIVE, active);
1768 mContext.sendOrderedBroadcast(intent, RECEIVE_DATA_ACTIVITY_CHANGE);
1769 }
1770
The Android Open Source Project28527d22009-03-03 19:31:44 -08001771 /**
1772 * Called when an attempt to fail over to another network has failed.
1773 * @param info the {@link NetworkInfo} for the failed network
1774 */
1775 private void handleConnectionFailure(NetworkInfo info) {
1776 mNetTrackers[info.getType()].setTeardownRequested(false);
The Android Open Source Project28527d22009-03-03 19:31:44 -08001777
Robert Greenwalt2034b912009-08-12 16:08:25 -07001778 String reason = info.getReason();
1779 String extraInfo = info.getExtraInfo();
Robert Greenwalt0659da32009-07-16 17:21:39 -07001780
Robert Greenwalte981bc52010-10-08 16:35:52 -07001781 String reasonText;
1782 if (reason == null) {
1783 reasonText = ".";
1784 } else {
1785 reasonText = " (" + reason + ").";
The Android Open Source Project28527d22009-03-03 19:31:44 -08001786 }
Wink Savillee70c6f52010-12-03 12:01:38 -08001787 loge("Attempt to connect to " + info.getTypeName() + " failed" + reasonText);
Robert Greenwalt2034b912009-08-12 16:08:25 -07001788
1789 Intent intent = new Intent(ConnectivityManager.CONNECTIVITY_ACTION);
1790 intent.putExtra(ConnectivityManager.EXTRA_NETWORK_INFO, info);
1791 if (getActiveNetworkInfo() == null) {
1792 intent.putExtra(ConnectivityManager.EXTRA_NO_CONNECTIVITY, true);
1793 }
1794 if (reason != null) {
1795 intent.putExtra(ConnectivityManager.EXTRA_REASON, reason);
1796 }
1797 if (extraInfo != null) {
1798 intent.putExtra(ConnectivityManager.EXTRA_EXTRA_INFO, extraInfo);
1799 }
1800 if (info.isFailover()) {
1801 intent.putExtra(ConnectivityManager.EXTRA_IS_FAILOVER, true);
1802 info.setFailover(false);
1803 }
Robert Greenwaltf55ced92010-01-20 19:29:41 -08001804
Robert Greenwalt34848c02011-03-25 13:09:25 -07001805 if (mNetConfigs[info.getType()].isDefault()) {
Robert Greenwalt4b7c55e2011-01-11 13:56:33 -08001806 tryFailover(info.getType());
1807 if (mActiveDefaultNetwork != -1) {
1808 NetworkInfo switchTo = mNetTrackers[mActiveDefaultNetwork].getNetworkInfo();
Robert Greenwalt3cc68d32010-01-25 17:54:29 -08001809 intent.putExtra(ConnectivityManager.EXTRA_OTHER_NETWORK_INFO, switchTo);
1810 } else {
Robert Greenwaltb2a9e492010-09-20 18:01:43 -07001811 mDefaultInetConditionPublished = 0;
Robert Greenwalt3cc68d32010-01-25 17:54:29 -08001812 intent.putExtra(ConnectivityManager.EXTRA_NO_CONNECTIVITY, true);
1813 }
Robert Greenwaltf55ced92010-01-20 19:29:41 -08001814 }
Robert Greenwalt3cc68d32010-01-25 17:54:29 -08001815
Robert Greenwaltb2a9e492010-09-20 18:01:43 -07001816 intent.putExtra(ConnectivityManager.EXTRA_INET_CONDITION, mDefaultInetConditionPublished);
Jeff Sharkey971cd162011-08-29 16:02:57 -07001817
1818 final Intent immediateIntent = new Intent(intent);
1819 immediateIntent.setAction(CONNECTIVITY_ACTION_IMMEDIATE);
1820 sendStickyBroadcast(immediateIntent);
Mike Lockwoodfde2b762009-08-14 14:18:49 -04001821 sendStickyBroadcast(intent);
Robert Greenwaltf55ced92010-01-20 19:29:41 -08001822 /*
1823 * If the failover network is already connected, then immediately send
1824 * out a followup broadcast indicating successful failover
1825 */
Robert Greenwalt4b7c55e2011-01-11 13:56:33 -08001826 if (mActiveDefaultNetwork != -1) {
1827 sendConnectedBroadcast(mNetTrackers[mActiveDefaultNetwork].getNetworkInfo());
Robert Greenwaltf55ced92010-01-20 19:29:41 -08001828 }
Mike Lockwoodfde2b762009-08-14 14:18:49 -04001829 }
1830
1831 private void sendStickyBroadcast(Intent intent) {
1832 synchronized(this) {
Dianne Hackborna417ff82009-12-08 19:45:14 -08001833 if (!mSystemReady) {
1834 mInitialBroadcast = new Intent(intent);
Mike Lockwoodfde2b762009-08-14 14:18:49 -04001835 }
Dianne Hackborna417ff82009-12-08 19:45:14 -08001836 intent.addFlags(Intent.FLAG_RECEIVER_REGISTERED_ONLY_BEFORE_BOOT);
Wink Savillea7d56572011-09-21 11:05:43 -07001837 if (VDBG) {
Jeff Sharkey971cd162011-08-29 16:02:57 -07001838 log("sendStickyBroadcast: action=" + intent.getAction());
Wink Saville4f0de1e2011-08-04 15:01:58 -07001839 }
1840
Dianne Hackborna417ff82009-12-08 19:45:14 -08001841 mContext.sendStickyBroadcast(intent);
Mike Lockwoodfde2b762009-08-14 14:18:49 -04001842 }
1843 }
1844
Wink Saville4f0de1e2011-08-04 15:01:58 -07001845 private void sendStickyBroadcastDelayed(Intent intent, int delayMs) {
1846 if (delayMs <= 0) {
1847 sendStickyBroadcast(intent);
1848 } else {
Wink Savillea7d56572011-09-21 11:05:43 -07001849 if (VDBG) {
Jeff Sharkey971cd162011-08-29 16:02:57 -07001850 log("sendStickyBroadcastDelayed: delayMs=" + delayMs + ", action="
1851 + intent.getAction());
1852 }
Wink Saville4f0de1e2011-08-04 15:01:58 -07001853 mHandler.sendMessageDelayed(mHandler.obtainMessage(
1854 EVENT_SEND_STICKY_BROADCAST_INTENT, intent), delayMs);
1855 }
1856 }
1857
Mike Lockwoodfde2b762009-08-14 14:18:49 -04001858 void systemReady() {
1859 synchronized(this) {
1860 mSystemReady = true;
Dianne Hackborna417ff82009-12-08 19:45:14 -08001861 if (mInitialBroadcast != null) {
1862 mContext.sendStickyBroadcast(mInitialBroadcast);
1863 mInitialBroadcast = null;
Mike Lockwoodfde2b762009-08-14 14:18:49 -04001864 }
1865 }
Robert Greenwaltc3c5f862010-10-11 16:00:27 -07001866 // load the global proxy at startup
1867 mHandler.sendMessage(mHandler.obtainMessage(EVENT_APPLY_GLOBAL_HTTP_PROXY));
The Android Open Source Project28527d22009-03-03 19:31:44 -08001868 }
1869
1870 private void handleConnect(NetworkInfo info) {
Jeff Sharkeyaac2c502011-10-04 16:54:49 -07001871 final int type = info.getType();
The Android Open Source Project28527d22009-03-03 19:31:44 -08001872
1873 // snapshot isFailover, because sendConnectedBroadcast() resets it
1874 boolean isFailover = info.isFailover();
Jeff Sharkeyaac2c502011-10-04 16:54:49 -07001875 final NetworkStateTracker thisNet = mNetTrackers[type];
The Android Open Source Project28527d22009-03-03 19:31:44 -08001876
Robert Greenwalt2034b912009-08-12 16:08:25 -07001877 // if this is a default net and other default is running
1878 // kill the one not preferred
Robert Greenwalt34848c02011-03-25 13:09:25 -07001879 if (mNetConfigs[type].isDefault()) {
Robert Greenwalt2034b912009-08-12 16:08:25 -07001880 if (mActiveDefaultNetwork != -1 && mActiveDefaultNetwork != type) {
1881 if ((type != mNetworkPreference &&
Wink Savillef2a62832011-04-07 14:23:45 -07001882 mNetConfigs[mActiveDefaultNetwork].priority >
1883 mNetConfigs[type].priority) ||
Robert Greenwalt2034b912009-08-12 16:08:25 -07001884 mNetworkPreference == mActiveDefaultNetwork) {
1885 // don't accept this one
Robert Greenwalt78f28112011-08-02 17:18:41 -07001886 if (VDBG) {
Wink Savillee70c6f52010-12-03 12:01:38 -08001887 log("Not broadcasting CONNECT_ACTION " +
Robert Greenwalt2034b912009-08-12 16:08:25 -07001888 "to torn down network " + info.getTypeName());
Wink Savillee70c6f52010-12-03 12:01:38 -08001889 }
Robert Greenwalt2034b912009-08-12 16:08:25 -07001890 teardown(thisNet);
1891 return;
1892 } else {
1893 // tear down the other
1894 NetworkStateTracker otherNet =
1895 mNetTrackers[mActiveDefaultNetwork];
Wink Savillee70c6f52010-12-03 12:01:38 -08001896 if (DBG) {
1897 log("Policy requires " + otherNet.getNetworkInfo().getTypeName() +
Robert Greenwalt2034b912009-08-12 16:08:25 -07001898 " teardown");
Wink Savillee70c6f52010-12-03 12:01:38 -08001899 }
Robert Greenwalt2034b912009-08-12 16:08:25 -07001900 if (!teardown(otherNet)) {
Wink Savillee70c6f52010-12-03 12:01:38 -08001901 loge("Network declined teardown request");
Robert Greenwalt99910172011-03-29 11:36:28 -07001902 teardown(thisNet);
Robert Greenwalt2034b912009-08-12 16:08:25 -07001903 return;
1904 }
Robert Greenwalt93dc1042010-06-15 12:19:37 -07001905 }
1906 }
1907 synchronized (ConnectivityService.this) {
1908 // have a new default network, release the transition wakelock in a second
1909 // if it's held. The second pause is to allow apps to reconnect over the
1910 // new network
1911 if (mNetTransitionWakeLock.isHeld()) {
1912 mHandler.sendMessageDelayed(mHandler.obtainMessage(
Robert Greenwaltccb36f92010-09-24 14:32:21 -07001913 EVENT_CLEAR_NET_TRANSITION_WAKELOCK,
Robert Greenwalt93dc1042010-06-15 12:19:37 -07001914 mNetTransitionWakeLockSerialNumber, 0),
1915 1000);
Robert Greenwalt2034b912009-08-12 16:08:25 -07001916 }
1917 }
1918 mActiveDefaultNetwork = type;
Robert Greenwalt986c7412010-09-08 15:24:47 -07001919 // this will cause us to come up initially as unconnected and switching
1920 // to connected after our normal pause unless somebody reports us as reall
1921 // disconnected
1922 mDefaultInetConditionPublished = 0;
1923 mDefaultConnectionSequence++;
1924 mInetConditionChangeInFlight = false;
1925 // Don't do this - if we never sign in stay, grey
1926 //reportNetworkCondition(mActiveDefaultNetwork, 100);
Robert Greenwalt2034b912009-08-12 16:08:25 -07001927 }
The Android Open Source Project28527d22009-03-03 19:31:44 -08001928 thisNet.setTeardownRequested(false);
Irfan Sheriff7f132d92010-06-09 15:39:36 -07001929 updateNetworkSettings(thisNet);
Robert Greenwalt36ea8692011-06-15 12:22:07 -07001930 handleConnectivityChange(type, false);
Wink Saville4f0de1e2011-08-04 15:01:58 -07001931 sendConnectedBroadcastDelayed(info, getConnectivityChangeDelay());
Jeff Sharkeyaac2c502011-10-04 16:54:49 -07001932
1933 // notify battery stats service about this network
1934 final String iface = thisNet.getLinkProperties().getInterfaceName();
1935 if (iface != null) {
1936 try {
1937 BatteryStatsService.getService().noteNetworkInterfaceType(iface, type);
1938 } catch (RemoteException e) {
1939 // ignored; service lives in system_server
1940 }
1941 }
The Android Open Source Project28527d22009-03-03 19:31:44 -08001942 }
1943
The Android Open Source Project28527d22009-03-03 19:31:44 -08001944 /**
Robert Greenwalt3afbead2010-07-23 15:46:26 -07001945 * After a change in the connectivity state of a network. We're mainly
1946 * concerned with making sure that the list of DNS servers is set up
1947 * according to which networks are connected, and ensuring that the
1948 * right routing table entries exist.
The Android Open Source Project28527d22009-03-03 19:31:44 -08001949 */
Robert Greenwalt36ea8692011-06-15 12:22:07 -07001950 private void handleConnectivityChange(int netType, boolean doReset) {
Wink Saville051a6642011-07-13 13:44:13 -07001951 int resetMask = doReset ? NetworkUtils.RESET_ALL_ADDRESSES : 0;
1952
The Android Open Source Project28527d22009-03-03 19:31:44 -08001953 /*
Robert Greenwalt2034b912009-08-12 16:08:25 -07001954 * If a non-default network is enabled, add the host routes that
Robert Greenwalt3afbead2010-07-23 15:46:26 -07001955 * will allow it's DNS servers to be accessed.
The Android Open Source Project28527d22009-03-03 19:31:44 -08001956 */
Robert Greenwalt3afbead2010-07-23 15:46:26 -07001957 handleDnsConfigurationChange(netType);
Robert Greenwalt2034b912009-08-12 16:08:25 -07001958
Robert Greenwalte8d2a4a2011-07-14 14:28:05 -07001959 LinkProperties curLp = mCurrentLinkProperties[netType];
1960 LinkProperties newLp = null;
1961
Robert Greenwalt3afbead2010-07-23 15:46:26 -07001962 if (mNetTrackers[netType].getNetworkInfo().isConnected()) {
Robert Greenwalte8d2a4a2011-07-14 14:28:05 -07001963 newLp = mNetTrackers[netType].getLinkProperties();
Wink Saville051a6642011-07-13 13:44:13 -07001964 if (VDBG) {
1965 log("handleConnectivityChange: changed linkProperty[" + netType + "]:" +
1966 " doReset=" + doReset + " resetMask=" + resetMask +
1967 "\n curLp=" + curLp +
1968 "\n newLp=" + newLp);
1969 }
1970
Robert Greenwalte8d2a4a2011-07-14 14:28:05 -07001971 if (curLp != null) {
1972 if (curLp.isIdenticalInterfaceName(newLp)) {
1973 CompareResult<LinkAddress> car = curLp.compareAddresses(newLp);
1974 if ((car.removed.size() != 0) || (car.added.size() != 0)) {
1975 for (LinkAddress linkAddr : car.removed) {
1976 if (linkAddr.getAddress() instanceof Inet4Address) {
1977 resetMask |= NetworkUtils.RESET_IPV4_ADDRESSES;
1978 }
1979 if (linkAddr.getAddress() instanceof Inet6Address) {
1980 resetMask |= NetworkUtils.RESET_IPV6_ADDRESSES;
1981 }
Wink Saville051a6642011-07-13 13:44:13 -07001982 }
Robert Greenwalte8d2a4a2011-07-14 14:28:05 -07001983 if (DBG) {
1984 log("handleConnectivityChange: addresses changed" +
1985 " linkProperty[" + netType + "]:" + " resetMask=" + resetMask +
1986 "\n car=" + car);
Wink Saville051a6642011-07-13 13:44:13 -07001987 }
Robert Greenwalte8d2a4a2011-07-14 14:28:05 -07001988 } else {
1989 if (DBG) {
1990 log("handleConnectivityChange: address are the same reset per doReset" +
1991 " linkProperty[" + netType + "]:" +
1992 " resetMask=" + resetMask);
1993 }
Wink Saville051a6642011-07-13 13:44:13 -07001994 }
1995 } else {
Robert Greenwalte8d2a4a2011-07-14 14:28:05 -07001996 resetMask = NetworkUtils.RESET_ALL_ADDRESSES;
Robert Greenwalt78f28112011-08-02 17:18:41 -07001997 if (DBG) {
1998 log("handleConnectivityChange: interface not not equivalent reset both" +
1999 " linkProperty[" + netType + "]:" +
2000 " resetMask=" + resetMask);
2001 }
Wink Saville051a6642011-07-13 13:44:13 -07002002 }
Wink Saville051a6642011-07-13 13:44:13 -07002003 }
Robert Greenwalt34848c02011-03-25 13:09:25 -07002004 if (mNetConfigs[netType].isDefault()) {
Chia-chi Yeh763a11c2011-10-03 15:34:04 -07002005 handleApplyDefaultProxy(newLp.getHttpProxy());
Robert Greenwalt3afbead2010-07-23 15:46:26 -07002006 }
2007 } else {
Robert Greenwalte8d2a4a2011-07-14 14:28:05 -07002008 if (VDBG) {
2009 log("handleConnectivityChange: changed linkProperty[" + netType + "]:" +
2010 " doReset=" + doReset + " resetMask=" + resetMask +
2011 "\n curLp=" + curLp +
2012 "\n newLp= null");
Robert Greenwalt2034b912009-08-12 16:08:25 -07002013 }
The Android Open Source Project28527d22009-03-03 19:31:44 -08002014 }
Robert Greenwalte8d2a4a2011-07-14 14:28:05 -07002015 mCurrentLinkProperties[netType] = newLp;
Robert Greenwalt8d777252011-08-15 12:31:55 -07002016 boolean resetDns = updateRoutes(newLp, curLp, mNetConfigs[netType].isDefault());
Robert Greenwalt36ea8692011-06-15 12:22:07 -07002017
Chia-chi Yeh4a243f52011-08-15 15:19:40 -07002018 if (resetMask != 0 || resetDns) {
Robert Greenwalt36ea8692011-06-15 12:22:07 -07002019 LinkProperties linkProperties = mNetTrackers[netType].getLinkProperties();
2020 if (linkProperties != null) {
2021 String iface = linkProperties.getInterfaceName();
2022 if (TextUtils.isEmpty(iface) == false) {
Chia-chi Yeh4a243f52011-08-15 15:19:40 -07002023 if (resetMask != 0) {
Robert Greenwalt8d777252011-08-15 12:31:55 -07002024 if (DBG) log("resetConnections(" + iface + ", " + resetMask + ")");
2025 NetworkUtils.resetConnections(iface, resetMask);
Chia-chi Yeh4a243f52011-08-15 15:19:40 -07002026
2027 // Tell VPN the interface is down. It is a temporary
2028 // but effective fix to make VPN aware of the change.
2029 if ((resetMask & NetworkUtils.RESET_IPV4_ADDRESSES) != 0) {
2030 mVpn.interfaceStatusChanged(iface, false);
2031 }
Robert Greenwalt8d777252011-08-15 12:31:55 -07002032 }
2033 if (resetDns) {
Wink Savillea7d56572011-09-21 11:05:43 -07002034 if (VDBG) log("resetting DNS cache for " + iface);
Robert Greenwalt8d777252011-08-15 12:31:55 -07002035 try {
2036 mNetd.flushInterfaceDnsCache(iface);
2037 } catch (Exception e) {
2038 // never crash - catch them all
Wink Savillea7d56572011-09-21 11:05:43 -07002039 if (DBG) loge("Exception resetting dns cache: " + e);
Robert Greenwalt8d777252011-08-15 12:31:55 -07002040 }
2041 }
Robert Greenwalt36ea8692011-06-15 12:22:07 -07002042 }
2043 }
2044 }
Kazuhiro Ondo07680062011-06-22 21:10:34 -05002045
2046 // TODO: Temporary notifying upstread change to Tethering.
2047 // @see bug/4455071
2048 /** Notify TetheringService if interface name has been changed. */
2049 if (TextUtils.equals(mNetTrackers[netType].getNetworkInfo().getReason(),
Wink Saville64e3f782012-07-10 12:37:54 -07002050 PhoneConstants.REASON_LINK_PROPERTIES_CHANGED)) {
Kazuhiro Ondo07680062011-06-22 21:10:34 -05002051 if (isTetheringSupported()) {
2052 mTethering.handleTetherIfaceChange();
2053 }
2054 }
The Android Open Source Project28527d22009-03-03 19:31:44 -08002055 }
2056
Robert Greenwalte8d2a4a2011-07-14 14:28:05 -07002057 /**
2058 * Add and remove routes using the old properties (null if not previously connected),
2059 * new properties (null if becoming disconnected). May even be double null, which
2060 * is a noop.
2061 * Uses isLinkDefault to determine if default routes should be set or conversely if
2062 * host routes should be set to the dns servers
Robert Greenwalt8d777252011-08-15 12:31:55 -07002063 * returns a boolean indicating the routes changed
Robert Greenwalte8d2a4a2011-07-14 14:28:05 -07002064 */
Robert Greenwalt8d777252011-08-15 12:31:55 -07002065 private boolean updateRoutes(LinkProperties newLp, LinkProperties curLp,
2066 boolean isLinkDefault) {
Robert Greenwalte8d2a4a2011-07-14 14:28:05 -07002067 Collection<RouteInfo> routesToAdd = null;
Robert Greenwalt98107422011-07-22 11:55:33 -07002068 CompareResult<InetAddress> dnsDiff = new CompareResult<InetAddress>();
2069 CompareResult<RouteInfo> routeDiff = new CompareResult<RouteInfo>();
Robert Greenwalte8d2a4a2011-07-14 14:28:05 -07002070 if (curLp != null) {
2071 // check for the delta between the current set and the new
Robert Greenwalt98107422011-07-22 11:55:33 -07002072 routeDiff = curLp.compareRoutes(newLp);
Robert Greenwalte8d2a4a2011-07-14 14:28:05 -07002073 dnsDiff = curLp.compareDnses(newLp);
Robert Greenwalt98107422011-07-22 11:55:33 -07002074 } else if (newLp != null) {
2075 routeDiff.added = newLp.getRoutes();
2076 dnsDiff.added = newLp.getDnses();
Irfan Sheriff7f132d92010-06-09 15:39:36 -07002077 }
Irfan Sheriff7f132d92010-06-09 15:39:36 -07002078
Robert Greenwalt8d777252011-08-15 12:31:55 -07002079 boolean routesChanged = (routeDiff.removed.size() != 0 || routeDiff.added.size() != 0);
2080
Robert Greenwalt98107422011-07-22 11:55:33 -07002081 for (RouteInfo r : routeDiff.removed) {
2082 if (isLinkDefault || ! r.isDefaultRoute()) {
Robert Greenwalt49c75d32011-11-02 14:37:19 -07002083 removeRoute(curLp, r, TO_DEFAULT_TABLE);
2084 }
2085 if (isLinkDefault == false) {
2086 // remove from a secondary route table
2087 removeRoute(curLp, r, TO_SECONDARY_TABLE);
Robert Greenwalte8d2a4a2011-07-14 14:28:05 -07002088 }
Robert Greenwalt98107422011-07-22 11:55:33 -07002089 }
Irfan Sheriff7f132d92010-06-09 15:39:36 -07002090
Robert Greenwalt98107422011-07-22 11:55:33 -07002091 for (RouteInfo r : routeDiff.added) {
2092 if (isLinkDefault || ! r.isDefaultRoute()) {
Robert Greenwalt49c75d32011-11-02 14:37:19 -07002093 addRoute(newLp, r, TO_DEFAULT_TABLE);
Robert Greenwalt08ae9bc2011-08-09 16:52:58 -07002094 } else {
Robert Greenwalt49c75d32011-11-02 14:37:19 -07002095 // add to a secondary route table
2096 addRoute(newLp, r, TO_SECONDARY_TABLE);
2097
Robert Greenwalt08ae9bc2011-08-09 16:52:58 -07002098 // many radios add a default route even when we don't want one.
2099 // remove the default route unless somebody else has asked for it
2100 String ifaceName = newLp.getInterfaceName();
2101 if (TextUtils.isEmpty(ifaceName) == false && mAddedRoutes.contains(r) == false) {
Wink Savillea7d56572011-09-21 11:05:43 -07002102 if (VDBG) log("Removing " + r + " for interface " + ifaceName);
Robert Greenwalt08ae9bc2011-08-09 16:52:58 -07002103 try {
2104 mNetd.removeRoute(ifaceName, r);
2105 } catch (Exception e) {
2106 // never crash - catch them all
Robert Greenwalt7fa3bdb2011-12-13 15:26:02 -08002107 if (DBG) loge("Exception trying to remove a route: " + e);
Robert Greenwalt08ae9bc2011-08-09 16:52:58 -07002108 }
2109 }
Robert Greenwalte8d2a4a2011-07-14 14:28:05 -07002110 }
2111 }
Irfan Sheriff7f132d92010-06-09 15:39:36 -07002112
Robert Greenwalte8d2a4a2011-07-14 14:28:05 -07002113 if (!isLinkDefault) {
2114 // handle DNS routes
Robert Greenwalt8d777252011-08-15 12:31:55 -07002115 if (routesChanged) {
Robert Greenwalt98107422011-07-22 11:55:33 -07002116 // routes changed - remove all old dns entries and add new
2117 if (curLp != null) {
2118 for (InetAddress oldDns : curLp.getDnses()) {
2119 removeRouteToAddress(curLp, oldDns);
2120 }
2121 }
2122 if (newLp != null) {
2123 for (InetAddress newDns : newLp.getDnses()) {
2124 addRouteToAddress(newLp, newDns);
2125 }
Robert Greenwalt03d53da2011-03-22 18:47:42 -07002126 }
Robert Greenwalt8d777252011-08-15 12:31:55 -07002127 } else {
2128 // no change in routes, check for change in dns themselves
2129 for (InetAddress oldDns : dnsDiff.removed) {
2130 removeRouteToAddress(curLp, oldDns);
2131 }
2132 for (InetAddress newDns : dnsDiff.added) {
2133 addRouteToAddress(newLp, newDns);
2134 }
Irfan Sheriff7f132d92010-06-09 15:39:36 -07002135 }
Irfan Sheriff7f132d92010-06-09 15:39:36 -07002136 }
Robert Greenwalt8d777252011-08-15 12:31:55 -07002137 return routesChanged;
Irfan Sheriff7f132d92010-06-09 15:39:36 -07002138 }
2139
2140
Irfan Sheriff7f132d92010-06-09 15:39:36 -07002141 /**
2142 * Reads the network specific TCP buffer sizes from SystemProperties
2143 * net.tcp.buffersize.[default|wifi|umts|edge|gprs] and set them for system
2144 * wide use
2145 */
2146 public void updateNetworkSettings(NetworkStateTracker nt) {
2147 String key = nt.getTcpBufferSizesPropName();
2148 String bufferSizes = SystemProperties.get(key);
2149
2150 if (bufferSizes.length() == 0) {
Robert Greenwalt78f28112011-08-02 17:18:41 -07002151 if (VDBG) log(key + " not found in system properties. Using defaults");
Irfan Sheriff7f132d92010-06-09 15:39:36 -07002152
2153 // Setting to default values so we won't be stuck to previous values
2154 key = "net.tcp.buffersize.default";
2155 bufferSizes = SystemProperties.get(key);
2156 }
2157
2158 // Set values in kernel
2159 if (bufferSizes.length() != 0) {
Robert Greenwalt78f28112011-08-02 17:18:41 -07002160 if (VDBG) {
Wink Savillee70c6f52010-12-03 12:01:38 -08002161 log("Setting TCP values: [" + bufferSizes
Irfan Sheriff7f132d92010-06-09 15:39:36 -07002162 + "] which comes from [" + key + "]");
2163 }
2164 setBufferSize(bufferSizes);
2165 }
2166 }
2167
2168 /**
2169 * Writes TCP buffer sizes to /sys/kernel/ipv4/tcp_[r/w]mem_[min/def/max]
2170 * which maps to /proc/sys/net/ipv4/tcp_rmem and tcpwmem
2171 *
2172 * @param bufferSizes in the format of "readMin, readInitial, readMax,
2173 * writeMin, writeInitial, writeMax"
2174 */
2175 private void setBufferSize(String bufferSizes) {
2176 try {
2177 String[] values = bufferSizes.split(",");
2178
2179 if (values.length == 6) {
2180 final String prefix = "/sys/kernel/ipv4/tcp_";
Mike Lockwood0d5916c2011-05-28 13:24:04 -04002181 FileUtils.stringToFile(prefix + "rmem_min", values[0]);
2182 FileUtils.stringToFile(prefix + "rmem_def", values[1]);
2183 FileUtils.stringToFile(prefix + "rmem_max", values[2]);
2184 FileUtils.stringToFile(prefix + "wmem_min", values[3]);
2185 FileUtils.stringToFile(prefix + "wmem_def", values[4]);
2186 FileUtils.stringToFile(prefix + "wmem_max", values[5]);
Irfan Sheriff7f132d92010-06-09 15:39:36 -07002187 } else {
Wink Savillee70c6f52010-12-03 12:01:38 -08002188 loge("Invalid buffersize string: " + bufferSizes);
Irfan Sheriff7f132d92010-06-09 15:39:36 -07002189 }
2190 } catch (IOException e) {
Wink Savillee70c6f52010-12-03 12:01:38 -08002191 loge("Can't set tcp buffer sizes:" + e);
Irfan Sheriff7f132d92010-06-09 15:39:36 -07002192 }
2193 }
2194
Robert Greenwalt2034b912009-08-12 16:08:25 -07002195 /**
2196 * Adjust the per-process dns entries (net.dns<x>.<pid>) based
2197 * on the highest priority active net which this process requested.
2198 * If there aren't any, clear it out
2199 */
2200 private void reassessPidDns(int myPid, boolean doBump)
2201 {
Robert Greenwalt78f28112011-08-02 17:18:41 -07002202 if (VDBG) log("reassessPidDns for pid " + myPid);
Robert Greenwalt2034b912009-08-12 16:08:25 -07002203 for(int i : mPriorityList) {
Robert Greenwalt34848c02011-03-25 13:09:25 -07002204 if (mNetConfigs[i].isDefault()) {
Robert Greenwalt2034b912009-08-12 16:08:25 -07002205 continue;
2206 }
2207 NetworkStateTracker nt = mNetTrackers[i];
Robert Greenwalt0659da32009-07-16 17:21:39 -07002208 if (nt.getNetworkInfo().isConnected() &&
2209 !nt.isTeardownRequested()) {
Robert Greenwalt1f1bcfe2010-08-30 10:56:47 -07002210 LinkProperties p = nt.getLinkProperties();
Robert Greenwalta7dfbd32010-06-15 15:43:39 -07002211 if (p == null) continue;
Robert Greenwalt2034b912009-08-12 16:08:25 -07002212 List pids = mNetRequestersPids[i];
2213 for (int j=0; j<pids.size(); j++) {
2214 Integer pid = (Integer)pids.get(j);
2215 if (pid.intValue() == myPid) {
Robert Greenwalta7dfbd32010-06-15 15:43:39 -07002216 Collection<InetAddress> dnses = p.getDnses();
Lorenzo Colittiaa035382011-09-28 22:31:45 -07002217 writePidDns(dnses, myPid);
Robert Greenwalt2034b912009-08-12 16:08:25 -07002218 if (doBump) {
2219 bumpDns();
2220 }
2221 return;
2222 }
2223 }
2224 }
2225 }
2226 // nothing found - delete
2227 for (int i = 1; ; i++) {
2228 String prop = "net.dns" + i + "." + myPid;
2229 if (SystemProperties.get(prop).length() == 0) {
2230 if (doBump) {
2231 bumpDns();
2232 }
2233 return;
2234 }
2235 SystemProperties.set(prop, "");
2236 }
2237 }
2238
Robert Greenwalt8ca88762010-12-17 15:20:36 -08002239 // return true if results in a change
Lorenzo Colittiaa035382011-09-28 22:31:45 -07002240 private boolean writePidDns(Collection <InetAddress> dnses, int pid) {
Robert Greenwalt2034b912009-08-12 16:08:25 -07002241 int j = 1;
Robert Greenwalt8ca88762010-12-17 15:20:36 -08002242 boolean changed = false;
Robert Greenwalta7dfbd32010-06-15 15:43:39 -07002243 for (InetAddress dns : dnses) {
Robert Greenwalt8ca88762010-12-17 15:20:36 -08002244 String dnsString = dns.getHostAddress();
2245 if (changed || !dnsString.equals(SystemProperties.get("net.dns" + j + "." + pid))) {
2246 changed = true;
yoonsung.nam48d18332012-03-02 19:56:16 +09002247 SystemProperties.set("net.dns" + j + "." + pid, dns.getHostAddress());
Robert Greenwalt8ca88762010-12-17 15:20:36 -08002248 }
yoonsung.nam48d18332012-03-02 19:56:16 +09002249 j++;
Robert Greenwalt2034b912009-08-12 16:08:25 -07002250 }
Robert Greenwalt8ca88762010-12-17 15:20:36 -08002251 return changed;
Robert Greenwalt2034b912009-08-12 16:08:25 -07002252 }
2253
2254 private void bumpDns() {
2255 /*
2256 * Bump the property that tells the name resolver library to reread
2257 * the DNS server list from the properties.
2258 */
2259 String propVal = SystemProperties.get("net.dnschange");
2260 int n = 0;
2261 if (propVal.length() != 0) {
2262 try {
2263 n = Integer.parseInt(propVal);
2264 } catch (NumberFormatException e) {}
2265 }
2266 SystemProperties.set("net.dnschange", "" + (n+1));
Robert Greenwalt051642b2010-11-02 14:08:23 -07002267 /*
2268 * Tell the VMs to toss their DNS caches
2269 */
2270 Intent intent = new Intent(Intent.ACTION_CLEAR_DNS_CACHE);
2271 intent.addFlags(Intent.FLAG_RECEIVER_REPLACE_PENDING);
Stan Chesnuttf444f502011-01-05 17:14:03 -08002272 /*
2273 * Connectivity events can happen before boot has completed ...
2274 */
2275 intent.addFlags(Intent.FLAG_RECEIVER_REGISTERED_ONLY_BEFORE_BOOT);
Robert Greenwalt051642b2010-11-02 14:08:23 -07002276 mContext.sendBroadcast(intent);
Robert Greenwalt2034b912009-08-12 16:08:25 -07002277 }
2278
Chia-chi Yehcc844502011-07-14 18:01:57 -07002279 // Caller must grab mDnsLock.
Robert Greenwaltfce71862011-07-25 16:06:25 -07002280 private boolean updateDns(String network, String iface,
Lorenzo Colittiaa035382011-09-28 22:31:45 -07002281 Collection<InetAddress> dnses, String domains) {
Chia-chi Yehcc844502011-07-14 18:01:57 -07002282 boolean changed = false;
2283 int last = 0;
2284 if (dnses.size() == 0 && mDefaultDns != null) {
2285 ++last;
2286 String value = mDefaultDns.getHostAddress();
2287 if (!value.equals(SystemProperties.get("net.dns1"))) {
2288 if (DBG) {
Robert Greenwalt78f28112011-08-02 17:18:41 -07002289 loge("no dns provided for " + network + " - using " + value);
Chia-chi Yehcc844502011-07-14 18:01:57 -07002290 }
2291 changed = true;
2292 SystemProperties.set("net.dns1", value);
2293 }
2294 } else {
2295 for (InetAddress dns : dnses) {
2296 ++last;
2297 String key = "net.dns" + last;
2298 String value = dns.getHostAddress();
2299 if (!changed && value.equals(SystemProperties.get(key))) {
2300 continue;
2301 }
Robert Greenwalt78f28112011-08-02 17:18:41 -07002302 if (VDBG) {
Chia-chi Yehcc844502011-07-14 18:01:57 -07002303 log("adding dns " + value + " for " + network);
2304 }
2305 changed = true;
2306 SystemProperties.set(key, value);
2307 }
2308 }
2309 for (int i = last + 1; i <= mNumDnsEntries; ++i) {
2310 String key = "net.dns" + i;
Robert Greenwalt78f28112011-08-02 17:18:41 -07002311 if (VDBG) log("erasing " + key);
Chia-chi Yehcc844502011-07-14 18:01:57 -07002312 changed = true;
2313 SystemProperties.set(key, "");
2314 }
2315 mNumDnsEntries = last;
2316
Robert Greenwaltfce71862011-07-25 16:06:25 -07002317 if (changed) {
2318 try {
2319 mNetd.setDnsServersForInterface(iface, NetworkUtils.makeStrings(dnses));
2320 mNetd.setDefaultInterfaceForDns(iface);
2321 } catch (Exception e) {
Robert Greenwalt7fa3bdb2011-12-13 15:26:02 -08002322 if (DBG) loge("exception setting default dns interface: " + e);
Robert Greenwaltfce71862011-07-25 16:06:25 -07002323 }
2324 }
Chia-chi Yehcc844502011-07-14 18:01:57 -07002325 if (!domains.equals(SystemProperties.get("net.dns.search"))) {
2326 SystemProperties.set("net.dns.search", domains);
2327 changed = true;
2328 }
2329 return changed;
2330 }
2331
Robert Greenwalt3afbead2010-07-23 15:46:26 -07002332 private void handleDnsConfigurationChange(int netType) {
Robert Greenwalt2034b912009-08-12 16:08:25 -07002333 // add default net's dns entries
Robert Greenwalt3afbead2010-07-23 15:46:26 -07002334 NetworkStateTracker nt = mNetTrackers[netType];
2335 if (nt != null && nt.getNetworkInfo().isConnected() && !nt.isTeardownRequested()) {
Robert Greenwalt1f1bcfe2010-08-30 10:56:47 -07002336 LinkProperties p = nt.getLinkProperties();
Robert Greenwalt3afbead2010-07-23 15:46:26 -07002337 if (p == null) return;
2338 Collection<InetAddress> dnses = p.getDnses();
Robert Greenwalt8ca88762010-12-17 15:20:36 -08002339 boolean changed = false;
Robert Greenwalt34848c02011-03-25 13:09:25 -07002340 if (mNetConfigs[netType].isDefault()) {
Chia-chi Yehcc844502011-07-14 18:01:57 -07002341 String network = nt.getNetworkInfo().getTypeName();
2342 synchronized (mDnsLock) {
2343 if (!mDnsOverridden) {
Lorenzo Colittiaa035382011-09-28 22:31:45 -07002344 changed = updateDns(network, p.getInterfaceName(), dnses, "");
Robert Greenwalt94daa182010-09-01 11:34:05 -07002345 }
Robert Greenwalt3afbead2010-07-23 15:46:26 -07002346 }
Robert Greenwalt3afbead2010-07-23 15:46:26 -07002347 } else {
Robert Greenwaltfce71862011-07-25 16:06:25 -07002348 try {
Robert Greenwalt49f762e2011-07-27 10:00:36 -07002349 mNetd.setDnsServersForInterface(p.getInterfaceName(),
Robert Greenwaltfce71862011-07-25 16:06:25 -07002350 NetworkUtils.makeStrings(dnses));
2351 } catch (Exception e) {
Robert Greenwalt7fa3bdb2011-12-13 15:26:02 -08002352 if (DBG) loge("exception setting dns servers: " + e);
Robert Greenwaltfce71862011-07-25 16:06:25 -07002353 }
Robert Greenwalt3afbead2010-07-23 15:46:26 -07002354 // set per-pid dns for attached secondary nets
2355 List pids = mNetRequestersPids[netType];
2356 for (int y=0; y< pids.size(); y++) {
2357 Integer pid = (Integer)pids.get(y);
Lorenzo Colittiaa035382011-09-28 22:31:45 -07002358 changed = writePidDns(dnses, pid.intValue());
The Android Open Source Project28527d22009-03-03 19:31:44 -08002359 }
2360 }
Robert Greenwalt8ca88762010-12-17 15:20:36 -08002361 if (changed) bumpDns();
The Android Open Source Project28527d22009-03-03 19:31:44 -08002362 }
Robert Greenwalt2034b912009-08-12 16:08:25 -07002363 }
2364
Robert Greenwalt20f819c2011-05-03 19:02:44 -07002365 private int getRestoreDefaultNetworkDelay(int networkType) {
Robert Greenwalt2034b912009-08-12 16:08:25 -07002366 String restoreDefaultNetworkDelayStr = SystemProperties.get(
2367 NETWORK_RESTORE_DELAY_PROP_NAME);
2368 if(restoreDefaultNetworkDelayStr != null &&
2369 restoreDefaultNetworkDelayStr.length() != 0) {
2370 try {
2371 return Integer.valueOf(restoreDefaultNetworkDelayStr);
2372 } catch (NumberFormatException e) {
2373 }
The Android Open Source Project28527d22009-03-03 19:31:44 -08002374 }
Robert Greenwalt20f819c2011-05-03 19:02:44 -07002375 // if the system property isn't set, use the value for the apn type
2376 int ret = RESTORE_DEFAULT_NETWORK_DELAY;
2377
2378 if ((networkType <= ConnectivityManager.MAX_NETWORK_TYPE) &&
2379 (mNetConfigs[networkType] != null)) {
2380 ret = mNetConfigs[networkType].restoreTime;
2381 }
2382 return ret;
The Android Open Source Project28527d22009-03-03 19:31:44 -08002383 }
2384
2385 @Override
2386 protected void dump(FileDescriptor fd, PrintWriter pw, String[] args) {
Robert Greenwalt0659da32009-07-16 17:21:39 -07002387 if (mContext.checkCallingOrSelfPermission(
2388 android.Manifest.permission.DUMP)
The Android Open Source Project28527d22009-03-03 19:31:44 -08002389 != PackageManager.PERMISSION_GRANTED) {
Robert Greenwalt0659da32009-07-16 17:21:39 -07002390 pw.println("Permission Denial: can't dump ConnectivityService " +
2391 "from from pid=" + Binder.getCallingPid() + ", uid=" +
2392 Binder.getCallingUid());
The Android Open Source Project28527d22009-03-03 19:31:44 -08002393 return;
2394 }
The Android Open Source Project28527d22009-03-03 19:31:44 -08002395 pw.println();
2396 for (NetworkStateTracker nst : mNetTrackers) {
Robert Greenwalt3eeb6032009-12-21 18:24:07 -08002397 if (nst != null) {
2398 if (nst.getNetworkInfo().isConnected()) {
2399 pw.println("Active network: " + nst.getNetworkInfo().
2400 getTypeName());
2401 }
2402 pw.println(nst.getNetworkInfo());
2403 pw.println(nst);
2404 pw.println();
Robert Greenwalt2034b912009-08-12 16:08:25 -07002405 }
The Android Open Source Project28527d22009-03-03 19:31:44 -08002406 }
Robert Greenwalt3eeb6032009-12-21 18:24:07 -08002407
2408 pw.println("Network Requester Pids:");
2409 for (int net : mPriorityList) {
2410 String pidString = net + ": ";
2411 for (Object pid : mNetRequestersPids[net]) {
2412 pidString = pidString + pid.toString() + ", ";
2413 }
2414 pw.println(pidString);
2415 }
2416 pw.println();
2417
2418 pw.println("FeatureUsers:");
2419 for (Object requester : mFeatureUsers) {
2420 pw.println(requester.toString());
2421 }
2422 pw.println();
Robert Greenwalt8e87f122010-02-11 18:18:40 -08002423
Robert Greenwalt93dc1042010-06-15 12:19:37 -07002424 synchronized (this) {
2425 pw.println("NetworkTranstionWakeLock is currently " +
2426 (mNetTransitionWakeLock.isHeld() ? "" : "not ") + "held.");
2427 pw.println("It was last requested for "+mNetTransitionWakeLockCausedBy);
2428 }
2429 pw.println();
2430
Robert Greenwalt8e87f122010-02-11 18:18:40 -08002431 mTethering.dump(fd, pw, args);
Robert Greenwalt0e80be12010-09-20 14:35:25 -07002432
2433 if (mInetLog != null) {
2434 pw.println();
2435 pw.println("Inet condition reports:");
2436 for(int i = 0; i < mInetLog.size(); i++) {
2437 pw.println(mInetLog.get(i));
2438 }
2439 }
The Android Open Source Project28527d22009-03-03 19:31:44 -08002440 }
2441
Robert Greenwalt2034b912009-08-12 16:08:25 -07002442 // must be stateless - things change under us.
Jeff Sharkeya1ef1be2012-07-23 13:19:46 -07002443 private class NetworkStateTrackerHandler extends Handler {
2444 public NetworkStateTrackerHandler(Looper looper) {
Wink Saville775aad62010-09-02 19:23:52 -07002445 super(looper);
2446 }
2447
The Android Open Source Project28527d22009-03-03 19:31:44 -08002448 @Override
2449 public void handleMessage(Message msg) {
2450 NetworkInfo info;
2451 switch (msg.what) {
2452 case NetworkStateTracker.EVENT_STATE_CHANGED:
2453 info = (NetworkInfo) msg.obj;
Robert Greenwalt12c44552009-12-07 11:33:18 -08002454 int type = info.getType();
2455 NetworkInfo.State state = info.getState();
Robert Greenwalt12c44552009-12-07 11:33:18 -08002456
Wink Savillea7d56572011-09-21 11:05:43 -07002457 if (VDBG || (state == NetworkInfo.State.CONNECTED) ||
2458 (state == NetworkInfo.State.DISCONNECTED)) {
2459 log("ConnectivityChange for " +
Robert Greenwalt0659da32009-07-16 17:21:39 -07002460 info.getTypeName() + ": " +
Robert Greenwalt12c44552009-12-07 11:33:18 -08002461 state + "/" + info.getDetailedState());
Wink Savillea7d56572011-09-21 11:05:43 -07002462 }
The Android Open Source Project28527d22009-03-03 19:31:44 -08002463
2464 // Connectivity state changed:
Robert Greenwalt310fa082012-04-25 13:45:02 -07002465 // [31-14] Reserved for future use
2466 // [13-10] Network subtype (for mobile network, as defined
Robert Greenwalt0659da32009-07-16 17:21:39 -07002467 // by TelephonyManager)
Robert Greenwalt310fa082012-04-25 13:45:02 -07002468 // [9-4] Detailed state ordinal (as defined by
Robert Greenwalt0659da32009-07-16 17:21:39 -07002469 // NetworkInfo.DetailedState)
Robert Greenwalt310fa082012-04-25 13:45:02 -07002470 // [3-0] Network type (as defined by ConnectivityManager)
2471 int eventLogParam = (info.getType() & 0xf) |
2472 ((info.getDetailedState().ordinal() & 0x3f) << 4) |
2473 (info.getSubtype() << 10);
Doug Zongker2fc96232009-12-04 10:31:43 -08002474 EventLog.writeEvent(EventLogTags.CONNECTIVITY_STATE_CHANGED,
Robert Greenwalt0659da32009-07-16 17:21:39 -07002475 eventLogParam);
2476
2477 if (info.getDetailedState() ==
2478 NetworkInfo.DetailedState.FAILED) {
The Android Open Source Project28527d22009-03-03 19:31:44 -08002479 handleConnectionFailure(info);
Robert Greenwalt12c44552009-12-07 11:33:18 -08002480 } else if (state == NetworkInfo.State.DISCONNECTED) {
The Android Open Source Project28527d22009-03-03 19:31:44 -08002481 handleDisconnect(info);
Robert Greenwalt12c44552009-12-07 11:33:18 -08002482 } else if (state == NetworkInfo.State.SUSPENDED) {
The Android Open Source Project28527d22009-03-03 19:31:44 -08002483 // TODO: need to think this over.
Robert Greenwalt0659da32009-07-16 17:21:39 -07002484 // the logic here is, handle SUSPENDED the same as
2485 // DISCONNECTED. The only difference being we are
2486 // broadcasting an intent with NetworkInfo that's
2487 // suspended. This allows the applications an
2488 // opportunity to handle DISCONNECTED and SUSPENDED
2489 // differently, or not.
The Android Open Source Project28527d22009-03-03 19:31:44 -08002490 handleDisconnect(info);
Robert Greenwalt12c44552009-12-07 11:33:18 -08002491 } else if (state == NetworkInfo.State.CONNECTED) {
The Android Open Source Project28527d22009-03-03 19:31:44 -08002492 handleConnect(info);
2493 }
The Android Open Source Project28527d22009-03-03 19:31:44 -08002494 break;
The Android Open Source Project28527d22009-03-03 19:31:44 -08002495 case NetworkStateTracker.EVENT_CONFIGURATION_CHANGED:
Robert Greenwalt3afbead2010-07-23 15:46:26 -07002496 info = (NetworkInfo) msg.obj;
Kazuhiro Ondo07680062011-06-22 21:10:34 -05002497 // TODO: Temporary allowing network configuration
2498 // change not resetting sockets.
2499 // @see bug/4455071
2500 handleConnectivityChange(info.getType(), false);
The Android Open Source Project28527d22009-03-03 19:31:44 -08002501 break;
Jeff Sharkeya1ef1be2012-07-23 13:19:46 -07002502 }
2503 }
2504 }
2505
2506 private class InternalHandler extends Handler {
2507 public InternalHandler(Looper looper) {
2508 super(looper);
2509 }
2510
2511 @Override
2512 public void handleMessage(Message msg) {
2513 NetworkInfo info;
2514 switch (msg.what) {
Robert Greenwaltccb36f92010-09-24 14:32:21 -07002515 case EVENT_CLEAR_NET_TRANSITION_WAKELOCK:
Robert Greenwalt93dc1042010-06-15 12:19:37 -07002516 String causedBy = null;
2517 synchronized (ConnectivityService.this) {
2518 if (msg.arg1 == mNetTransitionWakeLockSerialNumber &&
2519 mNetTransitionWakeLock.isHeld()) {
2520 mNetTransitionWakeLock.release();
2521 causedBy = mNetTransitionWakeLockCausedBy;
2522 }
2523 }
2524 if (causedBy != null) {
Wink Savillee70c6f52010-12-03 12:01:38 -08002525 log("NetTransition Wakelock for " + causedBy + " released by timeout");
Robert Greenwalt93dc1042010-06-15 12:19:37 -07002526 }
Robert Greenwaltcf1a56c2010-09-09 14:05:10 -07002527 break;
Robert Greenwalt6a2db8a2010-09-23 10:05:56 -07002528 case EVENT_RESTORE_DEFAULT_NETWORK:
Robert Greenwaltaffc3a12009-09-27 17:27:04 -07002529 FeatureUser u = (FeatureUser)msg.obj;
2530 u.expire();
Robert Greenwalt986c7412010-09-08 15:24:47 -07002531 break;
Robert Greenwalt6a2db8a2010-09-23 10:05:56 -07002532 case EVENT_INET_CONDITION_CHANGE:
2533 {
2534 int netType = msg.arg1;
2535 int condition = msg.arg2;
2536 handleInetConditionChange(netType, condition);
Robert Greenwalt93dc1042010-06-15 12:19:37 -07002537 break;
Robert Greenwalt6a2db8a2010-09-23 10:05:56 -07002538 }
2539 case EVENT_INET_CONDITION_HOLD_END:
2540 {
2541 int netType = msg.arg1;
2542 int sequence = msg.arg2;
2543 handleInetConditionHoldEnd(netType, sequence);
Robert Greenwalt986c7412010-09-08 15:24:47 -07002544 break;
Robert Greenwalt6a2db8a2010-09-23 10:05:56 -07002545 }
2546 case EVENT_SET_NETWORK_PREFERENCE:
2547 {
2548 int preference = msg.arg1;
2549 handleSetNetworkPreference(preference);
2550 break;
2551 }
Robert Greenwalt6a2db8a2010-09-23 10:05:56 -07002552 case EVENT_SET_MOBILE_DATA:
2553 {
2554 boolean enabled = (msg.arg1 == ENABLED);
2555 handleSetMobileData(enabled);
2556 break;
2557 }
Robert Greenwaltc3c5f862010-10-11 16:00:27 -07002558 case EVENT_APPLY_GLOBAL_HTTP_PROXY:
2559 {
2560 handleDeprecatedGlobalHttpProxy();
Robert Greenwalt34848c02011-03-25 13:09:25 -07002561 break;
2562 }
2563 case EVENT_SET_DEPENDENCY_MET:
2564 {
2565 boolean met = (msg.arg1 == ENABLED);
2566 handleSetDependencyMet(msg.arg2, met);
2567 break;
Robert Greenwaltc3c5f862010-10-11 16:00:27 -07002568 }
Chia-chi Yehcc844502011-07-14 18:01:57 -07002569 case EVENT_RESTORE_DNS:
2570 {
2571 if (mActiveDefaultNetwork != -1) {
2572 handleDnsConfigurationChange(mActiveDefaultNetwork);
2573 }
2574 break;
2575 }
Wink Saville4f0de1e2011-08-04 15:01:58 -07002576 case EVENT_SEND_STICKY_BROADCAST_INTENT:
2577 {
2578 Intent intent = (Intent)msg.obj;
Wink Saville4f0de1e2011-08-04 15:01:58 -07002579 sendStickyBroadcast(intent);
2580 break;
2581 }
Jeff Sharkey805662d2011-08-19 02:24:24 -07002582 case EVENT_SET_POLICY_DATA_ENABLE: {
2583 final int networkType = msg.arg1;
2584 final boolean enabled = msg.arg2 == ENABLED;
2585 handleSetPolicyDataEnable(networkType, enabled);
2586 }
The Android Open Source Project28527d22009-03-03 19:31:44 -08002587 }
2588 }
2589 }
Robert Greenwalt0c4828c2010-01-26 11:40:34 -08002590
2591 // javadoc from interface
Robert Greenwalt4283ded2010-03-02 17:25:02 -08002592 public int tether(String iface) {
Robert Greenwalt0c4828c2010-01-26 11:40:34 -08002593 enforceTetherChangePermission();
Robert Greenwalt4283ded2010-03-02 17:25:02 -08002594
2595 if (isTetheringSupported()) {
2596 return mTethering.tether(iface);
2597 } else {
2598 return ConnectivityManager.TETHER_ERROR_UNSUPPORTED;
2599 }
Robert Greenwalt0c4828c2010-01-26 11:40:34 -08002600 }
2601
2602 // javadoc from interface
Robert Greenwalt4283ded2010-03-02 17:25:02 -08002603 public int untether(String iface) {
Robert Greenwalt0c4828c2010-01-26 11:40:34 -08002604 enforceTetherChangePermission();
Robert Greenwalt4283ded2010-03-02 17:25:02 -08002605
2606 if (isTetheringSupported()) {
2607 return mTethering.untether(iface);
2608 } else {
2609 return ConnectivityManager.TETHER_ERROR_UNSUPPORTED;
2610 }
2611 }
2612
2613 // javadoc from interface
2614 public int getLastTetherError(String iface) {
2615 enforceTetherAccessPermission();
2616
2617 if (isTetheringSupported()) {
2618 return mTethering.getLastTetherError(iface);
2619 } else {
2620 return ConnectivityManager.TETHER_ERROR_UNSUPPORTED;
2621 }
Robert Greenwalt8e87f122010-02-11 18:18:40 -08002622 }
2623
2624 // TODO - proper iface API for selection by property, inspection, etc
2625 public String[] getTetherableUsbRegexs() {
2626 enforceTetherAccessPermission();
2627 if (isTetheringSupported()) {
2628 return mTethering.getTetherableUsbRegexs();
2629 } else {
2630 return new String[0];
2631 }
2632 }
2633
2634 public String[] getTetherableWifiRegexs() {
2635 enforceTetherAccessPermission();
2636 if (isTetheringSupported()) {
2637 return mTethering.getTetherableWifiRegexs();
2638 } else {
2639 return new String[0];
2640 }
Robert Greenwalt0c4828c2010-01-26 11:40:34 -08002641 }
2642
Danica Chang96567052010-08-11 14:54:43 -07002643 public String[] getTetherableBluetoothRegexs() {
2644 enforceTetherAccessPermission();
2645 if (isTetheringSupported()) {
2646 return mTethering.getTetherableBluetoothRegexs();
2647 } else {
2648 return new String[0];
2649 }
2650 }
2651
Mike Lockwooded4a1742011-07-19 13:04:47 -07002652 public int setUsbTethering(boolean enable) {
2653 enforceTetherAccessPermission();
2654 if (isTetheringSupported()) {
2655 return mTethering.setUsbTethering(enable);
2656 } else {
2657 return ConnectivityManager.TETHER_ERROR_UNSUPPORTED;
2658 }
2659 }
2660
Robert Greenwalt0c4828c2010-01-26 11:40:34 -08002661 // TODO - move iface listing, queries, etc to new module
2662 // javadoc from interface
2663 public String[] getTetherableIfaces() {
Robert Greenwalt8e87f122010-02-11 18:18:40 -08002664 enforceTetherAccessPermission();
Robert Greenwalt0c4828c2010-01-26 11:40:34 -08002665 return mTethering.getTetherableIfaces();
2666 }
2667
2668 public String[] getTetheredIfaces() {
Robert Greenwalt8e87f122010-02-11 18:18:40 -08002669 enforceTetherAccessPermission();
Robert Greenwalt0c4828c2010-01-26 11:40:34 -08002670 return mTethering.getTetheredIfaces();
2671 }
Robert Greenwalt8e87f122010-02-11 18:18:40 -08002672
Jeff Sharkey300f08f2011-09-16 01:52:49 -07002673 @Override
2674 public String[] getTetheredIfacePairs() {
2675 enforceTetherAccessPermission();
2676 return mTethering.getTetheredIfacePairs();
2677 }
2678
Robert Greenwalt4283ded2010-03-02 17:25:02 -08002679 public String[] getTetheringErroredIfaces() {
2680 enforceTetherAccessPermission();
2681 return mTethering.getErroredIfaces();
2682 }
2683
Robert Greenwalt8e87f122010-02-11 18:18:40 -08002684 // if ro.tether.denied = true we default to no tethering
2685 // gservices could set the secure setting to 1 though to enable it on a build where it
2686 // had previously been turned off.
2687 public boolean isTetheringSupported() {
2688 enforceTetherAccessPermission();
2689 int defaultVal = (SystemProperties.get("ro.tether.denied").equals("true") ? 0 : 1);
Robert Greenwaltf1b66e12010-02-25 12:29:30 -08002690 boolean tetherEnabledInSettings = (Settings.Secure.getInt(mContext.getContentResolver(),
2691 Settings.Secure.TETHER_SUPPORTED, defaultVal) != 0);
2692 return tetherEnabledInSettings && mTetheringConfigValid;
Robert Greenwalt8e87f122010-02-11 18:18:40 -08002693 }
Robert Greenwalt93dc1042010-06-15 12:19:37 -07002694
2695 // An API NetworkStateTrackers can call when they lose their network.
2696 // This will automatically be cleared after X seconds or a network becomes CONNECTED,
2697 // whichever happens first. The timer is started by the first caller and not
2698 // restarted by subsequent callers.
2699 public void requestNetworkTransitionWakelock(String forWhom) {
2700 enforceConnectivityInternalPermission();
2701 synchronized (this) {
2702 if (mNetTransitionWakeLock.isHeld()) return;
2703 mNetTransitionWakeLockSerialNumber++;
2704 mNetTransitionWakeLock.acquire();
2705 mNetTransitionWakeLockCausedBy = forWhom;
2706 }
2707 mHandler.sendMessageDelayed(mHandler.obtainMessage(
Robert Greenwaltccb36f92010-09-24 14:32:21 -07002708 EVENT_CLEAR_NET_TRANSITION_WAKELOCK,
Robert Greenwalt93dc1042010-06-15 12:19:37 -07002709 mNetTransitionWakeLockSerialNumber, 0),
2710 mNetTransitionWakeLockTimeout);
2711 return;
2712 }
Robert Greenwalt24118e82010-09-09 13:15:32 -07002713
Robert Greenwalt986c7412010-09-08 15:24:47 -07002714 // 100 percent is full good, 0 is full bad.
2715 public void reportInetCondition(int networkType, int percentage) {
Robert Greenwalt78f28112011-08-02 17:18:41 -07002716 if (VDBG) log("reportNetworkCondition(" + networkType + ", " + percentage + ")");
Robert Greenwalt986c7412010-09-08 15:24:47 -07002717 mContext.enforceCallingOrSelfPermission(
2718 android.Manifest.permission.STATUS_BAR,
2719 "ConnectivityService");
2720
Robert Greenwalt0e80be12010-09-20 14:35:25 -07002721 if (DBG) {
2722 int pid = getCallingPid();
2723 int uid = getCallingUid();
2724 String s = pid + "(" + uid + ") reports inet is " +
2725 (percentage > 50 ? "connected" : "disconnected") + " (" + percentage + ") on " +
2726 "network Type " + networkType + " at " + GregorianCalendar.getInstance().getTime();
2727 mInetLog.add(s);
2728 while(mInetLog.size() > INET_CONDITION_LOG_MAX_SIZE) {
2729 mInetLog.remove(0);
2730 }
2731 }
Robert Greenwalt986c7412010-09-08 15:24:47 -07002732 mHandler.sendMessage(mHandler.obtainMessage(
Robert Greenwalt6a2db8a2010-09-23 10:05:56 -07002733 EVENT_INET_CONDITION_CHANGE, networkType, percentage));
2734 }
2735
2736 private void handleInetConditionChange(int netType, int condition) {
Robert Greenwalt6a2db8a2010-09-23 10:05:56 -07002737 if (mActiveDefaultNetwork == -1) {
Wink Savillea7d56572011-09-21 11:05:43 -07002738 if (DBG) log("handleInetConditionChange: no active default network - ignore");
Robert Greenwalt6a2db8a2010-09-23 10:05:56 -07002739 return;
2740 }
2741 if (mActiveDefaultNetwork != netType) {
Wink Savillea7d56572011-09-21 11:05:43 -07002742 if (DBG) log("handleInetConditionChange: net=" + netType +
2743 " != default=" + mActiveDefaultNetwork + " - ignore");
Robert Greenwalt6a2db8a2010-09-23 10:05:56 -07002744 return;
2745 }
Wink Savillea7d56572011-09-21 11:05:43 -07002746 if (VDBG) {
2747 log("handleInetConditionChange: net=" +
2748 netType + ", condition=" + condition +
2749 ",mActiveDefaultNetwork=" + mActiveDefaultNetwork);
2750 }
Robert Greenwalt6a2db8a2010-09-23 10:05:56 -07002751 mDefaultInetCondition = condition;
2752 int delay;
2753 if (mInetConditionChangeInFlight == false) {
Wink Savillea7d56572011-09-21 11:05:43 -07002754 if (VDBG) log("handleInetConditionChange: starting a change hold");
Robert Greenwalt6a2db8a2010-09-23 10:05:56 -07002755 // setup a new hold to debounce this
2756 if (mDefaultInetCondition > 50) {
2757 delay = Settings.Secure.getInt(mContext.getContentResolver(),
2758 Settings.Secure.INET_CONDITION_DEBOUNCE_UP_DELAY, 500);
2759 } else {
2760 delay = Settings.Secure.getInt(mContext.getContentResolver(),
2761 Settings.Secure.INET_CONDITION_DEBOUNCE_DOWN_DELAY, 3000);
2762 }
2763 mInetConditionChangeInFlight = true;
2764 mHandler.sendMessageDelayed(mHandler.obtainMessage(EVENT_INET_CONDITION_HOLD_END,
2765 mActiveDefaultNetwork, mDefaultConnectionSequence), delay);
2766 } else {
Wink Savillea7d56572011-09-21 11:05:43 -07002767 // we've set the new condition, when this hold ends that will get picked up
2768 if (VDBG) log("handleInetConditionChange: currently in hold - not setting new end evt");
Robert Greenwalt6a2db8a2010-09-23 10:05:56 -07002769 }
2770 }
2771
2772 private void handleInetConditionHoldEnd(int netType, int sequence) {
Wink Savillea7d56572011-09-21 11:05:43 -07002773 if (DBG) {
2774 log("handleInetConditionHoldEnd: net=" + netType +
2775 ", condition=" + mDefaultInetCondition +
2776 ", published condition=" + mDefaultInetConditionPublished);
Robert Greenwalt6a2db8a2010-09-23 10:05:56 -07002777 }
2778 mInetConditionChangeInFlight = false;
2779
2780 if (mActiveDefaultNetwork == -1) {
Wink Savillea7d56572011-09-21 11:05:43 -07002781 if (DBG) log("handleInetConditionHoldEnd: no active default network - ignoring");
Robert Greenwalt6a2db8a2010-09-23 10:05:56 -07002782 return;
2783 }
2784 if (mDefaultConnectionSequence != sequence) {
Wink Savillea7d56572011-09-21 11:05:43 -07002785 if (DBG) log("handleInetConditionHoldEnd: event hold for obsolete network - ignoring");
Robert Greenwalt6a2db8a2010-09-23 10:05:56 -07002786 return;
2787 }
Wink Saville4f0de1e2011-08-04 15:01:58 -07002788 // TODO: Figure out why this optimization sometimes causes a
2789 // change in mDefaultInetCondition to be missed and the
2790 // UI to not be updated.
2791 //if (mDefaultInetConditionPublished == mDefaultInetCondition) {
2792 // if (DBG) log("no change in condition - aborting");
2793 // return;
2794 //}
Robert Greenwalt6a2db8a2010-09-23 10:05:56 -07002795 NetworkInfo networkInfo = mNetTrackers[mActiveDefaultNetwork].getNetworkInfo();
2796 if (networkInfo.isConnected() == false) {
Wink Savillea7d56572011-09-21 11:05:43 -07002797 if (DBG) log("handleInetConditionHoldEnd: default network not connected - ignoring");
Robert Greenwalt6a2db8a2010-09-23 10:05:56 -07002798 return;
2799 }
2800 mDefaultInetConditionPublished = mDefaultInetCondition;
2801 sendInetConditionBroadcast(networkInfo);
2802 return;
Robert Greenwalt986c7412010-09-08 15:24:47 -07002803 }
Robert Greenwaltc3c5f862010-10-11 16:00:27 -07002804
Chia-chi Yeh763a11c2011-10-03 15:34:04 -07002805 public ProxyProperties getProxy() {
2806 synchronized (mDefaultProxyLock) {
2807 return mDefaultProxyDisabled ? null : mDefaultProxy;
2808 }
Robert Greenwaltc3c5f862010-10-11 16:00:27 -07002809 }
2810
2811 public void setGlobalProxy(ProxyProperties proxyProperties) {
2812 enforceChangePermission();
2813 synchronized (mGlobalProxyLock) {
2814 if (proxyProperties == mGlobalProxy) return;
2815 if (proxyProperties != null && proxyProperties.equals(mGlobalProxy)) return;
2816 if (mGlobalProxy != null && mGlobalProxy.equals(proxyProperties)) return;
2817
2818 String host = "";
2819 int port = 0;
2820 String exclList = "";
2821 if (proxyProperties != null && !TextUtils.isEmpty(proxyProperties.getHost())) {
2822 mGlobalProxy = new ProxyProperties(proxyProperties);
2823 host = mGlobalProxy.getHost();
2824 port = mGlobalProxy.getPort();
2825 exclList = mGlobalProxy.getExclusionList();
2826 } else {
2827 mGlobalProxy = null;
2828 }
2829 ContentResolver res = mContext.getContentResolver();
2830 Settings.Secure.putString(res, Settings.Secure.GLOBAL_HTTP_PROXY_HOST, host);
2831 Settings.Secure.putInt(res, Settings.Secure.GLOBAL_HTTP_PROXY_PORT, port);
Robert Greenwalt6f7c6092010-12-02 11:31:00 -08002832 Settings.Secure.putString(res, Settings.Secure.GLOBAL_HTTP_PROXY_EXCLUSION_LIST,
Robert Greenwaltc3c5f862010-10-11 16:00:27 -07002833 exclList);
2834 }
2835
2836 if (mGlobalProxy == null) {
2837 proxyProperties = mDefaultProxy;
2838 }
Chia-chi Yehae166612011-09-28 16:38:18 -07002839 //sendProxyBroadcast(proxyProperties);
Robert Greenwaltc3c5f862010-10-11 16:00:27 -07002840 }
2841
Robert Greenwalt6f7c6092010-12-02 11:31:00 -08002842 private void loadGlobalProxy() {
2843 ContentResolver res = mContext.getContentResolver();
2844 String host = Settings.Secure.getString(res, Settings.Secure.GLOBAL_HTTP_PROXY_HOST);
2845 int port = Settings.Secure.getInt(res, Settings.Secure.GLOBAL_HTTP_PROXY_PORT, 0);
2846 String exclList = Settings.Secure.getString(res,
2847 Settings.Secure.GLOBAL_HTTP_PROXY_EXCLUSION_LIST);
2848 if (!TextUtils.isEmpty(host)) {
2849 ProxyProperties proxyProperties = new ProxyProperties(host, port, exclList);
2850 synchronized (mGlobalProxyLock) {
2851 mGlobalProxy = proxyProperties;
2852 }
2853 }
2854 }
2855
Robert Greenwaltc3c5f862010-10-11 16:00:27 -07002856 public ProxyProperties getGlobalProxy() {
2857 synchronized (mGlobalProxyLock) {
2858 return mGlobalProxy;
2859 }
2860 }
2861
Chia-chi Yeh763a11c2011-10-03 15:34:04 -07002862 private void handleApplyDefaultProxy(ProxyProperties proxy) {
2863 if (proxy != null && TextUtils.isEmpty(proxy.getHost())) {
2864 proxy = null;
2865 }
2866 synchronized (mDefaultProxyLock) {
Robert Greenwaltc3c5f862010-10-11 16:00:27 -07002867 if (mDefaultProxy != null && mDefaultProxy.equals(proxy)) return;
2868 if (mDefaultProxy == proxy) return;
Chia-chi Yeh763a11c2011-10-03 15:34:04 -07002869 mDefaultProxy = proxy;
2870
2871 if (!mDefaultProxyDisabled) {
2872 sendProxyBroadcast(proxy);
Robert Greenwaltc3c5f862010-10-11 16:00:27 -07002873 }
2874 }
Robert Greenwaltc3c5f862010-10-11 16:00:27 -07002875 }
2876
2877 private void handleDeprecatedGlobalHttpProxy() {
2878 String proxy = Settings.Secure.getString(mContext.getContentResolver(),
2879 Settings.Secure.HTTP_PROXY);
2880 if (!TextUtils.isEmpty(proxy)) {
2881 String data[] = proxy.split(":");
2882 String proxyHost = data[0];
2883 int proxyPort = 8080;
2884 if (data.length > 1) {
2885 try {
2886 proxyPort = Integer.parseInt(data[1]);
2887 } catch (NumberFormatException e) {
2888 return;
2889 }
2890 }
2891 ProxyProperties p = new ProxyProperties(data[0], proxyPort, "");
2892 setGlobalProxy(p);
2893 }
2894 }
2895
2896 private void sendProxyBroadcast(ProxyProperties proxy) {
Robert Greenwalt611291c2010-12-23 15:51:10 -08002897 if (proxy == null) proxy = new ProxyProperties("", 0, "");
Robert Greenwalt78f28112011-08-02 17:18:41 -07002898 if (DBG) log("sending Proxy Broadcast for " + proxy);
Robert Greenwaltc3c5f862010-10-11 16:00:27 -07002899 Intent intent = new Intent(Proxy.PROXY_CHANGE_ACTION);
Stan Chesnutt1f2a2ac2011-01-06 11:00:19 -08002900 intent.addFlags(Intent.FLAG_RECEIVER_REPLACE_PENDING |
2901 Intent.FLAG_RECEIVER_REGISTERED_ONLY_BEFORE_BOOT);
Robert Greenwaltc3c5f862010-10-11 16:00:27 -07002902 intent.putExtra(Proxy.EXTRA_PROXY_INFO, proxy);
Robert Greenwaltd93dc8f2010-12-06 11:29:17 -08002903 mContext.sendStickyBroadcast(intent);
Robert Greenwaltc3c5f862010-10-11 16:00:27 -07002904 }
2905
2906 private static class SettingsObserver extends ContentObserver {
2907 private int mWhat;
2908 private Handler mHandler;
2909 SettingsObserver(Handler handler, int what) {
2910 super(handler);
2911 mHandler = handler;
2912 mWhat = what;
2913 }
2914
2915 void observe(Context context) {
2916 ContentResolver resolver = context.getContentResolver();
2917 resolver.registerContentObserver(Settings.Secure.getUriFor(
2918 Settings.Secure.HTTP_PROXY), false, this);
2919 }
2920
2921 @Override
2922 public void onChange(boolean selfChange) {
2923 mHandler.obtainMessage(mWhat).sendToTarget();
2924 }
2925 }
Wink Savillee70c6f52010-12-03 12:01:38 -08002926
2927 private void log(String s) {
2928 Slog.d(TAG, s);
2929 }
2930
2931 private void loge(String s) {
2932 Slog.e(TAG, s);
2933 }
Chia-chi Yeh9a4ad7d2011-05-23 17:26:46 -07002934
repo syncf5de5572011-07-29 23:55:49 -07002935 int convertFeatureToNetworkType(int networkType, String feature) {
2936 int usedNetworkType = networkType;
2937
2938 if(networkType == ConnectivityManager.TYPE_MOBILE) {
2939 if (TextUtils.equals(feature, Phone.FEATURE_ENABLE_MMS)) {
2940 usedNetworkType = ConnectivityManager.TYPE_MOBILE_MMS;
2941 } else if (TextUtils.equals(feature, Phone.FEATURE_ENABLE_SUPL)) {
2942 usedNetworkType = ConnectivityManager.TYPE_MOBILE_SUPL;
2943 } else if (TextUtils.equals(feature, Phone.FEATURE_ENABLE_DUN) ||
2944 TextUtils.equals(feature, Phone.FEATURE_ENABLE_DUN_ALWAYS)) {
2945 usedNetworkType = ConnectivityManager.TYPE_MOBILE_DUN;
2946 } else if (TextUtils.equals(feature, Phone.FEATURE_ENABLE_HIPRI)) {
2947 usedNetworkType = ConnectivityManager.TYPE_MOBILE_HIPRI;
2948 } else if (TextUtils.equals(feature, Phone.FEATURE_ENABLE_FOTA)) {
2949 usedNetworkType = ConnectivityManager.TYPE_MOBILE_FOTA;
2950 } else if (TextUtils.equals(feature, Phone.FEATURE_ENABLE_IMS)) {
2951 usedNetworkType = ConnectivityManager.TYPE_MOBILE_IMS;
2952 } else if (TextUtils.equals(feature, Phone.FEATURE_ENABLE_CBS)) {
2953 usedNetworkType = ConnectivityManager.TYPE_MOBILE_CBS;
2954 } else {
2955 Slog.e(TAG, "Can't match any mobile netTracker!");
2956 }
2957 } else if (networkType == ConnectivityManager.TYPE_WIFI) {
2958 if (TextUtils.equals(feature, "p2p")) {
2959 usedNetworkType = ConnectivityManager.TYPE_WIFI_P2P;
2960 } else {
2961 Slog.e(TAG, "Can't match any wifi netTracker!");
2962 }
2963 } else {
2964 Slog.e(TAG, "Unexpected network type");
Wink Savillef6b76692011-02-24 17:58:51 -08002965 }
repo syncf5de5572011-07-29 23:55:49 -07002966 return usedNetworkType;
Wink Savillef6b76692011-02-24 17:58:51 -08002967 }
Jeff Sharkey921ebf22011-05-19 17:12:49 -07002968
2969 private static <T> T checkNotNull(T value, String message) {
2970 if (value == null) {
2971 throw new NullPointerException(message);
2972 }
2973 return value;
2974 }
Chia-chi Yeh9a4ad7d2011-05-23 17:26:46 -07002975
Chia-chi Yeh75cacd52011-06-15 17:07:27 -07002976 /**
2977 * Protect a socket from VPN routing rules. This method is used by
Chia-chi Yehbded3eb2011-07-04 03:23:12 -07002978 * VpnBuilder and not available in ConnectivityManager. Permissions
2979 * are checked in Vpn class.
Chia-chi Yeh75cacd52011-06-15 17:07:27 -07002980 * @hide
2981 */
Chia-chi Yeh9a4ad7d2011-05-23 17:26:46 -07002982 @Override
Chia-chi Yeh9e4ff6e2011-07-14 16:19:19 -07002983 public boolean protectVpn(ParcelFileDescriptor socket) {
2984 try {
2985 int type = mActiveDefaultNetwork;
2986 if (ConnectivityManager.isNetworkTypeValid(type)) {
2987 mVpn.protect(socket, mNetTrackers[type].getLinkProperties().getInterfaceName());
2988 return true;
2989 }
2990 } catch (Exception e) {
2991 // ignore
2992 } finally {
2993 try {
2994 socket.close();
2995 } catch (Exception e) {
2996 // ignore
2997 }
2998 }
2999 return false;
Chia-chi Yeh9a4ad7d2011-05-23 17:26:46 -07003000 }
3001
Chia-chi Yeh75cacd52011-06-15 17:07:27 -07003002 /**
3003 * Prepare for a VPN application. This method is used by VpnDialogs
Chia-chi Yehbded3eb2011-07-04 03:23:12 -07003004 * and not available in ConnectivityManager. Permissions are checked
3005 * in Vpn class.
Chia-chi Yeh75cacd52011-06-15 17:07:27 -07003006 * @hide
3007 */
Chia-chi Yeh9a4ad7d2011-05-23 17:26:46 -07003008 @Override
Chia-chi Yeh3e2e1da2011-07-03 16:52:38 -07003009 public boolean prepareVpn(String oldPackage, String newPackage) {
3010 return mVpn.prepare(oldPackage, newPackage);
Chia-chi Yeh9a4ad7d2011-05-23 17:26:46 -07003011 }
3012
Chia-chi Yeh75cacd52011-06-15 17:07:27 -07003013 /**
3014 * Configure a TUN interface and return its file descriptor. Parameters
3015 * are encoded and opaque to this class. This method is used by VpnBuilder
Chia-chi Yehbded3eb2011-07-04 03:23:12 -07003016 * and not available in ConnectivityManager. Permissions are checked in
3017 * Vpn class.
Chia-chi Yeh75cacd52011-06-15 17:07:27 -07003018 * @hide
3019 */
Chia-chi Yeh9a4ad7d2011-05-23 17:26:46 -07003020 @Override
Chia-chi Yeh75cacd52011-06-15 17:07:27 -07003021 public ParcelFileDescriptor establishVpn(VpnConfig config) {
Chia-chi Yeh9a4ad7d2011-05-23 17:26:46 -07003022 return mVpn.establish(config);
3023 }
3024
Chia-chi Yeh7e026b32011-07-02 17:15:00 -07003025 /**
Chia-chi Yehbded3eb2011-07-04 03:23:12 -07003026 * Start legacy VPN and return an intent to VpnDialogs. This method is
3027 * used by VpnSettings and not available in ConnectivityManager.
3028 * Permissions are checked in Vpn class.
Chia-chi Yeh7e026b32011-07-02 17:15:00 -07003029 * @hide
3030 */
3031 @Override
Chia-chi Yehbded3eb2011-07-04 03:23:12 -07003032 public void startLegacyVpn(VpnConfig config, String[] racoon, String[] mtpd) {
3033 mVpn.startLegacyVpn(config, racoon, mtpd);
3034 }
3035
3036 /**
3037 * Return the information of the ongoing legacy VPN. This method is used
3038 * by VpnSettings and not available in ConnectivityManager. Permissions
3039 * are checked in Vpn class.
3040 * @hide
3041 */
3042 @Override
3043 public LegacyVpnInfo getLegacyVpnInfo() {
3044 return mVpn.getLegacyVpnInfo();
Chia-chi Yeh7e026b32011-07-02 17:15:00 -07003045 }
3046
Chia-chi Yeh9a4ad7d2011-05-23 17:26:46 -07003047 /**
3048 * Callback for VPN subsystem. Currently VPN is not adapted to the service
3049 * through NetworkStateTracker since it works differently. For example, it
3050 * needs to override DNS servers but never takes the default routes. It
3051 * relies on another data network, and it could keep existing connections
3052 * alive after reconnecting, switching between networks, or even resuming
3053 * from deep sleep. Calls from applications should be done synchronously
3054 * to avoid race conditions. As these are all hidden APIs, refactoring can
3055 * be done whenever a better abstraction is developed.
3056 */
3057 public class VpnCallback {
3058
3059 private VpnCallback() {
3060 }
3061
Chia-chi Yehcc844502011-07-14 18:01:57 -07003062 public void override(List<String> dnsServers, List<String> searchDomains) {
3063 if (dnsServers == null) {
3064 restore();
3065 return;
3066 }
3067
3068 // Convert DNS servers into addresses.
3069 List<InetAddress> addresses = new ArrayList<InetAddress>();
3070 for (String address : dnsServers) {
3071 // Double check the addresses and remove invalid ones.
3072 try {
3073 addresses.add(InetAddress.parseNumericAddress(address));
3074 } catch (Exception e) {
3075 // ignore
3076 }
3077 }
3078 if (addresses.isEmpty()) {
3079 restore();
3080 return;
3081 }
3082
3083 // Concatenate search domains into a string.
3084 StringBuilder buffer = new StringBuilder();
3085 if (searchDomains != null) {
3086 for (String domain : searchDomains) {
3087 buffer.append(domain).append(' ');
3088 }
3089 }
3090 String domains = buffer.toString().trim();
3091
3092 // Apply DNS changes.
3093 boolean changed = false;
3094 synchronized (mDnsLock) {
Lorenzo Colittiaa035382011-09-28 22:31:45 -07003095 changed = updateDns("VPN", "VPN", addresses, domains);
Chia-chi Yehcc844502011-07-14 18:01:57 -07003096 mDnsOverridden = true;
3097 }
3098 if (changed) {
3099 bumpDns();
3100 }
3101
Chia-chi Yeh763a11c2011-10-03 15:34:04 -07003102 // Temporarily disable the default proxy.
3103 synchronized (mDefaultProxyLock) {
3104 mDefaultProxyDisabled = true;
3105 if (mDefaultProxy != null) {
3106 sendProxyBroadcast(null);
3107 }
3108 }
3109
3110 // TODO: support proxy per network.
Chia-chi Yeh9a4ad7d2011-05-23 17:26:46 -07003111 }
3112
Chia-chi Yehcc844502011-07-14 18:01:57 -07003113 public void restore() {
3114 synchronized (mDnsLock) {
Chia-chi Yeh763a11c2011-10-03 15:34:04 -07003115 if (mDnsOverridden) {
3116 mDnsOverridden = false;
3117 mHandler.sendEmptyMessage(EVENT_RESTORE_DNS);
Chia-chi Yehcc844502011-07-14 18:01:57 -07003118 }
Chia-chi Yehcc844502011-07-14 18:01:57 -07003119 }
Chia-chi Yeh763a11c2011-10-03 15:34:04 -07003120 synchronized (mDefaultProxyLock) {
3121 mDefaultProxyDisabled = false;
3122 if (mDefaultProxy != null) {
3123 sendProxyBroadcast(mDefaultProxy);
3124 }
3125 }
Chia-chi Yeh9a4ad7d2011-05-23 17:26:46 -07003126 }
3127 }
The Android Open Source Project28527d22009-03-03 19:31:44 -08003128}