Skip to content

Commit

Permalink
Added check to avoid the nullpointerexception when focusing a window (M…
Browse files Browse the repository at this point in the history
…ozillaReality#2477)

* Added check to avoid the nullpointerexception

* Check attached window null
  • Loading branch information
keianhzo authored and Alexandre Lissy committed Jan 21, 2020
1 parent b28f4e8 commit 7a8863e
Showing 1 changed file with 26 additions and 11 deletions.
37 changes: 26 additions & 11 deletions app/src/common/shared/org/mozilla/vrbrowser/ui/widgets/Windows.java
Expand Up @@ -207,6 +207,7 @@ public WindowWidget getFocusedWindow() {
return mFocusedWindow;
}

@NonNull
public WindowWidget addWindow() {
if (getCurrentWindows().size() >= MAX_WINDOWS) {
return null;
Expand Down Expand Up @@ -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) {
Expand All @@ -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);
}

}
Expand Down Expand Up @@ -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);
Expand All @@ -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);
Expand Down Expand Up @@ -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);
Expand All @@ -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);
}
Expand Down Expand Up @@ -583,6 +591,7 @@ public ArrayList<WindowWidget> getCurrentWindows() {
return mPrivateMode ? mPrivateWindows : mRegularWindows;
}

@Nullable
private WindowWidget getWindowWithPlacement(WindowPlacement aPlacement) {
for (WindowWidget window: getCurrentWindows()) {
if (window.getWindowPlacement() == aPlacement) {
Expand All @@ -592,17 +601,20 @@ private WindowWidget getWindowWithPlacement(WindowPlacement aPlacement) {
return null;
}

@Nullable
private WindowWidget getFrontWindow() {
if (mFullscreenWindow != null) {
return mFullscreenWindow;
}
return getWindowWithPlacement(WindowPlacement.FRONT);
}

@Nullable
private WindowWidget getLeftWindow() {
return getWindowWithPlacement(WindowPlacement.LEFT);
}

@Nullable
private WindowWidget getRightWindow() {
return getWindowWithPlacement(WindowPlacement.RIGHT);
}
Expand Down Expand Up @@ -849,6 +861,7 @@ private void updateTitleBars() {
}
}

@NonNull
private WindowWidget createWindow(@Nullable Session aSession) {
int newWindowId = sIndex++;
WindowWidget window;
Expand Down Expand Up @@ -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
Expand Down Expand Up @@ -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);
}
Expand Down

0 comments on commit 7a8863e

Please sign in to comment.