Better implementation of old code

This commit is contained in:
Avently 2020-02-29 02:57:54 +03:00
parent 6d7e37610c
commit 5c2ff9b777
5 changed files with 55 additions and 52 deletions

View File

@ -8,7 +8,7 @@ class StackItem implements Serializable {
private final int serviceId; private final int serviceId;
private String title; private String title;
private String url; private String url;
private final PlayQueue playQueue; private PlayQueue playQueue;
StackItem(int serviceId, String url, String title, PlayQueue playQueue) { StackItem(int serviceId, String url, String title, PlayQueue playQueue) {
this.serviceId = serviceId; this.serviceId = serviceId;
@ -25,6 +25,10 @@ class StackItem implements Serializable {
this.url = url; this.url = url;
} }
public void setPlayQueue(PlayQueue queue) {
this.playQueue = queue;
}
public int getServiceId() { public int getServiceId() {
return serviceId; return serviceId;
} }

View File

@ -99,8 +99,6 @@ public class VideoDetailFragment
PlayerServiceEventListener { PlayerServiceEventListener {
public static final String AUTO_PLAY = "auto_play"; public static final String AUTO_PLAY = "auto_play";
private boolean isFragmentStopped;
private int updateFlags = 0; private int updateFlags = 0;
private static final int RELATED_STREAMS_UPDATE_FLAG = 0x1; private static final int RELATED_STREAMS_UPDATE_FLAG = 0x1;
private static final int RESOLUTIONS_MENU_UPDATE_FLAG = 0x2; private static final int RESOLUTIONS_MENU_UPDATE_FLAG = 0x2;
@ -109,8 +107,10 @@ public class VideoDetailFragment
private static final float MAX_OVERLAY_ALPHA = 0.9f; private static final float MAX_OVERLAY_ALPHA = 0.9f;
private static final float MAX_PLAYER_HEIGHT = 0.7f; private static final float MAX_PLAYER_HEIGHT = 0.7f;
public static final String ACTION_SHOW_MAIN_PLAYER = "org.schabi.newpipe.fragments.VideoDetailFragment.ACTION_SHOW_MAIN_PLAYER"; public static final String ACTION_SHOW_MAIN_PLAYER = "org.schabi.newpipe.VideoDetailFragment.ACTION_SHOW_MAIN_PLAYER";
public static final String ACTION_HIDE_MAIN_PLAYER = "org.schabi.newpipe.fragments.VideoDetailFragment.ACTION_HIDE_MAIN_PLAYER"; public static final String ACTION_HIDE_MAIN_PLAYER = "org.schabi.newpipe.VideoDetailFragment.ACTION_HIDE_MAIN_PLAYER";
public static final String ACTION_VIDEO_FRAGMENT_RESUMED = "org.schabi.newpipe.VideoDetailFragment.ACTION_VIDEO_FRAGMENT_RESUMED";
public static final String ACTION_VIDEO_FRAGMENT_STOPPED = "org.schabi.newpipe.VideoDetailFragment.ACTION_VIDEO_FRAGMENT_STOPPED";
private boolean showRelatedStreams; private boolean showRelatedStreams;
private boolean showComments; private boolean showComments;
@ -233,15 +233,13 @@ public class VideoDetailFragment
startPlayerListener(); startPlayerListener();
// It will do nothing if the player is not in fullscreen mode // It will do nothing if the player is not in fullscreen mode
hideSystemUIIfNeeded(); hideSystemUiIfNeeded();
if (!player.videoPlayerSelected() && !playAfterConnect) return; if (!player.videoPlayerSelected() && !playAfterConnect) return;
// STATE_IDLE means the player is stopped if (playerIsNotStopped()) addVideoPlayerView();
if (player.getPlayer() != null && player.getPlayer().getPlaybackState() != Player.STATE_IDLE) addVideoPlayerView();
// If the video is playing but orientation changed let's make the video in fullscreen again // If the video is playing but orientation changed let's make the video in fullscreen again
if (isLandscape()) checkLandscape(); if (isLandscape()) checkLandscape();
else if (player.isInFullscreen()) player.toggleFullscreen(); else if (player.isInFullscreen()) player.toggleFullscreen();
@ -363,7 +361,7 @@ public class VideoDetailFragment
public void onResume() { public void onResume() {
super.onResume(); super.onResume();
isFragmentStopped = false; activity.sendBroadcast(new Intent(ACTION_VIDEO_FRAGMENT_RESUMED));
setupBrightness(false); setupBrightness(false);
@ -383,20 +381,16 @@ public class VideoDetailFragment
} }
// Check if it was loading when the fragment was stopped/paused // Check if it was loading when the fragment was stopped/paused
if (wasLoading.getAndSet(false) && !wasCleared()) { if (wasLoading.getAndSet(false) && !wasCleared())
selectAndLoadVideo(serviceId, url, name, playQueue); selectAndLoadVideo(serviceId, url, name, playQueue);
} else if (currentInfo != null) {
updateProgressInfo(currentInfo);
}
if (player != null && player.videoPlayerSelected()) addVideoPlayerView();
} }
@Override @Override
public void onStop() { public void onStop() {
super.onStop(); super.onStop();
isFragmentStopped = true; if (!activity.isChangingConfigurations())
activity.sendBroadcast(new Intent(ACTION_VIDEO_FRAGMENT_STOPPED));
} }
@Override @Override
@ -546,7 +540,7 @@ public class VideoDetailFragment
bottomSheetBehavior.setState(BottomSheetBehavior.STATE_EXPANDED); bottomSheetBehavior.setState(BottomSheetBehavior.STATE_EXPANDED);
break; break;
case R.id.overlay_play_pause_button: case R.id.overlay_play_pause_button:
if (player != null && player.getPlayer() != null && player.getPlayer().getPlaybackState() != Player.STATE_IDLE) { if (playerIsNotStopped()) {
player.onPlayPause(); player.onPlayPause();
player.hideControls(0,0); player.hideControls(0,0);
showSystemUi(); showSystemUi();
@ -1632,8 +1626,14 @@ public class VideoDetailFragment
playQueue = queue; playQueue = queue;
// This should be the only place where we push data to stack. It will allow to have live instance of PlayQueue with actual // This should be the only place where we push data to stack. It will allow to have live instance of PlayQueue with actual
// information about deleted/added items inside Channel/Playlist queue and makes possible to have a history of played items // information about deleted/added items inside Channel/Playlist queue and makes possible to have a history of played items
if (stack.isEmpty() || !stack.peek().getPlayQueue().equals(queue)) if (stack.isEmpty() || !stack.peek().getPlayQueue().equals(queue)) {
stack.push(new StackItem(serviceId, url, name, playQueue)); stack.push(new StackItem(serviceId, url, name, playQueue));
} else if (stack.peek().getPlayQueue().equals(queue)) {
// On every MainPlayer service's destroy() playQueue gets disposed and no longer able to track progress.
// That's why we update our cached disposed queue with the new one that is active and have the same history
// Without that the cached playQueue will have an old recovery position
stack.peek().setPlayQueue(queue);
}
if (DEBUG) { if (DEBUG) {
Log.d(TAG, "onQueueUpdate() called with: serviceId = [" Log.d(TAG, "onQueueUpdate() called with: serviceId = ["
@ -1668,21 +1668,6 @@ public class VideoDetailFragment
if (player.getPlayQueue().getItem().getUrl().equals(url)) if (player.getPlayQueue().getItem().getUrl().equals(url))
showPlaybackProgress(currentProgress, duration); showPlaybackProgress(currentProgress, duration);
// We don't want to interrupt playback and don't want to see notification if player is stopped
// since next lines of code will enable background playback if needed
if (!player.videoPlayerSelected()) return;
// This will be called when user goes to another app
if (isFragmentStopped) {
// Video enabled. Let's think what to do with source in background
if (player.backgroundPlaybackEnabled())
player.useVideoSource(false);
else if (player.minimizeOnPopupEnabled())
NavigationHelper.playOnPopupPlayer(activity, playQueue, true);
else player.onPause();
}
else player.useVideoSource(true);
} }
@Override @Override
@ -1731,7 +1716,7 @@ public class VideoDetailFragment
if (parent == null) return; if (parent == null) return;
if (fullscreen) { if (fullscreen) {
hideSystemUIIfNeeded(); hideSystemUiIfNeeded();
} else { } else {
showSystemUi(); showSystemUi();
} }
@ -1776,11 +1761,6 @@ public class VideoDetailFragment
valueAnimator.start(); valueAnimator.start();
} }
@Override
public boolean isFragmentStopped() {
return isFragmentStopped;
}
/*////////////////////////////////////////////////////////////////////////// /*//////////////////////////////////////////////////////////////////////////
// Player related utils // Player related utils
//////////////////////////////////////////////////////////////////////////*/ //////////////////////////////////////////////////////////////////////////*/
@ -1811,11 +1791,15 @@ public class VideoDetailFragment
} }
// Listener implementation // Listener implementation
public void hideSystemUIIfNeeded() { public void hideSystemUiIfNeeded() {
if (player != null && player.isInFullscreen() && bottomSheetBehavior.getState() == BottomSheetBehavior.STATE_EXPANDED) if (player != null && player.isInFullscreen() && bottomSheetBehavior.getState() == BottomSheetBehavior.STATE_EXPANDED)
hideSystemUi(); hideSystemUi();
} }
private boolean playerIsNotStopped() {
return player != null && player.getPlayer() != null && player.getPlayer().getPlaybackState() != Player.STATE_IDLE;
}
private void setupBrightness(boolean save) { private void setupBrightness(boolean save) {
if (activity == null) return; if (activity == null) return;
@ -1916,7 +1900,7 @@ public class VideoDetailFragment
bottomSheetBehavior.setPeekHeight(peekHeight); bottomSheetBehavior.setPeekHeight(peekHeight);
// Disable click because overlay buttons located on top of buttons from the player // Disable click because overlay buttons located on top of buttons from the player
setOverlayElementsClickable(false); setOverlayElementsClickable(false);
hideSystemUIIfNeeded(); hideSystemUiIfNeeded();
boolean needToExpand = isLandscape() boolean needToExpand = isLandscape()
&& player != null && player != null
&& player.isPlaying() && player.isPlaying()

View File

@ -177,6 +177,7 @@ public final class MainPlayer extends Service {
if (playerImpl != null) { if (playerImpl != null) {
removeViewFromParent(); removeViewFromParent();
playerImpl.setRecovery();
playerImpl.savePlaybackState(); playerImpl.savePlaybackState();
playerImpl.stopActivityBinding(); playerImpl.stopActivityBinding();
playerImpl.removePopupFromView(); playerImpl.removePopupFromView();

View File

@ -999,6 +999,9 @@ public class VideoPlayerImpl extends VideoPlayer
intentFilter.addAction(ACTION_FAST_REWIND); intentFilter.addAction(ACTION_FAST_REWIND);
intentFilter.addAction(ACTION_FAST_FORWARD); intentFilter.addAction(ACTION_FAST_FORWARD);
intentFilter.addAction(VideoDetailFragment.ACTION_VIDEO_FRAGMENT_RESUMED);
intentFilter.addAction(VideoDetailFragment.ACTION_VIDEO_FRAGMENT_STOPPED);
intentFilter.addAction(Intent.ACTION_CONFIGURATION_CHANGED); intentFilter.addAction(Intent.ACTION_CONFIGURATION_CHANGED);
intentFilter.addAction(Intent.ACTION_SCREEN_ON); intentFilter.addAction(Intent.ACTION_SCREEN_ON);
intentFilter.addAction(Intent.ACTION_SCREEN_OFF); intentFilter.addAction(Intent.ACTION_SCREEN_OFF);
@ -1036,6 +1039,24 @@ public class VideoPlayerImpl extends VideoPlayer
case ACTION_REPEAT: case ACTION_REPEAT:
onRepeatClicked(); onRepeatClicked();
break; break;
case VideoDetailFragment.ACTION_VIDEO_FRAGMENT_RESUMED:
useVideoSource(true);
break;
case VideoDetailFragment.ACTION_VIDEO_FRAGMENT_STOPPED:
// This will be called when user goes to another app
// We don't want to interrupt playback and don't want to see notification if player is stopped
// since next lines of code will enable background playback if needed
if (videoPlayerSelected() && isPlaying()) {
if (backgroundPlaybackEnabled()) {
useVideoSource(false);
} else if (minimizeOnPopupEnabled()) {
setRecovery();
NavigationHelper.playOnPopupPlayer(getParentActivity(), playQueue, true);
} else {
onPause();
}
}
break;
case Intent.ACTION_CONFIGURATION_CHANGED: case Intent.ACTION_CONFIGURATION_CHANGED:
// The only situation I need to re-calculate elements sizes is when a user rotates a device from landscape to landscape // The only situation I need to re-calculate elements sizes is when a user rotates a device from landscape to landscape
// because in that case the controls should be aligned to another side of a screen. The problem is when user leaves // because in that case the controls should be aligned to another side of a screen. The problem is when user leaves
@ -1223,7 +1244,7 @@ public class VideoPlayerImpl extends VideoPlayer
@Override @Override
public void hideSystemUIIfNeeded() { public void hideSystemUIIfNeeded() {
if (fragmentListener != null) if (fragmentListener != null)
fragmentListener.hideSystemUIIfNeeded(); fragmentListener.hideSystemUiIfNeeded();
} }
/** /**
@ -1327,12 +1348,7 @@ public class VideoPlayerImpl extends VideoPlayer
} }
public void useVideoSource(boolean video) { public void useVideoSource(boolean video) {
// Return when: old value of audioOnly equals to the new value, audio player is selected, if (playQueue == null || audioOnly == !video || audioPlayerSelected())
// video player is selected AND fragment is not shown
if (playQueue == null
|| audioOnly == !video
|| audioPlayerSelected()
|| (video && videoPlayerSelected() && fragmentListener.isFragmentStopped()))
return; return;
audioOnly = !video; audioOnly = !video;

View File

@ -11,7 +11,5 @@ public interface PlayerServiceEventListener extends PlayerEventListener {
void onPlayerError(ExoPlaybackException error); void onPlayerError(ExoPlaybackException error);
boolean isFragmentStopped(); void hideSystemUiIfNeeded();
void hideSystemUIIfNeeded();
} }