Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Original file line number Diff line number Diff line change
Expand Up @@ -66,6 +66,7 @@ public static final class Builder {
private @C.AudioAllowedCapturePolicy int allowedCapturePolicy;
private @C.SpatializationBehavior int spatializationBehavior;
private boolean isContentSpatialized;
private boolean hapticChannelsMuted;

/**
* Creates a new builder for {@link AudioAttributes}.
Expand All @@ -80,6 +81,7 @@ public Builder() {
allowedCapturePolicy = C.ALLOW_CAPTURE_BY_ALL;
spatializationBehavior = C.SPATIALIZATION_BEHAVIOR_AUTO;
isContentSpatialized = false;
hapticChannelsMuted = true;
}

/** See {@link android.media.AudioAttributes.Builder#setContentType(int)} */
Expand Down Expand Up @@ -125,6 +127,14 @@ public Builder setIsContentSpatialized(boolean isContentSpatialized) {
return this;
}

/** See {@link android.media.AudioAttributes.Builder#setHapticChannelsMuted(boolean)}. */
@CanIgnoreReturnValue
@UnstableApi
public Builder setHapticChannelsMuted(boolean hapticChannelsMuted) {
this.hapticChannelsMuted = hapticChannelsMuted;
return this;
}

/** Creates an {@link AudioAttributes} instance from this builder. */
public AudioAttributes build() {
return new AudioAttributes(
Expand All @@ -133,7 +143,8 @@ public AudioAttributes build() {
usage,
allowedCapturePolicy,
spatializationBehavior,
isContentSpatialized);
isContentSpatialized,
hapticChannelsMuted);
}
}

Expand All @@ -148,6 +159,7 @@ public static AudioAttributes fromPlatformAudioAttributes(
.setUsage(audioAttributes.getUsage());
if (SDK_INT >= 29) {
builder.setAllowedCapturePolicy(audioAttributes.getAllowedCapturePolicy());
builder.setHapticChannelsMuted(audioAttributes.areHapticChannelsMuted());
}
if (SDK_INT >= 32) {
builder.setSpatializationBehavior(audioAttributes.getSpatializationBehavior());
Expand All @@ -174,6 +186,9 @@ public static AudioAttributes fromPlatformAudioAttributes(
/** Whether the content is spatialized. */
@UnstableApi public final boolean isContentSpatialized;

/** Whether haptic channels are muted. */
@UnstableApi public final boolean hapticChannelsMuted;

@Nullable private android.media.AudioAttributes platformAudioAttributes;

private AudioAttributes(
Expand All @@ -182,13 +197,15 @@ private AudioAttributes(
@C.AudioUsage int usage,
@C.AudioAllowedCapturePolicy int allowedCapturePolicy,
@C.SpatializationBehavior int spatializationBehavior,
boolean isContentSpatialized) {
boolean isContentSpatialized,
boolean hapticChannelsMuted) {
this.contentType = contentType;
this.flags = flags;
this.usage = usage;
this.allowedCapturePolicy = allowedCapturePolicy;
this.spatializationBehavior = spatializationBehavior;
this.isContentSpatialized = isContentSpatialized;
this.hapticChannelsMuted = hapticChannelsMuted;
}

/**
Expand Down Expand Up @@ -216,6 +233,7 @@ public android.media.AudioAttributes getPlatformAudioAttributes() {
.setUsage(usage);
if (SDK_INT >= 29) {
Api29.setAllowedCapturePolicy(builder, allowedCapturePolicy);
Api29.setHapticChannelsMuted(builder, hapticChannelsMuted);
}
if (SDK_INT >= 32) {
Api32.setSpatializationBehavior(builder, spatializationBehavior);
Expand Down Expand Up @@ -277,7 +295,8 @@ public boolean equals(@Nullable Object obj) {
&& this.usage == other.usage
&& this.allowedCapturePolicy == other.allowedCapturePolicy
&& this.spatializationBehavior == other.spatializationBehavior
&& this.isContentSpatialized == other.isContentSpatialized;
&& this.isContentSpatialized == other.isContentSpatialized
&& this.hapticChannelsMuted == other.hapticChannelsMuted;
}

@Override
Expand All @@ -289,6 +308,7 @@ public int hashCode() {
result = 31 * result + allowedCapturePolicy;
result = 31 * result + spatializationBehavior;
result = 31 * result + (isContentSpatialized ? 1 : 0);
result = 31 * result + (hapticChannelsMuted ? 1 : 0);
return result;
}

Expand All @@ -298,6 +318,7 @@ public int hashCode() {
private static final String FIELD_ALLOWED_CAPTURE_POLICY = Util.intToStringMaxRadix(3);
private static final String FIELD_SPATIALIZATION_BEHAVIOR = Util.intToStringMaxRadix(4);
private static final String FIELD_IS_CONTENT_SPATIALIZED = Util.intToStringMaxRadix(5);
private static final String FIELD_HAPTIC_CHANNELS_MUTED = Util.intToStringMaxRadix(6);

@UnstableApi
public Bundle toBundle() {
Expand All @@ -308,6 +329,7 @@ public Bundle toBundle() {
bundle.putInt(FIELD_ALLOWED_CAPTURE_POLICY, allowedCapturePolicy);
bundle.putInt(FIELD_SPATIALIZATION_BEHAVIOR, spatializationBehavior);
bundle.putBoolean(FIELD_IS_CONTENT_SPATIALIZED, isContentSpatialized);
bundle.putBoolean(FIELD_HAPTIC_CHANNELS_MUTED, hapticChannelsMuted);
return bundle;
}

Expand All @@ -333,6 +355,9 @@ public static AudioAttributes fromBundle(Bundle bundle) {
if (bundle.containsKey(FIELD_IS_CONTENT_SPATIALIZED)) {
builder.setIsContentSpatialized(bundle.getBoolean(FIELD_IS_CONTENT_SPATIALIZED));
}
if (bundle.containsKey(FIELD_HAPTIC_CHANNELS_MUTED)) {
builder.setHapticChannelsMuted(bundle.getBoolean(FIELD_HAPTIC_CHANNELS_MUTED));
}
return builder.build();
}

Expand All @@ -344,6 +369,12 @@ public static void setAllowedCapturePolicy(
@C.AudioAllowedCapturePolicy int allowedCapturePolicy) {
builder.setAllowedCapturePolicy(allowedCapturePolicy);
}

public static void setHapticChannelsMuted(
android.media.AudioAttributes.Builder builder,
boolean hapticChannelsMuted) {
builder.setHapticChannelsMuted(hapticChannelsMuted);
}
}

@RequiresApi(32)
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -38,6 +38,7 @@ public void roundTripViaBundle_yieldsEqualInstance() {
.setAllowedCapturePolicy(C.ALLOW_CAPTURE_BY_SYSTEM)
.setSpatializationBehavior(C.SPATIALIZATION_BEHAVIOR_NEVER)
.setIsContentSpatialized(true)
.setHapticChannelsMuted(false)
.build();

assertThat(AudioAttributes.fromBundle(audioAttributes.toBundle())).isEqualTo(audioAttributes);
Expand Down Expand Up @@ -66,12 +67,14 @@ public void fromPlatformAudioAttributesV29_setsCorrectValues() {
android.media.AudioAttributes platformAttributes =
new android.media.AudioAttributes.Builder()
.setAllowedCapturePolicy(android.media.AudioAttributes.ALLOW_CAPTURE_BY_SYSTEM)
.setHapticChannelsMuted(false)
.build();

AudioAttributes audioAttributes =
AudioAttributes.fromPlatformAudioAttributes(platformAttributes);

assertThat(audioAttributes.allowedCapturePolicy).isEqualTo(C.ALLOW_CAPTURE_BY_SYSTEM);
assertThat(audioAttributes.hapticChannelsMuted).isFalse();
}

@Config(minSdk = 32)
Expand Down Expand Up @@ -99,7 +102,8 @@ public void builder_setsCorrectValues() {
.setFlags(C.FLAG_AUDIBILITY_ENFORCED)
.setAllowedCapturePolicy(C.ALLOW_CAPTURE_BY_SYSTEM)
.setIsContentSpatialized(true)
.setSpatializationBehavior(C.SPATIALIZATION_BEHAVIOR_NEVER);
.setSpatializationBehavior(C.SPATIALIZATION_BEHAVIOR_NEVER)
.setHapticChannelsMuted(false);

AudioAttributes audioAttributes = builder.build();

Expand All @@ -109,6 +113,7 @@ public void builder_setsCorrectValues() {
assertThat(audioAttributes.allowedCapturePolicy).isEqualTo(C.ALLOW_CAPTURE_BY_SYSTEM);
assertThat(audioAttributes.spatializationBehavior).isEqualTo(C.SPATIALIZATION_BEHAVIOR_NEVER);
assertThat(audioAttributes.isContentSpatialized).isTrue();
assertThat(audioAttributes.hapticChannelsMuted).isFalse();
}

@Test
Expand Down