Skip to content

Commit

Permalink
feat: local song card opening music file
Browse files Browse the repository at this point in the history
  • Loading branch information
Lambada10 committed Oct 21, 2024
1 parent 47b10b5 commit b9f32eb
Show file tree
Hide file tree
Showing 4 changed files with 50 additions and 1 deletion.
11 changes: 11 additions & 0 deletions app/src/main/AndroidManifest.xml
Original file line number Diff line number Diff line change
Expand Up @@ -35,6 +35,17 @@
<category android:name="android.intent.category.LAUNCHER" />
</intent-filter>
</activity>

<provider
android:name="androidx.core.content.FileProvider"
android:authorities="${applicationId}.provider"
android:exported="false"
android:grantUriPermissions="true">
<meta-data
android:name="android.support.FILE_PROVIDER_PATHS"
android:resource="@xml/file_paths" />
</provider>

</application>

</manifest>
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand All @@ -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
Expand All @@ -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
)
) {
Expand Down
28 changes: 28 additions & 0 deletions app/src/main/java/pl/lambada/songsync/util/MiscelaneousUtils.kt
Original file line number Diff line number Diff line change
@@ -1,15 +1,43 @@
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
return Build.VERSION.SDK_INT < Build.VERSION_CODES.R
&& 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(
Expand Down
3 changes: 3 additions & 0 deletions app/src/main/res/xml/file_paths.xml
Original file line number Diff line number Diff line change
@@ -0,0 +1,3 @@
<paths>
<external-path name="external_files" path="." />
</paths>

0 comments on commit b9f32eb

Please sign in to comment.