From 43da8627801a0b1cd5d52a0ce7fd3dbf706f6cef Mon Sep 17 00:00:00 2001 From: jaewan Date: Wed, 12 May 2021 06:55:42 +0100 Subject: [PATCH] Add copy constructor and remove APIs for Commands builder It helps to change allowed commands from session. PiperOrigin-RevId: 373302990 --- .../com/google/android/exoplayer2/Player.java | 47 +++++++++++++++++++ .../android/exoplayer2/util/ExoFlags.java | 42 +++++++++++++++++ 2 files changed, 89 insertions(+) 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 afdb472b71..f00a6307ec 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 @@ -597,6 +597,11 @@ public interface Player { flagsBuilder = new ExoFlags.Builder(); } + private Builder(Commands commands) { + flagsBuilder = new ExoFlags.Builder(); + flagsBuilder.addAll(commands.flags); + } + /** * Adds a {@link Command}. * @@ -646,6 +651,43 @@ public interface Player { return this; } + /** + * Removes a {@link Command}. + * + * @param command A {@link Command}. + * @return This builder. + * @throws IllegalStateException If {@link #build()} has already been called. + */ + public Builder remove(@Command int command) { + flagsBuilder.remove(command); + return this; + } + + /** + * Removes a {@link Command} if the provided condition is true. Does nothing otherwise. + * + * @param command A {@link Command}. + * @param condition A condition. + * @return This builder. + * @throws IllegalStateException If {@link #build()} has already been called. + */ + public Builder removeIf(@Command int command, boolean condition) { + flagsBuilder.removeIf(command, condition); + return this; + } + + /** + * Removes {@link Command commands}. + * + * @param commands The {@link Command commands} to remove. + * @return This builder. + * @throws IllegalStateException If {@link #build()} has already been called. + */ + public Builder removeAll(@Command int... commands) { + flagsBuilder.removeAll(commands); + return this; + } + /** * Builds a {@link Commands} instance. * @@ -665,6 +707,11 @@ public interface Player { this.flags = flags; } + /** Returns a {@link Builder} initialized with the values of this instance. */ + public Builder buildUpon() { + return new Builder(this); + } + /** Returns whether the set of commands contains the specified {@link Command}. */ public boolean contains(@Command int command) { return flags.contains(command); diff --git a/library/common/src/main/java/com/google/android/exoplayer2/util/ExoFlags.java b/library/common/src/main/java/com/google/android/exoplayer2/util/ExoFlags.java index 46c9e486df..a78af0cfab 100644 --- a/library/common/src/main/java/com/google/android/exoplayer2/util/ExoFlags.java +++ b/library/common/src/main/java/com/google/android/exoplayer2/util/ExoFlags.java @@ -99,6 +99,48 @@ public final class ExoFlags { return this; } + /** + * Removes a flag. + * + * @param flag A flag. + * @return This builder. + * @throws IllegalStateException If {@link #build()} has already been called. + */ + public Builder remove(int flag) { + checkState(!buildCalled); + flags.delete(flag); + return this; + } + + /** + * Removes a flag if the provided condition is true. Does nothing otherwise. + * + * @param flag A flag. + * @param condition A condition. + * @return This builder. + * @throws IllegalStateException If {@link #build()} has already been called. + */ + public Builder removeIf(int flag, boolean condition) { + if (condition) { + return remove(flag); + } + return this; + } + + /** + * Removes flags. + * + * @param flags The flags to remove. + * @return This builder. + * @throws IllegalStateException If {@link #build()} has already been called. + */ + public Builder removeAll(int... flags) { + for (int flag : flags) { + remove(flag); + } + return this; + } + /** * Builds an {@link ExoFlags} instance. *