From 19b7d4c0197df0499813835acbd68ecc50130538 Mon Sep 17 00:00:00 2001 From: "Urko." Date: Tue, 30 Jan 2024 20:32:49 +0100 Subject: [PATCH] orchestrate initialization with callback --- .../urkob/wittrail_android/MainActivity.kt | 1 + .../wittrail_android/RecordingService.kt | 74 +++++++++++++++---- 2 files changed, 59 insertions(+), 16 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 7c8c905..f3f4bd9 100644 --- a/app/src/main/java/com/urkob/wittrail_android/MainActivity.kt +++ b/app/src/main/java/com/urkob/wittrail_android/MainActivity.kt @@ -130,6 +130,7 @@ class MainActivity : AppCompatActivity() { val serviceIntent = Intent(this, RecordingService::class.java).apply { action = RecordingService.ACTION_STOP_RECORDING } + // startService(serviceIntent) // Note: Using startService to send the stop action stopService(serviceIntent) } 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 8ae3180..c90a462 100644 --- a/app/src/main/java/com/urkob/wittrail_android/RecordingService.kt +++ b/app/src/main/java/com/urkob/wittrail_android/RecordingService.kt @@ -37,6 +37,19 @@ class RecordingService : LifecycleService() { private var videoCapture: VideoCapture? = null private var recording: Recording? = null private lateinit var cameraExecutor: ExecutorService + private var isCameraInitialized = false + + private var cameraInitializedCallback: CameraInitializedCallback? = null + + companion object { + const val ACTION_START_RECORDING = "com.urkob.wittrail_android.action.START_RECORDING" + const val ACTION_STOP_RECORDING = "com.urkob.wittrail_android.action.STOP_RECORDING" + private const val CHANNEL_ID = "ForegroundServiceChannel" + private const val NOTIFICATION_ID = 1 + } + private fun setCameraInitializedCallback(callback: CameraInitializedCallback) { + this.cameraInitializedCallback = callback + } override fun onCreate() { super.onCreate() @@ -54,9 +67,15 @@ class RecordingService : LifecycleService() { .build() videoCapture = VideoCapture.withOutput(recorder) + Log.e(tag, "videoCapture initialized") // Now we can start the camera startCamera() + isCameraInitialized = true + + isCameraInitialized = true + cameraInitializedCallback?.onCameraInitialized() + cameraInitializedCallback = null }, ContextCompat.getMainExecutor(this)) Log.e(tag, "END onCreate") @@ -74,7 +93,8 @@ class RecordingService : LifecycleService() { } } - private fun createNotification(): Notification { + + private fun createNotification(): Notification { val notificationIntent = Intent(this, MainActivity::class.java) val pendingIntent = PendingIntent.getActivity( this, @@ -92,17 +112,23 @@ class RecordingService : LifecycleService() { .build() } - override fun onStartCommand(intent: Intent?, flags: Int, startId: Int): Int { useFrontCamera = intent?.getBooleanExtra("useFrontCamera", false) ?: false when (intent?.action) { + ACTION_START_RECORDING -> { - Log.e(tag, ACTION_START_RECORDING) - captureVideo() + if (isCameraInitialized) { + captureVideo() + } else { + setCameraInitializedCallback(object : CameraInitializedCallback { + override fun onCameraInitialized() { + captureVideo() + } + }) + } } ACTION_STOP_RECORDING -> { Log.e(tag, ACTION_STOP_RECORDING) - stopRecording() // return START_NOT_STICKY } @@ -113,6 +139,11 @@ class RecordingService : LifecycleService() { // 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() // } @@ -135,8 +166,19 @@ class RecordingService : LifecycleService() { // Implements VideoCapture use case, including start and stop capturing. private fun captureVideo() { + if (!isCameraInitialized) { + Log.e(tag, "Camera not initialized yet.") + // Optionally, queue this request or set a flag to try again later + return + } + Log.e(tag, "START CAPTURE VIDEO") - val videoCapture = this.videoCapture ?: return + + Log.e(tag, "Attempting to start video capture") + val videoCapture = this.videoCapture ?: run { + Log.e(tag, "VideoCapture not initialized") + return + } val curRecording = recording if (curRecording != null) { @@ -192,22 +234,22 @@ class RecordingService : LifecycleService() { } } } - companion object { - const val ACTION_START_RECORDING = "com.urkob.wittrail_android.action.START_RECORDING" - const val ACTION_STOP_RECORDING = "com.urkob.wittrail_android.action.STOP_RECORDING" - private const val CHANNEL_ID = "ForegroundServiceChannel" - private const val NOTIFICATION_ID = 1 - } - private fun stopRecording() { - Log.e(tag, "stopRecording") + private fun stopRecording() { + Log.e(tag, "Attempting to stop recording") val curRecording = recording if (curRecording != null) { - Log.e(tag, "stopRecording curRecording != null Stop the current recording session") curRecording.stop() recording = null - return + Log.e(tag, "Recording stopped. Now stopping service.") + stopSelf() // Add this line to stop the service. + } else { + Log.e(tag, "No active recording to stop") } } + } +interface CameraInitializedCallback { + fun onCameraInitialized() +}