Commit d49437c6 by rohks Committed by christosts

Initialise fields used for bundling as String directly

Initialising the fields as Integer and then getting a String on compute time is slow. Instead we directly initialise these fields as String. Improves the time taken in bundling PlayerInfo further to less than 200ms from ~300ms.

Also modified a test to improve productive coverage.

PiperOrigin-RevId: 500003935
parent bdd6818c
Showing with 317 additions and 713 deletions
...@@ -30,7 +30,6 @@ import static com.google.android.exoplayer2.util.Assertions.checkNotNull; ...@@ -30,7 +30,6 @@ import static com.google.android.exoplayer2.util.Assertions.checkNotNull;
import static com.google.android.exoplayer2.util.Assertions.checkState; import static com.google.android.exoplayer2.util.Assertions.checkState;
import static com.google.android.exoplayer2.util.Util.msToUs; import static com.google.android.exoplayer2.util.Util.msToUs;
import static com.google.android.exoplayer2.util.Util.usToMs; import static com.google.android.exoplayer2.util.Util.usToMs;
import static java.lang.annotation.ElementType.TYPE_USE;
import android.content.Context; import android.content.Context;
import android.net.Uri; import android.net.Uri;
...@@ -39,7 +38,6 @@ import android.os.Handler; ...@@ -39,7 +38,6 @@ import android.os.Handler;
import android.os.Looper; import android.os.Looper;
import android.util.Pair; import android.util.Pair;
import android.view.ViewGroup; import android.view.ViewGroup;
import androidx.annotation.IntDef;
import androidx.annotation.MainThread; import androidx.annotation.MainThread;
import androidx.annotation.Nullable; import androidx.annotation.Nullable;
import androidx.annotation.VisibleForTesting; import androidx.annotation.VisibleForTesting;
...@@ -97,10 +95,6 @@ import com.google.common.collect.ImmutableList; ...@@ -97,10 +95,6 @@ import com.google.common.collect.ImmutableList;
import com.google.common.collect.ImmutableMap; import com.google.common.collect.ImmutableMap;
import com.google.errorprone.annotations.CanIgnoreReturnValue; import com.google.errorprone.annotations.CanIgnoreReturnValue;
import java.io.IOException; import java.io.IOException;
import java.lang.annotation.Documented;
import java.lang.annotation.Retention;
import java.lang.annotation.RetentionPolicy;
import java.lang.annotation.Target;
import java.util.ArrayList; import java.util.ArrayList;
import java.util.Collection; import java.util.Collection;
import java.util.HashMap; import java.util.HashMap;
...@@ -326,13 +320,7 @@ public final class ImaServerSideAdInsertionMediaSource extends CompositeMediaSou ...@@ -326,13 +320,7 @@ public final class ImaServerSideAdInsertionMediaSource extends CompositeMediaSou
// Bundleable implementation. // Bundleable implementation.
@Documented private static final String FIELD_AD_PLAYBACK_STATES = Util.intToStringMaxRadix(1);
@Retention(RetentionPolicy.SOURCE)
@Target(TYPE_USE)
@IntDef({FIELD_AD_PLAYBACK_STATES})
private @interface FieldNumber {}
private static final int FIELD_AD_PLAYBACK_STATES = 1;
@Override @Override
public Bundle toBundle() { public Bundle toBundle() {
...@@ -341,7 +329,7 @@ public final class ImaServerSideAdInsertionMediaSource extends CompositeMediaSou ...@@ -341,7 +329,7 @@ public final class ImaServerSideAdInsertionMediaSource extends CompositeMediaSou
for (Map.Entry<String, AdPlaybackState> entry : adPlaybackStates.entrySet()) { for (Map.Entry<String, AdPlaybackState> entry : adPlaybackStates.entrySet()) {
adPlaybackStatesBundle.putBundle(entry.getKey(), entry.getValue().toBundle()); adPlaybackStatesBundle.putBundle(entry.getKey(), entry.getValue().toBundle());
} }
bundle.putBundle(keyForField(FIELD_AD_PLAYBACK_STATES), adPlaybackStatesBundle); bundle.putBundle(FIELD_AD_PLAYBACK_STATES, adPlaybackStatesBundle);
return bundle; return bundle;
} }
...@@ -352,8 +340,7 @@ public final class ImaServerSideAdInsertionMediaSource extends CompositeMediaSou ...@@ -352,8 +340,7 @@ public final class ImaServerSideAdInsertionMediaSource extends CompositeMediaSou
@Nullable @Nullable
ImmutableMap.Builder<String, AdPlaybackState> adPlaybackStateMap = ImmutableMap.Builder<String, AdPlaybackState> adPlaybackStateMap =
new ImmutableMap.Builder<>(); new ImmutableMap.Builder<>();
Bundle adPlaybackStateBundle = Bundle adPlaybackStateBundle = checkNotNull(bundle.getBundle(FIELD_AD_PLAYBACK_STATES));
checkNotNull(bundle.getBundle(keyForField(FIELD_AD_PLAYBACK_STATES)));
for (String key : adPlaybackStateBundle.keySet()) { for (String key : adPlaybackStateBundle.keySet()) {
AdPlaybackState adPlaybackState = AdPlaybackState adPlaybackState =
AdPlaybackState.CREATOR.fromBundle( AdPlaybackState.CREATOR.fromBundle(
...@@ -363,10 +350,6 @@ public final class ImaServerSideAdInsertionMediaSource extends CompositeMediaSou ...@@ -363,10 +350,6 @@ public final class ImaServerSideAdInsertionMediaSource extends CompositeMediaSou
} }
return new State(adPlaybackStateMap.buildOrThrow()); return new State(adPlaybackStateMap.buildOrThrow());
} }
private static String keyForField(@FieldNumber int field) {
return Integer.toString(field, Character.MAX_RADIX);
}
} }
private final ImaUtil.ServerSideAdInsertionConfiguration configuration; private final ImaUtil.ServerSideAdInsertionConfiguration configuration;
......
...@@ -20,6 +20,7 @@ import static java.lang.annotation.ElementType.TYPE_USE; ...@@ -20,6 +20,7 @@ import static java.lang.annotation.ElementType.TYPE_USE;
import android.os.Bundle; import android.os.Bundle;
import androidx.annotation.IntDef; import androidx.annotation.IntDef;
import androidx.annotation.Nullable; import androidx.annotation.Nullable;
import com.google.android.exoplayer2.util.Util;
import java.lang.annotation.Documented; import java.lang.annotation.Documented;
import java.lang.annotation.Retention; import java.lang.annotation.Retention;
import java.lang.annotation.RetentionPolicy; import java.lang.annotation.RetentionPolicy;
...@@ -85,22 +86,16 @@ public final class DeviceInfo implements Bundleable { ...@@ -85,22 +86,16 @@ public final class DeviceInfo implements Bundleable {
// Bundleable implementation. // Bundleable implementation.
@Documented private static final String FIELD_PLAYBACK_TYPE = Util.intToStringMaxRadix(0);
@Retention(RetentionPolicy.SOURCE) private static final String FIELD_MIN_VOLUME = Util.intToStringMaxRadix(1);
@Target(TYPE_USE) private static final String FIELD_MAX_VOLUME = Util.intToStringMaxRadix(2);
@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 @Override
public Bundle toBundle() { public Bundle toBundle() {
Bundle bundle = new Bundle(); Bundle bundle = new Bundle();
bundle.putInt(keyForField(FIELD_PLAYBACK_TYPE), playbackType); bundle.putInt(FIELD_PLAYBACK_TYPE, playbackType);
bundle.putInt(keyForField(FIELD_MIN_VOLUME), minVolume); bundle.putInt(FIELD_MIN_VOLUME, minVolume);
bundle.putInt(keyForField(FIELD_MAX_VOLUME), maxVolume); bundle.putInt(FIELD_MAX_VOLUME, maxVolume);
return bundle; return bundle;
} }
...@@ -108,14 +103,9 @@ public final class DeviceInfo implements Bundleable { ...@@ -108,14 +103,9 @@ public final class DeviceInfo implements Bundleable {
public static final Creator<DeviceInfo> CREATOR = public static final Creator<DeviceInfo> CREATOR =
bundle -> { bundle -> {
int playbackType = int playbackType =
bundle.getInt( bundle.getInt(FIELD_PLAYBACK_TYPE, /* defaultValue= */ PLAYBACK_TYPE_LOCAL);
keyForField(FIELD_PLAYBACK_TYPE), /* defaultValue= */ PLAYBACK_TYPE_LOCAL); int minVolume = bundle.getInt(FIELD_MIN_VOLUME, /* defaultValue= */ 0);
int minVolume = bundle.getInt(keyForField(FIELD_MIN_VOLUME), /* defaultValue= */ 0); int maxVolume = bundle.getInt(FIELD_MAX_VOLUME, /* defaultValue= */ 0);
int maxVolume = bundle.getInt(keyForField(FIELD_MAX_VOLUME), /* defaultValue= */ 0);
return new DeviceInfo(playbackType, minVolume, maxVolume); return new DeviceInfo(playbackType, minVolume, maxVolume);
}; };
private static String keyForField(@FieldNumber int field) {
return Integer.toString(field, Character.MAX_RADIX);
}
} }
...@@ -16,16 +16,11 @@ ...@@ -16,16 +16,11 @@
package com.google.android.exoplayer2; package com.google.android.exoplayer2;
import static com.google.android.exoplayer2.util.Assertions.checkArgument; import static com.google.android.exoplayer2.util.Assertions.checkArgument;
import static java.lang.annotation.ElementType.TYPE_USE;
import android.os.Bundle; import android.os.Bundle;
import androidx.annotation.IntDef;
import androidx.annotation.Nullable; import androidx.annotation.Nullable;
import com.google.android.exoplayer2.util.Util;
import com.google.common.base.Objects; import com.google.common.base.Objects;
import java.lang.annotation.Documented;
import java.lang.annotation.Retention;
import java.lang.annotation.RetentionPolicy;
import java.lang.annotation.Target;
/** /**
* A rating expressed as "heart" or "no heart". It can be used to indicate whether the content is a * A rating expressed as "heart" or "no heart". It can be used to indicate whether the content is a
...@@ -80,21 +75,15 @@ public final class HeartRating extends Rating { ...@@ -80,21 +75,15 @@ public final class HeartRating extends Rating {
private static final @RatingType int TYPE = RATING_TYPE_HEART; private static final @RatingType int TYPE = RATING_TYPE_HEART;
@Documented private static final String FIELD_RATED = Util.intToStringMaxRadix(1);
@Retention(RetentionPolicy.SOURCE) private static final String FIELD_IS_HEART = Util.intToStringMaxRadix(2);
@Target(TYPE_USE)
@IntDef({FIELD_RATING_TYPE, FIELD_RATED, FIELD_IS_HEART})
private @interface FieldNumber {}
private static final int FIELD_RATED = 1;
private static final int FIELD_IS_HEART = 2;
@Override @Override
public Bundle toBundle() { public Bundle toBundle() {
Bundle bundle = new Bundle(); Bundle bundle = new Bundle();
bundle.putInt(keyForField(FIELD_RATING_TYPE), TYPE); bundle.putInt(FIELD_RATING_TYPE, TYPE);
bundle.putBoolean(keyForField(FIELD_RATED), rated); bundle.putBoolean(FIELD_RATED, rated);
bundle.putBoolean(keyForField(FIELD_IS_HEART), isHeart); bundle.putBoolean(FIELD_IS_HEART, isHeart);
return bundle; return bundle;
} }
...@@ -102,16 +91,10 @@ public final class HeartRating extends Rating { ...@@ -102,16 +91,10 @@ public final class HeartRating extends Rating {
public static final Creator<HeartRating> CREATOR = HeartRating::fromBundle; public static final Creator<HeartRating> CREATOR = HeartRating::fromBundle;
private static HeartRating fromBundle(Bundle bundle) { private static HeartRating fromBundle(Bundle bundle) {
checkArgument( checkArgument(bundle.getInt(FIELD_RATING_TYPE, /* defaultValue= */ RATING_TYPE_UNSET) == TYPE);
bundle.getInt(keyForField(FIELD_RATING_TYPE), /* defaultValue= */ RATING_TYPE_UNSET) boolean isRated = bundle.getBoolean(FIELD_RATED, /* defaultValue= */ false);
== TYPE);
boolean isRated = bundle.getBoolean(keyForField(FIELD_RATED), /* defaultValue= */ false);
return isRated return isRated
? new HeartRating(bundle.getBoolean(keyForField(FIELD_IS_HEART), /* defaultValue= */ false)) ? new HeartRating(bundle.getBoolean(FIELD_IS_HEART, /* defaultValue= */ false))
: new HeartRating(); : new HeartRating();
} }
private static String keyForField(@FieldNumber int field) {
return Integer.toString(field, Character.MAX_RADIX);
}
} }
...@@ -16,17 +16,12 @@ ...@@ -16,17 +16,12 @@
package com.google.android.exoplayer2; package com.google.android.exoplayer2;
import static com.google.android.exoplayer2.util.Assertions.checkArgument; import static com.google.android.exoplayer2.util.Assertions.checkArgument;
import static java.lang.annotation.ElementType.TYPE_USE;
import android.os.Bundle; import android.os.Bundle;
import androidx.annotation.FloatRange; import androidx.annotation.FloatRange;
import androidx.annotation.IntDef;
import androidx.annotation.Nullable; import androidx.annotation.Nullable;
import com.google.android.exoplayer2.util.Util;
import com.google.common.base.Objects; import com.google.common.base.Objects;
import java.lang.annotation.Documented;
import java.lang.annotation.Retention;
import java.lang.annotation.RetentionPolicy;
import java.lang.annotation.Target;
/** A rating expressed as a percentage. */ /** A rating expressed as a percentage. */
public final class PercentageRating extends Rating { public final class PercentageRating extends Rating {
...@@ -78,19 +73,13 @@ public final class PercentageRating extends Rating { ...@@ -78,19 +73,13 @@ public final class PercentageRating extends Rating {
private static final @RatingType int TYPE = RATING_TYPE_PERCENTAGE; private static final @RatingType int TYPE = RATING_TYPE_PERCENTAGE;
@Documented private static final String FIELD_PERCENT = Util.intToStringMaxRadix(1);
@Retention(RetentionPolicy.SOURCE)
@Target(TYPE_USE)
@IntDef({FIELD_RATING_TYPE, FIELD_PERCENT})
private @interface FieldNumber {}
private static final int FIELD_PERCENT = 1;
@Override @Override
public Bundle toBundle() { public Bundle toBundle() {
Bundle bundle = new Bundle(); Bundle bundle = new Bundle();
bundle.putInt(keyForField(FIELD_RATING_TYPE), TYPE); bundle.putInt(FIELD_RATING_TYPE, TYPE);
bundle.putFloat(keyForField(FIELD_PERCENT), percent); bundle.putFloat(FIELD_PERCENT, percent);
return bundle; return bundle;
} }
...@@ -98,14 +87,8 @@ public final class PercentageRating extends Rating { ...@@ -98,14 +87,8 @@ public final class PercentageRating extends Rating {
public static final Creator<PercentageRating> CREATOR = PercentageRating::fromBundle; public static final Creator<PercentageRating> CREATOR = PercentageRating::fromBundle;
private static PercentageRating fromBundle(Bundle bundle) { private static PercentageRating fromBundle(Bundle bundle) {
checkArgument( checkArgument(bundle.getInt(FIELD_RATING_TYPE, /* defaultValue= */ RATING_TYPE_UNSET) == TYPE);
bundle.getInt(keyForField(FIELD_RATING_TYPE), /* defaultValue= */ RATING_TYPE_UNSET) float percent = bundle.getFloat(FIELD_PERCENT, /* defaultValue= */ RATING_UNSET);
== TYPE);
float percent = bundle.getFloat(keyForField(FIELD_PERCENT), /* defaultValue= */ RATING_UNSET);
return percent == RATING_UNSET ? new PercentageRating() : new PercentageRating(percent); return percent == RATING_UNSET ? new PercentageRating() : new PercentageRating(percent);
} }
private static String keyForField(@FieldNumber int field) {
return Integer.toString(field, Character.MAX_RADIX);
}
} }
...@@ -343,13 +343,12 @@ public class PlaybackException extends Exception implements Bundleable { ...@@ -343,13 +343,12 @@ public class PlaybackException extends Exception implements Bundleable {
/** Creates a new instance using the fields obtained from the given {@link Bundle}. */ /** Creates a new instance using the fields obtained from the given {@link Bundle}. */
protected PlaybackException(Bundle bundle) { protected PlaybackException(Bundle bundle) {
this( this(
/* message= */ bundle.getString(keyForField(FIELD_STRING_MESSAGE)), /* message= */ bundle.getString(FIELD_STRING_MESSAGE),
/* cause= */ getCauseFromBundle(bundle), /* cause= */ getCauseFromBundle(bundle),
/* errorCode= */ bundle.getInt( /* errorCode= */ bundle.getInt(
keyForField(FIELD_INT_ERROR_CODE), /* defaultValue= */ ERROR_CODE_UNSPECIFIED), FIELD_INT_ERROR_CODE, /* defaultValue= */ ERROR_CODE_UNSPECIFIED),
/* timestampMs= */ bundle.getLong( /* timestampMs= */ bundle.getLong(
keyForField(FIELD_LONG_TIMESTAMP_MS), FIELD_LONG_TIMESTAMP_MS, /* defaultValue= */ SystemClock.elapsedRealtime()));
/* defaultValue= */ SystemClock.elapsedRealtime()));
} }
/** Creates a new instance using the given values. */ /** Creates a new instance using the given values. */
...@@ -397,18 +396,18 @@ public class PlaybackException extends Exception implements Bundleable { ...@@ -397,18 +396,18 @@ public class PlaybackException extends Exception implements Bundleable {
// Bundleable implementation. // Bundleable implementation.
private static final int FIELD_INT_ERROR_CODE = 0; private static final String FIELD_INT_ERROR_CODE = Util.intToStringMaxRadix(0);
private static final int FIELD_LONG_TIMESTAMP_MS = 1; private static final String FIELD_LONG_TIMESTAMP_MS = Util.intToStringMaxRadix(1);
private static final int FIELD_STRING_MESSAGE = 2; private static final String FIELD_STRING_MESSAGE = Util.intToStringMaxRadix(2);
private static final int FIELD_STRING_CAUSE_CLASS_NAME = 3; private static final String FIELD_STRING_CAUSE_CLASS_NAME = Util.intToStringMaxRadix(3);
private static final int FIELD_STRING_CAUSE_MESSAGE = 4; private static final String FIELD_STRING_CAUSE_MESSAGE = Util.intToStringMaxRadix(4);
/** /**
* Defines a minimum field ID value for subclasses to use when implementing {@link #toBundle()} * Defines a minimum field ID value for subclasses to use when implementing {@link #toBundle()}
* and {@link Bundleable.Creator}. * and {@link Bundleable.Creator}.
* *
* <p>Subclasses should obtain their {@link Bundle Bundle's} field keys by applying a non-negative * <p>Subclasses should obtain their {@link Bundle Bundle's} field keys by applying a non-negative
* offset on this constant and passing the result to {@link #keyForField(int)}. * offset on this constant and passing the result to {@link Util#intToStringMaxRadix(int)}.
*/ */
protected static final int FIELD_CUSTOM_ID_BASE = 1000; protected static final int FIELD_CUSTOM_ID_BASE = 1000;
...@@ -419,28 +418,17 @@ public class PlaybackException extends Exception implements Bundleable { ...@@ -419,28 +418,17 @@ public class PlaybackException extends Exception implements Bundleable {
@Override @Override
public Bundle toBundle() { public Bundle toBundle() {
Bundle bundle = new Bundle(); Bundle bundle = new Bundle();
bundle.putInt(keyForField(FIELD_INT_ERROR_CODE), errorCode); bundle.putInt(FIELD_INT_ERROR_CODE, errorCode);
bundle.putLong(keyForField(FIELD_LONG_TIMESTAMP_MS), timestampMs); bundle.putLong(FIELD_LONG_TIMESTAMP_MS, timestampMs);
bundle.putString(keyForField(FIELD_STRING_MESSAGE), getMessage()); bundle.putString(FIELD_STRING_MESSAGE, getMessage());
@Nullable Throwable cause = getCause(); @Nullable Throwable cause = getCause();
if (cause != null) { if (cause != null) {
bundle.putString(keyForField(FIELD_STRING_CAUSE_CLASS_NAME), cause.getClass().getName()); bundle.putString(FIELD_STRING_CAUSE_CLASS_NAME, cause.getClass().getName());
bundle.putString(keyForField(FIELD_STRING_CAUSE_MESSAGE), cause.getMessage()); bundle.putString(FIELD_STRING_CAUSE_MESSAGE, cause.getMessage());
} }
return bundle; return bundle;
} }
/**
* Converts the given field number to a string which can be used as a field key when implementing
* {@link #toBundle()} and {@link Bundleable.Creator}.
*
* <p>Subclasses should use {@code field} values greater than or equal to {@link
* #FIELD_CUSTOM_ID_BASE}.
*/
protected static String keyForField(int field) {
return Integer.toString(field, Character.MAX_RADIX);
}
// Creates a new {@link Throwable} with possibly {@code null} message. // Creates a new {@link Throwable} with possibly {@code null} message.
@SuppressWarnings("nullness:argument") @SuppressWarnings("nullness:argument")
private static Throwable createThrowable(Class<?> clazz, @Nullable String message) private static Throwable createThrowable(Class<?> clazz, @Nullable String message)
...@@ -456,8 +444,8 @@ public class PlaybackException extends Exception implements Bundleable { ...@@ -456,8 +444,8 @@ public class PlaybackException extends Exception implements Bundleable {
@Nullable @Nullable
private static Throwable getCauseFromBundle(Bundle bundle) { private static Throwable getCauseFromBundle(Bundle bundle) {
@Nullable String causeClassName = bundle.getString(keyForField(FIELD_STRING_CAUSE_CLASS_NAME)); @Nullable String causeClassName = bundle.getString(FIELD_STRING_CAUSE_CLASS_NAME);
@Nullable String causeMessage = bundle.getString(keyForField(FIELD_STRING_CAUSE_MESSAGE)); @Nullable String causeMessage = bundle.getString(FIELD_STRING_CAUSE_MESSAGE);
@Nullable Throwable cause = null; @Nullable Throwable cause = null;
if (!TextUtils.isEmpty(causeClassName)) { if (!TextUtils.isEmpty(causeClassName)) {
try { try {
......
...@@ -15,19 +15,12 @@ ...@@ -15,19 +15,12 @@
*/ */
package com.google.android.exoplayer2; package com.google.android.exoplayer2;
import static java.lang.annotation.ElementType.TYPE_USE;
import android.os.Bundle; import android.os.Bundle;
import androidx.annotation.CheckResult; import androidx.annotation.CheckResult;
import androidx.annotation.FloatRange; import androidx.annotation.FloatRange;
import androidx.annotation.IntDef;
import androidx.annotation.Nullable; import androidx.annotation.Nullable;
import com.google.android.exoplayer2.util.Assertions; import com.google.android.exoplayer2.util.Assertions;
import com.google.android.exoplayer2.util.Util; import com.google.android.exoplayer2.util.Util;
import java.lang.annotation.Documented;
import java.lang.annotation.Retention;
import java.lang.annotation.RetentionPolicy;
import java.lang.annotation.Target;
/** Parameters that apply to playback, including speed setting. */ /** Parameters that apply to playback, including speed setting. */
public final class PlaybackParameters implements Bundleable { public final class PlaybackParameters implements Bundleable {
...@@ -120,32 +113,22 @@ public final class PlaybackParameters implements Bundleable { ...@@ -120,32 +113,22 @@ public final class PlaybackParameters implements Bundleable {
// Bundleable implementation. // Bundleable implementation.
@Documented private static final String FIELD_SPEED = Util.intToStringMaxRadix(0);
@Retention(RetentionPolicy.SOURCE) private static final String FIELD_PITCH = Util.intToStringMaxRadix(1);
@Target(TYPE_USE)
@IntDef({FIELD_SPEED, FIELD_PITCH})
private @interface FieldNumber {}
private static final int FIELD_SPEED = 0;
private static final int FIELD_PITCH = 1;
@Override @Override
public Bundle toBundle() { public Bundle toBundle() {
Bundle bundle = new Bundle(); Bundle bundle = new Bundle();
bundle.putFloat(keyForField(FIELD_SPEED), speed); bundle.putFloat(FIELD_SPEED, speed);
bundle.putFloat(keyForField(FIELD_PITCH), pitch); bundle.putFloat(FIELD_PITCH, pitch);
return bundle; return bundle;
} }
/** Object that can restore {@link PlaybackParameters} from a {@link Bundle}. */ /** Object that can restore {@link PlaybackParameters} from a {@link Bundle}. */
public static final Creator<PlaybackParameters> CREATOR = public static final Creator<PlaybackParameters> CREATOR =
bundle -> { bundle -> {
float speed = bundle.getFloat(keyForField(FIELD_SPEED), /* defaultValue= */ 1f); float speed = bundle.getFloat(FIELD_SPEED, /* defaultValue= */ 1f);
float pitch = bundle.getFloat(keyForField(FIELD_PITCH), /* defaultValue= */ 1f); float pitch = bundle.getFloat(FIELD_PITCH, /* defaultValue= */ 1f);
return new PlaybackParameters(speed, pitch); return new PlaybackParameters(speed, pitch);
}; };
private static String keyForField(@FieldNumber int field) {
return Integer.toString(field, Character.MAX_RADIX);
}
} }
...@@ -269,27 +269,14 @@ public interface Player { ...@@ -269,27 +269,14 @@ public interface Player {
} }
// Bundleable implementation. // Bundleable implementation.
@Documented
@Retention(RetentionPolicy.SOURCE)
@Target(TYPE_USE)
@IntDef({
FIELD_MEDIA_ITEM_INDEX,
FIELD_MEDIA_ITEM,
FIELD_PERIOD_INDEX,
FIELD_POSITION_MS,
FIELD_CONTENT_POSITION_MS,
FIELD_AD_GROUP_INDEX,
FIELD_AD_INDEX_IN_AD_GROUP
})
private @interface FieldNumber {}
private static final int FIELD_MEDIA_ITEM_INDEX = 0; private static final String FIELD_MEDIA_ITEM_INDEX = Util.intToStringMaxRadix(0);
private static final int FIELD_MEDIA_ITEM = 1; private static final String FIELD_MEDIA_ITEM = Util.intToStringMaxRadix(1);
private static final int FIELD_PERIOD_INDEX = 2; private static final String FIELD_PERIOD_INDEX = Util.intToStringMaxRadix(2);
private static final int FIELD_POSITION_MS = 3; private static final String FIELD_POSITION_MS = Util.intToStringMaxRadix(3);
private static final int FIELD_CONTENT_POSITION_MS = 4; private static final String FIELD_CONTENT_POSITION_MS = Util.intToStringMaxRadix(4);
private static final int FIELD_AD_GROUP_INDEX = 5; private static final String FIELD_AD_GROUP_INDEX = Util.intToStringMaxRadix(5);
private static final int FIELD_AD_INDEX_IN_AD_GROUP = 6; private static final String FIELD_AD_INDEX_IN_AD_GROUP = Util.intToStringMaxRadix(6);
/** /**
* {@inheritDoc} * {@inheritDoc}
...@@ -300,15 +287,15 @@ public interface Player { ...@@ -300,15 +287,15 @@ public interface Player {
@Override @Override
public Bundle toBundle() { public Bundle toBundle() {
Bundle bundle = new Bundle(); Bundle bundle = new Bundle();
bundle.putInt(keyForField(FIELD_MEDIA_ITEM_INDEX), mediaItemIndex); bundle.putInt(FIELD_MEDIA_ITEM_INDEX, mediaItemIndex);
if (mediaItem != null) { if (mediaItem != null) {
bundle.putBundle(keyForField(FIELD_MEDIA_ITEM), mediaItem.toBundle()); bundle.putBundle(FIELD_MEDIA_ITEM, mediaItem.toBundle());
} }
bundle.putInt(keyForField(FIELD_PERIOD_INDEX), periodIndex); bundle.putInt(FIELD_PERIOD_INDEX, periodIndex);
bundle.putLong(keyForField(FIELD_POSITION_MS), positionMs); bundle.putLong(FIELD_POSITION_MS, positionMs);
bundle.putLong(keyForField(FIELD_CONTENT_POSITION_MS), contentPositionMs); bundle.putLong(FIELD_CONTENT_POSITION_MS, contentPositionMs);
bundle.putInt(keyForField(FIELD_AD_GROUP_INDEX), adGroupIndex); bundle.putInt(FIELD_AD_GROUP_INDEX, adGroupIndex);
bundle.putInt(keyForField(FIELD_AD_INDEX_IN_AD_GROUP), adIndexInAdGroup); bundle.putInt(FIELD_AD_INDEX_IN_AD_GROUP, adIndexInAdGroup);
return bundle; return bundle;
} }
...@@ -316,22 +303,18 @@ public interface Player { ...@@ -316,22 +303,18 @@ public interface Player {
public static final Creator<PositionInfo> CREATOR = PositionInfo::fromBundle; public static final Creator<PositionInfo> CREATOR = PositionInfo::fromBundle;
private static PositionInfo fromBundle(Bundle bundle) { private static PositionInfo fromBundle(Bundle bundle) {
int mediaItemIndex = int mediaItemIndex = bundle.getInt(FIELD_MEDIA_ITEM_INDEX, /* defaultValue= */ C.INDEX_UNSET);
bundle.getInt(keyForField(FIELD_MEDIA_ITEM_INDEX), /* defaultValue= */ C.INDEX_UNSET); @Nullable Bundle mediaItemBundle = bundle.getBundle(FIELD_MEDIA_ITEM);
@Nullable Bundle mediaItemBundle = bundle.getBundle(keyForField(FIELD_MEDIA_ITEM));
@Nullable @Nullable
MediaItem mediaItem = MediaItem mediaItem =
mediaItemBundle == null ? null : MediaItem.CREATOR.fromBundle(mediaItemBundle); mediaItemBundle == null ? null : MediaItem.CREATOR.fromBundle(mediaItemBundle);
int periodIndex = int periodIndex = bundle.getInt(FIELD_PERIOD_INDEX, /* defaultValue= */ C.INDEX_UNSET);
bundle.getInt(keyForField(FIELD_PERIOD_INDEX), /* defaultValue= */ C.INDEX_UNSET); long positionMs = bundle.getLong(FIELD_POSITION_MS, /* defaultValue= */ C.TIME_UNSET);
long positionMs =
bundle.getLong(keyForField(FIELD_POSITION_MS), /* defaultValue= */ C.TIME_UNSET);
long contentPositionMs = long contentPositionMs =
bundle.getLong(keyForField(FIELD_CONTENT_POSITION_MS), /* defaultValue= */ C.TIME_UNSET); bundle.getLong(FIELD_CONTENT_POSITION_MS, /* defaultValue= */ C.TIME_UNSET);
int adGroupIndex = int adGroupIndex = bundle.getInt(FIELD_AD_GROUP_INDEX, /* defaultValue= */ C.INDEX_UNSET);
bundle.getInt(keyForField(FIELD_AD_GROUP_INDEX), /* defaultValue= */ C.INDEX_UNSET);
int adIndexInAdGroup = int adIndexInAdGroup =
bundle.getInt(keyForField(FIELD_AD_INDEX_IN_AD_GROUP), /* defaultValue= */ C.INDEX_UNSET); bundle.getInt(FIELD_AD_INDEX_IN_AD_GROUP, /* defaultValue= */ C.INDEX_UNSET);
return new PositionInfo( return new PositionInfo(
/* windowUid= */ null, /* windowUid= */ null,
mediaItemIndex, mediaItemIndex,
...@@ -343,10 +326,6 @@ public interface Player { ...@@ -343,10 +326,6 @@ public interface Player {
adGroupIndex, adGroupIndex,
adIndexInAdGroup); adIndexInAdGroup);
} }
private static String keyForField(@FieldNumber int field) {
return Integer.toString(field, Character.MAX_RADIX);
}
} }
/** /**
...@@ -579,13 +558,7 @@ public interface Player { ...@@ -579,13 +558,7 @@ public interface Player {
// Bundleable implementation. // Bundleable implementation.
@Documented private static final String FIELD_COMMANDS = Util.intToStringMaxRadix(0);
@Retention(RetentionPolicy.SOURCE)
@Target(TYPE_USE)
@IntDef({FIELD_COMMANDS})
private @interface FieldNumber {}
private static final int FIELD_COMMANDS = 0;
@Override @Override
public Bundle toBundle() { public Bundle toBundle() {
...@@ -594,7 +567,7 @@ public interface Player { ...@@ -594,7 +567,7 @@ public interface Player {
for (int i = 0; i < flags.size(); i++) { for (int i = 0; i < flags.size(); i++) {
commandsBundle.add(flags.get(i)); commandsBundle.add(flags.get(i));
} }
bundle.putIntegerArrayList(keyForField(FIELD_COMMANDS), commandsBundle); bundle.putIntegerArrayList(FIELD_COMMANDS, commandsBundle);
return bundle; return bundle;
} }
...@@ -602,8 +575,7 @@ public interface Player { ...@@ -602,8 +575,7 @@ public interface Player {
public static final Creator<Commands> CREATOR = Commands::fromBundle; public static final Creator<Commands> CREATOR = Commands::fromBundle;
private static Commands fromBundle(Bundle bundle) { private static Commands fromBundle(Bundle bundle) {
@Nullable @Nullable ArrayList<Integer> commands = bundle.getIntegerArrayList(FIELD_COMMANDS);
ArrayList<Integer> commands = bundle.getIntegerArrayList(keyForField(FIELD_COMMANDS));
if (commands == null) { if (commands == null) {
return Commands.EMPTY; return Commands.EMPTY;
} }
...@@ -613,10 +585,6 @@ public interface Player { ...@@ -613,10 +585,6 @@ public interface Player {
} }
return builder.build(); return builder.build();
} }
private static String keyForField(@FieldNumber int field) {
return Integer.toString(field, Character.MAX_RADIX);
}
} }
/** /**
......
...@@ -19,6 +19,7 @@ import static java.lang.annotation.ElementType.TYPE_USE; ...@@ -19,6 +19,7 @@ import static java.lang.annotation.ElementType.TYPE_USE;
import android.os.Bundle; import android.os.Bundle;
import androidx.annotation.IntDef; import androidx.annotation.IntDef;
import com.google.android.exoplayer2.util.Util;
import java.lang.annotation.Documented; import java.lang.annotation.Documented;
import java.lang.annotation.Retention; import java.lang.annotation.Retention;
import java.lang.annotation.RetentionPolicy; import java.lang.annotation.RetentionPolicy;
...@@ -59,21 +60,14 @@ public abstract class Rating implements Bundleable { ...@@ -59,21 +60,14 @@ public abstract class Rating implements Bundleable {
/* package */ static final int RATING_TYPE_STAR = 2; /* package */ static final int RATING_TYPE_STAR = 2;
/* package */ static final int RATING_TYPE_THUMB = 3; /* package */ static final int RATING_TYPE_THUMB = 3;
@Documented /* package */ static final String FIELD_RATING_TYPE = Util.intToStringMaxRadix(0);
@Retention(RetentionPolicy.SOURCE)
@Target(TYPE_USE)
@IntDef({FIELD_RATING_TYPE})
private @interface FieldNumber {}
/* package */ static final int FIELD_RATING_TYPE = 0;
/** Object that can restore a {@link Rating} from a {@link Bundle}. */ /** Object that can restore a {@link Rating} from a {@link Bundle}. */
public static final Creator<Rating> CREATOR = Rating::fromBundle; public static final Creator<Rating> CREATOR = Rating::fromBundle;
private static Rating fromBundle(Bundle bundle) { private static Rating fromBundle(Bundle bundle) {
@RatingType @RatingType
int ratingType = int ratingType = bundle.getInt(FIELD_RATING_TYPE, /* defaultValue= */ RATING_TYPE_UNSET);
bundle.getInt(keyForField(FIELD_RATING_TYPE), /* defaultValue= */ RATING_TYPE_UNSET);
switch (ratingType) { switch (ratingType) {
case RATING_TYPE_HEART: case RATING_TYPE_HEART:
return HeartRating.CREATOR.fromBundle(bundle); return HeartRating.CREATOR.fromBundle(bundle);
...@@ -88,8 +82,4 @@ public abstract class Rating implements Bundleable { ...@@ -88,8 +82,4 @@ public abstract class Rating implements Bundleable {
throw new IllegalArgumentException("Unknown RatingType: " + ratingType); throw new IllegalArgumentException("Unknown RatingType: " + ratingType);
} }
} }
private static String keyForField(@FieldNumber int field) {
return Integer.toString(field, Character.MAX_RADIX);
}
} }
...@@ -16,18 +16,13 @@ ...@@ -16,18 +16,13 @@
package com.google.android.exoplayer2; package com.google.android.exoplayer2;
import static com.google.android.exoplayer2.util.Assertions.checkArgument; import static com.google.android.exoplayer2.util.Assertions.checkArgument;
import static java.lang.annotation.ElementType.TYPE_USE;
import android.os.Bundle; import android.os.Bundle;
import androidx.annotation.FloatRange; import androidx.annotation.FloatRange;
import androidx.annotation.IntDef;
import androidx.annotation.IntRange; import androidx.annotation.IntRange;
import androidx.annotation.Nullable; import androidx.annotation.Nullable;
import com.google.android.exoplayer2.util.Util;
import com.google.common.base.Objects; import com.google.common.base.Objects;
import java.lang.annotation.Documented;
import java.lang.annotation.Retention;
import java.lang.annotation.RetentionPolicy;
import java.lang.annotation.Target;
/** A rating expressed as a fractional number of stars. */ /** A rating expressed as a fractional number of stars. */
public final class StarRating extends Rating { public final class StarRating extends Rating {
...@@ -105,21 +100,15 @@ public final class StarRating extends Rating { ...@@ -105,21 +100,15 @@ public final class StarRating extends Rating {
private static final @RatingType int TYPE = RATING_TYPE_STAR; private static final @RatingType int TYPE = RATING_TYPE_STAR;
private static final int MAX_STARS_DEFAULT = 5; private static final int MAX_STARS_DEFAULT = 5;
@Documented private static final String FIELD_MAX_STARS = Util.intToStringMaxRadix(1);
@Retention(RetentionPolicy.SOURCE) private static final String FIELD_STAR_RATING = Util.intToStringMaxRadix(2);
@Target(TYPE_USE)
@IntDef({FIELD_RATING_TYPE, FIELD_MAX_STARS, FIELD_STAR_RATING})
private @interface FieldNumber {}
private static final int FIELD_MAX_STARS = 1;
private static final int FIELD_STAR_RATING = 2;
@Override @Override
public Bundle toBundle() { public Bundle toBundle() {
Bundle bundle = new Bundle(); Bundle bundle = new Bundle();
bundle.putInt(keyForField(FIELD_RATING_TYPE), TYPE); bundle.putInt(FIELD_RATING_TYPE, TYPE);
bundle.putInt(keyForField(FIELD_MAX_STARS), maxStars); bundle.putInt(FIELD_MAX_STARS, maxStars);
bundle.putFloat(keyForField(FIELD_STAR_RATING), starRating); bundle.putFloat(FIELD_STAR_RATING, starRating);
return bundle; return bundle;
} }
...@@ -127,19 +116,11 @@ public final class StarRating extends Rating { ...@@ -127,19 +116,11 @@ public final class StarRating extends Rating {
public static final Creator<StarRating> CREATOR = StarRating::fromBundle; public static final Creator<StarRating> CREATOR = StarRating::fromBundle;
private static StarRating fromBundle(Bundle bundle) { private static StarRating fromBundle(Bundle bundle) {
checkArgument( checkArgument(bundle.getInt(FIELD_RATING_TYPE, /* defaultValue= */ RATING_TYPE_UNSET) == TYPE);
bundle.getInt(keyForField(FIELD_RATING_TYPE), /* defaultValue= */ RATING_TYPE_UNSET) int maxStars = bundle.getInt(FIELD_MAX_STARS, /* defaultValue= */ MAX_STARS_DEFAULT);
== TYPE); float starRating = bundle.getFloat(FIELD_STAR_RATING, /* defaultValue= */ RATING_UNSET);
int maxStars =
bundle.getInt(keyForField(FIELD_MAX_STARS), /* defaultValue= */ MAX_STARS_DEFAULT);
float starRating =
bundle.getFloat(keyForField(FIELD_STAR_RATING), /* defaultValue= */ RATING_UNSET);
return starRating == RATING_UNSET return starRating == RATING_UNSET
? new StarRating(maxStars) ? new StarRating(maxStars)
: new StarRating(maxStars, starRating); : new StarRating(maxStars, starRating);
} }
private static String keyForField(@FieldNumber int field) {
return Integer.toString(field, Character.MAX_RADIX);
}
} }
...@@ -16,16 +16,11 @@ ...@@ -16,16 +16,11 @@
package com.google.android.exoplayer2; package com.google.android.exoplayer2;
import static com.google.android.exoplayer2.util.Assertions.checkArgument; import static com.google.android.exoplayer2.util.Assertions.checkArgument;
import static java.lang.annotation.ElementType.TYPE_USE;
import android.os.Bundle; import android.os.Bundle;
import androidx.annotation.IntDef;
import androidx.annotation.Nullable; import androidx.annotation.Nullable;
import com.google.android.exoplayer2.util.Util;
import com.google.common.base.Objects; import com.google.common.base.Objects;
import java.lang.annotation.Documented;
import java.lang.annotation.Retention;
import java.lang.annotation.RetentionPolicy;
import java.lang.annotation.Target;
/** A rating expressed as "thumbs up" or "thumbs down". */ /** A rating expressed as "thumbs up" or "thumbs down". */
public final class ThumbRating extends Rating { public final class ThumbRating extends Rating {
...@@ -77,21 +72,15 @@ public final class ThumbRating extends Rating { ...@@ -77,21 +72,15 @@ public final class ThumbRating extends Rating {
private static final @RatingType int TYPE = RATING_TYPE_THUMB; private static final @RatingType int TYPE = RATING_TYPE_THUMB;
@Documented private static final String FIELD_RATED = Util.intToStringMaxRadix(1);
@Retention(RetentionPolicy.SOURCE) private static final String FIELD_IS_THUMBS_UP = Util.intToStringMaxRadix(2);
@Target(TYPE_USE)
@IntDef({FIELD_RATING_TYPE, FIELD_RATED, FIELD_IS_THUMBS_UP})
private @interface FieldNumber {}
private static final int FIELD_RATED = 1;
private static final int FIELD_IS_THUMBS_UP = 2;
@Override @Override
public Bundle toBundle() { public Bundle toBundle() {
Bundle bundle = new Bundle(); Bundle bundle = new Bundle();
bundle.putInt(keyForField(FIELD_RATING_TYPE), TYPE); bundle.putInt(FIELD_RATING_TYPE, TYPE);
bundle.putBoolean(keyForField(FIELD_RATED), rated); bundle.putBoolean(FIELD_RATED, rated);
bundle.putBoolean(keyForField(FIELD_IS_THUMBS_UP), isThumbsUp); bundle.putBoolean(FIELD_IS_THUMBS_UP, isThumbsUp);
return bundle; return bundle;
} }
...@@ -99,17 +88,10 @@ public final class ThumbRating extends Rating { ...@@ -99,17 +88,10 @@ public final class ThumbRating extends Rating {
public static final Creator<ThumbRating> CREATOR = ThumbRating::fromBundle; public static final Creator<ThumbRating> CREATOR = ThumbRating::fromBundle;
private static ThumbRating fromBundle(Bundle bundle) { private static ThumbRating fromBundle(Bundle bundle) {
checkArgument( checkArgument(bundle.getInt(FIELD_RATING_TYPE, /* defaultValue= */ RATING_TYPE_UNSET) == TYPE);
bundle.getInt(keyForField(FIELD_RATING_TYPE), /* defaultValue= */ RATING_TYPE_UNSET) boolean rated = bundle.getBoolean(FIELD_RATED, /* defaultValue= */ false);
== TYPE);
boolean rated = bundle.getBoolean(keyForField(FIELD_RATED), /* defaultValue= */ false);
return rated return rated
? new ThumbRating( ? new ThumbRating(bundle.getBoolean(FIELD_IS_THUMBS_UP, /* defaultValue= */ false))
bundle.getBoolean(keyForField(FIELD_IS_THUMBS_UP), /* defaultValue= */ false))
: new ThumbRating(); : new ThumbRating();
} }
private static String keyForField(@FieldNumber int field) {
return Integer.toString(field, Character.MAX_RADIX);
}
} }
...@@ -18,20 +18,15 @@ package com.google.android.exoplayer2; ...@@ -18,20 +18,15 @@ package com.google.android.exoplayer2;
import static com.google.android.exoplayer2.util.Assertions.checkArgument; import static com.google.android.exoplayer2.util.Assertions.checkArgument;
import static com.google.android.exoplayer2.util.Assertions.checkNotNull; import static com.google.android.exoplayer2.util.Assertions.checkNotNull;
import static com.google.android.exoplayer2.util.BundleableUtil.toBundleArrayList; import static com.google.android.exoplayer2.util.BundleableUtil.toBundleArrayList;
import static java.lang.annotation.ElementType.TYPE_USE;
import android.os.Bundle; import android.os.Bundle;
import androidx.annotation.IntDef;
import androidx.annotation.Nullable; import androidx.annotation.Nullable;
import com.google.android.exoplayer2.source.TrackGroup; import com.google.android.exoplayer2.source.TrackGroup;
import com.google.android.exoplayer2.util.BundleableUtil; import com.google.android.exoplayer2.util.BundleableUtil;
import com.google.android.exoplayer2.util.Util;
import com.google.common.base.MoreObjects; import com.google.common.base.MoreObjects;
import com.google.common.collect.ImmutableList; import com.google.common.collect.ImmutableList;
import com.google.common.primitives.Booleans; import com.google.common.primitives.Booleans;
import java.lang.annotation.Documented;
import java.lang.annotation.Retention;
import java.lang.annotation.RetentionPolicy;
import java.lang.annotation.Target;
import java.util.Arrays; import java.util.Arrays;
import java.util.List; import java.util.List;
...@@ -219,29 +214,19 @@ public final class Tracks implements Bundleable { ...@@ -219,29 +214,19 @@ public final class Tracks implements Bundleable {
} }
// Bundleable implementation. // Bundleable implementation.
@Documented
@Retention(RetentionPolicy.SOURCE) private static final String FIELD_TRACK_GROUP = Util.intToStringMaxRadix(0);
@Target(TYPE_USE) private static final String FIELD_TRACK_SUPPORT = Util.intToStringMaxRadix(1);
@IntDef({ private static final String FIELD_TRACK_SELECTED = Util.intToStringMaxRadix(3);
FIELD_TRACK_GROUP, private static final String FIELD_ADAPTIVE_SUPPORTED = Util.intToStringMaxRadix(4);
FIELD_TRACK_SUPPORT,
FIELD_TRACK_SELECTED,
FIELD_ADAPTIVE_SUPPORTED,
})
private @interface FieldNumber {}
private static final int FIELD_TRACK_GROUP = 0;
private static final int FIELD_TRACK_SUPPORT = 1;
private static final int FIELD_TRACK_SELECTED = 3;
private static final int FIELD_ADAPTIVE_SUPPORTED = 4;
@Override @Override
public Bundle toBundle() { public Bundle toBundle() {
Bundle bundle = new Bundle(); Bundle bundle = new Bundle();
bundle.putBundle(keyForField(FIELD_TRACK_GROUP), mediaTrackGroup.toBundle()); bundle.putBundle(FIELD_TRACK_GROUP, mediaTrackGroup.toBundle());
bundle.putIntArray(keyForField(FIELD_TRACK_SUPPORT), trackSupport); bundle.putIntArray(FIELD_TRACK_SUPPORT, trackSupport);
bundle.putBooleanArray(keyForField(FIELD_TRACK_SELECTED), trackSelected); bundle.putBooleanArray(FIELD_TRACK_SELECTED, trackSelected);
bundle.putBoolean(keyForField(FIELD_ADAPTIVE_SUPPORTED), adaptiveSupported); bundle.putBoolean(FIELD_ADAPTIVE_SUPPORTED, adaptiveSupported);
return bundle; return bundle;
} }
...@@ -250,23 +235,16 @@ public final class Tracks implements Bundleable { ...@@ -250,23 +235,16 @@ public final class Tracks implements Bundleable {
bundle -> { bundle -> {
// Can't create a Tracks.Group without a TrackGroup // Can't create a Tracks.Group without a TrackGroup
TrackGroup trackGroup = TrackGroup trackGroup =
TrackGroup.CREATOR.fromBundle( TrackGroup.CREATOR.fromBundle(checkNotNull(bundle.getBundle(FIELD_TRACK_GROUP)));
checkNotNull(bundle.getBundle(keyForField(FIELD_TRACK_GROUP))));
final @C.FormatSupport int[] trackSupport = final @C.FormatSupport int[] trackSupport =
MoreObjects.firstNonNull( MoreObjects.firstNonNull(
bundle.getIntArray(keyForField(FIELD_TRACK_SUPPORT)), new int[trackGroup.length]); bundle.getIntArray(FIELD_TRACK_SUPPORT), new int[trackGroup.length]);
boolean[] selected = boolean[] selected =
MoreObjects.firstNonNull( MoreObjects.firstNonNull(
bundle.getBooleanArray(keyForField(FIELD_TRACK_SELECTED)), bundle.getBooleanArray(FIELD_TRACK_SELECTED), new boolean[trackGroup.length]);
new boolean[trackGroup.length]); boolean adaptiveSupported = bundle.getBoolean(FIELD_ADAPTIVE_SUPPORTED, false);
boolean adaptiveSupported =
bundle.getBoolean(keyForField(FIELD_ADAPTIVE_SUPPORTED), false);
return new Group(trackGroup, adaptiveSupported, trackSupport, selected); return new Group(trackGroup, adaptiveSupported, trackSupport, selected);
}; };
private static String keyForField(@FieldNumber int field) {
return Integer.toString(field, Character.MAX_RADIX);
}
} }
/** Empty tracks. */ /** Empty tracks. */
...@@ -379,36 +357,23 @@ public final class Tracks implements Bundleable { ...@@ -379,36 +357,23 @@ public final class Tracks implements Bundleable {
} }
// Bundleable implementation. // Bundleable implementation.
@Documented private static final String FIELD_TRACK_GROUPS = Util.intToStringMaxRadix(0);
@Retention(RetentionPolicy.SOURCE)
@Target(TYPE_USE)
@IntDef({
FIELD_TRACK_GROUPS,
})
private @interface FieldNumber {}
private static final int FIELD_TRACK_GROUPS = 0;
@Override @Override
public Bundle toBundle() { public Bundle toBundle() {
Bundle bundle = new Bundle(); Bundle bundle = new Bundle();
bundle.putParcelableArrayList(keyForField(FIELD_TRACK_GROUPS), toBundleArrayList(groups)); bundle.putParcelableArrayList(FIELD_TRACK_GROUPS, toBundleArrayList(groups));
return bundle; return bundle;
} }
/** Object that can restore tracks from a {@link Bundle}. */ /** Object that can restore tracks from a {@link Bundle}. */
public static final Creator<Tracks> CREATOR = public static final Creator<Tracks> CREATOR =
bundle -> { bundle -> {
@Nullable @Nullable List<Bundle> groupBundles = bundle.getParcelableArrayList(FIELD_TRACK_GROUPS);
List<Bundle> groupBundles = bundle.getParcelableArrayList(keyForField(FIELD_TRACK_GROUPS));
List<Group> groups = List<Group> groups =
groupBundles == null groupBundles == null
? ImmutableList.of() ? ImmutableList.of()
: BundleableUtil.fromBundleList(Group.CREATOR, groupBundles); : BundleableUtil.fromBundleList(Group.CREATOR, groupBundles);
return new Tracks(groups); return new Tracks(groups);
}; };
private static String keyForField(@FieldNumber int field) {
return Integer.toString(field, Character.MAX_RADIX);
}
} }
...@@ -15,21 +15,14 @@ ...@@ -15,21 +15,14 @@
*/ */
package com.google.android.exoplayer2.audio; package com.google.android.exoplayer2.audio;
import static java.lang.annotation.ElementType.TYPE_USE;
import android.os.Bundle; import android.os.Bundle;
import androidx.annotation.DoNotInline; import androidx.annotation.DoNotInline;
import androidx.annotation.IntDef;
import androidx.annotation.Nullable; import androidx.annotation.Nullable;
import androidx.annotation.RequiresApi; import androidx.annotation.RequiresApi;
import com.google.android.exoplayer2.Bundleable; import com.google.android.exoplayer2.Bundleable;
import com.google.android.exoplayer2.C; import com.google.android.exoplayer2.C;
import com.google.android.exoplayer2.util.Util; import com.google.android.exoplayer2.util.Util;
import com.google.errorprone.annotations.CanIgnoreReturnValue; import com.google.errorprone.annotations.CanIgnoreReturnValue;
import java.lang.annotation.Documented;
import java.lang.annotation.Retention;
import java.lang.annotation.RetentionPolicy;
import java.lang.annotation.Target;
/** /**
* Attributes for audio playback, which configure the underlying platform {@link * Attributes for audio playback, which configure the underlying platform {@link
...@@ -206,32 +199,20 @@ public final class AudioAttributes implements Bundleable { ...@@ -206,32 +199,20 @@ public final class AudioAttributes implements Bundleable {
// Bundleable implementation. // Bundleable implementation.
@Documented private static final String FIELD_CONTENT_TYPE = Util.intToStringMaxRadix(0);
@Retention(RetentionPolicy.SOURCE) private static final String FIELD_FLAGS = Util.intToStringMaxRadix(1);
@Target(TYPE_USE) private static final String FIELD_USAGE = Util.intToStringMaxRadix(2);
@IntDef({ private static final String FIELD_ALLOWED_CAPTURE_POLICY = Util.intToStringMaxRadix(3);
FIELD_CONTENT_TYPE, private static final String FIELD_SPATIALIZATION_BEHAVIOR = Util.intToStringMaxRadix(4);
FIELD_FLAGS,
FIELD_USAGE,
FIELD_ALLOWED_CAPTURE_POLICY,
FIELD_SPATIALIZATION_BEHAVIOR
})
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;
private static final int FIELD_SPATIALIZATION_BEHAVIOR = 4;
@Override @Override
public Bundle toBundle() { public Bundle toBundle() {
Bundle bundle = new Bundle(); Bundle bundle = new Bundle();
bundle.putInt(keyForField(FIELD_CONTENT_TYPE), contentType); bundle.putInt(FIELD_CONTENT_TYPE, contentType);
bundle.putInt(keyForField(FIELD_FLAGS), flags); bundle.putInt(FIELD_FLAGS, flags);
bundle.putInt(keyForField(FIELD_USAGE), usage); bundle.putInt(FIELD_USAGE, usage);
bundle.putInt(keyForField(FIELD_ALLOWED_CAPTURE_POLICY), allowedCapturePolicy); bundle.putInt(FIELD_ALLOWED_CAPTURE_POLICY, allowedCapturePolicy);
bundle.putInt(keyForField(FIELD_SPATIALIZATION_BEHAVIOR), spatializationBehavior); bundle.putInt(FIELD_SPATIALIZATION_BEHAVIOR, spatializationBehavior);
return bundle; return bundle;
} }
...@@ -239,29 +220,24 @@ public final class AudioAttributes implements Bundleable { ...@@ -239,29 +220,24 @@ public final class AudioAttributes implements Bundleable {
public static final Creator<AudioAttributes> CREATOR = public static final Creator<AudioAttributes> CREATOR =
bundle -> { bundle -> {
Builder builder = new Builder(); Builder builder = new Builder();
if (bundle.containsKey(keyForField(FIELD_CONTENT_TYPE))) { if (bundle.containsKey(FIELD_CONTENT_TYPE)) {
builder.setContentType(bundle.getInt(keyForField(FIELD_CONTENT_TYPE))); builder.setContentType(bundle.getInt(FIELD_CONTENT_TYPE));
} }
if (bundle.containsKey(keyForField(FIELD_FLAGS))) { if (bundle.containsKey(FIELD_FLAGS)) {
builder.setFlags(bundle.getInt(keyForField(FIELD_FLAGS))); builder.setFlags(bundle.getInt(FIELD_FLAGS));
} }
if (bundle.containsKey(keyForField(FIELD_USAGE))) { if (bundle.containsKey(FIELD_USAGE)) {
builder.setUsage(bundle.getInt(keyForField(FIELD_USAGE))); builder.setUsage(bundle.getInt(FIELD_USAGE));
} }
if (bundle.containsKey(keyForField(FIELD_ALLOWED_CAPTURE_POLICY))) { if (bundle.containsKey(FIELD_ALLOWED_CAPTURE_POLICY)) {
builder.setAllowedCapturePolicy(bundle.getInt(keyForField(FIELD_ALLOWED_CAPTURE_POLICY))); builder.setAllowedCapturePolicy(bundle.getInt(FIELD_ALLOWED_CAPTURE_POLICY));
} }
if (bundle.containsKey(keyForField(FIELD_SPATIALIZATION_BEHAVIOR))) { if (bundle.containsKey(FIELD_SPATIALIZATION_BEHAVIOR)) {
builder.setSpatializationBehavior( builder.setSpatializationBehavior(bundle.getInt(FIELD_SPATIALIZATION_BEHAVIOR));
bundle.getInt(keyForField(FIELD_SPATIALIZATION_BEHAVIOR)));
} }
return builder.build(); return builder.build();
}; };
private static String keyForField(@FieldNumber int field) {
return Integer.toString(field, Character.MAX_RADIX);
}
@RequiresApi(29) @RequiresApi(29)
private static final class Api29 { private static final class Api29 {
@DoNotInline @DoNotInline
......
...@@ -16,11 +16,9 @@ ...@@ -16,11 +16,9 @@
package com.google.android.exoplayer2.source; package com.google.android.exoplayer2.source;
import static com.google.android.exoplayer2.util.Assertions.checkArgument; import static com.google.android.exoplayer2.util.Assertions.checkArgument;
import static java.lang.annotation.ElementType.TYPE_USE;
import android.os.Bundle; import android.os.Bundle;
import androidx.annotation.CheckResult; import androidx.annotation.CheckResult;
import androidx.annotation.IntDef;
import androidx.annotation.Nullable; import androidx.annotation.Nullable;
import com.google.android.exoplayer2.Bundleable; import com.google.android.exoplayer2.Bundleable;
import com.google.android.exoplayer2.C; import com.google.android.exoplayer2.C;
...@@ -29,11 +27,8 @@ import com.google.android.exoplayer2.Tracks; ...@@ -29,11 +27,8 @@ import com.google.android.exoplayer2.Tracks;
import com.google.android.exoplayer2.util.BundleableUtil; import com.google.android.exoplayer2.util.BundleableUtil;
import com.google.android.exoplayer2.util.Log; import com.google.android.exoplayer2.util.Log;
import com.google.android.exoplayer2.util.MimeTypes; import com.google.android.exoplayer2.util.MimeTypes;
import com.google.android.exoplayer2.util.Util;
import com.google.common.collect.ImmutableList; import com.google.common.collect.ImmutableList;
import java.lang.annotation.Documented;
import java.lang.annotation.Retention;
import java.lang.annotation.RetentionPolicy;
import java.lang.annotation.Target;
import java.util.ArrayList; import java.util.ArrayList;
import java.util.Arrays; import java.util.Arrays;
import java.util.List; import java.util.List;
...@@ -164,15 +159,8 @@ public final class TrackGroup implements Bundleable { ...@@ -164,15 +159,8 @@ public final class TrackGroup implements Bundleable {
} }
// Bundleable implementation. // Bundleable implementation.
private static final String FIELD_FORMATS = Util.intToStringMaxRadix(0);
@Documented private static final String FIELD_ID = Util.intToStringMaxRadix(1);
@Retention(RetentionPolicy.SOURCE)
@Target(TYPE_USE)
@IntDef({FIELD_FORMATS, FIELD_ID})
private @interface FieldNumber {}
private static final int FIELD_FORMATS = 0;
private static final int FIELD_ID = 1;
@Override @Override
public Bundle toBundle() { public Bundle toBundle() {
...@@ -181,28 +169,23 @@ public final class TrackGroup implements Bundleable { ...@@ -181,28 +169,23 @@ public final class TrackGroup implements Bundleable {
for (Format format : formats) { for (Format format : formats) {
arrayList.add(format.toBundle(/* excludeMetadata= */ true)); arrayList.add(format.toBundle(/* excludeMetadata= */ true));
} }
bundle.putParcelableArrayList(keyForField(FIELD_FORMATS), arrayList); bundle.putParcelableArrayList(FIELD_FORMATS, arrayList);
bundle.putString(keyForField(FIELD_ID), id); bundle.putString(FIELD_ID, id);
return bundle; return bundle;
} }
/** Object that can restore {@code TrackGroup} from a {@link Bundle}. */ /** Object that can restore {@code TrackGroup} from a {@link Bundle}. */
public static final Creator<TrackGroup> CREATOR = public static final Creator<TrackGroup> CREATOR =
bundle -> { bundle -> {
@Nullable @Nullable List<Bundle> formatBundles = bundle.getParcelableArrayList(FIELD_FORMATS);
List<Bundle> formatBundles = bundle.getParcelableArrayList(keyForField(FIELD_FORMATS));
List<Format> formats = List<Format> formats =
formatBundles == null formatBundles == null
? ImmutableList.of() ? ImmutableList.of()
: BundleableUtil.fromBundleList(Format.CREATOR, formatBundles); : BundleableUtil.fromBundleList(Format.CREATOR, formatBundles);
String id = bundle.getString(keyForField(FIELD_ID), /* defaultValue= */ ""); String id = bundle.getString(FIELD_ID, /* defaultValue= */ "");
return new TrackGroup(id, formats.toArray(new Format[0])); return new TrackGroup(id, formats.toArray(new Format[0]));
}; };
private static String keyForField(@FieldNumber int field) {
return Integer.toString(field, Character.MAX_RADIX);
}
private void verifyCorrectness() { private void verifyCorrectness() {
// TrackGroups should only contain tracks with exactly the same content (but in different // TrackGroups should only contain tracks with exactly the same content (but in different
// qualities). We only log an error instead of throwing to not break backwards-compatibility for // qualities). We only log an error instead of throwing to not break backwards-compatibility for
......
...@@ -460,44 +460,29 @@ public final class AdPlaybackState implements Bundleable { ...@@ -460,44 +460,29 @@ public final class AdPlaybackState implements Bundleable {
// Bundleable implementation. // Bundleable implementation.
@Documented private static final String FIELD_TIME_US = Util.intToStringMaxRadix(0);
@Retention(RetentionPolicy.SOURCE) private static final String FIELD_COUNT = Util.intToStringMaxRadix(1);
@Target(TYPE_USE) private static final String FIELD_URIS = Util.intToStringMaxRadix(2);
@IntDef({ private static final String FIELD_STATES = Util.intToStringMaxRadix(3);
FIELD_TIME_US, private static final String FIELD_DURATIONS_US = Util.intToStringMaxRadix(4);
FIELD_COUNT, private static final String FIELD_CONTENT_RESUME_OFFSET_US = Util.intToStringMaxRadix(5);
FIELD_URIS, private static final String FIELD_IS_SERVER_SIDE_INSERTED = Util.intToStringMaxRadix(6);
FIELD_STATES, private static final String FIELD_ORIGINAL_COUNT = Util.intToStringMaxRadix(7);
FIELD_DURATIONS_US,
FIELD_CONTENT_RESUME_OFFSET_US,
FIELD_IS_SERVER_SIDE_INSERTED,
FIELD_ORIGINAL_COUNT
})
private @interface FieldNumber {}
private static final int FIELD_TIME_US = 0;
private static final int FIELD_COUNT = 1;
private static final int FIELD_URIS = 2;
private static final int FIELD_STATES = 3;
private static final int FIELD_DURATIONS_US = 4;
private static final int FIELD_CONTENT_RESUME_OFFSET_US = 5;
private static final int FIELD_IS_SERVER_SIDE_INSERTED = 6;
private static final int FIELD_ORIGINAL_COUNT = 7;
// putParcelableArrayList actually supports null elements. // putParcelableArrayList actually supports null elements.
@SuppressWarnings("nullness:argument") @SuppressWarnings("nullness:argument")
@Override @Override
public Bundle toBundle() { public Bundle toBundle() {
Bundle bundle = new Bundle(); Bundle bundle = new Bundle();
bundle.putLong(keyForField(FIELD_TIME_US), timeUs); bundle.putLong(FIELD_TIME_US, timeUs);
bundle.putInt(keyForField(FIELD_COUNT), count); bundle.putInt(FIELD_COUNT, count);
bundle.putInt(keyForField(FIELD_ORIGINAL_COUNT), originalCount); bundle.putInt(FIELD_ORIGINAL_COUNT, originalCount);
bundle.putParcelableArrayList( bundle.putParcelableArrayList(
keyForField(FIELD_URIS), new ArrayList<@NullableType Uri>(Arrays.asList(uris))); FIELD_URIS, new ArrayList<@NullableType Uri>(Arrays.asList(uris)));
bundle.putIntArray(keyForField(FIELD_STATES), states); bundle.putIntArray(FIELD_STATES, states);
bundle.putLongArray(keyForField(FIELD_DURATIONS_US), durationsUs); bundle.putLongArray(FIELD_DURATIONS_US, durationsUs);
bundle.putLong(keyForField(FIELD_CONTENT_RESUME_OFFSET_US), contentResumeOffsetUs); bundle.putLong(FIELD_CONTENT_RESUME_OFFSET_US, contentResumeOffsetUs);
bundle.putBoolean(keyForField(FIELD_IS_SERVER_SIDE_INSERTED), isServerSideInserted); bundle.putBoolean(FIELD_IS_SERVER_SIDE_INSERTED, isServerSideInserted);
return bundle; return bundle;
} }
...@@ -507,17 +492,16 @@ public final class AdPlaybackState implements Bundleable { ...@@ -507,17 +492,16 @@ public final class AdPlaybackState implements Bundleable {
// getParcelableArrayList may have null elements. // getParcelableArrayList may have null elements.
@SuppressWarnings("nullness:type.argument") @SuppressWarnings("nullness:type.argument")
private static AdGroup fromBundle(Bundle bundle) { private static AdGroup fromBundle(Bundle bundle) {
long timeUs = bundle.getLong(keyForField(FIELD_TIME_US)); long timeUs = bundle.getLong(FIELD_TIME_US);
int count = bundle.getInt(keyForField(FIELD_COUNT)); int count = bundle.getInt(FIELD_COUNT);
int originalCount = bundle.getInt(keyForField(FIELD_ORIGINAL_COUNT)); int originalCount = bundle.getInt(FIELD_ORIGINAL_COUNT);
@Nullable @Nullable ArrayList<@NullableType Uri> uriList = bundle.getParcelableArrayList(FIELD_URIS);
ArrayList<@NullableType Uri> uriList = bundle.getParcelableArrayList(keyForField(FIELD_URIS));
@Nullable @Nullable
@AdState @AdState
int[] states = bundle.getIntArray(keyForField(FIELD_STATES)); int[] states = bundle.getIntArray(FIELD_STATES);
@Nullable long[] durationsUs = bundle.getLongArray(keyForField(FIELD_DURATIONS_US)); @Nullable long[] durationsUs = bundle.getLongArray(FIELD_DURATIONS_US);
long contentResumeOffsetUs = bundle.getLong(keyForField(FIELD_CONTENT_RESUME_OFFSET_US)); long contentResumeOffsetUs = bundle.getLong(FIELD_CONTENT_RESUME_OFFSET_US);
boolean isServerSideInserted = bundle.getBoolean(keyForField(FIELD_IS_SERVER_SIDE_INSERTED)); boolean isServerSideInserted = bundle.getBoolean(FIELD_IS_SERVER_SIDE_INSERTED);
return new AdGroup( return new AdGroup(
timeUs, timeUs,
count, count,
...@@ -528,10 +512,6 @@ public final class AdPlaybackState implements Bundleable { ...@@ -528,10 +512,6 @@ public final class AdPlaybackState implements Bundleable {
contentResumeOffsetUs, contentResumeOffsetUs,
isServerSideInserted); isServerSideInserted);
} }
private static String keyForField(@AdGroup.FieldNumber int field) {
return Integer.toString(field, Character.MAX_RADIX);
}
} }
/** /**
...@@ -1122,21 +1102,10 @@ public final class AdPlaybackState implements Bundleable { ...@@ -1122,21 +1102,10 @@ public final class AdPlaybackState implements Bundleable {
// Bundleable implementation. // Bundleable implementation.
@Documented private static final String FIELD_AD_GROUPS = Util.intToStringMaxRadix(1);
@Retention(RetentionPolicy.SOURCE) private static final String FIELD_AD_RESUME_POSITION_US = Util.intToStringMaxRadix(2);
@Target(TYPE_USE) private static final String FIELD_CONTENT_DURATION_US = Util.intToStringMaxRadix(3);
@IntDef({ private static final String FIELD_REMOVED_AD_GROUP_COUNT = Util.intToStringMaxRadix(4);
FIELD_AD_GROUPS,
FIELD_AD_RESUME_POSITION_US,
FIELD_CONTENT_DURATION_US,
FIELD_REMOVED_AD_GROUP_COUNT
})
private @interface FieldNumber {}
private static final int FIELD_AD_GROUPS = 1;
private static final int FIELD_AD_RESUME_POSITION_US = 2;
private static final int FIELD_CONTENT_DURATION_US = 3;
private static final int FIELD_REMOVED_AD_GROUP_COUNT = 4;
/** /**
* {@inheritDoc} * {@inheritDoc}
...@@ -1153,16 +1122,16 @@ public final class AdPlaybackState implements Bundleable { ...@@ -1153,16 +1122,16 @@ public final class AdPlaybackState implements Bundleable {
adGroupBundleList.add(adGroup.toBundle()); adGroupBundleList.add(adGroup.toBundle());
} }
if (!adGroupBundleList.isEmpty()) { if (!adGroupBundleList.isEmpty()) {
bundle.putParcelableArrayList(keyForField(FIELD_AD_GROUPS), adGroupBundleList); bundle.putParcelableArrayList(FIELD_AD_GROUPS, adGroupBundleList);
} }
if (adResumePositionUs != NONE.adResumePositionUs) { if (adResumePositionUs != NONE.adResumePositionUs) {
bundle.putLong(keyForField(FIELD_AD_RESUME_POSITION_US), adResumePositionUs); bundle.putLong(FIELD_AD_RESUME_POSITION_US, adResumePositionUs);
} }
if (contentDurationUs != NONE.contentDurationUs) { if (contentDurationUs != NONE.contentDurationUs) {
bundle.putLong(keyForField(FIELD_CONTENT_DURATION_US), contentDurationUs); bundle.putLong(FIELD_CONTENT_DURATION_US, contentDurationUs);
} }
if (removedAdGroupCount != NONE.removedAdGroupCount) { if (removedAdGroupCount != NONE.removedAdGroupCount) {
bundle.putInt(keyForField(FIELD_REMOVED_AD_GROUP_COUNT), removedAdGroupCount); bundle.putInt(FIELD_REMOVED_AD_GROUP_COUNT, removedAdGroupCount);
} }
return bundle; return bundle;
} }
...@@ -1175,9 +1144,7 @@ public final class AdPlaybackState implements Bundleable { ...@@ -1175,9 +1144,7 @@ public final class AdPlaybackState implements Bundleable {
public static final Bundleable.Creator<AdPlaybackState> CREATOR = AdPlaybackState::fromBundle; public static final Bundleable.Creator<AdPlaybackState> CREATOR = AdPlaybackState::fromBundle;
private static AdPlaybackState fromBundle(Bundle bundle) { private static AdPlaybackState fromBundle(Bundle bundle) {
@Nullable @Nullable ArrayList<Bundle> adGroupBundleList = bundle.getParcelableArrayList(FIELD_AD_GROUPS);
ArrayList<Bundle> adGroupBundleList =
bundle.getParcelableArrayList(keyForField(FIELD_AD_GROUPS));
@Nullable AdGroup[] adGroups; @Nullable AdGroup[] adGroups;
if (adGroupBundleList == null) { if (adGroupBundleList == null) {
adGroups = new AdGroup[0]; adGroups = new AdGroup[0];
...@@ -1188,23 +1155,15 @@ public final class AdPlaybackState implements Bundleable { ...@@ -1188,23 +1155,15 @@ public final class AdPlaybackState implements Bundleable {
} }
} }
long adResumePositionUs = long adResumePositionUs =
bundle.getLong( bundle.getLong(FIELD_AD_RESUME_POSITION_US, /* defaultValue= */ NONE.adResumePositionUs);
keyForField(FIELD_AD_RESUME_POSITION_US), /* defaultValue= */ NONE.adResumePositionUs);
long contentDurationUs = long contentDurationUs =
bundle.getLong( bundle.getLong(FIELD_CONTENT_DURATION_US, /* defaultValue= */ NONE.contentDurationUs);
keyForField(FIELD_CONTENT_DURATION_US), /* defaultValue= */ NONE.contentDurationUs);
int removedAdGroupCount = int removedAdGroupCount =
bundle.getInt( bundle.getInt(FIELD_REMOVED_AD_GROUP_COUNT, /* defaultValue= */ NONE.removedAdGroupCount);
keyForField(FIELD_REMOVED_AD_GROUP_COUNT),
/* defaultValue= */ NONE.removedAdGroupCount);
return new AdPlaybackState( return new AdPlaybackState(
/* adsId= */ null, adGroups, adResumePositionUs, contentDurationUs, removedAdGroupCount); /* adsId= */ null, adGroups, adResumePositionUs, contentDurationUs, removedAdGroupCount);
} }
private static String keyForField(@FieldNumber int field) {
return Integer.toString(field, Character.MAX_RADIX);
}
private static AdGroup[] createEmptyAdGroups(long[] adGroupTimesUs) { private static AdGroup[] createEmptyAdGroups(long[] adGroupTimesUs) {
AdGroup[] adGroups = new AdGroup[adGroupTimesUs.length]; AdGroup[] adGroups = new AdGroup[adGroupTimesUs.length];
for (int i = 0; i < adGroups.length; i++) { for (int i = 0; i < adGroups.length; i++) {
......
...@@ -34,6 +34,7 @@ import androidx.annotation.IntDef; ...@@ -34,6 +34,7 @@ import androidx.annotation.IntDef;
import androidx.annotation.Nullable; import androidx.annotation.Nullable;
import com.google.android.exoplayer2.Bundleable; import com.google.android.exoplayer2.Bundleable;
import com.google.android.exoplayer2.util.Assertions; import com.google.android.exoplayer2.util.Assertions;
import com.google.android.exoplayer2.util.Util;
import com.google.common.base.Objects; import com.google.common.base.Objects;
import com.google.errorprone.annotations.CanIgnoreReturnValue; import com.google.errorprone.annotations.CanIgnoreReturnValue;
import java.lang.annotation.Documented; import java.lang.annotation.Documented;
...@@ -970,68 +971,44 @@ public final class Cue implements Bundleable { ...@@ -970,68 +971,44 @@ public final class Cue implements Bundleable {
// Bundleable implementation. // Bundleable implementation.
@Documented private static final String FIELD_TEXT = Util.intToStringMaxRadix(0);
@Retention(RetentionPolicy.SOURCE) private static final String FIELD_TEXT_ALIGNMENT = Util.intToStringMaxRadix(1);
@Target(TYPE_USE) private static final String FIELD_MULTI_ROW_ALIGNMENT = Util.intToStringMaxRadix(2);
@IntDef({ private static final String FIELD_BITMAP = Util.intToStringMaxRadix(3);
FIELD_TEXT, private static final String FIELD_LINE = Util.intToStringMaxRadix(4);
FIELD_TEXT_ALIGNMENT, private static final String FIELD_LINE_TYPE = Util.intToStringMaxRadix(5);
FIELD_MULTI_ROW_ALIGNMENT, private static final String FIELD_LINE_ANCHOR = Util.intToStringMaxRadix(6);
FIELD_BITMAP, private static final String FIELD_POSITION = Util.intToStringMaxRadix(7);
FIELD_LINE, private static final String FIELD_POSITION_ANCHOR = Util.intToStringMaxRadix(8);
FIELD_LINE_TYPE, private static final String FIELD_TEXT_SIZE_TYPE = Util.intToStringMaxRadix(9);
FIELD_LINE_ANCHOR, private static final String FIELD_TEXT_SIZE = Util.intToStringMaxRadix(10);
FIELD_POSITION, private static final String FIELD_SIZE = Util.intToStringMaxRadix(11);
FIELD_POSITION_ANCHOR, private static final String FIELD_BITMAP_HEIGHT = Util.intToStringMaxRadix(12);
FIELD_TEXT_SIZE_TYPE, private static final String FIELD_WINDOW_COLOR = Util.intToStringMaxRadix(13);
FIELD_TEXT_SIZE, private static final String FIELD_WINDOW_COLOR_SET = Util.intToStringMaxRadix(14);
FIELD_SIZE, private static final String FIELD_VERTICAL_TYPE = Util.intToStringMaxRadix(15);
FIELD_BITMAP_HEIGHT, private static final String FIELD_SHEAR_DEGREES = Util.intToStringMaxRadix(16);
FIELD_WINDOW_COLOR,
FIELD_WINDOW_COLOR_SET,
FIELD_VERTICAL_TYPE,
FIELD_SHEAR_DEGREES
})
private @interface FieldNumber {}
private static final int FIELD_TEXT = 0;
private static final int FIELD_TEXT_ALIGNMENT = 1;
private static final int FIELD_MULTI_ROW_ALIGNMENT = 2;
private static final int FIELD_BITMAP = 3;
private static final int FIELD_LINE = 4;
private static final int FIELD_LINE_TYPE = 5;
private static final int FIELD_LINE_ANCHOR = 6;
private static final int FIELD_POSITION = 7;
private static final int FIELD_POSITION_ANCHOR = 8;
private static final int FIELD_TEXT_SIZE_TYPE = 9;
private static final int FIELD_TEXT_SIZE = 10;
private static final int FIELD_SIZE = 11;
private static final int FIELD_BITMAP_HEIGHT = 12;
private static final int FIELD_WINDOW_COLOR = 13;
private static final int FIELD_WINDOW_COLOR_SET = 14;
private static final int FIELD_VERTICAL_TYPE = 15;
private static final int FIELD_SHEAR_DEGREES = 16;
@Override @Override
public Bundle toBundle() { public Bundle toBundle() {
Bundle bundle = new Bundle(); Bundle bundle = new Bundle();
bundle.putCharSequence(keyForField(FIELD_TEXT), text); bundle.putCharSequence(FIELD_TEXT, text);
bundle.putSerializable(keyForField(FIELD_TEXT_ALIGNMENT), textAlignment); bundle.putSerializable(FIELD_TEXT_ALIGNMENT, textAlignment);
bundle.putSerializable(keyForField(FIELD_MULTI_ROW_ALIGNMENT), multiRowAlignment); bundle.putSerializable(FIELD_MULTI_ROW_ALIGNMENT, multiRowAlignment);
bundle.putParcelable(keyForField(FIELD_BITMAP), bitmap); bundle.putParcelable(FIELD_BITMAP, bitmap);
bundle.putFloat(keyForField(FIELD_LINE), line); bundle.putFloat(FIELD_LINE, line);
bundle.putInt(keyForField(FIELD_LINE_TYPE), lineType); bundle.putInt(FIELD_LINE_TYPE, lineType);
bundle.putInt(keyForField(FIELD_LINE_ANCHOR), lineAnchor); bundle.putInt(FIELD_LINE_ANCHOR, lineAnchor);
bundle.putFloat(keyForField(FIELD_POSITION), position); bundle.putFloat(FIELD_POSITION, position);
bundle.putInt(keyForField(FIELD_POSITION_ANCHOR), positionAnchor); bundle.putInt(FIELD_POSITION_ANCHOR, positionAnchor);
bundle.putInt(keyForField(FIELD_TEXT_SIZE_TYPE), textSizeType); bundle.putInt(FIELD_TEXT_SIZE_TYPE, textSizeType);
bundle.putFloat(keyForField(FIELD_TEXT_SIZE), textSize); bundle.putFloat(FIELD_TEXT_SIZE, textSize);
bundle.putFloat(keyForField(FIELD_SIZE), size); bundle.putFloat(FIELD_SIZE, size);
bundle.putFloat(keyForField(FIELD_BITMAP_HEIGHT), bitmapHeight); bundle.putFloat(FIELD_BITMAP_HEIGHT, bitmapHeight);
bundle.putBoolean(keyForField(FIELD_WINDOW_COLOR_SET), windowColorSet); bundle.putBoolean(FIELD_WINDOW_COLOR_SET, windowColorSet);
bundle.putInt(keyForField(FIELD_WINDOW_COLOR), windowColor); bundle.putInt(FIELD_WINDOW_COLOR, windowColor);
bundle.putInt(keyForField(FIELD_VERTICAL_TYPE), verticalType); bundle.putInt(FIELD_VERTICAL_TYPE, verticalType);
bundle.putFloat(keyForField(FIELD_SHEAR_DEGREES), shearDegrees); bundle.putFloat(FIELD_SHEAR_DEGREES, shearDegrees);
return bundle; return bundle;
} }
...@@ -1039,67 +1016,56 @@ public final class Cue implements Bundleable { ...@@ -1039,67 +1016,56 @@ public final class Cue implements Bundleable {
private static final Cue fromBundle(Bundle bundle) { private static final Cue fromBundle(Bundle bundle) {
Builder builder = new Builder(); Builder builder = new Builder();
@Nullable CharSequence text = bundle.getCharSequence(keyForField(FIELD_TEXT)); @Nullable CharSequence text = bundle.getCharSequence(FIELD_TEXT);
if (text != null) { if (text != null) {
builder.setText(text); builder.setText(text);
} }
@Nullable @Nullable Alignment textAlignment = (Alignment) bundle.getSerializable(FIELD_TEXT_ALIGNMENT);
Alignment textAlignment = (Alignment) bundle.getSerializable(keyForField(FIELD_TEXT_ALIGNMENT));
if (textAlignment != null) { if (textAlignment != null) {
builder.setTextAlignment(textAlignment); builder.setTextAlignment(textAlignment);
} }
@Nullable @Nullable
Alignment multiRowAlignment = Alignment multiRowAlignment = (Alignment) bundle.getSerializable(FIELD_MULTI_ROW_ALIGNMENT);
(Alignment) bundle.getSerializable(keyForField(FIELD_MULTI_ROW_ALIGNMENT));
if (multiRowAlignment != null) { if (multiRowAlignment != null) {
builder.setMultiRowAlignment(multiRowAlignment); builder.setMultiRowAlignment(multiRowAlignment);
} }
@Nullable Bitmap bitmap = bundle.getParcelable(keyForField(FIELD_BITMAP)); @Nullable Bitmap bitmap = bundle.getParcelable(FIELD_BITMAP);
if (bitmap != null) { if (bitmap != null) {
builder.setBitmap(bitmap); builder.setBitmap(bitmap);
} }
if (bundle.containsKey(keyForField(FIELD_LINE)) if (bundle.containsKey(FIELD_LINE) && bundle.containsKey(FIELD_LINE_TYPE)) {
&& bundle.containsKey(keyForField(FIELD_LINE_TYPE))) { builder.setLine(bundle.getFloat(FIELD_LINE), bundle.getInt(FIELD_LINE_TYPE));
builder.setLine(
bundle.getFloat(keyForField(FIELD_LINE)), bundle.getInt(keyForField(FIELD_LINE_TYPE)));
} }
if (bundle.containsKey(keyForField(FIELD_LINE_ANCHOR))) { if (bundle.containsKey(FIELD_LINE_ANCHOR)) {
builder.setLineAnchor(bundle.getInt(keyForField(FIELD_LINE_ANCHOR))); builder.setLineAnchor(bundle.getInt(FIELD_LINE_ANCHOR));
} }
if (bundle.containsKey(keyForField(FIELD_POSITION))) { if (bundle.containsKey(FIELD_POSITION)) {
builder.setPosition(bundle.getFloat(keyForField(FIELD_POSITION))); builder.setPosition(bundle.getFloat(FIELD_POSITION));
} }
if (bundle.containsKey(keyForField(FIELD_POSITION_ANCHOR))) { if (bundle.containsKey(FIELD_POSITION_ANCHOR)) {
builder.setPositionAnchor(bundle.getInt(keyForField(FIELD_POSITION_ANCHOR))); builder.setPositionAnchor(bundle.getInt(FIELD_POSITION_ANCHOR));
} }
if (bundle.containsKey(keyForField(FIELD_TEXT_SIZE)) if (bundle.containsKey(FIELD_TEXT_SIZE) && bundle.containsKey(FIELD_TEXT_SIZE_TYPE)) {
&& bundle.containsKey(keyForField(FIELD_TEXT_SIZE_TYPE))) { builder.setTextSize(bundle.getFloat(FIELD_TEXT_SIZE), bundle.getInt(FIELD_TEXT_SIZE_TYPE));
builder.setTextSize(
bundle.getFloat(keyForField(FIELD_TEXT_SIZE)),
bundle.getInt(keyForField(FIELD_TEXT_SIZE_TYPE)));
} }
if (bundle.containsKey(keyForField(FIELD_SIZE))) { if (bundle.containsKey(FIELD_SIZE)) {
builder.setSize(bundle.getFloat(keyForField(FIELD_SIZE))); builder.setSize(bundle.getFloat(FIELD_SIZE));
} }
if (bundle.containsKey(keyForField(FIELD_BITMAP_HEIGHT))) { if (bundle.containsKey(FIELD_BITMAP_HEIGHT)) {
builder.setBitmapHeight(bundle.getFloat(keyForField(FIELD_BITMAP_HEIGHT))); builder.setBitmapHeight(bundle.getFloat(FIELD_BITMAP_HEIGHT));
} }
if (bundle.containsKey(keyForField(FIELD_WINDOW_COLOR))) { if (bundle.containsKey(FIELD_WINDOW_COLOR)) {
builder.setWindowColor(bundle.getInt(keyForField(FIELD_WINDOW_COLOR))); builder.setWindowColor(bundle.getInt(FIELD_WINDOW_COLOR));
} }
if (!bundle.getBoolean(keyForField(FIELD_WINDOW_COLOR_SET), /* defaultValue= */ false)) { if (!bundle.getBoolean(FIELD_WINDOW_COLOR_SET, /* defaultValue= */ false)) {
builder.clearWindowColor(); builder.clearWindowColor();
} }
if (bundle.containsKey(keyForField(FIELD_VERTICAL_TYPE))) { if (bundle.containsKey(FIELD_VERTICAL_TYPE)) {
builder.setVerticalType(bundle.getInt(keyForField(FIELD_VERTICAL_TYPE))); builder.setVerticalType(bundle.getInt(FIELD_VERTICAL_TYPE));
} }
if (bundle.containsKey(keyForField(FIELD_SHEAR_DEGREES))) { if (bundle.containsKey(FIELD_SHEAR_DEGREES)) {
builder.setShearDegrees(bundle.getFloat(keyForField(FIELD_SHEAR_DEGREES))); builder.setShearDegrees(bundle.getFloat(FIELD_SHEAR_DEGREES));
} }
return builder.build(); return builder.build();
} }
private static String keyForField(@FieldNumber int field) {
return Integer.toString(field, Character.MAX_RADIX);
}
} }
...@@ -15,20 +15,14 @@ ...@@ -15,20 +15,14 @@
*/ */
package com.google.android.exoplayer2.text; package com.google.android.exoplayer2.text;
import static java.lang.annotation.ElementType.TYPE_USE;
import android.graphics.Bitmap; import android.graphics.Bitmap;
import android.os.Bundle; import android.os.Bundle;
import androidx.annotation.IntDef;
import androidx.annotation.Nullable; import androidx.annotation.Nullable;
import com.google.android.exoplayer2.Bundleable; import com.google.android.exoplayer2.Bundleable;
import com.google.android.exoplayer2.Timeline; import com.google.android.exoplayer2.Timeline;
import com.google.android.exoplayer2.util.BundleableUtil; import com.google.android.exoplayer2.util.BundleableUtil;
import com.google.android.exoplayer2.util.Util;
import com.google.common.collect.ImmutableList; import com.google.common.collect.ImmutableList;
import java.lang.annotation.Documented;
import java.lang.annotation.Retention;
import java.lang.annotation.RetentionPolicy;
import java.lang.annotation.Target;
import java.util.ArrayList; import java.util.ArrayList;
import java.util.List; import java.util.List;
...@@ -63,40 +57,30 @@ public final class CueGroup implements Bundleable { ...@@ -63,40 +57,30 @@ public final class CueGroup implements Bundleable {
// Bundleable implementation. // Bundleable implementation.
@Documented private static final String FIELD_CUES = Util.intToStringMaxRadix(0);
@Retention(RetentionPolicy.SOURCE) private static final String FIELD_PRESENTATION_TIME_US = Util.intToStringMaxRadix(1);
@Target(TYPE_USE)
@IntDef({FIELD_CUES, FIELD_PRESENTATION_TIME_US})
private @interface FieldNumber {}
private static final int FIELD_CUES = 0;
private static final int FIELD_PRESENTATION_TIME_US = 1;
@Override @Override
public Bundle toBundle() { public Bundle toBundle() {
Bundle bundle = new Bundle(); Bundle bundle = new Bundle();
bundle.putParcelableArrayList( bundle.putParcelableArrayList(
keyForField(FIELD_CUES), BundleableUtil.toBundleArrayList(filterOutBitmapCues(cues))); FIELD_CUES, BundleableUtil.toBundleArrayList(filterOutBitmapCues(cues)));
bundle.putLong(keyForField(FIELD_PRESENTATION_TIME_US), presentationTimeUs); bundle.putLong(FIELD_PRESENTATION_TIME_US, presentationTimeUs);
return bundle; return bundle;
} }
public static final Creator<CueGroup> CREATOR = CueGroup::fromBundle; public static final Creator<CueGroup> CREATOR = CueGroup::fromBundle;
private static final CueGroup fromBundle(Bundle bundle) { private static final CueGroup fromBundle(Bundle bundle) {
@Nullable ArrayList<Bundle> cueBundles = bundle.getParcelableArrayList(keyForField(FIELD_CUES)); @Nullable ArrayList<Bundle> cueBundles = bundle.getParcelableArrayList(FIELD_CUES);
List<Cue> cues = List<Cue> cues =
cueBundles == null cueBundles == null
? ImmutableList.of() ? ImmutableList.of()
: BundleableUtil.fromBundleList(Cue.CREATOR, cueBundles); : BundleableUtil.fromBundleList(Cue.CREATOR, cueBundles);
long presentationTimeUs = bundle.getLong(keyForField(FIELD_PRESENTATION_TIME_US)); long presentationTimeUs = bundle.getLong(FIELD_PRESENTATION_TIME_US);
return new CueGroup(cues, presentationTimeUs); return new CueGroup(cues, presentationTimeUs);
} }
private static String keyForField(@FieldNumber int field) {
return Integer.toString(field, Character.MAX_RADIX);
}
/** /**
* Filters out {@link Cue} objects containing {@link Bitmap}. It is used when transferring cues * Filters out {@link Cue} objects containing {@link Bitmap}. It is used when transferring cues
* between processes to prevent transferring too much data. * between processes to prevent transferring too much data.
......
...@@ -20,16 +20,13 @@ import static java.util.Collections.max; ...@@ -20,16 +20,13 @@ import static java.util.Collections.max;
import static java.util.Collections.min; import static java.util.Collections.min;
import android.os.Bundle; import android.os.Bundle;
import androidx.annotation.IntDef;
import androidx.annotation.Nullable; import androidx.annotation.Nullable;
import com.google.android.exoplayer2.Bundleable; import com.google.android.exoplayer2.Bundleable;
import com.google.android.exoplayer2.C; import com.google.android.exoplayer2.C;
import com.google.android.exoplayer2.source.TrackGroup; import com.google.android.exoplayer2.source.TrackGroup;
import com.google.android.exoplayer2.util.Util;
import com.google.common.collect.ImmutableList; import com.google.common.collect.ImmutableList;
import com.google.common.primitives.Ints; import com.google.common.primitives.Ints;
import java.lang.annotation.Documented;
import java.lang.annotation.Retention;
import java.lang.annotation.RetentionPolicy;
import java.util.List; import java.util.List;
/** /**
...@@ -56,16 +53,8 @@ public final class TrackSelectionOverride implements Bundleable { ...@@ -56,16 +53,8 @@ public final class TrackSelectionOverride implements Bundleable {
/** The indices of tracks in a {@link TrackGroup} to be selected. */ /** The indices of tracks in a {@link TrackGroup} to be selected. */
public final ImmutableList<Integer> trackIndices; public final ImmutableList<Integer> trackIndices;
@Documented private static final String FIELD_TRACK_GROUP = Util.intToStringMaxRadix(0);
@Retention(RetentionPolicy.SOURCE) private static final String FIELD_TRACKS = Util.intToStringMaxRadix(1);
@IntDef({
FIELD_TRACK_GROUP,
FIELD_TRACKS,
})
private @interface FieldNumber {}
private static final int FIELD_TRACK_GROUP = 0;
private static final int FIELD_TRACKS = 1;
/** /**
* Constructs an instance to force {@code trackIndex} in {@code trackGroup} to be selected. * Constructs an instance to force {@code trackIndex} in {@code trackGroup} to be selected.
...@@ -120,21 +109,17 @@ public final class TrackSelectionOverride implements Bundleable { ...@@ -120,21 +109,17 @@ public final class TrackSelectionOverride implements Bundleable {
@Override @Override
public Bundle toBundle() { public Bundle toBundle() {
Bundle bundle = new Bundle(); Bundle bundle = new Bundle();
bundle.putBundle(keyForField(FIELD_TRACK_GROUP), mediaTrackGroup.toBundle()); bundle.putBundle(FIELD_TRACK_GROUP, mediaTrackGroup.toBundle());
bundle.putIntArray(keyForField(FIELD_TRACKS), Ints.toArray(trackIndices)); bundle.putIntArray(FIELD_TRACKS, Ints.toArray(trackIndices));
return bundle; return bundle;
} }
/** Object that can restore {@code TrackSelectionOverride} from a {@link Bundle}. */ /** Object that can restore {@code TrackSelectionOverride} from a {@link Bundle}. */
public static final Creator<TrackSelectionOverride> CREATOR = public static final Creator<TrackSelectionOverride> CREATOR =
bundle -> { bundle -> {
Bundle trackGroupBundle = checkNotNull(bundle.getBundle(keyForField(FIELD_TRACK_GROUP))); Bundle trackGroupBundle = checkNotNull(bundle.getBundle(FIELD_TRACK_GROUP));
TrackGroup mediaTrackGroup = TrackGroup.CREATOR.fromBundle(trackGroupBundle); TrackGroup mediaTrackGroup = TrackGroup.CREATOR.fromBundle(trackGroupBundle);
int[] tracks = checkNotNull(bundle.getIntArray(keyForField(FIELD_TRACKS))); int[] tracks = checkNotNull(bundle.getIntArray(FIELD_TRACKS));
return new TrackSelectionOverride(mediaTrackGroup, Ints.asList(tracks)); return new TrackSelectionOverride(mediaTrackGroup, Ints.asList(tracks));
}; };
private static String keyForField(@FieldNumber int field) {
return Integer.toString(field, Character.MAX_RADIX);
}
} }
...@@ -2805,6 +2805,15 @@ public final class Util { ...@@ -2805,6 +2805,15 @@ public final class Util {
: resources.getDrawable(drawableRes); : resources.getDrawable(drawableRes);
} }
/**
* Returns a string representation of the integer using radix value {@link Character#MAX_RADIX}.
*
* @param i An integer to be converted to String.
*/
public static String intToStringMaxRadix(int i) {
return Integer.toString(i, Character.MAX_RADIX);
}
@Nullable @Nullable
private static String getSystemProperty(String name) { private static String getSystemProperty(String name) {
try { try {
......
...@@ -15,19 +15,13 @@ ...@@ -15,19 +15,13 @@
*/ */
package com.google.android.exoplayer2.video; package com.google.android.exoplayer2.video;
import static java.lang.annotation.ElementType.TYPE_USE;
import android.os.Bundle; import android.os.Bundle;
import androidx.annotation.IntDef;
import androidx.annotation.Nullable; import androidx.annotation.Nullable;
import com.google.android.exoplayer2.Bundleable; import com.google.android.exoplayer2.Bundleable;
import com.google.android.exoplayer2.C; import com.google.android.exoplayer2.C;
import com.google.android.exoplayer2.Format; import com.google.android.exoplayer2.Format;
import com.google.android.exoplayer2.util.Util;
import com.google.errorprone.annotations.CanIgnoreReturnValue; import com.google.errorprone.annotations.CanIgnoreReturnValue;
import java.lang.annotation.Documented;
import java.lang.annotation.Retention;
import java.lang.annotation.RetentionPolicy;
import java.lang.annotation.Target;
import java.util.Arrays; import java.util.Arrays;
import org.checkerframework.dataflow.qual.Pure; import org.checkerframework.dataflow.qual.Pure;
...@@ -288,41 +282,26 @@ public final class ColorInfo implements Bundleable { ...@@ -288,41 +282,26 @@ public final class ColorInfo implements Bundleable {
// Bundleable implementation // Bundleable implementation
@Documented private static final String FIELD_COLOR_SPACE = Util.intToStringMaxRadix(0);
@Retention(RetentionPolicy.SOURCE) private static final String FIELD_COLOR_RANGE = Util.intToStringMaxRadix(1);
@Target(TYPE_USE) private static final String FIELD_COLOR_TRANSFER = Util.intToStringMaxRadix(2);
@IntDef({ private static final String FIELD_HDR_STATIC_INFO = Util.intToStringMaxRadix(3);
FIELD_COLOR_SPACE,
FIELD_COLOR_RANGE,
FIELD_COLOR_TRANSFER,
FIELD_HDR_STATIC_INFO,
})
private @interface FieldNumber {}
private static final int FIELD_COLOR_SPACE = 0;
private static final int FIELD_COLOR_RANGE = 1;
private static final int FIELD_COLOR_TRANSFER = 2;
private static final int FIELD_HDR_STATIC_INFO = 3;
@Override @Override
public Bundle toBundle() { public Bundle toBundle() {
Bundle bundle = new Bundle(); Bundle bundle = new Bundle();
bundle.putInt(keyForField(FIELD_COLOR_SPACE), colorSpace); bundle.putInt(FIELD_COLOR_SPACE, colorSpace);
bundle.putInt(keyForField(FIELD_COLOR_RANGE), colorRange); bundle.putInt(FIELD_COLOR_RANGE, colorRange);
bundle.putInt(keyForField(FIELD_COLOR_TRANSFER), colorTransfer); bundle.putInt(FIELD_COLOR_TRANSFER, colorTransfer);
bundle.putByteArray(keyForField(FIELD_HDR_STATIC_INFO), hdrStaticInfo); bundle.putByteArray(FIELD_HDR_STATIC_INFO, hdrStaticInfo);
return bundle; return bundle;
} }
public static final Creator<ColorInfo> CREATOR = public static final Creator<ColorInfo> CREATOR =
bundle -> bundle ->
new ColorInfo( new ColorInfo(
bundle.getInt(keyForField(FIELD_COLOR_SPACE), Format.NO_VALUE), bundle.getInt(FIELD_COLOR_SPACE, Format.NO_VALUE),
bundle.getInt(keyForField(FIELD_COLOR_RANGE), Format.NO_VALUE), bundle.getInt(FIELD_COLOR_RANGE, Format.NO_VALUE),
bundle.getInt(keyForField(FIELD_COLOR_TRANSFER), Format.NO_VALUE), bundle.getInt(FIELD_COLOR_TRANSFER, Format.NO_VALUE),
bundle.getByteArray(keyForField(FIELD_HDR_STATIC_INFO))); bundle.getByteArray(FIELD_HDR_STATIC_INFO));
private static String keyForField(@FieldNumber int field) {
return Integer.toString(field, Character.MAX_RADIX);
}
} }
...@@ -15,18 +15,12 @@ ...@@ -15,18 +15,12 @@
*/ */
package com.google.android.exoplayer2.video; package com.google.android.exoplayer2.video;
import static java.lang.annotation.ElementType.TYPE_USE;
import android.os.Bundle; import android.os.Bundle;
import androidx.annotation.FloatRange; import androidx.annotation.FloatRange;
import androidx.annotation.IntDef;
import androidx.annotation.IntRange; import androidx.annotation.IntRange;
import androidx.annotation.Nullable; import androidx.annotation.Nullable;
import com.google.android.exoplayer2.Bundleable; import com.google.android.exoplayer2.Bundleable;
import java.lang.annotation.Documented; import com.google.android.exoplayer2.util.Util;
import java.lang.annotation.Retention;
import java.lang.annotation.RetentionPolicy;
import java.lang.annotation.Target;
/** Represents the video size. */ /** Represents the video size. */
public final class VideoSize implements Bundleable { public final class VideoSize implements Bundleable {
...@@ -130,46 +124,30 @@ public final class VideoSize implements Bundleable { ...@@ -130,46 +124,30 @@ public final class VideoSize implements Bundleable {
} }
// Bundleable implementation. // Bundleable implementation.
@Documented
@Retention(RetentionPolicy.SOURCE) private static final String FIELD_WIDTH = Util.intToStringMaxRadix(0);
@Target(TYPE_USE) private static final String FIELD_HEIGHT = Util.intToStringMaxRadix(1);
@IntDef({ private static final String FIELD_UNAPPLIED_ROTATION_DEGREES = Util.intToStringMaxRadix(2);
FIELD_WIDTH, private static final String FIELD_PIXEL_WIDTH_HEIGHT_RATIO = Util.intToStringMaxRadix(3);
FIELD_HEIGHT,
FIELD_UNAPPLIED_ROTATION_DEGREES,
FIELD_PIXEL_WIDTH_HEIGHT_RATIO,
})
private @interface FieldNumber {}
private static final int FIELD_WIDTH = 0;
private static final int FIELD_HEIGHT = 1;
private static final int FIELD_UNAPPLIED_ROTATION_DEGREES = 2;
private static final int FIELD_PIXEL_WIDTH_HEIGHT_RATIO = 3;
@Override @Override
public Bundle toBundle() { public Bundle toBundle() {
Bundle bundle = new Bundle(); Bundle bundle = new Bundle();
bundle.putInt(keyForField(FIELD_WIDTH), width); bundle.putInt(FIELD_WIDTH, width);
bundle.putInt(keyForField(FIELD_HEIGHT), height); bundle.putInt(FIELD_HEIGHT, height);
bundle.putInt(keyForField(FIELD_UNAPPLIED_ROTATION_DEGREES), unappliedRotationDegrees); bundle.putInt(FIELD_UNAPPLIED_ROTATION_DEGREES, unappliedRotationDegrees);
bundle.putFloat(keyForField(FIELD_PIXEL_WIDTH_HEIGHT_RATIO), pixelWidthHeightRatio); bundle.putFloat(FIELD_PIXEL_WIDTH_HEIGHT_RATIO, pixelWidthHeightRatio);
return bundle; return bundle;
} }
public static final Creator<VideoSize> CREATOR = public static final Creator<VideoSize> CREATOR =
bundle -> { bundle -> {
int width = bundle.getInt(keyForField(FIELD_WIDTH), DEFAULT_WIDTH); int width = bundle.getInt(FIELD_WIDTH, DEFAULT_WIDTH);
int height = bundle.getInt(keyForField(FIELD_HEIGHT), DEFAULT_HEIGHT); int height = bundle.getInt(FIELD_HEIGHT, DEFAULT_HEIGHT);
int unappliedRotationDegrees = int unappliedRotationDegrees =
bundle.getInt( bundle.getInt(FIELD_UNAPPLIED_ROTATION_DEGREES, DEFAULT_UNAPPLIED_ROTATION_DEGREES);
keyForField(FIELD_UNAPPLIED_ROTATION_DEGREES), DEFAULT_UNAPPLIED_ROTATION_DEGREES);
float pixelWidthHeightRatio = float pixelWidthHeightRatio =
bundle.getFloat( bundle.getFloat(FIELD_PIXEL_WIDTH_HEIGHT_RATIO, DEFAULT_PIXEL_WIDTH_HEIGHT_RATIO);
keyForField(FIELD_PIXEL_WIDTH_HEIGHT_RATIO), DEFAULT_PIXEL_WIDTH_HEIGHT_RATIO);
return new VideoSize(width, height, unappliedRotationDegrees, pixelWidthHeightRatio); return new VideoSize(width, height, unappliedRotationDegrees, pixelWidthHeightRatio);
}; };
private static String keyForField(@FieldNumber int field) {
return Integer.toString(field, Character.MAX_RADIX);
}
} }
...@@ -861,6 +861,8 @@ public class MediaItemTest { ...@@ -861,6 +861,8 @@ public class MediaItemTest {
@Test @Test
public void createMediaItemInstance_roundTripViaBundle_yieldsEqualInstance() { public void createMediaItemInstance_roundTripViaBundle_yieldsEqualInstance() {
Bundle extras = new Bundle();
extras.putString("key", "value");
// Creates instance by setting some non-default values // Creates instance by setting some non-default values
MediaItem mediaItem = MediaItem mediaItem =
new MediaItem.Builder() new MediaItem.Builder()
...@@ -876,11 +878,14 @@ public class MediaItemTest { ...@@ -876,11 +878,14 @@ public class MediaItemTest {
new RequestMetadata.Builder() new RequestMetadata.Builder()
.setMediaUri(Uri.parse("http://test.test")) .setMediaUri(Uri.parse("http://test.test"))
.setSearchQuery("search") .setSearchQuery("search")
.setExtras(extras)
.build()) .build())
.build(); .build();
MediaItem mediaItemFromBundle = MediaItem.CREATOR.fromBundle(mediaItem.toBundle()); MediaItem mediaItemFromBundle = MediaItem.CREATOR.fromBundle(mediaItem.toBundle());
assertThat(mediaItemFromBundle).isEqualTo(mediaItem); assertThat(mediaItemFromBundle).isEqualTo(mediaItem);
assertThat(mediaItemFromBundle.requestMetadata.extras)
.isEqualTo(mediaItem.requestMetadata.extras);
} }
} }
...@@ -240,17 +240,15 @@ public final class ExoPlaybackException extends PlaybackException { ...@@ -240,17 +240,15 @@ public final class ExoPlaybackException extends PlaybackException {
private ExoPlaybackException(Bundle bundle) { private ExoPlaybackException(Bundle bundle) {
super(bundle); super(bundle);
type = bundle.getInt(keyForField(FIELD_TYPE), /* defaultValue= */ TYPE_UNEXPECTED); type = bundle.getInt(FIELD_TYPE, /* defaultValue= */ TYPE_UNEXPECTED);
rendererName = bundle.getString(keyForField(FIELD_RENDERER_NAME)); rendererName = bundle.getString(FIELD_RENDERER_NAME);
rendererIndex = rendererIndex = bundle.getInt(FIELD_RENDERER_INDEX, /* defaultValue= */ C.INDEX_UNSET);
bundle.getInt(keyForField(FIELD_RENDERER_INDEX), /* defaultValue= */ C.INDEX_UNSET); @Nullable Bundle rendererFormatBundle = bundle.getBundle(FIELD_RENDERER_FORMAT);
@Nullable Bundle rendererFormatBundle = bundle.getBundle(keyForField(FIELD_RENDERER_FORMAT));
rendererFormat = rendererFormat =
rendererFormatBundle == null ? null : Format.CREATOR.fromBundle(rendererFormatBundle); rendererFormatBundle == null ? null : Format.CREATOR.fromBundle(rendererFormatBundle);
rendererFormatSupport = rendererFormatSupport =
bundle.getInt( bundle.getInt(FIELD_RENDERER_FORMAT_SUPPORT, /* defaultValue= */ C.FORMAT_HANDLED);
keyForField(FIELD_RENDERER_FORMAT_SUPPORT), /* defaultValue= */ C.FORMAT_HANDLED); isRecoverable = bundle.getBoolean(FIELD_IS_RECOVERABLE, /* defaultValue= */ false);
isRecoverable = bundle.getBoolean(keyForField(FIELD_IS_RECOVERABLE), /* defaultValue= */ false);
mediaPeriodId = null; mediaPeriodId = null;
} }
...@@ -389,12 +387,17 @@ public final class ExoPlaybackException extends PlaybackException { ...@@ -389,12 +387,17 @@ public final class ExoPlaybackException extends PlaybackException {
/** Object that can restore {@link ExoPlaybackException} from a {@link Bundle}. */ /** Object that can restore {@link ExoPlaybackException} from a {@link Bundle}. */
public static final Creator<ExoPlaybackException> CREATOR = ExoPlaybackException::new; public static final Creator<ExoPlaybackException> CREATOR = ExoPlaybackException::new;
private static final int FIELD_TYPE = FIELD_CUSTOM_ID_BASE + 1; private static final String FIELD_TYPE = Util.intToStringMaxRadix(FIELD_CUSTOM_ID_BASE + 1);
private static final int FIELD_RENDERER_NAME = FIELD_CUSTOM_ID_BASE + 2; private static final String FIELD_RENDERER_NAME =
private static final int FIELD_RENDERER_INDEX = FIELD_CUSTOM_ID_BASE + 3; Util.intToStringMaxRadix(FIELD_CUSTOM_ID_BASE + 2);
private static final int FIELD_RENDERER_FORMAT = FIELD_CUSTOM_ID_BASE + 4; private static final String FIELD_RENDERER_INDEX =
private static final int FIELD_RENDERER_FORMAT_SUPPORT = FIELD_CUSTOM_ID_BASE + 5; Util.intToStringMaxRadix(FIELD_CUSTOM_ID_BASE + 3);
private static final int FIELD_IS_RECOVERABLE = FIELD_CUSTOM_ID_BASE + 6; private static final String FIELD_RENDERER_FORMAT =
Util.intToStringMaxRadix(FIELD_CUSTOM_ID_BASE + 4);
private static final String FIELD_RENDERER_FORMAT_SUPPORT =
Util.intToStringMaxRadix(FIELD_CUSTOM_ID_BASE + 5);
private static final String FIELD_IS_RECOVERABLE =
Util.intToStringMaxRadix(FIELD_CUSTOM_ID_BASE + 6);
/** /**
* {@inheritDoc} * {@inheritDoc}
...@@ -405,14 +408,14 @@ public final class ExoPlaybackException extends PlaybackException { ...@@ -405,14 +408,14 @@ public final class ExoPlaybackException extends PlaybackException {
@Override @Override
public Bundle toBundle() { public Bundle toBundle() {
Bundle bundle = super.toBundle(); Bundle bundle = super.toBundle();
bundle.putInt(keyForField(FIELD_TYPE), type); bundle.putInt(FIELD_TYPE, type);
bundle.putString(keyForField(FIELD_RENDERER_NAME), rendererName); bundle.putString(FIELD_RENDERER_NAME, rendererName);
bundle.putInt(keyForField(FIELD_RENDERER_INDEX), rendererIndex); bundle.putInt(FIELD_RENDERER_INDEX, rendererIndex);
if (rendererFormat != null) { if (rendererFormat != null) {
bundle.putBundle(keyForField(FIELD_RENDERER_FORMAT), rendererFormat.toBundle()); bundle.putBundle(FIELD_RENDERER_FORMAT, rendererFormat.toBundle());
} }
bundle.putInt(keyForField(FIELD_RENDERER_FORMAT_SUPPORT), rendererFormatSupport); bundle.putInt(FIELD_RENDERER_FORMAT_SUPPORT, rendererFormatSupport);
bundle.putBoolean(keyForField(FIELD_IS_RECOVERABLE), isRecoverable); bundle.putBoolean(FIELD_IS_RECOVERABLE, isRecoverable);
return bundle; return bundle;
} }
} }
...@@ -15,20 +15,14 @@ ...@@ -15,20 +15,14 @@
*/ */
package com.google.android.exoplayer2.source; package com.google.android.exoplayer2.source;
import static java.lang.annotation.ElementType.TYPE_USE;
import android.os.Bundle; import android.os.Bundle;
import androidx.annotation.IntDef;
import androidx.annotation.Nullable; import androidx.annotation.Nullable;
import com.google.android.exoplayer2.Bundleable; import com.google.android.exoplayer2.Bundleable;
import com.google.android.exoplayer2.C; import com.google.android.exoplayer2.C;
import com.google.android.exoplayer2.util.BundleableUtil; import com.google.android.exoplayer2.util.BundleableUtil;
import com.google.android.exoplayer2.util.Log; import com.google.android.exoplayer2.util.Log;
import com.google.android.exoplayer2.util.Util;
import com.google.common.collect.ImmutableList; import com.google.common.collect.ImmutableList;
import java.lang.annotation.Documented;
import java.lang.annotation.Retention;
import java.lang.annotation.RetentionPolicy;
import java.lang.annotation.Target;
import java.util.List; import java.util.List;
/** /**
...@@ -115,21 +109,13 @@ public final class TrackGroupArray implements Bundleable { ...@@ -115,21 +109,13 @@ public final class TrackGroupArray implements Bundleable {
// Bundleable implementation. // Bundleable implementation.
@Documented private static final String FIELD_TRACK_GROUPS = Util.intToStringMaxRadix(0);
@Retention(RetentionPolicy.SOURCE)
@Target(TYPE_USE)
@IntDef({
FIELD_TRACK_GROUPS,
})
private @interface FieldNumber {}
private static final int FIELD_TRACK_GROUPS = 0;
@Override @Override
public Bundle toBundle() { public Bundle toBundle() {
Bundle bundle = new Bundle(); Bundle bundle = new Bundle();
bundle.putParcelableArrayList( bundle.putParcelableArrayList(
keyForField(FIELD_TRACK_GROUPS), BundleableUtil.toBundleArrayList(trackGroups)); FIELD_TRACK_GROUPS, BundleableUtil.toBundleArrayList(trackGroups));
return bundle; return bundle;
} }
...@@ -137,8 +123,7 @@ public final class TrackGroupArray implements Bundleable { ...@@ -137,8 +123,7 @@ public final class TrackGroupArray implements Bundleable {
public static final Creator<TrackGroupArray> CREATOR = public static final Creator<TrackGroupArray> CREATOR =
bundle -> { bundle -> {
@Nullable @Nullable
List<Bundle> trackGroupBundles = List<Bundle> trackGroupBundles = bundle.getParcelableArrayList(FIELD_TRACK_GROUPS);
bundle.getParcelableArrayList(keyForField(FIELD_TRACK_GROUPS));
if (trackGroupBundles == null) { if (trackGroupBundles == null) {
return new TrackGroupArray(); return new TrackGroupArray();
} }
...@@ -160,8 +145,4 @@ public final class TrackGroupArray implements Bundleable { ...@@ -160,8 +145,4 @@ public final class TrackGroupArray implements Bundleable {
} }
} }
} }
private static String keyForField(@FieldNumber int field) {
return Integer.toString(field, Character.MAX_RADIX);
}
} }
Markdown is supported
0% or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or sign in to comment