ui: never access CREATOR directly

All of the parcelers have their own type prefix. So we have to actually
use the legit methods. This is a bit annoying, as there's no fully
compatible way across all API versions, so we have to branch.

Signed-off-by: Jason A. Donenfeld <Jason@zx2c4.com>
This commit is contained in:
Jason A. Donenfeld 2023-04-02 15:18:26 +02:00
parent 3e9ddd8720
commit 46bf98d7f6

View File

@ -4,22 +4,27 @@
*/ */
package com.wireguard.android.viewmodel package com.wireguard.android.viewmodel
import android.os.Build
import android.os.Parcel import android.os.Parcel
import android.os.Parcelable import android.os.Parcelable
import androidx.core.os.ParcelCompat
import androidx.databinding.ObservableArrayList import androidx.databinding.ObservableArrayList
import androidx.databinding.ObservableList import androidx.databinding.ObservableList
import com.wireguard.config.BadConfigException import com.wireguard.config.BadConfigException
import com.wireguard.config.Config import com.wireguard.config.Config
import com.wireguard.config.Peer import com.wireguard.config.Peer
import java.util.ArrayList
class ConfigProxy : Parcelable { class ConfigProxy : Parcelable {
val `interface`: InterfaceProxy val `interface`: InterfaceProxy
val peers: ObservableList<PeerProxy> = ObservableArrayList() val peers: ObservableList<PeerProxy> = ObservableArrayList()
private constructor(parcel: Parcel) { private constructor(parcel: Parcel) {
`interface` = InterfaceProxy.CREATOR.createFromParcel(parcel) `interface` = ParcelCompat.readParcelable(parcel, InterfaceProxy::class.java.classLoader, InterfaceProxy::class.java) ?: InterfaceProxy()
parcel.readTypedList(peers, PeerProxy.CREATOR) if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.Q) {
ParcelCompat.readParcelableList(parcel, peers, PeerProxy::class.java.classLoader, PeerProxy::class.java)
} else {
parcel.readTypedList(peers, PeerProxy.CREATOR)
}
peers.forEach { it.bind(this) } peers.forEach { it.bind(this) }
} }
@ -57,7 +62,11 @@ class ConfigProxy : Parcelable {
override fun writeToParcel(dest: Parcel, flags: Int) { override fun writeToParcel(dest: Parcel, flags: Int) {
dest.writeParcelable(`interface`, flags) dest.writeParcelable(`interface`, flags)
dest.writeTypedList(peers) if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.Q) {
dest.writeParcelableList(peers, flags)
} else {
dest.writeTypedList(peers)
}
} }
private class ConfigProxyCreator : Parcelable.Creator<ConfigProxy> { private class ConfigProxyCreator : Parcelable.Creator<ConfigProxy> {