From 09b87221fe27dc2e11b91484b81d4d32bf94457b Mon Sep 17 00:00:00 2001 From: Luca Stefani Date: Tue, 5 Nov 2024 21:51:41 +0100 Subject: [PATCH] Recorder: Use File instead of Path We always end up converting to File anyways Change-Id: I92c48ca93baf87e778fc98794381bfaacaa263e1 --- .../repository/RecordingsRepository.kt | 19 ++++++------- .../recorder/service/GoodQualityRecorder.kt | 6 ++--- .../recorder/service/HighQualityRecorder.kt | 5 ++-- .../recorder/service/SoundRecorderService.kt | 27 +++++++++---------- .../recorder/service/SoundRecording.kt | 4 +-- 5 files changed, 30 insertions(+), 31 deletions(-) diff --git a/app/src/main/java/org/lineageos/recorder/repository/RecordingsRepository.kt b/app/src/main/java/org/lineageos/recorder/repository/RecordingsRepository.kt index d29c230d..6d741586 100644 --- a/app/src/main/java/org/lineageos/recorder/repository/RecordingsRepository.kt +++ b/app/src/main/java/org/lineageos/recorder/repository/RecordingsRepository.kt @@ -13,10 +13,9 @@ import android.util.Log import kotlinx.coroutines.Dispatchers import kotlinx.coroutines.withContext import org.lineageos.recorder.flow.RecordingsFlow +import java.io.File import java.io.FileOutputStream import java.io.IOException -import java.nio.file.Files -import java.nio.file.Path object RecordingsRepository { private val LOG_TAG = this::class.simpleName!! @@ -31,15 +30,15 @@ object RecordingsRepository { fun recordings(context: Context) = RecordingsFlow(context).flowData() suspend fun addRecordingToContentProvider( - context: Context, path: Path, mimeType: String + context: Context, file: File, mimeType: String ) = withContext(Dispatchers.IO) { val contentResolver = context.contentResolver val uri = contentResolver.insert( MediaStore.Audio.Media.getContentUri(MediaStore.VOLUME_EXTERNAL_PRIMARY), - buildCv(path, mimeType) + buildCv(file, mimeType) ) ?: run { - Log.e(LOG_TAG, "Failed to insert ${path.toAbsolutePath()}") + Log.e(LOG_TAG, "Failed to insert ${file.absoluteFile}") return@withContext null } @@ -49,14 +48,16 @@ object RecordingsRepository { uri, "w", null )?.use { pfd -> FileOutputStream(pfd.fileDescriptor).use { oStream -> - Files.copy(path, oStream) + file.inputStream().use { iStream -> + iStream.copyTo(oStream) + } } val values = ContentValues().apply { put(MediaStore.MediaColumns.IS_PENDING, 0) } contentResolver.update(uri, values, null, null) try { - Files.delete(path) + file.delete() } catch (e: IOException) { Log.w(LOG_TAG, "Failed to delete tmp file") } @@ -70,8 +71,8 @@ object RecordingsRepository { } } - private fun buildCv(path: Path, mimeType: String) = ContentValues().apply { - val name = path.fileName.toString() + private fun buildCv(file: File, mimeType: String) = ContentValues().apply { + val name = file.name put(MediaStore.Audio.Media.DISPLAY_NAME, name) put(MediaStore.Audio.Media.TITLE, name) diff --git a/app/src/main/java/org/lineageos/recorder/service/GoodQualityRecorder.kt b/app/src/main/java/org/lineageos/recorder/service/GoodQualityRecorder.kt index a626d9c5..1f7cd06a 100644 --- a/app/src/main/java/org/lineageos/recorder/service/GoodQualityRecorder.kt +++ b/app/src/main/java/org/lineageos/recorder/service/GoodQualityRecorder.kt @@ -10,8 +10,8 @@ import android.content.Context import android.media.MediaRecorder import android.os.Build import androidx.annotation.RequiresPermission +import java.io.File import java.io.IOException -import java.nio.file.Path class GoodQualityRecorder(private val context: Context) : SoundRecording { private var recorder: MediaRecorder? = null @@ -19,14 +19,14 @@ class GoodQualityRecorder(private val context: Context) : SoundRecording { @RequiresPermission(permission.RECORD_AUDIO) @Throws(IOException::class) - override fun startRecording(path: Path) { + override fun startRecording(file: File) { recorder = (if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.S) { MediaRecorder(context) } else { @Suppress("Deprecation") MediaRecorder() }).apply { - setOutputFile(path.toFile()) + setOutputFile(file) setAudioSource(MediaRecorder.AudioSource.DEFAULT) setOutputFormat(MediaRecorder.OutputFormat.MPEG_4) setAudioEncoder(MediaRecorder.AudioEncoder.AAC) diff --git a/app/src/main/java/org/lineageos/recorder/service/HighQualityRecorder.kt b/app/src/main/java/org/lineageos/recorder/service/HighQualityRecorder.kt index c71982ff..ce3b2b3a 100644 --- a/app/src/main/java/org/lineageos/recorder/service/HighQualityRecorder.kt +++ b/app/src/main/java/org/lineageos/recorder/service/HighQualityRecorder.kt @@ -17,7 +17,6 @@ import java.io.FileOutputStream import java.io.IOException import java.nio.ByteBuffer import java.nio.ByteOrder -import java.nio.file.Path import kotlin.math.abs class HighQualityRecorder : SoundRecording { @@ -27,8 +26,8 @@ class HighQualityRecorder : SoundRecording { private var isRecording = false @RequiresPermission(permission.RECORD_AUDIO) - override fun startRecording(path: Path) { - this.file = path.toFile() + override fun startRecording(file: File) { + this.file = file val audioFormat = AudioFormat.Builder() .setSampleRate(SAMPLING_RATE) diff --git a/app/src/main/java/org/lineageos/recorder/service/SoundRecorderService.kt b/app/src/main/java/org/lineageos/recorder/service/SoundRecorderService.kt index d586f3c5..c10f7928 100644 --- a/app/src/main/java/org/lineageos/recorder/service/SoundRecorderService.kt +++ b/app/src/main/java/org/lineageos/recorder/service/SoundRecorderService.kt @@ -40,10 +40,9 @@ import org.lineageos.recorder.models.UiStatus import org.lineageos.recorder.repository.RecordingsRepository import org.lineageos.recorder.utils.PreferencesManager import org.lineageos.recorder.utils.RecordIntentHelper +import java.io.File import java.io.IOException import java.lang.ref.WeakReference -import java.nio.file.Files -import java.nio.file.Path import java.util.Timer import java.util.TimerTask @@ -82,7 +81,7 @@ class SoundRecorderService : LifecycleService() { } private val messenger = Messenger(handler) private var recorder: SoundRecording? = null - private var recordPath: Path? = null + private var recordFile: File? = null private var amplitudeTimer: Timer? = null private var elapsedTimeTimer: Timer? = null private var isPaused = false @@ -163,17 +162,17 @@ class SoundRecorderService : LifecycleService() { } return recorder?.let { recorder -> - val optPath = createNewAudioFile(fileName, recorder.fileExtension) ?: run { + val file = createNewAudioFile(fileName, recorder.fileExtension) ?: run { Log.e(TAG, "Failed to prepare output file") return@let false } - this.recordPath = optPath + this.recordFile = file isPaused = false elapsedTime = 0 try { - recorder.startRecording(optPath) + recorder.startRecording(file) } catch (e: IOException) { Log.e(TAG, "Error while starting the recorder", e) return@let false @@ -202,7 +201,7 @@ class SoundRecorderService : LifecycleService() { val success = recorder.stopRecording() - return recordPath?.takeIf { success }?.let { + return recordFile?.takeIf { success }?.let { lifecycleScope.launch { RecordingsRepository.addRecordingToContentProvider( this@SoundRecorderService, @@ -299,26 +298,26 @@ class SoundRecorderService : LifecycleService() { private fun createNewAudioFile( fileName: String, extension: String - ): Path? { + ): File? { val recordingDir = if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.S) { - getExternalFilesDir(Environment.DIRECTORY_RECORDINGS)?.toPath() + getExternalFilesDir(Environment.DIRECTORY_RECORDINGS) } else { - getExternalFilesDir(Environment.DIRECTORY_MUSIC)?.toPath() + getExternalFilesDir(Environment.DIRECTORY_MUSIC) ?.resolve(LEGACY_MUSIC_DIR) } ?: throw Exception("Null external files dir") - val path = recordingDir.resolve(String.format(fileName, extension)) + val file = recordingDir.resolve(String.format(fileName, extension)) - if (!Files.exists(recordingDir)) { + if (!recordingDir.exists()) { try { - Files.createDirectories(recordingDir) + recordingDir.mkdirs() } catch (e: IOException) { Log.e(TAG, "Failed to create parent directories for output") return null } } - return path + return file } /* Timers */ diff --git a/app/src/main/java/org/lineageos/recorder/service/SoundRecording.kt b/app/src/main/java/org/lineageos/recorder/service/SoundRecording.kt index 51fdefa8..d2566d30 100644 --- a/app/src/main/java/org/lineageos/recorder/service/SoundRecording.kt +++ b/app/src/main/java/org/lineageos/recorder/service/SoundRecording.kt @@ -7,13 +7,13 @@ package org.lineageos.recorder.service import android.Manifest.permission import androidx.annotation.RequiresPermission +import java.io.File import java.io.IOException -import java.nio.file.Path interface SoundRecording { @RequiresPermission(permission.RECORD_AUDIO) @Throws(IOException::class) - fun startRecording(path: Path) + fun startRecording(file: File) fun stopRecording(): Boolean