From 7c6e44dd6928d3d34d47f487a60968bee5b3d558 Mon Sep 17 00:00:00 2001 From: Myzel394 <50424412+Myzel394@users.noreply.github.com> Date: Sat, 30 Dec 2023 21:04:24 +0100 Subject: [PATCH] refactor: Outsource into getOrCreateNewMediaFile method --- .../myzel394/alibi/helpers/BatchesFolder.kt | 60 +++++++++++++++++++ .../alibi/helpers/VideoBatchesFolder.kt | 59 ++---------------- 2 files changed, 66 insertions(+), 53 deletions(-) diff --git a/app/src/main/java/app/myzel394/alibi/helpers/BatchesFolder.kt b/app/src/main/java/app/myzel394/alibi/helpers/BatchesFolder.kt index c984635..58387f2 100644 --- a/app/src/main/java/app/myzel394/alibi/helpers/BatchesFolder.kt +++ b/app/src/main/java/app/myzel394/alibi/helpers/BatchesFolder.kt @@ -1,10 +1,13 @@ package app.myzel394.alibi.helpers +import android.content.ContentUris import app.myzel394.alibi.ui.MEDIA_RECORDINGS_PREFIX import android.content.Context import android.database.Cursor import android.net.Uri +import android.os.Build +import android.provider.MediaStore import android.provider.MediaStore.Video.Media import androidx.documentfile.provider.DocumentFile import java.io.File @@ -325,6 +328,63 @@ abstract class BatchesFolder( return File(getInternalFolder(), "$counter.$fileExtension") } + protected fun getOrCreateMediaFile( + name: String, + mimeType: String, + relativePath: String, + ): Uri { + // Check if already exists + var uri: Uri? = null + + context.contentResolver.query( + mediaContentUri, + arrayOf(MediaStore.MediaColumns._ID, MediaStore.MediaColumns.DISPLAY_NAME), + "${MediaStore.MediaColumns.DISPLAY_NAME} = '$name'", + null, + null, + )!!.use { cursor -> + if (cursor.moveToFirst()) { + // No need to check for the name since the query already did that + val id = cursor.getColumnIndex(MediaStore.MediaColumns._ID) + + if (id == -1) { + return@use + } + + uri = ContentUris.withAppendedId( + mediaContentUri, + cursor.getLong(id) + ) + } + } + + if (uri == null) { + // Create empty output file to be able to write to it + uri = context.contentResolver.insert( + mediaContentUri, + android.content.ContentValues().apply { + put( + MediaStore.MediaColumns.DISPLAY_NAME, + name + ) + put( + MediaStore.MediaColumns.MIME_TYPE, + mimeType + ) + + if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.Q) { + put( + Media.RELATIVE_PATH, + relativePath, + ) + } + } + )!! + } + + return uri!! + } + enum class BatchType { INTERNAL, CUSTOM, diff --git a/app/src/main/java/app/myzel394/alibi/helpers/VideoBatchesFolder.kt b/app/src/main/java/app/myzel394/alibi/helpers/VideoBatchesFolder.kt index 9cc7c59..ae91e63 100644 --- a/app/src/main/java/app/myzel394/alibi/helpers/VideoBatchesFolder.kt +++ b/app/src/main/java/app/myzel394/alibi/helpers/VideoBatchesFolder.kt @@ -1,9 +1,7 @@ package app.myzel394.alibi.helpers -import android.content.ContentUris import android.content.Context import android.net.Uri -import android.os.Build import android.os.Environment import android.os.ParcelFileDescriptor import android.provider.MediaStore @@ -47,60 +45,15 @@ class VideoBatchesFolder( } BatchType.MEDIA -> { - val name = getName(date, extension) - - // Check if already exists - var uri: Uri? = null - context.contentResolver.query( - mediaContentUri, - arrayOf(MediaStore.MediaColumns._ID, MediaStore.MediaColumns.DISPLAY_NAME), - // TODO: Improve - "${MediaStore.MediaColumns.DISPLAY_NAME} = '$name'", - null, - null, - )!!.use { cursor -> - if (cursor.moveToFirst()) { - // No need to check for the name since the query already did that - val id = cursor.getColumnIndex(MediaStore.MediaColumns._ID) - - if (id == -1) { - return@use - } - - uri = ContentUris.withAppendedId( - mediaContentUri, - cursor.getLong(id) - ) - } - } - - if (uri == null) { - // Create empty output file to be able to write to it - uri = context.contentResolver.insert( - mediaContentUri, - android.content.ContentValues().apply { - put( - MediaStore.MediaColumns.DISPLAY_NAME, - name - ) - put( - MediaStore.MediaColumns.MIME_TYPE, - "video/$extension" - ) - - if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.Q) { - put( - MediaStore.Video.Media.RELATIVE_PATH, - MEDIA_RELATIVE_PATH, - ) - } - } - )!! - } + val mediaUri = getOrCreateMediaFile( + name = getName(date, extension), + mimeType = "video/$extension", + relativePath = MEDIA_RELATIVE_PATH, + ) FFmpegKitConfig.getSafParameterForWrite( context, - uri + mediaUri )!! } }