Skip to content

Commit 64d8b2c

Browse files
committed
Adapt current item in DetailActivity if needed
1 parent 7375286 commit 64d8b2c

File tree

4 files changed

+26
-3
lines changed

4 files changed

+26
-3
lines changed

Diff for: app/src/main/AndroidManifest.xml

+1-1
Original file line numberDiff line numberDiff line change
@@ -29,7 +29,7 @@
2929

3030
<service
3131
android:name=".player.MediaPlaybackService"
32-
android:permission="false">
32+
android:exported="false">
3333
<intent-filter>
3434
<action android:name="android.media.browse.MediaBrowserService" />
3535
</intent-filter>

Diff for: app/src/main/java/com/ludovic/vimont/deezeropenapi/helper/AudioHelper.kt

+16
Original file line numberDiff line numberDiff line change
@@ -8,6 +8,7 @@ import android.support.v4.media.MediaMetadataCompat
88
import android.support.v4.media.session.PlaybackStateCompat
99
import com.ludovic.vimont.deezeropenapi.model.Album
1010
import com.ludovic.vimont.deezeropenapi.model.Track
11+
import com.ludovic.vimont.deezeropenapi.player.AudioSessionCallback
1112

1213
/**
1314
* Simplify the interaction with MediaSession. We create the builder and keep it in memory like
@@ -51,9 +52,24 @@ object AudioHelper {
5152
.build()
5253
}
5354

55+
5456
fun getPlaybackState(playbackState: Int, positionInCurrentTrack: Long, actions: Long = fullPlaybackStateAction): PlaybackStateCompat? {
5557
return playbackStateCompatBuilder.setState(playbackState, positionInCurrentTrack, PLAYBACK_SPEED)
5658
.setActions(actions)
5759
.build()
5860
}
61+
62+
fun getPlaybackState(playbackState: Int,
63+
positionInCurrentTrack: Long,
64+
currentTrack: Int? = null,
65+
actions: Long = fullPlaybackStateAction): PlaybackStateCompat? {
66+
val bundle = Bundle()
67+
currentTrack?.let { track ->
68+
bundle.putInt(AudioSessionCallback.KEY_CURRENT_TRACK, track)
69+
}
70+
return playbackStateCompatBuilder.setState(playbackState, positionInCurrentTrack, PLAYBACK_SPEED)
71+
.setActions(actions)
72+
.setExtras(bundle)
73+
.build()
74+
}
5975
}

Diff for: app/src/main/java/com/ludovic/vimont/deezeropenapi/player/AudioSessionCallback.kt

+2-2
Original file line numberDiff line numberDiff line change
@@ -125,7 +125,7 @@ class AudioSessionCallback(private val service: MediaPlaybackService,
125125
setCurrentMetaData(currentIndex)
126126

127127
val currentPlayBackState: Int = mediaSession.controller.playbackState.state
128-
val playBackState: PlaybackStateCompat? = AudioHelper.getPlaybackState(PlaybackStateCompat.STATE_SKIPPING_TO_PREVIOUS, 0)
128+
val playBackState: PlaybackStateCompat? = AudioHelper.getPlaybackState(PlaybackStateCompat.STATE_SKIPPING_TO_PREVIOUS, 0, currentIndex)
129129
mediaSession.setPlaybackState(playBackState)
130130
adaptPlayerState(currentPlayBackState)
131131
}
@@ -141,7 +141,7 @@ class AudioSessionCallback(private val service: MediaPlaybackService,
141141
setCurrentMetaData(currentIndex)
142142

143143
val currentPlayBackState: Int = mediaSession.controller.playbackState.state
144-
val playBackState: PlaybackStateCompat? = AudioHelper.getPlaybackState(PlaybackStateCompat.STATE_SKIPPING_TO_NEXT, 0)
144+
val playBackState: PlaybackStateCompat? = AudioHelper.getPlaybackState(PlaybackStateCompat.STATE_SKIPPING_TO_NEXT, 0, currentIndex)
145145
mediaSession.setPlaybackState(playBackState)
146146
adaptPlayerState(currentPlayBackState)
147147
}

Diff for: app/src/main/java/com/ludovic/vimont/deezeropenapi/screens/detail/DetailMediaSessionHandler.kt

+7
Original file line numberDiff line numberDiff line change
@@ -60,6 +60,13 @@ class DetailMediaSessionHandler(private val detailActivity: DetailActivity): Med
6060
val mediaController: MediaControllerCompat = MediaControllerCompat.getMediaController(detailActivity)
6161
mediaController.registerCallback(controllerCallback)
6262
detailActivity.computeQueue(mediaController)
63+
64+
mediaController.playbackState.extras?.let { bundle ->
65+
if (bundle.containsKey(AudioSessionCallback.KEY_CURRENT_TRACK)) {
66+
val newValue: Int = bundle.getInt(AudioSessionCallback.KEY_CURRENT_TRACK)
67+
detailActivity.updateAdapterViewHolderState(newValue)
68+
}
69+
}
6370
}
6471

6572
override fun onConnectionSuspended() {

0 commit comments

Comments
 (0)