improve the way all tracks are initially fetched, rely on artists and albums

This commit is contained in:
tibbi 2020-10-18 16:27:51 +02:00
parent b8cae3bff9
commit 50b9194fcd
3 changed files with 22 additions and 58 deletions

View file

@ -7,8 +7,9 @@ import androidx.room.RoomDatabase
import androidx.room.migration.Migration
import androidx.sqlite.db.SupportSQLiteDatabase
import com.simplemobiletools.musicplayer.R
import com.simplemobiletools.musicplayer.extensions.playlistDAO
import com.simplemobiletools.musicplayer.extensions.*
import com.simplemobiletools.musicplayer.helpers.ALL_TRACKS_PLAYLIST_ID
import com.simplemobiletools.musicplayer.helpers.RoomHelper
import com.simplemobiletools.musicplayer.interfaces.PlaylistsDao
import com.simplemobiletools.musicplayer.interfaces.QueueItemsDao
import com.simplemobiletools.musicplayer.interfaces.SongsDao
@ -60,9 +61,21 @@ abstract class SongsDatabase : RoomDatabase() {
}
private fun addInitialPlaylist(context: Context) {
val allTracks = context.resources.getString(R.string.all_tracks)
val playlist = Playlist(ALL_TRACKS_PLAYLIST_ID, allTracks)
val allTracksLabel = context.resources.getString(R.string.all_tracks)
val playlist = Playlist(ALL_TRACKS_PLAYLIST_ID, allTracksLabel)
context.playlistDAO.insert(playlist)
val allTracks = ArrayList<Track>()
context.getArtistsSync().forEach { artist ->
context.getAlbumsSync(artist).forEach { album ->
context.getAlbumTracksSync(album.id).forEach {
it.playListId = ALL_TRACKS_PLAYLIST_ID
allTracks.add(it)
}
}
}
RoomHelper(context).insertTracksWithPlaylist(allTracks)
}
// removing the "type" value of Song

View file

@ -6,10 +6,10 @@ import com.simplemobiletools.musicplayer.models.Track
@Dao
interface SongsDao {
@Insert(onConflict = OnConflictStrategy.REPLACE)
fun insert(song: Track)
fun insert(track: Track)
@Insert(onConflict = OnConflictStrategy.REPLACE)
fun insertAll(songs: List<Track>)
fun insertAll(tracks: List<Track>)
@Query("SELECT * FROM tracks")
fun getAll(): List<Track>

View file

@ -27,7 +27,10 @@ import android.util.Size
import android.view.KeyEvent
import androidx.core.app.NotificationCompat
import androidx.media.session.MediaButtonReceiver
import com.simplemobiletools.commons.extensions.*
import com.simplemobiletools.commons.extensions.getColoredBitmap
import com.simplemobiletools.commons.extensions.getRealPathFromURI
import com.simplemobiletools.commons.extensions.hasPermission
import com.simplemobiletools.commons.extensions.showErrorToast
import com.simplemobiletools.commons.helpers.PERMISSION_WRITE_STORAGE
import com.simplemobiletools.commons.helpers.ensureBackgroundThread
import com.simplemobiletools.commons.helpers.isOreoPlus
@ -50,7 +53,6 @@ import java.util.*
class MusicService : Service(), MediaPlayer.OnPreparedListener, MediaPlayer.OnErrorListener, MediaPlayer.OnCompletionListener, OnAudioFocusChangeListener {
companion object {
private const val MIN_INITIAL_DURATION = 30
private const val PROGRESS_UPDATE_INTERVAL = 1000L
private const val MAX_CLICK_DURATION = 700L
private const val FAST_FORWARD_SKIP_MS = 10000
@ -353,57 +355,6 @@ class MusicService : Service(), MediaPlayer.OnPreparedListener, MediaPlayer.OnEr
setupEqualizer()
}
private fun getAllDeviceTracks() {
val uri = Audio.Media.EXTERNAL_CONTENT_URI
val projection = arrayOf(
Audio.Media._ID,
Audio.Media.DURATION,
Audio.Media.DATA,
Audio.Media.ALBUM_ID,
Audio.Media.ALBUM,
Audio.Media.TITLE,
Audio.Media.TRACK,
Audio.Media.ARTIST)
if (isQPlus()) {
val showFilename = config.showFilename
val tracks = ArrayList<Track>()
queryCursor(uri, projection) { cursor ->
val duration = cursor.getIntValue(Audio.Media.DURATION) / 1000
if (duration > MIN_INITIAL_DURATION) {
val id = cursor.getLongValue(Audio.Media._ID)
val path = ContentUris.withAppendedId(uri, id).toString()
val title = cursor.getStringValue(Audio.Media.TITLE)
val artist = cursor.getStringValue(Audio.Media.ARTIST)
val album = cursor.getStringValue(Audio.Media.ALBUM)
val albumId = cursor.getLongValue(Audio.Media.ALBUM_ID)
val coverArt = ContentUris.withAppendedId(artworkUri, albumId).toString()
val trackId = cursor.getIntValue(Audio.Media.TRACK) % 1000
val track = Track(id, title, artist, path, duration, album, coverArt, ALL_TRACKS_PLAYLIST_ID, trackId)
track.title = track.getProperTitle(showFilename)
tracks.add(track)
}
}
tracksDAO.insertAll(tracks)
} else {
val ignoredPaths = config.ignoredPaths
val paths = ArrayList<String>()
queryCursor(uri, projection) { cursor ->
val duration = cursor.getIntValue(Audio.Media.DURATION) / 1000
if (duration > MIN_INITIAL_DURATION) {
val path = cursor.getStringValue(Audio.Media.DATA)
if (!ignoredPaths.contains(path) && !path.doesThisOrParentHaveNoMedia()) {
paths.add(path)
}
}
}
val storedAllTrackPaths = tracksDAO.getTracksFromPlaylist(ALL_TRACKS_PLAYLIST_ID).map { it.path }
paths.removeAll(storedAllTrackPaths)
RoomHelper(this).addPathsToPlaylist(paths)
}
}
private fun setupEqualizer() {
try {
mEqualizer = Equalizer(1, mPlayer!!.audioSessionId)