From 9a0c2c40bd6fa6e5f77fd9ee317ba8ebb881aba8 Mon Sep 17 00:00:00 2001 From: Avently <7953703+avently@users.noreply.github.com> Date: Sun, 27 Sep 2020 06:39:42 +0300 Subject: [PATCH] Refactoring and made the player returning from landscape & fullscreen on vertical video to portrait after clicking on fullscreen button --- .../newpipe/player/VideoPlayerImpl.java | 103 ++++++++++-------- 1 file changed, 56 insertions(+), 47 deletions(-) diff --git a/app/src/main/java/org/schabi/newpipe/player/VideoPlayerImpl.java b/app/src/main/java/org/schabi/newpipe/player/VideoPlayerImpl.java index cbb50f36d..06bc54453 100644 --- a/app/src/main/java/org/schabi/newpipe/player/VideoPlayerImpl.java +++ b/app/src/main/java/org/schabi/newpipe/player/VideoPlayerImpl.java @@ -112,6 +112,7 @@ import static org.schabi.newpipe.player.MainPlayer.ACTION_REPEAT; import static org.schabi.newpipe.player.MainPlayer.NOTIFICATION_ID; import static org.schabi.newpipe.player.helper.PlayerHelper.MinimizeMode.MINIMIZE_ON_EXIT_MODE_BACKGROUND; import static org.schabi.newpipe.player.helper.PlayerHelper.getTimeString; +import static org.schabi.newpipe.player.helper.PlayerHelper.globalScreenOrientationLocked; import static org.schabi.newpipe.util.AnimationUtils.Type.SLIDE_AND_ALPHA; import static org.schabi.newpipe.util.AnimationUtils.animateRotation; import static org.schabi.newpipe.util.AnimationUtils.animateView; @@ -708,62 +709,33 @@ public class VideoPlayerImpl extends VideoPlayer } /*////////////////////////////////////////////////////////////////////////// - // Player Overrides - //////////////////////////////////////////////////////////////////////////*/ + // Player Overrides + //////////////////////////////////////////////////////////////////////////*/ @Override public void toggleFullscreen() { if (DEBUG) { Log.d(TAG, "toggleFullscreen() called"); } - if (simpleExoPlayer == null || getCurrentMetadata() == null) { + if (popupPlayerSelected() + || simpleExoPlayer == null + || getCurrentMetadata() == null + || fragmentListener == null) { return; } - if (popupPlayerSelected()) { - 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.AUTO_PLAY, true); - service.onDestroy(); - context.startActivity(intent); - return; + isFullscreen = !isFullscreen; + if (!isFullscreen) { + // Apply window insets because Android will not do it when orientation changes + // from landscape to portrait (open vertical video to reproduce) + getControlsRoot().setPadding(0, 0, 0, 0); } else { - if (fragmentListener == null) { - return; - } - - isFullscreen = !isFullscreen; - if (!isFullscreen) { - // Apply window insets because Android will not do it when orientation changes - // from landscape to portrait (open vertical video to reproduce) - getControlsRoot().setPadding(0, 0, 0, 0); - } else { - // Android needs tens milliseconds to send new insets but a user is able to see - // how controls changes it's position from `0` to `nav bar height` padding. - // So just hide the controls to hide this visual inconsistency - hideControls(0, 0); - } - fragmentListener.onFullscreenStateChanged(isFullscreen()); + // Android needs tens milliseconds to send new insets but a user is able to see + // how controls changes it's position from `0` to `nav bar height` padding. + // So just hide the controls to hide this visual inconsistency + hideControls(0, 0); } + fragmentListener.onFullscreenStateChanged(isFullscreen()); if (!isFullscreen()) { titleTextView.setVisibility(View.GONE); @@ -777,6 +749,40 @@ public class VideoPlayerImpl extends VideoPlayer 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.AUTO_PLAY, true); + service.onDestroy(); + context.startActivity(intent); + } + @Override public void onClick(final View v) { super.onClick(v); @@ -804,9 +810,12 @@ public class VideoPlayerImpl extends VideoPlayer } else if (v.getId() == openInBrowser.getId()) { onOpenInBrowserClicked(); } else if (v.getId() == fullscreenButton.getId()) { - toggleFullscreen(); + switchFromPopupToMain(); } else if (v.getId() == screenRotationButton.getId()) { - if (!isVerticalVideo) { + // Only if it's not a vertical video or vertical video but in landscape with locked + // orientation a screen orientation can be changed automatically + if (!isVerticalVideo + || (service.isLandscape() && globalScreenOrientationLocked(service))) { fragmentListener.onScreenRotationButtonClicked(); } else { toggleFullscreen();