From 5e330ac45198ff8005e1d6086327511d9d20aa9d Mon Sep 17 00:00:00 2001 From: eddyizm Date: Sun, 23 Nov 2025 09:18:32 -0800 Subject: [PATCH] Revert "refactor MediaService" This reverts commit 7aa325f914e0bf47e4a575545d9f547645188bb2. --- .../tempo/service/MediaService.kt | 575 ++++++++++++++++- .../tempo/service/BaseMediaService.kt | 590 ------------------ .../tempo/ui/fragment/EqualizerFragment.kt | 9 +- .../tempo/util/ReplayGainUtil.java | 25 +- .../tempo/service/MediaService.kt | 457 +++++++++++++- 5 files changed, 1022 insertions(+), 634 deletions(-) delete mode 100644 app/src/main/java/com/cappielloantonio/tempo/service/BaseMediaService.kt diff --git a/app/src/degoogled/java/com/cappielloantonio/tempo/service/MediaService.kt b/app/src/degoogled/java/com/cappielloantonio/tempo/service/MediaService.kt index f1e68f5d..d595a8a3 100644 --- a/app/src/degoogled/java/com/cappielloantonio/tempo/service/MediaService.kt +++ b/app/src/degoogled/java/com/cappielloantonio/tempo/service/MediaService.kt @@ -1,6 +1,579 @@ package com.cappielloantonio.tempo.service +import android.annotation.SuppressLint +import android.app.PendingIntent.FLAG_IMMUTABLE +import android.app.PendingIntent.FLAG_UPDATE_CURRENT +import android.app.TaskStackBuilder +import android.content.Intent +import android.net.ConnectivityManager +import android.net.Network +import android.net.NetworkCapabilities +import android.os.Binder +import android.os.Bundle +import android.os.IBinder +import android.os.Handler +import android.os.Looper +import android.util.Log +import androidx.media3.common.* import androidx.media3.common.util.UnstableApi +import androidx.media3.exoplayer.DefaultLoadControl +import androidx.media3.exoplayer.ExoPlayer +import androidx.media3.exoplayer.source.MediaSource +import androidx.media3.exoplayer.source.ShuffleOrder.DefaultShuffleOrder +import androidx.media3.session.* +import androidx.media3.session.MediaSession.ControllerInfo +import com.cappielloantonio.tempo.R +import com.cappielloantonio.tempo.repository.QueueRepository +import com.cappielloantonio.tempo.ui.activity.MainActivity +import com.cappielloantonio.tempo.util.AssetLinkUtil +import com.cappielloantonio.tempo.util.Constants +import com.cappielloantonio.tempo.util.DownloadUtil +import com.cappielloantonio.tempo.util.DynamicMediaSourceFactory +import com.cappielloantonio.tempo.util.MappingUtil +import com.cappielloantonio.tempo.util.Preferences +import com.cappielloantonio.tempo.util.ReplayGainUtil +import com.cappielloantonio.tempo.widget.WidgetUpdateManager +import com.google.common.collect.ImmutableList +import com.google.common.util.concurrent.Futures +import com.google.common.util.concurrent.ListenableFuture + @UnstableApi -class MediaService : BaseMediaService() +class MediaService : MediaLibraryService() { + private val TAG = "MediaService" + private val librarySessionCallback = CustomMediaLibrarySessionCallback() + + private lateinit var player: ExoPlayer + private lateinit var mediaLibrarySession: MediaLibrarySession + private lateinit var shuffleCommands: List + private lateinit var repeatCommands: List + private lateinit var networkCallback: CustomNetworkCallback + lateinit var equalizerManager: EqualizerManager + + private var customLayout = ImmutableList.of() + private val widgetUpdateHandler = Handler(Looper.getMainLooper()) + private var widgetUpdateScheduled = false + private val widgetUpdateRunnable = object : Runnable { + override fun run() { + if (!player.isPlaying) { + widgetUpdateScheduled = false + return + } + updateWidget() + widgetUpdateHandler.postDelayed(this, WIDGET_UPDATE_INTERVAL_MS) + } + } + + inner class LocalBinder : Binder() { + fun getEqualizerManager(): EqualizerManager { + return this@MediaService.equalizerManager + } + } + + private val binder = LocalBinder() + + companion object { + private const val CUSTOM_COMMAND_TOGGLE_SHUFFLE_MODE_ON = + "android.media3.session.demo.SHUFFLE_ON" + private const val CUSTOM_COMMAND_TOGGLE_SHUFFLE_MODE_OFF = + "android.media3.session.demo.SHUFFLE_OFF" + private const val CUSTOM_COMMAND_TOGGLE_REPEAT_MODE_OFF = + "android.media3.session.demo.REPEAT_OFF" + private const val CUSTOM_COMMAND_TOGGLE_REPEAT_MODE_ONE = + "android.media3.session.demo.REPEAT_ONE" + private const val CUSTOM_COMMAND_TOGGLE_REPEAT_MODE_ALL = + "android.media3.session.demo.REPEAT_ALL" + const val ACTION_BIND_EQUALIZER = "com.cappielloantonio.tempo.service.BIND_EQUALIZER" + const val ACTION_EQUALIZER_UPDATED = "com.cappielloantonio.tempo.service.EQUALIZER_UPDATED" + } + + fun updateMediaItems() { + Log.d(TAG, "update items"); + val n = player.mediaItemCount + val k = player.currentMediaItemIndex + val current = player.currentPosition + val items = (0 .. n-1).map{i -> MappingUtil.mapMediaItem(player.getMediaItemAt(i))} + player.clearMediaItems() + player.setMediaItems(items, k, current) + } + + inner class CustomNetworkCallback : ConnectivityManager.NetworkCallback() { + var wasWifi = false + + init { + val manager = getSystemService(ConnectivityManager::class.java) + val network = manager.activeNetwork + val capabilities = manager.getNetworkCapabilities(network) + if (capabilities != null) + wasWifi = capabilities.hasTransport(NetworkCapabilities.TRANSPORT_WIFI) + } + + override fun onCapabilitiesChanged(network : Network, networkCapabilities : NetworkCapabilities) { + val isWifi = networkCapabilities.hasTransport(NetworkCapabilities.TRANSPORT_WIFI) + if (isWifi != wasWifi) { + wasWifi = isWifi + widgetUpdateHandler.post(Runnable { + updateMediaItems() + }) + } + } + } + + override fun onCreate() { + super.onCreate() + + initializeCustomCommands() + initializePlayer() + initializeMediaLibrarySession() + restorePlayerFromQueue() + initializePlayerListener() + initializeEqualizerManager() + initializeNetworkListener() + + setPlayer(player) + } + + override fun onGetSession(controllerInfo: ControllerInfo): MediaLibrarySession { + return mediaLibrarySession + } + + override fun onDestroy() { + releaseNetworkCallback() + equalizerManager.release() + stopWidgetUpdates() + releasePlayer() + super.onDestroy() + } + + override fun onBind(intent: Intent?): IBinder? { + // Check if the intent is for our custom equalizer binder + if (intent?.action == ACTION_BIND_EQUALIZER) { + return binder + } + // Otherwise, handle it as a normal MediaLibraryService connection + return super.onBind(intent) + } + + private inner class CustomMediaLibrarySessionCallback : MediaLibrarySession.Callback { + + override fun onConnect( + session: MediaSession, + controller: ControllerInfo + ): MediaSession.ConnectionResult { + val connectionResult = super.onConnect(session, controller) + val availableSessionCommands = connectionResult.availableSessionCommands.buildUpon() + + (shuffleCommands + repeatCommands).forEach { commandButton -> + commandButton.sessionCommand?.let { availableSessionCommands.add(it) } + } + + customLayout = buildCustomLayout(session.player) + + return MediaSession.ConnectionResult.AcceptedResultBuilder(session) + .setAvailableSessionCommands(availableSessionCommands.build()) + .setAvailablePlayerCommands(connectionResult.availablePlayerCommands) + .setCustomLayout(customLayout) + .build() + } + + override fun onPostConnect(session: MediaSession, controller: ControllerInfo) { + if (!customLayout.isEmpty() && controller.controllerVersion != 0) { + ignoreFuture(mediaLibrarySession.setCustomLayout(controller, customLayout)) + } + } + + fun buildCustomLayout(player: Player): ImmutableList { + val shuffle = shuffleCommands[if (player.shuffleModeEnabled) 1 else 0] + val repeat = when (player.repeatMode) { + Player.REPEAT_MODE_ONE -> repeatCommands[1] + Player.REPEAT_MODE_ALL -> repeatCommands[2] + else -> repeatCommands[0] + } + return ImmutableList.of(shuffle, repeat) + } + + override fun onCustomCommand( + session: MediaSession, + controller: ControllerInfo, + customCommand: SessionCommand, + args: Bundle + ): ListenableFuture { + when (customCommand.customAction) { + CUSTOM_COMMAND_TOGGLE_SHUFFLE_MODE_ON -> player.shuffleModeEnabled = true + CUSTOM_COMMAND_TOGGLE_SHUFFLE_MODE_OFF -> player.shuffleModeEnabled = false + CUSTOM_COMMAND_TOGGLE_REPEAT_MODE_OFF, + CUSTOM_COMMAND_TOGGLE_REPEAT_MODE_ALL, + CUSTOM_COMMAND_TOGGLE_REPEAT_MODE_ONE -> { + val nextMode = when (player.repeatMode) { + Player.REPEAT_MODE_ONE -> Player.REPEAT_MODE_ALL + Player.REPEAT_MODE_OFF -> Player.REPEAT_MODE_ONE + else -> Player.REPEAT_MODE_OFF + } + player.repeatMode = nextMode + } + } + + customLayout = librarySessionCallback.buildCustomLayout(player) + session.setCustomLayout(customLayout) + + return Futures.immediateFuture(SessionResult(SessionResult.RESULT_SUCCESS)) + } + + override fun onAddMediaItems( + mediaSession: MediaSession, + controller: ControllerInfo, + mediaItems: List + ): ListenableFuture> { + val updatedMediaItems = mediaItems.map { mediaItem -> + val mediaMetadata = mediaItem.mediaMetadata + + val newMetadata = mediaMetadata.buildUpon() + .setArtist( + if (mediaMetadata.artist != null) mediaMetadata.artist + else mediaMetadata.extras?.getString("uri") ?: "" + ) + .build() + + mediaItem.buildUpon() + .setUri(mediaItem.requestMetadata.mediaUri) + .setMediaMetadata(newMetadata) + .setMimeType(MimeTypes.BASE_TYPE_AUDIO) + .build() + } + return Futures.immediateFuture(updatedMediaItems) + } + } + + private fun initializeCustomCommands() { + shuffleCommands = listOf( + getShuffleCommandButton( + SessionCommand(CUSTOM_COMMAND_TOGGLE_SHUFFLE_MODE_ON, Bundle.EMPTY) + ), + getShuffleCommandButton( + SessionCommand(CUSTOM_COMMAND_TOGGLE_SHUFFLE_MODE_OFF, Bundle.EMPTY) + ) + ) + + repeatCommands = listOf( + getRepeatCommandButton( + SessionCommand(CUSTOM_COMMAND_TOGGLE_REPEAT_MODE_OFF, Bundle.EMPTY) + ), + getRepeatCommandButton( + SessionCommand(CUSTOM_COMMAND_TOGGLE_REPEAT_MODE_ONE, Bundle.EMPTY) + ), + getRepeatCommandButton( + SessionCommand(CUSTOM_COMMAND_TOGGLE_REPEAT_MODE_ALL, Bundle.EMPTY) + ) + ) + + customLayout = ImmutableList.of(shuffleCommands[0], repeatCommands[0]) + } + + private fun initializePlayer() { + player = ExoPlayer.Builder(this) + .setRenderersFactory(getRenderersFactory()) + .setMediaSourceFactory(getMediaSourceFactory()) + .setAudioAttributes(AudioAttributes.DEFAULT, true) + .setHandleAudioBecomingNoisy(true) + .setWakeMode(C.WAKE_MODE_NETWORK) + .setLoadControl(initializeLoadControl()) + .build() + + player.shuffleModeEnabled = Preferences.isShuffleModeEnabled() + player.repeatMode = Preferences.getRepeatMode() + } + + private fun initializeEqualizerManager() { + equalizerManager = EqualizerManager() + val audioSessionId = player.audioSessionId + attachEqualizerIfPossible(audioSessionId) + } + + private fun initializeMediaLibrarySession() { + val sessionActivityPendingIntent = + TaskStackBuilder.create(this).run { + addNextIntent(Intent(this@MediaService, MainActivity::class.java)) + getPendingIntent(0, FLAG_IMMUTABLE or FLAG_UPDATE_CURRENT) + } + + mediaLibrarySession = + MediaLibrarySession.Builder(this, player, librarySessionCallback) + .setSessionActivity(sessionActivityPendingIntent) + .build() + + if (!customLayout.isEmpty()) { + mediaLibrarySession.setCustomLayout(customLayout) + } + } + + private fun initializeNetworkListener() { + networkCallback = CustomNetworkCallback() + getSystemService(ConnectivityManager::class.java).registerDefaultNetworkCallback(networkCallback) + updateMediaItems() + } + + private fun restorePlayerFromQueue() { + if (player.mediaItemCount > 0) return + + val queueRepository = QueueRepository() + val storedQueue = queueRepository.media + if (storedQueue.isNullOrEmpty()) return + + val mediaItems = MappingUtil.mapMediaItems(storedQueue) + if (mediaItems.isEmpty()) return + + val lastIndex = try { + queueRepository.lastPlayedMediaIndex + } catch (_: Exception) { + 0 + }.coerceIn(0, mediaItems.size - 1) + + val lastPosition = try { + queueRepository.lastPlayedMediaTimestamp + } catch (_: Exception) { + 0L + }.let { if (it < 0L) 0L else it } + + player.setMediaItems(mediaItems, lastIndex, lastPosition) + player.prepare() + updateWidget() + } + + private fun initializePlayerListener() { + player.addListener(object : Player.Listener { + override fun onMediaItemTransition(mediaItem: MediaItem?, reason: Int) { + if (mediaItem == null) return + + if (reason == Player.MEDIA_ITEM_TRANSITION_REASON_SEEK || reason == Player.MEDIA_ITEM_TRANSITION_REASON_AUTO) { + MediaManager.setLastPlayedTimestamp(mediaItem) + } + updateWidget() + } + + override fun onTracksChanged(tracks: Tracks) { + Log.d(TAG, "onTracksChanged " + player.currentMediaItemIndex); + ReplayGainUtil.setReplayGain(player, tracks) + val currentMediaItem = player.currentMediaItem + if (currentMediaItem != null) { + val item = MappingUtil.mapMediaItem(currentMediaItem) + if (item.mediaMetadata.extras != null) + MediaManager.scrobble(item, false) + + if (player.nextMediaItemIndex == C.INDEX_UNSET) + MediaManager.continuousPlay(player.currentMediaItem) + } + + // https://stackoverflow.com/questions/56937283/exoplayer-shuffle-doesnt-reproduce-all-the-songs + if (MediaManager.justStarted.get()) { + Log.d(TAG, "update shuffle order") + MediaManager.justStarted.set(false) + val shuffledList = IntArray(player.mediaItemCount) { i -> i } + shuffledList.shuffle() + val index = shuffledList.indexOf(player.currentMediaItemIndex) + // swap current media index to the first index + if (index > -1 && shuffledList.isNotEmpty()) + run { val tmp = shuffledList[0]; shuffledList[0] = shuffledList[index]; shuffledList[index] = tmp} + player.shuffleOrder = DefaultShuffleOrder(shuffledList, kotlin.random.Random.nextLong()) + } + } + + override fun onIsPlayingChanged(isPlaying: Boolean) { + if (!isPlaying) { + MediaManager.setPlayingPausedTimestamp( + player.currentMediaItem, + player.currentPosition + ) + } else { + MediaManager.scrobble(player.currentMediaItem, false) + } + if (isPlaying) { + scheduleWidgetUpdates() + } else { + stopWidgetUpdates() + } + updateWidget() + } + + override fun onPlaybackStateChanged(playbackState: Int) { + super.onPlaybackStateChanged(playbackState) + if (!player.hasNextMediaItem() && + playbackState == Player.STATE_ENDED && + player.mediaMetadata.extras?.getString("type") == Constants.MEDIA_TYPE_MUSIC + ) { + MediaManager.scrobble(player.currentMediaItem, true) + MediaManager.saveChronology(player.currentMediaItem) + } + updateWidget() + } + + override fun onPositionDiscontinuity( + oldPosition: Player.PositionInfo, + newPosition: Player.PositionInfo, + reason: Int + ) { + super.onPositionDiscontinuity(oldPosition, newPosition, reason) + + if (reason == Player.DISCONTINUITY_REASON_AUTO_TRANSITION) { + if (oldPosition.mediaItem?.mediaMetadata?.extras?.getString("type") == Constants.MEDIA_TYPE_MUSIC) { + MediaManager.scrobble(oldPosition.mediaItem, true) + MediaManager.saveChronology(oldPosition.mediaItem) + } + + if (newPosition.mediaItem?.mediaMetadata?.extras?.getString("type") == Constants.MEDIA_TYPE_MUSIC) { + MediaManager.setLastPlayedTimestamp(newPosition.mediaItem) + } + } + } + + override fun onShuffleModeEnabledChanged(shuffleModeEnabled: Boolean) { + Preferences.setShuffleModeEnabled(shuffleModeEnabled) + customLayout = librarySessionCallback.buildCustomLayout(player) + mediaLibrarySession.setCustomLayout(customLayout) + } + + override fun onRepeatModeChanged(repeatMode: Int) { + Preferences.setRepeatMode(repeatMode) + customLayout = librarySessionCallback.buildCustomLayout(player) + mediaLibrarySession.setCustomLayout(customLayout) + } + + override fun onAudioSessionIdChanged(audioSessionId: Int) { + attachEqualizerIfPossible(audioSessionId) + } + }) + if (player.isPlaying) { + scheduleWidgetUpdates() + } + } + + private fun setPlayer(player: Player) { + mediaLibrarySession.player = player + } + + private fun releasePlayer() { + player.release() + mediaLibrarySession.release() + } + + private fun releaseNetworkCallback() { + getSystemService(ConnectivityManager::class.java).unregisterNetworkCallback(networkCallback) + } + + @SuppressLint("PrivateResource") + private fun getShuffleCommandButton(sessionCommand: SessionCommand): CommandButton { + val isOn = sessionCommand.customAction == CUSTOM_COMMAND_TOGGLE_SHUFFLE_MODE_ON + return CommandButton.Builder() + .setDisplayName( + getString( + if (isOn) R.string.exo_controls_shuffle_on_description + else R.string.exo_controls_shuffle_off_description + ) + ) + .setSessionCommand(sessionCommand) + .setIconResId(if (isOn) R.drawable.exo_icon_shuffle_off else R.drawable.exo_icon_shuffle_on) + .build() + } + + @SuppressLint("PrivateResource") + private fun getRepeatCommandButton(sessionCommand: SessionCommand): CommandButton { + val icon = when (sessionCommand.customAction) { + CUSTOM_COMMAND_TOGGLE_REPEAT_MODE_ONE -> R.drawable.exo_icon_repeat_one + CUSTOM_COMMAND_TOGGLE_REPEAT_MODE_ALL -> R.drawable.exo_icon_repeat_all + else -> R.drawable.exo_icon_repeat_off + } + val description = when (sessionCommand.customAction) { + CUSTOM_COMMAND_TOGGLE_REPEAT_MODE_ONE -> R.string.exo_controls_repeat_one_description + CUSTOM_COMMAND_TOGGLE_REPEAT_MODE_ALL -> R.string.exo_controls_repeat_all_description + else -> R.string.exo_controls_repeat_off_description + } + return CommandButton.Builder() + .setDisplayName(getString(description)) + .setSessionCommand(sessionCommand) + .setIconResId(icon) + .build() + } + + private fun ignoreFuture(@Suppress("UNUSED_PARAMETER") customLayout: ListenableFuture) { + /* Do nothing. */ + } + + private fun initializeLoadControl(): DefaultLoadControl { + return DefaultLoadControl.Builder() + .setBufferDurationsMs( + (DefaultLoadControl.DEFAULT_MIN_BUFFER_MS * Preferences.getBufferingStrategy()).toInt(), + (DefaultLoadControl.DEFAULT_MAX_BUFFER_MS * Preferences.getBufferingStrategy()).toInt(), + DefaultLoadControl.DEFAULT_BUFFER_FOR_PLAYBACK_MS, + DefaultLoadControl.DEFAULT_BUFFER_FOR_PLAYBACK_AFTER_REBUFFER_MS + ) + .build() + } + + private fun updateWidget() { + val mi = player.currentMediaItem + val title = mi?.mediaMetadata?.title?.toString() + ?: mi?.mediaMetadata?.extras?.getString("title") + val artist = mi?.mediaMetadata?.artist?.toString() + ?: mi?.mediaMetadata?.extras?.getString("artist") + val album = mi?.mediaMetadata?.albumTitle?.toString() + ?: mi?.mediaMetadata?.extras?.getString("album") + val extras = mi?.mediaMetadata?.extras + val coverId = extras?.getString("coverArtId") + val songLink = extras?.getString("assetLinkSong") + ?: AssetLinkUtil.buildLink(AssetLinkUtil.TYPE_SONG, extras?.getString("id")) + val albumLink = extras?.getString("assetLinkAlbum") + ?: AssetLinkUtil.buildLink(AssetLinkUtil.TYPE_ALBUM, extras?.getString("albumId")) + val artistLink = extras?.getString("assetLinkArtist") + ?: AssetLinkUtil.buildLink(AssetLinkUtil.TYPE_ARTIST, extras?.getString("artistId")) + val position = player.currentPosition.takeIf { it != C.TIME_UNSET } ?: 0L + val duration = player.duration.takeIf { it != C.TIME_UNSET } ?: 0L + WidgetUpdateManager.updateFromState( + this, + title ?: "", + artist ?: "", + album ?: "", + coverId, + player.isPlaying, + player.shuffleModeEnabled, + player.repeatMode, + position, + duration, + songLink, + albumLink, + artistLink + ) + } + + private fun scheduleWidgetUpdates() { + if (widgetUpdateScheduled) return + widgetUpdateHandler.postDelayed(widgetUpdateRunnable, WIDGET_UPDATE_INTERVAL_MS) + widgetUpdateScheduled = true + } + + private fun stopWidgetUpdates() { + if (!widgetUpdateScheduled) return + widgetUpdateHandler.removeCallbacks(widgetUpdateRunnable) + widgetUpdateScheduled = false + } + + private fun attachEqualizerIfPossible(audioSessionId: Int): Boolean { + if (audioSessionId == 0 || audioSessionId == -1) return false + val attached = equalizerManager.attachToSession(audioSessionId) + if (attached) { + val enabled = Preferences.isEqualizerEnabled() + equalizerManager.setEnabled(enabled) + val bands = equalizerManager.getNumberOfBands() + val savedLevels = Preferences.getEqualizerBandLevels(bands) + for (i in 0 until bands) { + equalizerManager.setBandLevel(i.toShort(), savedLevels[i]) + } + sendBroadcast(Intent(ACTION_EQUALIZER_UPDATED)) + } + return attached + } + + private fun getRenderersFactory() = DownloadUtil.buildRenderersFactory(this, false) + + private fun getMediaSourceFactory(): MediaSource.Factory = DynamicMediaSourceFactory(this) +} + +private const val WIDGET_UPDATE_INTERVAL_MS = 1000L diff --git a/app/src/main/java/com/cappielloantonio/tempo/service/BaseMediaService.kt b/app/src/main/java/com/cappielloantonio/tempo/service/BaseMediaService.kt deleted file mode 100644 index 6f5fc52d..00000000 --- a/app/src/main/java/com/cappielloantonio/tempo/service/BaseMediaService.kt +++ /dev/null @@ -1,590 +0,0 @@ -package com.cappielloantonio.tempo.service - -import android.annotation.SuppressLint -import android.app.PendingIntent.FLAG_IMMUTABLE -import android.app.PendingIntent.FLAG_UPDATE_CURRENT -import android.app.TaskStackBuilder -import android.content.Context -import android.content.Intent -import android.net.ConnectivityManager -import android.net.Network -import android.net.NetworkCapabilities -import android.os.Binder -import android.os.Bundle -import android.os.IBinder -import android.os.Handler -import android.os.Looper -import android.util.Log -import androidx.media3.common.* -import androidx.media3.common.util.UnstableApi -import androidx.media3.exoplayer.DefaultLoadControl -import androidx.media3.exoplayer.ExoPlayer -import androidx.media3.exoplayer.source.MediaSource -import androidx.media3.exoplayer.source.ShuffleOrder.DefaultShuffleOrder -import androidx.media3.session.* -import androidx.media3.session.MediaSession.ControllerInfo -import com.cappielloantonio.tempo.R -import com.cappielloantonio.tempo.repository.QueueRepository -import com.cappielloantonio.tempo.ui.activity.MainActivity -import com.cappielloantonio.tempo.util.* -import com.cappielloantonio.tempo.widget.WidgetUpdateManager -import com.google.common.collect.ImmutableList -import com.google.common.util.concurrent.Futures -import com.google.common.util.concurrent.ListenableFuture - -@UnstableApi -open class BaseMediaService : MediaLibraryService() { - companion object { - private const val CUSTOM_COMMAND_TOGGLE_SHUFFLE_MODE_ON = - "android.media3.session.demo.SHUFFLE_ON" - private const val CUSTOM_COMMAND_TOGGLE_SHUFFLE_MODE_OFF = - "android.media3.session.demo.SHUFFLE_OFF" - private const val CUSTOM_COMMAND_TOGGLE_REPEAT_MODE_OFF = - "android.media3.session.demo.REPEAT_OFF" - private const val CUSTOM_COMMAND_TOGGLE_REPEAT_MODE_ONE = - "android.media3.session.demo.REPEAT_ONE" - private const val CUSTOM_COMMAND_TOGGLE_REPEAT_MODE_ALL = - "android.media3.session.demo.REPEAT_ALL" - const val ACTION_BIND_EQUALIZER = "com.cappielloantonio.tempo.service.BIND_EQUALIZER" - const val ACTION_EQUALIZER_UPDATED = "com.cappielloantonio.tempo.service.EQUALIZER_UPDATED" - } - - protected lateinit var exoplayer: ExoPlayer - protected lateinit var mediaLibrarySession: MediaLibrarySession - private lateinit var networkCallback: CustomNetworkCallback - private lateinit var equalizerManager: EqualizerManager - private val widgetUpdateHandler = Handler(Looper.getMainLooper()) - private var widgetUpdateScheduled = false - private val widgetUpdateRunnable = object : Runnable { - override fun run() { - val player = mediaLibrarySession.player - if (!player.isPlaying) { - widgetUpdateScheduled = false - return - } - updateWidget(player) - widgetUpdateHandler.postDelayed(this, WIDGET_UPDATE_INTERVAL_MS) - } - } - - private val binder = LocalBinder() - - open fun playerInitHook() { - initializeExoPlayer() - initializeMediaLibrarySession(exoplayer) - initializePlayerListener(exoplayer) - setPlayer(null, exoplayer) - } - - open fun getMediaLibrarySessionCallback(): MediaLibrarySession.Callback { - return CustomMediaLibrarySessionCallback(baseContext) - } - - fun updateMediaItems(player: Player) { - Log.d(javaClass.toString(), "update items") - val n = player.mediaItemCount - val k = player.currentMediaItemIndex - val current = player.currentPosition - val items = (0..n - 1).map { MappingUtil.mapMediaItem(player.getMediaItemAt(it)) } - player.clearMediaItems() - player.setMediaItems(items, k, current) - } - - fun restorePlayerFromQueue(player: Player) { - if (player.mediaItemCount > 0) return - - val queueRepository = QueueRepository() - val storedQueue = queueRepository.media - if (storedQueue.isNullOrEmpty()) return - - val mediaItems = MappingUtil.mapMediaItems(storedQueue) - if (mediaItems.isEmpty()) return - - val lastIndex = try { - queueRepository.lastPlayedMediaIndex - } catch (_: Exception) { - 0 - }.coerceIn(0, mediaItems.size - 1) - - val lastPosition = try { - queueRepository.lastPlayedMediaTimestamp - } catch (_: Exception) { - 0L - }.let { if (it < 0L) 0L else it } - - player.setMediaItems(mediaItems, lastIndex, lastPosition) - player.prepare() - updateWidget(player) - } - - fun initializePlayerListener(player: Player) { - player.addListener(object : Player.Listener { - override fun onMediaItemTransition(mediaItem: MediaItem?, reason: Int) { - Log.d(javaClass.toString(), "onMediaItemTransition" + player.currentMediaItemIndex) - if (mediaItem == null) return - - if (reason == Player.MEDIA_ITEM_TRANSITION_REASON_SEEK || reason == Player.MEDIA_ITEM_TRANSITION_REASON_AUTO) { - MediaManager.setLastPlayedTimestamp(mediaItem) - } - updateWidget(player) - } - - override fun onTracksChanged(tracks: Tracks) { - Log.d(javaClass.toString(), "onTracksChanged " + player.currentMediaItemIndex) - ReplayGainUtil.setReplayGain(player, tracks) - val currentMediaItem = player.currentMediaItem - if (currentMediaItem != null) { - val item = MappingUtil.mapMediaItem(currentMediaItem) - if (item.mediaMetadata.extras != null) - MediaManager.scrobble(item, false) - - if (player.nextMediaItemIndex == C.INDEX_UNSET) - MediaManager.continuousPlay(player.currentMediaItem) - } - - if (player is ExoPlayer) { - // https://stackoverflow.com/questions/56937283/exoplayer-shuffle-doesnt-reproduce-all-the-songs - if (MediaManager.justStarted.get()) { - Log.d(javaClass.toString(), "update shuffle order") - MediaManager.justStarted.set(false) - val shuffledList = IntArray(player.mediaItemCount) { i -> i } - shuffledList.shuffle() - val index = shuffledList.indexOf(player.currentMediaItemIndex) - // swap current media index to the first index - if (index > -1 && shuffledList.isNotEmpty()) { - val tmp = shuffledList[0] - shuffledList[0] = shuffledList[index] - shuffledList[index] = tmp - } - player.shuffleOrder = - DefaultShuffleOrder(shuffledList, kotlin.random.Random.nextLong()) - } - } - } - - override fun onIsPlayingChanged(isPlaying: Boolean) { - Log.d(javaClass.toString(), "onIsPlayingChanged " + player.currentMediaItemIndex) - if (!isPlaying) { - MediaManager.setPlayingPausedTimestamp( - player.currentMediaItem, - player.currentPosition - ) - } else { - MediaManager.scrobble(player.currentMediaItem, false) - } - if (isPlaying) { - scheduleWidgetUpdates() - } else { - stopWidgetUpdates() - } - updateWidget(player) - } - - override fun onPlaybackStateChanged(playbackState: Int) { - Log.d(javaClass.toString(), "onPlaybackStateChanged") - super.onPlaybackStateChanged(playbackState) - if (!player.hasNextMediaItem() && - playbackState == Player.STATE_ENDED && - player.mediaMetadata.extras?.getString("type") == Constants.MEDIA_TYPE_MUSIC - ) { - MediaManager.scrobble(player.currentMediaItem, true) - MediaManager.saveChronology(player.currentMediaItem) - } - updateWidget(player) - } - - override fun onPositionDiscontinuity( - oldPosition: Player.PositionInfo, - newPosition: Player.PositionInfo, - reason: Int - ) { - Log.d(javaClass.toString(), "onPositionDiscontinuity") - super.onPositionDiscontinuity(oldPosition, newPosition, reason) - - if (reason == Player.DISCONTINUITY_REASON_AUTO_TRANSITION) { - if (oldPosition.mediaItem?.mediaMetadata?.extras?.getString("type") == Constants.MEDIA_TYPE_MUSIC) { - MediaManager.scrobble(oldPosition.mediaItem, true) - MediaManager.saveChronology(oldPosition.mediaItem) - } - - if (newPosition.mediaItem?.mediaMetadata?.extras?.getString("type") == Constants.MEDIA_TYPE_MUSIC) { - MediaManager.setLastPlayedTimestamp(newPosition.mediaItem) - } - } - } - - override fun onShuffleModeEnabledChanged(shuffleModeEnabled: Boolean) { - Preferences.setShuffleModeEnabled(shuffleModeEnabled) - } - - override fun onRepeatModeChanged(repeatMode: Int) { - Preferences.setRepeatMode(repeatMode) - } - - override fun onAudioSessionIdChanged(audioSessionId: Int) { - Log.d(javaClass.toString(), "onAudioSessionIdChanged") - attachEqualizerIfPossible(audioSessionId) - } - }) - if (player.isPlaying) { - scheduleWidgetUpdates() - } - } - - fun setPlayer(oldPlayer: Player?, newPlayer: Player) { - if (oldPlayer === newPlayer) return - if (oldPlayer != null) { - val currentQueue = getQueueFromPlayer(oldPlayer) - val currentIndex = oldPlayer.currentMediaItemIndex - val currentPosition = oldPlayer.currentPosition - val isPlaying = oldPlayer.playWhenReady - oldPlayer.stop() - newPlayer.setMediaItems(currentQueue, currentIndex, currentPosition) - newPlayer.playWhenReady = isPlaying - newPlayer.prepare() - } - mediaLibrarySession.player = newPlayer - } - - open fun releasePlayers() { - exoplayer.release() - } - - fun getQueueFromPlayer(player: Player): List { - return (0..player.mediaItemCount - 1).map(player::getMediaItemAt) - } - - override fun onTaskRemoved(rootIntent: Intent?) { - val player = mediaLibrarySession.player - - if (!player.playWhenReady || player.mediaItemCount == 0) { - stopSelf() - } - } - - override fun onCreate() { - super.onCreate() - - playerInitHook() - initializeEqualizerManager() - initializeNetworkListener() - restorePlayerFromQueue(mediaLibrarySession.player) - } - - override fun onGetSession(controllerInfo: ControllerInfo): MediaLibrarySession { - return mediaLibrarySession - } - - override fun onDestroy() { - releaseNetworkCallback() - equalizerManager.release() - stopWidgetUpdates() - releasePlayers() - mediaLibrarySession.release() - super.onDestroy() - } - - override fun onBind(intent: Intent?): IBinder? { - // Check if the intent is for our custom equalizer binder - if (intent?.action == ACTION_BIND_EQUALIZER) { - return binder - } - // Otherwise, handle it as a normal MediaLibraryService connection - return super.onBind(intent) - } - - private fun initializeExoPlayer() { - exoplayer = ExoPlayer.Builder(this) - .setRenderersFactory(getRenderersFactory()) - .setMediaSourceFactory(getMediaSourceFactory()) - .setAudioAttributes(AudioAttributes.DEFAULT, true) - .setHandleAudioBecomingNoisy(true) - .setWakeMode(C.WAKE_MODE_NETWORK) - .setLoadControl(initializeLoadControl()) - .build() - - exoplayer.shuffleModeEnabled = Preferences.isShuffleModeEnabled() - exoplayer.repeatMode = Preferences.getRepeatMode() - } - - private fun initializeEqualizerManager() { - equalizerManager = EqualizerManager() - val audioSessionId = exoplayer.audioSessionId - attachEqualizerIfPossible(audioSessionId) - } - - private fun initializeMediaLibrarySession(player: Player) { - Log.d(javaClass.toString(), "initializeMediaLibrarySession") - val sessionActivityPendingIntent = - TaskStackBuilder.create(this).run { - addNextIntent(Intent(baseContext, MainActivity::class.java)) - getPendingIntent(0, FLAG_IMMUTABLE or FLAG_UPDATE_CURRENT) - } - - mediaLibrarySession = - MediaLibrarySession.Builder(this, player, getMediaLibrarySessionCallback()) - .setSessionActivity(sessionActivityPendingIntent) - .build() - } - - private fun initializeNetworkListener() { - networkCallback = CustomNetworkCallback() - getSystemService(ConnectivityManager::class.java).registerDefaultNetworkCallback( - networkCallback - ) - updateMediaItems(mediaLibrarySession.player) - } - - private fun initializeLoadControl(): DefaultLoadControl { - return DefaultLoadControl.Builder() - .setBufferDurationsMs( - (DefaultLoadControl.DEFAULT_MIN_BUFFER_MS * Preferences.getBufferingStrategy()).toInt(), - (DefaultLoadControl.DEFAULT_MAX_BUFFER_MS * Preferences.getBufferingStrategy()).toInt(), - DefaultLoadControl.DEFAULT_BUFFER_FOR_PLAYBACK_MS, - DefaultLoadControl.DEFAULT_BUFFER_FOR_PLAYBACK_AFTER_REBUFFER_MS - ) - .build() - } - - private fun releaseNetworkCallback() { - getSystemService(ConnectivityManager::class.java).unregisterNetworkCallback(networkCallback) - } - - private fun updateWidget(player: Player) { - val mi = player.currentMediaItem - val title = mi?.mediaMetadata?.title?.toString() - ?: mi?.mediaMetadata?.extras?.getString("title") - val artist = mi?.mediaMetadata?.artist?.toString() - ?: mi?.mediaMetadata?.extras?.getString("artist") - val album = mi?.mediaMetadata?.albumTitle?.toString() - ?: mi?.mediaMetadata?.extras?.getString("album") - val extras = mi?.mediaMetadata?.extras - val coverId = extras?.getString("coverArtId") - val songLink = extras?.getString("assetLinkSong") - ?: AssetLinkUtil.buildLink(AssetLinkUtil.TYPE_SONG, extras?.getString("id")) - val albumLink = extras?.getString("assetLinkAlbum") - ?: AssetLinkUtil.buildLink(AssetLinkUtil.TYPE_ALBUM, extras?.getString("albumId")) - val artistLink = extras?.getString("assetLinkArtist") - ?: AssetLinkUtil.buildLink(AssetLinkUtil.TYPE_ARTIST, extras?.getString("artistId")) - val position = player.currentPosition.takeIf { it != C.TIME_UNSET } ?: 0L - val duration = player.duration.takeIf { it != C.TIME_UNSET } ?: 0L - WidgetUpdateManager.updateFromState( - this, - title ?: "", - artist ?: "", - album ?: "", - coverId, - player.isPlaying, - player.shuffleModeEnabled, - player.repeatMode, - position, - duration, - songLink, - albumLink, - artistLink - ) - } - - private fun scheduleWidgetUpdates() { - if (widgetUpdateScheduled) return - widgetUpdateHandler.postDelayed(widgetUpdateRunnable, WIDGET_UPDATE_INTERVAL_MS) - widgetUpdateScheduled = true - } - - private fun stopWidgetUpdates() { - if (!widgetUpdateScheduled) return - widgetUpdateHandler.removeCallbacks(widgetUpdateRunnable) - widgetUpdateScheduled = false - } - - private fun attachEqualizerIfPossible(audioSessionId: Int): Boolean { - if (audioSessionId == 0 || audioSessionId == -1) return false - val attached = equalizerManager.attachToSession(audioSessionId) - if (attached) { - val enabled = Preferences.isEqualizerEnabled() - equalizerManager.setEnabled(enabled) - val bands = equalizerManager.getNumberOfBands() - val savedLevels = Preferences.getEqualizerBandLevels(bands) - for (i in 0 until bands) { - equalizerManager.setBandLevel(i.toShort(), savedLevels[i]) - } - sendBroadcast(Intent(ACTION_EQUALIZER_UPDATED)) - } - return attached - } - - private fun getRenderersFactory() = DownloadUtil.buildRenderersFactory(this, false) - - private fun getMediaSourceFactory(): MediaSource.Factory = DynamicMediaSourceFactory(this) - - @UnstableApi - private class CustomMediaLibrarySessionCallback : MediaLibrarySession.Callback { - private val shuffleCommands: List - private val repeatCommands: List - - constructor(ctx: Context) { - shuffleCommands = listOf( - CUSTOM_COMMAND_TOGGLE_SHUFFLE_MODE_ON, - CUSTOM_COMMAND_TOGGLE_SHUFFLE_MODE_OFF - ) - .map { getShuffleCommandButton(SessionCommand(it, Bundle.EMPTY), ctx) } - repeatCommands = listOf( - CUSTOM_COMMAND_TOGGLE_REPEAT_MODE_OFF, - CUSTOM_COMMAND_TOGGLE_REPEAT_MODE_ONE, - CUSTOM_COMMAND_TOGGLE_REPEAT_MODE_ALL - ) - .map { getRepeatCommandButton(SessionCommand(it, Bundle.EMPTY), ctx) } - } - - override fun onConnect( - session: MediaSession, - controller: ControllerInfo - ): MediaSession.ConnectionResult { - val connectionResult = super.onConnect(session, controller) - val availableSessionCommands = connectionResult.availableSessionCommands.buildUpon() - - (shuffleCommands + repeatCommands).forEach { commandButton -> - commandButton.sessionCommand?.let { availableSessionCommands.add(it) } - } - - val result = MediaSession.ConnectionResult.AcceptedResultBuilder(session) - .setAvailableSessionCommands(availableSessionCommands.build()) - .setAvailablePlayerCommands(connectionResult.availablePlayerCommands) - .setMediaButtonPreferences(buildCustomLayout(session.player)) - .build() - return result - } - - override fun onCustomCommand( - session: MediaSession, - controller: ControllerInfo, - customCommand: SessionCommand, - args: Bundle - ): ListenableFuture { - Log.d(javaClass.toString(), "onCustomCommand") - when (customCommand.customAction) { - CUSTOM_COMMAND_TOGGLE_SHUFFLE_MODE_ON -> session.player.shuffleModeEnabled = true - CUSTOM_COMMAND_TOGGLE_SHUFFLE_MODE_OFF -> session.player.shuffleModeEnabled = false - CUSTOM_COMMAND_TOGGLE_REPEAT_MODE_OFF, - CUSTOM_COMMAND_TOGGLE_REPEAT_MODE_ALL, - CUSTOM_COMMAND_TOGGLE_REPEAT_MODE_ONE -> { - val nextMode = when (session.player.repeatMode) { - Player.REPEAT_MODE_ONE -> Player.REPEAT_MODE_ALL - Player.REPEAT_MODE_OFF -> Player.REPEAT_MODE_ONE - else -> Player.REPEAT_MODE_OFF - } - session.player.repeatMode = nextMode - } - } - - session.setMediaButtonPreferences(buildCustomLayout(session.player)) - return Futures.immediateFuture(SessionResult(SessionResult.RESULT_SUCCESS)) - } - - override fun onAddMediaItems( - mediaSession: MediaSession, - controller: ControllerInfo, - mediaItems: List - ): ListenableFuture> { - Log.d(javaClass.toString(), "onAddMediaItems") - val updatedMediaItems = mediaItems.map { mediaItem -> - val mediaMetadata = mediaItem.mediaMetadata - val newMetadata = mediaMetadata.buildUpon() - .setArtist( - if (mediaMetadata.artist != null) mediaMetadata.artist - else mediaMetadata.extras?.getString("uri") ?: "" - ) - .build() - - mediaItem.buildUpon() - .setUri(mediaItem.requestMetadata.mediaUri) - .setMediaMetadata(newMetadata) - .setMimeType(MimeTypes.BASE_TYPE_AUDIO) - .build() - } - return Futures.immediateFuture(updatedMediaItems) - } - - @SuppressLint("PrivateResource") - private fun getShuffleCommandButton( - sessionCommand: SessionCommand, - ctx: Context - ): CommandButton { - val isOn = sessionCommand.customAction == CUSTOM_COMMAND_TOGGLE_SHUFFLE_MODE_ON - return CommandButton.Builder(if (isOn) CommandButton.ICON_SHUFFLE_OFF else CommandButton.ICON_SHUFFLE_ON) - .setSessionCommand(sessionCommand) - .setDisplayName( - ctx.getString( - if (isOn) R.string.exo_controls_shuffle_on_description - else R.string.exo_controls_shuffle_off_description - ) - ) - .build() - } - - @SuppressLint("PrivateResource") - private fun getRepeatCommandButton( - sessionCommand: SessionCommand, - ctx: Context - ): CommandButton { - val icon = when (sessionCommand.customAction) { - CUSTOM_COMMAND_TOGGLE_REPEAT_MODE_ONE -> CommandButton.ICON_REPEAT_ONE - CUSTOM_COMMAND_TOGGLE_REPEAT_MODE_ALL -> CommandButton.ICON_REPEAT_ALL - else -> CommandButton.ICON_REPEAT_OFF - } - val description = when (sessionCommand.customAction) { - CUSTOM_COMMAND_TOGGLE_REPEAT_MODE_ONE -> R.string.exo_controls_repeat_one_description - CUSTOM_COMMAND_TOGGLE_REPEAT_MODE_ALL -> R.string.exo_controls_repeat_all_description - else -> R.string.exo_controls_repeat_off_description - } - return CommandButton.Builder(icon) - .setSessionCommand(sessionCommand) - .setDisplayName(ctx.getString(description)) - .build() - } - - private fun buildCustomLayout(player: Player): ImmutableList { - val shuffle = shuffleCommands[if (player.shuffleModeEnabled) 1 else 0] - val repeat = when (player.repeatMode) { - Player.REPEAT_MODE_ONE -> repeatCommands[1] - Player.REPEAT_MODE_ALL -> repeatCommands[2] - else -> repeatCommands[0] - } - return ImmutableList.of(shuffle, repeat) - } - } - - private inner class CustomNetworkCallback : ConnectivityManager.NetworkCallback() { - var wasWifi = false - - init { - val manager = getSystemService(ConnectivityManager::class.java) - val network = manager.activeNetwork - val capabilities = manager.getNetworkCapabilities(network) - if (capabilities != null) - wasWifi = capabilities.hasTransport(NetworkCapabilities.TRANSPORT_WIFI) - } - - override fun onCapabilitiesChanged( - network: Network, - networkCapabilities: NetworkCapabilities - ) { - val isWifi = networkCapabilities.hasTransport(NetworkCapabilities.TRANSPORT_WIFI) - if (isWifi != wasWifi) { - wasWifi = isWifi - widgetUpdateHandler.post { - updateMediaItems(mediaLibrarySession.player) - } - } - } - } - - inner class LocalBinder : Binder() { - fun getEqualizerManager(): EqualizerManager { - return equalizerManager - } - } -} - -private const val WIDGET_UPDATE_INTERVAL_MS = 1000L - diff --git a/app/src/main/java/com/cappielloantonio/tempo/ui/fragment/EqualizerFragment.kt b/app/src/main/java/com/cappielloantonio/tempo/ui/fragment/EqualizerFragment.kt index ac6608c7..0b97d516 100644 --- a/app/src/main/java/com/cappielloantonio/tempo/ui/fragment/EqualizerFragment.kt +++ b/app/src/main/java/com/cappielloantonio/tempo/ui/fragment/EqualizerFragment.kt @@ -19,7 +19,6 @@ import androidx.fragment.app.Fragment import androidx.media3.common.util.UnstableApi import com.cappielloantonio.tempo.R import com.cappielloantonio.tempo.service.EqualizerManager -import com.cappielloantonio.tempo.service.BaseMediaService import com.cappielloantonio.tempo.service.MediaService import com.cappielloantonio.tempo.util.Preferences @@ -36,7 +35,7 @@ class EqualizerFragment : Fragment() { private val equalizerUpdatedReceiver = object : BroadcastReceiver() { @OptIn(UnstableApi::class) override fun onReceive(context: Context?, intent: Intent?) { - if (intent?.action == BaseMediaService.ACTION_EQUALIZER_UPDATED) { + if (intent?.action == MediaService.ACTION_EQUALIZER_UPDATED) { initUI() restoreEqualizerPreferences() } @@ -46,7 +45,7 @@ class EqualizerFragment : Fragment() { private val connection = object : ServiceConnection { @OptIn(UnstableApi::class) override fun onServiceConnected(className: ComponentName, service: IBinder) { - val binder = service as BaseMediaService.LocalBinder + val binder = service as MediaService.LocalBinder equalizerManager = binder.getEqualizerManager() initUI() restoreEqualizerPreferences() @@ -61,14 +60,14 @@ class EqualizerFragment : Fragment() { override fun onStart() { super.onStart() Intent(requireContext(), MediaService::class.java).also { intent -> - intent.action = BaseMediaService.ACTION_BIND_EQUALIZER + intent.action = MediaService.ACTION_BIND_EQUALIZER requireActivity().bindService(intent, connection, Context.BIND_AUTO_CREATE) } if (!receiverRegistered) { ContextCompat.registerReceiver( requireContext(), equalizerUpdatedReceiver, - IntentFilter(BaseMediaService.ACTION_EQUALIZER_UPDATED), + IntentFilter(MediaService.ACTION_EQUALIZER_UPDATED), ContextCompat.RECEIVER_NOT_EXPORTED ) receiverRegistered = true diff --git a/app/src/main/java/com/cappielloantonio/tempo/util/ReplayGainUtil.java b/app/src/main/java/com/cappielloantonio/tempo/util/ReplayGainUtil.java index 48d727ee..510673cb 100644 --- a/app/src/main/java/com/cappielloantonio/tempo/util/ReplayGainUtil.java +++ b/app/src/main/java/com/cappielloantonio/tempo/util/ReplayGainUtil.java @@ -1,12 +1,11 @@ package com.cappielloantonio.tempo.util; import androidx.annotation.OptIn; -import androidx.media3.common.C; import androidx.media3.common.MediaItem; import androidx.media3.common.Metadata; import androidx.media3.common.Tracks; import androidx.media3.common.util.UnstableApi; -import androidx.media3.common.Player; +import androidx.media3.exoplayer.ExoPlayer; import com.cappielloantonio.tempo.model.ReplayGain; @@ -18,7 +17,7 @@ import java.util.Objects; public class ReplayGainUtil { private static final String[] tags = {"REPLAYGAIN_TRACK_GAIN", "REPLAYGAIN_ALBUM_GAIN", "R128_TRACK_GAIN", "R128_ALBUM_GAIN"}; - public static void setReplayGain(Player player, Tracks tracks) { + public static void setReplayGain(ExoPlayer player, Tracks tracks) { List metadata = getMetadata(tracks); List gains = getReplayGains(metadata); @@ -63,7 +62,7 @@ public class ReplayGainUtil { } } - if (gains.isEmpty()) gains.add(0, new ReplayGain()); + if (gains.size() == 0) gains.add(0, new ReplayGain()); if (gains.size() == 1) gains.add(1, new ReplayGain()); return gains; @@ -109,7 +108,7 @@ public class ReplayGainUtil { } } - private static void applyReplayGain(Player player, List gains) { + private static void applyReplayGain(ExoPlayer player, List gains) { if (Objects.equals(Preferences.getReplayGainMode(), "disabled") || gains == null || gains.isEmpty()) { setNoReplayGain(player); return; @@ -138,33 +137,33 @@ public class ReplayGainUtil { setNoReplayGain(player); } - private static void setNoReplayGain(Player player) { + private static void setNoReplayGain(ExoPlayer player) { setReplayGain(player, 0f); } - private static void setTrackReplayGain(Player player, List gains) { + private static void setTrackReplayGain(ExoPlayer player, List gains) { float trackGain = gains.get(0).getTrackGain() != 0f ? gains.get(0).getTrackGain() : gains.get(1).getTrackGain(); setReplayGain(player, trackGain != 0f ? trackGain : 0f); } - private static void setAlbumReplayGain(Player player, List gains) { + private static void setAlbumReplayGain(ExoPlayer player, List gains) { float albumGain = gains.get(0).getAlbumGain() != 0f ? gains.get(0).getAlbumGain() : gains.get(1).getAlbumGain(); setReplayGain(player, albumGain != 0f ? albumGain : 0f); } - private static void setAutoReplayGain(Player player, List gains) { + private static void setAutoReplayGain(ExoPlayer player, List gains) { float albumGain = gains.get(0).getAlbumGain() != 0f ? gains.get(0).getAlbumGain() : gains.get(1).getAlbumGain(); float trackGain = gains.get(0).getTrackGain() != 0f ? gains.get(0).getTrackGain() : gains.get(1).getTrackGain(); setReplayGain(player, albumGain != 0f ? albumGain : trackGain); } - private static boolean areTracksConsecutive(Player player) { + private static boolean areTracksConsecutive(ExoPlayer player) { MediaItem currentMediaItem = player.getCurrentMediaItem(); - int prevMediaItemIndex = player.getPreviousMediaItemIndex(); - MediaItem pastMediaItem = prevMediaItemIndex == C.INDEX_UNSET ? null : player.getMediaItemAt(prevMediaItemIndex); + int currentMediaItemIndex = player.getCurrentMediaItemIndex(); + MediaItem pastMediaItem = currentMediaItemIndex > 0 ? player.getMediaItemAt(currentMediaItemIndex - 1) : null; return currentMediaItem != null && pastMediaItem != null && @@ -173,7 +172,7 @@ public class ReplayGainUtil { pastMediaItem.mediaMetadata.albumTitle.toString().equals(currentMediaItem.mediaMetadata.albumTitle.toString()); } - private static void setReplayGain(Player player, float gain) { + private static void setReplayGain(ExoPlayer player, float gain) { player.setVolume((float) Math.pow(10f, gain / 20f)); } } diff --git a/app/src/tempus/java/com/cappielloantonio/tempo/service/MediaService.kt b/app/src/tempus/java/com/cappielloantonio/tempo/service/MediaService.kt index 784b1986..52fa8c1d 100644 --- a/app/src/tempus/java/com/cappielloantonio/tempo/service/MediaService.kt +++ b/app/src/tempus/java/com/cappielloantonio/tempo/service/MediaService.kt @@ -1,18 +1,182 @@ package com.cappielloantonio.tempo.service +import android.app.PendingIntent.FLAG_IMMUTABLE +import android.app.PendingIntent.FLAG_UPDATE_CURRENT +import android.app.TaskStackBuilder +import android.content.Intent +import android.net.ConnectivityManager +import android.net.Network +import android.net.NetworkCapabilities +import android.os.Binder +import android.os.IBinder +import android.os.Handler +import android.os.Looper +import android.util.Log import androidx.core.content.ContextCompat import androidx.media3.cast.CastPlayer import androidx.media3.cast.SessionAvailabilityListener +import androidx.media3.common.AudioAttributes +import androidx.media3.common.C +import androidx.media3.common.MediaItem +import androidx.media3.common.Player +import androidx.media3.common.Tracks import androidx.media3.common.util.UnstableApi +import androidx.media3.exoplayer.DefaultLoadControl +import androidx.media3.exoplayer.ExoPlayer +import androidx.media3.session.MediaLibraryService +import androidx.media3.session.MediaSession.ControllerInfo import com.cappielloantonio.tempo.repository.AutomotiveRepository +import com.cappielloantonio.tempo.repository.QueueRepository +import com.cappielloantonio.tempo.ui.activity.MainActivity +import com.cappielloantonio.tempo.util.AssetLinkUtil +import com.cappielloantonio.tempo.util.Constants +import com.cappielloantonio.tempo.util.DownloadUtil +import com.cappielloantonio.tempo.util.DynamicMediaSourceFactory +import com.cappielloantonio.tempo.util.MappingUtil +import com.cappielloantonio.tempo.util.Preferences +import com.cappielloantonio.tempo.util.ReplayGainUtil +import com.cappielloantonio.tempo.widget.WidgetUpdateManager import com.google.android.gms.cast.framework.CastContext import com.google.android.gms.common.ConnectionResult import com.google.android.gms.common.GoogleApiAvailability @UnstableApi -class MediaService : BaseMediaService(), SessionAvailabilityListener { - private val automotiveRepository = AutomotiveRepository() +class MediaService : MediaLibraryService(), SessionAvailabilityListener { + private lateinit var automotiveRepository: AutomotiveRepository + private lateinit var player: ExoPlayer private lateinit var castPlayer: CastPlayer + private lateinit var mediaLibrarySession: MediaLibrarySession + private lateinit var librarySessionCallback: MediaLibrarySessionCallback + private lateinit var networkCallback: CustomNetworkCallback + lateinit var equalizerManager: EqualizerManager + + inner class LocalBinder : Binder() { + fun getEqualizerManager(): EqualizerManager { + return this@MediaService.equalizerManager + } + } + + private val binder = LocalBinder() + + companion object { + const val ACTION_BIND_EQUALIZER = "com.cappielloantonio.tempo.service.BIND_EQUALIZER" + const val ACTION_EQUALIZER_UPDATED = "com.cappielloantonio.tempo.service.EQUALIZER_UPDATED" + } + private val widgetUpdateHandler = Handler(Looper.getMainLooper()) + private var widgetUpdateScheduled = false + private val widgetUpdateRunnable = object : Runnable { + override fun run() { + if (!player.isPlaying) { + widgetUpdateScheduled = false + return + } + updateWidget() + widgetUpdateHandler.postDelayed(this, WIDGET_UPDATE_INTERVAL_MS) + } + } + + fun updateMediaItems() { + Log.d("MediaService", "update items"); + val n = player.mediaItemCount + val k = player.currentMediaItemIndex + val current = player.currentPosition + val items = (0 .. n-1).map{i -> MappingUtil.mapMediaItem(player.getMediaItemAt(i))} + player.clearMediaItems() + player.setMediaItems(items, k, current) + } + + inner class CustomNetworkCallback : ConnectivityManager.NetworkCallback() { + var wasWifi = false + + init { + val manager = getSystemService(ConnectivityManager::class.java) + val network = manager.activeNetwork + val capabilities = manager.getNetworkCapabilities(network) + if (capabilities != null) + wasWifi = capabilities.hasTransport(NetworkCapabilities.TRANSPORT_WIFI) + } + + override fun onCapabilitiesChanged(network : Network, networkCapabilities : NetworkCapabilities) { + val isWifi = networkCapabilities.hasTransport(NetworkCapabilities.TRANSPORT_WIFI) + if (isWifi != wasWifi) { + wasWifi = isWifi + widgetUpdateHandler.post(Runnable { + updateMediaItems() + }) + } + } + } + + override fun onCreate() { + super.onCreate() + + initializeRepository() + initializePlayer() + initializeMediaLibrarySession() + restorePlayerFromQueue() + initializePlayerListener() + initializeCastPlayer() + initializeEqualizerManager() + initializeNetworkListener() + + setPlayer( + null, + if (this::castPlayer.isInitialized && castPlayer.isCastSessionAvailable) castPlayer else player + ) + } + + override fun onGetSession(controllerInfo: ControllerInfo): MediaLibrarySession { + return mediaLibrarySession + } + + override fun onTaskRemoved(rootIntent: Intent?) { + val player = mediaLibrarySession.player + + if (!player.playWhenReady || player.mediaItemCount == 0) { + stopSelf() + } + } + + override fun onDestroy() { + releaseNetworkCallback() + equalizerManager.release() + stopWidgetUpdates() + releasePlayer() + super.onDestroy() + } + + override fun onBind(intent: Intent?): IBinder? { + // Check if the intent is for our custom equalizer binder + if (intent?.action == ACTION_BIND_EQUALIZER) { + return binder + } + // Otherwise, handle it as a normal MediaLibraryService connection + return super.onBind(intent) + } + + private fun initializeRepository() { + automotiveRepository = AutomotiveRepository() + } + + private fun initializeEqualizerManager() { + equalizerManager = EqualizerManager() + val audioSessionId = player.audioSessionId + attachEqualizerIfPossible(audioSessionId) + } + + private fun initializePlayer() { + player = ExoPlayer.Builder(this) + .setRenderersFactory(getRenderersFactory()) + .setMediaSourceFactory(DynamicMediaSourceFactory(this)) + .setAudioAttributes(AudioAttributes.DEFAULT, true) + .setHandleAudioBecomingNoisy(true) + .setWakeMode(C.WAKE_MODE_NETWORK) + .setLoadControl(initializeLoadControl()) + .build() + + player.shuffleModeEnabled = Preferences.isShuffleModeEnabled() + player.repeatMode = Preferences.getRepeatMode() + } @Suppress("DEPRECATION") private fun initializeCastPlayer() { @@ -20,41 +184,284 @@ class MediaService : BaseMediaService(), SessionAvailabilityListener { .isGooglePlayServicesAvailable(this) == ConnectionResult.SUCCESS ) { CastContext.getSharedInstance(this, ContextCompat.getMainExecutor(this)) - .addOnSuccessListener { castContext -> - castPlayer = CastPlayer(castContext) - castPlayer.setSessionAvailabilityListener(this@MediaService) - initializePlayerListener(castPlayer) - if (castPlayer.isCastSessionAvailable) - setPlayer(mediaLibrarySession.player, castPlayer) - } + .addOnSuccessListener { castContext -> + castPlayer = CastPlayer(castContext) + castPlayer.setSessionAvailabilityListener(this@MediaService) + + if (castPlayer.isCastSessionAvailable && this::mediaLibrarySession.isInitialized) { + setPlayer(player, castPlayer) + } + } } } - override fun getMediaLibrarySessionCallback(): MediaLibrarySession.Callback { + private fun initializeMediaLibrarySession() { + val sessionActivityPendingIntent = + TaskStackBuilder.create(this).run { + addNextIntent(Intent(this@MediaService, MainActivity::class.java)) + getPendingIntent(0, FLAG_IMMUTABLE or FLAG_UPDATE_CURRENT) + } + + librarySessionCallback = createLibrarySessionCallback() + mediaLibrarySession = + MediaLibrarySession.Builder(this, player, librarySessionCallback) + .setSessionActivity(sessionActivityPendingIntent) + .build() + } + + private fun initializeNetworkListener() { + networkCallback = CustomNetworkCallback() + getSystemService(ConnectivityManager::class.java).registerDefaultNetworkCallback(networkCallback) + updateMediaItems() + } + + private fun restorePlayerFromQueue() { + if (player.mediaItemCount > 0) return + + val queueRepository = QueueRepository() + val storedQueue = queueRepository.media + if (storedQueue.isNullOrEmpty()) return + + val mediaItems = MappingUtil.mapMediaItems(storedQueue) + if (mediaItems.isEmpty()) return + + val lastIndex = try { + queueRepository.lastPlayedMediaIndex + } catch (_: Exception) { + 0 + }.coerceIn(0, mediaItems.size - 1) + + val lastPosition = try { + queueRepository.lastPlayedMediaTimestamp + } catch (_: Exception) { + 0L + }.let { if (it < 0L) 0L else it } + + player.setMediaItems(mediaItems, lastIndex, lastPosition) + player.prepare() + updateWidget() + } + + private fun createLibrarySessionCallback(): MediaLibrarySessionCallback { return MediaLibrarySessionCallback(this, automotiveRepository) } - override fun playerInitHook() { - super.playerInitHook() - initializeCastPlayer() - if (this::castPlayer.isInitialized && castPlayer.isCastSessionAvailable) - setPlayer(null, castPlayer) + private fun initializePlayerListener() { + player.addListener(object : Player.Listener { + override fun onMediaItemTransition(mediaItem: MediaItem?, reason: Int) { + if (mediaItem == null) return + + if (reason == Player.MEDIA_ITEM_TRANSITION_REASON_SEEK || reason == Player.MEDIA_ITEM_TRANSITION_REASON_AUTO) { + MediaManager.setLastPlayedTimestamp(mediaItem) + } + updateWidget() + } + + override fun onTracksChanged(tracks: Tracks) { + ReplayGainUtil.setReplayGain(player, tracks) + + val currentMediaItem = player.currentMediaItem + if (currentMediaItem != null && currentMediaItem.mediaMetadata.extras != null) { + MediaManager.scrobble(currentMediaItem, false) + } + + if (player.currentMediaItemIndex + 1 == player.mediaItemCount) + MediaManager.continuousPlay(player.currentMediaItem) + } + + override fun onIsPlayingChanged(isPlaying: Boolean) { + if (!isPlaying) { + MediaManager.setPlayingPausedTimestamp( + player.currentMediaItem, + player.currentPosition + ) + } else { + MediaManager.scrobble(player.currentMediaItem, false) + } + if (isPlaying) { + scheduleWidgetUpdates() + } else { + stopWidgetUpdates() + } + updateWidget() + } + + override fun onPlaybackStateChanged(playbackState: Int) { + super.onPlaybackStateChanged(playbackState) + + if (!player.hasNextMediaItem() && + playbackState == Player.STATE_ENDED && + player.mediaMetadata.extras?.getString("type") == Constants.MEDIA_TYPE_MUSIC + ) { + MediaManager.scrobble(player.currentMediaItem, true) + MediaManager.saveChronology(player.currentMediaItem) + } + updateWidget() + } + + override fun onPositionDiscontinuity( + oldPosition: Player.PositionInfo, + newPosition: Player.PositionInfo, + reason: Int + ) { + super.onPositionDiscontinuity(oldPosition, newPosition, reason) + + if (reason == Player.DISCONTINUITY_REASON_AUTO_TRANSITION) { + if (oldPosition.mediaItem?.mediaMetadata?.extras?.getString("type") == Constants.MEDIA_TYPE_MUSIC) { + MediaManager.scrobble(oldPosition.mediaItem, true) + MediaManager.saveChronology(oldPosition.mediaItem) + } + + if (newPosition.mediaItem?.mediaMetadata?.extras?.getString("type") == Constants.MEDIA_TYPE_MUSIC) { + MediaManager.setLastPlayedTimestamp(newPosition.mediaItem) + } + } + } + + override fun onShuffleModeEnabledChanged(shuffleModeEnabled: Boolean) { + Preferences.setShuffleModeEnabled(shuffleModeEnabled) + } + + override fun onRepeatModeChanged(repeatMode: Int) { + Preferences.setRepeatMode(repeatMode) + } + + override fun onAudioSessionIdChanged(audioSessionId: Int) { + attachEqualizerIfPossible(audioSessionId) + } + }) + if (player.isPlaying) { + scheduleWidgetUpdates() + } } - override fun releasePlayers() { - if (this::castPlayer.isInitialized) { - castPlayer.setSessionAvailabilityListener(null) - castPlayer.release() - } - automotiveRepository.deleteMetadata() - super.releasePlayers() + private fun updateWidget() { + val mi = player.currentMediaItem + val title = mi?.mediaMetadata?.title?.toString() + ?: mi?.mediaMetadata?.extras?.getString("title") + val artist = mi?.mediaMetadata?.artist?.toString() + ?: mi?.mediaMetadata?.extras?.getString("artist") + val album = mi?.mediaMetadata?.albumTitle?.toString() + ?: mi?.mediaMetadata?.extras?.getString("album") + val extras = mi?.mediaMetadata?.extras + val coverId = extras?.getString("coverArtId") + val songLink = extras?.getString("assetLinkSong") + ?: AssetLinkUtil.buildLink(AssetLinkUtil.TYPE_SONG, extras?.getString("id")) + val albumLink = extras?.getString("assetLinkAlbum") + ?: AssetLinkUtil.buildLink(AssetLinkUtil.TYPE_ALBUM, extras?.getString("albumId")) + val artistLink = extras?.getString("assetLinkArtist") + ?: AssetLinkUtil.buildLink(AssetLinkUtil.TYPE_ARTIST, extras?.getString("artistId")) + val position = player.currentPosition.takeIf { it != C.TIME_UNSET } ?: 0L + val duration = player.duration.takeIf { it != C.TIME_UNSET } ?: 0L + WidgetUpdateManager.updateFromState( + this, + title ?: "", + artist ?: "", + album ?: "", + coverId, + player.isPlaying, + player.shuffleModeEnabled, + player.repeatMode, + position, + duration, + songLink, + albumLink, + artistLink + ) } + private fun scheduleWidgetUpdates() { + if (widgetUpdateScheduled) return + widgetUpdateHandler.postDelayed(widgetUpdateRunnable, WIDGET_UPDATE_INTERVAL_MS) + widgetUpdateScheduled = true + } + + private fun stopWidgetUpdates() { + if (!widgetUpdateScheduled) return + widgetUpdateHandler.removeCallbacks(widgetUpdateRunnable) + widgetUpdateScheduled = false + } + + private fun initializeLoadControl(): DefaultLoadControl { + return DefaultLoadControl.Builder() + .setBufferDurationsMs( + (DefaultLoadControl.DEFAULT_MIN_BUFFER_MS * Preferences.getBufferingStrategy()).toInt(), + (DefaultLoadControl.DEFAULT_MAX_BUFFER_MS * Preferences.getBufferingStrategy()).toInt(), + DefaultLoadControl.DEFAULT_BUFFER_FOR_PLAYBACK_MS, + DefaultLoadControl.DEFAULT_BUFFER_FOR_PLAYBACK_AFTER_REBUFFER_MS + ) + .build() + } + + private fun getQueueFromPlayer(player: Player): List { + val queue = mutableListOf() + for (i in 0 until player.mediaItemCount) { + queue.add(player.getMediaItemAt(i)) + } + return queue + } + + private fun setPlayer(oldPlayer: Player?, newPlayer: Player) { + if (oldPlayer === newPlayer) return + oldPlayer?.stop() + mediaLibrarySession.player = newPlayer + } + + private fun releasePlayer() { + if (this::castPlayer.isInitialized) castPlayer.setSessionAvailabilityListener(null) + if (this::castPlayer.isInitialized) castPlayer.release() + player.release() + mediaLibrarySession.release() + automotiveRepository.deleteMetadata() + } + + private fun releaseNetworkCallback() { + getSystemService(ConnectivityManager::class.java).unregisterNetworkCallback(networkCallback) + } + + private fun getRenderersFactory() = DownloadUtil.buildRenderersFactory(this, false) + override fun onCastSessionAvailable() { - setPlayer(exoplayer, castPlayer) + val currentQueue = getQueueFromPlayer(player) + val currentIndex = player.currentMediaItemIndex + val currentPosition = player.currentPosition + val isPlaying = player.playWhenReady + + setPlayer(player, castPlayer) + + castPlayer.setMediaItems(currentQueue, currentIndex, currentPosition) + castPlayer.playWhenReady = isPlaying + castPlayer.prepare() } override fun onCastSessionUnavailable() { - setPlayer(castPlayer, exoplayer) + val currentQueue = getQueueFromPlayer(castPlayer) + val currentIndex = castPlayer.currentMediaItemIndex + val currentPosition = castPlayer.currentPosition + val isPlaying = castPlayer.playWhenReady + + setPlayer(castPlayer, player) + + player.setMediaItems(currentQueue, currentIndex, currentPosition) + player.playWhenReady = isPlaying + player.prepare() } -} \ No newline at end of file + + private fun attachEqualizerIfPossible(audioSessionId: Int): Boolean { + if (audioSessionId == 0 || audioSessionId == -1) return false + val attached = equalizerManager.attachToSession(audioSessionId) + if (attached) { + val enabled = Preferences.isEqualizerEnabled() + equalizerManager.setEnabled(enabled) + val bands = equalizerManager.getNumberOfBands() + val savedLevels = Preferences.getEqualizerBandLevels(bands) + for (i in 0 until bands) { + equalizerManager.setBandLevel(i.toShort(), savedLevels[i]) + } + sendBroadcast(Intent(ACTION_EQUALIZER_UPDATED)) + } + return attached + } +} + +private const val WIDGET_UPDATE_INTERVAL_MS = 1000L