From 3f180f86b1fc22877935bd4e116d2aad543d277d Mon Sep 17 00:00:00 2001 From: Manuel Martin Date: Thu, 30 Apr 2020 18:58:04 +0200 Subject: [PATCH] Improved set session code --- .../vrbrowser/ui/widgets/WindowWidget.java | 34 +++++++++++++------ .../mozilla/vrbrowser/ui/widgets/Windows.java | 30 ++++------------ 2 files changed, 30 insertions(+), 34 deletions(-) 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 31866cb51..fb22e9786 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 @@ -94,6 +94,11 @@ public class WindowWidget extends UIWidget implements SessionChangeListener, public static final int SESSION_RELEASE_DISPLAY = 0; public static final int SESSION_DO_NOT_RELEASE_DISPLAY = 1; + @IntDef(value = { ACTIVATE, DEACTIVATE, DO_NOT_CHANGE}) + public @interface SetSessionActiveState {} + public static final int ACTIVATE = 0; + public static final int DEACTIVATE = 1; + public static final int DO_NOT_CHANGE = 2; private Surface mSurface; private int mWidth; @@ -1107,21 +1112,35 @@ public void draw(Canvas aCanvas) { } } - public void setSession(@NonNull Session aSession) { - setSession(aSession, SESSION_RELEASE_DISPLAY); + public void setSession(@NonNull Session aSession, @SetSessionActiveState int aSessionState) { + setSession(aSession, SESSION_RELEASE_DISPLAY, aSessionState); } - public void setSession(@NonNull Session aSession, @OldSessionDisplayAction int aDisplayAction) { + public void setSession(@NonNull Session aSession, @OldSessionDisplayAction int aDisplayAction, @SetSessionActiveState int aSessionState) { if (mSession != aSession) { Session oldSession = mSession; if (oldSession != null) { cleanListeners(oldSession); + switch (aSessionState) { + case ACTIVATE: + mSession.setActive(true); + break; + case DEACTIVATE: + mSession.setActive(false); + break; + case DO_NOT_CHANGE: + break; + } if (aDisplayAction == SESSION_RELEASE_DISPLAY) { oldSession.releaseDisplay(); } } mSession = aSession; + + setupListeners(mSession); + SessionStore.get().setActiveSession(mSession); + mViewModel.setIsPrivateSession(mSession.isPrivateMode()); if (oldSession != null) { @@ -1161,9 +1180,7 @@ public void onStackSession(Session aSession) { // e.g. tab opened via window.open() aSession.updateLastUse(); Session current = mSession; - setupListeners(aSession); - setSession(aSession); - SessionStore.get().setActiveSession(aSession); + setSession(aSession, DEACTIVATE); current.captureBackgroundBitmap(getWindowWidth(), getWindowHeight()).thenAccept(aVoid -> current.setActive(false)); mWidgetManager.getWindows().showTabAddedNotification(); @@ -1173,10 +1190,7 @@ public void onStackSession(Session aSession) { @Override public void onUnstackSession(Session aSession, Session aParent) { if (mSession == aSession) { - aParent.setActive(true); - setupListeners(aParent); - setSession(aParent); - SessionStore.get().setActiveSession(aParent); + setSession(aParent, DEACTIVATE); SessionStore.get().destroySession(aSession); } } 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 4a077cd9c..2231e731a 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 @@ -1213,21 +1213,14 @@ public void onTabSelect(Session aTab) { Session moveTo = targetWindow.getSession(); moveFrom.surfaceDestroyed(); moveTo.surfaceDestroyed(); - windowToMove.setupListeners(moveTo); - windowToMove.setSession(moveTo, WindowWidget.SESSION_DO_NOT_RELEASE_DISPLAY); - targetWindow.setupListeners(moveFrom); - targetWindow.setSession(moveFrom, WindowWidget.SESSION_DO_NOT_RELEASE_DISPLAY); - SessionStore.get().setActiveSession(targetWindow.getSession()); + windowToMove.setSession(moveTo, WindowWidget.SESSION_DO_NOT_RELEASE_DISPLAY, WindowWidget.DO_NOT_CHANGE); + targetWindow.setSession(moveFrom, WindowWidget.SESSION_DO_NOT_RELEASE_DISPLAY, WindowWidget.DO_NOT_CHANGE); windowToMove.setActiveWindow(false); targetWindow.setActiveWindow(true); } else { setFirstPaint(targetWindow, aTab); - targetWindow.getSession().setActive(false); - targetWindow.setupListeners(aTab); - aTab.setActive(true); - targetWindow.setSession(aTab); - SessionStore.get().setActiveSession(aTab); + targetWindow.setSession(aTab, WindowWidget.DEACTIVATE); } } @@ -1238,16 +1231,12 @@ public void addTab(WindowWidget targetWindow) { public void addTab(@NonNull WindowWidget targetWindow, @Nullable String aUri) { Session session = SessionStore.get().createSuspendedSession(aUri, targetWindow.getSession().isPrivateMode()); setFirstPaint(targetWindow, session); - targetWindow.getSession().setActive(false); - targetWindow.setupListeners(session); - session.setActive(true); - targetWindow.setSession(session); + targetWindow.setSession(session, WindowWidget.DEACTIVATE); if (aUri == null || aUri.isEmpty()) { session.loadHomePage(); } else { session.loadUri(aUri); } - SessionStore.get().setActiveSession(session); } public void addBackgroundTab(WindowWidget targetWindow, String aUri) { @@ -1303,9 +1292,7 @@ public void onTabsClose(ArrayList aTabs) { Session tab = available.get(0); if (tab != null) { setFirstPaint(window, tab); - window.setupListeners(tab); - tab.setActive(true); - window.setSession(tab); + window.setSession(tab, WindowWidget.DO_NOT_CHANGE); } available.remove(0); @@ -1320,8 +1307,6 @@ public void onTabsClose(ArrayList aTabs) { cache.removeBitmap(session.getId()); SessionStore.get().destroySession(session); } - - SessionStore.get().setActiveSession(targetWindow.getSession()); } @Override @@ -1342,10 +1327,7 @@ public void onTabsReceived(@NonNull List aTabs) { if (i == 0 && !fullscreen) { // Set the first received tab of the list the current one. - SessionStore.get().setActiveSession(session); - targetWindow.setupListeners(session); - targetWindow.getSession().setActive(false); - targetWindow.setSession(session); + targetWindow.setSession(session, WindowWidget.DEACTIVATE); } }