From 2274c84df3ef0a9c505c290575eca6fdf8e73e71 Mon Sep 17 00:00:00 2001 From: tibbi Date: Tue, 31 Mar 2020 20:59:20 +0200 Subject: [PATCH] add a query for fetching recordings created by this app --- .../voicerecorder/fragments/PlayerFragment.kt | 48 +++++++++++++++++++ .../voicerecorder/models/Recording.kt | 3 ++ 2 files changed, 51 insertions(+) create mode 100644 app/src/main/kotlin/com/simplemobiletools/voicerecorder/models/Recording.kt 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 bdf5d55..4573828 100644 --- a/app/src/main/kotlin/com/simplemobiletools/voicerecorder/fragments/PlayerFragment.kt +++ b/app/src/main/kotlin/com/simplemobiletools/voicerecorder/fragments/PlayerFragment.kt @@ -1,11 +1,59 @@ package com.simplemobiletools.voicerecorder.fragments +import android.annotation.SuppressLint import android.content.Context +import android.database.Cursor +import android.os.Build +import android.provider.MediaStore import android.util.AttributeSet +import androidx.annotation.RequiresApi +import com.simplemobiletools.commons.extensions.getIntValue +import com.simplemobiletools.commons.extensions.getStringValue +import com.simplemobiletools.commons.extensions.showErrorToast +import com.simplemobiletools.voicerecorder.models.Recording class PlayerFragment(context: Context, attributeSet: AttributeSet) : MyViewPagerFragment(context, attributeSet) { override fun onResume() {} override fun onDestroy() {} + + override fun onAttachedToWindow() { + super.onAttachedToWindow() + getRecordings() + } + + @SuppressLint("InlinedApi") + private fun getRecordings(): ArrayList { + val recordings = ArrayList() + + val uri = MediaStore.Audio.Media.getContentUri(MediaStore.VOLUME_EXTERNAL_PRIMARY) + val projection = arrayOf( + MediaStore.Audio.Media.DISPLAY_NAME, + MediaStore.Audio.Media._ID + ) + + val selection = "${MediaStore.Audio.Media.OWNER_PACKAGE_NAME} = ?" + val selectionArgs = arrayOf(context.packageName) + val sorting = "${MediaStore.Audio.Media.DATE_ADDED} DESC" + + var cursor: Cursor? = null + try { + cursor = context.contentResolver.query(uri, projection, selection, selectionArgs, sorting) + if (cursor?.moveToFirst() == true) { + do { + val title = cursor.getStringValue(MediaStore.Audio.Media.DISPLAY_NAME) + val id = cursor.getIntValue(MediaStore.Audio.Media._ID) + val recording = Recording(id, title, "") + recordings.add(recording) + } while (cursor.moveToNext()) + } + } catch (e: Exception) { + context.showErrorToast(e) + } finally { + cursor?.close() + } + + return recordings + } } diff --git a/app/src/main/kotlin/com/simplemobiletools/voicerecorder/models/Recording.kt b/app/src/main/kotlin/com/simplemobiletools/voicerecorder/models/Recording.kt new file mode 100644 index 0000000..3e3bcfb --- /dev/null +++ b/app/src/main/kotlin/com/simplemobiletools/voicerecorder/models/Recording.kt @@ -0,0 +1,3 @@ +package com.simplemobiletools.voicerecorder.models + +class Recording(id: Int, title: String, path: String)