Refactor creation of DownloadDialog

This commit is contained in:
Stypox 2022-06-18 17:40:22 +02:00
parent e3c2aea3cc
commit 7ba79171c7
No known key found for this signature in database
GPG Key ID: 4BDF1B40A49FDD23
3 changed files with 39 additions and 120 deletions

View File

@ -70,7 +70,6 @@ import org.schabi.newpipe.player.playqueue.SinglePlayQueue;
import org.schabi.newpipe.util.Constants; import org.schabi.newpipe.util.Constants;
import org.schabi.newpipe.util.DeviceUtils; import org.schabi.newpipe.util.DeviceUtils;
import org.schabi.newpipe.util.ExtractorHelper; import org.schabi.newpipe.util.ExtractorHelper;
import org.schabi.newpipe.util.ListHelper;
import org.schabi.newpipe.util.Localization; import org.schabi.newpipe.util.Localization;
import org.schabi.newpipe.util.NavigationHelper; import org.schabi.newpipe.util.NavigationHelper;
import org.schabi.newpipe.util.PermissionHelper; import org.schabi.newpipe.util.PermissionHelper;
@ -676,9 +675,7 @@ public class RouterActivity extends AppCompatActivity {
.subscribeOn(Schedulers.io()) .subscribeOn(Schedulers.io())
.observeOn(AndroidSchedulers.mainThread()) .observeOn(AndroidSchedulers.mainThread())
.subscribe(result -> { .subscribe(result -> {
final DownloadDialog downloadDialog = DownloadDialog.newInstance(this, result); final DownloadDialog downloadDialog = new DownloadDialog(this, result);
downloadDialog.setSelectedVideoStream(ListHelper.getDefaultResolutionIndex(
this, downloadDialog.wrappedVideoStreams.getStreamsList()));
downloadDialog.setOnDismissListener(dialog -> finish()); downloadDialog.setOnDismissListener(dialog -> finish());
final FragmentManager fm = getSupportFragmentManager(); final FragmentManager fm = getSupportFragmentManager();

View File

@ -48,7 +48,6 @@ import org.schabi.newpipe.extractor.MediaFormat;
import org.schabi.newpipe.extractor.NewPipe; import org.schabi.newpipe.extractor.NewPipe;
import org.schabi.newpipe.extractor.localization.Localization; import org.schabi.newpipe.extractor.localization.Localization;
import org.schabi.newpipe.extractor.stream.AudioStream; 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.Stream;
import org.schabi.newpipe.extractor.stream.StreamInfo; import org.schabi.newpipe.extractor.stream.StreamInfo;
import org.schabi.newpipe.extractor.stream.SubtitlesStream; 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.DownloadManagerService.DownloadManagerBinder;
import us.shandian.giga.service.MissionState; 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.ListHelper.getStreamsOfSpecifiedDelivery;
import static org.schabi.newpipe.util.Localization.assureCorrectAppLanguage; import static org.schabi.newpipe.util.Localization.assureCorrectAppLanguage;
@ -94,17 +94,17 @@ public class DownloadDialog extends DialogFragment
@State @State
StreamInfo currentInfo; StreamInfo currentInfo;
@State @State
public StreamSizeWrapper<AudioStream> wrappedAudioStreams = StreamSizeWrapper.empty(); StreamSizeWrapper<AudioStream> wrappedAudioStreams;
@State @State
public StreamSizeWrapper<VideoStream> wrappedVideoStreams = StreamSizeWrapper.empty(); StreamSizeWrapper<VideoStream> wrappedVideoStreams;
@State @State
public StreamSizeWrapper<SubtitlesStream> wrappedSubtitleStreams = StreamSizeWrapper.empty(); StreamSizeWrapper<SubtitlesStream> wrappedSubtitleStreams;
@State @State
int selectedVideoIndex = 0; int selectedVideoIndex; // set in the constructor
@State @State
int selectedAudioIndex = 0; int selectedAudioIndex = 0; // default to the first item
@State @State
int selectedSubtitleIndex = 0; int selectedSubtitleIndex = 0; // default to the first item
@Nullable @Nullable
private OnDismissListener onDismissListener = null; private OnDismissListener onDismissListener = null;
@ -140,116 +140,48 @@ public class DownloadDialog extends DialogFragment
registerForActivityResult( registerForActivityResult(
new StartActivityForResult(), this::requestDownloadPickVideoFolderResult); new StartActivityForResult(), this::requestDownloadPickVideoFolderResult);
/*////////////////////////////////////////////////////////////////////////// /*//////////////////////////////////////////////////////////////////////////
// Instance creation // Instance creation
//////////////////////////////////////////////////////////////////////////*/ //////////////////////////////////////////////////////////////////////////*/
@NonNull /**
public static DownloadDialog newInstance(final Context context, * Create a new download dialog with the video, audio and subtitle streams from the provided
@NonNull final StreamInfo info) { * stream info. Video streams and video-only streams will be put into a single list menu,
// TODO: Adapt this code when the downloader support other types of stream deliveries * sorted according to their resolution and the default video resolution will be selected.
final List<VideoStream> progressiveHttpVideoStreams = *
getStreamsOfSpecifiedDelivery(info.getVideoStreams(), * @param context the context to use just to obtain preferences and strings (will not be stored)
DeliveryMethod.PROGRESSIVE_HTTP); * @param info the info from which to obtain downloadable streams and other info (e.g. title)
*/
final List<VideoStream> progressiveHttpVideoOnlyStreams = public DownloadDialog(final Context context, @NonNull final StreamInfo info) {
getStreamsOfSpecifiedDelivery(info.getVideoOnlyStreams(),
DeliveryMethod.PROGRESSIVE_HTTP);
final List<AudioStream> progressiveHttpAudioStreams =
getStreamsOfSpecifiedDelivery(info.getAudioStreams(),
DeliveryMethod.PROGRESSIVE_HTTP);
final List<SubtitlesStream> progressiveHttpSubtitlesStreams =
getStreamsOfSpecifiedDelivery(info.getSubtitles(),
DeliveryMethod.PROGRESSIVE_HTTP);
final List<VideoStream> 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) {
this.currentInfo = info; this.currentInfo = info;
}
public void setAudioStreams(@NonNull final List<AudioStream> audioStreams) { // TODO: Adapt this code when the downloader support other types of stream deliveries
this.wrappedAudioStreams = new StreamSizeWrapper<>(audioStreams, getContext()); final List<VideoStream> videoStreams = ListHelper.getSortedStreamVideosList(
} context,
getStreamsOfSpecifiedDelivery(info.getVideoStreams(), PROGRESSIVE_HTTP),
getStreamsOfSpecifiedDelivery(info.getVideoOnlyStreams(), PROGRESSIVE_HTTP),
false,
false
);
public void setVideoStreams(@NonNull final List<VideoStream> videoStreams) { this.wrappedVideoStreams = new StreamSizeWrapper<>(videoStreams, context);
this.wrappedVideoStreams = new StreamSizeWrapper<>(videoStreams, getContext()); 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<SubtitlesStream> subtitleStreams) { this.selectedVideoIndex = ListHelper.getDefaultResolutionIndex(context, videoStreams);
this.wrappedSubtitleStreams = new StreamSizeWrapper<>(subtitleStreams, getContext());
} }
/** /**
* Set the selected video stream, by using its index in the stream list. * @param onDismissListener the listener to call in {@link #onDismiss(DialogInterface)}
*
* 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}
*/ */
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<? extends Stream> wrappedStreams) {
return selectedIndexStream > 0
&& selectedIndexStream < wrappedStreams.getStreamsList().size();
}
public void setOnDismissListener(@Nullable final OnDismissListener onDismissListener) { public void setOnDismissListener(@Nullable final OnDismissListener onDismissListener) {
this.onDismissListener = onDismissListener; this.onDismissListener = onDismissListener;
} }
/*////////////////////////////////////////////////////////////////////////// /*//////////////////////////////////////////////////////////////////////////
// Android lifecycle // Android lifecycle
//////////////////////////////////////////////////////////////////////////*/ //////////////////////////////////////////////////////////////////////////*/
@ -754,13 +686,9 @@ public class DownloadDialog extends DialogFragment
if (format == MediaFormat.WEBMA_OPUS) { if (format == MediaFormat.WEBMA_OPUS) {
mimeTmp = "audio/ogg"; mimeTmp = "audio/ogg";
filenameTmp += "opus"; filenameTmp += "opus";
} else { } else if (format != null) {
if (format != null) { mimeTmp = format.mimeType;
mimeTmp = format.mimeType; filenameTmp += format.suffix;
}
if (format != null) {
filenameTmp += format.suffix;
}
} }
break; break;
case R.id.video_button: case R.id.video_button:
@ -769,8 +697,6 @@ public class DownloadDialog extends DialogFragment
format = videoStreamsAdapter.getItem(selectedVideoIndex).getFormat(); format = videoStreamsAdapter.getItem(selectedVideoIndex).getFormat();
if (format != null) { if (format != null) {
mimeTmp = format.mimeType; mimeTmp = format.mimeType;
}
if (format != null) {
filenameTmp += format.suffix; filenameTmp += format.suffix;
} }
break; break;
@ -1085,7 +1011,7 @@ public class DownloadDialog extends DialogFragment
new MissionRecoveryInfo(selectedStream) new MissionRecoveryInfo(selectedStream)
}; };
} else { } else {
if (secondaryStream.getDeliveryMethod() != DeliveryMethod.PROGRESSIVE_HTTP) { if (secondaryStream.getDeliveryMethod() != PROGRESSIVE_HTTP) {
throw new IllegalArgumentException("Unsupported stream delivery format" throw new IllegalArgumentException("Unsupported stream delivery format"
+ secondaryStream.getDeliveryMethod()); + secondaryStream.getDeliveryMethod());
} }

View File

@ -1689,11 +1689,7 @@ public final class VideoDetailFragment
} }
try { try {
final DownloadDialog downloadDialog = DownloadDialog.newInstance(activity, final DownloadDialog downloadDialog = new DownloadDialog(activity, currentInfo);
currentInfo);
downloadDialog.setSelectedVideoStream(ListHelper.getDefaultResolutionIndex(activity,
downloadDialog.wrappedVideoStreams.getStreamsList()));
downloadDialog.show(activity.getSupportFragmentManager(), "downloadDialog"); downloadDialog.show(activity.getSupportFragmentManager(), "downloadDialog");
} catch (final Exception e) { } catch (final Exception e) {
ErrorUtil.showSnackbar(activity, new ErrorInfo(e, UserAction.DOWNLOAD_OPEN_DIALOG, ErrorUtil.showSnackbar(activity, new ErrorInfo(e, UserAction.DOWNLOAD_OPEN_DIALOG,