Skip to content

Commit

Permalink
More fixes with opening VideoDetailFragment
Browse files Browse the repository at this point in the history
  • Loading branch information
Stypox committed Nov 8, 2020
1 parent bb882ad commit 2a2c82e
Show file tree
Hide file tree
Showing 13 changed files with 87 additions and 170 deletions.
2 changes: 1 addition & 1 deletion app/src/main/java/org/schabi/newpipe/RouterActivity.java
Original file line number Diff line number Diff line change
Expand Up @@ -685,7 +685,7 @@ public Consumer<Info> getResultHandler(final Choice choice) {
}

if (choice.playerChoice.equals(videoPlayerKey)) {
NavigationHelper.playOnMainPlayer(this, playQueue);
NavigationHelper.playOnMainPlayer(this, playQueue, false);
} else if (choice.playerChoice.equals(backgroundPlayerKey)) {
NavigationHelper.playOnBackgroundPlayer(this, playQueue, true);
} else if (choice.playerChoice.equals(popupPlayerKey)) {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -1097,7 +1097,7 @@ private void openNormalBackgroundPlayer(final boolean append) {

private void openMainPlayer() {
if (playerService == null) {
PlayerHolder.startService(App.getApp(), true, this);
PlayerHolder.startService(App.getApp(), autoPlayEnabled, this);
return;
}
if (currentInfo == null) {
Expand All @@ -1112,7 +1112,7 @@ private void openMainPlayer() {
addVideoPlayerView();

final Intent playerIntent = NavigationHelper
.getPlayerIntent(requireContext(), MainPlayer.class, queue, null, true);
.getPlayerIntent(requireContext(), MainPlayer.class, queue, true, autoPlayEnabled);
activity.startService(playerIntent);
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -2,11 +2,8 @@

import android.content.Intent;
import android.view.Menu;
import android.view.MenuItem;

import org.schabi.newpipe.R;
import org.schabi.newpipe.util.NavigationHelper;
import org.schabi.newpipe.util.PermissionHelper;

public final class BackgroundPlayerActivity extends ServicePlayerActivity {

Expand Down Expand Up @@ -46,31 +43,6 @@ public int getPlayerOptionMenuResource() {
return R.menu.menu_play_queue_bg;
}

@Override
public boolean onPlayerOptionSelected(final MenuItem item) {
if (item.getItemId() == R.id.action_switch_popup) {

if (!PermissionHelper.isPopupEnabled(this)) {
PermissionHelper.showPopupEnablementToast(this);
return true;
}

this.player.setRecovery();
NavigationHelper.playOnPopupPlayer(
getApplicationContext(), player.playQueue, this.player.isPlaying());
return true;
}

if (item.getItemId() == R.id.action_switch_background) {
this.player.setRecovery();
NavigationHelper.playOnBackgroundPlayer(
getApplicationContext(), player.playQueue, this.player.isPlaying());
return true;
}

return false;
}

@Override
public void setupMenu(final Menu menu) {
if (player == null) {
Expand Down
33 changes: 18 additions & 15 deletions app/src/main/java/org/schabi/newpipe/player/BasePlayer.java
Original file line number Diff line number Diff line change
Expand Up @@ -125,7 +125,7 @@ public abstract class BasePlayer implements
@NonNull
public static final String RESUME_PLAYBACK = "resume_playback";
@NonNull
public static final String START_PAUSED = "start_paused";
public static final String PLAY_WHEN_READY = "play_when_ready";
@NonNull
public static final String SELECT_ON_APPEND = "select_on_append";
@NonNull
Expand Down Expand Up @@ -224,7 +224,7 @@ public void onReceive(final Context ctx, final Intent intent) {
this.dataSource = new PlayerDataSource(context, userAgent, bandwidthMeter);

final TrackSelection.Factory trackSelectionFactory = PlayerHelper
.getQualitySelector(context);
.getQualitySelector();
this.trackSelector = new CustomTrackSelector(context, trackSelectionFactory);

this.loadControl = new LoadController();
Expand Down Expand Up @@ -302,6 +302,7 @@ public void handleIntent(final Intent intent) {
final boolean samePlayQueue = playQueue != null && playQueue.equals(queue);

final int repeatMode = intent.getIntExtra(REPEAT_MODE, getRepeatMode());
final boolean playWhenReady = intent.getBooleanExtra(PLAY_WHEN_READY, true);
final boolean isMuted = intent
.getBooleanExtra(IS_MUTED, simpleExoPlayer != null && isMuted());

Expand All @@ -327,16 +328,20 @@ public void handleIntent(final Intent intent) {
simpleExoPlayer.retry();
}
simpleExoPlayer.seekTo(playQueue.getIndex(), queue.getItem().getRecoveryPosition());
return;
simpleExoPlayer.setPlayWhenReady(playWhenReady);

} else if (samePlayQueue && !playQueue.isDisposed() && simpleExoPlayer != null) {
} else if (simpleExoPlayer != null
&& samePlayQueue
&& playQueue != null
&& !playQueue.isDisposed()) {
// Do not re-init the same PlayQueue. Save time
// Player can have state = IDLE when playback is stopped or failed
// and we should retry() in this case
if (simpleExoPlayer.getPlaybackState() == Player.STATE_IDLE) {
simpleExoPlayer.retry();
}
return;
simpleExoPlayer.setPlayWhenReady(playWhenReady);

} else if (intent.getBooleanExtra(RESUME_PLAYBACK, false)
&& isPlaybackResumeEnabled()
&& !samePlayQueue) {
Expand All @@ -351,32 +356,30 @@ && isPlaybackResumeEnabled()
state -> {
queue.setRecovery(queue.getIndex(), state.getProgressTime());
initPlayback(queue, repeatMode, playbackSpeed, playbackPitch,
playbackSkipSilence, true, isMuted);
playbackSkipSilence, playWhenReady, isMuted);
},
error -> {
if (DEBUG) {
error.printStackTrace();
}
// In case any error we can start playback without history
initPlayback(queue, repeatMode, playbackSpeed, playbackPitch,
playbackSkipSilence, true, isMuted);
playbackSkipSilence, playWhenReady, isMuted);
},
() -> {
// Completed but not found in history
initPlayback(queue, repeatMode, playbackSpeed, playbackPitch,
playbackSkipSilence, true, isMuted);
playbackSkipSilence, playWhenReady, isMuted);
}
);
databaseUpdateReactor.add(stateLoader);
return;
}
} else {
// Good to go...
// In a case of equal PlayQueues we can re-init old one but only when it is disposed
initPlayback(samePlayQueue ? playQueue : queue, repeatMode, playbackSpeed,
playbackPitch, playbackSkipSilence, playWhenReady, isMuted);
}
// Good to go...
// In a case of equal PlayQueues we can re-init old one but only when it is disposed
initPlayback(samePlayQueue ? playQueue : queue, repeatMode,
playbackSpeed, playbackPitch, playbackSkipSilence,
!intent.getBooleanExtra(START_PAUSED, false),
isMuted);
}

private PlaybackParameters retrievePlaybackParametersFromPreferences() {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -240,7 +240,7 @@ public View getView() {
}

public void removeViewFromParent() {
if (getView().getParent() != null) {
if (getView() != null && getView().getParent() != null) {
if (playerImpl.getParentActivity() != null) {
// This means view was added to fragment
final ViewGroup parent = (ViewGroup) getView().getParent();
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -27,9 +27,7 @@
import com.google.android.exoplayer2.PlaybackParameters;
import com.google.android.exoplayer2.Player;

import org.schabi.newpipe.MainActivity;
import org.schabi.newpipe.R;
import org.schabi.newpipe.extractor.StreamingService;
import org.schabi.newpipe.extractor.stream.StreamInfo;
import org.schabi.newpipe.fragments.OnScrollBelowItemsListener;
import org.schabi.newpipe.local.dialog.PlaylistAppendDialog;
Expand All @@ -42,9 +40,9 @@
import org.schabi.newpipe.player.playqueue.PlayQueueItemBuilder;
import org.schabi.newpipe.player.playqueue.PlayQueueItemHolder;
import org.schabi.newpipe.player.playqueue.PlayQueueItemTouchCallback;
import org.schabi.newpipe.util.Constants;
import org.schabi.newpipe.util.Localization;
import org.schabi.newpipe.util.NavigationHelper;
import org.schabi.newpipe.util.PermissionHelper;
import org.schabi.newpipe.util.ThemeHelper;

import java.util.Collections;
Expand Down Expand Up @@ -113,9 +111,8 @@ public abstract class ServicePlayerActivity extends AppCompatActivity

public abstract int getPlayerOptionMenuResource();

public abstract boolean onPlayerOptionSelected(MenuItem item);

public abstract void setupMenu(Menu m);

////////////////////////////////////////////////////////////////////////////
// Activity Lifecycle
////////////////////////////////////////////////////////////////////////////
Expand Down Expand Up @@ -187,12 +184,22 @@ public boolean onOptionsItemSelected(final MenuItem item) {
return true;
case R.id.action_switch_main:
this.player.setRecovery();
getApplicationContext().startActivity(
getSwitchIntent(MainActivity.class, MainPlayer.PlayerType.VIDEO)
.putExtra(BasePlayer.START_PAUSED, !this.player.isPlaying()));
NavigationHelper.playOnMainPlayer(this, player.getPlayQueue(), true);
return true;
case R.id.action_switch_popup:
if (PermissionHelper.isPopupEnabled(this)) {
this.player.setRecovery();
NavigationHelper.playOnPopupPlayer(this, player.playQueue, true);
} else {
PermissionHelper.showPopupEnablementToast(this);
}
return true;
case R.id.action_switch_background:
this.player.setRecovery();
NavigationHelper.playOnBackgroundPlayer(this, player.playQueue, true);
return true;
}
return onPlayerOptionSelected(item) || super.onOptionsItemSelected(item);
return super.onOptionsItemSelected(item);
}

@Override
Expand All @@ -201,24 +208,6 @@ protected void onDestroy() {
unbind();
}

protected Intent getSwitchIntent(final Class clazz, final MainPlayer.PlayerType playerType) {
return NavigationHelper.getPlayerIntent(getApplicationContext(), clazz,
this.player.getPlayQueue(), this.player.getRepeatMode(),
this.player.getPlaybackSpeed(), this.player.getPlaybackPitch(),
this.player.getPlaybackSkipSilence(),
null,
true,
!this.player.isPlaying(),
this.player.isMuted())
.addFlags(Intent.FLAG_ACTIVITY_NEW_TASK)
.putExtra(Constants.KEY_LINK_TYPE, StreamingService.LinkType.STREAM)
.putExtra(Constants.KEY_URL, this.player.getVideoUrl())
.putExtra(Constants.KEY_TITLE, this.player.getVideoTitle())
.putExtra(Constants.KEY_SERVICE_ID,
this.player.getCurrentMetadata().getMetadata().getServiceId())
.putExtra(VideoPlayer.PLAYER_TYPE, playerType);
}

////////////////////////////////////////////////////////////////////////////
// Service Connection
////////////////////////////////////////////////////////////////////////////
Expand Down Expand Up @@ -368,8 +357,8 @@ private void buildItemPopupMenu(final PlayQueueItem item, final View view) {
Menu.NONE, R.string.play_queue_stream_detail);
detail.setOnMenuItemClickListener(menuItem -> {
// playQueue is null since we don't want any queue change
NavigationHelper.openVideoDetail(
this, item.getServiceId(), item.getUrl(), item.getTitle(), null);
NavigationHelper.openVideoDetail(this, item.getServiceId(), item.getUrl(),
item.getTitle(), null, false);
return true;
});

Expand Down
48 changes: 9 additions & 39 deletions app/src/main/java/org/schabi/newpipe/player/VideoPlayerImpl.java
Original file line number Diff line number Diff line change
Expand Up @@ -76,9 +76,7 @@
import com.google.android.material.floatingactionbutton.FloatingActionButton;
import com.nostra13.universalimageloader.core.assist.FailReason;

import org.schabi.newpipe.MainActivity;
import org.schabi.newpipe.R;
import org.schabi.newpipe.extractor.StreamingService;
import org.schabi.newpipe.extractor.stream.StreamInfo;
import org.schabi.newpipe.extractor.stream.VideoStream;
import org.schabi.newpipe.fragments.OnScrollBelowItemsListener;
Expand All @@ -97,7 +95,6 @@
import org.schabi.newpipe.player.resolver.MediaSourceTag;
import org.schabi.newpipe.player.resolver.VideoPlaybackResolver;
import org.schabi.newpipe.util.AnimationUtils;
import org.schabi.newpipe.util.Constants;
import org.schabi.newpipe.util.DeviceUtils;
import org.schabi.newpipe.util.KoreUtil;
import org.schabi.newpipe.util.ListHelper;
Expand Down Expand Up @@ -260,7 +257,12 @@ public void handleIntent(final Intent intent) {
onQueueClosed();
// Android TV: without it focus will frame the whole player
playPauseButton.requestFocus();
onPlay();

if (simpleExoPlayer.getPlayWhenReady()) {
onPlay();
} else {
onPause();
}
}
NavigationHelper.sendPlayerStartedEvent(service);
}
Expand Down Expand Up @@ -756,40 +758,6 @@ public void toggleFullscreen() {
setupScreenRotationButton();
}

public void switchFromPopupToMain() {
if (DEBUG) {
Log.d(TAG, "switchFromPopupToMain() called");
}
if (!popupPlayerSelected() || simpleExoPlayer == null || getCurrentMetadata() == null) {
return;
}

setRecovery();
service.removeViewFromParent();
final Intent intent = NavigationHelper.getPlayerIntent(
service,
MainActivity.class,
this.getPlayQueue(),
this.getRepeatMode(),
this.getPlaybackSpeed(),
this.getPlaybackPitch(),
this.getPlaybackSkipSilence(),
null,
true,
!isPlaying(),
isMuted()
);
intent.addFlags(Intent.FLAG_ACTIVITY_NEW_TASK);
intent.putExtra(Constants.KEY_SERVICE_ID,
getCurrentMetadata().getMetadata().getServiceId());
intent.putExtra(Constants.KEY_LINK_TYPE, StreamingService.LinkType.STREAM);
intent.putExtra(Constants.KEY_URL, getVideoUrl());
intent.putExtra(Constants.KEY_TITLE, getVideoTitle());
intent.putExtra(VideoDetailFragment.KEY_SWITCHING_PLAYERS, true);
service.onDestroy();
context.startActivity(intent);
}

@Override
public void onClick(final View v) {
super.onClick(v);
Expand Down Expand Up @@ -817,7 +785,9 @@ public void onClick(final View v) {
} else if (v.getId() == openInBrowser.getId()) {
onOpenInBrowserClicked();
} else if (v.getId() == fullscreenButton.getId()) {
switchFromPopupToMain();
setRecovery();
NavigationHelper.playOnMainPlayer(context, getPlayQueue(), true);
return;
} else if (v.getId() == screenRotationButton.getId()) {
// Only if it's not a vertical video or vertical video but in landscape with locked
// orientation a screen orientation can be changed automatically
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -63,7 +63,7 @@ abstract class BasePlayerGestureListener(
private var isMovingInPopup = false
private var isResizing = false

private val tossFlingVelocity = PlayerHelper.getTossFlingVelocity(service)
private val tossFlingVelocity = PlayerHelper.getTossFlingVelocity()

// [popup] initial coordinates and distance between fingers
private var initPointerDistance = -1.0
Expand Down Expand Up @@ -104,9 +104,6 @@ abstract class BasePlayerGestureListener(
}

private fun onTouchInPopup(v: View, event: MotionEvent): Boolean {
if (playerImpl == null) {
return false
}
playerImpl.gestureDetector.onTouchEvent(event)
if (event.pointerCount == 2 && !isMovingInPopup && !isResizing) {
if (DEBUG) {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -164,7 +164,7 @@ public void onAnimationEnd(final Animator animation) {

@Override
public void onAudioSessionId(final EventTime eventTime, final int audioSessionId) {
if (!PlayerHelper.isUsingDSP(context)) {
if (!PlayerHelper.isUsingDSP()) {
return;
}

Expand Down
Loading

0 comments on commit 2a2c82e

Please sign in to comment.