Skip to content
This repository has been archived by the owner on Feb 4, 2023. It is now read-only.

bug fixed #796. #834

Open
wants to merge 2 commits into
base: master
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
73 changes: 57 additions & 16 deletions app/src/main/java/com/kabouzeid/gramophone/util/MusicUtil.java
Original file line number Diff line number Diff line change
Expand Up @@ -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;
Expand Down Expand Up @@ -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)
);
}

Expand All @@ -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
);
}

Expand All @@ -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)
);
}

Expand Down Expand Up @@ -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
Expand Down Expand Up @@ -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
Expand All @@ -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<Playlist> 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<Playlist> 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)));
}

Expand Down Expand Up @@ -381,4 +422,4 @@ public static String getLyrics(Song song) {

return lyrics;
}
}
}
33 changes: 28 additions & 5 deletions app/src/main/java/com/kabouzeid/gramophone/util/PlaylistsUtil.java
Original file line number Diff line number Diff line change
Expand Up @@ -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;
Expand Down Expand Up @@ -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);
Expand Down Expand Up @@ -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<PlaylistSong> previousSong = PlaylistSongLoader.getPlaylistSongList(context, playlistId);
List<Song> 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);
Expand All @@ -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) {
}
Expand Down
2 changes: 1 addition & 1 deletion app/src/main/res/values/strings.xml
Original file line number Diff line number Diff line change
Expand Up @@ -66,7 +66,7 @@
<string name="action_remove_from_playlist">Remove from playlist</string>
<string name="action_grid_size">Grid size</string>
<string name="action_grid_size_land">Grid size (land)</string>
<string name="inserted_x_songs_into_playlist_x">Inserted %1$d songs into the playlist %2$s.</string>
<string name="inserted_x_songs_into_playlist_x">Inserted %1$d songs into the playlist %2$s, %3$d of selected songs existed in this playlist.</string>
<string name="created_playlist_x">Created playlist %1$s.</string>
<string name="deleted_x_songs">Deleted %1$d songs.</string>
<string name="playlist_exists">Playlist %1$s already exists.</string>
Expand Down