From 7ba79171c7c9e84fb0e9f4bc242d81ef321c36fa Mon Sep 17 00:00:00 2001 From: Stypox Date: Sat, 18 Jun 2022 17:40:22 +0200 Subject: [PATCH] Refactor creation of DownloadDialog --- .../org/schabi/newpipe/RouterActivity.java | 5 +- .../newpipe/download/DownloadDialog.java | 148 +++++------------- .../fragments/detail/VideoDetailFragment.java | 6 +- 3 files changed, 39 insertions(+), 120 deletions(-) diff --git a/app/src/main/java/org/schabi/newpipe/RouterActivity.java b/app/src/main/java/org/schabi/newpipe/RouterActivity.java index 96f8ff1bc..1fe6ce7ec 100644 --- a/app/src/main/java/org/schabi/newpipe/RouterActivity.java +++ b/app/src/main/java/org/schabi/newpipe/RouterActivity.java @@ -70,7 +70,6 @@ import org.schabi.newpipe.player.playqueue.SinglePlayQueue; import org.schabi.newpipe.util.Constants; import org.schabi.newpipe.util.DeviceUtils; import org.schabi.newpipe.util.ExtractorHelper; -import org.schabi.newpipe.util.ListHelper; import org.schabi.newpipe.util.Localization; import org.schabi.newpipe.util.NavigationHelper; import org.schabi.newpipe.util.PermissionHelper; @@ -676,9 +675,7 @@ public class RouterActivity extends AppCompatActivity { .subscribeOn(Schedulers.io()) .observeOn(AndroidSchedulers.mainThread()) .subscribe(result -> { - final DownloadDialog downloadDialog = DownloadDialog.newInstance(this, result); - downloadDialog.setSelectedVideoStream(ListHelper.getDefaultResolutionIndex( - this, downloadDialog.wrappedVideoStreams.getStreamsList())); + final DownloadDialog downloadDialog = new DownloadDialog(this, result); downloadDialog.setOnDismissListener(dialog -> finish()); final FragmentManager fm = getSupportFragmentManager(); diff --git a/app/src/main/java/org/schabi/newpipe/download/DownloadDialog.java b/app/src/main/java/org/schabi/newpipe/download/DownloadDialog.java index 4fb47496b..e4adddc2a 100644 --- a/app/src/main/java/org/schabi/newpipe/download/DownloadDialog.java +++ b/app/src/main/java/org/schabi/newpipe/download/DownloadDialog.java @@ -48,7 +48,6 @@ import org.schabi.newpipe.extractor.MediaFormat; import org.schabi.newpipe.extractor.NewPipe; import org.schabi.newpipe.extractor.localization.Localization; import org.schabi.newpipe.extractor.stream.AudioStream; -import org.schabi.newpipe.extractor.stream.DeliveryMethod; import org.schabi.newpipe.extractor.stream.Stream; import org.schabi.newpipe.extractor.stream.StreamInfo; import org.schabi.newpipe.extractor.stream.SubtitlesStream; @@ -83,6 +82,7 @@ import us.shandian.giga.service.DownloadManagerService; import us.shandian.giga.service.DownloadManagerService.DownloadManagerBinder; import us.shandian.giga.service.MissionState; +import static org.schabi.newpipe.extractor.stream.DeliveryMethod.PROGRESSIVE_HTTP; import static org.schabi.newpipe.util.ListHelper.getStreamsOfSpecifiedDelivery; import static org.schabi.newpipe.util.Localization.assureCorrectAppLanguage; @@ -94,17 +94,17 @@ public class DownloadDialog extends DialogFragment @State StreamInfo currentInfo; @State - public StreamSizeWrapper wrappedAudioStreams = StreamSizeWrapper.empty(); + StreamSizeWrapper wrappedAudioStreams; @State - public StreamSizeWrapper wrappedVideoStreams = StreamSizeWrapper.empty(); + StreamSizeWrapper wrappedVideoStreams; @State - public StreamSizeWrapper wrappedSubtitleStreams = StreamSizeWrapper.empty(); + StreamSizeWrapper wrappedSubtitleStreams; @State - int selectedVideoIndex = 0; + int selectedVideoIndex; // set in the constructor @State - int selectedAudioIndex = 0; + int selectedAudioIndex = 0; // default to the first item @State - int selectedSubtitleIndex = 0; + int selectedSubtitleIndex = 0; // default to the first item @Nullable private OnDismissListener onDismissListener = null; @@ -140,116 +140,48 @@ public class DownloadDialog extends DialogFragment registerForActivityResult( new StartActivityForResult(), this::requestDownloadPickVideoFolderResult); + /*////////////////////////////////////////////////////////////////////////// // Instance creation //////////////////////////////////////////////////////////////////////////*/ - @NonNull - public static DownloadDialog newInstance(final Context context, - @NonNull final StreamInfo info) { - // TODO: Adapt this code when the downloader support other types of stream deliveries - final List progressiveHttpVideoStreams = - getStreamsOfSpecifiedDelivery(info.getVideoStreams(), - DeliveryMethod.PROGRESSIVE_HTTP); - - final List progressiveHttpVideoOnlyStreams = - getStreamsOfSpecifiedDelivery(info.getVideoOnlyStreams(), - DeliveryMethod.PROGRESSIVE_HTTP); - - final List progressiveHttpAudioStreams = - getStreamsOfSpecifiedDelivery(info.getAudioStreams(), - DeliveryMethod.PROGRESSIVE_HTTP); - - final List progressiveHttpSubtitlesStreams = - getStreamsOfSpecifiedDelivery(info.getSubtitles(), - DeliveryMethod.PROGRESSIVE_HTTP); - - final List videoStreamsList = ListHelper.getSortedStreamVideosList(context, - progressiveHttpVideoStreams, progressiveHttpVideoOnlyStreams, false, false); - - final DownloadDialog instance = new DownloadDialog(); - instance.setInfo(info); - instance.setVideoStreams(videoStreamsList); - instance.setAudioStreams(progressiveHttpAudioStreams); - instance.setSubtitleStreams(progressiveHttpSubtitlesStreams); - - return instance; - } - - - /*////////////////////////////////////////////////////////////////////////// - // Setters - //////////////////////////////////////////////////////////////////////////*/ - - private void setInfo(@NonNull final StreamInfo info) { + /** + * Create a new download dialog with the video, audio and subtitle streams from the provided + * stream info. Video streams and video-only streams will be put into a single list menu, + * sorted according to their resolution and the default video resolution will be selected. + * + * @param context the context to use just to obtain preferences and strings (will not be stored) + * @param info the info from which to obtain downloadable streams and other info (e.g. title) + */ + public DownloadDialog(final Context context, @NonNull final StreamInfo info) { this.currentInfo = info; - } - public void setAudioStreams(@NonNull final List audioStreams) { - this.wrappedAudioStreams = new StreamSizeWrapper<>(audioStreams, getContext()); - } + // TODO: Adapt this code when the downloader support other types of stream deliveries + final List videoStreams = ListHelper.getSortedStreamVideosList( + context, + getStreamsOfSpecifiedDelivery(info.getVideoStreams(), PROGRESSIVE_HTTP), + getStreamsOfSpecifiedDelivery(info.getVideoOnlyStreams(), PROGRESSIVE_HTTP), + false, + false + ); - public void setVideoStreams(@NonNull final List videoStreams) { - this.wrappedVideoStreams = new StreamSizeWrapper<>(videoStreams, getContext()); - } + this.wrappedVideoStreams = new StreamSizeWrapper<>(videoStreams, context); + this.wrappedAudioStreams = new StreamSizeWrapper<>( + getStreamsOfSpecifiedDelivery(info.getAudioStreams(), PROGRESSIVE_HTTP), context); + this.wrappedSubtitleStreams = new StreamSizeWrapper<>( + getStreamsOfSpecifiedDelivery(info.getSubtitles(), PROGRESSIVE_HTTP), context); - public void setSubtitleStreams(@NonNull final List subtitleStreams) { - this.wrappedSubtitleStreams = new StreamSizeWrapper<>(subtitleStreams, getContext()); + this.selectedVideoIndex = ListHelper.getDefaultResolutionIndex(context, videoStreams); } /** - * Set the selected video stream, by using its index in the stream list. - * - * The index of the select video stream will be not set if this index is not in the bounds - * of the stream list. - * - * @param svi the index of the selected {@link VideoStream} + * @param onDismissListener the listener to call in {@link #onDismiss(DialogInterface)} */ - public void setSelectedVideoStream(final int svi) { - if (selectedStreamIsInBoundsOfWrappedStreams(svi, this.wrappedVideoStreams)) { - this.selectedVideoIndex = svi; - } - } - - /** - * Set the selected audio stream, by using its index in the stream list. - * - * The index of the select audio stream will be not set if this index is not in the bounds - * of the stream list. - * - * @param sai the index of the selected {@link AudioStream} - */ - public void setSelectedAudioStream(final int sai) { - if (selectedStreamIsInBoundsOfWrappedStreams(sai, this.wrappedAudioStreams)) { - this.selectedAudioIndex = sai; - } - } - - /** - * Set the selected subtitles stream, by using its index in the stream list. - * - * The index of the select subtitles stream will be not set if this index is not in the bounds - * of the stream list. - * - * @param ssi the index of the selected {@link SubtitlesStream} - */ - public void setSelectedSubtitleStream(final int ssi) { - if (selectedStreamIsInBoundsOfWrappedStreams(ssi, this.wrappedSubtitleStreams)) { - this.selectedSubtitleIndex = ssi; - } - } - - private boolean selectedStreamIsInBoundsOfWrappedStreams( - final int selectedIndexStream, - final StreamSizeWrapper wrappedStreams) { - return selectedIndexStream > 0 - && selectedIndexStream < wrappedStreams.getStreamsList().size(); - } - public void setOnDismissListener(@Nullable final OnDismissListener onDismissListener) { this.onDismissListener = onDismissListener; } + /*////////////////////////////////////////////////////////////////////////// // Android lifecycle //////////////////////////////////////////////////////////////////////////*/ @@ -754,13 +686,9 @@ public class DownloadDialog extends DialogFragment if (format == MediaFormat.WEBMA_OPUS) { mimeTmp = "audio/ogg"; filenameTmp += "opus"; - } else { - if (format != null) { - mimeTmp = format.mimeType; - } - if (format != null) { - filenameTmp += format.suffix; - } + } else if (format != null) { + mimeTmp = format.mimeType; + filenameTmp += format.suffix; } break; case R.id.video_button: @@ -769,8 +697,6 @@ public class DownloadDialog extends DialogFragment format = videoStreamsAdapter.getItem(selectedVideoIndex).getFormat(); if (format != null) { mimeTmp = format.mimeType; - } - if (format != null) { filenameTmp += format.suffix; } break; @@ -1085,7 +1011,7 @@ public class DownloadDialog extends DialogFragment new MissionRecoveryInfo(selectedStream) }; } else { - if (secondaryStream.getDeliveryMethod() != DeliveryMethod.PROGRESSIVE_HTTP) { + if (secondaryStream.getDeliveryMethod() != PROGRESSIVE_HTTP) { throw new IllegalArgumentException("Unsupported stream delivery format" + secondaryStream.getDeliveryMethod()); } diff --git a/app/src/main/java/org/schabi/newpipe/fragments/detail/VideoDetailFragment.java b/app/src/main/java/org/schabi/newpipe/fragments/detail/VideoDetailFragment.java index ff2114b83..100af41cc 100644 --- a/app/src/main/java/org/schabi/newpipe/fragments/detail/VideoDetailFragment.java +++ b/app/src/main/java/org/schabi/newpipe/fragments/detail/VideoDetailFragment.java @@ -1689,11 +1689,7 @@ public final class VideoDetailFragment } try { - final DownloadDialog downloadDialog = DownloadDialog.newInstance(activity, - currentInfo); - downloadDialog.setSelectedVideoStream(ListHelper.getDefaultResolutionIndex(activity, - downloadDialog.wrappedVideoStreams.getStreamsList())); - + final DownloadDialog downloadDialog = new DownloadDialog(activity, currentInfo); downloadDialog.show(activity.getSupportFragmentManager(), "downloadDialog"); } catch (final Exception e) { ErrorUtil.showSnackbar(activity, new ErrorInfo(e, UserAction.DOWNLOAD_OPEN_DIALOG,