implement draggable queuelist

This commit is contained in:
Adrian Ulrich 2014-10-16 20:09:17 +02:00
parent 0e1c948278
commit 204728224f
4 changed files with 95 additions and 44 deletions

View File

@ -2056,5 +2056,15 @@ public final class PlaybackService extends Service
setCurrentSong(0);
play();
}
/**
* Moves a songs position in the queue
*
* @param from the index of the song to be moved
* @param to the new index position of the song
*/
public void moveSong(int from, int to) {
mTimeline.moveSong(from, to);
}
}

View File

@ -27,12 +27,15 @@ import android.widget.AdapterView;
import android.widget.AdapterView.OnItemClickListener;
import android.widget.AdapterView.OnItemLongClickListener;
import android.widget.ListView;
import android.util.Log;
public class ShowQueueActivity extends Activity {
public class ShowQueueActivity
extends Activity
implements ShowQueueAdapter.OnItemMovedListener
{
private DragListView mListView;
private ShowQueueAdapter listAdapter;
private PlaybackService mService;
@Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
@ -40,7 +43,7 @@ public class ShowQueueActivity extends Activity {
setTitle(R.string.queue);
setContentView(R.layout.showqueue_listview);
mService = PlaybackService.get(this);
mListView = (DragListView) findViewById(R.id.list);
listAdapter = new ShowQueueAdapter(this, R.layout.showqueue_row);
mListView.setAdapter(listAdapter);
@ -50,13 +53,13 @@ public class ShowQueueActivity extends Activity {
mListView.setOnItemClickListener(new OnItemClickListener() {
@Override
public void onItemClick(AdapterView<?> parent, View view, int position, long id) {
jumpToSong(position);
mService.jumpToQueuePosition(position);
finish();
}});
mListView.setOnItemLongClickListener(new OnItemLongClickListener() {
@Override
public boolean onItemLongClick(AdapterView<?> parent, View view, int position, long id) {
jumpToSong(position);
mService.jumpToQueuePosition(position);
/* This activity will stay open on longpress, so we have
* to update the playmerker ourselfs */
listAdapter.highlightRow(position);
@ -83,31 +86,33 @@ public class ShowQueueActivity extends Activity {
@Override
public void onResume() {
super.onResume();
refreshSongQueueList();
refreshSongQueueList(true);
}
/*
** Tells the playback service to jump to a specific song
*/
private void jumpToSong(int id) {
PlaybackService service = PlaybackService.get(this);
service.jumpToQueuePosition(id);
/**
* Fired from adapter list if user moved an item
* @param from the item index that was dragged
* @param to the index where the item was dropped
*/
public void onItemMoved(int from, int to) {
mService.moveSong(from, to);
refreshSongQueueList(false);
}
private void refreshSongQueueList() {
private void refreshSongQueueList(boolean scroll) {
int i, stotal, spos;
PlaybackService service = PlaybackService.get(this);
stotal = service.getTimelineLength(); /* Total number of songs in queue */
spos = service.getTimelinePosition(); /* Current position in queue */
stotal = mService.getTimelineLength(); /* Total number of songs in queue */
spos = mService.getTimelinePosition(); /* Current position in queue */
listAdapter.clear(); /* Flush all existing entries... */
listAdapter.highlightRow(spos); /* and highlight current position */
for(i=0 ; i<stotal; i++) {
listAdapter.add(service.getSongByQueuePosition(i));
listAdapter.add(mService.getSongByQueuePosition(i));
}
mListView.setSelectionFromTop(spos, 0); /* scroll to currently playing song */
if (scroll == true)
mListView.setSelectionFromTop(spos, 0); /* scroll to currently playing song */
}

View File

@ -1,5 +1,5 @@
/*
* Copyright (C) 2013 Adrian Ulrich <adrian@blinkenlights.ch>
* Copyright (C) 2013-2014 Adrian Ulrich <adrian@blinkenlights.ch>
*
* This program is free software: you can redistribute it and/or modify
* it under the terms of the GNU General Public License as published by
@ -31,35 +31,44 @@ import android.text.SpannableStringBuilder;
import android.text.style.ForegroundColorSpan;
import android.text.Spannable;
import android.text.SpannableStringBuilder;
import android.util.Log;
public class ShowQueueAdapter
extends ArrayAdapter<Song>
implements DragListView.DragAdapter {
int resource;
Context context;
int hl_row;
int mResource;
int mHighlightRow;
Context mContext;
OnItemMovedListener mCallback;
public ShowQueueAdapter(Context context, int resource) {
super(context, resource);
this.resource = resource;
this.context = context;
this.hl_row = -1;
mResource = resource;
mContext = context;
mCallback = (OnItemMovedListener) context;
mHighlightRow = -1;
}
/*
** Tells the adapter to highlight a specific row id
** Set this to -1 to disable the feature
/**
* Called if user moved a queue item
*/
public interface OnItemMovedListener {
public void onItemMoved(int from, int to);
}
/**
* Tells the adapter to highlight a specific row id
* Set this to -1 to disable the feature
*/
public void highlightRow(int pos) {
this.hl_row = pos;
mHighlightRow = pos;
}
@Override
public View getView(int position, View convertView, ViewGroup parent) {
LayoutInflater inflater = ((Activity)context).getLayoutInflater();
View row = inflater.inflate(resource, parent, false);
LayoutInflater inflater = ((Activity)mContext).getLayoutInflater();
View row = inflater.inflate(mResource, parent, false);
Song song = getItem(position);
if (song != null) { // unlikely to fail but seems to happen in the wild.
@ -72,7 +81,7 @@ public class ShowQueueAdapter
}
View pmark = ((View)row.findViewById(R.id.playmark));
pmark.setVisibility( ( position == this.hl_row ? View.VISIBLE : View.INVISIBLE ));
pmark.setVisibility( ( position == mHighlightRow ? View.VISIBLE : View.INVISIBLE ));
return row;
}
@ -82,9 +91,16 @@ public class ShowQueueAdapter
// not implemented
}
/**
* Moves a songs position in the queue
*
* @param from the index of the song to be moved
* @param to the new index position of the song
*/
@Override
public void move(int from, int to) {
Log.v("VanillaMusic", "Moved FROM "+from+" to "+to);
mCallback.onItemMoved(from, to);
}
}

View File

@ -827,6 +827,26 @@ public final class SongTimeline {
changed();
}
public void moveSong(int from, int to) {
synchronized (this) {
Song tmp = mSongs.remove(from);
mSongs.add(to, tmp);
if (mCurrentPos == from) {
mCurrentPos = to; // active song was dragged to 'to'
} else if (from > mCurrentPos && to <= mCurrentPos) {
mCurrentPos++;
} else if (from < mCurrentPos && to >= mCurrentPos) {
mCurrentPos--;
}
saveActiveSongs(); // the current state is now 'sane'
broadcastChangedSongs(); // void due to saveActiveSongs();
}
changed();
}
/**
* Broadcasts that the timeline state has changed.
*/