Added favorites.
This commit is contained in:
parent
50a5502e24
commit
3dad62d78d
26 changed files with 133 additions and 62 deletions
|
|
@ -27,6 +27,20 @@ public class PlaylistLoader {
|
|||
return playlist;
|
||||
}
|
||||
|
||||
public static Playlist getPlaylist(final Context context, final String playlistName) {
|
||||
Playlist playlist = new Playlist();
|
||||
Cursor cursor = makePlaylistCursor(context, PlaylistsColumns.NAME + "=?", new String[]{playlistName});
|
||||
|
||||
if (cursor != null && cursor.moveToFirst()) {
|
||||
final int id = cursor.getInt(0);
|
||||
final String name = cursor.getString(1);
|
||||
playlist = new Playlist(id, name);
|
||||
}
|
||||
if (cursor != null)
|
||||
cursor.close();
|
||||
return playlist;
|
||||
}
|
||||
|
||||
public static List<Playlist> getAllPlaylists(final Context context) {
|
||||
List<Playlist> playlists = new ArrayList<>();
|
||||
Cursor cursor = makePlaylistCursor(context, null, null);
|
||||
|
|
|
|||
|
|
@ -391,6 +391,7 @@ public class MusicControllerActivity extends AbsFabActivity {
|
|||
setUpAlbumArtAndApplyPalette();
|
||||
songTotalTime.setText(MusicUtil.getReadableDurationString(song.duration));
|
||||
songCurrentProgress.setText(MusicUtil.getReadableDurationString(0));
|
||||
invalidateOptionsMenu();
|
||||
}
|
||||
|
||||
private void setHeadersText() {
|
||||
|
|
@ -548,6 +549,10 @@ public class MusicControllerActivity extends AbsFabActivity {
|
|||
@Override
|
||||
public boolean onCreateOptionsMenu(Menu menu) {
|
||||
getMenuInflater().inflate(R.menu.menu_music_playing, menu);
|
||||
boolean isFavorite = MusicUtil.isFavorite(this, song);
|
||||
menu.findItem(R.id.action_toggle_favorite)
|
||||
.setIcon(isFavorite ? R.drawable.ic_favorite_white_24dp : R.drawable.ic_favorite_outline_white_24dp)
|
||||
.setTitle(isFavorite ? getString(R.string.action_remove_from_favorites) : getString(R.string.action_add_to_favorites));
|
||||
return true;
|
||||
}
|
||||
|
||||
|
|
@ -555,6 +560,10 @@ public class MusicControllerActivity extends AbsFabActivity {
|
|||
public boolean onOptionsItemSelected(MenuItem item) {
|
||||
int id = item.getItemId();
|
||||
switch (id) {
|
||||
case R.id.action_toggle_favorite:
|
||||
MusicUtil.toggleFavorite(this, song);
|
||||
invalidateOptionsMenu();
|
||||
return true;
|
||||
case R.id.action_share:
|
||||
SongShareDialog.create(song.id).show(getSupportFragmentManager(), "SHARE_SONG");
|
||||
return true;
|
||||
|
|
|
|||
|
|
@ -17,10 +17,12 @@ import android.widget.Toast;
|
|||
import com.kabouzeid.gramophone.App;
|
||||
import com.kabouzeid.gramophone.R;
|
||||
import com.kabouzeid.gramophone.helper.MusicPlayerRemote;
|
||||
import com.kabouzeid.gramophone.loader.PlaylistLoader;
|
||||
import com.kabouzeid.gramophone.loader.SongFilePathLoader;
|
||||
import com.kabouzeid.gramophone.loader.SongLoader;
|
||||
import com.kabouzeid.gramophone.model.Artist;
|
||||
import com.kabouzeid.gramophone.model.DataBaseChangedEvent;
|
||||
import com.kabouzeid.gramophone.model.Playlist;
|
||||
import com.kabouzeid.gramophone.model.Song;
|
||||
|
||||
import java.io.File;
|
||||
|
|
@ -190,4 +192,24 @@ public class MusicUtil {
|
|||
Toast.makeText(context, context.getString(R.string.deleted_x_songs, songs.size()), Toast.LENGTH_SHORT).show();
|
||||
App.bus.post(new DataBaseChangedEvent(DataBaseChangedEvent.DATABASE_CHANGED));
|
||||
}
|
||||
|
||||
private static Playlist getFavoritesPlaylist(final Context context) {
|
||||
return PlaylistLoader.getPlaylist(context, context.getString(R.string.favorites));
|
||||
}
|
||||
|
||||
private static Playlist getOrCreateFavoritesPlaylist(final Context context) {
|
||||
return PlaylistLoader.getPlaylist(context, PlaylistsUtil.createPlaylist(context, context.getString(R.string.favorites)));
|
||||
}
|
||||
|
||||
public static boolean isFavorite(final Context context, final Song song) {
|
||||
return PlaylistsUtil.doPlaylistContains(context, getFavoritesPlaylist(context).id, song.id);
|
||||
}
|
||||
|
||||
public static void toggleFavorite(final Context context, final Song song) {
|
||||
if (isFavorite(context, song)) {
|
||||
PlaylistsUtil.removeFromPlaylist(context, song, getFavoritesPlaylist(context).id);
|
||||
} else {
|
||||
PlaylistsUtil.addToPlaylist(context, song, getOrCreateFavoritesPlaylist(context).id);
|
||||
}
|
||||
}
|
||||
}
|
||||
|
|
|
|||
|
|
@ -25,32 +25,36 @@ import java.util.List;
|
|||
public class PlaylistsUtil {
|
||||
|
||||
public static int createPlaylist(final Context context, final String name) {
|
||||
int id = -1;
|
||||
if (name != null && name.length() > 0) {
|
||||
final ContentResolver resolver = context.getContentResolver();
|
||||
final String[] projection = new String[]{
|
||||
MediaStore.Audio.PlaylistsColumns.NAME
|
||||
};
|
||||
final String selection = MediaStore.Audio.PlaylistsColumns.NAME + " = '" + name + "'";
|
||||
Cursor cursor = resolver.query(MediaStore.Audio.Playlists.EXTERNAL_CONTENT_URI,
|
||||
projection, selection, null, null);
|
||||
if (cursor.getCount() <= 0) {
|
||||
Cursor cursor = context.getContentResolver().query(MediaStore.Audio.Playlists.EXTERNAL_CONTENT_URI,
|
||||
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);
|
||||
final Uri uri = resolver.insert(MediaStore.Audio.Playlists.EXTERNAL_CONTENT_URI,
|
||||
final Uri uri = context.getContentResolver().insert(
|
||||
MediaStore.Audio.Playlists.EXTERNAL_CONTENT_URI,
|
||||
values);
|
||||
cursor.close();
|
||||
if (uri != null) {
|
||||
Toast.makeText(context, context.getResources().getString(
|
||||
R.string.created_playlist_x, name), Toast.LENGTH_SHORT).show();
|
||||
App.bus.post(new DataBaseChangedEvent(DataBaseChangedEvent.PLAYLISTS_CHANGED));
|
||||
return Integer.parseInt(uri.getLastPathSegment());
|
||||
id = Integer.parseInt(uri.getLastPathSegment());
|
||||
}
|
||||
} else {
|
||||
if (cursor.moveToFirst()) {
|
||||
id = cursor.getInt(cursor.getColumnIndex(MediaStore.Audio.Playlists._ID));
|
||||
}
|
||||
}
|
||||
cursor.close();
|
||||
if (cursor != null) {
|
||||
cursor.close();
|
||||
}
|
||||
}
|
||||
Toast.makeText(context, context.getResources().getString(
|
||||
R.string.couldnot_create_playlist_x, name), Toast.LENGTH_SHORT).show();
|
||||
return -1;
|
||||
if (id == -1) {
|
||||
Toast.makeText(context, context.getResources().getString(
|
||||
R.string.could_not_create_playlist), Toast.LENGTH_SHORT).show();
|
||||
}
|
||||
return id;
|
||||
}
|
||||
|
||||
// public static void clearPlaylist(final Context context, final int playlistId) {
|
||||
|
|
@ -81,11 +85,11 @@ public class PlaylistsUtil {
|
|||
App.bus.post(new DataBaseChangedEvent(DataBaseChangedEvent.PLAYLISTS_CHANGED));
|
||||
}
|
||||
|
||||
// public static void addToPlaylist(final Context context, final Song song, final int playlistId) {
|
||||
// List<Song> helperList = new ArrayList<>();
|
||||
// helperList.add(song);
|
||||
// addToPlaylist(context, helperList, playlistId);
|
||||
// }
|
||||
public static void addToPlaylist(final Context context, final Song song, final int playlistId) {
|
||||
List<Song> helperList = new ArrayList<>();
|
||||
helperList.add(song);
|
||||
addToPlaylist(context, helperList, playlistId);
|
||||
}
|
||||
|
||||
public static void addToPlaylist(final Context context, final List<Song> songs, final int playlistId) {
|
||||
final int size = songs.size();
|
||||
|
|
@ -133,15 +137,15 @@ public class PlaylistsUtil {
|
|||
return contentValues;
|
||||
}
|
||||
|
||||
// public static void removeFromPlaylist(final Context context, final PlaylistSong song) {
|
||||
// Uri uri = MediaStore.Audio.Playlists.Members.getContentUri(
|
||||
// "external", song.playlistId);
|
||||
// String selection = MediaStore.Audio.Playlists.Members._ID + " =?";
|
||||
// String[] selectionArgs = new String[]{String.valueOf(song.idInPlayList)};
|
||||
//
|
||||
// context.getContentResolver().delete(uri, selection, selectionArgs);
|
||||
// App.bus.post(new DataBaseChangedEvent(DataBaseChangedEvent.PLAYLISTS_CHANGED));
|
||||
// }
|
||||
public static void removeFromPlaylist(final Context context, final Song song, int playlistId) {
|
||||
Uri uri = MediaStore.Audio.Playlists.Members.getContentUri(
|
||||
"external", playlistId);
|
||||
String selection = MediaStore.Audio.Playlists.Members.AUDIO_ID + " =?";
|
||||
String[] selectionArgs = new String[]{String.valueOf(song.id)};
|
||||
|
||||
context.getContentResolver().delete(uri, selection, selectionArgs);
|
||||
App.bus.post(new DataBaseChangedEvent(DataBaseChangedEvent.PLAYLISTS_CHANGED));
|
||||
}
|
||||
|
||||
public static void removeFromPlaylist(final Context context, final List<PlaylistSong> songs) {
|
||||
final int playlistId = songs.get(0).playlistId;
|
||||
|
|
@ -158,7 +162,22 @@ public class PlaylistsUtil {
|
|||
context.getContentResolver().delete(uri, selection, selectionArgs);
|
||||
App.bus.post(new DataBaseChangedEvent(DataBaseChangedEvent.PLAYLISTS_CHANGED));
|
||||
}
|
||||
//
|
||||
|
||||
public static boolean doPlaylistContains(final Context context, final long playlistId, final int songId) {
|
||||
if (playlistId != -1) {
|
||||
Cursor c = context.getContentResolver().query(
|
||||
MediaStore.Audio.Playlists.Members.getContentUri("external", playlistId),
|
||||
new String[]{MediaStore.Audio.Playlists.Members.AUDIO_ID}, MediaStore.Audio.Playlists.Members.AUDIO_ID + "=?", new String[]{String.valueOf(songId)}, null);
|
||||
int count = 0;
|
||||
if (c != null) {
|
||||
count = c.getCount();
|
||||
c.close();
|
||||
}
|
||||
return count > 0;
|
||||
}
|
||||
return false;
|
||||
}
|
||||
|
||||
// public static int getSongCountForPlaylist(final Context context, final long playlistId) {
|
||||
// Cursor c = context.getContentResolver().query(
|
||||
// MediaStore.Audio.Playlists.Members.getContentUri("external", playlistId),
|
||||
|
|
|
|||
Loading…
Add table
Add a link
Reference in a new issue