fix float conversion, rounding for icons

This commit is contained in:
Alex 2023-02-06 20:41:35 -06:00
parent 405cd21c32
commit b2b647e2f7
3 changed files with 18 additions and 17 deletions

View file

@ -70,7 +70,8 @@ class MediaManager(private var apiHandler: ApiHandler, var ctx: Context) {
if (userSettingsPref != null) {
try {
val userSettings = JSObject(userSettingsPref)
userSettings.put("playbackRate", newRate.toDouble()) // TODO: Handle storing exact values? This actually comes out to something like 2.00000004432
// toString().toDouble() to prevent float conversion issues (ex 1.2f becomes 1.2000000476837158d)
userSettings.put("playbackRate", newRate.toString().toDouble())
sharedPrefEditor.putString("userSettings", userSettings.toString())
sharedPrefEditor.apply()
userSettingsPlaybackRate = newRate
@ -82,7 +83,7 @@ class MediaManager(private var apiHandler: ApiHandler, var ctx: Context) {
// Not sure if this is the best place for this, but if a user has not changed any user settings in the app
// the object will not exist yet, could be moved to a centralized place or created on first app load
val userSettings = JSONObject()
userSettings.put("playbackRate", newRate.toDouble())
userSettings.put("playbackRate", newRate.toString().toDouble())
sharedPrefEditor.putString("userSettings", userSettings.toString())
userSettingsPlaybackRate = newRate
Log.d(tag, "Created and saved userSettings JSON from Android Auto with playbackRate=$newRate")

View file

@ -90,15 +90,15 @@ class MediaSessionCallback(var playerNotificationService:PlayerNotificationServi
private fun onChangeSpeed() {
// cycle to next speed, only contains preset android app options, as each increment needs it's own icon
// Rounding values in the event a non preset value (.5, 1, 1.2, 1.5, 2, 3) is selected in the phone app
val mediaManager = playerNotificationService.mediaManager
val currentSpeed = mediaManager.getSavedPlaybackRate()
val newSpeed = when (currentSpeed) {
0.5f -> 1.0f
1.0f -> 1.2f
1.2f -> 1.5f
1.5f -> 2.0f
2.0f -> 3.0f
3.0f -> 0.5f
val newSpeed = when (mediaManager.getSavedPlaybackRate()) {
in 0.5f..0.7f -> 1.0f
in 0.8f..1.0f -> 1.2f
in 1.1f..1.2f -> 1.5f
in 1.3f..1.5f -> 2.0f
in 1.6f..2.0f -> 3.0f
in 2.1f..3.0f -> 0.5f
// anything set above 3 (can happen in the android app) will be reset to 1
else -> 1.0f
}

View file

@ -1189,14 +1189,14 @@ class PlayerNotificationService : MediaBrowserServiceCompat() {
override fun getCustomAction(player: Player): PlaybackStateCompat.CustomAction? {
val playbackRate = mediaManager.getSavedPlaybackRate()
// TODO: Handle custom playback rates like 0.7, 1.3, 1.4, etc
// Rounding values in the event a non preset value (.5, 1, 1.2, 1.5, 2, 3) is selected in the phone app
val drawable: Int = when (playbackRate) {
0.5f -> R.drawable.ic_play_speed_0_5x
1.0f -> R.drawable.ic_play_speed_1_0x
1.2f -> R.drawable.ic_play_speed_1_2x
1.5f -> R.drawable.ic_play_speed_1_5x
2.0f -> R.drawable.ic_play_speed_2_0x
3.0f -> R.drawable.ic_play_speed_3_0x
in 0.5f..0.7f -> R.drawable.ic_play_speed_0_5x
in 0.8f..1.0f -> R.drawable.ic_play_speed_1_0x
in 1.1f..1.2f -> R.drawable.ic_play_speed_1_2x
in 1.3f..1.5f -> R.drawable.ic_play_speed_1_5x
in 1.6f..2.0f -> R.drawable.ic_play_speed_2_0x
in 2.1f..3.0f -> R.drawable.ic_play_speed_3_0x
// anything set above 3 will be show the 3x to save from creating 100 icons
else -> R.drawable.ic_play_speed_3_0x
}