diff --git a/app/src/common/shared/org/mozilla/vrbrowser/VRBrowserActivity.java b/app/src/common/shared/org/mozilla/vrbrowser/VRBrowserActivity.java index aeac4d8ac..16f3b616f 100644 --- a/app/src/common/shared/org/mozilla/vrbrowser/VRBrowserActivity.java +++ b/app/src/common/shared/org/mozilla/vrbrowser/VRBrowserActivity.java @@ -53,13 +53,9 @@ import org.mozilla.vrbrowser.search.SearchEngineWrapper; import org.mozilla.vrbrowser.telemetry.TelemetryWrapper; import org.mozilla.vrbrowser.ui.OffscreenDisplay; -import org.mozilla.vrbrowser.ui.views.BookmarksView; -import org.mozilla.vrbrowser.ui.widgets.BookmarkListener; import org.mozilla.vrbrowser.ui.widgets.KeyboardWidget; import org.mozilla.vrbrowser.ui.widgets.NavigationBarWidget; import org.mozilla.vrbrowser.ui.widgets.RootWidget; -import org.mozilla.vrbrowser.ui.widgets.TopBarWidget; -import org.mozilla.vrbrowser.ui.widgets.TrayListener; import org.mozilla.vrbrowser.ui.widgets.TrayWidget; import org.mozilla.vrbrowser.ui.widgets.UIWidget; import org.mozilla.vrbrowser.ui.widgets.VideoProjectionMenuWidget; @@ -74,7 +70,6 @@ import java.io.IOException; import java.net.URISyntaxException; -import java.util.ArrayList; import java.util.Arrays; import java.util.HashMap; import java.util.LinkedList; @@ -269,7 +264,6 @@ protected void initializeWidgets() { }); // Add widget listeners - mWindows.getBookmarksView().addListeners(mNavigationBar, mTray); mTray.addListeners(mWindows); attachToWindow(mWindows.getFocusedWindow(), null); @@ -281,6 +275,7 @@ private void attachToWindow(@NonNull WindowWidget aWindow, @Nullable WindowWidge mPermissionDelegate.setParentWidgetHandle(aWindow.getHandle()); mNavigationBar.attachToWindow(aWindow); mKeyboard.attachToWindow(aWindow); + mTray.attachToWindow(aWindow); if (aPrevWindow != null) { updateWidget(mNavigationBar); @@ -364,6 +359,7 @@ protected void onDestroy() { } // Remove all widget listeners + mTray.removeListeners(mWindows); mTray.onDestroy(); mWindows.onDestroy(); diff --git a/app/src/common/shared/org/mozilla/vrbrowser/ui/views/BookmarksView.java b/app/src/common/shared/org/mozilla/vrbrowser/ui/views/BookmarksView.java index 1fd1b1eb2..654683c4b 100644 --- a/app/src/common/shared/org/mozilla/vrbrowser/ui/views/BookmarksView.java +++ b/app/src/common/shared/org/mozilla/vrbrowser/ui/views/BookmarksView.java @@ -8,13 +8,10 @@ import android.content.Context; import android.util.AttributeSet; import android.view.LayoutInflater; -import android.view.View; import android.widget.FrameLayout; -import org.mozilla.geckoview.AllowOrDeny; -import org.mozilla.geckoview.GeckoResult; -import org.mozilla.geckoview.GeckoSession; -import org.mozilla.geckoview.WebRequestError; +import androidx.databinding.DataBindingUtil; + import org.mozilla.vrbrowser.R; import org.mozilla.vrbrowser.audio.AudioEngine; import org.mozilla.vrbrowser.browser.BookmarksStore; @@ -23,28 +20,18 @@ import org.mozilla.vrbrowser.databinding.BookmarksBinding; import org.mozilla.vrbrowser.ui.adapters.BookmarkAdapter; import org.mozilla.vrbrowser.ui.callbacks.BookmarkClickCallback; -import org.mozilla.vrbrowser.ui.widgets.BookmarkListener; -import org.mozilla.vrbrowser.ui.widgets.WindowWidget; import org.mozilla.vrbrowser.utils.UIThreadExecutor; -import java.util.ArrayList; -import java.util.Arrays; import java.util.List; -import androidx.annotation.NonNull; -import androidx.annotation.Nullable; -import androidx.databinding.DataBindingUtil; - import mozilla.components.concept.storage.BookmarkNode; public class BookmarksView extends FrameLayout implements BookmarksStore.BookmarkListener { private BookmarksBinding mBinding; private BookmarkAdapter mBookmarkAdapter; - private List mBookmarkListeners; private AudioEngine mAudio; private boolean mIgnoreNextListener; - private WindowWidget mAttachedWindow; public BookmarksView(Context aContext) { super(aContext); @@ -62,8 +49,6 @@ public BookmarksView(Context aContext, AttributeSet aAttrs, int aDefStyle) { } private void initialize(Context aContext) { - mBookmarkListeners = new ArrayList<>(); - mAudio = AudioEngine.fromContext(aContext); LayoutInflater inflater = LayoutInflater.from(aContext); @@ -80,48 +65,10 @@ private void initialize(Context aContext) { setVisibility(GONE); } - public void addListeners(BookmarkListener... listeners) { - mBookmarkListeners.addAll(Arrays.asList(listeners)); - } - - public void removeListeners(BookmarkListener... listeners) { - mBookmarkListeners.removeAll(Arrays.asList(listeners)); - } - - public void detachFromWindow() { - if (mAttachedWindow != null) { - mAttachedWindow.unsetView(this); - notifyBookmarksHidden(mAttachedWindow); - mAttachedWindow = null; - } - } - - public void attachToWindow(@NonNull WindowWidget aWindow) { - if (mAttachedWindow == aWindow) { - return; - } - mAttachedWindow = aWindow; - mAttachedWindow.setView(this); - notifyBookmarksShown(mAttachedWindow); - } - - public WindowWidget getAttachedWindow() { - return mAttachedWindow; - } - public void onDestroy() { - mBookmarkListeners.clear(); SessionManager.get().getBookmarkStore().removeListener(this); } - private void notifyBookmarksShown(WindowWidget aWindow) { - mBookmarkListeners.forEach(listener -> listener.onBookmarksShown(aWindow)); - } - - private void notifyBookmarksHidden(WindowWidget aWindow) { - mBookmarkListeners.forEach(listener -> listener.onBookmarksHidden(aWindow)); - } - private final BookmarkClickCallback mBookmarkClickCallback = new BookmarkClickCallback() { @Override public void onClick(BookmarkNode bookmark) { diff --git a/app/src/common/shared/org/mozilla/vrbrowser/ui/widgets/NavigationBarWidget.java b/app/src/common/shared/org/mozilla/vrbrowser/ui/widgets/NavigationBarWidget.java index bb5a5939a..1fb35d9c8 100644 --- a/app/src/common/shared/org/mozilla/vrbrowser/ui/widgets/NavigationBarWidget.java +++ b/app/src/common/shared/org/mozilla/vrbrowser/ui/widgets/NavigationBarWidget.java @@ -363,6 +363,8 @@ public void detachFromWindow() { mSessionStore.removeContentListener(this); mSessionStore = null; } + if (mAttachedWindow != null) + mAttachedWindow.removeBookmarksListener(this); mAttachedWindow = null; } @@ -375,6 +377,7 @@ public void attachToWindow(@NonNull WindowWidget aWindow) { mWidgetPlacement.parentHandle = aWindow.getHandle(); mAttachedWindow = aWindow; + mAttachedWindow.addBookmarksListener(this); mSessionStore = aWindow.getSessionStore(); if (mSessionStore != null) { @@ -634,7 +637,7 @@ public void release() { @Override public void onLocationChange(GeckoSession session, String url) { - if (mURLBar != null) { + if (mURLBar != null && !mAttachedWindow.isBookmarksVisible()) { Log.d(LOGTAG, "Got location change"); mURLBar.setURL(url); mReloadButton.setEnabled(true); diff --git a/app/src/common/shared/org/mozilla/vrbrowser/ui/widgets/TrayWidget.java b/app/src/common/shared/org/mozilla/vrbrowser/ui/widgets/TrayWidget.java index 90db2f46b..634779fb5 100644 --- a/app/src/common/shared/org/mozilla/vrbrowser/ui/widgets/TrayWidget.java +++ b/app/src/common/shared/org/mozilla/vrbrowser/ui/widgets/TrayWidget.java @@ -20,7 +20,6 @@ import org.mozilla.vrbrowser.R; import org.mozilla.vrbrowser.audio.AudioEngine; import org.mozilla.vrbrowser.browser.SessionChangeListener; -import org.mozilla.vrbrowser.browser.engine.SessionManager; import org.mozilla.vrbrowser.browser.engine.SessionStore; import org.mozilla.vrbrowser.ui.views.UIButton; import org.mozilla.vrbrowser.ui.widgets.settings.SettingsWidget; @@ -273,6 +272,8 @@ public void detachFromWindow() { mSessionStore.removeSessionChangeListener(this); mSessionStore = null; } + if (mAttachedWindow != null) + mAttachedWindow.removeBookmarksListener(this); } @Override @@ -280,13 +281,21 @@ public void attachToWindow(@NonNull WindowWidget aWindow) { if (mAttachedWindow == aWindow) { return; } + detachFromWindow(); + mAttachedWindow = aWindow; + mAttachedWindow.addBookmarksListener(this); mSessionStore = aWindow.getSessionStore(); if (mSessionStore != null) { mSessionStore.addSessionChangeListener(this); handleSessionState(); } + + if (mAttachedWindow.isBookmarksVisible()) + onBookmarksShown(aWindow); + else + onBookmarksHidden(aWindow); } // SessionStore.SessionChangeListener diff --git a/app/src/common/shared/org/mozilla/vrbrowser/ui/widgets/WindowWidget.java b/app/src/common/shared/org/mozilla/vrbrowser/ui/widgets/WindowWidget.java index b3315ce1e..1db84f6f4 100644 --- a/app/src/common/shared/org/mozilla/vrbrowser/ui/widgets/WindowWidget.java +++ b/app/src/common/shared/org/mozilla/vrbrowser/ui/widgets/WindowWidget.java @@ -10,7 +10,6 @@ import android.graphics.Point; import android.graphics.Rect; import android.graphics.SurfaceTexture; -import android.os.Bundle; import android.util.Log; import android.view.KeyEvent; import android.view.MotionEvent; @@ -19,16 +18,19 @@ import android.view.inputmethod.EditorInfo; import android.view.inputmethod.InputConnection; +import androidx.annotation.NonNull; +import androidx.annotation.Nullable; + import org.mozilla.geckoview.AllowOrDeny; import org.mozilla.geckoview.GeckoDisplay; import org.mozilla.geckoview.GeckoResult; import org.mozilla.geckoview.GeckoSession; import org.mozilla.vrbrowser.R; import org.mozilla.vrbrowser.browser.SessionChangeListener; +import org.mozilla.vrbrowser.browser.SettingsStore; import org.mozilla.vrbrowser.browser.VideoAvailabilityListener; import org.mozilla.vrbrowser.browser.engine.SessionManager; import org.mozilla.vrbrowser.browser.engine.SessionStore; -import org.mozilla.vrbrowser.browser.SettingsStore; import org.mozilla.vrbrowser.ui.views.BookmarksView; import org.mozilla.vrbrowser.ui.widgets.dialogs.ContextMenuWidget; import org.mozilla.vrbrowser.ui.widgets.prompts.AlertPromptWidget; @@ -37,12 +39,13 @@ import org.mozilla.vrbrowser.ui.widgets.prompts.ConfirmPromptWidget; import org.mozilla.vrbrowser.ui.widgets.prompts.TextPromptWidget; -import androidx.annotation.NonNull; +import java.util.ArrayList; import static org.mozilla.vrbrowser.utils.ServoUtils.isInstanceOfServoSession; public class WindowWidget extends UIWidget implements SessionChangeListener, - GeckoSession.ContentDelegate, GeckoSession.PromptDelegate, VideoAvailabilityListener { + GeckoSession.ContentDelegate, GeckoSession.PromptDelegate, + GeckoSession.NavigationDelegate, VideoAvailabilityListener { private static final String LOGTAG = "VRB"; @@ -72,6 +75,8 @@ public class WindowWidget extends UIWidget implements SessionChangeListener, private Point mLastMouseClickPos; private SessionStore mSessionStore; private int mWindowId; + private BookmarksView mBookmarksView; + private ArrayList mBookmarksListeners; private Windows.WindowPlacement mWindowPlacement = Windows.WindowPlacement.FRONT; public WindowWidget(Context aContext, int windowId, boolean privateMode) { @@ -85,8 +90,12 @@ public WindowWidget(Context aContext, int windowId, boolean privateMode) { mSessionStore.addPromptListener(this); mSessionStore.addContentListener(this); mSessionStore.addVideoAvailabilityListener(this); + mSessionStore.addNavigationListener(this); mSessionStore.newSession(); + mBookmarksView = new BookmarksView(aContext); + mBookmarksListeners = new ArrayList<>(); + mHandle = ((WidgetManagerDelegate)aContext).newWidgetHandle(); mWidgetPlacement = new WidgetPlacement(aContext); initializeWidgetPlacement(mWidgetPlacement); @@ -147,17 +156,22 @@ public void hide(@HideFlags int aHideFlag) { @Override protected void onDismiss() { - if (mView != null) { + if (isBookmarksVisible()) { + switchBookmarks(); + + } else { SessionStore activeStore = SessionManager.get().getSessionStore(mWindowId); if (activeStore.canGoBack()) { activeStore.goBack(); } - - unsetView(mView); } } - public void setView(View view) { + public void onDestroy() { + mBookmarksView.onDestroy(); + } + + private void setView(View view) { pauseCompositor(); mView = view; removeView(view); @@ -177,7 +191,7 @@ public void setView(View view) { postInvalidate(); } - public void unsetView(View view) { + private void unsetView(View view) { if (mView != null && mView == view) { mView = null; removeView(view); @@ -199,6 +213,31 @@ public void unsetView(View view) { } } + public boolean isBookmarksVisible() { + return (mView != null); + } + + public void addBookmarksListener(@NonNull BookmarkListener listener) { + mBookmarksListeners.add(listener); + } + + public void removeBookmarksListener(@NonNull BookmarkListener listener) { + mBookmarksListeners.remove(listener); + } + + public void switchBookmarks() { + if (mView == null) { + setView(mBookmarksView); + for (BookmarkListener listener : mBookmarksListeners) + listener.onBookmarksShown(this); + + } else { + unsetView(mBookmarksView); + for (BookmarkListener listener : mBookmarksListeners) + listener.onBookmarksHidden(this); + } + } + public void pauseCompositor() { if (mDisplay == null) { return; @@ -443,6 +482,7 @@ public void releaseWidget() { mSessionStore.removePromptListener(this); mSessionStore.removeContentListener(this); mSessionStore.removeVideoAvailabilityListener(this); + mSessionStore.removeNavigationListener(this); GeckoSession session = mSessionStore.getSession(mSessionId); if (session == null) { return; @@ -724,4 +764,18 @@ public void onVideoAvailabilityChanged(boolean aVideosAvailable) { WidgetManagerDelegate.CPU_LEVEL_NORMAL); } + // GeckoSession.NavigationDelegate + + @Override + public void onLocationChange(@NonNull GeckoSession session, @Nullable String url) { + if (isBookmarksVisible()) + switchBookmarks(); + } + + @Nullable + @Override + public GeckoResult onLoadRequest(@NonNull GeckoSession session, @NonNull LoadRequest request) { + return GeckoResult.ALLOW; + } + } diff --git a/app/src/common/shared/org/mozilla/vrbrowser/ui/widgets/Windows.java b/app/src/common/shared/org/mozilla/vrbrowser/ui/widgets/Windows.java index d7117b09d..d23e524e0 100644 --- a/app/src/common/shared/org/mozilla/vrbrowser/ui/widgets/Windows.java +++ b/app/src/common/shared/org/mozilla/vrbrowser/ui/widgets/Windows.java @@ -5,15 +5,12 @@ import androidx.annotation.NonNull; import androidx.annotation.Nullable; - import org.mozilla.geckoview.GeckoSession; import org.mozilla.vrbrowser.R; import org.mozilla.vrbrowser.browser.SettingsStore; -import org.mozilla.vrbrowser.ui.views.BookmarksView; import org.mozilla.vrbrowser.utils.InternalPages; import java.util.ArrayList; -import java.util.Comparator; public class Windows implements TrayListener, TopBarWidget.Delegate { private Context mContext; @@ -21,7 +18,6 @@ public class Windows implements TrayListener, TopBarWidget.Delegate { private Delegate mDelegate; private ArrayList mRegularWindows; private ArrayList mPrivateWindows; - private BookmarksView mBookmarksView; private WindowWidget mFocusedWindow; private static int sIndex; private boolean mPrivateMode = false; @@ -50,7 +46,6 @@ public Windows(Context aContext) { mWidgetManager = (WidgetManagerDelegate) aContext; mRegularWindows = new ArrayList<>(); mPrivateWindows = new ArrayList<>(); - mBookmarksView = new BookmarksView(aContext); restoreWindows(); } @@ -214,7 +209,12 @@ public void resumeCompositor() { } public void onDestroy() { - mBookmarksView.onDestroy(); + for (WindowWidget window: mRegularWindows) { + window.onDestroy(); + } + for (WindowWidget window: mPrivateWindows) { + window.onDestroy(); + } } public boolean isInPrivateMode() { @@ -261,10 +261,7 @@ public boolean handleBack() { if (mFocusedWindow == null) { return false; } - if (mBookmarksView.getAttachedWindow() == mFocusedWindow) { - onBookmarksClicked(); - return true; - } else if (mFocusedWindow.getSessionStore().canGoBack()) { + if (mFocusedWindow.getSessionStore().canGoBack()) { mFocusedWindow.getSessionStore().goBack(); return true; } else if (isInPrivateMode()) { @@ -275,10 +272,6 @@ public boolean handleBack() { return false; } - public BookmarksView getBookmarksView() { - return mBookmarksView; - } - private void showMaxWindowsMessage() { mFocusedWindow.showAlert("", mContext.getString(R.string.max_windows_message, String.valueOf(MAX_WINDOWS)), new GeckoSession.PromptDelegate.AlertCallback() { @Override @@ -324,17 +317,11 @@ private void removeWindow(@NonNull WindowWidget aWindow) { mPrivateWindows.remove(aWindow); aWindow.getTopBar().setVisible(false); aWindow.getTopBar().setDelegate((TopBarWidget.Delegate) null); - if (mBookmarksView.getAttachedWindow() == aWindow) { - mBookmarksView.detachFromWindow(); - } } private void setWindowVisible(WindowWidget aWindow, boolean aVisible) { aWindow.setVisible(aVisible); aWindow.getTopBar().setVisible(aVisible); - if (!aVisible && aWindow == mBookmarksView.getAttachedWindow()) { - mBookmarksView.detachFromWindow(); - } } private void placeWindow(WindowWidget aWindow, WindowPlacement aPosition) { @@ -394,6 +381,8 @@ private void updateViews() { for (WindowWidget window: getCurrentWindows()) { mWidgetManager.updateWidget(window); mWidgetManager.updateWidget(window.getTopBar()); + window.switchBookmarks(); + window.switchBookmarks(); } } @@ -429,11 +418,7 @@ private WindowWidget createWindow() { // Tray Listener @Override public void onBookmarksClicked() { - if (mBookmarksView.getParent() != null) { - mBookmarksView.detachFromWindow(); - } else if (mFocusedWindow != null) { - mBookmarksView.attachToWindow(mFocusedWindow); - } + mFocusedWindow.switchBookmarks(); } @Override