diff --git a/library/common/src/main/java/com/google/android/exoplayer2/audio/AudioAttributes.java b/library/common/src/main/java/com/google/android/exoplayer2/audio/AudioAttributes.java index 71ffb00982..35800e6bbd 100644 --- a/library/common/src/main/java/com/google/android/exoplayer2/audio/AudioAttributes.java +++ b/library/common/src/main/java/com/google/android/exoplayer2/audio/AudioAttributes.java @@ -15,6 +15,7 @@ */ package com.google.android.exoplayer2.audio; +import android.os.Bundle; import androidx.annotation.Nullable; import androidx.annotation.RequiresApi; import com.google.android.exoplayer2.C; @@ -33,6 +34,11 @@ import com.google.android.exoplayer2.util.Util; */ public final class AudioAttributes { + private static final String FIELD_CONTENT_TYPE = "contentType"; + private static final String FIELD_FLAGS = "flags"; + private static final String FIELD_USAGE = "usage"; + private static final String FIELD_ALLOWED_CAPTURE_POLICY = "allowedCapturePolicy"; + public static final AudioAttributes DEFAULT = new Builder().build(); /** @@ -159,4 +165,31 @@ public final class AudioAttributes { return result; } + /** Converts this instance into a {@link Bundle}. */ + public Bundle toBundle() { + Bundle bundle = new Bundle(); + bundle.putInt(FIELD_CONTENT_TYPE, contentType); + bundle.putInt(FIELD_FLAGS, flags); + bundle.putInt(FIELD_USAGE, usage); + bundle.putInt(FIELD_ALLOWED_CAPTURE_POLICY, allowedCapturePolicy); + return bundle; + } + + /** Creates an {@link AudioAttributes} instance from a {@link Bundle}. */ + public static AudioAttributes fromBundle(Bundle bundle) { + Builder builder = new Builder(); + if (bundle.containsKey(FIELD_CONTENT_TYPE)) { + builder.setContentType(bundle.getInt(FIELD_CONTENT_TYPE)); + } + if (bundle.containsKey(FIELD_FLAGS)) { + builder.setFlags(bundle.getInt(FIELD_FLAGS)); + } + if (bundle.containsKey(FIELD_USAGE)) { + builder.setUsage(bundle.getInt(FIELD_USAGE)); + } + if (bundle.containsKey(FIELD_ALLOWED_CAPTURE_POLICY)) { + builder.setAllowedCapturePolicy(bundle.getInt(FIELD_ALLOWED_CAPTURE_POLICY)); + } + return builder.build(); + } } diff --git a/library/common/src/main/java/com/google/android/exoplayer2/device/DeviceInfo.java b/library/common/src/main/java/com/google/android/exoplayer2/device/DeviceInfo.java index 8d662c318e..b640d7a820 100644 --- a/library/common/src/main/java/com/google/android/exoplayer2/device/DeviceInfo.java +++ b/library/common/src/main/java/com/google/android/exoplayer2/device/DeviceInfo.java @@ -15,6 +15,7 @@ */ package com.google.android.exoplayer2.device; +import android.os.Bundle; import androidx.annotation.IntDef; import androidx.annotation.Nullable; import java.lang.annotation.Documented; @@ -26,6 +27,10 @@ import java.lang.annotation.Target; /** Information about the playback device. */ public final class DeviceInfo { + private static final String FIELD_PLAYBACK_TYPE = "playbackType"; + private static final String FIELD_MIN_VOLUME = "minVolume"; + private static final String FIELD_MAX_VOLUME = "maxVolume"; + /** Types of playback. One of {@link #PLAYBACK_TYPE_LOCAL} or {@link #PLAYBACK_TYPE_REMOTE}. */ @Documented @Retention(RetentionPolicy.SOURCE) @@ -80,4 +85,21 @@ public final class DeviceInfo { result = 31 * result + maxVolume; return result; } + + /** Converts this instance into a {@link Bundle}. */ + public Bundle toBundle() { + Bundle bundle = new Bundle(); + bundle.putInt(FIELD_PLAYBACK_TYPE, playbackType); + bundle.putInt(FIELD_MIN_VOLUME, minVolume); + bundle.putInt(FIELD_MAX_VOLUME, maxVolume); + return bundle; + } + + /** Creates an {@link DeviceInfo} instance from a {@link Bundle}. */ + public static DeviceInfo fromBundle(Bundle bundle) { + int playbackType = bundle.getInt(FIELD_PLAYBACK_TYPE, /* defaultValue= */ PLAYBACK_TYPE_LOCAL); + int minVolume = bundle.getInt(FIELD_MIN_VOLUME, /* defaultValue= */ 0); + int maxVolume = bundle.getInt(FIELD_MAX_VOLUME, /* defaultValue= */ 0); + return new DeviceInfo(playbackType, minVolume, maxVolume); + } } diff --git a/library/common/src/test/java/com/google/android/exoplayer2/audio/AudioAttributesTest.java b/library/common/src/test/java/com/google/android/exoplayer2/audio/AudioAttributesTest.java new file mode 100644 index 0000000000..4d8193e662 --- /dev/null +++ b/library/common/src/test/java/com/google/android/exoplayer2/audio/AudioAttributesTest.java @@ -0,0 +1,41 @@ +/* + * Copyright 2021 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 com.google.android.exoplayer2.audio; + +import static com.google.common.truth.Truth.assertThat; + +import androidx.test.ext.junit.runners.AndroidJUnit4; +import com.google.android.exoplayer2.C; +import org.junit.Test; +import org.junit.runner.RunWith; + +/** Unit tests for {@link AudioAttributes}. */ +@RunWith(AndroidJUnit4.class) +public class AudioAttributesTest { + + @Test + public void roundtripViaBundle_yieldsEqualInstance() { + AudioAttributes audioAttributes = + new AudioAttributes.Builder() + .setContentType(C.CONTENT_TYPE_SONIFICATION) + .setFlags(C.FLAG_AUDIBILITY_ENFORCED) + .setUsage(C.USAGE_ALARM) + .setAllowedCapturePolicy(C.ALLOW_CAPTURE_BY_SYSTEM) + .build(); + + assertThat(AudioAttributes.fromBundle(audioAttributes.toBundle())).isEqualTo(audioAttributes); + } +} diff --git a/library/common/src/test/java/com/google/android/exoplayer2/device/DeviceInfoTest.java b/library/common/src/test/java/com/google/android/exoplayer2/device/DeviceInfoTest.java new file mode 100644 index 0000000000..d8a8e34818 --- /dev/null +++ b/library/common/src/test/java/com/google/android/exoplayer2/device/DeviceInfoTest.java @@ -0,0 +1,35 @@ +/* + * Copyright 2021 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 com.google.android.exoplayer2.device; + +import static com.google.common.truth.Truth.assertThat; + +import androidx.test.ext.junit.runners.AndroidJUnit4; +import org.junit.Test; +import org.junit.runner.RunWith; + +/** Unit tests for {@link DeviceInfo}. */ +@RunWith(AndroidJUnit4.class) +public class DeviceInfoTest { + + @Test + public void roundtripViaBundle_yieldsEqualInstance() { + DeviceInfo deviceInfo = + new DeviceInfo(DeviceInfo.PLAYBACK_TYPE_REMOTE, /* minVolume= */ 1, /* maxVolume= */ 9); + + assertThat(DeviceInfo.fromBundle(deviceInfo.toBundle())).isEqualTo(deviceInfo); + } +}