package com.urkob.wittrail_android import android.Manifest import android.content.Intent import android.content.pm.PackageManager import android.os.Build import android.os.Bundle import android.util.Log import android.widget.Button import android.widget.Toast import androidx.activity.result.contract.ActivityResultContracts import androidx.annotation.RequiresApi import androidx.appcompat.app.AppCompatActivity import androidx.core.app.ActivityCompat import androidx.core.content.ContextCompat class MainActivity : AppCompatActivity() { companion object { private const val REQUEST_CAMERA_PERMISSION = 101 private val REQUIRED_PERMISSIONS = mutableListOf ( Manifest.permission.CAMERA, Manifest.permission.RECORD_AUDIO ).apply { if (Build.VERSION.SDK_INT <= Build.VERSION_CODES.P) { add(Manifest.permission.WRITE_EXTERNAL_STORAGE) } }.toTypedArray() } private var useFrontCamera: Boolean = false private lateinit var startFrontCameraButton: Button private lateinit var startBackCameraButton: Button private lateinit var stopCameraButton: Button @RequiresApi(Build.VERSION_CODES.O) override fun onCreate(savedInstanceState: Bundle?) { super.onCreate(savedInstanceState) setContentView(R.layout.activity_main) startFrontCameraButton = findViewById(R.id.startFrontCameraButton) startBackCameraButton = findViewById(R.id.startBackCameraButton) stopCameraButton = findViewById(R.id.stopCameraButton) if (!allPermissionsGranted()) { requestPermissions() } else { Log.e("main", "INIT START CAMERA") initStartCamera() } } private fun allPermissionsGranted() = REQUIRED_PERMISSIONS.all { ContextCompat.checkSelfPermission( baseContext, it) == PackageManager.PERMISSION_GRANTED } @RequiresApi(Build.VERSION_CODES.O) private fun requestPermissions() { activityResultLauncher.launch(REQUIRED_PERMISSIONS) } @RequiresApi(Build.VERSION_CODES.O) private val activityResultLauncher = registerForActivityResult( ActivityResultContracts.RequestMultiplePermissions()) { permissions -> // Handle Permission granted/rejected var permissionGranted = true permissions.entries.forEach { if (it.key in REQUIRED_PERMISSIONS && !it.value) permissionGranted = false } if (!permissionGranted) { Toast.makeText(baseContext, "Permission request denied", Toast.LENGTH_SHORT).show() } else { Log.e("main", "INIT START CAMERA") initStartCamera() } } private fun hasCameraPermission() = ContextCompat.checkSelfPermission(this, Manifest.permission.CAMERA) == PackageManager.PERMISSION_GRANTED private fun requestCameraPermission() { ActivityCompat.requestPermissions( this, arrayOf(Manifest.permission.CAMERA), REQUEST_CAMERA_PERMISSION ) } @RequiresApi(Build.VERSION_CODES.O) private fun initStartCamera(){ startFrontCameraButton.setOnClickListener { useFrontCamera = true if (hasCameraPermission()) { startRecordingService(useFrontCamera) } else { requestCameraPermission() } } startBackCameraButton.setOnClickListener { useFrontCamera = false if (hasCameraPermission()) { startRecordingService(useFrontCamera) } else { requestCameraPermission() } } stopCameraButton.setOnClickListener { stopRecordingService() } } @RequiresApi(Build.VERSION_CODES.O) private fun startRecordingService(useFrontCamera: Boolean) { Log.e("main", "startRecordingService") val serviceIntent = Intent(this, RecordingService::class.java).apply { action = RecordingService.ACTION_START_RECORDING putExtra("useFrontCamera", useFrontCamera) } startForegroundService(serviceIntent) } private fun stopRecordingService() { Log.e("main", "stopRecordingService") val serviceIntent = Intent(this, RecordingService::class.java).apply { action = RecordingService.ACTION_STOP_RECORDING } stopService(serviceIntent) } }