Utilize CarConnection to determine whether to set repeat mode to ALL

This commit is contained in:
Alex Katlein 2023-08-09 21:16:52 +02:00 committed by tzugen
parent b87203bfe4
commit d2a98a3022
No known key found for this signature in database
GPG Key ID: 61E9C34BC10EC930
4 changed files with 49 additions and 2 deletions

View File

@ -4,6 +4,7 @@ gradle = "8.1.1"
navigation = "2.6.0"
gradlePlugin = "8.1.0"
androidxcar = "1.2.0"
androidxcore = "1.10.1"
ktlint = "0.43.2"
ktlintGradle = "11.5.0"
@ -49,6 +50,7 @@ kotlin = { module = "org.jetbrains.kotlin:kotlin-gradle-plugin"
ktlintGradle = { module = "org.jlleitschuh.gradle:ktlint-gradle", version.ref = "ktlintGradle" }
detekt = { module = "io.gitlab.arturbosch.detekt:detekt-gradle-plugin", version.ref = "detekt" }
car = { module = "androidx.car.app:app", version.ref = "androidxcar" }
core = { module = "androidx.core:core-ktx", version.ref = "androidxcore" }
design = { module = "com.google.android.material:material", version.ref = "materialDesign" }
annotations = { module = "androidx.annotation:annotation", version.ref = "androidSupport" }
@ -103,4 +105,4 @@ apacheCodecs = { module = "commons-codec:commons-codec", version.ref
robolectric = { module = "org.robolectric:robolectric", version.ref = "robolectric" }
[plugins]
ksp = { id = "com.google.devtools.ksp", version.ref = "ksp" }
ksp = { id = "com.google.devtools.ksp", version.ref = "ksp" }

View File

@ -100,6 +100,7 @@ dependencies {
exclude group: "com.android.support"
}
implementation libs.car
implementation libs.core
implementation libs.design
implementation libs.multidex

View File

@ -12,6 +12,8 @@
<uses-permission android:name="android.permission.BLUETOOTH"/>
<uses-permission android:name="android.permission.FOREGROUND_SERVICE" />
<uses-sdk tools:overrideLibrary="androidx.car.app" />
<supports-screens
android:anyDensity="true"
android:largeScreens="true"

View File

@ -7,7 +7,10 @@
package org.moire.ultrasonic.playback
import android.content.Context
import android.os.Build
import android.os.Bundle
import androidx.car.app.connection.CarConnection
import androidx.media3.common.HeartRating
import androidx.media3.common.MediaItem
import androidx.media3.common.MediaMetadata.MEDIA_TYPE_FOLDER_ALBUMS
@ -97,6 +100,7 @@ const val PLAY_COMMAND = "play "
@Suppress("TooManyFunctions", "LargeClass", "UnusedPrivateMember")
class AutoMediaBrowserCallback : MediaLibraryService.MediaLibrarySession.Callback, KoinComponent {
private val applicationContext: Context by inject()
private val activeServerProvider: ActiveServerProvider by inject()
private val serviceJob = SupervisorJob()
@ -115,6 +119,7 @@ class AutoMediaBrowserCallback : MediaLibraryService.MediaLibrarySession.Callbac
private val placeholderButton = getPlaceholderButton()
private var heartIsCurrentlyOn = false
private var customRepeatModeSet = false
// This button is used for an unstarred track, and its action will star the track
private val heartButtonToggleOn =
@ -230,7 +235,7 @@ class AutoMediaBrowserCallback : MediaLibraryService.MediaLibrarySession.Callbac
commandButton.sessionCommand?.let { availableSessionCommands.add(it) }
}
session.player.repeatMode = Player.REPEAT_MODE_ALL
configureRepeatMode(session.player)
return MediaSession.ConnectionResult.accept(
availableSessionCommands.build(),
@ -238,6 +243,42 @@ class AutoMediaBrowserCallback : MediaLibraryService.MediaLibrarySession.Callbac
)
}
private fun configureRepeatMode(player: Player) {
if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.M) {
Timber.d("Car app library available, observing CarConnection")
val originalRepeatMode = player.repeatMode
var lastCarConnectionType = -1
CarConnection(applicationContext).type.observeForever {
if (lastCarConnectionType == it)
return@observeForever
lastCarConnectionType = it
Timber.d("CarConnection type changed to %s", it)
when (it) {
CarConnection.CONNECTION_TYPE_PROJECTION ->
if (!customRepeatModeSet) {
Timber.d("[CarConnection] Setting repeat mode to ALL")
player.repeatMode = Player.REPEAT_MODE_ALL
customRepeatModeSet = true
}
CarConnection.CONNECTION_TYPE_NOT_CONNECTED ->
if (customRepeatModeSet) {
Timber.d("[CarConnection] Resetting repeat mode")
player.repeatMode = originalRepeatMode
customRepeatModeSet = false
}
}
}
} else
Timber.d("Car app library not available")
}
override fun onPostConnect(session: MediaSession, controller: MediaSession.ControllerInfo) {
if (controller.controllerVersion != 0) {
// Let Media3 controller (for instance the MediaNotificationProvider)
@ -369,6 +410,7 @@ class AutoMediaBrowserCallback : MediaLibraryService.MediaLibrarySession.Callbac
PlaybackService.CUSTOM_COMMAND_REPEAT_MODE -> {
customCommandFuture = Futures.immediateFuture(SessionResult(RESULT_SUCCESS))
customRepeatModeSet = true
session.player.setNextRepeatMode()
session.updateCustomCommands()