From 89684693106988db2923f1f5b64f8b7c8e8458e9 Mon Sep 17 00:00:00 2001 From: Kr328 <39107975+Kr328@users.noreply.github.com> Date: Wed, 27 Nov 2019 12:04:39 +0800 Subject: [PATCH] refactor Parcels --- app/build.gradle | 3 +- app/proguard-rules.pro | 44 +++---- .../com/github/kr328/clash/MainActivity.kt | 4 + .../com/github/kr328/clash/ProxyActivity.kt | 10 ++ .../kr328/clash/adapter/ProxyGroupAdapter.kt | 34 +++++ .../github/kr328/clash/core/model/Proxy.kt | 20 ++- .../kr328/clash/core/serialization/Parcels.kt | 117 ++++++++++++++++-- 7 files changed, 198 insertions(+), 34 deletions(-) create mode 100644 app/src/main/java/com/github/kr328/clash/adapter/ProxyGroupAdapter.kt diff --git a/app/build.gradle b/app/build.gradle index 786cc23..756a607 100644 --- a/app/build.gradle +++ b/app/build.gradle @@ -3,7 +3,6 @@ apply plugin: 'kotlinx-serialization' apply plugin: 'kotlin-android' apply plugin: 'kotlin-android-extensions' apply plugin: 'kotlin-kapt' -apply plugin: 'io.fabric' android { compileSdkVersion 29 @@ -17,7 +16,7 @@ android { } buildTypes { release { - minifyEnabled false + minifyEnabled true proguardFiles getDefaultProguardFile('proguard-android-optimize.txt'), 'proguard-rules.pro' } } diff --git a/app/proguard-rules.pro b/app/proguard-rules.pro index 6e7ffa9..f5fb65b 100644 --- a/app/proguard-rules.pro +++ b/app/proguard-rules.pro @@ -1,21 +1,23 @@ -# Add project specific ProGuard rules here. -# You can control the set of applied configuration files using the -# proguardFiles setting in build.gradle. -# -# For more details, see -# http://developer.android.com/guide/developing/tools/proguard.html - -# If your project uses WebView with JS, uncomment the following -# and specify the fully qualified class name to the JavaScript interface -# class: -#-keepclassmembers class fqcn.of.javascript.interface.for.webview { -# public *; -#} - -# Uncomment this to preserve the line number information for -# debugging stack traces. -#-keepattributes SourceFile,LineNumberTable - -# If you keep the line number information, uncomment this to -# hide the original source file name. -#-renamesourcefileattribute SourceFile +# Add project specific ProGuard rules here. +# You can control the set of applied configuration files using the +# proguardFiles setting in build.gradle. +# +# For more details, see +# http://developer.android.com/guide/developing/tools/proguard.html + +# If your project uses WebView with JS, uncomment the following +# and specify the fully qualified class name to the JavaScript interface +# class: +#-keepclassmembers class fqcn.of.javascript.interface.for.webview { +# public *; +#} + +# Uncomment this to preserve the line number information for +# debugging stack traces. +#-keepattributes SourceFile,LineNumberTable + +# If you keep the line number information, uncomment this to +# hide the original source file name. +#-renamesourcefileattribute SourceFile + +-dontobfuscate \ No newline at end of file diff --git a/app/src/main/java/com/github/kr328/clash/MainActivity.kt b/app/src/main/java/com/github/kr328/clash/MainActivity.kt index 6f4fe4c..a35d968 100644 --- a/app/src/main/java/com/github/kr328/clash/MainActivity.kt +++ b/app/src/main/java/com/github/kr328/clash/MainActivity.kt @@ -32,6 +32,10 @@ class MainActivity : BaseActivity() { setLogo(R.mipmap.ic_launcher_foreground) }) + activity_main_clash_proxies.setOnClickListener { + startActivity(Intent(this, ProxyActivity::class.java)) + } + activity_main_clash_profiles.setOnClickListener { startActivity(Intent(this, ProfilesActivity::class.java)) } diff --git a/app/src/main/java/com/github/kr328/clash/ProxyActivity.kt b/app/src/main/java/com/github/kr328/clash/ProxyActivity.kt index 8b146dc..0690f76 100644 --- a/app/src/main/java/com/github/kr328/clash/ProxyActivity.kt +++ b/app/src/main/java/com/github/kr328/clash/ProxyActivity.kt @@ -3,6 +3,8 @@ package com.github.kr328.clash import android.os.Bundle import androidx.recyclerview.widget.LinearLayoutManager import androidx.recyclerview.widget.RecyclerView +import com.github.kr328.clash.adapter.ProxyGroupAdapter +import com.github.kr328.clash.core.model.Proxy class ProxyActivity : BaseActivity() { override fun onCreate(savedInstanceState: Bundle?) { @@ -13,7 +15,15 @@ class ProxyActivity : BaseActivity() { setContentView(content) content.layoutManager = LinearLayoutManager(this) + content.adapter = ProxyGroupAdapter(this) + runClash { clash -> + val proxies = clash.queryAllProxies().proxies + runOnUiThread { + (content.adapter!! as ProxyGroupAdapter).data = proxies.toList() + .sortedWith(compareBy({ it.second.type == Proxy.TYPE_SELECT }, { it.first })) + } + } } } \ No newline at end of file diff --git a/app/src/main/java/com/github/kr328/clash/adapter/ProxyGroupAdapter.kt b/app/src/main/java/com/github/kr328/clash/adapter/ProxyGroupAdapter.kt new file mode 100644 index 0000000..b45dd26 --- /dev/null +++ b/app/src/main/java/com/github/kr328/clash/adapter/ProxyGroupAdapter.kt @@ -0,0 +1,34 @@ +package com.github.kr328.clash.adapter + +import android.content.Context +import android.view.LayoutInflater +import android.view.View +import android.view.ViewGroup +import android.widget.TextView +import androidx.recyclerview.widget.RecyclerView +import com.github.kr328.clash.R +import com.github.kr328.clash.core.model.Proxy + +class ProxyGroupAdapter(private val context: Context) : RecyclerView.Adapter() { + var data: List> = emptyList() + + class Holder(view: View) : RecyclerView.ViewHolder(view) { + val name = view.findViewById(R.id.adapter_proxy_group_name) + val list = view.findViewById(R.id.adapter_proxy_group_list) + } + + override fun onCreateViewHolder(parent: ViewGroup, viewType: Int): Holder { + return Holder(LayoutInflater.from(context) + .inflate(R.layout.adapter_proxy_group, parent, false)) + } + + override fun getItemCount(): Int { + return data.size + } + + override fun onBindViewHolder(holder: Holder, position: Int) { + val current = data[position] + + holder.name.text = current.first + } +} \ No newline at end of file diff --git a/core/src/main/java/com/github/kr328/clash/core/model/Proxy.kt b/core/src/main/java/com/github/kr328/clash/core/model/Proxy.kt index 6e704a7..1d6301b 100644 --- a/core/src/main/java/com/github/kr328/clash/core/model/Proxy.kt +++ b/core/src/main/java/com/github/kr328/clash/core/model/Proxy.kt @@ -9,4 +9,22 @@ import kotlinx.serialization.Serializable data class Proxy(val type: String, val all: List = emptyList(), val now: String = "", - val history: List = emptyList()) \ No newline at end of file + val history: List = emptyList()) { + @Serializable + data class History(val delay: Long) + + companion object { + const val TYPE_SELECT = "Selector" + const val TYPE_URL_TEST = "URLTest" + const val TYPE_DIRECT = "Direct" + const val TYPE_FALLBACK = "Fallback" + const val TYPE_REJECT = "Reject" + const val TYPE_SHADOWSOCKS = "Shadowsocks" + const val TYPE_SNELL = "Snell" + const val TYPE_SOCKS5 = "Socks5" + const val TYPE_HTTP = "Http" + const val TYPE_VMESS = "Vmess" + const val TYPE_LOAD_BALANCE = "LoadBalance" + const val TYPE_UNKNOWN = "Unknown" + } +} \ No newline at end of file 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 index 45675fd..7f71655 100644 --- 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 @@ -4,6 +4,9 @@ import android.os.Parcel import com.github.kr328.clash.core.utils.Log import kotlinx.serialization.* import kotlinx.serialization.modules.EmptyModule +import kotlinx.serialization.modules.SerialModule +import java.lang.IllegalArgumentException +import java.lang.NullPointerException object Parcels : AbstractSerialFormat(EmptyModule) { fun dump(serializer: SerializationStrategy, obj: T, parcel: Parcel) { @@ -25,22 +28,116 @@ object Parcels : AbstractSerialFormat(EmptyModule) { } } - class ParcelsDecoder(private val parcel: Parcel) : ElementValueDecoder() { - private var nextObject: Any? = null + class ParcelsDecoder(private val parcel: Parcel) : Decoder, CompositeDecoder { + override val context: SerialModule + get() = EmptyModule + override val updateMode: UpdateMode + get() = UpdateMode.BANNED - override fun decodeNotNullMark(): Boolean { - nextObject = parcel.readValue(Parcels::class.java.classLoader) + override fun decodeElementIndex(desc: SerialDescriptor) = + CompositeDecoder.READ_ALL - Log.i("-> $nextObject") + override fun decodeBooleanElement(desc: SerialDescriptor, index: Int) = + decodeBoolean() + override fun decodeByteElement(desc: SerialDescriptor, index: Int) = + decodeByte() + override fun decodeCharElement(desc: SerialDescriptor, index: Int) = + decodeChar() + override fun decodeDoubleElement(desc: SerialDescriptor, index: Int) = + decodeDouble() + override fun decodeFloatElement(desc: SerialDescriptor, index: Int) = + decodeFloat() + override fun decodeIntElement(desc: SerialDescriptor, index: Int) = + decodeInt() + override fun decodeShortElement(desc: SerialDescriptor, index: Int) = + decodeShort() + override fun decodeLongElement(desc: SerialDescriptor, index: Int) = + decodeLong() + override fun decodeStringElement(desc: SerialDescriptor, index: Int) = + decodeString() + override fun decodeUnitElement(desc: SerialDescriptor, index: Int) = + decodeUnit() - return nextObject != null + override fun decodeNullableSerializableElement( + desc: SerialDescriptor, + index: Int, + deserializer: DeserializationStrategy + ) = decodeNullableSerializableValue(deserializer) + override fun decodeSerializableElement( + desc: SerialDescriptor, + index: Int, + deserializer: DeserializationStrategy + ) = decodeSerializableValue(deserializer) + + override fun updateNullableSerializableElement( + desc: SerialDescriptor, + index: Int, + deserializer: DeserializationStrategy, + old: T? + ) = updateNullableSerializableValue(deserializer, old) + + override fun updateSerializableElement( + desc: SerialDescriptor, + index: Int, + deserializer: DeserializationStrategy, + old: T + ) = updateSerializableValue(deserializer, old) + + override fun beginStructure( + desc: SerialDescriptor, + vararg typeParams: KSerializer<*> + ): CompositeDecoder = this + + override fun decodeBoolean(): Boolean { + TODO("not implemented") //To change body of created functions use File | Settings | File Templates. } - override fun decodeNull(): Nothing? = null + override fun decodeByte(): Byte { + TODO("not implemented") //To change body of created functions use File | Settings | File Templates. + } - override fun decodeValue(): Any { - return nextObject ?: parcel.readValue(Parcels::class.java.classLoader) - ?: throw NullPointerException("Parcel read data null") + override fun decodeChar(): Char { + TODO("not implemented") //To change body of created functions use File | Settings | File Templates. + } + + override fun decodeDouble(): Double { + TODO("not implemented") //To change body of created functions use File | Settings | File Templates. + } + + override fun decodeEnum(enumDescription: SerialDescriptor): Int { + TODO("not implemented") //To change body of created functions use File | Settings | File Templates. + } + + override fun decodeFloat(): Float { + TODO("not implemented") //To change body of created functions use File | Settings | File Templates. + } + + override fun decodeInt(): Int { + TODO("not implemented") //To change body of created functions use File | Settings | File Templates. + } + + override fun decodeLong(): Long { + TODO("not implemented") //To change body of created functions use File | Settings | File Templates. + } + + override fun decodeNotNullMark(): Boolean { + TODO("not implemented") //To change body of created functions use File | Settings | File Templates. + } + + override fun decodeNull(): Nothing? { + TODO("not implemented") //To change body of created functions use File | Settings | File Templates. + } + + override fun decodeShort(): Short { + TODO("not implemented") //To change body of created functions use File | Settings | File Templates. + } + + override fun decodeString(): String { + TODO("not implemented") //To change body of created functions use File | Settings | File Templates. + } + + override fun decodeUnit() { + TODO("not implemented") //To change body of created functions use File | Settings | File Templates. } } }