From 438392ddf66f385a7fa8ee87ecb54d6ac557cfbb Mon Sep 17 00:00:00 2001 From: Kr328 <39107975+Kr328@users.noreply.github.com> Date: Sun, 24 Nov 2019 14:10:48 +0800 Subject: [PATCH] use serializable for parcelable --- .../com/github/kr328/clash/core/BaseClash.kt | 1 + .../kr328/clash/core/serialization/Parcels.kt | 41 +++++++++++++++++++ .../github/kr328/clash/core/transact/Proxy.kt | 23 ++++++++++- .../kr328/clash/service/IClashObserver.aidl | 4 +- 4 files changed, 67 insertions(+), 2 deletions(-) create mode 100644 core/src/main/java/com/github/kr328/clash/core/serialization/Parcels.kt diff --git a/core/src/main/java/com/github/kr328/clash/core/BaseClash.kt b/core/src/main/java/com/github/kr328/clash/core/BaseClash.kt index bdf1712..f9b8f70 100644 --- a/core/src/main/java/com/github/kr328/clash/core/BaseClash.kt +++ b/core/src/main/java/com/github/kr328/clash/core/BaseClash.kt @@ -9,6 +9,7 @@ import java.io.DataOutputStream import java.io.File import java.io.IOException +@Suppress("SameParameterValue") abstract class BaseClash(private val controllerPath: File) { protected fun runControl( command: Int, diff --git a/core/src/main/java/com/github/kr328/clash/core/serialization/Parcels.kt b/core/src/main/java/com/github/kr328/clash/core/serialization/Parcels.kt new file mode 100644 index 0000000..daddd62 --- /dev/null +++ b/core/src/main/java/com/github/kr328/clash/core/serialization/Parcels.kt @@ -0,0 +1,41 @@ +package com.github.kr328.clash.core.serialization + +import android.os.Parcel +import kotlinx.serialization.* +import kotlinx.serialization.modules.EmptyModule + +object Parcels : AbstractSerialFormat(EmptyModule) { + fun dump(serializer: SerializationStrategy, obj: T, parcel: Parcel) { + serializer.serialize(ParcelsEncoder(parcel), obj) + } + + fun load(deserializer: DeserializationStrategy, parcel: Parcel): T { + return deserializer.deserialize(ParcelsDecoder(parcel)) + } + + private class ParcelsEncoder(private val parcel: Parcel) : + ElementValueEncoder() { + override fun encodeValue(value: Any) { + parcel.writeValue(value) + } + override fun encodeNull() { + parcel.writeValue(null) + } + } + + class ParcelsDecoder(private val parcel: Parcel) : ElementValueDecoder() { + private var nextObject: Any? = null + + override fun decodeNotNullMark(): Boolean { + nextObject = parcel.readValue(Parcels::class.java.classLoader) + + return nextObject != null + } + + override fun decodeNull(): Nothing? = null + + override fun decodeValue(): Any { + return nextObject ?: throw NullPointerException("Parcel read data null") + } + } +} \ No newline at end of file diff --git a/core/src/main/java/com/github/kr328/clash/core/transact/Proxy.kt b/core/src/main/java/com/github/kr328/clash/core/transact/Proxy.kt index 9e752f3..2b70caf 100644 --- a/core/src/main/java/com/github/kr328/clash/core/transact/Proxy.kt +++ b/core/src/main/java/com/github/kr328/clash/core/transact/Proxy.kt @@ -1,9 +1,30 @@ package com.github.kr328.clash.core.transact +import android.os.Parcel +import android.os.Parcelable +import com.github.kr328.clash.core.serialization.Parcels import kotlinx.serialization.Serializable @Serializable -data class Proxy(val type: String) { +data class Proxy(val type: String): Parcelable { @Serializable data class Packet(val proxies: Map) + + override fun writeToParcel(parcel: Parcel, flags: Int) { + Parcels.dump(serializer(), this, parcel) + } + + override fun describeContents(): Int { + return 0 + } + + companion object CREATOR : Parcelable.Creator { + override fun createFromParcel(parcel: Parcel): Proxy { + return Parcels.load(serializer(), parcel) + } + + override fun newArray(size: Int): Array { + return arrayOfNulls(size) + } + } } \ No newline at end of file diff --git a/service/src/main/aidl/com/github/kr328/clash/service/IClashObserver.aidl b/service/src/main/aidl/com/github/kr328/clash/service/IClashObserver.aidl index 298285a..4be6895 100644 --- a/service/src/main/aidl/com/github/kr328/clash/service/IClashObserver.aidl +++ b/service/src/main/aidl/com/github/kr328/clash/service/IClashObserver.aidl @@ -3,5 +3,7 @@ package com.github.kr328.clash.service; import com.github.kr328.clash.core.ClashProcessStatus; interface IClashObserver { - void onStatusChanged(in ClashProcessStatus status); + void onProxyChanged(); + void onNewLog(); + void onProcessStatusChanged(in ClashProcessStatus status); }