From 7a8863ed3be1306daac62bb0a3139e2768a94007 Mon Sep 17 00:00:00 2001 From: Manuel Martin Date: Fri, 10 Jan 2020 20:54:44 +0100 Subject: [PATCH] Added check to avoid the nullpointerexception when focusing a window (#2477) * Added check to avoid the nullpointerexception * Check attached window null --- .../mozilla/vrbrowser/ui/widgets/Windows.java | 37 +++++++++++++------ 1 file changed, 26 insertions(+), 11 deletions(-) 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 0df7d0f99..f070c30fb 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 @@ -207,6 +207,7 @@ public WindowWidget getFocusedWindow() { return mFocusedWindow; } + @NonNull public WindowWidget addWindow() { if (getCurrentWindows().size() >= MAX_WINDOWS) { return null; @@ -290,12 +291,12 @@ public void closeWindow(@NonNull WindowWidget aWindow) { if (leftWindow == aWindow) { removeWindow(leftWindow); - if (mFocusedWindow == leftWindow) { + if (mFocusedWindow == leftWindow && frontWindow != null) { focusWindow(frontWindow); } } else if (rightWindow == aWindow) { removeWindow(rightWindow); - if (mFocusedWindow == rightWindow) { + if (mFocusedWindow == rightWindow && frontWindow != null) { focusWindow(frontWindow); } } else if (frontWindow == aWindow) { @@ -306,8 +307,9 @@ public void closeWindow(@NonNull WindowWidget aWindow) { placeWindow(leftWindow, WindowPlacement.FRONT); } - if (mFocusedWindow == frontWindow && !getCurrentWindows().isEmpty()) { - focusWindow(getFrontWindow()); + frontWindow = getFrontWindow(); + if (mFocusedWindow == frontWindow && !getCurrentWindows().isEmpty() && frontWindow != null) { + focusWindow(frontWindow); } } @@ -337,7 +339,7 @@ public void moveWindowRight(@NonNull WindowWidget aWindow) { WindowWidget leftWindow = getLeftWindow(); WindowWidget rightWindow = getRightWindow(); - if (aWindow == leftWindow) { + if (aWindow == leftWindow && frontWindow != null) { placeWindow(leftWindow, WindowPlacement.FRONT); placeWindow(frontWindow, WindowPlacement.LEFT); switchTopBars(leftWindow, frontWindow); @@ -362,7 +364,7 @@ public void moveWindowLeft(@NonNull WindowWidget aWindow) { WindowWidget leftWindow = getLeftWindow(); WindowWidget rightWindow = getRightWindow(); - if (aWindow == rightWindow) { + if (aWindow == rightWindow && frontWindow != null) { placeWindow(rightWindow, WindowPlacement.FRONT); placeWindow(frontWindow, WindowPlacement.RIGHT); switchTopBars(rightWindow, frontWindow); @@ -522,7 +524,10 @@ public void enterPrivateMode() { } } else { - focusWindow(getWindowWithPlacement(mPrivateWindowPlacement)); + WindowWidget window = getWindowWithPlacement(mRegularWindowPlacement); + if (window != null) { + focusWindow(window); + } } updateViews(); mWidgetManager.pushWorldBrightness(this, WidgetManagerDelegate.DEFAULT_DIM_BRIGHTNESS); @@ -546,7 +551,10 @@ public void exitPrivateMode() { for (WindowWidget window: mRegularWindows) { setWindowVisible(window, true); } - focusWindow(getWindowWithPlacement(mRegularWindowPlacement)); + WindowWidget window = getWindowWithPlacement(mRegularWindowPlacement); + if (window != null) { + focusWindow(window); + } updateViews(); mWidgetManager.popWorldBrightness(this); } @@ -583,6 +591,7 @@ public ArrayList getCurrentWindows() { return mPrivateMode ? mPrivateWindows : mRegularWindows; } + @Nullable private WindowWidget getWindowWithPlacement(WindowPlacement aPlacement) { for (WindowWidget window: getCurrentWindows()) { if (window.getWindowPlacement() == aPlacement) { @@ -592,6 +601,7 @@ private WindowWidget getWindowWithPlacement(WindowPlacement aPlacement) { return null; } + @Nullable private WindowWidget getFrontWindow() { if (mFullscreenWindow != null) { return mFullscreenWindow; @@ -599,10 +609,12 @@ private WindowWidget getFrontWindow() { return getWindowWithPlacement(WindowPlacement.FRONT); } + @Nullable private WindowWidget getLeftWindow() { return getWindowWithPlacement(WindowPlacement.LEFT); } + @Nullable private WindowWidget getRightWindow() { return getWindowWithPlacement(WindowPlacement.RIGHT); } @@ -849,6 +861,7 @@ private void updateTitleBars() { } } + @NonNull private WindowWidget createWindow(@Nullable Session aSession) { int newWindowId = sIndex++; WindowWidget window; @@ -1027,7 +1040,9 @@ public void onMoveRightClicked(TopBarWidget aWidget) { // Title Bar Delegate @Override public void onTitleClicked(@NonNull TitleBarWidget titleBar) { - focusWindow(titleBar.getAttachedWindow()); + if (titleBar.getAttachedWindow() != null) { + focusWindow(titleBar.getAttachedWindow()); + } } @Override @@ -1090,12 +1105,12 @@ private WindowWidget getWindowWithSession(Session aSession) { // WindowWidget.Delegate @Override - public void onFocusRequest(WindowWidget aWindow) { + public void onFocusRequest(@NonNull WindowWidget aWindow) { focusWindow(aWindow); } @Override - public void onBorderChanged(WindowWidget aWindow) { + public void onBorderChanged(@NonNull WindowWidget aWindow) { if (mDelegate != null) { mDelegate.onWindowBorderChanged(aWindow); }