From d844e0aba6550e60c417cd663a450ff14c5fcc89 Mon Sep 17 00:00:00 2001 From: Kalle Struik Date: Sat, 2 Oct 2021 19:21:25 +0200 Subject: [PATCH] Add a add to playlist option in the share menu. --- .../org/schabi/newpipe/RouterActivity.java | 59 +++++++++++++++++-- .../local/dialog/PlaylistAppendDialog.java | 28 ++++++++- .../local/dialog/PlaylistCreationDialog.java | 18 ++++++ app/src/main/res/values/settings_keys.xml | 3 + app/src/main/res/values/strings.xml | 1 + 5 files changed, 103 insertions(+), 6 deletions(-) diff --git a/app/src/main/java/org/schabi/newpipe/RouterActivity.java b/app/src/main/java/org/schabi/newpipe/RouterActivity.java index feb9e029d..eba080e62 100644 --- a/app/src/main/java/org/schabi/newpipe/RouterActivity.java +++ b/app/src/main/java/org/schabi/newpipe/RouterActivity.java @@ -1,5 +1,8 @@ package org.schabi.newpipe; +import static org.schabi.newpipe.extractor.StreamingService.ServiceInfo.MediaCapability.AUDIO; +import static org.schabi.newpipe.extractor.StreamingService.ServiceInfo.MediaCapability.VIDEO; + import android.annotation.SuppressLint; import android.app.IntentService; import android.content.Context; @@ -56,6 +59,8 @@ import org.schabi.newpipe.extractor.playlist.PlaylistInfo; import org.schabi.newpipe.extractor.stream.StreamInfo; import org.schabi.newpipe.extractor.stream.VideoStream; import org.schabi.newpipe.ktx.ExceptionUtils; +import org.schabi.newpipe.local.dialog.PlaylistAppendDialog; +import org.schabi.newpipe.local.dialog.PlaylistCreationDialog; import org.schabi.newpipe.player.MainPlayer; import org.schabi.newpipe.player.helper.PlayerHelper; import org.schabi.newpipe.player.helper.PlayerHolder; @@ -69,8 +74,8 @@ import org.schabi.newpipe.util.ExtractorHelper; import org.schabi.newpipe.util.ListHelper; import org.schabi.newpipe.util.NavigationHelper; import org.schabi.newpipe.util.PermissionHelper; -import org.schabi.newpipe.util.external_communication.ShareUtils; import org.schabi.newpipe.util.ThemeHelper; +import org.schabi.newpipe.util.external_communication.ShareUtils; import org.schabi.newpipe.util.urlfinder.UrlFinder; import org.schabi.newpipe.views.FocusOverlayView; @@ -89,9 +94,6 @@ import io.reactivex.rxjava3.disposables.Disposable; import io.reactivex.rxjava3.functions.Consumer; import io.reactivex.rxjava3.schedulers.Schedulers; -import static org.schabi.newpipe.extractor.StreamingService.ServiceInfo.MediaCapability.AUDIO; -import static org.schabi.newpipe.extractor.StreamingService.ServiceInfo.MediaCapability.VIDEO; - /** * Get the url from the intent and open it in the chosen preferred player. */ @@ -107,6 +109,7 @@ public class RouterActivity extends AppCompatActivity { protected String currentUrl; private StreamingService currentService; private boolean selectionIsDownload = false; + private boolean selectionIsAddToPlaylist = false; private AlertDialog alertDialogChoice = null; @Override @@ -350,7 +353,7 @@ public class RouterActivity extends AppCompatActivity { .setNegativeButton(R.string.just_once, dialogButtonsClickListener) .setPositiveButton(R.string.always, dialogButtonsClickListener) .setOnDismissListener((dialog) -> { - if (!selectionIsDownload) { + if (!selectionIsDownload && !selectionIsAddToPlaylist) { finish(); } }) @@ -446,6 +449,10 @@ public class RouterActivity extends AppCompatActivity { final AdapterChoiceItem backgroundPlayer = new AdapterChoiceItem( getString(R.string.background_player_key), getString(R.string.background_player), R.drawable.ic_headset); + final AdapterChoiceItem addToPlaylist = new AdapterChoiceItem( + getString(R.string.add_to_playlist_key), getString(R.string.add_to_playlist), + R.drawable.ic_add); + if (linkType == LinkType.STREAM) { if (isExtVideoEnabled) { @@ -482,6 +489,10 @@ public class RouterActivity extends AppCompatActivity { getString(R.string.download), R.drawable.ic_file_download)); + // Add to playlist is not necessary for CHANNEL and PLAYLIST linkType since those can + // not be added to a playlist + returnList.add(addToPlaylist); + } else { returnList.add(showInfo); if (capabilities.contains(VIDEO) && !isExtVideoEnabled) { @@ -547,6 +558,12 @@ public class RouterActivity extends AppCompatActivity { return; } + if (selectedChoiceKey.equals(getString(R.string.add_to_playlist_key))) { + selectionIsAddToPlaylist = true; + openAddToPlaylistDialog(); + return; + } + // stop and bypass FetcherService if InfoScreen was selected since // StreamDetailFragment can fetch data itself if (selectedChoiceKey.equals(getString(R.string.show_info_key))) { @@ -572,6 +589,38 @@ public class RouterActivity extends AppCompatActivity { finish(); } + private void openAddToPlaylistDialog() { + disposables.add(ExtractorHelper.getStreamInfo(currentServiceId, currentUrl, false) + .subscribeOn(Schedulers.io()) + .observeOn(AndroidSchedulers.mainThread()) + .subscribe(info -> { + final FragmentManager fm = getSupportFragmentManager(); + final PlaylistAppendDialog playlistAppendDialog = PlaylistAppendDialog + .fromStreamInfo(info); + + playlistAppendDialog.setOnDismissListener(dialog -> finish()); + + PlaylistAppendDialog.onPlaylistFound(getThemeWrapperContext(), + () -> { + playlistAppendDialog.show(fm, "addToPlaylistDialog"); + fm.executePendingTransactions(); + }, + () -> { + final PlaylistCreationDialog playlistCreationDialog = + PlaylistCreationDialog.newInstance(playlistAppendDialog); + playlistCreationDialog.show(fm, "addToPlaylistDialog"); + + fm.executePendingTransactions(); + + }); + + }, throwable -> handleError(this, + new ErrorInfo(throwable, UserAction.REQUESTED_STREAM, + "Tried to add " + currentUrl + " to a playlist", + currentService.getServiceId()))) + ); + } + @SuppressLint("CheckResult") private void openDownloadDialog() { disposables.add(ExtractorHelper.getStreamInfo(currentServiceId, currentUrl, true) diff --git a/app/src/main/java/org/schabi/newpipe/local/dialog/PlaylistAppendDialog.java b/app/src/main/java/org/schabi/newpipe/local/dialog/PlaylistAppendDialog.java index 93e1141c7..268848818 100644 --- a/app/src/main/java/org/schabi/newpipe/local/dialog/PlaylistAppendDialog.java +++ b/app/src/main/java/org/schabi/newpipe/local/dialog/PlaylistAppendDialog.java @@ -1,6 +1,8 @@ package org.schabi.newpipe.local.dialog; import android.content.Context; +import android.content.DialogInterface; +import android.content.DialogInterface.OnDismissListener; import android.os.Bundle; import android.view.LayoutInflater; import android.view.View; @@ -40,6 +42,9 @@ public final class PlaylistAppendDialog extends PlaylistDialog { private final CompositeDisposable playlistDisposables = new CompositeDisposable(); + @Nullable + private OnDismissListener onDismissListener = null; + public static Disposable onPlaylistFound( final Context context, final Runnable onSuccess, final Runnable onFailed ) { @@ -83,6 +88,14 @@ public final class PlaylistAppendDialog extends PlaylistDialog { return dialog; } + public void setOnDismissListener(@Nullable final OnDismissListener onDismissListener) { + this.onDismissListener = onDismissListener; + } + + public OnDismissListener getOnDismissListener() { + return onDismissListener; + } + /*////////////////////////////////////////////////////////////////////////// // LifeCycle - Creation //////////////////////////////////////////////////////////////////////////*/ @@ -141,6 +154,14 @@ public final class PlaylistAppendDialog extends PlaylistDialog { playlistAdapter = null; } + @Override + public void onDismiss(@NonNull final DialogInterface dialog) { + super.onDismiss(dialog); + if (onDismissListener != null) { + onDismissListener.onDismiss(dialog); + } + } + /*////////////////////////////////////////////////////////////////////////// // Helper //////////////////////////////////////////////////////////////////////////*/ @@ -150,7 +171,12 @@ public final class PlaylistAppendDialog extends PlaylistDialog { return; } - PlaylistCreationDialog.newInstance(getStreams()).show(getParentFragmentManager(), TAG); + final PlaylistCreationDialog dialog = PlaylistCreationDialog.newInstance(getStreams()); + // Move the dismissListener to the new dialog. + dialog.setOnDismissListener(this.onDismissListener); + this.onDismissListener = null; + + dialog.show(getParentFragmentManager(), TAG); requireDialog().dismiss(); } diff --git a/app/src/main/java/org/schabi/newpipe/local/dialog/PlaylistCreationDialog.java b/app/src/main/java/org/schabi/newpipe/local/dialog/PlaylistCreationDialog.java index f48c72d04..55d6cec89 100644 --- a/app/src/main/java/org/schabi/newpipe/local/dialog/PlaylistCreationDialog.java +++ b/app/src/main/java/org/schabi/newpipe/local/dialog/PlaylistCreationDialog.java @@ -1,6 +1,8 @@ package org.schabi.newpipe.local.dialog; import android.app.Dialog; +import android.content.DialogInterface; +import android.content.DialogInterface.OnDismissListener; import android.os.Bundle; import android.text.InputType; import android.widget.Toast; @@ -20,6 +22,9 @@ import java.util.List; import io.reactivex.rxjava3.android.schedulers.AndroidSchedulers; public final class PlaylistCreationDialog extends PlaylistDialog { + @Nullable + private OnDismissListener onDismissListener = null; + public static PlaylistCreationDialog newInstance(final List streams) { final PlaylistCreationDialog dialog = new PlaylistCreationDialog(); dialog.setInfo(streams); @@ -29,9 +34,22 @@ public final class PlaylistCreationDialog extends PlaylistDialog { public static PlaylistCreationDialog newInstance(final PlaylistAppendDialog appendDialog) { final PlaylistCreationDialog dialog = new PlaylistCreationDialog(); dialog.setInfo(appendDialog.getStreams()); + dialog.setOnDismissListener(appendDialog.getOnDismissListener()); return dialog; } + public void setOnDismissListener(@Nullable final OnDismissListener onDismissListener) { + this.onDismissListener = onDismissListener; + } + + @Override + public void onDismiss(@NonNull final DialogInterface dialog) { + super.onDismiss(dialog); + if (onDismissListener != null) { + onDismissListener.onDismiss(dialog); + } + } + /*////////////////////////////////////////////////////////////////////////// // Dialog //////////////////////////////////////////////////////////////////////////*/ diff --git a/app/src/main/res/values/settings_keys.xml b/app/src/main/res/values/settings_keys.xml index 9261dfae1..4d9927b0d 100644 --- a/app/src/main/res/values/settings_keys.xml +++ b/app/src/main/res/values/settings_keys.xml @@ -360,6 +360,7 @@ popup_player download always_ask_player + add_to_playlist @string/show_info @@ -368,6 +369,7 @@ @string/popup_player @string/download @string/always_ask_open_action + @string/add_to_playlist @string/show_info_key @@ -376,6 +378,7 @@ @string/popup_player_key @string/download_key @string/always_ask_open_action_key + @string/add_to_playlist_key diff --git a/app/src/main/res/values/strings.xml b/app/src/main/res/values/strings.xml index 225ccd126..0497d2660 100644 --- a/app/src/main/res/values/strings.xml +++ b/app/src/main/res/values/strings.xml @@ -433,6 +433,7 @@ Background player Popup player Always ask + Add to playlist Getting info… "Loading requested content"