diff --git a/res/drawable-hdpi/btn_rating_star_off_mtrl_alpha.png b/res/drawable-hdpi/btn_rating_star_off_mtrl_alpha.png new file mode 100644 index 00000000..51a895dc Binary files /dev/null and b/res/drawable-hdpi/btn_rating_star_off_mtrl_alpha.png differ diff --git a/res/drawable-hdpi/btn_rating_star_on_mtrl_alpha.png b/res/drawable-hdpi/btn_rating_star_on_mtrl_alpha.png new file mode 100644 index 00000000..2f59488d Binary files /dev/null and b/res/drawable-hdpi/btn_rating_star_on_mtrl_alpha.png differ diff --git a/res/drawable-hdpi/close.png b/res/drawable-hdpi/close.png deleted file mode 100644 index 8286ead9..00000000 Binary files a/res/drawable-hdpi/close.png and /dev/null differ diff --git a/res/drawable-hdpi/ic_menu_gallery.png b/res/drawable-hdpi/ic_menu_gallery.png deleted file mode 100644 index 7e94dc3c..00000000 Binary files a/res/drawable-hdpi/ic_menu_gallery.png and /dev/null differ diff --git a/res/drawable-mdpi/btn_rating_star_off_mtrl_alpha.png b/res/drawable-mdpi/btn_rating_star_off_mtrl_alpha.png new file mode 100644 index 00000000..d38aed24 Binary files /dev/null and b/res/drawable-mdpi/btn_rating_star_off_mtrl_alpha.png differ diff --git a/res/drawable-mdpi/btn_rating_star_on_mtrl_alpha.png b/res/drawable-mdpi/btn_rating_star_on_mtrl_alpha.png new file mode 100644 index 00000000..87dade39 Binary files /dev/null and b/res/drawable-mdpi/btn_rating_star_on_mtrl_alpha.png differ diff --git a/res/drawable-mdpi/close.png b/res/drawable-mdpi/close.png deleted file mode 100644 index 7192bb46..00000000 Binary files a/res/drawable-mdpi/close.png and /dev/null differ diff --git a/res/drawable-mdpi/ic_menu_gallery.png b/res/drawable-mdpi/ic_menu_gallery.png deleted file mode 100644 index 17c75eb8..00000000 Binary files a/res/drawable-mdpi/ic_menu_gallery.png and /dev/null differ diff --git a/res/drawable-xhdpi/btn_rating_star_off_mtrl_alpha.png b/res/drawable-xhdpi/btn_rating_star_off_mtrl_alpha.png new file mode 100644 index 00000000..33ec44c8 Binary files /dev/null and b/res/drawable-xhdpi/btn_rating_star_off_mtrl_alpha.png differ diff --git a/res/drawable-xhdpi/btn_rating_star_on_mtrl_alpha.png b/res/drawable-xhdpi/btn_rating_star_on_mtrl_alpha.png new file mode 100644 index 00000000..0166d70c Binary files /dev/null and b/res/drawable-xhdpi/btn_rating_star_on_mtrl_alpha.png differ diff --git a/res/drawable-xhdpi/close.png b/res/drawable-xhdpi/close.png deleted file mode 100644 index 56ed6d18..00000000 Binary files a/res/drawable-xhdpi/close.png and /dev/null differ diff --git a/res/drawable-xxhdpi/btn_rating_star_off_mtrl_alpha.png b/res/drawable-xxhdpi/btn_rating_star_off_mtrl_alpha.png new file mode 100644 index 00000000..4b49faf1 Binary files /dev/null and b/res/drawable-xxhdpi/btn_rating_star_off_mtrl_alpha.png differ diff --git a/res/drawable-xxhdpi/btn_rating_star_on_mtrl_alpha.png b/res/drawable-xxhdpi/btn_rating_star_on_mtrl_alpha.png new file mode 100644 index 00000000..561d9ef2 Binary files /dev/null and b/res/drawable-xxhdpi/btn_rating_star_on_mtrl_alpha.png differ diff --git a/res/drawable-xxhdpi/close.png b/res/drawable-xxhdpi/close.png deleted file mode 100644 index eceb9643..00000000 Binary files a/res/drawable-xxhdpi/close.png and /dev/null differ diff --git a/res/values/translatable.xml b/res/values/translatable.xml index a3e78825..bab29127 100644 --- a/res/values/translatable.xml +++ b/res/values/translatable.xml @@ -69,6 +69,7 @@ THE SOFTWARE. Save as playlist… Add to playlist… Add to favorites + Remove from favorites Favorites New playlist… Expand @@ -94,6 +95,10 @@ THE SOFTWARE. 1 song added to playlist %2$s. %1$d songs added to playlist %2$s. + + 1 song removed from playlist %2$s. + %1$d songs removed from playlist %2$s. + 1 song deleted. %d songs deleted. diff --git a/src/ch/blinkenlights/android/vanilla/FullPlaybackActivity.java b/src/ch/blinkenlights/android/vanilla/FullPlaybackActivity.java index 5c3ea7e4..e24982b7 100644 --- a/src/ch/blinkenlights/android/vanilla/FullPlaybackActivity.java +++ b/src/ch/blinkenlights/android/vanilla/FullPlaybackActivity.java @@ -1,5 +1,6 @@ /* * Copyright (C) 2012 Christopher Eby + * Copyright (C) 2016 Adrian Ulrich * * Permission is hereby granted, free of charge, to any person obtaining a copy * of this software and associated documentation files (the "Software"), to deal @@ -119,6 +120,7 @@ public class FullPlaybackActivity extends PlaybackActivity private TextView mFormatView; private String mReplayGain; private TextView mReplayGainView; + private MenuItem mFavorites; @Override public void onCreate(Bundle icicle) @@ -298,8 +300,6 @@ public class FullPlaybackActivity extends PlaybackActivity @Override protected void onSongChange(Song song) { - super.onSongChange(song); - setDuration(song == null ? 0 : song.duration); if (mTitle != null) { @@ -318,9 +318,14 @@ public class FullPlaybackActivity extends PlaybackActivity mCurrentSong = song; updateElapsedTime(); + mHandler.sendEmptyMessage(MSG_LOAD_FAVOURITE_INFO); + + // All quick UI updates are done: Time to update the cover + // and parse additional info if (mExtraInfoVisible) { mHandler.sendEmptyMessage(MSG_LOAD_EXTRA_INFO); } + super.onSongChange(song); } /** @@ -367,8 +372,11 @@ public class FullPlaybackActivity extends PlaybackActivity menu.add(0, MENU_ENQUEUE_ALBUM, 0, R.string.enqueue_current_album).setIcon(R.drawable.ic_menu_add); menu.add(0, MENU_ENQUEUE_ARTIST, 0, R.string.enqueue_current_artist).setIcon(R.drawable.ic_menu_add); menu.add(0, MENU_ENQUEUE_GENRE, 0, R.string.enqueue_current_genre).setIcon(R.drawable.ic_menu_add); - menu.add(0, MENU_SONG_FAVORITE, 0, R.string.add_to_favorites); + mFavorites = menu.add(0, MENU_SONG_FAVORITE, 0, R.string.add_to_favorites).setIcon(R.drawable.btn_rating_star_off_mtrl_alpha).setShowAsActionFlags(MenuItem.SHOW_AS_ACTION_IF_ROOM); menu.add(0, MENU_SHOW_QUEUE, 0, R.string.show_queue); + + // ensure that mFavorites is updated + mHandler.sendEmptyMessage(MSG_LOAD_FAVOURITE_INFO); return true; } @@ -390,15 +398,16 @@ public class FullPlaybackActivity extends PlaybackActivity PlaybackService.get(this).enqueueFromCurrent(MediaUtils.TYPE_GENRE); break; case MENU_SONG_FAVORITE: - String playlistName = getString(R.string.playlist_favorites); - long playlistId = Playlist.getOrCreatePlaylist(getContentResolver(), playlistName); Song song = (PlaybackService.get(this)).getSong(0); + long playlistId = Playlist.getFavoritesId(this, true); if (song != null) { - PlaylistTask playlistTask = new PlaylistTask(playlistId, playlistName); + PlaylistTask playlistTask = new PlaylistTask(playlistId, getString(R.string.playlist_favorites)); playlistTask.audioIds = new ArrayList(); playlistTask.audioIds.add(song.id); - mHandler.sendMessage(mHandler.obtainMessage(MSG_ADD_TO_PLAYLIST, playlistTask)); + int action = Playlist.isInPlaylist(getContentResolver(), playlistId, song) ? MSG_REMOVE_FROM_PLAYLIST : MSG_ADD_TO_PLAYLIST; + mHandler.sendMessage(mHandler.obtainMessage(action, playlistTask)); + mHandler.sendEmptyMessage(MSG_LOAD_FAVOURITE_INFO); } break; case MENU_DELETE: @@ -645,6 +654,14 @@ public class FullPlaybackActivity extends PlaybackActivity * Calls {@link PlaybackService#seekToProgress(int)}. */ private static final int MSG_SEEK_TO_PROGRESS = 18; + /** + * Check if passed song is a favorite + */ + private static final int MSG_LOAD_FAVOURITE_INFO = 19; + /** + * Updates the favorites state + */ + private static final int MSG_COMMIT_FAVOURITE_INFO = 20; @Override public boolean handleMessage(Message message) @@ -679,6 +696,19 @@ public class FullPlaybackActivity extends PlaybackActivity PlaybackService.get(this).seekToProgress(message.arg1); updateElapsedTime(); break; + case MSG_LOAD_FAVOURITE_INFO: + if (mCurrentSong != null) { + boolean found = Playlist.isInPlaylist(getContentResolver(), Playlist.getFavoritesId(this, false), mCurrentSong); + mUiHandler.sendMessage(mUiHandler.obtainMessage(MSG_COMMIT_FAVOURITE_INFO, found)); + } + break; + case MSG_COMMIT_FAVOURITE_INFO: + if (mFavorites != null) { + boolean found = (boolean)message.obj; + mFavorites.setIcon(found ? R.drawable.btn_rating_star_on_mtrl_alpha: R.drawable.btn_rating_star_off_mtrl_alpha); + mFavorites.setTitle(found ? R.string.remove_from_favorites : R.string.add_to_favorites); + } + break; default: return super.handleMessage(message); } diff --git a/src/ch/blinkenlights/android/vanilla/PlaybackActivity.java b/src/ch/blinkenlights/android/vanilla/PlaybackActivity.java index fc2b7f29..b68004da 100644 --- a/src/ch/blinkenlights/android/vanilla/PlaybackActivity.java +++ b/src/ch/blinkenlights/android/vanilla/PlaybackActivity.java @@ -399,8 +399,14 @@ public abstract class PlaybackActivity extends Activity * Call addToPlaylist with data from the playlisttask object. */ protected static final int MSG_ADD_TO_PLAYLIST = 2; - - protected static final int MSG_DELETE = 3; + /** + * Call removeFromPlaylist with data from the playlisttask object. + */ + protected static final int MSG_REMOVE_FROM_PLAYLIST = 3; + /** + * Removes a media object + */ + protected static final int MSG_DELETE = 4; @Override public boolean handleMessage(Message message) @@ -423,6 +429,11 @@ public abstract class PlaybackActivity extends Activity addToPlaylist(playlistTask); break; } + case MSG_REMOVE_FROM_PLAYLIST: { + PlaylistTask playlistTask = (PlaylistTask)message.obj; + removeFromPlaylist(playlistTask); + break; + } case MSG_RENAME_PLAYLIST: { NewPlaylistDialog dialog = (NewPlaylistDialog)message.obj; if (dialog.isAccepted()) { @@ -462,6 +473,27 @@ public abstract class PlaybackActivity extends Activity showToast(message, Toast.LENGTH_SHORT); } + /** + * Removes a set of songs represented by the playlistTask from a playlist. Displays a + * Toast notifying of success. + * + * @param playlistTask The pending PlaylistTask to execute + */ + private void removeFromPlaylist(PlaylistTask playlistTask) { + int count = 0; + + if (playlistTask.query != null) { + throw new IllegalArgumentException("Delete by query is not implemented yet"); + } + + if (playlistTask.audioIds != null) { + count += Playlist.removeFromPlaylist(getContentResolver(), playlistTask.playlistId, playlistTask.audioIds); + } + + String message = getResources().getQuantityString(R.plurals.removed_from_playlist, count, count, playlistTask.name); + showToast(message, Toast.LENGTH_SHORT); + } + /** * Delete the media represented by the given intent and show a Toast * informing the user of this. diff --git a/src/ch/blinkenlights/android/vanilla/Playlist.java b/src/ch/blinkenlights/android/vanilla/Playlist.java index 01d0a93b..f7e8a230 100644 --- a/src/ch/blinkenlights/android/vanilla/Playlist.java +++ b/src/ch/blinkenlights/android/vanilla/Playlist.java @@ -1,6 +1,6 @@ /* * Copyright (C) 2010 Christopher Eby - * Copyright (C) 2014 Adrian Ulrich + * Copyright (C) 2014-2016 Adrian Ulrich * * Permission is hereby granted, free of charge, to any person obtaining a copy * of this software and associated documentation files (the "Software"), to deal @@ -25,6 +25,7 @@ package ch.blinkenlights.android.vanilla; import java.util.ArrayList; +import android.content.Context; import android.content.ContentResolver; import android.content.ContentUris; import android.content.ContentValues; @@ -50,23 +51,6 @@ public class Playlist { return MediaUtils.queryResolver(resolver, media, projection, null, null, sort); } - /** - * Retrieves the id for a playlist with the given name. - * A new playlist will be created if given name does not exist - * @param resolver A ContentResolver to use. - * @param name The name of the playlist. - * @return The id of the playlist, or -1 if there is no playlist with the - * given name. - */ - public static long getOrCreatePlaylist(ContentResolver resolver, String name) - { - long id = getPlaylist(resolver, name); - if(id == -1) { - id = createPlaylist(resolver, name); - } - return id; - } - /** * Retrieves the id for a playlist with the given name. * @@ -147,7 +131,7 @@ public class Playlist { } /** - * Run the given query and add the results to the given playlist. Should be + * Adds a set of audioIds to the given playlist. Should be * run on a background thread. * * @param resolver A ContentResolver to use. @@ -184,6 +168,29 @@ public class Playlist { return count; } + /** + * Removes a set of audioIds from the given playlist. Should be + * run on a background thread. + * + * @param resolver A ContentResolver to use. + * @param playlistId The MediaStore.Audio.Playlist id of the playlist to + * modify. + * @param audioIds An ArrayList with all IDs to add + * @return The number of songs that were added to the playlist. + */ + public static int removeFromPlaylist(ContentResolver resolver, long playlistId, ArrayList audioIds) { + if (playlistId == -1) + return 0; + + int count = 0; + Uri uri = MediaStore.Audio.Playlists.Members.getContentUri("external", playlistId); + for (long id : audioIds) { + String where = MediaStore.Audio.Playlists.Members.AUDIO_ID + "=" + id; + count += resolver.delete(uri, where, null); + } + return count; + } + /** * Delete the playlist with the given id. * @@ -223,4 +230,44 @@ public class Playlist { deletePlaylist(resolver, id); } } + + /** + * Returns the ID of the 'favorites' playlist. + * + * @param context The Context to use + * @param create Create the playlist if it does not exist + * @return the id of the playlist, -1 on error + */ + public static long getFavoritesId(Context context, boolean create) { + String playlistName = context.getString(R.string.playlist_favorites); + long playlistId = getPlaylist(context.getContentResolver(), playlistName); + + if (playlistId == -1 && create == true) + playlistId = createPlaylist(context.getContentResolver(), playlistName); + + return playlistId; + } + + /** + * Searches for given song in given playlist + * + * @param resolver A ContentResolver to use. + * @param playlistId The ID of the Playlist to query + * @param song The Song to search in given playlistId + * @return true if `song' was found in `playlistId' + */ + public static boolean isInPlaylist(ContentResolver resolver, long playlistId, Song song) { + if (playlistId == -1 || song == null) + return false; + + boolean found = false; + String where = MediaStore.Audio.Playlists.Members.AUDIO_ID + "=" + song.id; + QueryTask query = MediaUtils.buildPlaylistQuery(playlistId, Song.EMPTY_PLAYLIST_PROJECTION, where); + Cursor cursor = query.runQuery(resolver); + if (cursor != null) { + found = cursor.getCount() != 0; + cursor.close(); + } + return found; + } }