Skip to content

Commit

Permalink
Allow sharing tracks (#236)
Browse files Browse the repository at this point in the history
Signed-off-by: Jyotiraditya Panda <[email protected]>
  • Loading branch information
imjyotiraditya authored Aug 11, 2024
1 parent 90e87bc commit 14e6768
Show file tree
Hide file tree
Showing 4 changed files with 56 additions and 11 deletions.
10 changes: 10 additions & 0 deletions app/src/main/AndroidManifest.xml
Original file line number Diff line number Diff line change
Expand Up @@ -103,6 +103,16 @@
<category android:name="android.intent.category.DEFAULT" />
</intent-filter>
</receiver>

<provider
android:name="androidx.core.content.FileProvider"
android:authorities="${applicationId}.fileProvider"
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 @@ -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
Expand Down Expand Up @@ -310,15 +314,41 @@ class SongAdapter(
true
}*/

/*
R.id.share -> {
val builder = ShareCompat.IntentBuilder(mainActivity)
val mimeTypes = mutableSetOf<String>()
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
}
}
Expand Down
3 changes: 1 addition & 2 deletions app/src/main/res/menu/more_menu.xml
Original file line number Diff line number Diff line change
Expand Up @@ -15,8 +15,7 @@
<!--<item
android:id="@+id/delete"
android:title="@string/delete" />-->
<!--
<item
android:id="@+id/share"
android:title="@string/share" /> -->
android:title="@string/share" />
</menu>
6 changes: 6 additions & 0 deletions app/src/main/res/xml/file_paths.xml
Original file line number Diff line number Diff line change
@@ -0,0 +1,6 @@
<?xml version="1.0" encoding="utf-8"?>
<paths>
<external-path
name="external_files"
path="." />
</paths>

0 comments on commit 14e6768

Please sign in to comment.