Skip to content

Commit

Permalink
Use serialization instead of parcelize to fix a bug on Android 11. Fixes
Browse files Browse the repository at this point in the history
  • Loading branch information
PaulWoitaschek committed Mar 6, 2023
1 parent 6151f79 commit 314570e
Show file tree
Hide file tree
Showing 2 changed files with 11 additions and 12 deletions.
6 changes: 2 additions & 4 deletions data/src/main/kotlin/voice/data/ChapterMark.kt
Original file line number Diff line number Diff line change
@@ -1,7 +1,5 @@
package voice.data

import android.os.Parcelable
import kotlinx.parcelize.Parcelize
import kotlinx.serialization.Serializable

@Serializable
Expand All @@ -14,12 +12,12 @@ data class MarkData(
}
}

@Parcelize
@Serializable
data class ChapterMark(
val name: String?,
val startMs: Long,
val endMs: Long,
) : Parcelable
)

val ChapterMark.durationMs: Long get() = (endMs - startMs).coerceAtLeast(0L)

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,6 @@ package voice.playback.session

import android.app.Application
import android.net.Uri
import android.os.Build
import android.os.Bundle
import androidx.core.net.toUri
import androidx.datastore.core.DataStore
Expand All @@ -14,6 +13,7 @@ import kotlinx.coroutines.runBlocking
import kotlinx.serialization.SerialName
import kotlinx.serialization.Serializable
import kotlinx.serialization.SerializationException
import kotlinx.serialization.builtins.ListSerializer
import kotlinx.serialization.json.Json
import voice.common.BookId
import voice.common.pref.CurrentBook
Expand Down Expand Up @@ -149,20 +149,21 @@ class MediaItemProvider
artist = content.author,
mediaType = MediaType.AudioBookChapter,
extras = Bundle().apply {
putParcelableArray(EXTRA_CHAPTER_MARKS, chapter.chapterMarks.toTypedArray())
putString(
EXTRA_CHAPTER_MARKS,
Json.encodeToString(ListSerializer(ChapterMark.serializer()), chapter.chapterMarks),
)
},
)

private fun File.toProvidedUri(): Uri = imageFileProvider.uri(this)
}

internal fun MediaItem.chapterMarks(): List<ChapterMark> {
return if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.TIRAMISU) {
mediaMetadata.extras!!.getParcelableArray(EXTRA_CHAPTER_MARKS, ChapterMark::class.java)!!
} else {
@Suppress("UNCHECKED_CAST", "DEPRECATION")
mediaMetadata.extras!!.getParcelableArray(EXTRA_CHAPTER_MARKS) as Array<ChapterMark>
}.toList()
return Json.decodeFromString(
deserializer = ListSerializer(ChapterMark.serializer()),
string = mediaMetadata.extras!!.getString(EXTRA_CHAPTER_MARKS)!!,
)
}

private const val EXTRA_CHAPTER_MARKS = "chapterMarks"
Expand Down

0 comments on commit 314570e

Please sign in to comment.