From cd88ac13437468e69e43aebdd45f679a5e40d7b7 Mon Sep 17 00:00:00 2001 From: tibbi Date: Thu, 2 Apr 2020 15:30:32 +0200 Subject: [PATCH] adding a helper function for getting audio file uris --- .../dialogs/RenameRecordingDialog.kt | 6 ++--- .../voicerecorder/fragments/PlayerFragment.kt | 24 ++++--------------- .../voicerecorder/helpers/Constants.kt | 17 +++++++++++++ 3 files changed, 24 insertions(+), 23 deletions(-) diff --git a/app/src/main/kotlin/com/simplemobiletools/voicerecorder/dialogs/RenameRecordingDialog.kt b/app/src/main/kotlin/com/simplemobiletools/voicerecorder/dialogs/RenameRecordingDialog.kt index 7592ad9..9f80752 100644 --- a/app/src/main/kotlin/com/simplemobiletools/voicerecorder/dialogs/RenameRecordingDialog.kt +++ b/app/src/main/kotlin/com/simplemobiletools/voicerecorder/dialogs/RenameRecordingDialog.kt @@ -1,6 +1,5 @@ package com.simplemobiletools.voicerecorder.dialogs -import android.content.ContentUris import android.content.ContentValues import android.provider.MediaStore import androidx.appcompat.app.AlertDialog @@ -9,6 +8,7 @@ import com.simplemobiletools.commons.extensions.* import com.simplemobiletools.commons.helpers.ensureBackgroundThread import com.simplemobiletools.commons.helpers.isQPlus import com.simplemobiletools.voicerecorder.R +import com.simplemobiletools.voicerecorder.helpers.getAudioFileContentUri import com.simplemobiletools.voicerecorder.models.Recording import kotlinx.android.synthetic.main.dialog_rename_recording.view.* @@ -52,8 +52,6 @@ class RenameRecordingDialog(val activity: BaseSimpleActivity, val recording: Rec } private fun updateMediaStoreTitle(recording: Recording, newTitle: String) { - val baseUri = MediaStore.Audio.Media.getContentUri(MediaStore.VOLUME_EXTERNAL_PRIMARY) - val uri = ContentUris.withAppendedId(baseUri, recording.id.toLong()) val oldExtension = recording.title.getFilenameExtension() val newDisplayName = "${newTitle.removeSuffix(".$oldExtension")}.$oldExtension" @@ -62,6 +60,6 @@ class RenameRecordingDialog(val activity: BaseSimpleActivity, val recording: Rec put(MediaStore.Audio.Media.DISPLAY_NAME, newDisplayName) } - activity.contentResolver.update(uri, values, null, null) + activity.contentResolver.update(getAudioFileContentUri(recording.id.toLong()), values, null, null) } } diff --git a/app/src/main/kotlin/com/simplemobiletools/voicerecorder/fragments/PlayerFragment.kt b/app/src/main/kotlin/com/simplemobiletools/voicerecorder/fragments/PlayerFragment.kt index 1275ed9..f9a32f1 100644 --- a/app/src/main/kotlin/com/simplemobiletools/voicerecorder/fragments/PlayerFragment.kt +++ b/app/src/main/kotlin/com/simplemobiletools/voicerecorder/fragments/PlayerFragment.kt @@ -1,7 +1,6 @@ package com.simplemobiletools.voicerecorder.fragments import android.annotation.SuppressLint -import android.content.ContentUris import android.content.Context import android.database.Cursor import android.media.AudioManager @@ -18,6 +17,7 @@ import com.simplemobiletools.voicerecorder.R import com.simplemobiletools.voicerecorder.activities.SimpleActivity import com.simplemobiletools.voicerecorder.adapters.RecordingsAdapter import com.simplemobiletools.voicerecorder.extensions.config +import com.simplemobiletools.voicerecorder.helpers.getAudioFileContentUri import com.simplemobiletools.voicerecorder.interfaces.RefreshRecordingsListener import com.simplemobiletools.voicerecorder.models.Events import com.simplemobiletools.voicerecorder.models.Recording @@ -194,24 +194,15 @@ class PlayerFragment(context: Context, attributeSet: AttributeSet) : MyViewPager } private fun getDurationFromUri(id: Long): Long { - val recordingUri = ContentUris.withAppendedId( - MediaStore.Audio.Media.EXTERNAL_CONTENT_URI, - id - ) - val retriever = MediaMetadataRetriever() - retriever.setDataSource(context, recordingUri) + retriever.setDataSource(context, getAudioFileContentUri(id)) val time = retriever.extractMetadata(MediaMetadataRetriever.METADATA_KEY_DURATION) return Math.round(time.toLong() / 1000.toDouble()) } private fun getSizeFromUri(id: Long): Int { - val recordingUri = ContentUris.withAppendedId( - MediaStore.Audio.Media.EXTERNAL_CONTENT_URI, - id - ) - - return context.contentResolver.openInputStream(recordingUri)?.available()?.toInt() ?: 0 + val recordingUri = getAudioFileContentUri(id) + return context.contentResolver.openInputStream(recordingUri)?.available() ?: 0 } private fun initMediaPlayer() { @@ -234,17 +225,12 @@ class PlayerFragment(context: Context, attributeSet: AttributeSet) : MyViewPager } override fun playRecording(recording: Recording) { - val recordingUri = ContentUris.withAppendedId( - MediaStore.Audio.Media.EXTERNAL_CONTENT_URI, - recording.id.toLong() - ) - resetProgress(recording) (recordings_list.adapter as RecordingsAdapter).updateCurrentRecording(recording.id) player!!.apply { reset() - setDataSource(context, recordingUri) + setDataSource(context, getAudioFileContentUri(recording.id.toLong())) prepare() } diff --git a/app/src/main/kotlin/com/simplemobiletools/voicerecorder/helpers/Constants.kt b/app/src/main/kotlin/com/simplemobiletools/voicerecorder/helpers/Constants.kt index dbd84e7..ec7d5ef 100644 --- a/app/src/main/kotlin/com/simplemobiletools/voicerecorder/helpers/Constants.kt +++ b/app/src/main/kotlin/com/simplemobiletools/voicerecorder/helpers/Constants.kt @@ -1,5 +1,11 @@ package com.simplemobiletools.voicerecorder.helpers +import android.annotation.SuppressLint +import android.content.ContentUris +import android.net.Uri +import android.provider.MediaStore +import com.simplemobiletools.commons.helpers.isQPlus + const val RECORDER_RUNNING_NOTIF_ID = 10000 private const val PATH = "com.simplemobiletools.voicerecorder.action." @@ -9,3 +15,14 @@ const val STOP_AMPLITUDE_UPDATE = PATH + "STOP_AMPLITUDE_UPDATE" // shared preferences const val HIDE_NOTIFICATION = "hide_notification" const val SAVE_RECORDINGS = "save_recordings" + +@SuppressLint("InlinedApi") +fun getAudioFileContentUri(id: Long): Uri { + val baseUri = if (isQPlus()) { + MediaStore.Audio.Media.getContentUri(MediaStore.VOLUME_EXTERNAL_PRIMARY) + } else { + MediaStore.Audio.Media.EXTERNAL_CONTENT_URI + } + + return ContentUris.withAppendedId(baseUri, id) +}