Skip to content

Commit

Permalink
Improve logic upon rotations
Browse files Browse the repository at this point in the history
  • Loading branch information
simoarpe committed Nov 11, 2024
1 parent 5ec1ac0 commit 2213fcd
Show file tree
Hide file tree
Showing 3 changed files with 11 additions and 19 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -13,8 +13,8 @@
@JNINamespace("chrome::android")
public class BackgroundVideoPlaybackTabHelper {

public static void toggleFullscreen(WebContents webContents, boolean isFullScreen) {
BackgroundVideoPlaybackTabHelperJni.get().toggleFullscreen(webContents, isFullScreen);
public static void setFullscreen(WebContents webContents) {
BackgroundVideoPlaybackTabHelperJni.get().setFullscreen(webContents);
}

public static boolean isPlayingMedia(WebContents webContents) {
Expand All @@ -23,7 +23,7 @@ public static boolean isPlayingMedia(WebContents webContents) {

@NativeMethods
interface Natives {
void toggleFullscreen(WebContents webContents, boolean isFullScreen);
void setFullscreen(WebContents webContents);

boolean isPlayingMedia(WebContents webContents);
}
Expand Down
10 changes: 5 additions & 5 deletions android/java/org/chromium/chrome/browser/app/BraveActivity.java
Original file line number Diff line number Diff line change
Expand Up @@ -520,7 +520,7 @@ public void onUserLeaveHint() {
&& isYTVideoUrl(currentTab.getUrl())
&& !isInPictureInPictureMode()
&& BackgroundVideoPlaybackTabHelper.isPlayingMedia(currentTab.getWebContents())) {
BackgroundVideoPlaybackTabHelper.toggleFullscreen(currentTab.getWebContents(), true);
BackgroundVideoPlaybackTabHelper.setFullscreen(currentTab.getWebContents());
try {
enterPictureInPictureMode(new PictureInPictureParams.Builder().build());
} catch (IllegalStateException | IllegalArgumentException e) {
Expand All @@ -537,10 +537,10 @@ public void performOnConfigurationChanged(Configuration newConfig) {
if (currentTab != null
&& currentTab.getUrl() != null
&& isYTVideoUrl(currentTab.getUrl())
&& !isInPictureInPictureMode()) {
BackgroundVideoPlaybackTabHelper.toggleFullscreen(
currentTab.getWebContents(),
newConfig.orientation == Configuration.ORIENTATION_LANDSCAPE);
&& !isInPictureInPictureMode()
&& newConfig.orientation == Configuration.ORIENTATION_LANDSCAPE) {
BackgroundVideoPlaybackTabHelper.setFullscreen(
currentTab.getWebContents());
}
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -108,15 +108,12 @@ void BackgroundVideoPlaybackTabHelper::MediaStoppedPlaying(
namespace chrome {
namespace android {

void JNI_BackgroundVideoPlaybackTabHelper_ToggleFullscreen(
void JNI_BackgroundVideoPlaybackTabHelper_SetFullscreen(
JNIEnv* env,
const base::android::JavaParamRef<jobject>& jweb_contents,
jboolean is_full_screen) {
const base::android::JavaParamRef<jobject>& jweb_contents) {
content::WebContents* web_contents =
content::WebContents::FromJavaWebContents(jweb_contents);
// Injecting this script to make youtube video fullscreen on landscape mode
// and exit fullscreen on portrait mode.
if (is_full_screen) {
// Injecting this script to make youtube video fullscreen on landscape mode.
constexpr const char16_t script[] =
uR"js(if(!document.fullscreenElement) {
var fullscreenBtn =
Expand All @@ -142,11 +139,6 @@ void JNI_BackgroundVideoPlaybackTabHelper_ToggleFullscreen(
ISOLATED_WORLD_ID_BRAVE_INTERNAL, script,
blink::mojom::UserActivationOption::kActivate,
blink::mojom::PromiseResultOption::kAwait, base::NullCallback());
} else {
if (web_contents->HasActiveEffectivelyFullscreenVideo()) {
web_contents->ExitFullscreen(true);
}
}
}

jboolean JNI_BackgroundVideoPlaybackTabHelper_IsPlayingMedia(
Expand Down

0 comments on commit 2213fcd

Please sign in to comment.