OSDN Git Service

Fix MediaSession queue handling.
authorDanny Baumann <dannybaumann@web.de>
Mon, 31 Oct 2016 12:29:51 +0000 (13:29 +0100)
committerDanny Baumann <dannybaumann@web.de>
Mon, 31 Oct 2016 12:32:55 +0000 (05:32 -0700)
- Go back to using the queue position instead of the track ID as queue
  item ID, as the track ID can be present multiple times in the queue
  and the queue item ID is supposed to be unique in the whole queue
- Make sure no null items are passed in

Change-Id: I9390ab8c7a5a20f2a2bb1efad02e460ecf7dc2e5

src/com/cyanogenmod/eleven/MusicPlaybackService.java

index 2328f4b..4fbbc63 100644 (file)
@@ -58,6 +58,7 @@ import android.provider.MediaStore.Audio.AlbumColumns;
 import android.provider.MediaStore.Audio.AudioColumns;
 import android.text.TextUtils;
 import android.util.Log;
+import android.util.LongSparseArray;
 import android.view.KeyEvent;
 
 import com.cyanogenmod.eleven.Config.IdType;
@@ -740,7 +741,7 @@ public class MusicPlaybackService extends Service {
             }
             @Override
             public void onSkipToQueueItem(long id) {
-                setQueueItem(id);
+                setQueuePosition((int) id);
             }
             @Override
             public boolean onMediaButtonEvent(@NonNull Intent mediaButtonIntent) {
@@ -2755,18 +2756,6 @@ public class MusicPlaybackService extends Service {
         }
     }
 
-    private void setQueueItem(final long id) {
-        synchronized (this) {
-            final int len = mPlaylist.size();
-            for (int i = 0; i < len; i++) {
-                if (id == mPlaylist.get(i).mId) {
-                    setQueuePosition(i);
-                    break;
-                }
-            }
-        }
-    }
-
     /**
      * Queues a new list for playback
      *
@@ -3868,7 +3857,7 @@ public class MusicPlaybackService extends Service {
         private long[] mQueue;
 
         public QueueUpdateTask(long[] queue) {
-            mQueue = queue;
+            mQueue = queue != null ? Arrays.copyOf(queue, queue.length) : null;
         }
 
         @Override
@@ -3896,7 +3885,7 @@ public class MusicPlaybackService extends Service {
             }
 
             try {
-                final MediaSession.QueueItem[] items = new MediaSession.QueueItem[mQueue.length];
+                LongSparseArray<MediaDescription> descsById = new LongSparseArray<>();
                 final int idColumnIndex = c.getColumnIndexOrThrow(AudioColumns._ID);
                 final int titleColumnIndex = c.getColumnIndexOrThrow(AudioColumns.TITLE);
                 final int artistColumnIndex = c.getColumnIndexOrThrow(AudioColumns.ARTIST);
@@ -3906,18 +3895,21 @@ public class MusicPlaybackService extends Service {
                             .setTitle(c.getString(titleColumnIndex))
                             .setSubtitle(c.getString(artistColumnIndex))
                             .build();
-
                     final long id = c.getLong(idColumnIndex);
-                    int index = 0;
-                    for (int i = 0; i < mQueue.length; i++) {
-                        if (mQueue[i] == id) {
-                            index = i;
-                            break;
-                        }
+                    descsById.put(id, desc);
+                }
+
+                List<MediaSession.QueueItem> items = new ArrayList<>();
+                for (int i = 0; i < mQueue.length; i++) {
+                    MediaDescription desc = descsById.get(mQueue[i]);
+                    if (desc == null) {
+                        // shouldn't happen except in corner cases like
+                        // music being deleted while we were processing
+                        desc = new MediaDescription.Builder().build();
                     }
-                    items[index] = new MediaSession.QueueItem(desc, id);
+                    items.add(new MediaSession.QueueItem(desc, i));
                 }
-                return Arrays.asList(items);
+                return items;
             } finally {
                 c.close();
             }