diff --git a/src/musikdroid/app/src/main/java/io/casey/musikcube/remote/service/playback/impl/streaming/StreamingPlaybackService.kt b/src/musikdroid/app/src/main/java/io/casey/musikcube/remote/service/playback/impl/streaming/StreamingPlaybackService.kt index 3e2acbbe5..eeff8ec25 100644 --- a/src/musikdroid/app/src/main/java/io/casey/musikcube/remote/service/playback/impl/streaming/StreamingPlaybackService.kt +++ b/src/musikdroid/app/src/main/java/io/casey/musikcube/remote/service/playback/impl/streaming/StreamingPlaybackService.kt @@ -177,6 +177,11 @@ class StreamingPlaybackService(context: Context) : IPlaybackService { override fun playFrom(service: IPlaybackService) { /* we only support switching from a play queue context! */ if (service.queryContext?.type == Messages.Request.QueryPlayQueueTracks) { + val index = service.queuePosition + val offsetMs = (service.currentTime * 1000).toInt() + val context = QueryContext(Messages.Request.PlaySnapshotTracks) + val type = PlayQueueType.Snapshot + val dummyListener: (() -> Unit) = { } connect(dummyListener) service.queryContext?.let { _ -> @@ -185,10 +190,6 @@ class StreamingPlaybackService(context: Context) : IPlaybackService { disconnect(dummyListener) resetPlayContextAndQueryFactory() - val index = service.queuePosition - val offsetMs = (service.currentTime * 1000).toInt() - val context = QueryContext(Messages.Request.PlaySnapshotTracks) - val type = PlayQueueType.Snapshot snapshotQueryFactory = object: ITrackListQueryFactory { override fun count(): Observable? = @@ -200,7 +201,6 @@ class StreamingPlaybackService(context: Context) : IPlaybackService { override fun offline(): Boolean = false } - service.pause() loadQueueAndPlay(context, index, offsetMs) }, onError = { diff --git a/src/musikdroid/app/src/main/java/io/casey/musikcube/remote/ui/home/activity/MainActivity.kt b/src/musikdroid/app/src/main/java/io/casey/musikcube/remote/ui/home/activity/MainActivity.kt index f5f4ab867..8f6e61bd6 100644 --- a/src/musikdroid/app/src/main/java/io/casey/musikcube/remote/ui/home/activity/MainActivity.kt +++ b/src/musikdroid/app/src/main/java/io/casey/musikcube/remote/ui/home/activity/MainActivity.kt @@ -42,6 +42,8 @@ import io.casey.musikcube.remote.ui.shared.util.UpdateCheck import io.casey.musikcube.remote.ui.tracks.activity.TrackListActivity class MainActivity : BaseActivity() { + private enum class SwitchMode { Seamless, Copy, Normal } + private val handler = Handler() private var updateCheck: UpdateCheck = UpdateCheck() private var seekbarValue = -1 @@ -118,22 +120,32 @@ class MainActivity : BaseActivity() { val remoteToggle = menu.findItem(R.id.action_remote_toggle) - remoteToggle.setIcon( + remoteToggle.actionView?.findViewById(R.id.icon)?.setImageResource( if (streaming) R.drawable.ic_toolbar_streaming else R.drawable.ic_toolbar_remote) + remoteToggle.actionView?.setOnClickListener { + togglePlaybackService() + } + + remoteToggle.actionView?.setOnLongClickListener { + showPlaybackTogglePopup() + true + } + return super.onPrepareOptionsMenu(menu) } - fun showPlaybackTogglePopup() { + private fun showPlaybackTogglePopup() { val toolbarButton = findViewById(R.id.action_remote_toggle) val popup = PopupMenu(this, toolbarButton) popup.inflate(R.menu.playback_toggle_menu) popup.setOnMenuItemClickListener { it -> when(it.itemId) { - R.id.menu_switch_seamless -> togglePlaybackService(true) - R.id.menu_switch_normal -> togglePlaybackService() + R.id.menu_switch_seamless -> togglePlaybackService(SwitchMode.Seamless) + R.id.menu_switch_copy -> togglePlaybackService(SwitchMode.Copy) + R.id.menu_switch_normal -> togglePlaybackService(SwitchMode.Normal) else -> { } } true @@ -227,7 +239,7 @@ class MainActivity : BaseActivity() { Prefs.Key.STREAMING_PLAYBACK, Prefs.Default.STREAMING_PLAYBACK) - private fun togglePlaybackService(transfer: Boolean = false) { + private fun togglePlaybackService(mode: SwitchMode = SwitchMode.Normal) { val isStreaming = isStreamingSelected prefs.edit().putBoolean(Prefs.Key.STREAMING_PLAYBACK, !isStreaming)?.apply() @@ -238,19 +250,27 @@ class MainActivity : BaseActivity() { showSnackbar(mainLayout, messageId) - if (transfer) { + if (mode == SwitchMode.Normal) { + if (isStreaming) { + playback.service.pause() + } + } + else { val streaming = PlaybackServiceFactory.streaming(this) val remote = PlaybackServiceFactory.remote(this) if (!isStreaming) { streaming.playFrom(remote) - } else { - remote.playFrom(streaming) + if (mode == SwitchMode.Seamless) { + remote.pause() + } + } + else { + remote.playFrom(streaming) + if (mode == SwitchMode.Seamless) { + streaming.pause() + } } - } - - if (isStreaming) { - playback.service.stop() } playback.reload() diff --git a/src/musikdroid/app/src/main/res/layout/remote_toggle.xml b/src/musikdroid/app/src/main/res/layout/remote_toggle.xml new file mode 100644 index 000000000..5b6bf480a --- /dev/null +++ b/src/musikdroid/app/src/main/res/layout/remote_toggle.xml @@ -0,0 +1,16 @@ + + + + + \ No newline at end of file diff --git a/src/musikdroid/app/src/main/res/menu/main_menu.xml b/src/musikdroid/app/src/main/res/menu/main_menu.xml index 3d859e80d..b5958e87d 100644 --- a/src/musikdroid/app/src/main/res/menu/main_menu.xml +++ b/src/musikdroid/app/src/main/res/menu/main_menu.xml @@ -6,8 +6,8 @@ + app:actionLayout="@layout/remote_toggle" + android:title="@string/menu_remote_toggle" /> + + diff --git a/src/musikdroid/app/src/main/res/values/strings.xml b/src/musikdroid/app/src/main/res/values/strings.xml index f074476b7..267692694 100644 --- a/src/musikdroid/app/src/main/res/values/strings.xml +++ b/src/musikdroid/app/src/main/res/values/strings.xml @@ -188,6 +188,7 @@ genre publisher year - seamless switch + transfer session + copy session new session