summaryrefslogtreecommitdiffstats
diff options
context:
space:
mode:
authorSteve Kondik <steve@cyngn.com>2016-10-09 12:49:30 (GMT)
committerGerrit Code Review <gerrit@cyanogenmod.org>2016-10-09 12:49:31 (GMT)
commitf1daeeb7b9c7efe848d6859d3d17930e362c9048 (patch)
tree7715ee5071aaa0020cbf3f0f1784c75a9a0b7361
parentbd9e6d218e7c82d5967ee8b6200ec352b616de59 (diff)
parent488ce053fd78ca3bd1e856db11e6e169cd9f11e1 (diff)
downloadandroid_packages_apps_Eleven-f1daeeb7b9c7efe848d6859d3d17930e362c9048.zip
android_packages_apps_Eleven-f1daeeb7b9c7efe848d6859d3d17930e362c9048.tar.gz
android_packages_apps_Eleven-f1daeeb7b9c7efe848d6859d3d17930e362c9048.tar.bz2
Merge "eleven: Add support for the MediaSession queue" into cm-13.0
-rw-r--r--src/com/cyanogenmod/eleven/MusicPlaybackService.java104
1 files changed, 103 insertions, 1 deletions
diff --git a/src/com/cyanogenmod/eleven/MusicPlaybackService.java b/src/com/cyanogenmod/eleven/MusicPlaybackService.java
index 0e95008..81cd74b 100644
--- a/src/com/cyanogenmod/eleven/MusicPlaybackService.java
+++ b/src/com/cyanogenmod/eleven/MusicPlaybackService.java
@@ -28,8 +28,8 @@ import android.content.ContentResolver;
import android.content.Context;
import android.content.Intent;
import android.content.IntentFilter;
-import android.content.pm.PackageManager;
import android.content.SharedPreferences;
+import android.content.pm.PackageManager;
import android.database.ContentObserver;
import android.database.Cursor;
import android.database.MatrixCursor;
@@ -37,12 +37,14 @@ import android.graphics.Bitmap;
import android.hardware.SensorManager;
import android.media.AudioManager;
import android.media.AudioManager.OnAudioFocusChangeListener;
+import android.media.MediaDescription;
import android.media.MediaMetadata;
import android.media.MediaPlayer;
import android.media.audiofx.AudioEffect;
import android.media.session.MediaSession;
import android.media.session.PlaybackState;
import android.net.Uri;
+import android.os.AsyncTask;
import android.os.Handler;
import android.os.HandlerThread;
import android.os.IBinder;
@@ -78,7 +80,9 @@ import java.io.File;
import java.io.IOException;
import java.lang.ref.WeakReference;
import java.util.ArrayList;
+import java.util.Arrays;
import java.util.LinkedList;
+import java.util.List;
import java.util.ListIterator;
import java.util.Random;
import java.util.TreeSet;
@@ -502,6 +506,8 @@ public class MusicPlaybackService extends Service {
private String mCachedKey;
private BitmapWithColors[] mCachedBitmapWithColors = new BitmapWithColors[2];
+ private QueueUpdateTask mQueueUpdateTask;
+
/**
* Image cache
*/
@@ -733,6 +739,10 @@ public class MusicPlaybackService extends Service {
releaseServiceUiAndStop();
}
@Override
+ public void onSkipToQueueItem(long id) {
+ setQueueItem(id);
+ }
+ @Override
public boolean onMediaButtonEvent(@NonNull Intent mediaButtonIntent) {
if (Intent.ACTION_MEDIA_BUTTON.equals(mediaButtonIntent.getAction())) {
KeyEvent ke = mediaButtonIntent.getParcelableExtra(Intent.EXTRA_KEY_EVENT);
@@ -1566,6 +1576,7 @@ public class MusicPlaybackService extends Service {
if (what.equals(PLAYSTATE_CHANGED) || what.equals(POSITION_CHANGED)) {
mSession.setPlaybackState(new PlaybackState.Builder()
.setActions(playBackStateActions)
+ .setActiveQueueItemId(getAudioId())
.setState(playState, position(), 1.0f).build());
} else if (what.equals(META_CHANGED) || what.equals(QUEUE_CHANGED)) {
Bitmap albumArt = getAlbumArt(false).getBitmap();
@@ -1592,12 +1603,25 @@ public class MusicPlaybackService extends Service {
mShowAlbumArtOnLockscreen ? albumArt : null)
.build());
+ if (what.equals(QUEUE_CHANGED)) {
+ updateMediaSessionQueue();
+ }
+
mSession.setPlaybackState(new PlaybackState.Builder()
.setActions(playBackStateActions)
+ .setActiveQueueItemId(getAudioId())
.setState(playState, position(), 1.0f).build());
}
}
+ private synchronized void updateMediaSessionQueue() {
+ if (mQueueUpdateTask != null) {
+ mQueueUpdateTask.cancel(true);
+ }
+ mQueueUpdateTask = new QueueUpdateTask(getQueue());
+ mQueueUpdateTask.execute();
+ }
+
private Notification buildNotification() {
final String albumName = getAlbumName();
final String artistName = getArtistName();
@@ -2727,6 +2751,18 @@ 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
*
@@ -3824,4 +3860,70 @@ public class MusicPlaybackService extends Service {
}
+ private class QueueUpdateTask extends AsyncTask<Void, Void, List<MediaSession.QueueItem>> {
+ private long[] mQueue;
+
+ public QueueUpdateTask(long[] queue) {
+ mQueue = queue;
+ }
+
+ @Override
+ protected List<MediaSession.QueueItem> doInBackground(Void... params) {
+ if (mQueue == null || mQueue.length == 0) {
+ return null;
+ }
+
+ final StringBuilder selection = new StringBuilder();
+ selection.append(MediaStore.Audio.Media._ID).append(" IN (");
+ for (int i = 0; i < mQueue.length; i++) {
+ if (i != 0) {
+ selection.append(",");
+ }
+ selection.append(mQueue[i]);
+ }
+ selection.append(")");
+
+ Cursor c = getContentResolver().query(
+ MediaStore.Audio.Media.EXTERNAL_CONTENT_URI,
+ new String[] { AudioColumns._ID, AudioColumns.TITLE, AudioColumns.ARTIST },
+ selection.toString(), null, null);
+ if (c == null) {
+ return null;
+ }
+
+ try {
+ final MediaSession.QueueItem[] items = new MediaSession.QueueItem[mQueue.length];
+ final int idColumnIndex = c.getColumnIndexOrThrow(AudioColumns._ID);
+ final int titleColumnIndex = c.getColumnIndexOrThrow(AudioColumns.TITLE);
+ final int artistColumnIndex = c.getColumnIndexOrThrow(AudioColumns.ARTIST);
+
+ while (c.moveToNext() && !isCancelled()) {
+ final MediaDescription desc = new MediaDescription.Builder()
+ .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;
+ }
+ }
+ items[index] = new MediaSession.QueueItem(desc, id);
+ }
+ return Arrays.asList(items);
+ } finally {
+ c.close();
+ }
+ }
+
+ @Override
+ protected void onPostExecute(List<MediaSession.QueueItem> items) {
+ if (!isCancelled()) {
+ mSession.setQueue(items);
+ }
+ }
+ }
}