add url test

This commit is contained in:
Kr328 committed 2019-12-07 01:30:35 +08:00
1 parent a72c2d8abb
commit bda542d2e5
16 files changed
+270 -66

No files matched your search

@@ -1,6 +1,7 @@
package com.github.kr328.clash
import android.app.Application
import android.content.Context
import com.github.kr328.clash.core.utils.Log
import com.google.firebase.FirebaseApp
@@ -9,6 +10,14 @@ class MainApplication : Application() {
const val KEY_PROXY_MODE = "key_proxy_mode"
const val PROXY_MODE_VPN = "vpn"
const val PROXY_MODE_PROXY_ONLY = "proxy_only"
lateinit var instance: MainApplication
}
override fun attachBaseContext(base: Context?) {
super.attachBaseContext(base)
instance = this
}
override fun onCreate() {
@@ -2,7 +2,9 @@ package com.github.kr328.clash
import android.os.Bundle
import com.github.kr328.clash.adapter.ProxyAdapter
import com.github.kr328.clash.callback.IUrlTestCallback
import com.github.kr328.clash.core.model.ProxyPacket
import com.github.kr328.clash.core.utils.Log
import com.github.kr328.clash.model.ListProxy
import kotlinx.android.synthetic.main.activity_proxies.*
@@ -14,11 +16,7 @@ class ProxyActivity : BaseActivity() {
setSupportActionBar(activity_proxies_toolbar)
activity_proxies_list.also {
it.adapter = ProxyAdapter(this) { p, s ->
runClash { clash ->
clash.setSelectProxy(p, s)
}
}
it.adapter = ProxyAdapter(this, this::setProxySelected, this::urlTest)
it.layoutManager = (it.adapter!! as ProxyAdapter).getLayoutManager()
}
@@ -29,6 +27,51 @@ class ProxyActivity : BaseActivity() {
refreshList()
}
private fun setProxySelected(name: String, selected: String) {
runClash {
it.setSelectProxy(name, selected)
}
}
private fun urlTest(position: Int, size: Int) {
val adapter = (activity_proxies_list.adapter as ProxyAdapter)
runClash {
val proxies = adapter.elements.subList(position, position+size)
.filterIsInstance<ListProxy.ListProxyItem>()
.mapIndexed { index, proxy -> proxy.name to IndexedValue(index, proxy)}
.toMap()
for ((_, p) in proxies) {
Log.i("$p -> ${p.index}")
p.value.delay = -1
}
it.startUrlTest(proxies.keys.toTypedArray(), object: IUrlTestCallback.Stub() {
override fun onResult(proxy: String?, delay: Long) {
if ( proxy == null ) {
(adapter.elements[position] as ListProxy.ListProxyHeader).urlTest = false
runOnUiThread {
adapter.notifyItemRangeChanged(position, size)
}
return
}
val p = proxies[proxy] ?: return
p.value.delay = delay
runOnUiThread {
adapter.notifyItemChanged(position + p.index + 1)
}
}
})
}
}
private fun refreshList() {
if (!activity_proxies_swipe.isRefreshing)
activity_proxies_swipe.isRefreshing = true
@@ -13,7 +13,9 @@ import com.github.kr328.clash.core.model.ProxyPacket
import com.github.kr328.clash.model.ListProxy
import com.google.android.material.card.MaterialCardView
class ProxyAdapter(private val context: Context, val listener: (String, String) -> Unit) :
class ProxyAdapter(private val context: Context,
private val onSelect: (String, String) -> Unit,
private val onUrlTest: (Int, Int) -> Unit) :
RecyclerView.Adapter<RecyclerView.ViewHolder>() {
var elements: List<ListProxy> = emptyList()
var clickable: Boolean = false
@@ -108,7 +110,7 @@ class ProxyAdapter(private val context: Context, val listener: (String, String)
notifyItemChanged(old)
notifyItemChanged(position)
listener(element.header.name, element.name)
onSelect(element.header.name, element.name)
}
} else {
holder.card.setOnClickListener(null)
@@ -119,20 +121,45 @@ class ProxyAdapter(private val context: Context, val listener: (String, String)
holder.name.text = current.name
holder.type.text = current.type
holder.delay.text =
if (current.delay > 0) {
current.delay.toString()
} else {
if (current.header.type != ProxyPacket.Type.SELECT)
"N/A"
else
""
when {
current.header.urlTest && current.delay < 0 -> "..."
current.delay < 0 -> "N/A"
current.delay > 0 -> {
current.delay.toString()
}
else -> {
if (current.header.type != ProxyPacket.Type.SELECT)
"N/A"
else
""
}
}
}
private fun bindHeaderView(holder: HeaderHolder, current: ListProxy.ListProxyHeader) {
holder.name.text = current.name
holder.test.visibility =
if (current.type == ProxyPacket.Type.SELECT) View.VISIBLE else View.GONE
holder.test.setOnClickListener {
if ( current.urlTest )
return@setOnClickListener
val indexed = elements.withIndex()
.filter { it.value is ListProxy.ListProxyHeader }
.filterIsInstance<IndexedValue<ListProxy.ListProxyHeader>>()
val headerIndex = indexed.indexOfFirst {
it.value === current
}.takeIf { it >= 0 } ?: return@setOnClickListener
val header = indexed[headerIndex]
indexed[headerIndex].value.urlTest = true
val position = header.index
val size = (indexed.getOrNull(headerIndex + 1)?.index ?: elements.size) - header.index
notifyItemRangeChanged(position, size)
onUrlTest(position, size)
}
}
}
@@ -3,13 +3,13 @@ package com.github.kr328.clash.model
import com.github.kr328.clash.core.model.ProxyPacket
interface ListProxy {
data class ListProxyHeader(val name: String, val type: ProxyPacket.Type, var now: Int) :
data class ListProxyHeader(val name: String, val type: ProxyPacket.Type, var now: Int, var urlTest: Boolean = false) :
ListProxy
data class ListProxyItem(
val name: String,
val type: String,
val delay: Long,
var delay: Long,
val header: ListProxyHeader
) : ListProxy
}
@@ -0,0 +1,13 @@
package com.github.kr328.clash.utils
import android.util.TypedValue
import com.github.kr328.clash.MainApplication
val Int.dp: Float
get() {
return TypedValue.applyDimension(
TypedValue.COMPLEX_UNIT_DIP,
this.toFloat(),
MainApplication.instance.resources.displayMetrics
)
}