Update notification on Rating change

This commit is contained in:
Maxence G 2022-07-03 18:23:22 +02:00
parent 16b3fcad32
commit 37e3ce09c1
No known key found for this signature in database
GPG Key ID: DC1FD9409E3FE284
3 changed files with 32 additions and 5 deletions

View File

@ -34,7 +34,6 @@ import com.google.common.collect.ImmutableList
import com.google.common.util.concurrent.FutureCallback
import com.google.common.util.concurrent.Futures
import com.google.common.util.concurrent.ListenableFuture
import com.google.common.util.concurrent.MoreExecutors
import kotlinx.coroutines.CoroutineScope
import kotlinx.coroutines.Dispatchers
import kotlinx.coroutines.Job
@ -52,6 +51,7 @@ import org.moire.ultrasonic.domain.SearchResult
import org.moire.ultrasonic.domain.Track
import org.moire.ultrasonic.service.MediaPlayerController
import org.moire.ultrasonic.service.MusicServiceFactory
import org.moire.ultrasonic.util.MainThreadExecutor
import org.moire.ultrasonic.util.Settings
import org.moire.ultrasonic.util.Util
import timber.log.Timber
@ -98,7 +98,7 @@ const val SESSION_CUSTOM_SET_RATING = "SESSION_CUSTOM_SET_RATING"
* MediaBrowserService implementation for e.g. Android Auto
*/
@Suppress("TooManyFunctions", "LargeClass", "UnusedPrivateMember")
class AutoMediaBrowserCallback(var player: Player) :
class AutoMediaBrowserCallback(var player: Player, val libraryService: MediaLibraryService) :
MediaLibraryService.MediaLibrarySession.Callback, KoinComponent {
private val mediaPlayerController by inject<MediaPlayerController>()
@ -234,7 +234,8 @@ class AutoMediaBrowserCallback(var player: Player) :
object : FutureCallback<SessionResult> {
override fun onSuccess(result: SessionResult) {
track.starred = !track.starred
// Handle notification reload here
// This needs to be called on the main Thread
libraryService.onUpdateNotification(session)
}
override fun onFailure(t: Throwable) {
@ -245,7 +246,7 @@ class AutoMediaBrowserCallback(var player: Player) :
).show()
}
},
MoreExecutors.directExecutor()
MainThreadExecutor()
)
}
}

View File

@ -111,7 +111,7 @@ class PlaybackService : MediaLibraryService(), KoinComponent {
player.experimentalSetOffloadSchedulingEnabled(true)
// Create browser interface
librarySessionCallback = AutoMediaBrowserCallback(player)
librarySessionCallback = AutoMediaBrowserCallback(player, this)
// This will need to use the AutoCalls
mediaLibrarySession = MediaLibrarySession.Builder(this, player, librarySessionCallback)

View File

@ -0,0 +1,26 @@
/*
* MainThreadExecutor.java
* Copyright (C) 2009-2022 Ultrasonic developers
*
* Distributed under terms of the GNU GPLv3 license.
*/
package org.moire.ultrasonic.util;
import android.os.Handler;
import android.os.Looper;
import java.util.concurrent.Executor;
/*
* Executor for running Futures on the main thread
* See https://stackoverflow.com/questions/52642246/how-to-get-executor-for-main-thread-on-api-level-28
*/
public class MainThreadExecutor implements Executor {
private final Handler handler = new Handler(Looper.getMainLooper());
@Override
public void execute(Runnable r) {
handler.post(r);
}
}