Add context menu to media items to allow appending to rather than replacing playlist

This commit is contained in:
Adam Baker 2015-07-30 21:23:51 +01:00
parent ba381244c9
commit 911686f0b7
4 changed files with 75 additions and 0 deletions

View file

@ -258,4 +258,13 @@ public class MainActivity extends ActionBarActivity {
mRouteFragment.play(playlist, start); mRouteFragment.play(playlist, start);
} }
/**
* Appends a list of tracks to the current playlist
*
* @param playlist - the list of items to add
*/
public void add(List<Item> playlist) {
mRouteFragment.append(playlist);
}
} }

View file

@ -531,6 +531,14 @@ public class RouteFragment extends MediaRouteDiscoveryFragment implements
} }
} }
/**
* Appends the supplied playlist to the current playlist
*/
public void append(List<Item> playlist) {
mPlaylistAdapter.add(playlist);
mMediaRouterPlayService.append(playlist);
}
/** /**
* Generates a time string in the format mm:ss from a time value in seconds. * Generates a time string in the format mm:ss from a time value in seconds.
* *

View file

@ -41,9 +41,13 @@ import android.os.IBinder;
import android.os.Parcelable; import android.os.Parcelable;
import android.support.v4.app.ListFragment; import android.support.v4.app.ListFragment;
import android.util.Log; import android.util.Log;
import android.view.ContextMenu;
import android.view.LayoutInflater; import android.view.LayoutInflater;
import android.view.Menu;
import android.view.MenuItem;
import android.view.View; import android.view.View;
import android.view.ViewGroup; import android.view.ViewGroup;
import android.widget.AdapterView.AdapterContextMenuInfo;
import android.widget.ListView; import android.widget.ListView;
import android.widget.TextView; import android.widget.TextView;
@ -232,6 +236,54 @@ public class ServerFragment extends ListFragment implements OnBackPressedListene
} }
} }
/**
* Create a suitable context menu for the currently selected item.
*/
@Override
public void onCreateContextMenu(ContextMenu menu, View v, ContextMenu.ContextMenuInfo info)
{
super.onCreateContextMenu(menu, v, info);
if (getListAdapter() != mFileAdapter)
return;
int position = ((AdapterContextMenuInfo)info).position;
if (mFileAdapter.getItem(position) instanceof Container)
return;
menu.add(Menu.NONE, 1, Menu.NONE, "Append track to playlist");
menu.add(Menu.NONE, 2, Menu.NONE, "Append folder playlist");
}
/**
* Process a context menu item selection
* @param item - the menu entry that was selected
* @return - true if the entry was processed
*/
@Override
public boolean onContextItemSelected(MenuItem item)
{
MainActivity activity = (MainActivity) getActivity();
List<Item> playlist = new ArrayList<Item>();
AdapterContextMenuInfo info = (AdapterContextMenuInfo) item.getMenuInfo();
switch(item.getItemId())
{
case 1:
playlist.add((Item) mFileAdapter.getItem((info.position)));
activity.add(playlist);
return true;
case 2:
for (int i = 0; i < mFileAdapter.getCount(); i++)
if (mFileAdapter.getItem(i) instanceof Item)
playlist.add((Item) mFileAdapter.getItem(i));
activity.add(playlist);
return true;
default:
return super.onContextItemSelected(item);
}
}
/** /**
* Displays available servers in the ListView. * Displays available servers in the ListView.
*/ */
@ -251,6 +303,7 @@ public class ServerFragment extends ListFragment implements OnBackPressedListene
getFiles(ROOT_DIRECTORY); getFiles(ROOT_DIRECTORY);
mEmptyView.setText(R.string.folder_list_empty); mEmptyView.setText(R.string.folder_list_empty);
getListView().setFastScrollEnabled(true); getListView().setFastScrollEnabled(true);
registerForContextMenu(getListView());
} }
/** /**

View file

@ -500,6 +500,11 @@ public class MediaRouterPlayService extends Service {
return mPlaylist; return mPlaylist;
} }
public void append(List<Item> list)
{
mPlaylist.addAll(list);
}
public void toggleShuffleEnabled() { public void toggleShuffleEnabled() {
mShuffle = !mShuffle; mShuffle = !mShuffle;
} }