summaryrefslogtreecommitdiff
path: root/framework/java/android/bluetooth/BluetoothLeBroadcastMetadata.java
diff options
context:
space:
mode:
authorJack He <siyuanh@google.com>2022-03-17 21:58:28 -0700
committerJack He <siyuanh@google.com>2022-03-18 02:26:14 -0700
commitee12f766d410a8d183bec4357626eadf591135ce (patch)
tree3ed7c9291c2be5d02afc1dc771fbd098d2d848b5 /framework/java/android/bluetooth/BluetoothLeBroadcastMetadata.java
parentb8918397ebc6422d6eed5b90a0306b5a4eaf3e3f (diff)
Broadcast: Strict argument checking for data structures
* Require @Nonnull elements to be not null when building or constructing objects * Enfore argument range Bug: 218683032 Test: make, cts Tag: #feature Change-Id: I53a6657323c9b93c971c9f43994a4a7c6b88e40d
Diffstat (limited to 'framework/java/android/bluetooth/BluetoothLeBroadcastMetadata.java')
-rw-r--r--framework/java/android/bluetooth/BluetoothLeBroadcastMetadata.java36
1 files changed, 31 insertions, 5 deletions
diff --git a/framework/java/android/bluetooth/BluetoothLeBroadcastMetadata.java b/framework/java/android/bluetooth/BluetoothLeBroadcastMetadata.java
index 2e748b9619..c63061a893 100644
--- a/framework/java/android/bluetooth/BluetoothLeBroadcastMetadata.java
+++ b/framework/java/android/bluetooth/BluetoothLeBroadcastMetadata.java
@@ -25,6 +25,7 @@ import android.os.Parcelable;
import java.util.ArrayList;
import java.util.List;
+import java.util.Objects;
/**
* This class represents a Broadcast Source group and the associated information that is needed
@@ -97,7 +98,7 @@ public final class BluetoothLeBroadcastMetadata implements Parcelable {
* @hide
*/
@SystemApi
- public @Nullable BluetoothDevice getSourceDevice() {
+ public @NonNull BluetoothDevice getSourceDevice() {
return mSourceDevice;
}
@@ -300,6 +301,7 @@ public final class BluetoothLeBroadcastMetadata implements Parcelable {
/**
* Create an empty builder.
+ *
* @hide
*/
@SystemApi
@@ -336,14 +338,26 @@ public final class BluetoothLeBroadcastMetadata implements Parcelable {
*
* @param sourceDevice source advertiser address
* @param sourceAddressType source advertiser address type
+ * @throws IllegalArgumentException if sourceAddressType is invalid
+ * @throws NullPointerException if sourceDevice is null
* @return this builder
* @hide
*/
@SystemApi
- public @NonNull Builder setSourceDevice(@Nullable BluetoothDevice sourceDevice,
+ public @NonNull Builder setSourceDevice(@NonNull BluetoothDevice sourceDevice,
@BluetoothDevice.AddressType int sourceAddressType) {
- mSourceDevice = sourceDevice;
+ if (sourceAddressType == BluetoothDevice.ADDRESS_TYPE_UNKNOWN) {
+ throw new IllegalArgumentException(
+ "sourceAddressType cannot be ADDRESS_TYPE_UNKNOWN");
+ }
+ if (sourceAddressType != BluetoothDevice.ADDRESS_TYPE_RANDOM
+ && sourceAddressType != BluetoothDevice.ADDRESS_TYPE_PUBLIC) {
+ throw new IllegalArgumentException("sourceAddressType " + sourceAddressType
+ + " is invalid");
+ }
+ Objects.requireNonNull(sourceDevice, "sourceDevice cannot be null");
mSourceAddressType = sourceAddressType;
+ mSourceDevice = sourceDevice;
return this;
}
@@ -433,12 +447,18 @@ public final class BluetoothLeBroadcastMetadata implements Parcelable {
*
* @param presentationDelayMicros presentation delay of this Broadcast Source in
* microseconds
+ * @throws IllegalArgumentException if presentationDelayMicros does not fall in
+ * [0, 0xFFFFFF]
* @return this builder
* @hide
*/
@SystemApi
public @NonNull Builder setPresentationDelayMicros(
@IntRange(from = 0, to = 0xFFFFFF) int presentationDelayMicros) {
+ if (presentationDelayMicros < 0 || presentationDelayMicros >= 0xFFFFFF) {
+ throw new IllegalArgumentException("presentationDelayMicros "
+ + presentationDelayMicros + " does not fall in [0, 0xFFFFFF]");
+ }
mPresentationDelayMicros = presentationDelayMicros;
return this;
}
@@ -447,11 +467,13 @@ public final class BluetoothLeBroadcastMetadata implements Parcelable {
* Add a subgroup to this broadcast source.
*
* @param subgroup {@link BluetoothLeBroadcastSubgroup} that contains a subgroup's metadata
+ * @throws NullPointerException if subgroup is null
* @return this builder
* @hide
*/
@SystemApi
public @NonNull Builder addSubgroup(@NonNull BluetoothLeBroadcastSubgroup subgroup) {
+ Objects.requireNonNull(subgroup, "subgroup cannot be null");
mSubgroups.add(subgroup);
return this;
}
@@ -474,6 +496,7 @@ public final class BluetoothLeBroadcastMetadata implements Parcelable {
*
* @return {@link BluetoothLeBroadcastMetadata}
* @throws IllegalArgumentException if the object cannot be built
+ * @throws NullPointerException if {@link NonNull} items are null
* @hide
*/
@SystemApi
@@ -481,9 +504,12 @@ public final class BluetoothLeBroadcastMetadata implements Parcelable {
if (mSourceAddressType == BluetoothDevice.ADDRESS_TYPE_UNKNOWN) {
throw new IllegalArgumentException("SourceAddressTyp cannot be unknown");
}
- if (mSourceDevice == null) {
- throw new IllegalArgumentException("SourceDevice cannot be null");
+ if (mSourceAddressType != BluetoothDevice.ADDRESS_TYPE_RANDOM
+ && mSourceAddressType != BluetoothDevice.ADDRESS_TYPE_PUBLIC) {
+ throw new IllegalArgumentException("sourceAddressType " + mSourceAddressType
+ + " is invalid");
}
+ Objects.requireNonNull(mSourceDevice, "mSourceDevice cannot be null");
if (mSubgroups.isEmpty()) {
throw new IllegalArgumentException("Must contain at least one subgroup");
}