Skip to content

Commit

Permalink
Refactor: Make ParcelSlicedList use writeValue() instead of writeParc…
Browse files Browse the repository at this point in the history
…elable()
  • Loading branch information
zhanghai committed Oct 14, 2024
1 parent ef58be7 commit e58c9f2
Show file tree
Hide file tree
Showing 2 changed files with 11 additions and 16 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -7,12 +7,13 @@ package me.zhanghai.android.files.provider.remote

import android.os.Parcel
import android.os.Parcelable
import java.io.IOException
import java8.nio.file.DirectoryIteratorException
import java8.nio.file.DirectoryStream
import java8.nio.file.Path
import me.zhanghai.android.files.provider.common.PathListDirectoryStream
import me.zhanghai.android.files.util.ParcelSlicedList
import java.io.IOException
import me.zhanghai.android.files.util.readParcelable

class ParcelableDirectoryStream : Parcelable {
private val paths: List<Path>
Expand All @@ -38,8 +39,7 @@ class ParcelableDirectoryStream : Parcelable {

private constructor(source: Parcel) {
@Suppress("UNCHECKED_CAST")
paths = source.readParcelable<ParcelSlicedList<Parcelable>>(Path::class.java.classLoader)!!
.list as List<Path>
paths = source.readParcelable<ParcelSlicedList<Parcelable>>()!!.list as List<Path>
}

companion object {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -10,7 +10,7 @@ import android.os.Parcel
import android.os.Parcelable

// @see android.content.pm.ParceledListSlice
class ParcelSlicedList<T : Parcelable?> : Parcelable {
class ParcelSlicedList<out T> : Parcelable {
val list: List<T>

constructor(list: List<T>) {
Expand Down Expand Up @@ -38,24 +38,19 @@ class ParcelSlicedList<T : Parcelable?> : Parcelable {
private fun readSliceFromParcel(list: MutableList<T>, source: Parcel) {
val size = source.readInt()
repeat(size) {
@Suppress("UNCHECKED_CAST")
val element = source.readParcelable<T>(ParcelSlicedList::class.java.classLoader) as T
@Suppress("UNCHECKED_CAST") val element = source.readValue<T>() as T
list += element
}
}

override fun describeContents(): Int =
list.fold(0) { contentFlags, parcelable ->
contentFlags or (parcelable?.describeContents() ?: 0)
}
override fun describeContents(): Int = 0

override fun writeToParcel(dest: Parcel, flags: Int) {
val size = list.size
dest.writeInt(size)
val iterator = list.iterator()
writeSliceToParcel(iterator, dest, flags)
writeSliceToParcel(iterator, dest)
if (iterator.hasNext()) {
val writeFlags = flags
dest.writeStrongBinder(object : Binder() {
override fun onTransact(
code: Int,
Expand All @@ -66,7 +61,7 @@ class ParcelSlicedList<T : Parcelable?> : Parcelable {
when (code) {
FIRST_CALL_TRANSACTION -> {
if (reply != null) {
writeSliceToParcel(iterator, reply, writeFlags)
writeSliceToParcel(iterator, reply)
}
true
}
Expand All @@ -76,13 +71,13 @@ class ParcelSlicedList<T : Parcelable?> : Parcelable {
}
}

private fun writeSliceToParcel(iterator: Iterator<T>, dest: Parcel, flags: Int) {
private fun writeSliceToParcel(iterator: Iterator<T>, dest: Parcel) {
val startPosition = dest.dataPosition()
dest.writeInt(0)
var size = 0
while (iterator.hasNext() && dest.dataSize() < MAX_IPC_SIZE) {
val element = iterator.next()
dest.writeParcelable(element, flags)
dest.writeValue(element)
++size
}
val endPosition = dest.dataPosition()
Expand All @@ -93,7 +88,7 @@ class ParcelSlicedList<T : Parcelable?> : Parcelable {

companion object {
// @see IBinder.MAX_IPC_SIZE
const val MAX_IPC_SIZE = 64 * 1024
private const val MAX_IPC_SIZE = 64 * 1024

@JvmField
val CREATOR = object : Parcelable.Creator<ParcelSlicedList<Parcelable?>> {
Expand Down

0 comments on commit e58c9f2

Please sign in to comment.