From 8d668cc118349433cedcadbea2e1f9827de3954f Mon Sep 17 00:00:00 2001 From: Adam Brown Date: Wed, 27 Oct 2021 17:53:53 +0100 Subject: [PATCH 1/4] avoiding redundant session id filesDir opening, this provision is called multiple times each time we open a room --- .../org/matrix/android/sdk/internal/session/SessionModule.kt | 1 + 1 file changed, 1 insertion(+) diff --git a/matrix-sdk-android/src/main/java/org/matrix/android/sdk/internal/session/SessionModule.kt b/matrix-sdk-android/src/main/java/org/matrix/android/sdk/internal/session/SessionModule.kt index dc59277f64..ebc2176a13 100644 --- a/matrix-sdk-android/src/main/java/org/matrix/android/sdk/internal/session/SessionModule.kt +++ b/matrix-sdk-android/src/main/java/org/matrix/android/sdk/internal/session/SessionModule.kt @@ -163,6 +163,7 @@ internal abstract class SessionModule { @JvmStatic @Provides @SessionFilesDirectory + @SessionScope fun providesFilesDir(@UserMd5 userMd5: String, @SessionId sessionId: String, context: Context): File { From c2ce10f89c94f33225c6a88f0d0bca593dd2edbe Mon Sep 17 00:00:00 2001 From: Adam Brown Date: Wed, 27 Oct 2021 17:54:42 +0100 Subject: [PATCH 2/4] delaying working dir creation to when its needed, lazy is sychronised by default --- .../internal/session/sync/RoomSyncEphemeralTemporaryStore.kt | 5 +++-- 1 file changed, 3 insertions(+), 2 deletions(-) diff --git a/matrix-sdk-android/src/main/java/org/matrix/android/sdk/internal/session/sync/RoomSyncEphemeralTemporaryStore.kt b/matrix-sdk-android/src/main/java/org/matrix/android/sdk/internal/session/sync/RoomSyncEphemeralTemporaryStore.kt index e8f74bbd48..c95c3bff91 100644 --- a/matrix-sdk-android/src/main/java/org/matrix/android/sdk/internal/session/sync/RoomSyncEphemeralTemporaryStore.kt +++ b/matrix-sdk-android/src/main/java/org/matrix/android/sdk/internal/session/sync/RoomSyncEphemeralTemporaryStore.kt @@ -39,8 +39,9 @@ internal class RoomSyncEphemeralTemporaryStoreFile @Inject constructor( moshi: Moshi ) : RoomSyncEphemeralTemporaryStore { - private val workingDir = File(fileDirectory, "rr") - .also { it.mkdirs() } + private val workingDir: File by lazy { + File(fileDirectory, "rr").also { it.mkdirs() } + } private val roomSyncEphemeralAdapter = moshi.adapter(RoomSyncEphemeral::class.java) From 8525925732b8c34355a13284e9d47bdf3e614ba0 Mon Sep 17 00:00:00 2001 From: Adam Brown Date: Wed, 27 Oct 2021 17:58:48 +0100 Subject: [PATCH 3/4] 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() + } } } From 23f8d055226e65915eb6a17c6348de6202666dcd Mon Sep 17 00:00:00 2001 From: Adam Brown Date: Thu, 28 Oct 2021 13:35:39 +0100 Subject: [PATCH 4/4] removing redundant exists() checks, mkdirs already does this --- .../internal/session/sync/RoomSyncEphemeralTemporaryStore.kt | 4 +++- .../im/vector/app/features/voice/AbstractVoiceRecorder.kt | 4 +--- .../java/im/vector/app/features/voice/VoicePlayerHelper.kt | 4 +--- 3 files changed, 5 insertions(+), 7 deletions(-) diff --git a/matrix-sdk-android/src/main/java/org/matrix/android/sdk/internal/session/sync/RoomSyncEphemeralTemporaryStore.kt b/matrix-sdk-android/src/main/java/org/matrix/android/sdk/internal/session/sync/RoomSyncEphemeralTemporaryStore.kt index c95c3bff91..8c68e224dc 100644 --- a/matrix-sdk-android/src/main/java/org/matrix/android/sdk/internal/session/sync/RoomSyncEphemeralTemporaryStore.kt +++ b/matrix-sdk-android/src/main/java/org/matrix/android/sdk/internal/session/sync/RoomSyncEphemeralTemporaryStore.kt @@ -40,7 +40,9 @@ internal class RoomSyncEphemeralTemporaryStoreFile @Inject constructor( ) : RoomSyncEphemeralTemporaryStore { private val workingDir: File by lazy { - File(fileDirectory, "rr").also { it.mkdirs() } + File(fileDirectory, "rr").also { + it.mkdirs() + } } private val roomSyncEphemeralAdapter = moshi.adapter(RoomSyncEphemeral::class.java) 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 794130d528..786920aa22 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 @@ -28,9 +28,7 @@ abstract class AbstractVoiceRecorder( ) : VoiceRecorder { private val outputDirectory: File by lazy { File(context.cacheDir, "voice_records").also { - if (!it.exists()) { - it.mkdirs() - } + it.mkdirs() } } 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 64b06a72f8..d2f7927d75 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 @@ -29,9 +29,7 @@ class VoicePlayerHelper @Inject constructor( ) { private val outputDirectory: File by lazy { File(context.cacheDir, "voice_records").also { - if (!it.exists()) { - it.mkdirs() - } + it.mkdirs() } }