Skip to content

Commit 7ba7917

Browse files
committed
Refactor creation of DownloadDialog
1 parent e3c2aea commit 7ba7917

3 files changed

Lines changed: 39 additions & 120 deletions

File tree

app/src/main/java/org/schabi/newpipe/RouterActivity.java

Lines changed: 1 addition & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -70,7 +70,6 @@
7070
import org.schabi.newpipe.util.Constants;
7171
import org.schabi.newpipe.util.DeviceUtils;
7272
import org.schabi.newpipe.util.ExtractorHelper;
73-
import org.schabi.newpipe.util.ListHelper;
7473
import org.schabi.newpipe.util.Localization;
7574
import org.schabi.newpipe.util.NavigationHelper;
7675
import org.schabi.newpipe.util.PermissionHelper;
@@ -676,9 +675,7 @@ private void openDownloadDialog() {
676675
.subscribeOn(Schedulers.io())
677676
.observeOn(AndroidSchedulers.mainThread())
678677
.subscribe(result -> {
679-
final DownloadDialog downloadDialog = DownloadDialog.newInstance(this, result);
680-
downloadDialog.setSelectedVideoStream(ListHelper.getDefaultResolutionIndex(
681-
this, downloadDialog.wrappedVideoStreams.getStreamsList()));
678+
final DownloadDialog downloadDialog = new DownloadDialog(this, result);
682679
downloadDialog.setOnDismissListener(dialog -> finish());
683680

684681
final FragmentManager fm = getSupportFragmentManager();

app/src/main/java/org/schabi/newpipe/download/DownloadDialog.java

Lines changed: 37 additions & 111 deletions
Original file line numberDiff line numberDiff line change
@@ -48,7 +48,6 @@
4848
import org.schabi.newpipe.extractor.NewPipe;
4949
import org.schabi.newpipe.extractor.localization.Localization;
5050
import org.schabi.newpipe.extractor.stream.AudioStream;
51-
import org.schabi.newpipe.extractor.stream.DeliveryMethod;
5251
import org.schabi.newpipe.extractor.stream.Stream;
5352
import org.schabi.newpipe.extractor.stream.StreamInfo;
5453
import org.schabi.newpipe.extractor.stream.SubtitlesStream;
@@ -83,6 +82,7 @@
8382
import us.shandian.giga.service.DownloadManagerService.DownloadManagerBinder;
8483
import us.shandian.giga.service.MissionState;
8584

85+
import static org.schabi.newpipe.extractor.stream.DeliveryMethod.PROGRESSIVE_HTTP;
8686
import static org.schabi.newpipe.util.ListHelper.getStreamsOfSpecifiedDelivery;
8787
import static org.schabi.newpipe.util.Localization.assureCorrectAppLanguage;
8888

@@ -94,17 +94,17 @@ public class DownloadDialog extends DialogFragment
9494
@State
9595
StreamInfo currentInfo;
9696
@State
97-
public StreamSizeWrapper<AudioStream> wrappedAudioStreams = StreamSizeWrapper.empty();
97+
StreamSizeWrapper<AudioStream> wrappedAudioStreams;
9898
@State
99-
public StreamSizeWrapper<VideoStream> wrappedVideoStreams = StreamSizeWrapper.empty();
99+
StreamSizeWrapper<VideoStream> wrappedVideoStreams;
100100
@State
101-
public StreamSizeWrapper<SubtitlesStream> wrappedSubtitleStreams = StreamSizeWrapper.empty();
101+
StreamSizeWrapper<SubtitlesStream> wrappedSubtitleStreams;
102102
@State
103-
int selectedVideoIndex = 0;
103+
int selectedVideoIndex; // set in the constructor
104104
@State
105-
int selectedAudioIndex = 0;
105+
int selectedAudioIndex = 0; // default to the first item
106106
@State
107-
int selectedSubtitleIndex = 0;
107+
int selectedSubtitleIndex = 0; // default to the first item
108108

109109
@Nullable
110110
private OnDismissListener onDismissListener = null;
@@ -140,116 +140,48 @@ public class DownloadDialog extends DialogFragment
140140
registerForActivityResult(
141141
new StartActivityForResult(), this::requestDownloadPickVideoFolderResult);
142142

143-
/*//////////////////////////////////////////////////////////////////////////
144-
// Instance creation
145-
//////////////////////////////////////////////////////////////////////////*/
146-
147-
@NonNull
148-
public static DownloadDialog newInstance(final Context context,
149-
@NonNull final StreamInfo info) {
150-
// TODO: Adapt this code when the downloader support other types of stream deliveries
151-
final List<VideoStream> progressiveHttpVideoStreams =
152-
getStreamsOfSpecifiedDelivery(info.getVideoStreams(),
153-
DeliveryMethod.PROGRESSIVE_HTTP);
154-
155-
final List<VideoStream> progressiveHttpVideoOnlyStreams =
156-
getStreamsOfSpecifiedDelivery(info.getVideoOnlyStreams(),
157-
DeliveryMethod.PROGRESSIVE_HTTP);
158-
159-
final List<AudioStream> progressiveHttpAudioStreams =
160-
getStreamsOfSpecifiedDelivery(info.getAudioStreams(),
161-
DeliveryMethod.PROGRESSIVE_HTTP);
162-
163-
final List<SubtitlesStream> progressiveHttpSubtitlesStreams =
164-
getStreamsOfSpecifiedDelivery(info.getSubtitles(),
165-
DeliveryMethod.PROGRESSIVE_HTTP);
166-
167-
final List<VideoStream> videoStreamsList = ListHelper.getSortedStreamVideosList(context,
168-
progressiveHttpVideoStreams, progressiveHttpVideoOnlyStreams, false, false);
169-
170-
final DownloadDialog instance = new DownloadDialog();
171-
instance.setInfo(info);
172-
instance.setVideoStreams(videoStreamsList);
173-
instance.setAudioStreams(progressiveHttpAudioStreams);
174-
instance.setSubtitleStreams(progressiveHttpSubtitlesStreams);
175-
176-
return instance;
177-
}
178-
179143

180144
/*//////////////////////////////////////////////////////////////////////////
181-
// Setters
145+
// Instance creation
182146
//////////////////////////////////////////////////////////////////////////*/
183147

184-
private void setInfo(@NonNull final StreamInfo info) {
185-
this.currentInfo = info;
186-
}
187-
188-
public void setAudioStreams(@NonNull final List<AudioStream> audioStreams) {
189-
this.wrappedAudioStreams = new StreamSizeWrapper<>(audioStreams, getContext());
190-
}
191-
192-
public void setVideoStreams(@NonNull final List<VideoStream> videoStreams) {
193-
this.wrappedVideoStreams = new StreamSizeWrapper<>(videoStreams, getContext());
194-
}
195-
196-
public void setSubtitleStreams(@NonNull final List<SubtitlesStream> subtitleStreams) {
197-
this.wrappedSubtitleStreams = new StreamSizeWrapper<>(subtitleStreams, getContext());
198-
}
199-
200148
/**
201-
* Set the selected video stream, by using its index in the stream list.
202-
*
203-
* The index of the select video stream will be not set if this index is not in the bounds
204-
* of the stream list.
149+
* Create a new download dialog with the video, audio and subtitle streams from the provided
150+
* stream info. Video streams and video-only streams will be put into a single list menu,
151+
* sorted according to their resolution and the default video resolution will be selected.
205152
*
206-
* @param svi the index of the selected {@link VideoStream}
153+
* @param context the context to use just to obtain preferences and strings (will not be stored)
154+
* @param info the info from which to obtain downloadable streams and other info (e.g. title)
207155
*/
208-
public void setSelectedVideoStream(final int svi) {
209-
if (selectedStreamIsInBoundsOfWrappedStreams(svi, this.wrappedVideoStreams)) {
210-
this.selectedVideoIndex = svi;
211-
}
212-
}
156+
public DownloadDialog(final Context context, @NonNull final StreamInfo info) {
157+
this.currentInfo = info;
213158

214-
/**
215-
* Set the selected audio stream, by using its index in the stream list.
216-
*
217-
* The index of the select audio stream will be not set if this index is not in the bounds
218-
* of the stream list.
219-
*
220-
* @param sai the index of the selected {@link AudioStream}
221-
*/
222-
public void setSelectedAudioStream(final int sai) {
223-
if (selectedStreamIsInBoundsOfWrappedStreams(sai, this.wrappedAudioStreams)) {
224-
this.selectedAudioIndex = sai;
225-
}
159+
// TODO: Adapt this code when the downloader support other types of stream deliveries
160+
final List<VideoStream> videoStreams = ListHelper.getSortedStreamVideosList(
161+
context,
162+
getStreamsOfSpecifiedDelivery(info.getVideoStreams(), PROGRESSIVE_HTTP),
163+
getStreamsOfSpecifiedDelivery(info.getVideoOnlyStreams(), PROGRESSIVE_HTTP),
164+
false,
165+
false
166+
);
167+
168+
this.wrappedVideoStreams = new StreamSizeWrapper<>(videoStreams, context);
169+
this.wrappedAudioStreams = new StreamSizeWrapper<>(
170+
getStreamsOfSpecifiedDelivery(info.getAudioStreams(), PROGRESSIVE_HTTP), context);
171+
this.wrappedSubtitleStreams = new StreamSizeWrapper<>(
172+
getStreamsOfSpecifiedDelivery(info.getSubtitles(), PROGRESSIVE_HTTP), context);
173+
174+
this.selectedVideoIndex = ListHelper.getDefaultResolutionIndex(context, videoStreams);
226175
}
227176

228177
/**
229-
* Set the selected subtitles stream, by using its index in the stream list.
230-
*
231-
* The index of the select subtitles stream will be not set if this index is not in the bounds
232-
* of the stream list.
233-
*
234-
* @param ssi the index of the selected {@link SubtitlesStream}
178+
* @param onDismissListener the listener to call in {@link #onDismiss(DialogInterface)}
235179
*/
236-
public void setSelectedSubtitleStream(final int ssi) {
237-
if (selectedStreamIsInBoundsOfWrappedStreams(ssi, this.wrappedSubtitleStreams)) {
238-
this.selectedSubtitleIndex = ssi;
239-
}
240-
}
241-
242-
private boolean selectedStreamIsInBoundsOfWrappedStreams(
243-
final int selectedIndexStream,
244-
final StreamSizeWrapper<? extends Stream> wrappedStreams) {
245-
return selectedIndexStream > 0
246-
&& selectedIndexStream < wrappedStreams.getStreamsList().size();
247-
}
248-
249180
public void setOnDismissListener(@Nullable final OnDismissListener onDismissListener) {
250181
this.onDismissListener = onDismissListener;
251182
}
252183

184+
253185
/*//////////////////////////////////////////////////////////////////////////
254186
// Android lifecycle
255187
//////////////////////////////////////////////////////////////////////////*/
@@ -754,13 +686,9 @@ private void prepareSelectedDownload() {
754686
if (format == MediaFormat.WEBMA_OPUS) {
755687
mimeTmp = "audio/ogg";
756688
filenameTmp += "opus";
757-
} else {
758-
if (format != null) {
759-
mimeTmp = format.mimeType;
760-
}
761-
if (format != null) {
762-
filenameTmp += format.suffix;
763-
}
689+
} else if (format != null) {
690+
mimeTmp = format.mimeType;
691+
filenameTmp += format.suffix;
764692
}
765693
break;
766694
case R.id.video_button:
@@ -769,8 +697,6 @@ private void prepareSelectedDownload() {
769697
format = videoStreamsAdapter.getItem(selectedVideoIndex).getFormat();
770698
if (format != null) {
771699
mimeTmp = format.mimeType;
772-
}
773-
if (format != null) {
774700
filenameTmp += format.suffix;
775701
}
776702
break;
@@ -1085,7 +1011,7 @@ private void continueSelectedDownload(@NonNull final StoredFileHelper storage) {
10851011
new MissionRecoveryInfo(selectedStream)
10861012
};
10871013
} else {
1088-
if (secondaryStream.getDeliveryMethod() != DeliveryMethod.PROGRESSIVE_HTTP) {
1014+
if (secondaryStream.getDeliveryMethod() != PROGRESSIVE_HTTP) {
10891015
throw new IllegalArgumentException("Unsupported stream delivery format"
10901016
+ secondaryStream.getDeliveryMethod());
10911017
}

app/src/main/java/org/schabi/newpipe/fragments/detail/VideoDetailFragment.java

Lines changed: 1 addition & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -1689,11 +1689,7 @@ public void openDownloadDialog() {
16891689
}
16901690

16911691
try {
1692-
final DownloadDialog downloadDialog = DownloadDialog.newInstance(activity,
1693-
currentInfo);
1694-
downloadDialog.setSelectedVideoStream(ListHelper.getDefaultResolutionIndex(activity,
1695-
downloadDialog.wrappedVideoStreams.getStreamsList()));
1696-
1692+
final DownloadDialog downloadDialog = new DownloadDialog(activity, currentInfo);
16971693
downloadDialog.show(activity.getSupportFragmentManager(), "downloadDialog");
16981694
} catch (final Exception e) {
16991695
ErrorUtil.showSnackbar(activity, new ErrorInfo(e, UserAction.DOWNLOAD_OPEN_DIALOG,

0 commit comments

Comments
 (0)