refactor Parcels

This commit is contained in:
Kr328 committed 2019-11-27 12:04:39 +08:00
1 parent d7d36f32a3
commit 8968469310
7 files changed
+198 -34

No files matched your search

+1 -2
View File
@@ -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'
}
}
+23 -21
View File
@@ -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
@@ -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))
}
@@ -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 }))
}
}
}
}
@@ -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<ProxyGroupAdapter.Holder>() {
var data: List<Pair<String, Proxy>> = emptyList()
class Holder(view: View) : RecyclerView.ViewHolder(view) {
val name = view.findViewById<TextView>(R.id.adapter_proxy_group_name)
val list = view.findViewById<RecyclerView>(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
}
}
@@ -9,4 +9,22 @@ import kotlinx.serialization.Serializable
data class Proxy(val type: String,
val all: List<String> = emptyList(),
val now: String = "",
val history: List<String> = emptyList())
val history: List<History> = 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"
}
}
@@ -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 <T> dump(serializer: SerializationStrategy<T>, 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 <T : Any> decodeNullableSerializableElement(
desc: SerialDescriptor,
index: Int,
deserializer: DeserializationStrategy<T?>
) = decodeNullableSerializableValue(deserializer)
override fun <T> decodeSerializableElement(
desc: SerialDescriptor,
index: Int,
deserializer: DeserializationStrategy<T>
) = decodeSerializableValue(deserializer)
override fun <T : Any> updateNullableSerializableElement(
desc: SerialDescriptor,
index: Int,
deserializer: DeserializationStrategy<T?>,
old: T?
) = updateNullableSerializableValue(deserializer, old)
override fun <T> updateSerializableElement(
desc: SerialDescriptor,
index: Int,
deserializer: DeserializationStrategy<T>,
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.
}
}
}