Skip to content

Commit

Permalink
sus
Browse files Browse the repository at this point in the history
  • Loading branch information
reocat committed Feb 5, 2025
1 parent 25a25ca commit 0357c7b
Showing 1 changed file with 35 additions and 19 deletions.
54 changes: 35 additions & 19 deletions app/src/main/java/com/dd3boh/outertune/playback/MusicService.kt
Original file line number Diff line number Diff line change
Expand Up @@ -5,13 +5,12 @@ import android.app.PendingIntent
import android.content.ComponentName
import android.content.Context
import android.content.Intent
import android.content.pm.ServiceInfo.FOREGROUND_SERVICE_TYPE_MEDIA_PLAYBACK
import android.database.SQLException
import android.graphics.Bitmap
import android.media.audiofx.AudioEffect
import android.net.ConnectivityManager
import android.net.Uri
import android.os.Binder
import android.os.Build
import android.widget.Toast
import androidx.core.app.NotificationChannelCompat
import androidx.core.app.NotificationManagerCompat
Expand Down Expand Up @@ -68,9 +67,9 @@ import com.dd3boh.outertune.constants.EnableDiscordRPCKey
import com.dd3boh.outertune.constants.KeepAliveKey
import com.dd3boh.outertune.constants.LastPosKey
import com.dd3boh.outertune.constants.MediaSessionConstants.CommandToggleLike
import com.dd3boh.outertune.constants.MediaSessionConstants.CommandToggleStartRadio
import com.dd3boh.outertune.constants.MediaSessionConstants.CommandToggleRepeatMode
import com.dd3boh.outertune.constants.MediaSessionConstants.CommandToggleShuffle
import com.dd3boh.outertune.constants.MediaSessionConstants.CommandToggleStartRadio
import com.dd3boh.outertune.constants.PauseListenHistoryKey
import com.dd3boh.outertune.constants.PersistentQueueKey
import com.dd3boh.outertune.constants.PlayerVolumeKey
Expand Down Expand Up @@ -141,7 +140,6 @@ import java.net.SocketTimeoutException
import java.net.UnknownHostException
import java.time.LocalDateTime
import javax.inject.Inject
import kotlin.collections.map
import kotlin.math.min
import kotlin.math.pow

Expand Down Expand Up @@ -452,25 +450,43 @@ class MusicService : MediaLibraryService(),
playerNotificationManager = PlayerNotificationManager.Builder(this, NOTIFICATION_ID, CHANNEL_ID)
.setNotificationListener(object : PlayerNotificationManager.NotificationListener {
override fun onNotificationPosted(notificationId: Int, notification: Notification, ongoing: Boolean) {
fun startFg() {
if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.Q) {
startForeground(notificationId, notification, FOREGROUND_SERVICE_TYPE_MEDIA_PLAYBACK)
} else {
startForeground(notificationId, notification)
}
}

// FG keep alive
if (dataStore.get(KeepAliveKey, false)) {
startFg()
startForeground(notificationId, notification)
} else {
// mimic media3 default behaviour
if (player.isPlaying) {
startFg()
} else {
stopForeground(notificationId)
}
stopForeground(notificationId)
}
}
})
.setMediaDescriptionAdapter(object : PlayerNotificationManager.MediaDescriptionAdapter {
override fun createCurrentContentIntent(player: Player): PendingIntent? {
val intent = Intent(this@MusicService, MainActivity::class.java).apply {
flags = Intent.FLAG_ACTIVITY_SINGLE_TOP
}
return PendingIntent.getActivity(
this@MusicService,
0,
intent,
PendingIntent.FLAG_UPDATE_CURRENT or PendingIntent.FLAG_IMMUTABLE
)
}

override fun getCurrentContentText(player: Player): CharSequence? {
return player.currentMediaItem?.mediaMetadata?.artist
}

override fun getCurrentContentTitle(player: Player): CharSequence {
return player.currentMediaItem?.mediaMetadata?.title ?: ""
}

override fun getCurrentLargeIcon(
player: Player,
callback: PlayerNotificationManager.BitmapCallback
): Bitmap? {
// If you're already handling artwork through the MediaSession,
// you can return null here since the PlayerNotificationManager
// will use the MediaSession's artwork
return null
}
})
.build()
Expand Down

0 comments on commit 0357c7b

Please sign in to comment.