Commit d18f590b authored by Thomas Guillem's avatar Thomas Guillem
Browse files

History: don't use MediaList instance from LibVLC

Get history from AudioService.

Still work in progress: AudioService needs to store last played items in a
sqlite database.
parent 420daba6
......@@ -678,6 +678,17 @@ public class AudioService extends Service {
}
}
private void executeOnMediaPlayedAdded() {
final Media media = mMediaListPlayer.getMediaList().getMedia(mCurrentIndex);
for (IAudioServiceCallback callback : mCallback.keySet()) {
try {
callback.onMediaPlayedAdded(new MediaParcelable(media), 0);
} catch (RemoteException e) {
e.printStackTrace();
}
}
}
/**
* Return the current media.
*
......
......@@ -25,10 +25,12 @@ import java.lang.reflect.Method;
import java.util.ArrayList;
import java.util.List;
import org.videolan.libvlc.Media;
import org.videolan.vlc.interfaces.IAudioPlayer;
import org.videolan.vlc.interfaces.IAudioPlayerControl;
import org.videolan.vlc.interfaces.IAudioService;
import org.videolan.vlc.interfaces.IAudioServiceCallback;
import org.videolan.vlc.interfaces.MediaParcelable;
import android.content.ComponentName;
import android.content.Context;
......@@ -49,6 +51,13 @@ public class AudioServiceController implements IAudioPlayerControl {
private IAudioService mAudioServiceBinder;
private ServiceConnection mAudioServiceConnection;
private final ArrayList<IAudioPlayer> mAudioPlayer;
private final ArrayList<MediaPlayedListener> mMediaPlayedListener;
public interface MediaPlayedListener {
public void onMediaPlayedAdded(Media media, int index);
public void onMediaPlayedRemoved(int index);
}
private final IAudioServiceCallback mCallback = new IAudioServiceCallback.Stub() {
@Override
public void update() throws RemoteException {
......@@ -59,10 +68,21 @@ public class AudioServiceController implements IAudioPlayerControl {
public void updateProgress() throws RemoteException {
updateProgressAudioPlayer();
}
@Override
public void onMediaPlayedAdded(MediaParcelable mp, int index) throws RemoteException {
updateMediaPlayedAdded(mp.media, index);
}
@Override
public void onMediaPlayedRemoved(int index) throws RemoteException {
updateMediaPlayedRemoved(index);
}
};
private AudioServiceController() {
mAudioPlayer = new ArrayList<IAudioPlayer>();
mMediaPlayedListener = new ArrayList<MediaPlayedListener>();
}
public static AudioServiceController getInstance() {
......@@ -168,6 +188,24 @@ public class AudioServiceController implements IAudioPlayerControl {
}
}
/**
* Add a MediaPlayedListener
* @param li
*/
public void addMediaPlayedListener(MediaPlayedListener li) {
if (!mMediaPlayedListener.contains(li))
mMediaPlayedListener.add(li);
}
/**
* Remove MediaPlayedListener from list
* @param li
*/
public void removeMediaPlayedListener(MediaPlayedListener li) {
if (mMediaPlayedListener.contains(li))
mMediaPlayedListener.remove(li);
}
/**
* Add a AudioPlayer
* @param ap
......@@ -202,6 +240,18 @@ public class AudioServiceController implements IAudioPlayerControl {
player.updateProgress();
}
private void updateMediaPlayedAdded(Media media, int index) {
for (MediaPlayedListener listener : mMediaPlayedListener) {
listener.onMediaPlayedAdded(media, index);
}
}
private void updateMediaPlayedRemoved(int index) {
for (MediaPlayedListener listener : mMediaPlayedListener) {
listener.onMediaPlayedRemoved(index);
}
}
/**
* This is a handy utility function to call remote procedure calls from mAudioServiceBinder
* to reduce code duplication across methods of AudioServiceController.
......
......@@ -20,19 +20,17 @@
*****************************************************************************/
package org.videolan.vlc.gui;
import org.videolan.libvlc.EventHandler;
import org.videolan.libvlc.LibVLC;
import org.videolan.libvlc.LibVlcException;
import java.util.ArrayList;
import org.videolan.libvlc.Media;
import org.videolan.vlc.R;
import org.videolan.vlc.VLCApplication;
import org.videolan.vlc.audio.AudioServiceController;
import org.videolan.vlc.gui.audio.AudioUtil;
import org.videolan.vlc.util.Util;
import org.videolan.vlc.util.WeakHandler;
import android.content.Context;
import android.graphics.Bitmap;
import android.os.Message;
import android.util.Log;
import android.view.LayoutInflater;
import android.view.View;
......@@ -41,33 +39,35 @@ import android.widget.BaseAdapter;
import android.widget.ImageView;
import android.widget.TextView;
public class HistoryAdapter extends BaseAdapter {
public class HistoryAdapter extends BaseAdapter implements AudioServiceController.MediaPlayedListener {
public final static String TAG = "VLC/HistoryAdapter";
private LayoutInflater mInflater;
private LibVLC mLibVLC;
private final AudioServiceController mAudioController;
private final ArrayList<Media> mMediaList;
public HistoryAdapter(Context context) {
mInflater = LayoutInflater.from(context);
try {
mLibVLC = LibVLC.getInstance();
} catch (LibVlcException e) {
Log.d(TAG, "LibVlcException encountered in HistoryAdapter", e);
return;
}
EventHandler em = mLibVLC.getMediaList().getEventHandler();
em.addHandler(new HistoryEventHandler(this));
mAudioController = AudioServiceController.getInstance();
mMediaList = new ArrayList<Media>();
mAudioController.addMediaPlayedListener(this);
}
public void release () {
mAudioController.removeMediaPlayedListener(this);
}
@Override
public int getCount() {
return mLibVLC.getMediaList().size();
return mMediaList.size();
}
@Override
public Object getItem(int arg0) {
return mLibVLC.getMediaList().getMRL(arg0);
return mMediaList.get(arg0).getLocation();
}
@Override
......@@ -94,7 +94,7 @@ public class HistoryAdapter extends BaseAdapter {
holder = (DirectoryAdapter.DirectoryViewHolder) v.getTag();
String holderText = "";
Media m = mLibVLC.getMediaList().getMedia(position);
Media m = mMediaList.get(position);
if (m == null )
return v;
......@@ -112,49 +112,19 @@ public class HistoryAdapter extends BaseAdapter {
return v;
}
/**
* The media list changed.
*
* @param added Set to true if the media list was added to
* @param uri The URI added/removed
* @param index The index added/removed at
*/
public void updateEvent(Boolean added, String uri, int index) {
if(added) {
Log.v(TAG, "Added index " + index + ": " + uri);
} else {
Log.v(TAG, "Removed index " + index + ": " + uri);
}
notifyDataSetChanged();
public void remove(int position) {
mAudioController.remove(position);
}
public void refresh() {
this.notifyDataSetChanged();
@Override
public void onMediaPlayedAdded(Media media, int index) {
mMediaList.add(index, media);
notifyDataSetChanged();
}
/**
* Handle changes to the media list
*/
private static class HistoryEventHandler extends WeakHandler<HistoryAdapter> {
public HistoryEventHandler(HistoryAdapter owner) {
super(owner);
}
@Override
public void handleMessage(Message msg) {
HistoryAdapter adapater = getOwner();
if(adapater == null) return;
String item_uri = msg.getData().getString("item_uri");
int item_index = msg.getData().getInt("item_index");
switch (msg.getData().getInt("event")) {
case EventHandler.CustomMediaListItemAdded:
adapater.updateEvent(true, item_uri, item_index);
break;
case EventHandler.CustomMediaListItemDeleted:
adapater.updateEvent(false, item_uri, item_index);
break;
}
}
};
@Override
public void onMediaPlayedRemoved(int index) {
mMediaList.remove(index);
notifyDataSetChanged();
}
}
......@@ -20,7 +20,6 @@
*****************************************************************************/
package org.videolan.vlc.gui;
import org.videolan.libvlc.LibVLC;
import org.videolan.libvlc.LibVlcUtil;
import org.videolan.vlc.R;
import org.videolan.vlc.audio.AudioServiceController;
......@@ -101,6 +100,12 @@ public class HistoryFragment extends ListFragment implements IBrowser, IRefresha
return v;
}
@Override
public void onDestroy() {
mHistoryAdapter.release();
super.onDestroy();
}
@Override
public void onCreateContextMenu(ContextMenu menu, View v, ContextMenuInfo menuInfo) {
MenuInflater menuInflater = getActivity().getMenuInflater();
......@@ -131,8 +136,7 @@ public class HistoryFragment extends ListFragment implements IBrowser, IRefresha
playListIndex(info.position);
return true;
} else if(id == R.id.history_view_delete) {
LibVLC.getExistingInstance().getMediaList().remove(info.position);
mHistoryAdapter.refresh();
mHistoryAdapter.remove(info.position);
return true;
}
return super.onContextItemSelected(item);
......@@ -141,7 +145,7 @@ public class HistoryFragment extends ListFragment implements IBrowser, IRefresha
@Override
public void refresh() {
if( mHistoryAdapter != null ) {
mHistoryAdapter.refresh();
mHistoryAdapter.notifyDataSetChanged();
focusHelper(mHistoryAdapter.getCount() == 0);
} else
focusHelper(true);
......
......@@ -19,8 +19,11 @@
*****************************************************************************/
package org.videolan.vlc.interfaces;
import org.videolan.libvlc.Media;
interface IAudioServiceCallback {
void update();
void updateProgress();
void onMediaPlayedAdded(in Media media, int index);
void onMediaPlayedRemoved(int index);
}
Markdown is supported
0% or .
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment