Commit 9f9d1d48 authored by Geoffrey Métais's avatar Geoffrey Métais
Browse files

Pass MediaWrapper objects to AudioService

parent 02605d9e
......@@ -1201,24 +1201,16 @@ public class AudioService extends Service {
* @throws RemoteException
*/
@Override
public void load(List<String> mediaPathList, int position)
public void loadLocations(List<String> mediaPathList, int position)
throws RemoteException {
Log.v(TAG, "Loading position " + ((Integer)position).toString() + " in " + mediaPathList.toString());
mEventHandler.addHandler(mVlcEventHandler);
mMediaListPlayer.getMediaList().removeEventListener(mListEventListener);
mMediaListPlayer.getMediaList().clear();
MediaWrapperList mediaList = mMediaListPlayer.getMediaList();
mPrevious.clear();
ArrayList<MediaWrapper> mediaList = new ArrayList<MediaWrapper>();
MediaDatabase db = MediaDatabase.getInstance();
for (int i = 0; i < mediaPathList.size(); i++) {
String location = mediaPathList.get(i);
MediaWrapper mediaWrapper = db.getMedia(location);
if(mediaWrapper == null) {
if(!validateLocation(location)) {
if (mediaWrapper == null) {
if (!validateLocation(location)) {
Log.w(TAG, "Invalid location " + location);
showToast(getResources().getString(R.string.invalid_location, location), Toast.LENGTH_SHORT);
continue;
......@@ -1231,6 +1223,25 @@ public class AudioService extends Service {
}
mediaList.add(mediaWrapper);
}
load(mediaList, position);
}
@Override
public void load(List<MediaWrapper> mediaList, int position)
throws RemoteException {
Log.v(TAG, "Loading position " + ((Integer) position).toString() + " in " + mediaList.toString());
mEventHandler.addHandler(mVlcEventHandler);
mMediaListPlayer.getMediaList().removeEventListener(mListEventListener);
mMediaListPlayer.getMediaList().clear();
MediaWrapperList currentMediaList = mMediaListPlayer.getMediaList();
mPrevious.clear();
for (int i = 0; i < mediaList.size(); i++) {
currentMediaList.add(mediaList.get(i));
}
if (mMediaListPlayer.getMediaList().size() == 0) {
Log.w(TAG, "Warning: empty media list, nothing to play !");
......@@ -1318,28 +1329,15 @@ public class AudioService extends Service {
* Append to the current existing playlist
*/
@Override
public void append(List<String> mediaLocationList) throws RemoteException {
public void append(List<MediaWrapper> mediaList) throws RemoteException {
if (!hasCurrentMedia())
{
load(mediaLocationList, 0);
load(mediaList, 0);
return;
}
MediaDatabase db = MediaDatabase.getInstance();
for (int i = 0; i < mediaLocationList.size(); i++) {
String location = mediaLocationList.get(i);
MediaWrapper mediaWrapper = db.getMedia(location);
if(mediaWrapper == null) {
if (!validateLocation(location)) {
showToast(getResources().getString(R.string.invalid_location, location), Toast.LENGTH_SHORT);
continue;
}
Log.v(TAG, "Creating on-the-fly Media object for " + location);
final Media media = new Media(mLibVLC, location);
media.parse(); // FIXME: parse should'nt be done asynchronously
media.release();
mediaWrapper = new MediaWrapper(media);
}
for (int i = 0; i < mediaList.size(); i++) {
MediaWrapper mediaWrapper = mediaList.get(i);
mMediaListPlayer.getMediaList().add(mediaWrapper);
}
AudioService.this.saveMediaList();
......@@ -1395,6 +1393,11 @@ public class AudioService extends Service {
return mMediaListPlayer.getMediaList().getMRL(mCurrentIndex);
}
@Override
public MediaWrapper getCurrentMediaWrapper() throws RemoteException {
return AudioService.this.getCurrentMedia();
}
@Override
public void next() throws RemoteException {
AudioService.this.next();
......@@ -1541,7 +1544,7 @@ public class AudioService extends Service {
int position = Math.max(0, mediaPathList.indexOf(currentMedia));
// load playlist
try {
mInterface.load(mediaPathList, position);
mInterface.loadLocations(mediaPathList, position);
} catch (RemoteException e) {
e.printStackTrace();
}
......
......@@ -290,28 +290,40 @@ public class AudioServiceController implements IAudioPlayerControl {
}
}
public void load(String mediaPath) {
ArrayList<String> arrayList = new ArrayList<String>();
public void loadLocation(String mediaPath) {
ArrayList < String > arrayList = new ArrayList<String>();
arrayList.add(mediaPath);
loadLocations(arrayList, 0);
}
public void load(MediaWrapper media) {
ArrayList<MediaWrapper> arrayList = new ArrayList<MediaWrapper>();
arrayList.add(media);
load(arrayList, 0);
}
public void load(List<String> mediaPathList, int position) {
remoteProcedureCall(mAudioServiceBinder, Void.class, (Void)null, "load",
new Class<?>[] { List.class, int.class },
new Object[] { mediaPathList, position } );
public void loadLocations(List<String> mediaPathList, int position) {
remoteProcedureCall(mAudioServiceBinder, Void.class, (Void) null, "loadLocations",
new Class<?>[]{List.class, int.class},
new Object[]{mediaPathList, position});
}
public void append(String mediaPath) {
ArrayList<String> arrayList = new ArrayList<String>();
arrayList.add(mediaPath);
public void load(List<MediaWrapper> mediaList, int position) {
remoteProcedureCall(mAudioServiceBinder, Void.class, (Void) null, "load",
new Class<?>[]{List.class, int.class},
new Object[]{mediaList, position});
}
public void append(MediaWrapper media) {
ArrayList<MediaWrapper> arrayList = new ArrayList<MediaWrapper>();
arrayList.add(media);
append(arrayList);
}
public void append(List<String> mediaPathList) {
remoteProcedureCall(mAudioServiceBinder, Void.class, (Void)null, "append",
new Class<?>[] { List.class },
new Object[] { mediaPathList } );
public void append(List<MediaWrapper> mediaList) {
remoteProcedureCall(mAudioServiceBinder, Void.class, (Void) null, "append",
new Class<?>[]{List.class},
new Object[]{mediaList});
}
public void moveItem(int positionStart, int positionEnd) {
......@@ -347,6 +359,10 @@ public class AudioServiceController implements IAudioPlayerControl {
return remoteProcedureCall(mAudioServiceBinder, String.class, (String)null, "getCurrentMediaLocation", null, null);
}
public MediaWrapper getCurrentMediaWrapper() {
return remoteProcedureCall(mAudioServiceBinder, MediaWrapper.class, (MediaWrapper)null, "getCurrentMediaWrapper", null, null);
}
public void stop() {
remoteProcedureCall(mAudioServiceBinder, Void.class, (Void)null, "stop", null, null);
}
......
......@@ -61,7 +61,6 @@ public class AudioAlbumFragment extends Fragment implements AdapterView.OnItemCl
private MediaLibrary mMediaLibrary;
private AlbumAdapter mAdapter;
ArrayList<String> mMediaLocations = new ArrayList<String>();
private ArrayList<MediaWrapper> mMediaList;
private String mTitle;
......@@ -82,13 +81,6 @@ public class AudioAlbumFragment extends Fragment implements AdapterView.OnItemCl
public void setMediaList(ArrayList<MediaWrapper> mediaList, String title) {
this.mMediaList = mediaList;
mTitle = title;
String location;
for (MediaWrapper media : mediaList) {
location = media.getLocation();
if (location != null) {
mMediaLocations.add(location);
}
}
}
@Override
......@@ -146,7 +138,7 @@ public class AudioAlbumFragment extends Fragment implements AdapterView.OnItemCl
@Override
public void onItemClick(AdapterView<?> parent, View view, int position, long id) {
mAudioController.load(mMediaLocations, position);
mAudioController.load(mMediaList, position);
if (getActivity() != null)
getActivity().finish();
}
......@@ -223,7 +215,7 @@ public class AudioAlbumFragment extends Fragment implements AdapterView.OnItemCl
final int id = v.getId();
switch (id){
case R.id.album_play:
mAudioController.load(mMediaLocations, 0);
mAudioController.load(mMediaList, 0);
getActivity().finish();
break;
default:
......
......@@ -234,7 +234,7 @@ public class AudioAlbumsSongsFragment extends Fragment implements SwipeRefreshLa
int startPosition;
int groupPosition;
List<String> medias;
List<MediaWrapper> medias;
int id = item.getItemId();
boolean useAllItems = id == R.id.audio_list_browser_play_all;
......@@ -250,7 +250,7 @@ public class AudioAlbumsSongsFragment extends Fragment implements SwipeRefreshLa
if (id == R.id.audio_list_browser_delete) {
AlertDialog alertDialog = CommonDialogs.deleteMedia(
getActivity(),
mSongsAdapter.getLocations(groupPosition).get(0),
mSongsAdapter.getMedias(groupPosition).get(0).getLocation(),
new VLCRunnable(mSongsAdapter.getItem(groupPosition)) {
@Override
public void run(Object o) {
......@@ -272,7 +272,7 @@ public class AudioAlbumsSongsFragment extends Fragment implements SwipeRefreshLa
}
if (useAllItems) {
medias = new ArrayList<String>();
medias = new ArrayList<MediaWrapper>();
startPosition = mSongsAdapter.getListWithPosition(medias, groupPosition);
}
else {
......@@ -280,10 +280,10 @@ public class AudioAlbumsSongsFragment extends Fragment implements SwipeRefreshLa
switch (mViewPager.getCurrentItem())
{
case MODE_ALBUM: // albums
medias = mAlbumsAdapter.getLocations(groupPosition);
medias = mAlbumsAdapter.getMedias(groupPosition);
break;
case MODE_SONG: // songs
medias = mSongsAdapter.getLocations(groupPosition);
medias = mSongsAdapter.getMedias(groupPosition);
break;
default:
return true;
......@@ -332,7 +332,7 @@ public class AudioAlbumsSongsFragment extends Fragment implements SwipeRefreshLa
OnItemClickListener albumsListener = new OnItemClickListener() {
@Override
public void onItemClick(AdapterView<?> av, View v, int p, long id) {
ArrayList<MediaWrapper> mediaList = mAlbumsAdapter.getMedia(p);
ArrayList<MediaWrapper> mediaList = mAlbumsAdapter.getMedias(p);
Intent i = new Intent(getActivity(), SecondaryActivity.class);
i.putExtra("fragment", SecondaryActivity.ALBUM);
i.putParcelableArrayListExtra("list", mediaList);
......@@ -345,8 +345,8 @@ public class AudioAlbumsSongsFragment extends Fragment implements SwipeRefreshLa
OnItemClickListener songsListener = new OnItemClickListener() {
@Override
public void onItemClick(AdapterView<?> av, View v, int p, long id) {
ArrayList<String> mediaLocation = mSongsAdapter.getLocations(p);
mAudioController.load(mediaLocation, 0);
List<MediaWrapper> media = mSongsAdapter.getItem(p).mMediaList;
mAudioController.load(media, 0);
}
};
......
......@@ -317,15 +317,14 @@ public class AudioBrowserFragment extends MediaBrowserFragment implements SwipeR
OnItemClickListener songListener = new OnItemClickListener() {
@Override
public void onItemClick(AdapterView<?> av, View v, int p, long id) {
ArrayList<String> mediaLocation = mSongsAdapter.getLocations(p);
mAudioController.load(mediaLocation, 0);
mAudioController.load(mSongsAdapter.getMedias(p), 0);
}
};
OnItemClickListener artistListListener = new OnItemClickListener() {
@Override
public void onItemClick(AdapterView<?> av, View v, int p, long id) {
ArrayList<MediaWrapper> mediaList = mArtistsAdapter.getMedia(p);
ArrayList<MediaWrapper> mediaList = mArtistsAdapter.getMedias(p);
MainActivity activity = (MainActivity)getActivity();
Intent i = new Intent(getActivity(), SecondaryActivity.class);
i.putExtra("fragment", "albumsSongs");
......@@ -338,7 +337,7 @@ public class AudioBrowserFragment extends MediaBrowserFragment implements SwipeR
OnItemClickListener albumListListener = new OnItemClickListener() {
@Override
public void onItemClick(AdapterView<?> av, View v, int p, long id) {
ArrayList<MediaWrapper> mediaList = mAlbumsAdapter.getMedia(p);
ArrayList<MediaWrapper> mediaList = mAlbumsAdapter.getMedias(p);
Intent i = new Intent(getActivity(), SecondaryActivity.class);
i.putExtra("fragment", SecondaryActivity.ALBUM);
i.putParcelableArrayListExtra("list", mediaList);
......@@ -350,7 +349,7 @@ public class AudioBrowserFragment extends MediaBrowserFragment implements SwipeR
OnItemClickListener genreListListener = new OnItemClickListener() {
@Override
public void onItemClick(AdapterView<?> av, View v, int p, long id) {
ArrayList<MediaWrapper> mediaList = mGenresAdapter.getMedia(p);
ArrayList<MediaWrapper> mediaList = mGenresAdapter.getMedias(p);
Intent i = new Intent(getActivity(), SecondaryActivity.class);
i.putExtra("fragment", SecondaryActivity.ALBUMS_SONGS);
i.putParcelableArrayListExtra("list", mediaList);
......@@ -369,11 +368,9 @@ public class AudioBrowserFragment extends MediaBrowserFragment implements SwipeR
private void loadPlaylist(int position) {
ArrayList<MediaWrapper> mediaList = mPlaylistAdapter.getItem(position).mMediaList;
if (mediaList.size() == 1 && mediaList.get(0).getType() == MediaWrapper.TYPE_PLAYLIST) {
String mediaLocation = mediaList.get(0).getLocation();
mAudioController.load(mediaLocation);
mAudioController.load(mediaList.get(0));
} else {
ArrayList<String> mediaLocations = mPlaylistAdapter.getLocations(position);
mAudioController.load(mediaLocations, 0);
mAudioController.load(mPlaylistAdapter.getMedias(position), 0);
}
}
......@@ -417,7 +414,7 @@ public class AudioBrowserFragment extends MediaBrowserFragment implements SwipeR
int startPosition;
int groupPosition;
int mode = mViewPager.getCurrentItem();
List<String> medias;
List<MediaWrapper> medias;
int id = item.getItemId();
boolean useAllItems = id == R.id.audio_list_browser_play_all;
......@@ -438,11 +435,12 @@ public class AudioBrowserFragment extends MediaBrowserFragment implements SwipeR
adapter = mPlaylistAdapter;
} else
return false;
if (adapter.getCount() <= groupPosition || adapter.getLocations(groupPosition) == null || adapter.getLocations(groupPosition).isEmpty())
List<MediaWrapper> mediaList = adapter.getMedias(groupPosition);
if (adapter.getCount() <= groupPosition || mediaList == null || mediaList.isEmpty())
return false;
AlertDialog alertDialog = CommonDialogs.deleteMedia(
getActivity(),
adapter.getLocations(groupPosition).get(0),
mediaList.get(0).getLocation(),
adapter.getItem(groupPosition).mTitle,
new VLCRunnable(adapter.getItem(groupPosition)) {
@Override
......@@ -473,7 +471,7 @@ public class AudioBrowserFragment extends MediaBrowserFragment implements SwipeR
if (useAllItems) {
if (mSongsAdapter.getCount() <= groupPosition)
return false;
medias = new ArrayList<String>();
medias = new ArrayList<MediaWrapper>();
startPosition = mSongsAdapter.getListWithPosition(medias, groupPosition);
} else {
startPosition = 0;
......@@ -492,11 +490,11 @@ public class AudioBrowserFragment extends MediaBrowserFragment implements SwipeR
adapter = mGenresAdapter;
break;
case MODE_PLAYLIST: //For file playlist, we browse tracks with mediabrowser, and add them in callbacks onMediaAdded and onBrowseEnd
medias = mPlaylistAdapter.getLocations(groupPosition);
if (medias.size() == 1 && mPlaylistAdapter.getMedia(groupPosition).get(0).getType() == MediaWrapper.TYPE_PLAYLIST) {
medias = mPlaylistAdapter.getMedias(groupPosition);
if (medias.size() == 1 && mPlaylistAdapter.getMedias(groupPosition).get(0).getType() == MediaWrapper.TYPE_PLAYLIST) {
if (mMediaBrowser == null)
mMediaBrowser = new MediaBrowser(VLCInstance.get(), this);
mMediaBrowser.browse(mPlaylistAdapter.getMedia(groupPosition).get(0).getLocation());
mMediaBrowser.browse(mPlaylistAdapter.getMedias(groupPosition).get(0).getLocation());
return true;
} else
adapter = mPlaylistAdapter;
......@@ -506,7 +504,7 @@ public class AudioBrowserFragment extends MediaBrowserFragment implements SwipeR
}
if (groupPosition >= adapter.getCount())
return false;
medias = adapter.getLocations(groupPosition);
medias = adapter.getMedias(groupPosition);
}
if (append)
......@@ -517,7 +515,7 @@ public class AudioBrowserFragment extends MediaBrowserFragment implements SwipeR
}
public void onFabPlayAllClick(View view) {
List<String> medias = new ArrayList<String>();
List<MediaWrapper> medias = new ArrayList<MediaWrapper>();
mSongsAdapter.getListWithPosition(medias, 0);
if (mSongsAdapter.getCount() > 0) {
Random rand = new Random();
......@@ -611,11 +609,11 @@ public class AudioBrowserFragment extends MediaBrowserFragment implements SwipeR
}
}
ArrayList<String> mLocationsToAppend = new ArrayList<String>(); //Playlist tracks to append
ArrayList<MediaWrapper> mTracksToAppend = new ArrayList<MediaWrapper>(); //Playlist tracks to append
@Override
public void onMediaAdded(int index, Media media) {
mLocationsToAppend.add(media.getMrl());
mTracksToAppend.add(new MediaWrapper(media));
}
@Override
......@@ -623,7 +621,7 @@ public class AudioBrowserFragment extends MediaBrowserFragment implements SwipeR
@Override
public void onBrowseEnd() {
mAudioController.append(mLocationsToAppend);
mAudioController.append(mTracksToAppend);
}
@Override
......
......@@ -494,7 +494,7 @@ public class AudioBrowserListAdapter extends BaseAdapter implements SectionIndex
return sections.toArray();
}
public ArrayList<MediaWrapper> getMedia(int position) {
public ArrayList<MediaWrapper> getMedias(int position) {
// Return all the media of a list item list.
ArrayList<MediaWrapper> mediaList = new ArrayList<MediaWrapper>();
ListItem item = mItems.get(position);
......@@ -503,27 +503,36 @@ public class AudioBrowserListAdapter extends BaseAdapter implements SectionIndex
return mediaList;
}
public String getTitle(int position) {
return getItem(position).mTitle;
}
public ArrayList<String> getLocations(int position) {
return getLocations(position, false);
}
public ArrayList<String> getLocations(int position, boolean sortByTrackNumber) {
// Return all the media locations of a list item list.
ArrayList<String> locations = new ArrayList<String>();
public ArrayList<MediaWrapper> getMedias(int position, boolean sortByTrackNumber) {
ArrayList<MediaWrapper> mediaList = getMedias(position);
if (isEnabled(position)) {
ArrayList<MediaWrapper> mediaList = mItems.get(position).mMediaList;
if (sortByTrackNumber)
Collections.sort(mediaList, MediaComparators.byTrackNumber);
for (int i = 0; i < mediaList.size(); ++i)
locations.add(mediaList.get(i).getLocation());
}
return locations;
return mediaList;
}
public String getTitle(int position) {
return getItem(position).mTitle;
}
// public ArrayList<String> getLocations(int position) {
// return getLocations(position, false);
// }
//
// public ArrayList<String> getLocations(int position, boolean sortByTrackNumber) {
// // Return all the media locations of a list item list.
// ArrayList<String> locations = new ArrayList<String>();
// if (isEnabled(position)) {
// ArrayList<MediaWrapper> mediaList = mItems.get(position).mMediaList;
// if (sortByTrackNumber)
// Collections.sort(mediaList, MediaComparators.byTrackNumber);
// for (int i = 0; i < mediaList.size(); ++i)
// locations.add(mediaList.get(i).getLocation());
// }
// return locations;
// }
/**
* Returns a single list containing all media, along with the position of
* the first media in 'position' in the _new_ single list.
......@@ -532,7 +541,7 @@ public class AudioBrowserListAdapter extends BaseAdapter implements SectionIndex
* @param position Position to retrieve in to _this_ adapter.
* @return The position of 'position' in the new single list, or 0 if not found.
*/
public int getListWithPosition(List<String> outputList, int position) {
public int getListWithPosition(List<MediaWrapper> outputList, int position) {
int outputPosition = 0;
outputList.clear();
for(int i = 0; i < mItems.size(); i++) {
......@@ -540,8 +549,8 @@ public class AudioBrowserListAdapter extends BaseAdapter implements SectionIndex
if(position == i && !mItems.get(i).mMediaList.isEmpty())
outputPosition = outputList.size();
for(MediaWrapper k : mItems.get(i).mMediaList) {
outputList.add(k.getLocation());
for(MediaWrapper mediaWrapper : mItems.get(i).mMediaList) {
outputList.add(mediaWrapper);
}
}
}
......
......@@ -228,7 +228,7 @@ public class AudioPlayer extends Fragment implements IAudioPlayer, View.OnClickL
mSongsList.setOnItemClickListener(new OnItemClickListener() {
@Override
public void onItemClick(AdapterView<?> av, View v, int p, long id) {
mAudioController.load(mSongsListAdapter.getLocations(), p);
mAudioController.load(mSongsListAdapter.getItems(), p);
}
});
mSongsList.setOnItemLongClickListener(new OnItemLongClickListener() {
......
......@@ -122,13 +122,13 @@ public class BaseBrowserAdapter extends RecyclerView.Adapter<RecyclerView.ViewH
Util.openMedia(v.getContext(), mw);
else {
int position = 0;
LinkedList<String> mediaLocations = new LinkedList<String>();
LinkedList<MediaWrapper> mediaLocations = new LinkedList<MediaWrapper>();
MediaWrapper mediaItem;
for (Object item : mMediaList)
if (item instanceof MediaWrapper) {
mediaItem = (MediaWrapper) item;
if (mediaItem.getType() == MediaWrapper.TYPE_VIDEO || mediaItem.getType() == MediaWrapper.TYPE_AUDIO) {
mediaLocations.add(mediaItem.getLocation());
mediaLocations.add(mediaItem);
if (mediaItem.equals(mw))
position = mediaLocations.size() - 1;
}
......
......@@ -391,7 +391,7 @@ public abstract class BaseBrowserFragment extends MediaBrowserFragment implement
Util.openMedia(getActivity(), (MediaWrapper) mAdapter.getItem(position));
return true;
case R.id.directory_view_append:
AudioServiceController.getInstance().append(mw.getLocation());
AudioServiceController.getInstance().append(mw);
return true;
case R.id.directory_view_delete:
AlertDialog alertDialog = CommonDialogs.deleteMedia(getActivity(), mw.getLocation(),
......@@ -404,7 +404,7 @@ public abstract class BaseBrowserFragment extends MediaBrowserFragment implement
alertDialog.show();
return true;
case R.id.directory_view_play_audio:
AudioServiceController.getInstance().load(mw.getLocation());
AudioServiceController.getInstance().load(mw);
return true;
case R.id.directory_view_play_video:
VideoPlayerActivity.start(getActivity(), mw.getLocation());
......
......@@ -303,7 +303,7 @@ public class VideoGridFragment extends MediaBrowserFragment implements ISortable
}
protected void playAudio(MediaWrapper media) {
mAudioController.load(media.getLocation());
mAudioController.load(media);
}
private boolean handleContextItemSelected(MenuItem menu, int position) {
......
......@@ -30,14 +30,16 @@ interface IAudioService {
void previous();
void shuffle();
void setTime(long time);
void load(in List<String> mediaPathList, int position);
void append(in List<String> mediaPathList);
void loadLocations(in List<String> mediaPathList, int position);
void load(in List<MediaWrapper> mediaList, int position);
void append(in List<MediaWrapper> mediaList);
void moveItem(int positionStart, int positionEnd);
void remove(int position);
void removeLocation(String location);
List<MediaWrapper> getMedias();
List<String> getMediaLocations();
String getCurrentMediaLocation();
MediaWrapper getCurrentMediaWrapper();
boolean isPlaying();
boolean isShuffling();
int getRepeatType();
......
......@@ -206,11 +206,19 @@ public class Util {
String mrl = media.getLocation();