Код: Выделить всё
import android.annotation.SuppressLint
import android.content.Context
import android.hardware.camera2.CameraManager
import kotlinx.coroutines.CoroutineScope
import kotlinx.coroutines.Dispatchers
import kotlinx.coroutines.Job
import kotlinx.coroutines.delay
import kotlinx.coroutines.isActive
import kotlinx.coroutines.launch
class FlashlightHelper(
var context: Context
) {
private val manager = context.getSystemService(Context.CAMERA_SERVICE) as CameraManager
private var cameraId: String? = null
private var job: Job? = null
companion object {
@SuppressLint("StaticFieldLeak")
private var instance: FlashlightHelper? = null
fun getInstance(
context: Context
): FlashlightHelper {
if (instance == null) {
instance = FlashlightHelper(context)
}
return instance!!
}
}
init {
kotlin.runCatching {
cameraId = manager.cameraIdList[0] ?: "0"
}
}
fun toggleSOS(
enable: Boolean,
timeOn: Int = 300,
timeOff: Int = 100,
numberOfFlashes: Int = -1,
callback: ((Boolean) -> Unit)? = null
) {
job?.cancel()
if (enable) {
job = CoroutineScope(Dispatchers.Default).launch {
if (numberOfFlashes
Подробнее здесь: [url]https://stackoverflow.com/questions/78308007/if-i-only-use-flashlight-do-i-need-to-ask-for-camera-permission[/url]