[Android] isAudio method for MediaWrapperList

Geoffrey Métais git at videolan.org
Fri Jun 9 17:19:33 CEST 2017


vlc-android | branch: master | Geoffrey Métais <geoffrey.metais at gmail.com> | Fri Jun  9 17:10:27 2017 +0200| [53a68fdc4c45e665a72130c24dfad760f9a7185f] | committer: Geoffrey Métais

isAudio method for MediaWrapperList

> https://code.videolan.org/videolan/vlc-android/commit/53a68fdc4c45e665a72130c24dfad760f9a7185f
---

 .../src/org/videolan/vlc/PlaybackService.java       | 21 ++++-----------------
 .../org/videolan/vlc/media/MediaWrapperList.java    | 14 ++++++++++++++
 2 files changed, 18 insertions(+), 17 deletions(-)

diff --git a/vlc-android/src/org/videolan/vlc/PlaybackService.java b/vlc-android/src/org/videolan/vlc/PlaybackService.java
index 5bb4125b0..399d1bef1 100644
--- a/vlc-android/src/org/videolan/vlc/PlaybackService.java
+++ b/vlc-android/src/org/videolan/vlc/PlaybackService.java
@@ -1567,13 +1567,8 @@ public class PlaybackService extends MediaBrowserServiceCompat implements IVLCVo
     }
 
     private synchronized void saveCurrentMedia() {
-        boolean audio = true;
-        for (int i = 0; i < mMediaList.size(); i++) {
-            if (mMediaList.getMedia(i).getType() == MediaWrapper.TYPE_VIDEO)
-                audio = false;
-        }
         SharedPreferences.Editor editor = mSettings.edit();
-        editor.putString(audio ? "current_song" : "current_media", mMediaList.getMRL(Math.max(mCurrentIndex, 0)));
+        editor.putString(mMediaList.isAudioList() ? "current_song" : "current_media", mMediaList.getMRL(Math.max(mCurrentIndex, 0)));
         editor.apply();
     }
 
@@ -1581,15 +1576,11 @@ public class PlaybackService extends MediaBrowserServiceCompat implements IVLCVo
         if (getCurrentMedia() == null)
             return;
         StringBuilder locations = new StringBuilder();
-        boolean audio = true;
-        for (int i = 0; i < mMediaList.size(); i++) {
-            if (mMediaList.getMedia(i).getType() == MediaWrapper.TYPE_VIDEO)
-                audio = false;
+        for (int i = 0; i < mMediaList.size(); i++)
             locations.append(" ").append(Uri.encode(mMediaList.getMRL(i)));
-        }
         //We save a concatenated String because putStringSet is APIv11.
         SharedPreferences.Editor editor = mSettings.edit();
-        editor.putString(audio ? "audio_list" : "media_list", locations.toString().trim());
+        editor.putString(mMediaList.isAudioList() ? "audio_list" : "media_list", locations.toString().trim());
         editor.apply();
     }
 
@@ -1597,11 +1588,7 @@ public class PlaybackService extends MediaBrowserServiceCompat implements IVLCVo
         if (getCurrentMedia() == null)
             return;
         SharedPreferences.Editor editor = mSettings.edit();
-        boolean audio = true;
-        for (int i = 0; i < mMediaList.size(); i++) {
-            if (mMediaList.getMedia(i).getType() == MediaWrapper.TYPE_VIDEO)
-                audio = false;
-        }
+        boolean audio = mMediaList.isAudioList();
         editor.putBoolean(audio ? "audio_shuffling" : "media_shuffling", mShuffling);
         editor.putInt(audio ? "audio_repeating" : "media_repeating", mRepeating);
         editor.putInt(audio ? "position_in_audio_list" : "position_in_media_list", mCurrentIndex);
diff --git a/vlc-android/src/org/videolan/vlc/media/MediaWrapperList.java b/vlc-android/src/org/videolan/vlc/media/MediaWrapperList.java
index 01ed26a97..addf65c13 100644
--- a/vlc-android/src/org/videolan/vlc/media/MediaWrapperList.java
+++ b/vlc-android/src/org/videolan/vlc/media/MediaWrapperList.java
@@ -44,6 +44,7 @@ public class MediaWrapperList {
     /* TODO: add locking */
     private ArrayList<MediaWrapper> mInternalList;
     private ArrayList<EventListener> mEventListenerList;
+    private int mVideoCount = 0;
 
     public MediaWrapperList() {
         mEventListenerList = new ArrayList<>();
@@ -52,6 +53,8 @@ public class MediaWrapperList {
 
     public void add(MediaWrapper media) {
         mInternalList.add(media);
+        if (media.getType() == MediaWrapper.TYPE_VIDEO)
+            ++mVideoCount;
     }
 
     public synchronized void addEventListener(EventListener listener) {
@@ -87,6 +90,7 @@ public class MediaWrapperList {
         for(int i = 0; i < mInternalList.size(); i++)
             signalEventListeners(EVENT_REMOVED, i, -1, mInternalList.get(i).getLocation());
         mInternalList.clear();
+        mVideoCount = 0;
     }
 
     private boolean isValid(int position) {
@@ -99,6 +103,8 @@ public class MediaWrapperList {
     public void insert(int position, MediaWrapper media) {
         mInternalList.add(position, media);
         signalEventListeners(EVENT_ADDED, position, -1, media.getLocation());
+        if (media.getType() == MediaWrapper.TYPE_VIDEO)
+            ++mVideoCount;
     }
 
     /**
@@ -125,6 +131,8 @@ public class MediaWrapperList {
     public void remove(int position) {
         if (!isValid(position))
             return;
+        if (mInternalList.get(position).getType() == MediaWrapper.TYPE_VIDEO)
+            --mVideoCount;
         String uri = mInternalList.get(position).getLocation();
         mInternalList.remove(position);
         signalEventListeners(EVENT_REMOVED, position, -1, uri);
@@ -134,6 +142,8 @@ public class MediaWrapperList {
         for (int i = 0; i < mInternalList.size(); ++i) {
             String uri = mInternalList.get(i).getLocation();
             if (uri.equals(location)) {
+                if (mInternalList.get(i).getType() == MediaWrapper.TYPE_VIDEO)
+                    --mVideoCount;
                 mInternalList.remove(i);
                 signalEventListeners(EVENT_REMOVED, i, -1, uri);
                 i--;
@@ -166,6 +176,10 @@ public class MediaWrapperList {
         return mInternalList.get(position).getLocation();
     }
 
+    public boolean isAudioList() {
+        return mVideoCount == 0;
+    }
+
     @Override
     public String toString() {
         StringBuilder sb = new StringBuilder();



More information about the Android mailing list