[Android] [PATCH 06/14] MediaWrapperList: don't use EventHandler

Thomas Guillem thomas at gllm.fr
Fri Jan 16 17:12:06 CET 2015


Use MediaWrapperList.EventListener instead.
---
 libvlc/src/org/videolan/libvlc/EventHandler.java   |   2 +-
 .../src/org/videolan/vlc/MediaWrapperList.java     |  68 +++++++-----
 .../src/org/videolan/vlc/audio/AudioService.java   | 115 +++++++++------------
 3 files changed, 93 insertions(+), 92 deletions(-)

diff --git a/libvlc/src/org/videolan/libvlc/EventHandler.java b/libvlc/src/org/videolan/libvlc/EventHandler.java
index 2141439..6242b2d 100644
--- a/libvlc/src/org/videolan/libvlc/EventHandler.java
+++ b/libvlc/src/org/videolan/libvlc/EventHandler.java
@@ -97,7 +97,7 @@ public class EventHandler {
     private ArrayList<Handler> mEventHandler;
     private static EventHandler mInstance;
 
-    public EventHandler() {
+    EventHandler() {
         mEventHandler = new ArrayList<Handler>();
     }
 
diff --git a/vlc-android/src/org/videolan/vlc/MediaWrapperList.java b/vlc-android/src/org/videolan/vlc/MediaWrapperList.java
index 33b0c7e..e28fe89 100644
--- a/vlc-android/src/org/videolan/vlc/MediaWrapperList.java
+++ b/vlc-android/src/org/videolan/vlc/MediaWrapperList.java
@@ -22,22 +22,28 @@ package org.videolan.vlc;
 
 import java.util.ArrayList;
 
-import org.videolan.libvlc.EventHandler;
 import org.videolan.libvlc.LibVLC;
 
-import android.os.Bundle;
-
 public class MediaWrapperList {
     private static final String TAG = "VLC/MediaWrapperList";
 
+    public interface EventListener {
+        public void onItemAdded(int index, String mrl);
+        public void onItemRemoved(int index, String mrl);
+        public void onItemMoved(int indexBefore, int indexAfter, String mrl);
+    }
+
+    private static final int EVENT_ADDED = 0;
+    private static final int EVENT_REMOVED = 1;
+    private static final int EVENT_MOVED = 2;
 
     /* TODO: add locking */
     private ArrayList<MediaWrapper> mInternalList;
     private LibVLC mLibVLC; // Used to create new objects that require a libvlc instance
-    private EventHandler mEventHandler;
+    private ArrayList<EventListener> mEventListenerList;
 
     public MediaWrapperList(LibVLC libVLC) {
-        mEventHandler = new EventHandler(); // used in init() below to fire events at the correct targets
+        mEventListenerList = new ArrayList<EventListener>();
         mInternalList = new ArrayList<MediaWrapper>();
         mLibVLC = libVLC;
     }
@@ -46,14 +52,38 @@ public class MediaWrapperList {
         mInternalList.add(media);
     }
 
+    public synchronized void addEventListener(EventListener listener) {
+        if (!mEventListenerList.contains(listener))
+            mEventListenerList.add(listener);
+    }
+
+    public synchronized void removeEventListener(EventListener listener) {
+        mEventListenerList.remove(listener);
+    }
+
+    private synchronized void signalEventListeners(int event, int arg1, int arg2, String mrl) {
+        for (EventListener listener : mEventListenerList) {
+            switch (event) {
+            case EVENT_ADDED:
+                listener.onItemAdded(arg1, mrl);
+                break;
+            case EVENT_REMOVED:
+                listener.onItemRemoved(arg1, mrl);
+                break;
+            case EVENT_MOVED:
+                listener.onItemMoved(arg1, arg2, mrl);
+                break;
+            }
+        }
+    }
+
     /**
      * Clear the media list. (remove all media)
      */
     public void clear() {
         // Signal to observers of media being deleted.
-        for(int i = 0; i < mInternalList.size(); i++) {
-            signal_list_event(EventHandler.CustomMediaListItemDeleted, i, mInternalList.get(i).getLocation());
-        }
+        for(int i = 0; i < mInternalList.size(); i++)
+            signalEventListeners(EVENT_REMOVED, i, -1, mInternalList.get(i).getLocation());
         mInternalList.clear();
     }
 
@@ -66,7 +96,7 @@ public class MediaWrapperList {
     }
     public void insert(int position, MediaWrapper media) {
         mInternalList.add(position, media);
-        signal_list_event(EventHandler.CustomMediaListItemAdded, position, media.getLocation());
+        signalEventListeners(EVENT_ADDED, position, -1, media.getLocation());
     }
 
     /**
@@ -87,10 +117,7 @@ public class MediaWrapperList {
             mInternalList.add(endPosition, toMove);
         else
             mInternalList.add(endPosition - 1, toMove);
-        Bundle b = new Bundle();
-        b.putInt("index_before", startPosition);
-        b.putInt("index_after", endPosition);
-        mEventHandler.callback(EventHandler.CustomMediaListItemMoved, b);
+        signalEventListeners(EVENT_MOVED, startPosition, endPosition, toMove.getLocation());
     }
 
     public void remove(int position) {
@@ -98,7 +125,7 @@ public class MediaWrapperList {
             return;
         String uri = mInternalList.get(position).getLocation();
         mInternalList.remove(position);
-        signal_list_event(EventHandler.CustomMediaListItemDeleted, position, uri);
+        signalEventListeners(EVENT_REMOVED, position, -1, uri);
     }
 
     public void remove(String location) {
@@ -106,7 +133,7 @@ public class MediaWrapperList {
             String uri = mInternalList.get(i).getLocation();
             if (uri.equals(location)) {
                 mInternalList.remove(i);
-                signal_list_event(EventHandler.CustomMediaListItemDeleted, i, uri);
+                signalEventListeners(EVENT_REMOVED, i, -1, uri);
                 i--;
             }
         }
@@ -132,10 +159,6 @@ public class MediaWrapperList {
         return mInternalList.get(position).getLocation();
     }
 
-    public EventHandler getEventHandler() {
-        return mEventHandler;
-    }
-
     @Override
     public String toString() {
         StringBuilder sb = new StringBuilder();
@@ -149,11 +172,4 @@ public class MediaWrapperList {
         sb.append("}");
         return sb.toString();
     }
-
-    private void signal_list_event(int event, int position, String uri) {
-        Bundle b = new Bundle();
-        b.putString("item_uri", uri);
-        b.putInt("item_index", position);
-        mEventHandler.callback(event, b);
-    }
 }
diff --git a/vlc-android/src/org/videolan/vlc/audio/AudioService.java b/vlc-android/src/org/videolan/vlc/audio/AudioService.java
index c8475ec..26d9235 100644
--- a/vlc-android/src/org/videolan/vlc/audio/AudioService.java
+++ b/vlc-android/src/org/videolan/vlc/audio/AudioService.java
@@ -562,75 +562,60 @@ public class AudioService extends Service {
         }
     };
 
-    private final Handler mListEventHandler = new MediaListEventHandler(this);
+    private final MediaWrapperList.EventListener mListEventListener = new MediaWrapperList.EventListener() {
 
-    private static class MediaListEventHandler extends WeakHandler<AudioService> {
+        @Override
+        public void onItemAdded(int index, String mrl) {
+            Log.i(TAG, "CustomMediaListItemAdded");
+            if(mCurrentIndex >= index && !mExpanding.get())
+                mCurrentIndex++;
 
-        public MediaListEventHandler(AudioService audioService) {
-            super(audioService);
+            determinePrevAndNextIndices();
+            executeUpdate();
         }
 
         @Override
-        public void handleMessage(Message msg) {
-            AudioService service = getOwner();
-            if(service == null) return;
+        public void onItemRemoved(int index, String mrl) {
+            Log.i(TAG, "CustomMediaListItemDeleted");
+            if (mCurrentIndex == index && !mExpanding.get()) {
+                // The current item has been deleted
+                mCurrentIndex--;
+                determinePrevAndNextIndices();
+                if (mNextIndex != -1)
+                    next();
+                else if (mCurrentIndex != -1) {
+                    mMediaListPlayer.playIndex(mCurrentIndex);
+                    executeOnMediaPlayedAdded();
+                } else
+                    stop();
+            }
 
-            int index;
-            switch (msg.getData().getInt("event")) {
-            case EventHandler.CustomMediaListItemAdded:
-                Log.i(TAG, "CustomMediaListItemAdded");
-                index = msg.getData().getInt("item_index");
-                if(service.mCurrentIndex >= index && !service.mExpanding.get())
-                    service.mCurrentIndex++;
-
-                service.determinePrevAndNextIndices();
-                service.executeUpdate();
-                break;
-            case EventHandler.CustomMediaListItemDeleted:
-                Log.i(TAG, "CustomMediaListItemDeleted");
-                index = msg.getData().getInt("item_index");
-                if (service.mCurrentIndex == index && !service.mExpanding.get()) {
-                    // The current item has been deleted
-                    service.mCurrentIndex--;
-                    service.determinePrevAndNextIndices();
-                    if (service.mNextIndex != -1)
-                        service.next();
-                    else if (service.mCurrentIndex != -1) {
-                        service.mMediaListPlayer.playIndex(service.mCurrentIndex);
-                        service.executeOnMediaPlayedAdded();
-                    } else
-                        service.stop();
-                    break;
-                }
+            if(mCurrentIndex > index && !mExpanding.get())
+                mCurrentIndex--;
+            determinePrevAndNextIndices();
+            executeUpdate();
+        }
 
-                if(service.mCurrentIndex > index && !service.mExpanding.get())
-                    service.mCurrentIndex--;
-                service.determinePrevAndNextIndices();
-                service.executeUpdate();
-                break;
-            case EventHandler.CustomMediaListItemMoved:
-                Log.i(TAG, "CustomMediaListItemMoved");
-                int positionStart = msg.getData().getInt("index_before");
-                int positionEnd = msg.getData().getInt("index_after");
-                if (service.mCurrentIndex == positionStart) {
-                    service.mCurrentIndex = positionEnd;
-                    if (positionEnd > positionStart)
-                        service.mCurrentIndex--;
-                } else if (positionStart > service.mCurrentIndex
-                        && positionEnd <= service.mCurrentIndex)
-                    service.mCurrentIndex++;
-                else if (positionStart < service.mCurrentIndex
-                        && positionEnd > service.mCurrentIndex)
-                    service.mCurrentIndex--;
-
-                // If we are in random mode, we completely reset the stored previous track
-                // as their indices changed.
-                service.mPrevious.clear();
-
-                service.determinePrevAndNextIndices();
-                service.executeUpdate();
-                break;
-            }
+        @Override
+        public void onItemMoved(int indexBefore, int indexAfter, String mrl) {
+            Log.i(TAG, "CustomMediaListItemMoved");
+            if (mCurrentIndex == indexBefore) {
+                mCurrentIndex = indexAfter;
+                if (indexAfter > indexBefore)
+                    mCurrentIndex--;
+            } else if (indexBefore > mCurrentIndex
+                    && indexAfter <= mCurrentIndex)
+                mCurrentIndex++;
+            else if (indexBefore < mCurrentIndex
+                    && indexAfter > mCurrentIndex)
+                mCurrentIndex--;
+
+            // If we are in random mode, we completely reset the stored previous track
+            // as their indices changed.
+            mPrevious.clear();
+
+            determinePrevAndNextIndices();
+            executeUpdate();
         }
     };
 
@@ -858,7 +843,7 @@ public class AudioService extends Service {
     private void stop() {
         mLibVLC.stop();
         mEventHandler.removeHandler(mVlcEventHandler);
-        mMediaListPlayer.getMediaList().getEventHandler().removeHandler(mListEventHandler);
+        mMediaListPlayer.getMediaList().removeEventListener(mListEventListener);
         setRemoteControlClientPlaybackState(EventHandler.MediaPlayerStopped);
         mCurrentIndex = -1;
         mPrevious.clear();
@@ -1204,7 +1189,7 @@ public class AudioService extends Service {
             Log.v(TAG, "Loading position " + ((Integer)position).toString() + " in " + mediaPathList.toString());
             mEventHandler.addHandler(mVlcEventHandler);
 
-            mMediaListPlayer.getMediaList().getEventHandler().removeHandler(mListEventHandler);
+            mMediaListPlayer.getMediaList().removeEventListener(mListEventListener);
             mMediaListPlayer.getMediaList().clear();
             MediaWrapperList mediaList = mMediaListPlayer.getMediaList();
 
@@ -1240,7 +1225,7 @@ public class AudioService extends Service {
             }
 
             // Add handler after loading the list
-            mMediaListPlayer.getMediaList().getEventHandler().addHandler(mListEventHandler);
+            mMediaListPlayer.getMediaList().addEventListener(mListEventListener);
 
             mMediaListPlayer.playIndex(mCurrentIndex);
             executeOnMediaPlayedAdded();
-- 
2.1.3



More information about the Android mailing list