Donate to e Foundation | Murena handsets with /e/OS | Own a part of Murena! Learn more

Unverified Commit 09b87221 authored by Luca Stefani's avatar Luca Stefani
Browse files

Recorder: Use File instead of Path

We always end up converting to File anyways

Change-Id: I92c48ca93baf87e778fc98794381bfaacaa263e1
parent 229da7c8
Loading
Loading
Loading
Loading
+10 −9
Original line number Diff line number Diff line
@@ -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)
+3 −3
Original line number Diff line number Diff line
@@ -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)
+2 −3
Original line number Diff line number Diff line
@@ -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)
+13 −14
Original line number Diff line number Diff line
@@ -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 */
+2 −2
Original line number Diff line number Diff line
@@ -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