diff options
5 files changed, 89 insertions, 22 deletions
diff --git a/framework/api/current.txt b/framework/api/current.txt index 2ede3b6c00..c8d8685bf8 100644 --- a/framework/api/current.txt +++ b/framework/api/current.txt @@ -1640,7 +1640,7 @@ package android.net.wifi.p2p { public static final class WifiP2pUsdBasedLocalServiceAdvertisementConfig.Builder { ctor public WifiP2pUsdBasedLocalServiceAdvertisementConfig.Builder(); method @NonNull public android.net.wifi.p2p.WifiP2pUsdBasedLocalServiceAdvertisementConfig build(); - method @NonNull public android.net.wifi.p2p.WifiP2pUsdBasedLocalServiceAdvertisementConfig.Builder setFrequencyMhz(@IntRange(from=0) int); + method @NonNull public android.net.wifi.p2p.WifiP2pUsdBasedLocalServiceAdvertisementConfig.Builder setFrequencyMhz(@IntRange(from=1) int); } @FlaggedApi("com.android.wifi.flags.wifi_direct_r2") public final class WifiP2pUsdBasedServiceDiscoveryConfig implements android.os.Parcelable { @@ -1751,6 +1751,7 @@ package android.net.wifi.p2p.nsd { @FlaggedApi("com.android.wifi.flags.wifi_direct_r2") public final class WifiP2pUsdBasedServiceConfig implements android.os.Parcelable { ctor public WifiP2pUsdBasedServiceConfig(); method public int describeContents(); + method public static int getMaxAllowedServiceSpecificInfoLength(); method @NonNull public String getServiceName(); method @IntRange(from=0, to=255) public int getServiceProtocolType(); method @Nullable public byte[] getServiceSpecificInfo(); @@ -1761,10 +1762,10 @@ package android.net.wifi.p2p.nsd { } public static final class WifiP2pUsdBasedServiceConfig.Builder { - ctor public WifiP2pUsdBasedServiceConfig.Builder(@NonNull String); + ctor public WifiP2pUsdBasedServiceConfig.Builder(@NonNull @Size(min=1) String); method @NonNull public android.net.wifi.p2p.nsd.WifiP2pUsdBasedServiceConfig build(); method @NonNull public android.net.wifi.p2p.nsd.WifiP2pUsdBasedServiceConfig.Builder setServiceProtocolType(@IntRange(from=0, to=255) int); - method @NonNull public android.net.wifi.p2p.nsd.WifiP2pUsdBasedServiceConfig.Builder setServiceSpecificInfo(@Nullable byte[]); + method @NonNull public android.net.wifi.p2p.nsd.WifiP2pUsdBasedServiceConfig.Builder setServiceSpecificInfo(@Nullable @Size(min=1) byte[]); } @FlaggedApi("com.android.wifi.flags.wifi_direct_r2") public final class WifiP2pUsdBasedServiceResponse implements android.os.Parcelable { diff --git a/framework/java/android/net/wifi/p2p/WifiP2pPairingBootstrappingConfig.java b/framework/java/android/net/wifi/p2p/WifiP2pPairingBootstrappingConfig.java index 4579cd2261..2b7de4b82d 100644 --- a/framework/java/android/net/wifi/p2p/WifiP2pPairingBootstrappingConfig.java +++ b/framework/java/android/net/wifi/p2p/WifiP2pPairingBootstrappingConfig.java @@ -23,6 +23,7 @@ import android.annotation.Nullable; import android.os.Build; import android.os.Parcel; import android.os.Parcelable; +import android.text.TextUtils; import androidx.annotation.RequiresApi; @@ -32,7 +33,7 @@ import java.lang.annotation.Retention; import java.lang.annotation.RetentionPolicy; /** - * A class representing Wi-Fi Direct pairing bootstrapping config + * A class representing Wi-Fi Direct pairing bootstrapping configuration. * * @see android.net.wifi.p2p.WifiP2pConfig */ @@ -92,19 +93,65 @@ public final class WifiP2pPairingBootstrappingConfig implements Parcelable { private int mPairingBootstrappingMethod; /** - * Password for pairing setup, if |pairingBootstrappingMethod| uses one of the - * |PAIRING_BOOTSTRAPPING_METHOD_*| methods other than - * PAIRING_BOOTSTRAPPING_METHOD_OPPORTUNISTIC, null otherwise. + * Password for pairing setup, if {@code mPairingBootstrappingMethod} uses + * {@link #PAIRING_BOOTSTRAPPING_METHOD_DISPLAY_PINCODE}, + * {@link #PAIRING_BOOTSTRAPPING_METHOD_DISPLAY_PASSPHRASE} or + * {@link #PAIRING_BOOTSTRAPPING_METHOD_OUT_OF_BAND}. + * Must be set to null for {@link #PAIRING_BOOTSTRAPPING_METHOD_OPPORTUNISTIC}, + * {@link #PAIRING_BOOTSTRAPPING_METHOD_KEYPAD_PINCODE} + * or {@link #PAIRING_BOOTSTRAPPING_METHOD_KEYPAD_PASSPHRASE}. */ @Nullable private String mPassword; + private static boolean isValidPairingBootstrappingMethod(@WifiP2pPairingBootstrappingConfig + .PairingBootstrappingMethod int method) { + switch (method) { + case PAIRING_BOOTSTRAPPING_METHOD_OPPORTUNISTIC: + case PAIRING_BOOTSTRAPPING_METHOD_DISPLAY_PINCODE: + case PAIRING_BOOTSTRAPPING_METHOD_DISPLAY_PASSPHRASE: + case PAIRING_BOOTSTRAPPING_METHOD_KEYPAD_PINCODE: + case PAIRING_BOOTSTRAPPING_METHOD_KEYPAD_PASSPHRASE: + case PAIRING_BOOTSTRAPPING_METHOD_OUT_OF_BAND: + return true; + default: + return false; + } + } + /** * Constructor for a WifiP2pPairingBootstrappingConfig. + * @param method One of the {@code PAIRING_BOOTSTRAPPING_METHOD_*}. + * @param password Password or PIN for pairing setup. if {@code method} is + * {@link #PAIRING_BOOTSTRAPPING_METHOD_DISPLAY_PINCODE}, the password must be + * a string containing 4 or more digits (0-9). For example: "1234", "56789". if + * {@code method} is {@link #PAIRING_BOOTSTRAPPING_METHOD_DISPLAY_PASSPHRASE} + * or {@link #PAIRING_BOOTSTRAPPING_METHOD_OUT_OF_BAND}, the password must be a + * UTF-8 string of minimum of 1 character. + * The password must be set to null if the + * {@code method} is {@link #PAIRING_BOOTSTRAPPING_METHOD_OPPORTUNISTIC}, + * {@link #PAIRING_BOOTSTRAPPING_METHOD_KEYPAD_PINCODE} or + * {@link #PAIRING_BOOTSTRAPPING_METHOD_KEYPAD_PASSPHRASE}. + * + * @throws IllegalArgumentException if the input pairing bootstrapping method is not + * one of the {@code PAIRING_BOOTSTRAPPING_METHOD_*}. + * @throws IllegalArgumentException if a non-null password is set for pairing bootstrapping + * method {@link #PAIRING_BOOTSTRAPPING_METHOD_OPPORTUNISTIC}, + * {@link #PAIRING_BOOTSTRAPPING_METHOD_KEYPAD_PINCODE} or + * {@link #PAIRING_BOOTSTRAPPING_METHOD_KEYPAD_PASSPHRASE}. */ public WifiP2pPairingBootstrappingConfig( @WifiP2pPairingBootstrappingConfig.PairingBootstrappingMethod int method, @Nullable String password) { + if (!isValidPairingBootstrappingMethod(method)) { + throw new IllegalArgumentException("Invalid PairingBootstrappingMethod =" + method); + } mPairingBootstrappingMethod = method; + if (!TextUtils.isEmpty(password) + && (method == PAIRING_BOOTSTRAPPING_METHOD_OPPORTUNISTIC + || method == PAIRING_BOOTSTRAPPING_METHOD_KEYPAD_PINCODE + || method == PAIRING_BOOTSTRAPPING_METHOD_KEYPAD_PASSPHRASE)) { + throw new IllegalArgumentException("Password is not required for =" + method); + } mPassword = password; } diff --git a/framework/java/android/net/wifi/p2p/WifiP2pUsdBasedLocalServiceAdvertisementConfig.java b/framework/java/android/net/wifi/p2p/WifiP2pUsdBasedLocalServiceAdvertisementConfig.java index 4d5a369047..3845f45c70 100644 --- a/framework/java/android/net/wifi/p2p/WifiP2pUsdBasedLocalServiceAdvertisementConfig.java +++ b/framework/java/android/net/wifi/p2p/WifiP2pUsdBasedLocalServiceAdvertisementConfig.java @@ -118,7 +118,7 @@ public final class WifiP2pUsdBasedLocalServiceAdvertisementConfig implements Par * */ @NonNull - public Builder setFrequencyMhz(@IntRange(from = 0) int frequencyMhz) { + public Builder setFrequencyMhz(@IntRange(from = 1) int frequencyMhz) { if (frequencyMhz <= 0) { throw new IllegalArgumentException("Frequency must be greater than 0"); } diff --git a/framework/java/android/net/wifi/p2p/WifiP2pUsdBasedServiceDiscoveryConfig.java b/framework/java/android/net/wifi/p2p/WifiP2pUsdBasedServiceDiscoveryConfig.java index ed0d9b804f..dfbe54c2ee 100644 --- a/framework/java/android/net/wifi/p2p/WifiP2pUsdBasedServiceDiscoveryConfig.java +++ b/framework/java/android/net/wifi/p2p/WifiP2pUsdBasedServiceDiscoveryConfig.java @@ -47,11 +47,10 @@ public final class WifiP2pUsdBasedServiceDiscoveryConfig implements Parcelable { /** * Frequencies on which the service needs to be scanned for. - * Used when band is set to WIFI_BAND_UNSPECIFIED. + * Used when band is set to ScanResult.UNSPECIFIED. */ private int[] mFrequenciesMhz; - private WifiP2pUsdBasedServiceDiscoveryConfig(int band, @NonNull int[] frequencies) { mBand = band; mFrequenciesMhz = frequencies; @@ -125,6 +124,8 @@ public final class WifiP2pUsdBasedServiceDiscoveryConfig implements Parcelable { * Builder for {@link WifiP2pUsdBasedServiceDiscoveryConfig}. */ public static final class Builder { + /** Maximum allowed number of channel frequencies */ + private static final int MAXIMUM_CHANNEL_FREQUENCIES = 48; private int mBand; private int[] mFrequenciesMhz; @@ -169,12 +170,18 @@ public final class WifiP2pUsdBasedServiceDiscoveryConfig implements Parcelable { * mutually exclusive. Setting band and frequencies both is invalid. * <p> * Optional. 2437 by default. - * @param frequenciesMhz Frequencies in MHz to scan for services. + * @param frequenciesMhz Frequencies in MHz to scan for services. This value cannot be an + * empty array of frequencies. * @return Instance of {@link Builder} to enable chaining of the builder method. */ @NonNull public Builder setFrequenciesMhz(@NonNull int[] frequenciesMhz) { Objects.requireNonNull(frequenciesMhz, "Frequencies cannot be null"); + if (frequenciesMhz.length < 1 || frequenciesMhz.length > MAXIMUM_CHANNEL_FREQUENCIES) { + throw new IllegalArgumentException("Number of frequencies: " + + frequenciesMhz.length + + " must be between 1 and " + MAXIMUM_CHANNEL_FREQUENCIES); + } mFrequenciesMhz = frequenciesMhz; return this; } diff --git a/framework/java/android/net/wifi/p2p/nsd/WifiP2pUsdBasedServiceConfig.java b/framework/java/android/net/wifi/p2p/nsd/WifiP2pUsdBasedServiceConfig.java index 41b8783d67..74834f3d44 100644 --- a/framework/java/android/net/wifi/p2p/nsd/WifiP2pUsdBasedServiceConfig.java +++ b/framework/java/android/net/wifi/p2p/nsd/WifiP2pUsdBasedServiceConfig.java @@ -22,6 +22,7 @@ import android.annotation.IntRange; import android.annotation.NonNull; import android.annotation.Nullable; import android.annotation.RequiresApi; +import android.annotation.Size; import android.os.Build; import android.os.Parcel; import android.os.Parcelable; @@ -44,6 +45,9 @@ import java.util.Objects; @RequiresApi(Build.VERSION_CODES.BAKLAVA) @FlaggedApi(Flags.FLAG_WIFI_DIRECT_R2) public final class WifiP2pUsdBasedServiceConfig implements Parcelable { + /** Maximum allowed length of service specific information */ + private static final int SERVICE_SPECIFIC_INFO_MAXIMUM_LENGTH = 1024; + /** Bonjour service protocol type */ public static final int SERVICE_PROTOCOL_TYPE_BONJOUR = 1; @@ -95,7 +99,7 @@ public final class WifiP2pUsdBasedServiceConfig implements Parcelable { return mServiceName; } - /** Get the service specific info of this USD service configuration. see also + /** Get the service specific info of this USD service configuration. See also * {@link Builder#setServiceSpecificInfo(byte[])} . * * @@ -107,6 +111,15 @@ public final class WifiP2pUsdBasedServiceConfig implements Parcelable { } /** + * Maximum allowed length of service specific information that can be set in the USD service + * configuration. + * See also {@link Builder#setServiceSpecificInfo(byte[])}. + */ + public static int getMaxAllowedServiceSpecificInfoLength() { + return SERVICE_SPECIFIC_INFO_MAXIMUM_LENGTH; + } + + /** * Generates a string of all the defined elements. * * @return a compiled string representing all elements @@ -156,9 +169,6 @@ public final class WifiP2pUsdBasedServiceConfig implements Parcelable { public static final class Builder { /** Maximum allowed length of service name */ private static final int SERVICE_NAME_MAXIMUM_LENGTH = 100; - - /** Maximum allowed length of service specific information */ - private static final int SERVICE_SPECIFIC_INFO_MAXIMUM_LENGTH = 1024; private int mServiceProtocolType = SERVICE_PROTOCOL_TYPE_GENERIC; private @NonNull String mServiceName; byte[] mServiceSpecificInfo; @@ -167,9 +177,9 @@ public final class WifiP2pUsdBasedServiceConfig implements Parcelable { * Constructor for {@link Builder}. * * @param serviceName The service name defining the service. The maximum - * allowed length of the service name is 100 bytes. + * allowed length of the service name is 100 characters. */ - public Builder(@NonNull String serviceName) { + public Builder(@Size(min = 1) @NonNull String serviceName) { Objects.requireNonNull(serviceName, "Service name cannot be null"); if (TextUtils.isEmpty(serviceName)) { throw new IllegalArgumentException("Service name cannot be empty!"); @@ -209,18 +219,20 @@ public final class WifiP2pUsdBasedServiceConfig implements Parcelable { * Optional. Empty by default. * * @param serviceSpecificInfo A byte-array of service-specific information available to the - * application to send additional information. The maximum - * allowed length of this byte-array is 1024 bytes. + * application to send additional information. Users must call + * {@link #getMaxAllowedServiceSpecificInfoLength()} method to + * know maximum allowed legth. * * @return The builder to facilitate chaining {@code builder.setXXX(..).setXXX(..)}. */ @NonNull - public Builder setServiceSpecificInfo(@Nullable byte[] serviceSpecificInfo) { + public Builder setServiceSpecificInfo( + @Size(min = 1) @Nullable byte[] serviceSpecificInfo) { if (serviceSpecificInfo != null - && serviceSpecificInfo.length > SERVICE_SPECIFIC_INFO_MAXIMUM_LENGTH) { + && serviceSpecificInfo.length > getMaxAllowedServiceSpecificInfoLength()) { throw new IllegalArgumentException("Service specific info length: " + serviceSpecificInfo.length - + " must be less than " + SERVICE_SPECIFIC_INFO_MAXIMUM_LENGTH); + + " must be less than " + getMaxAllowedServiceSpecificInfoLength()); } mServiceSpecificInfo = serviceSpecificInfo; return this; |