resolved conflicts for merge of 6c9a3004 to master
Change-Id: Ia1a8a2015c087004d3dfc64681dbe0315558dc95
diff --git a/core/java/android/net/ConnectivityManager.java b/core/java/android/net/ConnectivityManager.java
index 280ded6..6335296 100644
--- a/core/java/android/net/ConnectivityManager.java
+++ b/core/java/android/net/ConnectivityManager.java
@@ -524,5 +524,20 @@
} catch (RemoteException e) {
return TETHER_ERROR_SERVICE_UNAVAIL;
}
- }
+ }
+
+ /**
+ * Ensure the device stays awake until we connect with the next network
+ * @param forWhome The name of the network going down for logging purposes
+ * @return {@code true} on success, {@code false} on failure
+ * {@hide}
+ */
+ public boolean requestNetworkTransitionWakelock(String forWhom) {
+ try {
+ mService.requestNetworkTransitionWakelock(forWhom);
+ return true;
+ } catch (RemoteException e) {
+ return false;
+ }
+ }
}
diff --git a/core/java/android/net/IConnectivityManager.aidl b/core/java/android/net/IConnectivityManager.aidl
index b05c2ed..5a14cc9 100644
--- a/core/java/android/net/IConnectivityManager.aidl
+++ b/core/java/android/net/IConnectivityManager.aidl
@@ -72,4 +72,6 @@
String[] getTetherableUsbRegexs();
String[] getTetherableWifiRegexs();
+
+ void requestNetworkTransitionWakelock(in String forWhom);
}
diff --git a/core/java/android/net/NetworkInfo.java b/core/java/android/net/NetworkInfo.java
index 649cb8c..21f711c 100644
--- a/core/java/android/net/NetworkInfo.java
+++ b/core/java/android/net/NetworkInfo.java
@@ -121,7 +121,10 @@
*/
public NetworkInfo(int type) {}
- NetworkInfo(int type, int subtype, String typeName, String subtypeName) {
+ /**
+ * @hide
+ */
+ public NetworkInfo(int type, int subtype, String typeName, String subtypeName) {
if (!ConnectivityManager.isNetworkTypeValid(type)) {
throw new IllegalArgumentException("Invalid network type: " + type);
}
@@ -281,8 +284,9 @@
* if one was supplied. May be {@code null}.
* @param extraInfo an optional {@code String} providing addditional network state
* information passed up from the lower networking layers.
+ * @hide
*/
- void setDetailedState(DetailedState detailedState, String reason, String extraInfo) {
+ public void setDetailedState(DetailedState detailedState, String reason, String extraInfo) {
this.mDetailedState = detailedState;
this.mState = stateMap.get(detailedState);
this.mReason = reason;
diff --git a/services/java/com/android/server/ConnectivityService.java b/services/java/com/android/server/ConnectivityService.java
index 81b8d40..9ff7de6 100644
--- a/services/java/com/android/server/ConnectivityService.java
+++ b/services/java/com/android/server/ConnectivityService.java
@@ -28,11 +28,13 @@
import android.net.NetworkInfo;
import android.net.NetworkStateTracker;
import android.net.wifi.WifiStateTracker;
+import android.net.NetworkUtils;
import android.os.Binder;
import android.os.Handler;
import android.os.IBinder;
import android.os.Looper;
import android.os.Message;
+import android.os.PowerManager;
import android.os.RemoteException;
import android.os.ServiceManager;
import android.os.SystemProperties;
@@ -46,6 +48,8 @@
import com.android.server.connectivity.Tethering;
import java.io.FileDescriptor;
+import java.io.FileWriter;
+import java.io.IOException;
import java.io.PrintWriter;
import java.util.ArrayList;
import java.util.List;
@@ -64,7 +68,6 @@
private static final String NETWORK_RESTORE_DELAY_PROP_NAME =
"android.telephony.apn-restore";
-
private Tethering mTethering;
private boolean mTetheringConfigValid = false;
@@ -104,6 +107,11 @@
private boolean mSystemReady;
private Intent mInitialBroadcast;
+ private PowerManager.WakeLock mNetTransitionWakeLock;
+ private String mNetTransitionWakeLockCausedBy = "";
+ private int mNetTransitionWakeLockSerialNumber;
+ private int mNetTransitionWakeLockTimeout;
+
private static class NetworkAttributes {
/**
* Class for holding settings read from resources.
@@ -194,6 +202,12 @@
}
mContext = context;
+
+ PowerManager powerManager = (PowerManager)mContext.getSystemService(Context.POWER_SERVICE);
+ mNetTransitionWakeLock = powerManager.newWakeLock(PowerManager.PARTIAL_WAKE_LOCK, TAG);
+ mNetTransitionWakeLockTimeout = mContext.getResources().getInteger(
+ com.android.internal.R.integer.config_networkTransitionTimeout);
+
mNetTrackers = new NetworkStateTracker[
ConnectivityManager.MAX_NETWORK_TYPE+1];
mHandler = new MyHandler();
@@ -597,15 +611,7 @@
network.reconnect();
return Phone.APN_REQUEST_STARTED;
} else {
- synchronized(this) {
- mFeatureUsers.add(f);
- }
- mHandler.sendMessageDelayed(mHandler.obtainMessage(
- NetworkStateTracker.EVENT_RESTORE_DEFAULT_NETWORK,
- f), getRestoreDefaultNetworkDelay());
-
- return network.startUsingNetworkFeature(feature,
- getCallingPid(), getCallingUid());
+ return -1;
}
}
return Phone.APN_TYPE_NOT_AVAILABLE;
@@ -724,8 +730,7 @@
tracker.teardown();
return 1;
} else {
- // do it the old fashioned way
- return tracker.stopUsingNetworkFeature(feature, pid, uid);
+ return -1;
}
}
@@ -752,7 +757,32 @@
}
return false;
}
- return tracker.requestRouteToHost(hostAddress);
+ return addHostRoute(tracker, hostAddress);
+ }
+
+ /**
+ * Ensure that a network route exists to deliver traffic to the specified
+ * host via the mobile data network.
+ * @param hostAddress the IP address of the host to which the route is desired,
+ * in network byte order.
+ * @return {@code true} on success, {@code false} on failure
+ */
+ private boolean addHostRoute(NetworkStateTracker nt, int hostAddress) {
+ if (nt.getNetworkInfo().getType() == ConnectivityManager.TYPE_WIFI) {
+ return false;
+ }
+
+ String interfaceName = nt.getInterfaceName();
+
+ if (DBG) {
+ Slog.d(TAG, "Requested host route to " + Integer.toHexString(hostAddress) +
+ "(" + interfaceName + ")");
+ }
+ if (interfaceName != null && hostAddress != -1) {
+ return NetworkUtils.addHostRoute(interfaceName, hostAddress) == 0;
+ } else {
+ return false;
+ }
}
/**
@@ -859,6 +889,12 @@
"ConnectivityService");
}
+ private void enforceConnectivityInternalPermission() {
+ mContext.enforceCallingOrSelfPermission(
+ android.Manifest.permission.CONNECTIVITY_INTERNAL,
+ "ConnectivityService");
+ }
+
/**
* Handle a {@code DISCONNECTED} event. If this pertains to the non-active
* network, we ignore it. If it is for the active network, we send out a
@@ -1134,15 +1170,23 @@
Slog.e(TAG, "Network declined teardown request");
return;
}
- if (isFailover) {
- otherNet.releaseWakeLock();
- }
+ }
+ }
+ synchronized (ConnectivityService.this) {
+ // have a new default network, release the transition wakelock in a second
+ // if it's held. The second pause is to allow apps to reconnect over the
+ // new network
+ if (mNetTransitionWakeLock.isHeld()) {
+ mHandler.sendMessageDelayed(mHandler.obtainMessage(
+ NetworkStateTracker.EVENT_CLEAR_NET_TRANSITION_WAKELOCK,
+ mNetTransitionWakeLockSerialNumber, 0),
+ 1000);
}
}
mActiveDefaultNetwork = type;
}
thisNet.setTeardownRequested(false);
- thisNet.updateNetworkSettings();
+ updateNetworkSettings(thisNet);
handleConnectivityChange();
sendConnectedBroadcast(info);
}
@@ -1192,20 +1236,185 @@
for (int netType : mPriorityList) {
if (mNetTrackers[netType].getNetworkInfo().isConnected()) {
if (mNetAttributes[netType].isDefault()) {
- mNetTrackers[netType].addDefaultRoute();
+ addDefaultRoute(mNetTrackers[netType]);
} else {
- mNetTrackers[netType].addPrivateDnsRoutes();
+ addPrivateDnsRoutes(mNetTrackers[netType]);
}
} else {
if (mNetAttributes[netType].isDefault()) {
- mNetTrackers[netType].removeDefaultRoute();
+ removeDefaultRoute(mNetTrackers[netType]);
} else {
- mNetTrackers[netType].removePrivateDnsRoutes();
+ removePrivateDnsRoutes(mNetTrackers[netType]);
}
}
}
}
+ private void addPrivateDnsRoutes(NetworkStateTracker nt) {
+ String interfaceName = nt.getInterfaceName();
+ boolean privateDnsRouteSet = nt.isPrivateDnsRouteSet();
+
+ if (DBG) {
+ Slog.d(TAG, "addPrivateDnsRoutes for " + nt +
+ "(" + interfaceName + ") - mPrivateDnsRouteSet = " + privateDnsRouteSet);
+ }
+ String[] dnsList = getNameServerList(nt.getDnsPropNames());
+ if (interfaceName != null && !privateDnsRouteSet) {
+ for (String addrString : dnsList) {
+ int addr = NetworkUtils.lookupHost(addrString);
+ if (addr != -1 && addr != 0) {
+ if (DBG) Slog.d(TAG, " adding "+addrString+" ("+addr+")");
+ NetworkUtils.addHostRoute(interfaceName, addr);
+ }
+ }
+ nt.privateDnsRouteSet(true);
+ }
+ }
+
+ private void removePrivateDnsRoutes(NetworkStateTracker nt) {
+ // TODO - we should do this explicitly but the NetUtils api doesnt
+ // support this yet - must remove all. No worse than before
+ String interfaceName = nt.getInterfaceName();
+ boolean privateDnsRouteSet = nt.isPrivateDnsRouteSet();
+ if (interfaceName != null && privateDnsRouteSet) {
+ if (DBG) {
+ Slog.d(TAG, "removePrivateDnsRoutes for " + nt.getNetworkInfo().getTypeName() +
+ " (" + interfaceName + ")");
+ }
+ NetworkUtils.removeHostRoutes(interfaceName);
+ nt.privateDnsRouteSet(false);
+ }
+ }
+
+ /**
+ * Return the IP addresses of the DNS servers available for this
+ * network interface.
+ * @param propertyNames the names of the system properties whose values
+ * give the IP addresses. Properties with no values are skipped.
+ * @return an array of {@code String}s containing the IP addresses
+ * of the DNS servers, in dot-notation. This may have fewer
+ * non-null entries than the list of names passed in, since
+ * some of the passed-in names may have empty values.
+ */
+ String[] getNameServerList(String[] propertyNames) {
+ String[] dnsAddresses = new String[propertyNames.length];
+ int i, j;
+
+ for (i = 0, j = 0; i < propertyNames.length; i++) {
+ String value = SystemProperties.get(propertyNames[i]);
+ // The GSM layer sometimes sets a bogus DNS server address of
+ // 0.0.0.0
+ if (!TextUtils.isEmpty(value) && !TextUtils.equals(value, "0.0.0.0")) {
+ dnsAddresses[j++] = value;
+ }
+ }
+ return dnsAddresses;
+ }
+
+ private void addDefaultRoute(NetworkStateTracker nt) {
+ String interfaceName = nt.getInterfaceName();
+ int defaultGatewayAddr = nt.getDefaultGatewayAddr();
+ boolean defaultRouteSet = nt.isDefaultRouteSet();
+ NetworkInfo networkInfo = nt.getNetworkInfo();
+
+ if ((interfaceName != null) && (defaultGatewayAddr != 0) &&
+ defaultRouteSet == false) {
+ if (DBG) {
+ Slog.d(TAG, "addDefaultRoute for " + networkInfo.getTypeName() +
+ " (" + interfaceName + "), GatewayAddr=" + defaultGatewayAddr);
+ }
+ NetworkUtils.setDefaultRoute(interfaceName, defaultGatewayAddr);
+ nt.defaultRouteSet(true);
+ }
+ }
+
+
+ public void removeDefaultRoute(NetworkStateTracker nt) {
+ String interfaceName = nt.getInterfaceName();
+ boolean defaultRouteSet = nt.isDefaultRouteSet();
+ NetworkInfo networkInfo = nt.getNetworkInfo();
+
+ if (interfaceName != null && defaultRouteSet == true) {
+ if (DBG) {
+ Slog.d(TAG, "removeDefaultRoute for " + networkInfo.getTypeName() + " (" +
+ interfaceName + ")");
+ }
+ NetworkUtils.removeDefaultRoute(interfaceName);
+ nt.defaultRouteSet(false);
+ }
+ }
+
+ /**
+ * Reads the network specific TCP buffer sizes from SystemProperties
+ * net.tcp.buffersize.[default|wifi|umts|edge|gprs] and set them for system
+ * wide use
+ */
+ public void updateNetworkSettings(NetworkStateTracker nt) {
+ String key = nt.getTcpBufferSizesPropName();
+ String bufferSizes = SystemProperties.get(key);
+
+ if (bufferSizes.length() == 0) {
+ Slog.e(TAG, key + " not found in system properties. Using defaults");
+
+ // Setting to default values so we won't be stuck to previous values
+ key = "net.tcp.buffersize.default";
+ bufferSizes = SystemProperties.get(key);
+ }
+
+ // Set values in kernel
+ if (bufferSizes.length() != 0) {
+ if (DBG) {
+ Slog.v(TAG, "Setting TCP values: [" + bufferSizes
+ + "] which comes from [" + key + "]");
+ }
+ setBufferSize(bufferSizes);
+ }
+ }
+
+ /**
+ * Writes TCP buffer sizes to /sys/kernel/ipv4/tcp_[r/w]mem_[min/def/max]
+ * which maps to /proc/sys/net/ipv4/tcp_rmem and tcpwmem
+ *
+ * @param bufferSizes in the format of "readMin, readInitial, readMax,
+ * writeMin, writeInitial, writeMax"
+ */
+ private void setBufferSize(String bufferSizes) {
+ try {
+ String[] values = bufferSizes.split(",");
+
+ if (values.length == 6) {
+ final String prefix = "/sys/kernel/ipv4/tcp_";
+ stringToFile(prefix + "rmem_min", values[0]);
+ stringToFile(prefix + "rmem_def", values[1]);
+ stringToFile(prefix + "rmem_max", values[2]);
+ stringToFile(prefix + "wmem_min", values[3]);
+ stringToFile(prefix + "wmem_def", values[4]);
+ stringToFile(prefix + "wmem_max", values[5]);
+ } else {
+ Slog.e(TAG, "Invalid buffersize string: " + bufferSizes);
+ }
+ } catch (IOException e) {
+ Slog.e(TAG, "Can't set tcp buffer sizes:" + e);
+ }
+ }
+
+ /**
+ * Writes string to file. Basically same as "echo -n $string > $filename"
+ *
+ * @param filename
+ * @param string
+ * @throws IOException
+ */
+ private void stringToFile(String filename, String string) throws IOException {
+ FileWriter out = new FileWriter(filename);
+ try {
+ out.write(string);
+ } finally {
+ out.close();
+ }
+ }
+
+
/**
* Adjust the per-process dns entries (net.dns<x>.<pid>) based
* on the highest priority active net which this process requested.
@@ -1225,7 +1434,7 @@
for (int j=0; j<pids.size(); j++) {
Integer pid = (Integer)pids.get(j);
if (pid.intValue() == myPid) {
- String[] dnsList = nt.getNameServers();
+ String[] dnsList = getNameServerList(nt.getDnsPropNames());
writePidDns(dnsList, myPid);
if (doBump) {
bumpDns();
@@ -1279,7 +1488,7 @@
NetworkStateTracker nt = mNetTrackers[netType];
if (nt != null && nt.getNetworkInfo().isConnected() &&
!nt.isTeardownRequested()) {
- String[] dnsList = nt.getNameServers();
+ String[] dnsList = getNameServerList(nt.getDnsPropNames());
if (mNetAttributes[netType].isDefault()) {
int j = 1;
for (String dns : dnsList) {
@@ -1362,6 +1571,13 @@
}
pw.println();
+ synchronized (this) {
+ pw.println("NetworkTranstionWakeLock is currently " +
+ (mNetTransitionWakeLock.isHeld() ? "" : "not ") + "held.");
+ pw.println("It was last requested for "+mNetTransitionWakeLockCausedBy);
+ }
+ pw.println();
+
mTethering.dump(fd, pw, args);
}
@@ -1453,6 +1669,20 @@
FeatureUser u = (FeatureUser)msg.obj;
u.expire();
break;
+ case NetworkStateTracker.EVENT_CLEAR_NET_TRANSITION_WAKELOCK:
+ String causedBy = null;
+ synchronized (ConnectivityService.this) {
+ if (msg.arg1 == mNetTransitionWakeLockSerialNumber &&
+ mNetTransitionWakeLock.isHeld()) {
+ mNetTransitionWakeLock.release();
+ causedBy = mNetTransitionWakeLockCausedBy;
+ }
+ }
+ if (causedBy != null) {
+ Slog.d(TAG, "NetTransition Wakelock for " +
+ causedBy + " released by timeout");
+ }
+ break;
}
}
}
@@ -1536,4 +1766,23 @@
Settings.Secure.TETHER_SUPPORTED, defaultVal) != 0);
return tetherEnabledInSettings && mTetheringConfigValid;
}
+
+ // An API NetworkStateTrackers can call when they lose their network.
+ // This will automatically be cleared after X seconds or a network becomes CONNECTED,
+ // whichever happens first. The timer is started by the first caller and not
+ // restarted by subsequent callers.
+ public void requestNetworkTransitionWakelock(String forWhom) {
+ enforceConnectivityInternalPermission();
+ synchronized (this) {
+ if (mNetTransitionWakeLock.isHeld()) return;
+ mNetTransitionWakeLockSerialNumber++;
+ mNetTransitionWakeLock.acquire();
+ mNetTransitionWakeLockCausedBy = forWhom;
+ }
+ mHandler.sendMessageDelayed(mHandler.obtainMessage(
+ NetworkStateTracker.EVENT_CLEAR_NET_TRANSITION_WAKELOCK,
+ mNetTransitionWakeLockSerialNumber, 0),
+ mNetTransitionWakeLockTimeout);
+ return;
+ }
}