From 5dee3a20bb2804fbb3cbb3f7a15f59f628d0f38a Mon Sep 17 00:00:00 2001 From: "Urko." Date: Wed, 31 Jan 2024 07:35:12 +0100 Subject: [PATCH] clean code and show/hide recording buttons --- .../urkob/wittrail_android/MainActivity.kt | 21 +++++++++++--- .../wittrail_android/RecordingService.kt | 29 +++++++------------ 2 files changed, 27 insertions(+), 23 deletions(-) diff --git a/app/src/main/java/com/urkob/wittrail_android/MainActivity.kt b/app/src/main/java/com/urkob/wittrail_android/MainActivity.kt index f3f4bd9..4d4e526 100644 --- a/app/src/main/java/com/urkob/wittrail_android/MainActivity.kt +++ b/app/src/main/java/com/urkob/wittrail_android/MainActivity.kt @@ -6,6 +6,7 @@ import android.content.pm.PackageManager import android.os.Build import android.os.Bundle import android.util.Log +import android.view.View import android.widget.Button import android.widget.Toast import androidx.activity.result.contract.ActivityResultContracts @@ -94,6 +95,7 @@ class MainActivity : AppCompatActivity() { private fun initStartCamera(){ startFrontCameraButton.setOnClickListener { useFrontCamera = true + manageRecordingButtonsVisibility(startRecording = true) if (hasCameraPermission()) { startRecordingService(useFrontCamera) } else { @@ -103,6 +105,7 @@ class MainActivity : AppCompatActivity() { startBackCameraButton.setOnClickListener { useFrontCamera = false + manageRecordingButtonsVisibility(startRecording = true) if (hasCameraPermission()) { startRecordingService(useFrontCamera) } else { @@ -111,6 +114,7 @@ class MainActivity : AppCompatActivity() { } stopCameraButton.setOnClickListener { + manageRecordingButtonsVisibility(startRecording = false) stopRecordingService() } } @@ -127,11 +131,20 @@ class MainActivity : AppCompatActivity() { private fun stopRecordingService() { Log.e("main", "stopRecordingService") - val serviceIntent = Intent(this, RecordingService::class.java).apply { - action = RecordingService.ACTION_STOP_RECORDING - } - // startService(serviceIntent) // Note: Using startService to send the stop action + val serviceIntent = Intent(this, RecordingService::class.java) stopService(serviceIntent) } + private fun manageRecordingButtonsVisibility(startRecording: Boolean) { + if (startRecording) { + startFrontCameraButton.visibility = View.GONE + startBackCameraButton.visibility = View.GONE + stopCameraButton.visibility = View.VISIBLE + } else { + startFrontCameraButton.visibility = View.VISIBLE + startBackCameraButton.visibility = View.VISIBLE + stopCameraButton.visibility = View.GONE + } + } + } diff --git a/app/src/main/java/com/urkob/wittrail_android/RecordingService.kt b/app/src/main/java/com/urkob/wittrail_android/RecordingService.kt index c90a462..80617f2 100644 --- a/app/src/main/java/com/urkob/wittrail_android/RecordingService.kt +++ b/app/src/main/java/com/urkob/wittrail_android/RecordingService.kt @@ -94,7 +94,7 @@ class RecordingService : LifecycleService() { } - private fun createNotification(): Notification { + private fun createNotification(): Notification { val notificationIntent = Intent(this, MainActivity::class.java) val pendingIntent = PendingIntent.getActivity( this, @@ -115,7 +115,6 @@ class RecordingService : LifecycleService() { override fun onStartCommand(intent: Intent?, flags: Int, startId: Int): Int { useFrontCamera = intent?.getBooleanExtra("useFrontCamera", false) ?: false when (intent?.action) { - ACTION_START_RECORDING -> { if (isCameraInitialized) { captureVideo() @@ -127,25 +126,18 @@ class RecordingService : LifecycleService() { }) } } - ACTION_STOP_RECORDING -> { - Log.e(tag, ACTION_STOP_RECORDING) - stopRecording() - // return START_NOT_STICKY - } } - // return START_STICKY return super.onStartCommand(intent, flags, startId) } -// override fun onDestroy() { -// Log.e(tag, "ON DESTROY IS CALLED SHOUlD STOP CAMERA") -// // Release resources -// stopRecording() -// cameraExecutor.shutdown() -// // Consider releasing cameraProvider here if needed -// Log.e(tag, "Resources released") -// super.onDestroy() -// } + override fun onDestroy() { + Log.e(tag, "ON DESTROY IS CALLED SHOUlD STOP CAMERA") + // Release resources + stopRecording() + cameraExecutor.shutdown() + Log.e(tag, "Resources released") + super.onDestroy() + } private fun startCamera() { Log.e(tag, "startCamera") @@ -242,12 +234,11 @@ class RecordingService : LifecycleService() { curRecording.stop() recording = null Log.e(tag, "Recording stopped. Now stopping service.") - stopSelf() // Add this line to stop the service. + stopSelf() // This will stop the service. } else { Log.e(tag, "No active recording to stop") } } - } interface CameraInitializedCallback {