From d388ab25cc21cb3b74d2f76eb071bbc153a1ff44 Mon Sep 17 00:00:00 2001 From: jinpark Date: Thu, 6 May 2021 05:12:55 +0100 Subject: [PATCH] Make Player.Commands Bundleable PiperOrigin-RevId: 372266634 --- .../com/google/android/exoplayer2/Player.java | 43 ++++++++++++++++++- 1 file changed, 42 insertions(+), 1 deletion(-) diff --git a/library/common/src/main/java/com/google/android/exoplayer2/Player.java b/library/common/src/main/java/com/google/android/exoplayer2/Player.java index 89b5dbff1c..4f3a454671 100644 --- a/library/common/src/main/java/com/google/android/exoplayer2/Player.java +++ b/library/common/src/main/java/com/google/android/exoplayer2/Player.java @@ -41,6 +41,7 @@ import com.google.common.base.Objects; import java.lang.annotation.Documented; import java.lang.annotation.Retention; import java.lang.annotation.RetentionPolicy; +import java.util.ArrayList; import java.util.List; /** @@ -567,7 +568,7 @@ public interface Player { * *

Instances are immutable. */ - final class Commands { + final class Commands implements Bundleable { /** A builder for {@link Commands} instances. */ public static final class Builder { @@ -685,6 +686,46 @@ public interface Player { public int hashCode() { return flags.hashCode(); } + + // Bundleable implementation. + + @Documented + @Retention(RetentionPolicy.SOURCE) + @IntDef({FIELD_COMMANDS}) + private @interface FieldNumber {} + + private static final int FIELD_COMMANDS = 0; + + @Override + public Bundle toBundle() { + Bundle bundle = new Bundle(); + ArrayList commandsBundle = new ArrayList<>(); + for (int i = 0; i < flags.size(); i++) { + commandsBundle.add(flags.get(i)); + } + bundle.putIntegerArrayList(keyForField(FIELD_COMMANDS), commandsBundle); + return bundle; + } + + /** Object that can restore {@link Commands} from a {@link Bundle}. */ + public static final Creator CREATOR = Commands::fromBundle; + + private static Commands fromBundle(Bundle bundle) { + @Nullable + ArrayList commands = bundle.getIntegerArrayList(keyForField(FIELD_COMMANDS)); + if (commands == null) { + return Commands.EMPTY; + } + Builder builder = new Builder(); + for (int i = 0; i < commands.size(); i++) { + builder.add(commands.get(i)); + } + return builder.build(); + } + + private static String keyForField(@FieldNumber int field) { + return Integer.toString(field, Character.MAX_RADIX); + } } /**