Skip to content
This repository has been archived by the owner on Jul 22, 2024. It is now read-only.

Update video projection for currently playing video #3466

Open
wants to merge 1 commit into
base: main
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Original file line number Diff line number Diff line change
Expand Up @@ -15,7 +15,6 @@
import android.util.Log;
import android.util.Pair;
import android.view.LayoutInflater;
import android.view.MotionEvent;
import android.view.View;
import android.webkit.URLUtil;
import android.widget.EditText;
Expand All @@ -29,13 +28,15 @@

import org.mozilla.geckoview.GeckoSession;
import org.mozilla.geckoview.GeckoSessionSettings;
import org.mozilla.geckoview.MediaElement;
import org.mozilla.vrbrowser.R;
import org.mozilla.vrbrowser.VRBrowserActivity;
import org.mozilla.vrbrowser.VRBrowserApplication;
import org.mozilla.vrbrowser.audio.AudioEngine;
import org.mozilla.vrbrowser.browser.Media;
import org.mozilla.vrbrowser.browser.SessionChangeListener;
import org.mozilla.vrbrowser.browser.SettingsStore;
import org.mozilla.vrbrowser.browser.VideoAvailabilityListener;
import org.mozilla.vrbrowser.browser.content.TrackingProtectionStore;
import org.mozilla.vrbrowser.browser.engine.Session;
import org.mozilla.vrbrowser.browser.engine.SessionStore;
Expand Down Expand Up @@ -74,7 +75,7 @@ public class NavigationBarWidget extends UIWidget implements GeckoSession.Naviga
WidgetManagerDelegate.UpdateListener, SessionChangeListener,
NavigationURLBar.NavigationURLBarDelegate, VoiceSearchWidget.VoiceSearchDelegate,
SharedPreferences.OnSharedPreferenceChangeListener, SuggestionsWidget.URLBarPopupDelegate,
TrayListener, WindowWidget.WindowListener {
TrayListener, WindowWidget.WindowListener, VideoAvailabilityListener {

private static final int TAB_ADDED_NOTIFICATION_ID = 0;
private static final int TAB_SENT_NOTIFICATION_ID = 1;
Expand Down Expand Up @@ -557,13 +558,15 @@ private void setUpSession(@NonNull Session aSession) {
aSession.addSessionChangeListener(this);
aSession.addNavigationListener(this);
aSession.addContentListener(this);
aSession.addVideoAvailabilityListener(this);
mBinding.navigationBarNavigation.urlBar.setSession(getSession());
}

private void cleanSession(@NonNull Session aSession) {
aSession.removeSessionChangeListener(this);
aSession.removeNavigationListener(this);
aSession.removeContentListener(this);
aSession.removeVideoAvailabilityListener(this);
}

@Override
Expand Down Expand Up @@ -821,6 +824,7 @@ private void exitVRVideo() {
mProjectionMenu.getPlacement().copyFrom(mProjectionMenuPlacement);
mProjectionMenu.getPlacement().composited = composited;
mProjectionMenu.setSelectedProjection(VIDEO_PROJECTION_NONE);
mProjectionMenu.resetProjectionOverride();
mWidgetManager.updateWidget(mProjectionMenu);
closeFloatingMenus();
mWidgetManager.setControllersVisible(true);
Expand Down Expand Up @@ -1336,4 +1340,64 @@ public void onAllow() {
mQuickPermissionWidget.getPlacement().parentAnchorX = x / getWidth();
mQuickPermissionWidget.show(REQUEST_FOCUS);
}

// VideoAvailabilityListener

@Override
public void onVideoAvailabilityChanged(@NonNull Media aMedia, boolean aVideoAvailable) {
if (getSession() != null) {
if (aVideoAvailable) {
aMedia.addMediaListener(mMediaDelegate);

} else {
aMedia.removeMediaListener(mMediaDelegate);
}
}
}

MediaElement.Delegate mMediaDelegate = new MediaElement.Delegate() {
@Override
public void onPlaybackStateChange(@NonNull MediaElement mediaElement, int state) {
// This should handle the case where a video is being played in full-screen mode and a new
// video is played. That could happen because the user clicks on the previous/next buttons
// of the video controls or because the current video ends an the next video plays in case
// auto-play is enabled.
if (mViewModel.getIsFullscreen().getValue().get()) {
if (state == MediaElement.MEDIA_STATE_PLAYING){
AtomicBoolean autoEnter = new AtomicBoolean(false);
mAutoSelectedProjection = VideoProjectionMenuWidget.getAutomaticProjection(getSession().getCurrentUri(), autoEnter);
if (mViewModel.getIsInVRVideo().getValue().get()) {
if (mProjectionMenu.isIsProjectionOverridden()) {
// // Fullscreen && VRMode && ProjectionOverridden
mWidgetManager.showVRVideo(mAttachedWindow.getHandle(), mProjectionMenu.getSelectedProjection());

} else {
if (mAutoSelectedProjection == VIDEO_PROJECTION_NONE) {
// Fullscreen && VRMode && VIDEO_PROJECTION_NONE
// Exit VR video mode.
exitVRVideo();

} else {
// Fullscreen && VRMode && !VIDEO_PROJECTION_NONE
// Update the projection.
mProjectionMenu.setSelectedProjection(mAutoSelectedProjection);
mWidgetManager.showVRVideo(mAttachedWindow.getHandle(), mAutoSelectedProjection);
}
}

} else {
// Fullscreen && VRMode && !VIDEO_PROJECTION_NONE
// Enter VR Video mode.
if (mAutoSelectedProjection != VIDEO_PROJECTION_NONE) {
enterVRVideo(mAutoSelectedProjection);
}
}

} else if (state == MediaElement.MEDIA_STATE_ENDED) {
// Clear the menu override flag when the video ends
mProjectionMenu.resetProjectionOverride();
}
}
}
};
}
Original file line number Diff line number Diff line change
Expand Up @@ -47,6 +47,7 @@ public ProjectionMenuItem(@VideoProjectionFlags int aProjection, String aString,
ArrayList<MenuItem> mItems;
Delegate mDelegate;
@VideoProjectionFlags int mSelectedProjection = VIDEO_PROJECTION_3D_SIDE_BY_SIDE;
boolean mIsProjectionOverridden = false;

public VideoProjectionMenuWidget(Context aContext) {
super(aContext, R.layout.menu);
Expand Down Expand Up @@ -118,6 +119,7 @@ private void createMenuItems() {

private void handleClick(@VideoProjectionFlags int aVideoProjection) {
mSelectedProjection = aVideoProjection;
mIsProjectionOverridden = true;
if (mDelegate != null) {
mDelegate.onVideoProjectionClick(aVideoProjection);
}
Expand Down Expand Up @@ -174,6 +176,14 @@ public void setSelectedProjection(@VideoProjectionFlags int aProjection) {
return VIDEO_PROJECTION_NONE;
}

public void resetProjectionOverride () {
mIsProjectionOverridden = false;
}

public boolean isIsProjectionOverridden() {
return mIsProjectionOverridden;
}

@Override
public void onGlobalFocusChanged(View oldFocus, View newFocus) {
if (!ViewUtils.isEqualOrChildrenOf(this, newFocus) && isVisible()) {
Expand Down
2 changes: 2 additions & 0 deletions app/src/main/assets/web_extensions/webcompat_youtube/main.js
Original file line number Diff line number Diff line change
Expand Up @@ -118,6 +118,8 @@ class YoutubeExtension {
this.updateVideoStyle();
logDebug(`Video projection set to: ${qs.get(VIDEO_PROJECTION_PARAM)}`);
} else {
qs.delete('mozVideoProjection');
this.updateURL(qs);
logDebug(`Video is flat, no projection selected`);
}
}
Expand Down