Merge "ConnectivityManager: allow usage of TYPE_NONE" am: d54a27d898 am: 93e1941b43

am: 0278aa4b70

Change-Id: I1707e39308b0cf425b75e1e3f85323743c252c2f
This commit is contained in:
Hugo Benichi 2017-07-05 14:47:56 +00:00 • committed by android-build-merger
commit 3d2b4e04f3
3 changed files with 156 additions and 38 deletions

View file

@ -582,6 +582,8 @@ public class ConnectivityManager {
/** {@hide} */
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
* change the networkAttributes array in framework's config.xml.
@ -640,7 +642,7 @@ public class ConnectivityManager {
*/
@Deprecated
public static boolean isNetworkTypeValid(int networkType) {
return networkType >= 0 && networkType <= MAX_NETWORK_TYPE;
return MIN_NETWORK_TYPE <= networkType && networkType <= MAX_NETWORK_TYPE;
}
/**
@ -653,6 +655,8 @@ public class ConnectivityManager {
*/
public static String getNetworkTypeName(int type) {
switch (type) {
case TYPE_NONE:
return "NONE";
case TYPE_MOBILE:
return "MOBILE";
case TYPE_WIFI:

View file

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

View file

@ -127,7 +127,8 @@ public class NetworkInfo implements Parcelable {
* @hide
*/
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);
}
mNetworkType = type;