Merge "Fix API in CaptivePortal, StaticIpConfiguration"

This commit is contained in:
Remi NGUYEN VAN
2019-04-03 06:51:35 +00:00
committed by Gerrit Code Review
2 changed files with 90 additions and 23 deletions

View File

@@ -137,6 +137,8 @@ public class CaptivePortal implements Parcelable {
/** /**
* Log a captive portal login event. * Log a captive portal login event.
* @param eventId one of the CAPTIVE_PORTAL_LOGIN_* constants in metrics_constants.proto.
* @param packageName captive portal application package name.
* @hide * @hide
*/ */
@SystemApi @SystemApi

View File

@@ -22,6 +22,7 @@ import android.annotation.SystemApi;
import android.annotation.TestApi; import android.annotation.TestApi;
import android.annotation.UnsupportedAppUsage; import android.annotation.UnsupportedAppUsage;
import android.net.shared.InetAddressUtils; import android.net.shared.InetAddressUtils;
import android.os.Build;
import android.os.Parcel; import android.os.Parcel;
import android.os.Parcelable; import android.os.Parcelable;
@@ -33,20 +34,19 @@ import java.util.Objects;
/** /**
* Class that describes static IP configuration. * Class that describes static IP configuration.
* *
* This class is different from LinkProperties because it represents * <p>This class is different from {@link LinkProperties} because it represents
* configuration intent. The general contract is that if we can represent * configuration intent. The general contract is that if we can represent
* a configuration here, then we should be able to configure it on a network. * a configuration here, then we should be able to configure it on a network.
* The intent is that it closely match the UI we have for configuring networks. * The intent is that it closely match the UI we have for configuring networks.
* *
* In contrast, LinkProperties represents current state. It is much more * <p>In contrast, {@link LinkProperties} represents current state. It is much more
* expressive. For example, it supports multiple IP addresses, multiple routes, * expressive. For example, it supports multiple IP addresses, multiple routes,
* stacked interfaces, and so on. Because LinkProperties is so expressive, * stacked interfaces, and so on. Because LinkProperties is so expressive,
* using it to represent configuration intent as well as current state causes * using it to represent configuration intent as well as current state causes
* problems. For example, we could unknowingly save a configuration that we are * problems. For example, we could unknowingly save a configuration that we are
* not in fact capable of applying, or we could save a configuration that the * not in fact capable of applying, or we could save a configuration that the
* UI cannot display, which has the potential for malicious code to hide * UI cannot display, which has the potential for malicious code to hide
* hostile or unexpected configuration from the user: see, for example, * hostile or unexpected configuration from the user.
* http://b/12663469 and http://b/16893413 .
* *
* @hide * @hide
*/ */
@@ -54,24 +54,24 @@ import java.util.Objects;
@TestApi @TestApi
public final class StaticIpConfiguration implements Parcelable { public final class StaticIpConfiguration implements Parcelable {
/** @hide */ /** @hide */
@UnsupportedAppUsage @UnsupportedAppUsage(maxTargetSdk = Build.VERSION_CODES.P)
@Nullable @Nullable
public LinkAddress ipAddress; public LinkAddress ipAddress;
/** @hide */ /** @hide */
@UnsupportedAppUsage @UnsupportedAppUsage(maxTargetSdk = Build.VERSION_CODES.P)
@Nullable @Nullable
public InetAddress gateway; public InetAddress gateway;
/** @hide */ /** @hide */
@UnsupportedAppUsage @UnsupportedAppUsage(maxTargetSdk = Build.VERSION_CODES.P)
@NonNull @NonNull
public final ArrayList<InetAddress> dnsServers; public final ArrayList<InetAddress> dnsServers;
/** @hide */ /** @hide */
@UnsupportedAppUsage @UnsupportedAppUsage(maxTargetSdk = Build.VERSION_CODES.P)
@Nullable @Nullable
public String domains; public String domains;
public StaticIpConfiguration() { public StaticIpConfiguration() {
dnsServers = new ArrayList<InetAddress>(); dnsServers = new ArrayList<>();
} }
public StaticIpConfiguration(@Nullable StaticIpConfiguration source) { public StaticIpConfiguration(@Nullable StaticIpConfiguration source) {
@@ -92,32 +92,96 @@ public final class StaticIpConfiguration implements Parcelable {
domains = null; domains = null;
} }
/**
* Get the static IP address included in the configuration.
*/
public @Nullable LinkAddress getIpAddress() { public @Nullable LinkAddress getIpAddress() {
return ipAddress; return ipAddress;
} }
public void setIpAddress(@Nullable LinkAddress ipAddress) { /**
this.ipAddress = ipAddress; * Get the gateway included in the configuration.
} */
public @Nullable InetAddress getGateway() { public @Nullable InetAddress getGateway() {
return gateway; return gateway;
} }
public void setGateway(@Nullable InetAddress gateway) { /**
this.gateway = gateway; * Get the DNS servers included in the configuration.
} */
public @NonNull List<InetAddress> getDnsServers() { public @NonNull List<InetAddress> getDnsServers() {
return dnsServers; return dnsServers;
} }
/**
* Get a {@link String} listing in priority order of the comma separated domains to search when
* resolving host names on the link.
*/
public @Nullable String getDomains() { public @Nullable String getDomains() {
return domains; return domains;
} }
public void setDomains(@Nullable String newDomains) { /**
domains = newDomains; * Helper class to build a new instance of {@link StaticIpConfiguration}.
*/
public static final class Builder {
private LinkAddress mIpAddress;
private InetAddress mGateway;
private Iterable<InetAddress> mDnsServers;
private String mDomains;
/**
* Set the IP address to be included in the configuration; null by default.
* @return The {@link Builder} for chaining.
*/
public @NonNull Builder setIpAddress(@Nullable LinkAddress ipAddress) {
mIpAddress = ipAddress;
return this;
}
/**
* Set the address of the gateway to be included in the configuration; null by default.
* @return The {@link Builder} for chaining.
*/
public @NonNull Builder setGateway(@Nullable InetAddress gateway) {
mGateway = gateway;
return this;
}
/**
* Set the addresses of the DNS servers included in the configuration; empty by default.
* @return The {@link Builder} for chaining.
*/
public @NonNull Builder setDnsServers(@NonNull Iterable<InetAddress> dnsServers) {
mDnsServers = dnsServers;
return this;
}
/**
* Sets the DNS domain search path to be used on the link; null by default.
* @param newDomains A {@link String} containing the comma separated domains to search when
* resolving host names on this link, in priority order.
* @return The {@link Builder} for chaining.
*/
public @NonNull Builder setDomains(@Nullable String newDomains) {
mDomains = newDomains;
return this;
}
/**
* Create a {@link StaticIpConfiguration} from the parameters in this {@link Builder}.
* @return The newly created StaticIpConfiguration.
*/
public @NonNull StaticIpConfiguration build() {
final StaticIpConfiguration config = new StaticIpConfiguration();
config.ipAddress = mIpAddress;
config.gateway = mGateway;
for (InetAddress server : mDnsServers) {
config.dnsServers.add(server);
}
config.domains = mDomains;
return config;
}
} }
/** /**
@@ -129,16 +193,17 @@ public final class StaticIpConfiguration implements Parcelable {
/** /**
* Returns the network routes specified by this object. Will typically include a * Returns the network routes specified by this object. Will typically include a
* directly-connected route for the IP address's local subnet and a default route. If the * directly-connected route for the IP address's local subnet and a default route.
* default gateway is not covered by the directly-connected route, it will also contain a host * @param iface Interface to include in the routes.
* route to the gateway as well. This configuration is arguably invalid, but it used to work
* in K and earlier, and other OSes appear to accept it.
*/ */
public @NonNull List<RouteInfo> getRoutes(@Nullable String iface) { public @NonNull List<RouteInfo> getRoutes(@Nullable String iface) {
List<RouteInfo> routes = new ArrayList<RouteInfo>(3); List<RouteInfo> routes = new ArrayList<RouteInfo>(3);
if (ipAddress != null) { if (ipAddress != null) {
RouteInfo connectedRoute = new RouteInfo(ipAddress, null, iface); RouteInfo connectedRoute = new RouteInfo(ipAddress, null, iface);
routes.add(connectedRoute); routes.add(connectedRoute);
// If the default gateway is not covered by the directly-connected route, also add a
// host route to the gateway as well. This configuration is arguably invalid, but it
// used to work in K and earlier, and other OSes appear to accept it.
if (gateway != null && !connectedRoute.matches(gateway)) { if (gateway != null && !connectedRoute.matches(gateway)) {
routes.add(RouteInfo.makeHostRoute(gateway, iface)); routes.add(RouteInfo.makeHostRoute(gateway, iface));
} }