Donate to e Foundation | Murena handsets with /e/OS | Own a part of Murena! Learn more

Unverified Commit 279f24e4 authored by Danny Baumann's avatar Danny Baumann Committed by Adrian DC
Browse files

Fix MediaSession queue handling.

- 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

RM-290

Change-Id: I9390ab8c7a5a20f2a2bb1efad02e460ecf7dc2e5
parent 455aa314
Loading
Loading
Loading
Loading
+16 −24
Original line number Diff line number Diff line
@@ -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) {
@@ -2751,18 +2752,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
     *
@@ -3864,7 +3853,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
@@ -3892,7 +3881,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);
@@ -3902,18 +3891,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();
            }