Add toBundle/fromBundle to AudioAttributes and DeviceInfo

PiperOrigin-RevId: 353864181
This commit is contained in:
gyumin 2021-01-26 15:38:23 +00:00 committed by kim-vde
parent f69e4be40e
commit 3e8e3737d6
4 changed files with 131 additions and 0 deletions

View File

@ -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();
}
}

View File

@ -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);
}
}

View File

@ -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);
}
}

View File

@ -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);
}
}