Skip to content

Commit

Permalink
Release Virtual Display in onPause (#1332)
Browse files Browse the repository at this point in the history
  • Loading branch information
MortimerGoro committed Jun 14, 2019
1 parent 4b30c53 commit d1ab92f
Show file tree
Hide file tree
Showing 2 changed files with 49 additions and 20 deletions.
Expand Up @@ -311,11 +311,17 @@ protected void onPause() {
mConnectivityReceiver.unregister(this);
// Reset so the dialog will show again on resume.
mConnectionAvailable = true;
if (mOffscreenDisplay != null) {
mOffscreenDisplay.onPause();
}
super.onPause();
}

@Override
protected void onResume() {
if (mOffscreenDisplay != null) {
mOffscreenDisplay.onResume();
}
SessionStore.get().setActive(true);
mAudioEngine.resumeEngine();
for (Widget widget: mWidgets.values()) {
Expand Down
Expand Up @@ -7,56 +7,47 @@

import android.app.Presentation;
import android.content.Context;
import android.content.DialogInterface;
import android.graphics.SurfaceTexture;
import android.hardware.display.DisplayManager;
import android.hardware.display.VirtualDisplay;
import android.os.Bundle;
import android.util.DisplayMetrics;
import android.view.Display;
import android.view.LayoutInflater;
import android.view.Surface;
import android.view.View;
import android.util.Log;
import android.view.ViewGroup;

public class OffscreenDisplay {
final String LOGTAG = "VRB";
private Context mContext;
private int mWidth;
private int mHeight;
private VirtualDisplay mVirtualDisplay;
private SurfaceTexture mTexture;
private Surface mSurface;
private OffscreenPresentation mPresentation;
private View mContentView;

private DisplayMetrics mDefaultMetrics;

public OffscreenDisplay(Context aContext, SurfaceTexture aTexture, int aWidth, int aHeight) {
mContext = aContext;
mWidth = aWidth;
mHeight = aHeight;
aTexture.setDefaultBufferSize(aWidth, aHeight);
mTexture = aTexture;
mSurface = new Surface(aTexture);

DisplayManager manager = (DisplayManager) aContext.getSystemService(Context.DISPLAY_SERVICE);
Display defaultDisplay = manager.getDisplay(Display.DEFAULT_DISPLAY);

mDefaultMetrics = new DisplayMetrics();
defaultDisplay.getMetrics(mDefaultMetrics);

int flags = DisplayManager.VIRTUAL_DISPLAY_FLAG_OWN_CONTENT_ONLY |
DisplayManager.VIRTUAL_DISPLAY_FLAG_PRESENTATION |
DisplayManager.VIRTUAL_DISPLAY_FLAG_PUBLIC;

mVirtualDisplay = manager.createVirtualDisplay("OffscreenViews", aWidth, aHeight,
mDefaultMetrics.densityDpi, mSurface, flags);
mPresentation = new OffscreenPresentation(mContext, mVirtualDisplay.getDisplay());
mPresentation.show();
onResume();
}

public void setContentView(View aView) {
if (mPresentation == null) {
throw new IllegalStateException("No presentation!");
mContentView = aView;
if (mPresentation != null) {
mPresentation.setContentView(aView);
}

mPresentation.setContentView(aView);
}

public void resize(int aWidth, int aHeight) {
Expand All @@ -67,7 +58,7 @@ public void resize(int aWidth, int aHeight) {
mVirtualDisplay.resize(aWidth, aHeight, mDefaultMetrics.densityDpi);
}

public void release() {
public void onPause() {
if (mPresentation != null) {
mPresentation.dismiss();
mPresentation = null;
Expand All @@ -78,6 +69,38 @@ public void release() {
mVirtualDisplay = null;
}


if (mContentView != null && mContentView.getParent() != null) {
((ViewGroup)mContentView.getParent()).removeView(mContentView);
}
}

public void onResume() {
if (mVirtualDisplay == null) {
DisplayManager manager = (DisplayManager) mContext.getSystemService(Context.DISPLAY_SERVICE);
Display defaultDisplay = manager.getDisplay(Display.DEFAULT_DISPLAY);

int flags = DisplayManager.VIRTUAL_DISPLAY_FLAG_OWN_CONTENT_ONLY |
DisplayManager.VIRTUAL_DISPLAY_FLAG_PRESENTATION |
DisplayManager.VIRTUAL_DISPLAY_FLAG_PUBLIC;
defaultDisplay.getMetrics(mDefaultMetrics);

mVirtualDisplay = manager.createVirtualDisplay("OffscreenViews", mWidth, mHeight,
mDefaultMetrics.densityDpi, mSurface, flags);
}

if (mPresentation == null) {
mPresentation = new OffscreenPresentation(mContext, mVirtualDisplay.getDisplay());
mPresentation.show();
if (mContentView != null) {
mPresentation.setContentView(mContentView);
}
}
}

public void release() {
onPause();

if (mSurface != null) {
mSurface.release();
}
Expand Down

0 comments on commit d1ab92f

Please sign in to comment.