From b9f32eb1865a3c1b4585e2948c3352312c16c6d0 Mon Sep 17 00:00:00 2001
From: Lambada10 <62511588+Lambada10@users.noreply.github.com>
Date: Mon, 21 Oct 2024 21:17:21 +0200
Subject: [PATCH] feat: local song card opening music file
---
app/src/main/AndroidManifest.xml | 11 ++++++++
.../songsync/ui/components/SongCard.kt | 9 +++++-
.../songsync/util/MiscelaneousUtils.kt | 28 +++++++++++++++++++
app/src/main/res/xml/file_paths.xml | 3 ++
4 files changed, 50 insertions(+), 1 deletion(-)
create mode 100644 app/src/main/res/xml/file_paths.xml
diff --git a/app/src/main/AndroidManifest.xml b/app/src/main/AndroidManifest.xml
index ad9a8a8..988471b 100644
--- a/app/src/main/AndroidManifest.xml
+++ b/app/src/main/AndroidManifest.xml
@@ -35,6 +35,17 @@
+
+
+
+
+
\ No newline at end of file
diff --git a/app/src/main/java/pl/lambada/songsync/ui/components/SongCard.kt b/app/src/main/java/pl/lambada/songsync/ui/components/SongCard.kt
index 2b82b5f..0c34bfc 100644
--- a/app/src/main/java/pl/lambada/songsync/ui/components/SongCard.kt
+++ b/app/src/main/java/pl/lambada/songsync/ui/components/SongCard.kt
@@ -4,6 +4,7 @@ import androidx.compose.animation.AnimatedVisibilityScope
import androidx.compose.animation.ExperimentalSharedTransitionApi
import androidx.compose.animation.SharedTransitionScope
import androidx.compose.foundation.Image
+import androidx.compose.foundation.clickable
import androidx.compose.foundation.layout.Arrangement
import androidx.compose.foundation.layout.Column
import androidx.compose.foundation.layout.Row
@@ -28,6 +29,7 @@ import coil.compose.rememberAsyncImagePainter
import coil.imageLoader
import coil.request.ImageRequest
import pl.lambada.songsync.R
+import pl.lambada.songsync.util.openFileFromPath
@OptIn(ExperimentalSharedTransitionApi::class)
@Composable
@@ -41,11 +43,16 @@ fun SharedTransitionScope.SongCard(
animatedVisibilityScope: AnimatedVisibilityScope,
) {
val unknownArtistString = stringResource(R.string.unknown)
+ val context = LocalContext.current
OutlinedCard(
shape = RoundedCornerShape(10.dp),
modifier = CombinedModifier(
- outer = Modifier.fillMaxWidth(),
+ outer = Modifier
+ .fillMaxWidth()
+ .clickable(filePath != null) {
+ openFileFromPath(context, filePath!!)
+ },
inner = modifier
)
) {
diff --git a/app/src/main/java/pl/lambada/songsync/util/MiscelaneousUtils.kt b/app/src/main/java/pl/lambada/songsync/util/MiscelaneousUtils.kt
index 40d293b..bde2b66 100644
--- a/app/src/main/java/pl/lambada/songsync/util/MiscelaneousUtils.kt
+++ b/app/src/main/java/pl/lambada/songsync/util/MiscelaneousUtils.kt
@@ -1,8 +1,12 @@
package pl.lambada.songsync.util
import android.content.Context
+import android.content.Intent
+import android.net.Uri
import android.os.Build
import android.widget.Toast
+import androidx.core.content.FileProvider
+import java.io.File
fun isLegacyFileAccessRequired(filePath: String?): Boolean {
// Before Android 11, not in internal storage
@@ -10,6 +14,30 @@ fun isLegacyFileAccessRequired(filePath: String?): Boolean {
&& filePath?.contains("/storage/emulated/0/") == false
}
+fun openFileFromPath(context: Context, filePath: String) {
+ val file = File(filePath)
+ if (!file.exists()) {
+ showToast(context, "File does not exist")
+ return
+ }
+
+ val uri = if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.N) {
+ FileProvider.getUriForFile(context, context.packageName + ".provider", file)
+ } else {
+ Uri.fromFile(file)
+ }
+
+ val intent = Intent(Intent.ACTION_VIEW)
+ .setDataAndType(uri, "audio/mp3")
+ .addFlags(Intent.FLAG_GRANT_READ_URI_PERMISSION)
+
+ if (intent.resolveActivity(context.packageManager) != null) {
+ context.startActivity(intent)
+ } else {
+ Toast.makeText(context, "No app found to open the music file.", Toast.LENGTH_SHORT).show()
+ }
+}
+
fun showToast(context: Context, messageResId: Int, vararg args: Any, long: Boolean = true) {
Toast
.makeText(
diff --git a/app/src/main/res/xml/file_paths.xml b/app/src/main/res/xml/file_paths.xml
new file mode 100644
index 0000000..746a0cf
--- /dev/null
+++ b/app/src/main/res/xml/file_paths.xml
@@ -0,0 +1,3 @@
+
+
+
\ No newline at end of file