Skip to content

Commit 16ddc71

Browse files
committed
mq: Address review comments
1 parent 826f395 commit 16ddc71

File tree

6 files changed

+31
-53
lines changed

6 files changed

+31
-53
lines changed

app/src/main/java/org/akanework/gramophone/logic/GramophoneExtensions.kt

Lines changed: 16 additions & 28 deletions
Original file line numberDiff line numberDiff line change
@@ -353,13 +353,9 @@ fun MediaController.getInactiveQueues(): List<MultiQueueObject> =
353353
SessionCommand(SERVICE_QB_GET_INACTIVE, Bundle.EMPTY),
354354
Bundle.EMPTY
355355
).get().extras.run {
356-
if (containsKey("allQueues")) {
357-
val binder = getBinder("allQueues")!!
358-
BundleListRetriever.getList(binder).map {
359-
MultiQueueObject.fromBundle(it)
360-
}
361-
} else {
362-
throw IllegalArgumentException("expected allQueues to be set")
356+
val binder = getBinder("allQueues")!!
357+
BundleListRetriever.getList(binder).map {
358+
MultiQueueObject.fromBundle(it)
363359
}
364360
}
365361

@@ -369,13 +365,9 @@ fun MediaController.getQueue(index: Int = C.INDEX_UNSET): MultiQueueObject? =
369365
customExtras.putInt("index", index)
370366
}, Bundle.EMPTY
371367
).get().extras.run {
372-
if (containsKey("allQueues")) {
373-
val binder = getBinder("allQueues")!!
374-
BundleListRetriever.getList(binder).map {
375-
MultiQueueObject.fromBundle(it)
376-
}
377-
} else {
378-
throw IllegalArgumentException("expected allQueues to be set")
368+
val binder = getBinder("allQueues")!!
369+
BundleListRetriever.getList(binder).map {
370+
MultiQueueObject.fromBundle(it)
379371
}.firstOrNull()
380372
}
381373

@@ -416,21 +408,17 @@ fun MediaController.getQueueForUi(index: Int = C.INDEX_UNSET): Pair<MutableList<
416408
customExtras.putInt("index", index)
417409
}, Bundle.EMPTY
418410
).get().extras.run {
419-
if (containsKey("allQueues")) {
420-
val binder = getBinder("allQueues")!!
421-
BundleListRetriever.getList(binder).map {
422-
val mq = MultiQueueObject.fromBundle(it)
423-
val items = mq.queue
424-
val indexes: MutableList<Int> = if (mq.shuffleOrder == null) {
425-
(0 until mq.getSize()).toMutableList()
426-
} else {
427-
shuffledIndices(mq.shuffleOrder!!)
428-
}
429-
430-
Pair(indexes, items)
411+
val binder = getBinder("allQueues")!!
412+
BundleListRetriever.getList(binder).map {
413+
val mq = MultiQueueObject.fromBundle(it)
414+
val items = mq.queue
415+
val indexes: MutableList<Int> = if (mq.shuffleOrder == null) {
416+
(0 until mq.getSize()).toMutableList()
417+
} else {
418+
shuffledIndices(mq.shuffleOrder!!)
431419
}
432-
} else {
433-
throw IllegalArgumentException("expected allQueues to be set")
420+
421+
Pair(indexes, items)
434422
}.firstOrNull()
435423
}
436424
}

app/src/main/java/org/akanework/gramophone/logic/GramophonePlaybackService.kt

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -168,7 +168,7 @@ class GramophonePlaybackService : MediaLibraryService(), MediaSessionService.Lis
168168
val endedWorkaroundPlayer
169169
get() = mediaSession?.player as EndedWorkaroundPlayer?
170170
private var controller: MediaBrowser? = null
171-
val qb: QueueBoard = QueueBoard(this)
171+
lateinit var qb: QueueBoard
172172
private val sendLyrics = Runnable { scheduleSendingLyrics(false) }
173173
var lyrics: SemanticLyrics? = null
174174
private set
@@ -268,6 +268,7 @@ class GramophonePlaybackService : MediaLibraryService(), MediaSessionService.Lis
268268
override fun onCreate() {
269269
Log.i(TAG, "+onCreate()")
270270
super.onCreate()
271+
qb = QueueBoard(this)
271272
instanceForWidgetAndLyricsOnly = this
272273
internalPlaybackThread.start()
273274
playbackHandler = Handler(internalPlaybackThread.looper)

app/src/main/java/org/akanework/gramophone/logic/QueueBoard.kt

Lines changed: 11 additions & 18 deletions
Original file line numberDiff line numberDiff line change
@@ -91,7 +91,7 @@ class QueueBoard(
9191
val new = masterQueues[index]
9292
masterQueues.remove(new)
9393
masterQueues.add(new)
94-
setCurrQueue(new, false, shouldResume)
94+
setCurrQueue(new, true, shouldResume)
9595
}
9696

9797
fun pinQueue(index: Int) {
@@ -236,17 +236,18 @@ class QueueBoard(
236236
/**
237237
* Deletes a queue.
238238
*
239-
* When deleting the active queue,
239+
* When deleting the active queue, the last inactive queue is loaded. When the active queue is
240+
* the only queue, playback is stopped.
240241
*
241-
* @param mq
242+
* @param index
242243
*/
243244
fun deleteQueue(index: Int): Int {
244245
if (QUEUE_DEBUG)
245246
Log.d(TAG, "DELETING QUEUE AT INDEX: $index")
246247
if (index == masterQueues.lastIndex) {
247248
masterQueues.removeAt(index)
248249
if (index <= 0) {
249-
player.pauseAllPlayersAndStopSelf() // TODO: correct way to stop playback
250+
player.endedWorkaroundPlayer?.removeMediaItems(0, Int.MAX_VALUE)
250251
} else {
251252
commitQueue(index - 1, false)
252253
}
@@ -353,8 +354,6 @@ class QueueBoard(
353354
return null
354355
}
355356

356-
// I have no idea why this value gets reset to 0 by the end... but ig this works
357-
val startPositionMs = if (shouldResume) mq.startPositionMs else C.TIME_UNSET
358357
val startIndex = mq.startIndex
359358

360359
val mediaItems: MutableList<MediaItem> = mq.queue
@@ -373,20 +372,11 @@ class QueueBoard(
373372
val playerIndex = plr.currentMediaItemIndex
374373

375374
if (startIndex == 0) {
376-
val playerItemCount = plr.mediaItemCount
377-
// player.player.replaceMediaItems seems to stop playback so we
378-
// remove all songs except the currently playing one and then add the list of new items
379-
if (playerIndex < playerItemCount - 1) {
380-
plr.removeMediaItems(
381-
playerIndex + 1,
382-
playerItemCount
383-
)
384-
}
375+
// remove all songs before the currently playing one and then replace all the items after
385376
if (playerIndex > 0) {
386377
plr.removeMediaItems(0, playerIndex)
387378
}
388-
// add all songs except the first one since it is already present and playing
389-
plr.addMediaItems(mediaItems.drop(1))
379+
plr.replaceMediaItems(1, Int.MAX_VALUE, mediaItems.drop(1))
390380
} else {
391381
// replace items up to current playing, then replace items after current
392382
plr.replaceMediaItems(
@@ -400,7 +390,10 @@ class QueueBoard(
400390
}
401391
} else {
402392
Log.d(TAG, "Seamless is not supported. Loading songs in directly")
403-
plr.setMediaItems(mediaItems, startIndex, startPositionMs)
393+
plr.setMediaItems(
394+
mediaItems, startIndex,
395+
if (shouldResume) mq.startPositionMs else C.TIME_UNSET
396+
)
404397
}
405398

406399
if (plr.shuffleModeEnabled != mq.shuffleModeEnabled) {

app/src/main/java/org/akanework/gramophone/ui/components/PlaylistQueueSheet.kt

Lines changed: 0 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -171,10 +171,6 @@ class PlaylistQueueSheet(
171171
val durationView: Chronometer = view.findViewById(R.id.duration)
172172

173173
val current = (instance?.currentMediaItemIndex ?: 0)
174-
if (current > playlistAdapter.playlist.second.size) {
175-
// hax to workaround ui loading itself 4 times
176-
return@AndroidView
177-
}
178174
val elapsedCurrentMs = (instance?.currentPosition ?: 0)
179175
durationView.format =
180176
context.getString(

app/src/main/res/values/strings.xml

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -503,5 +503,5 @@
503503
<string name="actions_query_shuffle_specific">shuffle $item_name</string>
504504
<string name="please_allow_to_delete">Press allow to delete files</string>
505505
<string name="choose_sd">Choose %s</string>
506-
<string name="settings_mq_preview">Enable multiple queues preview</string>
506+
<string name="settings_mq_enabled">Multiple queues</string>
507507
</resources>

app/src/main/res/xml/settings_experimental.xml

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -77,7 +77,7 @@
7777
android:defaultValue="false"
7878
android:key="mq_preview"
7979
android:layout="@layout/preference_switch"
80-
android:title="@string/settings_mq_preview"
80+
android:title="@string/settings_mq_enabled"
8181
android:widgetLayout="@layout/preference_switch_widget"
8282
app:iconSpaceReserved="false" />
8383

0 commit comments

Comments
 (0)