[Android] [PATCH 3/6] PlaybackServiceClient: merge 2 callbacks into a inner interface

Thomas Guillem thomas at gllm.fr
Thu Jun 11 17:45:41 CEST 2015


---
 .../org/videolan/vlc/PlaybackServiceClient.java    | 65 ++++++++--------------
 .../vlc/gui/AudioPlayerContainerActivity.java      |  4 +-
 .../src/org/videolan/vlc/gui/HistoryAdapter.java   | 14 ++++-
 .../org/videolan/vlc/gui/audio/AudioPlayer.java    | 11 +++-
 .../org/videolan/vlc/interfaces/IAudioPlayer.java  | 28 ----------
 .../gui/tv/audioplayer/AudioPlayerActivity.java    |  7 +--
 6 files changed, 48 insertions(+), 81 deletions(-)
 delete mode 100644 vlc-android/src/org/videolan/vlc/interfaces/IAudioPlayer.java

diff --git a/vlc-android/src/org/videolan/vlc/PlaybackServiceClient.java b/vlc-android/src/org/videolan/vlc/PlaybackServiceClient.java
index 4b212b7..59dca87 100644
--- a/vlc-android/src/org/videolan/vlc/PlaybackServiceClient.java
+++ b/vlc-android/src/org/videolan/vlc/PlaybackServiceClient.java
@@ -31,7 +31,6 @@ import android.os.RemoteException;
 import android.preference.PreferenceManager;
 import android.util.Log;
 
-import org.videolan.vlc.interfaces.IAudioPlayer;
 import org.videolan.vlc.interfaces.IPlaybackService;
 import org.videolan.vlc.interfaces.IPlaybackServiceCallback;
 
@@ -43,17 +42,18 @@ import java.util.List;
 public class PlaybackServiceClient {
     public static final String TAG = "PlaybackServiceClient";
 
+    public interface Callback {
+        void update();
+        void updateProgress();
+        void onMediaPlayedAdded(MediaWrapper media, int index);
+        void onMediaPlayedRemoved(int index);
+    }
+
     private static PlaybackServiceClient mInstance;
     private static boolean mIsBound = false;
     private IPlaybackService mAudioServiceBinder;
     private ServiceConnection mAudioServiceConnection;
-    private final ArrayList<IAudioPlayer> mAudioPlayer;
-    private final ArrayList<MediaPlayedListener> mMediaPlayedListener;
-
-    public interface MediaPlayedListener {
-        public void onMediaPlayedAdded(MediaWrapper media, int index);
-        public void onMediaPlayedRemoved(int index);
-    }
+    private final ArrayList<Callback> mCallbacks;
 
     private final IPlaybackServiceCallback mCallback = new IPlaybackServiceCallback.Stub() {
         @Override
@@ -78,8 +78,7 @@ public class PlaybackServiceClient {
     };
 
     private PlaybackServiceClient() {
-        mAudioPlayer = new ArrayList<IAudioPlayer>();
-        mMediaPlayedListener = new ArrayList<MediaPlayedListener>();
+        mCallbacks = new ArrayList<Callback>();
     }
 
     public static PlaybackServiceClient getInstance() {
@@ -186,46 +185,28 @@ public class PlaybackServiceClient {
     }
 
     /**
-     * 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
+     * Add a Callback
+     * @param callback
      */
-    public void addAudioPlayer(IAudioPlayer ap) {
-        if (!mAudioPlayer.contains(ap))
-            mAudioPlayer.add(ap);
+    public void addCallback(Callback callback) {
+        if (!mCallbacks.contains(callback))
+            mCallbacks.add(callback);
     }
 
     /**
-     * Remove AudioPlayer from list
-     * @param ap
+     * Remove Callback from list
+     * @param callback
      */
-    public void removeAudioPlayer(IAudioPlayer ap) {
-        if (mAudioPlayer.contains(ap))
-            mAudioPlayer.remove(ap);
+    public void removeCallback(Callback callback) {
+        if (mCallbacks.contains(callback))
+            mCallbacks.remove(callback);
     }
 
     /**
      * Update all AudioPlayer
      */
     private void updateAudioPlayer() {
-        for (IAudioPlayer player : mAudioPlayer)
+        for (Callback player : mCallbacks)
             player.update();
     }
 
@@ -233,18 +214,18 @@ public class PlaybackServiceClient {
      * Update the progress of all AudioPlayers
      */
     private void updateProgressAudioPlayer() {
-        for (IAudioPlayer player : mAudioPlayer)
+        for (Callback player : mCallbacks)
             player.updateProgress();
     }
 
     private void updateMediaPlayedAdded(MediaWrapper media, int index) {
-        for (MediaPlayedListener listener : mMediaPlayedListener) {
+        for (Callback listener : mCallbacks) {
             listener.onMediaPlayedAdded(media, index);
         }
     }
 
     private void updateMediaPlayedRemoved(int index) {
-        for (MediaPlayedListener listener : mMediaPlayedListener) {
+        for (Callback listener : mCallbacks) {
             listener.onMediaPlayedRemoved(index);
         }
     }
diff --git a/vlc-android/src/org/videolan/vlc/gui/AudioPlayerContainerActivity.java b/vlc-android/src/org/videolan/vlc/gui/AudioPlayerContainerActivity.java
index 281f706..d3fb65b 100644
--- a/vlc-android/src/org/videolan/vlc/gui/AudioPlayerContainerActivity.java
+++ b/vlc-android/src/org/videolan/vlc/gui/AudioPlayerContainerActivity.java
@@ -111,14 +111,14 @@ public class AudioPlayerContainerActivity extends AppCompatActivity {
     @Override
     protected void onResume() {
         super.onResume();
-        mAudioController.addAudioPlayer(mAudioPlayer);
+        mAudioController.addCallback(mAudioPlayer);
         PlaybackServiceClient.getInstance().bindAudioService(this);
     }
 
     @Override
     protected void onPause() {
         super.onPause();
-        mAudioController.removeAudioPlayer(mAudioPlayer);
+        mAudioController.removeCallback(mAudioPlayer);
         PlaybackServiceClient.getInstance().unbindAudioService(this);
     }
 
diff --git a/vlc-android/src/org/videolan/vlc/gui/HistoryAdapter.java b/vlc-android/src/org/videolan/vlc/gui/HistoryAdapter.java
index a118d9b..0f10d9a 100644
--- a/vlc-android/src/org/videolan/vlc/gui/HistoryAdapter.java
+++ b/vlc-android/src/org/videolan/vlc/gui/HistoryAdapter.java
@@ -39,7 +39,7 @@ import org.videolan.vlc.util.Util;
 
 import java.util.ArrayList;
 
-public class HistoryAdapter extends BaseAdapter implements PlaybackServiceClient.MediaPlayedListener {
+public class HistoryAdapter extends BaseAdapter implements PlaybackServiceClient.Callback {
     public final static String TAG = "VLC/HistoryAdapter";
 
     private LayoutInflater mInflater;
@@ -53,11 +53,11 @@ public class HistoryAdapter extends BaseAdapter implements PlaybackServiceClient
 
         mMediaList = new ArrayList<MediaWrapper>();
 
-        mAudioController.addMediaPlayedListener(this);
+        mAudioController.addCallback(this);
     }
 
     public void release () {
-        mAudioController.removeMediaPlayedListener(this);
+        mAudioController.removeCallback(this);
     }
 
     @Override
@@ -116,6 +116,14 @@ public class HistoryAdapter extends BaseAdapter implements PlaybackServiceClient
     }
 
     @Override
+    public void update() {
+    }
+
+    @Override
+    public void updateProgress() {
+    }
+
+    @Override
     public void onMediaPlayedAdded(MediaWrapper media, int index) {
         mMediaList.add(index, media);
         notifyDataSetChanged();
diff --git a/vlc-android/src/org/videolan/vlc/gui/audio/AudioPlayer.java b/vlc-android/src/org/videolan/vlc/gui/audio/AudioPlayer.java
index 9d8f9bd..49a844c 100644
--- a/vlc-android/src/org/videolan/vlc/gui/audio/AudioPlayer.java
+++ b/vlc-android/src/org/videolan/vlc/gui/audio/AudioPlayer.java
@@ -61,7 +61,6 @@ import org.videolan.vlc.gui.audio.widget.CoverMediaSwitcher;
 import org.videolan.vlc.gui.audio.widget.HeaderMediaSwitcher;
 import org.videolan.vlc.gui.dialogs.AdvOptionsDialog;
 import org.videolan.vlc.gui.dialogs.SavePlaylistDialog;
-import org.videolan.vlc.interfaces.IAudioPlayer;
 import org.videolan.vlc.util.Strings;
 import org.videolan.vlc.util.Util;
 import org.videolan.vlc.widget.AudioMediaSwitcher.AudioMediaSwitcherListener;
@@ -69,7 +68,7 @@ import org.videolan.vlc.widget.AudioMediaSwitcher.AudioMediaSwitcherListener;
 import java.util.ArrayList;
 import java.util.List;
 
-public class AudioPlayer extends Fragment implements IAudioPlayer, View.OnClickListener {
+public class AudioPlayer extends Fragment implements PlaybackServiceClient.Callback, View.OnClickListener {
     public static final String TAG = "VLC/AudioPlayer";
 
     private ProgressBar mProgressBar;
@@ -400,6 +399,14 @@ public class AudioPlayer extends Fragment implements IAudioPlayer, View.OnClickL
         }
     }
 
+    @Override
+    public void onMediaPlayedAdded(MediaWrapper media, int index) {
+    }
+
+    @Override
+    public void onMediaPlayedRemoved(int index) {
+    }
+
     private void updateList() {
         int currentIndex = -1;
 
diff --git a/vlc-android/src/org/videolan/vlc/interfaces/IAudioPlayer.java b/vlc-android/src/org/videolan/vlc/interfaces/IAudioPlayer.java
deleted file mode 100644
index c3db49f..0000000
--- a/vlc-android/src/org/videolan/vlc/interfaces/IAudioPlayer.java
+++ /dev/null
@@ -1,28 +0,0 @@
-/*****************************************************************************
- * IAudioPlayer.java
- *****************************************************************************
- * Copyright © 2011-2012 VLC authors and VideoLAN
- *
- * This program is free software; you can redistribute it and/or modify
- * it under the terms of the GNU General Public License as published by
- * the Free Software Foundation; either version 2 of the License, or
- * (at your option) any later version.
- *
- * This program is distributed in the hope that it will be useful,
- * but WITHOUT ANY WARRANTY; without even the implied warranty of
- * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
- * GNU General Public License for more details.
- *
- * You should have received a copy of the GNU General Public License
- * along with this program; if not, write to the Free Software
- * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston MA 02110-1301, USA.
- *****************************************************************************/
-
-package org.videolan.vlc.interfaces;
-
-public interface IAudioPlayer {
-
-    public void update();
-    public void updateProgress();
-
-}
diff --git a/vlc-android/tv/src/org/videolan/vlc/gui/tv/audioplayer/AudioPlayerActivity.java b/vlc-android/tv/src/org/videolan/vlc/gui/tv/audioplayer/AudioPlayerActivity.java
index 1a6164a..15db87f 100644
--- a/vlc-android/tv/src/org/videolan/vlc/gui/tv/audioplayer/AudioPlayerActivity.java
+++ b/vlc-android/tv/src/org/videolan/vlc/gui/tv/audioplayer/AudioPlayerActivity.java
@@ -31,7 +31,6 @@ import org.videolan.vlc.R;
 import org.videolan.vlc.gui.DividerItemDecoration;
 import org.videolan.vlc.gui.audio.AudioUtil;
 import org.videolan.vlc.gui.audio.MediaComparators;
-import org.videolan.vlc.interfaces.IAudioPlayer;
 import org.videolan.vlc.util.AndroidDevices;
 
 import android.app.Activity;
@@ -47,7 +46,7 @@ import android.widget.ImageView;
 import android.widget.ProgressBar;
 import android.widget.TextView;
 
-public class AudioPlayerActivity extends Activity implements PlaybackServiceClient.AudioServiceConnectionListener, IAudioPlayer, View.OnFocusChangeListener {
+public class AudioPlayerActivity extends Activity implements PlaybackServiceClient.AudioServiceConnectionListener, PlaybackServiceClient.Callback, View.OnFocusChangeListener {
     public static final String TAG = "VLC/AudioPlayerActivity";
 
     public static final String MEDIA_LIST = "media_list";
@@ -101,12 +100,12 @@ public class AudioPlayerActivity extends Activity implements PlaybackServiceClie
     public void onStart(){
         super.onStart();
         mAudioController.bindAudioService(this, this);
-        mAudioController.addAudioPlayer(this);
+        mAudioController.addCallback(this);
     }
 
     public void onStop(){
         super.onStop();
-        mAudioController.removeAudioPlayer(this);
+        mAudioController.removeCallback(this);
         mAudioController.unbindAudioService(this);
         mMediaList.clear();
     }
-- 
2.1.4



More information about the Android mailing list