mirror of
https://github.com/vitorpamplona/amethyst.git
synced 2025-09-19 05:10:47 +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
|
package com.vitorpamplona.amethyst.service.playback.playerPool
|
||||||
|
|
||||||
|
import android.app.PendingIntent
|
||||||
import android.content.Context
|
import android.content.Context
|
||||||
|
import android.content.Intent
|
||||||
import android.util.Log
|
import android.util.Log
|
||||||
import android.util.LruCache
|
import android.util.LruCache
|
||||||
import androidx.annotation.OptIn
|
import androidx.annotation.OptIn
|
||||||
|
import androidx.core.net.toUri
|
||||||
import androidx.media3.common.MediaItem
|
import androidx.media3.common.MediaItem
|
||||||
import androidx.media3.common.Player
|
import androidx.media3.common.Player
|
||||||
import androidx.media3.common.util.UnstableApi
|
import androidx.media3.common.util.UnstableApi
|
||||||
@@ -56,9 +59,10 @@ class SessionListener(
|
|||||||
class MediaSessionPool(
|
class MediaSessionPool(
|
||||||
val exoPlayerPool: ExoPlayerPool,
|
val exoPlayerPool: ExoPlayerPool,
|
||||||
val okHttpClient: OkHttpClient,
|
val okHttpClient: OkHttpClient,
|
||||||
|
val appContext: Context,
|
||||||
val reset: (MediaSession, Boolean) -> Unit,
|
val reset: (MediaSession, Boolean) -> Unit,
|
||||||
) {
|
) {
|
||||||
val globalCallback = MediaSessionCallback(this)
|
val globalCallback = MediaSessionCallback(this, appContext)
|
||||||
var lastCleanup = TimeUtils.now()
|
var lastCleanup = TimeUtils.now()
|
||||||
|
|
||||||
// protects from LruCache killing playing sessions
|
// protects from LruCache killing playing sessions
|
||||||
@@ -185,6 +189,7 @@ class MediaSessionPool(
|
|||||||
|
|
||||||
class MediaSessionCallback(
|
class MediaSessionCallback(
|
||||||
val pool: MediaSessionPool,
|
val pool: MediaSessionPool,
|
||||||
|
val appContext: Context,
|
||||||
) : MediaSession.Callback {
|
) : MediaSession.Callback {
|
||||||
@OptIn(UnstableApi::class)
|
@OptIn(UnstableApi::class)
|
||||||
override fun onAddMediaItems(
|
override fun onAddMediaItems(
|
||||||
@@ -196,7 +201,14 @@ class MediaSessionPool(
|
|||||||
|
|
||||||
// set up return call when clicking on the Notification bar
|
// set up return call when clicking on the Notification bar
|
||||||
mediaItems.firstOrNull()?.mediaMetadata?.extras?.getString("callbackUri")?.let {
|
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)
|
return Futures.immediateFuture(mediaItems)
|
||||||
|
@@ -49,6 +49,7 @@ class PlaybackService : MediaSessionService() {
|
|||||||
poolSize = SimultaneousPlaybackCalculator.max(applicationContext),
|
poolSize = SimultaneousPlaybackCalculator.max(applicationContext),
|
||||||
),
|
),
|
||||||
okHttpClient = okHttp,
|
okHttpClient = okHttp,
|
||||||
|
appContext = applicationContext,
|
||||||
reset = { session, keepPlaying ->
|
reset = { session, keepPlaying ->
|
||||||
(session.player as ExoPlayer).apply {
|
(session.player as ExoPlayer).apply {
|
||||||
repeatMode = if (keepPlaying) Player.REPEAT_MODE_ONE else Player.REPEAT_MODE_OFF
|
repeatMode = if (keepPlaying) Player.REPEAT_MODE_ONE else Player.REPEAT_MODE_OFF
|
||||||
|
@@ -20,8 +20,6 @@
|
|||||||
*/
|
*/
|
||||||
package com.vitorpamplona.amethyst.ui
|
package com.vitorpamplona.amethyst.ui
|
||||||
|
|
||||||
import android.app.PendingIntent
|
|
||||||
import android.content.Intent
|
|
||||||
import android.os.Build
|
import android.os.Build
|
||||||
import android.os.Bundle
|
import android.os.Bundle
|
||||||
import android.util.Log
|
import android.util.Log
|
||||||
@@ -30,7 +28,6 @@ import androidx.activity.enableEdgeToEdge
|
|||||||
import androidx.annotation.RequiresApi
|
import androidx.annotation.RequiresApi
|
||||||
import androidx.appcompat.app.AppCompatActivity
|
import androidx.appcompat.app.AppCompatActivity
|
||||||
import androidx.compose.runtime.LaunchedEffect
|
import androidx.compose.runtime.LaunchedEffect
|
||||||
import androidx.core.net.toUri
|
|
||||||
import androidx.lifecycle.viewmodel.compose.viewModel
|
import androidx.lifecycle.viewmodel.compose.viewModel
|
||||||
import com.vitorpamplona.amethyst.Amethyst
|
import com.vitorpamplona.amethyst.Amethyst
|
||||||
import com.vitorpamplona.amethyst.debugState
|
import com.vitorpamplona.amethyst.debugState
|
||||||
@@ -130,16 +127,6 @@ class MainActivity : AppCompatActivity() {
|
|||||||
|
|
||||||
super.onDestroy()
|
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(
|
fun uriToRoute(
|
||||||
|
Reference in New Issue
Block a user