Skip to content

Commit

Permalink
Merge pull request #10912 from Stypox/download-fixes
Browse files Browse the repository at this point in the history
Download fixes
  • Loading branch information
Stypox authored Mar 28, 2024
2 parents 2af95cc + 5d9adce commit 3f94e7b
Show file tree
Hide file tree
Showing 3 changed files with 26 additions and 57 deletions.
27 changes: 6 additions & 21 deletions app/src/main/java/org/schabi/newpipe/download/DownloadDialog.java
Original file line number Diff line number Diff line change
Expand Up @@ -7,8 +7,6 @@
import android.app.Activity;
import android.content.ComponentName;
import android.content.Context;
import android.content.DialogInterface;
import android.content.DialogInterface.OnDismissListener;
import android.content.Intent;
import android.content.ServiceConnection;
import android.content.SharedPreferences;
Expand Down Expand Up @@ -113,14 +111,11 @@ public class DownloadDialog extends DialogFragment
@State
int selectedSubtitleIndex = 0; // default to the first item

@Nullable
private OnDismissListener onDismissListener = null;

private StoredDirectoryHelper mainStorageAudio = null;
private StoredDirectoryHelper mainStorageVideo = null;
private DownloadManager downloadManager = null;
private ActionMenuItemView okButton = null;
private Context context;
private Context context = null;
private boolean askForSavePath;

private AudioTrackAdapter audioTrackAdapter;
Expand Down Expand Up @@ -195,13 +190,6 @@ public DownloadDialog(@NonNull final Context context, @NonNull final StreamInfo
this.selectedVideoIndex = ListHelper.getDefaultResolutionIndex(context, videoStreams);
}

/**
* @param onDismissListener the listener to call in {@link #onDismiss(DialogInterface)}
*/
public void setOnDismissListener(@Nullable final OnDismissListener onDismissListener) {
this.onDismissListener = onDismissListener;
}


/*//////////////////////////////////////////////////////////////////////////
// Android lifecycle
Expand All @@ -221,6 +209,8 @@ public void onCreate(@Nullable final Bundle savedInstanceState) {
return;
}

// context will remain null if dismiss() was called above, allowing to check whether the
// dialog is being dismissed in onViewCreated()
context = getContext();

setStyle(STYLE_NO_TITLE, ThemeHelper.getDialogTheme(context));
Expand Down Expand Up @@ -305,6 +295,9 @@ public void onViewCreated(@NonNull final View view,
@Nullable final Bundle savedInstanceState) {
super.onViewCreated(view, savedInstanceState);
dialogBinding = DownloadDialogBinding.bind(view);
if (context == null) {
return; // the dialog is being dismissed, see the call to dismiss() in onCreate()
}

dialogBinding.fileName.setText(FilenameUtils.createFilename(getContext(),
currentInfo.getName()));
Expand Down Expand Up @@ -364,14 +357,6 @@ private void initToolbar(final Toolbar toolbar) {
});
}

@Override
public void onDismiss(@NonNull final DialogInterface dialog) {
super.onDismiss(dialog);
if (onDismissListener != null) {
onDismissListener.onDismiss(dialog);
}
}

@Override
public void onDestroy() {
super.onDestroy();
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -11,7 +11,6 @@
import org.schabi.newpipe.extractor.stream.VideoStream;
import org.schabi.newpipe.util.StreamItemAdapter.StreamInfoWrapper;

import java.util.Comparator;
import java.util.List;

public class SecondaryStreamHelper<T extends Stream> {
Expand Down Expand Up @@ -43,42 +42,27 @@ public static AudioStream getAudioStreamFor(@NonNull final Context context,
@NonNull final List<AudioStream> audioStreams,
@NonNull final VideoStream videoStream) {
final MediaFormat mediaFormat = videoStream.getFormat();
if (mediaFormat == null) {
return null;
}

switch (mediaFormat) {
case WEBM:
case MPEG_4: // Is MPEG-4 DASH?
break;
default:
return null;
}

final boolean m4v = mediaFormat == MediaFormat.MPEG_4;
final boolean isLimitingDataUsage = ListHelper.isLimitingDataUsage(context);

Comparator<AudioStream> comparator = ListHelper.getAudioFormatComparator(
m4v ? MediaFormat.M4A : MediaFormat.WEBMA, isLimitingDataUsage);
int preferredAudioStreamIndex = ListHelper.getAudioIndexByHighestRank(
audioStreams, comparator);

if (preferredAudioStreamIndex == -1) {
if (m4v) {
return null;
}

comparator = ListHelper.getAudioFormatComparator(
MediaFormat.WEBMA_OPUS, isLimitingDataUsage);
preferredAudioStreamIndex = ListHelper.getAudioIndexByHighestRank(
audioStreams, comparator);

if (preferredAudioStreamIndex == -1) {
return null;
}
if (mediaFormat == MediaFormat.WEBM) {
return audioStreams
.stream()
.filter(audioStream -> audioStream.getFormat() == MediaFormat.WEBMA
|| audioStream.getFormat() == MediaFormat.WEBMA_OPUS)
.max(ListHelper.getAudioFormatComparator(MediaFormat.WEBMA,
ListHelper.isLimitingDataUsage(context)))
.orElse(null);

} else if (mediaFormat == MediaFormat.MPEG_4) {
return audioStreams
.stream()
.filter(audioStream -> audioStream.getFormat() == MediaFormat.M4A)
.max(ListHelper.getAudioFormatComparator(MediaFormat.M4A,
ListHelper.isLimitingDataUsage(context)))
.orElse(null);

} else {
return null;
}

return audioStreams.get(preferredAudioStreamIndex);
}

public T getStream() {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -411,7 +411,7 @@ private void startMission(Intent intent) {
mission.threadCount = threads;
mission.source = source;
mission.nearLength = nearLength;
mission.recoveryInfo = recovery.toArray(MissionRecoveryInfo[]::new);
mission.recoveryInfo = recovery.toArray(new MissionRecoveryInfo[0]);

if (ps != null)
ps.setTemporalDir(DownloadManager.pickAvailableTemporalDir(this));
Expand Down

0 comments on commit 3f94e7b

Please sign in to comment.