From 744c91adbca9200f5d3346604e0378bef393ff5d Mon Sep 17 00:00:00 2001 From: Adrian Ulrich Date: Fri, 17 Oct 2014 14:36:58 +0200 Subject: [PATCH] migrated playlist to DSLV --- res/layout/playlist_activity.xml | 35 ++++++------- res/layout/playlist_row.xml | 52 ++++++++++++------- .../android/vanilla/PlaylistActivity.java | 27 +++++++--- .../android/vanilla/PlaylistAdapter.java | 39 +++++++++----- 4 files changed, 96 insertions(+), 57 deletions(-) diff --git a/res/layout/playlist_activity.xml b/res/layout/playlist_activity.xml index b9593a11..e983bcb5 100644 --- a/res/layout/playlist_activity.xml +++ b/res/layout/playlist_activity.xml @@ -1,29 +1,28 @@ - + android:layout_height="fill_parent" + dslv:drag_enabled="false" + dslv:drag_start_mode="onMove" + dslv:drag_handle_id="@+id/dragger"/> diff --git a/res/layout/playlist_row.xml b/res/layout/playlist_row.xml index 2512b769..b6d3751a 100644 --- a/res/layout/playlist_row.xml +++ b/res/layout/playlist_row.xml @@ -1,27 +1,39 @@ - + android:background="@drawable/selectable_item_bg" + android:layout_width="fill_parent" + android:layout_height="wrap_content" + android:orientation="horizontal"> + + + diff --git a/src/ch/blinkenlights/android/vanilla/PlaylistActivity.java b/src/ch/blinkenlights/android/vanilla/PlaylistActivity.java index 8b52b67c..b25fe1d3 100644 --- a/src/ch/blinkenlights/android/vanilla/PlaylistActivity.java +++ b/src/ch/blinkenlights/android/vanilla/PlaylistActivity.java @@ -37,6 +37,7 @@ import android.view.View; import android.widget.AbsListView; import android.widget.AdapterView; import android.widget.Button; +import com.mobeta.android.dslv.DragSortListView; /** * The playlist activity where playlist songs can be viewed and reordered. @@ -58,7 +59,7 @@ public class PlaylistActivity extends Activity * An event loop running on a worker thread. */ private Looper mLooper; - private DragListView mListView; + private DragSortListView mListView; private PlaylistAdapter mAdapter; /** @@ -97,9 +98,10 @@ public class PlaylistActivity extends Activity setContentView(R.layout.playlist_activity); - DragListView view = (DragListView)findViewById(R.id.list); + DragSortListView view = (DragSortListView)findViewById(R.id.list); view.setOnItemClickListener(this); view.setOnCreateContextMenuListener(this); + view.setDropListener(onDrop); mListView = view; View header = LayoutInflater.from(this).inflate(R.layout.playlist_buttons, null); @@ -108,7 +110,6 @@ public class PlaylistActivity extends Activity mDeleteButton = (Button)header.findViewById(R.id.delete); mDeleteButton.setOnClickListener(this); view.addHeaderView(header, null, false); - mLooper = thread.getLooper(); mAdapter = new PlaylistAdapter(this, mLooper); view.setAdapter(mAdapter); @@ -150,7 +151,7 @@ public class PlaylistActivity extends Activity */ public void setEditing(boolean editing) { - mListView.setEditable(editing); + mListView.setDragEnabled(editing); mAdapter.setEditable(editing); int visible = editing ? View.GONE : View.VISIBLE; mDeleteButton.setVisibility(visible); @@ -190,7 +191,7 @@ public class PlaylistActivity extends Activity Intent intent = new Intent(); intent.putExtra("id", info.id); intent.putExtra("position", info.position); - intent.putExtra("audioId", (Long)info.targetView.getTag()); + intent.putExtra("audioId", (Long)info.targetView.findViewById(R.id.text).getTag()); menu.add(0, MENU_PLAY, 0, R.string.play).setIntent(intent); menu.add(0, MENU_PLAY_ALL, 0, R.string.play_all).setIntent(intent); @@ -207,7 +208,7 @@ public class PlaylistActivity extends Activity int pos = intent.getIntExtra("position", -1); if (itemId == MENU_REMOVE) { - mAdapter.remove(pos - mListView.getHeaderViewsCount()); + mAdapter.removeItem(pos - mListView.getHeaderViewsCount()); } else { performAction(itemId, pos, intent.getLongExtra("audioId", -1)); } @@ -277,4 +278,18 @@ public class PlaylistActivity extends Activity return super.onOptionsItemSelected(item); } } + + /** + * Fired from adapter listview if user moved an item + * @param from the item index that was dragged + * @param to the index where the item was dropped + */ + private DragSortListView.DropListener onDrop = + new DragSortListView.DropListener() { + @Override + public void drop(int from, int to) { + mAdapter.moveItem(from, to); + } + }; + } diff --git a/src/ch/blinkenlights/android/vanilla/PlaylistAdapter.java b/src/ch/blinkenlights/android/vanilla/PlaylistAdapter.java index 55f74fe2..246a7236 100644 --- a/src/ch/blinkenlights/android/vanilla/PlaylistAdapter.java +++ b/src/ch/blinkenlights/android/vanilla/PlaylistAdapter.java @@ -24,10 +24,8 @@ package ch.blinkenlights.android.vanilla; import android.content.ContentResolver; import android.content.ContentUris; -import android.content.ContentValues; import android.content.Context; import android.database.Cursor; -import android.graphics.drawable.Drawable; import android.net.Uri; import android.os.Handler; import android.os.Looper; @@ -38,11 +36,13 @@ import android.view.View; import android.view.ViewGroup; import android.widget.CursorAdapter; import android.widget.TextView; +import android.provider.MediaStore.Audio.Playlists.Members; + /** * CursorAdapter backed by MediaStore playlists. */ -public class PlaylistAdapter extends CursorAdapter implements Handler.Callback, DragListView.DragAdapter { +public class PlaylistAdapter extends CursorAdapter implements Handler.Callback { private static final String[] PROJECTION = new String[] { MediaStore.Audio.Playlists.Members._ID, MediaStore.Audio.Playlists.Members.TITLE, @@ -55,7 +55,6 @@ public class PlaylistAdapter extends CursorAdapter implements Handler.Callback, private final Handler mWorkerHandler; private final Handler mUiHandler; private final LayoutInflater mInflater; - private final Drawable mExpander; private long mPlaylistId; @@ -75,7 +74,6 @@ public class PlaylistAdapter extends CursorAdapter implements Handler.Callback, mUiHandler = new Handler(this); mWorkerHandler = new Handler(worker, this); mInflater = (LayoutInflater) context.getSystemService(Context.LAYOUT_INFLATER_SERVICE); - mExpander = context.getResources().getDrawable(R.drawable.grabber); } /** @@ -107,9 +105,10 @@ public class PlaylistAdapter extends CursorAdapter implements Handler.Callback, @Override public void bindView(View view, Context context, Cursor cursor) { - TextView textView = (TextView)view; + View dragger = ((View)view.findViewById(R.id.dragger)); + dragger.setVisibility( mEditable ? View.VISIBLE : View.INVISIBLE ); + TextView textView = ((TextView)view.findViewById(R.id.text)); textView.setText(cursor.getString(1)); - textView.setCompoundDrawablesWithIntrinsicBounds(mEditable ? mExpander : null, null, null, null); textView.setTag(cursor.getLong(3)); } @@ -162,8 +161,21 @@ public class PlaylistAdapter extends CursorAdapter implements Handler.Callback, return query.runQuery(resolver); } - @Override - public void move(int from, int to) + /** + * Moves a song in the playlist + * @param from original position of item + * @param to destination of item + **/ + public void moveItem(int from, int to) { + if (from == to) + return; + android.provider.MediaStore.Audio.Playlists.Members.moveItem(mContext.getContentResolver(), mPlaylistId , from, to); + mUiHandler.sendEmptyMessage(MSG_RUN_QUERY); + } + + +/* fixme: does the move-after-delete bug still exist in 4.x? + public void moveItem(int from, int to) { if (from == to) // easy mode @@ -215,13 +227,14 @@ public class PlaylistAdapter extends CursorAdapter implements Handler.Callback, changeCursor(runQuery(resolver)); } - - @Override - public void remove(int position) +*/ + public void removeItem(int position) { ContentResolver resolver = mContext.getContentResolver(); Uri uri = MediaStore.Audio.Playlists.Members.getContentUri("external", mPlaylistId); resolver.delete(ContentUris.withAppendedId(uri, getItemId(position)), null, null); - changeCursor(runQuery(resolver)); + mUiHandler.sendEmptyMessage(MSG_RUN_QUERY); } + + }