Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Add download option to long-press menu #6563

Closed
7 changes: 6 additions & 1 deletion app/src/main/java/org/schabi/newpipe/MainActivity.java
Original file line number Diff line number Diff line change
Expand Up @@ -65,6 +65,7 @@
import org.schabi.newpipe.extractor.StreamingService;
import org.schabi.newpipe.extractor.exceptions.ExtractionException;
import org.schabi.newpipe.extractor.services.peertube.PeertubeInstance;
import org.schabi.newpipe.extractor.stream.StreamInfo;
import org.schabi.newpipe.fragments.BackPressable;
import org.schabi.newpipe.fragments.MainFragment;
import org.schabi.newpipe.fragments.detail.VideoDetailFragment;
Expand Down Expand Up @@ -617,7 +618,11 @@ public void onRequestPermissionsResult(final int requestCode,
final Fragment fragment = getSupportFragmentManager()
.findFragmentById(R.id.fragment_player_holder);
if (fragment instanceof VideoDetailFragment) {
((VideoDetailFragment) fragment).openDownloadDialog();
final StreamInfo currentInfo =
((VideoDetailFragment) fragment).getCurrentStreamInfo();
if (currentInfo != null) {
NavigationHelper.openDownloadDialog(this, currentInfo);
}
}
break;
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -449,7 +449,7 @@ public void onClick(final View v) {
case R.id.detail_controls_download:
if (PermissionHelper.checkStoragePermissions(activity,
PermissionHelper.DOWNLOAD_DIALOG_REQUEST_CODE)) {
this.openDownloadDialog();
NavigationHelper.openDownloadDialog(activity, currentInfo);
}
break;
case R.id.detail_controls_share:
Expand Down Expand Up @@ -1605,6 +1605,10 @@ private void displayBothUploaderAndSubChannel(final StreamInfo info) {
}
}

public StreamInfo getCurrentStreamInfo() {
return currentInfo;
}

public void openDownloadDialog() {
if (currentInfo == null) {
return;
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -371,6 +371,7 @@ protected void showStreamDialog(final StreamInfoItem item) {
));
}
entries.add(StreamDialogEntry.open_in_browser);
entries.add(StreamDialogEntry.download);
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Have you checked if the download option can be added to other places where StreamDialogEntry is used?

Copy link
Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

On it. I'll check it out.

Copy link
Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Not sure about what I'm supposed to do here. @Stypox please help me understand what I should check

Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I just made a similar change in #7160. You add your entry in BaseListFragment, but there are a bunch of other locations that have similar code (adding options to the long-press menu for stream items).

Do a search for add(StreamDialogEntry. and you should turn them all up.

You'll also want to test that it actually works in each other place, because some of those places may have different data available. In most cases besides BaseListFragment, the name of the fragment is a good indicator of where to find it in the app.

Copy link
Member

@Stypox Stypox Nov 13, 2021

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

@gitryder Android Studio tells me that StreamDialogEntry is used in these places:
image

if (KoreUtils.shouldShowPlayWithKodi(context, item.getServiceId())) {
entries.add(StreamDialogEntry.play_with_kodi);
}
Expand Down
38 changes: 38 additions & 0 deletions app/src/main/java/org/schabi/newpipe/util/NavigationHelper.java
Original file line number Diff line number Diff line change
Expand Up @@ -26,6 +26,10 @@
import org.schabi.newpipe.about.AboutActivity;
import org.schabi.newpipe.database.feed.model.FeedGroupEntity;
import org.schabi.newpipe.download.DownloadActivity;
import org.schabi.newpipe.download.DownloadDialog;
import org.schabi.newpipe.error.ErrorActivity;
import org.schabi.newpipe.error.ErrorInfo;
import org.schabi.newpipe.error.UserAction;
import org.schabi.newpipe.extractor.NewPipe;
import org.schabi.newpipe.extractor.StreamingService;
import org.schabi.newpipe.extractor.exceptions.ExtractionException;
Expand Down Expand Up @@ -56,6 +60,7 @@
import org.schabi.newpipe.util.external_communication.ShareUtils;

import java.util.ArrayList;
import java.util.List;

import static org.schabi.newpipe.util.external_communication.ShareUtils.installApp;

Expand Down Expand Up @@ -542,6 +547,39 @@ public static Intent getPlayQueueActivityIntent(final Context context) {
return intent;
}

public static void openDownloadDialog(
final AppCompatActivity activity, final StreamInfo currentInfo) {
if (currentInfo == null) {
return;
}

try {
// Get the sortedVideoStreams and selectedVideoStreamIndex using ListHelper
final List<VideoStream> sortedVideoStreams = new ArrayList<>(
ListHelper.getSortedStreamVideosList(
activity.getApplicationContext(),
currentInfo.getVideoStreams(),
currentInfo.getVideoOnlyStreams(),
false
)
);
final int selectedVideoStreamIndex = ListHelper.getDefaultResolutionIndex(
activity.getApplicationContext(), sortedVideoStreams);

final DownloadDialog downloadDialog = DownloadDialog.newInstance(currentInfo);
downloadDialog.setVideoStreams(sortedVideoStreams);
downloadDialog.setAudioStreams(currentInfo.getAudioStreams());
downloadDialog.setSelectedVideoStream(selectedVideoStreamIndex);
downloadDialog.setSubtitleStreams(currentInfo.getSubtitles());

downloadDialog.show(activity.getSupportFragmentManager(), "downloadDialog");
} catch (final Exception e) {
ErrorActivity.reportErrorInSnackbar(activity,
new ErrorInfo(e, UserAction.DOWNLOAD_OPEN_DIALOG, "Showing download dialog",
currentInfo));
}
}

/*//////////////////////////////////////////////////////////////////////////
// Link handling
//////////////////////////////////////////////////////////////////////////*/
Expand Down
19 changes: 19 additions & 0 deletions app/src/main/java/org/schabi/newpipe/util/StreamDialogEntry.java
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,7 @@
import android.content.Context;
import android.net.Uri;

import androidx.appcompat.app.AppCompatActivity;
import androidx.fragment.app.Fragment;

import org.schabi.newpipe.R;
Expand All @@ -18,6 +19,9 @@
import java.util.Collections;
import java.util.List;

import io.reactivex.rxjava3.android.schedulers.AndroidSchedulers;
import io.reactivex.rxjava3.schedulers.Schedulers;

import static org.schabi.newpipe.player.MainPlayer.PlayerType.AUDIO;
import static org.schabi.newpipe.player.MainPlayer.PlayerType.POPUP;

Expand All @@ -33,6 +37,21 @@ public enum StreamDialogEntry {
item.getServiceId(), item.getUploaderUrl(), item.getUploaderName())
),

download(R.string.download, (fragment, item) -> {
final int serviceId = item.getServiceId();
final String url = item.getUrl();

ExtractorHelper.getStreamInfo(serviceId, url, true)
.subscribeOn(Schedulers.io())
.observeOn(AndroidSchedulers.mainThread())
.subscribe(result -> {
if (PermissionHelper.checkStoragePermissions(fragment.getActivity(),
PermissionHelper.DOWNLOAD_DIALOG_REQUEST_CODE)) {
NavigationHelper.openDownloadDialog(
(AppCompatActivity) fragment.getActivity(), result);
}
});
AudricV marked this conversation as resolved.
Show resolved Hide resolved
}),
/**
* Enqueues the stream automatically to the current PlayerType.<br>
* <br>
Expand Down