diff --git a/app/src/main/java/ch/blinkenlights/android/medialibrary/MediaScanner.java b/app/src/main/java/ch/blinkenlights/android/medialibrary/MediaScanner.java index 0ba862eeb..389ee700e 100644 --- a/app/src/main/java/ch/blinkenlights/android/medialibrary/MediaScanner.java +++ b/app/src/main/java/ch/blinkenlights/android/medialibrary/MediaScanner.java @@ -500,13 +500,20 @@ private boolean rpcInspectFile(File file) { if (discNumber == null) discNumber = "1"; // untagged, but most likely '1' - this prevents annoying sorting issues with partially tagged files - long artistId = MediaLibrary.hash63(artist); - long albumId = MediaLibrary.hash63(album); + // albumartist is now always present + String albumartist = tags.getFirst(MediaMetadataExtractor.ALBUMARTIST); + if (albumartist == null) + albumartist = ""; + + long albumartistId = MediaLibrary.hash63(albumartist); - // Overwrite albumId with a hash that included the parent dir if set in preferences + // I have several albums all called "Greatest Hits" with different album artists + String albumIdString = album + "\n" + albumartist; + // Include the parent dir in the hash if set in preferences if (prefs.groupAlbumsByFolder) { - albumId = MediaLibrary.hash63(album + "\n" + file.getParent()); + albumIdString += "\n" + file.getParent(); } + long albumId = MediaLibrary.hash63(albumIdString); ContentValues v = new ContentValues(); v.put(MediaLibrary.SongColumns._ID, songId); @@ -527,31 +534,45 @@ private boolean rpcInspectFile(File file) { v.put(MediaLibrary.AlbumColumns._ID, albumId); v.put(MediaLibrary.AlbumColumns.ALBUM, album); v.put(MediaLibrary.AlbumColumns.ALBUM_SORT, MediaLibrary.keyFor(album)); - v.put(MediaLibrary.AlbumColumns.PRIMARY_ARTIST_ID, artistId); + v.put(MediaLibrary.AlbumColumns.PRIMARY_ARTIST_ID, albumartistId); v.put(MediaLibrary.AlbumColumns.PRIMARY_ALBUM_YEAR,tags.getFirst(MediaMetadataExtractor.YEAR)); long albumInsert = mBackend.insert(MediaLibrary.TABLE_ALBUMS, null, v); if (albumInsert == -1) { - // Insert failed, so the column probably already existed. + // Insert failed, so the entry probably already existed. // We need to ensure that the album table is up-to-date as it contains // some 'cached' (PRIMARY_*) values. // Failure to do so would mean that we never update the year or may point to an // orphaned artist id. v.clear(); - v.put(MediaLibrary.AlbumColumns.PRIMARY_ARTIST_ID, artistId); + v.put(MediaLibrary.AlbumColumns.PRIMARY_ARTIST_ID, albumartistId); v.put(MediaLibrary.AlbumColumns.PRIMARY_ALBUM_YEAR,tags.getFirst(MediaMetadataExtractor.YEAR)); mBackend.update(MediaLibrary.TABLE_ALBUMS, v, MediaLibrary.AlbumColumns._ID+"=?", new String[]{ Long.toString(albumId) }); } + long artistId = MediaLibrary.hash63(artist); + v.clear(); - v.put(MediaLibrary.ContributorColumns._ID, artistId); - v.put(MediaLibrary.ContributorColumns._CONTRIBUTOR, artist); + v.put(MediaLibrary.ContributorColumns._ID, artistId); + v.put(MediaLibrary.ContributorColumns._CONTRIBUTOR, artist); v.put(MediaLibrary.ContributorColumns._CONTRIBUTOR_SORT, MediaLibrary.keyFor(artist)); mBackend.insert(MediaLibrary.TABLE_CONTRIBUTORS, null, v); v.clear(); v.put(MediaLibrary.ContributorSongColumns._CONTRIBUTOR_ID, artistId); + v.put(MediaLibrary.ContributorSongColumns.SONG_ID, songId); + v.put(MediaLibrary.ContributorSongColumns.ROLE, MediaLibrary.ROLE_ARTIST); + mBackend.insert(MediaLibrary.TABLE_CONTRIBUTORS_SONGS, null, v); + + v.clear(); + v.put(MediaLibrary.ContributorColumns._ID, albumartistId); + v.put(MediaLibrary.ContributorColumns._CONTRIBUTOR, albumartist); + v.put(MediaLibrary.ContributorColumns._CONTRIBUTOR_SORT, MediaLibrary.keyFor(albumartist)); + mBackend.insert(MediaLibrary.TABLE_CONTRIBUTORS, null, v); + + v.clear(); + v.put(MediaLibrary.ContributorSongColumns._CONTRIBUTOR_ID, albumartistId); v.put(MediaLibrary.ContributorSongColumns.SONG_ID, songId); - v.put(MediaLibrary.ContributorSongColumns.ROLE, MediaLibrary.ROLE_ARTIST); + v.put(MediaLibrary.ContributorSongColumns.ROLE, MediaLibrary.ROLE_ALBUMARTIST); mBackend.insert(MediaLibrary.TABLE_CONTRIBUTORS_SONGS, null, v); // Composers are optional: only add if we found it @@ -571,23 +592,6 @@ private boolean rpcInspectFile(File file) { mBackend.insert(MediaLibrary.TABLE_CONTRIBUTORS_SONGS, null, v); } - // Same as with composer: albumartist is an optional tag - String albumartist = tags.getFirst(MediaMetadataExtractor.ALBUMARTIST); - if (albumartist != null) { - long albumartistId = MediaLibrary.hash63(albumartist); - v.clear(); - v.put(MediaLibrary.ContributorColumns._ID, albumartistId); - v.put(MediaLibrary.ContributorColumns._CONTRIBUTOR, albumartist); - v.put(MediaLibrary.ContributorColumns._CONTRIBUTOR_SORT, MediaLibrary.keyFor(albumartist)); - mBackend.insert(MediaLibrary.TABLE_CONTRIBUTORS, null, v); - - v.clear(); - v.put(MediaLibrary.ContributorSongColumns._CONTRIBUTOR_ID, albumartistId); - v.put(MediaLibrary.ContributorSongColumns.SONG_ID, songId); - v.put(MediaLibrary.ContributorSongColumns.ROLE, MediaLibrary.ROLE_ALBUMARTIST); - mBackend.insert(MediaLibrary.TABLE_CONTRIBUTORS_SONGS, null, v); - } - // A song might be in multiple genres if (tags.containsKey(MediaMetadataExtractor.GENRE)) { ArrayList genres = tags.get(MediaMetadataExtractor.GENRE); diff --git a/app/src/main/java/ch/blinkenlights/android/medialibrary/MediaSchema.java b/app/src/main/java/ch/blinkenlights/android/medialibrary/MediaSchema.java index dcc0a95e7..2cce18c9c 100644 --- a/app/src/main/java/ch/blinkenlights/android/medialibrary/MediaSchema.java +++ b/app/src/main/java/ch/blinkenlights/android/medialibrary/MediaSchema.java @@ -168,7 +168,7 @@ public class MediaSchema { +" ;"; /** - * View wich includes SONGS_ALBUMS_ARTISTS and any other contributors + * View which includes SONGS_ALBUMS_ARTISTS and any other contributors * This view should only be used if needed as the SQL query is pretty expensive */ private static final String VIEW_CREATE_SONGS_ALBUMS_ARTISTS_HUGE = "CREATE VIEW "+ MediaLibrary.VIEW_SONGS_ALBUMS_ARTISTS_HUGE+" AS "