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