[Android] [PATCH] MediaGroup refactoring.

Andrey Khalyavin halyavin at google.com
Thu Dec 4 19:56:55 CET 2014


This patch only makes algorithm clearer. It doesn't fix the bug that
groups may need to be merged as they became more general.

-- Andrey Khalyavin

>From f06a61155301f32e0bb719e3f181045ffe4d01ff Mon Sep 17 00:00:00 2001
From: Andrey Khalyavin <halyavin at google.com>
Date: Thu, 4 Dec 2014 15:07:58 +0300
Subject: [PATCH] MediaGroup refactoring.

Make current grouping algorithm clearer.
---
 vlc-android/src/org/videolan/vlc/MediaGroup.java | 40 ++++++++++--------------
 1 file changed, 17 insertions(+), 23 deletions(-)

diff --git a/vlc-android/src/org/videolan/vlc/MediaGroup.java
b/vlc-android/src/org/videolan/vlc/MediaGroup.java
index 8af6dc3..833f6ba 100644
--- a/vlc-android/src/org/videolan/vlc/MediaGroup.java
+++ b/vlc-android/src/org/videolan/vlc/MediaGroup.java
@@ -30,9 +30,8 @@ public class MediaGroup extends Media {

     public final static String TAG = "VLC/MediaGroup";

-    public final static int MIN_GROUP_LENGTH = 5;
+    public final static int MIN_GROUP_LENGTH = 6;

-    private Media mMedia;
     private ArrayList<Media> mMedias;

     public MediaGroup(Media media)
@@ -51,20 +50,20 @@ public class MediaGroup extends Media {
                 media.getArtworkURL(),
                 media.getAudioTrack(),
                 media.getSpuTrack());
-        mMedia = media;
         mMedias = new ArrayList<Media>();
+        mMedias.add(media);
     }
-
+
     public void add(Media media) {
         mMedias.add(media);
     }

     public Media getMedia() {
-        return size() == 0 ? mMedia : this;
+        return mMedias.size() == 1 ? mMedias.get(0) : this;
     }

     public Media getFirstMedia() {
-        return size() == 0 ? mMedia : mMedias.get(0);
+        return mMedias.get(0);
     }

     public int size() {
@@ -72,11 +71,6 @@ public class MediaGroup extends Media {
     }

     public void merge(Media media, String title) {
-        if (size() == 0) {
-            if (mMedia != null)
-                mMedias.add(mMedia);
-            mMedia = null;
-        }
         mMedias.add(media);
         this.mTitle = title;
     }
@@ -91,8 +85,8 @@ public class MediaGroup extends Media {
     private static void insertInto(ArrayList<MediaGroup> groups, Media media)
     {
         for (MediaGroup mediaGroup : groups) {
-            CharSequence group = mediaGroup.getTitle();
-            CharSequence item = media.getTitle();
+            String group = mediaGroup.getTitle();
+            String item = media.getTitle();

             // find common prefix
             int commonLength = 0;
@@ -100,16 +94,16 @@ public class MediaGroup extends Media {
             while (commonLength < minLength &&
group.charAt(commonLength) == item.charAt(commonLength))
                 ++commonLength;

-            // same prefix name, just add
-            if (commonLength == group.length() && mediaGroup.size() > 0)
-                mediaGroup.add(media);
-            // not the same prefix, but close : merge
-            else if (commonLength > 0 && (commonLength <
group.length() || mediaGroup.size() == 0) && commonLength >
MIN_GROUP_LENGTH)
-                mediaGroup.merge(media, group.subSequence(0,
commonLength).toString());
-            else
-                continue;
-
-            return;
+            if (commonLength >= MIN_GROUP_LENGTH) {
+                if (commonLength == group.length()) {
+                    // same prefix name, just add
+                    mediaGroup.add(media);
+                } else {
+                    // not the same prefix, but close : merge
+                    mediaGroup.merge(media, group.substring(0, commonLength));
+                }
+                return;
+            }
         }

         // does not match any group, so add one
-- 
2.2.0.rc0.207.ga3a616c


More information about the Android mailing list