implement new playlist creation from folder

This commit is contained in:
tibbi 2020-10-25 09:53:06 +01:00
parent 0f65e2a579
commit f1db48ed88
3 changed files with 65 additions and 20 deletions

View file

@ -24,15 +24,15 @@ import com.simplemobiletools.commons.models.Release
import com.simplemobiletools.musicplayer.BuildConfig
import com.simplemobiletools.musicplayer.R
import com.simplemobiletools.musicplayer.adapters.ViewPagerAdapter
import com.simplemobiletools.musicplayer.dialogs.NewPlaylistDialog
import com.simplemobiletools.musicplayer.dialogs.SleepTimerCustomDialog
import com.simplemobiletools.musicplayer.extensions.config
import com.simplemobiletools.musicplayer.extensions.queueDAO
import com.simplemobiletools.musicplayer.extensions.sendIntent
import com.simplemobiletools.musicplayer.extensions.*
import com.simplemobiletools.musicplayer.fragments.MyViewPagerFragment
import com.simplemobiletools.musicplayer.helpers.INIT_QUEUE
import com.simplemobiletools.musicplayer.helpers.START_SLEEP_TIMER
import com.simplemobiletools.musicplayer.helpers.STOP_SLEEP_TIMER
import com.simplemobiletools.musicplayer.models.Events
import com.simplemobiletools.musicplayer.models.Track
import com.simplemobiletools.musicplayer.services.MusicService
import kotlinx.android.synthetic.main.activity_main.*
import kotlinx.android.synthetic.main.fragment_albums.*
@ -242,21 +242,45 @@ class MainActivity : SimpleActivity() {
}
private fun createPlaylistFrom(path: String) {
val folderSongs = getFolderSongs(File(path))
val folderSongs = getFolderTracks(File(path))
val allTracks = tracksDAO.getAll()
val wantedTracks = ArrayList<Track>()
}
private fun getFolderSongs(folder: File): ArrayList<String> {
val songFiles = ArrayList<String>()
val files = folder.listFiles() ?: return songFiles
files.forEach {
if (it.isDirectory) {
songFiles.addAll(getFolderSongs(it))
} else if (it.isAudioFast()) {
songFiles.add(it.absolutePath)
folderSongs.forEach { trackPath ->
val mediaStoreId = getMediaStoreIdFromPath(trackPath)
if (mediaStoreId != 0L) {
allTracks.firstOrNull { it.mediaStoreId == mediaStoreId }?.apply {
wantedTracks.add(this)
}
}
}
return songFiles
runOnUiThread {
NewPlaylistDialog(this) { playlistId ->
wantedTracks.forEach {
it.id = 0
it.playListId = playlistId
}
ensureBackgroundThread {
tracksDAO.insertAll(wantedTracks)
EventBus.getDefault().post(Events.PlaylistsUpdated())
}
}
}
}
private fun getFolderTracks(folder: File): ArrayList<String> {
val trackFiles = ArrayList<String>()
val files = folder.listFiles() ?: return trackFiles
files.forEach {
if (it.isDirectory) {
trackFiles.addAll(getFolderTracks(it))
} else if (it.isAudioFast()) {
trackFiles.add(it.absolutePath)
}
}
return trackFiles
}
private fun showSleepTimer() {

View file

@ -4,11 +4,9 @@ import android.annotation.SuppressLint
import android.content.ContentUris
import android.content.Context
import android.content.Intent
import android.provider.MediaStore
import android.provider.MediaStore.Audio
import com.simplemobiletools.commons.extensions.getIntValue
import com.simplemobiletools.commons.extensions.getLongValue
import com.simplemobiletools.commons.extensions.getStringValue
import com.simplemobiletools.commons.extensions.showErrorToast
import com.simplemobiletools.commons.extensions.*
import com.simplemobiletools.commons.helpers.ensureBackgroundThread
import com.simplemobiletools.commons.helpers.isOreoPlus
import com.simplemobiletools.commons.helpers.isQPlus
@ -286,3 +284,26 @@ fun Context.getAllInitialTracks(): ArrayList<Track> {
}
return allTracks
}
fun Context.getMediaStoreIdFromPath(path: String): Long {
var id = 0L
val projection = arrayOf(
Audio.Media._ID
)
val uri = getFileUri(path)
val selection = "${MediaStore.MediaColumns.DATA} = ?"
val selectionArgs = arrayOf(path)
try {
val cursor = contentResolver.query(uri, projection, selection, selectionArgs, null)
cursor?.use {
if (cursor.moveToFirst()) {
id = cursor.getLongValue(Audio.Media._ID)
}
}
} catch (ignored: Exception) {
}
return id
}

View file

@ -17,7 +17,7 @@ import java.io.Serializable
@Entity(tableName = "tracks", indices = [Index(value = ["media_store_id", "playlist_id"], unique = true)])
data class Track(
@PrimaryKey(autoGenerate = true) val id: Long,
@PrimaryKey(autoGenerate = true) var id: Long,
@ColumnInfo(name = "media_store_id") val mediaStoreId: Long,
@ColumnInfo(name = "title") var title: String,
@ColumnInfo(name = "artist") var artist: String,