Add screen lock handling in A/V sync test app

Bug: 288056379
Test: manual test and ./gradlew bOS
Change-Id: I817628e9f787b76b91ca36bcd68039d19dfd6041
diff --git a/camera/integration-tests/avsynctestapp/src/main/java/androidx/camera/integration/avsync/MainActivity.kt b/camera/integration-tests/avsynctestapp/src/main/java/androidx/camera/integration/avsync/MainActivity.kt
index c7a330c..4d86552 100644
--- a/camera/integration-tests/avsynctestapp/src/main/java/androidx/camera/integration/avsync/MainActivity.kt
+++ b/camera/integration-tests/avsynctestapp/src/main/java/androidx/camera/integration/avsync/MainActivity.kt
@@ -16,9 +16,13 @@
 
 package androidx.camera.integration.avsync
 
+import android.os.Build
 import android.os.Bundle
+import android.view.WindowManager
 import androidx.activity.ComponentActivity
 import androidx.activity.compose.setContent
+import androidx.annotation.DoNotInline
+import androidx.annotation.RequiresApi
 import androidx.compose.material.MaterialTheme
 import androidx.compose.runtime.Composable
 import androidx.core.util.Preconditions
@@ -36,12 +40,30 @@
     override fun onCreate(savedInstanceState: Bundle?) {
         super.onCreate(savedInstanceState)
 
+        handleScreenLock()
         setScreenBrightness()
         setContent {
             App(getBeepFrequency(), getBeepEnabled())
         }
     }
 
+    private fun handleScreenLock() {
+        if (Build.VERSION.SDK_INT >= 27) {
+            Api27Impl.setShowWhenLocked(this, true)
+            Api27Impl.setTurnScreenOn(this, true)
+            window.addFlags(
+                WindowManager.LayoutParams.FLAG_KEEP_SCREEN_ON
+            )
+        } else {
+            @Suppress("DEPRECATION")
+            window.addFlags(
+                WindowManager.LayoutParams.FLAG_SHOW_WHEN_LOCKED
+                    or WindowManager.LayoutParams.FLAG_KEEP_SCREEN_ON
+                    or WindowManager.LayoutParams.FLAG_TURN_SCREEN_ON
+            )
+        }
+    }
+
     private fun getBeepFrequency(): Int {
         val frequency = intent.getStringExtra(KEY_BEEP_FREQUENCY)
 
@@ -67,6 +89,17 @@
         layoutParam.screenBrightness = brightness
         window.attributes = layoutParam
     }
+
+    @RequiresApi(27)
+    private object Api27Impl {
+        @DoNotInline
+        fun setShowWhenLocked(activity: ComponentActivity, showWhenLocked: Boolean) =
+            activity.setShowWhenLocked(showWhenLocked)
+
+        @DoNotInline
+        fun setTurnScreenOn(activity: ComponentActivity, turnScreenOn: Boolean) =
+            activity.setTurnScreenOn(turnScreenOn)
+    }
 }
 
 @Composable