From 6da57797f4a24922298e904470a195c61b53a15c Mon Sep 17 00:00:00 2001 From: Suhas Dissanayake Date: Sat, 29 Jul 2023 15:39:47 +0530 Subject: [PATCH] Remove StorageHelper in favour of FileRepository --- .../receivers/FinishedNotificationReceiver.kt | 5 +- .../recorder/services/AudioRecorderService.kt | 5 +- .../services/LosslessRecorderService.kt | 10 ++-- .../services/ScreenRecorderService.kt | 8 ++- .../ui/components/NamingPatternPref.kt | 4 +- .../com/bnyro/recorder/util/FileRepository.kt | 50 +++++++++++++++++-- .../com/bnyro/recorder/util/StorageHelper.kt | 42 ---------------- 7 files changed, 63 insertions(+), 61 deletions(-) delete mode 100644 app/src/main/java/com/bnyro/recorder/util/StorageHelper.kt diff --git a/app/src/main/java/com/bnyro/recorder/receivers/FinishedNotificationReceiver.kt b/app/src/main/java/com/bnyro/recorder/receivers/FinishedNotificationReceiver.kt index 0e0b4e95..65e029ce 100644 --- a/app/src/main/java/com/bnyro/recorder/receivers/FinishedNotificationReceiver.kt +++ b/app/src/main/java/com/bnyro/recorder/receivers/FinishedNotificationReceiver.kt @@ -4,15 +4,16 @@ import android.content.BroadcastReceiver import android.content.Context import android.content.Intent import androidx.core.app.NotificationManagerCompat +import com.bnyro.recorder.App import com.bnyro.recorder.services.RecorderService import com.bnyro.recorder.util.IntentHelper import com.bnyro.recorder.util.NotificationHelper -import com.bnyro.recorder.util.StorageHelper class FinishedNotificationReceiver : BroadcastReceiver() { override fun onReceive(context: Context, intent: Intent) { val fileName = intent.getStringExtra(RecorderService.FILE_NAME_EXTRA_KEY) ?: return - val file = StorageHelper.getOutputDir(context).findFile(fileName) + val file = (context.applicationContext as App).container.fileRepository + .getOutputDir().findFile(fileName) when (intent.getStringExtra(RecorderService.ACTION_EXTRA_KEY)) { RecorderService.SHARE_ACTION -> file?.let { IntentHelper.shareFile(context, it) } diff --git a/app/src/main/java/com/bnyro/recorder/services/AudioRecorderService.kt b/app/src/main/java/com/bnyro/recorder/services/AudioRecorderService.kt index ab22541a..0591e62f 100644 --- a/app/src/main/java/com/bnyro/recorder/services/AudioRecorderService.kt +++ b/app/src/main/java/com/bnyro/recorder/services/AudioRecorderService.kt @@ -1,12 +1,12 @@ package com.bnyro.recorder.services +import com.bnyro.recorder.App import com.bnyro.recorder.R import com.bnyro.recorder.enums.AudioChannels import com.bnyro.recorder.enums.AudioDeviceSource import com.bnyro.recorder.obj.AudioFormat import com.bnyro.recorder.util.PlayerHelper import com.bnyro.recorder.util.Preferences -import com.bnyro.recorder.util.StorageHelper class AudioRecorderService : RecorderService() { override val notificationTitle: String @@ -38,8 +38,7 @@ class AudioRecorderService : RecorderService() { setOutputFormat(audioFormat.format) setAudioEncoder(audioFormat.codec) - outputFile = StorageHelper.getOutputFile( - this@AudioRecorderService, + outputFile = (application as App).container.fileRepository.getOutputFile( audioFormat.extension ) fileDescriptor = contentResolver.openFileDescriptor(outputFile!!.uri, "w") diff --git a/app/src/main/java/com/bnyro/recorder/services/LosslessRecorderService.kt b/app/src/main/java/com/bnyro/recorder/services/LosslessRecorderService.kt index 2ba04f63..edaabb6d 100644 --- a/app/src/main/java/com/bnyro/recorder/services/LosslessRecorderService.kt +++ b/app/src/main/java/com/bnyro/recorder/services/LosslessRecorderService.kt @@ -7,10 +7,10 @@ import android.media.MediaRecorder import android.os.Build import androidx.annotation.RequiresApi import androidx.documentfile.provider.DocumentFile +import com.bnyro.recorder.App import com.bnyro.recorder.R import com.bnyro.recorder.enums.RecorderState import com.bnyro.recorder.util.PcmConverter -import com.bnyro.recorder.util.StorageHelper import java.io.File import kotlin.concurrent.thread import kotlin.experimental.and @@ -110,9 +110,11 @@ class LosslessRecorderService : RecorderService() { private fun convertToWav() { val inputStream = contentResolver.openInputStream(outputFile?.uri ?: return) ?: return - val outputStream = StorageHelper.getOutputFile(this, FILE_NAME_EXTENSION_WAV).let { - contentResolver.openOutputStream(it.uri) ?: return - } + val outputStream = (application as App).container.fileRepository + .getOutputFile(FILE_NAME_EXTENSION_WAV) + .let { + contentResolver.openOutputStream(it.uri) ?: return + } pcmConverter?.convertToWave(inputStream, outputStream, BUFFER_SIZE_IN_BYTES) outputFile?.delete() } diff --git a/app/src/main/java/com/bnyro/recorder/services/ScreenRecorderService.kt b/app/src/main/java/com/bnyro/recorder/services/ScreenRecorderService.kt index ce57e48e..20ab3cb2 100644 --- a/app/src/main/java/com/bnyro/recorder/services/ScreenRecorderService.kt +++ b/app/src/main/java/com/bnyro/recorder/services/ScreenRecorderService.kt @@ -13,6 +13,7 @@ import android.util.DisplayMetrics import android.util.Log import android.view.Display import androidx.activity.result.ActivityResult +import com.bnyro.recorder.App import com.bnyro.recorder.R import com.bnyro.recorder.enums.AudioChannels import com.bnyro.recorder.enums.AudioDeviceSource @@ -21,7 +22,6 @@ import com.bnyro.recorder.enums.VideoFormat import com.bnyro.recorder.obj.VideoResolution import com.bnyro.recorder.util.PlayerHelper import com.bnyro.recorder.util.Preferences -import com.bnyro.recorder.util.StorageHelper class ScreenRecorderService : RecorderService() { override val notificationTitle: String @@ -115,10 +115,8 @@ class ScreenRecorderService : RecorderService() { null ) - outputFile = StorageHelper.getOutputFile( - this@ScreenRecorderService, - videoFormat.extension - ) + outputFile = (application as App).container.fileRepository + .getOutputFile(videoFormat.extension) fileDescriptor = contentResolver.openFileDescriptor(outputFile!!.uri, "w") setOutputFile(fileDescriptor?.fileDescriptor) diff --git a/app/src/main/java/com/bnyro/recorder/ui/components/NamingPatternPref.kt b/app/src/main/java/com/bnyro/recorder/ui/components/NamingPatternPref.kt index 20fb9ea4..c69db8c8 100644 --- a/app/src/main/java/com/bnyro/recorder/ui/components/NamingPatternPref.kt +++ b/app/src/main/java/com/bnyro/recorder/ui/components/NamingPatternPref.kt @@ -14,8 +14,8 @@ import androidx.compose.ui.unit.dp import androidx.compose.ui.unit.sp import com.bnyro.recorder.R import com.bnyro.recorder.ui.common.DialogButton +import com.bnyro.recorder.util.FileRepositoryImpl import com.bnyro.recorder.util.Preferences -import com.bnyro.recorder.util.StorageHelper @Composable fun NamingPatternPref() { @@ -48,7 +48,7 @@ fun NamingPatternPref() { mutableStateOf( Preferences.getString( Preferences.namingPatternKey, - StorageHelper.DEFAULT_NAMING_PATTERN + FileRepositoryImpl.DEFAULT_NAMING_PATTERN ) ) } diff --git a/app/src/main/java/com/bnyro/recorder/util/FileRepository.kt b/app/src/main/java/com/bnyro/recorder/util/FileRepository.kt index edff9ee9..5b6d7a1d 100644 --- a/app/src/main/java/com/bnyro/recorder/util/FileRepository.kt +++ b/app/src/main/java/com/bnyro/recorder/util/FileRepository.kt @@ -1,28 +1,34 @@ package com.bnyro.recorder.util +import android.annotation.SuppressLint import android.content.Context import android.media.MediaMetadataRetriever +import android.net.Uri import androidx.documentfile.provider.DocumentFile import com.bnyro.recorder.enums.RecorderType import com.bnyro.recorder.enums.SortOrder import com.bnyro.recorder.obj.RecordingItemData +import java.text.SimpleDateFormat +import java.util.Calendar interface FileRepository { suspend fun getVideoRecordingItems(sortOrder: SortOrder): List suspend fun getAudioRecordingItems(sortOrder: SortOrder): List suspend fun deleteSelectedFiles(files: List) suspend fun deleteAllFiles() + fun getOutputFile(extension: String): DocumentFile + fun getOutputDir(): DocumentFile } class FileRepositoryImpl(val context: Context) : FileRepository { private fun getVideoFiles(): List = - StorageHelper.getOutputDir(context).listFiles().filter { + getOutputDir().listFiles().filter { it.isFile && it.name.orEmpty().endsWith("mp4") } private fun getAudioFiles(): List = - StorageHelper.getOutputDir(context).listFiles().filter { + getOutputDir().listFiles().filter { it.isFile && !it.name.orEmpty().endsWith("mp4") } @@ -50,10 +56,48 @@ class FileRepositoryImpl(val context: Context) : FileRepository { } override suspend fun deleteAllFiles() { - StorageHelper.getOutputDir(context).listFiles().forEach { + getOutputDir().listFiles().forEach { if (it.isFile) it.delete() } } + + override fun getOutputFile(extension: String): DocumentFile { + val currentTimeMillis = Calendar.getInstance().time + val currentDateTime = dateTimeFormat.format(currentTimeMillis) + val currentDate = currentDateTime.split("_").first() + val currentTime = currentDateTime.split("_").last() + + val fileName = Preferences.getString( + Preferences.namingPatternKey, + DEFAULT_NAMING_PATTERN + ) + .replace("%d", currentDate) + .replace("%t", currentTime) + .replace("%m", currentTimeMillis.time.toString()) + .replace("%s", currentTimeMillis.time.div(1000).toString()) + + val recordingFile = getOutputDir().createFile("audio/*", "$fileName.$extension") + return recordingFile!! + } + + override fun getOutputDir(): DocumentFile { + val prefDir = Preferences.prefs.getString(Preferences.targetFolderKey, "") + val audioDir = when { + prefDir.isNullOrBlank() -> { + val dir = context.getExternalFilesDir(null) ?: context.filesDir + DocumentFile.fromFile(dir) + } + + else -> DocumentFile.fromTreeUri(context, Uri.parse(prefDir)) + } + return audioDir!! + } + + companion object { + @SuppressLint("SimpleDateFormat") + private val dateTimeFormat = SimpleDateFormat("yyyy-MM-dd_HH-mm-ss") + const val DEFAULT_NAMING_PATTERN = "%d_%t" + } } fun List.sortedBy(sortOrder: SortOrder): List { diff --git a/app/src/main/java/com/bnyro/recorder/util/StorageHelper.kt b/app/src/main/java/com/bnyro/recorder/util/StorageHelper.kt deleted file mode 100644 index 077ae225..00000000 --- a/app/src/main/java/com/bnyro/recorder/util/StorageHelper.kt +++ /dev/null @@ -1,42 +0,0 @@ -package com.bnyro.recorder.util - -import android.annotation.SuppressLint -import android.content.Context -import android.net.Uri -import androidx.documentfile.provider.DocumentFile -import java.text.SimpleDateFormat -import java.util.Calendar - -object StorageHelper { - @SuppressLint("SimpleDateFormat") - private val dateTimeFormat = SimpleDateFormat("yyyy-MM-dd_HH-mm-ss") - const val DEFAULT_NAMING_PATTERN = "%d_%t" - - fun getOutputFile(context: Context, extension: String): DocumentFile { - val currentTimeMillis = Calendar.getInstance().time - val currentDateTime = dateTimeFormat.format(currentTimeMillis) - val currentDate = currentDateTime.split("_").first() - val currentTime = currentDateTime.split("_").last() - - val fileName = Preferences.getString(Preferences.namingPatternKey, DEFAULT_NAMING_PATTERN) - .replace("%d", currentDate) - .replace("%t", currentTime) - .replace("%m", currentTimeMillis.time.toString()) - .replace("%s", currentTimeMillis.time.div(1000).toString()) - - val recordingFile = getOutputDir(context).createFile("audio/*", "$fileName.$extension") - return recordingFile!! - } - - fun getOutputDir(context: Context): DocumentFile { - val prefDir = Preferences.prefs.getString(Preferences.targetFolderKey, "") - val audioDir = when { - prefDir.isNullOrBlank() -> { - val dir = context.getExternalFilesDir(null) ?: context.filesDir - DocumentFile.fromFile(dir) - } - else -> DocumentFile.fromTreeUri(context, Uri.parse(prefDir)) - } - return audioDir!! - } -}