2 * Copyright (C) 2008 The Android Open Source Project
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
8 * http://www.apache.org/licenses/LICENSE-2.0
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.
17 package com.android.server;
19 import android.bluetooth.BluetoothTetheringDataTracker;
20 import android.content.ContentResolver;
21 import android.content.Context;
22 import android.content.Intent;
23 import android.content.pm.PackageManager;
24 import android.database.ContentObserver;
25 import android.net.ConnectivityManager;
26 import android.net.DummyDataStateTracker;
27 import android.net.EthernetDataTracker;
28 import android.net.IConnectivityManager;
29 import android.net.LinkAddress;
30 import android.net.LinkProperties;
31 import android.net.MobileDataStateTracker;
32 import android.net.NetworkConfig;
33 import android.net.NetworkInfo;
34 import android.net.NetworkStateTracker;
35 import android.net.NetworkUtils;
36 import android.net.Proxy;
37 import android.net.ProxyProperties;
38 import android.net.RouteInfo;
39 import android.net.vpn.VpnManager;
40 import android.net.wifi.WifiStateTracker;
41 import android.os.Binder;
42 import android.os.Handler;
43 import android.os.HandlerThread;
44 import android.os.IBinder;
45 import android.os.INetworkManagementService;
46 import android.os.Looper;
47 import android.os.Message;
48 import android.os.PowerManager;
49 import android.os.RemoteException;
50 import android.os.ServiceManager;
51 import android.os.SystemProperties;
52 import android.provider.Settings;
53 import android.text.TextUtils;
54 import android.util.EventLog;
55 import android.util.Slog;
57 import com.android.internal.telephony.Phone;
58 import com.android.server.connectivity.Tethering;
60 import java.io.FileDescriptor;
61 import java.io.FileWriter;
62 import java.io.IOException;
63 import java.io.PrintWriter;
64 import java.net.InetAddress;
65 import java.net.Inet4Address;
66 import java.net.UnknownHostException;
67 import java.util.ArrayList;
68 import java.util.Collection;
69 import java.util.concurrent.atomic.AtomicBoolean;
70 import java.util.GregorianCalendar;
71 import java.util.List;
76 public class ConnectivityService extends IConnectivityManager.Stub {
78 private static final boolean DBG = true;
79 private static final String TAG = "ConnectivityService";
81 // how long to wait before switching back to a radio's default network
82 private static final int RESTORE_DEFAULT_NETWORK_DELAY = 1 * 60 * 1000;
83 // system property that can override the above value
84 private static final String NETWORK_RESTORE_DELAY_PROP_NAME =
85 "android.telephony.apn-restore";
87 // used in recursive route setting to add gateways for the host for which
88 // a host route was requested.
89 private static final int MAX_HOSTROUTE_CYCLE_COUNT = 10;
91 private Tethering mTethering;
92 private boolean mTetheringConfigValid = false;
95 * Sometimes we want to refer to the individual network state
96 * trackers separately, and sometimes we just want to treat them
99 private NetworkStateTracker mNetTrackers[];
102 * A per Net list of the PID's that requested access to the net
103 * used both as a refcount and for per-PID DNS selection
105 private List mNetRequestersPids[];
107 private WifiWatchdogService mWifiWatchdogService;
109 // priority order of the nettrackers
110 // (excluding dynamically set mNetworkPreference)
111 // TODO - move mNetworkTypePreference into this
112 private int[] mPriorityList;
114 private Context mContext;
115 private int mNetworkPreference;
116 private int mActiveDefaultNetwork = -1;
117 // 0 is full bad, 100 is full good
118 private int mDefaultInetCondition = 0;
119 private int mDefaultInetConditionPublished = 0;
120 private boolean mInetConditionChangeInFlight = false;
121 private int mDefaultConnectionSequence = 0;
123 private int mNumDnsEntries;
125 private boolean mTestMode;
126 private static ConnectivityService sServiceInstance;
128 private AtomicBoolean mBackgroundDataEnabled = new AtomicBoolean(true);
130 private INetworkManagementService mNetd;
132 private static final int ENABLED = 1;
133 private static final int DISABLED = 0;
135 // Share the event space with NetworkStateTracker (which can't see this
136 // internal class but sends us events). If you change these, change
137 // NetworkStateTracker.java too.
138 private static final int MIN_NETWORK_STATE_TRACKER_EVENT = 1;
139 private static final int MAX_NETWORK_STATE_TRACKER_EVENT = 100;
142 * used internally as a delayed event to make us switch back to the
145 private static final int EVENT_RESTORE_DEFAULT_NETWORK =
146 MAX_NETWORK_STATE_TRACKER_EVENT + 1;
149 * used internally to change our mobile data enabled flag
151 private static final int EVENT_CHANGE_MOBILE_DATA_ENABLED =
152 MAX_NETWORK_STATE_TRACKER_EVENT + 2;
155 * used internally to change our network preference setting
156 * arg1 = networkType to prefer
158 private static final int EVENT_SET_NETWORK_PREFERENCE =
159 MAX_NETWORK_STATE_TRACKER_EVENT + 3;
162 * used internally to synchronize inet condition reports
164 * arg2 = condition (0 bad, 100 good)
166 private static final int EVENT_INET_CONDITION_CHANGE =
167 MAX_NETWORK_STATE_TRACKER_EVENT + 4;
170 * used internally to mark the end of inet condition hold periods
173 private static final int EVENT_INET_CONDITION_HOLD_END =
174 MAX_NETWORK_STATE_TRACKER_EVENT + 5;
177 * used internally to set the background data preference
178 * arg1 = TRUE for enabled, FALSE for disabled
180 private static final int EVENT_SET_BACKGROUND_DATA =
181 MAX_NETWORK_STATE_TRACKER_EVENT + 6;
184 * used internally to set enable/disable cellular data
185 * arg1 = ENBALED or DISABLED
187 private static final int EVENT_SET_MOBILE_DATA =
188 MAX_NETWORK_STATE_TRACKER_EVENT + 7;
191 * used internally to clear a wakelock when transitioning
192 * from one net to another
194 private static final int EVENT_CLEAR_NET_TRANSITION_WAKELOCK =
195 MAX_NETWORK_STATE_TRACKER_EVENT + 8;
198 * used internally to reload global proxy settings
200 private static final int EVENT_APPLY_GLOBAL_HTTP_PROXY =
201 MAX_NETWORK_STATE_TRACKER_EVENT + 9;
204 * used internally to set external dependency met/unmet
205 * arg1 = ENABLED (met) or DISABLED (unmet)
208 private static final int EVENT_SET_DEPENDENCY_MET =
209 MAX_NETWORK_STATE_TRACKER_EVENT + 10;
211 private Handler mHandler;
213 // list of DeathRecipients used to make sure features are turned off when
215 private List mFeatureUsers;
217 private boolean mSystemReady;
218 private Intent mInitialBroadcast;
220 private PowerManager.WakeLock mNetTransitionWakeLock;
221 private String mNetTransitionWakeLockCausedBy = "";
222 private int mNetTransitionWakeLockSerialNumber;
223 private int mNetTransitionWakeLockTimeout;
225 private InetAddress mDefaultDns;
227 // used in DBG mode to track inet condition reports
228 private static final int INET_CONDITION_LOG_MAX_SIZE = 15;
229 private ArrayList mInetLog;
231 // track the current default http proxy - tell the world if we get a new one (real change)
232 private ProxyProperties mDefaultProxy = null;
233 // track the global proxy.
234 private ProxyProperties mGlobalProxy = null;
235 private final Object mGlobalProxyLock = new Object();
237 private SettingsObserver mSettingsObserver;
239 NetworkConfig[] mNetConfigs;
240 int mNetworksDefined;
242 private static class RadioAttributes {
243 public int mSimultaneity;
245 public RadioAttributes(String init) {
246 String fragments[] = init.split(",");
247 mType = Integer.parseInt(fragments[0]);
248 mSimultaneity = Integer.parseInt(fragments[1]);
251 RadioAttributes[] mRadioAttributes;
253 // the set of network types that can only be enabled by system/sig apps
254 List mProtectedNetworks;
256 public static synchronized ConnectivityService getInstance(Context context) {
257 if (sServiceInstance == null) {
258 sServiceInstance = new ConnectivityService(context);
260 return sServiceInstance;
263 private ConnectivityService(Context context) {
264 if (DBG) log("ConnectivityService starting up");
266 HandlerThread handlerThread = new HandlerThread("ConnectivityServiceThread");
267 handlerThread.start();
268 mHandler = new MyHandler(handlerThread.getLooper());
270 mBackgroundDataEnabled.set(Settings.Secure.getInt(context.getContentResolver(),
271 Settings.Secure.BACKGROUND_DATA, 1) == 1);
273 // setup our unique device name
274 if (TextUtils.isEmpty(SystemProperties.get("net.hostname"))) {
275 String id = Settings.Secure.getString(context.getContentResolver(),
276 Settings.Secure.ANDROID_ID);
277 if (id != null && id.length() > 0) {
278 String name = new String("android_").concat(id);
279 SystemProperties.set("net.hostname", name);
283 // read our default dns server ip
284 String dns = Settings.Secure.getString(context.getContentResolver(),
285 Settings.Secure.DEFAULT_DNS_SERVER);
286 if (dns == null || dns.length() == 0) {
287 dns = context.getResources().getString(
288 com.android.internal.R.string.config_default_dns_server);
291 mDefaultDns = NetworkUtils.numericToInetAddress(dns);
292 } catch (IllegalArgumentException e) {
293 loge("Error setting defaultDns using " + dns);
298 PowerManager powerManager = (PowerManager)mContext.getSystemService(Context.POWER_SERVICE);
299 mNetTransitionWakeLock = powerManager.newWakeLock(PowerManager.PARTIAL_WAKE_LOCK, TAG);
300 mNetTransitionWakeLockTimeout = mContext.getResources().getInteger(
301 com.android.internal.R.integer.config_networkTransitionTimeout);
303 mNetTrackers = new NetworkStateTracker[
304 ConnectivityManager.MAX_NETWORK_TYPE+1];
306 mNetworkPreference = getPersistedNetworkPreference();
308 mRadioAttributes = new RadioAttributes[ConnectivityManager.MAX_RADIO_TYPE+1];
309 mNetConfigs = new NetworkConfig[ConnectivityManager.MAX_NETWORK_TYPE+1];
311 // Load device network attributes from resources
312 String[] raStrings = context.getResources().getStringArray(
313 com.android.internal.R.array.radioAttributes);
314 for (String raString : raStrings) {
315 RadioAttributes r = new RadioAttributes(raString);
316 if (r.mType > ConnectivityManager.MAX_RADIO_TYPE) {
317 loge("Error in radioAttributes - ignoring attempt to define type " + r.mType);
320 if (mRadioAttributes[r.mType] != null) {
321 loge("Error in radioAttributes - ignoring attempt to redefine type " +
325 mRadioAttributes[r.mType] = r;
328 String[] naStrings = context.getResources().getStringArray(
329 com.android.internal.R.array.networkAttributes);
330 for (String naString : naStrings) {
332 NetworkConfig n = new NetworkConfig(naString);
333 if (n.type > ConnectivityManager.MAX_NETWORK_TYPE) {
334 loge("Error in networkAttributes - ignoring attempt to define type " +
338 if (mNetConfigs[n.type] != null) {
339 loge("Error in networkAttributes - ignoring attempt to redefine type " +
343 if (mRadioAttributes[n.radio] == null) {
344 loge("Error in networkAttributes - ignoring attempt to use undefined " +
345 "radio " + n.radio + " in network type " + n.type);
348 mNetConfigs[n.type] = n;
350 } catch(Exception e) {
351 // ignore it - leave the entry null
355 mProtectedNetworks = new ArrayList<Integer>();
356 int[] protectedNetworks = context.getResources().getIntArray(
357 com.android.internal.R.array.config_protectedNetworks);
358 for (int p : protectedNetworks) {
359 if ((mNetConfigs[p] != null) && (mProtectedNetworks.contains(p) == false)) {
360 mProtectedNetworks.add(p);
362 if (DBG) loge("Ignoring protectedNetwork " + p);
366 // high priority first
367 mPriorityList = new int[mNetworksDefined];
369 int insertionPoint = mNetworksDefined-1;
370 int currentLowest = 0;
372 while (insertionPoint > -1) {
373 for (NetworkConfig na : mNetConfigs) {
374 if (na == null) continue;
375 if (na.priority < currentLowest) continue;
376 if (na.priority > currentLowest) {
377 if (na.priority < nextLowest || nextLowest == 0) {
378 nextLowest = na.priority;
382 mPriorityList[insertionPoint--] = na.type;
384 currentLowest = nextLowest;
389 mNetRequestersPids = new ArrayList[ConnectivityManager.MAX_NETWORK_TYPE+1];
390 for (int i : mPriorityList) {
391 mNetRequestersPids[i] = new ArrayList();
394 mFeatureUsers = new ArrayList();
398 mTestMode = SystemProperties.get("cm.test.mode").equals("true")
399 && SystemProperties.get("ro.build.type").equals("eng");
401 * Create the network state trackers for Wi-Fi and mobile
402 * data. Maybe this could be done with a factory class,
403 * but it's not clear that it's worth it, given that
404 * the number of different network types is not going
405 * to change very often.
407 for (int netType : mPriorityList) {
408 switch (mNetConfigs[netType].radio) {
409 case ConnectivityManager.TYPE_WIFI:
410 if (DBG) log("Starting Wifi Service.");
411 WifiStateTracker wst = new WifiStateTracker();
412 WifiService wifiService = new WifiService(context);
413 ServiceManager.addService(Context.WIFI_SERVICE, wifiService);
414 wifiService.checkAndStartWifi();
415 mNetTrackers[ConnectivityManager.TYPE_WIFI] = wst;
416 wst.startMonitoring(context, mHandler);
418 //TODO: as part of WWS refactor, create only when needed
419 mWifiWatchdogService = new WifiWatchdogService(context);
422 case ConnectivityManager.TYPE_MOBILE:
423 mNetTrackers[netType] = new MobileDataStateTracker(netType,
424 mNetConfigs[netType].name);
425 mNetTrackers[netType].startMonitoring(context, mHandler);
427 case ConnectivityManager.TYPE_DUMMY:
428 mNetTrackers[netType] = new DummyDataStateTracker(netType,
429 mNetConfigs[netType].name);
430 mNetTrackers[netType].startMonitoring(context, mHandler);
432 case ConnectivityManager.TYPE_BLUETOOTH:
433 mNetTrackers[netType] = BluetoothTetheringDataTracker.getInstance();
434 mNetTrackers[netType].startMonitoring(context, mHandler);
436 case ConnectivityManager.TYPE_ETHERNET:
437 mNetTrackers[netType] = EthernetDataTracker.getInstance();
438 mNetTrackers[netType].startMonitoring(context, mHandler);
441 loge("Trying to create a DataStateTracker for an unknown radio type " +
442 mNetConfigs[netType].radio);
447 mTethering = new Tethering(mContext, mHandler.getLooper());
448 mTetheringConfigValid = ((mTethering.getTetherableUsbRegexs().length != 0 ||
449 mTethering.getTetherableWifiRegexs().length != 0 ||
450 mTethering.getTetherableBluetoothRegexs().length != 0) &&
451 mTethering.getUpstreamIfaceTypes().length != 0);
454 mInetLog = new ArrayList();
457 mSettingsObserver = new SettingsObserver(mHandler, EVENT_APPLY_GLOBAL_HTTP_PROXY);
458 mSettingsObserver.observe(mContext);
462 VpnManager.startVpnService(context);
467 * Sets the preferred network.
468 * @param preference the new preference
470 public void setNetworkPreference(int preference) {
471 enforceChangePermission();
473 mHandler.sendMessage(mHandler.obtainMessage(EVENT_SET_NETWORK_PREFERENCE, preference, 0));
476 public int getNetworkPreference() {
477 enforceAccessPermission();
480 preference = mNetworkPreference;
485 private void handleSetNetworkPreference(int preference) {
486 if (ConnectivityManager.isNetworkTypeValid(preference) &&
487 mNetConfigs[preference] != null &&
488 mNetConfigs[preference].isDefault()) {
489 if (mNetworkPreference != preference) {
490 final ContentResolver cr = mContext.getContentResolver();
491 Settings.Secure.putInt(cr, Settings.Secure.NETWORK_PREFERENCE, preference);
493 mNetworkPreference = preference;
500 private int getPersistedNetworkPreference() {
501 final ContentResolver cr = mContext.getContentResolver();
503 final int networkPrefSetting = Settings.Secure
504 .getInt(cr, Settings.Secure.NETWORK_PREFERENCE, -1);
505 if (networkPrefSetting != -1) {
506 return networkPrefSetting;
509 return ConnectivityManager.DEFAULT_NETWORK_PREFERENCE;
513 * Make the state of network connectivity conform to the preference settings
514 * In this method, we only tear down a non-preferred network. Establishing
515 * a connection to the preferred network is taken care of when we handle
516 * the disconnect event from the non-preferred network
517 * (see {@link #handleDisconnect(NetworkInfo)}).
519 private void enforcePreference() {
520 if (mNetTrackers[mNetworkPreference].getNetworkInfo().isConnected())
523 if (!mNetTrackers[mNetworkPreference].isAvailable())
526 for (int t=0; t <= ConnectivityManager.MAX_RADIO_TYPE; t++) {
527 if (t != mNetworkPreference && mNetTrackers[t] != null &&
528 mNetTrackers[t].getNetworkInfo().isConnected()) {
530 log("tearing down " + mNetTrackers[t].getNetworkInfo() +
531 " in enforcePreference");
533 teardown(mNetTrackers[t]);
538 private boolean teardown(NetworkStateTracker netTracker) {
539 if (netTracker.teardown()) {
540 netTracker.setTeardownRequested(true);
548 * Return NetworkInfo for the active (i.e., connected) network interface.
549 * It is assumed that at most one network is active at a time. If more
550 * than one is active, it is indeterminate which will be returned.
551 * @return the info for the active network, or {@code null} if none is
554 public NetworkInfo getActiveNetworkInfo() {
555 return getNetworkInfo(mActiveDefaultNetwork);
558 public NetworkInfo getNetworkInfo(int networkType) {
559 enforceAccessPermission();
560 if (ConnectivityManager.isNetworkTypeValid(networkType)) {
561 NetworkStateTracker t = mNetTrackers[networkType];
563 return t.getNetworkInfo();
568 public NetworkInfo[] getAllNetworkInfo() {
569 enforceAccessPermission();
570 NetworkInfo[] result = new NetworkInfo[mNetworksDefined];
572 for (NetworkStateTracker t : mNetTrackers) {
573 if(t != null) result[i++] = t.getNetworkInfo();
579 * Return LinkProperties for the active (i.e., connected) default
580 * network interface. It is assumed that at most one default network
581 * is active at a time. If more than one is active, it is indeterminate
582 * which will be returned.
583 * @return the ip properties for the active network, or {@code null} if
586 public LinkProperties getActiveLinkProperties() {
587 return getLinkProperties(mActiveDefaultNetwork);
590 public LinkProperties getLinkProperties(int networkType) {
591 enforceAccessPermission();
592 if (ConnectivityManager.isNetworkTypeValid(networkType)) {
593 NetworkStateTracker t = mNetTrackers[networkType];
594 if (t != null) return t.getLinkProperties();
599 public boolean setRadios(boolean turnOn) {
600 boolean result = true;
601 enforceChangePermission();
602 for (NetworkStateTracker t : mNetTrackers) {
603 if (t != null) result = t.setRadio(turnOn) && result;
608 public boolean setRadio(int netType, boolean turnOn) {
609 enforceChangePermission();
610 if (!ConnectivityManager.isNetworkTypeValid(netType)) {
613 NetworkStateTracker tracker = mNetTrackers[netType];
614 return tracker != null && tracker.setRadio(turnOn);
618 * Used to notice when the calling process dies so we can self-expire
620 * Also used to know if the process has cleaned up after itself when
621 * our auto-expire timer goes off. The timer has a link to an object.
624 private class FeatureUser implements IBinder.DeathRecipient {
632 FeatureUser(int type, String feature, IBinder binder) {
637 mPid = getCallingPid();
638 mUid = getCallingUid();
639 mCreateTime = System.currentTimeMillis();
642 mBinder.linkToDeath(this, 0);
643 } catch (RemoteException e) {
648 void unlinkDeathRecipient() {
649 mBinder.unlinkToDeath(this, 0);
652 public void binderDied() {
653 log("ConnectivityService FeatureUser binderDied(" +
654 mNetworkType + ", " + mFeature + ", " + mBinder + "), created " +
655 (System.currentTimeMillis() - mCreateTime) + " mSec ago");
656 stopUsingNetworkFeature(this, false);
659 public void expire() {
660 log("ConnectivityService FeatureUser expire(" +
661 mNetworkType + ", " + mFeature + ", " + mBinder +"), created " +
662 (System.currentTimeMillis() - mCreateTime) + " mSec ago");
663 stopUsingNetworkFeature(this, false);
666 public String toString() {
667 return "FeatureUser("+mNetworkType+","+mFeature+","+mPid+","+mUid+"), created " +
668 (System.currentTimeMillis() - mCreateTime) + " mSec ago";
672 // javadoc from interface
673 public int startUsingNetworkFeature(int networkType, String feature,
676 log("startUsingNetworkFeature for net " + networkType + ": " + feature);
678 enforceChangePermission();
679 if (!ConnectivityManager.isNetworkTypeValid(networkType) ||
680 mNetConfigs[networkType] == null) {
681 return Phone.APN_REQUEST_FAILED;
684 FeatureUser f = new FeatureUser(networkType, feature, binder);
686 // TODO - move this into the MobileDataStateTracker
687 int usedNetworkType = networkType;
688 if(networkType == ConnectivityManager.TYPE_MOBILE) {
689 usedNetworkType = convertFeatureToNetworkType(feature);
690 if (usedNetworkType < 0) {
691 Slog.e(TAG, "Can't match any netTracker!");
692 usedNetworkType = networkType;
696 if (mProtectedNetworks.contains(usedNetworkType)) {
697 enforceConnectivityInternalPermission();
700 NetworkStateTracker network = mNetTrackers[usedNetworkType];
701 if (network != null) {
702 Integer currentPid = new Integer(getCallingPid());
703 if (usedNetworkType != networkType) {
704 NetworkStateTracker radio = mNetTrackers[networkType];
705 NetworkInfo ni = network.getNetworkInfo();
707 if (ni.isAvailable() == false) {
708 if (DBG) log("special network not available");
709 if (!TextUtils.equals(feature,Phone.FEATURE_ENABLE_DUN_ALWAYS)) {
710 return Phone.APN_TYPE_NOT_AVAILABLE;
712 // else make the attempt anyway - probably giving REQUEST_STARTED below
717 mFeatureUsers.add(f);
718 if (!mNetRequestersPids[usedNetworkType].contains(currentPid)) {
719 // this gets used for per-pid dns when connected
720 mNetRequestersPids[usedNetworkType].add(currentPid);
724 int restoreTimer = getRestoreDefaultNetworkDelay(usedNetworkType);
726 if (restoreTimer >= 0) {
727 mHandler.sendMessageDelayed(
728 mHandler.obtainMessage(EVENT_RESTORE_DEFAULT_NETWORK, f), restoreTimer);
731 if ((ni.isConnectedOrConnecting() == true) &&
732 !network.isTeardownRequested()) {
733 if (ni.isConnected() == true) {
734 // add the pid-specific dns
735 handleDnsConfigurationChange(networkType);
736 if (DBG) log("special network already active");
737 return Phone.APN_ALREADY_ACTIVE;
739 if (DBG) log("special network already connecting");
740 return Phone.APN_REQUEST_STARTED;
743 // check if the radio in play can make another contact
744 // assume if cannot for now
746 if (DBG) log("reconnecting to special network");
748 return Phone.APN_REQUEST_STARTED;
750 // need to remember this unsupported request so we respond appropriately on stop
752 mFeatureUsers.add(f);
753 if (!mNetRequestersPids[usedNetworkType].contains(currentPid)) {
754 // this gets used for per-pid dns when connected
755 mNetRequestersPids[usedNetworkType].add(currentPid);
761 return Phone.APN_TYPE_NOT_AVAILABLE;
764 // javadoc from interface
765 public int stopUsingNetworkFeature(int networkType, String feature) {
766 enforceChangePermission();
768 int pid = getCallingPid();
769 int uid = getCallingUid();
771 FeatureUser u = null;
772 boolean found = false;
775 for (int i = 0; i < mFeatureUsers.size() ; i++) {
776 u = (FeatureUser)mFeatureUsers.get(i);
777 if (uid == u.mUid && pid == u.mPid &&
778 networkType == u.mNetworkType &&
779 TextUtils.equals(feature, u.mFeature)) {
785 if (found && u != null) {
786 // stop regardless of how many other time this proc had called start
787 return stopUsingNetworkFeature(u, true);
790 if (DBG) log("ignoring stopUsingNetworkFeature - not a live request");
795 private int stopUsingNetworkFeature(FeatureUser u, boolean ignoreDups) {
796 int networkType = u.mNetworkType;
797 String feature = u.mFeature;
801 NetworkStateTracker tracker = null;
802 boolean callTeardown = false; // used to carry our decision outside of sync block
805 log("stopUsingNetworkFeature for net " + networkType +
809 if (!ConnectivityManager.isNetworkTypeValid(networkType)) {
813 // need to link the mFeatureUsers list with the mNetRequestersPids state in this
816 // check if this process still has an outstanding start request
817 if (!mFeatureUsers.contains(u)) {
818 if (DBG) log("ignoring - this process has no outstanding requests");
821 u.unlinkDeathRecipient();
822 mFeatureUsers.remove(mFeatureUsers.indexOf(u));
823 // If we care about duplicate requests, check for that here.
825 // This is done to support the extension of a request - the app
826 // can request we start the network feature again and renew the
827 // auto-shutoff delay. Normal "stop" calls from the app though
828 // do not pay attention to duplicate requests - in effect the
829 // API does not refcount and a single stop will counter multiple starts.
830 if (ignoreDups == false) {
831 for (int i = 0; i < mFeatureUsers.size() ; i++) {
832 FeatureUser x = (FeatureUser)mFeatureUsers.get(i);
833 if (x.mUid == u.mUid && x.mPid == u.mPid &&
834 x.mNetworkType == u.mNetworkType &&
835 TextUtils.equals(x.mFeature, u.mFeature)) {
836 if (DBG) log("ignoring stopUsingNetworkFeature as dup is found");
842 // TODO - move to MobileDataStateTracker
843 int usedNetworkType = networkType;
844 if (networkType == ConnectivityManager.TYPE_MOBILE) {
845 usedNetworkType = convertFeatureToNetworkType(feature);
846 if (usedNetworkType < 0) {
847 usedNetworkType = networkType;
850 tracker = mNetTrackers[usedNetworkType];
851 if (tracker == null) {
852 if (DBG) log("ignoring - no known tracker for net type " + usedNetworkType);
855 if (usedNetworkType != networkType) {
856 Integer currentPid = new Integer(pid);
857 mNetRequestersPids[usedNetworkType].remove(currentPid);
858 reassessPidDns(pid, true);
859 if (mNetRequestersPids[usedNetworkType].size() != 0) {
860 if (DBG) log("not tearing down special network - " +
861 "others still using it");
866 if (DBG) log("not a known feature - dropping");
869 if (DBG) log("Doing network teardown");
879 * @deprecated use requestRouteToHostAddress instead
881 * Ensure that a network route exists to deliver traffic to the specified
882 * host via the specified network interface.
883 * @param networkType the type of the network over which traffic to the
884 * specified host is to be routed
885 * @param hostAddress the IP address of the host to which the route is
887 * @return {@code true} on success, {@code false} on failure
889 public boolean requestRouteToHost(int networkType, int hostAddress) {
890 InetAddress inetAddress = NetworkUtils.intToInetAddress(hostAddress);
892 if (inetAddress == null) {
896 return requestRouteToHostAddress(networkType, inetAddress.getAddress());
900 * Ensure that a network route exists to deliver traffic to the specified
901 * host via the specified network interface.
902 * @param networkType the type of the network over which traffic to the
903 * specified host is to be routed
904 * @param hostAddress the IP address of the host to which the route is
906 * @return {@code true} on success, {@code false} on failure
908 public boolean requestRouteToHostAddress(int networkType, byte[] hostAddress) {
909 enforceChangePermission();
910 if (mProtectedNetworks.contains(networkType)) {
911 enforceConnectivityInternalPermission();
914 if (!ConnectivityManager.isNetworkTypeValid(networkType)) {
917 NetworkStateTracker tracker = mNetTrackers[networkType];
919 if (tracker == null || !tracker.getNetworkInfo().isConnected() ||
920 tracker.isTeardownRequested()) {
922 log("requestRouteToHostAddress on down network " +
923 "(" + networkType + ") - dropped");
928 InetAddress addr = InetAddress.getByAddress(hostAddress);
929 return addHostRoute(tracker, addr, 0);
930 } catch (UnknownHostException e) {}
935 * Ensure that a network route exists to deliver traffic to the specified
936 * host via the mobile data network.
937 * @param hostAddress the IP address of the host to which the route is desired,
938 * in network byte order.
940 * @return {@code true} on success, {@code false} on failure
942 private boolean addHostRoute(NetworkStateTracker nt, InetAddress hostAddress, int cycleCount) {
943 LinkProperties lp = nt.getLinkProperties();
944 if ((lp == null) || (hostAddress == null)) return false;
946 String interfaceName = lp.getInterfaceName();
948 log("Requested host route to " + hostAddress + "(" + interfaceName + "), cycleCount=" +
951 if (interfaceName == null) {
952 if (DBG) loge("addHostRoute failed due to null interface name");
956 RouteInfo bestRoute = RouteInfo.selectBestRoute(lp.getRoutes(), hostAddress);
957 InetAddress gatewayAddress = null;
958 if (bestRoute != null) {
959 gatewayAddress = bestRoute.getGateway();
960 // if the best route is ourself, don't relf-reference, just add the host route
961 if (hostAddress.equals(gatewayAddress)) gatewayAddress = null;
963 if (gatewayAddress != null) {
964 if (cycleCount > MAX_HOSTROUTE_CYCLE_COUNT) {
965 loge("Error adding hostroute - too much recursion");
968 if (!addHostRoute(nt, gatewayAddress, cycleCount+1)) return false;
971 RouteInfo route = RouteInfo.makeHostRoute(hostAddress, gatewayAddress);
974 mNetd.addRoute(interfaceName, route);
976 } catch (Exception ex) {
981 // TODO support the removal of single host routes. Keep a ref count of them so we
982 // aren't over-zealous
983 private boolean removeHostRoute(NetworkStateTracker nt, InetAddress hostAddress) {
988 * @see ConnectivityManager#getBackgroundDataSetting()
990 public boolean getBackgroundDataSetting() {
991 return mBackgroundDataEnabled.get();
995 * @see ConnectivityManager#setBackgroundDataSetting(boolean)
997 public void setBackgroundDataSetting(boolean allowBackgroundDataUsage) {
998 mContext.enforceCallingOrSelfPermission(
999 android.Manifest.permission.CHANGE_BACKGROUND_DATA_SETTING,
1000 "ConnectivityService");
1002 mBackgroundDataEnabled.set(allowBackgroundDataUsage);
1004 mHandler.sendMessage(mHandler.obtainMessage(EVENT_SET_BACKGROUND_DATA,
1005 (allowBackgroundDataUsage ? ENABLED : DISABLED), 0));
1008 private void handleSetBackgroundData(boolean enabled) {
1009 Settings.Secure.putInt(mContext.getContentResolver(),
1010 Settings.Secure.BACKGROUND_DATA, enabled ? 1 : 0);
1011 Intent broadcast = new Intent(
1012 ConnectivityManager.ACTION_BACKGROUND_DATA_SETTING_CHANGED);
1013 mContext.sendBroadcast(broadcast);
1017 * @see ConnectivityManager#getMobileDataEnabled()
1019 public boolean getMobileDataEnabled() {
1020 // TODO: This detail should probably be in DataConnectionTracker's
1021 // which is where we store the value and maybe make this
1023 enforceAccessPermission();
1024 boolean retVal = Settings.Secure.getInt(mContext.getContentResolver(),
1025 Settings.Secure.MOBILE_DATA, 1) == 1;
1026 if (DBG) log("getMobileDataEnabled returning " + retVal);
1030 public void setDataDependency(int networkType, boolean met) {
1031 enforceConnectivityInternalPermission();
1034 log("setDataDependency(" + networkType + ", " + met + ")");
1036 mHandler.sendMessage(mHandler.obtainMessage(EVENT_SET_DEPENDENCY_MET,
1037 (met ? ENABLED : DISABLED), networkType));
1040 private void handleSetDependencyMet(int networkType, boolean met) {
1041 if (mNetTrackers[networkType] != null) {
1043 log("handleSetDependencyMet(" + networkType + ", " + met + ")");
1045 mNetTrackers[networkType].setDependencyMet(met);
1050 * @see ConnectivityManager#setMobileDataEnabled(boolean)
1052 public void setMobileDataEnabled(boolean enabled) {
1053 enforceChangePermission();
1054 if (DBG) log("setMobileDataEnabled(" + enabled + ")");
1056 mHandler.sendMessage(mHandler.obtainMessage(EVENT_SET_MOBILE_DATA,
1057 (enabled ? ENABLED : DISABLED), 0));
1060 private void handleSetMobileData(boolean enabled) {
1061 if (mNetTrackers[ConnectivityManager.TYPE_MOBILE] != null) {
1063 Slog.d(TAG, mNetTrackers[ConnectivityManager.TYPE_MOBILE].toString() + enabled);
1065 mNetTrackers[ConnectivityManager.TYPE_MOBILE].setDataEnable(enabled);
1069 private void enforceAccessPermission() {
1070 mContext.enforceCallingOrSelfPermission(
1071 android.Manifest.permission.ACCESS_NETWORK_STATE,
1072 "ConnectivityService");
1075 private void enforceChangePermission() {
1076 mContext.enforceCallingOrSelfPermission(
1077 android.Manifest.permission.CHANGE_NETWORK_STATE,
1078 "ConnectivityService");
1081 // TODO Make this a special check when it goes public
1082 private void enforceTetherChangePermission() {
1083 mContext.enforceCallingOrSelfPermission(
1084 android.Manifest.permission.CHANGE_NETWORK_STATE,
1085 "ConnectivityService");
1088 private void enforceTetherAccessPermission() {
1089 mContext.enforceCallingOrSelfPermission(
1090 android.Manifest.permission.ACCESS_NETWORK_STATE,
1091 "ConnectivityService");
1094 private void enforceConnectivityInternalPermission() {
1095 mContext.enforceCallingOrSelfPermission(
1096 android.Manifest.permission.CONNECTIVITY_INTERNAL,
1097 "ConnectivityService");
1101 * Handle a {@code DISCONNECTED} event. If this pertains to the non-active
1102 * network, we ignore it. If it is for the active network, we send out a
1103 * broadcast. But first, we check whether it might be possible to connect
1104 * to a different network.
1105 * @param info the {@code NetworkInfo} for the network
1107 private void handleDisconnect(NetworkInfo info) {
1109 int prevNetType = info.getType();
1111 mNetTrackers[prevNetType].setTeardownRequested(false);
1113 * If the disconnected network is not the active one, then don't report
1114 * this as a loss of connectivity. What probably happened is that we're
1115 * getting the disconnect for a network that we explicitly disabled
1116 * in accordance with network preference policies.
1118 if (!mNetConfigs[prevNetType].isDefault()) {
1119 List pids = mNetRequestersPids[prevNetType];
1120 for (int i = 0; i<pids.size(); i++) {
1121 Integer pid = (Integer)pids.get(i);
1122 // will remove them because the net's no longer connected
1123 // need to do this now as only now do we know the pids and
1124 // can properly null things that are no longer referenced.
1125 reassessPidDns(pid.intValue(), false);
1129 Intent intent = new Intent(ConnectivityManager.CONNECTIVITY_ACTION);
1130 intent.putExtra(ConnectivityManager.EXTRA_NETWORK_INFO, info);
1131 if (info.isFailover()) {
1132 intent.putExtra(ConnectivityManager.EXTRA_IS_FAILOVER, true);
1133 info.setFailover(false);
1135 if (info.getReason() != null) {
1136 intent.putExtra(ConnectivityManager.EXTRA_REASON, info.getReason());
1138 if (info.getExtraInfo() != null) {
1139 intent.putExtra(ConnectivityManager.EXTRA_EXTRA_INFO,
1140 info.getExtraInfo());
1143 if (mNetConfigs[prevNetType].isDefault()) {
1144 tryFailover(prevNetType);
1145 if (mActiveDefaultNetwork != -1) {
1146 NetworkInfo switchTo = mNetTrackers[mActiveDefaultNetwork].getNetworkInfo();
1147 intent.putExtra(ConnectivityManager.EXTRA_OTHER_NETWORK_INFO, switchTo);
1149 mDefaultInetConditionPublished = 0; // we're not connected anymore
1150 intent.putExtra(ConnectivityManager.EXTRA_NO_CONNECTIVITY, true);
1153 intent.putExtra(ConnectivityManager.EXTRA_INET_CONDITION, mDefaultInetConditionPublished);
1155 // Reset interface if no other connections are using the same interface
1156 boolean doReset = true;
1157 LinkProperties linkProperties = mNetTrackers[prevNetType].getLinkProperties();
1158 if (linkProperties != null) {
1159 String oldIface = linkProperties.getInterfaceName();
1160 if (TextUtils.isEmpty(oldIface) == false) {
1161 for (NetworkStateTracker networkStateTracker : mNetTrackers) {
1162 if (networkStateTracker == null) continue;
1163 NetworkInfo networkInfo = networkStateTracker.getNetworkInfo();
1164 if (networkInfo.isConnected() && networkInfo.getType() != prevNetType) {
1165 LinkProperties l = networkStateTracker.getLinkProperties();
1166 if (l == null) continue;
1167 if (oldIface.equals(l.getInterfaceName())) {
1176 // do this before we broadcast the change
1177 handleConnectivityChange(prevNetType, doReset);
1179 sendStickyBroadcast(intent);
1181 * If the failover network is already connected, then immediately send
1182 * out a followup broadcast indicating successful failover
1184 if (mActiveDefaultNetwork != -1) {
1185 sendConnectedBroadcast(mNetTrackers[mActiveDefaultNetwork].getNetworkInfo());
1189 private void tryFailover(int prevNetType) {
1191 * If this is a default network, check if other defaults are available.
1192 * Try to reconnect on all available and let them hash it out when
1193 * more than one connects.
1195 if (mNetConfigs[prevNetType].isDefault()) {
1196 if (mActiveDefaultNetwork == prevNetType) {
1197 mActiveDefaultNetwork = -1;
1200 // don't signal a reconnect for anything lower or equal priority than our
1201 // current connected default
1202 // TODO - don't filter by priority now - nice optimization but risky
1203 // int currentPriority = -1;
1204 // if (mActiveDefaultNetwork != -1) {
1205 // currentPriority = mNetConfigs[mActiveDefaultNetwork].mPriority;
1207 for (int checkType=0; checkType <= ConnectivityManager.MAX_NETWORK_TYPE; checkType++) {
1208 if (checkType == prevNetType) continue;
1209 if (mNetConfigs[checkType] == null) continue;
1210 if (!mNetConfigs[checkType].isDefault()) continue;
1212 // Enabling the isAvailable() optimization caused mobile to not get
1213 // selected if it was in the middle of error handling. Specifically
1214 // a moble connection that took 30 seconds to complete the DEACTIVATE_DATA_CALL
1215 // would not be available and we wouldn't get connected to anything.
1216 // So removing the isAvailable() optimization below for now. TODO: This
1217 // optimization should work and we need to investigate why it doesn't work.
1218 // This could be related to how DEACTIVATE_DATA_CALL is reporting its
1219 // complete before it is really complete.
1220 // if (!mNetTrackers[checkType].isAvailable()) continue;
1222 // if (currentPriority >= mNetConfigs[checkType].mPriority) continue;
1224 NetworkStateTracker checkTracker = mNetTrackers[checkType];
1225 NetworkInfo checkInfo = checkTracker.getNetworkInfo();
1226 if (!checkInfo.isConnectedOrConnecting() || checkTracker.isTeardownRequested()) {
1227 checkInfo.setFailover(true);
1228 checkTracker.reconnect();
1230 if (DBG) log("Attempting to switch to " + checkInfo.getTypeName());
1235 private void sendConnectedBroadcast(NetworkInfo info) {
1236 sendGeneralBroadcast(info, ConnectivityManager.CONNECTIVITY_ACTION);
1239 private void sendInetConditionBroadcast(NetworkInfo info) {
1240 sendGeneralBroadcast(info, ConnectivityManager.INET_CONDITION_ACTION);
1243 private void sendGeneralBroadcast(NetworkInfo info, String bcastType) {
1244 Intent intent = new Intent(bcastType);
1245 intent.putExtra(ConnectivityManager.EXTRA_NETWORK_INFO, info);
1246 if (info.isFailover()) {
1247 intent.putExtra(ConnectivityManager.EXTRA_IS_FAILOVER, true);
1248 info.setFailover(false);
1250 if (info.getReason() != null) {
1251 intent.putExtra(ConnectivityManager.EXTRA_REASON, info.getReason());
1253 if (info.getExtraInfo() != null) {
1254 intent.putExtra(ConnectivityManager.EXTRA_EXTRA_INFO,
1255 info.getExtraInfo());
1257 intent.putExtra(ConnectivityManager.EXTRA_INET_CONDITION, mDefaultInetConditionPublished);
1258 sendStickyBroadcast(intent);
1262 * Called when an attempt to fail over to another network has failed.
1263 * @param info the {@link NetworkInfo} for the failed network
1265 private void handleConnectionFailure(NetworkInfo info) {
1266 mNetTrackers[info.getType()].setTeardownRequested(false);
1268 String reason = info.getReason();
1269 String extraInfo = info.getExtraInfo();
1272 if (reason == null) {
1275 reasonText = " (" + reason + ").";
1277 loge("Attempt to connect to " + info.getTypeName() + " failed" + reasonText);
1279 Intent intent = new Intent(ConnectivityManager.CONNECTIVITY_ACTION);
1280 intent.putExtra(ConnectivityManager.EXTRA_NETWORK_INFO, info);
1281 if (getActiveNetworkInfo() == null) {
1282 intent.putExtra(ConnectivityManager.EXTRA_NO_CONNECTIVITY, true);
1284 if (reason != null) {
1285 intent.putExtra(ConnectivityManager.EXTRA_REASON, reason);
1287 if (extraInfo != null) {
1288 intent.putExtra(ConnectivityManager.EXTRA_EXTRA_INFO, extraInfo);
1290 if (info.isFailover()) {
1291 intent.putExtra(ConnectivityManager.EXTRA_IS_FAILOVER, true);
1292 info.setFailover(false);
1295 if (mNetConfigs[info.getType()].isDefault()) {
1296 tryFailover(info.getType());
1297 if (mActiveDefaultNetwork != -1) {
1298 NetworkInfo switchTo = mNetTrackers[mActiveDefaultNetwork].getNetworkInfo();
1299 intent.putExtra(ConnectivityManager.EXTRA_OTHER_NETWORK_INFO, switchTo);
1301 mDefaultInetConditionPublished = 0;
1302 intent.putExtra(ConnectivityManager.EXTRA_NO_CONNECTIVITY, true);
1306 intent.putExtra(ConnectivityManager.EXTRA_INET_CONDITION, mDefaultInetConditionPublished);
1307 sendStickyBroadcast(intent);
1309 * If the failover network is already connected, then immediately send
1310 * out a followup broadcast indicating successful failover
1312 if (mActiveDefaultNetwork != -1) {
1313 sendConnectedBroadcast(mNetTrackers[mActiveDefaultNetwork].getNetworkInfo());
1317 private void sendStickyBroadcast(Intent intent) {
1318 synchronized(this) {
1319 if (!mSystemReady) {
1320 mInitialBroadcast = new Intent(intent);
1322 intent.addFlags(Intent.FLAG_RECEIVER_REGISTERED_ONLY_BEFORE_BOOT);
1323 mContext.sendStickyBroadcast(intent);
1327 void systemReady() {
1328 IBinder b = ServiceManager.getService(Context.NETWORKMANAGEMENT_SERVICE);
1329 mNetd = INetworkManagementService.Stub.asInterface(b);
1331 synchronized(this) {
1332 mSystemReady = true;
1333 if (mInitialBroadcast != null) {
1334 mContext.sendStickyBroadcast(mInitialBroadcast);
1335 mInitialBroadcast = null;
1338 // load the global proxy at startup
1339 mHandler.sendMessage(mHandler.obtainMessage(EVENT_APPLY_GLOBAL_HTTP_PROXY));
1342 private void handleConnect(NetworkInfo info) {
1343 int type = info.getType();
1345 // snapshot isFailover, because sendConnectedBroadcast() resets it
1346 boolean isFailover = info.isFailover();
1347 NetworkStateTracker thisNet = mNetTrackers[type];
1349 // if this is a default net and other default is running
1350 // kill the one not preferred
1351 if (mNetConfigs[type].isDefault()) {
1352 if (mActiveDefaultNetwork != -1 && mActiveDefaultNetwork != type) {
1353 if ((type != mNetworkPreference &&
1354 mNetConfigs[mActiveDefaultNetwork].priority >
1355 mNetConfigs[type].priority) ||
1356 mNetworkPreference == mActiveDefaultNetwork) {
1357 // don't accept this one
1359 log("Not broadcasting CONNECT_ACTION " +
1360 "to torn down network " + info.getTypeName());
1365 // tear down the other
1366 NetworkStateTracker otherNet =
1367 mNetTrackers[mActiveDefaultNetwork];
1369 log("Policy requires " + otherNet.getNetworkInfo().getTypeName() +
1372 if (!teardown(otherNet)) {
1373 loge("Network declined teardown request");
1378 synchronized (ConnectivityService.this) {
1379 // have a new default network, release the transition wakelock in a second
1380 // if it's held. The second pause is to allow apps to reconnect over the
1382 if (mNetTransitionWakeLock.isHeld()) {
1383 mHandler.sendMessageDelayed(mHandler.obtainMessage(
1384 EVENT_CLEAR_NET_TRANSITION_WAKELOCK,
1385 mNetTransitionWakeLockSerialNumber, 0),
1389 mActiveDefaultNetwork = type;
1390 // this will cause us to come up initially as unconnected and switching
1391 // to connected after our normal pause unless somebody reports us as reall
1393 mDefaultInetConditionPublished = 0;
1394 mDefaultConnectionSequence++;
1395 mInetConditionChangeInFlight = false;
1396 // Don't do this - if we never sign in stay, grey
1397 //reportNetworkCondition(mActiveDefaultNetwork, 100);
1399 thisNet.setTeardownRequested(false);
1400 updateNetworkSettings(thisNet);
1401 handleConnectivityChange(type, false);
1402 sendConnectedBroadcast(info);
1406 * After a change in the connectivity state of a network. We're mainly
1407 * concerned with making sure that the list of DNS servers is set up
1408 * according to which networks are connected, and ensuring that the
1409 * right routing table entries exist.
1411 private void handleConnectivityChange(int netType, boolean doReset) {
1413 * If a non-default network is enabled, add the host routes that
1414 * will allow it's DNS servers to be accessed.
1416 handleDnsConfigurationChange(netType);
1418 if (mNetTrackers[netType].getNetworkInfo().isConnected()) {
1419 if (mNetConfigs[netType].isDefault()) {
1420 handleApplyDefaultProxy(netType);
1421 addDefaultRoute(mNetTrackers[netType]);
1423 addPrivateDnsRoutes(mNetTrackers[netType]);
1426 if (mNetConfigs[netType].isDefault()) {
1427 removeDefaultRoute(mNetTrackers[netType]);
1429 removePrivateDnsRoutes(mNetTrackers[netType]);
1434 LinkProperties linkProperties = mNetTrackers[netType].getLinkProperties();
1435 if (linkProperties != null) {
1436 String iface = linkProperties.getInterfaceName();
1437 if (TextUtils.isEmpty(iface) == false) {
1439 log("resetConnections(" + iface + ", NetworkUtils.RESET_ALL_ADDRESSES)");
1441 NetworkUtils.resetConnections(iface, NetworkUtils.RESET_ALL_ADDRESSES);
1446 // TODO: Temporary notifying upstread change to Tethering.
1448 /** Notify TetheringService if interface name has been changed. */
1449 if (TextUtils.equals(mNetTrackers[netType].getNetworkInfo().getReason(),
1450 Phone.REASON_LINK_PROPERTIES_CHANGED)) {
1451 if (isTetheringSupported()) {
1452 mTethering.handleTetherIfaceChange();
1457 private void addPrivateDnsRoutes(NetworkStateTracker nt) {
1458 boolean privateDnsRouteSet = nt.isPrivateDnsRouteSet();
1459 LinkProperties p = nt.getLinkProperties();
1460 if (p == null) return;
1461 String interfaceName = p.getInterfaceName();
1464 log("addPrivateDnsRoutes for " + nt +
1465 "(" + interfaceName + ") - mPrivateDnsRouteSet = " + privateDnsRouteSet);
1467 if (interfaceName != null && !privateDnsRouteSet) {
1468 Collection<InetAddress> dnsList = p.getDnses();
1469 for (InetAddress dns : dnsList) {
1470 addHostRoute(nt, dns, 0);
1472 nt.privateDnsRouteSet(true);
1476 private void removePrivateDnsRoutes(NetworkStateTracker nt) {
1477 LinkProperties p = nt.getLinkProperties();
1478 if (p == null) return;
1479 String interfaceName = p.getInterfaceName();
1480 boolean privateDnsRouteSet = nt.isPrivateDnsRouteSet();
1481 if (interfaceName != null && privateDnsRouteSet) {
1483 log("removePrivateDnsRoutes for " + nt.getNetworkInfo().getTypeName() +
1484 " (" + interfaceName + ")");
1487 Collection<InetAddress> dnsList = p.getDnses();
1488 for (InetAddress dns : dnsList) {
1489 if (DBG) log(" removing " + dns);
1490 RouteInfo route = RouteInfo.makeHostRoute(dns);
1492 mNetd.removeRoute(interfaceName, route);
1493 } catch (Exception ex) {
1494 loge("error (" + ex + ") removing dns route " + route);
1497 nt.privateDnsRouteSet(false);
1502 private void addDefaultRoute(NetworkStateTracker nt) {
1503 LinkProperties p = nt.getLinkProperties();
1504 if (p == null) return;
1505 String interfaceName = p.getInterfaceName();
1506 if (TextUtils.isEmpty(interfaceName)) return;
1508 for (RouteInfo route : p.getRoutes()) {
1509 //TODO - handle non-default routes
1510 if (route.isDefaultRoute()) {
1511 if (DBG) log("adding default route " + route);
1512 InetAddress gateway = route.getGateway();
1513 if (addHostRoute(nt, gateway, 0)) {
1515 mNetd.addRoute(interfaceName, route);
1516 } catch (Exception e) {
1517 loge("error adding default route " + route);
1521 NetworkInfo networkInfo = nt.getNetworkInfo();
1522 log("addDefaultRoute for " + networkInfo.getTypeName() +
1523 " (" + interfaceName + "), GatewayAddr=" +
1524 gateway.getHostAddress());
1527 loge("error adding host route for default route " + route);
1534 public void removeDefaultRoute(NetworkStateTracker nt) {
1535 LinkProperties p = nt.getLinkProperties();
1536 if (p == null) return;
1537 String interfaceName = p.getInterfaceName();
1539 if (interfaceName == null) return;
1541 for (RouteInfo route : p.getRoutes()) {
1542 //TODO - handle non-default routes
1543 if (route.isDefaultRoute()) {
1545 mNetd.removeRoute(interfaceName, route);
1546 } catch (Exception ex) {
1547 loge("error (" + ex + ") removing default route " + route);
1551 NetworkInfo networkInfo = nt.getNetworkInfo();
1552 log("removeDefaultRoute for " + networkInfo.getTypeName() + " (" +
1553 interfaceName + ")");
1560 * Reads the network specific TCP buffer sizes from SystemProperties
1561 * net.tcp.buffersize.[default|wifi|umts|edge|gprs] and set them for system
1564 public void updateNetworkSettings(NetworkStateTracker nt) {
1565 String key = nt.getTcpBufferSizesPropName();
1566 String bufferSizes = SystemProperties.get(key);
1568 if (bufferSizes.length() == 0) {
1569 loge(key + " not found in system properties. Using defaults");
1571 // Setting to default values so we won't be stuck to previous values
1572 key = "net.tcp.buffersize.default";
1573 bufferSizes = SystemProperties.get(key);
1576 // Set values in kernel
1577 if (bufferSizes.length() != 0) {
1579 log("Setting TCP values: [" + bufferSizes
1580 + "] which comes from [" + key + "]");
1582 setBufferSize(bufferSizes);
1587 * Writes TCP buffer sizes to /sys/kernel/ipv4/tcp_[r/w]mem_[min/def/max]
1588 * which maps to /proc/sys/net/ipv4/tcp_rmem and tcpwmem
1590 * @param bufferSizes in the format of "readMin, readInitial, readMax,
1591 * writeMin, writeInitial, writeMax"
1593 private void setBufferSize(String bufferSizes) {
1595 String[] values = bufferSizes.split(",");
1597 if (values.length == 6) {
1598 final String prefix = "/sys/kernel/ipv4/tcp_";
1599 stringToFile(prefix + "rmem_min", values[0]);
1600 stringToFile(prefix + "rmem_def", values[1]);
1601 stringToFile(prefix + "rmem_max", values[2]);
1602 stringToFile(prefix + "wmem_min", values[3]);
1603 stringToFile(prefix + "wmem_def", values[4]);
1604 stringToFile(prefix + "wmem_max", values[5]);
1606 loge("Invalid buffersize string: " + bufferSizes);
1608 } catch (IOException e) {
1609 loge("Can't set tcp buffer sizes:" + e);
1614 * Writes string to file. Basically same as "echo -n $string > $filename"
1618 * @throws IOException
1620 private void stringToFile(String filename, String string) throws IOException {
1621 FileWriter out = new FileWriter(filename);
1631 * Adjust the per-process dns entries (net.dns<x>.<pid>) based
1632 * on the highest priority active net which this process requested.
1633 * If there aren't any, clear it out
1635 private void reassessPidDns(int myPid, boolean doBump)
1637 if (DBG) log("reassessPidDns for pid " + myPid);
1638 for(int i : mPriorityList) {
1639 if (mNetConfigs[i].isDefault()) {
1642 NetworkStateTracker nt = mNetTrackers[i];
1643 if (nt.getNetworkInfo().isConnected() &&
1644 !nt.isTeardownRequested()) {
1645 LinkProperties p = nt.getLinkProperties();
1646 if (p == null) continue;
1647 List pids = mNetRequestersPids[i];
1648 for (int j=0; j<pids.size(); j++) {
1649 Integer pid = (Integer)pids.get(j);
1650 if (pid.intValue() == myPid) {
1651 Collection<InetAddress> dnses = p.getDnses();
1652 writePidDns(dnses, myPid);
1661 // nothing found - delete
1662 for (int i = 1; ; i++) {
1663 String prop = "net.dns" + i + "." + myPid;
1664 if (SystemProperties.get(prop).length() == 0) {
1670 SystemProperties.set(prop, "");
1674 // return true if results in a change
1675 private boolean writePidDns(Collection <InetAddress> dnses, int pid) {
1677 boolean changed = false;
1678 for (InetAddress dns : dnses) {
1679 String dnsString = dns.getHostAddress();
1680 if (changed || !dnsString.equals(SystemProperties.get("net.dns" + j + "." + pid))) {
1682 SystemProperties.set("net.dns" + j++ + "." + pid, dns.getHostAddress());
1688 private void bumpDns() {
1690 * Bump the property that tells the name resolver library to reread
1691 * the DNS server list from the properties.
1693 String propVal = SystemProperties.get("net.dnschange");
1695 if (propVal.length() != 0) {
1697 n = Integer.parseInt(propVal);
1698 } catch (NumberFormatException e) {}
1700 SystemProperties.set("net.dnschange", "" + (n+1));
1702 * Tell the VMs to toss their DNS caches
1704 Intent intent = new Intent(Intent.ACTION_CLEAR_DNS_CACHE);
1705 intent.addFlags(Intent.FLAG_RECEIVER_REPLACE_PENDING);
1707 * Connectivity events can happen before boot has completed ...
1709 intent.addFlags(Intent.FLAG_RECEIVER_REGISTERED_ONLY_BEFORE_BOOT);
1710 mContext.sendBroadcast(intent);
1713 private void handleDnsConfigurationChange(int netType) {
1714 // add default net's dns entries
1715 NetworkStateTracker nt = mNetTrackers[netType];
1716 if (nt != null && nt.getNetworkInfo().isConnected() && !nt.isTeardownRequested()) {
1717 LinkProperties p = nt.getLinkProperties();
1718 if (p == null) return;
1719 Collection<InetAddress> dnses = p.getDnses();
1720 boolean changed = false;
1721 if (mNetConfigs[netType].isDefault()) {
1723 if (dnses.size() == 0 && mDefaultDns != null) {
1724 String dnsString = mDefaultDns.getHostAddress();
1725 if (!dnsString.equals(SystemProperties.get("net.dns1"))) {
1727 log("no dns provided - using " + dnsString);
1730 SystemProperties.set("net.dns1", dnsString);
1734 for (InetAddress dns : dnses) {
1735 String dnsString = dns.getHostAddress();
1736 if (!changed && dnsString.equals(SystemProperties.get("net.dns" + j))) {
1741 log("adding dns " + dns + " for " +
1742 nt.getNetworkInfo().getTypeName());
1745 SystemProperties.set("net.dns" + j++, dnsString);
1748 for (int k=j ; k<mNumDnsEntries; k++) {
1749 if (changed || !TextUtils.isEmpty(SystemProperties.get("net.dns" + k))) {
1750 if (DBG) log("erasing net.dns" + k);
1752 SystemProperties.set("net.dns" + k, "");
1757 // set per-pid dns for attached secondary nets
1758 List pids = mNetRequestersPids[netType];
1759 for (int y=0; y< pids.size(); y++) {
1760 Integer pid = (Integer)pids.get(y);
1761 changed = writePidDns(dnses, pid.intValue());
1764 if (changed) bumpDns();
1768 private int getRestoreDefaultNetworkDelay(int networkType) {
1769 String restoreDefaultNetworkDelayStr = SystemProperties.get(
1770 NETWORK_RESTORE_DELAY_PROP_NAME);
1771 if(restoreDefaultNetworkDelayStr != null &&
1772 restoreDefaultNetworkDelayStr.length() != 0) {
1774 return Integer.valueOf(restoreDefaultNetworkDelayStr);
1775 } catch (NumberFormatException e) {
1778 // if the system property isn't set, use the value for the apn type
1779 int ret = RESTORE_DEFAULT_NETWORK_DELAY;
1781 if ((networkType <= ConnectivityManager.MAX_NETWORK_TYPE) &&
1782 (mNetConfigs[networkType] != null)) {
1783 ret = mNetConfigs[networkType].restoreTime;
1789 protected void dump(FileDescriptor fd, PrintWriter pw, String[] args) {
1790 if (mContext.checkCallingOrSelfPermission(
1791 android.Manifest.permission.DUMP)
1792 != PackageManager.PERMISSION_GRANTED) {
1793 pw.println("Permission Denial: can't dump ConnectivityService " +
1794 "from from pid=" + Binder.getCallingPid() + ", uid=" +
1795 Binder.getCallingUid());
1799 for (NetworkStateTracker nst : mNetTrackers) {
1801 if (nst.getNetworkInfo().isConnected()) {
1802 pw.println("Active network: " + nst.getNetworkInfo().
1805 pw.println(nst.getNetworkInfo());
1811 pw.println("Network Requester Pids:");
1812 for (int net : mPriorityList) {
1813 String pidString = net + ": ";
1814 for (Object pid : mNetRequestersPids[net]) {
1815 pidString = pidString + pid.toString() + ", ";
1817 pw.println(pidString);
1821 pw.println("FeatureUsers:");
1822 for (Object requester : mFeatureUsers) {
1823 pw.println(requester.toString());
1827 synchronized (this) {
1828 pw.println("NetworkTranstionWakeLock is currently " +
1829 (mNetTransitionWakeLock.isHeld() ? "" : "not ") + "held.");
1830 pw.println("It was last requested for "+mNetTransitionWakeLockCausedBy);
1834 mTethering.dump(fd, pw, args);
1836 if (mInetLog != null) {
1838 pw.println("Inet condition reports:");
1839 for(int i = 0; i < mInetLog.size(); i++) {
1840 pw.println(mInetLog.get(i));
1845 // must be stateless - things change under us.
1846 private class MyHandler extends Handler {
1847 public MyHandler(Looper looper) {
1852 public void handleMessage(Message msg) {
1855 case NetworkStateTracker.EVENT_STATE_CHANGED:
1856 info = (NetworkInfo) msg.obj;
1857 int type = info.getType();
1858 NetworkInfo.State state = info.getState();
1860 if (DBG) log("ConnectivityChange for " +
1861 info.getTypeName() + ": " +
1862 state + "/" + info.getDetailedState());
1864 // Connectivity state changed:
1865 // [31-13] Reserved for future use
1866 // [12-9] Network subtype (for mobile network, as defined
1867 // by TelephonyManager)
1868 // [8-3] Detailed state ordinal (as defined by
1869 // NetworkInfo.DetailedState)
1870 // [2-0] Network type (as defined by ConnectivityManager)
1871 int eventLogParam = (info.getType() & 0x7) |
1872 ((info.getDetailedState().ordinal() & 0x3f) << 3) |
1873 (info.getSubtype() << 9);
1874 EventLog.writeEvent(EventLogTags.CONNECTIVITY_STATE_CHANGED,
1877 if (info.getDetailedState() ==
1878 NetworkInfo.DetailedState.FAILED) {
1879 handleConnectionFailure(info);
1880 } else if (state == NetworkInfo.State.DISCONNECTED) {
1881 handleDisconnect(info);
1882 } else if (state == NetworkInfo.State.SUSPENDED) {
1883 // TODO: need to think this over.
1884 // the logic here is, handle SUSPENDED the same as
1885 // DISCONNECTED. The only difference being we are
1886 // broadcasting an intent with NetworkInfo that's
1887 // suspended. This allows the applications an
1888 // opportunity to handle DISCONNECTED and SUSPENDED
1889 // differently, or not.
1890 handleDisconnect(info);
1891 } else if (state == NetworkInfo.State.CONNECTED) {
1892 handleConnect(info);
1895 case NetworkStateTracker.EVENT_CONFIGURATION_CHANGED:
1896 info = (NetworkInfo) msg.obj;
1897 // TODO: Temporary allowing network configuration
1898 // change not resetting sockets.
1900 handleConnectivityChange(info.getType(), false);
1902 case EVENT_CLEAR_NET_TRANSITION_WAKELOCK:
1903 String causedBy = null;
1904 synchronized (ConnectivityService.this) {
1905 if (msg.arg1 == mNetTransitionWakeLockSerialNumber &&
1906 mNetTransitionWakeLock.isHeld()) {
1907 mNetTransitionWakeLock.release();
1908 causedBy = mNetTransitionWakeLockCausedBy;
1911 if (causedBy != null) {
1912 log("NetTransition Wakelock for " + causedBy + " released by timeout");
1915 case EVENT_RESTORE_DEFAULT_NETWORK:
1916 FeatureUser u = (FeatureUser)msg.obj;
1919 case EVENT_INET_CONDITION_CHANGE:
1921 int netType = msg.arg1;
1922 int condition = msg.arg2;
1923 handleInetConditionChange(netType, condition);
1926 case EVENT_INET_CONDITION_HOLD_END:
1928 int netType = msg.arg1;
1929 int sequence = msg.arg2;
1930 handleInetConditionHoldEnd(netType, sequence);
1933 case EVENT_SET_NETWORK_PREFERENCE:
1935 int preference = msg.arg1;
1936 handleSetNetworkPreference(preference);
1939 case EVENT_SET_BACKGROUND_DATA:
1941 boolean enabled = (msg.arg1 == ENABLED);
1942 handleSetBackgroundData(enabled);
1945 case EVENT_SET_MOBILE_DATA:
1947 boolean enabled = (msg.arg1 == ENABLED);
1948 handleSetMobileData(enabled);
1951 case EVENT_APPLY_GLOBAL_HTTP_PROXY:
1953 handleDeprecatedGlobalHttpProxy();
1956 case EVENT_SET_DEPENDENCY_MET:
1958 boolean met = (msg.arg1 == ENABLED);
1959 handleSetDependencyMet(msg.arg2, met);
1966 // javadoc from interface
1967 public int tether(String iface) {
1968 enforceTetherChangePermission();
1970 if (isTetheringSupported()) {
1971 return mTethering.tether(iface);
1973 return ConnectivityManager.TETHER_ERROR_UNSUPPORTED;
1977 // javadoc from interface
1978 public int untether(String iface) {
1979 enforceTetherChangePermission();
1981 if (isTetheringSupported()) {
1982 return mTethering.untether(iface);
1984 return ConnectivityManager.TETHER_ERROR_UNSUPPORTED;
1988 // javadoc from interface
1989 public int getLastTetherError(String iface) {
1990 enforceTetherAccessPermission();
1992 if (isTetheringSupported()) {
1993 return mTethering.getLastTetherError(iface);
1995 return ConnectivityManager.TETHER_ERROR_UNSUPPORTED;
1999 // TODO - proper iface API for selection by property, inspection, etc
2000 public String[] getTetherableUsbRegexs() {
2001 enforceTetherAccessPermission();
2002 if (isTetheringSupported()) {
2003 return mTethering.getTetherableUsbRegexs();
2005 return new String[0];
2009 public String[] getTetherableWifiRegexs() {
2010 enforceTetherAccessPermission();
2011 if (isTetheringSupported()) {
2012 return mTethering.getTetherableWifiRegexs();
2014 return new String[0];
2018 public String[] getTetherableBluetoothRegexs() {
2019 enforceTetherAccessPermission();
2020 if (isTetheringSupported()) {
2021 return mTethering.getTetherableBluetoothRegexs();
2023 return new String[0];
2027 // TODO - move iface listing, queries, etc to new module
2028 // javadoc from interface
2029 public String[] getTetherableIfaces() {
2030 enforceTetherAccessPermission();
2031 return mTethering.getTetherableIfaces();
2034 public String[] getTetheredIfaces() {
2035 enforceTetherAccessPermission();
2036 return mTethering.getTetheredIfaces();
2039 public String[] getTetheringErroredIfaces() {
2040 enforceTetherAccessPermission();
2041 return mTethering.getErroredIfaces();
2044 // if ro.tether.denied = true we default to no tethering
2045 // gservices could set the secure setting to 1 though to enable it on a build where it
2046 // had previously been turned off.
2047 public boolean isTetheringSupported() {
2048 enforceTetherAccessPermission();
2049 int defaultVal = (SystemProperties.get("ro.tether.denied").equals("true") ? 0 : 1);
2050 boolean tetherEnabledInSettings = (Settings.Secure.getInt(mContext.getContentResolver(),
2051 Settings.Secure.TETHER_SUPPORTED, defaultVal) != 0);
2052 return tetherEnabledInSettings && mTetheringConfigValid;
2055 // An API NetworkStateTrackers can call when they lose their network.
2056 // This will automatically be cleared after X seconds or a network becomes CONNECTED,
2057 // whichever happens first. The timer is started by the first caller and not
2058 // restarted by subsequent callers.
2059 public void requestNetworkTransitionWakelock(String forWhom) {
2060 enforceConnectivityInternalPermission();
2061 synchronized (this) {
2062 if (mNetTransitionWakeLock.isHeld()) return;
2063 mNetTransitionWakeLockSerialNumber++;
2064 mNetTransitionWakeLock.acquire();
2065 mNetTransitionWakeLockCausedBy = forWhom;
2067 mHandler.sendMessageDelayed(mHandler.obtainMessage(
2068 EVENT_CLEAR_NET_TRANSITION_WAKELOCK,
2069 mNetTransitionWakeLockSerialNumber, 0),
2070 mNetTransitionWakeLockTimeout);
2074 // 100 percent is full good, 0 is full bad.
2075 public void reportInetCondition(int networkType, int percentage) {
2076 if (DBG) log("reportNetworkCondition(" + networkType + ", " + percentage + ")");
2077 mContext.enforceCallingOrSelfPermission(
2078 android.Manifest.permission.STATUS_BAR,
2079 "ConnectivityService");
2082 int pid = getCallingPid();
2083 int uid = getCallingUid();
2084 String s = pid + "(" + uid + ") reports inet is " +
2085 (percentage > 50 ? "connected" : "disconnected") + " (" + percentage + ") on " +
2086 "network Type " + networkType + " at " + GregorianCalendar.getInstance().getTime();
2088 while(mInetLog.size() > INET_CONDITION_LOG_MAX_SIZE) {
2092 mHandler.sendMessage(mHandler.obtainMessage(
2093 EVENT_INET_CONDITION_CHANGE, networkType, percentage));
2096 private void handleInetConditionChange(int netType, int condition) {
2098 log("Inet connectivity change, net=" +
2099 netType + ", condition=" + condition +
2100 ",mActiveDefaultNetwork=" + mActiveDefaultNetwork);
2102 if (mActiveDefaultNetwork == -1) {
2103 if (DBG) log("no active default network - aborting");
2106 if (mActiveDefaultNetwork != netType) {
2107 if (DBG) log("given net not default - aborting");
2110 mDefaultInetCondition = condition;
2112 if (mInetConditionChangeInFlight == false) {
2113 if (DBG) log("starting a change hold");
2114 // setup a new hold to debounce this
2115 if (mDefaultInetCondition > 50) {
2116 delay = Settings.Secure.getInt(mContext.getContentResolver(),
2117 Settings.Secure.INET_CONDITION_DEBOUNCE_UP_DELAY, 500);
2119 delay = Settings.Secure.getInt(mContext.getContentResolver(),
2120 Settings.Secure.INET_CONDITION_DEBOUNCE_DOWN_DELAY, 3000);
2122 mInetConditionChangeInFlight = true;
2123 mHandler.sendMessageDelayed(mHandler.obtainMessage(EVENT_INET_CONDITION_HOLD_END,
2124 mActiveDefaultNetwork, mDefaultConnectionSequence), delay);
2126 // we've set the new condition, when this hold ends that will get
2128 if (DBG) log("currently in hold - not setting new end evt");
2132 private void handleInetConditionHoldEnd(int netType, int sequence) {
2134 log("Inet hold end, net=" + netType +
2135 ", condition =" + mDefaultInetCondition +
2136 ", published condition =" + mDefaultInetConditionPublished);
2138 mInetConditionChangeInFlight = false;
2140 if (mActiveDefaultNetwork == -1) {
2141 if (DBG) log("no active default network - aborting");
2144 if (mDefaultConnectionSequence != sequence) {
2145 if (DBG) log("event hold for obsolete network - aborting");
2148 if (mDefaultInetConditionPublished == mDefaultInetCondition) {
2149 if (DBG) log("no change in condition - aborting");
2152 NetworkInfo networkInfo = mNetTrackers[mActiveDefaultNetwork].getNetworkInfo();
2153 if (networkInfo.isConnected() == false) {
2154 if (DBG) log("default network not connected - aborting");
2157 mDefaultInetConditionPublished = mDefaultInetCondition;
2158 sendInetConditionBroadcast(networkInfo);
2162 public synchronized ProxyProperties getProxy() {
2163 if (mGlobalProxy != null) return mGlobalProxy;
2164 if (mDefaultProxy != null) return mDefaultProxy;
2168 public void setGlobalProxy(ProxyProperties proxyProperties) {
2169 enforceChangePermission();
2170 synchronized (mGlobalProxyLock) {
2171 if (proxyProperties == mGlobalProxy) return;
2172 if (proxyProperties != null && proxyProperties.equals(mGlobalProxy)) return;
2173 if (mGlobalProxy != null && mGlobalProxy.equals(proxyProperties)) return;
2177 String exclList = "";
2178 if (proxyProperties != null && !TextUtils.isEmpty(proxyProperties.getHost())) {
2179 mGlobalProxy = new ProxyProperties(proxyProperties);
2180 host = mGlobalProxy.getHost();
2181 port = mGlobalProxy.getPort();
2182 exclList = mGlobalProxy.getExclusionList();
2184 mGlobalProxy = null;
2186 ContentResolver res = mContext.getContentResolver();
2187 Settings.Secure.putString(res, Settings.Secure.GLOBAL_HTTP_PROXY_HOST, host);
2188 Settings.Secure.putInt(res, Settings.Secure.GLOBAL_HTTP_PROXY_PORT, port);
2189 Settings.Secure.putString(res, Settings.Secure.GLOBAL_HTTP_PROXY_EXCLUSION_LIST,
2193 if (mGlobalProxy == null) {
2194 proxyProperties = mDefaultProxy;
2196 sendProxyBroadcast(proxyProperties);
2199 private void loadGlobalProxy() {
2200 ContentResolver res = mContext.getContentResolver();
2201 String host = Settings.Secure.getString(res, Settings.Secure.GLOBAL_HTTP_PROXY_HOST);
2202 int port = Settings.Secure.getInt(res, Settings.Secure.GLOBAL_HTTP_PROXY_PORT, 0);
2203 String exclList = Settings.Secure.getString(res,
2204 Settings.Secure.GLOBAL_HTTP_PROXY_EXCLUSION_LIST);
2205 if (!TextUtils.isEmpty(host)) {
2206 ProxyProperties proxyProperties = new ProxyProperties(host, port, exclList);
2207 synchronized (mGlobalProxyLock) {
2208 mGlobalProxy = proxyProperties;
2213 public ProxyProperties getGlobalProxy() {
2214 synchronized (mGlobalProxyLock) {
2215 return mGlobalProxy;
2219 private void handleApplyDefaultProxy(int type) {
2220 // check if new default - push it out to all VM if so
2221 ProxyProperties proxy = mNetTrackers[type].getLinkProperties().getHttpProxy();
2222 synchronized (this) {
2223 if (mDefaultProxy != null && mDefaultProxy.equals(proxy)) return;
2224 if (mDefaultProxy == proxy) return;
2225 if (!TextUtils.isEmpty(proxy.getHost())) {
2226 mDefaultProxy = proxy;
2228 mDefaultProxy = null;
2231 if (DBG) log("changing default proxy to " + proxy);
2232 if ((proxy == null && mGlobalProxy == null) || proxy.equals(mGlobalProxy)) return;
2233 if (mGlobalProxy != null) return;
2234 sendProxyBroadcast(proxy);
2237 private void handleDeprecatedGlobalHttpProxy() {
2238 String proxy = Settings.Secure.getString(mContext.getContentResolver(),
2239 Settings.Secure.HTTP_PROXY);
2240 if (!TextUtils.isEmpty(proxy)) {
2241 String data[] = proxy.split(":");
2242 String proxyHost = data[0];
2243 int proxyPort = 8080;
2244 if (data.length > 1) {
2246 proxyPort = Integer.parseInt(data[1]);
2247 } catch (NumberFormatException e) {
2251 ProxyProperties p = new ProxyProperties(data[0], proxyPort, "");
2256 private void sendProxyBroadcast(ProxyProperties proxy) {
2257 if (proxy == null) proxy = new ProxyProperties("", 0, "");
2258 log("sending Proxy Broadcast for " + proxy);
2259 Intent intent = new Intent(Proxy.PROXY_CHANGE_ACTION);
2260 intent.addFlags(Intent.FLAG_RECEIVER_REPLACE_PENDING |
2261 Intent.FLAG_RECEIVER_REGISTERED_ONLY_BEFORE_BOOT);
2262 intent.putExtra(Proxy.EXTRA_PROXY_INFO, proxy);
2263 mContext.sendStickyBroadcast(intent);
2266 private static class SettingsObserver extends ContentObserver {
2268 private Handler mHandler;
2269 SettingsObserver(Handler handler, int what) {
2275 void observe(Context context) {
2276 ContentResolver resolver = context.getContentResolver();
2277 resolver.registerContentObserver(Settings.Secure.getUriFor(
2278 Settings.Secure.HTTP_PROXY), false, this);
2282 public void onChange(boolean selfChange) {
2283 mHandler.obtainMessage(mWhat).sendToTarget();
2287 private void log(String s) {
2291 private void loge(String s) {
2294 int convertFeatureToNetworkType(String feature){
2295 int networkType = -1;
2296 if (TextUtils.equals(feature, Phone.FEATURE_ENABLE_MMS)) {
2297 networkType = ConnectivityManager.TYPE_MOBILE_MMS;
2298 } else if (TextUtils.equals(feature, Phone.FEATURE_ENABLE_SUPL)) {
2299 networkType = ConnectivityManager.TYPE_MOBILE_SUPL;
2300 } else if (TextUtils.equals(feature, Phone.FEATURE_ENABLE_DUN) ||
2301 TextUtils.equals(feature, Phone.FEATURE_ENABLE_DUN_ALWAYS)) {
2302 networkType = ConnectivityManager.TYPE_MOBILE_DUN;
2303 } else if (TextUtils.equals(feature, Phone.FEATURE_ENABLE_HIPRI)) {
2304 networkType = ConnectivityManager.TYPE_MOBILE_HIPRI;
2305 } else if (TextUtils.equals(feature, Phone.FEATURE_ENABLE_FOTA)) {
2306 networkType = ConnectivityManager.TYPE_MOBILE_FOTA;
2307 } else if (TextUtils.equals(feature, Phone.FEATURE_ENABLE_IMS)) {
2308 networkType = ConnectivityManager.TYPE_MOBILE_IMS;
2309 } else if (TextUtils.equals(feature, Phone.FEATURE_ENABLE_CBS)) {
2310 networkType = ConnectivityManager.TYPE_MOBILE_CBS;