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.camera.integration.avsync
18
19 import android.annotation.SuppressLint
20 import android.os.Build
21 import android.os.Bundle
22 import android.view.WindowManager
23 import androidx.activity.ComponentActivity
24 import androidx.activity.compose.setContent
25 import androidx.annotation.OptIn
26 import androidx.annotation.RequiresApi
27 import androidx.camera.camera2.Camera2Config
28 import androidx.camera.camera2.pipe.integration.CameraPipeConfig
29 import androidx.camera.integration.avsync.model.CameraHelper.Companion.CameraImplementation
30 import androidx.camera.lifecycle.ExperimentalCameraProviderConfiguration
31 import androidx.camera.lifecycle.ProcessCameraProvider
32 import androidx.compose.material.MaterialTheme
33 import androidx.compose.runtime.Composable
34 import androidx.core.util.Preconditions
35
36 private const val KEY_BEEP_FREQUENCY = "beep_frequency"
37 private const val KEY_BEEP_ENABLED = "beep_enabled"
38 private const val KEY_CAMERA_IMPLEMENTATION = "camera_implementation"
39 private const val DEFAULT_BEEP_FREQUENCY = 1500
40 private const val DEFAULT_BEEP_ENABLED = true
41 private const val CAMERA_IMPLEMENTATION_CAMERA2 = "camera2"
42 private const val CAMERA_IMPLEMENTATION_CAMERA_PIPE = "camera_pipe"
43 private const val MIN_SCREEN_BRIGHTNESS = 0F
44 private const val MAX_SCREEN_BRIGHTNESS = 1F
45 private const val DEFAULT_SCREEN_BRIGHTNESS = 0.8F
46 private val DEFAULT_CAMERA_IMPLEMENTATION = CameraImplementation.CAMERA2
47
48 class MainActivity : ComponentActivity() {
49
onCreatenull50 override fun onCreate(savedInstanceState: Bundle?) {
51 super.onCreate(savedInstanceState)
52
53 handleScreenLock()
54 setScreenBrightness()
55
56 // Config CameraX.
57 val cameraImplementation = getCameraImplementation()
58 if (!isCameraXConfigured) {
59 isCameraXConfigured = true
60 configureCameraProvider(cameraImplementation)
61 }
62
63 setContent { App(getBeepFrequency(), getBeepEnabled(), cameraImplementation) }
64 }
65
handleScreenLocknull66 private fun handleScreenLock() {
67 if (Build.VERSION.SDK_INT >= 27) {
68 Api27Impl.setShowWhenLocked(this, true)
69 Api27Impl.setTurnScreenOn(this, true)
70 window.addFlags(WindowManager.LayoutParams.FLAG_KEEP_SCREEN_ON)
71 } else {
72 @Suppress("DEPRECATION")
73 window.addFlags(
74 WindowManager.LayoutParams.FLAG_SHOW_WHEN_LOCKED or
75 WindowManager.LayoutParams.FLAG_KEEP_SCREEN_ON or
76 WindowManager.LayoutParams.FLAG_TURN_SCREEN_ON
77 )
78 }
79 }
80
getBeepFrequencynull81 private fun getBeepFrequency(): Int {
82 val frequency = intent.getStringExtra(KEY_BEEP_FREQUENCY)
83
84 if (frequency != null) {
85 try {
86 return Integer.parseInt(frequency)
87 } catch (e: NumberFormatException) {
88 e.printStackTrace()
89 }
90 }
91
92 return DEFAULT_BEEP_FREQUENCY
93 }
94
getBeepEnablednull95 private fun getBeepEnabled(): Boolean {
96 return intent.getBooleanExtra(KEY_BEEP_ENABLED, DEFAULT_BEEP_ENABLED)
97 }
98
getCameraImplementationnull99 private fun getCameraImplementation(): CameraImplementation {
100 val implementation = intent.getStringExtra(KEY_CAMERA_IMPLEMENTATION)
101 return if (CAMERA_IMPLEMENTATION_CAMERA2.equals(implementation, true)) {
102 CameraImplementation.CAMERA2
103 } else if (CAMERA_IMPLEMENTATION_CAMERA_PIPE.equals(implementation, true)) {
104 CameraImplementation.CAMERA_PIPE
105 } else {
106 DEFAULT_CAMERA_IMPLEMENTATION
107 }
108 }
109
110 @SuppressLint("NullAnnotationGroup")
111 @OptIn(ExperimentalCameraProviderConfiguration::class)
configureCameraProvidernull112 private fun configureCameraProvider(cameraImplementation: CameraImplementation) {
113 val config =
114 when (cameraImplementation) {
115 CameraImplementation.CAMERA2 -> Camera2Config.defaultConfig()
116 CameraImplementation.CAMERA_PIPE -> CameraPipeConfig.defaultConfig()
117 }
118 ProcessCameraProvider.configureInstance(config)
119 }
120
setScreenBrightnessnull121 private fun setScreenBrightness(brightness: Float = DEFAULT_SCREEN_BRIGHTNESS) {
122 Preconditions.checkArgument(brightness in MIN_SCREEN_BRIGHTNESS..MAX_SCREEN_BRIGHTNESS)
123
124 val layoutParam = window.attributes
125 layoutParam.screenBrightness = brightness
126 window.attributes = layoutParam
127 }
128
129 @RequiresApi(27)
130 private object Api27Impl {
setShowWhenLockednull131 fun setShowWhenLocked(activity: ComponentActivity, showWhenLocked: Boolean) =
132 activity.setShowWhenLocked(showWhenLocked)
133
134 fun setTurnScreenOn(activity: ComponentActivity, turnScreenOn: Boolean) =
135 activity.setTurnScreenOn(turnScreenOn)
136 }
137
138 companion object {
139 private var isCameraXConfigured: Boolean = false
140 }
141 }
142
143 @Composable
Appnull144 fun App(beepFrequency: Int, beepEnabled: Boolean, cameraImplementation: CameraImplementation) {
145 MaterialTheme { SignalGeneratorScreen(beepFrequency, beepEnabled, cameraImplementation) }
146 }
147