Merge "[CS] Add a generic transport-specific information API" am: 0e18882d5c
am: 7081aa381d
Change-Id: If70fb160c90afbfb646ca7474d06fe735031aed5
This commit is contained in:
@@ -17,6 +17,7 @@
|
||||
package android.net;
|
||||
|
||||
import android.annotation.IntDef;
|
||||
import android.annotation.Nullable;
|
||||
import android.annotation.SystemApi;
|
||||
import android.annotation.TestApi;
|
||||
import android.annotation.UnsupportedAppUsage;
|
||||
@@ -79,6 +80,7 @@ public final class NetworkCapabilities implements Parcelable {
|
||||
mNetworkCapabilities = mTransportTypes = mUnwantedNetworkCapabilities = 0;
|
||||
mLinkUpBandwidthKbps = mLinkDownBandwidthKbps = LINK_BANDWIDTH_UNSPECIFIED;
|
||||
mNetworkSpecifier = null;
|
||||
mTransportInfo = null;
|
||||
mSignalStrength = SIGNAL_STRENGTH_UNSPECIFIED;
|
||||
mUids = null;
|
||||
mEstablishingVpnAppUid = INVALID_UID;
|
||||
@@ -95,6 +97,7 @@ public final class NetworkCapabilities implements Parcelable {
|
||||
mLinkUpBandwidthKbps = nc.mLinkUpBandwidthKbps;
|
||||
mLinkDownBandwidthKbps = nc.mLinkDownBandwidthKbps;
|
||||
mNetworkSpecifier = nc.mNetworkSpecifier;
|
||||
mTransportInfo = nc.mTransportInfo;
|
||||
mSignalStrength = nc.mSignalStrength;
|
||||
setUids(nc.mUids); // Will make the defensive copy
|
||||
mEstablishingVpnAppUid = nc.mEstablishingVpnAppUid;
|
||||
@@ -874,6 +877,7 @@ public final class NetworkCapabilities implements Parcelable {
|
||||
}
|
||||
|
||||
private NetworkSpecifier mNetworkSpecifier = null;
|
||||
private TransportInfo mTransportInfo = null;
|
||||
|
||||
/**
|
||||
* Sets the optional bearer specific network specifier.
|
||||
@@ -898,6 +902,19 @@ public final class NetworkCapabilities implements Parcelable {
|
||||
return this;
|
||||
}
|
||||
|
||||
/**
|
||||
* Sets the optional transport specific information.
|
||||
*
|
||||
* @param transportInfo A concrete, parcelable framework class that extends
|
||||
* {@link TransportInfo}.
|
||||
* @return This NetworkCapabilities instance, to facilitate chaining.
|
||||
* @hide
|
||||
*/
|
||||
public NetworkCapabilities setTransportInfo(TransportInfo transportInfo) {
|
||||
mTransportInfo = transportInfo;
|
||||
return this;
|
||||
}
|
||||
|
||||
/**
|
||||
* Gets the optional bearer specific network specifier.
|
||||
*
|
||||
@@ -910,6 +927,19 @@ public final class NetworkCapabilities implements Parcelable {
|
||||
return mNetworkSpecifier;
|
||||
}
|
||||
|
||||
/**
|
||||
* Returns a transport-specific information container. The application may cast this
|
||||
* container to a concrete sub-class based on its knowledge of the network request. The
|
||||
* application should be able to deal with a {@code null} return value or an invalid case,
|
||||
* e.g. use {@code instanceof} operation to verify expected type.
|
||||
*
|
||||
* @return A concrete implementation of the {@link TransportInfo} class or null if not
|
||||
* available for the network.
|
||||
*/
|
||||
@Nullable public TransportInfo getTransportInfo() {
|
||||
return mTransportInfo;
|
||||
}
|
||||
|
||||
private void combineSpecifiers(NetworkCapabilities nc) {
|
||||
if (mNetworkSpecifier != null && !mNetworkSpecifier.equals(nc.mNetworkSpecifier)) {
|
||||
throw new IllegalStateException("Can't combine two networkSpecifiers");
|
||||
@@ -926,6 +956,17 @@ public final class NetworkCapabilities implements Parcelable {
|
||||
return Objects.equals(mNetworkSpecifier, nc.mNetworkSpecifier);
|
||||
}
|
||||
|
||||
private void combineTransportInfos(NetworkCapabilities nc) {
|
||||
if (mTransportInfo != null && !mTransportInfo.equals(nc.mTransportInfo)) {
|
||||
throw new IllegalStateException("Can't combine two TransportInfos");
|
||||
}
|
||||
setTransportInfo(nc.mTransportInfo);
|
||||
}
|
||||
|
||||
private boolean equalsTransportInfo(NetworkCapabilities nc) {
|
||||
return Objects.equals(mTransportInfo, nc.mTransportInfo);
|
||||
}
|
||||
|
||||
/**
|
||||
* Magic value that indicates no signal strength provided. A request specifying this value is
|
||||
* always satisfied.
|
||||
@@ -1238,6 +1279,7 @@ public final class NetworkCapabilities implements Parcelable {
|
||||
combineTransportTypes(nc);
|
||||
combineLinkBandwidths(nc);
|
||||
combineSpecifiers(nc);
|
||||
combineTransportInfos(nc);
|
||||
combineSignalStrength(nc);
|
||||
combineUids(nc);
|
||||
combineSSIDs(nc);
|
||||
@@ -1347,6 +1389,7 @@ public final class NetworkCapabilities implements Parcelable {
|
||||
&& equalsLinkBandwidths(that)
|
||||
&& equalsSignalStrength(that)
|
||||
&& equalsSpecifier(that)
|
||||
&& equalsTransportInfo(that)
|
||||
&& equalsUids(that)
|
||||
&& equalsSSID(that));
|
||||
}
|
||||
@@ -1364,7 +1407,8 @@ public final class NetworkCapabilities implements Parcelable {
|
||||
+ Objects.hashCode(mNetworkSpecifier) * 23
|
||||
+ (mSignalStrength * 29)
|
||||
+ Objects.hashCode(mUids) * 31
|
||||
+ Objects.hashCode(mSSID) * 37;
|
||||
+ Objects.hashCode(mSSID) * 37
|
||||
+ Objects.hashCode(mTransportInfo) * 41;
|
||||
}
|
||||
|
||||
@Override
|
||||
@@ -1379,6 +1423,7 @@ public final class NetworkCapabilities implements Parcelable {
|
||||
dest.writeInt(mLinkUpBandwidthKbps);
|
||||
dest.writeInt(mLinkDownBandwidthKbps);
|
||||
dest.writeParcelable((Parcelable) mNetworkSpecifier, flags);
|
||||
dest.writeParcelable((Parcelable) mTransportInfo, flags);
|
||||
dest.writeInt(mSignalStrength);
|
||||
dest.writeArraySet(mUids);
|
||||
dest.writeString(mSSID);
|
||||
@@ -1396,6 +1441,7 @@ public final class NetworkCapabilities implements Parcelable {
|
||||
netCap.mLinkUpBandwidthKbps = in.readInt();
|
||||
netCap.mLinkDownBandwidthKbps = in.readInt();
|
||||
netCap.mNetworkSpecifier = in.readParcelable(null);
|
||||
netCap.mTransportInfo = in.readParcelable(null);
|
||||
netCap.mSignalStrength = in.readInt();
|
||||
netCap.mUids = (ArraySet<UidRange>) in.readArraySet(
|
||||
null /* ClassLoader, null for default */);
|
||||
@@ -1435,6 +1481,9 @@ public final class NetworkCapabilities implements Parcelable {
|
||||
if (mNetworkSpecifier != null) {
|
||||
sb.append(" Specifier: <").append(mNetworkSpecifier).append(">");
|
||||
}
|
||||
if (mTransportInfo != null) {
|
||||
sb.append(" TransportInfo: <").append(mTransportInfo).append(">");
|
||||
}
|
||||
if (hasSignalStrength()) {
|
||||
sb.append(" SignalStrength: ").append(mSignalStrength);
|
||||
}
|
||||
@@ -1501,6 +1550,9 @@ public final class NetworkCapabilities implements Parcelable {
|
||||
if (mNetworkSpecifier != null) {
|
||||
proto.write(NetworkCapabilitiesProto.NETWORK_SPECIFIER, mNetworkSpecifier.toString());
|
||||
}
|
||||
if (mTransportInfo != null) {
|
||||
// TODO b/120653863: write transport-specific info to proto?
|
||||
}
|
||||
|
||||
proto.write(NetworkCapabilitiesProto.CAN_REPORT_SIGNAL_STRENGTH, hasSignalStrength());
|
||||
proto.write(NetworkCapabilitiesProto.SIGNAL_STRENGTH, mSignalStrength);
|
||||
|
||||
25
core/java/android/net/TransportInfo.java
Normal file
25
core/java/android/net/TransportInfo.java
Normal file
@@ -0,0 +1,25 @@
|
||||
/*
|
||||
* Copyright (C) 2018 The Android Open Source Project
|
||||
*
|
||||
* Licensed under the Apache License, Version 2.0 (the "License");
|
||||
* you may not use this file except in compliance with the License.
|
||||
* You may obtain a copy of the License at
|
||||
*
|
||||
* http://www.apache.org/licenses/LICENSE-2.0
|
||||
*
|
||||
* Unless required by applicable law or agreed to in writing, software
|
||||
* distributed under the License is distributed on an "AS IS" BASIS,
|
||||
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
|
||||
* See the License for the specific language governing permissions and
|
||||
* limitations under the License.
|
||||
*/
|
||||
|
||||
package android.net;
|
||||
|
||||
/**
|
||||
* A container for transport-specific capabilities which is returned by
|
||||
* {@link NetworkCapabilities#getTransportInfo()}. Specific networks
|
||||
* may provide concrete implementations of this interface.
|
||||
*/
|
||||
public interface TransportInfo {
|
||||
}
|
||||
@@ -24,9 +24,9 @@ import static android.net.NetworkCapabilities.NET_CAPABILITY_INTERNET;
|
||||
import static android.net.NetworkCapabilities.NET_CAPABILITY_MMS;
|
||||
import static android.net.NetworkCapabilities.NET_CAPABILITY_NOT_METERED;
|
||||
import static android.net.NetworkCapabilities.NET_CAPABILITY_NOT_RESTRICTED;
|
||||
import static android.net.NetworkCapabilities.NET_CAPABILITY_OEM_PAID;
|
||||
import static android.net.NetworkCapabilities.NET_CAPABILITY_NOT_ROAMING;
|
||||
import static android.net.NetworkCapabilities.NET_CAPABILITY_NOT_VPN;
|
||||
import static android.net.NetworkCapabilities.NET_CAPABILITY_OEM_PAID;
|
||||
import static android.net.NetworkCapabilities.NET_CAPABILITY_VALIDATED;
|
||||
import static android.net.NetworkCapabilities.NET_CAPABILITY_WIFI_P2P;
|
||||
import static android.net.NetworkCapabilities.RESTRICTED_CAPABILITIES;
|
||||
@@ -46,7 +46,6 @@ import android.support.test.runner.AndroidJUnit4;
|
||||
import android.test.suitebuilder.annotation.SmallTest;
|
||||
import android.util.ArraySet;
|
||||
|
||||
|
||||
import org.junit.Test;
|
||||
import org.junit.runner.RunWith;
|
||||
|
||||
@@ -457,6 +456,62 @@ public class NetworkCapabilitiesTest {
|
||||
assertEquals(nc1, nc2);
|
||||
}
|
||||
|
||||
@Test
|
||||
public void testSetNetworkSpecifierOnMultiTransportNc() {
|
||||
// Sequence 1: Transport + Transport + NetworkSpecifier
|
||||
NetworkCapabilities nc1 = new NetworkCapabilities();
|
||||
nc1.addTransportType(TRANSPORT_CELLULAR).addTransportType(TRANSPORT_WIFI);
|
||||
try {
|
||||
nc1.setNetworkSpecifier(new StringNetworkSpecifier("specs"));
|
||||
fail("Cannot set NetworkSpecifier on a NetworkCapability with multiple transports!");
|
||||
} catch (IllegalStateException expected) {
|
||||
// empty
|
||||
}
|
||||
|
||||
// Sequence 2: Transport + NetworkSpecifier + Transport
|
||||
NetworkCapabilities nc2 = new NetworkCapabilities();
|
||||
nc2.addTransportType(TRANSPORT_CELLULAR).setNetworkSpecifier(
|
||||
new StringNetworkSpecifier("specs"));
|
||||
try {
|
||||
nc2.addTransportType(TRANSPORT_WIFI);
|
||||
fail("Cannot set NetworkSpecifier on a NetworkCapability with multiple transports!");
|
||||
} catch (IllegalStateException expected) {
|
||||
// empty
|
||||
}
|
||||
}
|
||||
|
||||
@Test
|
||||
public void testSetTransportInfoOnMultiTransportNc() {
|
||||
// Sequence 1: Transport + Transport + TransportInfo
|
||||
NetworkCapabilities nc1 = new NetworkCapabilities();
|
||||
nc1.addTransportType(TRANSPORT_CELLULAR).addTransportType(TRANSPORT_WIFI)
|
||||
.setTransportInfo(new TransportInfo() {});
|
||||
|
||||
// Sequence 2: Transport + NetworkSpecifier + Transport
|
||||
NetworkCapabilities nc2 = new NetworkCapabilities();
|
||||
nc2.addTransportType(TRANSPORT_CELLULAR).setTransportInfo(new TransportInfo() {})
|
||||
.addTransportType(TRANSPORT_WIFI);
|
||||
}
|
||||
|
||||
@Test
|
||||
public void testCombineTransportInfo() {
|
||||
NetworkCapabilities nc1 = new NetworkCapabilities();
|
||||
nc1.setTransportInfo(new TransportInfo() {
|
||||
// empty
|
||||
});
|
||||
NetworkCapabilities nc2 = new NetworkCapabilities();
|
||||
nc2.setTransportInfo(new TransportInfo() {
|
||||
// empty
|
||||
});
|
||||
|
||||
try {
|
||||
nc1.combineCapabilities(nc2);
|
||||
fail("Should not be able to combine NetworkCaabilities which contain TransportInfos");
|
||||
} catch (IllegalStateException expected) {
|
||||
// empty
|
||||
}
|
||||
}
|
||||
|
||||
private void assertEqualsThroughMarshalling(NetworkCapabilities netCap) {
|
||||
Parcel p = Parcel.obtain();
|
||||
netCap.writeToParcel(p, /* flags */ 0);
|
||||
|
||||
Reference in New Issue
Block a user