Merge "Revert "ConnectivityManager: allow usage of TYPE_NONE"" am: a4fcd1a2b7 am: 8ad104e62c

am: bdcebe39e6

Change-Id: I428cb473221a403e3c78cbc5d1224d46747ec315
This commit is contained in:
Roland Levillain
2017-07-04 11:46:56 +00:00
committed by android-build-merger
5 changed files with 38 additions and 156 deletions

View File

@@ -582,8 +582,6 @@ public class ConnectivityManager {
/** {@hide} */ /** {@hide} */
public static final int MAX_NETWORK_TYPE = TYPE_VPN; public static final int MAX_NETWORK_TYPE = TYPE_VPN;
private static final int MIN_NETWORK_TYPE = TYPE_MOBILE;
/** /**
* If you want to set the default network preference,you can directly * If you want to set the default network preference,you can directly
* change the networkAttributes array in framework's config.xml. * change the networkAttributes array in framework's config.xml.
@@ -642,7 +640,7 @@ public class ConnectivityManager {
*/ */
@Deprecated @Deprecated
public static boolean isNetworkTypeValid(int networkType) { public static boolean isNetworkTypeValid(int networkType) {
return MIN_NETWORK_TYPE <= networkType && networkType <= MAX_NETWORK_TYPE; return networkType >= 0 && networkType <= MAX_NETWORK_TYPE;
} }
/** /**
@@ -655,8 +653,6 @@ public class ConnectivityManager {
*/ */
public static String getNetworkTypeName(int type) { public static String getNetworkTypeName(int type) {
switch (type) { switch (type) {
case TYPE_NONE:
return "NONE";
case TYPE_MOBILE: case TYPE_MOBILE:
return "MOBILE"; return "MOBILE";
case TYPE_WIFI: case TYPE_WIFI:

View File

@@ -21,7 +21,6 @@ import android.os.Parcelable;
import com.android.internal.annotations.VisibleForTesting; import com.android.internal.annotations.VisibleForTesting;
import com.android.internal.util.BitUtils; import com.android.internal.util.BitUtils;
import com.android.internal.util.Preconditions;
import java.util.Objects; import java.util.Objects;
@@ -430,11 +429,6 @@ public final class NetworkCapabilities implements Parcelable {
/** @hide */ /** @hide */
public static final int MAX_TRANSPORT = TRANSPORT_LOWPAN; public static final int MAX_TRANSPORT = TRANSPORT_LOWPAN;
/** @hide */
public static boolean isValidTransport(int transportType) {
return (MIN_TRANSPORT <= transportType) && (transportType <= MAX_TRANSPORT);
}
private static final String[] TRANSPORT_NAMES = { private static final String[] TRANSPORT_NAMES = {
"CELLULAR", "CELLULAR",
"WIFI", "WIFI",
@@ -459,7 +453,9 @@ public final class NetworkCapabilities implements Parcelable {
* @hide * @hide
*/ */
public NetworkCapabilities addTransportType(int transportType) { public NetworkCapabilities addTransportType(int transportType) {
checkValidTransportType(transportType); if (transportType < MIN_TRANSPORT || transportType > MAX_TRANSPORT) {
throw new IllegalArgumentException("TransportType out of range");
}
mTransportTypes |= 1 << transportType; mTransportTypes |= 1 << transportType;
setNetworkSpecifier(mNetworkSpecifier); // used for exception checking setNetworkSpecifier(mNetworkSpecifier); // used for exception checking
return this; return this;
@@ -473,7 +469,9 @@ public final class NetworkCapabilities implements Parcelable {
* @hide * @hide
*/ */
public NetworkCapabilities removeTransportType(int transportType) { public NetworkCapabilities removeTransportType(int transportType) {
checkValidTransportType(transportType); if (transportType < MIN_TRANSPORT || transportType > MAX_TRANSPORT) {
throw new IllegalArgumentException("TransportType out of range");
}
mTransportTypes &= ~(1 << transportType); mTransportTypes &= ~(1 << transportType);
setNetworkSpecifier(mNetworkSpecifier); // used for exception checking setNetworkSpecifier(mNetworkSpecifier); // used for exception checking
return this; return this;
@@ -497,7 +495,10 @@ public final class NetworkCapabilities implements Parcelable {
* @return {@code true} if set on this instance. * @return {@code true} if set on this instance.
*/ */
public boolean hasTransport(int transportType) { public boolean hasTransport(int transportType) {
return isValidTransport(transportType) && ((mTransportTypes & (1 << transportType)) != 0); if (transportType < MIN_TRANSPORT || transportType > MAX_TRANSPORT) {
return false;
}
return ((mTransportTypes & (1 << transportType)) != 0);
} }
private void combineTransportTypes(NetworkCapabilities nc) { private void combineTransportTypes(NetworkCapabilities nc) {
@@ -905,14 +906,9 @@ public final class NetworkCapabilities implements Parcelable {
* @hide * @hide
*/ */
public static String transportNameOf(int transport) { public static String transportNameOf(int transport) {
if (!isValidTransport(transport)) { if (transport < 0 || TRANSPORT_NAMES.length <= transport) {
return "UNKNOWN"; return "UNKNOWN";
} }
return TRANSPORT_NAMES[transport]; return TRANSPORT_NAMES[transport];
} }
private static void checkValidTransportType(int transport) {
Preconditions.checkArgument(
isValidTransport(transport), "Invalid TransportType " + transport);
}
} }

View File

@@ -127,8 +127,7 @@ public class NetworkInfo implements Parcelable {
* @hide * @hide
*/ */
public NetworkInfo(int type, int subtype, String typeName, String subtypeName) { public NetworkInfo(int type, int subtype, String typeName, String subtypeName) {
if (!ConnectivityManager.isNetworkTypeValid(type) if (!ConnectivityManager.isNetworkTypeValid(type)) {
&& type != ConnectivityManager.TYPE_NONE) {
throw new IllegalArgumentException("Invalid network type: " + type); throw new IllegalArgumentException("Invalid network type: " + type);
} }
mNetworkType = type; mNetworkType = type;

View File

@@ -3784,9 +3784,6 @@ public class ConnectivityService extends IConnectivityManager.Stub
public void setProvisioningNotificationVisible(boolean visible, int networkType, public void setProvisioningNotificationVisible(boolean visible, int networkType,
String action) { String action) {
enforceConnectivityInternalPermission(); enforceConnectivityInternalPermission();
if (!ConnectivityManager.isNetworkTypeValid(networkType)) {
return;
}
final long ident = Binder.clearCallingIdentity(); final long ident = Binder.clearCallingIdentity();
try { try {
// Concatenate the range of types onto the range of NetIDs. // Concatenate the range of types onto the range of NetIDs.

View File

@@ -19,7 +19,6 @@ package com.android.server;
import static android.net.ConnectivityManager.CONNECTIVITY_ACTION; import static android.net.ConnectivityManager.CONNECTIVITY_ACTION;
import static android.net.ConnectivityManager.TYPE_ETHERNET; import static android.net.ConnectivityManager.TYPE_ETHERNET;
import static android.net.ConnectivityManager.TYPE_MOBILE; import static android.net.ConnectivityManager.TYPE_MOBILE;
import static android.net.ConnectivityManager.TYPE_NONE;
import static android.net.ConnectivityManager.TYPE_WIFI; import static android.net.ConnectivityManager.TYPE_WIFI;
import static android.net.ConnectivityManager.getNetworkTypeName; import static android.net.ConnectivityManager.getNetworkTypeName;
import static android.net.NetworkCapabilities.*; import static android.net.NetworkCapabilities.*;
@@ -115,7 +114,6 @@ import java.util.concurrent.LinkedBlockingQueue;
import java.util.concurrent.TimeUnit; import java.util.concurrent.TimeUnit;
import java.util.concurrent.atomic.AtomicBoolean; import java.util.concurrent.atomic.AtomicBoolean;
import java.util.function.BooleanSupplier; import java.util.function.BooleanSupplier;
import java.util.function.Predicate;
/** /**
* Tests for {@link ConnectivityService}. * Tests for {@link ConnectivityService}.
@@ -319,9 +317,6 @@ public class ConnectivityServiceTest extends AndroidTestCase {
case TRANSPORT_CELLULAR: case TRANSPORT_CELLULAR:
mScore = 50; mScore = 50;
break; break;
case TRANSPORT_LOWPAN:
mScore = 20;
break;
default: default:
throw new UnsupportedOperationException("unimplemented network type"); throw new UnsupportedOperationException("unimplemented network type");
} }
@@ -403,15 +398,6 @@ public class ConnectivityServiceTest extends AndroidTestCase {
* @param validated Indicate if network should pretend to be validated. * @param validated Indicate if network should pretend to be validated.
*/ */
public void connect(boolean validated) { public void connect(boolean validated) {
connect(validated, true);
}
/**
* Transition this NetworkAgent to CONNECTED state.
* @param validated Indicate if network should pretend to be validated.
* @param hasInternet Indicate if network should pretend to have NET_CAPABILITY_INTERNET.
*/
public void connect(boolean validated, boolean hasInternet) {
assertEquals("MockNetworkAgents can only be connected once", assertEquals("MockNetworkAgents can only be connected once",
mNetworkInfo.getDetailedState(), DetailedState.IDLE); mNetworkInfo.getDetailedState(), DetailedState.IDLE);
assertFalse(mNetworkCapabilities.hasCapability(NET_CAPABILITY_INTERNET)); assertFalse(mNetworkCapabilities.hasCapability(NET_CAPABILITY_INTERNET));
@@ -434,9 +420,7 @@ public class ConnectivityServiceTest extends AndroidTestCase {
}; };
mCm.registerNetworkCallback(request, callback); mCm.registerNetworkCallback(request, callback);
} }
if (hasInternet) {
addCapability(NET_CAPABILITY_INTERNET); addCapability(NET_CAPABILITY_INTERNET);
}
connectWithoutInternet(); connectWithoutInternet();
@@ -790,10 +774,7 @@ public class ConnectivityServiceTest extends AndroidTestCase {
* Fails if TIMEOUT_MS goes by before {@code conditionVariable} opens. * Fails if TIMEOUT_MS goes by before {@code conditionVariable} opens.
*/ */
static private void waitFor(ConditionVariable conditionVariable) { static private void waitFor(ConditionVariable conditionVariable) {
if (conditionVariable.block(TIMEOUT_MS)) { assertTrue(conditionVariable.block(TIMEOUT_MS));
return;
}
fail("ConditionVariable was blocked for more than " + TIMEOUT_MS + "ms");
} }
@Override @Override
@@ -851,7 +832,7 @@ public class ConnectivityServiceTest extends AndroidTestCase {
case TRANSPORT_CELLULAR: case TRANSPORT_CELLULAR:
return TYPE_MOBILE; return TYPE_MOBILE;
default: default:
return TYPE_NONE; throw new IllegalStateException("Unknown transport " + transport);
} }
} }
@@ -862,9 +843,6 @@ public class ConnectivityServiceTest extends AndroidTestCase {
// Test getActiveNetwork() // Test getActiveNetwork()
assertNotNull(mCm.getActiveNetwork()); assertNotNull(mCm.getActiveNetwork());
assertEquals(mCm.getActiveNetwork(), mCm.getActiveNetworkForUid(Process.myUid())); assertEquals(mCm.getActiveNetwork(), mCm.getActiveNetworkForUid(Process.myUid()));
if (!NetworkCapabilities.isValidTransport(transport)) {
throw new IllegalStateException("Unknown transport " + transport);
}
switch (transport) { switch (transport) {
case TRANSPORT_WIFI: case TRANSPORT_WIFI:
assertEquals(mCm.getActiveNetwork(), mWiFiNetworkAgent.getNetwork()); assertEquals(mCm.getActiveNetwork(), mWiFiNetworkAgent.getNetwork());
@@ -873,7 +851,7 @@ public class ConnectivityServiceTest extends AndroidTestCase {
assertEquals(mCm.getActiveNetwork(), mCellNetworkAgent.getNetwork()); assertEquals(mCm.getActiveNetwork(), mCellNetworkAgent.getNetwork());
break; break;
default: default:
break; throw new IllegalStateException("Unknown transport" + transport);
} }
// Test getNetworkInfo(Network) // Test getNetworkInfo(Network)
assertNotNull(mCm.getNetworkInfo(mCm.getActiveNetwork())); assertNotNull(mCm.getNetworkInfo(mCm.getActiveNetwork()));
@@ -1293,26 +1271,7 @@ public class ConnectivityServiceTest extends AndroidTestCase {
return expectCallback(state, agent, TIMEOUT_MS); return expectCallback(state, agent, TIMEOUT_MS);
} }
CallbackInfo expectCallbackLike(Predicate<CallbackInfo> fn) { void expectAvailableCallbacks(MockNetworkAgent agent, boolean expectSuspended, int timeoutMs) {
return expectCallbackLike(fn, TIMEOUT_MS);
}
CallbackInfo expectCallbackLike(Predicate<CallbackInfo> fn, int timeoutMs) {
int timeLeft = timeoutMs;
while (timeLeft > 0) {
long start = SystemClock.elapsedRealtime();
CallbackInfo info = nextCallback(timeLeft);
if (fn.test(info)) {
return info;
}
timeLeft -= (SystemClock.elapsedRealtime() - start);
}
fail("Did not receive expected callback after " + timeoutMs + "ms");
return null;
}
void expectAvailableCallbacks(
MockNetworkAgent agent, boolean expectSuspended, int timeoutMs) {
expectCallback(CallbackState.AVAILABLE, agent, timeoutMs); expectCallback(CallbackState.AVAILABLE, agent, timeoutMs);
if (expectSuspended) { if (expectSuspended) {
expectCallback(CallbackState.SUSPENDED, agent, timeoutMs); expectCallback(CallbackState.SUSPENDED, agent, timeoutMs);
@@ -1869,18 +1828,26 @@ public class ConnectivityServiceTest extends AndroidTestCase {
@SmallTest @SmallTest
public void testNoMutableNetworkRequests() throws Exception { public void testNoMutableNetworkRequests() throws Exception {
PendingIntent pendingIntent = PendingIntent.getBroadcast(mContext, 0, new Intent("a"), 0); PendingIntent pendingIntent = PendingIntent.getBroadcast(mContext, 0, new Intent("a"), 0);
NetworkRequest request1 = new NetworkRequest.Builder() NetworkRequest.Builder builder = new NetworkRequest.Builder();
.addCapability(NET_CAPABILITY_VALIDATED) builder.addCapability(NET_CAPABILITY_VALIDATED);
.build(); try {
NetworkRequest request2 = new NetworkRequest.Builder() mCm.requestNetwork(builder.build(), new NetworkCallback());
.addCapability(NET_CAPABILITY_CAPTIVE_PORTAL) fail();
.build(); } catch (IllegalArgumentException expected) {}
try {
Class<IllegalArgumentException> expected = IllegalArgumentException.class; mCm.requestNetwork(builder.build(), pendingIntent);
assertException(() -> { mCm.requestNetwork(request1, new NetworkCallback()); }, expected); fail();
assertException(() -> { mCm.requestNetwork(request1, pendingIntent); }, expected); } catch (IllegalArgumentException expected) {}
assertException(() -> { mCm.requestNetwork(request2, new NetworkCallback()); }, expected); builder = new NetworkRequest.Builder();
assertException(() -> { mCm.requestNetwork(request2, pendingIntent); }, expected); builder.addCapability(NET_CAPABILITY_CAPTIVE_PORTAL);
try {
mCm.requestNetwork(builder.build(), new NetworkCallback());
fail();
} catch (IllegalArgumentException expected) {}
try {
mCm.requestNetwork(builder.build(), pendingIntent);
fail();
} catch (IllegalArgumentException expected) {}
} }
@SmallTest @SmallTest
@@ -3282,79 +3249,6 @@ public class ConnectivityServiceTest extends AndroidTestCase {
} }
} }
@SmallTest
public void testNetworkInfoOfTypeNone() {
ConditionVariable broadcastCV = waitForConnectivityBroadcasts(1);
verifyNoNetwork();
MockNetworkAgent lowpanNetwork = new MockNetworkAgent(TRANSPORT_LOWPAN);
assertNull(mCm.getActiveNetworkInfo());
Network[] allNetworks = mCm.getAllNetworks();
assertEquals(1, allNetworks.length);
Network network = allNetworks[0];
NetworkCapabilities capabilities = mCm.getNetworkCapabilities(network);
assertTrue(capabilities.hasTransport(TRANSPORT_LOWPAN));
final NetworkRequest request =
new NetworkRequest.Builder().addTransportType(TRANSPORT_LOWPAN).build();
final TestNetworkCallback callback = new TestNetworkCallback();
mCm.registerNetworkCallback(request, callback);
// Bring up lowpan.
lowpanNetwork.connect(false, false);
callback.expectAvailableCallbacks(lowpanNetwork);
assertNull(mCm.getActiveNetworkInfo());
assertNull(mCm.getActiveNetwork());
// TODO: getAllNetworkInfo is dirty and returns a non-empty array rght from the start
// of this test. Fix it and uncomment the assert below.
//assertEquals(0, mCm.getAllNetworkInfo().length);
// Disconnect lowpan.
lowpanNetwork.disconnect();
callback.expectCallback(CallbackState.LOST, lowpanNetwork);
mCm.unregisterNetworkCallback(callback);
verifyNoNetwork();
if (broadcastCV.block(10)) {
fail("expected no broadcast, but got CONNECTIVITY_ACTION broadcast");
}
}
@SmallTest
public void testDeprecatedAndUnsupportedOperations() throws Exception {
final int TYPE_NONE = ConnectivityManager.TYPE_NONE;
assertNull(mCm.getNetworkInfo(TYPE_NONE));
assertNull(mCm.getNetworkForType(TYPE_NONE));
assertNull(mCm.getLinkProperties(TYPE_NONE));
assertFalse(mCm.isNetworkSupported(TYPE_NONE));
assertException(() -> { mCm.networkCapabilitiesForType(TYPE_NONE); },
IllegalArgumentException.class);
Class<UnsupportedOperationException> unsupported = UnsupportedOperationException.class;
assertException(() -> { mCm.startUsingNetworkFeature(TYPE_WIFI, ""); }, unsupported);
assertException(() -> { mCm.stopUsingNetworkFeature(TYPE_WIFI, ""); }, unsupported);
// TODO: let test context have configuration application target sdk version
// and test that pre-M requesting for TYPE_NONE sends back APN_REQUEST_FAILED
assertException(() -> { mCm.startUsingNetworkFeature(TYPE_NONE, ""); }, unsupported);
assertException(() -> { mCm.stopUsingNetworkFeature(TYPE_NONE, ""); }, unsupported);
assertException(() -> { mCm.requestRouteToHostAddress(TYPE_NONE, null); }, unsupported);
}
private static <T> void assertException(Runnable block, Class<T> expected) {
try {
block.run();
fail("Expected exception of type " + expected);
} catch (Exception got) {
if (!got.getClass().equals(expected)) {
fail("Expected exception of type " + expected + " but got " + got);
}
return;
}
}
/* test utilities */ /* test utilities */
// TODO: eliminate all usages of sleepFor and replace by proper timeouts/waitForIdle. // TODO: eliminate all usages of sleepFor and replace by proper timeouts/waitForIdle.
static private void sleepFor(int ms) { static private void sleepFor(int ms) {