From c9b6a73cd8b820cac80d3343829e1f3dc122db18 Mon Sep 17 00:00:00 2001 From: GiuseppePiscopo Date: Tue, 24 Jul 2018 09:53:52 +0200 Subject: [PATCH 1/4] feat(playlist): add API to remove range of indices --- .../source/ConcatenatingMediaSource.java | 92 +++++++++++++++++++ 1 file changed, 92 insertions(+) diff --git a/library/core/src/main/java/com/google/android/exoplayer2/source/ConcatenatingMediaSource.java b/library/core/src/main/java/com/google/android/exoplayer2/source/ConcatenatingMediaSource.java index 985c43fa55..b9ce48d3c8 100644 --- a/library/core/src/main/java/com/google/android/exoplayer2/source/ConcatenatingMediaSource.java +++ b/library/core/src/main/java/com/google/android/exoplayer2/source/ConcatenatingMediaSource.java @@ -50,6 +50,7 @@ public class ConcatenatingMediaSource extends CompositeMediaSourceNote: If you want to move the instance, it's preferable to use {@link #moveMediaSource(int, * int)} instead. * + *

Note: If you want to remove a set of contiguous sources, it's preferable to use + * {@link #removeMediaSourceRange(int, int)} instead. + * * @param index The index at which the media source will be removed. This index must be in the * range of 0 <= index < {@link #getSize()}. */ @@ -278,6 +282,9 @@ public class ConcatenatingMediaSource extends CompositeMediaSourceNote: If you want to move the instance, it's preferable to use {@link #moveMediaSource(int, * int)} instead. * + *

Note: If you want to remove a set of contiguous sources, it's preferable to use + * {@link #removeMediaSourceRange(int, int)} instead. + * * @param index The index at which the media source will be removed. This index must be in the * range of 0 <= index < {@link #getSize()}. * @param actionOnCompletion A {@link Runnable} which is executed immediately after the media @@ -297,6 +304,79 @@ public class ConcatenatingMediaSource extends CompositeMediaSourceNote: when specified range is empty, no actual media source is removed and no exception + * is thrown. + * + * @param fromIndex The initial range index, pointing to the first media source that will be + * removed. This index must be in the range of 0 <= index < {@link #getSize()}. + * @param toIndex The final range index, pointing to the first media source that will be left + * untouched. The last media source to be removed is at index {@code toIndex} - 1. + * This index must be in the range of 0 <= index < {@link #getSize()}. + * + * @throws IndexOutOfBoundsException when either index is out of playlist bounds, i.e. {@code + * fromIndex} < 0 or >= {@link #getSize()}, {@code toIndex} < 0 or > {@link + * #getSize()} + * @throws IndexOutOfBoundsException when range is malformed, i.e. {@code fromIndex} > + * {@code toIndex} + */ + public final synchronized void removeMediaSourceRange(int fromIndex, int toIndex) { + removeMediaSourceRange(fromIndex, toIndex, null); + } + + /** + * Removes a range of {@link MediaSource}s from the playlist, by specifying an initial index + * (included) and a final index (excluded), and executes a custom action on completion.. + * + *

Note: when specified range is empty, no actual media source is removed and no exception + * is thrown. + * + * @param fromIndex The initial range index, pointing to the first media source that will be + * removed. This index must be in the range of 0 <= index < {@link #getSize()}. + * @param toIndex The final range index, pointing to the first media source that will be left + * untouched. The last media source to be removed is at index {@code toIndex} - 1. + * This index must be in the range of 0 <= index < {@link #getSize()}. + * + * @throws IndexOutOfBoundsException when either index is out of playlist bounds, i.e. {@code + * fromIndex} < 0 or >= {@link #getSize()}, {@code toIndex} < 0 or > {@link + * #getSize()} + * @throws IndexOutOfBoundsException when range is malformed, i.e. {@code fromIndex} > + * {@code toIndex} + * @param actionOnCompletion A {@link Runnable} which is executed immediately after the media + * source has been removed from the playlist. + */ + public final synchronized void removeMediaSourceRange( + int fromIndex, int toIndex, @Nullable Runnable actionOnCompletion) { + if (fromIndex < 0 || fromIndex >= mediaSourcesPublic.size()) { + throw new IndexOutOfBoundsException(String.format("Cannot remove source range: initial index (%d) out of bounds", fromIndex)); + } + if (toIndex < 0 || toIndex > mediaSourcesPublic.size()) { + throw new IndexOutOfBoundsException(String.format("Cannot remove source range: final index (%d) out of bounds", toIndex)); + } + if (fromIndex > toIndex) { + throw new IndexOutOfBoundsException(String.format("Cannot remove source range: range malformed (%d, %d)", fromIndex, toIndex)); + } + if (fromIndex == toIndex) { + if (actionOnCompletion != null) { + actionOnCompletion.run(); + } + return; + } + mediaSourcesPublic.subList(fromIndex, toIndex).clear(); + if (player != null) { + player + .createMessage(this) + .setType(MSG_REMOVE_RANGE) + .setPayload(new MessageData<>(fromIndex, toIndex, actionOnCompletion)) + .send(); + } else if (actionOnCompletion != null) { + actionOnCompletion.run(); + } + } + /** * Moves an existing {@link MediaSource} within the playlist. * @@ -489,6 +569,18 @@ public class ConcatenatingMediaSource extends CompositeMediaSource removeRangeMessage = (MessageData) message; + int fromIndex = removeRangeMessage.index; + int toIndex = removeRangeMessage.customData; + for (int index = toIndex - 1; index >= fromIndex; index--) { + shuffleOrder = shuffleOrder.cloneAndRemove(index); + } + for (int index = toIndex - 1; index >= fromIndex; index--) { + removeMediaSourceInternal(index); + } + scheduleListenerNotification(removeRangeMessage.actionOnCompletion); + break; case MSG_MOVE: MessageData moveMessage = (MessageData) message; shuffleOrder = shuffleOrder.cloneAndRemove(moveMessage.index); From 2c801ca2678016d5464caf214f45e6448f018756 Mon Sep 17 00:00:00 2001 From: GiuseppePiscopo Date: Tue, 24 Jul 2018 09:55:19 +0200 Subject: [PATCH 2/4] docs(playlist): fix javadoc comments --- .../android/exoplayer2/source/ConcatenatingMediaSource.java | 6 +++--- 1 file changed, 3 insertions(+), 3 deletions(-) diff --git a/library/core/src/main/java/com/google/android/exoplayer2/source/ConcatenatingMediaSource.java b/library/core/src/main/java/com/google/android/exoplayer2/source/ConcatenatingMediaSource.java index b9ce48d3c8..a3bc8511b8 100644 --- a/library/core/src/main/java/com/google/android/exoplayer2/source/ConcatenatingMediaSource.java +++ b/library/core/src/main/java/com/google/android/exoplayer2/source/ConcatenatingMediaSource.java @@ -280,10 +280,10 @@ public class ConcatenatingMediaSource extends CompositeMediaSourceNote: If you want to move the instance, it's preferable to use {@link #moveMediaSource(int, - * int)} instead. + * int, Runnable)} instead. * *

Note: If you want to remove a set of contiguous sources, it's preferable to use - * {@link #removeMediaSourceRange(int, int)} instead. + * {@link #removeMediaSourceRange(int, int, Runnable)} instead. * * @param index The index at which the media source will be removed. This index must be in the * range of 0 <= index < {@link #getSize()}. @@ -346,7 +346,7 @@ public class ConcatenatingMediaSource extends CompositeMediaSource Date: Wed, 25 Jul 2018 18:30:35 +0200 Subject: [PATCH 3/4] feat(playlist): stick to list.subList API --- .../source/ConcatenatingMediaSource.java | 37 +++++-------------- 1 file changed, 9 insertions(+), 28 deletions(-) diff --git a/library/core/src/main/java/com/google/android/exoplayer2/source/ConcatenatingMediaSource.java b/library/core/src/main/java/com/google/android/exoplayer2/source/ConcatenatingMediaSource.java index a3bc8511b8..565636a120 100644 --- a/library/core/src/main/java/com/google/android/exoplayer2/source/ConcatenatingMediaSource.java +++ b/library/core/src/main/java/com/google/android/exoplayer2/source/ConcatenatingMediaSource.java @@ -312,16 +312,11 @@ public class ConcatenatingMediaSource extends CompositeMediaSource= mediaSourcesPublic.size()) { - throw new IndexOutOfBoundsException(String.format("Cannot remove source range: initial index (%d) out of bounds", fromIndex)); - } - if (toIndex < 0 || toIndex > mediaSourcesPublic.size()) { - throw new IndexOutOfBoundsException(String.format("Cannot remove source range: final index (%d) out of bounds", toIndex)); - } - if (fromIndex > toIndex) { - throw new IndexOutOfBoundsException(String.format("Cannot remove source range: range malformed (%d, %d)", fromIndex, toIndex)); - } + Util.removeRange(mediaSourcesPublic, fromIndex, toIndex); if (fromIndex == toIndex) { if (actionOnCompletion != null) { actionOnCompletion.run(); } return; } - mediaSourcesPublic.subList(fromIndex, toIndex).clear(); if (player != null) { player .createMessage(this) From 7878bf6a3148d2cdaab8245dd8fbac1754391415 Mon Sep 17 00:00:00 2001 From: GiuseppePiscopo Date: Wed, 25 Jul 2018 18:31:14 +0200 Subject: [PATCH 4/4] chore(playlist): reorder message IDs, fix tabbing --- .../source/ConcatenatingMediaSource.java | 20 +++++++++---------- 1 file changed, 10 insertions(+), 10 deletions(-) diff --git a/library/core/src/main/java/com/google/android/exoplayer2/source/ConcatenatingMediaSource.java b/library/core/src/main/java/com/google/android/exoplayer2/source/ConcatenatingMediaSource.java index 565636a120..c5f7c5e1c9 100644 --- a/library/core/src/main/java/com/google/android/exoplayer2/source/ConcatenatingMediaSource.java +++ b/library/core/src/main/java/com/google/android/exoplayer2/source/ConcatenatingMediaSource.java @@ -50,11 +50,11 @@ public class ConcatenatingMediaSource extends CompositeMediaSource mediaSourcesPublic; @@ -324,7 +324,7 @@ public class ConcatenatingMediaSource extends CompositeMediaSourceNote: when specified range is empty, no actual media source is removed and no exception * is thrown. @@ -349,10 +349,10 @@ public class ConcatenatingMediaSource extends CompositeMediaSource(fromIndex, toIndex, actionOnCompletion)) - .send(); + .createMessage(this) + .setType(MSG_REMOVE_RANGE) + .setPayload(new MessageData<>(fromIndex, toIndex, actionOnCompletion)) + .send(); } else if (actionOnCompletion != null) { actionOnCompletion.run(); }