Skip to content

Commit

Permalink
Added support for disc number (FossifyOrg#18)
Browse files Browse the repository at this point in the history
  • Loading branch information
Aga-C committed Apr 5, 2024
1 parent 2288aa7 commit 1abb56f
Show file tree
Hide file tree
Showing 10 changed files with 57 additions and 12 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -132,7 +132,11 @@ class TracksHeaderAdapter(activity: SimpleActivity, items: ArrayList<ListItem>,
}

trackDuration.text = track.duration.getFormattedDuration()
trackId.text = track.trackId.toString()
if (track.discNumber != null) {
trackId.text = context.getString(R.string.track_on_disk, track.discNumber, track.trackId.toString().padStart(2, '0'))
} else {
trackId.text = track.trackId.toString()
}
trackImage.beGone()
trackId.beVisible()
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -10,7 +10,7 @@ import org.fossify.musicplayer.interfaces.*
import org.fossify.musicplayer.models.*
import org.fossify.musicplayer.objects.MyExecutor

@Database(entities = [Track::class, Playlist::class, QueueItem::class, Artist::class, Album::class, Genre::class], version = 13)
@Database(entities = [Track::class, Playlist::class, QueueItem::class, Artist::class, Album::class, Genre::class], version = 14)
abstract class SongsDatabase : RoomDatabase() {

abstract fun SongsDao(): SongsDao
Expand Down Expand Up @@ -46,6 +46,7 @@ abstract class SongsDatabase : RoomDatabase() {
.addMigrations(MIGRATION_10_11)
.addMigrations(MIGRATION_11_12)
.addMigrations(MIGRATION_12_13)
.addMigrations(MIGRATION_13_14)
.build()
}
}
Expand Down Expand Up @@ -201,5 +202,13 @@ abstract class SongsDatabase : RoomDatabase() {
}
}
}

private val MIGRATION_13_14 = object : Migration(13, 14) {
override fun migrate(database: SupportSQLiteDatabase) {
database.apply {
execSQL("ALTER TABLE tracks ADD COLUMN disc_number INTEGER DEFAULT NULL")
}
}
}
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -12,6 +12,7 @@ import org.fossify.musicplayer.helpers.EXTRA_ARTIST
import org.fossify.musicplayer.helpers.EXTRA_ARTIST_ID
import org.fossify.musicplayer.helpers.EXTRA_COVER_ART
import org.fossify.musicplayer.helpers.EXTRA_DATE_ADDED
import org.fossify.musicplayer.helpers.EXTRA_DISC_NUMBER
import org.fossify.musicplayer.helpers.EXTRA_DURATION
import org.fossify.musicplayer.helpers.EXTRA_FLAGS
import org.fossify.musicplayer.helpers.EXTRA_FOLDER_NAME
Expand All @@ -37,6 +38,7 @@ fun buildMediaItem(
mediaType: @MediaMetadata.MediaType Int,
trackCnt: Int? = null,
trackNumber: Int? = null,
discNumber: Int? = null,
year: Int? = null,
sourceUri: Uri? = null,
artworkUri: Uri? = null,
Expand All @@ -51,6 +53,7 @@ fun buildMediaItem(
.setIsPlayable(mediaType == MediaMetadata.MEDIA_TYPE_MUSIC)
.setTotalTrackCount(trackCnt)
.setTrackNumber(trackNumber)
.setDiscNumber(discNumber)
.setReleaseYear(year)
.setMediaType(MediaMetadata.MEDIA_TYPE_MUSIC)
.setArtworkUri(artworkUri)
Expand All @@ -77,6 +80,7 @@ fun Track.toMediaItem(): MediaItem {
genre = genre,
mediaType = MediaMetadata.MEDIA_TYPE_MUSIC,
trackNumber = trackId,
discNumber = discNumber,
sourceUri = getUri(),
artworkUri = coverArt.toUri(),
track = this
Expand Down Expand Up @@ -155,6 +159,7 @@ private fun createBundleFromTrack(track: Track) = bundleOf(
EXTRA_COVER_ART to track.coverArt,
EXTRA_PLAYLIST_ID to track.playListId,
EXTRA_TRACK_ID to track.trackId,
EXTRA_DISC_NUMBER to (track.discNumber ?: Int.MIN_VALUE),
EXTRA_FOLDER_NAME to track.folderName,
EXTRA_ALBUM_ID to track.albumId,
EXTRA_ARTIST_ID to track.artistId,
Expand All @@ -166,6 +171,11 @@ private fun createBundleFromTrack(track: Track) = bundleOf(
)

private fun createTrackFromBundle(bundle: Bundle): Track {
var discNumber: Int? = bundle.getInt(EXTRA_DISC_NUMBER)
if (discNumber == Int.MIN_VALUE) {
discNumber = null
}

return Track(
id = bundle.getLong(EXTRA_ID),
mediaStoreId = bundle.getLong(EXTRA_MEDIA_STORE_ID),
Expand All @@ -178,6 +188,7 @@ private fun createTrackFromBundle(bundle: Bundle): Track {
coverArt = bundle.getString(EXTRA_COVER_ART) ?: "",
playListId = bundle.getInt(EXTRA_PLAYLIST_ID),
trackId = bundle.getInt(EXTRA_TRACK_ID),
discNumber = discNumber,
folderName = bundle.getString(EXTRA_FOLDER_NAME) ?: "",
albumId = bundle.getLong(EXTRA_ALBUM_ID),
artistId = bundle.getLong(EXTRA_ARTIST_ID),
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -127,7 +127,7 @@ class AudioHelper(private val context: Context) {
fun getAlbumTracks(albumId: Long): ArrayList<Track> {
val tracks = context.tracksDAO.getTracksFromAlbum(albumId)
.applyProperFilenames(config.showFilename)
tracks.sortWith(compareBy({ it.trackId }, { it.title.lowercase() }))
tracks.sortWith(compareBy({ it.discNumber }, { it.trackId }, { it.title.lowercase() }))
return tracks
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -34,6 +34,7 @@ const val EXTRA_GENRE = "genre"
const val EXTRA_COVER_ART = "cover_art"
const val EXTRA_PLAYLIST_ID = "playlist_id"
const val EXTRA_TRACK_ID = "track_id"
const val EXTRA_DISC_NUMBER = "disc_number"
const val EXTRA_FOLDER_NAME = "folder_name"
const val EXTRA_ALBUM_ID = "album_id"
const val EXTRA_ARTIST_ID = "artist_id"
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -47,6 +47,7 @@ class RoomHelper(val context: Context) {
if (isRPlus()) {
projection.add(Audio.Media.GENRE)
projection.add(Audio.Media.GENRE_ID)
projection.add(Audio.Media.DISC_NUMBER)
}

val pathsMap = HashSet<String>()
Expand Down Expand Up @@ -83,18 +84,21 @@ class RoomHelper(val context: Context) {

val genre: String
val genreId: Long
val discNumber: Int?
if (isRPlus()) {
genre = cursor.getStringValue(Audio.Media.GENRE)
genreId = cursor.getLongValue(Audio.Media.GENRE_ID)
discNumber = cursor.getStringValue(Audio.Media.DISC_NUMBER)?.toIntOrNull()
} else {
genre = ""
genreId = 0
discNumber = null
}

val song = Track(
id = 0, mediaStoreId = mediaStoreId, title = title, artist = artist, path = path, duration = duration, album = album, genre = genre,
coverArt = coverArt, playListId = playlistId, trackId = 0, folderName = folderName, albumId = albumId, artistId = artistId,
genreId = genreId, year = year, dateAdded = dateAdded, orderInPlaylist = 0
coverArt = coverArt, playListId = playlistId, trackId = 0, discNumber = discNumber, folderName = folderName, albumId = albumId,
artistId = artistId, genreId = genreId, year = year, dateAdded = dateAdded, orderInPlaylist = 0
)
song.title = song.getProperTitle(showFilename)
songs.add(song)
Expand All @@ -114,8 +118,8 @@ class RoomHelper(val context: Context) {

val song = Track(
id = 0, mediaStoreId = 0, title = title, artist = artist, path = it, duration = context.getDuration(it) ?: 0, album = "",
genre = "", coverArt = "", playListId = playlistId, trackId = 0, folderName = "", albumId = 0, artistId = 0, genreId = 0,
year = 0, dateAdded = dateAdded, orderInPlaylist = 0
genre = "", coverArt = "", playListId = playlistId, trackId = 0, discNumber = null, folderName = "", albumId = 0, artistId = 0,
genreId = 0, year = 0, dateAdded = dateAdded, orderInPlaylist = 0
)
song.title = song.getProperTitle(showFilename)
songs.add(song)
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -225,6 +225,7 @@ class SimpleMediaScanner(private val context: Application) {
if (isRPlus()) {
projection.add(Audio.Media.GENRE)
projection.add(Audio.Media.GENRE_ID)
projection.add(Audio.Media.DISC_NUMBER)
}

context.queryCursor(uri, projection.toTypedArray(), showErrors = true) { cursor ->
Expand All @@ -250,19 +251,22 @@ class SimpleMediaScanner(private val context: Application) {

val genre: String
val genreId: Long
val discNumber: Int?
if (isRPlus()) {
genre = cursor.getStringValue(Audio.Media.GENRE).orEmpty()
genreId = cursor.getLongValue(Audio.Media.GENRE_ID)
discNumber = cursor.getStringValue(Audio.Media.DISC_NUMBER)?.toIntOrNull()
} else {
genre = ""
genreId = 0
discNumber = null
}

if (!title.isNullOrEmpty()) {
val track = Track(
id = 0, mediaStoreId = id, title = title, artist = artist, path = path, duration = duration, album = album, genre = genre,
coverArt = coverArt, playListId = 0, trackId = trackId, folderName = folderName, albumId = albumId, artistId = artistId, genreId = genreId,
year = year, dateAdded = dateAdded, orderInPlaylist = 0
coverArt = coverArt, playListId = 0, trackId = trackId, discNumber = discNumber, folderName = folderName, albumId = albumId, artistId = artistId,
genreId = genreId, year = year, dateAdded = dateAdded, orderInPlaylist = 0
)
tracks.add(track)
}
Expand Down Expand Up @@ -442,6 +446,7 @@ class SimpleMediaScanner(private val context: Application) {
val album = retriever.extractMetadata(METADATA_KEY_ALBUM) ?: folderName
val trackNumber = retriever.extractMetadata(METADATA_KEY_CD_TRACK_NUMBER)
val trackId = trackNumber?.split("/")?.first()?.toIntOrNull() ?: 0
val discNumber = retriever.extractMetadata(METADATA_KEY_DISC_NUMBER)?.toIntOrNull() ?: 0
val year = retriever.extractMetadata(METADATA_KEY_YEAR)?.toIntOrNull() ?: 0
val dateAdded = try {
(File(path).lastModified() / 1000L).toInt()
Expand All @@ -454,8 +459,8 @@ class SimpleMediaScanner(private val context: Application) {
if (title.isNotEmpty()) {
val track = Track(
id = 0, mediaStoreId = 0, title = title, artist = artist, path = path, duration = duration, album = album, genre = genre,
coverArt = "", playListId = 0, trackId = trackId, folderName = folderName, albumId = 0, artistId = 0, genreId = 0,
year = year, dateAdded = dateAdded, orderInPlaylist = 0, flags = FLAG_MANUAL_CACHE
coverArt = "", playListId = 0, trackId = trackId, discNumber = discNumber, folderName = folderName, albumId = 0, artistId = 0,
genreId = 0, year = year, dateAdded = dateAdded, orderInPlaylist = 0, flags = FLAG_MANUAL_CACHE
)
// use hashCode() as id for tracking purposes, there's a very slim chance of collision
track.mediaStoreId = track.hashCode().toLong()
Expand Down
10 changes: 9 additions & 1 deletion app/src/main/kotlin/org/fossify/musicplayer/models/Track.kt
Original file line number Diff line number Diff line change
Expand Up @@ -31,6 +31,7 @@ data class Track(
@ColumnInfo(name = "cover_art") val coverArt: String,
@ColumnInfo(name = "playlist_id") var playListId: Int,
@ColumnInfo(name = "track_id") val trackId: Int, // order id within the tracks' album
@ColumnInfo(name = "disc_number") var discNumber: Int?,
@ColumnInfo(name = "folder_name") var folderName: String,
@ColumnInfo(name = "album_id") var albumId: Long,
@ColumnInfo(name = "artist_id") var artistId: Long,
Expand Down Expand Up @@ -62,7 +63,14 @@ data class Track(
}
}

sorting and PLAYER_SORT_BY_TRACK_ID != 0 -> first.trackId.compareTo(second.trackId)
sorting and PLAYER_SORT_BY_TRACK_ID != 0 -> {
val discComparison = (first.discNumber ?: Int.MAX_VALUE).compareTo(second.discNumber ?: Int.MAX_VALUE)
if (discComparison == 0) {
first.trackId.compareTo(second.trackId)
} else {
discComparison
}
}
sorting and PLAYER_SORT_BY_DATE_ADDED != 0 -> first.dateAdded.compareTo(second.dateAdded)
sorting and PLAYER_SORT_BY_CUSTOM != 0 -> first.orderInPlaylist.compareTo(second.orderInPlaylist)
else -> first.duration.compareTo(second.duration)
Expand Down
1 change: 1 addition & 0 deletions app/src/main/res/values-pl/strings.xml
Original file line number Diff line number Diff line change
Expand Up @@ -72,6 +72,7 @@
<item quantity="many">%d utworów</item>
<item quantity="other">%d utworów</item>
</plurals>
<string name="track_on_disk">%1$d.%2$s</string>
<string name="next_track">Następny:</string>
<string name="track_queue">Kolejka utworów</string>
<string name="add_to_queue">Dodaj do kolejki</string>
Expand Down
2 changes: 2 additions & 0 deletions app/src/main/res/values/strings.xml
Original file line number Diff line number Diff line change
Expand Up @@ -71,6 +71,8 @@
<item quantity="one">%d Track</item>
<item quantity="other">%d Tracks</item>
</plurals>
<!-- %1 is disc number, %2 is track number -->
<string name="track_on_disk">%1$d.%2$s</string>
<!-- Queue -->
<string name="next_track">Next:</string>
<string name="track_queue">Track queue</string>
Expand Down

0 comments on commit 1abb56f

Please sign in to comment.