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 f62c6b424a..c190b42a7d 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 @@ -16,11 +16,14 @@ package com.google.android.exoplayer2.audio; import android.os.Bundle; +import androidx.annotation.IntDef; import androidx.annotation.Nullable; import androidx.annotation.RequiresApi; import com.google.android.exoplayer2.Bundleable; import com.google.android.exoplayer2.C; import com.google.android.exoplayer2.util.Util; +import java.lang.annotation.Retention; +import java.lang.annotation.RetentionPolicy; /** * Attributes for audio playback, which configure the underlying platform {@link @@ -35,11 +38,6 @@ import com.google.android.exoplayer2.util.Util; */ public final class AudioAttributes implements Bundleable { - 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(); /** @@ -166,31 +164,46 @@ public final class AudioAttributes implements Bundleable { return result; } + // Bundleable implementation. + + @Retention(RetentionPolicy.SOURCE) + @IntDef({FIELD_CONTENT_TYPE, FIELD_FLAGS, FIELD_USAGE, FIELD_ALLOWED_CAPTURE_POLICY}) + private @interface FieldNumber {} + + private static final int FIELD_CONTENT_TYPE = 0; + private static final int FIELD_FLAGS = 1; + private static final int FIELD_USAGE = 2; + private static final int FIELD_ALLOWED_CAPTURE_POLICY = 3; + @Override 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); + bundle.putInt(keyForField(FIELD_CONTENT_TYPE), contentType); + bundle.putInt(keyForField(FIELD_FLAGS), flags); + bundle.putInt(keyForField(FIELD_USAGE), usage); + bundle.putInt(keyForField(FIELD_ALLOWED_CAPTURE_POLICY), allowedCapturePolicy); return bundle; } public static final Creator CREATOR = bundle -> { Builder builder = new Builder(); - if (bundle.containsKey(FIELD_CONTENT_TYPE)) { - builder.setContentType(bundle.getInt(FIELD_CONTENT_TYPE)); + if (bundle.containsKey(keyForField(FIELD_CONTENT_TYPE))) { + builder.setContentType(bundle.getInt(keyForField(FIELD_CONTENT_TYPE))); } - if (bundle.containsKey(FIELD_FLAGS)) { - builder.setFlags(bundle.getInt(FIELD_FLAGS)); + if (bundle.containsKey(keyForField(FIELD_FLAGS))) { + builder.setFlags(bundle.getInt(keyForField(FIELD_FLAGS))); } - if (bundle.containsKey(FIELD_USAGE)) { - builder.setUsage(bundle.getInt(FIELD_USAGE)); + if (bundle.containsKey(keyForField(FIELD_USAGE))) { + builder.setUsage(bundle.getInt(keyForField(FIELD_USAGE))); } - if (bundle.containsKey(FIELD_ALLOWED_CAPTURE_POLICY)) { - builder.setAllowedCapturePolicy(bundle.getInt(FIELD_ALLOWED_CAPTURE_POLICY)); + if (bundle.containsKey(keyForField(FIELD_ALLOWED_CAPTURE_POLICY))) { + builder.setAllowedCapturePolicy(bundle.getInt(keyForField(FIELD_ALLOWED_CAPTURE_POLICY))); } return builder.build(); }; + + private static String keyForField(@FieldNumber int field) { + return Integer.toString(field, Character.MAX_RADIX); + } } 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 6e01704884..bb0386314f 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 @@ -28,10 +28,6 @@ import java.lang.annotation.Target; /** Information about the playback device. */ public final class DeviceInfo implements Bundleable { - 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) @@ -87,21 +83,36 @@ public final class DeviceInfo implements Bundleable { return result; } + // Bundleable implementation. + + @Retention(RetentionPolicy.SOURCE) + @IntDef({FIELD_PLAYBACK_TYPE, FIELD_MIN_VOLUME, FIELD_MAX_VOLUME}) + private @interface FieldNumber {} + + private static final int FIELD_PLAYBACK_TYPE = 0; + private static final int FIELD_MIN_VOLUME = 1; + private static final int FIELD_MAX_VOLUME = 2; + @Override 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); + bundle.putInt(keyForField(FIELD_PLAYBACK_TYPE), playbackType); + bundle.putInt(keyForField(FIELD_MIN_VOLUME), minVolume); + bundle.putInt(keyForField(FIELD_MAX_VOLUME), maxVolume); return bundle; } public static final Creator CREATOR = 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); + bundle.getInt( + keyForField(FIELD_PLAYBACK_TYPE), /* defaultValue= */ PLAYBACK_TYPE_LOCAL); + int minVolume = bundle.getInt(keyForField(FIELD_MIN_VOLUME), /* defaultValue= */ 0); + int maxVolume = bundle.getInt(keyForField(FIELD_MAX_VOLUME), /* defaultValue= */ 0); return new DeviceInfo(playbackType, minVolume, maxVolume); }; + + private static String keyForField(@FieldNumber int field) { + return Integer.toString(field, Character.MAX_RADIX); + } }