[Android] [PATCH 2/3] History: don't use MediaList instance from LibVLC

Thomas Guillem thomas at gllm.fr
Mon Jan 5 16:17:18 CET 2015


Get history from AudioService.

Still work in progress: AudioService needs to store last played items in a
sqlite database.
---
 .../src/org/videolan/vlc/audio/AudioService.java   | 11 +++
 .../videolan/vlc/audio/AudioServiceController.java | 50 ++++++++++++
 .../src/org/videolan/vlc/gui/HistoryAdapter.java   | 90 ++++++++--------------
 .../src/org/videolan/vlc/gui/HistoryFragment.java  | 12 ++-
 .../vlc/interfaces/IAudioServiceCallback.aidl      |  3 +
 5 files changed, 102 insertions(+), 64 deletions(-)

diff --git a/vlc-android/src/org/videolan/vlc/audio/AudioService.java b/vlc-android/src/org/videolan/vlc/audio/AudioService.java
index 1055716..e818c72 100644
--- a/vlc-android/src/org/videolan/vlc/audio/AudioService.java
+++ b/vlc-android/src/org/videolan/vlc/audio/AudioService.java
@@ -675,6 +675,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.
      *
diff --git a/vlc-android/src/org/videolan/vlc/audio/AudioServiceController.java b/vlc-android/src/org/videolan/vlc/audio/AudioServiceController.java
index 1bd4902..b58961c 100644
--- a/vlc-android/src/org/videolan/vlc/audio/AudioServiceController.java
+++ b/vlc-android/src/org/videolan/vlc/audio/AudioServiceController.java
@@ -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() {
@@ -169,6 +189,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.
diff --git a/vlc-android/src/org/videolan/vlc/gui/HistoryAdapter.java b/vlc-android/src/org/videolan/vlc/gui/HistoryAdapter.java
index 29a9cd9..2b96c8a 100644
--- a/vlc-android/src/org/videolan/vlc/gui/HistoryAdapter.java
+++ b/vlc-android/src/org/videolan/vlc/gui/HistoryAdapter.java
@@ -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();
+    }
 }
diff --git a/vlc-android/src/org/videolan/vlc/gui/HistoryFragment.java b/vlc-android/src/org/videolan/vlc/gui/HistoryFragment.java
index 3ce3681..bd1f982 100644
--- a/vlc-android/src/org/videolan/vlc/gui/HistoryFragment.java
+++ b/vlc-android/src/org/videolan/vlc/gui/HistoryFragment.java
@@ -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;
@@ -102,6 +101,12 @@ public class HistoryFragment extends ListFragment implements IBrowser, IRefresha
     }
 
     @Override
+    public void onDestroy() {
+        mHistoryAdapter.release();
+        super.onDestroy();
+    }
+
+    @Override
     public void onCreateContextMenu(ContextMenu menu, View v, ContextMenuInfo menuInfo) {
         MenuInflater menuInflater = getActivity().getMenuInflater();
         menuInflater.inflate(R.menu.history_view, menu);
@@ -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);
diff --git a/vlc-android/src/org/videolan/vlc/interfaces/IAudioServiceCallback.aidl b/vlc-android/src/org/videolan/vlc/interfaces/IAudioServiceCallback.aidl
index 3e91745..7497c1d 100644
--- a/vlc-android/src/org/videolan/vlc/interfaces/IAudioServiceCallback.aidl
+++ b/vlc-android/src/org/videolan/vlc/interfaces/IAudioServiceCallback.aidl
@@ -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);
 }
-- 
2.1.3



More information about the Android mailing list