diff --git a/app/src/main/java/com/kabouzeid/gramophone/util/MusicUtil.java b/app/src/main/java/com/kabouzeid/gramophone/util/MusicUtil.java index e7998834e..89dda5bb5 100644 --- a/app/src/main/java/com/kabouzeid/gramophone/util/MusicUtil.java +++ b/app/src/main/java/com/kabouzeid/gramophone/util/MusicUtil.java @@ -5,15 +5,18 @@ import android.content.ContentValues; import android.content.Context; import android.content.Intent; +import android.content.SharedPreferences; import android.database.Cursor; import android.net.Uri; import android.os.Environment; import android.provider.BaseColumns; import android.provider.MediaStore; import android.provider.Settings; + import androidx.annotation.NonNull; import androidx.annotation.Nullable; import androidx.core.content.FileProvider; + import android.text.TextUtils; import android.util.Log; import android.widget.Toast; @@ -71,15 +74,14 @@ public static Intent createShareSongFileIntent(@NonNull final Song song, Context } - @NonNull public static String getArtistInfoString(@NonNull final Context context, @NonNull final Artist artist) { int albumCount = artist.getAlbumCount(); int songCount = artist.getSongCount(); return MusicUtil.buildInfoString( - MusicUtil.getAlbumCountString(context, albumCount), - MusicUtil.getSongCountString(context, songCount) + MusicUtil.getAlbumCountString(context, albumCount), + MusicUtil.getSongCountString(context, songCount) ); } @@ -88,16 +90,16 @@ public static String getAlbumInfoString(@NonNull final Context context, @NonNull int songCount = album.getSongCount(); return MusicUtil.buildInfoString( - album.getArtistName(), - MusicUtil.getSongCountString(context, songCount) + album.getArtistName(), + MusicUtil.getSongCountString(context, songCount) ); } @NonNull public static String getSongInfoString(@NonNull final Song song) { return MusicUtil.buildInfoString( - song.artistName, - song.albumName + song.artistName, + song.albumName ); } @@ -112,8 +114,8 @@ public static String getPlaylistInfoString(@NonNull final Context context, @NonN final long duration = getTotalDuration(context, songs); return MusicUtil.buildInfoString( - MusicUtil.getSongCountString(context, songs.size()), - MusicUtil.getReadableDurationString(duration) + MusicUtil.getSongCountString(context, songs.size()), + MusicUtil.getReadableDurationString(duration) ); } @@ -154,15 +156,14 @@ public static String getReadableDurationString(long songDurationMillis) { } } - /** + /** * Build a concatenated string from the provided arguments * The intended purpose is to show extra annotations * to a music library item. * Ex: for a given album --> buildInfoString(album.artist, album.songCount) */ @NonNull - public static String buildInfoString(@Nullable final String string1, @Nullable final String string2) - { + public static String buildInfoString(@Nullable final String string1, @Nullable final String string2) { // Skip empty strings if (TextUtils.isEmpty(string1)) { //noinspection ConstantConditions @@ -264,7 +265,7 @@ public static void deleteTracks(@NonNull final Context context, @NonNull final L final File f = new File(name); if (f.delete()) { // Step 3: Remove selected track from the database - context.getContentResolver().delete(ContentUris.withAppendedId(MediaStore.Audio.Media.EXTERNAL_CONTENT_URI, id),null, null); + context.getContentResolver().delete(ContentUris.withAppendedId(MediaStore.Audio.Media.EXTERNAL_CONTENT_URI, id), null, null); deletedCount++; } else { // I'm not sure if we'd ever get here (deletion would @@ -287,14 +288,54 @@ public static void deleteTracks(@NonNull final Context context, @NonNull final L } public static boolean isFavoritePlaylist(@NonNull final Context context, @NonNull final Playlist playlist) { - return playlist.name != null && playlist.name.equals(context.getString(R.string.favorites)); + SharedPreferences sharedPreferences = context.getSharedPreferences("Shared", Context.MODE_PRIVATE); + String previous = sharedPreferences.getString("FAV", ""); + boolean isFavorite = playlist.name != null && playlist.name.equals(previous); + if (isFavorite && !playlist.name.equals(context.getString(R.string.favorites))) { + PlaylistsUtil.renamePlaylist(context, playlist.id, context.getString(R.string + .favorites)); + SharedPreferences.Editor editor = sharedPreferences.edit(); + editor.putString("FAV", context.getString(R.string.favorites)); + editor.apply(); + } + return isFavorite; } public static Playlist getFavoritesPlaylist(@NonNull final Context context) { - return PlaylistLoader.getPlaylist(context, context.getString(R.string.favorites)); + SharedPreferences sharedPreferences = context.getSharedPreferences("Shared", Context.MODE_PRIVATE); + String previous = sharedPreferences.getString("FAV", ""); + List playlists = PlaylistLoader.getAllPlaylists(context); + for (Playlist playlist : playlists) { + if (playlist.name.equals(previous)) { + if (!playlist.name.equals(context.getString(R.string.favorites))) { + PlaylistsUtil.renamePlaylist(context, playlist.id, context.getString(R.string + .favorites)); + SharedPreferences.Editor editor = sharedPreferences.edit(); + editor.putString("FAV", context.getString(R.string.favorites)); + editor.apply(); + } + break; + } + } + return PlaylistLoader.getPlaylist(context, previous); } private static Playlist getOrCreateFavoritesPlaylist(@NonNull final Context context) { + SharedPreferences sharedPreferences = context.getSharedPreferences("Shared", Context.MODE_PRIVATE); + String previous = sharedPreferences.getString("FAV", ""); + List playlists = PlaylistLoader.getAllPlaylists(context); + for (Playlist playlist : playlists) { + if (playlist.name.equals(previous)) { + if (!playlist.name.equals(context.getString(R.string.favorites))) { + PlaylistsUtil.renamePlaylist(context, playlist.id, context.getString(R.string + .favorites)); + SharedPreferences.Editor editor = sharedPreferences.edit(); + editor.putString("FAV", context.getString(R.string.favorites)); + editor.apply(); + } + return playlist; + } + } return PlaylistLoader.getPlaylist(context, PlaylistsUtil.createPlaylist(context, context.getString(R.string.favorites))); } @@ -381,4 +422,4 @@ public static String getLyrics(Song song) { return lyrics; } -} +} \ No newline at end of file diff --git a/app/src/main/java/com/kabouzeid/gramophone/util/PlaylistsUtil.java b/app/src/main/java/com/kabouzeid/gramophone/util/PlaylistsUtil.java index 3bfa079ae..88b7a4fcc 100644 --- a/app/src/main/java/com/kabouzeid/gramophone/util/PlaylistsUtil.java +++ b/app/src/main/java/com/kabouzeid/gramophone/util/PlaylistsUtil.java @@ -3,17 +3,21 @@ import android.content.ContentResolver; import android.content.ContentValues; import android.content.Context; +import android.content.SharedPreferences; import android.database.Cursor; import android.net.Uri; import android.os.Environment; import android.provider.BaseColumns; import android.provider.MediaStore; + import androidx.annotation.NonNull; import androidx.annotation.Nullable; + import android.widget.Toast; import com.kabouzeid.gramophone.R; import com.kabouzeid.gramophone.helper.M3UWriter; +import com.kabouzeid.gramophone.loader.PlaylistSongLoader; import com.kabouzeid.gramophone.model.Playlist; import com.kabouzeid.gramophone.model.PlaylistSong; import com.kabouzeid.gramophone.model.Song; @@ -53,6 +57,12 @@ public static int createPlaylist(@NonNull final Context context, @Nullable final if (cursor == null || cursor.getCount() < 1) { final ContentValues values = new ContentValues(1); values.put(MediaStore.Audio.PlaylistsColumns.NAME, name); + if(name.equals(context.getString(R.string.favorites))) { + SharedPreferences sharedPreferences = context.getSharedPreferences("Shared", Context.MODE_PRIVATE); + SharedPreferences.Editor editor = sharedPreferences.edit(); + editor.putString("FAV", context.getString(R.string.favorites)); + editor.apply(); + } final Uri uri = context.getContentResolver().insert( EXTERNAL_CONTENT_URI, values); @@ -111,7 +121,21 @@ public static void addToPlaylist(@NonNull final Context context, @NonNull final final Uri uri = MediaStore.Audio.Playlists.Members.getContentUri("external", playlistId); Cursor cursor = null; int base = 0; - + List previousSong = PlaylistSongLoader.getPlaylistSongList(context, playlistId); + List newSongs = new ArrayList<>(); + for (Song song : songs) { + boolean flag = false; + for (Song previous : previousSong) { + if (song.id == previous.id) { + flag = true; + break; + } + } + if (!flag) { + newSongs.add(song); + } + } + final int newSize = newSongs.size(); try { try { cursor = resolver.query(uri, projection, null, null, null); @@ -126,12 +150,11 @@ public static void addToPlaylist(@NonNull final Context context, @NonNull final } int numInserted = 0; - for (int offSet = 0; offSet < size; offSet += 1000) - numInserted += resolver.bulkInsert(uri, makeInsertItems(songs, offSet, 1000, base)); - + for (int offSet = 0; offSet < newSize; offSet += 1000) + numInserted += resolver.bulkInsert(uri, makeInsertItems(newSongs, offSet, 1000, base)); if (showToastOnFinish) { Toast.makeText(context, context.getResources().getString( - R.string.inserted_x_songs_into_playlist_x, numInserted, getNameForPlaylist(context, playlistId)), Toast.LENGTH_SHORT).show(); + R.string.inserted_x_songs_into_playlist_x, numInserted, getNameForPlaylist(context, playlistId), size - numInserted), Toast.LENGTH_SHORT).show(); } } catch (SecurityException ignored) { } diff --git a/app/src/main/res/values/strings.xml b/app/src/main/res/values/strings.xml index bded8aa70..eefa89bd6 100644 --- a/app/src/main/res/values/strings.xml +++ b/app/src/main/res/values/strings.xml @@ -66,7 +66,7 @@ Remove from playlist Grid size Grid size (land) - Inserted %1$d songs into the playlist %2$s. + Inserted %1$d songs into the playlist %2$s, %3$d of selected songs existed in this playlist. Created playlist %1$s. Deleted %1$d songs. Playlist %1$s already exists.