From 8525925732b8c34355a13284e9d47bdf3e614ba0 Mon Sep 17 00:00:00 2001 From: Adam Brown Date: Wed, 27 Oct 2021 17:58:48 +0100 Subject: [PATCH] lazily creating the voice directory references, avoid eagerly reading the file on the main thread --- .../app/features/voice/AbstractVoiceRecorder.kt | 14 +++++++------- .../vector/app/features/voice/VoicePlayerHelper.kt | 10 +++++----- 2 files changed, 12 insertions(+), 12 deletions(-) diff --git a/vector/src/main/java/im/vector/app/features/voice/AbstractVoiceRecorder.kt b/vector/src/main/java/im/vector/app/features/voice/AbstractVoiceRecorder.kt index 8a0f829f94..794130d528 100644 --- a/vector/src/main/java/im/vector/app/features/voice/AbstractVoiceRecorder.kt +++ b/vector/src/main/java/im/vector/app/features/voice/AbstractVoiceRecorder.kt @@ -26,17 +26,17 @@ abstract class AbstractVoiceRecorder( context: Context, private val filenameExt: String ) : VoiceRecorder { - private val outputDirectory = File(context.cacheDir, "voice_records") + private val outputDirectory: File by lazy { + File(context.cacheDir, "voice_records").also { + if (!it.exists()) { + it.mkdirs() + } + } + } private var mediaRecorder: MediaRecorder? = null private var outputFile: File? = null - init { - if (!outputDirectory.exists()) { - outputDirectory.mkdirs() - } - } - abstract fun setOutputFormat(mediaRecorder: MediaRecorder) abstract fun convertFile(recordedFile: File?): File? diff --git a/vector/src/main/java/im/vector/app/features/voice/VoicePlayerHelper.kt b/vector/src/main/java/im/vector/app/features/voice/VoicePlayerHelper.kt index f1b316c456..64b06a72f8 100644 --- a/vector/src/main/java/im/vector/app/features/voice/VoicePlayerHelper.kt +++ b/vector/src/main/java/im/vector/app/features/voice/VoicePlayerHelper.kt @@ -27,11 +27,11 @@ import javax.inject.Inject class VoicePlayerHelper @Inject constructor( context: Context ) { - private val outputDirectory = File(context.cacheDir, "voice_records") - - init { - if (!outputDirectory.exists()) { - outputDirectory.mkdirs() + private val outputDirectory: File by lazy { + File(context.cacheDir, "voice_records").also { + if (!it.exists()) { + it.mkdirs() + } } }