From 14e676816ed94721e7273119e66cbd41011d3e58 Mon Sep 17 00:00:00 2001 From: Jyotiraditya Panda Date: Sun, 11 Aug 2024 16:57:20 +0530 Subject: [PATCH] Allow sharing tracks (#236) Signed-off-by: Jyotiraditya Panda --- app/src/main/AndroidManifest.xml | 10 ++++ .../gramophone/ui/adapters/SongAdapter.kt | 48 +++++++++++++++---- app/src/main/res/menu/more_menu.xml | 3 +- app/src/main/res/xml/file_paths.xml | 6 +++ 4 files changed, 56 insertions(+), 11 deletions(-) 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 464c6424d..d64173617 100644 --- a/app/src/main/AndroidManifest.xml +++ b/app/src/main/AndroidManifest.xml @@ -103,6 +103,16 @@ + + + + \ No newline at end of file diff --git a/app/src/main/kotlin/org/akanework/gramophone/ui/adapters/SongAdapter.kt b/app/src/main/kotlin/org/akanework/gramophone/ui/adapters/SongAdapter.kt index eed045927..4a5ab9938 100644 --- a/app/src/main/kotlin/org/akanework/gramophone/ui/adapters/SongAdapter.kt +++ b/app/src/main/kotlin/org/akanework/gramophone/ui/adapters/SongAdapter.kt @@ -19,13 +19,17 @@ package org.akanework.gramophone.ui.adapters import android.net.Uri import android.view.View +import android.widget.Toast import androidx.appcompat.widget.PopupMenu +import androidx.core.app.ShareCompat +import androidx.core.content.FileProvider import androidx.fragment.app.Fragment import androidx.fragment.app.activityViewModels import androidx.lifecycle.MutableLiveData import androidx.media3.common.C import androidx.media3.common.MediaItem import androidx.media3.common.Player +import java.io.File import kotlinx.coroutines.CoroutineScope import kotlinx.coroutines.Dispatchers import kotlinx.coroutines.launch @@ -310,15 +314,41 @@ class SongAdapter( true }*/ - /* - R.id.share -> { - val builder = ShareCompat.IntentBuilder(mainActivity) - val mimeTypes = mutableSetOf() - builder.addStream(viewModel.fileUriList.value?.get(songList[holder.bindingAdapterPosition].mediaId.toLong())!!) - mimeTypes.add(viewModel.mimeTypeList.value?.get(songList[holder.bindingAdapterPosition].mediaId.toLong())!!) - builder.setType(mimeTypes.singleOrNull() ?: "audio/*").startChooser() - } */ - */ + R.id.share -> { + val mediaItem = viewModel.mediaItemList.value?.find { + it.mediaId == item.mediaId + } ?: return@setOnMenuItemClickListener true + + val uri = mediaItem.requestMetadata.mediaUri + ?: mediaItem.localConfiguration?.uri + ?: return@setOnMenuItemClickListener true + + val mimeType = mediaItem.mediaMetadata.extras?.getString("MIME_TYPE") + ?: "audio/*" + + try { + val contentUri = if (uri.scheme == "file") { + FileProvider.getUriForFile( + mainActivity, + "${mainActivity.packageName}.fileProvider", + File(uri.path!!) + ) + } else uri + + ShareCompat.IntentBuilder(mainActivity) + .setType(mimeType) + .setStream(contentUri) + .setChooserTitle("Share audio file") + .startChooser() + } catch (e: Exception) { + Toast.makeText( + mainActivity, + "Unable to share: ${e.message}", + Toast.LENGTH_SHORT + ).show() + } + true + } else -> false } } diff --git a/app/src/main/res/menu/more_menu.xml b/app/src/main/res/menu/more_menu.xml index 9621ed8c7..31a9863f7 100644 --- a/app/src/main/res/menu/more_menu.xml +++ b/app/src/main/res/menu/more_menu.xml @@ -15,8 +15,7 @@ - + android:title="@string/share" /> \ No newline at end of file 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 000000000..fafa14f89 --- /dev/null +++ b/app/src/main/res/xml/file_paths.xml @@ -0,0 +1,6 @@ + + + + \ No newline at end of file