From 1d935b46f9ca4b62f576e7a3637fb9c45a96017b Mon Sep 17 00:00:00 2001
From: Stypox <stypox@pm.me>
Date: Tue, 27 Jul 2021 11:02:32 +0200
Subject: [PATCH] Open fullscreen when switching from popup to main player

---
 .../fragments/detail/VideoDetailFragment.java     | 15 +++++++++++----
 .../org/schabi/newpipe/util/NavigationHelper.java |  4 +++-
 2 files changed, 14 insertions(+), 5 deletions(-)

diff --git a/app/src/main/java/org/schabi/newpipe/fragments/detail/VideoDetailFragment.java b/app/src/main/java/org/schabi/newpipe/fragments/detail/VideoDetailFragment.java
index 8a5008aed..3894fb71b 100644
--- a/app/src/main/java/org/schabi/newpipe/fragments/detail/VideoDetailFragment.java
+++ b/app/src/main/java/org/schabi/newpipe/fragments/detail/VideoDetailFragment.java
@@ -241,7 +241,7 @@ public final class VideoDetailFragment
                 && isAutoplayEnabled()
                 && player.getParentActivity() == null)) {
             autoPlayEnabled = true; // forcefully start playing
-            openVideoPlayer(true);
+            openVideoPlayerAutoFullscreen();
         }
     }
 
@@ -499,7 +499,7 @@ public final class VideoDetailFragment
                 break;
             case R.id.detail_thumbnail_root_layout:
                 autoPlayEnabled = true; // forcefully start playing
-                openVideoPlayer(true);
+                openVideoPlayerAutoFullscreen();
                 break;
             case R.id.detail_title_root_layout:
                 toggleTitleAndSecondaryControls();
@@ -899,7 +899,7 @@ public final class VideoDetailFragment
                         }
 
                         if (isAutoplayEnabled()) {
-                            openVideoPlayer(true);
+                            openVideoPlayerAutoFullscreen();
                         }
                     }
                 }, throwable -> showError(new ErrorInfo(throwable, UserAction.REQUESTED_STREAM,
@@ -1105,8 +1105,9 @@ public final class VideoDetailFragment
     }
 
     public void openVideoPlayer(final boolean directlyFullscreenIfApplicable) {
+        // Toggle to landscape orientation (which will then cause fullscreen mode) if we are not
+        // already in landscape and screen orientation is locked.
         if (directlyFullscreenIfApplicable
-                && PlayerHelper.isStartMainPlayerFullscreenEnabled(requireContext())
                 && !DeviceUtils.isLandscape(requireContext())
                 && PlayerHelper.globalScreenOrientationLocked(requireContext())) {
             // Make sure the bottom sheet turns out expanded. When this code kicks in the bottom
@@ -1129,6 +1130,12 @@ public final class VideoDetailFragment
         }
     }
 
+    public void openVideoPlayerAutoFullscreen() {
+        // if the option to start directly fullscreen is enabled, openVideoPlayer will be called
+        // with directlyFullscreenIfApplicable=true and therefore open fullscreen if applicable
+        openVideoPlayer(PlayerHelper.isStartMainPlayerFullscreenEnabled(requireContext()));
+    }
+
     private void openNormalBackgroundPlayer(final boolean append) {
         // See UI changes while remote playQueue changes
         if (!isPlayerAvailable()) {
diff --git a/app/src/main/java/org/schabi/newpipe/util/NavigationHelper.java b/app/src/main/java/org/schabi/newpipe/util/NavigationHelper.java
index 67043d808..eba24020f 100644
--- a/app/src/main/java/org/schabi/newpipe/util/NavigationHelper.java
+++ b/app/src/main/java/org/schabi/newpipe/util/NavigationHelper.java
@@ -366,7 +366,9 @@ public final class NavigationHelper {
             if (switchingPlayers) {
                 // Situation when user switches from players to main player. All needed data is
                 // here, we can start watching (assuming newQueue equals playQueue).
-                detailFragment.openVideoPlayer(true);
+                // Starting directly in fullscreen if the previous player type was popup.
+                detailFragment.openVideoPlayer(playerType == MainPlayer.PlayerType.POPUP
+                        || PlayerHelper.isStartMainPlayerFullscreenEnabled(context));
             } else {
                 detailFragment.selectAndLoadVideo(serviceId, url, title, playQueue);
             }