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 a "add to playlist" option in the share menu #7194

Merged
merged 8 commits into from
Oct 13, 2021
Merged
59 changes: 54 additions & 5 deletions app/src/main/java/org/schabi/newpipe/RouterActivity.java
Original file line number Diff line number Diff line change
@@ -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;
Expand Down Expand Up @@ -56,6 +59,8 @@
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;
Expand All @@ -69,8 +74,8 @@
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;

Expand All @@ -89,9 +94,6 @@
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.
*/
Expand All @@ -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
Expand Down Expand Up @@ -350,7 +353,7 @@ private void showDialog(final List<AdapterChoiceItem> choices) {
.setNegativeButton(R.string.just_once, dialogButtonsClickListener)
.setPositiveButton(R.string.always, dialogButtonsClickListener)
.setOnDismissListener((dialog) -> {
if (!selectionIsDownload) {
if (!selectionIsDownload && !selectionIsAddToPlaylist) {
finish();
}
})
Expand Down Expand Up @@ -446,6 +449,10 @@ private List<AdapterChoiceItem> getChoicesForService(final StreamingService serv
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) {
Expand Down Expand Up @@ -482,6 +489,10 @@ private List<AdapterChoiceItem> getChoicesForService(final StreamingService serv
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) {
Expand Down Expand Up @@ -547,6 +558,12 @@ private void handleChoice(final String selectedChoiceKey) {
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))) {
Expand All @@ -572,6 +589,38 @@ private void handleChoice(final String selectedChoiceKey) {
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)
Expand Down
Original file line number Diff line number Diff line change
@@ -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;
Expand Down Expand Up @@ -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
) {
Expand Down Expand Up @@ -83,6 +88,14 @@ public static PlaylistAppendDialog fromPlayQueueItems(final List<PlayQueueItem>
return dialog;
}

public void setOnDismissListener(@Nullable final OnDismissListener onDismissListener) {
this.onDismissListener = onDismissListener;
}

public OnDismissListener getOnDismissListener() {
return onDismissListener;
}

/*//////////////////////////////////////////////////////////////////////////
// LifeCycle - Creation
//////////////////////////////////////////////////////////////////////////*/
Expand Down Expand Up @@ -141,6 +154,14 @@ public void onDestroyView() {
playlistAdapter = null;
}

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

/*//////////////////////////////////////////////////////////////////////////
// Helper
//////////////////////////////////////////////////////////////////////////*/
Expand All @@ -150,7 +171,12 @@ public void openCreatePlaylistDialog() {
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();
}

Expand Down
Original file line number Diff line number Diff line change
@@ -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;
Expand All @@ -20,6 +22,9 @@
import io.reactivex.rxjava3.android.schedulers.AndroidSchedulers;

public final class PlaylistCreationDialog extends PlaylistDialog {
@Nullable
private OnDismissListener onDismissListener = null;

public static PlaylistCreationDialog newInstance(final List<StreamEntity> streams) {
final PlaylistCreationDialog dialog = new PlaylistCreationDialog();
dialog.setInfo(streams);
Expand All @@ -29,9 +34,22 @@ public static PlaylistCreationDialog newInstance(final List<StreamEntity> stream
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
//////////////////////////////////////////////////////////////////////////*/
Expand Down
3 changes: 3 additions & 0 deletions app/src/main/res/values/settings_keys.xml
Original file line number Diff line number Diff line change
Expand Up @@ -360,6 +360,7 @@
<string name="popup_player_key" translatable="false">popup_player</string>
<string name="download_key" translatable="false">download</string>
<string name="always_ask_open_action_key" translatable="false">always_ask_player</string>
<string name="add_to_playlist_key" translatable="false">add_to_playlist</string>

<string-array name="preferred_open_action_description_list" translatable="false">
<item>@string/show_info</item>
Expand All @@ -368,6 +369,7 @@
<item>@string/popup_player</item>
<item>@string/download</item>
<item>@string/always_ask_open_action</item>
<item>@string/add_to_playlist</item>
</string-array>
<string-array name="preferred_open_action_values_list" translatable="false">
<item>@string/show_info_key</item>
Expand All @@ -376,6 +378,7 @@
<item>@string/popup_player_key</item>
<item>@string/download_key</item>
<item>@string/always_ask_open_action_key</item>
<item>@string/add_to_playlist_key</item>
</string-array>

<!-- Updates -->
Expand Down
1 change: 1 addition & 0 deletions app/src/main/res/values/strings.xml
Original file line number Diff line number Diff line change
Expand Up @@ -433,6 +433,7 @@
<string name="background_player">Background player</string>
<string name="popup_player">Popup player</string>
<string name="always_ask_open_action">Always ask</string>
<string name="add_to_playlist">Add to playlist</string>
litetex marked this conversation as resolved.
Show resolved Hide resolved
<string name="preferred_player_fetcher_notification_title">Getting info…</string>
<string name="preferred_player_fetcher_notification_message">"Loading requested content"</string>
<!-- Local Playlist -->
Expand Down