mirror of
https://github.com/vitorpamplona/amethyst.git
synced 2025-09-18 22:51:46 +02:00
Passes appcontext down on MediaSession Pool to avoid the direct dependency on Amethyst
This commit is contained in:
@@ -20,10 +20,13 @@
|
||||
*/
|
||||
package com.vitorpamplona.amethyst.service.playback.playerPool
|
||||
|
||||
import android.app.PendingIntent
|
||||
import android.content.Context
|
||||
import android.content.Intent
|
||||
import android.util.Log
|
||||
import android.util.LruCache
|
||||
import androidx.annotation.OptIn
|
||||
import androidx.core.net.toUri
|
||||
import androidx.media3.common.MediaItem
|
||||
import androidx.media3.common.Player
|
||||
import androidx.media3.common.util.UnstableApi
|
||||
@@ -56,9 +59,10 @@ class SessionListener(
|
||||
class MediaSessionPool(
|
||||
val exoPlayerPool: ExoPlayerPool,
|
||||
val okHttpClient: OkHttpClient,
|
||||
val appContext: Context,
|
||||
val reset: (MediaSession, Boolean) -> Unit,
|
||||
) {
|
||||
val globalCallback = MediaSessionCallback(this)
|
||||
val globalCallback = MediaSessionCallback(this, appContext)
|
||||
var lastCleanup = TimeUtils.now()
|
||||
|
||||
// protects from LruCache killing playing sessions
|
||||
@@ -185,6 +189,7 @@ class MediaSessionPool(
|
||||
|
||||
class MediaSessionCallback(
|
||||
val pool: MediaSessionPool,
|
||||
val appContext: Context,
|
||||
) : MediaSession.Callback {
|
||||
@OptIn(UnstableApi::class)
|
||||
override fun onAddMediaItems(
|
||||
@@ -196,7 +201,14 @@ class MediaSessionPool(
|
||||
|
||||
// set up return call when clicking on the Notification bar
|
||||
mediaItems.firstOrNull()?.mediaMetadata?.extras?.getString("callbackUri")?.let {
|
||||
mediaSession.setSessionActivity(MainActivity.createIntent(it))
|
||||
mediaSession.setSessionActivity(
|
||||
PendingIntent.getActivity(
|
||||
appContext,
|
||||
0,
|
||||
Intent(Intent.ACTION_VIEW, it.toUri(), appContext, MainActivity::class.java),
|
||||
PendingIntent.FLAG_IMMUTABLE or PendingIntent.FLAG_UPDATE_CURRENT,
|
||||
),
|
||||
)
|
||||
}
|
||||
|
||||
return Futures.immediateFuture(mediaItems)
|
||||
|
@@ -49,6 +49,7 @@ class PlaybackService : MediaSessionService() {
|
||||
poolSize = SimultaneousPlaybackCalculator.max(applicationContext),
|
||||
),
|
||||
okHttpClient = okHttp,
|
||||
appContext = applicationContext,
|
||||
reset = { session, keepPlaying ->
|
||||
(session.player as ExoPlayer).apply {
|
||||
repeatMode = if (keepPlaying) Player.REPEAT_MODE_ONE else Player.REPEAT_MODE_OFF
|
||||
|
@@ -20,8 +20,6 @@
|
||||
*/
|
||||
package com.vitorpamplona.amethyst.ui
|
||||
|
||||
import android.app.PendingIntent
|
||||
import android.content.Intent
|
||||
import android.os.Build
|
||||
import android.os.Bundle
|
||||
import android.util.Log
|
||||
@@ -30,7 +28,6 @@ import androidx.activity.enableEdgeToEdge
|
||||
import androidx.annotation.RequiresApi
|
||||
import androidx.appcompat.app.AppCompatActivity
|
||||
import androidx.compose.runtime.LaunchedEffect
|
||||
import androidx.core.net.toUri
|
||||
import androidx.lifecycle.viewmodel.compose.viewModel
|
||||
import com.vitorpamplona.amethyst.Amethyst
|
||||
import com.vitorpamplona.amethyst.debugState
|
||||
@@ -130,16 +127,6 @@ class MainActivity : AppCompatActivity() {
|
||||
|
||||
super.onDestroy()
|
||||
}
|
||||
|
||||
companion object {
|
||||
fun createIntent(callbackUri: String): PendingIntent =
|
||||
PendingIntent.getActivity(
|
||||
Amethyst.instance,
|
||||
0,
|
||||
Intent(Intent.ACTION_VIEW, callbackUri.toUri(), Amethyst.instance, MainActivity::class.java),
|
||||
PendingIntent.FLAG_IMMUTABLE or PendingIntent.FLAG_UPDATE_CURRENT,
|
||||
)
|
||||
}
|
||||
}
|
||||
|
||||
fun uriToRoute(
|
||||
|
Reference in New Issue
Block a user