1 /*
2  * Copyright 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 
17 package androidx.graphics.surface
18 
19 import android.app.Activity
20 import android.os.Bundle
21 import android.view.Gravity
22 import android.view.SurfaceHolder
23 import android.view.SurfaceView
24 import android.widget.FrameLayout
25 
26 class SurfaceControlWrapperTestActivity : Activity() {
27     lateinit var mSurfaceView: SurfaceView
28     lateinit var mFrameLayout: FrameLayout
29     lateinit var mLayoutParams: FrameLayout.LayoutParams
30 
31     companion object {
32         var DEFAULT_WIDTH = 100
33         var DEFAULT_HEIGHT = 100
34     }
35 
36     private var mDestroyCallback: (() -> Unit)? = null
37 
setDestroyCallbacknull38     fun setDestroyCallback(callback: () -> Unit) {
39         mDestroyCallback = callback
40     }
41 
onCreatenull42     override fun onCreate(savedInstanceState: Bundle?) {
43         super.onCreate(savedInstanceState)
44 
45         mLayoutParams =
46             FrameLayout.LayoutParams(DEFAULT_WIDTH, DEFAULT_HEIGHT, Gravity.LEFT or Gravity.TOP)
47         mLayoutParams.topMargin = 100
48         mLayoutParams.leftMargin = 100
49 
50         mFrameLayout = FrameLayout(this)
51         mSurfaceView = SurfaceView(this)
52         mSurfaceView.holder.setFixedSize(DEFAULT_WIDTH, DEFAULT_HEIGHT)
53         setContentView(mFrameLayout)
54     }
55 
addSurfacenull56     fun addSurface(surfaceView: SurfaceView, callback: SurfaceHolder.Callback) {
57         surfaceView.holder.addCallback(callback)
58         mFrameLayout.addView(surfaceView, mLayoutParams)
59     }
60 
getSurfaceViewnull61     fun getSurfaceView(): SurfaceView {
62         return mSurfaceView
63     }
64 
onDestroynull65     override fun onDestroy() {
66         super.onDestroy()
67         mDestroyCallback?.invoke()
68     }
69 }
70