Skip to content
This repository was archived by the owner on Jul 22, 2024. It is now read-only.

Commit d1ab92f

Browse files
authored
Release Virtual Display in onPause (#1332)
1 parent 4b30c53 commit d1ab92f

File tree

2 files changed

+49
-20
lines changed

2 files changed

+49
-20
lines changed

app/src/common/shared/org/mozilla/vrbrowser/VRBrowserActivity.java

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -311,11 +311,17 @@ protected void onPause() {
311311
mConnectivityReceiver.unregister(this);
312312
// Reset so the dialog will show again on resume.
313313
mConnectionAvailable = true;
314+
if (mOffscreenDisplay != null) {
315+
mOffscreenDisplay.onPause();
316+
}
314317
super.onPause();
315318
}
316319

317320
@Override
318321
protected void onResume() {
322+
if (mOffscreenDisplay != null) {
323+
mOffscreenDisplay.onResume();
324+
}
319325
SessionStore.get().setActive(true);
320326
mAudioEngine.resumeEngine();
321327
for (Widget widget: mWidgets.values()) {

app/src/common/shared/org/mozilla/vrbrowser/ui/OffscreenDisplay.java

Lines changed: 43 additions & 20 deletions
Original file line numberDiff line numberDiff line change
@@ -7,56 +7,47 @@
77

88
import android.app.Presentation;
99
import android.content.Context;
10-
import android.content.DialogInterface;
1110
import android.graphics.SurfaceTexture;
1211
import android.hardware.display.DisplayManager;
1312
import android.hardware.display.VirtualDisplay;
1413
import android.os.Bundle;
1514
import android.util.DisplayMetrics;
1615
import android.view.Display;
17-
import android.view.LayoutInflater;
1816
import android.view.Surface;
1917
import android.view.View;
20-
import android.util.Log;
18+
import android.view.ViewGroup;
2119

2220
public class OffscreenDisplay {
2321
final String LOGTAG = "VRB";
2422
private Context mContext;
23+
private int mWidth;
24+
private int mHeight;
2525
private VirtualDisplay mVirtualDisplay;
2626
private SurfaceTexture mTexture;
2727
private Surface mSurface;
2828
private OffscreenPresentation mPresentation;
29+
private View mContentView;
2930

3031
private DisplayMetrics mDefaultMetrics;
3132

3233
public OffscreenDisplay(Context aContext, SurfaceTexture aTexture, int aWidth, int aHeight) {
3334
mContext = aContext;
35+
mWidth = aWidth;
36+
mHeight = aHeight;
3437
aTexture.setDefaultBufferSize(aWidth, aHeight);
3538
mTexture = aTexture;
3639
mSurface = new Surface(aTexture);
3740

38-
DisplayManager manager = (DisplayManager) aContext.getSystemService(Context.DISPLAY_SERVICE);
39-
Display defaultDisplay = manager.getDisplay(Display.DEFAULT_DISPLAY);
40-
4141
mDefaultMetrics = new DisplayMetrics();
42-
defaultDisplay.getMetrics(mDefaultMetrics);
43-
44-
int flags = DisplayManager.VIRTUAL_DISPLAY_FLAG_OWN_CONTENT_ONLY |
45-
DisplayManager.VIRTUAL_DISPLAY_FLAG_PRESENTATION |
46-
DisplayManager.VIRTUAL_DISPLAY_FLAG_PUBLIC;
4742

48-
mVirtualDisplay = manager.createVirtualDisplay("OffscreenViews", aWidth, aHeight,
49-
mDefaultMetrics.densityDpi, mSurface, flags);
50-
mPresentation = new OffscreenPresentation(mContext, mVirtualDisplay.getDisplay());
51-
mPresentation.show();
43+
onResume();
5244
}
5345

5446
public void setContentView(View aView) {
55-
if (mPresentation == null) {
56-
throw new IllegalStateException("No presentation!");
47+
mContentView = aView;
48+
if (mPresentation != null) {
49+
mPresentation.setContentView(aView);
5750
}
58-
59-
mPresentation.setContentView(aView);
6051
}
6152

6253
public void resize(int aWidth, int aHeight) {
@@ -67,7 +58,7 @@ public void resize(int aWidth, int aHeight) {
6758
mVirtualDisplay.resize(aWidth, aHeight, mDefaultMetrics.densityDpi);
6859
}
6960

70-
public void release() {
61+
public void onPause() {
7162
if (mPresentation != null) {
7263
mPresentation.dismiss();
7364
mPresentation = null;
@@ -78,6 +69,38 @@ public void release() {
7869
mVirtualDisplay = null;
7970
}
8071

72+
73+
if (mContentView != null && mContentView.getParent() != null) {
74+
((ViewGroup)mContentView.getParent()).removeView(mContentView);
75+
}
76+
}
77+
78+
public void onResume() {
79+
if (mVirtualDisplay == null) {
80+
DisplayManager manager = (DisplayManager) mContext.getSystemService(Context.DISPLAY_SERVICE);
81+
Display defaultDisplay = manager.getDisplay(Display.DEFAULT_DISPLAY);
82+
83+
int flags = DisplayManager.VIRTUAL_DISPLAY_FLAG_OWN_CONTENT_ONLY |
84+
DisplayManager.VIRTUAL_DISPLAY_FLAG_PRESENTATION |
85+
DisplayManager.VIRTUAL_DISPLAY_FLAG_PUBLIC;
86+
defaultDisplay.getMetrics(mDefaultMetrics);
87+
88+
mVirtualDisplay = manager.createVirtualDisplay("OffscreenViews", mWidth, mHeight,
89+
mDefaultMetrics.densityDpi, mSurface, flags);
90+
}
91+
92+
if (mPresentation == null) {
93+
mPresentation = new OffscreenPresentation(mContext, mVirtualDisplay.getDisplay());
94+
mPresentation.show();
95+
if (mContentView != null) {
96+
mPresentation.setContentView(mContentView);
97+
}
98+
}
99+
}
100+
101+
public void release() {
102+
onPause();
103+
81104
if (mSurface != null) {
82105
mSurface.release();
83106
}

0 commit comments

Comments
 (0)