From 0873318931d0e3f81d9be93cfc19270f15dd7740 Mon Sep 17 00:00:00 2001 From: Adrian Ulrich Date: Thu, 24 Nov 2016 13:23:04 +0100 Subject: [PATCH] fix +1 updates and re-implement playlist sorting --- .../android/medialibrary/MediaLibrary.java | 53 ++++++++++++++++--- .../medialibrary/MediaLibraryBackend.java | 14 +++-- .../android/vanilla/PlaylistAdapter.java | 38 +------------ 3 files changed, 59 insertions(+), 46 deletions(-) diff --git a/src/ch/blinkenlights/android/medialibrary/MediaLibrary.java b/src/ch/blinkenlights/android/medialibrary/MediaLibrary.java index c1857720..f9e7829d 100644 --- a/src/ch/blinkenlights/android/medialibrary/MediaLibrary.java +++ b/src/ch/blinkenlights/android/medialibrary/MediaLibrary.java @@ -95,9 +95,8 @@ public class MediaLibrary { */ public static void updateSongPlayCounts(Context context, long id, boolean played) { final String column = played ? MediaLibrary.SongColumns.PLAYCOUNT : MediaLibrary.SongColumns.SKIPCOUNT; - ContentValues v = new ContentValues(); - v.put(column, column+" + 1"); - getBackend(context).update(MediaLibrary.TABLE_SONGS, v, MediaLibrary.SongColumns._ID+"="+id, null, false); + String selection = MediaLibrary.SongColumns._ID+"="+id; + getBackend(context).execSQL("UPDATE "+MediaLibrary.TABLE_SONGS+" SET "+column+"="+column+"+1 WHERE "+selection); } /** @@ -141,9 +140,9 @@ public class MediaLibrary { // First we need to get the position of the last item String[] projection = { MediaLibrary.PlaylistSongColumns.POSITION }; String selection = MediaLibrary.PlaylistSongColumns.PLAYLIST_ID+"="+playlistId; - String order = MediaLibrary.PlaylistSongColumns.POSITION; + String order = MediaLibrary.PlaylistSongColumns.POSITION+" DESC"; Cursor cursor = queryLibrary(context, MediaLibrary.TABLE_PLAYLISTS_SONGS, projection, selection, null, order); - if (cursor.moveToLast()) + if (cursor.moveToFirst()) pos = cursor.getLong(0) + 1; cursor.close(); @@ -188,12 +187,54 @@ public class MediaLibrary { String selection = MediaLibrary.PlaylistSongColumns.PLAYLIST_ID+"="+playlistId; ContentValues v = new ContentValues(); v.put(MediaLibrary.PlaylistSongColumns.PLAYLIST_ID, newId); - getBackend(context).update(MediaLibrary.TABLE_PLAYLISTS_SONGS, v, selection, null, true); + getBackend(context).update(MediaLibrary.TABLE_PLAYLISTS_SONGS, v, selection, null); removePlaylist(context, playlistId); } return newId; } + /** + * Moves an item in a playlist. Note: both items should be in the + * same playlist - 'fun things' will happen otherwise. + * + * @param context the context to use + * @param from the _id of the 'dragged' element + * @param to the _id of the 'repressed' element + */ + public static void movePlaylistItem(Context context, long from, long to) { + long fromPos, toPos, playlistId; + + String[] projection = { MediaLibrary.PlaylistSongColumns.POSITION, MediaLibrary.PlaylistSongColumns.PLAYLIST_ID }; + String selection = MediaLibrary.PlaylistSongColumns._ID+"="; + + // Get playlist id and position of the 'from' item + Cursor cursor = queryLibrary(context, MediaLibrary.TABLE_PLAYLISTS_SONGS, projection, selection+Long.toString(from), null, null); + cursor.moveToFirst(); + fromPos = cursor.getLong(0); + playlistId = cursor.getLong(1); + cursor.close(); + + // Get position of the target item + cursor = queryLibrary(context, MediaLibrary.TABLE_PLAYLISTS_SONGS, projection, selection+Long.toString(to), null, null); + cursor.moveToFirst(); + toPos = cursor.getLong(0); + cursor.close(); + + // Moving down -> We actually want to be below the target + if (toPos > fromPos) + toPos++; + + // shift all rows +1 + String setArg = MediaLibrary.PlaylistSongColumns.POSITION+"="+MediaLibrary.PlaylistSongColumns.POSITION+"+1"; + selection = MediaLibrary.PlaylistSongColumns.PLAYLIST_ID+"="+playlistId+" AND "+MediaLibrary.PlaylistSongColumns.POSITION+" >= "+toPos; + getBackend(context).execSQL("UPDATE "+MediaLibrary.TABLE_PLAYLISTS_SONGS+" SET "+setArg+" WHERE "+selection); + + ContentValues v = new ContentValues(); + v.put(MediaLibrary.PlaylistSongColumns.POSITION, toPos); + selection = MediaLibrary.PlaylistSongColumns._ID+"="+from; + getBackend(context).update(MediaLibrary.TABLE_PLAYLISTS_SONGS, v, selection, null); + } + /** * Registers a new content observer for the media library * diff --git a/src/ch/blinkenlights/android/medialibrary/MediaLibraryBackend.java b/src/ch/blinkenlights/android/medialibrary/MediaLibraryBackend.java index 420df8c1..d587124c 100644 --- a/src/ch/blinkenlights/android/medialibrary/MediaLibraryBackend.java +++ b/src/ch/blinkenlights/android/medialibrary/MediaLibraryBackend.java @@ -149,13 +149,12 @@ public class MediaLibraryBackend extends SQLiteOpenHelper { * @param values the data to set / modify * @param whereClause the selection * @param whereArgs arguments to selection - * @param userVisible controls if we shall call notifyObserver() to refresh the UI * @return the number of affected rows */ - public int update (String table, ContentValues values, String whereClause, String[] whereArgs, boolean userVisible) { + public int update (String table, ContentValues values, String whereClause, String[] whereArgs) { SQLiteDatabase dbh = getWritableDatabase(); int res = dbh.update(table, values, whereClause, whereArgs); - if (res > 0 && userVisible == true) { + if (res > 0) { // Note: we are not running notifyObserver for performance reasons here // Code which changes relations should just delete + re-insert data notifyObserver(); @@ -163,6 +162,15 @@ public class MediaLibraryBackend extends SQLiteOpenHelper { return res; } + /** + * Wrapper for SQLiteDatabase.execSQL() function + * + * @param sql the raw sql string + */ + public void execSQL(String sql) { + getWritableDatabase().execSQL(sql); + } + /** * Wrapper for SQLiteDatabase.insert() function * diff --git a/src/ch/blinkenlights/android/vanilla/PlaylistAdapter.java b/src/ch/blinkenlights/android/vanilla/PlaylistAdapter.java index ff6f06e1..440e1b05 100644 --- a/src/ch/blinkenlights/android/vanilla/PlaylistAdapter.java +++ b/src/ch/blinkenlights/android/vanilla/PlaylistAdapter.java @@ -185,43 +185,7 @@ public class PlaylistAdapter extends CursorAdapter implements Handler.Callback { // this can happen when the adapter changes during the drag return; -/* - * FIXME OBSOLETED - ContentResolver resolver = mContext.getContentResolver(); - Uri uri = MediaStore.Audio.Playlists.Members.getContentUri("external", mPlaylistId); - Cursor cursor = getCursor(); - - int start = Math.min(from, to); - int end = Math.max(from, to); - - long order; - if (start == 0) { - order = 0; - } else { - cursor.moveToPosition(start - 1); - order = cursor.getLong(5) + 1; - } - - cursor.moveToPosition(end); - long endOrder = cursor.getLong(5); - - // clear the rows we are replacing - String[] args = new String[] { Long.toString(order), Long.toString(endOrder) }; - resolver.delete(uri, "play_order >= ? AND play_order <= ?", args); - - // create the new rows - ContentValues[] values = new ContentValues[end - start + 1]; - for (int i = start, j = 0; i <= end; ++i, ++j, ++order) { - cursor.moveToPosition(i == to ? from : i > to ? i - 1 : i + 1); - ContentValues value = new ContentValues(2); - value.put(MediaStore.Audio.Playlists.Members.PLAY_ORDER, Long.valueOf(order)); - value.put(MediaStore.Audio.Playlists.Members.AUDIO_ID, cursor.getLong(3)); - values[j] = value; - } - - // insert the new rows - resolver.bulkInsert(uri, values); -*/ + MediaLibrary.movePlaylistItem(mContext, getItemId(from), getItemId(to)); changeCursor(runQuery()); }