1 /**
2  * Copyright (C) 2022 The Android Open Source Project
3  *
4  * Licensed under the Apache License, Version 2.0 (the "License");
5  * you may not use this file except in compliance with the License.
6  * You may obtain a copy of the License at
7  *
8  *      http://www.apache.org/licenses/LICENSE-2.0
9  *
10  * Unless required by applicable law or agreed to in writing, software
11  * distributed under the License is distributed on an "AS IS" BASIS,
12  * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13  * See the License for the specific language governing permissions and
14  * limitations under the License.
15  */
16 package android.view.cts;
17 
18 import android.app.Activity;
19 import android.graphics.SurfaceTexture;
20 import android.os.Bundle;
21 import android.view.SurfaceHolder;
22 import android.view.SurfaceView;
23 import android.view.TextureView;
24 import android.view.TextureView.SurfaceTextureListener;
25 import android.view.ViewGroup.LayoutParams;
26 import android.widget.FrameLayout;
27 
28 import java.util.concurrent.CountDownLatch;
29 import java.util.concurrent.TimeUnit;
30 import java.util.concurrent.TimeoutException;
31 
32 public class SDRTestActivity extends Activity
33         implements SurfaceHolder.Callback, SurfaceTextureListener {
34     private static final long TIME_OUT_MS = 1000;
35     private final Object mLock = new Object();
36     private SurfaceView mSurfaceView;
37     private TextureView mTextureView;
38     private SurfaceTexture mSurface;
39 
40     private CountDownLatch mEnterAnimationFence = new CountDownLatch(1);
41 
42     @Override
surfaceCreated(SurfaceHolder holder)43     public void surfaceCreated(SurfaceHolder holder) {}
44 
45     @Override
surfaceChanged(SurfaceHolder holder, int format, int width, int height)46     public void surfaceChanged(SurfaceHolder holder, int format, int width, int height) {}
47 
48     @Override
surfaceDestroyed(SurfaceHolder holder)49     public void surfaceDestroyed(SurfaceHolder holder) {}
50 
51     @Override
onCreate(Bundle savedInstanceState)52     protected void onCreate(Bundle savedInstanceState) {
53         super.onCreate(savedInstanceState);
54 
55         mTextureView = new TextureView(this);
56         mSurfaceView = new SurfaceView(this);
57         mTextureView.setSurfaceTextureListener(this);
58 
59         FrameLayout content = new FrameLayout(this);
60         content.addView(mSurfaceView,
61                 LayoutParams.WRAP_CONTENT, LayoutParams.WRAP_CONTENT);
62         content.addView(mTextureView,
63                 LayoutParams.WRAP_CONTENT, LayoutParams.WRAP_CONTENT);
64 
65         mSurfaceView.getHolder().addCallback(this);
66         setContentView(content);
67     }
68 
69     @Override
onDestroy()70     protected void onDestroy() {
71         super.onDestroy();
72     }
73 
74     @Override
onEnterAnimationComplete()75     public void onEnterAnimationComplete() {
76         super.onEnterAnimationComplete();
77         mEnterAnimationFence.countDown();
78     }
79 
waitForEnterAnimationComplete()80     public void waitForEnterAnimationComplete() throws TimeoutException, InterruptedException {
81         if (!mEnterAnimationFence.await(TIME_OUT_MS, TimeUnit.MILLISECONDS)) {
82             throw new TimeoutException();
83         }
84     }
85 
getTextureView()86     public TextureView getTextureView() {
87         return mTextureView;
88     }
89 
getSurfaceView()90     public SurfaceView getSurfaceView() {
91         return mSurfaceView;
92     }
93 
94     @Override
onSurfaceTextureAvailable(SurfaceTexture surface, int width, int height)95     public void onSurfaceTextureAvailable(SurfaceTexture surface, int width, int height) {
96         synchronized (mLock) {
97             mSurface = surface;
98             mLock.notifyAll();
99         }
100     }
101 
102     @Override
onSurfaceTextureSizeChanged(SurfaceTexture surface, int width, int height)103     public void onSurfaceTextureSizeChanged(SurfaceTexture surface, int width, int height) {}
104 
105     @Override
onSurfaceTextureDestroyed(SurfaceTexture surface)106     public boolean onSurfaceTextureDestroyed(SurfaceTexture surface) {
107         synchronized (mLock) {
108             mSurface = null;
109             mLock.notifyAll();
110         }
111         return true;
112     }
113 
114     @Override
onSurfaceTextureUpdated(SurfaceTexture surface)115     public void onSurfaceTextureUpdated(SurfaceTexture surface) {
116         synchronized (mLock) {
117             mLock.notifyAll();
118         }
119     }
120 
waitForSurface()121     public void waitForSurface() throws InterruptedException {
122         synchronized (mLock) {
123             while (mSurface == null) {
124                 mLock.wait(TIME_OUT_MS);
125             }
126         }
127     }
128 }
129 
130