From e817e1b7333e2176d87a59ac88b9572fec49014b Mon Sep 17 00:00:00 2001 From: "Randall E. Barker" Date: Fri, 29 May 2020 17:31:01 -0700 Subject: [PATCH] Wait to load URI from intent until after session restore. Fixes #3431 This patch changes the behavior of launching or opening a URI from an intent. Previously the URI would be opened in the focuses window. With this patch it matches Fenix and desktop and creates a new tab in the focused window. --- .../mozilla/vrbrowser/VRBrowserActivity.java | 37 ++++------------ .../mozilla/vrbrowser/ui/widgets/Windows.java | 42 +++++++++++++++++++ 2 files changed, 51 insertions(+), 28 deletions(-) diff --git a/app/src/common/shared/org/mozilla/vrbrowser/VRBrowserActivity.java b/app/src/common/shared/org/mozilla/vrbrowser/VRBrowserActivity.java index d8fe7ba51..f56b767a7 100644 --- a/app/src/common/shared/org/mozilla/vrbrowser/VRBrowserActivity.java +++ b/app/src/common/shared/org/mozilla/vrbrowser/VRBrowserActivity.java @@ -93,11 +93,9 @@ import java.util.HashMap; import java.util.HashSet; import java.util.LinkedList; -import java.util.Objects; import java.util.Set; import java.util.concurrent.CopyOnWriteArrayList; import java.util.function.Consumer; -import java.util.function.Function; import static org.mozilla.vrbrowser.ui.widgets.UIWidget.REMOVE_WIDGET; @@ -549,12 +547,11 @@ protected void onNewIntent(final Intent intent) { Log.d(LOGTAG,"VRBrowserActivity onNewIntent"); super.onNewIntent(intent); setIntent(intent); - final String action = intent.getAction(); - if (Intent.ACTION_VIEW.equals(action)) { - loadFromIntent(intent); - } else if (GeckoRuntime.ACTION_CRASHED.equals(intent.getAction())) { + if (GeckoRuntime.ACTION_CRASHED.equals(intent.getAction())) { Log.e(LOGTAG, "Restarted after a crash"); + } else { + loadFromIntent(intent); } } @@ -600,7 +597,6 @@ void loadFromIntent(final Intent intent) { Uri uri = intent.getData(); boolean openInWindow = false; - boolean openInTab = false; boolean openInBackground = false; Bundle extras = intent.getExtras(); @@ -616,14 +612,6 @@ void loadFromIntent(final Intent intent) { SettingsStore.getInstance(this).setHomepage(homepageUri.toString()); } - // Open the provided URL in a new tab, if there is no URL provided we just open the homepage - if (extras.containsKey("create_new_tab")) { - openInTab = extras.getBoolean("create_new_tab", false); - if (uri == null) { - uri = Uri.parse(SettingsStore.getInstance(this).getHomepage()); - } - } - // Open the tab in background/foreground, if there is no URL provided we just open the homepage if (extras.containsKey("background")) { openInBackground = extras.getBoolean("background", false); @@ -659,21 +647,14 @@ void loadFromIntent(final Intent intent) { if (uri != null) { Log.d(LOGTAG, "Loading URI from intent: " + uri.toString()); - if (openInWindow) { - openNewWindow(uri.toString()); - - } else if (openInTab) { - if (openInBackground) { - openNewTab(uri.toString()); - - } else { - openNewTabForeground(uri.toString()); - } + int location = Windows.OPEN_IN_FOREGROUND; - } else { - SessionStore.get().getActiveSession().loadUri(uri.toString()); + if (openInWindow) { + location = Windows.OPEN_IN_NEW_WINDOW; + } else if (openInBackground) { + location = Windows.OPEN_IN_BACKGROUND; } - + mWindows.openNewTabAfterRestore(uri.toString(), location); } else { mWindows.getFocusedWindow().loadHomeIfNotRestored(); } 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 2bbfb0440..00de03598 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 @@ -3,6 +3,7 @@ import android.content.Context; import android.util.Log; +import androidx.annotation.IntDef; import androidx.annotation.NonNull; import androidx.annotation.Nullable; @@ -55,6 +56,13 @@ public class Windows implements TrayListener, TopBarWidget.Delegate, TitleBarWid private static final String LOGTAG = SystemUtils.createLogtag(Windows.class); + @IntDef(value = { OPEN_IN_FOREGROUND, OPEN_IN_BACKGROUND, OPEN_IN_NEW_WINDOW}) + public @interface NewTabLocation {} + public static final int OPEN_IN_FOREGROUND = 0; + public static final int OPEN_IN_BACKGROUND = 1; + public static final int OPEN_IN_NEW_WINDOW = 2; + + private static final String WINDOWS_SAVE_FILENAME = "windows_state.json"; private static final int TAB_ADDED_NOTIFICATION_ID = 0; @@ -135,6 +143,9 @@ class WindowsState { private boolean mCompositorPaused = false; private WindowsState mWindowsState; private boolean mIsRestoreEnabled; + private boolean mAfterRestore; + private String mAddedTabUri; + private @NewTabLocation int mAddedTabLocation = OPEN_IN_FOREGROUND; public enum PanelType { NONE, @@ -759,6 +770,13 @@ public void restoreSessions() { exitPrivateMode(); } } + + if (mAddedTabUri != null) { + openNewTab(mAddedTabUri, mAddedTabLocation); + mAddedTabUri = null; + } + + mAfterRestore = true; } private void removeWindow(@NonNull WindowWidget aWindow) { @@ -1243,6 +1261,30 @@ public void addTab(WindowWidget targetWindow) { addTab(targetWindow, null); } + public void openNewTabAfterRestore(@NonNull String aUri, @NewTabLocation int aLocation) { + if (mAfterRestore) { + openNewTab(aUri, aLocation); + } else { + mAddedTabUri = aUri; + mAddedTabLocation = aLocation; + } + } + + private void openNewTab(@NonNull String aUri, @NewTabLocation int aLocation) { + if (aLocation == OPEN_IN_NEW_WINDOW) { + WindowWidget newWindow = addWindow(); + if ((newWindow != null) && (newWindow.getSession() != null)) { + newWindow.getSession().loadUri(aUri); + } + } else if (mFocusedWindow != null) { + if (aLocation == OPEN_IN_FOREGROUND) { + addTab(mFocusedWindow, aUri); + } else if (aLocation == OPEN_IN_BACKGROUND) { + addBackgroundTab(mFocusedWindow, aUri); + } + } + } + public void addTab(@NonNull WindowWidget targetWindow, @Nullable String aUri) { Session session = SessionStore.get().createSuspendedSession(aUri, targetWindow.getSession().isPrivateMode()); setFirstPaint(targetWindow, session);