Ignore blacklist and other things, so any song can be played when opened from File Managers, and also from Folders tab.

This commit is contained in:
Prathamesh More 2022-01-12 14:05:19 +05:30
parent 4d9396873e
commit 5415d3869a
5 changed files with 68 additions and 41 deletions

View file

@ -1,5 +1,6 @@
package code.name.monkey.retromusic.fragments.backup package code.name.monkey.retromusic.fragments.backup
import android.content.ContentResolver
import android.net.Uri import android.net.Uri
import android.os.Bundle import android.os.Bundle
import android.provider.MediaStore import android.provider.MediaStore
@ -65,10 +66,10 @@ class RestoreActivity : AppCompatActivity() {
private fun getFileName(uri: Uri?): String? { private fun getFileName(uri: Uri?): String? {
when (uri?.scheme) { when (uri?.scheme) {
"file" -> { ContentResolver.SCHEME_FILE -> {
return uri.lastPathSegment return uri.lastPathSegment
} }
"content" -> { ContentResolver.SCHEME_CONTENT -> {
val proj = arrayOf(MediaStore.Files.FileColumns.DISPLAY_NAME) val proj = arrayOf(MediaStore.Files.FileColumns.DISPLAY_NAME)
contentResolver.query( contentResolver.query(
uri, proj, null, null, null uri, proj, null, null, null

View file

@ -308,7 +308,7 @@ class FoldersFragment : AbsMainActivityFragment(R.layout.fragment_folder),
openQueue(songs, startIndex, true) openQueue(songs, startIndex, true)
} else { } else {
Snackbar.make( Snackbar.make(
binding.root, mainActivity.slidingPanel,
Html.fromHtml( Html.fromHtml(
String.format( String.format(
getString(R.string.not_listed_in_media_store), file1.name getString(R.string.not_listed_in_media_store), file1.name

View file

@ -15,12 +15,10 @@
package code.name.monkey.retromusic.helper package code.name.monkey.retromusic.helper
import android.annotation.SuppressLint import android.annotation.SuppressLint
import android.annotation.TargetApi
import android.app.Activity import android.app.Activity
import android.content.* import android.content.*
import android.database.Cursor import android.database.Cursor
import android.net.Uri import android.net.Uri
import android.os.Build
import android.os.Environment import android.os.Environment
import android.os.IBinder import android.os.IBinder
import android.provider.DocumentsContract import android.provider.DocumentsContract
@ -440,12 +438,14 @@ object MusicPlayerRemote : KoinComponent {
} else if (uri.authority == "media") { } else if (uri.authority == "media") {
songId = uri.lastPathSegment songId = uri.lastPathSegment
} }
if (songId != null) { songs = if (songId != null) {
songs = songRepository.songs(songId) songRepository.songs(songId)
} else {
songRepository.songsIgnoreBlacklist(uri)
} }
} }
} }
if (songs == null) { if (songs == null || songs.isEmpty()) {
var songFile: File? = null var songFile: File? = null
if (uri.authority != null && uri.authority == "com.android.externalstorage.documents") { if (uri.authority != null && uri.authority == "com.android.externalstorage.documents") {
songFile = File( songFile = File(
@ -462,7 +462,7 @@ object MusicPlayerRemote : KoinComponent {
songFile = File(uri.path!!) songFile = File(uri.path!!)
} }
if (songFile != null) { if (songFile != null) {
songs = songRepository.songsByFilePath(songFile.absolutePath) songs = songRepository.songsByFilePath(songFile.absolutePath, true)
} }
} }
if (songs != null && songs.isNotEmpty()) { if (songs != null && songs.isNotEmpty()) {
@ -474,7 +474,6 @@ object MusicPlayerRemote : KoinComponent {
} }
} }
@TargetApi(Build.VERSION_CODES.KITKAT)
private fun getSongIdFromMediaProvider(uri: Uri): String { private fun getSongIdFromMediaProvider(uri: Uri): String {
return DocumentsContract.getDocumentId(uri).split(":".toRegex()) return DocumentsContract.getDocumentId(uri).split(":".toRegex())
.dropLastWhile { it.isEmpty() }.toTypedArray()[1] .dropLastWhile { it.isEmpty() }.toTypedArray()[1]

View file

@ -16,6 +16,7 @@ package code.name.monkey.retromusic.repository
import android.content.Context import android.content.Context
import android.database.Cursor import android.database.Cursor
import android.net.Uri
import android.os.Environment import android.os.Environment
import android.provider.MediaStore import android.provider.MediaStore
import android.provider.MediaStore.Audio.AudioColumns import android.provider.MediaStore.Audio.AudioColumns
@ -42,11 +43,13 @@ interface SongRepository {
fun songs(query: String): List<Song> fun songs(query: String): List<Song>
fun songsByFilePath(filePath: String): List<Song> fun songsByFilePath(filePath: String, ignoreBlacklist: Boolean = false): List<Song>
fun song(cursor: Cursor?): Song fun song(cursor: Cursor?): Song
fun song(songId: Long): Song fun song(songId: Long): Song
fun songsIgnoreBlacklist(uri: Uri): List<Song>
} }
class RealSongRepository(private val context: Context) : SongRepository { class RealSongRepository(private val context: Context) : SongRepository {
@ -84,15 +87,38 @@ class RealSongRepository(private val context: Context) : SongRepository {
return song(makeSongCursor(AudioColumns._ID + "=?", arrayOf(songId.toString()))) return song(makeSongCursor(AudioColumns._ID + "=?", arrayOf(songId.toString())))
} }
override fun songsByFilePath(filePath: String): List<Song> { override fun songsByFilePath(filePath: String, ignoreBlacklist: Boolean): List<Song> {
return songs( return songs(
makeSongCursor( makeSongCursor(
AudioColumns.DATA + "=?", AudioColumns.DATA + "=?",
arrayOf(filePath) arrayOf(filePath),
ignoreBlacklist = ignoreBlacklist
) )
) )
} }
override fun songsIgnoreBlacklist(uri: Uri): List<Song> {
var filePath = ""
context.contentResolver.query(
uri,
arrayOf(AudioColumns.DATA),
null,
null,
null
).use { cursor ->
if (cursor != null) {
if (cursor.count != 0) {
cursor.moveToFirst()
filePath = cursor.getString(AudioColumns.DATA)
println("File Path: $filePath")
}
}
}
return songsByFilePath(
filePath, true
)
}
private fun getSongFromCursorImpl( private fun getSongFromCursorImpl(
cursor: Cursor cursor: Cursor
): Song { ): Song {
@ -128,13 +154,14 @@ class RealSongRepository(private val context: Context) : SongRepository {
@JvmOverloads @JvmOverloads
fun makeSongCursor( fun makeSongCursor(
selection: String?, selection: String?,
selectionValues: Array<String>?, selectionValues: Array<String>?,
sortOrder: String = PreferenceUtil.songSortOrder sortOrder: String = PreferenceUtil.songSortOrder,
ignoreBlacklist: Boolean = false
): Cursor? { ): Cursor? {
var selectionFinal = selection var selectionFinal = selection
var selectionValuesFinal = selectionValues var selectionValuesFinal = selectionValues
if (!ignoreBlacklist) {
selectionFinal = if (selection != null && selection.trim { it <= ' ' } != "") { selectionFinal = if (selection != null && selection.trim { it <= ' ' } != "") {
"$IS_MUSIC AND $selectionFinal" "$IS_MUSIC AND $selectionFinal"
} else { } else {
@ -161,7 +188,7 @@ class RealSongRepository(private val context: Context) : SongRepository {
selectionFinal = selectionFinal =
selectionFinal + " AND " + Media.DURATION + ">= " + (PreferenceUtil.filterLength * 1000) selectionFinal + " AND " + Media.DURATION + ">= " + (PreferenceUtil.filterLength * 1000)
}
val uri = if (android.os.Build.VERSION.SDK_INT >= android.os.Build.VERSION_CODES.Q) { val uri = if (android.os.Build.VERSION.SDK_INT >= android.os.Build.VERSION_CODES.Q) {
Media.getContentUri(MediaStore.VOLUME_EXTERNAL) Media.getContentUri(MediaStore.VOLUME_EXTERNAL)
} else { } else {

View file

@ -92,7 +92,7 @@ public final class FileUtil {
} }
Cursor songCursor = Cursor songCursor =
new RealSongRepository(context).makeSongCursor(selection, selection == null ? null : paths); new RealSongRepository(context).makeSongCursor(selection, selection == null ? null : paths, PreferenceUtil.INSTANCE.getSongSortOrder(), true);
return songCursor == null return songCursor == null
? null ? null