Recreate SurfaceTexture for CameraXTestActivity onResume

In the camera disconnect test, test step is
(1) Start CameraXTestActivity
(2) Start Camera2Activity, and CameraXTestActivity will
 going to background
(3) Close Camera2Activity, and CameraXTestActivity resume
For the step (2)&(3), CameraXTestActivity might be paused
and then resumed. The surfaceTexture might need to be
recreated for some specific hardware level devices.

Update the code to create a new surfaceTexture by requirement.

Bug: 183704747
Test: Manually pause/resume the CameraXTestActivity for testing
Change-Id: I576f1b7428b71096e2c325f56907d4b067a42046
diff --git a/camera/camera-testing/src/main/java/androidx/camera/testing/activity/CameraXTestActivity.java b/camera/camera-testing/src/main/java/androidx/camera/testing/activity/CameraXTestActivity.java
index 0952135..30e7dea 100644
--- a/camera/camera-testing/src/main/java/androidx/camera/testing/activity/CameraXTestActivity.java
+++ b/camera/camera-testing/src/main/java/androidx/camera/testing/activity/CameraXTestActivity.java
@@ -17,11 +17,13 @@
 package androidx.camera.testing.activity;
 
 
+import static androidx.camera.testing.SurfaceTextureProvider.createSurfaceTextureProvider;
+
 import android.graphics.SurfaceTexture;
 import android.os.Bundle;
 import android.util.Size;
-import android.view.Surface;
 import android.view.TextureView;
+import android.view.ViewGroup;
 
 import androidx.annotation.NonNull;
 import androidx.annotation.Nullable;
@@ -32,10 +34,10 @@
 import androidx.camera.core.Logger;
 import androidx.camera.core.Preview;
 import androidx.camera.core.impl.CameraInternal;
-import androidx.camera.core.impl.utils.executor.CameraXExecutors;
 import androidx.camera.core.internal.CameraUseCaseAdapter;
 import androidx.camera.testing.CameraUtil;
 import androidx.camera.testing.R;
+import androidx.camera.testing.SurfaceTextureProvider;
 import androidx.test.espresso.idling.CountingIdlingResource;
 
 import java.util.Collections;
@@ -113,7 +115,6 @@
                     public void onSurfaceTextureAvailable(@NonNull SurfaceTexture surfaceTexture,
                             int width, int height) {
                         Logger.d(TAG, "SurfaceTexture available");
-                        setSurfaceProvider(surfaceTexture);
                     }
 
                     @Override
@@ -138,6 +139,24 @@
                     }
                 });
 
+        mPreview.setSurfaceProvider(createSurfaceTextureProvider(
+                new SurfaceTextureProvider.SurfaceTextureCallback() {
+                    @Override
+                    public void onSurfaceTextureReady(@NonNull SurfaceTexture surfaceTexture,
+                            @NonNull Size resolution) {
+                        ViewGroup viewGroup = (ViewGroup) textureView.getParent();
+                        viewGroup.removeView(textureView);
+                        viewGroup.addView(textureView, resolution.getWidth(),
+                                resolution.getHeight());
+                        textureView.setSurfaceTexture(surfaceTexture);
+                    }
+
+                    @Override
+                    public void onSafeToRelease(@NonNull SurfaceTexture surfaceTexture) {
+                        surfaceTexture.release();
+                    }
+                }));
+
         try {
             final CameraX cameraX = CameraX.getOrCreateInstance(this).get();
             final LinkedHashSet<CameraInternal> cameras =
@@ -158,25 +177,6 @@
                 cameraSelector).getCameraInfoInternal().getCameraId();
     }
 
-    void setSurfaceProvider(@NonNull SurfaceTexture surfaceTexture) {
-        if (mPreview == null) {
-            return;
-        }
-        mPreview.setSurfaceProvider((surfaceRequest) -> {
-            final Size resolution = surfaceRequest.getResolution();
-            surfaceTexture.setDefaultBufferSize(resolution.getWidth(), resolution.getHeight());
-
-            final Surface surface = new Surface(surfaceTexture);
-            surfaceRequest.provideSurface(
-                    surface,
-                    CameraXExecutors.directExecutor(),
-                    (surfaceResponse) -> {
-                        surface.release();
-                        surfaceTexture.release();
-                    });
-        });
-    }
-
     @Nullable
     public String getCameraId() {
         return mCameraId;
diff --git a/camera/camera-testing/src/main/res/layout/activity_camera_main.xml b/camera/camera-testing/src/main/res/layout/activity_camera_main.xml
index fdc92c0..5a08f695 100644
--- a/camera/camera-testing/src/main/res/layout/activity_camera_main.xml
+++ b/camera/camera-testing/src/main/res/layout/activity_camera_main.xml
@@ -13,7 +13,7 @@
 See the License for the specific language governing permissions and
 limitations under the License.
 -->
-<RelativeLayout
+<FrameLayout
     xmlns:android="http://schemas.android.com/apk/res/android"
     android:layout_width="match_parent"
     android:layout_height="match_parent">
@@ -23,4 +23,4 @@
         android:layout_width="match_parent"
         android:layout_height="match_parent" />
 
-</RelativeLayout>
+</FrameLayout>