Fix favorite toggling

This commit is contained in:
Eugene Cheung 2017-09-09 12:06:41 -04:00
commit 1f37982536
No known key found for this signature in database
GPG key ID: E1FD745328866B0A
4 changed files with 57 additions and 40 deletions

View file

@ -31,20 +31,15 @@ import static android.provider.MediaStore.Audio.Playlists.EXTERNAL_CONTENT_URI;
public class PlaylistsUtil {
public static boolean doesPlaylistExist(@NonNull final Context context, final int playlistId) {
if (playlistId == -1) {
return false;
}
return playlistId != -1 && doesPlaylistExist(context,
MediaStore.Audio.Playlists._ID + "=?",
new String[]{String.valueOf(playlistId)});
}
Cursor cursor = context.getContentResolver().query(
MediaStore.Audio.Playlists.Members.getContentUri("external", playlistId),
new String[]{}, null, null, null);
if (cursor == null || cursor.getCount() == 0) {
return false;
}
cursor.close();
return true;
public static boolean doesPlaylistExist(@NonNull final Context context, final String name) {
return doesPlaylistExist(context,
MediaStore.Audio.PlaylistsColumns.NAME + "=?",
new String[]{name});
}
public static int createPlaylist(@NonNull final Context context, @Nullable final String name) {
@ -52,7 +47,9 @@ public class PlaylistsUtil {
if (name != null && name.length() > 0) {
try {
Cursor cursor = context.getContentResolver().query(EXTERNAL_CONTENT_URI,
new String[]{MediaStore.Audio.Playlists._ID}, MediaStore.Audio.PlaylistsColumns.NAME + "=?", new String[]{name}, null);
new String[]{MediaStore.Audio.Playlists._ID},
MediaStore.Audio.PlaylistsColumns.NAME + "=?", new String[]{name},
null);
if (cursor == null || cursor.getCount() < 1) {
final ContentValues values = new ContentValues(1);
values.put(MediaStore.Audio.PlaylistsColumns.NAME, name);
@ -60,7 +57,7 @@ public class PlaylistsUtil {
EXTERNAL_CONTENT_URI,
values);
if (uri != null) {
// necessary because somehow the MediaStoreObserver is not notified when adding a playlist
// Necessary because somehow the MediaStoreObserver is not notified when adding a playlist
context.getContentResolver().notifyChange(Uri.parse("content://media"), null);
Toast.makeText(context, context.getResources().getString(
R.string.created_playlist_x, name), Toast.LENGTH_SHORT).show();
@ -69,10 +66,7 @@ public class PlaylistsUtil {
} else {
// Playlist exists
if (cursor.moveToFirst()) {
Toast.makeText(context, context.getResources().getString(
R.string.playlist_exists, name), Toast.LENGTH_SHORT).show();
cursor.close();
return -1;
id = cursor.getInt(cursor.getColumnIndex(MediaStore.Audio.Playlists._ID));
}
}
if (cursor != null) {
@ -231,8 +225,7 @@ public class PlaylistsUtil {
public static String getNameForPlaylist(@NonNull final Context context, final long id) {
try {
Cursor cursor = context.getContentResolver().query(
EXTERNAL_CONTENT_URI,
Cursor cursor = context.getContentResolver().query(EXTERNAL_CONTENT_URI,
new String[]{MediaStore.Audio.PlaylistsColumns.NAME},
BaseColumns._ID + "=?",
new String[]{String.valueOf(id)},
@ -254,4 +247,16 @@ public class PlaylistsUtil {
public static File savePlaylist(Context context, Playlist playlist) throws IOException {
return M3UWriter.write(context, new File(Environment.getExternalStorageDirectory(), "Playlists"), playlist);
}
private static boolean doesPlaylistExist(@NonNull Context context, @NonNull final String selection, @NonNull final String[] values) {
Cursor cursor = context.getContentResolver().query(EXTERNAL_CONTENT_URI,
new String[]{}, selection, values, null);
boolean exists = false;
if (cursor != null) {
exists = cursor.getCount() != 0;
cursor.close();
}
return exists;
}
}