| 1 |
/* |
| 2 |
* Copyright (C) 2020 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 com.android.test |
| 17 |
|
| 18 |
import android.content.Context |
| 19 |
import android.content.pm.ActivityInfo |
| 20 |
import android.content.res.Configuration.ORIENTATION_LANDSCAPE |
| 21 |
import android.graphics.Color |
| 22 |
import android.graphics.Paint |
| 23 |
import android.graphics.Point |
| 24 |
import android.graphics.Rect |
| 25 |
import android.os.Bundle |
| 26 |
import android.view.Gravity |
| 27 |
import android.view.Surface |
| 28 |
import android.view.SurfaceHolder |
| 29 |
import android.view.SurfaceView |
| 30 |
import android.view.View |
| 31 |
import android.view.WindowManager |
| 32 |
import android.view.cts.surfacevalidator.CapturedActivity |
| 33 |
import android.widget.FrameLayout |
| 34 |
import java.util.concurrent.CountDownLatch |
| 35 |
import java.util.concurrent.locks.ReentrantLock |
| 36 |
import kotlin.concurrent.withLock |
| 37 |
|
| 38 |
class MainActivity : CapturedActivity() { |
| 39 |
val mSurfaceProxy = SurfaceProxy() |
| 40 |
private var mSurfaceHolder: SurfaceHolder? = null |
| 41 |
private val mDrawLock = ReentrantLock() |
| 42 |
var mSurfaceView: SurfaceView? = null |
| 43 |
private var mCountDownLatch: CountDownLatch? = null |
| 44 |
|
| 45 |
val surface: Surface? get() = mSurfaceHolder?.surface |
| 46 |
|
| 47 |
override fun onCreate(savedInstanceState: Bundle?) { |
| 48 |
super.onCreate(savedInstanceState) |
| 49 |
window.decorView.apply { |
| 50 |
systemUiVisibility = |
| 51 |
View.SYSTEM_UI_FLAG_HIDE_NAVIGATION or View.SYSTEM_UI_FLAG_FULLSCREEN |
| 52 |
} |
| 53 |
} |
| 54 |
|
| 55 |
override fun getCaptureDurationMs(): Long { |
| 56 |
return 30000 |
| 57 |
} |
| 58 |
|
| 59 |
fun addSurfaceView(size: Point): CountDownLatch { |
| 60 |
val layout = findViewById<FrameLayout>(android.R.id.content) |
| 61 |
val surfaceReadyLatch = CountDownLatch(1) |
| 62 |
mSurfaceView = createSurfaceView(applicationContext, size, surfaceReadyLatch) |
| 63 |
layout!!.addView(mSurfaceView!!, |
| 64 |
FrameLayout.LayoutParams(size.x, size.y, Gravity.TOP or Gravity.LEFT) |
| 65 |
.also { it.setMargins(100, 100, 0, 0) }) |
| 66 |
|
| 67 |
return surfaceReadyLatch |
| 68 |
} |
| 69 |
|
| 70 |
fun resizeSurfaceView(size: Point): CountDownLatch { |
| 71 |
mCountDownLatch = CountDownLatch(1) |
| 72 |
mSurfaceView!!.layoutParams.also { |
| 73 |
it.width = size.x |
| 74 |
it.height = size.y |
| 75 |
} |
| 76 |
mSurfaceView!!.requestLayout() |
| 77 |
return mCountDownLatch!! |
| 78 |
} |
| 79 |
|
| 80 |
fun enableSeamlessRotation() { |
| 81 |
val p: WindowManager.LayoutParams = window.attributes |
| 82 |
p.rotationAnimation = WindowManager.LayoutParams.ROTATION_ANIMATION_SEAMLESS |
| 83 |
window.attributes = p |
| 84 |
} |
| 85 |
|
| 86 |
fun rotate90() { |
| 87 |
if (getResources().getConfiguration().orientation == ORIENTATION_LANDSCAPE) { |
| 88 |
setRequestedOrientation(ActivityInfo.SCREEN_ORIENTATION_PORTRAIT) |
| 89 |
} else { |
| 90 |
setRequestedOrientation(ActivityInfo.SCREEN_ORIENTATION_LANDSCAPE) |
| 91 |
} |
| 92 |
} |
| 93 |
|
| 94 |
private fun createSurfaceView( |
| 95 |
context: Context, |
| 96 |
size: Point, |
| 97 |
surfaceReadyLatch: CountDownLatch |
| 98 |
): SurfaceView { |
| 99 |
val surfaceView = SurfaceView(context) |
| 100 |
surfaceView.setWillNotDraw(false) |
| 101 |
surfaceView.holder.addCallback(object : SurfaceHolder.Callback { |
| 102 |
override fun surfaceCreated(holder: SurfaceHolder) { |
| 103 |
mDrawLock.withLock { |
| 104 |
mSurfaceHolder = holder |
| 105 |
mSurfaceProxy.setSurface(holder.surface) |
| 106 |
} |
| 107 |
surfaceReadyLatch.countDown() |
| 108 |
} |
| 109 |
|
| 110 |
override fun surfaceChanged( |
| 111 |
holder: SurfaceHolder, |
| 112 |
format: Int, |
| 113 |
width: Int, |
| 114 |
height: Int |
| 115 |
) { |
| 116 |
mCountDownLatch?.countDown() |
| 117 |
} |
| 118 |
|
| 119 |
override fun surfaceDestroyed(holder: SurfaceHolder) { |
| 120 |
mDrawLock.withLock { |
| 121 |
mSurfaceHolder = null |
| 122 |
} |
| 123 |
} |
| 124 |
}) |
| 125 |
return surfaceView |
| 126 |
} |
| 127 |
|
| 128 |
fun drawFrame(): Rect { |
| 129 |
mDrawLock.withLock { |
| 130 |
val holder = mSurfaceHolder ?: return Rect() |
| 131 |
val canvas = holder.lockCanvas() |
| 132 |
val canvasSize = Rect(0, 0, canvas.width, canvas.height) |
| 133 |
canvas.drawColor(Color.GREEN) |
| 134 |
val p = Paint() |
| 135 |
p.color = Color.RED |
| 136 |
canvas.drawRect(canvasSize, p) |
| 137 |
holder.unlockCanvasAndPost(canvas) |
| 138 |
return canvasSize |
| 139 |
} |
| 140 |
} |
| 141 |
} |